From c13fa5261332b4b2c6d1648a1157e4012a99f0ba Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Thu, 15 Jul 2021 10:48:25 +0300 Subject: [PATCH] Use custom local branch name as explicit remote user branch name for PR In a scenario where onee has several parallel PRs, one needs to use several local branches, and when updating PRs, _needs to provide PR branch name explicitly_ each time (my-PR-name-* in the example): git checkout featureFoo [ edit session ] git commit ./g review my-PR-name-1 git checkout featureBar [ edit session ] git commit ./g review my-PR-name-2 This changes the processing, so that when the user's local branch name is different from the tracked branch name, it is treated as if user used that name explicitly in a call: git checkout -b featureFoo origin/master git checkout -b featureBar origin/distro/collabora/co-6-4 [ ... ] git checkout featureFoo [ edit session ] git commit ./g review # this uses "featureFoo" as implicit second argument git checkout featureBar [ edit session ] git commit ./g review # this uses "featureBar" as implicit second argument so no need to remember which PR name was used where. This only works for local branch names that are not equal to a remote branch name, or to the last part (after last /) of any distro branch. Signed-off-by: Mike Kaganski Change-Id: I8b95f92f6e205e3c93ef30f4813f3549c2315872 --- g | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/g b/g index 23ca8aa77..14af07459 100755 --- a/g +++ b/g @@ -15,6 +15,21 @@ # 3) Uses './g review' to submit a pull request against . # +# not_in_standard_branches $BRANCH $REMOTE +not_in_standard_branches() { + # e.g., with $1 = co-4-2, and $2 = origin + # check for origin/co-4-2 branch + if git rev-parse --quiet --verify $2/$1 >/dev/null; then + return 1 # indicate failure - there's a top-level branch named that way + fi + # check for origin/distro/*/co-4-2 branches + local distro_branch + for distro_branch in $(git branch --remotes --list "$2/distro/*/$1"); do + return 2 # indicate failure - there's a distro branch named that way + done + return 0 +} + if [ -z "$(type -p gh)" ]; then echo "'gh' not found, install it from ." exit 1 @@ -38,6 +53,12 @@ if [ "$1" == "review" ]; then if [ -n "$2" ]; then REMOTE_BRANCH=private/$USER/$2 CUSTOM_BRANCH=y + elif [ "$TRACKED_BRANCH" != "$BRANCH" ]; then + if not_in_standard_branches "$BRANCH" "$REMOTE"; then + # there's no top-level or distro-specific remote branch named as local branch: use its name + REMOTE_BRANCH=private/$USER/$BRANCH + CUSTOM_BRANCH=y + fi fi # So that we have an up to date view on what remote branches exist.