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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

Perform the check beforehand, as we already know whether we have
enough information to merge and can fail immediately otherwise.

Signed-off-by: Carlos Martín Nieto <cmn@xxxxxxxx>
---
 git-pull.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 45 insertions(+), 17 deletions(-)

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.

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. I'll
revisit this at some point, but I wanted to get this out since it's
working.

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)
 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
+
+	# 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.
+	fetch=$(git config --get-all "remote.$use_remote.fetch")
+	if [ -z "$fetch" ]; then
+		return
+	fi
+
+	# 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.
+	if [ -z "$curr_branch_short" -o -z "$upstream" ]; then
+		. git-parse-remote
+		error_on_missing_default_upstream "pull" $op_type $op_prep \
+		    "git pull <remote> <branch>"
+		exit 1
+	fi
+}
 
 error_on_no_merge_candidates () {
 	exec >&2
@@ -151,19 +194,6 @@ error_on_no_merge_candidates () {
 		esac
 	done
 
-	if test true = "$rebase"
-	then
-		op_type=rebase
-		op_prep=against
-	else
-		op_type=merge
-		op_prep=with
-	fi
-
-	curr_branch=${curr_branch#refs/heads/}
-	upstream=$(git config "branch.$curr_branch.merge")
-	remote=$(git config "branch.$curr_branch.remote")
-
 	if [ $# -gt 1 ]; then
 		if [ "$rebase" = true ]; then
 			printf "There is no candidate for rebasing against "
@@ -177,10 +207,6 @@ error_on_no_merge_candidates () {
 		echo "You asked to pull from the remote '$1', but did not specify"
 		echo "a branch. Because this is not the default configured remote"
 		echo "for your current branch, you must specify a branch on the command line."
-	elif [ -z "$curr_branch" -o -z "$upstream" ]; then
-		. git-parse-remote
-		error_on_missing_default_upstream "pull" $op_type $op_prep \
-			"git pull <remote> <branch>"
 	else
 		echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
 		echo "from the remote, but no such ref was fetched."
@@ -213,6 +239,8 @@ test true = "$rebase" && {
 		fi
 	done
 }
+
+check_args_against_config "$@"
 orig_head=$(git rev-parse -q --verify HEAD)
 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
 test -z "$dry_run" || exit 0
-- 
1.8.2.524.g8f8def7

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]