All of lore.kernel.org
 help / color / mirror / Atom feed
From: Philip Pokorny <ppokorny@mindspring.com>
To: Git Mailing List <git@vger.kernel.org>
Subject: PATCH: Allow tree-id to return the ID of a tree object
Date: Wed, 27 Apr 2005 09:20:55 -0700	[thread overview]
Message-ID: <426FBBE7.1090806@mindspring.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1089 bytes --]

While playing with cg-ls, I tried:

% cg-ls
... snip ...
100644  blob    bc607fd55f6ce4e56ce87766369b5d4d55ec79af        object.h
100755  blob    f35877a6aa5b68d2fb4a388dcfa9b3e64262604e        parent-id
040000  tree    bfb75011c32589b282dd9c86621dadb0f0bb3866        ppc
100644  blob    d922305ee0f5583bdfcb629f6d4061e11e0fa859        read-cache.c
100644  blob    1ad7ffc555b635fe57fa7834b12d71ff576be065        read-tree.c
... snip ...
% cg-ls bfb75011c32589b282dd9c86621dadb0f0bb3866       <-- the ppc tree ID
Invalid id: bfb75011c32589b282dd9c86621dadb0f0bb3866
usage: cat-file [-t | tagname] <sha1>
usage: cat-file [-t | tagname] <sha1>
Invalid id:


Shouldn't cg-ls give a listing of a sub-tree?  The cg-help says it takes
a TREE-ID?

The problem seems to be that tree-id really only accepts a commit-id and
returns the TREE-ID of that commit.

So I modified commit-id, tree-id and parent-id to make them more similar
in coding style, force "short-id" names to be at least 4 lower case
letters, and have tree-id accept short, unambiguous ID's and bare SHA1-ID's.

Patch attached.




[-- Attachment #2: cogito-0.8-idparse.patch --]
[-- Type: text/plain, Size: 3014 bytes --]

Index: commit-id
===================================================================
--- 6ad600e20c89323c1d3049f75b8ca9b0a2d72167/commit-id  (mode:100755 sha1:4efcb6bdfdb2b2c5744f5d4d47d92beb7777ed59)
+++ uncommitted/commit-id  (mode:100775)
@@ -9,22 +9,30 @@
 SHA1ONLY="^$SHA1$"
 
 id=$1
+
 if [ ! "$id" ] || [ "$id" = "this" ] || [ "$id" = "HEAD" ]; then
 	id=$(cat .git/HEAD)
-fi
 
-if (echo $id | egrep -vq "$SHA1ONLY") && [ -r ".git/refs/tags/$id" ]; then
+elif [ -r ".git/refs/tags/$id" ]; then
 	id=$(cat ".git/refs/tags/$id")
-fi
 
-if (echo $id | egrep -vq "$SHA1ONLY") && [ -r ".git/refs/heads/$id" ]; then
+elif [ -r ".git/refs/heads/$id" ]; then
 	id=$(cat ".git/refs/heads/$id")
-fi
 
-idpref=$(echo "$id" | cut -c -2)
-idpost=$(echo "$id" | cut -c 3-)
-if [ $(find ".git/objects/$idpref" -name "$idpost*" 2>/dev/null | wc -l) -eq 1 ]; then
-	id=$idpref$(basename $(echo .git/objects/$idpref/$idpost*))
+# Short id's must be lower case and at least 4 digits.
+elif [[ "$id" == [0-9a-z][0-9a-z][0-9a-z][0-9a-z]* ]]; then
+	idpost=${id#??}
+	idpref=${id%$idpost}
+
+	# Assign array elements to matching names
+	idmatch=($(echo .git/objects/$idpref/$idpost*))
+
+	if [ ${#idmatch[*]} -eq 1 ] && [ -r "$idmatch" ]; then
+		id=$idpref${idmatch#.git/objects/$idpref/}
+	elif [ ${#idmatch[*]} -gt 1 ]; then
+		echo "Ambiguous id: $id" >&2
+		exit 1
+	fi
 fi
 
 if echo $id | egrep -vq "$SHA1ONLY"; then
Index: tree-id
===================================================================
--- 6ad600e20c89323c1d3049f75b8ca9b0a2d72167/tree-id  (mode:100755 sha1:1495ff78af71b57e21653512932bcda88fe05454)
+++ uncommitted/tree-id  (mode:100775)
@@ -7,8 +7,35 @@
 
 SHA1="[A-Za-z0-9]{40}"
 TREE="^tree $SHA1$"
+SHA1ONLY="^$SHA1$"
 
-id=$(cat-file commit $(commit-id "$1") | egrep "$TREE" | cut -d ' ' -f 2)
+id=$1
+
+# Is it a commit?
+commit=$(commit-id $id 2>/dev/null)
+if [ "$commit" ]; then
+	id=$(cat-file commit "$commit") | egrep "$TREE" | cut -d ' ' -f 2)
+
+# Short id's must be lower case and at least 4 digits.
+elif [[ "$id" == [0-9a-z][0-9a-z][0-9a-z][0-9a-z]* ]]; then
+	idpost=${id#??}
+	idpref=${id%$idpost}
+
+	# Assign array elements to matching names
+	idmatch=($(echo .git/objects/$idpref/$idpost*))
+
+	if [ ${#idmatch[*]} -eq 1 ] && [ -r "$idmatch" ]; then
+		id=$idpref${idmatch#.git/objects/$idpref/}
+	elif [ ${#idmatch[*]} -gt 1 ]; then
+		echo "Ambiguous id: $id" >&2
+		exit 1
+	fi
+fi
+
+if echo $id | egrep -vq "$SHA1ONLY"; then
+	echo "Invalid id: $id" >&2
+	exit 1
+fi
 
 if [ "$(cat-file -t "$id")" != "tree" ]; then
 	echo "Invalid id: $id" >&2
Index: parent-id
===================================================================
--- 6ad600e20c89323c1d3049f75b8ca9b0a2d72167/parent-id  (mode:100755 sha1:f35877a6aa5b68d2fb4a388dcfa9b3e64262604e)
+++ uncommitted/parent-id  (mode:100775)
@@ -5,7 +5,8 @@
 #
 # Takes ID of the current commit, defaults to HEAD.
 
-PARENT="^parent [A-Za-z0-9]{40}$"
+SHA1="[A-Za-z0-9]{40}"
+PARENT="^parent $SHA1$"
 
 id=$(commit-id $1) || exit 1
 


             reply	other threads:[~2005-04-27 16:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-27 16:20 Philip Pokorny [this message]
2005-04-27 23:22 ` PATCH: Allow tree-id to return the ID of a tree object Petr Baudis
2005-04-28  7:11   ` PATCH[0/4]: " Philip Pokorny
2005-04-28  7:14   ` PATCH[1/4]: " Philip Pokorny
2005-04-28  7:15   ` PATCH[2/4]: " Philip Pokorny
2005-05-02  5:46     ` Philip Pokorny
2005-04-28  7:16   ` PATCH[3/4]: " Philip Pokorny
2005-04-28  7:17   ` PATCH[4/4]: " Philip Pokorny

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=426FBBE7.1090806@mindspring.com \
    --to=ppokorny@mindspring.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.