git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ramkumar Ramachandra <artagnon@gmail.com>
To: Git List <git@vger.kernel.org>
Cc: Martin von Zweigbergk <martinvonz@gmail.com>,
	Johannes Schindelin <Johannes.Schindelin@gmx.de>
Subject: [PATCH 7/7] rebase: implement --[no-]autostash and rebase.autostash
Date: Tue, 23 Apr 2013 19:32:04 +0530	[thread overview]
Message-ID: <1366725724-1016-8-git-send-email-artagnon@gmail.com> (raw)
In-Reply-To: <1366725724-1016-1-git-send-email-artagnon@gmail.com>

This new feature allows a rebase to be executed on a dirty worktree.
It works by creating a temporary stash and storing it in
$state_dir/autostash before the operation, and applying it after a
successful operation.  It will be removed along with the $state_dir if
the operation is aborted.

The feature creates a special stash that does not affect the normal
stash's reflogs, and will therefore be invisible to the end user.
This special stash is essentially a dangling merge commit which has
reasonable lifetime specified by gc.pruneexpire (default 2 weeks).

Most significantly, this feature means that a caller like pull (with
pull.rebase set to true) can easily be patched to remove the
require_clean_work_tree restriction.

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 Documentation/config.txt     |  8 ++++++++
 Documentation/git-rebase.txt | 10 ++++++++++
 git-rebase.sh                | 38 +++++++++++++++++++++++++++++++++++---
 3 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index c67038b..03ad701 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -1867,6 +1867,14 @@ rebase.stat::
 rebase.autosquash::
 	If set to true enable '--autosquash' option by default.
 
+rebase.autostash::
+	When set to true, automatically create a temporary stash
+	before the operation begins, and apply it after the operation
+	ends.  This means that you can run rebase on a dirty worktree.
+	However, use with care: the final stash application after a
+	successful rebase might result in non-trivial conflicts.
+	Defaults to false.
+
 receive.autogc::
 	By default, git-receive-pack will run "git-gc --auto" after
 	receiving data from git-push and updating refs.  You can stop
diff --git a/Documentation/git-rebase.txt b/Documentation/git-rebase.txt
index aca8405..c84854a 100644
--- a/Documentation/git-rebase.txt
+++ b/Documentation/git-rebase.txt
@@ -208,6 +208,9 @@ rebase.stat::
 rebase.autosquash::
 	If set to true enable '--autosquash' option by default.
 
+rebase.autostash::
+	If set to true enable '--autostash' option by default.
+
 OPTIONS
 -------
 --onto <newbase>::
@@ -394,6 +397,13 @@ If the '--autosquash' option is enabled by default using the
 configuration variable `rebase.autosquash`, this option can be
 used to override and disable this setting.
 
+--[no-]autostash::
+	Automatically create a temporary stash before the operation
+	begins, and apply it after the operation ends.  This means
+	that you can run rebase on a dirty worktree.  However, use
+	with care: the final stash application after a successful
+	rebase might result in non-trivial conflicts.
+
 --no-ff::
 	With --interactive, cherry-pick all rebased commits instead of
 	fast-forwarding over the unchanged ones.  This ensures that the
diff --git a/git-rebase.sh b/git-rebase.sh
index 8412d81..c8fddfe 100755
--- a/git-rebase.sh
+++ b/git-rebase.sh
@@ -13,6 +13,7 @@ git-rebase --continue | --abort | --skip | --edit-todo
  Available options are
 v,verbose!         display a diffstat of what changed upstream
 q,quiet!           be quiet. implies --no-stat
+autostash!         automatically stash/stash pop before and after
 onto=!             rebase onto given branch instead of upstream
 p,preserve-merges! try to recreate merges instead of ignoring them
 s,strategy=!       use the given merge strategy
@@ -64,6 +65,7 @@ apply_dir="$GIT_DIR"/rebase-apply
 verbose=
 diffstat=
 test "$(git config --bool rebase.stat)" = true && diffstat=t
+autostash="$(git config --bool rebase.autostash || echo false)"
 git_am_opt=
 rebase_root=
 force_rebase=
@@ -143,6 +145,24 @@ move_to_original_branch () {
 	esac
 }
 
