git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lidong Yan <yldhome2d2@gmail.com>
To: yldhome2d2@gmail.com
Cc: git@vger.kernel.org, gitster@pobox.com, hi@looping.me,
	j6t@kdbg.org, sunshine@sunshineco.com
Subject: [PATCH v2] pull: add pull.autoStash config option
Date: Sun, 20 Jul 2025 20:43:34 +0800	[thread overview]
Message-ID: <20250720124334.12045-1-yldhome2d2@gmail.com> (raw)
In-Reply-To: <20250718035221.2293-1-yldhome2d2@gmail.com>

Git uses `rebase.autostash` or `merge.autostash` to determine whether a
dirty worktree is allowed during pull. However, this behavior is not
clearly documented, making it difficult for users to discover how to
enable autostash, or causing them to unknowingly enable it. Add new
config option `pull.autostash` along with its documentation and test
cases.

`pull.autostash` provides the same functionality as `rebase.autostash`
and `merge.autostash`, but overrides them when set. If `pull.autostash`
is not set, it falls back to `rebase.autostash` or `merge.autostash`,
depending on the value of `pull.rebase`.

Signed-off-by: Lidong Yan <yldhome2d2@gmail.com>
---
Range-diff against v1:
1:  5b7d10d7e9 ! 1:  51a7c66783 pull: add pull.autoStash config option
    @@ Commit message
         Signed-off-by: Lidong Yan <yldhome2d2@gmail.com>
     
      ## Documentation/config/pull.adoc ##
    -@@ Documentation/config/pull.adoc: pull.rebase::
    - 	of merging the default branch from the default remote when "git
    - 	pull" is run. See "branch.<name>.rebase" for setting this on a
    - 	per-branch basis.
    -+
    +@@ Documentation/config/pull.adoc: pull.octopus::
    + 	The default merge strategy to use when pulling multiple branches
    + 	at once.
    + 
     +pull.autoStash::
    -+	When true, Git will automatically perform a `git stash` before the
    -+	operation and then restore the local changes with `git stash pop`
    -+	after the pull is complete. This means that you can run pull on a
    -+	dirty worktree. If `pull.autostash` is set, it takes precedence over
    -+	`rebase.autostash` and `merge.autostash`. If `pull.autostash` is not
    -+	set, it falls back to `rebase.autostash` or `merge.autostash`,
    -+	depending on the value of `pull.rebase`. This option can be
    -+	overridden by the `--no-autostash` and `--autostash` options of
    -+	linkgit:git-pull[1]. Defaults to false.
    - +
    - When `merges` (or just 'm'), pass the `--rebase-merges` option to 'git rebase'
    - so that the local merge commits are included in the rebase (see
    ++	When set to true, automatically create a temporary stash entry
    ++	to record the local changes before the operation begins, and
    ++	restore them after the operation completes.  When your "git
    ++	pull" rebases (instead of merges), this may be convenient, since
    ++	unlike merging pull that tolerates local changes that do not
    ++	interfere with the merge, rebasing pull refuses to work with any
    ++	local changes.
    +++
    ++If `pull.autostash` is set (either to true or false),
    ++`merge.autostash` and `rebase.autostash` are ignored.  If
    ++`pull.autostash` is not set at all, depending on the value of
    ++`pull.rebase`, `merge.autostash` or `rebase.autostash` is used
    ++instead.  Can be overridden by the `--[no-]autostash` command line
    ++option.
    ++
    + pull.twohead::
    + 	The default merge strategy to use when pulling a single branch.
     
      ## builtin/pull.c ##
     @@ builtin/pull.c: static char *opt_ff;
    @@ t/t5520-pull.sh: test_expect_success 'pull --no-autostash & merge.autostash unse
      	test_pull_autostash_fail --no-autostash --no-rebase
      '
      
    -+test_expect_success 'pull succeeds with dirty working directory and pull.autostash set' '
    ++test_expect_success 'pull succeeds with dirty working directory and pull.autostash=true' '
     +	test_config pull.autostash true &&
     +	test_pull_autostash 1 --rebase &&
    -+	test_pull_autostash 2 --no-rebase
    -+'
    -+
    -+test_expect_success 'pull --autostash & pull.autostash=true' '
    -+	test_config pull.autostash true &&
    ++	test_pull_autostash 2 --no-rebase &&
     +	test_pull_autostash 1 --autostash --rebase &&
     +	test_pull_autostash 2 --autostash --no-rebase
     +'
     +
    -+test_expect_success 'pull --autostash & pull.autostash=false' '
    ++test_expect_success 'pull fails with dirty working directory and pull.autostash=false' '
     +	test_config pull.autostash false &&
    -+	test_pull_autostash 1 --autostash --rebase &&
    -+	test_pull_autostash 2 --autostash --no-rebase
    -+'
    -+
    -+test_expect_success 'pull --autostash & pull.autostash unset' '
    -+	test_unconfig pull.autostash &&
    -+	test_pull_autostash 1 --autostash --rebase &&
    -+	test_pull_autostash 2 --autostash --no-rebase
    -+'
    -+
    -+test_expect_success 'pull --no-autostash & pull.autostash=true' '
    -+	test_config pull.autostash true &&
    ++	test_pull_autostash_fail --rebase &&
    ++	test_pull_autostash_fail --no-rebase &&
     +	test_pull_autostash_fail --no-autostash --rebase &&
     +	test_pull_autostash_fail --no-autostash --no-rebase
     +'
     +
    -+test_expect_success 'pull --no-autostash & pull.autostash=false' '
    ++test_expect_success 'pull --autostash overrides pull.autostash=false' '
     +	test_config pull.autostash false &&
    -+	test_pull_autostash_fail --no-autostash --rebase &&
    -+	test_pull_autostash_fail --no-autostash --no-rebase
    ++	test_pull_autostash 1 --autostash --rebase &&
    ++	test_pull_autostash 2 --autostash --no-rebase
     +'
     +
    -+test_expect_success 'pull --no-autostash & pull.autostash unset' '
    -+	test_unconfig pull.autostash &&
    ++test_expect_success 'pull --no-autostash overrides pull.autostash=true' '
    ++	test_config pull.autostash true &&
     +	test_pull_autostash_fail --no-autostash --rebase &&
     +	test_pull_autostash_fail --no-autostash --no-rebase
     +'
     +
    -+test_expect_success 'pull.autostash=true & rebase.autostash=true' '
    ++test_expect_success 'pull.autostash=true overrides rebase.autostash' '
     +	test_config pull.autostash true &&
     +	test_config rebase.autostash true &&
    -+	test_pull_autostash 1 --rebase
    -+'
    -+
    -+test_expect_success 'pull.autostash=true & rebase.autostash=false' '
    -+	test_config pull.autostash true &&
    ++	test_pull_autostash 1 --rebase &&
     +	test_config rebase.autostash false &&
     +	test_pull_autostash 1 --rebase
     +'
     +
    -+test_expect_success 'pull.autostash=false & rebase.autostash=true' '
    ++test_expect_success 'pull.autostash=false overrides rebase.autostash' '
     +	test_config pull.autostash false &&
     +	test_config rebase.autostash true &&
    -+	test_pull_autostash_fail --rebase
    -+'
    -+
    -+test_expect_success 'pull.autostash=false & rebase.autostash=false' '
    -+	test_config pull.autostash false &&
    ++	test_pull_autostash_fail --rebase &&
     +	test_config rebase.autostash false &&
     +	test_pull_autostash_fail --rebase
     +'
     +
    -+test_expect_success 'pull.autostash=true & merge.autostash=true' '
    ++test_expect_success 'pull.autostash=true overrides merge.autostash' '
     +	test_config pull.autostash true &&
     +	test_config merge.autostash true &&
    -+	test_pull_autostash 2 --no-rebase
    -+'
    -+
    -+test_expect_success 'pull.autostash=true & merge.autostash=false' '
    -+	test_config pull.autostash true &&
    ++	test_pull_autostash 2 --no-rebase &&
     +	test_config merge.autostash false &&
     +	test_pull_autostash 2 --no-rebase
     +'
     +
    -+test_expect_success 'pull.autostash=false & merge.autostash=true' '
    ++test_expect_success 'pull.autostash=false overrides merge.autostash' '
     +	test_config pull.autostash false &&
     +	test_config merge.autostash true &&
    -+	test_pull_autostash_fail --no-rebase
    -+'
    -+
    -+test_expect_success 'pull.autostash=false & merge.autostash=false' '
    -+	test_config pull.autostash false &&
    ++	test_pull_autostash_fail --no-rebase &&
     +	test_config merge.autostash false &&
     +	test_pull_autostash_fail --no-rebase
     +'

 Documentation/config/pull.adoc | 16 +++++++++
 builtin/pull.c                 | 20 ++++++++++--
 t/t5520-pull.sh                | 60 ++++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+), 3 deletions(-)

