Skip to content

Commit 854b5c0

Browse files
Code cleanup for better diffability
1 parent c6f2c95 commit 854b5c0

File tree

3 files changed

+82
-70
lines changed

3 files changed

+82
-70
lines changed

src/GitVersion.Core/GitVersion.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<PackageId>GitVersion.Core</PackageId>
99
<Title>GitVersion</Title>
1010
<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>
11-
11+
1212
<Product>$(AssemblyName)</Product>
1313
</PropertyGroup>
1414

src/GitVersion.Core/Helpers/OperationWithExponentialBackoff.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public OperationWithExponentialBackoff(IThreadSleep threadSleep, ILog log, Actio
1111
: base(threadSleep, log, () => { operation(); return false; }, maxRetries)
1212
{
1313
}
14+
1415
public new Task ExecuteAsync()
1516
{
1617
return base.ExecuteAsync();
1718
}
1819

19-
2020
}
2121
public class OperationWithExponentialBackoff<T, Result> where T : Exception
2222
{

src/GitVersion.LibGit2Sharp/Git/GitRepository.cs

Lines changed: 80 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -65,94 +65,106 @@ public int GetNumberOfUncommittedChanges()
6565
{
6666
return new OperationWithExponentialBackoff<LibGit2Sharp.LockedFileException, int>(new ThreadSleep(), log, () =>
6767
{
68-
// check if we have a branch tip at all to behave properly with empty repos
69-
// => return that we have actually uncomitted changes because we are apparently
70-
// running GitVersion on something which lives inside this brand new repo _/\Ö/\_
71-
if (repositoryInstance.Head?.Tip == null || repositoryInstance.Diff == null)
68+
return GetNumberOfUncommittedChangesInternal();
69+
}).ExecuteAsync().Result;
70+
}
71+
72+
private int GetNumberOfUncommittedChangesInternal()
73+
{
74+
// check if we have a branch tip at all to behave properly with empty repos
75+
// => return that we have actually uncomitted changes because we are apparently
76+
// running GitVersion on something which lives inside this brand new repo _/\Ö/\_
77+
if (repositoryInstance.Head?.Tip == null || repositoryInstance.Diff == null)
78+
{
79+
// this is a somewhat cumbersome way of figuring out the number of changes in the repo
80+
// which is more expensive than to use the Diff as it gathers more info, but
81+
// we can't use the other method when we are dealing with a new/empty repo
82+
try
7283
{
73-
// this is a somewhat cumbersome way of figuring out the number of changes in the repo
74-
// which is more expensive than to use the Diff as it gathers more info, but
75-
// we can't use the other method when we are dealing with a new/empty repo
76-
try
77-
{
78-
var status = repositoryInstance.RetrieveStatus();
79-
return status.Untracked.Count() + status.Staged.Count();
80-
}
81-
catch (Exception)
82-
{
83-
return int.MaxValue; // this should be somewhat puzzling to see,
84-
// so we may have reached our goal to show that
85-
// that repo is really "Dirty"...
86-
}
84+
var status = repositoryInstance.RetrieveStatus();
85+
return status.Untracked.Count() + status.Staged.Count();
8786
}
87+
catch (Exception)
88+
{
89+
return int.MaxValue; // this should be somewhat puzzling to see,
90+
// so we may have reached our goal to show that
91+
// that repo is really "Dirty"...
92+
}
93+
}
8894

89-
// gets all changes of the last commit vs Staging area and WT
90-
var changes = repositoryInstance.Diff.Compare<TreeChanges>(repositoryInstance.Head.Tip.Tree,
91-
DiffTargets.Index | DiffTargets.WorkingDirectory);
95+
// gets all changes of the last commit vs Staging area and WT
96+
var changes = repositoryInstance.Diff.Compare<TreeChanges>(repositoryInstance.Head.Tip.Tree,
97+
DiffTargets.Index | DiffTargets.WorkingDirectory);
9298

93-
return changes.Count;
94-
}).ExecuteAsync().Result;
99+
return changes.Count;
95100
}
101+
96102
public void CreateBranchForPullRequestBranch(AuthenticationInfo auth)
97103
{
98104
new OperationWithExponentialBackoff<LockedFileException>(new ThreadSleep(), log, () =>
99105
{
100-
var network = repositoryInstance.Network;
101-
var remote = network.Remotes.Single();
106+
CreateBranchForPullRequestBranchInternal(auth);
107+
}).ExecuteAsync().Wait();
108+
}
102109

