git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] pull: abort by default if fast-forwarding is impossible

From
Elijah Newren <newren@gmail.com>
Date
Jun 27, 2021, 01:34 UTC
Message-ID
<CABPp-BGko7BP6ZMyRKwKrv0xz7FM9ehe67HSAtzgHF9eV2TD3A@mail.gmail.com>
In-Reply-To
<20210627000855.530985-1-alexhenrie24@gmail.com>
On Sat, Jun 26, 2021 at 5:10 PM Alex Henrie <alexhenrie24@gmail.com> wrote:
>
> The behavior of warning but merging anyway is difficult to explain to
> users because it sends mixed signals. End the confusion by firmly
> requiring the user to specify whether they want a merge or a rebase.

I would use some of Junio's wording from https://lore.kernel.org/git/xmqq360h8286.fsf@gitster.c.googlers.com/:

Given how annoying the "loudly warn but still go ahead" behaviour for git pull's default behavior when fast-forwarding is not possible (which was also displayed even when fast-forwarding was possible until c525de335e ("pull: display default warning only when non-ff", 2020-12-12)), and how easy it is for existing users to have squelched the annoyance by choosing between rebase and merge already, turn the warning into an error. As an added bonus, the previous behavior of git pull was difficult to explain, and making this change helps us simplify it.

Show 17 quoted lines
> Signed-off-by: Alex Henrie <alexhenrie24@gmail.com>
> ---
>  Documentation/git-pull.txt | 14 +++++++++-----
>  builtin/pull.c             | 32 +++++++++++++++-----------------
>  2 files changed, 24 insertions(+), 22 deletions(-)
>
> diff --git a/Documentation/git-pull.txt b/Documentation/git-pull.txt
> index 5c3fb67c01..0fb185811e 100644
> --- a/Documentation/git-pull.txt
> +++ b/Documentation/git-pull.txt
> @@ -16,13 +16,17 @@ DESCRIPTION
>  -----------
>
>  Incorporates changes from a remote repository into the current
> -branch.  In its default mode, `git pull` is shorthand for
> -`git fetch` followed by `git merge FETCH_HEAD`.
> +branch.  `git pull` is shorthand for `git fetch` followed by
I'd replace "shorthand for" with "shorthand for either"
Show 9 quoted lines
> +`git merge FETCH_HEAD` or `git rebase FETCH_HEAD`.
>
>  More precisely, 'git pull' runs 'git fetch' with the given
> -parameters and calls 'git merge' to merge the retrieved branch
> -heads into the current branch.
> -With `--rebase`, it runs 'git rebase' instead of 'git merge'.
> +parameters and then, by default, attempts to fast-forward the
> +current branch to the remote branch head.  If fast-forwarding
> +is not possible because the local and remote branches have
Good to here.
> +diverged, the `--rebase` option causes 'git rebase' to be...
> +called to rebase the local branch upon the remote branch, and
> +the `--no-rebase` option causes 'git merge' to be called to
> +merge the retrieved branch heads into the current branch.
This is okay, but can we just simplify it to:

....diverged, then the user must specify whether to rebase or merge (see the --rebase and --no-rebase flags).

While at it, we may also want to change the documentation for the --no-rebase flag. Instead of

--no-rebase::
    Override earlier --rebase.
which can be confusing since --rebase takes so many different choices, instead:
--no-rebase::
    This is shorthand for --rebase=false
Show 55 quoted lines
>  <repository> should be the name of a remote repository as
>  passed to linkgit:git-fetch[1].  <refspec> can name an
> diff --git a/builtin/pull.c b/builtin/pull.c
> index e8927fc2ff..21eaebc463 100644
> --- a/builtin/pull.c
> +++ b/builtin/pull.c
> @@ -925,20 +925,20 @@ static int get_can_ff(struct object_id *orig_head, struct object_id *orig_merge_
>         return ret;
>  }
>
> -static void show_advice_pull_non_ff(void)
> +static void die_pull_non_ff(void)
>  {
> -       advise(_("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.rebase false  # merge (the default strategy)\n"
> -                "  git config pull.rebase true   # rebase\n"
> -                "  git config pull.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"));
> +       die(_("Pulling without specifying how to reconcile divergent branches is not\n"
> +             "possible. You can squelch this message by running one of the following\n"
> +             "commands sometime before your next pull:\n"
> +             "\n"
> +             "  git config pull.rebase false  # merge\n"
> +             "  git config pull.rebase true   # rebase\n"
> +             "  git config pull.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"));
>  }
>
>  int cmd_pull(int argc, const char **argv, const char *prefix)
> @@ -1047,10 +1047,8 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
>
>         can_ff = get_can_ff(&orig_head, &merge_heads.oid[0]);
>
> -       if (rebase_unspecified && !opt_ff && !can_ff) {
> -               if (opt_verbosity >= 0)
> -                       show_advice_pull_non_ff();
> -       }
> +       if (rebase_unspecified && !opt_ff && !can_ff)
> +               die_pull_non_ff();
>
>         if (opt_rebase) {
>                 int ret = 0;
> --
> 2.32.0.95.g4a22da5c5a

The code changes look good, but you'll need to add several test changes as well, or this code change will cause test failures. You'll need to go through the relevant `git pull` invocations in the testsuite and add the --no-rebase flag.

Thanks for working on this.
Previous: Alex HenrieNext: Felipe Contreras
Message 2 of 10 in “pull: abort by default if fast-forwarding is impossible”
  1. pull: abort by default if fast-forwarding is impossibleAlex Henrie, Jun 27, 2021
  2. Elijah NewrenJun 27, 2021
  3. Felipe ContrerasJun 27, 2021
  4. Elijah NewrenJun 27, 2021
  5. Felipe ContrerasJun 27, 2021
  6. Alex HenrieJun 27, 2021
  7. Felipe ContrerasJun 27, 2021
  8. Ævar Arnfjörð BjarmasonJun 28, 2021
  9. Felipe ContrerasJun 28, 2021
  10. Felipe ContrerasJun 27, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.