Skip to content

Commit

Permalink
Merge pull request #3245 from rominator1983/addTroubleshootingInforma…
Browse files Browse the repository at this point in the history
…tionForNormalisationBug

Adds trouble shooting info for #1627
  • Loading branch information
arturcic authored Mar 19, 2023
2 parents 52994cb + b94dddc commit d49e570
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions src/GitVersion.Core/Core/GitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,47 +156,49 @@ private void NormalizeGitDirectory(bool noFetch, string? currentBranchName, bool
var expectedSha = this.repository.Head.Tip?.Sha;
var expectedBranchName = this.repository.Head.Name.Canonical;

try
var remote = EnsureOnlyOneRemoteIsDefined();
EnsureRepositoryHeadDuringNormalisation(nameof(EnsureOnlyOneRemoteIsDefined), expectedSha);
FetchRemotesIfRequired(remote, noFetch, authentication);
EnsureRepositoryHeadDuringNormalisation(nameof(FetchRemotesIfRequired), expectedSha);
EnsureLocalBranchExistsForCurrentBranch(remote, currentBranchName);
EnsureRepositoryHeadDuringNormalisation(nameof(EnsureLocalBranchExistsForCurrentBranch), expectedSha);
CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(remote.Name);
EnsureRepositoryHeadDuringNormalisation(nameof(CreateOrUpdateLocalBranchesFromRemoteTrackingOnes), expectedSha);

var currentBranch = this.repository.Branches.FirstOrDefault(x => x.Name.EquivalentTo(currentBranchName));
// Bug fix for https://github.com/GitTools/GitVersion/issues/1754, head maybe have been changed
// if this is a dynamic repository. But only allow this in case the branches are different (branch switch)
if (expectedSha != this.repository.Head.Tip?.Sha)
{
var remote = EnsureOnlyOneRemoteIsDefined();

FetchRemotesIfRequired(remote, noFetch, authentication);
EnsureLocalBranchExistsForCurrentBranch(remote, currentBranchName);
CreateOrUpdateLocalBranchesFromRemoteTrackingOnes(remote.Name);

var currentBranch = this.repository.Branches.FirstOrDefault(x => x.Name.EquivalentTo(currentBranchName));
// Bug fix for https://github.com/GitTools/GitVersion/issues/1754, head maybe have been changed
// if this is a dynamic repository. But only allow this in case the branches are different (branch switch)
if (expectedSha != this.repository.Head.Tip?.Sha)
if (isDynamicRepository || currentBranch is null || !this.repository.Head.Equals(currentBranch))
{
if (isDynamicRepository || currentBranch is null || !this.repository.Head.Equals(currentBranch))
{
var newExpectedSha = this.repository.Head.Tip?.Sha;
var newExpectedBranchName = this.repository.Head.Name.Canonical;
var newExpectedSha = this.repository.Head.Tip?.Sha;
var newExpectedBranchName = this.repository.Head.Name.Canonical;

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

expectedSha = newExpectedSha;
}
expectedSha = newExpectedSha;
}

EnsureHeadIsAttachedToBranch(currentBranchName, authentication);
}
finally
{
if (this.repository.Head.Tip?.Sha != expectedSha)
{
if (this.environment.GetEnvironmentVariable("IGNORE_NORMALISATION_GIT_HEAD_MOVE") != "1")
{
// Whoa, HEAD has moved, it shouldn't have. We need to blow up because there is a bug in normalisation
throw new BugException($@"GitVersion has a bug, your HEAD has moved after repo normalisation.

To disable this error set an environmental variable called IGNORE_NORMALISATION_GIT_HEAD_MOVE to 1
EnsureHeadIsAttachedToBranch(currentBranchName, authentication);
EnsureRepositoryHeadDuringNormalisation(nameof(EnsureHeadIsAttachedToBranch), expectedSha);
}

private void EnsureRepositoryHeadDuringNormalisation(string occasion, string? expectedSha)
{
expectedSha.NotNull();
if (this.repository.Head.Tip?.Sha == expectedSha)
return;

if (this.environment.GetEnvironmentVariable("IGNORE_NORMALISATION_GIT_HEAD_MOVE") == "1")
return;

// Whoa, HEAD has moved, it shouldn't have. We need to blow up because there is a bug in normalisation
throw new BugException($@"
GitVersion has a bug, your HEAD has moved after repo normalisation after step '{occasion}'
To disable this error set an environmental variable called IGNORE_NORMALISATION_GIT_HEAD_MOVE to 1
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");
}
}
}
}

private void EnsureHeadIsAttachedToBranch(string? currentBranchName, AuthenticationInfo authentication)
Expand Down

0 comments on commit d49e570

Please sign in to comment.