Perforce: How to find the original number of a change list - perforce

In perforce changelists get renumbered on submission. So for e.g. when the changelist was created it would be numbered 777 , but on submission of changelist it would get renumbered to say 790.
My question is how do I get the new CL number (790) , if I know the old CL number 777 , or vice versa ?

If you really want the original changelist number, that can be retrieved from Perforce without having to embed the original changelist number in the description. You can use the -ztag command line option to get at it. And you can only get at it through the 'changes' command (as far as I know):
d:\sandbox>p4 submit -c 24510
Submitting change 24510.
Locking 1 files ...
edit //depot/testfile.txt#2
Change 24510 renamed change 24512 and submitted.
d:\sandbox>p4 -ztag changes -m1 //depot/testfile.txt
... change 24512
... time 1294249178
... user test.user
... client client-test.user
... status submitted
... oldChange 24510
... desc <enter description here>
<saved
As pointed out, it's probably not that useful. However, I did want to note that it's possible to get at it.

The only way I can think of is adding the original changelist number as part of the changelist description field. First, you'll need a script to store the original changelist number:
#!/bin/env perl
$id = $ARGV[0];
open (CHANGE_IN, "p4 change -o $id|");
open (CHANGE_OUT, "|p4 change -i $id");
while (<CHANGE_IN>)
{
if (/^Description:/ and not /ORIGID/)
{
s/(^Description:)(.*)$/$1 ORIGID $id. $2/;
}
print CHANGE_OUT $_;
}
close (CHANGE_IN);
close (CHANGE_OUT);
Save this as origid.pl on the Perforce server with the executable bit set. Then setup a trigger with p4 triggers.
Triggers:
add_origid change-submit //depot/... /usr/bin/origid.pl %change%

Version 2012.1 of Perforce introduced the -O (capital oh) argument to p4 describe, which allows you to query a changelist by its original number (before being renumbered by p4 submit).
I find this very helpful, since I often find myself keeping notes about a changeset before it is submitted, then forgetting to note what it was renumbered to on submission.
So if I have a note talking about change 12300, I can now see what it refers to by typing:
p4 describe -s -O 12300
and having Perforce tell me:
Change 12345 by me#myhost on 2013/10/31 00:00:00
Fix that thing I wrote that note about
Affected files ...
... //Proj/MAIN/foo.c
The ztag method mentioned earlier can be used to find the old changelist number of a submitted change:
> p4 -ztag describe -s 12345 | grep oldChange
... oldChange 12300

Adding to Eric Miller's reply, because I can't comment (not enough points):
to just emit the 1 number
p4 -ztag describe $ORIG | sed -e 's/^\.\.\. oldChange //;t-ok;d;:-ok'
e.g.
OLD=$(p4 -ztag describe $ORIG | sed -e 's/^\.\.\. oldChange //;t-ok;d;:-ok')
or if you want to look up many numbers, this will output a map of new old on each line (may be useful with the "join" command). If there is no old commit, then it re-emits the new commit.
p4 -ztag describe 782546 782547 ... | sed -e '${x;p};s/^\.\.\. change //;t-keep;b-next;:-keep;x;/./p;g;G;s/\n/ /;x;d;:-next;s/^\.\.\. oldChange //;t-ok;d;:-ok;H;x;s/ .*\n/ /;x;d;'
I tried to avoid using GNU extensions to sed.

Unless you do something like Tim suggests the old change list number will be lost on submission.
Change list numbers are only temporary until you actually submit. So if you create a new change list (777 say) and then decide to delete it, the next change list you create will be 778.

It can be a bit more elegant if you use the P4 Python module.
i.e.
import P4
p4 = P4.P4()
p4.connect() # having a valid p4 workspace/connection is on you :)
c = p4.run_describe('969696') # describe a Submitted, renumbered changelist, i.e. 969696
old_pending_cl_number = c['oldChange'] # print out prior/pending CL# if this exists.
Cheers

Related

Given the Function Name, how to find the Changelist in perforce when that function was added?

I am looking for a way to find the changelist in perforce if I have the Function name and File in which that function is present ?
For Eg: if "A.cpp" has function added "void b()" via changelist 1234
I would like to get back changelist 1234 by giving the Function name only or both function name and file name. I was thinking to use python script for this but not sure how to proceed.
Use p4 annotate, e.g.:
p4 annotate -c A.cpp | grep "void b()"
Check out p4 help annotate for the options available -- you can use the -I flag to find the origin changelist if the line was added by a merge, you can use the -a flag to find text in earlier versions of the file, etc.

How should be paths to p4 sync -L formatted

I'm trying to figure out what is correct syntax for p4 sync -L. When I try p4 sync -L //one/of/my/files it complains that revision is not specified while if I try p4 sync -L //one/of/my/files#1234 it complains that # and # are illegal.
Well, the documentation says:
a list of valid file arguments in full depot syntax with a valid revision number
Revision numbers are #1, #17, etc. The # syntax is used to reference a label name, client name, or changelist number, which are not revision numbers.
For more details on this, check [p4 help revisions][2]; as it says, you want:
`file#n Revision specifier: The nth revision of file.`
So, specify //one/of/my/files#17, assuming that revision 17 is the revision of that file that you want to sync.
To see the revisions of your files, use p4 filelog //one/of/my/files.

