From b74ec71a32053ff66f993509ae6e090a2a1164be Mon Sep 17 00:00:00 2001 From: Markus Horstmann Date: Fri, 5 Feb 2021 14:16:04 -0800 Subject: [PATCH] Make VersionVariables.FromFile work with no logger. Preserve exception. --- .../OperationWithExponentialBackoff.cs | 1 - src/GitVersion.Core/Model/VersionVariables.cs | 22 ++++++++++++++++--- .../Git/GitRepository.cs | 12 +++------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/GitVersion.Core/Helpers/OperationWithExponentialBackoff.cs b/src/GitVersion.Core/Helpers/OperationWithExponentialBackoff.cs index c3a9ae4de8..e8e50e5e11 100644 --- a/src/GitVersion.Core/Helpers/OperationWithExponentialBackoff.cs +++ b/src/GitVersion.Core/Helpers/OperationWithExponentialBackoff.cs @@ -16,7 +16,6 @@ public new Task ExecuteAsync() { return base.ExecuteAsync(); } - } public class OperationWithExponentialBackoff where T : Exception { diff --git a/src/GitVersion.Core/Model/VersionVariables.cs b/src/GitVersion.Core/Model/VersionVariables.cs index 951de1a4d5..18b257b38f 100644 --- a/src/GitVersion.Core/Model/VersionVariables.cs +++ b/src/GitVersion.Core/Model/VersionVariables.cs @@ -168,9 +168,25 @@ public static VersionVariables FromJson(string json) public static VersionVariables FromFile(string filePath, IFileSystem fileSystem, ILog log) { - var retryOperation = new OperationWithExponentialBackoff(new ThreadSleep(), log, () => FromFileInternal(filePath, fileSystem)); - var versionVariables = retryOperation.ExecuteAsync().Result; - return versionVariables; + try + { + if (log == null) + { + return FromFileInternal(filePath, fileSystem); + } + var retryOperation = new OperationWithExponentialBackoff(new ThreadSleep(), log, () => FromFileInternal(filePath, fileSystem)); + var versionVariables = retryOperation.ExecuteAsync().Result; + return versionVariables; + } + catch (AggregateException ex) + { + var lastException = ex.InnerExceptions?.LastOrDefault() ?? ex.InnerException; + if (lastException != null) + { + throw lastException; + } + throw; + } } private static VersionVariables FromFileInternal(string filePath, IFileSystem fileSystem) { diff --git a/src/GitVersion.LibGit2Sharp/Git/GitRepository.cs b/src/GitVersion.LibGit2Sharp/Git/GitRepository.cs index cc40361805..4737d11884 100644 --- a/src/GitVersion.LibGit2Sharp/Git/GitRepository.cs +++ b/src/GitVersion.LibGit2Sharp/Git/GitRepository.cs @@ -60,7 +60,6 @@ public ICommit FindMergeBase(ICommit commit, ICommit otherCommit) return new Commit(mergeBase); }).ExecuteAsync().Result; } - public int GetNumberOfUncommittedChanges() { return new OperationWithExponentialBackoff(new ThreadSleep(), log, () => @@ -68,7 +67,6 @@ public int GetNumberOfUncommittedChanges() return GetNumberOfUncommittedChangesInternal(); }).ExecuteAsync().Result; } - private int GetNumberOfUncommittedChangesInternal() { // check if we have a branch tip at all to behave properly with empty repos @@ -87,18 +85,17 @@ private int GetNumberOfUncommittedChangesInternal() catch (Exception) { return int.MaxValue; // this should be somewhat puzzling to see, - // so we may have reached our goal to show that - // that repo is really "Dirty"... + // so we may have reached our goal to show that + // that repo is really "Dirty"... } } // gets all changes of the last commit vs Staging area and WT var changes = repositoryInstance.Diff.Compare(repositoryInstance.Head.Tip.Tree, - DiffTargets.Index | DiffTargets.WorkingDirectory); + DiffTargets.Index | DiffTargets.WorkingDirectory); return changes.Count; } - public void CreateBranchForPullRequestBranch(AuthenticationInfo auth) { new OperationWithExponentialBackoff(new ThreadSleep(), log, () => @@ -106,7 +103,6 @@ public void CreateBranchForPullRequestBranch(AuthenticationInfo auth) CreateBranchForPullRequestBranchInternal(auth); }).ExecuteAsync().Wait(); } - private void CreateBranchForPullRequestBranchInternal(AuthenticationInfo auth) { var network = repositoryInstance.Network; @@ -164,7 +160,6 @@ private void CreateBranchForPullRequestBranchInternal(AuthenticationInfo auth) throw new WarningException(message); } } - public void Clone(string sourceUrl, string workdirPath, AuthenticationInfo auth) { try @@ -198,7 +193,6 @@ public void Checkout(string commitOrBranchSpec) { new OperationWithExponentialBackoff(new ThreadSleep(), log, () => Commands.Checkout(repositoryInstance, commitOrBranchSpec)).ExecuteAsync().Wait(); } - public void Fetch(string remote, IEnumerable refSpecs, AuthenticationInfo auth, string logMessage) { new OperationWithExponentialBackoff(new ThreadSleep(), log, () => Commands.Fetch((Repository)repositoryInstance, remote, refSpecs, GetFetchOptions(auth), logMessage)).ExecuteAsync().Wait();