Skip to content

Make repo.Init() accept a separate git dir #453

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 “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

Merged
merged 3 commits into from
Jun 25, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion LibGit2Sharp.Tests/BranchFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,10 @@ public void TrackedBranchExistsFromDefaultConfigInEmptyClone()
}

SelfCleaningDirectory scd2 = BuildSelfCleaningDirectory();
using (Repository repo = Repository.Clone(uri.AbsoluteUri, scd2.RootedDirectoryPath))

string clonedRepoPath = Repository.Clone(uri.AbsoluteUri, scd2.DirectoryPath);

using (var repo = new Repository(clonedRepoPath))
{
Assert.Empty(Directory.GetFiles(scd2.RootedDirectoryPath));
Assert.Equal(repo.Head.Name, "master");
Expand Down
9 changes: 5 additions & 4 deletions LibGit2Sharp.Tests/CheckoutFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@ public void CheckingOutWithMergeConflictsThrows()
[Fact]
public void CanCancelCheckoutThroughNotifyCallback()
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string repoPath = InitNewRepository();

using (var repo = Repository.Init(scd.DirectoryPath))
using (var repo = new Repository(repoPath))
{
string relativePath = "a.txt";
Touch(repo.Info.WorkingDirectory, relativePath, "Hello\n");
Expand Down Expand Up @@ -432,8 +432,9 @@ public void CheckingOutCallsCheckoutNotify(CheckoutNotifyFlags notifyFlags, stri
expectedNotificationPath = expectedNotificationPath + Path.DirectorySeparatorChar;
}

SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
using (var repo = Repository.Init(scd.DirectoryPath))
string repoPath = InitNewRepository();

using (var repo = new Repository(repoPath))
{
PopulateBasicRepository(repo);

Expand Down
45 changes: 30 additions & 15 deletions LibGit2Sharp.Tests/CloneFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public class CloneFixture : BaseFixture
public void CanClone(string url)
{
var scd = BuildSelfCleaningDirectory();
using (Repository repo = Repository.Clone(url, scd.RootedDirectoryPath))

string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath);

using (var repo = new Repository(clonedRepoPath))
{
string dir = repo.Info.Path;
Assert.True(Path.IsPathRooted(dir));
Expand All @@ -36,7 +39,10 @@ public void CanClone(string url)
private void AssertLocalClone(string path)
{
var scd = BuildSelfCleaningDirectory();
using (Repository clonedRepo = Repository.Clone(path, scd.RootedDirectoryPath))

string clonedRepoPath = Repository.Clone(path, scd.DirectoryPath);

using (var clonedRepo = new Repository(clonedRepoPath))
using (var originalRepo = new Repository(BareTestRepoPath))
{
Assert.NotEqual(originalRepo.Info.Path, clonedRepo.Info.Path);
Expand Down Expand Up @@ -71,7 +77,10 @@ public void CanCloneALocalRepositoryFromAStandardPath()
public void CanCloneBarely(string url)
{
var scd = BuildSelfCleaningDirectory();
using (Repository repo = Repository.Clone(url, scd.RootedDirectoryPath, bare: true))

string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath, bare: true);

using (var repo = new Repository(clonedRepoPath))
{
string dir = repo.Info.Path;
Assert.True(Path.IsPathRooted(dir));
Expand All @@ -88,9 +97,12 @@ public void CanCloneBarely(string url)
public void WontCheckoutIfAskedNotTo(string url)
{
var scd = BuildSelfCleaningDirectory();
using (Repository repo = Repository.Clone(url, scd.RootedDirectoryPath, checkout: false))

string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath, checkout: false);

using (var repo = new Repository(clonedRepoPath))
{
Assert.False(File.Exists(Path.Combine(scd.RootedDirectoryPath, "master.txt")));
Assert.False(File.Exists(Path.Combine(repo.Info.WorkingDirectory, "master.txt")));
}
}

Expand All @@ -102,13 +114,13 @@ public void CallsProgressCallbacks(string url)
bool checkoutWasCalled = false;

var scd = BuildSelfCleaningDirectory();
using (Repository repo = Repository.Clone(url, scd.RootedDirectoryPath,
onTransferProgress: (_) => { transferWasCalled = true; return 0; },
onCheckoutProgress: (a, b, c) => checkoutWasCalled = true))
{
Assert.True(transferWasCalled);
Assert.True(checkoutWasCalled);
}

Repository.Clone(url, scd.DirectoryPath,
onTransferProgress: _ => { transferWasCalled = true; return 0; },
onCheckoutProgress: (a, b, c) => checkoutWasCalled = true);

Assert.True(transferWasCalled);
Assert.True(checkoutWasCalled);
}

[SkippableFact]
Expand All @@ -118,13 +130,16 @@ public void CanCloneWithCredentials()
"Populate Constants.PrivateRepo* to run this test");

var scd = BuildSelfCleaningDirectory();
using (Repository repo = Repository.Clone(
Constants.PrivateRepoUrl, scd.RootedDirectoryPath,

string clonedRepoPath = Repository.Clone(Constants.PrivateRepoUrl, scd.DirectoryPath,
credentials: new Credentials
{
Username = Constants.PrivateRepoUsername,
Password = Constants.PrivateRepoPassword
}))
});


using (var repo = new Repository(clonedRepoPath))
{
string dir = repo.Info.Path;
Assert.True(Path.IsPathRooted(dir));
Expand Down
5 changes: 4 additions & 1 deletion LibGit2Sharp.Tests/FetchHeadFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ private class FetchHeadExpected
public void CanIterateFetchHead(string url)
{
var scd = BuildSelfCleaningDirectory();
using (var repo = Repository.Clone(url, scd.RootedDirectoryPath))

string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath);

using (var repo = new Repository(clonedRepoPath))
{
repo.Reset(ResetOptions.Hard, "HEAD~2");

Expand Down
11 changes: 7 additions & 4 deletions LibGit2Sharp.Tests/NetworkFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ public void CanListRemoteReferences(string url)
[Fact]
public void CanListRemoteReferenceObjects()
{
string url = "http://github.com/libgit2/TestGitRepository";
string remoteName = "origin";
const string url = "http://github.com/libgit2/TestGitRepository";
const string remoteName = "origin";

var scd = BuildSelfCleaningDirectory();
using (Repository repo = Repository.Clone(url, scd.RootedDirectoryPath))

string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath);

using (var repo = new Repository(clonedRepoPath))
{
Remote remote = repo.Network.Remotes[remoteName];
IEnumerable<DirectReference> references = repo.Network.ListReferences(remote);

List<Tuple<string, string>> actualRefs = new List<Tuple<string,string>>();
var actualRefs = new List<Tuple<string,string>>();

foreach(DirectReference reference in references)
{
Expand Down
8 changes: 6 additions & 2 deletions LibGit2Sharp.Tests/PushFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ private void OnPushStatusError(PushStatusError pushStatusErrors)
private void AssertPush(Action<Repository> push)
{
var scd = BuildSelfCleaningDirectory();
using (var originalRepo = new Repository(CloneBareTestRepo()))
using (Repository clonedRepo = Repository.Clone(originalRepo.Info.Path, scd.RootedDirectoryPath))

string originalRepoPath = CloneBareTestRepo();
string clonedRepoPath = Repository.Clone(originalRepoPath, scd.DirectoryPath);

using (var originalRepo = new Repository(originalRepoPath))
using (var clonedRepo = new Repository(clonedRepoPath))
{
Remote remote = clonedRepo.Network.Remotes["origin"];

Expand Down
56 changes: 54 additions & 2 deletions LibGit2Sharp.Tests/RepositoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,54 @@ public void CanCreateStandardRepo()
}
}

[Fact]
public void CanCreateStandardRepoAndSpecifyAFolderWhichWillContainTheNewlyCreatedGitDirectory()
{
var scd1 = BuildSelfCleaningDirectory();
var scd2 = BuildSelfCleaningDirectory();

string repoPath = Repository.Init(scd1.DirectoryPath, scd2.DirectoryPath);

Assert.True(Repository.IsValid(repoPath));

using (var repo = new Repository(repoPath))
{
Assert.True(Repository.IsValid(repo.Info.WorkingDirectory));
Assert.True(Repository.IsValid(repo.Info.Path));

Assert.Equal(false, repo.Info.IsBare);

char sep = Path.DirectorySeparatorChar;
Assert.Equal(scd1.RootedDirectoryPath + sep, repo.Info.WorkingDirectory);
Assert.Equal(scd2.RootedDirectoryPath + sep + ".git" + sep, repo.Info.Path);
}
}

[Fact]
public void CanCreateStandardRepoAndDirectlySpecifyAGitDirectory()
{
var scd1 = BuildSelfCleaningDirectory();
var scd2 = BuildSelfCleaningDirectory();

var gitDir = Path.Combine(scd2.DirectoryPath, ".git/");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it matters, but what happens if we pass ".git" at the end, without the trailing slash?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it matters, but what happens if we pass ".git" at the end, without the trailing slash?

@yorah Could you submit a PR with some additional test coverage regarding this?


string repoPath = Repository.Init(scd1.DirectoryPath, gitDir);

Assert.True(Repository.IsValid(repoPath));

using (var repo = new Repository(repoPath))
{
Assert.True(Repository.IsValid(repo.Info.WorkingDirectory));
Assert.True(Repository.IsValid(repo.Info.Path));

Assert.Equal(false, repo.Info.IsBare);

char sep = Path.DirectorySeparatorChar;
Assert.Equal(scd1.RootedDirectoryPath + sep, repo.Info.WorkingDirectory);
Assert.Equal(Path.GetFullPath(gitDir), repo.Info.Path);
}
}

private static void CheckGitConfigFile(string dir)
{
string configFilePath = Path.Combine(dir, "config");
Expand Down Expand Up @@ -155,9 +203,13 @@ public void CanReinitARepository()
string repoPath = InitNewRepository();

using (var repository = new Repository(repoPath))
using (var repository2 = new Repository(repoPath))
{
Assert.Equal(repository2.Info.Path, repository.Info.Path);
string repoPath2 = Repository.Init(repoPath, false);

using (var repository2 = new Repository(repoPath2))
{
Assert.Equal(repository2.Info.Path, repository.Info.Path);
}
}
}

Expand Down
67 changes: 0 additions & 67 deletions LibGit2Sharp.Tests/RepositoryOptionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,72 +174,5 @@ public void CanProvideDifferentConfigurationFilesToARepository()

AssertValueInConfigFile(systemLocation, "xpaulbettsx");
}

[Fact]
public void CanProvideDifferentWorkingDirOnInit()
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
var options = new RepositoryOptions {WorkingDirectoryPath = newWorkdir};

using (var repo = Repository.Init(scd.DirectoryPath, false, options))
{
Assert.Equal(Path.GetFullPath(newWorkdir) + Path.DirectorySeparatorChar, repo.Info.WorkingDirectory);
}
}

[Fact]
public void CanProvideDifferentConfigurationFilesOnInit()
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
var options = BuildFakeConfigs(scd);

using (var repo = Repository.Init(scd.DirectoryPath, false, options))
{
Assert.True(repo.Config.HasConfig(ConfigurationLevel.Global));
Assert.Equal("global", repo.Config.Get<string>("woot.this-rocks").Value);
Assert.Equal(42, repo.Config.Get<int>("wow.man-I-am-totally-global").Value);

Assert.True(repo.Config.HasConfig(ConfigurationLevel.Xdg));
Assert.Equal("xdg", repo.Config.Get<string>("woot.this-rocks", ConfigurationLevel.Xdg).Value);

Assert.True(repo.Config.HasConfig(ConfigurationLevel.System));
Assert.Equal("system", repo.Config.Get<string>("woot.this-rocks", ConfigurationLevel.System).Value);
}
}

[Fact]
public void CanProvideDifferentWorkingDirOnClone()
{
string url = "https://github.com/libgit2/TestGitRepository";
var scd = BuildSelfCleaningDirectory();
var options = new RepositoryOptions { WorkingDirectoryPath = newWorkdir };

using (var repo = Repository.Clone(url, scd.DirectoryPath, false, true, null, null, options))
{
Assert.Equal(Path.GetFullPath(newWorkdir) + Path.DirectorySeparatorChar, repo.Info.WorkingDirectory);
}
}

[Fact]
public void CanProvideDifferentConfigurationFilesOnClone()
{
string url = "https://github.com/libgit2/TestGitRepository";
var scd = BuildSelfCleaningDirectory();
var configScd = BuildSelfCleaningDirectory();
var options = BuildFakeConfigs(configScd);

using (var repo = Repository.Clone(url, scd.DirectoryPath, false, true, null, null, options))
{
Assert.True(repo.Config.HasConfig(ConfigurationLevel.Global));
Assert.Equal("global", repo.Config.Get<string>("woot.this-rocks").Value);
Assert.Equal(42, repo.Config.Get<int>("wow.man-I-am-totally-global").Value);

Assert.True(repo.Config.HasConfig(ConfigurationLevel.Xdg));
Assert.Equal("xdg", repo.Config.Get<string>("woot.this-rocks", ConfigurationLevel.Xdg).Value);

Assert.True(repo.Config.HasConfig(ConfigurationLevel.System));
Assert.Equal("system", repo.Config.Get<string>("woot.this-rocks", ConfigurationLevel.System).Value);
}
}
}
}
5 changes: 1 addition & 4 deletions LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,7 @@ protected string InitNewRepository(bool isBare = false)
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();

using (var repo = Repository.Init(scd.DirectoryPath, isBare, null))
{
return repo.Info.Path;
}
return Repository.Init(scd.DirectoryPath, isBare);
}

public void Register(string directoryPath)
Expand Down
64 changes: 64 additions & 0 deletions LibGit2Sharp/Core/GitRepositoryInitOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace LibGit2Sharp.Core
{
[StructLayout(LayoutKind.Sequential)]
internal class GitRepositoryInitOptions : IDisposable
{
public uint Version = 1;
public GitRepositoryInitFlags Flags;
public int Mode;
public IntPtr WorkDirPath;
public IntPtr Description;
public IntPtr TemplatePath;
public IntPtr InitialHead;
public IntPtr OriginUrl;

public static GitRepositoryInitOptions BuildFrom(FilePath workdirPath, bool isBare)
{
var opts = new GitRepositoryInitOptions
{
Flags = GitRepositoryInitFlags.GIT_REPOSITORY_INIT_MKPATH,
Mode = 0 /* GIT_REPOSITORY_INIT_SHARED_UMASK */
};

if (workdirPath != null)
{
Debug.Assert(!isBare);

opts.WorkDirPath = FilePathMarshaler.FromManaged(workdirPath);
}

if (isBare)
{
opts.Flags |= GitRepositoryInitFlags.GIT_REPOSITORY_INIT_BARE;
}

return opts;
}

public void Dispose()
{
if (WorkDirPath == IntPtr.Zero)
{
return;
}

Marshal.FreeHGlobal(WorkDirPath);
WorkDirPath = IntPtr.Zero;
}
}

[Flags]
internal enum GitRepositoryInitFlags
{
GIT_REPOSITORY_INIT_BARE = (1 << 0),
GIT_REPOSITORY_INIT_NO_REINIT = (1 << 1),
GIT_REPOSITORY_INIT_NO_DOTGIT_DIR = (1 << 2),
GIT_REPOSITORY_INIT_MKDIR = (1 << 3),
GIT_REPOSITORY_INIT_MKPATH = (1 << 4),
GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE = (1 << 5),
}
}
Loading