1

I get a list of labels when I type in the following command in Linux:

p4 labels -e '< pattern-for-required-label >'

But I want to grab only the latest label in the list. If there any command in perforce through which I can select the latest label from the long list?

sarnold
  • 102,305
  • 22
  • 181
  • 238
iDev
  • 2,163
  • 10
  • 39
  • 64

4 Answers4

1

p4 labels -e ''|head(or "|tail", depending on whether you want to see the top or bottom of the list).

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • That does not help, even if I use head or tail, I still get a bunch of labels as output – iDev Jun 29 '12 at 23:03
  • OK, looking at the ["man" page](http://www.perforce.com/perforce/doc.current/manuals/cmdref/labels.html), I gotta ask: does `p4 labels|head` (no "-e") work? Does `p4 labels|tail -25` show only the last 25 lines? If not, does `p4 labels > /tmp/tmp.txt` show NOTHING, but redirect to "/tmp/tmp.txt"??? – paulsm4 Jun 30 '12 at 02:53
  • @paulsm4 can u give above command `os independent`. What will be the generic command for getting latest label ? I tried `p4 labels -E "pattern" -m 1` but this gives oldest label – Patrick Sep 09 '14 at 14:50
  • It seems `p4 labels` returns labels sorted alphabetically rather by date. There doesn't seem to be an option to sort by date according to [documentation](https://www.perforce.com/perforce/r16.2/manuals/cmdref/p4_labels.html). Your best bet is @Garrett Waiss 's answer: `p4 labels | sort -k3` – andreycpp Mar 23 '17 at 23:22
1

You can do this with sort command:

p4 labels <branch> | sort -k3

The third column is the date. This has plagued me for months and just finally had the aha moment.

Zulu
  • 8,765
  • 9
  • 49
  • 56
0

To limit the number of labels returned, you can use the -m option:

p4 labels -m 1 -e 'your pattern'

This will only return a single label. Also of note, many of the p4 commands will take the -m parameter to control the number of results that are returned. One more thing: the -E version of the pattern parameter makes the filter pattern case insensitive if you desire/need that.

Reference: http://www.perforce.com/perforce/doc.current/manuals/cmdref/labels.html

If the p4 labels command isn't getting you what you desire, I would suggest taking a look at the Perl, Python and Ruby APIs provided by perforce - these are all supported libraries by the staff at Perforce, and quite capable of interfacing your custom code needs.

http://www.perforce.com/product/components/apis

Goyuix
  • 23,614
  • 14
  • 84
  • 128
  • There are some labels which are test cases. I cant really grab the one I require in the list. Any idea if it could be done through git? – iDev Jun 30 '12 at 00:59
  • Something like git describe --tags – iDev Jun 30 '12 at 00:59
  • What are you really trying to accomplish - it seems like maybe the labels approach isn't the right one? Really the best use of labels is to pull together a set of files across one or more changelists. There might be a better way to achieve what you are looking to do. – Goyuix Jun 30 '12 at 03:41
  • When you say 'latest' label, do you mean latest in the list of labels, or latest to be updated/accessed? The -m1 option gives you the first label in the set that matches the -e filter, but that is not necessarily the most recently used label. (There is no way, as far as I know, to get Perforce to sort the list of labels by date.) – user1054341 Jun 30 '12 at 15:10
0

To complement @Garrett Waiss's and @Zulu's answer:

You can also tell p4 labels to output time using -t switch, and then sort by both date and time. Pipe this thru tail to get just the latest result as in your question:

p4 labels -t <expression> | sort -k3,3 -k4,4 | tail -n1

Also note:

  • some p4 client versions expect you to specify -e before label expression
  • if you're using a label naming scheme, for example mylabel_001, mylabel_002,... then you'd want expression to be mylabel_* i.e.: p4 labels -t -e mylabel_* | sort -k3,3 -k4,4 | tail -n1
andreycpp
  • 1,109
  • 9
  • 9