Add checkout local changes handling

This commit adds the local changes handling behavior for branch checkout.
One of three can be selected: stash and reapply after checkout, discard changes or leave them as is (previous behaviour)
This commit is contained in:
Alexander Bogomolets 2024-04-28 16:43:53 +03:00 committed by leo
parent 5e6059ba36
commit 927a1cab24
7 changed files with 101 additions and 5 deletions

View file

@ -53,6 +53,10 @@
<x:String x:Key="Text.ChangeDisplayMode.Tree" xml:space="preserve">Show as Tree</x:String>
<x:String x:Key="Text.Checkout" xml:space="preserve">Checkout Branch</x:String>
<x:String x:Key="Text.Checkout.Target" xml:space="preserve">Target :</x:String>
<x:String x:Key="Text.Checkout.LocalChanges" xml:space="preserve">Local Changes :</x:String>
<x:String x:Key="Text.Checkout.LocalChanges.StashAndReply" xml:space="preserve">Stash &amp; Reapply</x:String>
<x:String x:Key="Text.Checkout.LocalChanges.Discard" xml:space="preserve">Discard</x:String>
<x:String x:Key="Text.Checkout.LocalChanges.Leave" xml:space="preserve">Leave</x:String>
<x:String x:Key="Text.CherryPick" xml:space="preserve">Cherry-Pick This Commit</x:String>
<x:String x:Key="Text.CherryPick.Commit" xml:space="preserve">Commit :</x:String>
<x:String x:Key="Text.CherryPick.CommitChanges" xml:space="preserve">Commit all changes</x:String>

View file

@ -53,6 +53,10 @@
<x:String x:Key="Text.ChangeDisplayMode.Tree" xml:space="preserve">树形模式</x:String>
<x:String x:Key="Text.Checkout" xml:space="preserve">检出(checkout)分支</x:String>
<x:String x:Key="Text.Checkout.Target" xml:space="preserve">目标分支 </x:String>
<x:String x:Key="Text.Checkout.LocalChanges" xml:space="preserve">未提交更改 </x:String>
<x:String x:Key="Text.Checkout.LocalChanges.StashAndReply" xml:space="preserve">贮藏(stash)并自动恢复</x:String>
<x:String x:Key="Text.Checkout.LocalChanges.Discard" xml:space="preserve">忽略</x:String>
<x:String x:Key="Text.Checkout.LocalChanges.Leave" xml:space="preserve">保持原样</x:String>
<x:String x:Key="Text.CherryPick" xml:space="preserve">挑选(cherry-pick)此提交</x:String>
<x:String x:Key="Text.CherryPick.Commit" xml:space="preserve">提交ID </x:String>
<x:String x:Key="Text.CherryPick.CommitChanges" xml:space="preserve">提交变化</x:String>

View file

@ -9,27 +9,98 @@ namespace SourceGit.ViewModels
get;
private set;
}
public bool HasLocalChanges
{
get => _repo.WorkingCopyChangesCount > 0;
}
public bool LeaveLocalChanges
{
get => _leaveLocalChanges;
set => SetProperty(ref _leaveLocalChanges, value);
}
public bool DiscardLocalChanges
{
get => _discardLocalChanges;
set => SetProperty(ref _discardLocalChanges, value);
}
public bool StashLocalChanges
{
get => _stashLocalChanges;
set => SetProperty(ref _stashLocalChanges, value);
}
public Checkout(Repository repo, string branch)
{
_repo = repo;
Branch = branch;
View = new Views.Checkout() { DataContext = this };
StashLocalChanges = true;
}
public override Task<bool> Sure()
{
_repo.SetWatcherEnabled(false);
ProgressDescription = $"Checkout '{Branch}' ...";
var hasLocalChanges = HasLocalChanges;
return Task.Run(() =>
{
var succ = new Commands.Checkout(_repo.FullPath).Branch(Branch, SetProgressDescription);
var succ = false;
if (hasLocalChanges)
{
if (DiscardLocalChanges)
{
SetProgressDescription("Discard local changes...");
Commands.Discard.All(_repo.FullPath);
}
if (StashLocalChanges)
{
SetProgressDescription("Stash local changes...");
succ = new Commands.Add(_repo.FullPath).Exec();
succ = new Commands.Stash(_repo.FullPath).Push("CHECKOUT_AUTO_STASH");
}
}
SetProgressDescription("Checkout branch ...");
succ = new Commands.Checkout(_repo.FullPath).Branch(Branch, SetProgressDescription);
if(hasLocalChanges && StashLocalChanges)
{
SetProgressDescription("Re-apply local changes...");
succ = new Commands.Stash(_repo.FullPath).Apply("stash@{0}");
if (succ)
{
succ = new Commands.Stash(_repo.FullPath).Drop("stash@{0}");
}
}
CallUIThread(() => _repo.SetWatcherEnabled(true));
return succ;
});
}
public static void ShowPopup(Repository repo, string branch)
{
var checkout = new Checkout(repo, branch);
if (repo.WorkingCopyChangesCount > 0)
{
PopupHost.ShowPopup(checkout);
}
else
{
PopupHost.ShowAndStartPopup(checkout);
}
}
private readonly Repository _repo;
private bool _leaveLocalChanges;
private bool _discardLocalChanges;
private bool _stashLocalChanges;
}
}

View file

@ -448,7 +448,9 @@ namespace SourceGit.ViewModels
checkout.Click += (o, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowAndStartPopup(new Checkout(_repo, branch.Name));
{
Checkout.ShowPopup(_repo, branch.Name);
}
e.Handled = true;
};
submenu.Items.Add(checkout);
@ -527,7 +529,7 @@ namespace SourceGit.ViewModels
if (b.IsCurrent)
return;
if (PopupHost.CanCreatePopup())
PopupHost.ShowAndStartPopup(new Checkout(_repo, b.Name));
Checkout.ShowPopup(_repo, b.Name);
return;
}
}

View file

@ -843,7 +843,10 @@ namespace SourceGit.ViewModels
checkout.Click += (o, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowAndStartPopup(new Checkout(this, branch.Name));
{
Checkout.ShowPopup(this, branch.Name);
}
e.Handled = true;
};
menu.Items.Add(checkout);

View file

@ -15,5 +15,17 @@
<Path Width="14" Height="14" Margin="8,0" Data="{StaticResource Icons.Branch}"/>
<TextBlock Text="{Binding Branch}"/>
</StackPanel>
<StackPanel Orientation="Vertical" IsVisible="{Binding HasLocalChanges}">
<TextBlock Text="{DynamicResource Text.Checkout.LocalChanges}"/>
<RadioButton Content="{DynamicResource Text.Checkout.LocalChanges.StashAndReply}"
GroupName="LocalChanges"
IsChecked="{Binding StashLocalChanges, Mode=TwoWay }"/>
<RadioButton Content="{DynamicResource Text.Checkout.LocalChanges.Discard}"
GroupName="LocalChanges"
IsChecked="{Binding DiscardLocalChanges, Mode=TwoWay}"/>
<RadioButton Content="{DynamicResource Text.Checkout.LocalChanges.Leave}"
GroupName="LocalChanges"
IsChecked="{Binding LeaveLocalChanges, Mode=TwoWay}"/>
</StackPanel>
</StackPanel>
</UserControl>

View file

@ -298,7 +298,7 @@ namespace SourceGit.Views
if (branch.IsCurrent)
return;
ViewModels.PopupHost.ShowAndStartPopup(new ViewModels.Checkout(repo, branch.Name));
ViewModels.Checkout.ShowPopup(repo, branch.Name);
e.Handled = true;
}
}