Skip to content

Instantly share code, notes, and snippets.

@chiastolite
Last active December 26, 2016 09:08
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 chiastolite/781654396ade961ced79894cfefa4a53 to your computer and use it in GitHub Desktop.
Save chiastolite/781654396ade961ced79894cfefa4a53 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Gitリポジトリ内ではなければ処理をしない
git status > /dev/null 2>&1
if [ $? != 0 ] ; then
echo "Not a git repository"
exit 1
fi
# stagingされているファイルがある場合は処理をしない
if [ -n "`git diff --name-only --cached`" ]; then
echo 'Some file(s) are staged.'
exit 1
fi
# 元々あったファイルを記録
current_files="$(git ls-files --others --exclude-standard)"
# コマンドを実行
CMD=$@
if ! $@; then
echo "Failed: ${CMD}"
exit 1
fi
# コマンド実行結果により追加されたファイルを記録
new_files=`diff <(echo "${current_files}") <((git ls-files --others --exclude-standard)) | grep -e '^> ' | sed "s/^> //"`
if [ -z "$new_files" ]; then
echo "No change"
exit 1
fi
# 新しいファイルを記録してコミットを作成
git add ${new_files}
git commit -m "[rec] ${CMD}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment