Skip to content

Commit

Permalink
Merge pull request #795 from spacewander/git-squash-commit-msg
Browse files Browse the repository at this point in the history
Git squash commit msg
  • Loading branch information
spacewander authored Nov 5, 2019
2 parents b0d3a77 + 46ea733 commit a6a1754
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
33 changes: 30 additions & 3 deletions bin/git-squash
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
#!/usr/bin/env bash

SQUASH_MSG=
for arg in "$@"; do
case "$arg" in
--squash-msg)
SQUASH_MSG=1
;;
*)
# set the argument back
set -- "$@" "$arg"
;;
esac

shift
done

src="$1"
msg="$2"
if [[ -n "$msg" ]] && [[ -n "$SQUASH_MSG" ]]; then
>&2 echo "When commit message is given, --squash-msg is not allowed."
exit 1
fi

is_branch() {
git show-ref --verify --quiet "refs/heads/$src"
Expand All @@ -12,7 +31,8 @@ is_commit_reference() {
}

is_on_current_branch() {
local commit_sha=`git rev-parse "$src"`
local commit_sha
commit_sha=$(git rev-parse "$src")
git rev-list HEAD |
grep -q -- "$commit_sha"
}
Expand All @@ -35,18 +55,25 @@ prompt_continuation_if_squashing_master() {

squash_branch() {
prompt_continuation_if_squashing_master
if [ -n "$SQUASH_MSG" ]; then
base=$(git merge-base "$src" @)
msg=$(git log "$base".."$src" --format="%s%n%n%b" --no-merges --reverse)
fi
git merge --squash "$src" || exit 1 # quits if `git merge` fails
commit_if_msg_provided
}

squash_current_branch() {
if [ -n "$SQUASH_MSG" ]; then
msg=$(git log "$src"..@ --format="%s%n%n%b" --no-merges --reverse)
fi
git reset --soft "$src" || exit 1 # quits if `git reset` fails
commit_if_msg_provided
}

if `is_branch`; then
if is_branch; then
squash_branch
elif `is_commit_reference` && `is_on_current_branch`; then
elif is_commit_reference && is_on_current_branch; then
squash_current_branch
else
echo "Source branch or commit reference required." 1>&2 && exit 1
Expand Down
1 change: 1 addition & 0 deletions etc/git-extras-completion.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ _git-refactor() {


_git-squash() {
_arguments '--squash-msg[commit with the squashed commit messages]'
_arguments \
':branch-name:__gitex_branch_names'
}
Expand Down
11 changes: 9 additions & 2 deletions man/git-squash.1
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "GIT\-SQUASH" "1" "October 2017" "" "Git Extras"
.TH "GIT\-SQUASH" "1" "November 2019" "" "Git Extras"
.
.SH "NAME"
\fBgit\-squash\fR \- Import changes from a branch
.
.SH "SYNOPSIS"
\fBgit\-squash\fR <source\-branch|commit ref> [<commit\-message>]
\fBgit\-squash\fR [<\-\-squash\-messages>] <source\-branch|commit ref> [<commit\-message>]
.
.SH "DESCRIPTION"
Produce the working tree and index state as if a real merge happened without the commit or merge marks\.
Expand All @@ -22,6 +22,12 @@ Branch to squash on the current branch\.
<commit reference> A commit reference (has to be from the current branch) can also be used as the first argument\. A range of commits \fIsha\fR\.\.HEAD will be squashed\.
.
.P
<\-\-squash\-msg>
.
.P
Commit the squash result with the concatenated squashed committed messages\. This option can not be used together with <commit\-message>\.
.
.P
<commit\-message>
.
.P
Expand All @@ -40,6 +46,7 @@ Squash commit \-\- not updating HEAD
$ git commit \-m "New commit without a real merge"

$ git squash HEAD~3 "Commit message"
$ git squash \-\-squash\-msg @~3
.
.fi
.
Expand Down
12 changes: 9 additions & 3 deletions man/git-squash.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion man/git-squash.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ git-squash(1) -- Import changes from a branch

## SYNOPSIS

`git-squash` &lt;source-branch|commit ref&gt; [&lt;commit-message&gt;]
`git-squash` [&lt;--squash-messages&gt;] &lt;source-branch|commit ref&gt; [&lt;commit-message&gt;]

## DESCRIPTION

Expand All @@ -20,6 +20,11 @@ git-squash(1) -- Import changes from a branch
A commit reference (has to be from the current branch) can also be used as the
first argument. A range of commits <sha>..HEAD will be squashed.

&lt;--squash-msg&gt;

Commit the squash result with the concatenated squashed committed messages.
This option can not be used together with &lt;commit-message&gt;.

&lt;commit-message&gt;

If commit-message is given, commit the squash result.
Expand All @@ -35,6 +40,7 @@ git-squash(1) -- Import changes from a branch
$ git commit -m "New commit without a real merge"

$ git squash HEAD~3 "Commit message"
$ git squash --squash-msg @~3

## AUTHOR

Expand Down

0 comments on commit a6a1754

Please sign in to comment.