forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPull.cs
More file actions
204 lines (176 loc) · 6.48 KB
/
Copy pathPull.cs
File metadata and controls
204 lines (176 loc) · 6.48 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
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class Pull : Popup
{
public List<Models.Remote> Remotes => _repo.Remotes;
public Models.Branch Current { get; }
public bool HasSpecifiedRemoteBranch
{
get;
private set;
}
public Models.Remote SelectedRemote
{
get => _selectedRemote;
set
{
if (SetProperty(ref _selectedRemote, value))
PostRemoteSelected();
}
}
public List<Models.Branch> RemoteBranches
{
get => _remoteBranches;
private set => SetProperty(ref _remoteBranches, value);
}
[Required(ErrorMessage = "Remote branch to pull is required!!!")]
public Models.Branch SelectedBranch
{
get => _selectedBranch;
set => SetProperty(ref _selectedBranch, value, true);
}
public bool DiscardLocalChanges
{
get;
set;
} = false;
public bool UseRebase
{
get => _repo.UIStates.PreferRebaseInsteadOfMerge;
set => _repo.UIStates.PreferRebaseInsteadOfMerge = value;
}
public Pull(Repository repo, Models.Branch specifiedRemoteBranch)
{
_repo = repo;
Current = repo.CurrentBranch;
if (specifiedRemoteBranch != null)
{
_selectedRemote = repo.Remotes.Find(x => x.Name == specifiedRemoteBranch.Remote);
_selectedBranch = specifiedRemoteBranch;
var branches = new List<Models.Branch>();
foreach (var branch in _repo.Branches)
{
if (branch.Remote == specifiedRemoteBranch.Remote)
branches.Add(branch);
}
_remoteBranches = branches;
HasSpecifiedRemoteBranch = true;
}
else
{
Models.Remote autoSelectedRemote = null;
if (!string.IsNullOrEmpty(Current.Upstream))
{
var remoteNameEndIdx = Current.Upstream.IndexOf('/', 13);
if (remoteNameEndIdx > 0)
{
var remoteName = Current.Upstream.Substring(13, remoteNameEndIdx - 13);
autoSelectedRemote = _repo.Remotes.Find(x => x.Name == remoteName);
}
}
if (autoSelectedRemote == null)
{
Models.Remote remote = null;
if (!string.IsNullOrEmpty(_repo.Settings.DefaultRemote))
remote = _repo.Remotes.Find(x => x.Name == _repo.Settings.DefaultRemote);
_selectedRemote = remote ?? _repo.Remotes[0];
}
else
{
_selectedRemote = autoSelectedRemote;
}
PostRemoteSelected();
HasSpecifiedRemoteBranch = false;
}
}
public override async Task<bool> Sure()
{
using var lockWatcher = _repo.LockWatcher();
var log = _repo.CreateLog("Pull");
Use(log);
var changes = await new Commands.CountLocalChanges(_repo.FullPath, false).GetResultAsync();
var needPopStash = false;
if (changes > 0)
{
if (DiscardLocalChanges)
{
await Commands.Discard.AllAsync(_repo.FullPath, false, false, log);
}
else
{
var succ = await new Commands.Stash(_repo.FullPath).Use(log).PushAsync("PULL_AUTO_STASH", false);
if (!succ)
{
log.Complete();
return false;
}
needPopStash = true;
}
}
bool rs = await new Commands.Pull(
_repo.FullPath,
_selectedRemote.Name,
!string.IsNullOrEmpty(Current.Upstream) && Current.Upstream.Equals(_selectedBranch.FullName) ? string.Empty : _selectedBranch.Name,
UseRebase).Use(log).RunAsync();
if (rs)
{
await _repo.AutoUpdateSubmodulesAsync(log);
if (needPopStash)
await new Commands.Stash(_repo.FullPath).Use(log).PopAsync("stash@{0}");
}
log.Complete();
if (_repo.SelectedViewIndex == 0)
{
var head = await new Commands.QueryRevisionByRefName(_repo.FullPath, "HEAD").GetResultAsync();
_repo.NavigateToCommit(head, true);
}
return rs;
}
private void PostRemoteSelected()
{
var remoteName = _selectedRemote.Name;
var branches = new List<Models.Branch>();
foreach (var branch in _repo.Branches)
{
if (branch.Remote == remoteName)
branches.Add(branch);
}
RemoteBranches = branches;
var autoSelectedBranch = false;
if (!string.IsNullOrEmpty(Current.Upstream) &&
Current.Upstream.StartsWith($"refs/remotes/{remoteName}/", System.StringComparison.Ordinal))
{
foreach (var branch in branches)
{
if (Current.Upstream == branch.FullName)
{
SelectedBranch = branch;
autoSelectedBranch = true;
break;
}
}
}
if (!autoSelectedBranch)
{
foreach (var branch in branches)
{
if (Current.Name == branch.Name)
{
SelectedBranch = branch;
autoSelectedBranch = true;
break;
}
}
}
if (!autoSelectedBranch)
SelectedBranch = null;
}
private readonly Repository _repo = null;
private Models.Remote _selectedRemote = null;
private List<Models.Branch> _remoteBranches = null;
private Models.Branch _selectedBranch = null;
}
}