Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mzp
Created August 5, 2011 07:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mzp/1127078 to your computer and use it in GitHub Desktop.
Save mzp/1127078 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 == "--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
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 -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 --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
@mzp
Copy link
Author

mzp commented Oct 30, 2011

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