Skip to content

Instantly share code, notes, and snippets.

@peccu
Created June 5, 2017 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peccu/e0f8fd9b1435deb5c0bde6ac73e42790 to your computer and use it in GitHub Desktop.
Save peccu/e0f8fd9b1435deb5c0bde6ac73e42790 to your computer and use it in GitHub Desktop.
git archive only difference files
#!/bin/bash
function usage(){
echo 'usage: git diff-export target1 target2 [output]'
echo
echo 'target1に指定したコミットとtarget2に指定したコミットで差分のあったファイルのみ、'
echo 'target1またはtarget2の新しい方のコミットでのスナップショットを書き出します。'
echo '(差分のなかったファイルや、より新しいコミットで修正されたファイルは書き出されません)'
echo
echo 'outputを指定した場合、outputに書き出します。デフォルトは ../output.zip です。'
exit
}
if [ -z $2 ] ; then
usage
fi
if [ -z $3 ] ; then
output_file=../export.zip
else
output_file=$3
fi
sha1_1=$1
sha1_2=$2
tmp=$(echo $sha1_1 $sha1_2 | xargs -n 1 git log -1 --pretty=tformat:%at:%H | sort -n |cut -d: -f2)
old_sha=$(echo $tmp | cut -d' ' -f 1)
new_sha=$(echo $tmp | cut -d' ' -f 2)
git archive -o $output_file $new_sha $(git diff --name-only $old_sha..$new_sha)
echo exported to $output_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment