summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--piem-b4.el15
-rw-r--r--piem.el77
-rw-r--r--piem.texi17
3 files changed, 87 insertions, 22 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 5ad2b94..784d0b3 100644
--- a/piem.el
+++ b/piem.el
@@ -154,6 +154,18 @@ 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-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."
@@ -636,8 +648,22 @@ 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)
+(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
@@ -650,8 +676,12 @@ 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.
+
+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)
@@ -663,15 +693,19 @@ If CODEREPO is given, switch to this directory before calling
(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))
- (interactivep (eq (car-safe mbox) :interactive)))
+ (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)))
- (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
@@ -679,10 +713,19 @@ If CODEREPO is given, switch to this directory before calling
(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 (string-empty-p new-branch)
- (list "--detach")
- (list "-b" new-branch))
+ (when use-worktree
+ (setq am-directory
+ (expand-file-name
+ (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
+ (append (if use-worktree
+ (list "worktree" "add")
+ (list "checkout"))
+ (if new-branch (list "-b" new-branch) (list "--detach"))
+ (and use-worktree (list am-directory))
(and (not (string-blank-p base))
(list base)))))
(let ((args (cons (concat "--patch-format=" format)
@@ -690,15 +733,15 @@ If CODEREPO is given, switch to this directory before calling
(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 ()
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