feature<Achive>: add git archive to commit's context menu

This commit is contained in:
leo 2021-05-27 22:30:40 +08:00
parent 93835b48bc
commit 3f55d66e01
7 changed files with 182 additions and 2 deletions

22
src/Commands/Archive.cs Normal file
View file

@ -0,0 +1,22 @@
using System;
namespace SourceGit.Commands {
/// <summary>
/// 存档命令
/// </summary>
public class Archive : Command {
private Action<string> handler;
public Archive(string repo, string revision, string to, Action<string> onProgress) {
Cwd = repo;
Args = $"archive --format=zip --verbose --output=\"{to}\" {revision}";
TraitErrorAsOutput = true;
handler = onProgress;
}
public override void OnReadline(string line) {
handler?.Invoke(line);
}
}
}

View file

@ -39,6 +39,11 @@
<sys:String x:Key="Text.Apply.ErrorAll">Error All</sys:String>
<sys:String x:Key="Text.Apply.ErrorAll.Desc">Similar to 'error', but shows more</sys:String>
<sys:String x:Key="Text.Archive">Archive</sys:String>
<sys:String x:Key="Text.Archive.Revision">Revision :</sys:String>
<sys:String x:Key="Text.Archive.File">Save Archive To :</sys:String>
<sys:String x:Key="Text.Archive.File.Placeholder">Select archive file path</sys:String>
<sys:String x:Key="Text.Blame">Blame</sys:String>
<sys:String x:Key="Text.Blame.Tip">Right click to see commit info</sys:String>
<sys:String x:Key="Text.Blame.SHA">COMMIT SHA</sys:String>
@ -184,7 +189,8 @@
<sys:String x:Key="Text.CommitCM.Rebase">Rebase '{0}' to Here</sys:String>
<sys:String x:Key="Text.CommitCM.CherryPick">Cherry-Pick This Commit</sys:String>
<sys:String x:Key="Text.CommitCM.Revert">Revert Commit</sys:String>
<sys:String x:Key="Text.CommitCM.SaveAsPatch">Save as Patch</sys:String>
<sys:String x:Key="Text.CommitCM.SaveAsPatch">Save as Patch ...</sys:String>
<sys:String x:Key="Text.CommitCM.Archive">Archive ...</sys:String>
<sys:String x:Key="Text.CommitCM.CopySHA">Copy Commit SHA</sys:String>
<sys:String x:Key="Text.CommitCM.CopyInfo">Copy Commit Info</sys:String>
@ -442,4 +448,5 @@
<sys:String x:Key="Text.EmptyCommitMessage">Commit subject can NOT be empty</sys:String>
<sys:String x:Key="Text.BadPatchFile">Invalid path for patch file</sys:String>
<sys:String x:Key="Text.BadSubmodulePath">Invalid path for submodules</sys:String>
<sys:String x:Key="Text.BadArchiveFile">Invalid path for archive file</sys:String>
</ResourceDictionary>

View file

@ -39,6 +39,11 @@
<sys:String x:Key="Text.Apply.ErrorAll">更多错误</sys:String>
<sys:String x:Key="Text.Apply.ErrorAll.Desc">与【错误】级别相似,但输出内容更多</sys:String>
<sys:String x:Key="Text.Archive">存档</sys:String>
<sys:String x:Key="Text.Archive.Revision">指定的提交:</sys:String>
<sys:String x:Key="Text.Archive.File">存档文件路径:</sys:String>
<sys:String x:Key="Text.Archive.File.Placeholder">选择存档文件的存放路径</sys:String>
<sys:String x:Key="Text.Blame">逐行追溯</sys:String>
<sys:String x:Key="Text.Blame.Tip">右键点击查看所选行修改记录</sys:String>
<sys:String x:Key="Text.Blame.SHA">提交指纹</sys:String>
@ -184,7 +189,8 @@
<sys:String x:Key="Text.CommitCM.Rebase">变基 '{0}' 到此处</sys:String>
<sys:String x:Key="Text.CommitCM.CherryPick">挑选此提交</sys:String>
<sys:String x:Key="Text.CommitCM.Revert">回滚此提交</sys:String>
<sys:String x:Key="Text.CommitCM.SaveAsPatch">另存为补丁</sys:String>
<sys:String x:Key="Text.CommitCM.SaveAsPatch">另存为补丁 ...</sys:String>
<sys:String x:Key="Text.CommitCM.Archive">存档 ...</sys:String>
<sys:String x:Key="Text.CommitCM.CopySHA">复制提交指纹</sys:String>
<sys:String x:Key="Text.CommitCM.CopyInfo">复制提交信息</sys:String>
@ -442,4 +448,5 @@
<sys:String x:Key="Text.EmptyCommitMessage">提交信息未填写!</sys:String>
<sys:String x:Key="Text.BadPatchFile">补丁文件不存在或不可访问!</sys:String>
<sys:String x:Key="Text.BadSubmodulePath">非法的子模块路径!</sys:String>
<sys:String x:Key="Text.BadArchiveFile">非法的存档文件路径!</sys:String>
</ResourceDictionary>

View file

@ -0,0 +1,69 @@
<controls:PopupWidget
x:Class="SourceGit.Views.Popups.Archive"
x:Name="me"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:controls="clr-namespace:SourceGit.Views.Controls"
xmlns:validations="clr-namespace:SourceGit.Views.Validations"
mc:Ignorable="d"
d:DesignWidth="500">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="32"/>
<RowDefinition Height="32"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock
Grid.Row="0" Grid.Column="0"
Margin="0,0,8,0"
Text="{StaticResource Text.Archive.Revision}"
HorizontalAlignment="Right"/>
<StackPanel
Grid.Row="0" Grid.Column="1"
Orientation="Horizontal"
VerticalAlignment="Center">
<Path Width="14" Height="14" Data="{StaticResource Icon.Commit}"/>
<TextBlock x:Name="txtBased" Margin="8,0,0,0"/>
</StackPanel>
<TextBlock
Grid.Row="1" Grid.Column="0"
Margin="0,0,8,0"
Text="{StaticResource Text.Archive.File}"
HorizontalAlignment="Right"/>
<Grid Grid.Row="1" Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<controls:TextEdit
Grid.Column="0"
x:Name="txtSaveTo"
Height="24"
Placeholder="{StaticResource Text.Archive.File.Placeholder}">
<controls:TextEdit.Text>
<Binding Path="SaveTo" ElementName="me" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
<Binding.ValidationRules>
<validations:ArchiveFile/>
</Binding.ValidationRules>
</Binding>
</controls:TextEdit.Text>
</controls:TextEdit>
<Button
Grid.Column="1"
Click="OpenFileBrowser"
Height="24" Width="24"
Margin="2,0,0,0">
<Path Width="14" Height="14" Data="{StaticResource Icon.Folder}"/>
</Button>
</Grid>
</Grid>
</controls:PopupWidget>

View file

@ -0,0 +1,55 @@
using Microsoft.Win32;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace SourceGit.Views.Popups {
/// <summary>
/// 存档操作面板
/// </summary>
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<bool> 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();
}
}
}
}

View file

@ -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;
}
}
}

View file

@ -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();