From 1924eb91974e9d91e302a6a1c1505b38c2fefd9e Mon Sep 17 00:00:00 2001 From: spacewander Date: Tue, 5 Nov 2019 10:02:22 +0800 Subject: [PATCH] git-squash: add --squash-messages to concat commit messages into one. --- bin/git-squash | 26 ++++++++++++++++++++++++++ man/git-squash.1 | 11 +++++++++-- man/git-squash.html | 12 +++++++++--- man/git-squash.md | 8 +++++++- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/bin/git-squash b/bin/git-squash index 249db5e..b6f412e 100755 --- a/bin/git-squash +++ b/bin/git-squash @@ -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 } diff --git a/man/git-squash.1 b/man/git-squash.1 index ea7961f..6d73f4e 100644 --- a/man/git-squash.1 +++ b/man/git-squash.1 @@ -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 [] +\fBgit\-squash\fR [<\-\-squash\-messages>] [] . .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\. 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 \. +. +.P . .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 . diff --git a/man/git-squash.html b/man/git-squash.html index 890666e..a988afc 100644 --- a/man/git-squash.html +++ b/man/git-squash.html @@ -76,7 +76,7 @@

SYNOPSIS

-

git-squash <source-branch|commit ref> [<commit-message>]

+

git-squash [<--squash-messages>] <source-branch|commit ref> [<commit-message>]

DESCRIPTION

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

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

AUTHOR

-

Written by Jesús Espino <jespinog@gmail.com>

+

Written by Jesús Espino <jespinog@gmail.com>

REPORTING BUGS

@@ -125,7 +131,7 @@ $ git squash HEAD~3 "Commit message"
  1. -
  2. October 2017
  3. +
  4. November 2019
  5. git-squash(1)
diff --git a/man/git-squash.md b/man/git-squash.md index baa4bcf..f358ec2 100644 --- a/man/git-squash.md +++ b/man/git-squash.md @@ -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 ..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