From 3f55d66e01b8e4523956799cf28f85a368bc45d0 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 27 May 2021 22:30:40 +0800 Subject: [PATCH] feature: add `git archive` to commit's context menu --- src/Commands/Archive.cs | 22 +++++++++ src/Resources/Locales/en_US.xaml | 9 +++- src/Resources/Locales/zh_CN.xaml | 9 +++- src/Views/Popups/Archive.xaml | 69 ++++++++++++++++++++++++++++ src/Views/Popups/Archive.xaml.cs | 55 ++++++++++++++++++++++ src/Views/Validations/ArchiveFile.cs | 12 +++++ src/Views/Widgets/Histories.xaml.cs | 8 ++++ 7 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 src/Commands/Archive.cs create mode 100644 src/Views/Popups/Archive.xaml create mode 100644 src/Views/Popups/Archive.xaml.cs create mode 100644 src/Views/Validations/ArchiveFile.cs diff --git a/src/Commands/Archive.cs b/src/Commands/Archive.cs new file mode 100644 index 00000000..b4153e34 --- /dev/null +++ b/src/Commands/Archive.cs @@ -0,0 +1,22 @@ +using System; + +namespace SourceGit.Commands { + + /// + /// 存档命令 + /// + public class Archive : Command { + private Action handler; + + public Archive(string repo, string revision, string to, Action onProgress) { + Cwd = repo; + Args = $"archive --format=zip --verbose --output=\"{to}\" {revision}"; + TraitErrorAsOutput = true; + handler = onProgress; + } + + public override void OnReadline(string line) { + handler?.Invoke(line); + } + } +} diff --git a/src/Resources/Locales/en_US.xaml b/src/Resources/Locales/en_US.xaml index e2ba8ddd..d58c3d29 100644 --- a/src/Resources/Locales/en_US.xaml +++ b/src/Resources/Locales/en_US.xaml @@ -39,6 +39,11 @@ Error All Similar to 'error', but shows more + Archive + Revision : + Save Archive To : + Select archive file path + Blame Right click to see commit info COMMIT SHA @@ -184,7 +189,8 @@ Rebase '{0}' to Here Cherry-Pick This Commit Revert Commit - Save as Patch + Save as Patch ... + Archive ... Copy Commit SHA Copy Commit Info @@ -442,4 +448,5 @@ Commit subject can NOT be empty Invalid path for patch file Invalid path for submodules + Invalid path for archive file \ No newline at end of file diff --git a/src/Resources/Locales/zh_CN.xaml b/src/Resources/Locales/zh_CN.xaml index 94fd0028..5a133e17 100644 --- a/src/Resources/Locales/zh_CN.xaml +++ b/src/Resources/Locales/zh_CN.xaml @@ -39,6 +39,11 @@ 更多错误 与【错误】级别相似,但输出内容更多 + 存档 + 指定的提交: + 存档文件路径: + 选择存档文件的存放路径 + 逐行追溯 右键点击查看所选行修改记录 提交指纹 @@ -184,7 +189,8 @@ 变基 '{0}' 到此处 挑选此提交 回滚此提交 - 另存为补丁 + 另存为补丁 ... + 存档 ... 复制提交指纹 复制提交信息 @@ -442,4 +448,5 @@ 提交信息未填写! 补丁文件不存在或不可访问! 非法的子模块路径! + 非法的存档文件路径! \ No newline at end of file diff --git a/src/Views/Popups/Archive.xaml b/src/Views/Popups/Archive.xaml new file mode 100644 index 00000000..99e29e2d --- /dev/null +++ b/src/Views/Popups/Archive.xaml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Views/Popups/Archive.xaml.cs b/src/Views/Popups/Archive.xaml.cs new file mode 100644 index 00000000..ff0f2a1a --- /dev/null +++ b/src/Views/Popups/Archive.xaml.cs @@ -0,0 +1,55 @@ +using Microsoft.Win32; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; + +namespace SourceGit.Views.Popups { + + /// + /// 存档操作面板 + /// + public partial class Archive : Controls.PopupWidget { + private string repo; + private string revision; + + public string SaveTo { get; set; } = "archive.zip"; + + public Archive(string repo, Models.Commit revision) { + this.repo = repo; + this.revision = revision.SHA; + + InitializeComponent(); + + txtBased.Text = $"{revision.ShortSHA} {revision.Subject}"; + } + + public override string GetTitle() { + return App.Text("Archive"); + } + + public override Task Start() { + txtSaveTo.GetBindingExpression(TextBox.TextProperty).UpdateSource(); + if (Validation.GetHasError(txtSaveTo)) return null; + + return Task.Run(() => { + Models.Watcher.SetEnabled(repo, false); + var succ = new Commands.Archive(repo, revision, SaveTo, UpdateProgress).Exec(); + Models.Watcher.SetEnabled(repo, true); + return succ; + }); + } + + private void OpenFileBrowser(object sender, RoutedEventArgs e) { + var dialog = new OpenFileDialog(); + dialog.Filter = "ZIP|*.zip"; + dialog.Title = App.Text("Archive.File"); + dialog.InitialDirectory = repo; + dialog.CheckFileExists = false; + + if (dialog.ShowDialog() == true) { + SaveTo = dialog.FileName; + txtSaveTo.GetBindingExpression(TextBox.TextProperty).UpdateTarget(); + } + } + } +} diff --git a/src/Views/Validations/ArchiveFile.cs b/src/Views/Validations/ArchiveFile.cs new file mode 100644 index 00000000..eb347445 --- /dev/null +++ b/src/Views/Validations/ArchiveFile.cs @@ -0,0 +1,12 @@ +using System.Globalization; +using System.Windows.Controls; + +namespace SourceGit.Views.Validations { + public class ArchiveFile : ValidationRule { + public override ValidationResult Validate(object value, CultureInfo cultureInfo) { + var path = value as string; + if (string.IsNullOrEmpty(path) || !path.EndsWith(".zip")) return new ValidationResult(false, App.Text("BadArchiveFile")); + return ValidationResult.ValidResult; + } + } +} \ No newline at end of file diff --git a/src/Views/Widgets/Histories.xaml.cs b/src/Views/Widgets/Histories.xaml.cs index ea9a9a24..b8cb9bff 100644 --- a/src/Views/Widgets/Histories.xaml.cs +++ b/src/Views/Widgets/Histories.xaml.cs @@ -348,6 +348,14 @@ namespace SourceGit.Views.Widgets { } }; menu.Items.Add(saveToPatch); + + var archive = new MenuItem(); + archive.Header = App.Text("CommitCM.Archive"); + archive.Click += (o, e) => { + new Popups.Archive(repo.Path, commit).Show(); + e.Handled = true; + }; + menu.Items.Add(archive); menu.Items.Add(new Separator()); var copySHA = new MenuItem();