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

@ -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} ");
var succ = new Commands.CherryPick(_repo.FullPath, builder.ToString(), !AutoCommit).Exec();
succ = new Commands.CherryPick(
_repo.FullPath,
builder.ToString(),
!AutoCommit,
AppendSourceToMessage,
string.Empty).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);