Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions diff-hl.el
Original file line number Diff line number Diff line change
Expand Up @@ -476,16 +476,18 @@ BUFFER defaults to the current buffer."
"diff-files"
(cons "-p" (vc-switches 'git 'diff))))
((eq new-rev 'git-index)
;; With no revision, `git diff --cached' uses HEAD when it exists and
;; the empty tree on an unborn branch.
(apply #'vc-git-command buffer
(if (diff-hl--use-async-p) 'async 1)
(list file)
"diff-index"
"diff"
(append
(vc-switches 'git 'diff)
(list "-p" "--cached"
(or diff-hl-reference-revision

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Could you try this version?

(or diff-hl-reference-revision
      (vc-git--rev-parse "HEAD")
      "4b825dc642cb6eb9a060e54bf8d69288fbee4904")

still with diff-index, without switching to git diff. The above literal is the "empty repository" hash for SHA-1, we use it below too.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, or maybe not - that adds +1 process call on the common path

(diff-hl-head-revision backend))
"--"))))
(list "-p" "--cached")
(when diff-hl-reference-revision
(list diff-hl-reference-revision))
(list "--"))))
(t
(condition-case err
(vc-call-backend backend 'diff (list file)
Expand Down
49 changes: 43 additions & 6 deletions test/diff-hl-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,14 @@
(should
(null
(assoc-default :reference (diff-hl-changes)))))
(let* ((diff-hl-show-staged-changes nil)
(res-buf (assoc-default :reference (diff-hl-changes))))
(should
(equal
(diff-hl-changes-from-buffer res-buf)
'((1 1 0 insert)))))))
(dolist (reference '(nil "HEAD"))
(let* ((diff-hl-reference-revision reference)
(diff-hl-show-staged-changes nil)
(res-buf (assoc-default :reference (diff-hl-changes))))
(should
(equal
(diff-hl-changes-from-buffer res-buf)
'((1 1 0 insert))))))))

(diff-hl-deftest diff-hl-flydiff-can-ignore-staged-changes ()
(diff-hl-test-in-source
Expand All @@ -195,6 +197,41 @@
(diff-hl-diff-buffer-with-reference (diff-hl--buffer-file-name)))
'((12 1 0 insert)))))))

(ert-deftest diff-hl-can-ignore-staged-changes-in-unborn-repository ()
(let* ((directory (make-temp-file "diff-hl-unborn-" t))
(default-directory directory)
(file (expand-file-name "tracked" directory))
buffer)
(unwind-protect
(progn
(vc-git-command nil 0 nil "init")
(with-temp-file file
(insert "staged\n"))
(vc-git-command nil 0 file "add")
(with-temp-file file
(insert "staged\nworking\n"))
(setq buffer (find-file-noselect file))
(with-current-buffer buffer
(dolist (async '(t nil))
(let* ((diff-hl-highlight-reference-function #'ignore)
(diff-hl-show-staged-changes nil)
(diff-hl-update-async async)
(changes (diff-hl-changes))
(reference (assoc-default :reference changes))
(working (assoc-default :working changes)))
(while (or (process-live-p (get-buffer-process reference))
(process-live-p (get-buffer-process working)))
(accept-process-output nil 0.05))
(should (equal (diff-hl-changes-from-buffer reference)
'((1 1 0 insert))))
(should (equal (diff-hl-changes-from-buffer working)
'((2 1 0 insert))))))))
(when (buffer-live-p buffer)
(with-current-buffer buffer
(set-buffer-modified-p nil))
(kill-buffer buffer))
(delete-directory directory t))))

(diff-hl-deftest diff-hl-can-split-away-no-trailing-newline ()
(diff-hl-test-in-source
(goto-char (point-max))
Expand Down
Loading