diff --git a/Documentation/config/pull.adoc b/Documentation/config/pull.adoc
index 9349e09261..125c930f72 100644
--- a/Documentation/config/pull.adoc
+++ b/Documentation/config/pull.adoc
@@ -29,5 +29,21 @@ pull.octopus::
 	The default merge strategy to use when pulling multiple branches
 	at once.
 
+pull.autoStash::
+	When set to true, automatically create a temporary stash entry
+	to record the local changes before the operation begins, and
+	restore them after the operation completes.  When your "git
+	pull" rebases (instead of merges), this may be convenient, since
+	unlike merging pull that tolerates local changes that do not
+	interfere with the merge, rebasing pull refuses to work with any
+	local changes.
++
+If `pull.autostash` is set (either to true or false),
+`merge.autostash` and `rebase.autostash` are ignored.  If
+`pull.autostash` is not set at all, depending on the value of
+`pull.rebase`, `merge.autostash` or `rebase.autostash` is used
+instead.  Can be overridden by the `--[no-]autostash` command line
+option.
+
 pull.twohead::
 	The default merge strategy to use when pulling a single branch.
diff --git a/builtin/pull.c b/builtin/pull.c
index c593f324fe..2a6c2e4a37 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -90,7 +90,8 @@ static char *opt_ff;
 static const char *opt_verify_signatures;
 static const char *opt_verify;
 static int opt_autostash = -1;
