Skip to content

Commit

Permalink
Use symbolic-ref instead on initial-branch for Git repo init in Mock (#9
Browse files Browse the repository at this point in the history
)

* Use symbolic-ref instead on initial-branch for Git repo init in Mock

- --initial-branch is only available on recent version of Git
- stable version in debian 10 is older and don't has the option
- Add errors details for easier debug
  • Loading branch information
Toa741 authored May 25, 2021
1 parent 7f8df3c commit 609a48b
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions NGitLab.Mock/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,28 @@ private LibGit2Sharp.Repository GetGitRepository()
var directory = TemporaryDirectory.Create();
if (Project.ForkedFrom == null)
{
LibGit2Sharp.Repository.Init(directory.FullPath);

// libgit2sharp cannot init with a branch other than master
using var process = Process.Start("git", $"init --initial-branch \"{Project.DefaultBranch}\" \"{directory.FullPath}\"");
// Use symbolic-ref to keep the code compatible with older version of git
using var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "git",
Arguments = $"symbolic-ref HEAD \"refs/heads/{Project.DefaultBranch}\"",
RedirectStandardError = true,
WorkingDirectory = directory.FullPath,
},
};

process.Start();
process.WaitForExit();
if (process.ExitCode != 0)
throw new GitLabException($"Cannot init repository with branch '{Project.DefaultBranch}' in '{directory.FullPath}'");
{
var error = process.StandardError.ReadToEnd();
throw new GitLabException($"Cannot update symbolic ref with branch '{Project.DefaultBranch}' in '{directory.FullPath}': {error}");
}
}
else
{
Expand Down

0 comments on commit 609a48b

Please sign in to comment.