1
1
using System ;
2
2
using System . IO ;
3
3
using System . Linq ;
4
+ using GitVersion . Common ;
4
5
using GitVersion . Extensions ;
5
6
using GitVersion . Logging ;
6
7
using GitVersion . Model . Configuration ;
@@ -15,18 +16,20 @@ public class GitPreparer : IGitPreparer
15
16
private readonly IGitRepository repository ;
16
17
private readonly IOptions < GitVersionOptions > options ;
17
18
private readonly IGitRepositoryInfo repositoryInfo ;
19
+ private readonly IRepositoryMetadataProvider repositoryMetadataProvider ;
18
20
private readonly ICurrentBuildAgent buildAgent ;
19
21
20
22
private const string DefaultRemoteName = "origin" ;
21
23
22
- public GitPreparer ( ILog log , IEnvironment environment , ICurrentBuildAgent buildAgent ,
23
- IOptions < GitVersionOptions > options , IGitRepository repository , IGitRepositoryInfo repositoryInfo )
24
+ public GitPreparer ( ILog log , IEnvironment environment , ICurrentBuildAgent buildAgent , IOptions < GitVersionOptions > options ,
25
+ IGitRepository repository , IGitRepositoryInfo repositoryInfo , IRepositoryMetadataProvider repositoryMetadataProvider )
24
26
{
25
27
this . log = log ?? throw new ArgumentNullException ( nameof ( log ) ) ;
26
28
this . environment = environment ?? throw new ArgumentNullException ( nameof ( environment ) ) ;
27
29
this . repository = repository ?? throw new ArgumentNullException ( nameof ( repository ) ) ;
28
30
this . options = options ?? throw new ArgumentNullException ( nameof ( options ) ) ;
29
31
this . repositoryInfo = repositoryInfo ?? throw new ArgumentNullException ( nameof ( repositoryInfo ) ) ;
32
+ this . repositoryMetadataProvider = repositoryMetadataProvider ?? throw new ArgumentNullException ( nameof ( repositoryMetadataProvider ) ) ;
30
33
this . buildAgent = buildAgent ;
31
34
}
32
35
@@ -141,7 +144,7 @@ private void CloneRepository(string repositoryUrl, string gitDirectory, Authenti
141
144
/// Normalization of a git directory turns all remote branches into local branches, turns pull request refs into a real branch and a few other things. This is designed to be run *only on the build server* which checks out repositories in different ways.
142
145
/// It is not recommended to run normalization against a local repository
143
146
/// </summary>
144
- private void NormalizeGitDirectory ( string gitDirectory , bool noFetch , string currentBranch , bool isDynamicRepository )
147
+ private void NormalizeGitDirectory ( string gitDirectory , bool noFetch , string currentBranchName , bool isDynamicRepository )
145
148
{
146
149
var authentication = options . Value . Authentication ;
147
150
using var repository = this . repository . CreateNew ( gitDirectory ) ;
@@ -164,13 +167,15 @@ private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string cur
164
167
repository . Fetch ( remote . Name , new string [ 0 ] , authentication , null ) ;
165
168
}
166
169
167
- EnsureLocalBranchExistsForCurrentBranch ( repository , log , remote , currentBranch ) ;
170
+ EnsureLocalBranchExistsForCurrentBranch ( repository , log , remote , currentBranchName ) ;
168
171
CreateOrUpdateLocalBranchesFromRemoteTrackingOnes ( repository , log , remote . Name ) ;
169
172
173
+ var currentBranch = repositoryMetadataProvider . FindBranch ( currentBranchName ) ;
170
174
// Bug fix for https://github.com/GitTools/GitVersion/issues/1754, head maybe have been changed
171
175
// if this is a dynamic repository. But only allow this in case the branches are different (branch switch)
172
176
if ( expectedSha != repository . Head . Tip . Sha &&
173
- ( isDynamicRepository || ! expectedBranchName . IsBranch ( currentBranch ) ) )
177
+ ( isDynamicRepository || currentBranch is null
178
+ || ! repository . Head . Equals ( currentBranch ) ) )
174
179
{
175
180
var newExpectedSha = repository . Head . Tip . Sha ;
176
181
var newExpectedBranchName = repository . Head . CanonicalName ;
@@ -196,12 +201,12 @@ private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string cur
196
201
// If no, go ahead and check out a new branch, using the known commit SHA as the pointer
197
202
var localBranchesWhereCommitShaIsHead = repository . Branches . Where ( b => ! b . IsRemote && b . Tip . Sha == headSha ) . ToList ( ) ;
198
203
199
- var matchingCurrentBranch = ! string . IsNullOrEmpty ( currentBranch )
200
- ? localBranchesWhereCommitShaIsHead . SingleOrDefault ( b => b . CanonicalName . Replace ( "/heads/" , "/" ) == currentBranch . Replace ( "/heads/" , "/" ) )
204
+ var matchingCurrentBranch = ! string . IsNullOrEmpty ( currentBranchName )
205
+ ? localBranchesWhereCommitShaIsHead . SingleOrDefault ( b => b . CanonicalName . Replace ( "/heads/" , "/" ) == currentBranchName . Replace ( "/heads/" , "/" ) )
201
206
: null ;
202
207
if ( matchingCurrentBranch != null )
203
208
{
204
- log . Info ( $ "Checking out local branch '{ currentBranch } '.") ;
209
+ log . Info ( $ "Checking out local branch '{ currentBranchName } '.") ;
205
210
repository . Checkout ( matchingCurrentBranch ) ;
206
211
}
207
212
else if ( localBranchesWhereCommitShaIsHead . Count > 1 )
0 commit comments