rebase.autostash completed
v3 cover rebase.autostash completed
Minor changes since v2 in response to reviews by Junio and Eric Sunshine.
Should be ready to merge soon.
Ramkumar Ramachandra (7):
am: tighten a conditional that checks for $dotest rebase -i: don't error out if $state_dir already exists
rebase: prepare to do generic housekeeping
am: return control to caller, for housekeeping rebase -i: return control to caller, for housekeeping rebase --merge: return control to caller, for housekeeping
rebase: implement --[no-]autostash and rebase.autostash
Documentation/config.txt | 8 +++ Documentation/git-rebase.txt | 10 +++ git-am.sh | 13 +++- git-rebase--am.sh | 8 +-- git-rebase--interactive.sh | 11 ++-- git-rebase--merge.sh | 5 +- git-rebase.sh | 46 +++++++++++++- t/t3420-rebase-autostash.sh | 148 +++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 233 insertions(+), 16 deletions(-) create mode 100755 t/t3420-rebase-autostash.sh
Signature · 1 line
1.8.3.rc1.51.gd7a04de
PATCH v3 1/7 am: tighten a conditional that checks for $dotest
In preparation for a later patch that creates $dotest/autostash in git-rebase.sh before anything else happens, don't assume that the presence of a $dotest directory implies the existence of the $dotest/next and $dotest/last files. Look for them explicitly.
--- git-am.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
git-am.sh +3 -1
@@ -446,6 +446,8 @@ done # If the dotest directory exists, but we have finished applying all the # patches in them, clear it out. if test -d "$dotest" && + test -f "$dotest/last" && + test -f "$dotest/next" && last=$(cat "$dotest/last") && next=$(cat "$dotest/next") && test $# != 0 &&
@@ -454,7 +456,7 @@ then rm -fr "$dotest" fi -if test -d "$dotest" +if test -d "$dotest" && test -f "$dotest/last" && test -f "$dotest/next" then case "$#,$skip$resolved$abort" in 0,*t*)
Signature · 1 line
1.8.3.rc1.51.gd7a04de
PATCH v3 2/7 rebase -i: don't error out if $state_dir already exists
In reply to Ramkumar RamachandraIn preparation for a later patch that will create $state_dir/autostash in git-rebase.sh before anything else can happen, change a mkdir $state_dir call to mkdir -p $state_dir. The change is safe, because this is not a test to detect an in-progress rebase (that is already done much earlier in git-rebase.sh).
--- git-rebase--interactive.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
git-rebase--interactive.sh +1 -1
@@ -842,7 +842,7 @@ then fi orig_head=$(git rev-parse --verify HEAD) || die "No HEAD?" -mkdir "$state_dir" || die "Could not create temporary $state_dir" +mkdir -p "$state_dir" || die "Could not create temporary $state_dir" : > "$state_dir"/interactive || die "Could not mark as interactive" write_basic_state
Signature · 1 line
1.8.3.rc1.51.gd7a04de
PATCH v3 3/7 rebase: prepare to do generic housekeeping
In reply to Ramkumar RamachandraOn successful completion of a rebase in git-rebase--$backend.sh, the $backend script cleans up on its own and exits. The cleanup routine is however, independent of the $backend, and each $backend script unnecessarily duplicates this work:
rm -rf "$state_dir"
git gc --autoPrepare git-rebase.sh for later patches that return control from each $backend script back to us, for performing this generic cleanup routine. The code that this patch adds is currently unreachable, and will only start to be used when git-rebase--$backend.sh scripts are taught to return control in later patches.
Another advantage is that git-rebase.sh can implement a generic finish_rebase() to possibly do additional tasks in addition to the cleanup.
--- git-rebase.sh | 7 +++++++ 1 file changed, 7 insertions(+)
git-rebase.sh +7 -0
@@ -150,6 +150,13 @@ run_specific_rebase () { autosquash= fi . git-rebase--$type + ret=$? + if test $ret -eq 0 + then + git gc --auto && + rm -rf "$state_dir" + fi + exit $ret } run_pre_rebase_hook () {
Signature · 1 line
1.8.3.rc1.51.gd7a04de
PATCH v3 4/7 am: return control to caller, for housekeeping
In reply to Ramkumar RamachandraWe only need to do these two tasks
git gc --auto
rm -fr "$dotest"
ourselves if the script was invoked as a standalone program; when invoked with --rebasing (from git-rebase--am.sh), cascade control back to the ultimate caller git-rebase.sh to do this for us.
--- git-am.sh | 9 +++++++-- git-rebase--am.sh | 8 ++++---- 2 files changed, 11 insertions(+), 6 deletions(-)
git-am.sh +7 -2
@@ -906,5 +906,10 @@ if test -s "$dotest"/rewritten; then fi fi -rm -fr "$dotest" -git gc --auto +# If am was called with --rebasing (from git-rebase--am), it's up to +# the caller to take care of housekeeping. +if ! test -f "$dotest/rebasing" +then + rm -fr "$dotest" + git gc --auto +fi
git-rebase--am.sh +4 -4
@@ -7,12 +7,12 @@ case "$action" in continue) git am --resolved --resolvemsg="$resolvemsg" && move_to_original_branch - exit + return ;; skip) git am --skip --resolvemsg="$resolvemsg" && move_to_original_branch - exit + return ;; esac
@@ -56,7 +56,7 @@ else As a result, git cannot rebase them. EOF - exit $? + return $? fi git am $git_am_opt --rebasing --resolvemsg="$resolvemsg" <"$GIT_DIR/rebased-patches"
@@ -68,7 +68,7 @@ fi if test 0 != $ret then test -d "$state_dir" && write_basic_state - exit $ret + return $ret fi move_to_original_branch
Signature · 1 line
1.8.3.rc1.51.gd7a04de
PATCH v3 5/7 rebase -i: return control to caller, for housekeeping
In reply to Ramkumar RamachandraReturn control to the caller git-rebase.sh to get these two tasks
rm -fr "$dotest"
git gc --auto
done by it.
--- git-rebase--interactive.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
git-rebase--interactive.sh +5 -4
@@ -628,17 +628,16 @@ do_next () { "$GIT_DIR"/hooks/post-rewrite rebase < "$rewritten_list" true # we don't care if this hook failed fi && - rm -rf "$state_dir" && - git gc --auto && warn "Successfully rebased and updated $head_name." - exit + return 1 # not failure; just to break the do_rest loop } +# can only return 0, when the infinite loop breaks do_rest () { while : do - do_next + do_next || break done }
@@ -805,11 +804,13 @@ first and then run 'git rebase --continue' again." require_clean_work_tree "rebase" do_rest + return 0 ;; skip) git rerere clear do_rest + return 0 ;; edit-todo) git stripspace --strip-comments <"$todo" >"$todo".new
Signature · 1 line
1.8.3.rc1.51.gd7a04de
PATCH v3 6/7 rebase --merge: return control to caller, for housekeeping
In reply to Ramkumar RamachandraReturn control to the caller git-rebase.sh to get these two tasks
rm -fr "$dotest"
git gc --auto
done by it.
--- git-rebase--merge.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
git-rebase--merge.sh +2 -3
@@ -96,7 +96,6 @@ finish_rb_merge () { "$GIT_DIR"/hooks/post-rewrite rebase <"$state_dir"/rewritten fi fi - rm -r "$state_dir" say All done. }
@@ -110,7 +109,7 @@ continue) continue_merge done finish_rb_merge - exit + return ;; skip) read_state
@@ -122,7 +121,7 @@ skip) continue_merge done finish_rb_merge - exit + return ;; esac
Signature · 1 line
1.8.3.rc1.51.gd7a04de
PATCH v3 7/7 rebase: implement --[no-]autostash and rebase.autostash
In reply to Ramkumar RamachandraThis new feature allows a rebase to be executed on a dirty worktree or index. It works by creating a temporary "dangling merge commit" out of the worktree and index changes (via 'git stash create'), and automatically applying it after a successful rebase or abort.
rebase stores the SHA-1 hex of the temporary merge commit, along with the rest of the rebase state, in either .git/{rebase-merge,rebase-apply}/autostash depending on the kind of rebase. Since $state_dir is automatically removed at the end of a successful rebase or abort, so is the autostash.
The advantage of this approach is that we do not affect the normal stash's reflogs, making the autostash invisible to the end-user. This means that you can use 'git stash' during a rebase as usual.
When the autostash application results in a conflict, we push $state_dir/autostash onto the normal stash and remove $state_dir ending the rebase. The user can inspect the stash, and pop or drop at any time.
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.
--- Documentation/config.txt | 8 +++ Documentation/git-rebase.txt | 10 +++ git-rebase.sh | 43 ++++++++++++- t/t3420-rebase-autostash.sh | 148 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 206 insertions(+), 3 deletions(-) create mode 100755 t/t3420-rebase-autostash.sh
Documentation/config.txt +8 -0
@@ -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
Documentation/git-rebase.txt +10 -0
@@ -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
git-rebase.sh +40 -3
85 lines not on this page · raw message
t/t3420-rebase-autostash.sh new file +148 -0
148 lines not on this page · raw message
Signature · 1 line
1.8.3.rc1.51.gd7a04de
Re: [PATCH 7/7] rebase: implement --[no-]autostash and rebase.autostash
git-rebase.sh move_to_original_branch () { · line 159
Quoted 12 lines · Ramkumar Ramachandra
git-rebase.sh +12
-0+finish_rebase () { + if test -f "$state_dir/autostash" + then + stash_sha1=$(cat "$state_dir/autostash") + if git stash apply $stash_sha1 2>&1 >/dev/null + then + echo "Applied autostash" + else + ref_stash=refs/stash && + : >>"$GIT_DIR/logs/$ref_stash" && + git update-ref -m "autostash" $ref_stash $stash_sha1 \ + || die "$(eval_gettext 'Cannot store $stash_sha1')"
Writing it like this:
ref_stash=refs/stash && : >>"$GIT_DIR/logs/$ref_stash" && git update-ref -m "autostash" $ref_stash $stash_sha1 || die "$(eval_gettext 'Cannot store $stash_sha1')"
with a blank line before the next "echo", it would be more readable.
As I said in a separate message, having a code that knows where "stash" is and how it is organized outside the implementation of "git stash" is less than ideal. It probably makes more sense to let programs like this to say:
git stash store -m "autostash" $stash_sha1 || exit
git-rebase.sh move_to_original_branch () { · line 163
Quoted 4 lines
git-rebase.sh +4
-0+ echo " +$(gettext 'Applying autostash resulted in conflicts. +Your changes are safe in the stash. +You can apply or drop it at any time.')"
This feels funny. Why not
gettext "$msg"
without an extra command substitution with an echo?
Re: [PATCH 7/7] rebase: implement --[no-]autostash and rebase.autostash
Quoted 7 lines · Junio C Hamano
Writing it like this:
[...]
with a blank line before the next "echo", it would be more readable.This feels funny. Why not
[...]
without an extra command substitution with an echo?
I'll re-roll if there are more comments. Otherwise, can you fix these up locally? Thanks.
Quoted 1 line
git stash store -m "autostash" $stash_sha1 || exit
Definitely nicer. I'll write follow-ups for pull and stash.
Re: [PATCH 7/7] rebase: implement --[no-]autostash and rebase.autostash
Quoted 11 lines · Ramkumar Ramachandra
Quoted 7 lines · Junio C Hamano
Writing it like this:
[...]
with a blank line before the next "echo", it would be more readable.This feels funny. Why not
[...]
without an extra command substitution with an echo?I'll re-roll if there are more comments. Otherwise, can you fix these up locally? Thanks.
No, thanks. I won't be even taking the patch right now, so you have plenty of time ;-).
Re: [PATCH 7/7] rebase: implement --[no-]autostash and rebase.autostash
Quoted 2 lines · Junio C Hamano
No, thanks. I won't be even taking the patch right now, so you have plenty of time ;-).
There were some additional comments from Matthieu, so I will re-roll (just this part). I've even posted a stash series based on this one. Do you have any additional comments?
Re: [PATCH 7/7] rebase: implement --[no-]autostash and rebase.autostash
In reply to Ramkumar Ramachandragit-rebase.sh move_to_original_branch () { · line 154
Quoted 7 lines · Ramkumar Ramachandra
git-rebase.sh +7
-0+finish_rebase () { + if test -f "$state_dir/autostash" + then + stash_sha1=$(cat "$state_dir/autostash") + if git stash apply $stash_sha1 2>&1 >/dev/null + then + echo "Applied autostash"
Any reason why this is not using gettext and the other messages do.
git-rebase.sh move_to_original_branch () { · line 163
Quoted 9 lines
git-rebase.sh +9
-0+ else + ref_stash=refs/stash && + : >>"$GIT_DIR/logs/$ref_stash" && + git update-ref -m "autostash" $ref_stash $stash_sha1 \ + || die "$(eval_gettext 'Cannot store $stash_sha1')" + echo " +$(gettext 'Applying autostash resulted in conflicts. +Your changes are safe in the stash. +You can apply or drop it at any time.')"
Good idea to put the autostash in an actual stash. Perhaps the message can be made more explicit, e.g.
You can run "git stash apply" or "git stash drop" at any time.
(actually, "git stash pop" may be a better suggestion to "git stash apply" here).
Signature · 2 lines
Matthieu Moy http://www-verimag.imag.fr/~moy/
Re: [PATCH 7/7] rebase: implement --[no-]autostash and rebase.autostash
Quoted 2 lines · Matthieu Moy
Any reason why this is not using gettext and the other messages do.
You can run "git stash apply" or "git stash drop" at any time.
Fixed. Thanks.
Re: [PATCH 7/7] rebase: implement --[no-]autostash and rebase.autostash
Quoted 5 lines · Ramkumar Ramachandra
Quoted 2 lines · Matthieu Moy
Any reason why this is not using gettext and the other messages do.
You can run "git stash apply" or "git stash drop" at any time.Fixed. Thanks.
After thinking a bit, I have another nit about the message: it's not clear whether it's saying "your changes are in the work tree, but with conflicts so you may want to retry later with "git stash pop"", or "I could not apply your changes at all, do it yourself with "git stash pop"".
I suppose it's the former, but it could be more explicit.
Signature · 2 lines
Matthieu Moy http://www-verimag.imag.fr/~moy/
Quoted 4 lines · Ramkumar Ramachandra
Minor changes since v2 in response to reviews by Junio and Eric Sunshine.
Should be ready to merge soon.
That, and the "completed" on the Subject line are not for you to decide, I would have to say ;-)
Overall the patches look cleanly done, but I did not carefully look into the implementation. Especially I did not check if there are still undesirable data loss behaviour in corner cases that people were worried about in the discussion. It would have been nice to CC people who voiced concerns during the previous reviews.
A few comments on possible follow-up work, not meant as a suggestion to include in this series:
* The series shows that it is necessary to first prepare a stash entry with "stash create", then later decide to queue it to the stash (or decide not to do so) in some applications.
In the longer term, it is unmaintainable to make such users (like this new code) of the stash mechanism manually do so, and we may want to add a "git stash __store" subcommand, not meant for the interactive use of end users. The implementation of the stash can later be changed without affecting such users by doing so.
* The primary reason why you wanted this autostash was because "rebase" is implemented in a lazy and stupid way, compared to "merge" that detects possible conflicts with local changes and refrains from touching the working tree at all, and any local change that do not interfere with the merge are permitted.
Perhaps "rebase" can be taught to be more careful when checking if local changes may overlap with the changes being replayed. When replaying a range A..B on top of the onto commit O, perhaps "rebase" can at least do these:
- If the index is dirty with respect to HEAD, stop just like "merge" does.
- Take a union of paths different between O and the working tree, and untracked new paths in the working tree. These are the possible "local changes". If there is no way the replay of A..B touches any of these paths, we do not even have to worry about stashing.
- Take output from "diff-tree -m --name-only" for each commit in the replayed range (note that I am not adding -M to take maximal coverage). Check if any of the paths overlap with the "local changes", and if so, stop.
to be par with "merge".
When the latter is done, there is _no_ justification for autostash to be an option specific fo "rebase. At that point, "rebase" and "merge" would refuse to start in the same situation, i.e. the local changes you have are known to interfere with the integration with the branch, diminishing the need to "autostash" greatly.
When autostash is still needed to proceed after such change to "rebase", I suspect that the user may be better off saving such work-in-progress in a temporary commit (possibly after making it into a better shape before even starting to "rebase" or "merge") than using a transient mechanism like stash to save such a work, because it is likely that such local changes have a real conflict with the work being integrated.
Those who still want to use stash would benefit from having an autostash, but at that point, there is no reason to single out "rebase" for the autostash feature. Those who want to stash immediately before a "merge" that is known to conflict can use the same autostash and may want to do so for exactly the same reason they may want to use it for "rebase".
Quoted 3 lines · Junio C Hamano
Especially I did not check if there are
still undesirable data loss behaviour in corner cases that people were worried about in the discussion.
Check the tests. What am I missing?
Quoted 5 lines
In the longer term, it is unmaintainable to make such users (like this new code) of the stash mechanism manually do so, and we may want to add a "git stash
__store" subcommand, not meant for the interactive use of end users. The implementation of the stash can later be changed without affecting such users by doing so.
Yes, a "store" that stores a commit created with "create" would be nice. Why the horrible double-underscore though? "git stash create" is not meant for interactive use of end users either.
Quoted 2 lines
Perhaps "rebase" can be taught to be more careful when checking if local changes may overlap with the changes being replayed.
Frankly, I don't know if it's worth the effort. It might be a nice theoretical exercise, but what tangible benefit do I get as the end user (now that I have rebase.autostash)? In fact, I'll probably be slowing down the interactive rebase noticeably by executing a diff-tree at every step. And for what?
Quoted 6 lines
Those who still want to use stash would benefit from having an autostash, but at that point, there is no reason to single out "rebase" for the autostash feature. Those who want to stash immediately before a "merge" that is known to conflict can use the same autostash and may want to do so for exactly the same reason they may want to use it for "rebase".
Each command that wants to have autostash will have to implement it independently. You argued that an implicit merge.autostash may be harmful earlier: maybe an explicit --autostash; but at this point, the returns are diminishing: what is the great advantage of --autostash over a quick manual g ss, g sp (git stash save, git stash pop)? I don't know what problem you're trying to solve anymore.
Quoted 13 lines · Ramkumar Ramachandra
Quoted 3 lines · Junio C Hamano
Especially I did not check if there are
still undesirable data loss behaviour in corner cases that people were worried about in the discussion.Check the tests. What am I missing?
Quoted 5 lines
In the longer term, it is unmaintainable to make such users (like this new code) of the stash mechanism manually do so, and we may want to add a "git stash
__store" subcommand, not meant for the interactive use of end users. The implementation of the stash can later be changed without affecting such users by doing so.
Quoted 3 lines
Yes, a "store" that stores a commit created with "create" would be nice. Why the horrible double-underscore though? "git stash create" is not meant for interactive use of end users either.
"create" is not advertised very widely, but "store" is too close to what is already familiar to the people "save" and we really do not want to confuse them. "store -m message commit" sounds as if you are creating a stash to apply to the given $commit.
Quoted 4 lines · Junio C Hamano
"create" is not advertised very widely, but "store" is too close to what is already familiar to the people "save" and we really do not want to confuse them. "store -m message commit" sounds as if you are creating a stash to apply to the given $commit.
In the store series I posted a few minutes ago, I use the format "store <commit> <message>". I've not advertised it in the usage (like create), and documented it just like create. Maybe we should add the line "Not for end user interactive use" to both descriptions? We can get that double-underscore if you really want, but it'd stick out like a sore thumb since we can't change create.
Quoted 4 lines · Ramkumar Ramachandra
Maybe we should add the
line "Not for end user interactive use" to both descriptions? We can get that double-underscore if you really want, but it'd stick out like a sore thumb since we can't change create.
Yeah, good point about the __name.
It was a mistake that the description of 'create' does not have "This is probably not what you want to use; see 'save'" there. Let's do that, and do the same for 'store' (that is, not advertise in 'git stash -h' output, describe in the manual for scripters, and mark it not for the end user in the description).
Quoted 6 lines · Ramkumar Ramachandra
Quoted 3 lines · Junio C Hamano
Especially I did not check if there are
still undesirable data loss behaviour in corner cases that people were worried about in the discussion.Check the tests. What am I missing?
I didn't do a thorough check, but my earlier comments are taken into account, I didn't see anything wrong and the tests in 7/7 are good.
Quoted 8 lines
Quoted 2 lines
Perhaps "rebase" can be taught to be more careful when checking if local changes may overlap with the changes being replayed.
Frankly, I don't know if it's worth the effort. It might be a nice theoretical exercise, but what tangible benefit do I get as the end user (now that I have rebase.autostash)? In fact, I'll probably be slowing down the interactive rebase noticeably by executing a diff-tree at every step. And for what?
One benefit would be to avoid triggering rebuild (and editor reload) by keeping the timestamps intact. But I agree it's probably not worth the effort (and definitely isn't in the scope of this series).
Signature · 2 lines
Matthieu Moy http://www-verimag.imag.fr/~moy/
Quoted 3 lines · Matthieu Moy
One benefit would be to avoid triggering rebuild (and editor reload) by keeping the timestamps intact. But I agree it's probably not worth the effort (and definitely isn't in the scope of this series).
It isn't in the scope, of course. If rebase were done right, the series will become much less necessary in the first place ;-).