git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 3/4] update-hook: parse the tag header in preparation to use the tag type
@ 2007-03-20 10:58 Andy Parkins
  2007-03-20 14:38 ` Shawn O. Pearce
  0 siblings, 1 reply; 4+ messages in thread
From: Andy Parkins @ 2007-03-20 10:58 UTC (permalink / raw)
  To: git

The tag header is parsed with a while loop and read command.  I'm not
entirely sure how portable it is, as it had to use the following unusual
heredoc format:

  while read field value
  do
    # ..
  done <<< "$(some command)"

This was necessary because piping the output to a while doesn't work
because the pipe is run in a separate process and hence setting
variables inside it has no effect in the main script process.  This
"<<<" heredoc notation works around that problem, but I have no idea how
widely supported it is.  The alternative (should it be necessary) is
to wastefully make multiple "git-cat-file | sed" calls - yuck.

This patch also updates the user/time extraction from the "tagger" field
to use the variable $tagger, rather than the "git-cat-file | sed" call
used previously.

Signed-off-by: Andy Parkins <andyparkins@gmail.com>
---
 templates/hooks--update |   24 +++++++++++++++++++++---
 1 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/templates/hooks--update b/templates/hooks--update
index 31e72ca..fd4c081 100644
--- a/templates/hooks--update
+++ b/templates/hooks--update
@@ -215,15 +215,33 @@ case "$refname_type" in
 			echo "      from  $oldrev"
 		fi
 
+		# Read the tag header
+		while read field value
+		do
+			case "$field" in
+			object)
+				tagobject="$value"
+				;;
+			type)
+				tagtype="$value"
+				;;
+			tag)
+				# Confirm that this tag has the right name?  Nah
+				;;
+			tagger)
+				tagger="$value"
+				;;
+			esac
+		done <<< "$(git cat-file tag $newrev | head -q -n 4)"
+
 		# If this tag succeeds another, then show which tag it replaces
 		prevtag=$(git describe --abbrev=0 $newrev^ 2>/dev/null)
 		if [ -n "$prevtag" ]; then
 			echo "  replaces  $prevtag"
 		fi
 
-		# Read the tag details
-		eval $(git cat-file tag $newrev | \
-			sed -n '4s/tagger \([^>]*>\)[^0-9]*\([0-9]*\).*/tagger="\1" ts="\2"/p')
+		# Extract user and time from tagger variable
+		eval $(sed -n 's/\([^>]*>\)[^0-9]*\([0-9]*\).*/tagger="\1" ts="\2"/p' <<< "$tagger")
 		tagged=$(date --date="1970-01-01 00:00:00 +0000 $ts seconds" +"$DATEFORMAT")
 
 		echo " tagged by  $tagger"
-- 
1.5.0.3.402.g0c48

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

* Re: [PATCH 3/4] update-hook: parse the tag header in preparation to use the tag type
  2007-03-20 10:58 [PATCH 3/4] update-hook: parse the tag header in preparation to use the tag type Andy Parkins
@ 2007-03-20 14:38 ` Shawn O. Pearce
  2007-03-20 15:24   ` Andy Parkins
  0 siblings, 1 reply; 4+ messages in thread
From: Shawn O. Pearce @ 2007-03-20 14:38 UTC (permalink / raw)
  To: Andy Parkins; +Cc: git

Andy Parkins <andyparkins@gmail.com> wrote:
> The tag header is parsed with a while loop and read command.  I'm not
> entirely sure how portable it is, as it had to use the following unusual
> heredoc format:
> 
>   while read field value
>   do
>     # ..
>   done <<< "$(some command)"

To bad you can't use for-each-ref.  ;-)

The problem of course is the update hook is running before a ref
is created to point at the tag.  If you used post-receive hook
on the other hand...  Oh, right, that has its own problems too!

-- 
Shawn.

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

* Re: [PATCH 3/4] update-hook: parse the tag header in preparation to use the tag type
  2007-03-20 14:38 ` Shawn O. Pearce
@ 2007-03-20 15:24   ` Andy Parkins
  2007-03-20 15:38     ` Shawn O. Pearce
  0 siblings, 1 reply; 4+ messages in thread
From: Andy Parkins @ 2007-03-20 15:24 UTC (permalink / raw)
  To: git; +Cc: Shawn O. Pearce

On Tuesday 2007 March 20 14:38, Shawn O. Pearce wrote:

> The problem of course is the update hook is running before a ref
> is created to point at the tag.  If you used post-receive hook
> on the other hand...  Oh, right, that has its own problems too!

I've obviously not described the problem very well; as I don't understand how 
your comment relates to this patch.

All I'm doing is pulling the individual fields out of

 $ git cat-file -p v1.5.0
 object 437b1b20df4b356c9342dac8d38849f24ef44f27
 type commit
 tag v1.5.0
 tagger Junio C Hamano <junkio@cox.net> Wed Feb 14 00:00:00 2007 +0000

And putting them in their own variables.  I'm not sure how for-each-ref or in 
fact, any ref-based tool would help me do this.  At this point it's nothing 
to do with git, it's a standard shell script problem.

 git cat-file -p $newrev | while read field value
 do
   variable = "found"
 done
 echo $variable

Doesn't work, because the right half of the pipe is run in its own process and 
so setting "variable" has no effect once the while loop is over.

I found that "<<<" heredoc thing in the bash manual, which is almost like 
having a reverse pipe and allows the while loop to run in the main process.



Andy
-- 
Dr Andy Parkins, M Eng (hons), MIET
andyparkins@gmail.com

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

* Re: [PATCH 3/4] update-hook: parse the tag header in preparation to use the tag type
  2007-03-20 15:24   ` Andy Parkins
@ 2007-03-20 15:38     ` Shawn O. Pearce
  0 siblings, 0 replies; 4+ messages in thread
From: Shawn O. Pearce @ 2007-03-20 15:38 UTC (permalink / raw)
  To: Andy Parkins; +Cc: git

Andy Parkins <andyparkins@gmail.com> wrote:
> On Tuesday 2007 March 20 14:38, Shawn O. Pearce wrote:
> 
> > The problem of course is the update hook is running before a ref
> > is created to point at the tag.  If you used post-receive hook
> > on the other hand...  Oh, right, that has its own problems too!
> 
> I've obviously not described the problem very well; as I don't understand how 
> your comment relates to this patch.
> 

eval `git for-each-ref --format='taggername=%(taggername) taggerdate=%(taggerdate)' thetag`

Then you have $taggername and $taggerdate available.  ;-)

The problem is, there is no ref to for-each over.  :-(

-- 
Shawn.

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

end of thread, other threads:[~2007-03-20 15:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-03-20 10:58 [PATCH 3/4] update-hook: parse the tag header in preparation to use the tag type Andy Parkins
2007-03-20 14:38 ` Shawn O. Pearce
2007-03-20 15:24   ` Andy Parkins
2007-03-20 15:38     ` Shawn O. Pearce

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).