Skip to content

Commit 5cdae0f

Browse files
LukeShugitster
authored andcommitted
subtree: add comments and sanity checks
For each function in subtree, add a usage comment saying what the arguments are, and add an `assert` checking the number of arguments. In figuring out each thing's arguments in order to write those comments and assertions, it turns out that find_existing_splits is written as if it takes multiple 'revs', but it is in fact only ever passed a single 'rev': unrevs="$(find_existing_splits "$dir" "$rev")" || exit $? So go ahead and codify that by documenting and asserting that it takes exactly two arguments, one dir and one rev. Signed-off-by: Luke Shumaker <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cbb5de8 commit 5cdae0f

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

contrib/subtree/git-subtree.sh

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,23 @@ arg_split_annotate=
5555
arg_addmerge_squash=
5656
arg_addmerge_message=
5757

58+
# Usage: debug [MSG...]
5859
debug () {
5960
if test -n "$arg_debug"
6061
then
6162
printf "%s\n" "$*" >&2
6263
fi
6364
}
6465

66+
# Usage: progress [MSG...]
6567
progress () {
6668
if test -z "$GIT_QUIET"
6769
then
6870
printf "%s\r" "$*" >&2
6971
fi
7072
}
7173

74+
# Usage: assert CMD...
7275
assert () {
7376
if ! "$@"
7477
then
@@ -192,7 +195,9 @@ main () {
192195
"cmd_$arg_command" "$@"
193196
}
194197

198+
# Usage: cache_setup
195199
cache_setup () {
200+
assert test $# = 0
196201
cachedir="$GIT_DIR/subtree-cache/$$"
197202
rm -rf "$cachedir" ||
198203
die "Can't delete old cachedir: $cachedir"
@@ -203,6 +208,7 @@ cache_setup () {
203208
debug "Using cachedir: $cachedir" >&2
204209
}
205210

211+
# Usage: cache_get [REVS...]
206212
cache_get () {
207213
for oldrev in "$@"
208214
do
@@ -214,6 +220,7 @@ cache_get () {
214220
done
215221
}
216222

223+
# Usage: cache_miss [REVS...]
217224
cache_miss () {
218225
for oldrev in "$@"
219226
do
@@ -224,7 +231,9 @@ cache_miss () {
224231
done
225232
}
226233

234+
# Usage: check_parents PARENTS_EXPR INDENT
227235
check_parents () {
236+
assert test $# = 2
228237
missed=$(cache_miss "$1") || exit $?
229238
local indent=$(($2 + 1))
230239
for miss in $missed
@@ -237,11 +246,15 @@ check_parents () {
237246
done
238247
}
239248

249+
# Usage: set_notree REV
240250
set_notree () {
251+
assert test $# = 1
241252
echo "1" > "$cachedir/notree/$1"
242253
}
243254

255+
# Usage: cache_set OLDREV NEWREV
244256
cache_set () {
257+
assert test $# = 2
245258
oldrev="$1"
246259
newrev="$2"
247260
if test "$oldrev" != "latest_old" &&
@@ -253,7 +266,9 @@ cache_set () {
253266
echo "$newrev" >"$cachedir/$oldrev"
254267
}
255268

269+
# Usage: rev_exists REV
256270
rev_exists () {
271+
assert test $# = 1
257272
if git rev-parse "$1" >/dev/null 2>&1
258273
then
259274
return 0
@@ -262,17 +277,22 @@ rev_exists () {
262277
fi
263278
}
264279

265-
# if a commit doesn't have a parent, this might not work. But we only want
280+
# Usage: try_remove_previous REV
281+
#
282+
# If a commit doesn't have a parent, this might not work. But we only want
266283
# to remove the parent from the rev-list, and since it doesn't exist, it won't
267284
# be there anyway, so do nothing in that case.
268285
try_remove_previous () {
286+
assert test $# = 1
269287
if rev_exists "$1^"
270288
then
271289
echo "^$1^"
272290
fi
273291
}
274292

293+
# Usage: find_latest_squash DIR
275294
find_latest_squash () {
295+
assert test $# = 1
276296
debug "Looking for latest squash ($dir)..."
277297
dir="$1"
278298
sq=
@@ -316,10 +336,12 @@ find_latest_squash () {
316336
done || exit $?
317337
}
318338

339+
# Usage: find_existing_splits DIR REV
319340
find_existing_splits () {
341+
assert test $# = 2
320342
debug "Looking for prior splits..."
321343
dir="$1"
322-
revs="$2"
344+
rev="$2"
323345
main=
324346
sub=
325347
local grep_format="^git-subtree-dir: $dir/*\$"
@@ -328,7 +350,7 @@ find_existing_splits () {
328350
grep_format="^Add '$dir/' from commit '"
329351
fi
330352
git log --grep="$grep_format" \
331-
--no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
353+
--no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' "$rev" |
332354
while read a b junk
333355
do
334356
case "$a" in
@@ -365,7 +387,9 @@ find_existing_splits () {
365387
done || exit $?
366388
}
367389

390+
# Usage: copy_commit REV TREE FLAGS_STR
368391
copy_commit () {
392+
assert test $# = 3
369393
# We're going to set some environment vars here, so
370394
# do it in a subshell to get rid of them safely later
371395
debug copy_commit "{$1}" "{$2}" "{$3}"
@@ -391,7 +415,9 @@ copy_commit () {
391415
) || die "Can't copy commit $1"
392416
}
393417

418+
# Usage: add_msg DIR LATEST_OLD LATEST_NEW
394419
add_msg () {
420+
assert test $# = 3
395421
dir="$1"
396422
latest_old="$2"
397423
latest_new="$3"
@@ -410,7 +436,9 @@ add_msg () {
410436
EOF
411437
}
412438

439+
# Usage: add_squashed_msg REV DIR
413440
add_squashed_msg () {
441+
assert test $# = 2
414442
if test -n "$arg_addmerge_message"
415443
then
416444
echo "$arg_addmerge_message"
@@ -419,7 +447,9 @@ add_squashed_msg () {
419447
fi
420448
}
421449

450+
# Usage: rejoin_msg DIR LATEST_OLD LATEST_NEW
422451
rejoin_msg () {
452+
assert test $# = 3
423453
dir="$1"
424454
latest_old="$2"
425455
latest_new="$3"
@@ -438,7 +468,9 @@ rejoin_msg () {
438468
EOF
439469
}
440470

471+
# Usage: squash_msg DIR OLD_SUBTREE_COMMIT NEW_SUBTREE_COMMIT
441472
squash_msg () {
473+
assert test $# = 3
442474
dir="$1"
443475
oldsub="$2"
444476
newsub="$3"
@@ -460,12 +492,16 @@ squash_msg () {
460492
echo "git-subtree-split: $newsub"
461493
}
462494

495+
# Usage: toptree_for_commit COMMIT
463496
toptree_for_commit () {
497+
assert test $# = 1
464498
commit="$1"
465499
git rev-parse --verify "$commit^{tree}" || exit $?
466500
}
467501

502+
# Usage: subtree_for_commit COMMIT DIR
468503
subtree_for_commit () {
504+
assert test $# = 2
469505
commit="$1"
470506
dir="$2"
471507
git ls-tree "$commit" -- "$dir" |
@@ -479,7 +515,9 @@ subtree_for_commit () {
479515
done || exit $?
480516
}
481517

518+
# Usage: tree_changed TREE [PARENTS...]
482519
tree_changed () {
520+
assert test $# -gt 0
483521
tree=$1
484522
shift
485523
if test $# -ne 1
@@ -496,7 +534,9 @@ tree_changed () {
496534
fi
497535
}
498536

537+
# Usage: new_squash_commit OLD_SQUASHED_COMMIT OLD_NONSQUASHED_COMMIT NEW_NONSQUASHED_COMMIT
499538
new_squash_commit () {
539+
assert test $# = 3
500540
old="$1"
501541
oldsub="$2"
502542
newsub="$3"
@@ -511,7 +551,9 @@ new_squash_commit () {
511551
fi
512552
}
513553

554+
# Usage: copy_or_skip REV TREE NEWPARENTS
514555
copy_or_skip () {
556+
assert test $# = 3
515557
rev="$1"
516558
tree="$2"
517559
newparents="$3"
@@ -586,7 +628,9 @@ copy_or_skip () {
586628
fi
587629
}
588630

631+
# Usage: ensure_clean
589632
ensure_clean () {
633+
assert test $# = 0
590634
if ! git diff-index HEAD --exit-code --quiet 2>&1
591635
then
592636
die "Working tree has modifications. Cannot add."
@@ -597,12 +641,16 @@ ensure_clean () {
597641
fi
598642
}
599643

644+
# Usage: ensure_valid_ref_format REF
600645
ensure_valid_ref_format () {
646+
assert test $# = 1
601647
git check-ref-format "refs/heads/$1" ||
602648
die "'$1' does not look like a ref"
603649
}
604650

651+
# Usage: process_split_commit REV PARENTS INDENT
605652
process_split_commit () {
653+
assert test $# = 3
606654
local rev="$1"
607655
local parents="$2"
608656
local indent=$3
@@ -654,6 +702,8 @@ process_split_commit () {
654702
cache_set latest_old "$rev"
655703
}
656704

705+
# Usage: cmd_add REV
706+
# Or: cmd_add REPOSITORY REF
657707
cmd_add () {
658708

659709
ensure_clean
@@ -681,17 +731,21 @@ cmd_add () {
681731
fi
682732
}
683733

734+
# Usage: cmd_add_repository REPOSITORY REFSPEC
684735
cmd_add_repository () {
736+
assert test $# = 2
685737
echo "git fetch" "$@"
686738
repository=$1
687739
refspec=$2
688740
git fetch "$@" || exit $?
689741
cmd_add_commit FETCH_HEAD
690742
}
691743

744+
# Usage: cmd_add_commit REV
692745
cmd_add_commit () {
693746
# The rev has already been validated by cmd_add(), we just
694747
# need to normalize it.
748+
assert test $# = 1
695749
rev=$(git rev-parse --verify "$1^{commit}") || exit $?
696750

697751
debug "Adding $dir as '$rev'..."
@@ -722,6 +776,7 @@ cmd_add_commit () {
722776
say >&2 "Added dir '$dir'"
723777
}
724778

779+
# Usage: cmd_split [REV]
725780
cmd_split () {
726781
if test $# -eq 0
727782
then
@@ -801,6 +856,7 @@ cmd_split () {
801856
exit 0
802857
}
803858

859+
# Usage: cmd_merge REV
804860
cmd_merge () {
805861
test $# -eq 1 ||
806862
die "You must provide exactly one revision. Got: '$*'"
@@ -837,6 +893,7 @@ cmd_merge () {
837893
fi
838894
}
839895

896+
# Usage: cmd_pull REPOSITORY REMOTEREF
840897
cmd_pull () {
841898
if test $# -ne 2
842899
then
@@ -848,6 +905,7 @@ cmd_pull () {
848905
cmd_merge FETCH_HEAD
849906
}
850907

908+
# Usage: cmd_push REPOSITORY REMOTEREF
851909
cmd_push () {
852910
if test $# -ne 2
853911
then

0 commit comments

Comments
 (0)