Skip to content

Instantly share code, notes, and snippets.

@masaru-b-cl
Forked from mzp/git-now
Last active January 24, 2024 02:22
Show Gist options
  • Save masaru-b-cl/7038197 to your computer and use it in GitHub Desktop.
Save masaru-b-cl/7038197 to your computer and use it in GitHub Desktop.
#!/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
@masaru-b-cl
Copy link
Author

masaru-b-cl commented Oct 18, 2013

  • @mzp さんのgit-now https://gist.github.com/mzp/1127078 のfork
    • git logは--no-colorオプションつきで実行しないと、~/.gitconfig等で色がついていると最初のnowコミット取得に失敗するため

@masaru-b-cl
Copy link
Author

~/.gitconfig等で色がついている = git config color.ui auto のケース

@masaru-b-cl
Copy link
Author

間違って[from now] yyyy/MM/dd HH:mm:ssをgit svn dcommitしたリポジトリでgit now --fixupすると悲しいことになるので、フィーチャーブランチに限ってはmasterからfixupするように変えてみた

@masaru-b-cl
Copy link
Author

カレントブランチがmasterだったら、最古のnowコミットからfixupする

@masaru-b-cl
Copy link
Author

メインブランチ名を第一引数で指定できるようにする

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