From b8be238af7271493fbfe9649de0283a20310c0e2 Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Mon, 30 Mar 2026 11:52:12 -0400 Subject: [PATCH] gitseg: add support for setting upstream remote Signed-off-by: Hugo Villeneuve --- scripts/gitseg | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/scripts/gitseg b/scripts/gitseg index 5b336ac..fc80f5f 100644 --- a/scripts/gitseg +++ b/scripts/gitseg @@ -36,6 +36,7 @@ catch() # Set default values force_push="0" orig_branch="" +remote="" print_usage() { @@ -45,6 +46,7 @@ print_usage() echo "Options:" echo " -d debug mode" echo " -p force push stacked branches (\"stack\" command)" + echo " -r set remote for tracking" echo } @@ -79,6 +81,30 @@ gitseg_new() fi git checkout ${Q} -b ${dest_branch} ${src_branch} + + if git config --get branch.$(git branch --show-current).remote 1> /dev/null; then + log_dbg "Remove existing tracking infos" + git branch --unset-upstream + fi + + if [ "${remote}" != "" ]; then + if ! git ls-remote --exit-code ${remote} ${dest_branch} 1> /dev/null; then + # Git does not allow to set the upstream for a new branch if it + # doesn't already exist on the remote. + + log_dbg "New remote branch (push): ${remote}/${dest_branch}" + # 2> >(grep -v "remote:" >&2): + # >(...): process substitution, creates a temporary pipe that behaves like a file + # grep -v "remote:": Filters stderr removing "remote:" lines to filter out github annoying pull messages + # >&2: Inside the substitution, sends the filtered results back to the original stderr + git push ${Q} ${remote} ${dest_branch} 2> >(grep -v "remote:" >&2) + else + log_dbg "Existing remote branch (set upstream): ${remote}/${dest_branch}" + + # Set upstream tracking: + git branch ${Q} -u ${remote}/${dest_branch} + fi + fi } # Arg1: segment name @@ -165,7 +191,7 @@ gitseg_stack() fi } -while getopts "dhp" flag ;do +while getopts "dhpr:" flag ;do case ${flag} in d) debug="1" @@ -177,6 +203,9 @@ while getopts "dhp" flag ;do p) force_push="1" ;; + r) + remote="${OPTARG}" + ;; ?) log_err "${PROG_NAME}: Option invalide: ${OPTARG}." exit 1 -- 2.47.3