Perforce cheatsheet

Perforce is a version control system. p4 is its primary commandline tool. p4g is its GUI tool. The reference for all this is the p4 command reference.

  • To get help on any p4 command named foobar:
$ p4 help foobar
  • To list all depots:
$ p4 depots
  • To view info about your workspace:
$ p4 info
  • To list environment variables that P4 understands and their values:
$ p4 set
  • To view all clients of all users:
$ p4 clients
  • To view all clients of a particular user or yours:
$ p4 clients -u joe
  • To create or view or change the configuration of your current client or default client:
$ p4 client

You typically use this command to add new branches or paths from the depot to your directory view.

  • To create or view or change the configuration of a new client or existing client with a name:
$ p4 client my_client_2
  • To delete a client from the server:
$ p4 client -d my_client_2
  • To sync all the latest branches and paths of your client from the depot:
$ p4 sync
  • To list (and not actually sync) the unsynced files between workspace and depot:
$ p4 sync -n
  • To sync only a specific directory or file use its depot path:
$ p4 sync //dev/foo/some.cpp
  • To sync a specific changelist to your working directory:
$ p4 sync ...@123456

Changelist

p4 commands related to changelist are collected here.

Log

  • To view all the changelists from the server:
$ p4 changes

This will list all changelists of everybody, so it is pretty much useless. You want to filter it down using other criteria, like shown below.

  • To view all the changelists of a particular depot:
$ p4 changes //somedepot/
  • To view all the changelists of a particular branch in a depot:
$ p4 changes //somedepot/somebranch/
  • To view the most recent 5 changelists added to the server:
$ p4 changes -m5
  • To view the changelists added by a particular user:
$ p4 changes -u joe
  • By default, a single truncated line of information is shown for each changelist. To view the complete description of every changelist:
$ p4 changes -l
  • To list changelists, with each description limited to 250 characters:
$ p4 changes -L
  • To view the changelists of a particular status, say pending:
$ p4 changes -s pending
  • To view the changelist checked out in the current workspace:
$ p4 changes -m1 ...#have
  • To add a new file, either cause you want to create a new file or add an existing file to be tracked by version control:
$ p4 add foobar.txt

If it is a new file, then you will need to create the file yourself after this command. This command just indicates to p4 to start tracking such a file.

  • To delete a file:
$ p4 delete foobar.txt
  • To delete a file and record that to a particular changelist:
$ p4 delete -c 123456 foobar.txt
  • To open a file for editing:
$ p4 edit foobar.txt
  • To revert a specific file or all files in directory with versions from depot:
$ p4 revert foobar.txt
$ p4 revert ...
$ p4 revert src/...

Note that you will lose changed made your files on disk with this operation. This operation can be used after shelving a changelist to revert the files back to their original state.

  • To revert back only unchanged (but opened) files:
$ p4 revert -a
  • To view the currently changed files as seen by p4:
$ p4 opened
$ p4 opened ...
$ p4 opened src/...

File information

  • To view the changelist of a file:
$ p4 changes //somedepot/somepath/foobar.txt
  • To view the log of a file:
$ p4 filelog //somedepot/somepath/foobar.txt
  • To see a beautiful timelapse view of file history, it is better to view it in p4g by right-clicking the file and choosing Timelapse view. The color of the lines show their age and the slider can be used to move through history.

  • To print all the lines of a file:

$ p4 print //somedepot/somepath/foobar.txt
  • To print the revision numbers of every line of a file:
$ p4 annotate //somedepot/somepath/foobar.txt
  • To print changelists for every line of a file:
$ p4 annotate -c //somedepot/somepath/foobar.txt
  • To print the user who last changed every line of a file:
$ p4 annotate -u //somedepot/somepath/foobar.txt

Diff

  • To view the diff of all currently open files:
$ p4 diff

This prints a text diff at the shell by default. If you set the P4DIFF environment variable to a GUI program, like Meld, it will use that to show the diffs.

  • By default, the diff is shown in P4’s own format. To display in the unified diff format that is understood by most tools:
$ p4 diff -du
  • To view the diff of opened files that are actually changed:
$ p4 diff -sa
  • To diff two files in the depot:
$ p4 diff2 //branch1/some.cpp //branch2/some.cpp

This prints a diff at the shell. Set the P4DIFF2 environment variable to a GUI diff program to view the diff using that.

Branch

To create a new branch from an old branch, there are typically 3 steps:

First, create the branch:

$ p4 integrate //foo/old_branch/... //foo/new_branch/...

Now the branch is created, but you cannot yet view the files in the new branch in your filesystem.

Second, change the client specification add the new branch files to your view and sync:

$ p4 client
$ p4 sync

Finally, submit the new branch to depot:

$ p4 submit //foo/new_branch/...

Merge

  • To merge, resolve automatically and finally only resolve merge conflicts manually:
$ p4 integrate //foo/from_branch/... //foo/my_branch/...
$ p4 resolve -a
$ p4 resolve
  • After an integrate, show what files cannot be resolved automatically:
$ p4 resolve -n
  • Integrate a specific changelist 123456 from one branch to another:
$ p4 integrate from_branch/...@=123456 to_branch/...
$ p4 resolve to_branch/...
$ p4 change
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.