From 0729c3044ae2a4f160a5fdc43320eddb5d5e2bdb Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 27 Jun 2025 15:58:00 +0800 Subject: [PATCH] enhance: supports to use `${BRANCH}`/`${COMMIT}`/`${TAG}` in `Default` field of `TextBox`/`PathSelector` in custom actions (#1466) Signed-off-by: leo --- src/ViewModels/ExecuteCustomAction.cs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/ViewModels/ExecuteCustomAction.cs b/src/ViewModels/ExecuteCustomAction.cs index 7883dc2c..198b7b2d 100644 --- a/src/ViewModels/ExecuteCustomAction.cs +++ b/src/ViewModels/ExecuteCustomAction.cs @@ -127,12 +127,7 @@ namespace SourceGit.ViewModels ProgressDescription = "Run custom action ..."; var cmdline = CustomAction.Arguments.Replace("${REPO}", GetWorkdir()); - if (Target is Models.Branch b) - cmdline = cmdline.Replace("${BRANCH}", b.FriendlyName); - else if (Target is Models.Commit c) - cmdline = cmdline.Replace("${SHA}", c.SHA); - else if (Target is Models.Tag t) - cmdline = cmdline.Replace("${TAG}", t.Name); + cmdline = PrepareStringByTarget(cmdline); for (var i = ControlParameters.Count - 1; i >= 0; i--) { @@ -165,18 +160,30 @@ namespace SourceGit.ViewModels switch (ctl.Type) { case Models.CustomActionControlType.TextBox: - ControlParameters.Add(new CustomActionControlTextBox(ctl.Label, ctl.Description, ctl.StringValue)); + ControlParameters.Add(new CustomActionControlTextBox(ctl.Label, ctl.Description, PrepareStringByTarget(ctl.StringValue))); break; case Models.CustomActionControlType.CheckBox: ControlParameters.Add(new CustomActionControlCheckBox(ctl.Label, ctl.Description, ctl.StringValue, ctl.BoolValue)); break; case Models.CustomActionControlType.PathSelector: - ControlParameters.Add(new CustomActionControlPathSelector(ctl.Label, ctl.Description, ctl.BoolValue, ctl.StringValue)); + ControlParameters.Add(new CustomActionControlPathSelector(ctl.Label, ctl.Description, ctl.BoolValue, PrepareStringByTarget(ctl.StringValue))); break; } } } + private string PrepareStringByTarget(string org) + { + if (Target is Models.Branch b) + return org.Replace("${BRANCH}", b.FriendlyName); + else if (Target is Models.Commit c) + return org.Replace("${SHA}", c.SHA); + else if (Target is Models.Tag t) + return org.Replace("${TAG}", t.Name); + + return org; + } + private string GetWorkdir() { return OperatingSystem.IsWindows() ? _repo.FullPath.Replace("/", "\\") : _repo.FullPath;