unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Philip Kaludercic <philipk@posteo.net>
To: Antoine Kalmbach <ane@iki.fi>
Cc: 57400@debbugs.gnu.org
Subject: bug#57400: 29.0.50; Support sending patches from VC directly
Date: Mon, 03 Oct 2022 18:59:09 +0000	[thread overview]
Message-ID: <87y1twvima.fsf@posteo.net> (raw)
In-Reply-To: <lyo7w7s2ow.fsf@iki.fi> (Antoine Kalmbach's message of "Fri, 26 Aug 2022 13:45:51 +0300")

[-- Attachment #1: Type: text/plain, Size: 1060 bytes --]

Antoine Kalmbach <ane@iki.fi> writes:

> Philip Kaludercic <philipk@posteo.net> writes:
>
>> Yes, and I began implementing a different approach (as mentioned on the
>> emacs-devel thread), which I have since abandoned.  If you haven't
>> written anything yet, and don't insist on it, I could propose to start
>> sketching out your suggestions.
>
> Sure!  I was thinking we could start from a very basic command, call it
> `vc-prepare-patch` as per your suggestion. Since VC uses generics, we
> can dispatch to backend-specific implementations, something like this,
> with Git:
>
>   1. `M-x vc-prepare-patch`
>   2. Dispatch to `vc-git-prepare-patch`
>   3. Git wants a revision range, so interactively prompt for that
>      (e.g. `HEAD^`, `abcd1234..ghjk5678`, or `-1`)
>   4. `call-process` to `git format-patch $REV`, and so forth, get the
>      list of files.
>   5. Loop each file in `message-mode`. `C-c C-c` sends and goes to the
>      next patch, `C-c C-k` cancels the whole thing.

Sorry for the delay, here is a first approximation of this idea:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-a-command-to-send-patches.patch --]
[-- Type: text/x-patch, Size: 6852 bytes --]

From a350b7cbd1b61925c687b501c6251a8ef4fb5549 Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <philipk@posteo.net>
Date: Mon, 3 Oct 2022 20:54:38 +0200
Subject: [PATCH] Add a command to send patches

* lisp/vc/vc-git.el (vc-git-prepare-patch):  Add Git implementation.
* lisp/vc/vc.el (vc-read-revision):  Add a MULTIPLE argument.
 (vc-read-multiple-revisions):  Add an auxiliary function that always
 calls 'vc-read-revision' with a non-nil value for MULTIPLE.
(vc-compose-patches-inline):  Add user option.
(message-goto-body):  Declare function.
(message--name-table):  Declare function.
(vc-compose-patch):  Add command.  (bug#57400)
---
 lisp/vc/vc-git.el | 27 ++++++++++++++++++
 lisp/vc/vc.el     | 70 ++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 93 insertions(+), 4 deletions(-)

diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el
index f5ac43f536..439e82b12e 100644
--- a/lisp/vc/vc-git.el
+++ b/lisp/vc/vc-git.el
@@ -1705,6 +1705,33 @@ vc-git-extra-status-menu
 (defun vc-git-root (file)
   (vc-find-root file ".git"))
 
+(defun vc-git-prepare-patch (rev)
+  (with-temp-buffer
+    (call-process vc-git-program nil t nil "format-patch"
+                  "--no-numbered" "--stdout"
+                  ;; From gitrevisions(7): ^<n> means the <n>th parent
+                  ;; (i.e.  <rev>^ is equivalent to <rev>^1). As a
+                  ;; special rule, <rev>^0 means the commit itself and
+                  ;; is used when <rev> is the object name of a tag
+                  ;; object that refers to a commit object.
+                  (concat rev "^0"))
+    (let (filename subject body)
+      ;; Save the patch in a temporary file if required
+      (when (bound-and-true-p vc-compose-patches-inline)
+        (setq filename (make-temp-file "vc-git-prepare-patch"))
+        (write-region nil nil filename)) ;FIXME: Clean up
+      ;; Extract the subject line
+      (goto-char (point-min))
+      (search-forward-regexp "^Subject: \\(.+\\)")
+      (setq subject (match-string 1))
+      ;; Extract the patch body
+      (search-forward-regexp "\n\n")
+      (setq body (buffer-substring (point) (point-max)))
+      ;; Return the extracted data
+      (list :filename filename
+            :subject subject
+            :body body))))
+
 ;; grep-compute-defaults autoloads grep.
 (declare-function grep-read-regexp "grep" ())
 (declare-function grep-read-files "grep" (regexp))
diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el
index 24300e014a..e990f51a91 100644
--- a/lisp/vc/vc.el
+++ b/lisp/vc/vc.el
@@ -574,6 +574,14 @@
 ;;   containing FILE-OR-DIR.  The optional REMOTE-NAME specifies the
 ;;   remote (in Git parlance) whose URL is to be returned.  It has
 ;;   only a meaning for distributed VCS and is ignored otherwise.
+;;
+;; - prepare-patch (rev)
+;;
+;;   Prepare a patch and return a property list with the keys
+;;   `:subject' indicating the patch message as a string, `:body'
+;;   containing the contents of the patch as a string (excluding the
+;;   header) and `:filename' pointing to a file where the patch has
+;;   been stored.
 
 ;;; Changes from the pre-25.1 API:
 ;;
@@ -1910,7 +1918,7 @@ vc-diff-internal
 (defvar vc-revision-history nil
   "History for `vc-read-revision'.")
 
-(defun vc-read-revision (prompt &optional files backend default initial-input)
+(defun vc-read-revision (prompt &optional files backend default initial-input multiple)
   (cond
    ((null files)
     (let ((vc-fileset (vc-deduce-fileset t))) ;FIXME: why t?  --Stef
@@ -1920,9 +1928,16 @@ vc-read-revision
   (let ((completion-table
          (vc-call-backend backend 'revision-completion-table files)))
     (if completion-table
-        (completing-read prompt completion-table
-                         nil nil initial-input 'vc-revision-history default)
-      (read-string prompt initial-input nil default))))
+        (funcall
+         (if multiple #'completing-read-multiple #'completing-read)
+         prompt completion-table nil nil initial-input 'vc-revision-history default)
+      (let ((answer (read-string prompt initial-input nil default)))
+        (if multiple
+            (split-string answer "[ \t]*,[ \t]*")
+          answer)))))
+
+(defun vc-read-multiple-revisions (prompt &optional files backend default initial-input)
+  (vc-read-revision prompt files backend default initial-input t))
 
 (defun vc-diff-build-argument-list-internal (&optional fileset)
   "Build argument list for calling internal diff functions."
@@ -3243,6 +3258,53 @@ vc-update-change-log
   (vc-call-backend (vc-responsible-backend default-directory)
                    'update-changelog args))
 
+(defcustom vc-compose-patches-inline nil
+  "Non-nil means that `vc-compose-patch' creates a single message."
+  :type 'boolean
+  :safe #'booleanp)
+
+(declare-function message-goto-body "message" (&optional interactive))
+(declare-function message--name-table "message" (orig-string))
+
+;;;###autoload
+(defun vc-compose-patch (addressee subject revisions)
+  "Compose a message sending REVISIONS to ADDRESSEE with SUBJECT."
+  (interactive (save-current-buffer
+                 (require 'message)
+                 (vc-ensure-vc-buffer)
+                 (let ((revs (vc-read-multiple-revisions "Revisions: ")) to)
+                   (while (null (setq to (completing-read-multiple
+                                          "Whom to send: "
+                                          (message--name-table ""))))
+                     (message "At least one addressee required.")
+                     (sit-for 1))
+                   (list (string-join to ", ")
+                         (read-string "Subject: " "[PATCH] " nil nil t)
+                         revs))))
+  (require 'message)
+  (save-current-buffer
+    (vc-ensure-vc-buffer)
+    (let ((patches (mapcar (lambda (rev)
+                             (vc-call-backend
+                              (vc-responsible-backend default-directory)
+                              'prepare-patch rev))
+                           revisions)))
+      (if (not vc-compose-patches-inline)
+          (dolist (patch patches)
+            (compose-mail addressee (plist-get patch :subject)
+                          nil nil nil nil
+                          `((exit-recursive-edit)))
+            (message-goto-body)
+            (save-excursion             ;don't jump to the end
+              (insert (plist-get patch :body)))
+            (recursive-edit))
+        (compose-mail addressee subject)
+        (message-goto-body)
+        (dolist (patch patches)
+          (mml-attach-file (plist-get patch :filename) "text/x-patch"))
+        (message-goto-body)
+        (open-line 2)))))
+
 (defun vc-default-responsible-p (_backend _file)
   "Indicate whether BACKEND is responsible for FILE.
 The default is to return nil always."
-- 
2.37.3


[-- Attachment #3: Type: text/plain, Size: 967 bytes --]


The documentation is not complete yet, and I haven't tested it yet
extensively, but it fundamentally does the right thing.  I am not too
sure about the generic interface and the plist that `prepare-patch'
returns.  There might be a better way to do this, generating less
garbage, but I'm stuck between trying to be VC-generic and MUA-generic
which is unsurprisingly tricky.

> Once the file opens in message-mode, most likely we need to strip the
> magic From <sha> <date> header from the beginning of the mail. Then we ensure
> don't do any nasty whitespace removal or wrapping.
>
> Most likely, depending on the backend, we should not require any
> parameters besides the "set of changes". For instance, in Git you can
> configure `git-format-patch` in the git configuration for several
> attributes, like --to=, --annotate, --prefix, etc.
>
> I don't remember how Mercurial works with this. Probably similar. It
> should generate mbox entries as well, I think. 


  parent reply	other threads:[~2022-10-03 18:59 UTC|newest]

Thread overview: 117+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-25  8:47 bug#57400: 29.0.50; Support sending patches from VC directly Antoine Kalmbach
2022-08-26  7:37 ` Philip Kaludercic
2022-08-26 10:15   ` Antoine Kalmbach
2022-08-26 10:35     ` Philip Kaludercic
2022-08-26 10:45       ` Antoine Kalmbach
2022-08-26 10:58         ` Eli Zaretskii
2022-08-26 11:26           ` Philip Kaludercic
2022-08-26 11:44             ` Eli Zaretskii
2022-08-26 12:05               ` Philip Kaludercic
2022-08-26 12:26                 ` Eli Zaretskii
2022-08-26 13:10                   ` Antoine Kalmbach
2022-08-26 13:17                     ` Eli Zaretskii
2022-08-26 13:29                   ` Philip Kaludercic
2022-08-26 14:21                     ` Eli Zaretskii
2022-08-27  8:21                       ` Philip Kaludercic
2022-08-27  9:21                         ` Eli Zaretskii
2022-08-27  9:30                           ` Philip Kaludercic
2022-08-26 12:08               ` Antoine Kalmbach
2022-08-26 12:28                 ` Eli Zaretskii
2022-08-28  4:07         ` Richard Stallman
2022-10-03 18:59         ` Philip Kaludercic [this message]
2022-10-03 19:06           ` Lars Ingebrigtsen
2022-10-03 19:23             ` Eli Zaretskii
2022-10-04 19:19               ` Philip Kaludercic
2022-10-04 19:33                 ` Eli Zaretskii
2022-10-03 21:22             ` Philip Kaludercic
2022-10-03 21:55               ` Philip Kaludercic
2022-10-03 23:32                 ` Lars Ingebrigtsen
2022-10-04  6:46                   ` Eli Zaretskii
2022-10-04  6:41                 ` Eli Zaretskii
2022-10-04  7:10                   ` Philip Kaludercic
2022-10-04  8:00                     ` Eli Zaretskii
2022-10-04 10:40                       ` Philip Kaludercic
2022-10-04 10:42                         ` Philip Kaludercic
2022-10-04 12:25                           ` Eli Zaretskii
2022-10-04 12:24                         ` Eli Zaretskii
2022-10-04 18:08                           ` Philip Kaludercic
2022-10-05 16:07           ` Antoine Kalmbach
2022-10-05 17:34             ` Philip Kaludercic
2022-10-05 17:48               ` Philip Kaludercic
2022-10-05 18:32                 ` Lars Ingebrigtsen
2022-10-05 18:46                   ` Philip Kaludercic
2022-10-05 19:08                     ` Lars Ingebrigtsen
2022-10-06  8:21                       ` Robert Pluim
2022-10-06  8:35                         ` Philip Kaludercic
2022-10-06  8:59                           ` Robert Pluim
2022-10-06  9:12                             ` Philip Kaludercic
2022-10-06 11:02                     ` Philip Kaludercic
2022-10-05 19:57                   ` Juri Linkov
2022-10-06 12:03                     ` Lars Ingebrigtsen
2022-10-07 22:48                     ` Richard Stallman
2022-10-10 14:39                     ` Filipp Gunbin
2022-10-10 18:58                       ` Juri Linkov
2022-10-11  0:27                         ` Lars Ingebrigtsen
2022-10-11  0:26                       ` Lars Ingebrigtsen
2022-10-05 18:17               ` Eli Zaretskii
2022-10-05 18:45                 ` Philip Kaludercic
2022-10-06  9:14                   ` Philip Kaludercic
2022-10-06  9:19                     ` Eli Zaretskii
2022-10-06 22:22                       ` Dmitry Gutov
2022-10-07  6:36                         ` Eli Zaretskii
2022-10-07 12:06                           ` Dmitry Gutov
2022-10-06 11:33               ` Robert Pluim
2022-10-06 12:38                 ` Philip Kaludercic
2022-10-06 12:58                   ` Robert Pluim
2022-10-06 14:37                     ` Philip Kaludercic
2022-10-06 14:43                       ` Robert Pluim
2022-10-06 15:54                       ` Eli Zaretskii
2022-10-06 16:27                         ` Philip Kaludercic
2022-10-07  7:58                     ` Juri Linkov
2022-10-07 11:42                       ` Philip Kaludercic
2022-10-08 10:03                         ` Philip Kaludercic
2022-10-08 19:34                         ` Juri Linkov
2022-10-09 12:15                           ` Philip Kaludercic
2022-10-10 19:03                             ` Juri Linkov
2022-10-11 12:44                               ` Philip Kaludercic
2022-10-11 13:58                                 ` Robert Pluim
2022-10-15 18:54                                 ` Juri Linkov
2022-10-16  9:40                                   ` Philip Kaludercic
2022-10-11 19:30                               ` Philip Kaludercic
2022-10-11 19:47                                 ` Juri Linkov
2022-10-11 19:49                                   ` Philip Kaludercic
2022-10-12 22:01                                   ` Richard Stallman
2022-10-13  7:04                                     ` Juri Linkov
2022-10-13 21:12                                       ` Richard Stallman
2022-10-15 19:02                                         ` Juri Linkov
2022-10-13  8:55                                   ` Philip Kaludercic
2022-10-13 17:30                                     ` Juri Linkov
2022-10-13 19:44                                       ` Philip Kaludercic
2022-10-13 20:25                                         ` Philip Kaludercic
2022-10-13 20:33                                           ` Eli Zaretskii
2022-10-13 22:05                                             ` Philip Kaludercic
2022-10-14  6:50                                               ` Eli Zaretskii
2022-10-14 21:28                                                 ` Richard Stallman
2022-10-14 21:47                                                 ` Philip Kaludercic
2022-10-15  6:57                                                   ` Eli Zaretskii
2022-10-15 11:44                                                     ` Philip Kaludercic
2022-10-15 12:20                                                       ` Eli Zaretskii
2022-10-15 15:15                                                         ` Philip Kaludercic
2022-10-15 15:16                                                           ` Eli Zaretskii
2022-10-15 15:24                                                             ` Philip Kaludercic
2022-10-10 22:01                           ` Richard Stallman
2022-10-11  5:37                             ` Eli Zaretskii
2022-10-12 21:59                               ` Richard Stallman
2022-10-06 22:10                   ` Dmitry Gutov
2022-10-07  8:03                     ` Philip Kaludercic
2022-10-07 12:56                       ` Dmitry Gutov
2022-10-07 15:30                         ` Philip Kaludercic
2022-10-07 15:47                           ` Dmitry Gutov
2022-10-07 15:54                             ` Philip Kaludercic
2022-10-08 22:34                             ` Richard Stallman
2022-10-08 12:11                         ` Philip Kaludercic
2022-10-08 12:44                           ` German Pacenza
2022-10-08 13:02                             ` Philip Kaludercic
2022-10-08 13:07                           ` Dmitry Gutov
2022-10-08 13:42                             ` Philip Kaludercic
2022-10-08 14:02                               ` Dmitry Gutov

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:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=87y1twvima.fsf@posteo.net \
    --to=philipk@posteo.net \
    --cc=57400@debbugs.gnu.org \
    --cc=ane@iki.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* 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

	https://git.savannah.gnu.org/cgit/emacs.git

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