3 # Copyright (c) 2005 Martin Langhoff
5 # Walk the tags and find if they match a commit
6 # expects a SHA1 of a commit. Option -t enables
15 my $git_dir = $ENV{GIT_DIR
} || '.git';
16 $git_dir =~ s
|/$||; # chomp trailing slash
20 getopts
("t") || usage
();
22 my @tagfiles = `find $git_dir/refs/tags -follow -type f`; # haystack
23 my $target = shift @ARGV; # needle
28 # drive the processing from the find hook
29 # slower, safer (?) than the find utility
30 find
( { wanted
=> \
&process
,
33 }, "$git_dir/refs/tags");
37 my ($dev,$ino,$mode,$nlink,$uid,$gid);
39 # process only regular files
40 unless ((($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -f _
) {
41 return 1; # ignored anyway
46 my $tagname = substr($tagfile, length($git_dir.'/refs/tags/'));
48 my $tagid = quickread
($tagfile);
51 # is it just a soft tag?
52 if ($tagid eq $target) {
54 return 1; # done with this tag
57 # grab the first 2 lines (the whole tag could be large)
58 my $tagobj = `git-cat-file tag $tagid | head -n2 `;
59 if ($tagobj =~ m/^type commit$/m) { # only deal with commits
61 if ($tagobj =~ m/^object $target$/m) { # match on the commit
64 } elsif ( $opt_t && # follow the commit
65 $tagobj =~ m/^object (\S+)$/m) { # and try to match trees
67 my $commitobj = `git-cat-file commit $commitid | head -n1`;
69 $commitobj =~ m/^tree (\S+)$/;
71 if ($target eq $treeid) {
80 local $/; # undef: slurp mode
82 or die "Cannot open $file : $!";
90 Usage: ${\basename $0} # find tags for a commit or tree
91 [ -t ] <commit-or-tree-sha1>