feature: allow to ignore local changes also when switching branch or pulling changes (#151)

This commit is contained in:
leo 2024-05-29 16:42:47 +08:00
parent 69f9dceece
commit 0b09d210be
13 changed files with 253 additions and 212 deletions

View file

@ -10,10 +10,10 @@ namespace SourceGit.ViewModels
private set;
}
public bool AutoStash
public Models.DealWithLocalChanges PreAction
{
get => _autoStash;
set => SetProperty(ref _autoStash, value);
get => _preAction;
set => SetProperty(ref _preAction, value);
}
public Checkout(Repository repo, string branch)
@ -34,7 +34,7 @@ namespace SourceGit.ViewModels
var needPopStash = false;
if (hasLocalChanges)
{
if (AutoStash)
if (_preAction == Models.DealWithLocalChanges.StashAndReaply)
{
SetProgressDescription("Adding untracked changes ...");
var succ = new Commands.Add(_repo.FullPath).Exec();
@ -52,7 +52,7 @@ namespace SourceGit.ViewModels
needPopStash = true;
}
else
else if (_preAction == Models.DealWithLocalChanges.Discard)
{
SetProgressDescription("Discard local changes ...");
Commands.Discard.All(_repo.FullPath);
@ -78,6 +78,6 @@ namespace SourceGit.ViewModels
}
private readonly Repository _repo = null;
private bool _autoStash = true;
private Models.DealWithLocalChanges _preAction = Models.DealWithLocalChanges.StashAndReaply;
}
}

View file

@ -2,14 +2,7 @@
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public enum BeforeCreateBranchAction
{
StashAndReaply,
Discard,
DoNothing,
}
{
public class CreateBranch : Popup
{
[Required(ErrorMessage = "Branch name is required!")]
@ -27,7 +20,7 @@ namespace SourceGit.ViewModels
private set;
}
public BeforeCreateBranchAction PreAction
public Models.DealWithLocalChanges PreAction
{
get => _preAction;
set => SetProperty(ref _preAction, value);
@ -97,7 +90,7 @@ namespace SourceGit.ViewModels
bool needPopStash = false;
if (_repo.WorkingCopyChangesCount > 0)
{
if (_preAction == BeforeCreateBranchAction.StashAndReaply)
if (_preAction == Models.DealWithLocalChanges.StashAndReaply)
{
SetProgressDescription("Adding untracked changes...");
var succ = new Commands.Add(_repo.FullPath).Exec();
@ -115,7 +108,7 @@ namespace SourceGit.ViewModels
needPopStash = true;
}
else if (_preAction == BeforeCreateBranchAction.Discard)
else if (_preAction == Models.DealWithLocalChanges.Discard)
{
SetProgressDescription("Discard local changes...");
Commands.Discard.All(_repo.FullPath);
@ -144,6 +137,6 @@ namespace SourceGit.ViewModels
private readonly Repository _repo = null;
private string _name = null;
private readonly string _baseOnRevision = null;
private BeforeCreateBranchAction _preAction = BeforeCreateBranchAction.StashAndReaply;
private Models.DealWithLocalChanges _preAction = Models.DealWithLocalChanges.StashAndReaply;
}
}

View file

@ -513,7 +513,7 @@ namespace SourceGit.ViewModels
checkout.Icon = App.CreateMenuIcon("Icons.Check");
checkout.Click += (o, e) =>
{
_repo.CheckoutLocalBranch(branch.Name);
_repo.CheckoutBranch(branch);
e.Handled = true;
};
submenu.Items.Add(checkout);
@ -585,20 +585,7 @@ namespace SourceGit.ViewModels
checkout.Icon = App.CreateMenuIcon("Icons.Check");
checkout.Click += (o, e) =>
{
foreach (var b in _repo.Branches)
{
if (b.IsLocal && b.Upstream == branch.FullName)
{
if (!b.IsCurrent)
_repo.CheckoutLocalBranch(b.Name);
return;
}
}
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new CreateBranch(_repo, branch));
_repo.CheckoutBranch(branch);
e.Handled = true;
};
submenu.Items.Add(checkout);

View file

@ -47,13 +47,13 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _selectedBranch, value);
}
public bool UseRebase
public Models.DealWithLocalChanges PreAction
{
get;
set;
} = true;
get => _preAction;
set => SetProperty(ref _preAction, value);
}
public bool AutoStash
public bool UseRebase
{
get;
set;
@ -117,23 +117,31 @@ namespace SourceGit.ViewModels
return Task.Run(() =>
{
var needPopStash = false;
if (AutoStash && _repo.WorkingCopyChangesCount > 0)
if (_repo.WorkingCopyChangesCount > 0)
{
SetProgressDescription("Adding untracked changes...");
var succ = new Commands.Add(_repo.FullPath).Exec();
if (succ)
if (_preAction == Models.DealWithLocalChanges.StashAndReaply)
{
SetProgressDescription("Stash local changes...");
succ = new Commands.Stash(_repo.FullPath).Push("PULL_AUTO_STASH");
}
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)
if (!succ)
{
CallUIThread(() => _repo.SetWatcherEnabled(true));
return false;
}
needPopStash = true;
}
else if (_preAction == Models.DealWithLocalChanges.Discard)
{
CallUIThread(() => _repo.SetWatcherEnabled(true));
return false;
SetProgressDescription("Discard local changes ...");
Commands.Discard.All(_repo.FullPath);
}
needPopStash = true;
}
SetProgressDescription($"Pull {_selectedRemote.Name}/{_selectedBranch.Name}...");
@ -154,5 +162,6 @@ namespace SourceGit.ViewModels
private Models.Remote _selectedRemote = null;
private List<Models.Branch> _remoteBranches = null;
private Models.Branch _selectedBranch = null;
private Models.DealWithLocalChanges _preAction = Models.DealWithLocalChanges.StashAndReaply;
}
}

