All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nanako Shiraishi <nanako3@lavabit.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	Nicolas Sebrecht <nicolas.s.dev@gmx.fr>,
	Jakub Narebski <jnareb@gmail.com>,
	Teemu Likonen <tlikonen@iki.fi>,
	Matthieu Moy <Matthieu.Moy@imag.fr>,
	Sverre Rabbelier <srabbelier@gmail.com>,
	John Tapsell <johnflux@gmail.com>
Subject: [PATCH v2] rebase -i --autosquash: auto-squash commits
Date: Fri, 19 Jun 2009 06:55:34 +0900	[thread overview]
Message-ID: <20090619065534.6117@nanako3.lavabit.com> (raw)
In-Reply-To: <7vvdmu15j0.fsf@alter.siamese.dyndns.org>

Teach a new option, --autosquash, to the interactive rebase.
When the commit log message begins with "!fixup ...", and there
is a commit whose title begins with the same ..., automatically
modify the todo list of rebase -i so that the commit marked for
squashing come right after the commit to be modified, and change
the action of the moved commit from pick to squash.

This will help the use case outlined in

    From: Junio C Hamano <gitster@pobox.com>
    Date: Wed, 17 Jun 2009 09:33:19 -0700
    Subject: Re: git rebase --interactive squash/squish/fold/rollup
    Message-ID: <7vvdmurfao.fsf@alter.siamese.dyndns.org>

and further explained in

    From: Junio C Hamano <gitster@pobox.com>
    Date: Thu, 18 Jun 2009 00:54:47 -0700
    Subject: Re: [PATCH] rebase -i: auto-squash commits
    Message-ID: <7vws7ayo1k.fsf@alter.siamese.dyndns.org>

Signed-off-by: Nanako Shiraishi <nanako3@lavabit.com>
---

Changes from my yesterday's patch are as follows.

 * The feature is disabled by default; the user needs to explicitly ask for it with --autosquash option.
 * Squashing more than one commits to the same commit should work.
 * The commit message must begin with a more magic string "!fixup" instead of "squash to".
 * Commands in the test script are joined with &&.
 * The test examines the content of the file to verify that the commit was correctly squashed.
 * Add documentation.

 Documentation/git-rebase.txt |    9 +++++++++
 git-rebase--interactive.sh   |   35 +++++++++++++++++++++++++++++++++++
 t/t3414-rebase-autosquash.sh |   37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 81 insertions(+), 0 deletions(-)
 create mode 100755 t/t3414-rebase-autosquash.sh

diff --git a/Documentation/git-rebase.txt b/Documentation/git-rebase.txt
index 26f3b7b..0c2f99e 100644
--- a/Documentation/git-rebase.txt
+++ b/Documentation/git-rebase.txt
@@ -293,6 +293,15 @@ OPTIONS
 	root commits will be rewritten to have <newbase> as parent
 	instead.
 
+--autosquash::
+	When the commit log message begins with "!fixup ...", and there
+	is a commit whose title begins with the same ..., automatically
+	modify the todo list of rebase -i so that the commit marked for
+	squashing come right after the commit to be modified, and change
+	the action of the moved commit from pick to squash.
++
+This option is only valid when '--interactive' option is used.
+
 include::merge-strategies.txt[]
 
 NOTES
diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index f96d887..6e223d5 100755
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -28,6 +28,7 @@ abort              abort rebasing process and restore original branch
 skip               skip current patch and continue rebasing process
 no-verify          override pre-rebase hook from stopping the operation
 root               rebase all reachable commmits up to the root(s)
+autosquash         automatically squash commits that begin with !fixup
 "
 
 . git-sh-setup
@@ -46,6 +47,7 @@ ONTO=
 VERBOSE=
 OK_TO_SKIP_PRE_REBASE=
 REBASE_ROOT=
+AUTOSQUASH=
 
 GIT_CHERRY_PICK_HELP="  After resolving the conflicts,
 mark the corrected paths with 'git add <paths>', and
@@ -482,6 +484,35 @@ get_saved_options () {
 	test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
 }
 
+# Rearrange the todo list that has both "pick sha1 msg" and
+# "pick sha1 !fixup msg" appears in it so that the latter
+# comes immediately after the former, and change "pick" to
+# "squash".
+rearrange_squash () {
+	sed -n -e 's/^pick \([0-9a-f]*\) !fixup /\1 /p' "$1" >"$1.sq"
+	test -s "$1.sq" || return
+
+	used=
+	while read pick sha1 message
+	do
+		case " $used" in
+		*" $sha1 "*) continue ;;
+		esac
+		echo "$pick $sha1 $message"
+		while read squash msg
+		do
+			case "$message" in
+			"$msg"*)
+				echo "squash $squash !fixup $msg"
+				used="$used$squash "
+				;;
+			esac
+		done <"$1.sq"
+	done <"$1" >"$1.rearranged"
+
+	cat "$1.rearranged" >"$1"
+}
+
 while test $# != 0
 do
 	case "$1" in