Getting the change number of the first revision of a file in Perforce?

Is there a way to retrieve the change of the first revision of a file in Perforce? Sometimes I want to read the job description of the change that introduced a file, in order to learn what the file does, as well as to find related CRs for more information. I'm looking for something in the command line, e.g.
p4 <something> <file>
Ah, never mind, I found the subcommand I needed, p4 fstat, just after posting the question.
$ p4 fstat cama_Preempt#1
... depotFile //depot/MMA/products/CAMA/main/src/testScripts/cama_Preempt
... clientFile /ext1/acheong/CAMA/main/src/testScripts/cama_Preempt
... isMapped
... headAction add
... headType text
... headTime 1174070670
... headRev 1
... headChange 168703
... headModTime 1174070479
... haveRev 1
The headChange field shows the change number.

How to find (grep) text for files in a perforce changelist?

How to grep/find for a particular text in all files within a pending changelist?
My use case:
I have a debug_flag in my code and would want to make sure I do not check-in any code with the debug_flag which will cause a compiler error for all others. (Not for me since I have the debug_flag declared locally)
p4 describe changelist# gives you the list of the files in a changelist, but it has some extra information, and the paths are with respect to the depo. Example:
p4 describe 12334
output:
Change 12334 by me on 2014/01/04 00:57:08 pending
Some test changelist
Affected files ...
... //depot/path/to/my/files/file1#15 edit
... //depot/path/to/my/files/file2#12 edit
With a few search/replace or a simple perls script, you can change this text output to a list of files with actual path and then run grep on them:
xargs grep "debug_flag" < file_list.txt

How do you quickly find/delete all empty changelists in Perforce?

I was wondering what could be the point in trying to delete committed changelists, because a committed changelist is not supposed to be empty.
But then I am playing with the tutorial depot, and using the obliterate command on a whole branch, I can see there are situation where you can end up with empty committed changelists (that need deletion with the -f flag).
However, I don't know how to find them with the command line, as I don't know how to look for changelists with no files associated with.
Is there an easy way to do that ?
Thanks,
Thomas
Ah !
I should have browse more documentation before asking this...
http://public.perforce.com/wiki/Perforce_Command_Line_Recipes
Description: Delete all empty submitted changelists.
Shell command: p4 changes -s submitted | cut -d " " -f 2 | xargs -n1 p4 change -d -f
Powershell: p4 changes -s submitted | %{p4 change -d -f $_.split()[1]}
px: px -F %change% changes -s submitted | px -X- change -d -f
Contributors: Sam Stafford, Philip Kania, Shawn Hladky
Duh.
Thomas
To simply find all empty submitted changelists without deleting them, you can compare the output of these two commands:
p4 changes -s submitted - all changelists
p4 changes -s submitted //... - all changelists with associated files
In Windows PowerShell, for example, run
diff -ReferenceObject (p4 changes -s submitted) -DifferenceObject (p4 changes -s submitted //...)
As I am on Windows, I have created a little script doing the exact same thing in PERL, rather than Shell, powershell or px :) :
#*******************************************************************************
# Module: delete_empty_changelist.pl
# Purpose: A script to delete empty changelist
#
#list = `p4 changes -s submitted`;
foreach $chg (#list)
{
$chgnbr = (split /\s+/, $chg)[1];
print `p4 change -d -f $chgnbr`;
}
exit 0;
Note that in fact, in all cases, it's not a very clever script: It tries to delete absolutely every submitted changelist, and is prevented by perforce to do so, because if files are associated with it, you will get an error.
I suppose the result of the script should be sent to a log, and parse, so that only the relevant lines are highlighted.
Running the script will produce an output similar to:
Change 857 has 1 files associated with it and can't be deleted.
Change 856 has 1 fixes associated with it and can't be deleted.
Change 855 has 1 fixes associated with it and can't be deleted.
Change 854 deleted.
Change 853 has 1 fixes associated with it and can't be deleted.
Change 852 has 8 files associated with it and can't be deleted.
Change 851 has 1 files associated with it and can't be deleted.
Change 850 has 2 files associated with it and can't be deleted.
Change 849 has 2 files associated with it and can't be deleted.
Change 846 deleted.
Change 845 has 2 files associated with it and can't be deleted.
Cheers,
Thomas
Here is a DOS CMD only version. Just replace %p4streamsUser%.
for /f "tokens=* delims=" %%i in ('p4 changes -u %p4streamsUser% -s pending') do (
for /f "tokens=1-7*" %%a in ("%%i") do (
echo Deleting CL %%b %%h %%f
p4 change -d -f %%b
)
)
I'm on a Windows 7 machine. This will work on several other versions of windows/DOS.

Resources