git-submodule.sh: improve variables readability

When git-submodule.sh parses various options and switches, it sets some variables to values; the variables in turn affect the options given to git-submodule--helper. Currently, variables which correspond to switches have boolean values (for example, whenever "--force" is passed, force=1), while variables which correspond to options which take arguments have string values that sometimes contain the option name and sometimes only the option value. Set all of the variables to strings which contain the option name (e.g. force="--force" rather than force=1); this has a couple of advantages: it improves consistency, readability and debuggability. Suggested-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Roy Eldar <royeldar0@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Roy Eldar committed Dec 11, 2024 at 08:32 UTC 3ad0ba72274436f4e1eef6bed392e3e875484e2b
1 file changed +95 -118
git-submodule.sh
+95 -118
@@ -52,6 +52,10 @@ single_branch=
52 jobs=
53 recommend_shallow=
54 filter=
55 +deinit_all=
56 +default=
57 +summary_limit=
58 +for_status=
59
60 #
61 # Add a new submodule to the working tree, .gitmodules and the index
@@ -63,37 +67,33 @@ filter=
67 cmd_add()
68 {
69 # parse $args after "submodule ... add".
66 - reference_path=
70 while test $# -ne 0
71 do
72 case "$1" in
73 -b | --branch)
74 case "$2" in '') usage ;; esac
72 - branch=$2
75 + branch="--branch=$2"
76 shift
77 ;;
75 - -b*)
76 - branch="${1#-b}"
77 - ;;
78 - --branch=*)
79 - branch="${1#--branch=}"
78 + -b* | --branch=*)
79 + branch="$1"
80 ;;
81 -f | --force)
82 force=$1
83 ;;
84 -q|--quiet)
85 - quiet=1
85 + quiet=$1
86 ;;
87 --progress)
88 - progress=1
88 + progress=$1
89 ;;
90 --reference)
91 case "$2" in '') usage ;; esac
92 - reference_path=$2
92 + reference="--reference=$2"
93 shift
94 ;;
95 --reference=*)
96 - reference_path="${1#--reference=}"
96 + reference="$1"
97 ;;
98 --ref-format)
99 case "$2" in '') usage ;; esac
@@ -104,15 +104,15 @@ cmd_add()
104 ref_format="$1"
105 ;;
106 --dissociate)
107 - dissociate=1
107 + dissociate=$1
108 ;;
109 --name)
110 case "$2" in '') usage ;; esac
111 - custom_name=$2
111 + custom_name="--name=$2"
112 shift
113 ;;
114 --name=*)
115 - custom_name="${1#--name=}"
115 + custom_name="$1"
116 ;;
117 --depth)
118 case "$2" in '') usage ;; esac
@@ -120,7 +120,7 @@ cmd_add()
120 shift
121 ;;
122 --depth=*)
123 - depth=$1
123 + depth="$1"
124 ;;
125 --)
126 shift
@@ -142,14 +142,14 @@ cmd_add()
142 fi
143
144 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper add \
145 - ${quiet:+--quiet} \
146 - ${force:+--force} \
147 - ${progress:+"--progress"} \
148 - ${branch:+--branch "$branch"} \
149 - ${reference_path:+--reference "$reference_path"} \
145 + $quiet \
146 + $force \
147 + $progress \
148 + ${branch:+"$branch"} \
149 + ${reference:+"$reference"} \
150 ${ref_format:+"$ref_format"} \
151 - ${dissociate:+--dissociate} \
152 - ${custom_name:+--name "$custom_name"} \
151 + $dissociate \
152 + ${custom_name:+"$custom_name"} \
153 ${depth:+"$depth"} \
154 -- \
155 "$@"
@@ -168,10 +168,10 @@ cmd_foreach()
168 do
169 case "$1" in
170 -q|--quiet)
171 - quiet=1
171 + quiet=$1
172 ;;
173 --recursive)
174 - recursive=1
174 + recursive=$1
175 ;;
176 -*)
177 usage
@@ -184,8 +184,8 @@ cmd_foreach()
184 done
185
186 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper foreach \
187 - ${quiet:+--quiet} \
188 - ${recursive:+--recursive} \
187 + $quiet \
188 + $recursive \
189 -- \
190 "$@"
191 }
@@ -202,7 +202,7 @@ cmd_init()
202 do
203 case "$1" in
204 -q|--quiet)
205 - quiet=1
205 + quiet=$1
206 ;;
207 --)
208 shift
@@ -219,7 +219,7 @@ cmd_init()
219 done
220
221 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper init \
222 - ${quiet:+--quiet} \
222 + $quiet \
223 -- \
224 "$@"
225 }
@@ -230,7 +230,6 @@ cmd_init()
230 cmd_deinit()
231 {
232 # parse $args after "submodule ... deinit".
233 - deinit_all=
233 while test $# -ne 0
234 do
235 case "$1" in
@@ -238,10 +237,10 @@ cmd_deinit()
237 force=$1
238 ;;
239 -q|--quiet)
241 - quiet=1
240 + quiet=$1
241 ;;
242 --all)
244 - deinit_all=t
243 + deinit_all=$1
244 ;;
245 --)
246 shift
@@ -258,9 +257,9 @@ cmd_deinit()
257 done
258
259 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit \
261 - ${quiet:+--quiet} \
262 - ${force:+--force} \
263 - ${deinit_all:+--all} \
260 + $quiet \
261 + $force \
262 + $deinit_all \
263 -- \
264 "$@"
265 }
@@ -277,31 +276,31 @@ cmd_update()
276 do
277 case "$1" in
278 -q|--quiet)
280 - quiet=1
279 + quiet=$1
280 ;;
281 -v|--verbose)
283 - quiet=0
282 + quiet=
283 ;;
284 --progress)
286 - progress=1
285 + progress=$1
286 ;;
287 -i|--init)
289 - init=1
288 + init=$1
289 ;;
290 --require-init)
292 - require_init=1
291 + require_init=$1
292 ;;
293 --remote)
295 - remote=1
294 + remote=$1
295 ;;
296 -N|--no-fetch)
298 - nofetch=1
297 + nofetch=$1
298 ;;
299 -f|--force)
300 force=$1
301 ;;
302 -r|--rebase)
304 - rebase=1
303 + rebase=$1
304 ;;
305 --ref-format)
306 case "$2" in '') usage ;; esac
@@ -320,22 +319,19 @@ cmd_update()
319 reference="$1"
320 ;;
321 --dissociate)
323 - dissociate=1
322 + dissociate=$1
323 ;;
324 -m|--merge)
326 - merge=1
325 + merge=$1
326 ;;
327 --recursive)
329 - recursive=1
328 + recursive=$1
329 ;;
330 --checkout)
332 - checkout=1
333 - ;;
334 - --recommend-shallow)
335 - recommend_shallow="--recommend-shallow"
331 + checkout=$1
332 ;;
337 - --no-recommend-shallow)
338 - recommend_shallow="--no-recommend-shallow"
333 + --recommend-shallow|--no-recommend-shallow)
334 + recommend_shallow=$1
335 ;;
336 --depth)
337 case "$2" in '') usage ;; esac
@@ -343,24 +339,18 @@ cmd_update()
339 shift
340 ;;
341 --depth=*)
346 - depth=$1
342 + depth="$1"
343 ;;
344 -j|--jobs)
345 case "$2" in '') usage ;; esac
346 jobs="--jobs=$2"
347 shift
348 ;;
353 - -j*)
354 - jobs="--jobs=${1#-j}"
355 - ;;
356 - --jobs=*)
357 - jobs=$1
349 + -j*|--jobs=*)
350 + jobs="$1"
351 ;;
359 - --single-branch)
360 - single_branch="--single-branch"
361 - ;;
362 - --no-single-branch)
363 - single_branch="--no-single-branch"
352 + --single-branch|--no-single-branch)
353 + single_branch=$1
354 ;;
355 --filter)
356 case "$2" in '') usage ;; esac
@@ -385,22 +375,21 @@ cmd_update()
375 done
376
377 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper update \
388 - ${quiet:+--quiet} \
389 - ${force:+--force} \
390 - ${progress:+"--progress"} \
391 - ${remote:+--remote} \
392 - ${recursive:+--recursive} \
393 - ${init:+--init} \
394 - ${nofetch:+--no-fetch} \
395 - ${rebase:+--rebase} \
396 - ${merge:+--merge} \
397 - ${checkout:+--checkout} \
378 + $quiet \
379 + $force \
380 + $progress \
381 + $remote \
382 + $recursive \
383 + $init \
384 + $nofetch \
385 + $rebase \
386 + $merge \
387 + $checkout \
388 ${ref_format:+"$ref_format"} \
389 ${reference:+"$reference"} \
400 - ${dissociate:+"--dissociate"} \
390 + $dissociate \
391 ${depth:+"$depth"} \
402 - ${require_init:+--require-init} \
403 - ${dissociate:+"--dissociate"} \
392 + $require_init \
393 $single_branch \
394 $recommend_shallow \
395 $jobs \
@@ -415,9 +404,6 @@ cmd_update()
404 # $@ = requested path
405 #
406 cmd_set_branch() {
418 - default=
419 - branch=
420 -
407 # parse $args after "submodule ... set-branch".
408 while test $# -ne 0
409 do
@@ -426,18 +412,15 @@ cmd_set_branch() {
412 # we don't do anything with this but we need to accept it
413 ;;
414 -d|--default)
429 - default=1
415 + default=$1
416 ;;
417 -b|--branch)
418 case "$2" in '') usage ;; esac
433 - branch=$2
419 + branch="--branch=$2"
420 shift
421 ;;
436 - -b*)
437 - branch="${1#-b}"
438 - ;;
439 - --branch=*)
440 - branch="${1#--branch=}"
422 + -b*|--branch=*)
423 + branch="$1"
424 ;;
425 --)
426 shift
@@ -454,9 +437,9 @@ cmd_set_branch() {
437 done
438
439 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-branch \
457 - ${quiet:+--quiet} \
458 - ${branch:+--branch "$branch"} \
459 - ${default:+--default} \
440 + $quiet \
441 + ${branch:+"$branch"} \
442 + $default \
443 -- \
444 "$@"
445 }
@@ -472,7 +455,7 @@ cmd_set_url() {
455 do
456 case "$1" in
457 -q|--quiet)
475 - quiet=1
458 + quiet=$1
459 ;;
460 --)
461 shift
@@ -489,7 +472,7 @@ cmd_set_url() {
472 done
473
474 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-url \
492 - ${quiet:+--quiet} \
475 + $quiet \
476 -- \
477 "$@"
478 }
@@ -503,32 +486,26 @@ cmd_set_url() {
486 # $@ = [commit (default 'HEAD'),] requested paths (default all)
487 #
488 cmd_summary() {
506 - summary_limit=-1
507 - for_status=
508 -
489 # parse $args after "submodule ... summary".
490 while test $# -ne 0
491 do
492 case "$1" in
493 --cached)
514 - cached=1
494 + cached=$1
495 ;;
496 --files)
517 - files="$1"
497 + files=$1
498 ;;
499 --for-status)
520 - for_status="$1"
500 + for_status=$1
501 ;;
502 -n|--summary-limit)
503 case "$2" in '') usage ;; esac
524 - summary_limit="$2"
504 + summary_limit="--summary-limit=$2"
505 shift
506 ;;
527 - -n*)
528 - summary_limit="${1#-n}"
529 - ;;
530 - --summary-limit=*)
531 - summary_limit="${1#--summary-limit=}"
507 + -n*|--summary-limit=*)
508 + summary_limit="$1"
509 ;;
510 --)
511 shift
@@ -545,10 +522,10 @@ cmd_summary() {
522 done
523
524 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper summary \
548 - ${files:+--files} \
549 - ${cached:+--cached} \
550 - ${for_status:+--for-status} \
551 - ${summary_limit:+-n "$summary_limit"} \
525 + $files \
526 + $cached \
527 + $for_status \
528 + ${summary_limit:+"$summary_limit"} \
529 -- \
530 "$@"
531 }
@@ -569,13 +546,13 @@ cmd_status()
546 do
547 case "$1" in
548 -q|--quiet)
572 - quiet=1
549 + quiet=$1
550 ;;
551 --cached)
575 - cached=1
552 + cached=$1
553 ;;
554 --recursive)
578 - recursive=1
555 + recursive=$1
556 ;;
557 --)
558 shift
@@ -592,9 +569,9 @@ cmd_status()
569 done
570
571 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper status \
595 - ${quiet:+--quiet} \
596 - ${cached:+--cached} \
597 - ${recursive:+--recursive} \
572 + $quiet \
573 + $cached \
574 + $recursive \
575 -- \
576 "$@"
577 }
@@ -611,11 +588,11 @@ cmd_sync()
588 do
589 case "$1" in
590 -q|--quiet)
614 - quiet=1
591 + quiet=$1
592 shift
593 ;;
594 --recursive)
618 - recursive=1
595 + recursive=$1
596 shift
597 ;;
598 --)
@@ -632,8 +609,8 @@ cmd_sync()
609 done
610
611 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper sync \
635 - ${quiet:+--quiet} \
636 - ${recursive:+--recursive} \
612 + $quiet \
613 + $recursive \
614 -- \
615 "$@"
616 }
@@ -656,10 +633,10 @@ do
633 command=$1
634 ;;
635 -q|--quiet)
659 - quiet=1
636 + quiet=$1
637 ;;
638 --cached)
662 - cached=1
639 + cached=$1
640 ;;
641 --)
642 break