threads / patch / 2769

patchOffload most of cg-object-id to git-rev-parse

Subject: [PATCH] Offload most of cg-object-id to git-rev-parse

## tl;dr

3 messages between Dec 7, 2005 and Dec 11, 2005. Diffs are folded; open one to read it.

replies: 2people: 1as markdown or json

Jonas Fonseca· Dec 7, 2005, 21:39 UTC · lore
Signed-off-by: Jonas Fonseca <fonseca@diku.dk>

--- commit 964e5ec2dd0ff3b073fbb9f3ad6f11c640a96a04 tree 97f861c24303936f012523a54f3d53f4939ebf52 parent 554014e31b92c98b35f77b80b19f5b21e8a76cfc author Jonas Fonseca <fonseca@diku.dk> Wed, 07 Dec 2005 07:44:26 +0100 committer Jonas Fonseca <fonseca@antimatter.localdomain> Wed, 07 Dec 2005 07:44:26 +0100

 TODO         |    2 --
 cg-object-id |   29 +++++++++--------------------
 2 files changed, 9 insertions(+), 22 deletions(-)
Show changes to 2 files +9 −22

TODO, cg-object-id

diff --git a/TODO b/TODO
index 0658b39..26bb4d0 100644
--- a/TODO
+++ b/TODO
@@ -27,8 +27,6 @@ cg-*patch should be pre-1.0.)
 
 	Includes merge-order cg-log and cg-diff checking for renames.
 
-* Offload most of cg-object-id to git-rev-parse
-
 * Show only first 12 (or so) nibbles of the hashes everywhere
 	Even this might be too much, but more than this is really useless
 	for anyone remotely human. And it's less scary, too.
