Perforce Plugin

Download

An Updated version is now available at Sublime Plugins – Perforce

First

What is Sublime Text 2 and what does the Perforce Plugin do?

Sublime Text 2 is a light weight text editor that is really fast and extendable. At the moment, I use it to edit PHP, Python, HTML, CSS and JavaScript files. There is a lot of community support to provide plugins (programmed in Python) so the editor keeps improving.

Trying to do my part, I did a small Perforce integration plugin. Right now it takes for granted that you have an open ticket with your Perforce server, otherwise the actions will simply fail. The plugin is still work in progress but I try to work on it daily to add features here and there.

Getting Started

Getting Sublime Text 2

At the time of writing, Sublime Text 2 is “free” (a pop up will appear every now and then to suggest you buy a license) or available for pretty cheap, 59$. For such a great editor, I highly recommend buying it to support the developer. Download

Getting Perforce Plugin

My plugin is available in a couple of ways, but here’s my recommendations:

  • Download and install Package Control for Sublime Text 2
  • Open the command window (ctrl+shift+p in the default install)
  • Select Package Control: Install Package
  • Type “perforce”, press enter

or

  • Browse to GitHub for my Sublime Text 2 Perforce Plugin repository
  • Download the Zip File
  • Locate your “roaming app data” folder, on Vista (old laptop, I know…) it’s located here: C:\Users\*your username*\AppData\Roaming
  • Browse to Sublime Text 2\Packages
  • Create a Perforce folder
  • Copy the content of the folder ericmartel-Sublime-Text-2-Perforce-Plugin-?????? in the newly created Perforce folder so that Perforce.py resides in the root of Sublime Text2\Packages\Perforce

Features

Event Handling

The plugin has two commands that react to events related to saving files.

Auto Add

When the plugin detects that you’re trying to save a new file (didn’t exist pre-save, exists post-save) and the file resides within your client root, it will automatically mark it for add.

You can disable this feature by changing the value of perforce_auto_add to false.

Auto Checkout

When the plugin detects that you’re modifying (perforce_auto_checkout_on_modified set to true) or you’re trying to save an existing file (exists pre-save – perforce_auto_checkout_on_save set to true) and the file resides within your client root, it will automatically try to checkout the file. For the moment there’s no handling of exclusive checkout or other conditions that could make the checkout fail.

You can disable this feature by changing the value of perforce_auto_checkout to false.

Text Commands

The text commands are all available through the command window (ctrl+shift+p in the default install). By typing perforce you can filter only the commands related to this plugin:

Filtered Perforce Text Commands

As you can see, you will even see the keyboard shortcuts assigned to these commands, as an example I assigned “ctrl+alt+x” to the Graphical Diff with Depot. Triggering the command from this window, from the Console (ctrl+`), from a menu item or from a keyboard shortcut will all result to the same thing.

These commands all redirect the p4 output to the Console window (ctrl+`) and a warning in some cases where the file in the view is not in the client root, if the perforce_warnings_enabled setting is set to true.

perforce_checkout

This command will attempt to checkout the file in the current view. Not much error handling is made. Use this command to manually control your checkout, if the perforce_auto_checkout setting is set to false.

perforce_add

This command will attempt to add the file in the current view. This is useful if the file was not created directly through Sublime Text 2 or if the perforce_auto_add setting is set to false.

perforce_rename

This command will prompt you for a new name for the file in the current view.

perforce_delete

This command will delete the file in the current view from Perforce.

perforce_revert

This command will attempt to revert the changes made to the file in the current view.

perforce_diff

