Last active
December 12, 2019 16:10
-
-
Save nhoriguchi/4c692b3c03f83608456044e0d073a322 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/bash | |
# | |
# Usage | |
# git-squash.sh [-h|--help] [-E|--no-edit] NR | |
# | |
# Description | |
# カレントディレクトリを含むリポジトリの HEAD コミットから過去 | |
# NR コミットを一つのコミットにまとめる。 | |
# | |
show_help() { | |
sed -n 2,$[$BASH_LINENO-4]p $BASH_SOURCE | grep "^#" | sed 's/^#/ /' | |
} | |
EDIT=true | |
while [[ $# -gt 0 ]] ; do | |
key="$1" | |
case $key in | |
-h|--help) | |
show_help | |
exit 0 | |
;; | |
-e|--edit) | |
EDIT=true | |
shift 1 | |
;; | |
-E|--no-edit) | |
EDIT= | |
shift 1 | |
;; | |
*) # end of options | |
break | |
;; | |
esac | |
done | |
TSTAMP="`date +%y%m%d_%H%M%S`" | |
mkdir -p /tmp/$(basename $BASH_SOURCE) | |
TMPD=$(mktemp -d /tmp/$(basename $BASH_SOURCE)/${TSTAMP}.XXXXXX) | |
NR=$1 | |
if [ ! "$NR" ] || ( ! [ "$NR" -gt 0 ] ) ; then | |
echo "invalid argument" >&2 | |
show_help | |
exit 1 | |
elif [ "$NR" -eq 1 ] ; then | |
echo "NR must be > 1" >&2 | |
exit 1 | |
fi | |
if ! git diff --quiet ; then | |
echo "Current working tree is dirty." >&2 | |
exit 1 | |
fi | |
if [ "$EDIT" ] && [ ! "$EDITOR" ] ; then | |
echo "You need set environment variable EDITOR to your favorite editor" >&2 | |
exit 1 | |
fi | |
if [ "$(git log --oneline --merges HEAD~${NR} HEAD)" ] ; then | |
echo "Given range includes merge commits, so you can't squash them" >&2 | |
exit 1 | |
fi | |
git log --pretty="format:%B%n" -n ${NR} > $TMPD/description.txt | |
$EDITOR $TMPD/description.txt | |
git reset --soft HEAD~${NR} || exit 1 | |
git commit -F $TMPD/description.txt || exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment