Subject: Re: bug#52349: 29.0.50; vc-git and diff-mode: stage hunks

From: Juri Linkov <[email-masked]>
Date: Sat, 27 Aug 2022 23:07:12 +0300

>> @@ -944,6 +944,8 @@ vc-git-checkin
>>                 (let ((default-directory (file-name-directory file1)))
>>                   (make-nearby-temp-file "git-msg")))))
>>       (when vc-git-patch-string
>> +      (unless (eq (vc-git-command nil t nil "diff" "--cached" "--quiet") 0)
>> +        (error "Index not empty"))
>
> user-error, maybe?

Will be fixed.

>>         (let ((patch-file (make-temp-file "git-patch")))
>>           (with-temp-file patch-file
>>             (insert vc-git-patch-string))
>
> Looking great otherwise. Please install whenever you think it's ready.

Not yet great :)  I tried to fix another long-standing problem
because its solution should also define the names of new functions here.

The problem is that currently using 'C-c C-d' (log-edit-show-diff)
in the *vc-log* buffer doesn't show the same diff that will be really
committed when different files were marked in *vc-dir* parent buffer
before typing 'C-c C-c' in *vc-log*.

This is because currently log-edit-diff-function is set to vc-diff
that is not suitable here since it tries to deduce fileset again
from *vc-dir*, but files to commit are set in the buffer-local
'vc-log-fileset' in *vc-log*.  So I added a new function
'log-edit-diff-fileset' that shows the diff from 'vc-log-fileset'.
Using the same naming scheme, I renamed 'vc-diff-patch' to
'log-edit-diff-patch'.

This shows the difference from the previous patch,
but later these changes could be committed separately:

diff --git a/lisp/vc/log-edit.el b/lisp/vc/log-edit.el
index e958673fea..5290616302 100644
--- a/lisp/vc/log-edit.el
+++ b/lisp/vc/log-edit.el
@@ -664,6 +664,19 @@ log-edit-set-common-indentation
       (indent-rigidly (point) (point-max)
 		      (- log-edit-common-indent common)))))
 
+(defvar vc-patch-string)
+
+(autoload 'vc-diff-patch-string "vc")
+(defun log-edit-diff-patch ()
+  (vc-diff-patch-string vc-patch-string))
+
+(defvar vc-log-fileset)
+
+(defun log-edit-diff-fileset ()
+  "Display diffs for the files to be committed."
+  (interactive)
+  (vc-diff nil nil (list log-edit-vc-backend vc-log-fileset)))
+
 (defun log-edit-show-diff ()
   "Show the diff for the files to be committed."
   (interactive)
diff --git a/lisp/vc/vc-dispatcher.el b/lisp/vc/vc-dispatcher.el
index 74e624999f..36a6f27891 100644
--- a/lisp/vc/vc-dispatcher.el
+++ b/lisp/vc/vc-dispatcher.el
@@ -656,7 +656,7 @@ vc-log-edit
                        (lambda (file) (file-relative-name file root))
                        fileset))))
 	      (log-edit-diff-function
-               . ,(if vc-patch-string 'vc-diff-patch 'vc-diff))
+               . ,(if vc-patch-string 'log-edit-diff-patch 'log-edit-diff-fileset))
 	      (log-edit-vc-backend . ,backend)
 	      (vc-log-fileset . ,fileset)
 	      (vc-patch-string . ,vc-patch-string))
diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el
index cce692f7ea..51ae10ae5c 100644
--- a/lisp/vc/vc.el
+++ b/lisp/vc/vc.el
@@ -1795,18 +1795,16 @@ vc-diff-added-files
 
 (defvar vc-patch-string nil)
 
-(defun vc-diff-patch ()
-  (let ((buffer "*vc-diff*")
-        (patch-string vc-patch-string))
+(defun vc-diff-patch-string (patch-string)
+  (let ((buffer "*vc-diff*"))
     (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)))
+    (setq-local revert-buffer-function (lambda (_ _) (vc-diff-patch-string)))
     (setq-local vc-patch-string patch-string)
     (pop-to-buffer (current-buffer))
     (vc-run-delayed (vc-diff-finish (current-buffer) nil))))
@@ -2000,7 +1998,7 @@ vc-maybe-buffer-sync
     (when buffer-file-name (vc-buffer-sync not-urgent))))
 
 ;;;###autoload
-(defun vc-diff (&optional historic not-urgent)
+(defun vc-diff (&optional historic not-urgent fileset)
   "Display diffs between file revisions.
 Normally this compares the currently selected fileset with their
 working revisions.  With a prefix argument HISTORIC, it reads two revision
@@ -2012,7 +2010,7 @@ vc-diff
   (if historic
       (call-interactively 'vc-version-diff)
     (vc-maybe-buffer-sync not-urgent)
-    (let ((fileset (vc-deduce-fileset t)))
+    (let ((fileset (or fileset (vc-deduce-fileset t))))
       (vc-buffer-sync-fileset fileset not-urgent)
       (vc-diff-internal t fileset nil nil
 			(called-interactively-p 'interactive)))))