Permalink
Browse files

Make Repository.Init() return an instance of the Repository type, ins…

…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...
1 parent 6fea5f0 commit c3eb715377988e4a23b6a79af8085f4fc6017d15 @uluhonolulu committed with nulltoken Jan 29, 2012
@@ -431,12 +431,13 @@ public void DirectlyAccessingAnUnknownTreeEntryOfTheCommitReturnsNull()
public void CanCommitWithSignatureFromConfig()
{
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");
const string relativeFilepath = "new.txt";
@@ -467,12 +468,13 @@ public void CanCommitWithSignatureFromConfig()
public void CanCommitALittleBit()
{
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";
string filePath = Path.Combine(repo.Info.WorkingDirectory, relativeFilepath);
@@ -534,9 +536,8 @@ private static void AssertBlobContent(TreeEntry entry, string expectedContent)
public void CanGeneratePredictableObjectShas()
{
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";
string filePath = Path.Combine(repo.Info.WorkingDirectory, relativeFilepath);
@@ -352,9 +352,8 @@ public void UnstagingFileWithBadParamsThrows()
public void CanRenameAFile()
{
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);
@@ -556,9 +555,8 @@ public void CanRetrieveTheStatusOfTheWholeWorkingDirectory()
public void CanRetrieveTheStatusOfANewRepository()
{
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();
status.ShouldNotBeNull();
@@ -579,7 +577,6 @@ public void RetrievingTheStatusOfARepositoryReturnNativeFilePaths()
{
// Initialize a new repository
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
- string dir = Repository.Init(scd.DirectoryPath);
const string directoryName = "directory";
const string fileName = "Testfile.txt";
@@ -592,7 +589,7 @@ public void RetrievingTheStatusOfARepositoryReturnNativeFilePaths()
File.WriteAllText(filePath, "Anybody out there?");
// Open the repository
- using (var repo = new Repository(dir))
+ using (var repo = Repository.Init(scd.DirectoryPath))
{
// Add the file to the index
repo.Index.Stage(filePath);
@@ -615,7 +612,6 @@ public void PathsOfIndexEntriesAreExpressedInNativeFormat()
{
// Initialize a new repository
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
- string dir = Repository.Init(scd.DirectoryPath);
const string directoryName = "directory";
const string fileName = "Testfile.txt";
@@ -627,8 +623,8 @@ public void PathsOfIndexEntriesAreExpressedInNativeFormat()
Directory.CreateDirectory(directoryPath);
File.WriteAllText(filePath, "Anybody out there?");
- // Open the repository
- using (var repo = new Repository(dir))
+ // Initialize the repository
+ using (var repo = Repository.Init(scd.DirectoryPath))
{
// Stage the file
repo.Index.Stage(filePath);
@@ -15,13 +15,13 @@ public class RepositoryFixture : BaseFixture
public void CanCreateBareRepo()
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
- string dir = Repository.Init(scd.DirectoryPath, true);
- Path.IsPathRooted(dir).ShouldBeTrue();
- Directory.Exists(dir).ShouldBeTrue();
- CheckGitConfigFile(dir);
-
- using (var repo = new Repository(dir))
+ using (var repo = Repository.Init(scd.DirectoryPath, true))
{
+ string dir = repo.Info.Path;
+ Path.IsPathRooted(dir).ShouldBeTrue();
+ Directory.Exists(dir).ShouldBeTrue();
+ CheckGitConfigFile(dir);
+
repo.Info.WorkingDirectory.ShouldBeNull();
repo.Info.Path.ShouldEqual(scd.RootedDirectoryPath + Path.DirectorySeparatorChar);
repo.Info.IsBare.ShouldBeTrue();
@@ -34,13 +34,14 @@ public void CanCreateBareRepo()
public void CanCreateStandardRepo()
{
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.Path.ShouldEqual(Path.Combine(scd.RootedDirectoryPath, ".git" + Path.DirectorySeparatorChar));
repo.Info.IsBare.ShouldBeFalse();
@@ -72,10 +73,11 @@ public void CanReinitARepository()
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
- string dir = Repository.Init(scd.DirectoryPath);
- string dir2 = Repository.Init(scd.DirectoryPath);
-
- dir.ShouldEqual(dir2);
+ using (Repository repository = Repository.Init(scd.DirectoryPath))
+ using (Repository repository2 = Repository.Init(scd.DirectoryPath))
+ {
+ repository.Info.Path.ShouldEqual(repository2.Info.Path);
+ }
}
[Test]
@@ -242,9 +244,8 @@ public void CanLookupWhithShortIdentifers()
const string expectedSha = expectedAbbrevSha + "02d96c9dbf64f6e238c45ddcfa762eef0";
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");
@@ -13,9 +13,8 @@ public class ResetFixture : BaseFixture
public void ResetANewlyInitializedRepositoryThrows(bool isBare)
{
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));
}
@@ -75,9 +74,8 @@ public void SoftResetSetsTheDetachedHeadToTheSpecifiedCommit()
private void AssertSoftReset(Func<Branch, string> branchIdentifierRetriever, bool shouldHeadBeDetached, Func<Branch, string> expectedHeadNameRetriever)
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
- string dir = Repository.Init(scd.DirectoryPath);
- using (var repo = new Repository(dir))
+ using (var repo = Repository.Init(scd.DirectoryPath))
{
FeedTheRepository(repo);
@@ -132,9 +130,8 @@ private static void FeedTheRepository(Repository repo)
public void MixedResetRefreshesTheIndex()
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
- string dir = Repository.Init(scd.DirectoryPath);
- using (var repo = new Repository(dir))
+ using (var repo = Repository.Init(scd.DirectoryPath))
{
FeedTheRepository(repo);
@@ -173,9 +173,8 @@ public void CreatingAnAnnotatedTagIsDeterministic()
public void CreatingATagInAEmptyRepositoryThrows()
{
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"));
}
@@ -186,9 +185,8 @@ public void CreatingATagInAEmptyRepositoryThrows()
public void CreatingATagForHeadInAEmptyRepositoryThrows()
{
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"));
}
@@ -541,9 +539,8 @@ public void CanListTags()
public void CanListAllTagsInAEmptyRepository()
{
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.Tags.Count().ShouldEqual(0);
@@ -164,12 +164,12 @@ protected virtual void Dispose(bool disposing)
#endregion
/// <summary>
- /// Init a repo at the specified <paramref name = "path" />.
+ /// Initialize a repository at the specified <paramref name = "path" />.
/// </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 = "isBare">true to initialize a bare repository. False otherwise, to initialize a standard ".git" repository.</param>
- /// <returns>Path the git repository.</returns>
- public static string Init(string path, bool isBare = false)
+ /// <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 Repository Init(string path, bool isBare = false)
{
Ensure.ArgumentNotNullOrEmptyString(path, "path");
@@ -182,7 +182,7 @@ public static string Init(string path, bool isBare = false)
string nativePath = PosixPathHelper.ToNative(normalizedPath);
- return nativePath;
+ return new Repository(nativePath);
}
/// <summary>

0 comments on commit c3eb715

Please sign in to comment.