1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . ComponentModel . DataAnnotations ;
34using System . IO ;
45using System . Threading . Tasks ;
@@ -51,7 +52,11 @@ public string SelectedBranch
5152 public bool SetTrackingBranch
5253 {
5354 get => _setTrackingBranch ;
54- set => SetProperty ( ref _setTrackingBranch , value ) ;
55+ set
56+ {
57+ if ( SetProperty ( ref _setTrackingBranch , value ) )
58+ AutoSelectTrackingBranch ( ) ;
59+ }
5560 }
5661
5762 public string SelectedTrackingBranch
@@ -73,11 +78,6 @@ public AddWorktree(Repository repo)
7378 else
7479 RemoteBranches . Add ( branch . FriendlyName ) ;
7580 }
76-
77- if ( RemoteBranches . Count > 0 )
78- SelectedTrackingBranch = RemoteBranches [ 0 ] ;
79- else
80- SelectedTrackingBranch = string . Empty ;
8181 }
8282
8383 public static ValidationResult ValidateWorktreePath ( string path , ValidationContext ctx )
@@ -123,6 +123,23 @@ public override async Task<bool> Sure()
123123 return succ ;
124124 }
125125
126+ private void AutoSelectTrackingBranch ( )
127+ {
128+ if ( ! _setTrackingBranch || RemoteBranches . Count == 0 )
129+ return ;
130+
131+ var name = string . IsNullOrEmpty ( _selectedBranch ) ? System . IO . Path . GetFileName ( _path . TrimEnd ( '/' , '\\ ' ) ) : _selectedBranch ;
132+ var remoteBranch = RemoteBranches . Find ( b => b . Substring ( b . IndexOf ( '/' ) + 1 ) . Equals ( name , StringComparison . Ordinal ) ) ;
133+ if ( string . IsNullOrEmpty ( remoteBranch ) )
134+ remoteBranch = RemoteBranches [ 0 ] ;
135+
136+ if ( ! remoteBranch . Equals ( SelectedTrackingBranch , StringComparison . Ordinal ) )
137+ {
138+ SelectedTrackingBranch = remoteBranch ;
139+ OnPropertyChanged ( nameof ( SelectedTrackingBranch ) ) ;
140+ }
141+ }
142+
126143 private Repository _repo = null ;
127144 private string _path = string . Empty ;
128145 private bool _createNewBranch = true ;
0 commit comments