Skip to content

Commit a6a50a3

Browse files
rominator1983arturcic
authored andcommitted
- adds trouble shooting information for #1627
1 parent 52994cb commit a6a50a3

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

src/GitVersion.Core/Core/GitPreparer.cs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -156,45 +156,47 @@ private void NormalizeGitDirectory(bool noFetch, string? currentBranchName, bool
156156
var expectedSha = this.repository.Head.Tip?.Sha;
157157
var expectedBranchName = this.repository.Head.Name.Canonical;
158158

159-
try
159+
var remote = EnsureOnlyOneRemoteIsDefined();
160+
EnsureRepositoryHeadDuringNormalisation(nameof(EnsureOnlyOneRemoteIsDefined), expectedSha);
161+
FetchRemotesIfRequired(remote, noFetch, authentication);
162+
EnsureRepositoryHeadDuringNormalisation(nameof(FetchRemotesIfRequired), expectedSha);
163+
EnsureLocalBranchExistsForCurrentBranch(remote, currentBranchName);
164+
EnsureRepositoryHeadDuringNormalisation(nameof(EnsureLocalBranchExistsForCurrentBranch), expectedSha);
165+
CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(remote.Name);
166+
EnsureRepositoryHeadDuringNormalisation(nameof(CreateOrUpdateLocalBranchesFromRemoteTrackingOnes), expectedSha);
167+
168+
var currentBranch = this.repository.Branches.FirstOrDefault(x => x.Name.EquivalentTo(currentBranchName));
169+
// Bug fix for https://github.com/GitTools/GitVersion/issues/1754, head maybe have been changed
170+
// if this is a dynamic repository. But only allow this in case the branches are different (branch switch)
171+
if (expectedSha != this.repository.Head.Tip?.Sha)
160172
{
161-
var remote = EnsureOnlyOneRemoteIsDefined();
162-
163-
FetchRemotesIfRequired(remote, noFetch, authentication);
164-
EnsureLocalBranchExistsForCurrentBranch(remote, currentBranchName);
165-
CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(remote.Name);
166-
167-
var currentBranch = this.repository.Branches.FirstOrDefault(x => x.Name.EquivalentTo(currentBranchName));
168-
// Bug fix for https://github.com/GitTools/GitVersion/issues/1754, head maybe have been changed
169-
// if this is a dynamic repository. But only allow this in case the branches are different (branch switch)
170-
if (expectedSha != this.repository.Head.Tip?.Sha)
173+
if (isDynamicRepository || currentBranch is null || !this.repository.Head.Equals(currentBranch))
171174
{
172-
if (isDynamicRepository || currentBranch is null || !this.repository.Head.Equals(currentBranch))
173-
{
174-
var newExpectedSha = this.repository.Head.Tip?.Sha;
175-
var newExpectedBranchName = this.repository.Head.Name.Canonical;
175+
var newExpectedSha = this.repository.Head.Tip?.Sha;
176+
var newExpectedBranchName = this.repository.Head.Name.Canonical;
176177

177-
this.log.Info($"Head has moved from '{expectedBranchName} | {expectedSha}' => '{newExpectedBranchName} | {newExpectedSha}', allowed since this is a dynamic repository");
178+
this.log.Info($"Head has moved from '{expectedBranchName} | {expectedSha}' => '{newExpectedBranchName} | {newExpectedSha}', allowed since this is a dynamic repository");
178179

179-
expectedSha = newExpectedSha;
180-
}
180+
expectedSha = newExpectedSha;
181181
}
182-
183-
EnsureHeadIsAttachedToBranch(currentBranchName, authentication);
184182
}
185-
finally
183+
184+
EnsureHeadIsAttachedToBranch(currentBranchName, authentication);
185+
EnsureRepositoryHeadDuringNormalisation(nameof(EnsureHeadIsAttachedToBranch), expectedSha);
186+
}
187+
188+
private void EnsureRepositoryHeadDuringNormalisation(string occasion, string expectedSha)
189+
{
190+
if (this.repository.Head.Tip?.Sha != expectedSha)
186191
{
187-
if (this.repository.Head.Tip?.Sha != expectedSha)
192+
if (this.environment.GetEnvironmentVariable("IGNORE_NORMALISATION_GIT_HEAD_MOVE") != "1")
188193
{
189-
if (this.environment.GetEnvironmentVariable("IGNORE_NORMALISATION_GIT_HEAD_MOVE") != "1")
190-
{
191-
// Whoa, HEAD has moved, it shouldn't have. We need to blow up because there is a bug in normalisation
192-
throw new BugException($@"GitVersion has a bug, your HEAD has moved after repo normalisation.
194+
// Whoa, HEAD has moved, it shouldn't have. We need to blow up because there is a bug in normalisation
195+
throw new BugException($@"GitVersion has a bug, your HEAD has moved after repo normalisation after step '{occasion}'
193196
194197
To disable this error set an environmental variable called IGNORE_NORMALISATION_GIT_HEAD_MOVE to 1
195198
196199
Please run `git {GitExtensions.CreateGitLogArgs(100)}` and submit it along with your build log (with personal info removed) in a new issue at https://github.com/GitTools/GitVersion");
197-
}
198200
}
199201
}
200202
}

0 commit comments

Comments
 (0)