-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`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
- Loading branch information
Bruno Sutic
committed
Jul 20, 2014
1 parent
458c08d
commit b2576ad
Showing
5 changed files
with
81 additions
and
42 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,53 @@ | ||
#!/bin/sh | ||
#!/bin/bash | ||
|
||
src=$1 | ||
msg=$2 | ||
src="$1" | ||
msg="$2" | ||
|
||
test -z $src && echo "source branch required, or use --me." && exit 1 | ||
is_branch() { | ||
git show-ref --verify --quiet "refs/heads/$src" | ||
} | ||
|
||
# squash current branch | ||
# TODO: do this in a less hacky way | ||
# TODO: less sketchy arguments | ||
is_commit_reference() { | ||
git rev-parse --verify --quiet "$src" > /dev/null 2>&1 | ||
} | ||
|
||
if [[ $1 == "--me" ]]; then | ||
msg=$3 | ||
branch=`git rev-parse --abbrev-ref HEAD` | ||
|
||
# merge against target branch or master | ||
git checkout ${2-master} | ||
|
||
git checkout -B squash-tmp | ||
git squash $branch | ||
git branch -D $branch | ||
git checkout -B $branch | ||
git branch -D squash-tmp | ||
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 | ||
|
||
exit | ||
fi | ||
|
||
# squash $src | ||
|
||
git merge --squash $src | ||
|
||
if test -n "$msg"; then | ||
git commit -a -m "$msg" && git branch -D $src | ||
} | ||
|
||
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 |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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