libgit2 / libgit2sharp

Git + .NET = ❤
http://libgit2.github.com
MIT License
3.17k stars 886 forks source link

How to control the name of the default branch from Repository.Init? #1964

Open AArnott opened 2 years ago

AArnott commented 2 years ago

I have automated test failures due to Repository.Init(string) creating repos with main as the default branch name instead of master. How can I control the name of the default branch for a new repository so that my tests pass consistently across machines?

I tried using libgit2sharp APIs to create a new branch and deleting the old, but in fact on a brand new repository, there is no branch and no commit, so I guess the default branch is just the name that HEAD will use for a new branch once the first commit is authored. It appears this is stored in .git/HEAD. Is there an API to help us rewrite that file, perhaps?

ethomson commented 2 years ago

You can use the init.defaultBranch configuration setting, which libgit2 learned to obey in v1.1.0. You could also update HEAD after the fact. That feels a little more clunky to me, but should work.

AArnott commented 2 years ago

Thanks. I couldn't find an API to set config settings before creating a repo. Can you direct me to it?

jeroen-mostert commented 2 years ago

Same issue here, seems to be a chicken and egg problem. git init has an --initial-branch option, but Repository.Init doesn't surface that. Changing any configuration other than the repo's own is a global solution for a local problem, and changing the local configuration can't be done until after the repo exists (and if you do the setting has no effect, of course).

"Updating HEAD after the fact" is probably obvious for Git wizards, but if that can be done with libgit2sharp elegantly it's not clear how. My various attempts either all failed on account of having no branches and no way to create them without an initial commit, or ending up with a detached HEAD. Just committing and then renaming the branch works, at the cost of having this in the reflog, but in my book that doesn't really count as the same thing.

Haffi921 commented 2 years ago

Would really appreciate this feature! 'init.defaultBranch' is set for me in my only git setup, still libgit2 doesn't seem to listen to it