tags:

views:

120

answers:

1

I have multiple branches of a project checked out, each under their own directory (pretty standard).

src/branch1/some/code/directories src/branch2/some/code/directories

I often find myself wanting to copy selected files from one branch to another. An example would be copying cvsignore files, or intellij module files. The pseudocommand for what I'm trying to do is "copy all files under branch1 matching PATTERN to branch2, preserving the relative path of the copied file".

This question looks close to what I'm looking for, but I need an OS X/linux/unix solution.

+1  A: 

Use "cp -r --parents" command like this, in branch1 directory

find . -name ".cvsignore" -exec cp -r --parents {} ../branch2/ \;

OR

When in the src/ directory, run this script. You can get the variables from command line parameters if you want.

SOURCE="branch1/"
TARGET="branch2/"
PATTERN=".cvsignore"

find $SOURCE -name $PATTERN | while read f ;
do
        FILEPATH=$(dirname $f | cut -d'/' -f2-)
        FILENAME=$(basename $f)
        DESTPATH=$TARGET/$FILEPATH;
        if [ ! -d $DESTPATH ]
                then mkdir -p $DESTPATH
        fi
        cp $f $DESTPATH
done
hayalci
thanks. the --parents flag in the first solution doesn't appear to be supported in os X, and i can't find it in any man pages either. second solution worked great though.
jon
--parents is probably a GNU extension
hayalci