From b5646829b5682f9adbd952761fd2b7eba89f06cd Mon Sep 17 00:00:00 2001 From: Markus Horstmann Date: Fri, 5 Feb 2021 13:51:50 -0800 Subject: [PATCH] Code cleanup for better diffability --- src/GitVersion.Core/GitVersion.Core.csproj | 2 +- .../OperationWithExponentialBackoff.cs | 2 +- .../Git/GitRepository.cs | 148 ++++++++++-------- 3 files changed, 82 insertions(+), 70 deletions(-) diff --git a/src/GitVersion.Core/GitVersion.Core.csproj b/src/GitVersion.Core/GitVersion.Core.csproj index 8d63289831..60b4786406 100644 --- a/src/GitVersion.Core/GitVersion.Core.csproj +++ b/src/GitVersion.Core/GitVersion.Core.csproj @@ -8,7 +8,7 @@ GitVersion.Core GitVersion Derives SemVer information from a repository following GitFlow or GitHubFlow. This is the Core library which both GitVersion cli and Task use allowing programatic usage of GitVersion. - + $(AssemblyName) diff --git a/src/GitVersion.Core/Helpers/OperationWithExponentialBackoff.cs b/src/GitVersion.Core/Helpers/OperationWithExponentialBackoff.cs index da12a7909d..c3a9ae4de8 100644 --- a/src/GitVersion.Core/Helpers/OperationWithExponentialBackoff.cs +++ b/src/GitVersion.Core/Helpers/OperationWithExponentialBackoff.cs @@ -11,12 +11,12 @@ public OperationWithExponentialBackoff(IThreadSleep threadSleep, ILog log, Actio : base(threadSleep, log, () => { operation(); return false; }, maxRetries) { } + public new Task ExecuteAsync() { return base.ExecuteAsync(); } - } public class OperationWithExponentialBackoff where T : Exception { diff --git a/src/GitVersion.LibGit2Sharp/Git/GitRepository.cs b/src/GitVersion.LibGit2Sharp/Git/GitRepository.cs index 9ce177fe47..cc40361805 100644 --- a/src/GitVersion.LibGit2Sharp/Git/GitRepository.cs +++ b/src/GitVersion.LibGit2Sharp/Git/GitRepository.cs @@ -65,94 +65,106 @@ public int GetNumberOfUncommittedChanges() { return new OperationWithExponentialBackoff(new ThreadSleep(), log, () => { - // check if we have a branch tip at all to behave properly with empty repos - // => return that we have actually uncomitted changes because we are apparently - // running GitVersion on something which lives inside this brand new repo _/\Ö/\_ - if (repositoryInstance.Head?.Tip == null || repositoryInstance.Diff == null) + return GetNumberOfUncommittedChangesInternal(); + }).ExecuteAsync().Result; + } + + private int GetNumberOfUncommittedChangesInternal() + { + // check if we have a branch tip at all to behave properly with empty repos + // => return that we have actually uncomitted changes because we are apparently + // running GitVersion on something which lives inside this brand new repo _/\Ö/\_ + if (repositoryInstance.Head?.Tip == null || repositoryInstance.Diff == null) + { + // this is a somewhat cumbersome way of figuring out the number of changes in the repo + // which is more expensive than to use the Diff as it gathers more info, but + // we can't use the other method when we are dealing with a new/empty repo + try { - // this is a somewhat cumbersome way of figuring out the number of changes in the repo - // which is more expensive than to use the Diff as it gathers more info, but - // we can't use the other method when we are dealing with a new/empty repo - try - { - var status = repositoryInstance.RetrieveStatus(); - return status.Untracked.Count() + status.Staged.Count(); - } - 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"... - } + var status = repositoryInstance.RetrieveStatus(); + return status.Untracked.Count() + status.Staged.Count(); } + 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"... + } + } - // 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); + // 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); - return changes.Count; - }).ExecuteAsync().Result; + return changes.Count; } + public void CreateBranchForPullRequestBranch(AuthenticationInfo auth) { new OperationWithExponentialBackoff(new ThreadSleep(), log, () => { - var network = repositoryInstance.Network; - var remote = network.Remotes.Single(); + CreateBranchForPullRequestBranchInternal(auth); + }).ExecuteAsync().Wait(); + } - log.Info("Fetching remote refs to see if there is a pull request ref"); - var credentialsProvider = GetCredentialsProvider(auth); - var remoteTips = (credentialsProvider != null - ? network.ListReferences(remote, credentialsProvider) - : network.ListReferences(remote)) - .Select(r => r.ResolveToDirectReference()).ToList(); + private void CreateBranchForPullRequestBranchInternal(AuthenticationInfo auth) + { + var network = repositoryInstance.Network; + var remote = network.Remotes.Single(); - log.Info($"Remote Refs:{System.Environment.NewLine}" + string.Join(System.Environment.NewLine, remoteTips.Select(r => r.CanonicalName))); + log.Info("Fetching remote refs to see if there is a pull request ref"); + var credentialsProvider = GetCredentialsProvider(auth); + var remoteTips = (credentialsProvider != null + ? network.ListReferences(remote, credentialsProvider) + : network.ListReferences(remote)) + .Select(r => r.ResolveToDirectReference()).ToList(); - var headTipSha = Head.Tip?.Sha; + log.Info($"Remote Refs:{System.Environment.NewLine}" + string.Join(System.Environment.NewLine, remoteTips.Select(r => r.CanonicalName))); - var refs = remoteTips.Where(r => r.TargetIdentifier == headTipSha).ToList(); + var headTipSha = Head.Tip?.Sha; - if (refs.Count == 0) - { - var message = $"Couldn't find any remote tips from remote '{remote.Url}' pointing at the commit '{headTipSha}'."; - throw new WarningException(message); - } + var refs = remoteTips.Where(r => r.TargetIdentifier == headTipSha).ToList(); - if (refs.Count > 1) - { - var names = string.Join(", ", refs.Select(r => r.CanonicalName)); - var message = $"Found more than one remote tip from remote '{remote.Url}' pointing at the commit '{headTipSha}'. Unable to determine which one to use ({names})."; - throw new WarningException(message); - } + if (refs.Count == 0) + { + var message = $"Couldn't find any remote tips from remote '{remote.Url}' pointing at the commit '{headTipSha}'."; + throw new WarningException(message); + } - var reference = refs.First(); - var canonicalName = reference.CanonicalName; - var referenceName = ReferenceName.Parse(reference.CanonicalName); - log.Info($"Found remote tip '{canonicalName}' pointing at the commit '{headTipSha}'."); + if (refs.Count > 1) + { + var names = string.Join(", ", refs.Select(r => r.CanonicalName)); + var message = $"Found more than one remote tip from remote '{remote.Url}' pointing at the commit '{headTipSha}'. Unable to determine which one to use ({names})."; + throw new WarningException(message); + } - if (referenceName.IsTag) - { - log.Info($"Checking out tag '{canonicalName}'"); - Checkout(reference.Target.Sha); - } - else if (referenceName.IsPullRequest) - { - var fakeBranchName = canonicalName.Replace("refs/pull/", "refs/heads/pull/").Replace("refs/pull-requests/", "refs/heads/pull-requests/"); + var reference = refs.First(); + var canonicalName = reference.CanonicalName; + var referenceName = ReferenceName.Parse(reference.CanonicalName); + log.Info($"Found remote tip '{canonicalName}' pointing at the commit '{headTipSha}'."); + + if (referenceName.IsTag) + { + log.Info($"Checking out tag '{canonicalName}'"); + Checkout(reference.Target.Sha); + } + else if (referenceName.IsPullRequest) + { + var fakeBranchName = canonicalName.Replace("refs/pull/", "refs/heads/pull/").Replace("refs/pull-requests/", "refs/heads/pull-requests/"); - log.Info($"Creating fake local branch '{fakeBranchName}'."); - Refs.Add(fakeBranchName, headTipSha); + log.Info($"Creating fake local branch '{fakeBranchName}'."); + Refs.Add(fakeBranchName, headTipSha); - log.Info($"Checking local branch '{fakeBranchName}' out."); - Checkout(fakeBranchName); - } - else - { - var message = $"Remote tip '{canonicalName}' from remote '{remote.Url}' doesn't look like a valid pull request."; - throw new WarningException(message); - } - }).ExecuteAsync().Wait(); + log.Info($"Checking local branch '{fakeBranchName}' out."); + Checkout(fakeBranchName); + } + else + { + var message = $"Remote tip '{canonicalName}' from remote '{remote.Url}' doesn't look like a valid pull request."; + throw new WarningException(message); + } } + public void Clone(string sourceUrl, string workdirPath, AuthenticationInfo auth) { try