Useful Perforce Commands Series: 1

I have recently started using perforce at my work place as our version control system. Before this  I have had experience using SVN on google code and GIT for github. Perforce is an enterprise level version control system which uses the concept of depot and workspaces on each perforce clients. Its very easy to set up and even comes with a GUI interface [Perforce Visual Client ] where you can easily see file histories, diffs and other code check-in details.

To read more about how perforce organizes files and changes you can read the official perforce introduction documentation at:

So here are some useful perforce commands:

1. Setup Perforce Workspace:

export P4PORT=<p4depotIP>:<PORT>
export P4USER=<username>
mkdir <workspace_folder>
cd <workspace_folder>
export P4CLIENT=<clientname>
p4 client

On doing p4 client a file will open in your default editor. In that file fill in the following details:

 //depot/<folderpath_to_sync>/... //<workspace_folder>/<folder_to_sync_to>/...

Now save the file and your client will be configured.

To sync your current folder with the depot code fire the following command:

p4 sync ...

If you feel that the sync was not done properly you can run p4 sync … again till it gives the message “there are no more files to sync”

Also if you want to force a file to sync again you can type:

p4 sync -f ...

2. Get to know your perforce clients:

 If you have multiple clients configured you can see the brief details of all your clients by typing

p4 clients -u &lt;username&gt;

To delete a client:

p4 client -d &lt;clientname&gt;

3. Edit/Add files for change:

All synced files in a perforce workspace are in readonly mode by default. So if you want to edit any file you should first add an edit status to it in the perforce workspace. This will notify the main depot that the file is opened for editing in your workspace which can be used by other users who are making changes to same file.

p4 edit &lt;file_name/relative_file_path&gt;

Similarly, you can add a newly created file by you to the perforce workspace, which again will notify the depot about the change.

p4 edit &lt;file_name/relative_file_path&gt;

4. Creating changelists:

All changes in perforce are saved as changelists. If you want to submit any of your changes to the main depot then you need to create a changelist number. All files which are opened for edit or are added are by default the part of the “default” changelist. If you have created the changelist you will see a number next to your file which will denote the changelist to which the file is added.

To see all the files opened on your workspace:

p4 opened ... # for all files opened for change in workspace.
//depot/&lt;folderpath_to_sync&gt;/&lt;relative_file_path&gt;#&lt;change_number&gt; - edit default change (ktext) #This file is a part of default changelist
//depot/&lt;folderpath_to_sync&gt;/&lt;relative_file_path&gt;#&lt;file_change_number&gt; - edit change &lt;changelist_number&gt;(ktext) #This file is a part of changelist with changelist number =&lt;changelist_number&gt;

To create a changelist:

p4 change #it will open a file where you need to fill the Description, Reviewer and BUGID for the change, Save the file and you will get the changelist number

To see all the changelists by a specific user/client:

p4 changes -u &lt;username&gt; #lists all changes by that user
p4 changes -c &lt;clientname&gt; #lists all changes by that client
p4 changes -c &lt;clientname&gt; -l #displays the changes by that client with their full description texts
p4 changes -c &lt;clientname&gt; -L #displays the changes by that client with their short description texts truncated to 250 words.
p4 changes -s &lt;fixed|opened|...&gt; -c &lt;clientname&gt; #displays all changes by the client which are

5. Submitting changes:

Once you have made all changes you need to submit it to the depot. The change submission usually follows the following process:

Viewing the change diff w.r.t. depot:

p4 diff ... # will present the diff of your change files with the recent files in the depot
#if you want to see the changes in vimdiff you need to make the p4diff editor to be vimdiff
export P4DIFF=vimdiff
p4 diff ... # will show the diff in vimdiff

Marking the bug fixed by the change:

p4 fix -s fixed -c &lt;changelist_number&gt; &lt;space separated list of BUGIDs&gt;

Submitting the change:

p4 submit -c &lt;changelist_number&gt; #usually if other people are working on the same file this command will result in an error

Resolve the changes & submit:

p4 resolve ... #This will present you will a prompt to resolve the changes. It will have options to either merge the changes or edit the file manually
p4 submit -c &lt;changelist_number&gt; #Submits your changelist with resolved changes
p4 sync ... #sync your workspace so that it reflects the recent changes in the depot

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your 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