threads / patch / 2078

patchAdd git-findtags

Subject: [PATCH] Add git-findtags

## tl;dr

3 messages between Oct 12, 2005 and Oct 13, 2005. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Martin Langhoff· Oct 12, 2005, 03:19 UTC · lore

A short perl script that will walk the tag refs, tag objects, and even commit objects in its quest to figure out whether the given SHA1 (for a commit or tree) was ever tagged.

Usage: git-findtags.perl [ -t ] <commit-or-tree-sha1>
Signed-off-by: Martin Langhoff <martin@catalyst.net.nz>
---
 Makefile          |    3 ++
 git-findtags.perl |   70 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 1 deletions(-)
 create mode 100755 git-findtags.perl

applies-to: ef9b5c2cb61cf509adf2f5ef37fa1db517291f48 58597ae99b77a690a788d2f096cecc1b7ae809fd

Show changes to 2 files +72 −1

Makefile, git-findtags.perl

diff --git a/Makefile b/Makefile
index 8697d52..a5d9cd4 100644
--- a/Makefile
+++ b/Makefile
@@ -93,7 +93,8 @@ SCRIPT_SH = \
 
 SCRIPT_PERL = \
 	git-archimport.perl git-cvsimport.perl git-relink.perl \
-	git-rename.perl git-shortlog.perl git-fmt-merge-msg.perl
+	git-rename.perl git-shortlog.perl git-fmt-merge-msg.perl \
+	git-findtags.perl
 
 SCRIPT_PYTHON = \
 	git-merge-recursive.py
diff --git a/git-findtags.perl b/git-findtags.perl
new file mode 100755
index 0000000..40eb284
--- /dev/null
+++ b/git-findtags.perl
@@ -0,0 +1,70 @@
+#!/usr/bin/perl -w
+#
+# Copyright (c) 2005 Martin Langhoff
+#
+# Walk the tags and find if they match a commit
+# expects a SHA1 of a commit. Option -t enables 
+# searching trees too.
+#
+
+use strict;
+use File::Basename;
+use Getopt::Std;
+
+my $git_dir = $ENV{GIT_DIR} || '.git';
+
+# options
+our $opt_t;
+getopts("t") || usage();
+
+my @tagfiles   = `find $git_dir/refs/tags -follow -type f`; # haystack
+my $target = shift @ARGV;                     # needle
+
+unless ($target) { 
+    usage();
+}
+
+
+foreach my $tagfile (@tagfiles) {
+    chomp $tagfile;
+    my $tagid = quickread($tagfile);
+    chomp $tagid;
+
+    # grab the first 2 lines (the whole tag could be large)
+    my $tagobj = `git-cat-file tag $tagid | head -n2 `;
+    if ($tagobj =~  m/^type commit$/m) { # only deal with commits
+
+	if ($tagobj =~ m/^object $target$/m) { # match on the commit
+	    print basename($tagfile) . "\n";
+
+	} elsif ( $opt_t &&                      # follow the commit
+		 $tagobj =~ m/^object (\S+)$/m) { # and try to match trees
+	    my $commitid = $1;
+	    my $commitobj = `git-cat-file commit $commitid | head -n1`;
+	    chomp $commitobj;
+	    $commitobj =~ m/^tree (\S+)$/;
+	    my $treeid = $1;
+	    if ($target eq $treeid) {
+		print  basename($tagfile) . "\n";
+	    }
+	}
+    }
+}
+
+sub quickread {
+    my $file = shift;
+    local $/; undef $/; # slurp mode
+    open FILE, "<$file"
+	or die "Cannot open $file : $!";
+    my $content = <FILE>;
+    close FILE;
+    return $content;
+}
+
+sub usage {
+	print STDERR <<END;
+Usage: ${\basename $0}     # find tags for a commit or tree
+       [ -t ] <commit-or-tree-sha1>
+END
+	exit(1);
+}
---
0.99.8.GIT
Junio C Hamano· Oct 12, 2005, 05:19 UTC · re: Martin Langhoff · lore

Re: [PATCH] Add git-findtags

Martin Langhoff <martin@catalyst.net.nz> writes:
> +my @tagfiles   = `find $git_dir/refs/tags -follow -type f`; # haystack

Please do this with File::Find(), unless you have a compelling reason not to. $git_dir could contain shell metacharacters and/or whitespace.