+apply_autostash () {
+	if test -f "$state_dir/autostash"
+	then
+		stash_sha1=$(cat "$state_dir/autostash")
+		git stash apply $stash_sha1 2>&1 >/dev/null ||
+		die "
+$(eval_gettext 'Applying autostash resulted in conflicts.
+Either fix the conflicts now, or run
+	git reset --hard
+and apply the stash on your desired branch:
+	git stash apply $stash_sha1
+at any time.')" &&
+		echo "Applied autostash"
+	fi
+	git gc --auto &&
+	rm -rf "$state_dir"
+}
+
 run_specific_rebase () {
 	if [ "$interactive_rebase" = implied ]; then
 		GIT_EDITOR=:
@@ -153,8 +173,7 @@ run_specific_rebase () {
 	ret=$?
 	if test $ret = 0
 	then
-		git gc --auto &&
-		rm -rf "$state_dir"
+		apply_autostash
 	fi
 	exit $ret
 }
@@ -248,6 +267,9 @@ do
 	--stat)
 		diffstat=t
 		;;
+	--autostash)
+		autostash=true
+		;;
 	-v)
 		verbose=t
 		diffstat=t
@@ -348,7 +370,7 @@ abort)
 		;;
 	esac
 	output git reset --hard $orig_head
-	rm -r "$state_dir"
+	apply_autostash
 	exit
 	;;
 edit-todo)
@@ -487,6 +509,16 @@ case "$#" in
 	;;
 esac
 
+if test "$autostash" = true && ! require_clean_work_tree --quiet
+then
+	stash_sha1=$(git stash create) || die "$(gettext "Cannot autostash")" &&
+	mkdir -p "$state_dir" &&
+	echo $stash_sha1 >"$state_dir/autostash" &&
+	stash_abbrev=$(git rev-parse --short $stash_sha1) &&
+	echo "$(gettext "Created autostash: $stash_abbrev")" &&
+	git reset --hard
+fi
+
 require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")"
 
 # Now we are rebasing commits $upstream..$orig_head (or with --root,
-- 
1.8.2.1.578.ga933817

  parent reply	other threads:[~2013-04-23 14:01 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-23 14:01 [PATCH 0/7] Introduce rebase.autostash Ramkumar Ramachandra
2013-04-23 14:01 ` [PATCH 1/7] am: suppress error output from a conditional Ramkumar Ramachandra
2013-04-23 14:38   ` Martin von Zweigbergk
2013-04-23 16:29     ` Junio C Hamano
2013-04-23 16:31     ` Ramkumar Ramachandra
2013-04-23 17:29       ` Junio C Hamano
2013-04-23 17:31         ` Ramkumar Ramachandra
2013-04-23 14:01 ` [PATCH 2/7] rebase -i: don't error out if $state_dir already exists Ramkumar Ramachandra
2013-04-23 14:45   ` Martin von Zweigbergk
2013-04-23 16:35   ` Junio C Hamano
2013-04-23 16:38     ` Ramkumar Ramachandra
2013-04-23 14:02 ` [PATCH 3/7] am: tighten a conditional that checks for $dotest Ramkumar Ramachandra
2013-04-23 16:45   ` Junio C Hamano
2013-04-23 14:02 ` [PATCH 4/7] am: don't do housekeeping when rebasing Ramkumar Ramachandra
2013-04-23 16:51   ` Junio C Hamano
2013-04-23 14:02 ` [PATCH 5/7] rebase -i: return control to the caller, for housekeeping Ramkumar Ramachandra
2013-04-23 16:51   ` Martin von Zweigbergk
2013-04-25 14:30     ` Ramkumar Ramachandra
2013-04-23 16:57   ` Junio C Hamano
2013-04-23 14:02 ` [PATCH 6/7] sh-setup: introduce require_clean_work_tree --quiet Ramkumar Ramachandra
2013-04-23 17:00   ` Junio C Hamano
2013-04-23 14:02 ` Ramkumar Ramachandra [this message]
2013-04-23 17:07   ` [PATCH 7/7] rebase: implement --[no-]autostash and rebase.autostash Junio C Hamano
2013-04-23 17:34     ` Junio C Hamano
2013-04-23 17:37       ` Ramkumar Ramachandra
2013-04-23 22:45   ` Phil Hord
2013-04-24  8:27     ` Ramkumar Ramachandra
2013-04-24 17:52     ` Matthieu Moy
  -- strict thread matches above, loose matches on Subject: below --
2013-05-12 11:56 [PATCH v3 0/7] rebase.autostash completed Ramkumar Ramachandra
2013-05-12 11:56 ` [PATCH 7/7] rebase: implement --[no-]autostash and rebase.autostash Ramkumar Ramachandra
2013-05-13  6:28   ` Junio C Hamano
2013-05-13  8:03     ` Ramkumar Ramachandra
2013-05-13 13:49       ` Junio C Hamano
2013-05-13 13:52         ` Ramkumar Ramachandra
2013-05-13  8:24   ` Matthieu Moy
2013-05-13  8:27     ` Ramkumar Ramachandra
2013-05-13  8:41       ` Matthieu Moy

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=1366725724-1016-8-git-send-email-artagnon@gmail.com \
    --to=artagnon@gmail.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=martinvonz@gmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).