@@ -156,47 +156,49 @@ private void NormalizeGitDirectory(bool noFetch, string? currentBranchName, bool
156
156
var expectedSha = this . repository . Head . Tip ? . Sha ;
157
157
var expectedBranchName = this . repository . Head . Name . Canonical ;
158
158
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 )
160
172
{
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 ) )
171
174
{
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 ;
176
177
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") ;
178
179
179
- expectedSha = newExpectedSha ;
180
- }
180
+ expectedSha = newExpectedSha ;
181
181
}
182
-
183
- EnsureHeadIsAttachedToBranch ( currentBranchName , authentication ) ;
184
182
}
185
- finally
186
- {
187
- if ( this . repository . Head . Tip ? . Sha != expectedSha )
188
- {
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.
193
183
194
- To disable this error set an environmental variable called IGNORE_NORMALISATION_GIT_HEAD_MOVE to 1
184
+ EnsureHeadIsAttachedToBranch ( currentBranchName , authentication ) ;
185
+ EnsureRepositoryHeadDuringNormalisation ( nameof ( EnsureHeadIsAttachedToBranch ) , expectedSha ) ;
186
+ }
195
187
188
+ private void EnsureRepositoryHeadDuringNormalisation ( string occasion , string ? expectedSha )
189
+ {
190
+ expectedSha . NotNull ( ) ;
191
+ if ( this . repository . Head . Tip ? . Sha == expectedSha )
192
+ return ;
193
+
194
+ if ( this . environment . GetEnvironmentVariable ( "IGNORE_NORMALISATION_GIT_HEAD_MOVE" ) == "1" )
195
+ return ;
196
+
197
+ // Whoa, HEAD has moved, it shouldn't have. We need to blow up because there is a bug in normalisation
198
+ throw new BugException ( $@ "
199
+ GitVersion has a bug, your HEAD has moved after repo normalisation after step '{ occasion } '
200
+ To disable this error set an environmental variable called IGNORE_NORMALISATION_GIT_HEAD_MOVE to 1
196
201
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
- }
198
- }
199
- }
200
202
}
201
203
202
204
private void EnsureHeadIsAttachedToBranch ( string ? currentBranchName , AuthenticationInfo authentication )
0 commit comments