Repository navigation
Expand file tree
/
Copy pathFetch.cs
More file actions
164 lines (139 loc) · 4.99 KB
/
Copy pathFetch.cs
File metadata and controls
164 lines (139 loc) · 4.99 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
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class Fetch : Popup
{
public List<Models.Remote> Remotes
{
get => _repo.Remotes;
}
public bool IsFetchAllRemoteVisible
{
get;
}
public bool FetchAllRemotes
{
get => _fetchAllRemotes;
set => SetProperty(ref _fetchAllRemotes, value);
}
public Models.Remote SelectedRemote
{
get;
set;
}
public bool NoTags
{
get => _noTags;
set => SetProperty(ref _noTags, value);
}
public bool Force
{
get => _force;
set => SetProperty(ref _force, value);
}
public override bool HasDefaultOptions
{
get => true;
}
public Fetch(Repository repo, Models.Remote preferredRemote = null)
{
_repo = repo;
IsFetchAllRemoteVisible = repo.Remotes.Count > 1 && preferredRemote == null;
_fetchAllRemotes = IsFetchAllRemoteVisible && _repo.UIStates.FetchAllRemotes;
_noTags = _repo.UIStates.FetchWithoutTags;
_force = _repo.UIStates.EnableForceOnFetch;
CanTerminate = true;
if (preferredRemote != null)
{
SelectedRemote = preferredRemote;
}
else if (!string.IsNullOrEmpty(_repo.Settings.DefaultRemote))
{
var def = _repo.Remotes.Find(r => r.Name == _repo.Settings.DefaultRemote);
SelectedRemote = def ?? _repo.Remotes[0];
}
else
{
SelectedRemote = _repo.Remotes[0];
}
}
public override void SaveOptionsAsDefault()
{
if (IsFetchAllRemoteVisible)
_repo.UIStates.FetchAllRemotes = _fetchAllRemotes;
_repo.UIStates.FetchWithoutTags = _noTags;
_repo.UIStates.EnableForceOnFetch = _force;
_repo.UIStates.Save();
}
public override void ResetDefaultOptions()
{
var defaults = new Models.RepositoryUIStates();
_repo.UIStates.FetchAllRemotes = defaults.FetchAllRemotes;
_repo.UIStates.FetchWithoutTags = defaults.FetchWithoutTags;
_repo.UIStates.EnableForceOnFetch = defaults.EnableForceOnFetch;
_repo.UIStates.Save();
FetchAllRemotes = IsFetchAllRemoteVisible && defaults.FetchAllRemotes;
NoTags = defaults.FetchWithoutTags;
Force = defaults.EnableForceOnFetch;
}
public override async Task<bool> Sure()
{
using var lockWatcher = _repo.LockWatcher();
var navigateToUpstreamHEAD = _repo.IsDashboardVisible &&
_repo.Histories.SelectedCommits.Count == 1 &&
_repo.Histories.SelectedCommits[0].IsCurrentHead;
var notags = _noTags;
var force = _force;
var log = _repo.CreateLog("Fetch");
Use(log);
_cancellation = new CancellationTokenSource();
var token = _cancellation.Token;
var succ = true;
if (FetchAllRemotes)
{
foreach (var remote in _repo.Remotes)
{
succ &= await new Commands.Fetch(_repo.FullPath, remote, notags, force)
.WithCancellation(token)
.Use(log)
.ExecAsync();
if (token.IsCancellationRequested)
break;
}
}
else
{
succ = await new Commands.Fetch(_repo.FullPath, SelectedRemote, notags, force)
.WithCancellation(token)
.Use(log)
.ExecAsync();
}
log.Complete();
if (navigateToUpstreamHEAD && !token.IsCancellationRequested)
{
var upstream = _repo.CurrentBranch?.Upstream;
if (!string.IsNullOrEmpty(upstream))
{
var upstreamHead = await new Commands.QueryRevisionByRefName(_repo.FullPath, upstream.Substring(13)).GetResultAsync();
_repo.NavigateToCommit(upstreamHead, true);
}
}
if (!token.IsCancellationRequested)
_repo.MarkFetched();
_cancellation = null;
return succ;
}
public override void Terminate()
{
// Just fire cancel event and UI will auto wait the `Sure` complete
var _ = _cancellation?.CancelAsync();
}
private readonly Repository _repo = null;
private bool _fetchAllRemotes = false;
private bool _noTags = false;
private bool _force = false;
private CancellationTokenSource _cancellation = null;
}
}