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 3/6] am: Support creating a new worktree
Date: Sun, 15 Nov 2020 01:15:15 -0500	[thread overview]
Message-ID: <20201115061518.22191-4-kyle@kyleam.com> (raw)
In-Reply-To: <20201115061518.22191-1-kyle@kyleam.com>

On the guix-patches list, simon described a workflow in which a new
worktree is used to apply patches.  Such a workflow is fairly
straightforward to support in piem-am (and thus piem-b4-am-from-mid).
Aside form reading the worktree from the caller, the main change
needed is to replace 'git checkout (-b BRANCH|--detatched) [base]'
with 'git worktree add (-b BRANCH|--detatched) PATH [base]'.

Teach piem-am to use a worktree when piem-am-create-worktree is

Suggested-by: zimoun <zimon.toutoune@gmail.com>
Ref: https://yhetil.org/guix-patches/86361cys9h.fsf@tournier.info
 piem.el | 45 +++++++++++++++++++++++++++++++++++++--------
 1 file changed, 37 insertions(+), 8 deletions(-)

diff --git a/piem.el b/piem.el
index a42ab3d..da7f9f8 100644
--- a/piem.el
+++ b/piem.el
@@ -154,6 +154,10 @@ (defcustom piem-default-branch-function
       The reported base commit of the patch, if any."
   :type 'function)
+(defcustom piem-am-create-worktree nil
+  "Whether to create a dedicated worktree for applying patches."
+  :type 'boolean)
 (defcustom piem-maildir-directory nil
   "Inject public-inbox threads into this directory.
 If non-nil, this must be an existing Maildir directory."
@@ -636,6 +640,20 @@ (defun piem-name-branch-who-what-v (info)
 (defvar piem-am-args (list "--scissors" "--3way"))
+(defun piem-am-read-worktree (coderepo branch)
+  "Read a worktree to create for applying patches.
+This function is intended to be used as a value of
+`piem-am-read-worktree-function'.  The worktree directory is
+completed from the parent directory of CODEREPO.  If BRANCH is
+non-nil, it is used as for construct the default completion."
+  (let ((fname (directory-file-name coderepo)))
+    (read-directory-name
+     "Create worktree: "
+     (file-name-directory fname) nil nil
+     (and branch
+          (concat (file-name-nondirectory fname) "-"
+                  (replace-regexp-in-string "/" "-" branch))))))
 (defun piem-am (mbox &optional format info coderepo)
   "Feed an am-ready mbox to `git am'.
@@ -665,8 +683,9 @@ (defun piem-am (mbox &optional format info coderepo)
            (piem-extract-mbox-info mbox)
   (setq format (or format "mboxrd"))
-  (let ((default-directory (or coderepo default-directory))
-        (interactivep (eq (car-safe mbox) :interactive)))
+  (let* ((default-directory (or coderepo default-directory))
+         (am-directory default-directory)
+         (interactivep (eq (car-safe mbox) :interactive)))
     (when interactivep
       (setq mbox (cdr mbox)))
     (let ((new-branch
@@ -680,8 +699,18 @@ (defun piem-am (mbox &optional format info coderepo)
                        (base (plist-get info :base-commit)))
                    (if base (cons base cands) cands)))))
-      (apply #'piem-process-call nil piem-git-executable "checkout"
-             (append (if new-branch (list "-b" new-branch) (list "--detach"))
+      (when piem-am-create-worktree
+        (setq am-directory
+              (expand-file-name
+               (piem-am-read-worktree default-directory new-branch)))
+        (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
+                         (list "worktree" "add")
+                       (list "checkout"))
+                     (if new-branch (list "-b" new-branch) (list "--detach"))
+                     (and piem-am-create-worktree (list am-directory))
                      (and (not (string-blank-p base))
                           (list base)))))
     (let ((args (cons (concat "--patch-format=" format)
@@ -689,15 +718,15 @@ (defun piem-am (mbox &optional format info coderepo)
       (if (bufferp mbox)
               (apply #'piem-process-call-with-buffer-input
-                     nil mbox piem-git-executable "am" args)
+                     am-directory mbox piem-git-executable "am" args)
             (when interactivep
               (kill-buffer mbox)))
-        (apply #'piem-process-call nil piem-git-executable "am"
+        (apply #'piem-process-call am-directory piem-git-executable "am"
                (append args (list mbox)))))
     (if (and piem-use-magit
              (fboundp 'magit-status-setup-buffer))
-        (magit-status-setup-buffer)
-      (dired "."))))
+        (magit-status-setup-buffer am-directory)
+      (dired am-directory))))
 ;;;###autoload (autoload 'piem-dispatch "piem" nil t)
 (define-transient-command piem-dispatch ()

  parent reply	other threads:[~2020-11-15  6:15 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 ` Kyle Meyer [this message]
2020-11-15  6:15 ` [PATCH 4/6] am: Add option to configure how worktree is read Kyle Meyer
2020-11-15  6:15 ` [PATCH 5/6] am: Allow flipping worktree creation with prefix argument Kyle Meyer
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-4-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).