Skip to content

Commit

Permalink
Make Repository.Init() return an instance of the Repository type, ins…
Browse files Browse the repository at this point in the history
…tead of a string containing the path of the repository

This instance has to be released by a call to Dispose() or through usage of the using() statement.
  • Loading branch information
uluhonolulu authored and nulltoken committed Jan 29, 2012
1 parent 6fea5f0 commit c3eb715
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 52 deletions.
21 changes: 11 additions & 10 deletions LibGit2Sharp.Tests/CommitFixture.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -431,12 +431,13 @@ public void DirectlyAccessingAnUnknownTreeEntryOfTheCommitReturnsNull()
public void CanCommitWithSignatureFromConfig() public void CanCommitWithSignatureFromConfig()
{ {
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath);
Path.IsPathRooted(dir).ShouldBeTrue();
Directory.Exists(dir).ShouldBeTrue();


using (var repo = new Repository(dir)) using (var repo = Repository.Init(scd.DirectoryPath))
{ {
string dir = repo.Info.Path;
Path.IsPathRooted(dir).ShouldBeTrue();
Directory.Exists(dir).ShouldBeTrue();

InconclusiveIf(() => !repo.Config.HasGlobalConfig, "No Git global configuration available"); InconclusiveIf(() => !repo.Config.HasGlobalConfig, "No Git global configuration available");


const string relativeFilepath = "new.txt"; const string relativeFilepath = "new.txt";
Expand Down Expand Up @@ -467,12 +468,13 @@ public void CanCommitWithSignatureFromConfig()
public void CanCommitALittleBit() public void CanCommitALittleBit()
{ {
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath);
Path.IsPathRooted(dir).ShouldBeTrue();
Directory.Exists(dir).ShouldBeTrue();


using (var repo = new Repository(dir)) using (var repo = Repository.Init(scd.DirectoryPath))
{ {
string dir = repo.Info.Path;
Path.IsPathRooted(dir).ShouldBeTrue();
Directory.Exists(dir).ShouldBeTrue();

const string relativeFilepath = "new.txt"; const string relativeFilepath = "new.txt";
string filePath = Path.Combine(repo.Info.WorkingDirectory, relativeFilepath); string filePath = Path.Combine(repo.Info.WorkingDirectory, relativeFilepath);


Expand Down Expand Up @@ -534,9 +536,8 @@ private static void AssertBlobContent(TreeEntry entry, string expectedContent)
public void CanGeneratePredictableObjectShas() public void CanGeneratePredictableObjectShas()
{ {
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath);


using (var repo = new Repository(dir)) using (var repo = Repository.Init(scd.DirectoryPath))
{ {
const string relativeFilepath = "test.txt"; const string relativeFilepath = "test.txt";
string filePath = Path.Combine(repo.Info.WorkingDirectory, relativeFilepath); string filePath = Path.Combine(repo.Info.WorkingDirectory, relativeFilepath);
Expand Down
14 changes: 5 additions & 9 deletions LibGit2Sharp.Tests/IndexFixture.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -352,9 +352,8 @@ public void UnstagingFileWithBadParamsThrows()
public void CanRenameAFile() public void CanRenameAFile()
{ {
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath);


using (var repo = new Repository(dir)) using (var repo = Repository.Init(scd.DirectoryPath))
{ {
repo.Index.Count.ShouldEqual(0); repo.Index.Count.ShouldEqual(0);


Expand Down Expand Up @@ -556,9 +555,8 @@ public void CanRetrieveTheStatusOfTheWholeWorkingDirectory()
public void CanRetrieveTheStatusOfANewRepository() public void CanRetrieveTheStatusOfANewRepository()
{ {
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath);


using (var repo = new Repository(dir)) using (var repo = Repository.Init(scd.DirectoryPath))
{ {
RepositoryStatus status = repo.Index.RetrieveStatus(); RepositoryStatus status = repo.Index.RetrieveStatus();
status.ShouldNotBeNull(); status.ShouldNotBeNull();
Expand All @@ -579,7 +577,6 @@ public void RetrievingTheStatusOfARepositoryReturnNativeFilePaths()
{ {
// Initialize a new repository // Initialize a new repository
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath);


const string directoryName = "directory"; const string directoryName = "directory";
const string fileName = "Testfile.txt"; const string fileName = "Testfile.txt";
Expand All @@ -592,7 +589,7 @@ public void RetrievingTheStatusOfARepositoryReturnNativeFilePaths()
File.WriteAllText(filePath, "Anybody out there?"); File.WriteAllText(filePath, "Anybody out there?");


// Open the repository // Open the repository
using (var repo = new Repository(dir)) using (var repo = Repository.Init(scd.DirectoryPath))
{ {
// Add the file to the index // Add the file to the index
repo.Index.Stage(filePath); repo.Index.Stage(filePath);
Expand All @@ -615,7 +612,6 @@ public void PathsOfIndexEntriesAreExpressedInNativeFormat()
{ {
// Initialize a new repository // Initialize a new repository
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath);


const string directoryName = "directory"; const string directoryName = "directory";
const string fileName = "Testfile.txt"; const string fileName = "Testfile.txt";
Expand All @@ -627,8 +623,8 @@ public void PathsOfIndexEntriesAreExpressedInNativeFormat()
Directory.CreateDirectory(directoryPath); Directory.CreateDirectory(directoryPath);
File.WriteAllText(filePath, "Anybody out there?"); File.WriteAllText(filePath, "Anybody out there?");


// Open the repository // Initialize the repository
using (var repo = new Repository(dir)) using (var repo = Repository.Init(scd.DirectoryPath))
{ {
// Stage the file // Stage the file
repo.Index.Stage(filePath); repo.Index.Stage(filePath);
Expand Down
35 changes: 18 additions & 17 deletions LibGit2Sharp.Tests/RepositoryFixture.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class RepositoryFixture : BaseFixture
public void CanCreateBareRepo() public void CanCreateBareRepo()
{ {
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath, true); using (var repo = Repository.Init(scd.DirectoryPath, true))
Path.IsPathRooted(dir).ShouldBeTrue();
Directory.Exists(dir).ShouldBeTrue();
CheckGitConfigFile(dir);

using (var repo = new Repository(dir))
{ {
string dir = repo.Info.Path;
Path.IsPathRooted(dir).ShouldBeTrue();
Directory.Exists(dir).ShouldBeTrue();
CheckGitConfigFile(dir);

repo.Info.WorkingDirectory.ShouldBeNull(); repo.Info.WorkingDirectory.ShouldBeNull();
repo.Info.Path.ShouldEqual(scd.RootedDirectoryPath + Path.DirectorySeparatorChar); repo.Info.Path.ShouldEqual(scd.RootedDirectoryPath + Path.DirectorySeparatorChar);
repo.Info.IsBare.ShouldBeTrue(); repo.Info.IsBare.ShouldBeTrue();
Expand All @@ -34,13 +34,14 @@ public void CanCreateBareRepo()
public void CanCreateStandardRepo() public void CanCreateStandardRepo()
{ {
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath);
Path.IsPathRooted(dir).ShouldBeTrue();
Directory.Exists(dir).ShouldBeTrue();
CheckGitConfigFile(dir);


using (var repo = new Repository(dir)) using (var repo = Repository.Init(scd.DirectoryPath))
{ {
string dir = repo.Info.Path;
Path.IsPathRooted(dir).ShouldBeTrue();
Directory.Exists(dir).ShouldBeTrue();
CheckGitConfigFile(dir);

repo.Info.WorkingDirectory.ShouldNotBeNull(); repo.Info.WorkingDirectory.ShouldNotBeNull();
repo.Info.Path.ShouldEqual(Path.Combine(scd.RootedDirectoryPath, ".git" + Path.DirectorySeparatorChar)); repo.Info.Path.ShouldEqual(Path.Combine(scd.RootedDirectoryPath, ".git" + Path.DirectorySeparatorChar));
repo.Info.IsBare.ShouldBeFalse(); repo.Info.IsBare.ShouldBeFalse();
Expand Down Expand Up @@ -72,10 +73,11 @@ public void CanReinitARepository()
{ {
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();


string dir = Repository.Init(scd.DirectoryPath); using (Repository repository = Repository.Init(scd.DirectoryPath))
string dir2 = Repository.Init(scd.DirectoryPath); using (Repository repository2 = Repository.Init(scd.DirectoryPath))

{
dir.ShouldEqual(dir2); repository.Info.Path.ShouldEqual(repository2.Info.Path);
}
} }


[Test] [Test]
Expand Down Expand Up @@ -242,9 +244,8 @@ public void CanLookupWhithShortIdentifers()
const string expectedSha = expectedAbbrevSha + "02d96c9dbf64f6e238c45ddcfa762eef0"; const string expectedSha = expectedAbbrevSha + "02d96c9dbf64f6e238c45ddcfa762eef0";


SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath);


using (var repo = new Repository(dir)) using (var repo = Repository.Init(scd.DirectoryPath))
{ {
string filePath = Path.Combine(repo.Info.WorkingDirectory, "new.txt"); string filePath = Path.Combine(repo.Info.WorkingDirectory, "new.txt");


Expand Down
9 changes: 3 additions & 6 deletions LibGit2Sharp.Tests/ResetFixture.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ public class ResetFixture : BaseFixture
public void ResetANewlyInitializedRepositoryThrows(bool isBare) public void ResetANewlyInitializedRepositoryThrows(bool isBare)
{ {
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath, isBare);


using (var repo = new Repository(dir)) using (var repo = Repository.Init(scd.DirectoryPath, isBare))
{ {
Assert.Throws<LibGit2Exception>(() => repo.Reset(ResetOptions.Soft, repo.Head.CanonicalName)); Assert.Throws<LibGit2Exception>(() => repo.Reset(ResetOptions.Soft, repo.Head.CanonicalName));
} }
Expand Down Expand Up @@ -75,9 +74,8 @@ public void SoftResetSetsTheDetachedHeadToTheSpecifiedCommit()
private void AssertSoftReset(Func<Branch, string> branchIdentifierRetriever, bool shouldHeadBeDetached, Func<Branch, string> expectedHeadNameRetriever) private void AssertSoftReset(Func<Branch, string> branchIdentifierRetriever, bool shouldHeadBeDetached, Func<Branch, string> expectedHeadNameRetriever)
{ {
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath);


using (var repo = new Repository(dir)) using (var repo = Repository.Init(scd.DirectoryPath))
{ {
FeedTheRepository(repo); FeedTheRepository(repo);


Expand Down Expand Up @@ -132,9 +130,8 @@ private static void FeedTheRepository(Repository repo)
public void MixedResetRefreshesTheIndex() public void MixedResetRefreshesTheIndex()
{ {
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath);


using (var repo = new Repository(dir)) using (var repo = Repository.Init(scd.DirectoryPath))
{ {
FeedTheRepository(repo); FeedTheRepository(repo);


Expand Down
9 changes: 3 additions & 6 deletions LibGit2Sharp.Tests/TagFixture.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -173,9 +173,8 @@ public void CreatingAnAnnotatedTagIsDeterministic()
public void CreatingATagInAEmptyRepositoryThrows() public void CreatingATagInAEmptyRepositoryThrows()
{ {
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath);


using (var repo = new Repository(dir)) using (var repo = Repository.Init(scd.DirectoryPath))
{ {
Assert.Throws<LibGit2Exception>(() => repo.ApplyTag("mynotag")); Assert.Throws<LibGit2Exception>(() => repo.ApplyTag("mynotag"));
} }
Expand All @@ -186,9 +185,8 @@ public void CreatingATagInAEmptyRepositoryThrows()
public void CreatingATagForHeadInAEmptyRepositoryThrows() public void CreatingATagForHeadInAEmptyRepositoryThrows()
{ {
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath);


using (var repo = new Repository(dir)) using (var repo = Repository.Init(scd.DirectoryPath))
{ {
Assert.Throws<LibGit2Exception>(() => repo.ApplyTag("mytaghead", "HEAD")); Assert.Throws<LibGit2Exception>(() => repo.ApplyTag("mytaghead", "HEAD"));
} }
Expand Down Expand Up @@ -541,9 +539,8 @@ public void CanListTags()
public void CanListAllTagsInAEmptyRepository() public void CanListAllTagsInAEmptyRepository()
{ {
SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
string dir = Repository.Init(scd.DirectoryPath);


using (var repo = new Repository(dir)) using (var repo = Repository.Init(scd.DirectoryPath))
{ {
repo.Info.IsEmpty.ShouldBeTrue(); repo.Info.IsEmpty.ShouldBeTrue();
repo.Tags.Count().ShouldEqual(0); repo.Tags.Count().ShouldEqual(0);
Expand Down
8 changes: 4 additions & 4 deletions LibGit2Sharp/Repository.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ protected virtual void Dispose(bool disposing)
#endregion #endregion


/// <summary> /// <summary>
/// Init a repo at the specified <paramref name = "path" />. /// Initialize a repository at the specified <paramref name = "path" />.
/// </summary> /// </summary>
/// <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> /// <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>
/// <param name = "isBare">true to initialize a bare repository. False otherwise, to initialize a standard ".git" repository.</param> /// <param name = "isBare">true to initialize a bare repository. False otherwise, to initialize a standard ".git" repository.</param>
/// <returns>Path the git repository.</returns> /// <returns> a new instance of the <see cref = "Repository" /> class. The client code is responsible for calling <see cref="Dispose"/> on this instance.</returns>
public static string Init(string path, bool isBare = false) public static Repository Init(string path, bool isBare = false)
{ {
Ensure.ArgumentNotNullOrEmptyString(path, "path"); Ensure.ArgumentNotNullOrEmptyString(path, "path");


Expand All @@ -182,7 +182,7 @@ public static string Init(string path, bool isBare = false)


string nativePath = PosixPathHelper.ToNative(normalizedPath); string nativePath = PosixPathHelper.ToNative(normalizedPath);


return nativePath; return new Repository(nativePath);
} }


/// <summary> /// <summary>
Expand Down

0 comments on commit c3eb715

Please sign in to comment.