8000 How to control the name of the default branch from Repository.Init? · Issue #1964 · libgit2/libgit2sharp · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking &ld 8000 quo;Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
AArnott opened this issue May 1, 2022 · 5 comments
Open

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

AArnott opened this issue May 1, 2022 · 5 comments

Comments

@AArnott
Copy link
Contributor
AArnott commented May 1, 2022

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
Copy link
Member
ethomson commented May 2, 2022

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
Copy link
Contributor Author
AArnott commented May 4, 2022

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

@jeroen-mostert
Copy link
jeroen-mostert commented Jul 5, 2022

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
Copy link

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

0xced added a commit to 0xced/Incrementalist that referenced this issue May 6, 2025
Many tests are assuming the default branch is `master`. But if the `~/.gitconfig` file contains another default branch name, then all these tests will fail.

```ini
[init]
	defaultBranch = "main"
```

See [How to control the name of the default branch from Repository.Init?][1] on the LibGit2Sharp repository.

[1]: libgit2/libgit2sharp#1964
Aaronontheweb pushed a commit to petabridge/Incrementalist that referenced this issue May 8, 2025
* Fix running tests on macOS

On macOS, the temporary directory contains a symlink and all tests using `DisposableRepository` would fail with the following error:

> System.ArgumentException
Unable to process file '/var/folders/62/pz7b9x0x7hv54vy47pllqlvw0000gn/T/edic1ukd/.gitignore'. This f
81F8
ile is not located under the working directory of the repository ('/private/var/folders/62/pz7b9x0x7hv54vy47pllqlvw0000gn/T/edic1ukd/').
   at LibGit2Sharp.RepositoryExtensions.BuildRelativePathFrom(IRepository repo, String path) in /_/LibGit2Sharp/RepositoryExtensions.cs:line 215
   at LibGit2Sharp.Repository.ToFilePaths(IEnumerable`1 paths) in /_/LibGit2Sharp/Repository.cs:line 1676
   at LibGit2Sharp.Diff.BuildDiffList(ObjectId oldTreeId, ObjectId newTreeId, TreeComparisonHandleRetriever comparisonHandleRetriever, DiffModifiers diffOptions, IEnumerable`1 paths, ExplicitPathsOptions explicitPathsOptions, CompareOptions compareOptions) in /_/LibGit2Sharp/Diff.cs:line 544
   at LibGit2Sharp.Diff.Compare[T](DiffModifiers diffOptions, IEnumerable`1 paths, ExplicitPathsOptions explicitPathsOptions, CompareOptions compareOptions) in /_/LibGit2Sharp/Diff.cs:line 483
   at LibGit2Sharp.Commands.Stage(IRepository repository, IEnumerable`1 paths, StageOptions stageOptions) in /_/LibGit2Sharp/Commands/Stage.cs:line 77
   at LibGit2Sharp.Commands.Stage(IRepository repository, String path) in /_/LibGit2Sharp/Commands/Stage.cs:line 25
   at Incrementalist.Tests.Helpers.DisposableRepository.WriteFile(String fileName, String fileText) in ~/Incrementalist/src/Incrementalist.Tests/Helpers/DisposableRepository.cs:line 131
   at Incrementalist.Tests.Helpers.DisposableRepository.Init() in ~/Incrementalist/src/Incrementalist.Tests/Helpers/DisposableRepository.cs:line 93
   at Incrementalist.Tests.Helpers.DisposableRepository..ctor(AbsolutePath basePath) in ~/Incrementalist/src/Incrementalist.Tests/Helpers/DisposableRepository.cs:line 56
   at Incrementalist.Tests.Helpers.DisposableRepository..ctor() in ~/Incrementalist/src/Incrementalist.Tests/Helpers/DisposableRepository.cs:line 46
   at Incrementalist.Tests.Git.GitBranchDetectionSpecs..ctor() in ~/Incrementalist/src/Incrementalist.Tests/Git/GitBranchDetectionSpecs.cs:line 18
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)

See also libgit2/libgit2sharp#1945

* Ensure that the default branch is named `master`

Many tests are assuming the default branch is `master`. But if the `~/.gitconfig` file contains another default branch name, then all these tests will fail.

```ini
[init]
	defaultBranch = "main"
```

See [How to control the name of the default branch from Repository.Init?][1] on the LibGit2Sharp repository.

[1]: libgit2/libgit2sharp#1964
@0xced
Copy link
0xced commented May 8, 2025

For reference, here's how to translate update HEAD after the fact into code, assuming you want to ensure that the default branch is named main.

if (repository.Refs.Head.TargetIdentifier != "refs/heads/main")
{
    // ensure the default branch is named "main", see https://github.com/libgit2/libgit2sharp/issues/1964
    var master = repository.CreateBranch("main");
    repository.Refs.UpdateTarget(repository.Refs.Head, master.Reference);
}

Successfully used here: petabridge/Incrementalist@e871112

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

No branches or pull requests

5 participants
0