Skip to content

Commit a636d32

Browse files
committed
Added GitVersionException to wrap LibGit2Sharp.NotFoundException when the repository might be a shallow clone
1 parent 896ea71 commit a636d32

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/GitVersionCore/GitVersionCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
<Compile Include="GitVersionCache.cs" />
119119
<Compile Include="GitVersionCacheKey.cs" />
120120
<Compile Include="GitVersionCacheKeyFactory.cs" />
121+
<Compile Include="GitVersionException.cs" />
121122
<Compile Include="Helpers\EncodingHelper.cs" />
122123
<Compile Include="Helpers\FileSystem.cs" />
123124
<Compile Include="Helpers\IFileSystem.cs" />
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace GitVersion
2+
{
3+
using System;
4+
using System.Runtime.Serialization;
5+
6+
[Serializable]
7+
public class GitVersionException : ApplicationException
8+
{
9+
public GitVersionException()
10+
{
11+
}
12+
13+
14+
public GitVersionException(string message)
15+
: base(message)
16+
{
17+
}
18+
19+
20+
public GitVersionException(string message, Exception innerException)
21+
: base(message, innerException)
22+
{
23+
}
24+
25+
26+
protected GitVersionException(SerializationInfo info, StreamingContext context)
27+
: base(info, context)
28+
{
29+
}
30+
}
31+
}

src/GitVersionCore/VersionCalculation/FallbackBaseVersionStrategy.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,22 @@ public class FallbackBaseVersionStrategy : BaseVersionStrategy
1414
{
1515
public override IEnumerable<BaseVersion> GetVersions(GitVersionContext context)
1616
{
17-
var baseVersionSource = context.Repository.Commits.QueryBy(new CommitFilter
17+
Commit baseVersionSource;
18+
var currentBranchTip = context.CurrentBranch.Tip;
19+
20+
try
21+
{
22+
baseVersionSource = context.Repository.Commits.QueryBy(new CommitFilter
23+
{
24+
IncludeReachableFrom = currentBranchTip
25+
}).First(c => !c.Parents.Any());
26+
}
27+
catch (NotFoundException exception)
1828
{
19-
IncludeReachableFrom = context.CurrentBranch.Tip
20-
}).First(c => !c.Parents.Any());
21-
yield return new BaseVersion(context, "Fallback base version", false, new SemanticVersion(minor: 1), baseVersionSource, null);
29+
throw new GitVersionException($"Can't find commit {currentBranchTip.Sha}. Please ensure that the repository is an unshallow clone with `git fetch --unshallow`.", exception);
30+
}
31+
32+
yield return new BaseVersion(context, "Fallback base version", false, new SemanticVersion(minor : 1), baseVersionSource, null);
2233
}
2334
}
2435
}

0 commit comments

Comments
 (0)