discussion and development of piem
 help / color / mirror / code / Atom feed
From: Kyle Meyer <kyle@kyleam.com>
To: piem@inbox.kyleam.com
Cc: zimoun <zimon.toutoune@gmail.com>
Subject: [PATCH 5/6] am: Allow flipping worktree creation with prefix argument
Date: Sun, 15 Nov 2020 01:15:17 -0500	[thread overview]
Message-ID: <20201115061518.22191-6-kyle@kyleam.com> (raw)
In-Reply-To: <20201115061518.22191-1-kyle@kyleam.com>

I tend to use a few dedicated worktrees for projects and am not
interested in creating a worktree for each patch series I apply.
However, I can imagine wanting to create one every now and then.  Make
it possible by adding a prefix argument to piem-am and
piem-b4-am-from-mid that flips the meaning of piem-am-create-worktree.
 piem-b4.el | 15 +++++++++++----
 piem.el    | 18 ++++++++++++------
 2 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/piem-b4.el b/piem-b4.el
index 27ecf19..172236f 100644
--- a/piem-b4.el
+++ b/piem-b4.el
@@ -130,15 +130,21 @@ (defun piem-b4-am-ready-from-mid (mid &optional args)
          (append args (list mid))))
-(defun piem-b4-am-from-mid (mid &optional args)
+(defun piem-b4-am-from-mid (mid &optional args toggle-worktree)
   "Get the thread for MID, extract an am-ready mbox, and apply it.
 Try to get a thread for the Message-Id MID with
 `piem-mid-to-thread-functions', falling back to letting b4
 download it.  After calling `b4 am' with ARGS to prepare an
-am-ready mbox, feed the result to `git am'."
+am-ready mbox, feed the result to `git am'.
+When prefix argument TOGGLE-WORKTREE is non-nil, invert the
+meaning of `piem-am-create-worktree'.  With the default value,
+this triggers the creation of a new worktree."
   (interactive (list (or (piem-mid)
                          (read-string "Message ID: "))
-                     (transient-args 'piem-b4-am)))
+                     (transient-args 'piem-b4-am)
+                     current-prefix-arg))
   (when-let ((badopt (cl-some
                       (lambda (arg)
                         (and (string-match
@@ -158,7 +164,8 @@ (defun piem-b4-am-from-mid (mid &optional args)
                    (insert-file-contents (or cover mbox-file))
-                 coderepo)
+                 coderepo
+                 toggle-worktree)
       (when clean-fn
         (funcall clean-fn)))))
diff --git a/piem.el b/piem.el
index ac9812c..0cb1093 100644
--- a/piem.el
+++ b/piem.el
@@ -663,7 +663,7 @@ (defun piem-am-read-worktree (coderepo branch)
                   (replace-regexp-in-string "/" "-" branch))))))
-(defun piem-am (mbox &optional format info coderepo)
+(defun piem-am (mbox &optional format info coderepo toggle-worktree)
   "Feed an am-ready mbox to `git am'.
 MBOX is a buffer whose contents are an am-ready mbox (obtained
@@ -677,7 +677,11 @@ (defun piem-am (mbox &optional format info coderepo)
 for a list of possible properties).
 CODEREPO, if given, indicates the code repository to operate
-within.  If not specified, the default directory is used."
+within.  If not specified, the default directory is used.
+When prefix argument TOGGLE-WORKTREE is non-nil, invert the
+meaning of `piem-am-create-worktree'.  With the default value,
+this triggers the creation of a new worktree."
    (pcase-let ((`(,mbox . ,format)
                 (or (piem-am-ready-mbox)
@@ -689,10 +693,12 @@ (defun piem-am (mbox &optional format info coderepo)
      (list (cons :interactive mbox)
            (piem-extract-mbox-info mbox)
-           (piem-inbox-coderepo-maybe-read))))
+           (piem-inbox-coderepo-maybe-read)
+           current-prefix-arg)))
   (setq format (or format "mboxrd"))
   (let* ((default-directory (or coderepo default-directory))
          (am-directory default-directory)
+         (use-worktree (xor piem-am-create-worktree toggle-worktree))
          (interactivep (eq (car-safe mbox) :interactive)))
     (when interactivep
       (setq mbox (cdr mbox)))
@@ -707,7 +713,7 @@ (defun piem-am (mbox &optional format info coderepo)
                        (base (plist-get info :base-commit)))
                    (if base (cons base cands) cands)))))
-      (when piem-am-create-worktree
+      (when use-worktree
         (setq am-directory
                (funcall piem-am-read-worktree-function
@@ -715,11 +721,11 @@ (defun piem-am (mbox &optional format info coderepo)
         (when (file-exists-p am-directory)
           (user-error "Worktree directory already exists")))
       (apply #'piem-process-call nil piem-git-executable
-             (append (if piem-am-create-worktree
+             (append (if use-worktree
                          (list "worktree" "add")
                        (list "checkout"))
                      (if new-branch (list "-b" new-branch) (list "--detach"))
-                     (and piem-am-create-worktree (list am-directory))
+                     (and use-worktree (list am-directory))
                      (and (not (string-blank-p base))
                           (list base)))))
     (let ((args (cons (concat "--patch-format=" format)

  parent reply	other threads:[~2020-11-15  6:16 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-15  6:15 [PATCH 0/6] Support applying patches in a new worktree Kyle Meyer
2020-11-15  6:15 ` [PATCH 1/6] piem-am: Rephrase CODEREPO description Kyle Meyer
2020-11-15  6:15 ` [PATCH 2/6] piem-am: Store "empty string" branch check Kyle Meyer
2020-11-15  6:15 ` [PATCH 3/6] am: Support creating a new worktree Kyle Meyer
2020-11-15  6:15 ` [PATCH 4/6] am: Add option to configure how worktree is read Kyle Meyer
2020-11-15  6:15 ` Kyle Meyer [this message]
2020-11-15  6:15 ` [PATCH 6/6] manual: Document worktree-related options Kyle Meyer
2020-11-15 13:31 ` [PATCH 0/6] Support applying patches in a new worktree zimoun
2020-11-15 19:26   ` Kyle Meyer
2020-11-15 22:32     ` Kyle Meyer
2020-11-15 23:47       ` zimoun

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://git.kyleam.com/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201115061518.22191-6-kyle@kyleam.com \
    --to=kyle@kyleam.com \
    --cc=piem@inbox.kyleam.com \
    --cc=zimon.toutoune@gmail.com \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).