<p>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.</p>

[{'answer_id': 47012, 'author': 'Greg Whitfield', 'author_id': 2102, 'author_profile': 'https://Stackoverflow.com/users/2102', 'pm_score': 5, 'selected': False, 'text': "<p>Just to answer this myself in keeping with Jeff's suggestion of using Stackoverflow as a place to keep technical snippets....</p>\n\n<p>From the command line use:</p>\n\n<pre><code>p4 changes -m1 @&lt;clientname&gt;\n</code></pre>\n\n<p>And just replace  with the name of your client spec. This will produce output of the form:</p>\n\n<pre><code>Change 12345 on 2008/08/21 by joebloggs@mainline-client '....top line of description...'\n</code></pre>\n\n<p>Which is easily parsed to extract the changelist number.</p>\n"}, {'answer_id': 47019, 'author': 'erickson', 'author_id': 3474, 'author_profile': 'https://Stackoverflow.com/users/3474', 'pm_score': 3, 'selected': False, 'text': "<p>For a serious build (one that is being prepared for testing), explicitly specify the desired label or changelist number, <em>sync to label,</em> and imbed it in build artifacts. </p>\n\n<p>If a changelist (or label) is not given, use <code>p4 counter change</code> to get the current change number, and record it. But you still need to <em>sync everything using that change number.</em></p>\n\n<p>I don't think you can achieve exactly what you want, because in general, an entire workspace isn't synced to a particular changelist number. One can explicitly sync some files to older revisions, and then a single changelist number is meaningless. That's why a fresh <code>sync</code> is required to ensure that a single changelist number accurately represents the code version.</p>\n\n<hr>\n\n<p>Regarding the comments: Yes, my answer is intended for use by configuration managers preparing a build to give to QA. Our developers don't normally sync as part of a build; they do a build prior to submitting&mdash;so that they can make sure their changes don't break the build or tests. In that context, we don't bother to embed a repository label.</p>\n\n<p>With your approach, you are making the assumption that your whole workspace was synced to head at the time of your last changelist submission, and that changelist included all of your open files. It's too easy to be mistaken in those assumptions, hard to detect, and horribly expensive in terms of lost time. On the other hand, solving the problem is easy, with no drawbacks. And because a changelist number can be explicitly specified, it doesn't matter what revision you need or how quickly the codebase is changing.</p>\n"}, {'answer_id': 256455, 'author': 'Syeberman', 'author_id': 14576, 'author_profile': 'https://Stackoverflow.com/users/14576', 'pm_score': 8, 'selected': True, 'text': '<p>I recommend the opposite for automatic build systems: you should first get the latest changelist from the server using:</p>\n<pre><code>p4 changes -s submitted -m1\n</code></pre>\n<p>then sync to that change and record it in the revision info.  The reason is as follows.  Although <a href="https://portal.perforce.com/s/article/3458" rel="nofollow noreferrer">Perforce recommends the following</a> to determine the changelist to which the workspace is synced:</p>\n<pre><code>p4 changes -m1 @clientname\n</code></pre>\n<p>they note a few gotchas:</p>\n<ul>\n<li>This only works if you have not submitted anything from the workspace in question.</li>\n<li>It is also possible that a client workspace is not synced to any specific changelist.</li>\n</ul>\n<p>and there\'s an additional gotcha they don\'t mention:</p>\n<ul>\n<li>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).</li>\n</ul>\n<p>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:</p>\n<pre><code>p4 sync -n @changelist_number\n</code></pre>\n'}, {'answer_id': 668631, 'author': 'eel ghEEz', 'author_id': 80772, 'author_profile': 'https://Stackoverflow.com/users/80772', 'pm_score': 4, 'selected': False, 'text': '<p>You may try finding the maximum change number in the output of the "p4 files" command.  The working directory should not contain post-sync commits, though.  This is just a tad better than </p>\n\n<pre><code>p4 changes -m1 "./...#have"\n</code></pre>\n\n<p>as the latter seems to run on the server and may fail on big source trees due to "MaxResults" limits.</p>\n\n<pre><code>$ p4 changes -m1 "./...#have"\nRequest too large (over 850000); see \'p4 help maxresults\'.\n\n$ p4 -G files "./...#have" | python c:/cygwin/usr/local/bin/p4lastchange.py\nFiles: 266948\n2427657\n</code></pre>\n\n<p>where p4lastchange.py is based on the code from the <a href="http://www.perforce.com/perforce/conferences/us/2005/presentations/GoldstonePresentation.pdf" rel="noreferrer">Using P4G.py From the Command Line</a> presentation by J.T.Goldstone, Kodak Information Network/Ofoto, April 15, 2005.</p>\n\n<pre><code>#! /usr/bin/env python\nimport sys, os, marshal\n\nif os.name == "nt":\n    # Disable newline translation in Windows.  Other operating systems do not\n    # translate file contents.\n    import msvcrt\n    msvcrt.setmode( sys.stdin.fileno(), os.O_BINARY )\n\nlastcl = 0\nnum = 0\ntry:\n    while 1:\n        dict = marshal.load(sys.stdin)\n        num = num + 1\n        for key in dict.keys():\n            # print "%s: %s" % (key,dict[key])\n            if key == "change":\n                cl = int(dict[key])\n                if cl &gt; lastcl:\n                    lastcl = cl\nexcept EOFError:\n    pass\nprint "Files: %s" % num\nprint lastcl\n</code></pre>\n'}, {'answer_id': 687228, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>The best I've found so far is to do your sync to whatever changelist you want to build and then use changes -m1 //...#have to get the current local changelist (revision).</p>\n\n<p>p4 sync @CHANGELIST_NUM\np4 changes -m1 //...#have | awk '{print $2}'</p>\n\n<p>Gives you the changelist number that you can the use wherever you want.  I am currently looking for a simpler way than p4 changes -m1 //...#have.</p>\n"}, {'answer_id': 10182886, 'author': 'gmaghera', 'author_id': 209849, 'author_profile': 'https://Stackoverflow.com/users/209849', 'pm_score': 3, 'selected': False, 'text': "<p>You could also use the cstat command:</p>\n\n<p>p4 help cstat</p>\n\n<pre><code>cstat -- Dump change/sync status for current client\n\np4 cstat [files...]\n\nLists changes that are needed, had or partially synced in the current\nclient. The output is returned in tagged format, similar to the fstat\ncommand.\n\nThe fields that cstat displays are:\n\n    change   changelist number\n    status   'have', 'need' or 'partial'\n</code></pre>\n"}, {'answer_id': 10937773, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>For the whole depot (not just your workspace/client)</p>\n\n<pre><code>p4 counter change\n</code></pre>\n\n<p>does the job, just telling the last changelist.</p>\n'}, {'answer_id': 14938631, 'author': 'Assaf Mendelson', 'author_id': 1547734, 'author_profile': 'https://Stackoverflow.com/users/1547734', 'pm_score': 0, 'selected': False, 'text': '<p>I am not sure if you got the answer you needed but I had a similar problem. The goal was to write in our logger the specific version of the project. The problem was that while we are making our own makefile, the overall build system is controlled by our configuration management. This means that all the solutions which say "sync to something then do something" don\'t really work and I didn\'t want to manually change the version whenever we commit (a sure source for errors). \nThe solution (which is actually hinted in some of the answers above) is this:\nin our makefile, I do p4 changes -m1 "./...#have"\nThe result for this is Change change_number on date by user@client \'msg\'\nI simply create the message into a string which is printed by the logger (the change number is the important element but the other is also useful to quickly decide if a certain version contains changes you know you made yourself without going to perforce to check).\nHope this helps.</p>\n'}, {'answer_id': 33069657, 'author': 'gsf', 'author_id': 1870790, 'author_profile': 'https://Stackoverflow.com/users/1870790', 'pm_score': 4, 'selected': False, 'text': '<p><code>p4 changes -m1 @clientname</code> which is the "recommended" way to do it for my client takes about 10 minutes</p>\n\n<p>this is what I use:</p>\n\n<pre><code>p4 cstat ...#have | grep change | awk \'$3 &gt; x { x = $3 };END { print x }\'\n</code></pre>\n\n<p>for the same client takes 2.1 seconds </p>\n'}, {'answer_id': 36795719, 'author': 'user31389', 'author_id': 2436687, 'author_profile': 'https://Stackoverflow.com/users/2436687', 'pm_score': 4, 'selected': False, 'text': "<p>If you are using P4V you can do this graphically:</p>\n\n<ul>\n<li>In the Dashboard tab (View->Dashboard) choose a folder and you will see a list of changelists that the folder isn't yet updated with. Note the lowest number (in the highest row).</li>\n<li>Make sure that in the Workspace Tree you have selected the same folder as previously in the Dashboard. Then go to the History tab (View->History) and scroll down to the number noted previously. The number just below that number is the number of your current changelist.</li>\n</ul>\n"}]
['https://Stackoverflow.com/questions/47007', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2102/']