enhance: cherry-pick (#563)

* supports to cherry-pick a merge commit
* add option to enable the `-x` parameter
This commit is contained in:
leo 2024-10-14 15:18:29 +08:00
parent 688f10e02f
commit 5fef6e93b9
No known key found for this signature in database
7 changed files with 144 additions and 19 deletions

View file

@ -2,12 +2,19 @@
{
public class CherryPick : Command
{
public CherryPick(string repo, string commits, bool noCommit)
public CherryPick(string repo, string commits, bool noCommit, bool appendSourceToMessage, string extraParams)
{
var mode = noCommit ? "-n" : "--ff";
WorkingDirectory = repo;
Context = repo;
Args = $"cherry-pick {mode} {commits}";
Args = "cherry-pick ";
if (noCommit)
Args += "-n ";
if (appendSourceToMessage)
Args += "-x ";
if (!string.IsNullOrEmpty(extraParams))
Args += $"{extraParams} ";
Args += commits;
}
}
}

View file

@ -83,8 +83,11 @@
<x:String x:Key="Text.Checkout.LocalChanges.DoNothing" xml:space="preserve">Do Nothing</x:String>
<x:String x:Key="Text.Checkout.LocalChanges.StashAndReply" xml:space="preserve">Stash &amp; Reapply</x:String>
<x:String x:Key="Text.CherryPick" xml:space="preserve">Cherry Pick</x:String>
<x:String x:Key="Text.CherryPick.AppendSourceToMessage" xml:space="preserve">Append source to commit message</x:String>
<x:String x:Key="Text.CherryPick.Commit" xml:space="preserve">Commit(s):</x:String>
<x:String x:Key="Text.CherryPick.CommitChanges" xml:space="preserve">Commit all changes</x:String>
<x:String x:Key="Text.CherryPick.Mainline" xml:space="preserve">Mainline:</x:String>
<x:String x:Key="Text.CherryPick.Mainline.Tips" xml:space="preserve">Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the mainline. This option allows cherry-pick to replay the change relative to the specified parent.</x:String>
<x:String x:Key="Text.ClearStashes" xml:space="preserve">Clear Stashes</x:String>
<x:String x:Key="Text.ClearStashes.Message" xml:space="preserve">You are trying to clear all stashes. Are you sure to continue?</x:String>
<x:String x:Key="Text.Clone" xml:space="preserve">Clone Remote Repository</x:String>

View file

@ -86,8 +86,11 @@
<x:String x:Key="Text.Checkout.LocalChanges.DoNothing" xml:space="preserve">不做处理</x:String>
<x:String x:Key="Text.Checkout.LocalChanges.StashAndReply" xml:space="preserve">贮藏并自动恢复</x:String>
<x:String x:Key="Text.CherryPick" xml:space="preserve">挑选提交</x:String>
<x:String x:Key="Text.CherryPick.AppendSourceToMessage" xml:space="preserve">提交信息中追加来源信息</x:String>
<x:String x:Key="Text.CherryPick.Commit" xml:space="preserve">提交列表 </x:String>
<x:String x:Key="Text.CherryPick.CommitChanges" xml:space="preserve">提交变化</x:String>
<x:String x:Key="Text.CherryPick.Mainline" xml:space="preserve">作为对比的父提交 </x:String>
<x:String x:Key="Text.CherryPick.Mainline.Tips" xml:space="preserve">通常你不能对一个合并进行挑选,因为你不知道合并的哪一边应该被视为主线。这个选项指定了作为主线的父提交,允许挑选相对于该提交的修改。</x:String>
<x:String x:Key="Text.ClearStashes" xml:space="preserve">丢弃贮藏确认</x:String>
<x:String x:Key="Text.ClearStashes.Message" xml:space="preserve">您正在丢弃所有的贮藏,一经操作,无法回退,是否继续?</x:String>
<x:String x:Key="Text.Clone" xml:space="preserve">克隆远程仓库</x:String>

View file

@ -86,8 +86,11 @@
<x:String x:Key="Text.Checkout.LocalChanges.DoNothing" xml:space="preserve">不做處理</x:String>
<x:String x:Key="Text.Checkout.LocalChanges.StashAndReply" xml:space="preserve">擱置變更並自動復原</x:String>
<x:String x:Key="Text.CherryPick" xml:space="preserve">揀選提交</x:String>
<x:String x:Key="Text.CherryPick.AppendSourceToMessage" xml:space="preserve">提交資訊中追加來源資訊</x:String>
<x:String x:Key="Text.CherryPick.Commit" xml:space="preserve">提交列表:</x:String>
<x:String x:Key="Text.CherryPick.CommitChanges" xml:space="preserve">提交變更</x:String>
<x:String x:Key="Text.CherryPick.Mainline" xml:space="preserve">作為對比的父提交:</x:String>
<x:String x:Key="Text.CherryPick.Mainline.Tips" xml:space="preserve">通常你不能對一個合併進行揀選,因為你不知道合併的哪一邊應該被視為主線。這個選項指定了作為主線的父提交,允許揀選相對於該提交的修改。</x:String>
<x:String x:Key="Text.ClearStashes" xml:space="preserve">捨棄擱置變更確認</x:String>
<x:String x:Key="Text.ClearStashes.Message" xml:space="preserve">您正在捨棄所有的擱置變更,一經操作便無法復原,是否繼續?</x:String>
<x:String x:Key="Text.Clone" xml:space="preserve">複製 (clone) 遠端存放庫</x:String>

View file

@ -12,6 +12,30 @@ namespace SourceGit.ViewModels
private set;
}
public bool IsMergeCommit
{
get;
private set;
}
public List<Models.Commit> ParentsForMergeCommit
{
get;
private set;
}
public int MainlineForMergeCommit
{
get;
set;
}
public bool AppendSourceToMessage
{
get;
set;
}
public bool AutoCommit
{
get;
@ -22,6 +46,22 @@ namespace SourceGit.ViewModels
{
_repo = repo;
Targets = targets;
IsMergeCommit = false;
ParentsForMergeCommit = [];
MainlineForMergeCommit = 0;
AppendSourceToMessage = true;
AutoCommit = true;
View = new Views.CherryPick() { DataContext = this };
}
public CherryPick(Repository repo, Models.Commit merge, List<Models.Commit> parents)
{
_repo = repo;
Targets = [merge];
IsMergeCommit = true;
ParentsForMergeCommit = parents;
MainlineForMergeCommit = 0;
AppendSourceToMessage = true;
AutoCommit = true;
View = new Views.CherryPick() { DataContext = this };
}
@ -33,12 +73,30 @@ namespace SourceGit.ViewModels
return Task.Run(() =>
{
// Get commit SHAs reverted
var builder = new StringBuilder();
for (int i = Targets.Count - 1; i >= 0; i--)
builder.Append($"{Targets[i].SHA} ");
var succ = false;
if (IsMergeCommit)
{
succ = new Commands.CherryPick(
_repo.FullPath,
Targets[0].SHA,
!AutoCommit,
AppendSourceToMessage,
$"-m {MainlineForMergeCommit+1}").Exec();
}
else
{
var builder = new StringBuilder();
for (int i = Targets.Count - 1; i >= 0; i--)
builder.Append($"{Targets[i].SHA} ");
succ = new Commands.CherryPick(
_repo.FullPath,
builder.ToString(),
!AutoCommit,
AppendSourceToMessage,
string.Empty).Exec();
}
var succ = new Commands.CherryPick(_repo.FullPath, builder.ToString(), !AutoCommit).Exec();
CallUIThread(() => _repo.SetWatcherEnabled(true));
return succ;
});

View file

@ -424,7 +424,28 @@ namespace SourceGit.ViewModels
cherryPick.Click += (_, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new CherryPick(_repo, [commit]));
{
if (commit.Parents.Count <= 1)
{
PopupHost.ShowPopup(new CherryPick(_repo, [commit]));
}
else
{
var parents = new List<Models.Commit>();
foreach (var sha in commit.Parents)
{
var parent = _commits.Find(x => x.SHA == sha);
if (parent == null)
parent = new Commands.QuerySingleCommit(_repo.FullPath, sha).Result();
if (parent != null)
parents.Add(parent);
}
PopupHost.ShowPopup(new CherryPick(_repo, commit, parents));
}
}
e.Handled = true;
};
menu.Items.Add(cherryPick);

