threads / patch / 1092

patchCogito: Use git-rev-parse instead of cg-Xnormid.

Subject: [PATCH] Cogito: Use git-rev-parse instead of cg-Xnormid.

## tl;dr

8 messages between Jul 3, 2005 and Jul 3, 2005. Diffs are folded; open one to read it.

replies: 7people: 4as markdown or json

Brian Gerst· Jul 3, 2005, 05:59 UTC · lore

Use git-rev-parse instead of cg-Xnormid. This allows Cogito to work properly with packed objects.

Signed off by: Brian Gerst <bgerst@didntduck.org>
Use git-rev-parse instead of cg-Xnormid.  This allows Cogito to work properly with packed objects.
Signed off by: Brian Gerst <bgerst@didntduck.org>

--- commit 32c6125190d7d183110790b556a8e1128f29c52d tree 0e53bc37eb8ad12f093cfb0e903689ef95c54ece parent 266e27c48ba20c1af33f6a3eb966e8cd0d3c8b65 author Brian Gerst <bgerst@didntduck.org> Sun, 03 Jul 2005 01:53:22 -0400 committer Brian Gerst <bgerst@didntduck.org> Sun, 03 Jul 2005 01:53:22 -0400

 Makefile   |    2 +-
 cg-Xnormid |   63 ------------------------------------------------------------
 commit-id  |   12 +----------
 parent-id  |    4 +---
 tree-id    |    4 ++--
 5 files changed, 5 insertions(+), 80 deletions(-)
Show changes to 5 files +5 −80

Makefile, cg-Xnormid, commit-id, parent-id, tree-id

diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -67,7 +67,7 @@ SCRIPT=	commit-id tree-id parent-id cg-a
 	cg-pull cg-restore cg-rm cg-seek cg-status cg-tag cg-tag-ls cg-update \
 	cg cg-admin-ls cg-push cg-branch-chg
 
-LIB_SCRIPT=cg-Xlib cg-Xmergefile cg-Xnormid
+LIB_SCRIPT=cg-Xlib cg-Xmergefile
 
 GEN_SCRIPT= cg-version
 
