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-02-05 23:08:37 -08:00
|
|
|
|
var branches = new List<Models.Branch>();
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var branch in _repo.Branches)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (branch.Remote == value.Name)
|
|
|
|
|
branches.Add(branch);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
RemoteBranches = branches;
|
|
|
|
|
SelectedBranch = branches.Count > 0 ? branches[0] : null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-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;
|
|
|
|
|
_current = repo.Branches.Find(x => x.IsCurrent);
|
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;
|
|
|
|
|
HasSpecifiedRemoteBranch = true;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(_current.Upstream))
|
|
|
|
|
{
|
|
|
|
|
foreach (var branch in repo.Branches)
|
|
|
|
|
{
|
|
|
|
|
if (!branch.IsLocal && _current.Upstream == branch.FullName)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_selectedRemote = repo.Remotes.Find(x => x.Name == branch.Remote);
|
|
|
|
|
_selectedBranch = branch;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
|
|
HasSpecifiedRemoteBranch = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure remote is exists.
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (_selectedRemote == null)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_selectedRemote = repo.Remotes[0];
|
|
|
|
|
_selectedBranch = null;
|
|
|
|
|
HasSpecifiedRemoteBranch = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_remoteBranches = new List<Models.Branch>();
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var branch in _repo.Branches)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (branch.Remote == _selectedRemote.Name)
|
|
|
|
|
_remoteBranches.Add(branch);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (_selectedBranch == null && _remoteBranches.Count > 0)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_selectedBranch = _remoteBranches[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-02-05 23:08:37 -08:00
|
|
|
|
var needPopStash = false;
|
2024-05-29 01:42:47 -07:00
|
|
|
|
if (_repo.WorkingCopyChangesCount > 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-05-29 01:42:47 -07:00
|
|
|
|
SetProgressDescription("Adding untracked changes...");
|
|
|
|
|
var succ = new Commands.Add(_repo.FullPath).Exec();
|
|
|
|
|
if (succ)
|
|
|
|
|
{
|
|
|
|
|
SetProgressDescription("Stash local changes...");
|
|
|
|
|
succ = new Commands.Stash(_repo.FullPath).Push("PULL_AUTO_STASH");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetProgressDescription($"Pull {_selectedRemote.Name}/{_selectedBranch.Name}...");
|
2024-07-01 01:19:08 -07:00
|
|
|
|
var rs = new Commands.Pull(_repo.FullPath, _selectedRemote.Name, _selectedBranch.Name, UseRebase, NoTags, SetProgressDescription).Exec();
|
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-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
|
|
|
|
}
|