diff mbox series

[v8,10/10] pull: improve default warning

Message ID 20201125032938.786393-11-felipe.contreras@gmail.com (mailing list archive)
State New, archived
Headers show
Series Reject non-ff pulls by default | expand

Commit Message

Felipe Contreras Nov. 25, 2020, 3:29 a.m. UTC
Also, use pull.mode to determine when to show it, and --ff/--no-ff
shouldn't squelch the warning.

Also, improve the documentation so "git pull --help" actually does
explain what's going on.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 Documentation/git-pull.txt   | 17 +++++++++++++++
 builtin/pull.c               | 34 +++++++++++++++---------------
 t/t5520-pull.sh              | 14 +++++++++++++
 t/t7601-merge-pull-config.sh | 40 +++++++-----------------------------
 4 files changed, 55 insertions(+), 50 deletions(-)
diff mbox series

Patch

diff --git a/Documentation/git-pull.txt b/Documentation/git-pull.txt
index 5c3fb67c01..ad33d2472c 100644
--- a/Documentation/git-pull.txt
+++ b/Documentation/git-pull.txt
@@ -38,6 +38,20 @@  as set by linkgit:git-branch[1] `--track`.
 Assume the following history exists and the current branch is
 "`master`":
 
+------------
+	  A---B---C master on origin
+	 /
+    D---E master
+------------
+
+Then `git pull` will merge in a fast-foward way up to the new master.
+
+------------
+    D---E---A---B---C master, origin/master
+------------
+
+However, a non-fast-foward case looks very different.
+
 ------------
 	  A---B---C master on origin
 	 /
@@ -46,6 +60,9 @@  Assume the following history exists and the current branch is
 	origin/master in your repository
 ------------
 
+By default `git pull` will warn about these situations, however, most likely
+you would want to force a merge, which you can do with `git pull --no-rebase`.
+
 Then "`git pull`" will fetch and replay the changes from the remote
 `master` branch since it diverged from the local `master` (i.e., `E`)
 until its current commit (`C`) on top of `master` and record the
diff --git a/builtin/pull.c b/builtin/pull.c
index 3aa7f56142..8e577b6322 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -1060,25 +1060,25 @@  int cmd_pull(int argc, const char **argv, const char *prefix)
 
 	can_ff = get_can_ff(&orig_head, &merge_heads.oid[0]);
 
