fix(use,delete,rename): support multiple directories per identity#39
Merged
Conversation
Each includeIf block in ~/.gitconfig was keyed by the bare identity name (sentinel '# >>> gitswitch:<name>'), and every use/rename stripped the old block before writing the new one. So binding a SECOND directory to an identity overwrote the first directory's block: config.json recorded both bindings, but ~/.gitconfig held only one, and the un-blocked directory silently fell back to the global identity. Binding two repos to one 'private' identity is a completely normal thing to want, and it broke quietly. Key each block per (identity, directory) instead — sentinel '# >>> gitswitch:<name>:<dirhash>' — so multiple directories can share an identity without clobbering. A shared syncBindingBlocks helper rewrites all of an identity's blocks from config.json (the source of truth) and removes the legacy bare-name block, migrating existing users forward on their next use/rename. Also make blocks idempotent: appendBlock now normalises trailing newlines instead of only ever adding them, and stripBlock swallows the trailing blank separator — without this, the repeated strip+re-append in syncBindingBlocks leaked a blank line into ~/.gitconfig on every run. Tests: multi-dir sync (one block per dir, legacy removed, others untouched, idempotent); per-binding name distinct+stable; blocks upsert/remove idempotency.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
Binding two directories to the same identity silently broke — the second binding clobbered the first.
Each
includeIfblock in~/.gitconfigwas keyed by the bare identity name (sentinel# >>> gitswitch:<name>), anduse/renamestrip the old block before writing the new one. So:Now
config.jsonrecords both bindings, but~/.gitconfigholds one block. The un-blocked directory falls back to the global identity with no warning —gitswitch why/doctorwould even report the binding as active becauseconfig.jsonlooks right. Binding several repos to one identity (e.g. all your personal projects →personal) is completely normal, and it failed quietly.The fix
Key each block per (identity, directory) instead — sentinel
# >>> gitswitch:<name>:<dirhash>— so multiple directories can share an identity without clobbering.syncBindingBlocks(cfg, name, path)rewrites all of an identity's blocks fromconfig.json(the source of truth), one per bound directory, and removes the legacy bare-name block — migrating existing users forward on their nextuse/rename.use,delete,rename, and--unbindall route through per-binding keys / the sync helper.blocks:appendBlocknow normalises trailing newlines (instead of only ever adding them) andstripBlockswallows the trailing blank separator. Without this, the repeated strip+re-append insyncBindingBlocksleaked a blank line into~/.gitconfigon every run.Before / after
Test plan
go build ./...,go vet ./...— cleango test ./internal/...— all pass, including new tests:blocks: upsert/remove idempotency across many blocks (no blank-line leak)cmd: multi-dir sync (one block per dir, legacy block removed, other identities untouched, idempotent); per-binding name distinct + stableincludeIfblocks and both directories resolve to the correct identity; re-runninguseis byte-stable (no leaked blank lines).