From: Sean Whitton Date: Wed, 16 Jul 2025 08:55:54 +0000 (+0100) Subject: vc-git-diff: Fix case where REV1 & REV2 are nil and no commits yet X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=1c99bb9c656118e950d51a485c05b8273dde1f79;p=emacs.git vc-git-diff: Fix case where REV1 & REV2 are nil and no commits yet * 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) --- diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el index 72b7cea174c..db5c7186bbe 100644 --- a/lisp/vc/vc-git.el +++ b/lisp/vc/vc-git.el @@ -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)