Skip to content

Handle unborn Git branches when staged changes are hidden. - #297

Open
am5083 wants to merge 1 commit into
dgutov:masterfrom
am5083:fix/unborn-git-head
Open

Handle unborn Git branches when staged changes are hidden.#297
am5083 wants to merge 1 commit into
dgutov:masterfrom
am5083:fix/unborn-git-head

Conversation

@am5083

@am5083 am5083 commented Aug 23, 2026

Copy link
Copy Markdown

Fixes #298

When diff-hl-show-staged-changes is nil, diff-hl currently
computes the staged/reference layer with:

git diff-index --cached HEAD

This fails in a repository with an unborn branch because HEAD
does not exist yet. Synchronous updates signal status 128.
Asynchronous updates subsequently pass Git's fatal diagnostic to
diff-hl-changes-from-buffer, which fails in
diff-beginning-of-hunk.

Use git diff --cached [<commit>] instead. When no explicit commit
is supplied, Git compares the index against HEAD when it exists
and against the empty tree on an unborn branch. An explicit
diff-hl-reference-revision is still passed through unchanged.

The regression test creates a temporary unborn repository with
separate staged and worktree changes and verifies both layers with
synchronous and asynchronous updates. The existing test also
checks implicit and explicit HEAD references.

Tested with Emacs 30.2:

  • staged-change tests: 3/3 passed
  • remaining applicable ERT tests: 15/15 passed
  • byte compilation with warnings as errors passed
  • git diff --check passed

Comment thread diff-hl.el
(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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error when hiding staged changes in a Git repository without commits

2 participants