11using System ;
2- using System . Collections . Generic ;
32using System . ComponentModel . DataAnnotations ;
43using System . IO ;
54using System . Threading . Tasks ;
@@ -8,98 +7,35 @@ namespace SourceGit.ViewModels
87{
98 public class AddWorktree : Popup
109 {
11- [ Required ( ErrorMessage = "Worktree path is required!" ) ]
1210 [ CustomValidation ( typeof ( AddWorktree ) , nameof ( ValidateWorktreePath ) ) ]
13- public string Path
11+ public string WorktreePath
1412 {
15- get => _path ;
16- set => SetProperty ( ref _path , value , true ) ;
13+ get => _worktreePath ;
14+ set => SetProperty ( ref _worktreePath , value , true ) ;
1715 }
1816
19- public bool CreateNewBranch
17+ [ CustomValidation ( typeof ( AddWorktree ) , nameof ( ValidateBranchName ) ) ]
18+ public string BranchName
2019 {
21- get => _createNewBranch ;
22- set
23- {
24- if ( SetProperty ( ref _createNewBranch , value , true ) )
25- {
26- if ( value )
27- SelectedBranch = null ;
28- else
29- SelectedBranch = LocalBranches . Count > 0 ? LocalBranches [ 0 ] : null ;
30- }
31- }
32- }
33-
34- public List < Models . Branch > LocalBranches
35- {
36- get ;
37- private set ;
38- }
39-
40- public Models . Branch SelectedBranch
41- {
42- get => _selectedBranch ;
43- set => SetProperty ( ref _selectedBranch , value ) ;
44- }
45-
46- public string NewBranchName
47- {
48- get => _newBranchName ;
49- set => SetProperty ( ref _newBranchName , value ) ;
50- }
51-
52- public bool SetTrackingBranch
53- {
54- get => _setTrackingBranch ;
55- set
56- {
57- if ( SetProperty ( ref _setTrackingBranch , value ) )
58- AutoSelectTrackingBranch ( ) ;
59- }
60- }
61-
62- public List < Models . Branch > RemoteBranches
63- {
64- get ;
65- private set ;
66- }
67-
68- public Models . Branch SelectedTrackingBranch
69- {
70- get => _selectedTrackingBranch ;
71- set => SetProperty ( ref _selectedTrackingBranch , value ) ;
20+ get => _branchName ;
21+ set => SetProperty ( ref _branchName , value , true ) ;
7222 }
7323
7424 public AddWorktree ( Repository repo )
7525 {
7626 _repo = repo ;
77-
78- LocalBranches = new List < Models . Branch > ( ) ;
79- RemoteBranches = new List < Models . Branch > ( ) ;
80- foreach ( var branch in repo . Branches )
81- {
82- if ( branch . IsLocal )
83- {
84- if ( ! branch . IsCurrent && ! branch . HasWorktree )
85- LocalBranches . Add ( branch ) ;
86- }
87- else
88- {
89- RemoteBranches . Add ( branch ) ;
90- }
91- }
27+ _worktreePath = Path . Combine ( repo . GetRecommandedWorktreeDir ( ) , "new-worktree" ) ;
9228 }
9329
9430 public static ValidationResult ValidateWorktreePath ( string path , ValidationContext ctx )
9531 {
9632 if ( ctx . ObjectInstance is not AddWorktree creator )
97- return new ValidationResult ( "Missing runtime context to create branch !" ) ;
33+ return new ValidationResult ( "Missing runtime context to add worktree !" ) ;
9834
9935 if ( string . IsNullOrEmpty ( path ) )
10036 return new ValidationResult ( "Worktree path is required!" ) ;
10137
102- var fullPath = System . IO . Path . IsPathRooted ( path ) ? path : System . IO . Path . Combine ( creator . _repo . FullPath , path ) ;
38+ var fullPath = Path . IsPathRooted ( path ) ? path : Path . Combine ( creator . _repo . FullPath , path ) ;
10339 var info = new DirectoryInfo ( fullPath ) ;
10440 if ( info . Exists )
10541 {
@@ -115,70 +51,42 @@ public static ValidationResult ValidateWorktreePath(string path, ValidationConte
11551 return ValidationResult . Success ;
11652 }
11753
118- public override async Task < bool > Sure ( )
54+ public static ValidationResult ValidateBranchName ( string name , ValidationContext ctx )
11955 {
120- using var lockWatcher = _repo . LockWatcher ( ) ;
121- ProgressDescription = "Adding worktree ..." ;
122-
123- var branchName = GetBranchName ( false ) ;
124- var tracking = ( _createNewBranch && _setTrackingBranch && _selectedTrackingBranch != null ) ? _selectedTrackingBranch . FriendlyName : string . Empty ;
125- var log = _repo . CreateLog ( "Add Worktree" ) ;
126-
127- Use ( log ) ;
128-
129- var succ = await new Commands . Worktree ( _repo . FullPath )
130- . Use ( log )
131- . AddAsync ( _path , branchName , _createNewBranch , tracking ) ;
132-
133- log . Complete ( ) ;
134- return succ ;
135- }
56+ if ( ctx . ObjectInstance is not AddWorktree creator )
57+ return new ValidationResult ( "Missing runtime context to add worktree!" ) ;
13658
137- private void AutoSelectTrackingBranch ( )
138- {
139- if ( ! _setTrackingBranch || RemoteBranches . Count == 0 )
140- return ;
59+ var test = ! string . IsNullOrEmpty ( name ) ? name : Path . GetFileName ( creator . _worktreePath . TrimEnd ( '/' ) . TrimEnd ( '\\ ' ) ) ;
60+ if ( ! Models . RefName . IsValidBranchName ( test ) )
61+ return new ValidationResult ( $ "{ test } is not a valid branch name!") ;
14162
142- var name = GetBranchName ( true ) ;
143- if ( ! string . IsNullOrEmpty ( name ) )
63+ foreach ( var b in creator . _repo . Branches )
14464 {
145- var remoteBranch = RemoteBranches . Find ( b => b . Name . Equals ( name , StringComparison . Ordinal ) ) ;
146- remoteBranch ??= RemoteBranches . Find ( b => b . Name . EndsWith ( "/" + name , StringComparison . Ordinal ) ) ;
147- if ( remoteBranch != null )
148- {
149- SelectedTrackingBranch = remoteBranch ;
150- return ;
151- }
65+ if ( b . IsLocal && b . Name . Equals ( test , StringComparison . Ordinal ) )
66+ return new ValidationResult ( $ "A branch with name '{ test } ' already exists!") ;
15267 }
15368
154- SelectedTrackingBranch = RemoteBranches [ 0 ] ;
69+ return ValidationResult . Success ;
15570 }
15671
157- private string GetBranchName ( bool fallback )
72+ public override async Task < bool > Sure ( )
15873 {
159- do
160- {
161- if ( ! _createNewBranch )
162- {
163- if ( _selectedBranch != null )
164- return _selectedBranch . Name ;
74+ using var lockWatcher = _repo . LockWatcher ( ) ;
75+ ProgressDescription = $ "Adding a new worktree ...";
16576
166- break ;
167- }
77+ var log = _repo . CreateLog ( "Add Worktree" ) ;
78+ Use ( log ) ;
16879
169- if ( ! string . IsNullOrEmpty ( _newBranchName ) )
170- return _newBranchName ;
171- } while ( false ) ;
80+ var succ = await new Commands . Worktree ( _repo . FullPath )
81+ . Use ( log )
82+ . AddAsync ( _worktreePath , _branchName ) ;
17283
173- return fallback ? System . IO . Path . GetFileName ( _path . TrimEnd ( '/' , '\\ ' ) ) : string . Empty ;
84+ log . Complete ( ) ;
85+ return succ ;
17486 }
17587
176- private Repository _repo = null ;
177- private string _path = string . Empty ;
178- private bool _createNewBranch = true ;
179- private Models . Branch _selectedBranch = null ;
180- private string _newBranchName = string . Empty ;
181- private bool _setTrackingBranch = false ;
182- private Models . Branch _selectedTrackingBranch = null ;
88+ private readonly Repository _repo = null ;
89+ private string _worktreePath = string . Empty ;
90+ private string _branchName = string . Empty ;
18391 }
18492}
0 commit comments