r-lib / usethis

Set up commonly used 📦 components
https://usethis.r-lib.org/
Other
857 stars 285 forks source link

`git_sitrep()` can report >1 value for the default default branch #2057

Open jennybc opened 2 weeks ago

jennybc commented 2 weeks ago

I discovered this in Windows Positron work. My Git for Windows installation has system-level config that sets init.defaultBranch to master. I have init.defaultBranch set to main at the user level. git_sitrep() shows this:

• Default initial branch name: "master" and "main"

which is weird!

The underlying issue is this:

dat <- gert::git_config_global()
subset(dat, name == "init.defaultbranch")
#>                  name  value  level
#> 10 init.defaultbranch master system
#> 13 init.defaultbranch   main global

I think I've never personally experienced this before because here's the same information on macOS:

dat <- gert::git_config_global()
subset(dat, name == "init.defaultbranch")
#>                 name value  level
#> 7 init.defaultbranch  main global

For the record, here are the Git versions in each location:

on macOS

git version 2.39.3 (Apple Git-145)

on Windows

git version 2.45.2.windows.1

so the Windows Git version is actually newer.

All that really matters is that usethis:::git_cfg_get() needs to account for this possibility, though:

https://github.com/r-lib/usethis/blob/2cc9e5a4e5e3034399fafb65d871eec352d83a15/R/utils-git.R#L53-L72

Currently it seems to basically gloss over system config and only accounts for global and local.

jennybc commented 2 weeks ago

Interesting tidbit from macOS. It's true that gert::git_config_global() doesn't reveal any system config, but there does seem to be some:

~/rrr/usethis % git config --show-origin --show-scope --list
unknown file:/Library/Developer/CommandLineTools/usr/share/git-core/gitconfig   credential.helper=osxkeychain
unknown file:/Library/Developer/CommandLineTools/usr/share/git-core/gitconfig   init.defaultbranch=main
global  file:/Users/jenny/.gitconfig    user.name=Jenny Bryan
global  file:/Users/jenny/.gitconfig    user.email=jenny.f.bryan@gmail.com
global  file:/Users/jenny/.gitconfig    user.signingkey=/Users/jenny/.ssh/id_ed25519.pub
... and so on and so forth ...

Notably this system config is labelled as "unknown", as opposed to "global" and "local" (or even "system").

jeroen commented 2 weeks ago

Haven't tested this, but from the source it looks like git-for-windows lets the user choose the "system" value of init.defaultbranch during the installer process? So it is not specific to the version but rather to the vendor.

I think the unknown config file was hacked in by apple's git build to override some defaults in xcode. Note the path of this config is inside git-core so it really seems specific to this build of command-line-git and not intended for other git versions or libraries. That would explain why libgit2 does not find it, but I'll ask upstream in libgit2: https://github.com/libgit2/libgit2/issues/6883