Eliminater74
12/27/2016 - 1:11 AM

copy-git-file.sh

#!/bin/bash
#Inspired by http://blog.neutrino.es/2012/git-copy-a-file-or-directory-from-another-repository-preserving-history/
#Copy a file or directory out of a git repository, preserving history!
#Creates '/destination/patch/path' with patches that can be applied with git am
#e.g.
#0001-First-Commit.patch
#0002-Second-Commit.patch
#...
#Usage: copy-git-file.sh /some/repo/interesting/thing /destination/patch/path

S=$1
DS=$S
D=$2
C=$(pwd)

if [ ! -d $S ]; then
  DS=$(dirname $S)
fi
echo "REPO FOLDER: $DS"

if [[ ! "$D" = /* ]]; then
  D="$C/$D"
fi
echo "DESTINATION FOLDER: $D"

pushd $DS

LOG=$(git log|grep ^commit)
COMMIT_COUNT=$(echo $LOG | wc -l)
FIRST_COMMIT=$(echo $LOG|tail -1|awk '{print $2}')

if [ $COMMIT_COUNT -eq 1 ]; then
    git format-patch -o $D --root $FIRST_COMMIT $S
else
    git format-patch -o $D $FIRST_COMMIT..HEAD $S
fi

popd