Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Migration to replace GitPreparer by GitRepositoryFactory #25

Merged
merged 2 commits into from
Dec 4, 2015
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using Testing;

[TestFixture]
public class GitPreparerTests
public class GitRepositoryFactoryTests
{
const string DefaultBranchName = "master";
const string SpecificBranchName = "feature/foo";
Expand Down Expand Up @@ -41,16 +41,20 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
// Copy contents into working directory
File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));

var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new AuthenticationInfo(), false, tempDir);
gitPreparer.Initialise(false, branchName);
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();

gitPreparer.IsDynamicGitRepository.ShouldBe(true);
gitPreparer.DynamicGitRepositoryPath.ShouldBe(expectedDynamicRepoLocation + "\\.git");
var repositoryInfo = new RepositoryInfo
{
Url = fixture.RepositoryPath,
Branch = branchName
};

using (var repository = new Repository(dynamicRepositoryPath))
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
{
var currentBranch = repository.Head.CanonicalName;
dynamicRepositoryPath = gitRepository.DotGitDirectory;

gitRepository.IsDynamic.ShouldBe(true);
gitRepository.DotGitDirectory.ShouldBe(expectedDynamicRepoLocation + "\\.git");

var currentBranch = gitRepository.Repository.Head.CanonicalName;

currentBranch.ShouldEndWith(expectedBranchName);
}
Expand All @@ -59,8 +63,11 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
finally
{
Directory.Delete(tempDir, true);

if (dynamicRepositoryPath != null)
{
DeleteHelper.DeleteGitRepository(dynamicRepositoryPath);
}
}
}

Expand All @@ -79,26 +86,35 @@ public void UpdatesExistingDynamicRepository()
{
mainRepositoryFixture.Repository.MakeCommits(1);

var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new AuthenticationInfo(), false, tempDir);
gitPreparer.Initialise(false, "master");
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
var repositoryInfo = new RepositoryInfo
{
Url = mainRepositoryFixture.RepositoryPath,
Branch = "master"
};

using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
{
dynamicRepositoryPath = gitRepository.DotGitDirectory;
}

var newCommit = mainRepositoryFixture.Repository.MakeACommit();
gitPreparer.Initialise(false, "master");

using (var repository = new Repository(dynamicRepositoryPath))
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
{
mainRepositoryFixture.Repository.DumpGraph();
repository.DumpGraph();
repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
gitRepository.Repository.DumpGraph();
gitRepository.Repository.Commits.ShouldContain(c => c.Sha == newCommit.Sha);
}
}
}
finally
{
Directory.Delete(tempDir, true);

if (dynamicRepositoryPath != null)
{
DeleteHelper.DeleteGitRepository(dynamicRepositoryPath);
}
}
}

Expand All @@ -120,32 +136,46 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split('\\').Last());
Directory.CreateDirectory(expectedDynamicRepoLocation);

var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new AuthenticationInfo(), false, tempDir);
gitPreparer.Initialise(false, "master");
var repositoryInfo = new RepositoryInfo
{
Url = fixture.RepositoryPath,
Branch = "master"
};

gitPreparer.IsDynamicGitRepository.ShouldBe(true);
gitPreparer.DynamicGitRepositoryPath.ShouldBe(expectedDynamicRepoLocation + "_1\\.git");
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
{
gitRepository.IsDynamic.ShouldBe(true);
gitRepository.DotGitDirectory.ShouldBe(expectedDynamicRepoLocation + "_1\\.git");
}
}
}
finally
{
Directory.Delete(tempDir, true);
if (expectedDynamicRepoLocation != null)
{
Directory.Delete(expectedDynamicRepoLocation, true);
}

if (expectedDynamicRepoLocation != null)
{
DeleteHelper.DeleteGitRepository(expectedDynamicRepoLocation + "_1");
}
}
}

[Test]
public void WorksCorrectlyWithLocalRepository()
public void ThrowsExceptionWhenNotEnoughInfo()
{
var tempDir = Path.GetTempPath();
var gitPreparer = new GitPreparer(null, null, null, false, tempDir);
var dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();

dynamicRepositoryPath.ShouldBe(null);
gitPreparer.IsDynamicGitRepository.ShouldBe(false);
var repositoryInfo = new RepositoryInfo
{
Url = tempDir,
Branch = "master"
};

Should.Throw<Exception>(() => GitRepositoryFactory.CreateRepository(repositoryInfo));
}

[Test]
Expand All @@ -162,12 +192,21 @@ public void UsingDynamicRepositoryWithFeatureBranchWorks()
{
mainRepositoryFixture.Repository.MakeACommit();

var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new AuthenticationInfo(), false, tempDir);
gitPreparer.Initialise(true, "feature1");
var repositoryInfo = new RepositoryInfo
{
Url = mainRepositoryFixture.RepositoryPath,
Branch = "feature1"
};

mainRepositoryFixture.Repository.Checkout(mainRepositoryFixture.Repository.CreateBranch("feature1"));

Should.NotThrow(() => gitPreparer.Initialise(true, "feature1"));
Should.NotThrow(() =>
{
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
{
// this code shouldn't throw
}
});
}
}
finally
Expand All @@ -190,9 +229,19 @@ public void UsingDynamicRepositoryWithoutTargetBranchFails()
{
mainRepositoryFixture.Repository.MakeACommit();

var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new AuthenticationInfo(), false, tempDir);
var repositoryInfo = new RepositoryInfo
{
Url = mainRepositoryFixture.RepositoryPath,
Branch = null
};

