-
-
Save masaru-b-cl/7038197 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
PREFIX="from now" | |
MESSAGE="[${PREFIX}] `date +\"%Y/%m/%d %T\"`" | |
MAIN_BRANCH=${1:-master} # デフォルトは 'master'、第1引数で上書き可能 | |
shift # 引数リストからメインブランチ名を除外 | |
get_amend() { | |
if [ -z `git log --pretty=oneline --no-color -1 | cut -d " " -f 2- | grep "^\[${PREFIX}]"` ] | |
then | |
return 1 | |
fi | |
local added_files=`git diff --name-only --cached` | |
for f1 in `git diff --name-only HEAD^ HEAD` | |
do | |
for f2 in $added_files | |
do | |
if [ $f1 == $f2 ] | |
then | |
return 1 | |
fi | |
done | |
done | |
echo "--amend" | |
return 1 | |
} | |
if [ $# -eq 0 ] | |
then | |
git add -u | |
git commit -m "${MESSAGE}" | |
elif [ $1 == "--compact" ] | |
then | |
git add -A | |
AMEND=`get_amend` | |
git commit $AMEND -m "${MESSAGE}" | |
elif [ $1 == "--recent" ] | |
then | |
git add -A | |
if [ -z "`git log --since=1.minute`" ] | |
then | |
git commit -m "${MESSAGE}" | |
else | |
git commit --amend -m "${MESSAGE}" | |
fi | |
elif [ $1 == "--fixup" ] | |
then | |
WORKING_BRANCH=`git branch -l | grep "*" | cut -d " " -f 2` | |
if [ $WORKING_BRANCH == $MAIN_BRANCH ] | |
then | |
FIRST_NOW_COMMIT=`git log --pretty=oneline --no-color --grep="${PREFIX}" | tail -n 1 | cut -d " " -f 1` | |
else | |
FIRST_NOW_COMMIT=`git log $MAIN_BRANCH.. --pretty=oneline --no-color --grep="${PREFIX}" | tail -n 1 | cut -d " " -f 1` | |
fi | |
git reset ${FIRST_NOW_COMMIT} | |
git add -A | |
if [ $# -eq 2 ] | |
then | |
git commit --amend -m "$2" | |
else | |
git commit --amend | |
fi | |
elif [ $1 != "--rebase" ] | |
then | |
if [ $1 != "--all" ] | |
then | |
git add $@ | |
else | |
git add -u | |
git add . | |
fi | |
printf "${MESSAGE}\n\n%s" "`git diff --cached`" | git commit -F - | |
else | |
FIRST_NOW_COMMIT=`git log $MAIN_BRANCH.. --pretty=oneline --no-color --grep="${PREFIX}" | tail -n 1 | cut -d " " -f 1` | |
INITIAL_COMMIT=`git log $MAIN_BRANCH.. --pretty=oneline --no-color | tail -n 1 | cut -d " " -f 1` | |
if [ ${FIRST_NOW_COMMIT} != ${INITIAL_COMMIT} ] | |
then | |
git rebase -i ${FIRST_NOW_COMMIT}^ | |
else | |
WORKING_BRANCH=`git branch -l | grep "*" | cut -d " " -f 2` | |
git checkout ${FIRST_NOW_COMMIT} | |
git commit --amend | |
git rebase --onto HEAD ${FIRST_NOW_COMMIT} ${WORKING_BRANCH} | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
メインブランチ名を第一引数で指定できるようにする