Skip to content

Commit 2e94339

Browse files
phil-blaingitster
authored andcommitted
subtree: add 'die_incompatible_opt' function to reduce duplication
9a3e3ca (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a50fcc1 commit 2e94339

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

contrib/subtree/git-subtree.sh

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ assert () {
102102
fi
103103
}
104104

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+
105113
main () {
106114
if test $# -eq 0
107115
then
@@ -176,16 +184,16 @@ main () {
176184
arg_debug=1
177185
;;
178186
--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"
180188
arg_split_annotate="$1"
181189
shift
182190
;;
183191
--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"
185193
arg_split_annotate=
186194
;;
187195
-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"
189197
arg_split_branch="$1"
190198
shift
191199
;;
@@ -194,42 +202,42 @@ main () {
194202
shift
195203
;;
196204
-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"
198206
arg_addmerge_message="$1"
199207
shift
200208
;;
201209
--no-prefix)
202210
arg_prefix=
203211
;;
204212
--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"
206214
arg_split_onto="$1"
207215
shift
208216
;;
209217
--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"
211219
arg_split_onto=
212220
;;
213221
--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"
215223
;;
216224
--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"
218226
;;
219227
--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"
221229
arg_split_ignore_joins=1
222230
;;
223231
--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"
225233
arg_split_ignore_joins=
226234
;;
227235
--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"
229237
arg_addmerge_squash=1
230238
;;
231239
--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"
233241
arg_addmerge_squash=
234242
;;
235243
--)

0 commit comments

Comments
 (0)