Skip to content

Commit d66a083

Browse files
committed
feature: support creating custom action for selected remote (#1726)
Signed-off-by: leo <longshuang@msn.cn>
1 parent c4ae21e commit d66a083

File tree

10 files changed

+55
-39
lines changed

10 files changed

+55
-39
lines changed

src/Models/CustomAction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public enum CustomActionScope
99
Commit,
1010
Branch,
1111
Tag,
12+
Remote,
1213
}
1314

1415
public enum CustomActionControlType

src/Resources/Locales/en_US.axaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
<x:String x:Key="Text.Configure.CommitMessageTemplate.Name" xml:space="preserve">Template Name:</x:String>
174174
<x:String x:Key="Text.Configure.CustomAction" xml:space="preserve">CUSTOM ACTION</x:String>
175175
<x:String x:Key="Text.Configure.CustomAction.Arguments" xml:space="preserve">Arguments:</x:String>
176-
<x:String x:Key="Text.Configure.CustomAction.Arguments.Tip" xml:space="preserve">Built-in parameters: ${REPO} - repository's path; ${BRANCH} - selected branch; ${SHA} - selected commit's hash; ${TAG} - selected tag</x:String>
176+
<x:String x:Key="Text.Configure.CustomAction.Arguments.Tip" xml:space="preserve">Built-in parameters: ${REPO} - repository's path; ${REMOTE} - selected remote or selected branch's remote; ${BRANCH} - selected branch; ${SHA} - selected commit's hash; ${TAG} - selected tag</x:String>
177177
<x:String x:Key="Text.Configure.CustomAction.Executable" xml:space="preserve">Executable File:</x:String>
178178
<x:String x:Key="Text.Configure.CustomAction.InputControls" xml:space="preserve">Input Controls:</x:String>
179179
<x:String x:Key="Text.Configure.CustomAction.InputControls.Edit" xml:space="preserve">Edit</x:String>
@@ -182,6 +182,7 @@
182182
<x:String x:Key="Text.Configure.CustomAction.Scope" xml:space="preserve">Scope:</x:String>
183183
<x:String x:Key="Text.Configure.CustomAction.Scope.Branch" xml:space="preserve">Branch</x:String>
184184
<x:String x:Key="Text.Configure.CustomAction.Scope.Commit" xml:space="preserve">Commit</x:String>
185+
<x:String x:Key="Text.Configure.CustomAction.Scope.Remote" xml:space="preserve">Remote</x:String>
185186
<x:String x:Key="Text.Configure.CustomAction.Scope.Repository" xml:space="preserve">Repository</x:String>
186187
<x:String x:Key="Text.Configure.CustomAction.Scope.Tag" xml:space="preserve">Tag</x:String>
187188
<x:String x:Key="Text.Configure.CustomAction.WaitForExit" xml:space="preserve">Wait for action exit</x:String>
@@ -616,6 +617,7 @@
616617
<x:String x:Key="Text.Remote.URL" xml:space="preserve">Repository URL:</x:String>
617618
<x:String x:Key="Text.Remote.URL.Placeholder" xml:space="preserve">Remote git repository URL</x:String>
618619
<x:String x:Key="Text.RemoteCM.CopyURL" xml:space="preserve">Copy URL</x:String>
620+
<x:String x:Key="Text.RemoteCM.CustomAction" xml:space="preserve">Custom Action</x:String>
619621
<x:String x:Key="Text.RemoteCM.Delete" xml:space="preserve">Delete...</x:String>
620622
<x:String x:Key="Text.RemoteCM.Edit" xml:space="preserve">Edit...</x:String>
621623
<x:String x:Key="Text.RemoteCM.Fetch" xml:space="preserve">Fetch</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
<x:String x:Key="Text.Configure.CommitMessageTemplate.Name" xml:space="preserve">模板名 :</x:String>
178178
<x:String x:Key="Text.Configure.CustomAction" xml:space="preserve">自定义操作</x:String>
179179
<x:String x:Key="Text.Configure.CustomAction.Arguments" xml:space="preserve">命令行参数 :</x:String>
180-
<x:String x:Key="Text.Configure.CustomAction.Arguments.Tip" xml:space="preserve">内置变量:${REPO} 仓库路径、${BRANCH} 选中的分支、${SHA} 选中的提交哈希,${TAG} 选中的标签</x:String>
180+
<x:String x:Key="Text.Configure.CustomAction.Arguments.Tip" xml:space="preserve">内置变量:${REPO} 仓库路径、${REMOTE} 选中的远程仓库或选中分支所属的远程仓库、${BRANCH} 选中的分支、${SHA} 选中的提交哈希,${TAG} 选中的标签</x:String>
181181
<x:String x:Key="Text.Configure.CustomAction.Executable" xml:space="preserve">可执行文件路径 :</x:String>
182182
<x:String x:Key="Text.Configure.CustomAction.InputControls" xml:space="preserve">输入控件 :</x:String>
183183
<x:String x:Key="Text.Configure.CustomAction.InputControls.Edit" xml:space="preserve">编辑</x:String>
@@ -186,6 +186,7 @@
186186
<x:String x:Key="Text.Configure.CustomAction.Scope" xml:space="preserve">作用目标 :</x:String>
187187
<x:String x:Key="Text.Configure.CustomAction.Scope.Branch" xml:space="preserve">选中的分支</x:String>
188188
<x:String x:Key="Text.Configure.CustomAction.Scope.Commit" xml:space="preserve">选中的提交</x:String>
189+
<x:String x:Key="Text.Configure.CustomAction.Scope.Remote" xml:space="preserve">远程仓库</x:String>
189190
<x:String x:Key="Text.Configure.CustomAction.Scope.Repository" xml:space="preserve">仓库</x:String>
190191
<x:String x:Key="Text.Configure.CustomAction.Scope.Tag" xml:space="preserve">选中的标签</x:String>
191192
<x:String x:Key="Text.Configure.CustomAction.WaitForExit" xml:space="preserve">等待操作执行完成</x:String>
@@ -620,6 +621,7 @@
620621
<x:String x:Key="Text.Remote.URL" xml:space="preserve">仓库地址 :</x:String>
621622
<x:String x:Key="Text.Remote.URL.Placeholder" xml:space="preserve">远程仓库的地址</x:String>
622623
<x:String x:Key="Text.RemoteCM.CopyURL" xml:space="preserve">复制远程地址</x:String>
624+
<x:String x:Key="Text.RemoteCM.CustomAction" xml:space="preserve">自定义操作</x:String>
623625
<x:String x:Key="Text.RemoteCM.Delete" xml:space="preserve">删除 ...</x:String>
624626
<x:String x:Key="Text.RemoteCM.Edit" xml:space="preserve">编辑 ...</x:String>
625627
<x:String x:Key="Text.RemoteCM.Fetch" xml:space="preserve">拉取(fetch)更新</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
<x:String x:Key="Text.Configure.CommitMessageTemplate.Name" xml:space="preserve">範本名稱:</x:String>
178178
<x:String x:Key="Text.Configure.CustomAction" xml:space="preserve">自訂動作</x:String>
179179
<x:String x:Key="Text.Configure.CustomAction.Arguments" xml:space="preserve">指令參數:</x:String>
180-
<x:String x:Key="Text.Configure.CustomAction.Arguments.Tip" xml:space="preserve">內建參數: ${REPO} 存放庫路徑、${BRANCH} 所選的分支、${SHA} 所選的提交編號、${TAG} 所選的標籤</x:String>
180+
<x:String x:Key="Text.Configure.CustomAction.Arguments.Tip" xml:space="preserve">內建參數: ${REPO} 存放庫路徑、${REMOTE} 所選的遠端存放庫或所選分支的遠端、${BRANCH} 所選的分支、${SHA} 所選的提交編號、${TAG} 所選的標籤</x:String>
181181
<x:String x:Key="Text.Configure.CustomAction.Executable" xml:space="preserve">可執行檔案路徑:</x:String>
182182
<x:String x:Key="Text.Configure.CustomAction.InputControls" xml:space="preserve">輸入控制項:</x:String>
183183
<x:String x:Key="Text.Configure.CustomAction.InputControls.Edit" xml:space="preserve">編輯</x:String>
@@ -186,6 +186,7 @@
186186
<x:String x:Key="Text.Configure.CustomAction.Scope" xml:space="preserve">執行範圍:</x:String>
187187
<x:String x:Key="Text.Configure.CustomAction.Scope.Branch" xml:space="preserve">選取的分支</x:String>
188188
<x:String x:Key="Text.Configure.CustomAction.Scope.Commit" xml:space="preserve">選取的提交</x:String>
189+
<x:String x:Key="Text.Configure.CustomAction.Scope.Remote" xml:space="preserve">遠端存放庫</x:String>
189190
<x:String x:Key="Text.Configure.CustomAction.Scope.Repository" xml:space="preserve">存放庫</x:String>
190191
<x:String x:Key="Text.Configure.CustomAction.Scope.Tag" xml:space="preserve">選取的標籤</x:String>
191192
<x:String x:Key="Text.Configure.CustomAction.WaitForExit" xml:space="preserve">等待自訂動作執行結束</x:String>
@@ -620,6 +621,7 @@
620621
<x:String x:Key="Text.Remote.URL" xml:space="preserve">存放庫網址:</x:String>
621622
<x:String x:Key="Text.Remote.URL.Placeholder" xml:space="preserve">遠端存放庫的網址</x:String>
622623
<x:String x:Key="Text.RemoteCM.CopyURL" xml:space="preserve">複製遠端網址</x:String>
624+
<x:String x:Key="Text.RemoteCM.CustomAction" xml:space="preserve">自訂動作</x:String>
623625
<x:String x:Key="Text.RemoteCM.Delete" xml:space="preserve">刪除...</x:String>
624626
<x:String x:Key="Text.RemoteCM.Edit" xml:space="preserve">編輯...</x:String>
625627
<x:String x:Key="Text.RemoteCM.Fetch" xml:space="preserve">提取 (fetch) 更新</x:String>

