Skip to content

Commit 04dbd73

Browse files
committed
Throw a more meaningful exception when the working directory does not contain the .git folder. Fixes #853.
1 parent 9680883 commit 04dbd73

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/GitVersionCore.Tests/ExecuteCoreTests.cs

+13
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ public void ConfigChangeInvalidatesCache()
116116
});
117117
}
118118

119+
120+
[Test]
121+
public void WorkingDirectoryWithoutGit()
122+
{
123+
var versionAndBranchFinder = new ExecuteCore(fileSystem);
124+
125+
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
126+
{
127+
var exception = Assert.Throws<DirectoryNotFoundException>(() => versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, Environment.SystemDirectory, null));
128+
exception.Message.ShouldContain("Can't find the .git directory in");
129+
});
130+
}
131+
119132
string RepositoryScope(ExecuteCore executeCore = null, Action<EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
120133
{
121134
// Make sure GitVersion doesn't trigger build server mode when we are running the tests

src/GitVersionCore/GitPreparer.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ public string GetDotGitDirectory()
9797
if (IsDynamicGitRepository)
9898
return DynamicGitRepositoryPath;
9999

100-
var dotGitDirectory = Repository.Discover(targetPath).TrimEnd('/', '\\');
100+
var dotGitDirectory = Repository.Discover(targetPath);
101+
102+
if (String.IsNullOrEmpty(dotGitDirectory))
103+
throw new DirectoryNotFoundException("Can't find the .git directory in " + targetPath);
104+
105+
dotGitDirectory = dotGitDirectory.TrimEnd('/', '\\');
101106
if (string.IsNullOrEmpty(dotGitDirectory))
102107
throw new DirectoryNotFoundException("Can't find the .git directory in " + targetPath);
103108

src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using GitTools.Testing;
34
using NUnit.Framework;
45

@@ -65,4 +66,11 @@ public void InvalidWorkingDirectoryCrashesWithInformativeMessage()
6566
var results = GitVersionHelper.ExecuteIn("InvalidDirectory", null, isTeamCity : false, logToFile : false);
6667
results.Output.ShouldContain("InvalidDirectory");
6768
}
69+
70+
[Test]
71+
public void WorkingDirectoryWithoutGitFolderCrashesWithInformativeMessage()
72+
{
73+
var results = GitVersionHelper.ExecuteIn(Environment.SystemDirectory, null, isTeamCity: false, logToFile: false);
74+
results.Output.ShouldContain("Can't find the .git directory in");
75+
}
6876
}

0 commit comments

Comments
 (0)