From 613b6255abcb2feba8a99f847f17b16f5af50e48 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Sun, 15 Nov 2020 01:15:13 -0500 Subject: piem-am: Rephrase CODEREPO description Describing CODEREPO in terms of where git-am is called is a bit confusing because piem-am does other things here as well (e.g. reading the base and checking out a branch). And it won't necessarily be where git-am is called once worktree support is added. Give a more generic description. Message-Id: <20201115061518.22191-2-kyle@kyleam.com> --- piem.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/piem.el b/piem.el index 5ad2b94..8ee5b0b 100644 --- a/piem.el +++ b/piem.el @@ -650,8 +650,8 @@ INFO is a plist that with information to help choose a default branch name or starting point (see `piem-default-branch-function' for a list of possible properties). -If CODEREPO is given, switch to this directory before calling -`git am'." +CODEREPO, if given, indicates the code repository to operate +within. If not specified, the default directory is used." (interactive (pcase-let ((`(,mbox . ,format) (or (piem-am-ready-mbox) -- cgit v1.2.3 From 291878e359aa05bffe8ad971e309e31197d573a5 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Sun, 15 Nov 2020 01:15:14 -0500 Subject: piem-am: Store "empty string" branch check This will be needed in another spot. Message-Id: <20201115061518.22191-3-kyle@kyleam.com> --- piem.el | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/piem.el b/piem.el index 8ee5b0b..a42ab3d 100644 --- a/piem.el +++ b/piem.el @@ -669,9 +669,10 @@ within. If not specified, the default directory is used." (interactivep (eq (car-safe mbox) :interactive))) (when interactivep (setq mbox (cdr mbox))) - (let ((new-branch (read-string - "New branch (empty for detached): " - (funcall piem-default-branch-function info))) + (let ((new-branch + (let ((b (read-string "New branch (empty for detached): " + (funcall piem-default-branch-function info)))) + (and (not (string-empty-p b)) b))) (base (completing-read "Base commit: " (let ((cands (and piem-use-magit @@ -680,9 +681,7 @@ within. If not specified, the default directory is used." (base (plist-get info :base-commit))) (if base (cons base cands) cands))))) (apply #'piem-process-call nil piem-git-executable "checkout" - (append (if (string-empty-p new-branch) - (list "--detach") - (list "-b" new-branch)) + (append (if new-branch (list "-b" new-branch) (list "--detach")) (and (not (string-blank-p base)) (list base))))) (let ((args (cons (concat "--patch-format=" format) -- cgit v1.2.3 From 060055dcf7817aa75462994a5e2051e2ae8daefa Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Sun, 15 Nov 2020 01:15:15 -0500 Subject: am: Support creating a new worktree 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 non-nil. Suggested-by: zimoun Ref: https://yhetil.org/guix-patches/86361cys9h.fsf@tournier.info Message-Id: <20201115061518.22191-4-kyle@kyleam.com> --- piem.el | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/piem.el b/piem.el index a42ab3d..dbcd4d5 100644 --- a/piem.el +++ b/piem.el @@ -154,6 +154,10 @@ the following information about the patch series: 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 @@ in `piem-default-branch-function'." (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 to construct the default completion value." + (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)))))) + ;;;###autoload (defun piem-am (mbox &optional format info coderepo) "Feed an am-ready mbox to `git am'. @@ -665,8 +683,9 @@ within. If not specified, the default directory is used." (piem-extract-mbox-info mbox) (piem-inbox-coderepo-maybe-read)))) (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 @@ within. If not specified, the default directory is used." (magit-list-local-branch-names))) (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 @@ within. If not specified, the default directory is used." (if (bufferp mbox) (unwind-protect (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 () -- cgit v1.2.3 From 251f9a264b3d0b5a4f4a976bf18b505c769b7e04 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Sun, 15 Nov 2020 01:15:16 -0500 Subject: am: Add option to configure how worktree is read It seems likely that piem-am-read-worktree won't quite behave as some callers want. Let users specify a custom function. Message-Id: <20201115061518.22191-5-kyle@kyleam.com> --- piem.el | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/piem.el b/piem.el index dbcd4d5..81c5196 100644 --- a/piem.el +++ b/piem.el @@ -158,6 +158,14 @@ the following information about the patch series: "Whether to create a dedicated worktree for applying patches." :type 'boolean) +(defcustom piem-am-read-worktree-function #'piem-am-read-worktree + "Function that reads a to-be-created worktree from the user. +This function is called with two arguments, the directory of the +code repository that the worktree will be created from and the +name of the branch that will be created. The branch may be nil +if the caller requested a detached HEAD." + :type 'function) + (defcustom piem-maildir-directory nil "Inject public-inbox threads into this directory. If non-nil, this must be an existing Maildir directory." @@ -702,7 +710,8 @@ within. If not specified, the default directory is used." (when piem-am-create-worktree (setq am-directory (expand-file-name - (piem-am-read-worktree default-directory new-branch))) + (funcall piem-am-read-worktree-function + default-directory new-branch))) (when (file-exists-p am-directory) (user-error "Worktree directory already exists"))) (apply #'piem-process-call nil piem-git-executable -- cgit v1.2.3 From 688c8783810c4c2da44d9fe41ace36021391f289 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Sun, 15 Nov 2020 01:15:17 -0500 Subject: am: Allow flipping worktree creation with prefix argument 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. Message-Id: <20201115061518.22191-6-kyle@kyleam.com> --- 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 @@ list of arguments specified via ARGS." (append args (list mid)))) ;;;###autoload -(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 @@ am-ready mbox, feed the result to `git am'." (with-temp-buffer (insert-file-contents (or cover mbox-file)) (piem-extract-mbox-info)) - coderepo) + coderepo + toggle-worktree) (when clean-fn (funcall clean-fn))))) diff --git a/piem.el b/piem.el index 81c5196..784d0b3 100644 --- a/piem.el +++ b/piem.el @@ -663,7 +663,7 @@ non-nil, it is used to construct the default completion value." (replace-regexp-in-string "/" "-" branch)))))) ;;;###autoload -(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 @@ branch name or starting point (see `piem-default-branch-function' 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." (interactive (pcase-let ((`(,mbox . ,format) (or (piem-am-ready-mbox) @@ -689,10 +693,12 @@ within. If not specified, the default directory is used." (list (cons :interactive mbox) format (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 @@ within. If not specified, the default directory is used." (magit-list-local-branch-names))) (base (plist-get info :base-commit))) (if base (cons base cands) cands))))) - (when piem-am-create-worktree + (when use-worktree (setq am-directory (expand-file-name (funcall piem-am-read-worktree-function @@ -715,11 +721,11 @@ within. If not specified, the default directory is used." (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) -- cgit v1.2.3 From 5db8c3114b38aa403ea5171087da8ca3df2c1632 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Sun, 15 Nov 2020 01:15:18 -0500 Subject: manual: Document worktree-related options Message-Id: <20201115061518.22191-7-kyle@kyleam.com> --- piem.texi | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/piem.texi b/piem.texi index cf39fe8..852d054 100644 --- a/piem.texi +++ b/piem.texi @@ -251,6 +251,17 @@ branch. If the sender specified a base commit for the series, that will be provided as the default completion candidate. Entering an empty base signals to use the current branch of the repository as the base. +@vindex piem-am-create-worktree +@vindex piem-am-read-worktree-function +Rather than applying the patches directly to the associated code +repository, you can create a dedicated worktree by setting +@code{piem-am-create-worktree} to a non-nil value. Giving a prefix +argument to @code{piem-am} inverts the meaning of +@code{piem-am-create-worktree}; that is, by default a prefix argument is +useful if you generally prefer to work within the configured code +repository but would like to trigger the one-off creation of a worktree +for a particular call. + @cindex magit @vindex piem-use-magit When piem loads, it detects whether Magit is loaded and sets @@ -314,13 +325,17 @@ for more information on using Transient.) @itemx M-x piem-b4-am-from-mid @findex piem-b4-am-from-mid @findex piem-mid +@vindex piem-am-create-worktree +@vindex piem-am-read-worktree-function Generate or download a thread's mbox for the current buffer's message ID, process it into an am-ready mbox with b4, and then feed it to @code{git am} called within an associated Git repository. If a message ID of the current buffer is not known (i.e. @code{piem-mid} returns nil), one is read from the caller. The caller is also queried for the branch name and base, as described for @code{piem-am} (@pxref{Applying -patches contained in a message}). +patches contained in a message}). And, as with @code{piem-am}, a +worktree can be created by configuring @code{piem-am-create-worktree} to +a non-nil value or by giving a prefix argument. @findex piem-mid-to-thread-functions To generate the input thread, first any functions in -- cgit v1.2.3