merge.con commit Merge branch 'jc/t1512-fix' into maint (069dba2)
   1#include "cache.h"
   2#include "commit.h"
   3#include "run-command.h"
   4#include "resolve-undo.h"
   5#include "tree-walk.h"
   6#include "unpack-trees.h"
   7#include "dir.h"
   8
   9static const char *merge_argument(struct commit *commit)
  10{
  11        if (commit)
  12                return sha1_to_hex(commit->object.sha1);
  13        else
  14                return EMPTY_TREE_SHA1_HEX;
  15}
  16
  17int try_merge_command(const char *strategy, size_t xopts_nr,
  18                      const char **xopts, struct commit_list *common,
  19                      const char *head_arg, struct commit_list *remotes)
  20{
  21        const char **args;
  22        int i = 0, x = 0, ret;
  23        struct commit_list *j;
  24        struct strbuf buf = STRBUF_INIT;
  25
  26        args = xmalloc((4 + xopts_nr + commit_list_count(common) +
  27                        commit_list_count(remotes)) * sizeof(char *));
  28        strbuf_addf(&buf, "merge-%s", strategy);
  29        args[i++] = buf.buf;
  30        for (x = 0; x < xopts_nr; x++) {
  31                char *s = xmalloc(strlen(xopts[x])+2+1);
  32                strcpy(s, "--");
  33                strcpy(s+2, xopts[x]);
  34                args[i++] = s;
  35        }
  36        for (j = common; j; j = j->next)
  37                args[i++] = xstrdup(merge_argument(j->item));
  38        args[i++] = "--";
  39        args[i++] = head_arg;
  40        for (j = remotes; j; j = j->next)
  41                args[i++] = xstrdup(merge_argument(j->item));
  42        args[i] = NULL;
  43        ret = run_command_v_opt(args, RUN_GIT_CMD);
  44        strbuf_release(&buf);
  45        i = 1;
  46        for (x = 0; x < xopts_nr; x++)
  47                free((void *)args[i++]);
  48        for (j = common; j; j = j->next)
  49                free((void *)args[i++]);
  50        i += 2;
  51        for (j = remotes; j; j = j->next)
  52                free((void *)args[i++]);
  53        free(args);
  54        discard_cache();
  55        if (read_cache() < 0)
  56                die(_("failed to read the cache"));
  57        resolve_undo_clear();
  58
  59        return ret;
  60}
  61
  62int checkout_fast_forward(const unsigned char *head,
  63                          const unsigned char *remote,
  64                          int overwrite_ignore)
  65{
  66        struct tree *trees[MAX_UNPACK_TREES];
  67        struct unpack_trees_options opts;
  68        struct tree_desc t[MAX_UNPACK_TREES];
  69        int i, fd, nr_trees = 0;
  70        struct dir_struct dir;
  71        struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
  72
  73        refresh_cache(REFRESH_QUIET);
  74
  75        fd = hold_locked_index(lock_file, 1);
  76
  77        memset(&trees, 0, sizeof(trees));
  78        memset(&opts, 0, sizeof(opts));
  79        memset(&t, 0, sizeof(t));
  80        if (overwrite_ignore) {
  81                memset(&dir, 0, sizeof(dir));
  82                dir.flags |= DIR_SHOW_IGNORED;
  83                setup_standard_excludes(&dir);
  84                opts.dir = &dir;
  85        }
  86
  87        opts.head_idx = 1;
  88        opts.src_index = &the_index;
  89        opts.dst_index = &the_index;
  90        opts.update = 1;
  91        opts.verbose_update = 1;
  92        opts.merge = 1;
  93        opts.fn = twoway_merge;
  94        setup_unpack_trees_porcelain(&opts, "merge");
  95
  96        trees[nr_trees] = parse_tree_indirect(head);
  97        if (!trees[nr_trees++])
  98                return -1;
  99        trees[nr_trees] = parse_tree_indirect(remote);
 100        if (!trees[nr_trees++])
 101                return -1;
 102        for (i = 0; i < nr_trees; i++) {
 103                parse_tree(trees[i]);
 104                init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
 105        }
 106        if (unpack_trees(nr_trees, t, &opts))
 107                return -1;
 108        if (write_cache(fd, active_cache, active_nr) ||
 109                commit_locked_index(lock_file))
 110                die(_("unable to write new index file"));
 111        return 0;
 112}