# git tag --contains <commit> -n=1 ?

4 messages from 2009-12-23 to 2009-12-24. Participants: NODA, Kai, Andreas Schwab, Jeff King.
Thread: https://gitlist.dev/t/22015

## NODA, Kai, 2009-12-23 16:19

Subject: git tag --contains <commit> -n=1 ?
Message-ID: <4B324327.5010809@gmail.com>
URL: https://gitlist.dev/e/4B324327.5010809%40gmail.com

```
Hi,
I'm thinking how to find the oldest tag containing a specified commit.
Eg.
$ cd /usr/src/linux-linus/Documentation
$ for i in *.txt; do
for> echo $i
for> hash=`git log -1 --format=format:%H -- $i`
for> git tag --contains $hash -l 'v2.6.[0-9][0-9]'|head -1
for> done
DMA-API.txt
v2.6.31
DMA-ISA-LPC.txt
v2.6.20
DMA-attributes.txt
v2.6.27
...

Here I wonder whether "head -1" is generally correct or not when I want
the oldest tag.
Moreover, as "git tag --contains ..." takes considerable time, I will
be happy if I can set the maximum number in searching tags containing
a commit.  Or are there already some (better) ways to achieve this?

Any advice is welcome.

Thanks,
Kai

```

## Andreas Schwab, 2009-12-23 17:19

Subject: Re: git tag --contains <commit> -n=1 ?
Message-ID: <m2fx71pq0p.fsf@whitebox.home>
URL: https://gitlist.dev/e/m2fx71pq0p.fsf%40whitebox.home
In-Reply-To: <4B324327.5010809@gmail.com>

```
"NODA, Kai" <nodakai@gmail.com> writes:

> Here I wonder whether "head -1" is generally correct or not when I want
> the oldest tag.

Since the output of git tag is sorted by name, generally not.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

```

## NODA, Kai, 2009-12-23 20:37

Subject: Re: git tag --contains <commit> -n=1 ?
Message-ID: <4B327F8F.2060106@gmail.com>
URL: https://gitlist.dev/e/4B327F8F.2060106%40gmail.com
In-Reply-To: <m2fx71pq0p.fsf@whitebox.home>

```
Thank you for your reply, Andreas

Andreas Schwab wrote:
...
>> Here I wonder whether "head -1" is generally correct or not when I want
>> the oldest tag.
> 
> Since the output of git tag is sorted by name, generally not.

Wow, I didn't know that.
But then, under the assumption that tags have names like verNNN,
that behavior ensures me that "head -1" works as intended.

Maybe I look at its implementation ( refs.c:do_for_each_ref , right?)
but this seems a tough code to comprehend, especially around
packed/loose/extra ...

Thanks,
Kai

```

## Jeff King, 2009-12-24 05:52

Subject: Re: git tag --contains <commit> -n=1 ?
Message-ID: <20091224055257.GA419@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20091224055257.GA419%40sigill.intra.peff.net
In-Reply-To: <4B327F8F.2060106@gmail.com>

```
On Thu, Dec 24, 2009 at 05:37:35AM +0900, NODA, Kai wrote:

> >Since the output of git tag is sorted by name, generally not.
> 
> Wow, I didn't know that.
> But then, under the assumption that tags have names like verNNN,
> that behavior ensures me that "head -1" works as intended.

If that assumption does not hold, you can also sort by date. See:

  http://article.gmane.org/gmane.comp.version-control.git/133586

-Peff

```
