Skip to content

Commit

Permalink
Code cleanup for better diffability
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusHorstmann authored and arturcic committed Feb 7, 2021
1 parent a4538f9 commit b564682
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/GitVersion.Core/GitVersion.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageId>GitVersion.Core</PackageId>
<Title>GitVersion</Title>
<Description>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.</Description>

<Product>$(AssemblyName)</Product>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, Result> where T : Exception
{
Expand Down
148 changes: 80 additions & 68 deletions src/GitVersion.LibGit2Sharp/Git/GitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,94 +65,106 @@ public int GetNumberOfUncommittedChanges()
{
return new OperationWithExponentialBackoff<LibGit2Sharp.LockedFileException, int>(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<TreeChanges>(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<TreeChanges>(repositoryInstance.Head.Tip.Tree,
DiffTargets.Index | DiffTargets.WorkingDirectory);

return changes.Count;
}).ExecuteAsync().Result;
return changes.Count;
}

public void CreateBranchForPullRequestBranch(AuthenticationInfo auth)
{
new OperationWithExponentialBackoff<LockedFileException>(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
Expand Down

0 comments on commit b564682

Please sign in to comment.