Perforce (p4) Command Line: Tips and Tricks

p4 is your friend

Perforce (p4) Command Line: Tips and Tricks

Tip #1: p4 is your friend

You can generate sophisticated change lists automatically according to custom rules from the shell script, e.g. "Add a bunch of files into a change list" or "Split a big changelist into little ones".

Quick start with p4

  1. Open p4v (visual Perforce client).
  2. Right click on the project folder.
  3. Click "Open Terminal".
  4. Now you can use p4 in a preconfigured console, you don't need to setup workspace and server connection.

Create a new change list

p4 change -o | p4 change -i
  • p4 change -o: default change list description
  • p4 change -i: create new change list

Create 20 new change lists

for i in {1..20}; do
  p4 change -o | p4 change -i;
done

Add, checkout (edit), delete and revert

  • Default change list:
    p4 add ./file
    p4 edit ./file
    p4 delete ./file
    p4 revert ./file
    
  • Custom change list:
    p4 add -c 12345678 ./file
    p4 edit -c 12345678 ./file
    p4 delete -c 12345678 ./file
    p4 revert -c 12345678 ./file
    

Add a bunch of files into a change list

Imagine situation, when you need to add 40000 files into a single changelist to create an initial commit.

cat ./files | p4 -x- add -c 12345678
  • -x is an option where to read the input, and -x- means to read the files from standard input.
  • -c is an option of add operation to specify a concrete change list number.

Split a big changelist into little ones

Now, perhaps, you need to create a 40 changelists with 1000 files in the each one. And perhaps you have a file "files" with 40000 lines inside one. Then:

split -l 1000 ./files

Output is "xaa", "xab", "xac" files, total count is 40 new files per 1000 lines.

Create 40 change lists:

for i in {1..40}; do
  p4 change -o | p4 change -i;
done

Write down change list numbers. For example, we've got numbers 9901, 9902, and so on, up to 9940.

Rename files into existing change list numbers:

mv xaa 9901
mv xab 9902
mv xac 9903
# ...
mv xbp 9940

Add files according to change lists:

for i in 99*; do
  cat {i} | p4 -x- add -c {i};
done

File list "9901" will be added to the 9901 change list
File list "9902" will be added to the 9902 change list.
And so on.

Print files

  1. In the local folder of workspace:
    p4 files "*"
    
  2. In the local folder recursively:
    p4 files "..."
    
  3. In the remote depot:
    p4 files "//Depot/..."
    

The latest revision of a file

p4 filelog -m1 ./file

Diff between two revisions

p4 diff2 -u file#3 file#5