From a636d3271c00e47759eed5e027bd5c3f929360d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Tue, 5 Sep 2017 12:10:21 +0200 Subject: [PATCH 1/3] Added GitVersionException to wrap LibGit2Sharp.NotFoundException when the repository might be a shallow clone --- src/GitVersionCore/GitVersionCore.csproj | 1 + src/GitVersionCore/GitVersionException.cs | 31 +++++++++++++++++++ .../FallbackBaseVersionStrategy.cs | 19 +++++++++--- 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 src/GitVersionCore/GitVersionException.cs diff --git a/src/GitVersionCore/GitVersionCore.csproj b/src/GitVersionCore/GitVersionCore.csproj index 8131080017..0fd85a6b3f 100644 --- a/src/GitVersionCore/GitVersionCore.csproj +++ b/src/GitVersionCore/GitVersionCore.csproj @@ -118,6 +118,7 @@ + diff --git a/src/GitVersionCore/GitVersionException.cs b/src/GitVersionCore/GitVersionException.cs new file mode 100644 index 0000000000..9b73c1884e --- /dev/null +++ b/src/GitVersionCore/GitVersionException.cs @@ -0,0 +1,31 @@ +namespace GitVersion +{ + using System; + using System.Runtime.Serialization; + + [Serializable] + public class GitVersionException : ApplicationException + { + public GitVersionException() + { + } + + + public GitVersionException(string message) + : base(message) + { + } + + + public GitVersionException(string message, Exception innerException) + : base(message, innerException) + { + } + + + protected GitVersionException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} \ No newline at end of file diff --git a/src/GitVersionCore/VersionCalculation/FallbackBaseVersionStrategy.cs b/src/GitVersionCore/VersionCalculation/FallbackBaseVersionStrategy.cs index 85a2c966f1..a7087923d4 100644 --- a/src/GitVersionCore/VersionCalculation/FallbackBaseVersionStrategy.cs +++ b/src/GitVersionCore/VersionCalculation/FallbackBaseVersionStrategy.cs @@ -14,11 +14,22 @@ public class FallbackBaseVersionStrategy : BaseVersionStrategy { public override IEnumerable GetVersions(GitVersionContext context) { - var baseVersionSource = context.Repository.Commits.QueryBy(new CommitFilter + Commit baseVersionSource; + var currentBranchTip = context.CurrentBranch.Tip; + + try + { + baseVersionSource = context.Repository.Commits.QueryBy(new CommitFilter + { + IncludeReachableFrom = currentBranchTip + }).First(c => !c.Parents.Any()); + } + catch (NotFoundException exception) { - IncludeReachableFrom = context.CurrentBranch.Tip - }).First(c => !c.Parents.Any()); - yield return new BaseVersion(context, "Fallback base version", false, new SemanticVersion(minor: 1), baseVersionSource, null); + throw new GitVersionException($"Can't find commit {currentBranchTip.Sha}. Please ensure that the repository is an unshallow clone with `git fetch --unshallow`.", exception); + } + + yield return new BaseVersion(context, "Fallback base version", false, new SemanticVersion(minor : 1), baseVersionSource, null); } } } \ No newline at end of file From f906d9a7e0befcc4090b1abbd130a24a7ecff966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Tue, 5 Sep 2017 12:11:54 +0200 Subject: [PATCH 2/3] Made other exceptions inherit GitVersionException for consistency --- .../GitVersionConfigurationException.cs | 13 +++++++++++-- .../Configuration/OldConfigurationException.cs | 11 ++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/GitVersionCore/Configuration/GitVersionConfigurationException.cs b/src/GitVersionCore/Configuration/GitVersionConfigurationException.cs index 76cfb026e8..bf583e913c 100644 --- a/src/GitVersionCore/Configuration/GitVersionConfigurationException.cs +++ b/src/GitVersionCore/Configuration/GitVersionConfigurationException.cs @@ -1,10 +1,19 @@ namespace GitVersion { using System; + using System.Runtime.Serialization; - public class GitVersionConfigurationException : Exception + [Serializable] + public class GitVersionConfigurationException : GitVersionException { - public GitVersionConfigurationException(string msg) : base(msg) + public GitVersionConfigurationException(string msg) + : base(msg) + { + } + + + protected GitVersionConfigurationException(SerializationInfo info, StreamingContext context) + : base(info, context) { } } diff --git a/src/GitVersionCore/Configuration/OldConfigurationException.cs b/src/GitVersionCore/Configuration/OldConfigurationException.cs index 08603b4daf..776f4e15b8 100644 --- a/src/GitVersionCore/Configuration/OldConfigurationException.cs +++ b/src/GitVersionCore/Configuration/OldConfigurationException.cs @@ -4,15 +4,16 @@ namespace GitVersion using System.Runtime.Serialization; [Serializable] - public class OldConfigurationException : Exception + public class OldConfigurationException : GitVersionException { - public OldConfigurationException(string message) : base(message) + public OldConfigurationException(string message) + : base(message) { } - protected OldConfigurationException( - SerializationInfo info, - StreamingContext context) : base(info, context) + + protected OldConfigurationException(SerializationInfo info, StreamingContext context) + : base(info, context) { } } From a90ce421c45982f85f4aa0e86c22195421f0eeb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Ulsberg?= Date: Wed, 6 Sep 2017 11:42:17 +0200 Subject: [PATCH 3/3] Inherit GitVersionException from GitToolsException instead of ApplicationException --- .../GitVersionConfigurationException.cs | 7 ------- .../Configuration/OldConfigurationException.cs | 7 ------- src/GitVersionCore/GitVersionException.cs | 16 +++------------- 3 files changed, 3 insertions(+), 27 deletions(-) diff --git a/src/GitVersionCore/Configuration/GitVersionConfigurationException.cs b/src/GitVersionCore/Configuration/GitVersionConfigurationException.cs index bf583e913c..6b4387a9fd 100644 --- a/src/GitVersionCore/Configuration/GitVersionConfigurationException.cs +++ b/src/GitVersionCore/Configuration/GitVersionConfigurationException.cs @@ -1,7 +1,6 @@ namespace GitVersion { using System; - using System.Runtime.Serialization; [Serializable] public class GitVersionConfigurationException : GitVersionException @@ -10,11 +9,5 @@ public GitVersionConfigurationException(string msg) : base(msg) { } - - - protected GitVersionConfigurationException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } } \ No newline at end of file diff --git a/src/GitVersionCore/Configuration/OldConfigurationException.cs b/src/GitVersionCore/Configuration/OldConfigurationException.cs index 776f4e15b8..58a8ef2625 100644 --- a/src/GitVersionCore/Configuration/OldConfigurationException.cs +++ b/src/GitVersionCore/Configuration/OldConfigurationException.cs @@ -1,7 +1,6 @@ namespace GitVersion { using System; - using System.Runtime.Serialization; [Serializable] public class OldConfigurationException : GitVersionException @@ -10,11 +9,5 @@ public OldConfigurationException(string message) : base(message) { } - - - protected OldConfigurationException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } } \ No newline at end of file diff --git a/src/GitVersionCore/GitVersionException.cs b/src/GitVersionCore/GitVersionException.cs index 9b73c1884e..b6afb92952 100644 --- a/src/GitVersionCore/GitVersionException.cs +++ b/src/GitVersionCore/GitVersionException.cs @@ -1,16 +1,12 @@ namespace GitVersion { using System; - using System.Runtime.Serialization; + + using GitTools; [Serializable] - public class GitVersionException : ApplicationException + public class GitVersionException : GitToolsException { - public GitVersionException() - { - } - - public GitVersionException(string message) : base(message) { @@ -21,11 +17,5 @@ public GitVersionException(string message, Exception innerException) : base(message, innerException) { } - - - protected GitVersionException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } } \ No newline at end of file