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

[PATCH 6/8] cogito: Don't ever assume object type in cg-object-id

From
PRPavel Roskin <proski@gnu.org>
Date
Sep 20, 2005, 02:25 UTC
Message-ID
<1127183120.31115.34.camel@dv>
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
Next: Petr Baudis
Message 1 of 8 in “cogito: Don't ever assume object type in cg-object-id”
  1. 6/8 cogito: Don't ever assume object type in cg-object-idPavel Roskin, Sep 20, 2005
  2. Petr BaudisSep 21, 2005
  3. Pavel RoskinSep 22, 2005
  4. Petr BaudisSep 22, 2005
  5. Petr BaudisSep 22, 2005
  6. Pavel RoskinSep 22, 2005
  7. Petr BaudisSep 22, 2005
  8. Pavel RoskinSep 22, 2005

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.