From: Juri Linkov <juri@linkov.net>
To: Dmitry Gutov <dgutov@yandex.ru>
Cc: 52349@debbugs.gnu.org
Subject: bug#52349: 29.0.50; vc-git and diff-mode: stage hunks
Date: Sun, 21 Aug 2022 21:53:46 +0300 [thread overview]
Message-ID: <861qt9h1hh.fsf@mail.linkov.net> (raw)
In-Reply-To: <d58a3dab-5feb-b6d9-59c4-ecbad9263ab4@yandex.ru> (Dmitry Gutov's message of "Mon, 14 Feb 2022 00:37:03 +0200")
[-- Attachment #1: Type: text/plain, Size: 469 bytes --]
> Another solution for making sure the current backend supports the new
> feature is to add a new backend function. Call it 'checkin-patch' or
> `checkin-diff`.
>
> vc-git-checkin and vc-git-checking patch can share some implementation, so
> most of code duplication can be avoided.
>
> But when a backend does not support this, the user will see a more-or-less
> understandable error like "not supported".
So a new patch adds a new backend function 'checkin-patch':
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: diff-vc-deduce-fileset.patch --]
[-- Type: text/x-diff, Size: 9575 bytes --]
diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index e4a1996c1b..5147f37e95 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -2928,6 +2928,15 @@ diff-syntax-fontify-props
(forward-line 1)))
(nreverse props)))
+;;;###autoload
+(defun diff-vc-deduce-fileset ()
+ (let ((backend (vc-responsible-backend default-directory))
+ files)
+ (save-excursion
+ (goto-char (point-min))
+ (while (progn (diff-file-next) (not (eobp)))
+ (push (diff-find-file-name nil t) files)))
+ (list backend (nreverse files) nil nil 'patch)))
(defun diff--filter-substring (str)
(when diff-font-lock-prettify
diff --git a/lisp/vc/vc-dispatcher.el b/lisp/vc/vc-dispatcher.el
index e2a490092b..93bcf4e976 100644
--- a/lisp/vc/vc-dispatcher.el
+++ b/lisp/vc/vc-dispatcher.el
@@ -624,6 +624,8 @@ vc-buffer-sync
(declare-function log-edit-empty-buffer-p "log-edit" ())
+(defvar vc-patch-string)
+
(defun vc-log-edit (fileset mode backend)
"Set up `log-edit' for use on FILE."
(setq default-directory
@@ -653,15 +655,17 @@ vc-log-edit
(mapcar
(lambda (file) (file-relative-name file root))
fileset))))
- (log-edit-diff-function . vc-diff)
+ (log-edit-diff-function
+ . ,(if vc-patch-string 'vc-diff-patch 'vc-diff))
(log-edit-vc-backend . ,backend)
- (vc-log-fileset . ,fileset))
+ (vc-log-fileset . ,fileset)
+ (vc-patch-string . ,vc-patch-string))
nil
mode)
(set-buffer-modified-p nil)
(setq buffer-file-name nil))
-(defun vc-start-logentry (files comment initial-contents msg logbuf mode action &optional after-hook backend)
+(defun vc-start-logentry (files comment initial-contents msg logbuf mode action &optional after-hook backend patch-string)
"Accept a comment for an operation on FILES.
If COMMENT is nil, pop up a LOGBUF buffer, emit MSG, and set the
action on close to ACTION. If COMMENT is a string and
@@ -688,6 +692,7 @@ vc-start-logentry
(setq-local vc-parent-buffer parent)
(setq-local vc-parent-buffer-name
(concat " from " (buffer-name vc-parent-buffer)))
+ (setq-local vc-patch-string patch-string)
(vc-log-edit files mode backend)
(make-local-variable 'vc-log-after-operation-hook)
(when after-hook
@@ -753,7 +758,8 @@ vc-finish-logentry
(defun vc-dispatcher-browsing ()
"Are we in a directory browser buffer?"
(or (derived-mode-p 'vc-dir-mode)
- (derived-mode-p 'dired-mode)))
+ (derived-mode-p 'dired-mode)
+ (derived-mode-p 'diff-mode)))
;; These are unused.
;; (defun vc-dispatcher-in-fileset-p (fileset)
diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el
index 46a486a46c..8458b40714 100644
--- a/lisp/vc/vc-git.el
+++ b/lisp/vc/vc-git.el
@@ -53,7 +53,8 @@
;; - responsible-p (file) OK
;; - receive-file (file rev) NOT NEEDED
;; - unregister (file) OK
-;; * checkin (files rev comment) OK
+;; * checkin (files comment rev) OK
+;; - checkin-patch (patch-string comment) OK
;; * find-revision (file rev buffer) OK
;; * checkout (file &optional rev) OK
;; * revert (file &optional contents-done) OK
@@ -914,6 +915,12 @@ vc-git-log-edit-mode
"Major mode for editing Git log messages.
It is based on `log-edit-mode', and has Git-specific extensions.")
+(defvar vc-git-patch-string nil)
+
+(defun vc-git-checkin-patch (patch-string comment)
+ (let ((vc-git-patch-string patch-string))
+ (vc-git-checkin nil comment nil)))
+
(defun vc-git-checkin (files comment &optional _rev)
(let* ((file1 (or (car files) default-directory))
(root (vc-git-root file1))
@@ -936,12 +943,20 @@ vc-git-checkin
(if (eq system-type 'windows-nt)
(let ((default-directory (file-name-directory file1)))
(make-nearby-temp-file "git-msg")))))
+ (when vc-git-patch-string
+ (let ((patch-file (make-temp-file "git-patch")))
+ (with-temp-file patch-file
+ (insert vc-git-patch-string))
+ (unwind-protect
+ (apply #'vc-git-command nil 0 patch-file
+ (list "apply" "--cached"))
+ (delete-file patch-file))))
(cl-flet ((boolean-arg-fn
(argument)
(lambda (value) (when (equal value "yes") (list argument)))))
;; When operating on the whole tree, better pass "-a" than ".", since "."
;; fails when we're committing a merge.
- (apply #'vc-git-command nil 0 (if only files)
+ (apply #'vc-git-command nil 0 (if (and only (not vc-git-patch-string)) files)
(nconc (if msg-file (list "commit" "-F"
(file-local-name msg-file))
(list "commit" "-m"))
@@ -959,7 +974,8 @@ vc-git-checkin
(write-region (car args) nil msg-file))
(setq args (cdr args)))
args)
- (if only (list "--only" "--") '("-a")))))
+ (unless vc-git-patch-string
+ (if only (list "--only" "--") '("-a"))))))
(if (and msg-file (file-exists-p msg-file)) (delete-file msg-file))))
(defun vc-git-find-revision (file rev buffer)
diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el
index b05adfb2d5..c5c9835de9 100644
--- a/lisp/vc/vc.el
+++ b/lisp/vc/vc.el
@@ -247,6 +247,11 @@
;; revision argument is only supported with some older VCSes, like
;; RCS and CVS, and is otherwise silently ignored.
;;
+;; - checkin-patch (patch-string comment)
+;;
+;; Commit a single patch PATCH-STRING to this backend, bypassing
+;; the changes in filesets. COMMENT is used as a check-in comment.
+;;
;; * find-revision (file rev buffer)
;;
;; Fetch revision REV of file FILE and put it into BUFFER.
@@ -1102,6 +1107,8 @@ vc-deduce-fileset-1
(vc-dir-deduce-fileset state-model-only-files))
((derived-mode-p 'dired-mode)
(dired-vc-deduce-fileset state-model-only-files not-state-changing))
+ ((derived-mode-p 'diff-mode)
+ (diff-vc-deduce-fileset))
((setq backend (vc-backend buffer-file-name))
(if state-model-only-files
(list backend (list buffer-file-name)
@@ -1114,7 +1121,8 @@ vc-deduce-fileset-1
(or (buffer-file-name vc-parent-buffer)
(with-current-buffer vc-parent-buffer
(or (derived-mode-p 'vc-dir-mode)
- (derived-mode-p 'dired-mode)))))
+ (derived-mode-p 'dired-mode)
+ (derived-mode-p 'diff-mode)))))
(progn ;FIXME: Why not `with-current-buffer'? --Stef.
(set-buffer vc-parent-buffer)
(vc-deduce-fileset-1 not-state-changing allow-unregistered state-model-only-files)))
@@ -1230,6 +1238,8 @@ vc-next-action
(error "Fileset files are missing, so cannot be operated on"))
((eq state 'ignored)
(error "Fileset files are ignored by the version-control system"))
+ ((eq model 'patch)
+ (vc-checkin files backend nil nil nil (buffer-string)))
((or (null state) (eq state 'unregistered))
(cond (verbose
(let ((backend (vc-read-backend "Backend to register to: ")))
@@ -1615,7 +1625,9 @@ vc-steal-lock
".\n")
(message "Please explain why you stole the lock. Type C-c C-c when done.")))
-(defun vc-checkin (files backend &optional comment initial-contents rev)
+(defvar vc-patch-string nil)
+
+(defun vc-checkin (files backend &optional comment initial-contents rev patch-string)
"Check in FILES. COMMENT is a comment string; if omitted, a
buffer is popped up to accept a comment. If INITIAL-CONTENTS is
non-nil, then COMMENT is used as the initial contents of the log
@@ -1643,7 +1655,9 @@ vc-checkin
;; vc-checkin-switches, but 'the' local buffer is
;; not a well-defined concept for filesets.
(progn
- (vc-call-backend backend 'checkin files comment rev)
+ (if patch-string
+ (vc-call-backend backend 'checkin-patch patch-string comment)
+ (vc-call-backend backend 'checkin files comment rev))
(mapc #'vc-delete-automatic-version-backups files))
`((vc-state . up-to-date)
(vc-checkout-time . ,(file-attribute-modification-time
@@ -1651,7 +1665,8 @@ vc-checkin
(vc-working-revision . nil)))
(message "Checking in %s...done" (vc-delistify files)))
'vc-checkin-hook
- backend))
+ backend
+ patch-string))
;;; Additional entry points for examining version histories
@@ -1779,6 +1794,23 @@ vc-diff-finish
(defvar vc-diff-added-files nil
"If non-nil, diff added files by comparing them to /dev/null.")
+(defvar vc-patch-buffer nil)
+
+(defun vc-diff-patch ()
+ (let ((buffer "*vc-diff*")
+ (patch-string vc-patch-string))
+ (vc-setup-buffer buffer)
+ (set-buffer buffer)
+ (let ((buffer-undo-list t)
+ (inhibit-read-only t))
+ (insert patch-string))
+ (setq buffer-read-only t)
+ (diff-mode)
+ (setq-local diff-vc-backend (vc-responsible-backend default-directory))
+ (setq-local revert-buffer-function (lambda (_ _) (vc-diff-patch)))
+ (pop-to-buffer (current-buffer))
+ (vc-run-delayed (vc-diff-finish (current-buffer) nil))))
+
(defun vc-diff-internal (async vc-fileset rev1 rev2 &optional verbose buffer)
"Report diffs between two revisions of a fileset.
Output goes to the buffer BUFFER, which defaults to *vc-diff*.
[-- Attachment #3: Type: text/plain, Size: 181 bytes --]
> Perhaps we should also check that there are no existing staged changes for
> those files, and if so, abort? This way we won't commit anything else by
> accident.
Will do later.
next prev parent reply other threads:[~2022-08-21 18:53 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-07 8:59 bug#52349: 29.0.50; vc-git and diff-mode: stage hunks Manuel Uberti via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-12-07 17:03 ` Juri Linkov
2021-12-07 17:08 ` Manuel Uberti via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-12-07 19:04 ` Juri Linkov
2021-12-07 17:10 ` Dmitry Gutov
2021-12-07 19:06 ` Juri Linkov
2021-12-08 2:29 ` Dmitry Gutov
2021-12-08 18:57 ` Juri Linkov
2021-12-09 23:17 ` Dmitry Gutov
2022-02-08 19:57 ` Juri Linkov
2022-02-12 1:43 ` Dmitry Gutov
2022-02-12 19:42 ` Juri Linkov
2022-02-13 1:32 ` Dmitry Gutov
2022-02-13 19:48 ` Juri Linkov
2022-02-13 22:51 ` Dmitry Gutov
2022-02-13 19:56 ` Juri Linkov
2022-02-13 22:41 ` Dmitry Gutov
2022-08-21 16:07 ` Juri Linkov
2022-02-13 22:37 ` Dmitry Gutov
2022-08-21 18:53 ` Juri Linkov [this message]
2022-08-24 2:06 ` Dmitry Gutov
2022-08-24 18:20 ` Juri Linkov
2022-08-24 20:20 ` Dmitry Gutov
2022-08-27 20:07 ` Juri Linkov
2022-08-28 0:40 ` Dmitry Gutov
2022-08-28 19:45 ` Juri Linkov
2022-08-28 23:34 ` Dmitry Gutov
2022-09-08 19:29 ` Juri Linkov
2022-09-08 22:48 ` Sean Whitton
2022-09-10 1:36 ` Dmitry Gutov
2022-09-11 15:05 ` Juri Linkov
2022-09-11 21:57 ` Dmitry Gutov
2022-09-12 18:19 ` Juri Linkov
2022-09-19 2:09 ` Dmitry Gutov
2022-09-19 6:50 ` Juri Linkov
2022-09-19 12:57 ` Dmitry Gutov
2022-10-02 18:51 ` Juri Linkov
2022-11-04 1:32 ` Dmitry Gutov
2022-08-26 17:11 ` Filipp Gunbin
2022-08-27 19:56 ` Juri Linkov
2021-12-07 20:07 ` Lars Ingebrigtsen
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=861qt9h1hh.fsf@mail.linkov.net \
--to=juri@linkov.net \
--cc=52349@debbugs.gnu.org \
--cc=dgutov@yandex.ru \
/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).