2024-02-05 23:08:37 -08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class Pull : Popup
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
public List<Models.Remote> Remotes => _repo.Remotes;
|
|
|
|
|
public Models.Branch Current => _current;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public bool HasSpecifiedRemoteBranch
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get;
|
|
|
|
|
private set;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public Models.Remote SelectedRemote
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _selectedRemote;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (SetProperty(ref _selectedRemote, value))
|
2024-08-08 23:03:31 -07:00
|
|
|
|
PostRemoteSelected();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public List<Models.Branch> RemoteBranches
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _remoteBranches;
|
|
|
|
|
private set => SetProperty(ref _remoteBranches, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Required(ErrorMessage = "Remote branch to pull is required!!!")]
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public Models.Branch SelectedBranch
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _selectedBranch;
|
|
|
|
|
set => SetProperty(ref _selectedBranch, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 01:42:47 -07:00
|
|
|
|
public Models.DealWithLocalChanges PreAction
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-07-02 23:41:43 -07:00
|
|
|
|
get => _repo.Settings.DealWithLocalChangesOnPull;
|
|
|
|
|
set => _repo.Settings.DealWithLocalChangesOnPull = value;
|
2024-05-29 01:42:47 -07:00
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-05-29 01:42:47 -07:00
|
|
|
|
public bool UseRebase
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-06-30 20:57:13 -07:00
|
|
|
|
get => _repo.Settings.PreferRebaseInsteadOfMerge;
|
|
|
|
|
set => _repo.Settings.PreferRebaseInsteadOfMerge = value;
|
2024-06-19 00:36:49 -07:00
|
|
|
|
}
|
2024-07-31 01:26:58 -07:00
|
|
|
|
|
2024-07-27 05:35:19 -07:00
|
|
|
|
public bool FetchAllBranches
|
2024-07-27 02:52:01 -07:00
|
|
|
|
{
|
|
|
|
|
get => _repo.Settings.FetchAllBranchesOnPull;
|
|
|
|
|
set => _repo.Settings.FetchAllBranchesOnPull = value;
|
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-07-01 01:19:08 -07:00
|
|
|
|
public bool NoTags
|
|
|
|
|
{
|
2024-07-02 23:41:43 -07:00
|
|
|
|
get => _repo.Settings.FetchWithoutTagsOnPull;
|
|
|
|
|
set => _repo.Settings.FetchWithoutTagsOnPull = value;
|
|
|
|
|
}
|
2024-07-01 01:19:08 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public Pull(Repository repo, Models.Branch specifiedRemoteBranch)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo = repo;
|
2024-07-24 00:36:26 -07:00
|
|
|
|
_current = repo.CurrentBranch;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
|
|
|
|
|
if (specifiedRemoteBranch != null)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_selectedRemote = repo.Remotes.Find(x => x.Name == specifiedRemoteBranch.Remote);
|
|
|
|
|
_selectedBranch = specifiedRemoteBranch;
|
2024-08-08 23:03:31 -07:00
|
|
|
|
|
|
|
|
|
var branches = new List<Models.Branch>();
|
|
|
|
|
foreach (var branch in _repo.Branches)
|
|
|
|
|
{
|
|
|
|
|
if (branch.Remote == specifiedRemoteBranch.Remote)
|
|
|
|
|
branches.Add(branch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_remoteBranches = branches;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
HasSpecifiedRemoteBranch = true;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-08-08 23:03:31 -07:00
|
|
|
|
var autoSelectedRemote = null as Models.Remote;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (!string.IsNullOrEmpty(_current.Upstream))
|
|
|
|
|
{
|
2024-08-08 23:03:31 -07:00
|
|
|
|
var remoteNameEndIdx = _current.Upstream.IndexOf('/', 13);
|
|
|
|
|
if (remoteNameEndIdx > 0)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-08-08 23:03:31 -07:00
|
|
|
|
var remoteName = _current.Upstream.Substring(13, remoteNameEndIdx - 13);
|
|
|
|
|
autoSelectedRemote = _repo.Remotes.Find(x => x.Name == remoteName);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-10-02 00:27:09 -07:00
|
|
|
|
if (autoSelectedRemote == null)
|
|
|
|
|
{
|
|
|
|
|
var remote = null as Models.Remote;
|
|
|
|
|
if (!string.IsNullOrEmpty(_repo.Settings.DefaultRemote))
|
|
|
|
|
remote = _repo.Remotes.Find(x => x.Name == _repo.Settings.DefaultRemote);
|
|
|
|
|
_selectedRemote = remote ?? _repo.Remotes[0];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_selectedRemote = autoSelectedRemote;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 23:03:31 -07:00
|
|
|
|
PostRemoteSelected();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
HasSpecifiedRemoteBranch = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
View = new Views.Pull() { DataContext = this };
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public override Task<bool> Sure()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo.SetWatcherEnabled(false);
|
2024-07-02 23:41:43 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
return Task.Run(() =>
|
|
|
|
|
{
|
2024-08-07 00:52:58 -07:00
|
|
|
|
var changes = new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).Result();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var needPopStash = false;
|
2024-08-07 00:52:58 -07:00
|
|
|
|
if (changes > 0)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-07-02 23:41:43 -07:00
|
|
|
|
if (PreAction == Models.DealWithLocalChanges.StashAndReaply)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-08-07 00:20:21 -07:00
|
|
|
|
SetProgressDescription("Stash local changes...");
|
|
|
|
|
var succ = new Commands.Stash(_repo.FullPath).Push("PULL_AUTO_STASH");
|
2024-05-29 01:42:47 -07:00
|
|
|
|
if (!succ)
|
|
|
|
|
{
|
|
|
|
|
CallUIThread(() => _repo.SetWatcherEnabled(true));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
|
2024-05-29 01:42:47 -07:00
|
|
|
|
needPopStash = true;
|
2024-06-06 00:31:02 -07:00
|
|
|
|
}
|
2024-07-02 23:41:43 -07:00
|
|
|
|
else if (PreAction == Models.DealWithLocalChanges.Discard)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-05-29 01:42:47 -07:00
|
|
|
|
SetProgressDescription("Discard local changes ...");
|
|
|
|
|
Commands.Discard.All(_repo.FullPath);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 00:27:09 -07:00
|
|
|
|
bool rs;
|
2024-07-27 05:35:19 -07:00
|
|
|
|
if (FetchAllBranches)
|
2024-07-27 02:52:01 -07:00
|
|
|
|
{
|
|
|
|
|
SetProgressDescription($"Fetching remote: {_selectedRemote.Name}...");
|
2024-07-27 05:35:19 -07:00
|
|
|
|
rs = new Commands.Fetch(_repo.FullPath, _selectedRemote.Name, false, NoTags, SetProgressDescription).Exec();
|
|
|
|
|
if (!rs)
|
|
|
|
|
return false;
|
|
|
|
|
|
2024-09-25 19:50:21 -07:00
|
|
|
|
_repo.MarkFetched();
|
|
|
|
|
|
2024-07-27 05:35:19 -07:00
|
|
|
|
// Use merge/rebase instead of pull as fetch is done manually.
|
|
|
|
|
if (UseRebase)
|
|
|
|
|
{
|
|
|
|
|
SetProgressDescription($"Rebase {_current.Name} on {_selectedBranch.FriendlyName} ...");
|
|
|
|
|
rs = new Commands.Rebase(_repo.FullPath, _selectedBranch.FriendlyName, false).Exec();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SetProgressDescription($"Merge {_selectedBranch.FriendlyName} into {_current.Name} ...");
|
|
|
|
|
rs = new Commands.Merge(_repo.FullPath, _selectedBranch.FriendlyName, "", SetProgressDescription).Exec();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SetProgressDescription($"Pull {_selectedRemote.Name}/{_selectedBranch.Name}...");
|
|
|
|
|
rs = new Commands.Pull(_repo.FullPath, _selectedRemote.Name, _selectedBranch.Name, UseRebase, NoTags, SetProgressDescription).Exec();
|
2024-07-27 02:52:01 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (rs && needPopStash)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SetProgressDescription("Re-apply local changes...");
|
|
|
|
|
rs = new Commands.Stash(_repo.FullPath).Pop("stash@{0}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CallUIThread(() => _repo.SetWatcherEnabled(true));
|
|
|
|
|
return rs;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 23:03:31 -07:00
|
|
|
|
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;
|
2024-08-08 23:08:25 -07:00
|
|
|
|
if (!string.IsNullOrEmpty(_current.Upstream) &&
|
|
|
|
|
_current.Upstream.StartsWith($"refs/remotes/{remoteName}/", System.StringComparison.Ordinal))
|
2024-08-08 23:03:31 -07:00
|
|
|
|
{
|
2024-08-08 23:08:25 -07:00
|
|
|
|
foreach (var branch in branches)
|
2024-08-08 23:03:31 -07:00
|
|
|
|
{
|
2024-08-08 23:08:25 -07:00
|
|
|
|
if (_current.Upstream == branch.FullName)
|
2024-08-08 23:03:31 -07:00
|
|
|
|
{
|
2024-08-08 23:08:25 -07:00
|
|
|
|
SelectedBranch = branch;
|
|
|
|
|
autoSelectedBranch = true;
|
|
|
|
|
break;
|
2024-08-08 23:03:31 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-08 23:08:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!autoSelectedBranch)
|
|
|
|
|
{
|
|
|
|
|
foreach (var branch in branches)
|
2024-08-08 23:03:31 -07:00
|
|
|
|
{
|
2024-08-08 23:08:25 -07:00
|
|
|
|
if (_current.Name == branch.Name)
|
2024-08-08 23:03:31 -07:00
|
|
|
|
{
|
2024-08-08 23:08:25 -07:00
|
|
|
|
SelectedBranch = branch;
|
|
|
|
|
autoSelectedBranch = true;
|
|
|
|
|
break;
|
2024-08-08 23:03:31 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!autoSelectedBranch)
|
2024-09-13 02:27:56 -07:00
|
|
|
|
SelectedBranch = null;
|
2024-08-08 23:03:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly Repository _repo = null;
|
|
|
|
|
private readonly Models.Branch _current = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
private Models.Remote _selectedRemote = null;
|
|
|
|
|
private List<Models.Branch> _remoteBranches = null;
|
|
|
|
|
private Models.Branch _selectedBranch = null;
|
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|