Skip to content

Commit a8b1d51

Browse files
committed
Add support to pass "initial head" on repo init
1 parent 4daf618 commit a8b1d51

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

LibGit2Sharp.Tests/RepositoryFixture.cs

+21
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,27 @@ public void CanCreateStandardRepoAndDirectlySpecifyAGitDirectory()
156156
}
157157
}
158158

159+
[Fact]
160+
public void CanCreateStandardRepoAndDefineCustomHead()
161+
{
162+
var scd1 = BuildSelfCleaningDirectory();
163+
var scd2 = BuildSelfCleaningDirectory();
164+
165+
var gitDir = Path.Combine(scd2.DirectoryPath, ".git/");
166+
167+
string repoPath = Repository.Init(scd1.DirectoryPath, gitDir, new InitOptions(){ InitialHead = "monkey"});
168+
169+
Assert.True(Repository.IsValid(repoPath));
170+
171+
using (var repo = new Repository(repoPath))
172+
{
173+
Assert.True(Repository.IsValid(repo.Info.WorkingDirectory));
174+
Assert.True(Repository.IsValid(repo.Info.Path));
175+
176+
Assert.Equal("monkey", repo.Head.FriendlyName);
177+
}
178+
}
179+
159180
private static void CheckGitConfigFile(string dir)
160181
{
161182
string configFilePath = Path.Combine(dir, "config");

LibGit2Sharp/Core/GitRepositoryInitOptions.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal class GitRepositoryInitOptions : IDisposable
1616
public IntPtr InitialHead;
1717
public IntPtr OriginUrl;
1818

19-
public static GitRepositoryInitOptions BuildFrom(FilePath workdirPath, bool isBare)
19+
public static GitRepositoryInitOptions BuildFrom(FilePath workdirPath, bool isBare, string initialHead)
2020
{
2121
var opts = new GitRepositoryInitOptions
2222
{
@@ -31,6 +31,11 @@ public static GitRepositoryInitOptions BuildFrom(FilePath workdirPath, bool isBa
3131
opts.WorkDirPath = StrictFilePathMarshaler.FromManaged(workdirPath);
3232
}
3333

34+
if (!string.IsNullOrEmpty(initialHead))
35+
{
36+
opts.InitialHead = StrictUtf8Marshaler.FromManaged(initialHead);
37+
}
38+
3439
if (isBare)
3540
{
3641
opts.Flags |= GitRepositoryInitFlags.GIT_REPOSITORY_INIT_BARE;

LibGit2Sharp/Core/Proxy.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2480,9 +2480,10 @@ public static unsafe IndexHandle git_repository_index(RepositoryHandle repo)
24802480
public static unsafe RepositoryHandle git_repository_init_ext(
24812481
FilePath workdirPath,
24822482
FilePath gitdirPath,
2483-
bool isBare)
2483+
bool isBare,
2484+
string initialHead)
24842485
{
2485-
using (var opts = GitRepositoryInitOptions.BuildFrom(workdirPath, isBare))
2486+
using (var opts = GitRepositoryInitOptions.BuildFrom(workdirPath, isBare, initialHead))
24862487
{
24872488
git_repository* repo;
24882489
int res = NativeMethods.git_repository_init_ext(out repo, gitdirPath, opts);

LibGit2Sharp/InitOptions.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace LibGit2Sharp
2+
{
3+
/// <summary>
4+
/// Optional parameters when invoking Init.
5+
/// </summary>
6+
public sealed class InitOptions
7+
{
8+
/// <summary>
9+
/// Use the specified name for the initial branch in the newly created repository.
10+
/// If not specified, fall back to the default name
11+
/// </summary>
12+
public string InitialHead { get; set; }
13+
}
14+
}

LibGit2Sharp/Repository.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -490,12 +490,13 @@ public static string Init(string path)
490490
/// </summary>
491491
/// <param name="path">The path to the working folder when initializing a standard ".git" repository. Otherwise, when initializing a bare repository, the path to the expected location of this later.</param>
492492
/// <param name="isBare">true to initialize a bare repository. False otherwise, to initialize a standard ".git" repository.</param>
493+
/// <param name="options">Additional optional parameters to be passed to the Init invocation</param>
493494
/// <returns>The path to the created repository.</returns>
494-
public static string Init(string path, bool isBare)
495+
public static string Init(string path, bool isBare, InitOptions options = null)
495496
{
496497
Ensure.ArgumentNotNullOrEmptyString(path, "path");
497498

498-
using (RepositoryHandle repo = Proxy.git_repository_init_ext(null, path, isBare))
499+
using (RepositoryHandle repo = Proxy.git_repository_init_ext(null, path, isBare, options?.InitialHead))
499500
{
500501
FilePath repoPath = Proxy.git_repository_path(repo);
501502
return repoPath.Native;
@@ -507,8 +508,9 @@ public static string Init(string path, bool isBare)
507508
/// </summary>
508509
/// <param name="workingDirectoryPath">The path to the working directory.</param>
509510
/// <param name="gitDirectoryPath">The path to the git repository to be created.</param>
511+
/// <param name="options">Additional optional parameters to be passed to the Init invocation</param>
510512
/// <returns>The path to the created repository.</returns>
511-
public static string Init(string workingDirectoryPath, string gitDirectoryPath)
513+
public static string Init(string workingDirectoryPath, string gitDirectoryPath, InitOptions options = null)
512514
{
513515
Ensure.ArgumentNotNullOrEmptyString(workingDirectoryPath, "workingDirectoryPath");
514516
Ensure.ArgumentNotNullOrEmptyString(gitDirectoryPath, "gitDirectoryPath");
@@ -520,7 +522,7 @@ public static string Init(string workingDirectoryPath, string gitDirectoryPath)
520522

521523
// TODO: Shouldn't we ensure that the working folder isn't under the gitDir?
522524

523-
using (RepositoryHandle repo = Proxy.git_repository_init_ext(wd, gitDirectoryPath, false))
525+
using (RepositoryHandle repo = Proxy.git_repository_init_ext(wd, gitDirectoryPath, false, options?.InitialHead))
524526
{
525527
FilePath repoPath = Proxy.git_repository_path(repo);
526528
return repoPath.Native;
@@ -1050,7 +1052,7 @@ public Commit Commit(string message, Signature author, Signature committer, Comm
10501052

10511053
if (treesame && !amendMergeCommit)
10521054
{
1053-
throw (options.AmendPreviousCommit ?
1055+
throw (options.AmendPreviousCommit ?
10541056
new EmptyCommitException("Amending this commit would produce a commit that is identical to its parent (id = {0})", parents[0].Id) :
10551057
new EmptyCommitException("No changes; nothing to commit."));
10561058
}
@@ -1241,7 +1243,7 @@ public MergeResult MergeFetchedRefs(Signature merger, MergeOptions options)
12411243
if (fetchHeads.Length == 0)
12421244
{
12431245
var expectedRef = this.Head.UpstreamBranchCanonicalName;
1244-
throw new MergeFetchHeadNotFoundException("The current branch is configured to merge with the reference '{0}' from the remote, but this reference was not fetched.",
1246+
throw new MergeFetchHeadNotFoundException("The current branch is configured to merge with the reference '{0}' from the remote, but this reference was not fetched.",
12451247
expectedRef);
12461248
}
12471249

0 commit comments

Comments
 (0)