summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Contreras <felipe.contreras@gmail.com>2013-09-08 20:23:15 -0500
committerJunio C Hamano <gitster@pobox.com>2013-09-10 14:34:52 -0700
commit26d7bc92ee023e516c37078c704c001db07e6161 (patch)
tree0db54a9c60d2a19d277473127c91f03d9797a391
parent9e1f34ba22cab702537cad77775f56a955bc6ab3 (diff)
downloadgit-26d7bc92ee023e516c37078c704c001db07e6161.tar.gz
pull: add merge-ff-only option
It is very typical for Git newcomers to inadvertently create merges and worst; inadvertently pushing them. This is one of the reasons many experienced users prefer to avoid 'git pull', and recommend newcomers to avoid it as well. To avoid these problems and keep 'git pull' useful, it has been suggested that 'git pull' barfs by default if the merge is non-fast-forward, which unfortunately would break backwards compatibility. This patch leaves everything in place to enable this new mode, but it only gets enabled if the user specifically configures it; pull.mode = merge-ff-only. Later on this mode can be enabled by default (e.g. in v2.0). For the full discussion you can read: http://thread.gmane.org/gmane.comp.version-control.git/225146/focus=225305 Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--Documentation/config.txt8
-rwxr-xr-xgit-pull.sh12
-rwxr-xr-xt/t5520-pull.sh36
3 files changed, 53 insertions, 3 deletions
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 400a1a9a08..da4560f647 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -1875,9 +1875,11 @@ pretty.<name>::
pull.mode::
When "git pull" is run, this determines if it would either merge or
- rebase the fetched branch. The possible values are 'merge' and
- 'rebase'. See "branch.<name>.pullmode" for doing this in a non
- branch-specific manner.
+ rebase the fetched branch. The possible values are 'merge',
+ 'rebase', and 'merge-ff-only'. If 'merge-ff-only' is specified, the
+ merge will only succeed if it's fast-forward. See
+ "branch.<name>.pullmode" for doing this in a non branch-specific
+ manner.
+
*NOTE*: this is a possibly dangerous operation; do *not* use
it unless you understand the implications (see linkgit:git-rebase[1]
diff --git a/git-pull.sh b/git-pull.sh
index fbb8a9ba0a..580d63336e 100755
--- a/git-pull.sh
+++ b/git-pull.sh
@@ -62,6 +62,7 @@ then
echo "Please use pull.mode and branch.<name>.pullmode instead."
fi
fi
+test -z "$mode" && mode=merge
dry_run=
while :
do
@@ -282,6 +283,17 @@ case "$merge_head" in
die "$(gettext "Cannot rebase onto multiple branches")"
fi
;;
+*)
+ # check if a non-fast-foward merge would be needed
+ merge_head=${merge_head% }
+ if test "$mode" = merge-ff-only -a -z "$no_ff$ff_only${squash#--no-squash}" &&
+ test -n "$orig_head" &&
+ ! git merge-base --is-ancestor "$orig_head" "$merge_head" &&
+ ! git merge-base --is-ancestor "$merge_head" "$orig_head"
+ then
+ die "The pull was not fast-forward, please either merge or rebase."
+ fi
+ ;;
esac
# Pulling into unborn branch: a shorthand for branching off
diff --git a/t/t5520-pull.sh b/t/t5520-pull.sh
index 978a3c1b62..798ae2f391 100755
--- a/t/t5520-pull.sh
+++ b/t/t5520-pull.sh
@@ -310,4 +310,40 @@ test_expect_success 'branch.to-rebase.pullmode should override pull.mode' '
test new = $(git show HEAD:file2)
'
+test_expect_success 'git pull fast-forward' '
+ test_when_finished "git checkout master && git branch -D other test" &&
+ test_config pull.mode merge-ff-only &&
+ 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
+'
+
+test_expect_success 'git pull non-fast-forward' '
+ test_when_finished "git checkout master && git branch -D other test" &&
+ test_config pull.mode merge-ff-only &&
+ git checkout -b other master^ &&
+ >new &&
+ git add new &&
+ git commit -m new &&
+ git checkout -b test -t other &&
+ git reset --hard master &&
+ test_must_fail git pull
+'
+
+test_expect_success 'git pull non-fast-forward (merge)' '
+ test_when_finished "git checkout master && git branch -D other test" &&
+ test_config pull.mode merge-ff-only &&
+ 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 --merge
+'
+
test_done