rehabilitate 'git index-pack' inside the object store
[gitweb.git] / builtin-merge.c
index e97c79e60cdb8227ad682a3927ae955005cbe1f3..d0bf1fc1edb5fc7783669e4086210a521a3ab6e3 100644 (file)
@@ -77,6 +77,7 @@ static int option_parse_message(const struct option *opt,
 static struct strategy *get_strategy(const char *name)
 {
        int i;
+       struct strbuf err;
 
        if (!name)
                return NULL;
@@ -84,7 +85,13 @@ static struct strategy *get_strategy(const char *name)
        for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
                if (!strcmp(name, all_strategy[i].name))
                        return &all_strategy[i];
-       return NULL;
+
+       strbuf_init(&err, 0);
+       for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
+               strbuf_addf(&err, " %s", all_strategy[i].name);
+       fprintf(stderr, "Could not find merge strategy '%s'.\n", name);
+       fprintf(stderr, "Available strategies are:%s.\n", err.buf);
+       exit(1);
 }
 
 static void append_strategy(struct strategy *s)
@@ -96,25 +103,10 @@ static void append_strategy(struct strategy *s)
 static int option_parse_strategy(const struct option *opt,
                                 const char *name, int unset)
 {
-       int i;
-       struct strategy *s;
-
        if (unset)
                return 0;
 
-       s = get_strategy(name);
-
-       if (s)
-               append_strategy(s);
-       else {
-               struct strbuf err;
-               strbuf_init(&err, 0);
-               for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
-                       strbuf_addf(&err, " %s", all_strategy[i].name);
-               fprintf(stderr, "Could not find merge strategy '%s'.\n", name);
-               fprintf(stderr, "Available strategies are:%s.\n", err.buf);
-               exit(1);
-       }
+       append_strategy(get_strategy(name));
        return 0;
 }
 
@@ -404,12 +396,12 @@ static void merge_name(const char *remote, struct strbuf *msg)
                struct strbuf truname = STRBUF_INIT;
                strbuf_addstr(&truname, "refs/heads/");
                strbuf_addstr(&truname, remote);
-               strbuf_setlen(&truname, len+11);
+               strbuf_setlen(&truname, truname.len - len);
                if (resolve_ref(truname.buf, buf_sha, 0, 0)) {
                        strbuf_addf(msg,
                                    "%s\t\tbranch '%s'%s of .\n",
                                    sha1_to_hex(remote_head->sha1),
-                                   truname.buf,
+                                   truname.buf + 11,
                                    (early ? " (early part)" : ""));
                        return;
                }
@@ -439,7 +431,7 @@ static void merge_name(const char *remote, struct strbuf *msg)
                sha1_to_hex(remote_head->sha1), remote);
 }
 
-int git_merge_config(const char *k, const char *v, void *cb)
+static int git_merge_config(const char *k, const char *v, void *cb)
 {
        if (branch && !prefixcmp(k, "branch.") &&
                !prefixcmp(k + 7, branch) &&
@@ -450,6 +442,8 @@ int git_merge_config(const char *k, const char *v, void *cb)
 
                buf = xstrdup(v);
                argc = split_cmdline(buf, &argv);
+               if (argc < 0)
+                       die("Bad branch.%s.mergeoptions string", branch);
                argv = xrealloc(argv, sizeof(*argv) * (argc + 2));
                memmove(argv + 1, argv, sizeof(*argv) * (argc + 1));
                argc++;
@@ -572,14 +566,14 @@ static int checkout_fast_forward(unsigned char *head, unsigned char *remote)
        struct dir_struct dir;
        struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
 
-       if (read_cache_unmerged())
-               die("you need to resolve your current index first");
+       refresh_cache(REFRESH_QUIET);
 
        fd = hold_locked_index(lock_file, 1);
 
        memset(&trees, 0, sizeof(trees));
        memset(&opts, 0, sizeof(opts));
        memset(&t, 0, sizeof(t));
+       memset(&dir, 0, sizeof(dir));
        dir.show_ignored = 1;
        dir.exclude_per_dir = ".gitignore";
        opts.dir = &dir;
@@ -643,14 +637,9 @@ static void add_strategies(const char *string, unsigned attr)
 
        memset(&list, 0, sizeof(list));
        split_merge_strategies(string, &list, &list_nr, &list_alloc);
-       if (list != NULL) {
-               for (i = 0; i < list_nr; i++) {
-                       struct strategy *s;
-
-                       s = get_strategy(list[i].name);
-                       if (s)
-                               append_strategy(s);
-               }
+       if (list) {
+               for (i = 0; i < list_nr; i++)
+                       append_strategy(get_strategy(list[i].name));
                return;
        }
        for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
@@ -662,13 +651,15 @@ static void add_strategies(const char *string, unsigned attr)
 static int merge_trivial(void)
 {
        unsigned char result_tree[20], result_commit[20];
-       struct commit_list parent;
+       struct commit_list *parent = xmalloc(sizeof(*parent));
 
        write_tree_trivial(result_tree);
        printf("Wonderful.\n");
-       parent.item = remoteheads->item;
-       parent.next = NULL;
-       commit_tree(merge_msg.buf, result_tree, &parent, result_commit);
+       parent->item = lookup_commit(head);
+       parent->next = xmalloc(sizeof(*parent->next));
+       parent->next->item = remoteheads->item;
+       parent->next->next = NULL;
+       commit_tree(merge_msg.buf, result_tree, parent, result_commit);
        finish(result_commit, "In-index merge");
        drop_save();
        return 0;
@@ -754,6 +745,7 @@ static int evaluate_result(void)
        int cnt = 0;
        struct rev_info rev;
 
+       discard_cache();
        if (read_cache() < 0)
                die("failed to read the cache");
 
@@ -787,7 +779,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
        struct commit_list **remotes = &remoteheads;
 
        setup_work_tree();
-       if (unmerged_cache())
+       if (read_cache_unmerged())
                die("You are in the middle of a conflicted merge.");
 
        /*
@@ -948,7 +940,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                        hex,
                        find_unique_abbrev(remoteheads->item->object.sha1,
                        DEFAULT_ABBREV));
-               refresh_cache(REFRESH_QUIET);
                strbuf_init(&msg, 0);
                strbuf_addstr(&msg, "Fast forward");
                if (have_message)
@@ -1085,6 +1076,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                }
 
                /* Automerge succeeded. */
+               discard_cache();
                write_tree_trivial(result_tree);
                automerge_was_ok = 1;
                break;