git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 6/8] cogito: Don't ever assume object type in cg-object-id
@ 2005-09-20  2:25 Pavel Roskin
  2005-09-21 10:00 ` Petr Baudis
  0 siblings, 1 reply; 8+ messages in thread
From: Pavel Roskin @ 2005-09-20  2:25 UTC (permalink / raw)
  To: git

Don't ever assume object type in cg-object-id.

If type is unknown in normalize_id(), make it empty so it's checked
later.  Check parent ID that it's a commit.

diff --git a/cg-object-id b/cg-object-id
--- a/cg-object-id
+++ b/cg-object-id
@@ -24,22 +24,22 @@
 USAGE="cg-object-id [-p] [-t] [OBJECT_ID]"
 
 . ${COGITO_LIB}cg-Xlib
 
 
-# Normalize argument.  The normalized SHA1 ID is put to $normid,
-# type is put to $type.
+# Normalize argument.  The normalized SHA1 ID is put to $normid.
+# If type is known, it's put to $type.
 normalize_id()
 {
 	local id="$1"
+	local type=
 
 	if [ "${id:(-1):1}" = "^" ]; then
 		# find first parent
 		normalize_id "${id%^}"
 		normid=$(git-cat-file commit "$normid" | \
 			 awk '/^parent/{print $2; exit};/^$/{exit}') || exit 1
-		type="commit"
 		return
 	fi
 
 	if [ ! "$id" ] || [ "$id" = "this" ] || [ "$id" = "HEAD" ]; then
 		read id < "$_git/HEAD"
@@ -118,31 +118,33 @@ if [ "$show_parent" -a "$show_tree" ]; t
 	exit 1
 fi
 
 id="${ARGS[0]}"
 normalize_id "$id"
+[ "$type" ] || type=$(git-cat-file -t "$normid")
+
+if [ -z "$show_tree" ]; then
+	if [ "$type" != "commit" ]; then
+		echo "Invalid commit id: $normid" >&2
+		exit 1
+	fi
+fi
 
 if [ "$show_parent" ]; then
 	git-cat-file commit "$normid" | awk '/^parent/{print $2};/^$/{exit}'
 	exit 0
 fi
 
-[ "$type" ] || type=$(git-cat-file -t "$normid")
 if [ "$show_tree" ]; then
 	if [ "$type" = "commit" ]; then
 		normid=$(git-cat-file commit "$normid" | sed -e 's/tree //;q')
 		type=$(git-cat-file -t "$normid")
 	fi
 
 	if [ "$type" != "tree" ]; then
 		echo "Invalid tree id: $normid" >&2
 		exit 1
 	fi
-else
-	if [ "$type" != "commit" ]; then
-		echo "Invalid commit id: $normid" >&2
-		exit 1
-	fi
 fi
 
 echo $normid
 

-- 
Regards,
Pavel Roskin

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2005-09-22 16:56 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-09-20  2:25 [PATCH 6/8] cogito: Don't ever assume object type in cg-object-id Pavel Roskin
2005-09-21 10:00 ` Petr Baudis
2005-09-22  3:27   ` Pavel Roskin
2005-09-22  9:46     ` Petr Baudis
2005-09-22  9:52       ` Petr Baudis
2005-09-22 15:37       ` Pavel Roskin
2005-09-22 15:50         ` Petr Baudis
2005-09-22 16:55           ` Pavel Roskin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).