]> git.eshelyaron.com Git - emacs.git/commitdiff
vc-git-diff: Fix case where REV1 & REV2 are nil and no commits yet
authorSean Whitton <spwhitton@spwhitton.name>
Wed, 16 Jul 2025 08:55:54 +0000 (09:55 +0100)
committerEshel Yaron <me@eshelyaron.com>
Thu, 24 Jul 2025 09:48:09 +0000 (11:48 +0200)
* lisp/vc/vc-git.el (vc-git--empty-tree): New constant.
(vc-git-diff): When REV1 and REV2 are both nil and there are no
commits yet, diff against the empty tree object (bug#78987).

(cherry picked from commit aec5c5f037b72f2ab49b17f4e3f6655e0c2125b3)

lisp/vc/vc-git.el

index 72b7cea174ca1fd0a6363bfbcaae59acd17e6dae..db5c7186bbe9c05a876244fc0f4b9791a0205416 100644 (file)
@@ -1809,16 +1809,21 @@ This requires git 1.8.4 or later, for the \"-L\" option of \"git log\"."
                                 samp coding-system-for-read t)))
     (setq coding-system-for-read 'undecided)))
 
+(defconst vc-git--empty-tree "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
+  "Git object ID of the empty tree object.")
+
 (defun vc-git-diff (files &optional rev1 rev2 buffer async)
   "Get a difference report using Git between two revisions of FILES."
   (let (process-file-side-effects
         (command "diff-tree"))
     (vc-git--asciify-coding-system)
     (if rev2
-        ;; Diffing against the empty tree.
-        (unless rev1 (setq rev1 "4b825dc642cb6eb9a060e54bf8d69288fbee4904"))
+        (unless rev1 (setq rev1 vc-git--empty-tree))
       (setq command "diff-index")
-      (unless rev1 (setq rev1 "HEAD")))
+      (unless rev1
+        ;; If there aren't any commits yet then there is no HEAD.
+        ;; So diff against the empty tree object.
+        (setq rev1 (if (vc-git--empty-db-p) vc-git--empty-tree "HEAD"))))
     (if vc-git-diff-switches
         (apply #'vc-git-command (or buffer "*vc-diff*")
                (if async 'async 1)