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

Commit 24c5835

Browse files
Using properties instead of methods on GitRepository
1 parent cef7cf5 commit 24c5835

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

src/GitTools.Core.Tests/Git/GitRepositoryFactoryTests.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
4949

5050
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
5151
{
52-
dynamicRepositoryPath = gitRepository.GetDotGitDirectory();
52+
dynamicRepositoryPath = gitRepository.DotGitDirectory;
5353

5454
gitRepository.IsDynamic.ShouldBe(true);
55-
gitRepository.GetDotGitDirectory().ShouldBe(expectedDynamicRepoLocation + "\\.git");
55+
gitRepository.DotGitDirectory.ShouldBe(expectedDynamicRepoLocation + "\\.git");
5656

5757
var currentBranch = gitRepository.Repository.Head.CanonicalName;
5858

@@ -94,7 +94,7 @@ public void UpdatesExistingDynamicRepository()
9494

9595
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
9696
{
97-
dynamicRepositoryPath = gitRepository.GetDotGitDirectory();
97+
dynamicRepositoryPath = gitRepository.DotGitDirectory;
9898
}
9999

100100
var newCommit = mainRepositoryFixture.Repository.MakeACommit();
@@ -145,7 +145,7 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
145145
using (var gitRepository = GitRepositoryFactory.CreateRepository(repositoryInfo))
146146
{
147147
gitRepository.IsDynamic.ShouldBe(true);
148-
gitRepository.GetDotGitDirectory().ShouldBe(expectedDynamicRepoLocation + "_1\\.git");
148+
gitRepository.DotGitDirectory.ShouldBe(expectedDynamicRepoLocation + "_1\\.git");
149149
}
150150
}
151151
}
@@ -164,6 +164,20 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
164164
}
165165
}
166166

167+
[Test]
168+
public void ThrowsExceptionWhenNotEnoughInfo()
169+
{
170+
var tempDir = Path.GetTempPath();
171+
172+
var repositoryInfo = new RepositoryInfo
173+
{
174+
Url = tempDir,
175+
Branch = "master"
176+
};
177+
178+
Should.Throw<Exception>(() => GitRepositoryFactory.CreateRepository(repositoryInfo));
179+
}
180+
167181
[Test]
168182
public void UsingDynamicRepositoryWithFeatureBranchWorks()
169183
{

src/GitTools.Core/GitTools.Core.Shared/Git/GitRepository.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,18 @@ public GitRepository(IRepository repository, bool isDynamic)
99
{
1010
Repository = repository;
1111
IsDynamic = isDynamic;
12+
13+
ProjectRootDirectory = repository.Info.WorkingDirectory;
14+
DotGitDirectory = Path.Combine(ProjectRootDirectory, ".git");
1215
}
1316

1417
public IRepository Repository { get; private set; }
1518

1619
public bool IsDynamic { get; private set; }
1720

18-
// TODO: Consider using properties
19-
public string GetDotGitDirectory()
20-
{
21-
var rootDirectory = GetProjectRootDirectory();
22-
var directory = Path.Combine(rootDirectory, ".git");
23-
return directory;
24-
}
21+
public string DotGitDirectory { get; private set; }
2522

26-
// TODO: Consider using properties
27-
public string GetProjectRootDirectory()
28-
{
29-
return Repository.Info.WorkingDirectory;
30-
}
23+
public string ProjectRootDirectory { get; private set; }
3124

3225
protected override void DisposeManaged()
3326
{

src/GitTools.Core/GitTools.Core.Shared/Git/GitRepositoryFactory.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ public static GitRepository CreateRepository(RepositoryInfo repositoryInfo, bool
2626
// TODO: find a better way to check for existing repositories
2727
if (!string.IsNullOrWhiteSpace(repositoryInfo.Directory))
2828
{
29-
repositoryDirectory = repositoryInfo.Directory;
29+
var expectedDirectory = Path.Combine(repositoryInfo.Directory, ".git");
30+
if (Directory.Exists(expectedDirectory))
31+
{
32+
repositoryDirectory = expectedDirectory;
33+
}
3034
}
31-
else
35+
36+
if (string.IsNullOrWhiteSpace(repositoryDirectory))
3237
{
3338
isDynamicRepository = true;
3439

@@ -37,6 +42,12 @@ public static GitRepository CreateRepository(RepositoryInfo repositoryInfo, bool
3742
repositoryInfo.Url, repositoryInfo.Branch, noFetch);
3843
}
3944

45+
if (string.IsNullOrWhiteSpace(repositoryDirectory))
46+
{
47+
Log.Warn("Could not create a repository, not enough information was specified");
48+
return null;
49+
}
50+
4051
// TODO: Should we do something with fetch for existing repositoriess?
4152

4253
var repository = new Repository(repositoryDirectory);

0 commit comments

Comments
 (0)