]> git.ipfire.org Git - thirdparty/git.git/commitdiff
contrib/subtree: reduce function side-effects
authorColin Stagner <ask+git@howdoi.land>
Thu, 5 Mar 2026 23:55:47 +0000 (17:55 -0600)
committerJunio C Hamano <gitster@pobox.com>
Fri, 6 Mar 2026 21:57:48 +0000 (13:57 -0800)
`process_subtree_split_trailer()` communicates its return value
to the caller by setting a variable (`sub`) that is also defined
by the calling function. This is both unclear and encourages
side-effects.

Invoke this function in a sub-shell instead.

Signed-off-by: Colin Stagner <ask+git@howdoi.land>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
contrib/subtree/git-subtree.sh

index 791fd8260c470395ead376d1c2556f610b64c3a8..bae5d9170bd7a3e2ba3b702c94dd187c371db15b 100755 (executable)
@@ -373,6 +373,10 @@ try_remove_previous () {
 }
 
 # Usage: process_subtree_split_trailer SPLIT_HASH MAIN_HASH [REPOSITORY]
+#
+# Parse SPLIT_HASH as a commit. If the commit is not found, fetches
+# REPOSITORY and tries again. If found, prints full commit hash.
+# Otherwise, dies.
 process_subtree_split_trailer () {
        assert test $# -ge 2
        assert test $# -le 3
@@ -400,6 +404,7 @@ process_subtree_split_trailer () {
                        die "$fail_msg"
                fi
        fi
+       echo "${sub}"
 }
 
 # Usage: find_latest_squash DIR [REPOSITORY]
@@ -432,7 +437,7 @@ find_latest_squash () {
                        main="$b"
                        ;;
                git-subtree-split:)
-                       process_subtree_split_trailer "$b" "$sq" "$repository"
+                       sub="$(process_subtree_split_trailer "$b" "$sq" "$repository")" || exit 1
                        ;;
                END)
                        if test -n "$sub"
@@ -489,7 +494,7 @@ find_existing_splits () {
                        main="$b"
                        ;;
                git-subtree-split:)
-                       process_subtree_split_trailer "$b" "$sq" "$repository"
+                       sub="$(process_subtree_split_trailer "$b" "$sq" "$repository")" || exit 1
                        ;;
                END)
                        debug "Main is: '$main'"