forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateBranch.cs
More file actions
239 lines (203 loc) · 7.79 KB
/
Copy pathCreateBranch.cs
File metadata and controls
239 lines (203 loc) · 7.79 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class CreateBranch : Popup
{
[Required(ErrorMessage = "Branch name is required!")]
[RegularExpression(@"^[\w\-/\.#\+]+$", ErrorMessage = "Bad branch name format!")]
[CustomValidation(typeof(CreateBranch), nameof(ValidateBranchName))]
public string Name
{
get => _name;
set => SetProperty(ref _name, value, true);
}
public object BasedOn
{
get;
}
public bool DiscardLocalChanges
{
get;
set;
}
public bool CheckoutAfterCreated
{
get => _repo.Settings.CheckoutBranchOnCreateBranch;
set
{
if (_repo.Settings.CheckoutBranchOnCreateBranch != value)
{
_repo.Settings.CheckoutBranchOnCreateBranch = value;
OnPropertyChanged();
}
}
}
public bool IsBareRepository
{
get => _repo.IsBare;
}
public bool AllowOverwrite
{
get => _allowOverwrite;
set
{
if (SetProperty(ref _allowOverwrite, value))
ValidateProperty(_name, nameof(Name));
}
}
public bool IsRecurseSubmoduleVisible
{
get => _repo.Submodules.Count > 0;
}
public bool RecurseSubmodules
{
get => _repo.Settings.UpdateSubmodulesOnCheckoutBranch;
set => _repo.Settings.UpdateSubmodulesOnCheckoutBranch = value;
}
public CreateBranch(Repository repo, Models.Branch branch)
{
_repo = repo;
_baseOnRevision = branch.Head;
if (!branch.IsLocal && repo.Branches.Find(x => x.IsLocal && x.Name == branch.Name) == null)
Name = branch.Name;
BasedOn = branch;
DiscardLocalChanges = false;
}
public CreateBranch(Repository repo, Models.Commit commit)
{
_repo = repo;
_baseOnRevision = commit.SHA;
BasedOn = commit;
DiscardLocalChanges = false;
}
public CreateBranch(Repository repo, Models.Tag tag)
{
_repo = repo;
_baseOnRevision = tag.SHA;
BasedOn = tag;
DiscardLocalChanges = false;
}
public static ValidationResult ValidateBranchName(string name, ValidationContext ctx)
{
if (ctx.ObjectInstance is CreateBranch creator)
{
if (!creator._allowOverwrite)
{
foreach (var b in creator._repo.Branches)
{
if (b.FriendlyName.Equals(name, StringComparison.Ordinal))
return new ValidationResult("A branch with same name already exists!");
}
}
return ValidationResult.Success;
}
return new ValidationResult("Missing runtime context to create branch!");
}
public override async Task<bool> Sure()
{
using var lockWatcher = _repo.LockWatcher();
var log = _repo.CreateLog($"Create Branch '{_name}'");
Use(log);
if (CheckoutAfterCreated)
{
if (_repo.CurrentBranch is { IsDetachedHead: true } && !_repo.CurrentBranch.Head.Equals(_baseOnRevision, StringComparison.Ordinal))
{
var refs = await new Commands.QueryRefsContainsCommit(_repo.FullPath, _repo.CurrentBranch.Head).GetResultAsync();
if (refs.Count == 0)
{
var msg = App.Text("Checkout.WarnLostCommits");
var shouldContinue = await App.AskConfirmAsync(msg);
if (!shouldContinue)
return true;
}
}
}
bool succ;
if (CheckoutAfterCreated && !_repo.IsBare)
{
var needPopStash = false;
if (!DiscardLocalChanges)
{
var changes = await new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).GetResultAsync();
if (changes > 0)
{
succ = await new Commands.Stash(_repo.FullPath)
.Use(log)
.PushAsync("CREATE_BRANCH_AUTO_STASH");
if (!succ)
{
log.Complete();
return false;
}
needPopStash = true;
}
}
succ = await new Commands.Checkout(_repo.FullPath)
.Use(log)
.BranchAsync(_name, _baseOnRevision, DiscardLocalChanges, _allowOverwrite);
if (succ)
{
if (IsRecurseSubmoduleVisible && RecurseSubmodules)
{
var submodules = await new Commands.QueryUpdatableSubmodules(_repo.FullPath).GetResultAsync();
if (submodules.Count > 0)
await new Commands.Submodule(_repo.FullPath)
.Use(log)
.UpdateAsync(submodules, true, true);
}
if (needPopStash)
await new Commands.Stash(_repo.FullPath)
.Use(log)
.PopAsync("stash@{0}");
}
}
else
{
succ = await new Commands.Branch(_repo.FullPath, _name)
.Use(log)
.CreateAsync(_baseOnRevision, _allowOverwrite);
}
if (succ && BasedOn is Models.Branch { IsLocal: false } basedOn)
{
var autoSetUpstream = true;
foreach (var b in _repo.Branches)
{
if (b.IsLocal && b.Upstream.Equals(basedOn.FullName, StringComparison.Ordinal))
{
autoSetUpstream = false;
break;
}
}
if (autoSetUpstream)
await new Commands.Branch(_repo.FullPath, _name)
.Use(log)
.SetUpstreamAsync(basedOn);
}
log.Complete();
if (succ && CheckoutAfterCreated)
{
var fake = new Models.Branch() { IsLocal = true, FullName = $"refs/heads/{_name}" };
if (BasedOn is Models.Branch { IsLocal: false } based)
fake.Upstream = based.FullName;
var folderEndIdx = fake.FullName.LastIndexOf('/');
if (folderEndIdx > 10)
_repo.Settings.ExpandedBranchNodesInSideBar.Add(fake.FullName.Substring(0, folderEndIdx));
if (_repo.HistoriesFilterMode == Models.FilterMode.Included)
_repo.SetBranchFilterMode(fake, Models.FilterMode.Included, false, false);
}
_repo.MarkBranchesDirtyManually();
if (CheckoutAfterCreated)
{
ProgressDescription = "Waiting for branch updated...";
await Task.Delay(400);
}
return true;
}
private readonly Repository _repo = null;
private string _name = null;
private readonly string _baseOnRevision = null;
private bool _allowOverwrite = false;
}
}