11using System . Collections . Generic ;
22using System . ComponentModel . DataAnnotations ;
33using System . IO ;
4- using System . Text . RegularExpressions ;
54using System . Threading . Tasks ;
65
76namespace SourceGit . ViewModels
87{
9- public partial class AddWorktree : Popup
8+ public class AddWorktree : Popup
109 {
11- [ GeneratedRegex ( @"^[\w\-/\.]+$" ) ]
12- private static partial Regex REG_NAME ( ) ;
13-
1410 [ Required ( ErrorMessage = "Worktree path is required!" ) ]
1511 [ CustomValidation ( typeof ( AddWorktree ) , nameof ( ValidateWorktreePath ) ) ]
1612 public string Path
1713 {
1814 get => _path ;
19- set => SetProperty ( ref _path , value , true ) ;
15+ set => SetProperty ( ref _path , value ) ;
2016 }
2117
22- [ CustomValidation ( typeof ( AddWorktree ) , nameof ( ValidateBranchName ) ) ]
23- public string CustomName
18+ public bool UseExistingBranch
2419 {
25- get => _customName ;
26- set => SetProperty ( ref _customName , value , true ) ;
20+ get => _useExistingBranch ;
21+ set
22+ {
23+ if ( SetProperty ( ref _useExistingBranch , value , true ) )
24+ {
25+ if ( value )
26+ SelectedBranch = LocalBranches . Count > 0 ? LocalBranches [ 0 ] : string . Empty ;
27+ else
28+ SelectedBranch = string . Empty ;
29+ }
30+ }
2731 }
2832
29- public bool SetTrackingBranch
33+ public List < string > LocalBranches
3034 {
31- get => _setTrackingBranch ;
32- set => SetProperty ( ref _setTrackingBranch , value ) ;
35+ get ;
36+ private set ;
3337 }
3438
35- public List < string > TrackingBranches
39+ public List < string > RemoteBranches
3640 {
3741 get ;
3842 private set ;
3943 }
4044
45+ public string SelectedBranch
46+ {
47+ get => _selectedBranch ;
48+ set => SetProperty ( ref _selectedBranch , value ) ;
49+ }
50+
51+ public bool SetTrackingBranch
52+ {
53+ get => _setTrackingBranch ;
54+ set => SetProperty ( ref _setTrackingBranch , value ) ;
55+ }
56+
4157 public string SelectedTrackingBranch
4258 {
4359 get ;
@@ -48,15 +64,23 @@ public AddWorktree(Repository repo)
4864 {
4965 _repo = repo ;
5066
51- TrackingBranches = new List < string > ( ) ;
67+ LocalBranches = new List < string > ( ) ;
68+ RemoteBranches = new List < string > ( ) ;
5269 foreach ( var branch in repo . Branches )
5370 {
54- if ( ! branch . IsLocal )
55- TrackingBranches . Add ( $ "{ branch . Remote } /{ branch . Name } ") ;
71+ if ( branch . IsLocal )
72+ LocalBranches . Add ( branch . Name ) ;
73+ else
74+ RemoteBranches . Add ( $ "{ branch . Remote } /{ branch . Name } ") ;
5675 }
5776
58- if ( TrackingBranches . Count > 0 )
59- SelectedTrackingBranch = TrackingBranches [ 0 ] ;
77+ if ( LocalBranches . Count > 0 )
78+ SelectedBranch = LocalBranches [ 0 ] ;
79+ else
80+ SelectedBranch = string . Empty ;
81+
82+ if ( RemoteBranches . Count > 0 )
83+ SelectedTrackingBranch = RemoteBranches [ 0 ] ;
6084 else
6185 SelectedTrackingBranch = string . Empty ;
6286
@@ -81,25 +105,6 @@ public static ValidationResult ValidateWorktreePath(string path, ValidationConte
81105 if ( folders . Length > 0 )
82106 return new ValidationResult ( "Given path is not empty!!!" ) ;
83107 }
84-
85- return ValidationResult . Success ;
86- }
87-
88- public static ValidationResult ValidateBranchName ( string name , ValidationContext ctx )
89- {
90- if ( string . IsNullOrEmpty ( name ) )
91- return ValidationResult . Success ;
92-
93- var creator = ctx . ObjectInstance as AddWorktree ;
94- if ( creator == null )
95- return new ValidationResult ( "Missing runtime context to create branch!" ) ;
96-
97- foreach ( var b in creator . _repo . Branches )
98- {
99- var test = b . IsLocal ? b . Name : $ "{ b . Remote } /{ b . Name } ";
100- if ( test == name )
101- return new ValidationResult ( "A branch with same name already exists!" ) ;
102- }
103108
104109 return ValidationResult . Success ;
105110 }
@@ -113,15 +118,16 @@ public override Task<bool> Sure()
113118
114119 return Task . Run ( ( ) =>
115120 {
116- var succ = new Commands . Worktree ( _repo . FullPath ) . Add ( _path , _customName , tracking , SetProgressDescription ) ;
121+ var succ = new Commands . Worktree ( _repo . FullPath ) . Add ( _path , _selectedBranch , tracking , SetProgressDescription ) ;
117122 CallUIThread ( ( ) => _repo . SetWatcherEnabled ( true ) ) ;
118123 return succ ;
119124 } ) ;
120125 }
121126
122127 private Repository _repo = null ;
123128 private string _path = string . Empty ;
124- private string _customName = string . Empty ;
129+ private bool _useExistingBranch = true ;
130+ private string _selectedBranch = string . Empty ;
125131 private bool _setTrackingBranch = false ;
126132 }
127133}
0 commit comments