enhance: git format-patch

* use `--output=<file>` instead of `-o <dir>` to avoid failure because the directory cannot be created
* make generated patches in order when format multiple commits

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2024-11-22 18:40:59 +08:00
parent c50508d4ac
commit c78e2e59d9
No known key found for this signature in database
2 changed files with 41 additions and 11 deletions

View file

@ -6,7 +6,7 @@
{ {
WorkingDirectory = repo; WorkingDirectory = repo;
Context = repo; Context = repo;
Args = $"format-patch {commit} -1 -o \"{saveTo}\""; Args = $"format-patch {commit} -1 --output=\"{saveTo}\"";
} }
} }
} }

View file

@ -239,16 +239,16 @@ namespace SourceGit.ViewModels
} }
} }
var multipleMenu = new ContextMenu();
if (canCherryPick)
{
// Sort selected commits in order. // Sort selected commits in order.
selected.Sort((l, r) => selected.Sort((l, r) =>
{ {
return _commits.IndexOf(r) - _commits.IndexOf(l); return _commits.IndexOf(r) - _commits.IndexOf(l);
}); });
var multipleMenu = new ContextMenu();
if (canCherryPick)
{
var cherryPickMultiple = new MenuItem(); var cherryPickMultiple = new MenuItem();
cherryPickMultiple.Header = App.Text("CommitCM.CherryPickMultiple"); cherryPickMultiple.Header = App.Text("CommitCM.CherryPickMultiple");
cherryPickMultiple.Icon = App.CreateMenuIcon("Icons.CherryPick"); cherryPickMultiple.Icon = App.CreateMenuIcon("Icons.CherryPick");
@ -277,11 +277,11 @@ namespace SourceGit.ViewModels
var picker = await storageProvider.OpenFolderPickerAsync(options); var picker = await storageProvider.OpenFolderPickerAsync(options);
if (picker.Count == 1) if (picker.Count == 1)
{ {
var saveTo = $"{picker[0].Path.LocalPath}/patches";
var succ = false; var succ = false;
foreach (var c in selected) for (var i = 0; i < selected.Count; i++)
{ {
succ = await Task.Run(() => new Commands.FormatPatch(_repo.FullPath, c.SHA, saveTo).Exec()); var saveTo = GetPatchFileName(picker[0].Path.LocalPath, selected[i], i);
succ = await Task.Run(() => new Commands.FormatPatch(_repo.FullPath, selected[i].SHA, saveTo).Exec());
if (!succ) if (!succ)
break; break;
} }
@ -621,7 +621,8 @@ namespace SourceGit.ViewModels
var selected = await storageProvider.OpenFolderPickerAsync(options); var selected = await storageProvider.OpenFolderPickerAsync(options);
if (selected.Count == 1) if (selected.Count == 1)
{ {
var succ = new Commands.FormatPatch(_repo.FullPath, commit.SHA, selected[0].Path.LocalPath).Exec(); var saveTo = GetPatchFileName(selected[0].Path.LocalPath, commit);
var succ = new Commands.FormatPatch(_repo.FullPath, commit.SHA, saveTo).Exec();
if (succ) if (succ)
App.SendNotification(_repo.FullPath, App.Text("SaveAsPatchSuccess")); App.SendNotification(_repo.FullPath, App.Text("SaveAsPatchSuccess"));
} }
@ -1053,6 +1054,35 @@ namespace SourceGit.ViewModels
menu.Items.Add(submenu); menu.Items.Add(submenu);
} }
private string GetPatchFileName(string dir, Models.Commit commit, int index = 0)
{
var ignore_chars = new HashSet<char> { '/', '\\', ':', ',', '*', '?', '\"', '<', '>', '|', '`', '$', '^', '%', '[', ']', '+', '-' };
var builder = new StringBuilder();
builder.Append(index.ToString("D4"));
builder.Append('-');
var chars = commit.Subject.ToCharArray();
var len = 0;
foreach (var c in chars)
{
if (!ignore_chars.Contains(c))
{
if (c == ' ' || c == '\t')
builder.Append('-');
else
builder.Append(c);
len++;
if (len >= 48)
break;
}
}
builder.Append(".patch");
return System.IO.Path.Combine(dir, builder.ToString());
}
private Repository _repo = null; private Repository _repo = null;
private bool _isLoading = true; private bool _isLoading = true;
private List<Models.Commit> _commits = new List<Models.Commit>(); private List<Models.Commit> _commits = new List<Models.Commit>();