Skip to content

Instantly share code, notes, and snippets.

@wadackel
Last active February 19, 2016 02:11
Show Gist options
  • Save wadackel/06f2a67c96b50f81b2e6 to your computer and use it in GitHub Desktop.
Save wadackel/06f2a67c96b50f81b2e6 to your computer and use it in GitHub Desktop.
gitで差分ファイルの抽出用関数
#!/bin/sh
function git_diff_archive()
{
local OPTIND OPTARG OPT
local dateTime=`date '+%Y%m%d%H%M'`
local mode="AMCRD"
local diff=""
local h="HEAD"
usage()
{
cat <<_EOT_
Usage:
git_diff_archive [-m mode] commit_id [old_commit_id]
Description:
Generate to the zip file in git diff command.
Options:
-m mode [A|M|C|R|D] (Defaut:AMCRD)
-h help
_EOT_
}
# parse opts
while getopts "m:h" OPT
do
case $OPT in
m)
mode=$OPTARG
;;
h)
usage
return
;;
\?)
usage
return
;;
esac
done
shift $((OPTIND - 1))
# parse commit_id
if [ $# -eq 1 ]; then
if expr "$1" : '[0-9]*$' > /dev/null ; then
diff="HEAD HEAD~${1}"
else
diff="HEAD ${1}"
fi
elif [ $# -eq 2 ]; then
diff="${1} ${2}"
h=$1
fi
if [ "$diff" != "" ]; then
diff="git diff --diff-filter=${mode} --name-only ${diff}"
fi
git archive --format=zip --prefix=${PWD##*/}/ $h `eval $diff` -o ${PWD##*/}-$dateTime.zip
}
@wadackel
Copy link
Author

調整箇所

以下、参考サイトからの変更箇所。

  • 最終的に纏めるzipのファイル名を「ディレクトリ名+タイムスタンプ」に
  • --prefixには現在のディレクトリ名を (展開後に分かりやすいように)
  • --diff-filterの値を-mオプションで別途指定できるように
  • -hオプションで簡単なヘルプを参照

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment