Skip to content

Commit 9104060

Browse files
committed
feature: support add custom actions for selected branch (#980)
Signed-off-by: leo <[email protected]>
1 parent 9b07034 commit 9104060

File tree

7 files changed

+73
-22
lines changed

7 files changed

+73
-22
lines changed

src/Models/CustomAction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public enum CustomActionScope
66
{
77
Repository,
88
Commit,
9+
Branch,
910
}
1011

1112
public class CustomAction : ObservableObject

src/Resources/Locales/en_US.axaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<x:String x:Key="Text.BranchCM.CompareWithHead" xml:space="preserve">Compare with HEAD</x:String>
5757
<x:String x:Key="Text.BranchCM.CompareWithWorktree" xml:space="preserve">Compare with Worktree</x:String>
5858
<x:String x:Key="Text.BranchCM.CopyName" xml:space="preserve">Copy Branch Name</x:String>
59+
<x:String x:Key="Text.BranchCM.CustomAction" xml:space="preserve">Custom Action</x:String>
5960
<x:String x:Key="Text.BranchCM.Delete" xml:space="preserve">Delete ${0}$...</x:String>
6061
<x:String x:Key="Text.BranchCM.DeleteMultiBranches" xml:space="preserve">Delete selected {0} branches</x:String>
6162
<x:String x:Key="Text.BranchCM.DiscardAll" xml:space="preserve">Discard all changes</x:String>
@@ -152,10 +153,11 @@
152153
<x:String x:Key="Text.Configure.CommitMessageTemplate.Content" xml:space="preserve">Template Content:</x:String>
153154
<x:String x:Key="Text.Configure.CustomAction" xml:space="preserve">CUSTOM ACTION</x:String>
154155
<x:String x:Key="Text.Configure.CustomAction.Arguments" xml:space="preserve">Arguments:</x:String>
155-
<x:String x:Key="Text.Configure.CustomAction.Arguments.Tip" xml:space="preserve">${REPO} - Repository's path; ${SHA} - Selected commit's SHA</x:String>
156+
<x:String x:Key="Text.Configure.CustomAction.Arguments.Tip" xml:space="preserve">${REPO} - Repository's path; ${BRANCH} - Selected branch; ${SHA} - Selected commit's SHA</x:String>
156157
<x:String x:Key="Text.Configure.CustomAction.Executable" xml:space="preserve">Executable File:</x:String>
157158
<x:String x:Key="Text.Configure.CustomAction.Name" xml:space="preserve">Name:</x:String>
158159
<x:String x:Key="Text.Configure.CustomAction.Scope" xml:space="preserve">Scope:</x:String>
160+
<x:String x:Key="Text.Configure.CustomAction.Scope.Branch" xml:space="preserve">Branch</x:String>
159161
<x:String x:Key="Text.Configure.CustomAction.Scope.Commit" xml:space="preserve">Commit</x:String>
160162
<x:String x:Key="Text.Configure.CustomAction.Scope.Repository" xml:space="preserve">Repository</x:String>
161163
<x:String x:Key="Text.Configure.CustomAction.WaitForExit" xml:space="preserve">Wait for action exit</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<x:String x:Key="Text.BranchCM.CompareWithHead" xml:space="preserve">与当前HEAD比较</x:String>
6060
<x:String x:Key="Text.BranchCM.CompareWithWorktree" xml:space="preserve">与本地工作树比较</x:String>
6161
<x:String x:Key="Text.BranchCM.CopyName" xml:space="preserve">复制分支名</x:String>
62+
<x:String x:Key="Text.BranchCM.CustomAction" xml:space="preserve">自定义操作</x:String>
6263
<x:String x:Key="Text.BranchCM.Delete" xml:space="preserve">删除 ${0}$...</x:String>
6364
<x:String x:Key="Text.BranchCM.DeleteMultiBranches" xml:space="preserve">删除选中的 {0} 个分支</x:String>
6465
<x:String x:Key="Text.BranchCM.DiscardAll" xml:space="preserve">放弃所有更改</x:String>
@@ -155,10 +156,11 @@
155156
<x:String x:Key="Text.Configure.CommitMessageTemplate.Content" xml:space="preserve">模板内容 :</x:String>
156157
<x:String x:Key="Text.Configure.CustomAction" xml:space="preserve">自定义操作</x:String>
157158
<x:String x:Key="Text.Configure.CustomAction.Arguments" xml:space="preserve">命令行参数 :</x:String>
158-
<x:String x:Key="Text.Configure.CustomAction.Arguments.Tip" xml:space="preserve">请使用${REPO}代替仓库路径,${SHA}代替提交哈希</x:String>
159+
<x:String x:Key="Text.Configure.CustomAction.Arguments.Tip" xml:space="preserve">请使用${REPO}代替仓库路径,${BRANCH}代替选中的分支,${SHA}代替提交哈希</x:String>
159160
<x:String x:Key="Text.Configure.CustomAction.Executable" xml:space="preserve">可执行文件路径 :</x:String>
160161
<x:String x:Key="Text.Configure.CustomAction.Name" xml:space="preserve">名称 :</x:String>
161162
<x:String x:Key="Text.Configure.CustomAction.Scope" xml:space="preserve">作用目标 :</x:String>
163+
<x:String x:Key="Text.Configure.CustomAction.Scope.Branch" xml:space="preserve">选中的分支</x:String>
162164
<x:String x:Key="Text.Configure.CustomAction.Scope.Commit" xml:space="preserve">选中的提交</x:String>
163165
<x:String x:Key="Text.Configure.CustomAction.Scope.Repository" xml:space="preserve">仓库</x:String>
164166
<x:String x:Key="Text.Configure.CustomAction.WaitForExit" xml:space="preserve">等待操作执行完成</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<x:String x:Key="Text.BranchCM.CompareWithHead" xml:space="preserve">與目前 HEAD 比較</x:String>
6060
<x:String x:Key="Text.BranchCM.CompareWithWorktree" xml:space="preserve">與本機工作區比較</x:String>
6161
<x:String x:Key="Text.BranchCM.CopyName" xml:space="preserve">複製分支名稱</x:String>
62+
<x:String x:Key="Text.BranchCM.CustomAction" xml:space="preserve">自訂動作</x:String>
6263
<x:String x:Key="Text.BranchCM.Delete" xml:space="preserve">刪除 ${0}$...</x:String>
6364
<x:String x:Key="Text.BranchCM.DeleteMultiBranches" xml:space="preserve">刪除所選的 {0} 個分支</x:String>
6465
<x:String x:Key="Text.BranchCM.DiscardAll" xml:space="preserve">捨棄所有變更</x:String>
@@ -155,10 +156,11 @@
155156
<x:String x:Key="Text.Configure.CommitMessageTemplate.Content" xml:space="preserve">範本內容:</x:String>
156157
<x:String x:Key="Text.Configure.CustomAction" xml:space="preserve">自訂動作</x:String>
157158
<x:String x:Key="Text.Configure.CustomAction.Arguments" xml:space="preserve">指令參數:</x:String>
158-
<x:String x:Key="Text.Configure.CustomAction.Arguments.Tip" xml:space="preserve">使用 ${REPO} 表示存放庫路徑、${SHA} 表示所選的提交編號</x:String>
159+
<x:String x:Key="Text.Configure.CustomAction.Arguments.Tip" xml:space="preserve">使用 ${REPO} 表示存放庫路徑、${BRANCH} 表示所選的分支、${SHA} 表示所選的提交編號</x:String>
159160
<x:String x:Key="Text.Configure.CustomAction.Executable" xml:space="preserve">可執行檔案路徑:</x:String>
160161
<x:String x:Key="Text.Configure.CustomAction.Name" xml:space="preserve">名稱:</x:String>
161162
<x:String x:Key="Text.Configure.CustomAction.Scope" xml:space="preserve">執行範圍:</x:String>
163+
<x:String x:Key="Text.Configure.CustomAction.Scope.Branch" xml:space="preserve">選取的分支</x:String>
162164
<x:String x:Key="Text.Configure.CustomAction.Scope.Commit" xml:space="preserve">選取的提交</x:String>
163165
<x:String x:Key="Text.Configure.CustomAction.Scope.Repository" xml:space="preserve">存放庫</x:String>
164166
<x:String x:Key="Text.Configure.CustomAction.WaitForExit" xml:space="preserve">等待自訂操作退出</x:String>

src/ViewModels/ExecuteCustomAction.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,26 @@ public Models.CustomAction CustomAction
1010
private set;
1111
}
1212

13-
public ExecuteCustomAction(Repository repo, Models.CustomAction action, Models.Commit commit)
13+
public ExecuteCustomAction(Repository repo, Models.CustomAction action)
1414
{
1515
_repo = repo;
1616
_args = action.Arguments.Replace("${REPO}", _repo.FullPath);
17-
if (commit != null)
18-
_args = _args.Replace("${SHA}", commit.SHA);
17+
CustomAction = action;
18+
View = new Views.ExecuteCustomAction() { DataContext = this };
19+
}
1920

21+
public ExecuteCustomAction(Repository repo, Models.CustomAction action, Models.Branch branch)
22+
{
23+
_repo = repo;
24+
_args = action.Arguments.Replace("${REPO}", _repo.FullPath).Replace("${BRANCH}", branch.FriendlyName);
25+
CustomAction = action;
26+
View = new Views.ExecuteCustomAction() { DataContext = this };
27+
}
28+
29+
public ExecuteCustomAction(Repository repo, Models.CustomAction action, Models.Commit commit)
30+
{
31+
_repo = repo;
32+
_args = action.Arguments.Replace("${REPO}", _repo.FullPath).Replace("${SHA}", commit.SHA);
2033
CustomAction = action;
2134
View = new Views.ExecuteCustomAction() { DataContext = this };
2235
}

src/ViewModels/Repository.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ public ContextMenu CreateContextMenuForCustomAction()
14391439
item.Click += (_, e) =>
14401440
{
14411441
if (CanCreatePopup())
1442-
ShowAndStartPopup(new ExecuteCustomAction(this, dup, null));
1442+
ShowAndStartPopup(new ExecuteCustomAction(this, dup));
14431443

14441444
e.Handled = true;
14451445
};
@@ -1698,6 +1698,7 @@ public ContextMenu CreateContextMenuForLocalBranch(Models.Branch branch)
16981698
menu.Items.Add(createBranch);
16991699
menu.Items.Add(createTag);
17001700
menu.Items.Add(new MenuItem() { Header = "-" });
1701+
TryToAddCustomActionsToBranchContextMenu(menu, branch);
17011702

17021703
if (!IsBare)
17031704
{
@@ -1968,7 +1969,9 @@ public ContextMenu CreateContextMenuForRemoteBranch(Models.Branch branch)
19681969
menu.Items.Add(new MenuItem() { Header = "-" });
19691970
menu.Items.Add(archive);
19701971
menu.Items.Add(new MenuItem() { Header = "-" });
1972+
TryToAddCustomActionsToBranchContextMenu(menu, branch);
19711973
menu.Items.Add(copy);
1974+
19721975
return menu;
19731976
}
19741977

@@ -2319,6 +2322,43 @@ private BranchTreeNode FindBranchNode(List<BranchTreeNode> nodes, string path)
23192322
return null;
23202323
}
23212324

2325+
private void TryToAddCustomActionsToBranchContextMenu(ContextMenu menu, Models.Branch branch)
2326+
{
2327+
var actions = new List<Models.CustomAction>();
2328+
foreach (var action in Settings.CustomActions)
2329+
{
2330+
if (action.Scope == Models.CustomActionScope.Branch)
2331+
actions.Add(action);
2332+
}
2333+
2334+
if (actions.Count == 0)
2335+
return;
2336+
2337+
var custom = new MenuItem();
2338+
custom.Header = App.Text("BranchCM.CustomAction");
2339+
custom.Icon = App.CreateMenuIcon("Icons.Action");
2340+
2341+
foreach (var action in actions)
2342+
{
2343+
var dup = action;
2344+
var item = new MenuItem();
2345+
item.Icon = App.CreateMenuIcon("Icons.Action");
2346+
item.Header = dup.Name;
2347+
item.Click += (_, e) =>
2348+
{
2349+
if (CanCreatePopup())
2350+
ShowAndStartPopup(new ExecuteCustomAction(this, dup, branch));
2351+
2352+
e.Handled = true;
2353+
};
2354+
2355+
custom.Items.Add(item);
2356+
}
2357+
2358+
menu.Items.Add(custom);
2359+
menu.Items.Add(new MenuItem() { Header = "-" });
2360+
}
2361+
23222362
private void UpdateCurrentRevisionFilesForSearchSuggestion()
23232363
{
23242364
_revisionFiles.Clear();

src/Views/RepositoryConfigure.axaml

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
xmlns:m="using:SourceGit.Models"
66
xmlns:vm="using:SourceGit.ViewModels"
77
xmlns:v="using:SourceGit.Views"
8-
xmlns:ac="using:Avalonia.Controls.Converters"
98
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
109
x:Class="SourceGit.Views.RepositoryConfigure"
1110
x:DataType="vm:RepositoryConfigure"
@@ -412,20 +411,12 @@
412411
<TextBlock Text="{DynamicResource Text.Configure.CustomAction.Name}"/>
413412
<TextBox Margin="0,4,0,0" CornerRadius="3" Height="28" Text="{Binding Name, Mode=TwoWay}"/>
414413

415-
<StackPanel Orientation="Horizontal" Margin="0,12,0,0">
416-
<StackPanel.Resources>
417-
<ac:EnumToBoolConverter x:Key="EnumToBoolConverter"/>
418-
</StackPanel.Resources>
419-
420-
<TextBlock Text="{DynamicResource Text.Configure.CustomAction.Scope}"/>
421-
<RadioButton Content="{DynamicResource Text.Configure.CustomAction.Scope.Repository}"
422-
GroupName="CustomActionScope"
423-
Margin="8,0"
424-
IsChecked="{Binding Scope, Mode=TwoWay, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static m:CustomActionScope.Repository}}"/>
425-
<RadioButton Content="{DynamicResource Text.Configure.CustomAction.Scope.Commit}"
426-
GroupName="CustomActionScope"
427-
IsChecked="{Binding Scope, Mode=TwoWay, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static m:CustomActionScope.Commit}}"/>
428-
</StackPanel>
414+
<TextBlock Margin="0,12,0,0" Text="{DynamicResource Text.Configure.CustomAction.Scope}"/>
415+
<ComboBox Margin="0,4,0,0" Height="28" HorizontalAlignment="Stretch" SelectedIndex="{Binding Scope, Mode=TwoWay}">
416+
<ComboBoxItem Content="{DynamicResource Text.Configure.CustomAction.Scope.Repository}"/>
417+
<ComboBoxItem Content="{DynamicResource Text.Configure.CustomAction.Scope.Commit}"/>
418+
<ComboBoxItem Content="{DynamicResource Text.Configure.CustomAction.Scope.Branch}"/>
419+
</ComboBox>
429420

430421
<TextBlock Margin="0,12,0,0" Text="{DynamicResource Text.Configure.CustomAction.Executable}"/>
431422
<TextBox Margin="0,4,0,0" Height="28" CornerRadius="3" Text="{Binding Executable, Mode=TwoWay}">

0 commit comments

Comments
 (0)