Skip to content

Commit

Permalink
Make VersionVariables.FromFile work with no logger. Preserve exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusHorstmann authored and arturcic committed Feb 7, 2021
1 parent b564682 commit b74ec71
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public new Task ExecuteAsync()
{
return base.ExecuteAsync();
}

}
public class OperationWithExponentialBackoff<T, Result> where T : Exception
{
Expand Down
22 changes: 19 additions & 3 deletions src/GitVersion.Core/Model/VersionVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,25 @@ public static VersionVariables FromJson(string json)

public static VersionVariables FromFile(string filePath, IFileSystem fileSystem, ILog log)
{
var retryOperation = new OperationWithExponentialBackoff<IOException, VersionVariables>(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<IOException, VersionVariables>(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)
{
Expand Down
12 changes: 3 additions & 9 deletions src/GitVersion.LibGit2Sharp/Git/GitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,13 @@ public ICommit FindMergeBase(ICommit commit, ICommit otherCommit)
return new Commit(mergeBase);
}).ExecuteAsync().Result;
}

public int GetNumberOfUncommittedChanges()
{
return new OperationWithExponentialBackoff<LibGit2Sharp.LockedFileException, int>(new ThreadSleep(), log, () =>
{
return GetNumberOfUncommittedChangesInternal();
}).ExecuteAsync().Result;
}

private int GetNumberOfUncommittedChangesInternal()
{
// check if we have a branch tip at all to behave properly with empty repos
Expand All @@ -87,26 +85,24 @@ 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<TreeChanges>(repositoryInstance.Head.Tip.Tree,
DiffTargets.Index | DiffTargets.WorkingDirectory);
DiffTargets.Index | DiffTargets.WorkingDirectory);

return changes.Count;
}

public void CreateBranchForPullRequestBranch(AuthenticationInfo auth)
{
new OperationWithExponentialBackoff<LockedFileException>(new ThreadSleep(), log, () =>
{
CreateBranchForPullRequestBranchInternal(auth);
}).ExecuteAsync().Wait();
}

private void CreateBranchForPullRequestBranchInternal(AuthenticationInfo auth)
{
var network = repositoryInstance.Network;
Expand Down Expand Up @@ -164,7 +160,6 @@ private void CreateBranchForPullRequestBranchInternal(AuthenticationInfo auth)
throw new WarningException(message);
}
}

public void Clone(string sourceUrl, string workdirPath, AuthenticationInfo auth)
{
try
Expand Down Expand Up @@ -198,7 +193,6 @@ public void Checkout(string commitOrBranchSpec)
{
new OperationWithExponentialBackoff<LockedFileException>(new ThreadSleep(), log, () => Commands.Checkout(repositoryInstance, commitOrBranchSpec)).ExecuteAsync().Wait();
}

public void Fetch(string remote, IEnumerable<string> refSpecs, AuthenticationInfo auth, string logMessage)
{
new OperationWithExponentialBackoff<LockedFileException>(new ThreadSleep(), log, () => Commands.Fetch((Repository)repositoryInstance, remote, refSpecs, GetFetchOptions(auth), logMessage)).ExecuteAsync().Wait();
Expand Down

0 comments on commit b74ec71

Please sign in to comment.