forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddWorktree.cs
More file actions
178 lines (150 loc) · 5.39 KB
/
Copy pathAddWorktree.cs
File metadata and controls
178 lines (150 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class AddWorktree : Popup
{
[Required(ErrorMessage = "Worktree path is required!")]
[CustomValidation(typeof(AddWorktree), nameof(ValidateWorktreePath))]
public string Path
{
get => _path;
set => SetProperty(ref _path, value, true);
}
public bool CreateNewBranch
{
get => _createNewBranch;
set
{
if (SetProperty(ref _createNewBranch, value, true))
{
if (value)
SelectedBranch = null;
else
SelectedBranch = LocalBranches.Count > 0 ? LocalBranches[0] : null;
}
}
}
public List<Models.Branch> LocalBranches
{
get;
private set;
}
public Models.Branch SelectedBranch
{
get => _selectedBranch;
set => SetProperty(ref _selectedBranch, value);
}
public string NewBranchName
{
get => _newBranchName;
set => SetProperty(ref _newBranchName, value);
}
public bool SetTrackingBranch
{
get => _setTrackingBranch;
set
{
if (SetProperty(ref _setTrackingBranch, value))
AutoSelectTrackingBranch();
}
}
public List<Models.Branch> RemoteBranches
{
get;
private set;
}
public Models.Branch SelectedTrackingBranch
{
get => _selectedTrackingBranch;
set => SetProperty(ref _selectedTrackingBranch, value);
}
public AddWorktree(Repository repo)
{
_repo = repo;
LocalBranches = new List<Models.Branch>();
RemoteBranches = new List<Models.Branch>();
foreach (var branch in repo.Branches)
{
if (branch.IsLocal)
LocalBranches.Add(branch);
else
RemoteBranches.Add(branch);
}
}
public static ValidationResult ValidateWorktreePath(string path, ValidationContext ctx)
{
if (ctx.ObjectInstance is not AddWorktree creator)
return new ValidationResult("Missing runtime context to create branch!");
if (string.IsNullOrEmpty(path))
return new ValidationResult("Worktree path is required!");
var fullPath = System.IO.Path.IsPathRooted(path) ? path : System.IO.Path.Combine(creator._repo.FullPath, path);
var info = new DirectoryInfo(fullPath);
if (info.Exists)
{
var files = info.GetFiles();
if (files.Length > 0)
return new ValidationResult("Given path is not empty!!!");
var folders = info.GetDirectories();
if (folders.Length > 0)
return new ValidationResult("Given path is not empty!!!");
}
return ValidationResult.Success;
}
public override async Task<bool> Sure()
{
using var lockWatcher = _repo.LockWatcher();
ProgressDescription = "Adding worktree ...";
var branchName = GetBranchName(false);
var tracking = (_setTrackingBranch && _selectedTrackingBranch != null) ? _selectedTrackingBranch.FriendlyName : string.Empty;
var log = _repo.CreateLog("Add Worktree");
Use(log);
var succ = await new Commands.Worktree(_repo.FullPath)
.Use(log)
.AddAsync(_path, branchName, _createNewBranch, tracking);
log.Complete();
return succ;
}
private void AutoSelectTrackingBranch()
{
if (!_setTrackingBranch || RemoteBranches.Count == 0)
return;
var name = GetBranchName(true);
if (!string.IsNullOrEmpty(name))
{
var remoteBranch = RemoteBranches.Find(b => b.Name.EndsWith(name, StringComparison.Ordinal));
if (remoteBranch != null)
{
SelectedTrackingBranch = remoteBranch;
return;
}
}
SelectedTrackingBranch = RemoteBranches[0];
}
private string GetBranchName(bool fallback)
{
do
{
if (!_createNewBranch)
{
if (_selectedBranch != null)
return _selectedBranch.Name;
break;
}
if (!string.IsNullOrEmpty(_newBranchName))
return _newBranchName;
} while (false);
return fallback ? System.IO.Path.GetFileName(_path.TrimEnd('/', '\\')) : string.Empty;
}
private Repository _repo = null;
private string _path = string.Empty;
private bool _createNewBranch = true;
private Models.Branch _selectedBranch = null;
private string _newBranchName = string.Empty;
private bool _setTrackingBranch = false;
private Models.Branch _selectedTrackingBranch = null;
}
}