Skip to content

Stop gh re-drifting the gitconfig credential blocks - #18

Merged
DROOdotFOO merged 1 commit into
mainfrom
fix-gitconfig-drift
Aug 8, 2026
Merged

Stop gh re-drifting the gitconfig credential blocks#18
DROOdotFOO merged 1 commit into
mainfrom
fix-gitconfig-drift

Conversation

@DROOdotFOO

Copy link
Copy Markdown
Owner

Closes the last known self-rewriting file, after #17 handled the two JSON ones.

Cause

gh auth setup-git does not hand-write the credential blocks -- it goes through
git config, which has its own canonical format. Reproduced against a scratch
file:

[credential "https://github.com"]
<TAB>helper =<TRAILING SPACE>
<TAB>helper = !/opt/homebrew/bin/gh auth git-credential

The source was space-indented with no trailing space, so the two never matched
and every gh auth setup-git re-drifted ~/.gitconfig. The previous mitigation
was a comment telling you to run chezmoi apply --force.

Fix

Match git config byte-for-byte, so gh's rewrite is a no-op.

The trailing space is the catch: it cannot exist literally in the source, because
the trailing-whitespace pre-commit hook only excludes home/.*/encrypted_ and
would strip it on the next commit -- silently reintroducing the exact drift being
fixed. So it is emitted by a {{ " " }} template expression, leaving the source
file free of literal trailing whitespace.

No modify_ script here. Unlike settings.json, only these two blocks are ever
rewritten and the values are already fully managed, so there is nothing to merge
-- and INI has no jq equivalent, which would have meant git config -f against
a temp file for no benefit.

Manual testing

  • Reproduced git's exact output on a scratch file to get the format right rather than guessing
  • Rendered template matches git config byte-for-byte (tab + trailing space)
  • grep -n " $" on the source: no literal trailing whitespace
  • pre-commit run passes, and the template still renders the tab and trailing space afterwards -- the hook no longer defeats it
  • Ran gh auth setup-git three times; chezmoi status ~/.gitconfig stayed clean each time
  • Both helpers still resolve (git config --get-all), and gh auth status still reports logged in over https

🤖 Generated with Claude Code

`gh auth setup-git` writes the credential helpers through `git config`,
which emits a tab indent and a trailing space after the empty
`helper =`. The source was space-indented with no trailing space, so
every run re-drifted the file and the fix was to remember to pass
`chezmoi apply --force`.

Match `git config` byte-for-byte instead, so gh's rewrite is a no-op.
A literal trailing space cannot live in the source -- the
trailing-whitespace hook strips it and would silently reintroduce the
drift -- so it is emitted by a {{ " " }} template expression.

No modify_ script needed here: unlike the JSON settings files, only
these two blocks are ever rewritten, and INI has no jq equivalent.

Verified by running `gh auth setup-git` three times; chezmoi status
stays clean and both helpers still resolve.
@DROOdotFOO
DROOdotFOO merged commit c6a0b27 into main Aug 8, 2026
5 checks passed
@DROOdotFOO
DROOdotFOO deleted the fix-gitconfig-drift branch August 8, 2026 19:28
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.

1 participant