tj.git-extras/bin/git-squash
Bruno Sutic b2576adae0 Refactor and improve git squash
`git squash` refactor:
- first argument can be a branch or a commit reference
- the `--me` argument is not required anymore
- use `git reset --soft` is squashing on a current branch
- if squashing on master branch, prompt for confirmation
- does not delete provided branch after squashing
- fix bug: if `git merge --squash` command fails, exit script

Update README and man pages.

Fixes #95
Closes #198
2014-07-20 17:33:15 +02:00

54 lines
1.1 KiB
Bash
Executable file

#!/bin/bash
src="$1"
msg="$2"
is_branch() {
git show-ref --verify --quiet "refs/heads/$src"
}
is_commit_reference() {
git rev-parse --verify --quiet "$src" > /dev/null 2>&1
}
is_on_current_branch() {
local commit_sha=`git rev-parse "$src"`
git rev-list HEAD |
grep -q "$commit_sha"
}
commit_if_msg_provided() {
if test -n "$msg"; then
git commit -a -m "$msg"
fi
}
prompt_continuation_if_squashing_master() {
if [[ $src =~ ^master$ ]]; then
read -p "Warning: squashing '$src'! Continue [y/N]? " -r
if ! [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Exiting"
exit 1
fi
fi
}
squash_branch() {
prompt_continuation_if_squashing_master
git merge --squash "$src" || exit 1 # quits if `git merge` fails
commit_if_msg_provided
}
squash_current_branch() {
git reset --soft "$src" || exit 1 # quits if `git reset` fails
commit_if_msg_provided
}
if `is_branch`; then
squash_branch
elif `is_commit_reference` && `is_on_current_branch`; then
squash_current_branch
else
echo "Source branch or commit reference required." 1>&2 && exit 1
fi