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

[PATCH v4 2/9] stash: extract stash-like check into its own function

From
Jon Seymour <jon.seymour@gmail.com>
Date
Aug 10, 2010, 00:04 UTC
Message-ID
<1281398688-21008-3-git-send-email-jon.seymour@gmail.com>
In-Reply-To
<1281242771-24764-1-git-send-email-jon.seymour@gmail.com>

A similar code block is used in three places; this change factors the stash-like check into a separate function so that it can be used in these and other places.

Note to reviewers: The existing code in apply_stash appears to assume w_tree, b_tree and i_tree are all trees as opposed to tree-ish (which is what they actually are).

This refactoring does not change the existing derivation, but one wonders whether this difference might be subverting the intent of the test which compares c_tree with i_tree.

Signed-off-by: Jon Seymour <jon.seymour@gmail.com>
---
 git-stash.sh |   46 ++++++++++++++++++++++++++++++----------------
 1 files changed, 30 insertions(+), 16 deletions(-)
diff --git a/git-stash.sh b/git-stash.sh
index 31ea333..dbb7944 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -218,13 +218,30 @@ show_stash () {
 		flags=--stat
 	fi
 
-	w_commit=$(git rev-parse --quiet --verify --default $ref_stash "$@") &&
-	b_commit=$(git rev-parse --quiet --verify "$w_commit^") ||
-		die "'$*' is not a stash"
+	assert_stash_like "$@"
 
 	git diff $flags $b_commit $w_commit
 }
 
+#
+# if this function returns, then:
+#   s is set to the stash commit
+#   w_commit is set to the commit containing the working tree
+#   b_commit is set to the base commit
+#   i_commit is set to the commit containing the index tree
+# otherwise:
+#   the function exits via die
+#
+assert_stash_like() {
+	# stash records the work tree, and is a merge between the
+	# base commit (first parent) and the index tree (second parent).
+	s=$(git rev-parse --revs-only --quiet --verify --default $ref_stash "$@") &&
+	w_commit=$(git rev-parse --quiet --verify "$s:") &&
+	b_commit=$(git rev-parse --quiet --verify "$s^1:") &&
+	i_commit=$(git rev-parse --quiet --verify "$s^2:") ||
+		die "$*: no valid stashed state found"
+}
+
 apply_stash () {
 	applied_stash=
 	unstash_index=
@@ -253,13 +270,11 @@ apply_stash () {
 		applied_stash="$*"
 	fi
 
-	# stash records the work tree, and is a merge between the
-	# base commit (first parent) and the index tree (second parent).
-	s=$(git rev-parse --quiet --verify --default $ref_stash "$@") &&
-	w_tree=$(git rev-parse --quiet --verify "$s:") &&
-	b_tree=$(git rev-parse --quiet --verify "$s^1:") &&
-	i_tree=$(git rev-parse --quiet --verify "$s^2:") ||
-		die "$*: no valid stashed state found"
+	assert_stash_like "$@"
+
+	b_tree=$b_commit
+	w_tree=$w_commit
+	i_tree=$i_commit
 
 	git update-index -q --refresh &&
 	git diff-files --quiet --ignore-submodules ||
@@ -270,6 +285,9 @@ apply_stash () {
 		die 'Cannot apply a stash in the middle of a merge'
 
 	unstashed_index_tree=
+	#
+	# note to reviewers: comparing $c_tree to $i_tree seems unsound, since i_tree is only tree-ish
+	#
 	if test -n "$unstash_index" && test "$b_tree" != "$i_tree" &&
 			test "$c_tree" != "$i_tree"
 	then
@@ -351,12 +369,8 @@ drop_stash () {
 		set x "$ref_stash@{0}"
 		shift
 	fi
-	# Verify supplied argument looks like a stash entry
-	s=$(git rev-parse --verify "$@") &&
-	git rev-parse --verify "$s:"   > /dev/null 2>&1 &&
-	git rev-parse --verify "$s^1:" > /dev/null 2>&1 &&
-	git rev-parse --verify "$s^2:" > /dev/null 2>&1 ||
-		die "$*: not a valid stashed state"
+
+	assert_stash_like "$@"
 
 	git reflog delete --updateref --rewrite "$@" &&
 		say "Dropped $* ($s)" || die "$*: Could not drop stash entry"
-- 
1.6.5.rc3.8.g8ba5e
Previous: Jon SeymourNext: Junio C Hamano
Message 16 of 30 in “stash: modifiy handling of stash-like references”
  1. 0/9 stash: modifiy handling of stash-like referencesJon Seymour, Aug 8, 2010
  2. 1/9 stash: refactor - create pop_stash functionJon Seymour, Aug 8, 2010
  3. 2/9 stash: extract stash-like check into its own functionJon Seymour, Aug 8, 2010
  4. 3/9 stash: introduce is_stash_ref and assert_stash_ref functions.Jon Seymour, Aug 8, 2010
  5. 4/9 stash: teach git stash pop to fail early if the argument is not a stash refJon Seymour, Aug 8, 2010
  6. 5/9 stash: teach git stash drop to fail early if the specified revision is not a stash referenceJon Seymour, Aug 8, 2010
  7. 6/9 stash: teach git stash branch to tolerate stash-like argumentsJon Seymour, Aug 8, 2010
  8. 7/9 stash: teach git stash show to always tolerate stash-like arguments.Jon Seymour, Aug 8, 2010
  9. 8/9 t3903-stash.sh: tests of git stash with stash-like argumentsJon Seymour, Aug 8, 2010
  10. Johannes SixtAug 8, 2010
  11. Jon SeymourAug 8, 2010
  12. 9/9 Documentation: git stash branch now tolerates non-stash references.Jon Seymour, Aug 8, 2010
  13. Jon SeymourAug 8, 2010
  14. 0/9 stash: modifiy handling of stash-like referencesJon Seymour, Aug 10, 2010
  15. 1/9 stash: refactor - create pop_stash functionJon Seymour, Aug 10, 2010
  16. 2/9 stash: extract stash-like check into its own functionJon Seymour, Aug 10, 2010
  17. Junio C HamanoAug 13, 2010
  18. Jon SeymourAug 13, 2010
  19. 3/9 stash: introduce is_stash_ref and assert_stash_ref functions.Jon Seymour, Aug 10, 2010
  20. Junio C HamanoAug 13, 2010
  21. Jon SeymourAug 14, 2010
  22. 4/9 stash: teach git stash pop to fail early if the argument is not a stash refJon Seymour, Aug 10, 2010
  23. 5/9 stash: teach git stash drop to fail early if the specified revision is not a stash referenceJon Seymour, Aug 10, 2010
  24. 6/9 stash: teach git stash branch to tolerate stash-like argumentsJon Seymour, Aug 10, 2010
  25. 7/9 stash: teach git stash show to always tolerate stash-like arguments.Jon Seymour, Aug 10, 2010
  26. Junio C HamanoAug 13, 2010
  27. 8/9 t3903-stash.sh: tests of git stash with stash-like argumentsJon Seymour, Aug 10, 2010
  28. 9/9 Documentation: git stash branch now tolerates non-stash references.Jon Seymour, Aug 10, 2010
  29. Junio C HamanoAug 13, 2010
  30. Jon SeymourAug 14, 2010

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.