Skip to content

Commit 3013d98

Browse files
phil-blaingitster
authored andcommitted
pull --rebase: honor rebase.autostash when fast-forwarding
"pull --rebase" internally uses the merge machinery when the other history is a descendant of ours (i.e. perform fast-forward). This came from [1], where the discussion was started from a feature request to do so. It is a bit hard to read the rationale behind it in the discussion, but it seems that it was an established fact for everybody involved that does not even need to be mentioned that fast-forwarding done with "rebase" was much undesirable than done with "merge", and more importantly, the result left by "merge" is as good as (or better than) that by "rebase". Except for one thing. Because "git merge" does not (and should not) honor rebase.autostash, "git pull" needs to read it and forward it when we use "git merge" as a (hopefully better) substitute for "git rebase" during the fast-forwarding. But we forgot to do so (we only add "--[no-]autostash" to the "git merge" command when "git pull" itself was invoked with "--[no-]autostash" command line option. Make sure "git merge" is run with "--autostash" when rebase.autostash is set and used to fast-forward the history on behalf of "git rebase". Incidentally this change also takes care of the case where - "git pull --rebase" (without other command line options) is run - "rebase.autostash" is not set - The history fast-forwards In such a case, "git merge" is run with an explicit "--no-autostash" to prevent it from honoring merge.autostash configuration, which is what we want. After all, we want the "git merge" to pretend as if it is "git rebase" while being used for this purpose. [1] https://lore.kernel.org/git/[email protected]/ Reported-by: Tilman Vogel <[email protected]> Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Philippe Blain <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e9d7761 commit 3013d98

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

builtin/pull.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,14 +1036,13 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
10361036
oidclr(&orig_head);
10371037

10381038
if (opt_rebase) {
1039-
int autostash = config_autostash;
1040-
if (opt_autostash != -1)
1041-
autostash = opt_autostash;
1039+
if (opt_autostash == -1)
1040+
opt_autostash = config_autostash;
10421041

10431042
if (is_null_oid(&orig_head) && !is_cache_unborn())
10441043
die(_("Updating an unborn branch with changes added to the index."));
10451044

1046-
if (!autostash)
1045+
if (!opt_autostash)
10471046
require_clean_work_tree(the_repository,
10481047
N_("pull with rebase"),
10491048
_("please commit or stash them."), 1, 0);

t/t5520-pull.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,19 @@ test_expect_success '--rebase --autostash fast forward' '
330330
test_cmp_rev HEAD to-rebase-ff
331331
'
332332

333+
test_expect_success '--rebase with rebase.autostash succeeds on ff' '
334+
test_when_finished "rm -fr src dst actual" &&
335+
git init src &&
336+
test_commit -C src "initial" file "content" &&
337+
git clone src dst &&
338+
test_commit -C src --printf "more_content" file "more content\ncontent\n" &&
339+
echo "dirty" >>dst/file &&
340+
test_config -C dst rebase.autostash true &&
341+
git -C dst pull --rebase >actual 2>&1 &&
342+
grep -q "Fast-forward" actual &&
343+
grep -q "Applied autostash." actual
344+
'
345+
333346
test_expect_success '--rebase with conflicts shows advice' '
334347
test_when_finished "git rebase --abort; git checkout -f to-rebase" &&
335348
git checkout -b seq &&

0 commit comments

Comments
 (0)