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>
Paul Tan committed
Jun 18, 2015 at 18:54 UTC
49ec402d52dc8f8e55e62537c95766b623e63d6b
1 file changed
+28
-1
builtin/pull.c
+28
-1
@@ -13,6 +13,7 @@
13
#include "sha1-array.h"
14
#include "remote.h"
15
#include "dir.h"
16
+#include "refs.h"
17
18
static const char * const pull_usage[] = {
19
N_("git pull [options] [<repository> [<refspec>...]]"),
@@ -367,6 +368,27 @@ static int run_fetch(const char *repo, const char **refspecs)
368
return ret;
369
}
370
371
+/**
372
+ * "Pulls into void" by branching off merge_head.
373
+ */
374
+static int pull_into_void(const unsigned char *merge_head,
375
+ const unsigned char *curr_head)
376
+{
377
+ /*
378
+ * Two-way merge: we treat the index as based on an empty tree,
379
+ * and try to fast-forward to HEAD. This ensures we will not lose
380
+ * index/worktree changes that the user already made on the unborn
381
+ * branch.
382
+ */
383
+ if (checkout_fast_forward(EMPTY_TREE_SHA1_BIN, merge_head, 0))
384
+ return 1;
385
+
386
+ if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR))
387
+ return 1;
388
+
389
+ return 0;
390
+}
391
+
392
/**
393
* Runs git-merge, returning its exit status.
394
*/
@@ -476,5 +498,10 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
498
if (!merge_heads.nr)
499
die_no_merge_candidates(repo, refspecs);
500
479
- return run_merge();
501
+ if (is_null_sha1(orig_head)) {
502
+ if (merge_heads.nr > 1)
503
+ die(_("Cannot merge multiple branches into empty head."));
504
+ return pull_into_void(*merge_heads.sha1, curr_head);
505
+ } else
506
+ return run_merge();
507
}