My work uses perforce for source control, so I ended up having to install perforce integration for emacs. I’m pretty OK with this, since Perforce is so far my favorite version control system. I would like it if perforce allowed at least some functionality when not connected to the network, but I really prefer the interface of p4 to git or svn. As it is, Perforce is required to be able to connect to the central repository in order to do anything.
If you are using perforce, it’s probably for work, in which case they’ll probably have a guide on setting up your client. I don’t actually know a huge amount about setting perforce up: the places I’ve worked that use it have had it pre-set-up, so creating a repository and connecting to it may be ridiculously hard, but using it is quite easy.
Perforce has a very large number of commands; to see the full list, you can type ‘p4 help commands’. Fortunately, you won’t ever need to use most of those. There’s a pretty core group of about ten commands that I end up using frequently, which I’ll describe in a minute; the rest you can look up as you need to find out how to do something. Perforce also has a graphical interface you can use if you prefer that, but I find the command-line much easier. The commands I tend to use most are:
p4 add - This command will add a file to the repository. Before adding files, the version control system has no notion of it, so make sure to add all necessary files before committing.
p4 change - This command allows you to create a changelist. A changelist is a collection of files that you can commit together - basically it allows you to create groups of files that can be operated on. When you execute ‘p4 change’, a screen much like the ‘p4 submit’ screen will pop up, and you can edit the description and delete lines corresponding of files to remove them from the changelist that will be created.
p4 delete - This command deletes a file from the repository. Unlike p4 obliterate, history is kept of this file, but you do need to actually ‘p4 delete’ files that should not be checked out instead of just ‘rm’ing them.
p4 diff - p4 diff allows you to diff either a file on your workspace against a revision of that file in the repository, or two different repository revisions.
p4 edit - p4 edit will open up a file that currently exists in the repository for editing. The next time you do a p4 submit, the edited file will be in the list of files to submit.
p4 help - Perforce’s help command will let you know more about the system and commands. If you want more info about a command, ‘p4 help
p4 move - This command moves a file that is opened either for edit or add in your repository. ‘p4 rename’ will describe how to use this function.
p4 opened - This command will list every file you currently have opened. Similar to ’svn status’ for subversion, you can use it to ensure that every file has been added to the repository before committing.
p4 resolve - This command is used to resolve conflicts that occur when syncing. Resolving is always annoying, but this does what it can in order to be able to resolve conflicts. You can edit the file to remove them, accept either version of the file, and other options that are described when you use this command.
p4 revert - The standard version-control ‘revert’ command, this will undo changes to one or many files.
p4 submit - This command is used to submit your changes to the repository. When you type submit, you’ll receive a list of opened files that will be committed. Unlike SVN, deleting files from this list will prevent them from being committed. You can then type in a description of changes and exit and the commit will be pushed to the repository. You can also submit specific changelists, with ‘p4 submit -cl
p4 sync - The equivalent of ’svn up’, this will pull all the changes made to the repository since you last synced. This can require doing some resolving, which is done with ‘p4 resolve’. This can be a pain, but if you sync frequently you shouldn’t get very many conflicts.
Of course, to be maximally efficient you want to use Perforce directly from your editor. Vim, Emacs, and Eclipse all have ways to do this. In Emacs, you need to find an external elisp file for this. There are two main options for using perforce in emacs: p4.el and vc-p4.el. P4.el is more powerful - for example, it can operate on groups of files - but vc-p4.el contains hooks for emac’s usual source control actions, like vc-next-action. Both are compatible with each other, and you can use vc-p4 for most things while dipping into p4 for the difficult options. Personally, I only use p4.el, since I also don’t like Emacs’ version control integration too much for reasons I’ll talk about in another blog, and so the setup for me is simple. You download p4.el from here and put it in your load path, and then add the following to your initialization file:
(require 'p4)
All of the commands that you can use in this package are prefixed by p4, and are usually just p4-commandname. For example, to do a submit from emacs is just ‘p4 submit’. Also, most of the commands have keybindings that are ‘C-x p ‘ followed by the first letter of the name - so to add a file you would do ‘C-x p a’, edit is ‘C-x p e’ etcetera. There are a few exceptions, such as ‘p4-diff’ being ‘C-x p =’, but for the most part the keybindings are quite logical. You can look at this keybindings by doing M-x describe-function on the function you want, which tells you what the function does and any keybindings it has.
I haven’t installed vc-p4.el, but you can look at the emacs-wiki page here to see how to set it up.
Tags: emacs, version control
just fyi - I also wrote about perforce in my emacs-vcs article at http://xtalk.msk.su/~ott/en/writings/emacs-vcs/