-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathCreateBranch.cs
More file actions
249 lines (216 loc) · 8.03 KB
/
CreateBranch.cs
File metadata and controls
249 lines (216 loc) · 8.03 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
240
241
242
243
244
245
246
247
248
249
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 HasLocalChanges
{
get => _repo.LocalChangesCount > 0;
}
public Models.DealWithLocalChanges DealWithLocalChanges
{
get;
set;
}
public bool CheckoutAfterCreated
{
get => _repo.UIStates.CheckoutBranchOnCreateBranch;
set
{
if (_repo.UIStates.CheckoutBranchOnCreateBranch != value)
{
_repo.UIStates.CheckoutBranchOnCreateBranch = value;
OnPropertyChanged();
UpdateOverrideTip();
}
}
}
public bool IsBareRepository
{
get => _repo.IsBare;
}
public string OverrideTip
{
get => _overrideTip;
private set => SetProperty(ref _overrideTip, value);
}
public bool AllowOverwrite
{
get => _allowOverwrite;
set
{
if (SetProperty(ref _allowOverwrite, value))
ValidateProperty(_name, nameof(Name));
}
}
public CreateBranch(Repository repo, Models.Branch branch)
{
_repo = repo;
_baseOnRevision = branch.Head;
_committerDate = branch.CommitterDate;
_head = branch.Head;
if (!branch.IsLocal)
Name = branch.Name;
BasedOn = branch;
DealWithLocalChanges = Models.DealWithLocalChanges.DoNothing;
UpdateOverrideTip();
}
public CreateBranch(Repository repo, Models.Commit commit)
{
_repo = repo;
_baseOnRevision = commit.SHA;
_committerDate = commit.CommitterTime;
_head = commit.SHA;
BasedOn = commit;
DealWithLocalChanges = Models.DealWithLocalChanges.DoNothing;
UpdateOverrideTip();
}
public CreateBranch(Repository repo, Models.Tag tag)
{
_repo = repo;
_baseOnRevision = tag.SHA;
_committerDate = tag.CreatorDate;
_head = tag.SHA;
BasedOn = tag;
DealWithLocalChanges = Models.DealWithLocalChanges.DoNothing;
UpdateOverrideTip();
}
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;
}
}
}
Models.Branch created = new()
{
Name = _name,
FullName = $"refs/heads/{_name}",
CommitterDate = _committerDate,
Head = _head,
IsLocal = true,
};
bool succ;
if (CheckoutAfterCreated && !_repo.IsBare)
{
var needPopStash = false;
if (DealWithLocalChanges == Models.DealWithLocalChanges.DoNothing)
{
succ = await new Commands.Checkout(_repo.FullPath)
.Use(log)
.BranchAsync(_name, _baseOnRevision, false, _allowOverwrite);
}
else if (DealWithLocalChanges == Models.DealWithLocalChanges.StashAndReapply)
{
var changes = await new Commands.CountLocalChanges(_repo.FullPath, false).GetResultAsync();
if (changes > 0)
{
succ = await new Commands.Stash(_repo.FullPath)
.Use(log)
.PushAsync("CREATE_BRANCH_AUTO_STASH", false);
if (!succ)
{
log.Complete();
_repo.MarkWorkingCopyDirtyManually();
return false;
}
needPopStash = true;
}
succ = await new Commands.Checkout(_repo.FullPath)
.Use(log)
.BranchAsync(_name, _baseOnRevision, false, _allowOverwrite);
}
else
{
succ = await new Commands.Checkout(_repo.FullPath)
.Use(log)
.BranchAsync(_name, _baseOnRevision, true, _allowOverwrite);
}
if (succ)
{
await _repo.AutoUpdateSubmodulesAsync(log);
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)
{
if (BasedOn is Models.Branch { IsLocal: false } basedOn && _name.Equals(basedOn.Name, StringComparison.Ordinal))
{
await new Commands.Branch(_repo.FullPath, _name)
.Use(log)
.SetUpstreamAsync(basedOn);
created.Upstream = basedOn.FullName;
}
_repo.RefreshAfterCreateBranch(created, CheckoutAfterCreated);
}
else
{
_repo.MarkWorkingCopyDirtyManually();
}
log.Complete();
return true;
}
private void UpdateOverrideTip()
{
OverrideTip = CheckoutAfterCreated ? "-B in `git checkout`" : "-f in `git branch`";
}
private readonly Repository _repo = null;
private readonly string _baseOnRevision = null;
private readonly ulong _committerDate = 0;
private readonly string _head = string.Empty;
private string _name = null;
private string _overrideTip = "-B";
private bool _allowOverwrite = false;
}
}