diff --git a/cg-Xnormid b/cg-Xnormid
deleted file mode 100755
--- a/cg-Xnormid
+++ /dev/null
@@ -1,63 +0,0 @@
-#!/usr/bin/env bash
-#
-# Normalize an ID to an SHA1 hash value.
-# Copyright (c) Petr Baudis, 2005
-# Copyright (c) Philip Pokorny, 2005
-#
-# Strings resolve in this order:
-# 	NULL, this, HEAD  => .git/HEAD
-# 	<tags>
-# 	<heads>
-# 	short SHA1 (4 or more hex digits)
-#
-# Takes the ID to normalize and returns the normalized ID.
-
-. ${COGITO_LIB}cg-Xlib
-
-id="$1"
-
-if [ ! "$id" ] || [ "$id" = "this" ] || [ "$id" = "HEAD" ]; then
-	read id < "$_git/HEAD"
-
-elif [ -r "$_git/refs/tags/$id" ]; then
-	read id < "$_git/refs/tags/$id"
-
-elif [ -r "$_git/refs/heads/$id" ]; then
-	read id < "$_git/refs/heads/$id"
-
-# 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
-	idpref=${id:0:2}
-	idpost=${id:2}
-
-	# Assign array elements to matching names
-	idmatch=($_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 ([ "$id" ] && [ "$id" != " " ]) && ([ ${#id} -ne 40 ] || [ ! -f .git/objects/${id:0:2}/${id:2} ]); then
-	reqsecs=$(date --date="$id" +'%s' 2>/dev/null)
-
-	if [ "$reqsecs" ]; then
-		id=$(git-rev-list --min-age=$reqsecs --max-count=1 HEAD)
-	fi
-fi
-
-# If we don't have a 40-char ID by now, it's an error
-if [ ${#id} -ne 40 ] || [ ! -f $_git_objects/${id:0:2}/${id:2} ]; then
-	echo "Invalid id: $id" >&2
-	exit 1
-fi
-
-if [ "$(git-cat-file -t "$id")" = "tag" ]; then
-	id=$(git-cat-file tag "$id" | head -n 1)
-	id="${id#object }"
-fi
-
-echo $id
diff --git a/commit-id b/commit-id
--- a/commit-id
+++ b/commit-id
@@ -5,14 +5,4 @@
 #
 # Takes the appropriate ID, defaults to HEAD.
 
-. ${COGITO_LIB}cg-Xlib
-
-id="$1"
-normid=$(${COGITO_LIB}cg-Xnormid "$id") || exit 1
-
-if [ "$(git-cat-file -t "$normid")" != "commit" ]; then
-	echo "Invalid commit id: $id" >&2
-	exit 1
-fi
-
-echo $normid
+git-rev-parse "${1:-HEAD}"
diff --git a/parent-id b/parent-id
--- a/parent-id
+++ b/parent-id
@@ -7,6 +7,4 @@
 #
 # NOTE: Will return multiple SHA1s if ID is a commit with multiple parents.
 
-id=$(commit-id "$1") || exit 1
-
-git-cat-file commit $id | awk '/^parent/{print $2};/^$/{exit}'
+git-rev-parse "${1:-HEAD}^"
diff --git a/tree-id b/tree-id
--- a/tree-id
+++ b/tree-id
@@ -5,8 +5,8 @@
 #
 # Takes ID of the appropriate commit, defaults to HEAD.
 
-id="$1"
-normid=$(${COGITO_LIB}cg-Xnormid "$id") || exit 1
+id="${1:-HEAD}"
+normid=$(git-rev-parse "$id")
 type=$(git-cat-file -t "$normid")
 
 if [ "$type" = "commit" ]; then
Petr Baudis· Jul 3, 2005, 06:59 UTC · re: Brian Gerst · lore

Re: [PATCH] Cogito: Use git-rev-parse instead of cg-Xnormid.

Dear diary, on Sun, Jul 03, 2005 at 07:59:42AM CEST, I got a letter where Brian Gerst <bgerst@didntduck.org> told me that...

> Use git-rev-parse instead of cg-Xnormid.  This allows Cogito to work 
> properly with packed objects.
> 
> Signed off by: Brian Gerst <bgerst@didntduck.org>

But git-rev-parse sucks. It won't detect invalid IDs (--revs-only?), and does not support short object IDs (that's a must, it's tremendously useful). You need to add that for it to be useful first.

Show 14 quoted lines
> diff --git a/tree-id b/tree-id
> --- a/tree-id
> +++ b/tree-id
> @@ -5,8 +5,8 @@
>  #
>  # Takes ID of the appropriate commit, defaults to HEAD.
>  
> -id="$1"
> -normid=$(${COGITO_LIB}cg-Xnormid "$id") || exit 1
> +id="${1:-HEAD}"
> +normid=$(git-rev-parse "$id")
>  type=$(git-cat-file -t "$normid")
>  
>  if [ "$type" = "commit" ]; then

This is broken too. You need to be able to pass _tree_ ID to tree-id too, not just commit ID. Hmm, or is git-rev-parse able to process any ids? Then it's terribly misnamed too. :-) A comment would be useful in that case. (Or better a patch to rename it.)

Thanks,
-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
<Espy> be careful, some twit might quote you out of context..
Brian Gerst· Jul 3, 2005, 12:25 UTC · re: Petr Baudis · lore

Re: [PATCH] Cogito: Use git-rev-parse instead of cg-Xnormid.

Petr Baudis wrote:
Show 12 quoted lines
> Dear diary, on Sun, Jul 03, 2005 at 07:59:42AM CEST, I got a letter
> where Brian Gerst <bgerst@didntduck.org> told me that...
> 
>>Use git-rev-parse instead of cg-Xnormid.  This allows Cogito to work 
>>properly with packed objects.
>>
>>Signed off by: Brian Gerst <bgerst@didntduck.org>
> 
> 
> But git-rev-parse sucks. It won't detect invalid IDs (--revs-only?), and
> does not support short object IDs (that's a must, it's tremendously
> useful). You need to add that for it to be useful first.

cg-Xnormid is terminally broken in the presence of packed files, since it cannot look into them to find objects. Moreover, many uses of commit-id in the scripts can be eliminated because the underlying git commands can already understand tags directly, and will check for invalid ids then. The only thing missing is the short id matching.

Show 22 quoted lines
> 
> 
>>diff --git a/tree-id b/tree-id
>>--- a/tree-id
>>+++ b/tree-id
>>@@ -5,8 +5,8 @@
>> #
>> # Takes ID of the appropriate commit, defaults to HEAD.
>> 
>>-id="$1"
>>-normid=$(${COGITO_LIB}cg-Xnormid "$id") || exit 1
>>+id="${1:-HEAD}"
>>+normid=$(git-rev-parse "$id")
>> type=$(git-cat-file -t "$normid")
>> 
>> if [ "$type" = "commit" ]; then
> 
> 
> This is broken too. You need to be able to pass _tree_ ID to tree-id too,
> not just commit ID. Hmm, or is git-rev-parse able to process any ids?
> Then it's terribly misnamed too. :-) A comment would be useful in that
> case. (Or better a patch to rename it.)
Yes you can still pass in the tree id.
--
				Brian Gerst
Petr Baudis· Jul 3, 2005, 15:41 UTC · re: Brian Gerst · lore

Re: [PATCH] Cogito: Use git-rev-parse instead of cg-Xnormid.

Dear diary, on Sun, Jul 03, 2005 at 02:25:09PM CEST, I got a letter where Brian Gerst <bgerst@didntduck.org> told me that...

Show 16 quoted lines
> Petr Baudis wrote:
> >Dear diary, on Sun, Jul 03, 2005 at 07:59:42AM CEST, I got a letter
> >where Brian Gerst <bgerst@didntduck.org> told me that...
> >
> >>Use git-rev-parse instead of cg-Xnormid.  This allows Cogito to work 
> >>properly with packed objects.
> >>
> >>Signed off by: Brian Gerst <bgerst@didntduck.org>
> >
> >
> >But git-rev-parse sucks. It won't detect invalid IDs (--revs-only?), and
> >does not support short object IDs (that's a must, it's tremendously
> >useful). You need to add that for it to be useful first.
> 
> cg-Xnormid is terminally broken in the presence of packed files, since 
> it cannot look into them to find objects.

That's right. Well, for everything but the short id matching we could just check the ID validity by git-rev-parse instead of peeking into the object store - I just did that. If I'm not missing anything, that will just make the short id matching for packed objects impossible, but works fine otherwise...?

> Moreover, many uses of commit-id in the scripts can be eliminated because
> the underlying git commands can already understand tags directly, and will
> check for invalid ids then.

I feel reserved about that. I want to have custom error handling here to give the user less confusing output (no core GIT command name, since that confuses users), and as I said, Cogito's rev resolving is more powerful than Core Git's.

> The only thing missing is the short id matching.

Yes, but as I said, I think it's very important to have. BTW, another cool thing cg-Xnormid does and git-rev-parse does not: time specifiers. E.g. you could specify revisions as "2 days ago" or so, very useful for cg-log, cg-diff and such.

Thanks for pointing this out, BTW.
-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
<Espy> be careful, some twit might quote you out of context..
Sven Verdoolaege· Jul 3, 2005, 15:40 UTC · re: Petr Baudis · lore

Re: [PATCH] Cogito: Use git-rev-parse instead of cg-Xnormid.

On Sun, Jul 03, 2005 at 05:41:27PM +0200, Petr Baudis wrote:
> That's right. Well, for everything but the short id matching we could
> just check the ID validity by git-rev-parse instead of peeking into
> the object store - I just did that. 

Why not use it to actually resolve ids ? I really miss the '^' parent notation in cogito.

skimo
Petr Baudis· Jul 3, 2005, 22:31 UTC · re: Sven Verdoolaege · lore

Re: [PATCH] Cogito: Use git-rev-parse instead of cg-Xnormid.

Dear diary, on Sun, Jul 03, 2005 at 05:40:49PM CEST, I got a letter where Sven Verdoolaege <skimo@kotnet.org> told me that...

Show 7 quoted lines
> On Sun, Jul 03, 2005 at 05:41:27PM +0200, Petr Baudis wrote:
> > That's right. Well, for everything but the short id matching we could
> > just check the ID validity by git-rev-parse instead of peeking into
> > the object store - I just did that. 
> 
> Why not use it to actually resolve ids ?
> I really miss the '^' parent notation in cogito.

I've decided to go the less troublesome way and just teach cg-Xnormid about the ^ prefix. ;-)

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
<Espy> be careful, some twit might quote you out of context..
Linus Torvalds· Jul 3, 2005, 22:40 UTC · re: Petr Baudis · lore

Re: [PATCH] Cogito: Use git-rev-parse instead of cg-Xnormid.

On Sun, 3 Jul 2005, Petr Baudis wrote:
> 
> That's right. Well, for everything but the short id matching we could
> just check the ID validity by git-rev-parse instead of peeking into
> the object store - I just did that.

What's the format of the short ID? Minimum 6 characters of the SHA1 or something?

Pack-files in many ways should be even easier to look up the ID in, since the pack-file index has all the SHA1's for that pack file listed in sorted order, so it should be fairly easy to just look it up based on the first few bytes.. If somebody sends me a patch, that should be fine.

Show 6 quoted lines
> > The only thing missing is the short id matching.
> 
> Yes, but as I said, I think it's very important to have. BTW, another
> cool thing cg-Xnormid does and git-rev-parse does not: time specifiers.
> E.g. you could specify revisions as "2 days ago" or so, very useful for
> cg-log, cg-diff and such.
That is indeed something that seems to make more sense in cg-Xnormid.
		Linus
Petr Baudis· Jul 3, 2005, 23:03 UTC · re: Linus Torvalds · lore

Re: [PATCH] Cogito: Use git-rev-parse instead of cg-Xnormid.

Dear diary, on Mon, Jul 04, 2005 at 12:40:52AM CEST, I got a letter where Linus Torvalds <torvalds@osdl.org> told me that...

Show 15 quoted lines
> 
> 
> On Sun, 3 Jul 2005, Petr Baudis wrote:
> > 
> > That's right. Well, for everything but the short id matching we could
> > just check the ID validity by git-rev-parse instead of peeking into
> > the object store - I just did that.
> 
> What's the format of the short ID? Minimum 6 characters of the SHA1 or
> something?
> 
> Pack-files in many ways should be even easier to look up the ID in, since
> the pack-file index has all the SHA1's for that pack file listed in sorted
> order, so it should be fairly easy to just look it up based on the first
> few bytes.. If somebody sends me a patch, that should be fine.

Actually, the minimum is 4 characters, but that shouldn't be much of an issue. Cogito's behaviour is to raise an error in case of multiple matches; but that could be controlled by a switch too...

	bash$ complete -C git-rev-parse git* cg*
I will cook up a patch in a day or two if noone outruns me.
-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
<Espy> be careful, some twit might quote you out of context..

← back to recent threads