]> Untitled Git - hvutilities.git/commitdiff
gitseg: add support for setting upstream remote
authorHugo Villeneuve <hugo@hugovil.com>
Mon, 30 Mar 2026 15:52:12 +0000 (11:52 -0400)
committerHugo Villeneuve <hugo@hugovil.com>
Thu, 2 Apr 2026 23:34:04 +0000 (19:34 -0400)
Signed-off-by: Hugo Villeneuve <hugo@hugovil.com>
scripts/gitseg

index 5b336acf9e4f9656db3f553919ee43c51357f853..fc80f5f5b088844769b1ef162d19a0ff3346df86 100644 (file)
@@ -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