@@ -32,7 +32,7 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
32
32
}
33
33
34
34
EnsureLocalBranchExistsForCurrentBranch ( repo , currentBranch ) ;
35
- CreateMissingLocalBranchesFromRemoteTrackingOnes ( repo , remote . Name ) ;
35
+ CreateOrUpdateLocalBranchesFromRemoteTrackingOnes ( repo , remote . Name ) ;
36
36
37
37
var headSha = repo . Refs . Head . TargetIdentifier ;
38
38
@@ -104,12 +104,23 @@ static void EnsureLocalBranchExistsForCurrentBranch(Repository repo, string curr
104
104
if ( string . IsNullOrEmpty ( currentBranch ) ) return ;
105
105
var isBranch = currentBranch . Contains ( "refs/heads" ) ;
106
106
var localCanonicalName = isBranch ? currentBranch : currentBranch . Replace ( "refs/" , "refs/heads/" ) ;
107
- if ( repo . Branches [ localCanonicalName ] != null ) return ;
107
+ var repoTip = repo . Head . Tip ;
108
+ var repoTipId = repoTip . Id ;
108
109
109
- Logger . WriteInfo ( isBranch ?
110
- string . Format ( "Creating local branch {0}" , localCanonicalName ) :
111
- string . Format ( "Creating local branch {0} from ref {1}" , localCanonicalName , currentBranch ) ) ;
112
- repo . Refs . Add ( localCanonicalName , repo . Head . Tip . Id ) ;
110
+ if ( repo . Branches . All ( b => b . CanonicalName != localCanonicalName ) )
111
+ {
112
+ Logger . WriteInfo ( isBranch ?
113
+ string . Format ( "Creating local branch {0}" , localCanonicalName ) :
114
+ string . Format ( "Creating local branch {0} from ref {1}" , localCanonicalName , currentBranch ) ) ;
115
+ repo . Refs . Add ( localCanonicalName , repoTipId ) ;
116
+ }
117
+ else
118
+ {
119
+ Logger . WriteInfo ( isBranch ?
120
+ string . Format ( "Updating local branch {0} to point at {1}" , localCanonicalName , repoTip . Sha ) :
121
+ string . Format ( "Updating local branch {0} to match ref {1}" , localCanonicalName , currentBranch ) ) ;
122
+ repo . Refs . UpdateTarget ( repo . Refs [ localCanonicalName ] , repoTipId ) ;
123
+ }
113
124
}
114
125
115
126
public static bool LooksLikeAValidPullRequestNumber ( string issueNumber )
@@ -241,7 +252,7 @@ static IEnumerable<DirectReference> GetRemoteTipsForAnonymousUser(Repository rep
241
252
return repo . Network . ListReferences ( remote ) ;
242
253
}
243
254
244
- static void CreateMissingLocalBranchesFromRemoteTrackingOnes ( Repository repo , string remoteName )
255
+ static void CreateOrUpdateLocalBranchesFromRemoteTrackingOnes ( Repository repo , string remoteName )
245
256
{
246
257
var prefix = string . Format ( "refs/remotes/{0}/" , remoteName ) ;
247
258
var remoteHeadCanonicalName = string . Format ( "{0}{1}" , prefix , "HEAD" ) ;
@@ -254,7 +265,16 @@ static void CreateMissingLocalBranchesFromRemoteTrackingOnes(Repository repo, st
254
265
255
266
if ( repo . Refs . Any ( x => x . CanonicalName == localCanonicalName ) )
256
267
{
257
- Logger . WriteInfo ( string . Format ( "Skipping local branch creation since it already exists '{0}'." , remoteTrackingReference . CanonicalName ) ) ;
268
+ var localRef = repo . Refs [ localCanonicalName ] ;
269
+ var remotedirectReference = remoteTrackingReference . ResolveToDirectReference ( ) ;
270
+ if ( localRef . ResolveToDirectReference ( ) . TargetIdentifier == remotedirectReference . TargetIdentifier )
271
+ {
272
+ Logger . WriteInfo ( string . Format ( "Skipping update of '{0}' as it already matches the remote ref." , remoteTrackingReference . CanonicalName ) ) ;
273
+ continue ;
274
+ }
275
+ var remoteRefTipId = remotedirectReference . Target . Id ;
276
+ Logger . WriteInfo ( string . Format ( "Updating local ref '{0}' to point at {1}." , remoteTrackingReference . CanonicalName , remoteRefTipId ) ) ;
277
+ repo . Refs . UpdateTarget ( localRef , remoteRefTipId ) ;
258
278
continue ;
259
279
}
260
280
Logger . WriteInfo ( string . Format ( "Creating local branch from remote tracking '{0}'." , remoteTrackingReference . CanonicalName ) ) ;
0 commit comments