February 4, 2009

Sweet Reconciliation

Traceability
MERGE User Conference
Flexible Workflows
Integration

Wind-up Running Man

If you're like me, you often neglect to check out files as you are working on them. This could be because you forget. It might be that your IDE doesn't have SCM support, or perhaps you are a recovering Subversion user longing for the warm caress of 'svn status'! Whatever the case, I'm here to tell you that reconciling offline work in Perforce doesn't have to be difficult.

I don't know why I didn't hack this together earlier - it took me about three years to get around to it and only about three minutes to write. It's very simple. I call it 'auto reconcile', but you can call it 'p4 status' or whatever you want. Put simply it opens new files for add, changed files for edit and missing files for delete.

#! /bin/bash# resolve and validate filepath.FILEPATH=`p4 -ztag where "$4"| head -n3| tail -n1\| cut -f3- -d ' '| sed -e s:/\.\.\.$::`
if[ -z "$FILEPATH"];then
echo "Can't reconcile given path."exit1fi# detect p4 config filename (to screen it out).P4CONFIGFILE=`p4 set| grep P4CONFIG | cut -f2- -d '='`
if[ -z "$P4CONFIGFILE"];thenP4CONFIGFILE='.p4'fi# add new files (excluding P4CONFIG and .DS_Store files).
echo 'Adding new files...'
find "$FILEPATH" -type f -print \| grep -vF '.DS_Store'\| grep -vF "$P4CONFIGFILE"\| p4 -p "$1" -u "$2" -c "$3" -x- add 2>&1\| grep -vFi "can't add"\| grep -vFi "existing file"\| grep -vFi "no permission"\| grep -vFi "use -f option to force"# edit modified files.
echo 'Checking out modified files...'
p4 -p "$1" -u "$2" -c "$3" diff -se "$4"2>&1\| grep -vFi "file(s) not on client"\| p4 -p "$1" -u "$2" -c "$3" -x- edit

# delete missing files.
echo 'Deleting missing files...'
p4 -p "$1" -u "$2" -c "$3" diff -sd "$4"2>&1\| grep -vFi "file(s) not on client"\| p4 -p "$1" -u "$2" -c "$3" -x- delete

echo 'Done.'

It also plugs nicely into P4V...

  1. Click Tools -> Manage Custom Tools...
  2. New -> Tool...
  3. In the ensuing dialog...
    • Give it a name (ie. Auto Reconcile)
    • Check 'Add to applicable context menus'
    • Enter the path to your script (don't forget to make it executable!)
    • Enter '$p $u $c %d' for the arguments
    • Check 'Run tool in terminal window'
    • Check 'Refresh P4V upon completion'

This is just a crude example of what can be done. Auto-reconcile works well for my purposes, but if you need more control, check out P4V's interactive reconcile feature. Also... if you make any improvements to the script, please come back and post your changes in the comments!