-	if (!opt_rebase && !can_ff && opt_verbosity >= 0 && !opt_ff) {
-		warning(_("Pulling without specifying how to reconcile divergent branches is\n"
-			"discouraged. You can squelch this message by running one of the following\n"
-			"commands sometime before your next pull:\n"
-			"\n"
-			"  git config pull.mode merge    # (the default strategy)\n"
-			"  git config pull.mode rebase\n"
-			"  git config pull.mode ff-only  # fast-forward only\n"
-			"\n"
-			"You can replace \"git config\" with \"git config --global\" to set a default\n"
-			"preference for all repositories. You can also pass --rebase, --no-rebase,\n"
-			"or --ff-only on the command line to override the configured default per\n"
-			"invocation.\n"));
+	if (!can_ff) {
+		if (mode == PULL_MODE_FF_ONLY)
+			die(_("The pull was not fast-forward, please either merge or rebase.\n"
+				"If unsure, run 'git pull --no-rebase'."));
+		else if (!mode && opt_verbosity >= 0) {
+			warning(_("The pull was not fast-forward, in the future you will have to choose a merge, or a rebase.\n"
+				"To squelch this message and maintain the current behavior, use:\n"
+				"\n"
+				"  git config --global pull.mode merge\n"
+				"\n"
+				"To squelch this message and adopt the new behavior now, use:\n"
+				"\n"
+				"  git config --global push.mode ff-only\n"
+				"\n"
+				"Falling back to old style for now (merge).\n"
+				"Read 'git pull --help' for more information."));
+		}
 	}
 
-	if (mode == PULL_MODE_FF_ONLY && !can_ff)
-		die(_("The pull was not fast-forward, please either merge or rebase.\n"
-			"If unsure, run 'git pull --no-rebase'."));
-
 	if (opt_rebase >= REBASE_TRUE) {
 		int ret = 0;
 		if ((recurse_submodules == RECURSE_SUBMODULES_ON ||
diff --git a/t/t5520-pull.sh b/t/t5520-pull.sh
index ba78c16d73..29d44d000e 100755
--- a/t/t5520-pull.sh
+++ b/t/t5520-pull.sh
@@ -897,4 +897,18 @@  test_expect_success 'git pull non-fast-forward with merge (ff-only)' '
 	git pull --no-rebase
 '
 
+test_expect_success 'git pull non-fast-forward (default)' '
+	test_when_finished "git checkout master && git branch -D other test" &&
+	git checkout -b other master^ &&
+	>new &&
+	git add new &&
+	git commit -m new &&
+	git checkout -b test -t other &&
+	git reset --hard master &&
+	git pull 2> error &&
+	cat error &&
+	grep -q "The pull was not fast-forward" error &&
+	grep -q "in the future you will have to choose" error
+'
+
 test_done
diff --git a/t/t7601-merge-pull-config.sh b/t/t7601-merge-pull-config.sh
index 4a36ad30e2..8a93b37d87 100755
--- a/t/t7601-merge-pull-config.sh
+++ b/t/t7601-merge-pull-config.sh
@@ -30,71 +30,45 @@  test_expect_success 'setup' '
 test_expect_success 'pull.rebase not set' '
 	git reset --hard c2 &&
 	git pull . c1 2>err &&
-	test_i18ngrep "Pulling without specifying how to reconcile" err
+	test_i18ngrep "choose a merge, or a rebase" err
 '
 
 test_expect_success 'pull.rebase not set (fast-forward)' '
 	git reset --hard c0 &&
 	git pull . c1 2>err &&
-	test_i18ngrep ! "Pulling without specifying how to reconcile" err
+	test_i18ngrep ! "choose a merge, or a rebase" err
 '
 
 test_expect_success 'pull.mode set' '
 	git reset --hard c2 &&
 	test_config pull.mode merge &&
 	git pull . c1 2>err &&
-	test_i18ngrep ! "Pulling without specifying how to reconcile" err
-'
-
-test_expect_success 'pull.rebase not set and pull.ff=true' '
-	git reset --hard c2 &&
-	test_config pull.ff true &&
-	git pull . c1 2>err &&
-	test_i18ngrep ! "Pulling without specifying how to reconcile" err
-'
-
-test_expect_success 'pull.rebase not set and pull.ff=false' '
-	git reset --hard c2 &&
-	test_config pull.ff false &&
-	git pull . c1 2>err &&
-	test_i18ngrep ! "Pulling without specifying how to reconcile" err
+	test_i18ngrep ! "choose a merge, or a rebase" err
 '
 
 test_expect_success 'pull.rebase not set and pull.ff=only' '
 	git reset --hard c2 &&
 	test_config pull.ff only &&
 	test_must_fail git pull . c1 2>err &&
-	test_i18ngrep ! "Pulling without specifying how to reconcile" err
+	test_i18ngrep ! "choose a merge, or a rebase" err
 '
 
 test_expect_success 'pull.rebase not set and --rebase given' '
 	git reset --hard c2 &&
 	git pull --rebase . c1 2>err &&
-	test_i18ngrep ! "Pulling without specifying how to reconcile" err
+	test_i18ngrep ! "choose a merge, or a rebase" err
 '
 
 test_expect_success 'pull.rebase not set and --no-rebase given' '
 	git reset --hard c2 &&
 	git pull --no-rebase . c1 2>err &&
-	test_i18ngrep ! "Pulling without specifying how to reconcile" err
-'
-
-test_expect_success 'pull.rebase not set and --ff given' '
-	git reset --hard c2 &&
-	git pull --ff . c1 2>err &&
-	test_i18ngrep ! "Pulling without specifying how to reconcile" err
-'
-
-test_expect_success 'pull.rebase not set and --no-ff given' '
-	git reset --hard c2 &&
-	git pull --no-ff . c1 2>err &&
-	test_i18ngrep ! "Pulling without specifying how to reconcile" err
+	test_i18ngrep ! "choose a merge, or a rebase" err
 '
 
 test_expect_success 'pull.rebase not set and --ff-only given' '
 	git reset --hard c2 &&
 	test_must_fail git pull --ff-only . c1 2>err &&
-	test_i18ngrep ! "Pulling without specifying how to reconcile" err
+	test_i18ngrep ! "choose a merge, or a rebase" err
 '
 
 test_expect_success 'merge c1 with c2' '