17 February 2007

Using multiple Perforce depots and TextMate's P4 Bundle

I'm a big TextMate user. We're also a Perforce shop. TextMate has a P4 bundle, and it works great, but it's main problem is that it has minimal knowledge of your P4 settings, and certainly doesn't handle multiple P4 clients or depots. For example, I have a few different clientspecs I work with, and the server, port, clientspec, etc. all differ. So, it's not feasible to use a single .p4config file, or set the values in TextMate or globally in the environment. This makes using the P4 bundle essentially impossible for all but whichever one you pick as your main clientspec.

To alleviate this, and based on how I organize my code on my machine, I wrote a simple little script that is what I set the "TM_P4" textmate environment variable to. This script knows that all my code is kept in subdirectories off my ~/Code directory. So, I then stash specific .p4config files at the root of each of these as needed, and the script looks for those based on the currently open file, and sets that for the P4 settings. This seems to work well. Here's the script in case it's useful to you:


#!/bin/sh
#
# Script to run p4 commands in TextMate, and set the P4 variables
# depending on the location of the file being operated on.

# Where all my code lives
CODE_PATH="${HOME}/Code/"

# figure out the Code subdirectory for the current file in TextMate
P4_CONFIG_PATH=${TM_FILEPATH##$CODE_PATH}
P4_CONFIG_PATH=${P4_CONFIG_PATH%%/*}

# Reconstitute as path to potential config file
P4_CONFIG_FILE="${CODE_PATH}${P4_CONFIG_PATH}/.p4config"

# if dir contains a .p4config, set P4CONFIG to that
if [ -f $P4_CONFIG_FILE ]; then
export P4CONFIG="$P4_CONFIG_FILE"
else
export P4CONFIG="~/.p4config"
fi

# now do p4
/usr/local/bin/p4 $*

5 comments:

Anonymous said...

I'm amazed that anybody uses the Perforce bundle -- I wrote it as a stopgap while my company transitioned to Subversion. Glad you find it useful.

Chris said...

There's a number of us that use it, and appreciate it. I've made tweaks, but really haven't had to do much. Thanks.

Chris said...

Note, I realized the script I posted originally was missing the "export" part of setting the P4CONFIG variable. I've updated the blog entry to be correct.

jubishop said...

Add me to the list of daily textmate p4 bundle users...I was scheming to update it in the near future...let me know if you or anyone has already started down that road.

Hendrik said...

Thanks for the script. Simple but useful.
I had to replace
export P4CONFIG="~/.p4config"
with
export P4CONFIG="${HOME}/.p4config"
to make it work.