Skip to content

Instantly share code, notes, and snippets.

@bleis-tift
Forked from mzp/git-now
Created October 29, 2011 13:05
Show Gist options
  • Save bleis-tift/1324421 to your computer and use it in GitHub Desktop.
Save bleis-tift/1324421 to your computer and use it in GitHub Desktop.
git-now
#!/bin/sh
PREFIX="from now"
MESSAGE="[${PREFIX}] `date +\"%Y/%m/%d %T\"`"
get_amend() {
if [ -z `git log --pretty=oneline -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 == "--fixup" ]
then
FIRST_NOW_COMMIT=`git log --pretty=oneline --grep="${PREFIX}" | tail -n 1 | cut -d " " -f 1`
git reset ${FIRST_NOW_COMMIT}
git add -A
if [ $# -eq 2 ]
then
git commit --amend -F "$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 --pretty=oneline --grep="${PREFIX}" | tail -n 1 | cut -d " " -f 1`
INITIAL_COMMIT=`git log --pretty=oneline | 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
@bleis-tift
Copy link
Author

mzpのgit-nowからの変更

  1. compactオプションを追加し、前回のコミットと全く異なるファイルに対するgit-nowは、amendして一つにまとめるようにした
  2. コミットメッセージにdiffを含めないようにした
  3. fixupの際にコミットメッセージを指定できるようにした
  4. fixupの際にgit add -Aを行うようにした

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