Skip to content

Commit 6512302

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

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#region License
2+
3+
// --------------------------------------------------
4+
// Copyright © PayEx. All Rights Reserved.
5+
//
6+
// This software is proprietary information of PayEx.
7+
// USE IS SUBJECT TO LICENSE TERMS.
8+
// --------------------------------------------------
9+
10+
#endregion
11+
12+
namespace GitVersion
13+
{
14+
using System;
15+
using System.Runtime.Serialization;
16+
17+
[Serializable]
18+
public class GitVersionException : ApplicationException
19+
{
20+
public GitVersionException()
21+
{
22+
}
23+
24+
25+
public GitVersionException(string message)
26+
: base(message)
27+
{
28+
}
29+
30+
31+
public GitVersionException(string message, Exception innerException)
32+
: base(message, innerException)
33+
{
34+
}
35+
36+
37+
protected GitVersionException(SerializationInfo info, StreamingContext context)
38+
: base(info, context)
39+
{
40+
}
41+
}
42+
}

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)