-static int config_autostash;
+static int config_rebase_autostash;
+static int config_pull_autostash = -1;
 static int check_trust_level = 1;
 static struct strvec opt_strategies = STRVEC_INIT;
 static struct strvec opt_strategy_opts = STRVEC_INIT;
@@ -367,7 +368,18 @@ static int git_pull_config(const char *var, const char *value,
 			   const struct config_context *ctx, void *cb)
 {
 	if (!strcmp(var, "rebase.autostash")) {
-		config_autostash = git_config_bool(var, value);
+		/*
+		 * run_rebase() also reads this option. The reason we handle it here is
+		 * that when pull.rebase is true, a fast-forward may occur without
+		 * invoking run_rebase(). We need to ensure that autostash is set even
+		 * in the fast-forward case.
+		 *
+		 * run_merge() handles merge.autostash, so we don't handle it here.
+		 */
+		config_rebase_autostash = git_config_bool(var, value);
+		return 0;
+	} else if (!strcmp(var, "pull.autostash")) {
+		config_pull_autostash = git_config_bool(var, value);
 		return 0;
 	} else if (!strcmp(var, "submodule.recurse")) {
 		recurse_submodules = git_config_bool(var, value) ?
@@ -1006,6 +1018,8 @@ int cmd_pull(int argc,
 	}
 
 	argc = parse_options(argc, argv, prefix, pull_options, pull_usage, 0);
+	if (opt_autostash == -1)
+		opt_autostash = config_pull_autostash;
 
 	if (recurse_submodules_cli != RECURSE_SUBMODULES_DEFAULT)
 		recurse_submodules = recurse_submodules_cli;
@@ -1052,7 +1066,7 @@ int cmd_pull(int argc,
 
 	if (opt_rebase) {
 		if (opt_autostash == -1)
-			opt_autostash = config_autostash;
+			opt_autostash = config_rebase_autostash;
 
 		if (is_null_oid(&orig_head) && !is_index_unborn(the_repository->index))
 			die(_("Updating an unborn branch with changes added to the index."));
diff --git a/t/t5520-pull.sh b/t/t5520-pull.sh
index 63c9a8f04b..0e0019347e 100755
--- a/t/t5520-pull.sh
+++ b/t/t5520-pull.sh
@@ -472,6 +472,66 @@ test_expect_success 'pull --no-autostash & merge.autostash unset' '
 	test_pull_autostash_fail --no-autostash --no-rebase
 '
 
+test_expect_success 'pull succeeds with dirty working directory and pull.autostash=true' '
+	test_config pull.autostash true &&
+	test_pull_autostash 1 --rebase &&
+	test_pull_autostash 2 --no-rebase &&
+	test_pull_autostash 1 --autostash --rebase &&
+	test_pull_autostash 2 --autostash --no-rebase
+'
+
+test_expect_success 'pull fails with dirty working directory and pull.autostash=false' '
+	test_config pull.autostash false &&
+	test_pull_autostash_fail --rebase &&
+	test_pull_autostash_fail --no-rebase &&
+	test_pull_autostash_fail --no-autostash --rebase &&
+	test_pull_autostash_fail --no-autostash --no-rebase
+'
+
+test_expect_success 'pull --autostash overrides pull.autostash=false' '
+	test_config pull.autostash false &&
+	test_pull_autostash 1 --autostash --rebase &&
+	test_pull_autostash 2 --autostash --no-rebase
+'
+
+test_expect_success 'pull --no-autostash overrides pull.autostash=true' '
+	test_config pull.autostash true &&
+	test_pull_autostash_fail --no-autostash --rebase &&
+	test_pull_autostash_fail --no-autostash --no-rebase
+'
+
+test_expect_success 'pull.autostash=true overrides rebase.autostash' '
+	test_config pull.autostash true &&
+	test_config rebase.autostash true &&
+	test_pull_autostash 1 --rebase &&
+	test_config rebase.autostash false &&
+	test_pull_autostash 1 --rebase
+'
+
+test_expect_success 'pull.autostash=false overrides rebase.autostash' '
+	test_config pull.autostash false &&
+	test_config rebase.autostash true &&
+	test_pull_autostash_fail --rebase &&
+	test_config rebase.autostash false &&
+	test_pull_autostash_fail --rebase
+'
+
+test_expect_success 'pull.autostash=true overrides merge.autostash' '
+	test_config pull.autostash true &&
+	test_config merge.autostash true &&
+	test_pull_autostash 2 --no-rebase &&
+	test_config merge.autostash false &&
+	test_pull_autostash 2 --no-rebase
+'
+
+test_expect_success 'pull.autostash=false overrides merge.autostash' '
+	test_config pull.autostash false &&
+	test_config merge.autostash true &&
+	test_pull_autostash_fail --no-rebase &&
+	test_config merge.autostash false &&
+	test_pull_autostash_fail --no-rebase
+'
+
 test_expect_success 'pull.rebase' '
 	git reset --hard before-rebase &&
 	test_config pull.rebase true &&
-- 
2.39.5 (Apple Git-154)


  parent reply	other threads:[~2025-07-20 12:43 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-15  3:26 [BUG] git pull ignores pull.autostash=true configuration when used with --git-dir and --work-tree flags on a bare repository Bryan Lee
2025-07-15  4:09 ` Lidong Yan
2025-07-15  5:31   ` Bryan Lee
2025-07-15  5:31   ` Bryan Lee
2025-07-15 15:02     ` Lidong Yan
2025-07-15 20:46       ` Junio C Hamano
2025-07-16  1:39         ` Lidong Yan
2025-07-16  5:55           ` Johannes Sixt
2025-07-16 11:20             ` Lidong Yan
2025-07-16 15:16             ` Junio C Hamano
2025-07-17  3:07               ` [PATCH] pull: add pull.autoStash config option Lidong Yan
2025-07-17  3:27                 ` Eric Sunshine
2025-07-17  4:09                   ` Lidong Yan
2025-07-17  4:37                     ` Junio C Hamano
2025-07-17  5:01                       ` Lidong Yan
2025-07-17  5:48                         ` Junio C Hamano
2025-07-17  4:44                   ` Junio C Hamano
2025-07-18  3:52                 ` Lidong Yan
2025-07-18 22:13                   ` Junio C Hamano
2025-07-19  3:14                     ` Lidong Yan
2025-07-20 12:43                   ` Lidong Yan [this message]
2025-07-21 22:10                     ` [PATCH v2] " Junio C Hamano
2025-07-17 19:32               ` [BUG] git pull ignores pull.autostash=true configuration when used with --git-dir and --work-tree flags on a bare repository Ben Knoble

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=20250720124334.12045-1-yldhome2d2@gmail.com \
    --to=yldhome2d2@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=hi@looping.me \
    --cc=j6t@kdbg.org \
    --cc=sunshine@sunshineco.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).