Skip to content

Commit a08b0ad

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

File tree

5 files changed

+75
-9
lines changed

5 files changed

+75
-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

+31-6
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ private void Dispose(bool disposing)
482482
/// <returns>The path to the created repository.</returns>
483483
public static string Init(string path)
484484
{
485-
return Init(path, false);
485+
return Init(path, false, new InitOptions());
486486
}
487487

488488
/// <summary>
@@ -492,10 +492,34 @@ public static string Init(string path)
492492
/// <param name="isBare">true to initialize a bare repository. False otherwise, to initialize a standard ".git" repository.</param>
493493
/// <returns>The path to the created repository.</returns>
494494
public static string Init(string path, bool isBare)
495+
{
496+
return Init(path, isBare, new InitOptions());
497+
}
498+
499+
500+
/// <summary>
501+
/// Initialize a repository by explictly setting the path to both the working directory and the git directory.
502+
/// </summary>
503+
/// <param name="workingDirectoryPath">The path to the working directory.</param>
504+
/// <param name="gitDirectoryPath">The path to the git repository to be created.</param>
505+
/// <returns>The path to the created repository.</returns>
506+
public static string Init(string workingDirectoryPath, string gitDirectoryPath)
507+
{
508+
return Init(workingDirectoryPath, gitDirectoryPath, new InitOptions());
509+
}
510+
511+
/// <summary>
512+
/// Initialize a repository at the specified <paramref name="path"/>.
513+
/// </summary>
514+
/// <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>
515+
/// <param name="isBare">true to initialize a bare repository. False otherwise, to initialize a standard ".git" repository.</param>
516+
/// <param name="options">Additional optional parameters to be passed to the Init invocation</param>
517+
/// <returns>The path to the created repository.</returns>
518+
public static string Init(string path, bool isBare, InitOptions options)
495519
{
496520
Ensure.ArgumentNotNullOrEmptyString(path, "path");
497521

498-
using (RepositoryHandle repo = Proxy.git_repository_init_ext(null, path, isBare))
522+
using (RepositoryHandle repo = Proxy.git_repository_init_ext(null, path, isBare, options.InitialHead))
499523
{
500524
FilePath repoPath = Proxy.git_repository_path(repo);
501525
return repoPath.Native;
@@ -507,8 +531,9 @@ public static string Init(string path, bool isBare)
507531
/// </summary>
508532
/// <param name="workingDirectoryPath">The path to the working directory.</param>
509533
/// <param name="gitDirectoryPath">The path to the git repository to be created.</param>
534+
/// <param name="options">Additional optional parameters to be passed to the Init invocation</param>
510535
/// <returns>The path to the created repository.</returns>
511-
public static string Init(string workingDirectoryPath, string gitDirectoryPath)
536+
public static string Init(string workingDirectoryPath, string gitDirectoryPath, InitOptions options)
512537
{
513538
Ensure.ArgumentNotNullOrEmptyString(workingDirectoryPath, "workingDirectoryPath");
514539
Ensure.ArgumentNotNullOrEmptyString(gitDirectoryPath, "gitDirectoryPath");
@@ -520,7 +545,7 @@ public static string Init(string workingDirectoryPath, string gitDirectoryPath)
520545

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

523-
using (RepositoryHandle repo = Proxy.git_repository_init_ext(wd, gitDirectoryPath, false))
548+
using (RepositoryHandle repo = Proxy.git_repository_init_ext(wd, gitDirectoryPath, false, options.InitialHead))
524549
{
525550
FilePath repoPath = Proxy.git_repository_path(repo);
526551
return repoPath.Native;
@@ -1050,7 +1075,7 @@ public Commit Commit(string message, Signature author, Signature committer, Comm
10501075

10511076
if (treesame && !amendMergeCommit)
10521077
{
1053-
throw (options.AmendPreviousCommit ?
1078+
throw (options.AmendPreviousCommit ?
10541079
new EmptyCommitException("Amending this commit would produce a commit that is identical to its parent (id = {0})", parents[0].Id) :
10551080
new EmptyCommitException("No changes; nothing to commit."));
10561081
}
@@ -1241,7 +1266,7 @@ public MergeResult MergeFetchedRefs(Signature merger, MergeOptions options)
12411266
if (fetchHeads.Length == 0)
12421267
{
12431268
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.",
1269+
throw new MergeFetchHeadNotFoundException("The current branch is configured to merge with the reference '{0}' from the remote, but this reference was not fetched.",
12451270
expectedRef);
12461271
}
12471272

0 commit comments

Comments
 (0)