View file

@ -12,17 +12,11 @@
<TextBlock FontSize="18"
Classes="bold"
Text="{DynamicResource Text.CherryPick}"/>
<Grid Margin="0,16,0,0" ColumnDefinitions="100,*">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="32"/>
</Grid.RowDefinitions>
<Grid Margin="0,16,0,0" RowDefinitions="Auto,Auto,32,32" ColumnDefinitions="100,*">
<TextBlock Grid.Row="0" Grid.Column="0"
HorizontalAlignment="Right" VerticalAlignment="Top"
HorizontalAlignment="Right" VerticalAlignment="Center"
Margin="0,0,8,0"
Text="{DynamicResource Text.CherryPick.Commit}"/>
<ListBox Grid.Row="0" Grid.Column="1"
MinHeight="32" MaxHeight="100"
ItemsSource="{Binding Targets}"
@ -58,9 +52,45 @@
</ListBox.ItemTemplate>
</ListBox>
<CheckBox Grid.Row="1" Grid.Column="1"
<TextBlock Grid.Row="1" Grid.Column="0"
HorizontalAlignment="Right" VerticalAlignment="Center"
Margin="0,0,8,0"
Text="{DynamicResource Text.CherryPick.Mainline}"
IsVisible="{Binding IsMergeCommit}"/>
<Grid Grid.Row="1" Grid.Column="1" Height="32" ColumnDefinitions="*,24" IsVisible="{Binding IsMergeCommit}">
<ComboBox Grid.Column="0"
Height="28" Padding="4,0"
VerticalAlignment="Center" HorizontalAlignment="Stretch"
ItemsSource="{Binding ParentsForMergeCommit}"
SelectedIndex="{Binding MainlineForMergeCommit, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="{x:Type m:Commit}">
<Grid ColumnDefinitions="Auto,*">
<TextBlock Grid.Column="0" FontFamily="{DynamicResource Fonts.Monospace}" VerticalAlignment="Center" Text="{Binding SHA, Converter={x:Static c:StringConverters.ToShortSHA}}" Foreground="DarkOrange" Margin="6,0,4,0"/>
<TextBlock Grid.Column="1" VerticalAlignment="Center" Text="{Binding Subject}" TextTrimming="CharacterEllipsis"/>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Border Grid.Column="1"
Background="Transparent"
ToolTip.Tip="{DynamicResource Text.CherryPick.Mainline.Tips}">
<Path Grid.Column="1"
Width="14" Height="14"
Data="{StaticResource Icons.Info}"/>
</Border>
</Grid>
<CheckBox Grid.Row="2" Grid.Column="1"
Content="{DynamicResource Text.CherryPick.CommitChanges}"
IsChecked="{Binding AutoCommit, Mode=TwoWay}"/>
<CheckBox Grid.Row="3" Grid.Column="1"
Content="{DynamicResource Text.CherryPick.AppendSourceToMessage}"
IsChecked="{Binding AppendSourceToMessage, Mode=TwoWay}"
IsEnabled="{Binding AutoCommit}"
ToolTip.Tip="-x"/>
</Grid>
</StackPanel>
</UserControl>