Making p4 and Cygwin play nice

November 2, 2009

Say you’re a developer doing development on Windows, and say you’re using Perforce (better known as p4) for source control. Let’s also say you have *nix background and like using Cygwin to keep you from going insane. What are your options for interacting with p4?

Fortunately, Perforce provides a binary compiled against Cygwin. You can download that, drop it in your $PATH, and be on your merry way.

But I happen to like P4Win as a graphical client, and P4Win doesn’t play nicely with the Cygwin p4. Why? Client roots. With P4Win (or any other non-Cygwin client, for that matter), your client root is a Windows-style path, e.g. C:\path\to\root. But Cygwin sees things differently. It wants your client root to be something like /cygdrive/c/path/to/root.

So, if you have a client set up with P4Win and try to interact with the command line p4 in Cygwin, you’ll get messages like this:

'awesomecode.cc' is not under client's root 'C:\path\to\root'

You can get around this by using absolute paths, e.g.:

p4 edit $(cygpath -wa awesomecode.cc)

That gets tiresome very fast, though. But you, fearless reader, you are in luck! I have just the solution for you! It turns out that all p4 needs is a little persuasion to change its PWD environment variable. I whipped up a little C program that fixes up its PWD and exec’s the real p4, so it no longer gets confused.

You can download my program here. Enjoy!

5 Responses to “Making p4 and Cygwin play nice”

  1. I tried downloading the cygwin p4, and I get the same error about paths not matching. Sorry to post here about it, but I can’t find info anywhere and I’m getting desperate.

  2. The Cygwin build of the p4 client (e.g. the one at <ftp://ftp.perforce.com/perforce/r09.2/bin.cygwinx86/>) isn’t all that different from the version that comes with P4Win. It’s more aware of Cygwin features, but to use it, you still need to define your p4 clientspec with a client root of something like /cygdrive/c/blah. In particular, if you have a client set up in P4Win with a client root of C:\blah, the p4 Cygwin build will still be confused.

    Use my p4 program (for which I’ve thus far failed to come up with a good name; do you have any suggestions?) to fix this; if you want it to use the Cygwin p4 on the backend instead of the P4Win p4, just replace the executable path in the call to execv().

  3. Thanks for the program and the tip. This was EXACTLY what I have been looking for. I am very very surprised Perforce have not sorted this out in their cygwin command line client.

  4. Nice input. To make P4 work with Cygwin, your program tip was extremely good to do the job for me. Cygwin works pretty well. But one need to check the Main TAR files.

  5. FWIW, another option would be to use AltRoots in your clientspec so that your Root was Windows-friendly and one of your AltRoots was cygwin-friendly (or vice-versa).

    http://kb.perforce.com/article/465/choosing-between-altroots
    http://kb.perforce.com/article/1217/creating-a-shared-workspace-between-cygwin-and-windows