git-squash: add --squash-messages to concat commit messages into one.

This commit is contained in:
spacewander 2019-11-05 10:02:22 +08:00
parent f2934bd0bd
commit 1924eb9197
4 changed files with 51 additions and 6 deletions

View file

@ -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"
@ -35,11 +54,18 @@ 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
}

View file

@ -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\.
@ -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
@ -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
.

View file

@ -76,7 +76,7 @@
<h2 id="SYNOPSIS">SYNOPSIS</h2>
<p><code>git-squash</code> &lt;source-branch|commit ref&gt; [&lt;commit-message&gt;]</p>
<p><code>git-squash</code> [&lt;--squash-messages&gt;] &lt;source-branch|commit ref&gt; [&lt;commit-message&gt;]</p>
<h2 id="DESCRIPTION">DESCRIPTION</h2>
@ -93,6 +93,11 @@
A commit reference (has to be from the current branch) can also be used as the
first argument. A range of commits <var>sha</var>..HEAD will be squashed.</p>
<p> &lt;--squash-msg&gt;</p>
<p> Commit the squash result with the concatenated squashed committed messages.
This option can not be used together with &lt;commit-message&gt;.</p>
<p> &lt;commit-message&gt;</p>
<p> If commit-message is given, commit the squash result.</p>
@ -108,11 +113,12 @@ 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
</code></pre>
<h2 id="AUTHOR">AUTHOR</h2>
<p>Written by Jesús Espino &lt;<a href="&#109;&#x61;&#x69;&#108;&#116;&#x6f;&#58;&#x6a;&#101;&#x73;&#x70;&#x69;&#110;&#111;&#x67;&#x40;&#x67;&#109;&#97;&#105;&#x6c;&#x2e;&#x63;&#x6f;&#109;" data-bare-link="true">&#x6a;&#101;&#115;&#112;&#105;&#x6e;&#111;&#x67;&#64;&#x67;&#x6d;&#97;&#x69;&#x6c;&#46;&#99;&#111;&#x6d;</a>&gt;</p>
<p>Written by Jesús Espino &lt;<a href="&#x6d;&#97;&#105;&#x6c;&#116;&#111;&#x3a;&#106;&#101;&#115;&#112;&#105;&#110;&#111;&#x67;&#64;&#x67;&#x6d;&#x61;&#105;&#x6c;&#x2e;&#x63;&#111;&#x6d;" data-bare-link="true">&#x6a;&#x65;&#x73;&#x70;&#105;&#110;&#111;&#103;&#x40;&#103;&#x6d;&#x61;&#x69;&#x6c;&#46;&#x63;&#x6f;&#109;</a>&gt;</p>
<h2 id="REPORTING-BUGS">REPORTING BUGS</h2>
@ -125,7 +131,7 @@ $ git squash HEAD~3 "Commit message"
<ol class='man-decor man-foot man foot'>
<li class='tl'></li>
<li class='tc'>October 2017</li>
<li class='tc'>November 2019</li>
<li class='tr'>git-squash(1)</li>
</ol>

View file

@ -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
@ -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.
@ -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