discussion and development of piem
 help / color / mirror / code / Atom feed
blob d29527d959ff0e7ff9ad9a5803132a3ce954fef4 9667 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
 
;;; piem-b4.el --- Emacs interface to the b4 tool  -*- lexical-binding: t; -*-

;; Copyright (C) 2020-2021  all contributors <piem@inbox.kyleam.com>

;; Author: Kyle Meyer <kyle@kyleam.com>
;; Keywords: vc, tools
;; Package-Requires: ((emacs "26.3") (transient "0.3.0"))

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; This library provides a Transient [1] interface to b4 [2].  It is
;; documented in the piem manual.
;;
;; [1] https://magit.vc/manual/transient/
;; [2] https://git.kernel.org/pub/scm/utils/b4/b4.git

;;; Code:

(require 'cl-lib)
(require 'mail-extr)
(require 'message)
(require 'piem)
(require 'transient)

\f
;;;; Options

(defgroup piem-b4 nil
  "Control the b4 tool from Emacs."
  :link '(info-link "(piem)Using b4 to apply patches")
  :group 'piem)

(defcustom piem-b4-b4-executable "b4"
  "Which b4 executable to use."
  :type 'string)

\f
;;;; Internals

(defvar piem-b4-keep-temp-directory nil
  "Don't clean up the directory created by `piem-b4--get-am-files'.
This is intended to be used for debugging purposes.")

(defun piem-b4--get-am-files (mid coderepo args)
  (let* ((outdir (file-name-as-directory
                  (make-temp-file "piem-b4-" t)))
         (root (concat outdir "m"))
         (mbox-thread (concat root "-piem"))
         (local-mbox-p nil)
         (clean-fn (and (not piem-b4-keep-temp-directory)
                        (lambda () (delete-directory outdir t)))))
    (when-let ((fn (run-hook-with-args-until-success
                    'piem-mid-to-thread-functions mid)))
      (with-temp-file mbox-thread
        (funcall fn)
        (unless (= (point-max) 1)
          (setq local-mbox-p t))))
    ;; `piem-mid-to-thread-functions' didn't generate an mbox.  Next
    ;; try to download it from an inbox's URL.  Finally, fall back to
    ;; b4's configuration.
    (unless local-mbox-p
      (when-let ((url (and (equal mid (piem-mid))
                           (piem-inbox-get :url))))
        (ignore-errors
          (piem-with-url-contents
              (concat url (piem-escape-mid mid) "/t.mbox.gz")
            (piem-gunzip-buffer)
            (write-region nil nil mbox-thread))
          (setq local-mbox-p t))))
    ;; Move to the coderepo so that we pick up any b4 configuration
    ;; from there.
    (condition-case err
        (apply #'piem-process-call coderepo piem-b4-b4-executable "am"
               (and local-mbox-p
                    (concat "--use-local-mbox=" mbox-thread))
               (concat "--outdir=" outdir)
               (concat "--mbox-name=m")
               (append args (list mid)))
      (piem-process-error
       (when clean-fn
         (funcall clean-fn))
       (signal (car err) (cdr err))))
    (let ((mbox-cover (concat root ".cover"))
          (mbox-am (concat root ".mbx")))
      (list (and (file-exists-p mbox-cover)
                 mbox-cover)
            (if (file-exists-p mbox-am)
                mbox-am
              (when clean-fn
                (funcall clean-fn))
              (error "Expected mbox file does not exist: %s" mbox-am))
            clean-fn))))

\f
;;;; Commands

;;;###autoload
(defun piem-b4-am-ready-from-mbox (mbox &optional args)
  "Extract an am-ready mbox from a thread of messages.
MBOX is the file name of an mbox that contains a patch series.
ARGS is a list of arguments to pass to `b4 am'."
  (interactive (list (read-file-name "mbox: ")
                     (transient-args 'piem-b4-am)))
  (apply #'piem-process-start nil piem-b4-b4-executable "am"
         (cons (concat "--use-local-mbox=" mbox) args)))

;;;###autoload
(defun piem-b4-am-ready-from-mid (mid &optional args)
  "Download the thread for MID and and extract an am-ready mbox.
MID is a message ID to pass directly to `b4 am', along with the
list of arguments specified via ARGS."
  (interactive (list (read-string "Message ID: " nil nil (piem-mid))
                     (transient-args 'piem-b4-am)))
  (apply #'piem-process-start nil piem-b4-b4-executable "am"
         (append args (list mid))))

;;;###autoload
(defun piem-b4-am-from-mid-create (mid &optional args toggle-worktree)
  "Get the thread for MID, extract an am-ready mbox, and apply it.

Try to generate a thread for the Message-Id MID with
`piem-mid-to-thread-functions'.  If that fails, try to download
the thread from an inbox URL associated with the current buffer,
provided that the current buffer's message ID matches MID.  And
if that doesn't work, let `b4 am' download the thread according
to its own configuration.

After calling `b4 am' with ARGS to prepare an 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)
                     current-prefix-arg))
  (when-let ((badopt (cl-some
                      (lambda (arg)
                        (and (string-match
                              (rx string-start
                                  (group (or "--outdir" "--mbox-name")) "=")
                              arg)
                             (match-string 1 arg)))
                      args)))
    (user-error "%s is incompatible with this command" badopt))
  (pcase-let* ((coderepo (piem-inbox-coderepo-maybe-read))
               (`(,cover ,mbox-file ,clean-fn)
                (piem-b4--get-am-files mid coderepo args))
               (default-directory coderepo))
    (unwind-protect
        (piem-am-create mbox-file
                        nil
                        (with-temp-buffer
                          (insert-file-contents (or cover mbox-file))
                          (piem-extract-mbox-info))
                        coderepo
                        toggle-worktree)
      (when clean-fn
        (funcall clean-fn)))))

(transient-define-argument piem-b4-am:--outdir ()
  :description "Output directory"
  :class 'transient-option
  :shortarg "-o"
  :argument "--outdir="
  :reader #'transient-read-existing-directory)

(transient-define-argument piem-b4-am:--mbox-name ()
  :description "Base file name for mbox"
  :class 'transient-option
  :shortarg "-n"
  :argument "--mbox-name="
  :reader #'read-string)

(transient-define-argument piem-b4-am:--use-version ()
  :description "Desired version of patch series"
  :class 'transient-option
  :shortarg "-v"
  :argument "--use-version="
  :reader #'transient-read-number-N+)

(transient-define-argument piem-b4-am:--cherry-pick ()
  :description "Select a subset of patches by number"
  :class 'transient-option
  :shortarg "-P"
  :argument "--cherry-pick="
  :reader #'read-string)

(transient-define-argument piem-b4-am:--use-project ()
  :description "Use a specific lore project instead of guessing"
  :class 'transient-option
  :shortarg "-p"
  :argument "--use-project="
  :level 7
  :reader #'read-string)

(transient-define-argument piem-b4-am:--guess-branch ()
  :description "Restrict base guess to this branch"
  :class 'transient-option
  :shortarg "-b"
  :argument "--guess-branch="
  ;; TODO: Optionally support `magit-read-branch'.
  :reader #'read-string)

(transient-define-argument piem-b4-am:--guess-lookback ()
  :description "How many days to go back when guessing base"
  :class 'transient-option
  :shortarg "-G"
  :argument "--guess-lookback="
  :reader #'transient-read-number-N+)

;;;###autoload (autoload 'piem-b4-am "piem-b4" nil t)
(transient-define-prefix piem-b4-am ()
  "Filter mbox to patches and feed to git-am"
  :man-page "b4"
  ["General options"
   ("-c" "Check newer versions" "--check-newer-revisions")
   ("-C" "Don't use local cache" "--no-cache")
   ;; Hide by default because it hard codes the URL.
   (7 "-l" "Add a lore.kernel.org/r/ to patches" "--add-link")
   ("-L" "Do not reroll partial series" "--no-partial-reroll")
   ("-s" "Add my signed-off-by" "--add-my-sob")
   ("-S" "Apply trailers without checking email addresses" "--sloppy-trailers")
   ("-t" "Apply cover letter trailers" "--apply-cover-trailers")
   ("-T" "Do not add trailers" "--no-add-trailers")
   (piem-b4-am:--use-version)
   (piem-b4-am:--cherry-pick)]
  ["Options for creating am-ready mboxes"
   ("-3" "Prepare for 3-way merge" "--prep-3way")
   ("-g" "Try to guess base" "--guess-base")
   (piem-b4-am:--guess-branch)
   (piem-b4-am:--guess-lookback)
   (piem-b4-am:--use-project)
   (piem-b4-am:--outdir)
   (piem-b4-am:--mbox-name)
   ("-M" "Save as maildir" "--save-as-maildir")
   (7 "-Q" "Save as quilt-read folder" "--quilt-ready")
   ;; Hide because this is unlikely to be useful outside of
   ;; command-line piping to `git am'.
   (5 "-V" "Do not save cover letter" "--no-cover")]
  ["Actions"
   [("a" "Message ID -> mbox -> git-am" piem-b4-am-from-mid-create)]
   [("b" "Local mbox -> am-ready mbox" piem-b4-am-ready-from-mbox)
    ("i" "Message ID -> am-ready mbox" piem-b4-am-ready-from-mid)]])

;;; piem-b4.el ends here
(provide 'piem-b4)

debug log:

solving d29527d9 ...
found d29527d9 in https://inbox.kyleam.com/piem/20211229235036.372313-8-kyle@kyleam.com/
found 18a68d95 in https://git.kyleam.com/piem/
preparing index
index prepared:
100644 18a68d95deb95588c68e2afab7080c9a1ea9daf1	piem-b4.el

applying [1/1] https://inbox.kyleam.com/piem/20211229235036.372313-8-kyle@kyleam.com/
diff --git a/piem-b4.el b/piem-b4.el
index 18a68d95..d29527d9 100644

Checking patch piem-b4.el...
Applied patch piem-b4.el cleanly.

index at:
100644 d29527d959ff0e7ff9ad9a5803132a3ce954fef4	piem-b4.el

Code repositories for project(s) associated with this public inbox

	https://git.kyleam.com/piem/

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).