src/ViewModels/ExecuteCustomAction.cs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -119,35 +119,11 @@ public List<ICustomActionControlParameter> ControlParameters
119119
get;
120120
} = [];
121121

122-
public ExecuteCustomAction(Repository repo, Models.CustomAction action)
122+
public ExecuteCustomAction(Repository repo, Models.CustomAction action, object scopeTarget)
123123
{
124124
_repo = repo;
125125
CustomAction = action;
126-
Target = new Models.Null();
127-
PrepareControlParameters();
128-
}
129-
130-
public ExecuteCustomAction(Repository repo, Models.CustomAction action, Models.Branch branch)
131-
{
132-
_repo = repo;
133-
CustomAction = action;
134-
Target = branch;
135-
PrepareControlParameters();
136-
}
137-
138-
public ExecuteCustomAction(Repository repo, Models.CustomAction action, Models.Commit commit)
139-
{
140-
_repo = repo;
141-
CustomAction = action;
142-
Target = commit;
143-
PrepareControlParameters();
144-
}
145-
146-
public ExecuteCustomAction(Repository repo, Models.CustomAction action, Models.Tag tag)
147-
{
148-
_repo = repo;
149-
CustomAction = action;
150-
Target = tag;
126+
Target = scopeTarget ?? new Models.Null();
151127
PrepareControlParameters();
152128
}
153129

