git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 23/30] subtree: add comments and sanity checks

From
Luke Shumaker <lukeshu@lukeshu.com>
Date
Apr 26, 2021, 17:45 UTC
Message-ID
<20210426174525.3937858-24-lukeshu@lukeshu.com>
In-Reply-To
<20210426174525.3937858-1-lukeshu@lukeshu.com>
From: Luke Shumaker <lukeshu@datawire.io>

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 <lukeshu@datawire.io>
---
v2:
 - Expand on the the commit message.
 - Fix capitalization in one of the comments.
 contrib/subtree/git-subtree.sh | 64 ++++++++++++++++++++++++++++++++--
 1 file changed, 61 insertions(+), 3 deletions(-)
diff --git a/contrib/subtree/git-subtree.sh b/contrib/subtree/git-subtree.sh
index 7fbd8481ed..441571c85a 100755
--- a/contrib/subtree/git-subtree.sh
+++ b/contrib/subtree/git-subtree.sh
@@ -55,6 +55,7 @@ arg_split_annotate=
 arg_addmerge_squash=
 arg_addmerge_message=
 
+# Usage: debug [MSG...]
 debug () {
 	if test -n "$arg_debug"
 	then
@@ -62,6 +63,7 @@ debug () {
 	fi
 }
 
+# Usage: progress [MSG...]
 progress () {
 	if test -z "$GIT_QUIET"
 	then
@@ -69,6 +71,7 @@ progress () {
 	fi
 }
 
+# Usage: assert CMD...
 assert () {
 	if ! "$@"
 	then
@@ -192,7 +195,9 @@ main () {
 	"cmd_$arg_command" "$@"
 }
 
+# Usage: cache_setup
 cache_setup () {
+	assert test $# = 0
 	cachedir="$GIT_DIR/subtree-cache/$$"
 	rm -rf "$cachedir" ||
 		die "Can't delete old cachedir: $cachedir"
@@ -203,6 +208,7 @@ cache_setup () {
 	debug "Using cachedir: $cachedir" >&2
 }
 
+# Usage: cache_get [REVS...]
 cache_get () {
 	for oldrev in "$@"
 	do
@@ -214,6 +220,7 @@ cache_get () {
 	done
 }
 
+# Usage: cache_miss [REVS...]
 cache_miss () {
 	for oldrev in "$@"
 	do
@@ -224,7 +231,9 @@ cache_miss () {
 	done
 }
 
+# Usage: check_parents PARENTS_EXPR INDENT
 check_parents () {
+	assert test $# = 2
 	missed=$(cache_miss "$1") || exit $?
 	local indent=$(($2 + 1))
 	for miss in $missed
@@ -237,11 +246,15 @@ check_parents () {
 	done
 }
 
+# Usage: set_notree REV
 set_notree () {
+	assert test $# = 1
 	echo "1" > "$cachedir/notree/$1"
 }
 
+# Usage: cache_set OLDREV NEWREV
 cache_set () {
+	assert test $# = 2
 	oldrev="$1"
 	newrev="$2"
 	if test "$oldrev" != "latest_old" &&
@@ -253,7 +266,9 @@ cache_set () {
 	echo "$newrev" >"$cachedir/$oldrev"
 }
 
+# Usage: rev_exists REV
 rev_exists () {
+	assert test $# = 1
 	if git rev-parse "$1" >/dev/null 2>&1
 	then
 		return 0
@@ -262,17 +277,22 @@ rev_exists () {
 	fi
 }
 
-# if a commit doesn't have a parent, this might not work.  But we only want
+# Usage: try_remove_previous REV
+#
+# If a commit doesn't have a parent, this might not work.  But we only want
 # to remove the parent from the rev-list, and since it doesn't exist, it won't
 # be there anyway, so do nothing in that case.
 try_remove_previous () {
+	assert test $# = 1
 	if rev_exists "$1^"
 	then
 		echo "^$1^"
 	fi
 }
 
+# Usage: find_latest_squash DIR
 find_latest_squash () {
+	assert test $# = 1
 	debug "Looking for latest squash ($dir)..."
 	dir="$1"
 	sq=
@@ -316,10 +336,12 @@ find_latest_squash () {
 	done || exit $?
 }
 
+# Usage: find_existing_splits DIR REV
 find_existing_splits () {
+	assert test $# = 2
 	debug "Looking for prior splits..."
 	dir="$1"
-	revs="$2"
+	rev="$2"
 	main=
 	sub=
 	local grep_format="^git-subtree-dir: $dir/*\$"
@@ -328,7 +350,7 @@ find_existing_splits () {
 		grep_format="^Add '$dir/' from commit '"
 	fi
 	git log --grep="$grep_format" \
-		--no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' $revs |
+		--no-show-signature --pretty=format:'START %H%n%s%n%n%b%nEND%n' "$rev" |
 	while read a b junk
 	do
 		case "$a" in
@@ -365,7 +387,9 @@ find_existing_splits () {
 	done || exit $?
 }
 
+# Usage: copy_commit REV TREE FLAGS_STR
 copy_commit () {
+	assert test $# = 3
 	# We're going to set some environment vars here, so
 	# do it in a subshell to get rid of them safely later
 	debug copy_commit "{$1}" "{$2}" "{$3}"
@@ -391,7 +415,9 @@ copy_commit () {
 	) || die "Can't copy commit $1"
 }
 
+# Usage: add_msg DIR LATEST_OLD LATEST_NEW
 add_msg () {
+	assert test $# = 3
 	dir="$1"
 	latest_old="$2"
 	latest_new="$3"
@@ -410,7 +436,9 @@ add_msg () {
 	EOF
 }
 
+# Usage: add_squashed_msg REV DIR
 add_squashed_msg () {
+	assert test $# = 2
 	if test -n "$arg_addmerge_message"
 	then
 		echo "$arg_addmerge_message"
@@ -419,7 +447,9 @@ add_squashed_msg () {
 	fi
 }
 
+# Usage: rejoin_msg DIR LATEST_OLD LATEST_NEW
 rejoin_msg () {
+	assert test $# = 3
 	dir="$1"
 	latest_old="$2"
 	latest_new="$3"
@@ -438,7 +468,9 @@ rejoin_msg () {
 	EOF
 }
 
+# Usage: squash_msg DIR OLD_SUBTREE_COMMIT NEW_SUBTREE_COMMIT
 squash_msg () {
+	assert test $# = 3
 	dir="$1"
 	oldsub="$2"
 	newsub="$3"
@@ -460,12 +492,16 @@ squash_msg () {
 	echo "git-subtree-split: $newsub"
 }
 
+# Usage: toptree_for_commit COMMIT
 toptree_for_commit () {
+	assert test $# = 1
 	commit="$1"
 	git rev-parse --verify "$commit^{tree}" || exit $?
 }
 
+# Usage: subtree_for_commit COMMIT DIR
 subtree_for_commit () {
+	assert test $# = 2
 	commit="$1"
 	dir="$2"
 	git ls-tree "$commit" -- "$dir" |
@@ -479,7 +515,9 @@ subtree_for_commit () {
 	done || exit $?
 }
 
+# Usage: tree_changed TREE [PARENTS...]
 tree_changed () {
+	assert test $# -gt 0
 	tree=$1
 	shift
 	if test $# -ne 1
@@ -496,7 +534,9 @@ tree_changed () {
 	fi
 }
 
+# Usage: new_squash_commit OLD_SQUASHED_COMMIT OLD_NONSQUASHED_COMMIT NEW_NONSQUASHED_COMMIT
 new_squash_commit () {
+	assert test $# = 3
 	old="$1"
 	oldsub="$2"
 	newsub="$3"
@@ -511,7 +551,9 @@ new_squash_commit () {
 	fi
 }
 
+# Usage: copy_or_skip REV TREE NEWPARENTS
 copy_or_skip () {
+	assert test $# = 3
 	rev="$1"
 	tree="$2"
 	newparents="$3"
@@ -586,7 +628,9 @@ copy_or_skip () {
 	fi
 }
 
+# Usage: ensure_clean
 ensure_clean () {
+	assert test $# = 0
 	if ! git diff-index HEAD --exit-code --quiet 2>&1
 	then
 		die "Working tree has modifications.  Cannot add."
@@ -597,12 +641,16 @@ ensure_clean () {
 	fi
 }
 
+# Usage: ensure_valid_ref_format REF
 ensure_valid_ref_format () {
+	assert test $# = 1
 	git check-ref-format "refs/heads/$1" ||
 		die "'$1' does not look like a ref"
 }
 
+# Usage: process_split_commit REV PARENTS INDENT
 process_split_commit () {
+	assert test $# = 3
 	local rev="$1"
 	local parents="$2"
 	local indent=$3
@@ -654,6 +702,8 @@ process_split_commit () {
 	cache_set latest_old "$rev"
 }
 
+# Usage: cmd_add REV
+#    Or: cmd_add REPOSITORY REF
 cmd_add () {
 
 	ensure_clean
@@ -681,7 +731,9 @@ cmd_add () {
 	fi
 }
 
+# Usage: cmd_add_repository REPOSITORY REFSPEC
 cmd_add_repository () {
+	assert test $# = 2
 	echo "git fetch" "$@"
 	repository=$1
 	refspec=$2
@@ -689,9 +741,11 @@ cmd_add_repository () {
 	cmd_add_commit FETCH_HEAD
 }
 
+# Usage: cmd_add_commit REV
 cmd_add_commit () {
 	# The rev has already been validated by cmd_add(), we just
 	# need to normalize it.
+	assert test $# = 1
 	rev=$(git rev-parse --verify "$1^{commit}") || exit $?
 
 	debug "Adding $dir as '$rev'..."
@@ -722,6 +776,7 @@ cmd_add_commit () {
 	say >&2 "Added dir '$dir'"
 }
 
+# Usage: cmd_split [REV]
 cmd_split () {
 	if test $# -eq 0
 	then
@@ -801,6 +856,7 @@ cmd_split () {
 	exit 0
 }
 
+# Usage: cmd_merge REV
 cmd_merge () {
 	test $# -eq 1 ||
 		die "You must provide exactly one revision.  Got: '$*'"
@@ -837,6 +893,7 @@ cmd_merge () {
 	fi
 }
 
+# Usage: cmd_pull REPOSITORY REMOTEREF
 cmd_pull () {
 	if test $# -ne 2
 	then
@@ -848,6 +905,7 @@ cmd_pull () {
 	cmd_merge FETCH_HEAD
 }
 
+# Usage: cmd_push REPOSITORY REMOTEREF
 cmd_push () {
 	if test $# -ne 2
 	then
-- 
2.31.1
Previous: Luke ShumakerNext: Luke Shumaker
Message 93 of 144 in “subtree: clean up, improve UX”
  1. 00/30 subtree: clean up, improve UXLuke Shumaker, Apr 23, 2021
  2. 01/30 .gitignore: Ignore /git-subtreeLuke Shumaker, Apr 23, 2021
  3. 02/30 subtree: t7900: update for having the default branch name be 'main'Luke Shumaker, Apr 23, 2021
  4. 04/30 subtree: t7900: use consistent formattingLuke Shumaker, Apr 23, 2021
  5. Eric SunshineApr 23, 2021
  6. Luke ShumakerApr 23, 2021
  7. Junio C HamanoApr 27, 2021
  8. Luke ShumakerApr 27, 2021
  9. Junio C HamanoApr 28, 2021
  10. 05/30 subtree: t7900: comment subtree_test_create_repoLuke Shumaker, Apr 23, 2021
  11. 03/30 subtree: t7900: use test-lib.sh's test_countLuke Shumaker, Apr 23, 2021
  12. 06/30 subtree: t7900: use 'test' for string equalityLuke Shumaker, Apr 23, 2021
  13. 07/30 subtree: t7900: delete some dead codeLuke Shumaker, Apr 23, 2021
  14. 08/30 subtree: t7900: fix 'verify one file change per commit'Luke Shumaker, Apr 23, 2021
  15. 09/30 subtree: t7900: rename last_commit_message to last_commit_subjectLuke Shumaker, Apr 23, 2021
  16. 10/30 subtree: t7900: add a test for the -h flagLuke Shumaker, Apr 23, 2021
  17. 11/30 subtree: t7900: add porcelain tests for 'pull' and 'push'Luke Shumaker, Apr 23, 2021
  18. Eric SunshineApr 23, 2021
  19. Luke ShumakerApr 23, 2021
  20. 12/30 subtree: don't have loose code outside of a functionLuke Shumaker, Apr 23, 2021
  21. Luke ShumakerApr 23, 2021
  22. Eric SunshineApr 23, 2021
  23. Luke ShumakerApr 23, 2021
  24. Eric SunshineApr 23, 2021
  25. Luke ShumakerApr 23, 2021
  26. 14/30 subtree: drop support for git < 1.7Luke Shumaker, Apr 23, 2021
  27. Luke ShumakerApr 23, 2021
  28. Eric SunshineApr 23, 2021
  29. Luke ShumakerApr 23, 2021
  30. Eric SunshineApr 23, 2021
  31. Luke ShumakerApr 24, 2021
  32. 13/30 subtree: more consistent error propagationLuke Shumaker, Apr 23, 2021
  33. 15/30 subtree: use `git merge-base --is-ancestor`Luke Shumaker, Apr 23, 2021
  34. 16/30 subtree: use git-sh-setup's `say`Luke Shumaker, Apr 23, 2021
  35. 17/30 subtree: use more explicit variable names for cmdline argsLuke Shumaker, Apr 23, 2021
  36. 18/30 subtree: use $* instead of $@ as appropriateLuke Shumaker, Apr 23, 2021
  37. Eric SunshineApr 23, 2021
  38. Luke ShumakerApr 23, 2021
  39. Eric SunshineApr 24, 2021
  40. 19/30 subtree: give `$(git --exec-path)` precedence over `$PATH`Luke Shumaker, Apr 23, 2021
  41. =?utf-8?B?w4Z2YXIgQXJuZmrDtnLDsA==?= BjarmasonApr 26, 2021
  42. 20/30 subtree: use "^{commit}" instead of "^0"Luke Shumaker, Apr 23, 2021
  43. Ævar Arnfjörð BjarmasonApr 26, 2021
  44. 21/30 subtree: parse revs in individual cmd_ functionsLuke Shumaker, Apr 23, 2021
  45. 22/30 subtree: remove duplicate checkLuke Shumaker, Apr 23, 2021
  46. 23/30 subtree: add comments and sanity checksLuke Shumaker, Apr 23, 2021
  47. Eric SunshineApr 23, 2021
  48. Luke ShumakerApr 23, 2021
  49. 24/30 subtree: don't let debug and progress output clashLuke Shumaker, Apr 23, 2021
  50. Eric SunshineApr 23, 2021
  51. Luke ShumakerApr 24, 2021
  52. Eric SunshineApr 24, 2021
  53. 25/30 subtree: have $indent actually affect indentationLuke Shumaker, Apr 23, 2021
  54. 26/30 subtree: give the docs a once-overLuke Shumaker, Apr 23, 2021
  55. 27/30 subtree: allow --squash to be used with --rejoinLuke Shumaker, Apr 23, 2021
  56. Eric SunshineApr 24, 2021
  57. Luke ShumakerApr 25, 2021
  58. 28/30 subtree: allow 'split' flags to be passed to 'push'Luke Shumaker, Apr 23, 2021
  59. 29/30 subtree: push: allow specifying a local rev other than HEADLuke Shumaker, Apr 23, 2021
  60. 30/30 subtree: be stricter about validating flagsLuke Shumaker, Apr 23, 2021
  61. Danny LinApr 25, 2021
  62. Luke ShumakerApr 26, 2021
  63. Luke ShumakerApr 23, 2021
  64. =?utf-8?B?w4Z2YXIgQXJuZmrDtnLDsA==?= BjarmasonApr 26, 2021
  65. Junio C HamanoApr 27, 2021
  66. 00/30 subtree: clean up, improve UXLuke Shumaker, Apr 26, 2021
  67. 01/30 .gitignore: Ignore /git-subtreeLuke Shumaker, Apr 26, 2021
  68. 02/30 subtree: t7900: update for having the default branch name be 'main'Luke Shumaker, Apr 26, 2021
  69. 04/30 subtree: t7900: use consistent formattingLuke Shumaker, Apr 26, 2021
  70. Luke ShumakerApr 26, 2021
  71. 03/30 subtree: t7900: use test-lib.sh's test_countLuke Shumaker, Apr 26, 2021
  72. 05/30 subtree: t7900: comment subtree_test_create_repoLuke Shumaker, Apr 26, 2021
  73. 06/30 subtree: t7900: use 'test' for string equalityLuke Shumaker, Apr 26, 2021
  74. 07/30 subtree: t7900: delete some dead codeLuke Shumaker, Apr 26, 2021
  75. 08/30 subtree: t7900: fix 'verify one file change per commit'Luke Shumaker, Apr 26, 2021
  76. 09/30 subtree: t7900: rename last_commit_message to last_commit_subjectLuke Shumaker, Apr 26, 2021
  77. 10/30 subtree: t7900: add a test for the -h flagLuke Shumaker, Apr 26, 2021
  78. 11/30 subtree: t7900: add porcelain tests for 'pull' and 'push'Luke Shumaker, Apr 26, 2021
  79. 12/30 subtree: don't have loose code outside of a functionLuke Shumaker, Apr 26, 2021
  80. 13/30 subtree: more consistent error propagationLuke Shumaker, Apr 26, 2021
  81. 16/30 subtree: use git-sh-setup's `say`Luke Shumaker, Apr 26, 2021
  82. 14/30 subtree: drop support for git < 1.7Luke Shumaker, Apr 26, 2021
  83. 17/30 subtree: use more explicit variable names for cmdline argsLuke Shumaker, Apr 26, 2021
  84. 15/30 subtree: use `git merge-base --is-ancestor`Luke Shumaker, Apr 26, 2021
  85. 18/30 subtree: use "$*" instead of "$@" as appropriateLuke Shumaker, Apr 26, 2021
  86. 20/30 subtree: use "^{commit}" instead of "^0"Luke Shumaker, Apr 26, 2021
  87. 21/30 subtree: parse revs in individual cmd_ functionsLuke Shumaker, Apr 26, 2021
  88. 19/30 subtree: Don't fuss with PATHLuke Shumaker, Apr 26, 2021
  89. Luke ShumakerApr 26, 2021
  90. 22/30 subtree: remove duplicate checkLuke Shumaker, Apr 26, 2021
  91. 28/30 subtree: allow 'split' flags to be passed to 'push'Luke Shumaker, Apr 26, 2021
  92. 24/30 subtree: don't let debug and progress output clashLuke Shumaker, Apr 26, 2021
  93. 23/30 subtree: add comments and sanity checksLuke Shumaker, Apr 26, 2021
  94. 30/30 subtree: be stricter about validating flagsLuke Shumaker, Apr 26, 2021
  95. 29/30 subtree: push: allow specifying a local rev other than HEADLuke Shumaker, Apr 26, 2021
  96. 25/30 subtree: have $indent actually affect indentationLuke Shumaker, Apr 26, 2021
  97. 27/30 subtree: allow --squash to be used with --rejoinLuke Shumaker, Apr 26, 2021
  98. Luke ShumakerApr 26, 2021
  99. 26/30 subtree: give the docs a once-overLuke Shumaker, Apr 26, 2021
  100. 00/30 subtree: clean up, improve UXLuke Shumaker, Apr 27, 2021
  101. 01/30 .gitignore: Ignore /git-subtreeLuke Shumaker, Apr 27, 2021
  102. 02/30 subtree: t7900: update for having the default branch name be 'main'Luke Shumaker, Apr 27, 2021
  103. Ævar Arnfjörð BjarmasonApr 30, 2021
  104. Luke ShumakerApr 30, 2021
  105. 03/30 subtree: t7900: use test-lib.sh's test_countLuke Shumaker, Apr 27, 2021
  106. Ævar Arnfjörð BjarmasonApr 30, 2021
  107. Luke ShumakerApr 30, 2021
  108. 04/30 subtree: t7900: use consistent formattingLuke Shumaker, Apr 27, 2021
  109. 05/30 subtree: t7900: comment subtree_test_create_repoLuke Shumaker, Apr 27, 2021
  110. Ævar Arnfjörð BjarmasonApr 30, 2021
  111. Luke ShumakerApr 30, 2021
  112. 06/30 subtree: t7900: use 'test' for string equalityLuke Shumaker, Apr 27, 2021
  113. Ævar Arnfjörð BjarmasonApr 30, 2021
  114. Luke ShumakerApr 30, 2021
  115. 07/30 subtree: t7900: delete some dead codeLuke Shumaker, Apr 27, 2021
  116. 08/30 subtree: t7900: fix 'verify one file change per commit'Luke Shumaker, Apr 27, 2021
  117. 09/30 subtree: t7900: rename last_commit_message to last_commit_subjectLuke Shumaker, Apr 27, 2021
  118. Ævar Arnfjörð BjarmasonApr 30, 2021
  119. 10/30 subtree: t7900: add a test for the -h flagLuke Shumaker, Apr 27, 2021
  120. Ævar Arnfjörð BjarmasonApr 30, 2021
  121. Luke ShumakerApr 30, 2021
  122. Bagas SanjayaApr 30, 2021
  123. Luke ShumakerApr 30, 2021
  124. 11/30 subtree: t7900: add porcelain tests for 'pull' and 'push'Luke Shumaker, Apr 27, 2021
  125. 12/30 subtree: don't have loose code outside of a functionLuke Shumaker, Apr 27, 2021
  126. 14/30 subtree: drop support for git < 1.7Luke Shumaker, Apr 27, 2021
  127. 13/30 subtree: more consistent error propagationLuke Shumaker, Apr 27, 2021
  128. 15/30 subtree: use `git merge-base --is-ancestor`Luke Shumaker, Apr 27, 2021
  129. 16/30 subtree: use git-sh-setup's `say`Luke Shumaker, Apr 27, 2021
  130. 17/30 subtree: use more explicit variable names for cmdline argsLuke Shumaker, Apr 27, 2021
  131. 18/30 subtree: use "$*" instead of "$@" as appropriateLuke Shumaker, Apr 27, 2021
  132. 19/30 subtree: don't fuss with PATHLuke Shumaker, Apr 27, 2021
  133. 20/30 subtree: use "^{commit}" instead of "^0"Luke Shumaker, Apr 27, 2021
  134. 21/30 subtree: parse revs in individual cmd_ functionsLuke Shumaker, Apr 27, 2021
  135. 22/30 subtree: remove duplicate checkLuke Shumaker, Apr 27, 2021
  136. 24/30 subtree: don't let debug and progress output clashLuke Shumaker, Apr 27, 2021
  137. 25/30 subtree: have $indent actually affect indentationLuke Shumaker, Apr 27, 2021
  138. 23/30 subtree: add comments and sanity checksLuke Shumaker, Apr 27, 2021
  139. 26/30 subtree: give the docs a once-overLuke Shumaker, Apr 27, 2021
  140. 28/30 subtree: allow 'split' flags to be passed to 'push'Luke Shumaker, Apr 27, 2021
  141. 29/30 subtree: push: allow specifying a local rev other than HEADLuke Shumaker, Apr 27, 2021
  142. 27/30 subtree: allow --squash to be used with --rejoinLuke Shumaker, Apr 27, 2021
  143. 30/30 subtree: be stricter about validating flagsLuke Shumaker, Apr 27, 2021
  144. Luke ShumakerApr 28, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.