sourcegit icon indicating copy to clipboard operation
sourcegit copied to clipboard

[Enhancement] Custom Action on Branch needs to know more about Remote (when applicable)

Open goran-w opened this issue 4 months ago • 5 comments

When running a Custom Action on the Branch scope, the ${BRANCH} variable contains the (short / abbreviated) name of the branch, but there's currently no (easy) way for the Action to distinguish whether the branch is a local or a remote one (and in the latter case, what the name of the Remote is). This information can be crucial for correct processing, for example when an Action needs to communicate (push etc) with a remote service like GitHub.

For a remote branch, its (abbreviated) name starts with the Remote name: <remote>/<branch> (for example: origin/my-branch) However, there's nothing preventing a user from creating a local branch with a completely identical name, or any other name which has a <ref-folder>/<ref-file> structure. Therefore, without additional info we can't assume that the "top-folder-part" of the name is a Remote (rather than just any other ref-folder).

While it's possible to use Git commands and additional string-parsing to extract the remote name (after extracting the full ref name), a much more user-friendly solution would be to strip the Remote prefix from the ${BRANCH} variable and instead expose it in a separate variable ${BRANCH_REMOTE} (or similar) which would be an empty string for a local branch. From a user standpoint, it's much easier to re-assemble the <remote>/<branch> name (if needed) than to extract the needed info from the (abbreviated) branch-name alone.

Separating the Branch and Remote names into separate variables is IMHO the most user-friendly solution, but other (less attractive) alternatives exist:

  • Adding a new variable ${FULL_REF} (or similar), containing the full (non-abbreviated) ref-name for the Branch and Tag scopes (as returned by git rev-parse --symbolic-full-name <abbreviated-ref-name>).
  • Separating Local Branch and Remote Branch into two separate Action Scopes.
  • Adding a new variable ${BRANCH_IS_REMOTE}.
  • Do nothing, leaving the user to figure out an alternative solution for themselves.

These latter alternatives are IMHO not good solutions on their own.

However, the ${FULL_REF} idea would be useful in addition to the ${BRANCH_REMOTE} solution for remote branches...

(Also, separating the Local / Remote branch-scopes would make it easier to make Actions apply to only one of the scopes, but might necessitate a 3rd "combined" Local + Remote branch-scope to avoid having to duplicate Actions that apply to both...)

goran-w avatar Aug 05 '25 10:08 goran-w

#1657 https://github.com/sourcegit-scm/sourcegit/issues/1629 https://github.com/sourcegit-scm/sourcegit/issues/1628

heartacker avatar Aug 05 '25 11:08 heartacker

One of my use-cases is to provide a Custom "Rename remote branch" Action (see #1457).

Separating a remote branch name (like origin/my-branch) into a Remote part (${REMOTE} = origin) and a Branch part (${BRANCH} = my-branch) already at the Action Input stage would be VERY helpful - I could then use ${BRANCH} as the default (initial) value in the Input text-box for entering the new Branch-name, without having the Remote-name clutter its content...

I.e, in this example, I don't want the 'origin/' part in the "New branch name" text-box : Image

My aim is to edit the old branch-name into a new one, so the 'origin/' part is superfluous clutter: Image

NOTE:

  • The "Target:" indicator can stay as-is.
  • (I'm only concerned with separating the Remote & Branch variables, for greater ease-of-use.)
  • If I really wanted to have ' origin/my-branch' displayed in the Action Input dialog, i could easily recreate it as '${REMOTE}/${BRANCH}'

goran-w avatar Aug 05 '25 12:08 goran-w

Currently, a new scope Remote has been added. And a new built-in variable ${REMOTE} also has been added.

  • When the target is a selected remote branch, ${BRANCH} is still the FriendlyName of selected branch, which includes the remote's name. ${REMOTE} is the name of remote that the branch belongs to.
  • When the target is a selected remote, ${REMOTE} is the name of it.

love-linger avatar Aug 14 '25 10:08 love-linger

Thank you, this is great! However, something is still missing...

Since there is already a built-in 'Rename <branch>' context-command for Local Branches, I want my Custom Action "Rename Remote Branch" to appear only on actual Remote branches but not on Local branches.

Therefore, could we please separate the "Branch" scope into two separate scopes "Local Branch" and "Remote Branch", which IMHO would make a lot of sense as the two cases are quite different? They are, after all, separated into separate lists/trees in the SourceGit UI. (The only issue I can see is that it becomes slightly more tedious to provide a Custom Action for BOTH of these scopes, if wanted, but that too would become easier once the #1634 PR gets merged... Or we could, of course, add a 3rd scope "Any Branch", but that seems a bit redundant.)

Also, I would now strongly suggest that the '<remote>/' part of the branch name should be stripped from the ${BRANCH} variable, since the separate ${REMOTE} variable now exists and is applied for Remote Branches. Otherwise, it becomes rather unconvenient to use the variable as a Default value for editing! See for example the screenshot below, where I want to edit the original/old name of a branch (for renaming it), but I definitely don't want to edit the name of the remote. 😅 (BTW, the '(Remote)' textbox was added here just for illustration / comparison purposes...)

Image

goran-w avatar Aug 15 '25 07:08 goran-w

Related commit 4e01270cc08702e6743d968f14f2df7b1178ffeb changes the ${BRANCH} variable to contain only the pure branch name, just as I requested - thank you!

It also adds a new variable ${BRANCH_FRIENDLY_NAME} (which IMHO is actually a bit redundant, since it could easily be recreated as ${REMOTE}/${BRANCH} whenever ${REMOTE} is non-empty and as just ${BRANCH} otherwise).

(NOTE: The tooltip / info-box explaining the variables has a typo, it misspells "Friendly" as "Firendly".)

What remains now is a way to specify that a branch-scope Custom Action should be visible/available only for Local Branches, or only for Remote Branches, or (possibly) for both types... Either by splitting the Branch scope into two separate ones for Local vs Remote branch, or by providing some kind of sub-scope specifier?

goran-w avatar Aug 17 '25 18:08 goran-w