> +    # grab the first 2 lines (the whole tag could be large)
> +    my $tagobj = `git-cat-file tag $tagid | head -n2 `;

Careful; $tagfile could be a lightweight tag, and you would want to consider it a hit if ($tagid eq $target).

BTW, does `` in Perl behave well upon SIGPIPE (hehe, we now have Merlyn on the list so I can ask these silly Perl questions ;-))?

> +    if ($tagobj =~  m/^type commit$/m) { # only deal with commits
> +
> +	if ($tagobj =~ m/^object $target$/m) { # match on the commit
> +	    print basename($tagfile) . "\n";

Please show "tags/bl/ah" when ".git/refs/tags/bl/ah" points at the target, not just "ah". Alternatively, "bl/ah" may also be acceptable, since this command is about tags.

> +	} elsif ( $opt_t &&                      # follow the commit
> +		 $tagobj =~ m/^object (\S+)$/m) { # and try to match trees

I am not sure how useful '-t' would be in practice. If it is, I wonder if it would also be useful to look for a subtree match..

> +sub quickread {
> +    my $file = shift;
> +    local $/; undef $/; # slurp mode
Nit -- "local $/;" without "undef $/;" would do just fine.

BTW, wouldn't it be easier for this particular script, and more useful in general, if something like what 'git-rev-parse' does for commit objects when given "REV^0" is supported for tags?

I am uncertain the about syntax/notation, but just like "^0" is a postfix operator for "peel the onion repeatedly until you get a commit, or barf if you ended up with a non-commit", maybe if we had "peel the onion repeatedly until you get a non-tag" operator, let's call it '%', then you could say something like this:

        # This is pseudo-code -- it does not handle tagname with
        # embedded $IFS letter very well.
        # list local refs
	git-rev-parse --symbolic --all |
        # limit only to tags
        sed -ne '/^refs\/tags\/p' |
        while read tagname
        do
        	if test $(git-rev-parse --verify "${tagname}%") = "$target"
		then
                	echo "$tagname"
		fi
	done
Junio C Hamano· Oct 13, 2005, 05:18 UTC · lore

Re: [PATCH] Add git-findtags

Martin Langhoff <martin.langhoff@gmail.com> writes:
Show 6 quoted lines
> On 10/12/05, Junio C Hamano <junkio@cox.net> wrote:
>> Martin Langhoff <martin@catalyst.net.nz> writes:
>
> I'm preparing a better patch based on your comments, but File::Find is
> _not_ my friend, really. I really feel stupid after failing for 1hr to
> use it.
Something like this?
    use strict;
    use File::Find qw(find);
    my $git_dir = $ENV{GIT_DIR} || '.git';
    my @tagfiles = ();
    find({
        follow => 1,
        wanted => sub {
            if (-f _) {
                push @tagfiles, $File::Find::name;
            }
        } }, "$git_dir/refs/tags");
    for (@tagfiles) {
        print "$_\n";
    }
Show 5 quoted lines
>> BTW, wouldn't it be easier for this particular script, and more
>> useful in general, if something like what 'git-rev-parse' does
>> for commit objects when given "REV^0" is supported for tags?
>
> I don't quite follow...
What I meant is this.

There is an existing notation "^0" which is a postfix "dereference until you get a commit" operator.

	git-rev-parse --verify refs/tags/v0.99^0
	git-cat-file -t refs/tags/v0.99^0
does:
	1. reads SHA1 from "refs/tags/v0.99", finds the object;
        2. if it is a tag object, find the object pointed by it;
           if the result is still a tag object, then dereference
           it repeatedly;
        3. if the resulting object is a commit, let the caller
           to use it; otherwise barf.

What _might_ be useful for your application is a similar operator, say, "refs/tags/junio-gpg-pub%", that does:

	1. reads SHA1 from "refs/tags/junio-gpg-pub", finds the
           object;
        2. if it is a tag object, find the object pointed by it;
           if the result is still a tag object, then dereference
           it repeatedly;
	3. do not worry about the type of the result.  Just
           output it.

Instead of reserving yet another letter '%', it might be better to use something like "refs/tags/junio-gpg-pub^{tag}" as a notation for this. If you had something like this, you would not have to read tag objects yourself and dereference them by hand.

← back to recent threads