@@ -206,9 +182,10 @@ private string PrepareStringByTarget(string org)
206182

207183
return Target switch
208184
{
209-
Models.Branch b => org.Replace("${BRANCH}", b.FriendlyName),
185+
Models.Branch b => org.Replace("${BRANCH}", b.FriendlyName).Replace("${REMOTE}", b.Remote),
210186
Models.Commit c => org.Replace("${SHA}", c.SHA),
211187
Models.Tag t => org.Replace("${TAG}", t.Name),
188+
Models.Remote r => org.Replace("${REMOTE}", r.Name),
212189
_ => org
213190
};
214191
}

src/ViewModels/Repository.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -876,19 +876,12 @@ public void ApplyPatch()
876876
ShowPopup(new Apply(this));
877877
}
878878

879-
public async Task ExecCustomActionAsync(Models.CustomAction action, object scope)
879+
public async Task ExecCustomActionAsync(Models.CustomAction action, object scopeTarget)
880880
{
881881
if (!CanCreatePopup())
882882
return;
883883

884-
var popup = scope switch
885-
{
886-
Models.Branch b => new ExecuteCustomAction(this, action, b),
887-
Models.Commit c => new ExecuteCustomAction(this, action, c),
888-
Models.Tag t => new ExecuteCustomAction(this, action, t),
889-
_ => new ExecuteCustomAction(this, action)
890-
};
891-
884+
var popup = new ExecuteCustomAction(this, action, scopeTarget);
892885
if (action.Controls.Count == 0)
893886
await ShowAndStartPopupAsync(popup);
894887
else

src/Views/BranchTree.axaml.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ private ContextMenu CreateContextMenuForRemote(ViewModels.Repository repo, Model
940940
menu.Items.Add(edit);
941941
menu.Items.Add(delete);
942942
menu.Items.Add(new MenuItem() { Header = "-" });
943+
TryToAddCustomActionsToRemoteContextMenu(repo, menu, remote);
943944
menu.Items.Add(copy);
944945
return menu;
945946
}
@@ -1112,6 +1113,35 @@ private void TryToAddCustomActionsToBranchContextMenu(ViewModels.Repository repo
11121113
menu.Items.Add(new MenuItem() { Header = "-" });
11131114
}
11141115

1116+
private void TryToAddCustomActionsToRemoteContextMenu(ViewModels.Repository repo, ContextMenu menu, Models.Remote remote)
1117+
{
1118+
var actions = repo.GetCustomActions(Models.CustomActionScope.Remote);
1119+
if (actions.Count == 0)
1120+
return;
1121+
1122+
var custom = new MenuItem();
1123+
custom.Header = App.Text("RemoteCM.CustomAction");
1124+
custom.Icon = App.CreateMenuIcon("Icons.Action");
1125+
1126+
foreach (var action in actions)
1127+
{
1128+
var (dup, label) = action;
1129+
var item = new MenuItem();
1130+
item.Icon = App.CreateMenuIcon("Icons.Action");
1131+
item.Header = label;
1132+
item.Click += async (_, e) =>
1133+
{
1134+
await repo.ExecCustomActionAsync(dup, remote);
1135+
e.Handled = true;
1136+
};
1137+
1138+
custom.Items.Add(item);
1139+
}
1140+
1141+
menu.Items.Add(custom);
1142+
menu.Items.Add(new MenuItem() { Header = "-" });
1143+
}
1144+
11151145
private bool _disableSelectionChangingEvent = false;
11161146
}
11171147
}

