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

Re: [PATCH] pull: fail early if we know we can't merge from upstream

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 11, 2013, 17:37 UTC
Message-ID
<7v1uahj7do.fsf@alter.siamese.dyndns.org>
In-Reply-To
<1365686801-17206-1-git-send-email-cmn@elego.de>
Carlos Martín Nieto <cmn@elego.de> writes:
Show 5 quoted lines
> I can't quite decide whether the behaviour of 'git pull' with no
> upstream configured but a default remote with no fetch refspecs
> merging the remote's HEAD is a feature, a bug or something in between,
> but it's used by t7409 so maybe someone else is using it and we
> shouldn't break it.

Isn't it the simplest "works without any configuration" from the original days?

> There's another check that could be made earlier ('git pull
> someremote' when that's not the branch's upstream remote), but then
> you have to start figuring out what the flags to fetch are.

When the user gave us explicitly the name of the remote, it does not sound too bad to fetch from there. "git pull someremote thatbranch" can be given after seeing a failure and succeed without retransfer, no?

I am not sure if it is worth the added complexity and potential to introduce new bugs in general by trying to outsmart the for-merge logic that kicks in only after we learn what the other side offers and fetch from it, but anyway, let's see what we got here...

Show 11 quoted lines
> diff --git a/git-pull.sh b/git-pull.sh
> index 266e682..b62f5d3 100755
> --- a/git-pull.sh
> +++ b/git-pull.sh
> @@ -43,6 +43,8 @@ log_arg= verbosity= progress= recurse_submodules=
>  merge_args= edit=
>  curr_branch=$(git symbolic-ref -q HEAD)
>  curr_branch_short="${curr_branch#refs/heads/}"
> +upstream=$(git config "branch.$curr_branch_short.merge")
> +remote=$(git config "branch.$curr_branch_short.remote")
>  rebase=$(git config --bool branch.$curr_branch_short.rebase)
Learning these upfront sounds sensible.
Show 22 quoted lines
>  if test -z "$rebase"
>  then
> @@ -138,6 +140,47 @@ do
>  	esac
>  	shift
>  done
> +if test true = "$rebase"
> +then
> +    op_type=rebase
> +    op_prep=against
> +else
> +    op_type=merge
> +    op_prep=with
> +fi
> +
> +check_args_against_config () {
> +	# If fetch gets user-provided arguments, the user is
> +	# overriding the upstream configuration, so we have to wait
> +	# for fetch to do its work to know if we can merge.
> +	if [ $# -gt 0 ]; then
> +		return
> +	fi
Show 8 quoted lines
> +	# Figure out what remote we're going to be fetching from
> +	use_remote=origin
> +	if [ -n "$remote" ]; then
> +		use_remote="$remote"
> +	fi
> +
> +	# If the remote doesn't have a fetch refspec, then we'll merge
> +	# whatever fetch marks for-merge, same as above.
The "above" in this sentence refers to...?
I guess "we have to wait", but it wasn't very clear.
> +	fetch=$(git config --get-all "remote.$use_remote.fetch")
> +	if [ -z "$fetch" ]; then
> +		return
> +	fi

Hmm, it is probably correct to punt on this case, but it defeats large part of the effect of your effort, doesn't it? We fetch what is covered by remote.$name.fetch _and_ what need to complete the merge operation (otherwise branch.$name.merge that is not covered by remote.$there.fetch will not work). So

    [remote "origin"]
            url = $over_there
    [branch "master"]
            remote = origin
            merge = refs/heads/master
would still fetch refs/heads/master from there and merge it.
> +	# The typical 'git pull' case where it should merge from the
> +	# current branch's upstream. We can already check whether we
> +	# we can do it. If HEAD is detached or there is no upstream
> +	# branch, complain now.

Drop "typical", and rephrase "merge from" to also cover "rebase" (I often say "integrate with").

To return to your original description:
    A 'git pull' without specifying a remote is asked to take the
    current branch's upstream as the branch to merge from. This
    cannot work without an upstream configuration nor with HEAD
    detached, but we only check for this after fetching.

Wouldn't it be sufficient to add something like this before fetch happens:

	if test $# != 0 || # args explicitly specified
           test -n "$curr_branch" || # not detached
	   test -n "$upstream" # what to integrate with is known
	then
		return ;# then no problem
	fi
	die "underspecified 'git pull'"

without changing anything else? For that matter, $upstream is likely to be empty when detached, so the second test may not even be necessary.

Previous: Carlos Martín NietoNext: Carlos Martín Nieto
Message 2 of 4 in “pull: fail early if we know we can't merge from upstream”
  1. pull: fail early if we know we can't merge from upstreamCarlos Martín Nieto, Apr 11, 2013
  2. Junio C HamanoApr 11, 2013
  3. Carlos Martín NietoApr 12, 2013
  4. Junio C HamanoApr 12, 2013

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.