Skip to content

Commit 6fe666b

Browse files
committed
Merge branch 'ly/pull-autostash'
"git pull" learned to pay attention to pull.autostash configuration variable, which overrides rebase/merge.autostash. * ly/pull-autostash: pull: add pull.autoStash config option
2 parents cbcde15 + e337860 commit 6fe666b

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

Documentation/config/pull.adoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,21 @@ pull.octopus::
2929
The default merge strategy to use when pulling multiple branches
3030
at once.
3131

32+
pull.autoStash::
33+
When set to true, automatically create a temporary stash entry
34+
to record the local changes before the operation begins, and
35+
restore them after the operation completes. When your "git
36+
pull" rebases (instead of merges), this may be convenient, since
37+
unlike merging pull that tolerates local changes that do not
38+
interfere with the merge, rebasing pull refuses to work with any
39+
local changes.
40+
+
41+
If `pull.autostash` is set (either to true or false),
42+
`merge.autostash` and `rebase.autostash` are ignored. If
43+
`pull.autostash` is not set at all, depending on the value of
44+
`pull.rebase`, `merge.autostash` or `rebase.autostash` is used
45+
instead. Can be overridden by the `--[no-]autostash` command line
46+
option.
47+
3248
pull.twohead::
3349
The default merge strategy to use when pulling a single branch.

builtin/pull.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ static char *opt_ff;
9090
static const char *opt_verify_signatures;
9191
static const char *opt_verify;
9292
static int opt_autostash = -1;
93-
static int config_autostash;
93+
static int config_rebase_autostash;
94+
static int config_pull_autostash = -1;
9495
static int check_trust_level = 1;
9596
static struct strvec opt_strategies = STRVEC_INIT;
9697
static struct strvec opt_strategy_opts = STRVEC_INIT;
@@ -367,7 +368,18 @@ static int git_pull_config(const char *var, const char *value,
367368
const struct config_context *ctx, void *cb)
368369
{
369370
if (!strcmp(var, "rebase.autostash")) {
370-
config_autostash = git_config_bool(var, value);
371+
/*
372+
* run_rebase() also reads this option. The reason we handle it here is
373+
* that when pull.rebase is true, a fast-forward may occur without
374+
* invoking run_rebase(). We need to ensure that autostash is set even
375+
* in the fast-forward case.
376+
*
377+
* run_merge() handles merge.autostash, so we don't handle it here.
378+
*/
379+
config_rebase_autostash = git_config_bool(var, value);
380+
return 0;
381+
} else if (!strcmp(var, "pull.autostash")) {
382+
config_pull_autostash = git_config_bool(var, value);
371383
return 0;
372384
} else if (!strcmp(var, "submodule.recurse")) {
373385
recurse_submodules = git_config_bool(var, value) ?
@@ -1006,6 +1018,8 @@ int cmd_pull(int argc,
10061018
}
10071019

10081020
argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
1021+
if (opt_autostash == -1)
1022+
opt_autostash = config_pull_autostash;
10091023

10101024
if (recurse_submodules_cli != RECURSE_SUBMODULES_DEFAULT)
10111025
recurse_submodules = recurse_submodules_cli;
@@ -1052,7 +1066,7 @@ int cmd_pull(int argc,
10521066

10531067
if (opt_rebase) {
10541068
if (opt_autostash == -1)
1055-
opt_autostash = config_autostash;
1069+
opt_autostash = config_rebase_autostash;
10561070

10571071
if (is_null_oid(&orig_head) && !is_index_unborn(the_repository->index))
10581072
die(_("Updating an unborn branch with changes added to the index."));

t/t5520-pull.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,66 @@ test_expect_success 'pull --no-autostash & merge.autostash unset' '
472472
test_pull_autostash_fail --no-autostash --no-rebase
473473
'
474474

475+
test_expect_success 'pull succeeds with dirty working directory and pull.autostash=true' '
476+
test_config pull.autostash true &&
477+
test_pull_autostash 1 --rebase &&
478+
test_pull_autostash 2 --no-rebase &&
479+
test_pull_autostash 1 --autostash --rebase &&
480+
test_pull_autostash 2 --autostash --no-rebase
481+
'
482+
483+
test_expect_success 'pull fails with dirty working directory and pull.autostash=false' '
484+
test_config pull.autostash false &&
485+
test_pull_autostash_fail --rebase &&
486+
test_pull_autostash_fail --no-rebase &&
487+
test_pull_autostash_fail --no-autostash --rebase &&
488+
test_pull_autostash_fail --no-autostash --no-rebase
489+
'
490+
491+
test_expect_success 'pull --autostash overrides pull.autostash=false' '
492+
test_config pull.autostash false &&
493+
test_pull_autostash 1 --autostash --rebase &&
494+
test_pull_autostash 2 --autostash --no-rebase
495+
'
496+
497+
test_expect_success 'pull --no-autostash overrides pull.autostash=true' '
498+
test_config pull.autostash true &&
499+
test_pull_autostash_fail --no-autostash --rebase &&
500+
test_pull_autostash_fail --no-autostash --no-rebase
501+
'
502+
503+
test_expect_success 'pull.autostash=true overrides rebase.autostash' '
504+
test_config pull.autostash true &&
505+
test_config rebase.autostash true &&
506+
test_pull_autostash 1 --rebase &&
507+
test_config rebase.autostash false &&
508+
test_pull_autostash 1 --rebase
509+
'
510+
511+
test_expect_success 'pull.autostash=false overrides rebase.autostash' '
512+
test_config pull.autostash false &&
513+
test_config rebase.autostash true &&
514+
test_pull_autostash_fail --rebase &&
515+
test_config rebase.autostash false &&
516+
test_pull_autostash_fail --rebase
517+
'
518+
519+
test_expect_success 'pull.autostash=true overrides merge.autostash' '
520+
test_config pull.autostash true &&
521+
test_config merge.autostash true &&
522+
test_pull_autostash 2 --no-rebase &&
523+
test_config merge.autostash false &&
524+
test_pull_autostash 2 --no-rebase
525+
'
526+
527+
test_expect_success 'pull.autostash=false overrides merge.autostash' '
528+
test_config pull.autostash false &&
529+
test_config merge.autostash true &&
530+
test_pull_autostash_fail --no-rebase &&
531+
test_config merge.autostash false &&
532+
test_pull_autostash_fail --no-rebase
533+
'
534+
475535
test_expect_success 'pull.rebase' '
476536
git reset --hard before-rebase &&
477537
test_config pull.rebase true &&

0 commit comments

Comments
 (0)