src/Views/ExecuteCustomAction.axaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@
5050
<TextBlock VerticalAlignment="Center" Text="{Binding Name}" Margin="8,0,0,0"/>
5151
</StackPanel>
5252
</DataTemplate>
53+
54+
<DataTemplate x:DataType="m:Remote">
55+
<StackPanel Orientation="Horizontal" Height="20" VerticalAlignment="Center">
56+
<Path Width="14" Height="14" Data="{StaticResource Icons.Remote}"/>
57+
<TextBlock VerticalAlignment="Center" Text="{Binding Name}" Margin="8,0,0,0"/>
58+
</StackPanel>
59+
</DataTemplate>
5360
</ContentControl.DataTemplates>
5461
</ContentControl>
5562
</Grid>

src/Views/Preferences.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@
656656
<ComboBoxItem Content="{DynamicResource Text.Configure.CustomAction.Scope.Commit}"/>
657657
<ComboBoxItem Content="{DynamicResource Text.Configure.CustomAction.Scope.Branch}"/>
658658
<ComboBoxItem Content="{DynamicResource Text.Configure.CustomAction.Scope.Tag}"/>
659+
<ComboBoxItem Content="{DynamicResource Text.Configure.CustomAction.Scope.Remote}"/>
659660
</ComboBox>
660661

661662
<TextBlock Margin="0,12,0,0" Text="{DynamicResource Text.Configure.CustomAction.Executable}"/>

src/Views/RepositoryConfigure.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@
480480
<ComboBoxItem Content="{DynamicResource Text.Configure.CustomAction.Scope.Commit}"/>
481481
<ComboBoxItem Content="{DynamicResource Text.Configure.CustomAction.Scope.Branch}"/>
482482
<ComboBoxItem Content="{DynamicResource Text.Configure.CustomAction.Scope.Tag}"/>
483+
<ComboBoxItem Content="{DynamicResource Text.Configure.CustomAction.Scope.Remote}"/>
483484
</ComboBox>
484485

485486
<TextBlock Margin="0,12,0,0" Text="{DynamicResource Text.Configure.CustomAction.Executable}"/>

0 commit comments

Comments
 (0)