Skip to content

Commit c78e2e5

Browse files
committed
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 <[email protected]>
1 parent c50508d commit c78e2e5

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

src/Commands/FormatPatch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public FormatPatch(string repo, string commit, string saveTo)
66
{
77
WorkingDirectory = repo;
88
Context = repo;
9-
Args = $"format-patch {commit} -1 -o \"{saveTo}\"";
9+
Args = $"format-patch {commit} -1 --output=\"{saveTo}\"";
1010
}
1111
}
1212
}

src/ViewModels/Histories.cs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,16 @@ public ContextMenu MakeContextMenu(ListBox list)
239239
}
240240
}
241241

242+
// Sort selected commits in order.
243+
selected.Sort((l, r) =>
244+
{
245+
return _commits.IndexOf(r) - _commits.IndexOf(l);
246+
});
247+
242248
var multipleMenu = new ContextMenu();
243249

244250
if (canCherryPick)
245251
{
246-
// Sort selected commits in order.
247-
selected.Sort((l, r) =>
248-
{
249-
return _commits.IndexOf(r) - _commits.IndexOf(l);
250-
});
251-
252252
var cherryPickMultiple = new MenuItem();
253253
cherryPickMultiple.Header = App.Text("CommitCM.CherryPickMultiple");
254254
cherryPickMultiple.Icon = App.CreateMenuIcon("Icons.CherryPick");
@@ -277,11 +277,11 @@ public ContextMenu MakeContextMenu(ListBox list)
277277
var picker = await storageProvider.OpenFolderPickerAsync(options);
278278
if (picker.Count == 1)
279279
{
280-
var saveTo = $"{picker[0].Path.LocalPath}/patches";
281280
var succ = false;
282-
foreach (var c in selected)
281+
for (var i = 0; i < selected.Count; i++)
283282
{
284-
succ = await Task.Run(() => new Commands.FormatPatch(_repo.FullPath, c.SHA, saveTo).Exec());
283+
var saveTo = GetPatchFileName(picker[0].Path.LocalPath, selected[i], i);
284+
succ = await Task.Run(() => new Commands.FormatPatch(_repo.FullPath, selected[i].SHA, saveTo).Exec());
285285
if (!succ)
286286
break;
287287
}
@@ -621,7 +621,8 @@ public ContextMenu MakeContextMenu(ListBox list)
621621
var selected = await storageProvider.OpenFolderPickerAsync(options);
622622
if (selected.Count == 1)
623623
{
624-
var succ = new Commands.FormatPatch(_repo.FullPath, commit.SHA, selected[0].Path.LocalPath).Exec();
624+
var saveTo = GetPatchFileName(selected[0].Path.LocalPath, commit);
625+
var succ = new Commands.FormatPatch(_repo.FullPath, commit.SHA, saveTo).Exec();
625626
if (succ)
626627
App.SendNotification(_repo.FullPath, App.Text("SaveAsPatchSuccess"));
627628
}
@@ -1053,6 +1054,35 @@ private void FillTagMenu(ContextMenu menu, Models.Tag tag, Models.Branch current
10531054
menu.Items.Add(submenu);
10541055
}
10551056

1057+
private string GetPatchFileName(string dir, Models.Commit commit, int index = 0)
1058+
{
1059+
var ignore_chars = new HashSet<char> { '/', '\\', ':', ',', '*', '?', '\"', '<', '>', '|', '`', '$', '^', '%', '[', ']', '+', '-' };
1060+
var builder = new StringBuilder();
1061+
builder.Append(index.ToString("D4"));
1062+
builder.Append('-');
1063+
1064+
var chars = commit.Subject.ToCharArray();
1065+
var len = 0;
1066+
foreach (var c in chars)
1067+
{
1068+
if (!ignore_chars.Contains(c))
1069+
{
1070+
if (c == ' ' || c == '\t')
1071+
builder.Append('-');
1072+
else
1073+
builder.Append(c);
1074+
1075+
len++;
1076+
1077+
if (len >= 48)
1078+
break;
1079+
}
1080+
}
1081+
builder.Append(".patch");
1082+
1083+
return System.IO.Path.Combine(dir, builder.ToString());
1084+
}
1085+
10561086
private Repository _repo = null;
10571087
private bool _isLoading = true;
10581088
private List<Models.Commit> _commits = new List<Models.Commit>();

0 commit comments

Comments
 (0)