subtree: don't have loose code outside of a function

Shove all of the loose code inside of a main() function. This comes down to personal preference more than anything else. A preference that I've developed over years of maintaining large Bash scripts, but still a mere personal preference. In this specific case, it's also moving the `set -- -h`, the `git rev-parse --parseopt`, and the `. git-sh-setup` to be closer to all the rest of the argument parsing, which is a readability win on its own, IMO. "Ignore space change" is probably helpful when viewing this diff. Signed-off-by: Luke Shumaker <lukeshu@datawire.io> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Luke Shumaker committed Apr 27, 2021 at 15:17 UTC 5a3569774f7b6dcdbdf470ae1e40367200d987ec
1 file changed +125 -120
contrib/subtree/git-subtree.sh
+125 -120
@@ -4,10 +4,7 @@
4 #
5 # Copyright (C) 2009 Avery Pennarun <apenwarr@gmail.com>
6 #
7 -if test $# -eq 0
8 -then
9 - set -- -h
10 -fi
7 +
8 OPTS_SPEC="\
9 git subtree add --prefix=<prefix> <commit>
10 git subtree add --prefix=<prefix> <repository> <ref>
@@ -30,12 +27,8 @@ rejoin merge the new branch back into HEAD
27 options for 'add', 'merge', and 'pull'
28 squash merge subtree changes as a single commit
29 "
33 -eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
30
31 PATH=$PATH:$(git --exec-path)
36 -. git-sh-setup
37 -
38 -require_work_tree
32
33 quiet=
34 branch=
@@ -84,126 +77,138 @@ ensure_single_rev () {
77 fi
78 }
79
87 -while test $# -gt 0
88 -do
89 - opt="$1"
90 - shift
80 +main () {
81 + if test $# -eq 0
82 + then
83 + set -- -h
84 + fi
85 + eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
86 + . git-sh-setup
87 + require_work_tree
88
92 - case "$opt" in
93 - -q)
94 - quiet=1
95 - ;;
96 - -d)
97 - debug=1
98 - ;;
99 - --annotate)
100 - annotate="$1"
101 - shift
102 - ;;
103 - --no-annotate)
104 - annotate=
105 - ;;
106 - -b)
107 - branch="$1"
108 - shift
109 - ;;
110 - -P)
111 - prefix="${1%/}"
112 - shift
113 - ;;
114 - -m)
115 - message="$1"
116 - shift
117 - ;;
118 - --no-prefix)
119 - prefix=
120 - ;;
121 - --onto)
122 - onto="$1"
89 + while test $# -gt 0
90 + do
91 + opt="$1"
92 shift
93 +
94 + case "$opt" in
95 + -q)
96 + quiet=1
97 + ;;
98 + -d)
99 + debug=1
100 + ;;
101 + --annotate)
102 + annotate="$1"
103 + shift
104 + ;;
105 + --no-annotate)
106 + annotate=
107 + ;;
108 + -b)
109 + branch="$1"
110 + shift
111 + ;;
112 + -P)
113 + prefix="${1%/}"
114 + shift
115 + ;;
116 + -m)
117 + message="$1"
118 + shift
119 + ;;
120 + --no-prefix)
121 + prefix=
122 + ;;
123 + --onto)
124 + onto="$1"
125 + shift
126 + ;;
127 + --no-onto)
128 + onto=
129 + ;;
130 + --rejoin)
131 + rejoin=1
132 + ;;
133 + --no-rejoin)
134 + rejoin=
135 + ;;
136 + --ignore-joins)
137 + ignore_joins=1
138 + ;;
139 + --no-ignore-joins)
140 + ignore_joins=
141 + ;;
142 + --squash)
143 + squash=1
144 + ;;
145 + --no-squash)
146 + squash=
147 + ;;
148 + --)
149 + break
150 + ;;
151 + *)
152 + die "Unexpected option: $opt"
153 + ;;
154 + esac
155 + done
156 +
157 + command="$1"
158 + shift
159 +
160 + case "$command" in
161 + add|merge|pull)
162 + default=
163 ;;
125 - --no-onto)
126 - onto=
127 - ;;
128 - --rejoin)
129 - rejoin=1
130 - ;;
131 - --no-rejoin)
132 - rejoin=
133 - ;;
134 - --ignore-joins)
135 - ignore_joins=1
136 - ;;
137 - --no-ignore-joins)
138 - ignore_joins=
139 - ;;
140 - --squash)
141 - squash=1
164 + split|push)
165 + default="--default HEAD"
166 ;;
143 - --no-squash)
144 - squash=
167 + *)
168 + die "Unknown command '$command'"
169 ;;
146 - --)
147 - break
170 + esac
171 +
172 + if test -z "$prefix"
173 + then
174 + die "You must provide the --prefix option."
175 + fi
176 +
177 + case "$command" in
178 + add)
179 + test -e "$prefix" &&
180 + die "prefix '$prefix' already exists."
181 ;;
182 *)
150 - die "Unexpected option: $opt"
183 + test -e "$prefix" ||
184 + die "'$prefix' does not exist; use 'git subtree add'"
185 ;;
186 esac
153 -done
154 -
155 -command="$1"
156 -shift
157 -
158 -case "$command" in
159 -add|merge|pull)
160 - default=
161 - ;;
162 -split|push)
163 - default="--default HEAD"
164 - ;;
165 -*)
166 - die "Unknown command '$command'"
167 - ;;
168 -esac
169 -
170 -if test -z "$prefix"
171 -then
172 - die "You must provide the --prefix option."
173 -fi
174 -
175 -case "$command" in
176 -add)
177 - test -e "$prefix" &&
178 - die "prefix '$prefix' already exists."
179 - ;;
180 -*)
181 - test -e "$prefix" ||
182 - die "'$prefix' does not exist; use 'git subtree add'"
183 - ;;
184 -esac
185 -
186 -dir="$(dirname "$prefix/.")"
187 -
188 -if test "$command" != "pull" &&
189 - test "$command" != "add" &&
190 - test "$command" != "push"
191 -then
192 - revs=$(git rev-parse $default --revs-only "$@") || exit $?
193 - dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $?
194 - ensure_single_rev $revs
195 - if test -n "$dirs"
196 - then
197 - die "Error: Use --prefix instead of bare filenames."
198 - fi
199 -fi
200 -
201 -debug "command: {$command}"
202 -debug "quiet: {$quiet}"
203 -debug "revs: {$revs}"
204 -debug "dir: {$dir}"
205 -debug "opts: {$*}"
206 -debug
187 +
188 + dir="$(dirname "$prefix/.")"
189 +
190 + if test "$command" != "pull" &&
191 + test "$command" != "add" &&
192 + test "$command" != "push"
193 + then
194 + revs=$(git rev-parse $default --revs-only "$@") || exit $?
195 + dirs=$(git rev-parse --no-revs --no-flags "$@") || exit $?
196 + ensure_single_rev $revs
197 + if test -n "$dirs"
198 + then
199 + die "Error: Use --prefix instead of bare filenames."
200 + fi
201 + fi
202 +
203 + debug "command: {$command}"
204 + debug "quiet: {$quiet}"
205 + debug "revs: {$revs}"
206 + debug "dir: {$dir}"
207 + debug "opts: {$*}"
208 + debug
209 +
210 + "cmd_$command" "$@"
211 +}
212
213 cache_setup () {
214 cachedir="$GIT_DIR/subtree-cache/$$"
@@ -898,4 +903,4 @@ cmd_push () {
903 fi
904 }
905
901 -"cmd_$command" "$@"
906 +main "$@"