mirror of
https://github.com/tj/git-extras.git
synced 2026-09-16 02:16:17 -04:00
Merge pull request #795 from spacewander/git-squash-commit-msg
Git squash commit msg
This commit is contained in:
commit
a6a1754093
|
|
@ -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"
|
||||
|
|
@ -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"
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -400,6 +400,7 @@ _git-refactor() {
|
|||
|
||||
|
||||
_git-squash() {
|
||||
_arguments '--squash-msg[commit with the squashed commit messages]'
|
||||
_arguments \
|
||||
':branch-name:__gitex_branch_names'
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
.
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@
|
|||
|
||||
<h2 id="SYNOPSIS">SYNOPSIS</h2>
|
||||
|
||||
<p><code>git-squash</code> <source-branch|commit ref> [<commit-message>]</p>
|
||||
<p><code>git-squash</code> [<--squash-messages>] <source-branch|commit ref> [<commit-message>]</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> <--squash-msg></p>
|
||||
|
||||
<p> Commit the squash result with the concatenated squashed committed messages.
|
||||
This option can not be used together with <commit-message>.</p>
|
||||
|
||||
<p> <commit-message></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 <<a href="mailto:jespinog@gmail.com" data-bare-link="true">jespinog@gmail.com</a>></p>
|
||||
<p>Written by Jesús Espino <<a href="mailto:jespinog@gmail.com" data-bare-link="true">jespinog@gmail.com</a>></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>
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ git-squash(1) -- Import changes from a branch
|
|||
|
||||
## SYNOPSIS
|
||||
|
||||
`git-squash` <source-branch|commit ref> [<commit-message>]
|
||||
`git-squash` [<--squash-messages>] <source-branch|commit ref> [<commit-message>]
|
||||
|
||||
## 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.
|
||||
|
||||
<--squash-msg>
|
||||
|
||||
Commit the squash result with the concatenated squashed committed messages.
|
||||
This option can not be used together with <commit-message>.
|
||||
|
||||
<commit-message>
|
||||
|
||||
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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue