Determining the last changelist synced to in Perforce

Greg Whitfield picture Greg Whitfield · Sep 6, 2008 · Viewed 82.1k times · Source

A question that occasionally arises is what is the best way to determine the changelist that you last synced to in Perforce. This is often needed for things like injecting the changelist number into the revision info by the automatic build system.

Answer

Syeberman picture Syeberman · Nov 2, 2008

I recommend the opposite for automatic build systems: you should first get the latest changelist from the server using:

p4 changes -s submitted -m1

then sync to that change and record it in the revision info. The reason is as follows. Although Perforce recommends the following to determine the changelist to which the workspace is synced:

p4 changes -m1 @clientname

they note a few gotchas:

  • This only works if you have not submitted anything from the workspace in question.
  • It is also possible that a client workspace is not synced to any specific changelist.

and there's an additional gotcha they don't mention:

  • If the highest changelist to which the sync occured strictly deleted files from the workspace, the next-highest changelist will be reported (unless it, too, strictly deleted files).

If you must sync first and record later, Perforce recommends running the following command to determine if you've been bit by the above gotchas; it should indicate nothing was synced or removed:

p4 sync -n @changelist_number