I ran into an issue when trying to automate setup for new machines, where .gitconfig is empty. If git-idm track
is invoked before git-idm use
is invoked for the first time (and user.name
and include.path
aren't already set), then use
will create the sections [user]
and [include]
after [includeif ...]
, and those sections will always take precedence and remain active even when in an auto-tracked directory.
It's possible to workaround this by activating an identity first before tracking any directories, but the behavior surprised and confused me.
To reproduce
# create a dummy repository
mkdir -p ~/Work/bizcorp-repo
git init ~/Work/bizcorp-repo
# configure git identities
git-idm add personal --name "JaNe DoPe" --email "[email protected]" --ssh-command "..."
git-idm add work --name "Jane Doe" --email "[email protected]" --ssh-command "..."
git-idm track work --directory ~/Work
git-idm use personal
# check that it worked
cd ~/Work/bizcorp-repo
git config user.name # -> JaNe DoPe
# oh no, it didn't! how unprofessional.
Explanation
The commands above create ~/.gitconfig like this:
[gitidm "personal"]
name = JaNe DoPe
email = [email protected]
sshCommand = ...
[gitidm "work"]
name = Jane Doe
email = [email protected]
sshCommand = ...
[includeIf "gitdir:~/Work"]
idm = work
path = ~/.gitconfig_idm_work
[user]
name = JaNe DoPe
email = [email protected]
activeidm = personal
[include]
path = ~/.gitconfig_idm_personal
Because the [user] and [include] sections exist below [includeIf], they end up overwriting the tracking configs.
Solution
I don't think git-config has any commands for re-ordering sections, so I think invoking git-idm track
should check first if an active identity is set (i.e. if key user.name
and include.path
already exist), and if not, try to activate one of them in order to populate those sections with dummy values prior to the creation of include rules which should overwrite them.
Kind of rusty with Bash scripting right now, but I might try to implement the fix myself.