subtree: add 'die_incompatible_opt' function to reduce duplication

9a3e3ca2ba (subtree: be stricter about validating flags, 2021-04-27) added validation code to check that options given to 'git subtree <cmd>' made sense with the command being used. Refactor these checks by adding a 'die_incompatible_opt' function to reduce code duplication. Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Philippe Blain committed Oct 21, 2022 at 15:13 UTC 2e94339fdc3122fd2f92e865105523bc3866a65f
1 file changed +20 -12
contrib/subtree/git-subtree.sh
+20 -12
@@ -102,6 +102,14 @@ assert () {
102 fi
103 }
104
105 +# Usage: die_incompatible_opt OPTION COMMAND
106 +die_incompatible_opt () {
107 + assert test "$#" = 2
108 + opt="$1"
109 + arg_command="$2"
110 + die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
111 +}
112 +
113 main () {
114 if test $# -eq 0
115 then
@@ -176,16 +184,16 @@ main () {
184 arg_debug=1
185 ;;
186 --annotate)
179 - test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
187 + test -n "$allow_split" || die_incompatible_opt "$opt" "$arg_command"
188 arg_split_annotate="$1"
189 shift
190 ;;
191 --no-annotate)
184 - test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
192 + test -n "$allow_split" || die_incompatible_opt "$opt" "$arg_command"
193 arg_split_annotate=
194 ;;
195 -b)
188 - test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
196 + test -n "$allow_split" || die_incompatible_opt "$opt" "$arg_command"
197 arg_split_branch="$1"
198 shift
199 ;;
@@ -194,7 +202,7 @@ main () {
202 shift
203 ;;
204 -m)
197 - test -n "$allow_addmerge" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
205 + test -n "$allow_addmerge" || die_incompatible_opt "$opt" "$arg_command"
206 arg_addmerge_message="$1"
207 shift
208 ;;
@@ -202,34 +210,34 @@ main () {
210 arg_prefix=
211 ;;
212 --onto)
205 - test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
213 + test -n "$allow_split" || die_incompatible_opt "$opt" "$arg_command"
214 arg_split_onto="$1"
215 shift
216 ;;
217 --no-onto)
210 - test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
218 + test -n "$allow_split" || die_incompatible_opt "$opt" "$arg_command"
219 arg_split_onto=
220 ;;
221 --rejoin)
214 - test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
222 + test -n "$allow_split" || die_incompatible_opt "$opt" "$arg_command"
223 ;;
224 --no-rejoin)
217 - test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
225 + test -n "$allow_split" || die_incompatible_opt "$opt" "$arg_command"
226 ;;
227 --ignore-joins)
220 - test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
228 + test -n "$allow_split" || die_incompatible_opt "$opt" "$arg_command"
229 arg_split_ignore_joins=1
230 ;;
231 --no-ignore-joins)
224 - test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
232 + test -n "$allow_split" || die_incompatible_opt "$opt" "$arg_command"
233 arg_split_ignore_joins=
234 ;;
235 --squash)
228 - test -n "$allow_addmerge" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
236 + test -n "$allow_addmerge" || die_incompatible_opt "$opt" "$arg_command"
237 arg_addmerge_squash=1
238 ;;
239 --no-squash)
232 - test -n "$allow_addmerge" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
240 + test -n "$allow_addmerge" || die_incompatible_opt "$opt" "$arg_command"
241 arg_addmerge_squash=
242 ;;
243 --)