summaryrefslogtreecommitdiff
path: root/builtin/pull.c
diff options
context:
space:
mode:
authorPaul Tan <pyokagan@gmail.com>2015-06-18 10:54:06 (GMT)
committerJunio C Hamano <gitster@pobox.com>2015-06-18 20:17:32 (GMT)
commit49ec402d52dc8f8e55e62537c95766b623e63d6b (patch)
tree242314d869d00b6d744b510acdd6d85ece9f2b2e /builtin/pull.c
parentfe911b8ca0b466697b38880512ea2b28f2699dbd (diff)
downloadgit-49ec402d52dc8f8e55e62537c95766b623e63d6b.zip
git-49ec402d52dc8f8e55e62537c95766b623e63d6b.tar.gz
git-49ec402d52dc8f8e55e62537c95766b623e63d6b.tar.bz2
pull: implement pulling into an unborn branch
b4dc085 (pull: merge into unborn by fast-forwarding from empty tree, 2013-06-20) established git-pull's current behavior of pulling into an unborn branch by fast-forwarding the work tree from an empty tree to the merge head, then setting HEAD to the merge head. Re-implement this behavior by introducing pull_into_void() which will be called instead of run_merge() if HEAD is invalid. Helped-by: Stephen Robin <stephen.robin@gmail.com> Signed-off-by: Paul Tan <pyokagan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/pull.c')
-rw-r--r--builtin/pull.c29
1 files changed, 28 insertions, 1 deletions
diff --git a/builtin/pull.c b/builtin/pull.c
index 110e719..492bb0e 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -13,6 +13,7 @@
#include "sha1-array.h"
#include "remote.h"
#include "dir.h"
+#include "refs.h"
static const char * const pull_usage[] = {
N_("git pull [options] [<repository> [<refspec>...]]"),
@@ -368,6 +369,27 @@ static int run_fetch(const char *repo, const char **refspecs)
}
/**
+ * "Pulls into void" by branching off merge_head.
+ */
+static int pull_into_void(const unsigned char *merge_head,
+ const unsigned char *curr_head)
+{
+ /*
+ * Two-way merge: we treat the index as based on an empty tree,
+ * and try to fast-forward to HEAD. This ensures we will not lose
+ * index/worktree changes that the user already made on the unborn
+ * branch.
+ */
+ if (checkout_fast_forward(EMPTY_TREE_SHA1_BIN, merge_head, 0))
+ return 1;
+
+ if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR))
+ return 1;
+
+ return 0;
+}
+
+/**
* Runs git-merge, returning its exit status.
*/
static int run_merge(void)
@@ -476,5 +498,10 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
if (!merge_heads.nr)
die_no_merge_candidates(repo, refspecs);
- return run_merge();
+ if (is_null_sha1(orig_head)) {
+ if (merge_heads.nr > 1)
+ die(_("Cannot merge multiple branches into empty head."));
+ return pull_into_void(*merge_heads.sha1, curr_head);
+ } else
+ return run_merge();
}