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-08-08 23:03:31 -07:00
|
|
|
|
_selectedRemote = autoSelectedRemote ?? repo.Remotes[0];
|
|
|
|
|
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-07-27 05:35:19 -07:00
|
|
|
|
var rs = false;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
if (!string.IsNullOrEmpty(_current.Upstream))
|
|
|
|
|
{
|
|
|
|
|
if (_current.Upstream.StartsWith($"refs/remotes/{remoteName}/", System.StringComparison.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
foreach (var branch in branches)
|
|
|
|
|
{
|
|
|
|
|
if (_current.Upstream == branch.FullName)
|
|
|
|
|
{
|
|
|
|
|
SelectedBranch = branch;
|
|
|
|
|
autoSelectedBranch = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
foreach (var branch in branches)
|
|
|
|
|
{
|
|
|
|
|
if (_current.Name == branch.Name)
|
|
|
|
|
{
|
|
|
|
|
SelectedBranch = branch;
|
|
|
|
|
autoSelectedBranch = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!autoSelectedBranch)
|
|
|
|
|
SelectedBranch = branches.Count > 0 ? branches[0] : null;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|