Skip to content

Commit 1d037c7

Browse files
committed
feature: add context menu Save As Patch... for selected stash (#1018)
Signed-off-by: leo <[email protected]>
1 parent 4868ea1 commit 1d037c7

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

src/Commands/SaveChangesAsPatch.cs

+13
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ public static bool ProcessRevisionCompareChanges(string repo, List<Models.Change
3737
return true;
3838
}
3939

40+
public static bool ProcessStashChanges(string repo, List<Models.DiffOption> opts, string saveTo)
41+
{
42+
using (var sw = File.Create(saveTo))
43+
{
44+
foreach (var opt in opts)
45+
{
46+
if (!ProcessSingleChange(repo, opt, sw))
47+
return false;
48+
}
49+
}
50+
return true;
51+
}
52+
4053
private static bool ProcessSingleChange(string repo, Models.DiffOption opt, FileStream writer)
4154
{
4255
var starter = new ProcessStartInfo();

src/Resources/Locales/en_US.axaml

+1
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@
669669
<x:String x:Key="Text.StashCM.Apply" xml:space="preserve">Apply</x:String>
670670
<x:String x:Key="Text.StashCM.Drop" xml:space="preserve">Drop</x:String>
671671
<x:String x:Key="Text.StashCM.Pop" xml:space="preserve">Pop</x:String>
672+
<x:String x:Key="Text.StashCM.SaveAsPatch" xml:space="preserve">Save as Patch...</x:String>
672673
<x:String x:Key="Text.StashDropConfirm" xml:space="preserve">Drop Stash</x:String>
673674
<x:String x:Key="Text.StashDropConfirm.Label" xml:space="preserve">Drop:</x:String>
674675
<x:String x:Key="Text.Stashes" xml:space="preserve">STASHES</x:String>

src/Resources/Locales/zh_CN.axaml

+1
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@
673673
<x:String x:Key="Text.StashCM.Apply" xml:space="preserve">应用(apply)</x:String>
674674
<x:String x:Key="Text.StashCM.Drop" xml:space="preserve">删除(drop)</x:String>
675675
<x:String x:Key="Text.StashCM.Pop" xml:space="preserve">应用并删除(pop)</x:String>
676+
<x:String x:Key="Text.StashCM.SaveAsPatch" xml:space="preserve">另存为补丁...</x:String>
676677
<x:String x:Key="Text.StashDropConfirm" xml:space="preserve">丢弃贮藏确认</x:String>
677678
<x:String x:Key="Text.StashDropConfirm.Label" xml:space="preserve">丢弃贮藏 :</x:String>
678679
<x:String x:Key="Text.Stashes" xml:space="preserve">贮藏列表</x:String>

src/Resources/Locales/zh_TW.axaml

+1
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@
672672
<x:String x:Key="Text.StashCM.Apply" xml:space="preserve">套用 (apply)</x:String>
673673
<x:String x:Key="Text.StashCM.Drop" xml:space="preserve">刪除 (drop)</x:String>
674674
<x:String x:Key="Text.StashCM.Pop" xml:space="preserve">套用並刪除 (pop)</x:String>
675+
<x:String x:Key="Text.StashCM.SaveAsPatch" xml:space="preserve">另存為修補檔 (patch)...</x:String>
675676
<x:String x:Key="Text.StashDropConfirm" xml:space="preserve">捨棄擱置變更確認</x:String>
676677
<x:String x:Key="Text.StashDropConfirm.Label" xml:space="preserve">捨棄擱置變更:</x:String>
677678
<x:String x:Key="Text.Stashes" xml:space="preserve">擱置變更</x:String>

src/ViewModels/StashesPage.cs

+37
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading.Tasks;
55

66
using Avalonia.Controls;
7+
using Avalonia.Platform.Storage;
78
using Avalonia.Threading;
89

910
using CommunityToolkit.Mvvm.ComponentModel;
@@ -157,9 +158,45 @@ public ContextMenu MakeContextMenu(Models.Stash stash)
157158
ev.Handled = true;
158159
};
159160

161+
var patch = new MenuItem();
162+
patch.Header = App.Text("StashCM.SaveAsPatch");
163+
patch.Icon = App.CreateMenuIcon("Icons.Diff");
164+
patch.Click += async (_, e) =>
165+
{
166+
var storageProvider = App.GetStorageProvider();
167+
if (storageProvider == null)
168+
return;
169+
170+
var options = new FilePickerSaveOptions();
171+
options.Title = App.Text("StashCM.SaveAsPatch");
172+
options.DefaultExtension = ".patch";
173+
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
174+
175+
var storageFile = await storageProvider.SaveFilePickerAsync(options);
176+
if (storageFile != null)
177+
{
178+
var opts = new List<Models.DiffOption>();
179+
foreach (var c in _changes)
180+
{
181+
if (_untrackedChanges.Contains(c.Path))
182+
opts.Add(new Models.DiffOption("4b825dc642cb6eb9a060e54bf8d69288fbee4904", _selectedStash.Parents[2], c));
183+
else
184+
opts.Add(new Models.DiffOption(_selectedStash.Parents[0], _selectedStash.SHA, c));
185+
}
186+
187+
var succ = await Task.Run(() => Commands.SaveChangesAsPatch.ProcessStashChanges(_repo.FullPath, opts, storageFile.Path.LocalPath));
188+
if (succ)
189+
App.SendNotification(_repo.FullPath, App.Text("SaveAsPatchSuccess"));
190+
}
191+
192+
e.Handled = true;
193+
};
194+
160195
var menu = new ContextMenu();
161196
menu.Items.Add(apply);
162197
menu.Items.Add(drop);
198+
menu.Items.Add(new MenuItem { Header = "-" });
199+
menu.Items.Add(patch);
163200
return menu;
164201
}
165202

0 commit comments

Comments
 (0)