@@ -587,6 +618,9 @@ first and then run 'git rebase --continue' again."
 	--root)
 		REBASE_ROOT=t
 		;;
+	--autosquash)
+		AUTOSQUASH=t
+		;;
 	--onto)
 		shift
 		ONTO=$(git rev-parse --verify "$1") ||
@@ -746,6 +780,7 @@ first and then run 'git rebase --continue' again."
 		fi
 
 		test -s "$TODO" || echo noop >> "$TODO"
+		test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
 		cat >> "$TODO" << EOF
 
 # Rebase $SHORTREVISIONS onto $SHORTONTO
diff --git a/t/t3414-rebase-autosquash.sh b/t/t3414-rebase-autosquash.sh
new file mode 100755
index 0000000..161cab4
--- /dev/null
+++ b/t/t3414-rebase-autosquash.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+test_description='auto squash'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+	echo 0 > file0 &&
+	git add . &&
+	test_tick &&
+	git commit -m "initial commit" &&
+	echo 0 > file1 &&
+	echo 2 > file2 &&
+	git add . &&
+	test_tick &&
+	git commit -m "first commit" &&
+	echo 3 > file3 &&
+	git add . &&
+	test_tick &&
+	git commit -m "second commit"
+'
+
+test_expect_success 'auto squash' '
+	echo 1 > file1 &&
+	git add -u &&
+	test_tick &&
+	git commit -m "!fixup first"
+	git tag final &&
+	test_tick &&
+	git rebase --autosquash -i HEAD^^^ &&
+	git log --oneline >actual &&
+	test 3 = $(wc -l <actual) &&
+	git diff --exit-code final &&
+	test 1 = "$(git cat-file blob HEAD^:file1)"
+'
+
+test_done
-- 
1.6.2.GIT

-- 
Nanako Shiraishi
http://ivory.ap.teacup.com/nanako3/

  reply	other threads:[~2009-06-18 21:56 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-17 12:06 git rebase --interactive squash/squish/fold/rollup Minty
2009-06-17 12:55 ` John Tapsell
2009-06-17 13:45   ` Minty
2009-06-17 16:33   ` Junio C Hamano
2009-06-17 16:40     ` John Tapsell
2009-06-17 16:48     ` Paolo Bonzini
2009-06-17 17:05     ` John Koleszar
2009-06-17 20:50       ` John Tapsell
2009-06-17 18:20     ` Clemens Buchacher
2009-06-18 22:31       ` Minty
2009-06-17 21:33     ` [PATCH] rebase -i: auto-squash commits Nanako Shiraishi
2009-06-17 22:08       ` Johannes Schindelin
2009-06-18  0:11         ` [PATCH] " Nicolas Sebrecht
2009-06-18  5:07           ` Junio C Hamano
2009-06-18  8:06             ` Johannes Schindelin
2009-06-18  8:11               ` Jakub Narebski
2009-06-18  8:21                 ` Junio C Hamano
2009-06-18  8:26                   ` Johannes Schindelin
2009-06-18  8:17               ` Teemu Likonen
2009-06-18  8:29                 ` Johannes Schindelin
2009-06-18  8:44                   ` Teemu Likonen
2009-06-18 12:16                     ` Johannes Schindelin
2009-06-18 13:10                       ` Jakub Narebski
2009-06-18 14:04                   ` John Koleszar
2009-06-18  8:20               ` Junio C Hamano
2009-06-18  8:33                 ` Johannes Schindelin
2009-06-18  8:44                   ` Michael J Gruber
2009-06-19  7:18                   ` Miles Bader
2009-06-18 11:18                 ` Nicolas Sebrecht
2009-06-18  8:34               ` Matthieu Moy
2009-06-18  8:44                 ` Johannes Schindelin
2009-06-18  8:59                   ` Matthieu Moy
2009-06-18 10:59             ` Nicolas Sebrecht
2009-06-18  5:21       ` [PATCH] " Junio C Hamano
2009-06-18 21:55         ` Nanako Shiraishi [this message]
2009-06-18 22:35           ` [PATCH v2] rebase -i --autosquash: " Alex Riesen
2009-06-19 23:07           ` Wincent Colaiuta
2009-06-20  1:46             ` Nanako Shiraishi
2009-06-18  7:20       ` [PATCH] rebase -i: " Michael Haggerty
2009-06-18  7:54         ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090619065534.6117@nanako3.lavabit.com \
    --to=nanako3@lavabit.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=Matthieu.Moy@imag.fr \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jnareb@gmail.com \
    --cc=johnflux@gmail.com \
    --cc=nicolas.s.dev@gmx.fr \
    --cc=srabbelier@gmail.com \
    --cc=tlikonen@iki.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.