This command outputs directly the result of a call to p4 diff with the file in view to the Console (ctrl+`). Recommended to more advanced users as the output is not really user-friendly.

perforce_graphical_diff_with_depot

This command calls p4diff and compares the local version of the current file with the version in the depot. I wish I could extend this to support Araxis Merge, but unfortunately I’m on sick leave for a couple of months and don’t feel like buying a license for my personal needs. Feel free to donate one and I’ll make the plugin configurable ;)

perforce_create_changelist

This command will create an empty changelist and prompt the user for a single line description.

perforce_add_line_to_changelist_description

This command lists all the available changelists and then lets the user type a line to be added to the description.

perforce_move_current_file_to_changelist

This command lists all the available changelists, adding “New” to the list. By selecting a changelist, the file will be moved to this changelist. If new is selected, the user is prompted to enter a description for the new changelist.

perforce_list_checked_out_files

This command lists all the files that are currently checked out sorted by changelist with part of the changelist description.

List of Checkedout Files

Here’s my current listing for a small web project I’m working on. Under the black square is the rest of the local path, but I didn’t want to make public the name of my project yet!

Menus

All the commands are easily available through the Tools menu (screenshot not updated):

What’s Next?

Please make suggestions. The plugin does what I need for now, I might polish it or update it depending on my usage but if you’d like something added, feel free to ask!

How you can contribute

I will gladly accept bug reports on GitHub and suggestions by email (or use the contact form on this page if you can’t find my email address – with all the spam I keep receiving recently it shouldn’t be hard to find).

I keep a changelog in the main file of this plugin so you will be credited for your contribution!

Also, please buy Sublime Text 2 to support its developer!

43 thoughts on “Perforce Plugin

  1. Jon says:

    Very nice! I’m going to give this a try right now.

  2. Jon says:

    I get an error: Perforce Plugin: p4 info didn’t supply a valid clientspec, launching p4 client.
    My p4 is already running and connected.

  3. Terry says:

    Hey, I’ve been waiting for a Perforce plugin. Thanks.

    Works well for me, but how about a Submit command?

  4. Eric says:

    Sounds like a good idea! I always checkin through P4Win but with the way the plugin is built it will be really easy to add the Submit command! Thanks for the input!

    Jon, I will take care of you situation, please reply to my mail with your OS, p4 client and “p4 info” output, it will help me debug the situation :)

  5. Ben says:

    Hey, I really love this plugin but I am having some problems trying to figure out where I set perforce_auto_checkout_on_modified to true. Could you provide some extra information on where to set this, thanks.

  6. Ben says:

    I figured out where to set the perforce_auto_checkout_on_modified to true, I guess I should have looked a bit harder before posting.

    Thanks for the awsome plugin!

  7. Dan says:

    Unfortunately, you don’t currently set the CWD consistently when you call p4 through the command line, which is required for p4 to pick up p4config.txt files, which are a fairly standard way of working with many branches and clientspecs:
    http://kb.perforce.com/article/1194/using-multiple-workspaces-with-p4exp

    The alternative is mapping most of //depot/ into an uber clientspec, which is terrible for your dbhave and p4 server performance if you deal with many large branches.

    Perhaps something like this:
    http://yuji.wordpress.com/2011/07/13/sublime-text-2-beta-project-specific-settings/

    could be used to find a p4config for the current sublime project, if the CWD cannot be set to the folder of a particular file?

    Many thanks!

  8. Grant says:

    @Dan: That’s the problem I was just running into as well. I use the P4CONFIG environment variable for all of my projects to select the proper workspace, but this plugin isn’t picking up my p4config files. I hope this gets fixed soon. This plugin shows a lot of promise, but unfortunately is not usable for me yet.

  9. Eric says:

    I’ll be spending the day looking at it tomorrow, but according to this http://www.perforce.com/perforce/doc.current/manuals/cmdref/env.P4CONFIG.html p4 should already be looking for the file you set, the plugin only calls p4 with the operation parameters, nothing specifying the user, the server, the port, etc…

    Thanks for your feedback guys!

  10. Eric says:

    Ahh I know… I’m pretty sure I found the problem, should be fixed soon, sorry for the inconvenience!

    PS.: cwd=None :(

  11. gotan says:

    ok, I dont get it…. where do I put my perfoce login information? I always get :
    Perforce: File is not under the client root. or
    Perforce:Myfile.js – file(s) not opened on this client.
    I clearly missed something…

  12. Eric says:

    Hi Gotan,

    Which OS are you using? You can either use P4V or P4Win to open a perforce session or I guess you could use p4 login to login manually (http://www.perforce.com/perforce/doc.current/manuals/cmdref/login.html)

    HTH

  13. gotan says:

    I am using Windows7 and P4V which is running and working paralell. I am new to perforce but your plugin will need somekind of information to perform any actions…?
    I bet this really sounds stupid but I am abit confused here :)
    cheers.

  14. GaryM says:

    I’m having same issue — Win7 with P4V running parallel, I get the same error “Perforce: File is not under the client root.” when I try to checkout.

    If I try to Add or Diff, I get “Perforce [warning]: ‘p4′ is not recognized as an internal or external comman, operable program or batch file.

    If I try to “login”, it asks for password but entering correct or incorrect or empty password results in same — nothing, no indicator of successful login, or logout…

    The files I’m opening are in the local P4 workspace. Any ideas on how to get this working? I’d like to finalize a migration from Aptana to Sublime, thanks…
    Gary

  15. Eric says:

    Gary, it appears that your P4 installation didn’t set up the environment variables… first start by adding your installation folder the the PATH environment variable.

    You can verify in “regedit” that your Perforce Environment is set up properly by looking at the key HKEY_CURRENT_USER\Software\Perforce\Environment. “p4 client” will allow you to set up properly the clientspec you want to use and should be recognized both by Perforce and by this plugin (this plugin only calls perforce commands, there’s nothing “proprietary”)

  16. GaryM says:

    Thanks, Eric. You said “first start by adding your installation folder the the PATH environment variable”. I actually don’t understand the instruction.

    I looked up the available perforce environment variables per the perforce docs (http://www.perforce.com/perforce/doc.current/manuals/cmdref/_env.html) and the only PATH related variable is “P4CLIENTPATH”. It’s considered an ESOTERIC VARIABLE and its for the workspace path, nonetheless I created it and set the path to my workspace root path.

    Secondly, I added P4CLIENT to confirm that it was set correctly (the default of computer name was actually correct as well). Retried and get all the same errors.

    I don’t see where to add the installation folder to the PATH environment variable. Any guidance on what you mean?

  17. gotan says:

    Eric,

    I added the installation folder to the PATH Variable, and I still get “file(s) not opened on this client.”
    I checked
    HKEY_CURRENT_USER\Software\Perforce\Environment. P4PASSWD which is the password I typed in via your plugin login. It doesnt matter if there is a wrong password. P4 is still working.

  18. gotan says:

    I think you need more information to help me:
    I have P4V and VS2010 with a perforce plugin setup and working. I added the perforce installallation folder to the User variable PATH.
    Under HKEY_CURRENT_USER\Software\Perforce\Environment. there are the following entries:
    P4EDITOR,P4PASSWD,P4PLUGINOPTS,P4PORT,P4USER. the value of most of the were wrong so I added the correct value(for example P4Port just contains the port without path).
    I could not find P4CLIENT, but I was not able to add it also since I got the message that there is already a P4CLIENT somewhere.
    Now I get following error when I want to checkout a file via your plugin:
    Perforce Plugin: p4 info didn’t supply a valid clientspec, launching p4 client”

    any ideas?
    thanks for your help so far, really appreciated.
    cheers goTAN

  19. Seb Maynard says:

    Hiya,
    I just submitted a pull request to add support for specifying the P4 config variables P4USER, P4CLIENT, P4USER, P4PASSWD in Perforce.sublime-settings which should help people get connected :)
    Thanks

  20. Seb Maynard says:

    (by the way, this plugin is awesome. Thanks for the great work!)

  21. GaryM says:

    Okay, got it working somehow, probably via my reinstallation of perforce.

    Nonetheless, Eric, I now see what the PATH environment variables you were referring to — It’s in the WINDOWS SYSTEM environment variables, not perforce plugin or sublime.

    Gotan, if this is your issue too, this is where to add the installation path (not in the HKEY_:CURRENT_USER reg variables):
    - go to “Control Panel\System and Security\System”
    - go to “Advanced system settings”
    - click “Environment Variables” button
    - scroll down and EDIT the “Path” system variable
    - confirm path to perforce is there, separated with semi-colon from other paths — e.g. “;C:\Program Files (x86)\Perforce”

  22. gotan says:

    Thanks Gary, but I already added the installation path to the PATH variable. I still get
    “File is not under the client root.”
    I just want to know where this plugin gets its information so I can edit them…
    since I added almost everything a perforce plugin could need in HKEY_CURRENT_USER\Software\Perforce\Environment. _except_ the workspace (P4CLIENT).
    Since P4V is working and using those variables (P4PORT,P4USER … and so on) I think they are “stored” in a different place than your plugin is looking..
    any help?

  23. gotan says:

    ok, I got it working now… I added all environment variables via console which are P4EDITOR,P4CLIENT,P4PORT,P4USER.

    Thanks Eric, this plugin is a real timesaver :)

  24. Eric says:

    So, Gotan and Gary, both of you have the plugin working? :)

  25. GaryM says:

    Yes, thanks Eric. It’s working very nicely… thank you!
    Gary

  26. gotan says:

    Eric, well it was working…
    Atm it is not working anymore but it might be the problem, that I am trying to switch to another perforce account…
    But since the last success was more or less trial and error it would be nice if you could tell me _where_ exactly does your plugin get all the connection data from?
    I added those environment vars using cmd and the set command and apparently they were not persistant..
    I am sorry for my lack of windows knowledge but its just not my OS…

  27. Eric says:

    Hi Gotan!

    The “beauty” of the plugin is that it doesn’t do much other than calling p4, the command line perforce client. I just reverted a “3rd party” change that I didn’t fully test that might have had an impact on the plugin. If you experience problems, you could try restarting sublime text if you installed the plugin using the Package Manager.

    But to come back to your question, usually these settings will be stored in the registry on windows (HKEY_CURRENT_USER\Software\Perforce\Environment) or in p4config files (http://www.perforce.com/perforce/doc.current/manuals/cmdref/env.P4CONFIG.html#1040647). Maybe this link could be helpful? http://www.perforce.com/perforce/doc.current/manuals/cmdref/_env.html

    Hope this helps!

    Eric

  28. gotan says:

    yea thanks, I setup those environmental variable so it works now. I was just confused since the perforce client P4V is not using those environmental varbles nor those entries in the regestry. So I guess I could have use the P4config somehow but I am fine for now :)
    I just have 2 things: I sometimes get the sublime text message, that your plugin takes too much time (in most cases when I wanna save a not checked out file) and I cannot submit or add a line to a changelist description(Perforce:There are no changelists to list).
    Apart from that you plugin is really awesome … love it :)
    thanks man/

  29. HR says:

    How about commands to show the current file in the depot tree in perforce visual client? Thanks

  30. Matt says:

    I am having the same issue as gotan I think. On Windows 7 with P4V running and open I can perform all commands except for ‘submit’. On ‘submit’ I always get an error “Perforce: There are no changelists to list.” In this case, I can still view checked out files and it knows the files are in my ‘default’ changelist.

    Please let me know if there is any further info I can provide to debug this – your plugin is awesome and has allowed me to move away from Eclipse! Thanks :D

  31. Eric says:

    Hi Matt!

    Thanks for the feedback! I’ll contact you directly to try to resolve the problem!

  32. Jonathan says:

    This does not seem to be working for me in OS X 10.7.4. I threw some print statements in Perforce.py to see what was happening and it looks like the “source .bash_profile” is added to the command, however it is not actually affecting the path.

    [print command] source ~/.bash_profile && echo $PATH && p4 info
    [print result] /usr/bin:/bin:/usr/sbin:/sbin

    Perforce [warning]: /bin/sh: p4: command not found
    Perforce [warning]: File is not under the client root

    I also modified WarnUser as below to always print warnings to the console:
    if(perforce_settings.get(‘perforce_log_warnings_to_status’)):
    sublime.status_message(“Perforce [warning]: ” + message)
    print “Perforce [warning]: ” + message

  33. Eric says:

    Jonathan, have you tried following the advices on this page? http://www.ericmartel.com/2012/02/26/mac-osx-p4-command-line-and-sublime-text-2/

  34. Uglycoyote says:

    Unfortunately, the Perforce server was set up at my company as being case-sensitive, so I find that this sublime perforce plugin fails to check out my files because it passes the file path with the wrong case. The capitalization on my hard drive is consistent with that in the Perforce depot, but for some reason the path capitalization in that the perforce plugin is getting is inconsistent with what’s on my file system. Any thoughts on a remedy there?

  35. Eric says:

    There’s a fix that I had to do, so I’ll do it tonight and add an option keep the capitalization of the paths, hopefully that will solve your problem! :)

  36. Santosh says:

    Eric, this is a great plugin. I am used to similar plugin in Textmate. I am trying to migrate form Textmate to Sublime Text 2. I installed the perforce plugin, but seems like I am experiencing the same issue that @Dan and @Grant had. You mentioned that you had fix for it related to PWD. Am I missing anything here. I am on Mac.

  37. Eric says:

    Hi Santosh! Did you check http://www.ericmartel.com/2012/02/26/mac-osx-p4-command-line-and-sublime-text-2/ ? I don’t own a mac so it’s harder for me to debug but hopefully this thread will help?

  38. John says:

    Thanks for the great plugin! It works great for me, except I have a “null” client root. This is easily fixed by adding a check in the IsFolderUnderClientRoot and returning 1 if the root is “null”, since every folder is available.

  39. Javier Lorenzana says:

    Hi, Eric.

    I’m switching to Sublime 2 from Scribes, and trying to incorporate your plugin as opposed to using the P4 CLI with a bunch of personal scripts, and file:// links on the Gnome Terminal.

    I was having the same “file is not under client root” issue until I figured out that, for some reason, the subprocess module wasn’t inheriting the environment variables I source through some Zsh configuration files.

    Curiously enough, I tried .zshenv, which is supposed to be run for absolutely all terminals, and still nothing.

    I ended up just adding P4CONFIG to /etc/environment, which is a pretty ugly hack, but as sure as sugar is sweet works.

    I’d like to move that variable into my user configuration, but I have no idea where to put it in, and have little time to test it. If you have any idea of how Sublime might be building its environment, help would be welcome.

    Cheers – and thanks.

  40. Gordon McGregor says:

    Just started trying to use the Perforce integration (and new to Sublime Edit too)

    I get an error:

    Exception in thread Thread-3:
    Traceback (most recent call last):
    File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py”, line 532, in __bootstrap_inner
    self.run()
    File “./Perforce.py”, line 639, in run
    File “./Perforce.py”, line 616, in MakeCheckedOutFileList
    File “./Perforce.py”, line 590, in MakeFileListFromChangelist
    File “./Perforce.py”, line 87, in GetUserFromClientspec
    File “./Perforce.py”, line 244, in WarnUser
    RuntimeError: Must call on main thread, consider using sublime.set_timeout(function, timeout)

    When I try to list the Perforce files.

    Now I know why this is (p4 isn’t in my default path) so I can fix it, but it seems there’s a problem in how the warnings are being issues from Perforce.py because it is the routine below that’s generating the RuntimeError exception, when it tries to do the WarnUser

    def GetUserFromClientspec():
    command = ConstructCommand(‘p4 info’)
    p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=global_folder, shell=True)
    result, err = p.communicate()

    if(err):
    WarnUser(“usererr ” + err.strip())
    return -1

    Makes it tricky to debug, unless you know enough Python to follow the traceback

  41. Akos says:

    Hi Eric,

    Thanks for the plugin! Do you have any plans to update it for Sublime Text 3?

    Thanks, Akos

  42. Eric says:

    Hi Akos

    I’m about to update (probably this weekend) a version on Git Hub (buffer management is broken in the current version)

    Thanks

  43. Akos says:

    Sounds perfect :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>