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!

71 thoughts on “Perforce Plugin

  1. IK says:

    Thank you for plugin, but I am having a trouble with plugin.

    I am using Ubuntu 64 bit and Sublime Text 3.(I tried this onto Sublime Text 2, same error encountered)
    P4 is under different directory, but it is executable on terminal without path

    I tried diff, it shows me “/bin/sh: p4: command not found
    So I tried the comment you posted here for Mac OS X, but no luck.
    I’ve checked $PATH, $P4_HOME, $P4USER, $P4CLIENT, $P4PORT, everything is all correctly set. I added these settings on .shell.

    Please help me I really want to use this.
    Thank you in advance

  2. Eric says:

    Hi!

    I will try creating a setup using Ubuntu and ST3 and see what’s happening! I validated it once ages ago, maybe something broke since then :(

    Thanks

  3. Darin says:

    How do I edit the settings for Auto Add and Auto Checkout? I installed your Perforce plugin using Package Control on ST3. I assumed there was a Sublime settings file, but I don’t see it.

  4. Darin says:

    I was able to modify the settings by pulling a settings file from GitHub and placing it in ~\AppData\Roaming\Sublime Text 3\Packages\User. That file was not created when installing through the ST3 Package Manager.

    I am experiencing significant typing lag when editing a file that has been checked out with your Perforce plug-in. I disabled all of the auto settings thinking the event checking might be slowing things down. Any other ideas?

  5. Eric says:

    Hi Darin,

    Yes sorry, that’s exactly how to provide a settings file.

    Unfortunately this version of the plugin is synchronous and doesn’t cache anything, so I suspect the on_modify is poking your Perforce server and it takes a while to respond?

    Eric

  6. Darin says:

    For some reason, something in your Perforce is still causing significant typing lag. I disabled the auto_checkout and auto_add, but is there still something else that the plugin is doing/polling in the background? It seems to me that after I checkout the file, the plugin would be idle until I tell it to do something.

  7. Eric says:

    That’s indeed what it “should” do! I wrote this over a year and a half ago so I’ll dig into the code to see what could be happening.

    It seems like the only other check is to see if the file is writable, this shouldn’t cause that much latency :(

  8. Darin says:

    When do you check if the file is writable? Is that with every input? For example, I can move the cursor with no lag at all. However, when I edit the file there is lag. I can then disable the plugin and immediately the lag goes away.

    In my environment, I’m editing files that are not stored locally but on a shared network drive. I’m guessing whatever is happening on_input with your plug-in is exaggerating the latency.

  9. Eric says:

    Check line 286 of the plugin, you could probably move the auto check out check before the writable check and it would solve your current issue!

    I’ll think about shared network drives for the next implementation of the plugin! Thanks

  10. Darin says:

    Yes! Working perfectly now. Just installed the plugin manually from your ST3 github version. Then I re-ordered the auto checks to only check file writability based on the settings. Thanks for the help.

  11. Obi-Wan says:

    Hi Eric!

    I would like to know if the following is possible with this plugin.

    An example:
    If one searches for a keyword the results page returns 100 files that contain that keyword.
    Is there a way to checkout all those files in one shot?

    Thank you in advance.

  12. Eric says:

    Hi Obi-Wan, sorry I didn’t see your message earlier!

    Take a look at p4 edit, it supports wildcards, you can probably use it directly for what you’re looking for: http://www.perforce.com/perforce/r12.1/manuals/cmdref/edit.html

    Eric

  13. Chris says:

    I’ve tried this with both Sublime 2 & 3 on OSX, both with the same results. Despite being able to interact with Perforce via command line to my hearts content, when I try to use this, I always get the error “File is not under client root.”

    The file IS under the client root, and doing a ‘p4 edit ‘ works fine.

  14. Eric says:

    Interesting! On OSX I’ve seen cases where the path didn’t match the case (it’s supposed to all be tested in lower case so it shouldn’t be a problem) and it was failing to find it under the client root. Would you mind sharing some of your “p4 info” with me by email? emartel at gmail

    Thanks!

  15. Robert Schmidt says:

    Hi Eric,
    Thank you for a great plugin!
    I have an issue with workspaces. I use multiple on the same PC, but the plugin seems to assume it should use the global default name (machine name or the P4CLIENT environment variable?).

    P4 command line tools traditionally support the P4CONFIG variable (defaults to “.p4config”). They then search for a file with this name up the tree from a file’s location on disk. When found, it provides overrides for any/all P4 environment variables.

    My example in a workspace root:
    P4CLIENT=robert-surface
    P4PORT=:1666
    P4USER=robert
    P4PASSWORD=

    Do you think this would be feasible to support in the plugin? This would streamline my work in ST3 tremendously! As is, I’m only able to use the plugin in my main workspace, and have to remember to check out/add files in other workspaces before editing or saving. If I don’t the plugin will automatically create a new workspace named after my PC.

    Thanks again!
    Robert

  16. Eric says:

    Hi Robert,

    Thanks! :)

    I’m a bit busy at the moment but I’m taking a note to look at your specific issue ASAP. Since the plugin only calls the command line tools, I would have assumed the .p4config files would be taken into account.

    I’ll do my best to keep you updated soon

    Thank you

    Eric

  17. Robert Schmidt says:

    Sorry Eric, my mistake!
    Your reply got me thinking that the fault was on my side.

    It turns out I was using ST on a new machine, which didn’t have the P4CONFIG environment variable set. Setting it fixed the problem!

    Thanks again!

  18. Andrew says:

    Like Chris, I am on OSX and am also getting “File is not under client root.”, although the p4 cli works fine. Any updates on this issue?

  19. Eric says:

    After investigation, with a setup similar to yours I got it working no problem. I’ve seen issues in the past with Case Sensitivity on the server, is this turned on?

  20. Andrew says:

    Thanks for looking into this Eric. Oddly turns out I had an error in my .bash_profile which looks like sublime wasn’t letting slip. I found this by watching the console after setting:
    perforce_log_warnings_to_status”: false

    Thanks for the cool plugin!

  21. Eric says:

    I’m glad the plugin is useful to you! Cheers!

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>