View file

@ -711,17 +711,35 @@ namespace SourceGit.ViewModels
PopupHost.ShowPopup(new CreateBranch(this, current));
}
public void CheckoutLocalBranch(string branch)
public void CheckoutBranch(Models.Branch branch)
{
if (!PopupHost.CanCreatePopup())
return;
if (WorkingCopyChangesCount > 0)
PopupHost.ShowPopup(new Checkout(this, branch));
if (branch.IsLocal)
{
if (WorkingCopyChangesCount > 0)
PopupHost.ShowPopup(new Checkout(this, branch.Name));
else
PopupHost.ShowAndStartPopup(new Checkout(this, branch.Name));
}
else
PopupHost.ShowAndStartPopup(new Checkout(this, branch));
{
foreach (var b in Branches)
{
if (b.IsLocal && b.Upstream == branch.FullName)
{
if (!b.IsCurrent)
CheckoutBranch(b);
return;
}
}
PopupHost.ShowPopup(new CreateBranch(this, branch));
}
}
public void DeleteMultipleBranches(List<Models.Branch> branches, bool isLocal)
{
if (PopupHost.CanCreatePopup())
@ -880,7 +898,7 @@ namespace SourceGit.ViewModels
checkout.Icon = App.CreateMenuIcon("Icons.Check");
checkout.Click += (o, e) =>
{
CheckoutLocalBranch(branch.Name);
CheckoutBranch(branch);
e.Handled = true;
};
menu.Items.Add(checkout);
@ -1182,20 +1200,7 @@ namespace SourceGit.ViewModels
checkout.Icon = App.CreateMenuIcon("Icons.Check");
checkout.Click += (o, e) =>
{
foreach (var b in Branches)
{
if (b.IsLocal && b.Upstream == branch.FullName)
{
if (b.IsCurrent)
return;
if (PopupHost.CanCreatePopup())
PopupHost.ShowAndStartPopup(new Checkout(this, b.Name));
return;
}
}
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new CreateBranch(this, branch));
CheckoutBranch(branch);
e.Handled = true;
};
menu.Items.Add(checkout);