diff --git a/cg-object-id b/cg-object-id
index ec0362a..c7efa8a 100755
--- a/cg-object-id
+++ b/cg-object-id
@@ -40,27 +40,16 @@ deprecated_alias cg-object-id commit-id 
 normalize_id()
 {
 	local id="$1"
+	local revid=
+	local valid=
 
-	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/$(git-symbolic-ref 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"
+	if [ ! "$id" ] || [ "$id" = "this" ]; then
+		id=HEAD;
+	fi
 
-	elif [ -r "$_git/refs/$id" ]; then
-		read id < "$_git/refs/$id"
+	revid="$(git-rev-parse --verify "$id^0" 2>/dev/null)"
+	if [ "$revid" ]; then
+		id="$revid"
 
 	# Short id's must be lower case and at least 4 digits.
 	elif [[ "$id" == [0-9a-f][0-9a-f][0-9a-f][0-9a-f]* ]]; then
@@ -146,7 +135,7 @@ fi
 
 
 if [ "$show_parent" ]; then
-	git-cat-file commit "$normid" | awk '/^parent/{print $2};/^$/{exit}'
+	git-rev-parse "$normid^"
 	exit 0
 fi
 
-- 
Jonas Fonseca
Jonas Fonseca· Dec 11, 2005, 18:19 UTC · re: Jonas Fonseca · lore
Signed-off-by: Jonas Fonseca <fonseca@diku.dk>
 
---
 Forget the other 4 patches. Here's one that fixes the thing we
 discussed on IRC, i.e. ambiguous ID's are handled correctly
 (unfortunately without giving a proper warning message).
 
 This one also assumes that there is no reason for checking revs
 returned by git-rev-list with git-rev-parse --revs-only.
 TODO         |    2 --
 cg-object-id |   58 ++++++++++++++++------------------------------------------
 2 files changed, 16 insertions(+), 44 deletions(-)
Show changes to 2 files +16 −44

TODO, cg-object-id

diff --git a/TODO b/TODO
index 0658b39..26bb4d0 100644
--- a/TODO
+++ b/TODO
@@ -27,8 +27,6 @@ cg-*patch should be pre-1.0.)
 
 	Includes merge-order cg-log and cg-diff checking for renames.
 
-* Offload most of cg-object-id to git-rev-parse
-
 * Show only first 12 (or so) nibbles of the hashes everywhere
 	Even this might be too much, but more than this is really useless
 	for anyone remotely human. And it's less scary, too.
diff --git a/cg-object-id b/cg-object-id
index 1628e93..49d6d99 100755
--- a/cg-object-id
+++ b/cg-object-id
@@ -40,54 +40,29 @@ deprecated_alias cg-object-id commit-id 
 normalize_id()
 {
 	local id="$1"
+	local revid=
+	local valid=
 
-	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/$(git-symbolic-ref 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"
-
-	elif [ -r "$_git/refs/$id" ]; then
-		read id < "$_git/refs/$id"
-
-	# Short id's must be lower case and at least 4 digits.
-	elif [[ "$id" == [0-9a-f][0-9a-f][0-9a-f][0-9a-f]* ]]; 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
-			exid=$idpref${idmatch#$_git_objects/$idpref/}
-			[ ${#exid} -eq 40 ] && [ "$(git-rev-parse --revs-only "$exid")" ] && id="$exid"
-		elif [ ${#idmatch[*]} -gt 1 ]; then
-			echo "Ambiguous id: $id" >&2
-			echo "${idmatch[@]}" >&2
-			exit 1
-		fi
+	if [ ! "$id" ] || [ "$id" = "this" ]; then
+		id=HEAD;
+	fi
+
+ 	revid="$(git-rev-parse --verify "$id^0" 2>/dev/null)"
+ 	if [ "$revid" ]; then
+ 		id="$revid"
+		valid=1
 	fi
 
-	valid=; [ ${#id} -eq 40 ] && [ "$(git-rev-parse --revs-only "$id")" ] && valid=1
 	# date does the wrong thing for empty and single-letter ids
 	if [ ${#id} -gt 1 ] && [ ! "$valid" ]; then
 		reqsecs=$(date --date="$id" +'%s' 2>/dev/null)
 
 		if [ "$reqsecs" ]; then
-			id=$(git-rev-list --min-age=$reqsecs --max-count=1 HEAD)
-			valid=; [ ${#id} -eq 40 ] && [ "$(git-rev-parse --revs-only "$id")" ] && valid=1
+			revid=$(git-rev-list --min-age=$reqsecs --max-count=1 HEAD)
+			if [ ${#revid} -eq 40 ]; then
+				id="$revid"
+				valid=1
+			fi
 		fi
 	fi
 
@@ -147,7 +122,7 @@ fi
 
 
 if [ "$show_parent" ]; then
-	git-cat-file commit "$normid" | awk '/^parent/{print $2};/^$/{exit}'
+	git-rev-parse "$normid^"
 	exit 0
 fi
 
@@ -170,4 +145,3 @@ else
 fi
 
 echo $normid
-
-- 
Jonas Fonseca
Jonas Fonseca· Dec 11, 2005, 18:31 UTC · re: Jonas Fonseca · lore

[PATCH] cg-object-id: use git-rev-parse(1) for date parsing

Using the --until switch, git-rev-parse(1) will first be given the ID. If it cannot make sense of the ID fallback to using date(1).

Signed-off-by: Jonas Fonseca <fonseca@diku.dk>
 
---
 
Show changes to cg-object-id +8 −1
diff --git a/cg-object-id b/cg-object-id
index 49d6d99..4e3a3e2 100755
--- a/cg-object-id
+++ b/cg-object-id
@@ -55,7 +55,14 @@ normalize_id()
 
 	# date does the wrong thing for empty and single-letter ids
 	if [ ${#id} -gt 1 ] && [ ! "$valid" ]; then
-		reqsecs=$(date --date="$id" +'%s' 2>/dev/null)
+		cursecs=$(git-rev-parse --until=yksap); cursecs=${cursecs:10}
+		reqsecs=$(git-rev-parse --until="$id"); reqsecs=${reqsecs:10}
+ 
+		# git-rev-parse(1) will output the current time if the ID
+		# doesn't make sense. Workaround it so date(1) can have a try.
+		if [ "$cursecs" -le "$reqsecs" ]; then
+			reqsecs=$(date --date="$id" +'%s' 2>/dev/null)
+		fi
 
 		if [ "$reqsecs" ]; then
 			revid=$(git-rev-list --min-age=$reqsecs --max-count=1 HEAD)
-- 
Jonas Fonseca

← back to recent threads