103-
log.Info("Fetching remote refs to see if there is a pull request ref");
104-
var credentialsProvider = GetCredentialsProvider(auth);
105-
var remoteTips = (credentialsProvider != null
106-
? network.ListReferences(remote, credentialsProvider)
107-
: network.ListReferences(remote))
108-
.Select(r => r.ResolveToDirectReference()).ToList();
110+
private void CreateBranchForPullRequestBranchInternal(AuthenticationInfo auth)
111+
{
112+
var network = repositoryInstance.Network;
113+
var remote = network.Remotes.Single();
109114

110-
log.Info($"Remote Refs:{System.Environment.NewLine}" + string.Join(System.Environment.NewLine, remoteTips.Select(r => r.CanonicalName)));
115+
log.Info("Fetching remote refs to see if there is a pull request ref");
116+
var credentialsProvider = GetCredentialsProvider(auth);
117+
var remoteTips = (credentialsProvider != null
118+
? network.ListReferences(remote, credentialsProvider)
119+
: network.ListReferences(remote))
120+
.Select(r => r.ResolveToDirectReference()).ToList();
111121

112-
var headTipSha = Head.Tip?.Sha;
122+
log.Info($"Remote Refs:{System.Environment.NewLine}" + string.Join(System.Environment.NewLine, remoteTips.Select(r => r.CanonicalName)));
113123

114-
var refs = remoteTips.Where(r => r.TargetIdentifier == headTipSha).ToList();
124+
var headTipSha = Head.Tip?.Sha;
115125

116-
if (refs.Count == 0)
117-
{
118-
var message = $"Couldn't find any remote tips from remote '{remote.Url}' pointing at the commit '{headTipSha}'.";
119-
throw new WarningException(message);
120-
}
126+
var refs = remoteTips.Where(r => r.TargetIdentifier == headTipSha).ToList();
121127

122-
if (refs.Count > 1)
123-
{
124-
var names = string.Join(", ", refs.Select(r => r.CanonicalName));
125-
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}).";
126-
throw new WarningException(message);
127-
}
128+
if (refs.Count == 0)
129+
{
130+
var message = $"Couldn't find any remote tips from remote '{remote.Url}' pointing at the commit '{headTipSha}'.";
131+
throw new WarningException(message);
132+
}
128133

129-
var reference = refs.First();
130-
var canonicalName = reference.CanonicalName;
131-
var referenceName = ReferenceName.Parse(reference.CanonicalName);
132-
log.Info($"Found remote tip '{canonicalName}' pointing at the commit '{headTipSha}'.");
134+
if (refs.Count > 1)
135+
{
136+
var names = string.Join(", ", refs.Select(r => r.CanonicalName));
137+
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}).";
138+
throw new WarningException(message);
139+
}
133140

134-
if (referenceName.IsTag)
135-
{
136-
log.Info($"Checking out tag '{canonicalName}'");
137-
Checkout(reference.Target.Sha);
138-
}
139-
else if (referenceName.IsPullRequest)
140-
{
141-
var fakeBranchName = canonicalName.Replace("refs/pull/", "refs/heads/pull/").Replace("refs/pull-requests/", "refs/heads/pull-requests/");
141+
var reference = refs.First();
142+
var canonicalName = reference.CanonicalName;
143+
var referenceName = ReferenceName.Parse(reference.CanonicalName);
144+
log.Info($"Found remote tip '{canonicalName}' pointing at the commit '{headTipSha}'.");
145+
146+
if (referenceName.IsTag)
147+
{
148+
log.Info($"Checking out tag '{canonicalName}'");
149+
Checkout(reference.Target.Sha);
150+
}
151+
else if (referenceName.IsPullRequest)
152+
{
153+
var fakeBranchName = canonicalName.Replace("refs/pull/", "refs/heads/pull/").Replace("refs/pull-requests/", "refs/heads/pull-requests/");
142154

143-
log.Info($"Creating fake local branch '{fakeBranchName}'.");
144-
Refs.Add(fakeBranchName, headTipSha);
155+
log.Info($"Creating fake local branch '{fakeBranchName}'.");
156+
Refs.Add(fakeBranchName, headTipSha);
145157

146-
log.Info($"Checking local branch '{fakeBranchName}' out.");
147-
Checkout(fakeBranchName);
148-
}
149-
else
150-
{
151-
var message = $"Remote tip '{canonicalName}' from remote '{remote.Url}' doesn't look like a valid pull request.";
152-
throw new WarningException(message);
153-
}
154-
}).ExecuteAsync().Wait();
158+
log.Info($"Checking local branch '{fakeBranchName}' out.");
159+
Checkout(fakeBranchName);
160+
}
161+
else
162+
{
163+
var message = $"Remote tip '{canonicalName}' from remote '{remote.Url}' doesn't look like a valid pull request.";
164+
throw new WarningException(message);
165+
}
155166
}
167+
156168
public void Clone(string sourceUrl, string workdirPath, AuthenticationInfo auth)
157169
{
158170
try

0 commit comments

Comments
 (0)