Should.Throw<Exception>(() => gitPreparer.Initialise(true, null));
Should.Throw<Exception>(() =>
{
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
{
// this code shouldn't throw
}
});
}
}
finally
Expand All @@ -211,9 +260,19 @@ public void TestErrorThrownForInvalidRepository()

try
{
var gitPreparer = new GitPreparer("http://127.0.0.1/testrepo.git", null, new AuthenticationInfo(), false, tempDir);
var repositoryInfo = new RepositoryInfo
{
Url = "http://127.0.0.1/testrepo.git",
Branch = "master"
};

Should.Throw<Exception>(() => gitPreparer.Initialise(true, "master"));
Should.Throw<Exception>(() =>
{
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
{
// this code shouldn't throw
}
});
}
finally
{
Expand Down
10 changes: 5 additions & 5 deletions src/GitTools.Core.Tests/GitRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void NormalisationOfPullRequestsWithFetch()
using (var localFixture = fixture.CloneRepository())
{
localFixture.Checkout(commit.Sha);
GitRepository.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: string.Empty);
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: string.Empty);

var normalisedPullBranch = localFixture.Repository.Branches["pull/3/merge"];
normalisedPullBranch.ShouldNotBe(null);
Expand All @@ -43,7 +43,7 @@ public void NormalisationOfPullRequestsWithoutFetch()
using (var localFixture = fixture.CloneRepository())
{
localFixture.Checkout(commit.Sha);
GitRepository.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: true, currentBranch: "refs/pull/3/merge");
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: true, currentBranch: "refs/pull/3/merge");

var normalisedPullBranch = localFixture.Repository.Branches["pull/3/merge"];
normalisedPullBranch.ShouldNotBe(null);
Expand All @@ -65,7 +65,7 @@ public void UpdatesLocalBranchesWhen()
localFixture.Checkout("feature/foo");
// Advance remote
var advancedCommit = fixture.Repository.MakeACommit();
GitRepository.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: null);
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: null);

var normalisedBranch = localFixture.Repository.Branches["feature/foo"];
normalisedBranch.ShouldNotBe(null);
Expand All @@ -90,7 +90,7 @@ public void UpdatesCurrentBranch()
var advancedCommit = fixture.Repository.MakeACommit();
localFixture.Repository.Network.Fetch(localFixture.Repository.Network.Remotes["origin"]);
localFixture.Repository.Checkout(advancedCommit.Sha);
GitRepository.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: "ref/heads/develop");
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: "ref/heads/develop");

var normalisedBranch = localFixture.Repository.Branches["develop"];
normalisedBranch.ShouldNotBe(null);
Expand Down Expand Up @@ -121,7 +121,7 @@ public void ShouldNotChangeBranchWhenNormalizingTheDirectory()
fixture.Checkout("feature/foo");
fixture.Repository.MakeACommit();

GitRepository.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: null);
GitRepositoryHelper.NormalizeGitDirectory(localFixture.RepositoryPath, new AuthenticationInfo(), noFetch: false, currentBranch: null);

localFixture.Repository.Head.Tip.Sha.ShouldBe(lastCommitOnDevelop.Sha);
}
Expand Down
2 changes: 1 addition & 1 deletion src/GitTools.Core.Tests/GitTools.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<Compile Include="Git\Extensions\AuthenticationInfoExtensionsTests.cs" />
<Compile Include="Git\GitDirFinderTests.cs" />
<Compile Include="Git\GitHelperTests.cs" />
<Compile Include="Git\GitPreparerTests.cs" />
<Compile Include="Git\GitRepositoryFactoryTests.cs" />
<Compile Include="GlobalInitialization.cs" />
<Compile Include="ModuleInitializer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
125 changes: 125 additions & 0 deletions src/GitTools.Core/GitTools.Core.Shared/Diposable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
namespace GitTools
{
using System;
using Logging;

/// <summary>
/// Base class for disposable objects.
/// </summary>
public abstract class Disposable : IDisposable
{
#region Fields
private static readonly ILog Log = LogProvider.GetCurrentClassLogger();

private readonly object _syncRoot = new object();

private bool _disposing;
#endregion

#region Constructors
/// <summary>
/// Finalizes an instance of the <see cref="Disposable"/> class.
/// </summary>
~Disposable()
{
Dispose(false);
}
#endregion

#region Properties
private bool IsDisposed { get; set; }
#endregion

#region Methods
/// <summary>
/// Disposes this instance.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Checks whether the object is disposed. If so, it will throw the <see cref="ObjectDisposedException"/>.
/// </summary>
/// <exception cref="System.ObjectDisposedException">The object is disposed.</exception>
protected void CheckDisposed()
{
lock (_syncRoot)
{
if (IsDisposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
}
}

/// <summary>
/// Disposes the managed resources.
/// </summary>
protected virtual void DisposeManaged()
{
}

/// <summary>
/// Disposes the unmanaged resources.
/// </summary>
protected virtual void DisposeUnmanaged()
{
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="isDisposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
private void Dispose(bool isDisposing)
{
lock (_syncRoot)
{
if (!IsDisposed)
{
if (!_disposing)
{
_disposing = true;

if (isDisposing)
{
try
{
DisposeManaged();
}
catch (Exception ex)
{
//if (ex.IsCritical())
//{
// throw;
//}

Log.ErrorException("Error while disposing managed resources of '{0}'.", ex, GetType().FullName);
}
}

try
{
DisposeUnmanaged();
}
catch (Exception ex)
{
//if (ex.IsCritical())
//{
// throw;
//}

Log.ErrorException("Error while disposing unmanaged resources of '{0}'.", ex, GetType().FullName);
}

IsDisposed = true;
_disposing = false;
}
}
}
}
#endregion
}
}
Loading