Merge branch 'jk/command-line-config-empty-string' into maint
[git/mjg.git] / merge.c
blob74ced7f70b5beec045c134b5719c926fc791f1e2
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"
9 static const char *merge_argument(struct commit *commit)
11 if (commit)
12 return sha1_to_hex(commit->object.sha1);
13 else
14 return EMPTY_TREE_SHA1_HEX;
17 int 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)
21 struct argv_array args = ARGV_ARRAY_INIT;
22 int i, ret;
23 struct commit_list *j;
25 argv_array_pushf(&args, "merge-%s", strategy);
26 for (i = 0; i < xopts_nr; i++)
27 argv_array_pushf(&args, "--%s", xopts[i]);
28 for (j = common; j; j = j->next)
29 argv_array_push(&args, merge_argument(j->item));
30 argv_array_push(&args, "--");
31 argv_array_push(&args, head_arg);
32 for (j = remotes; j; j = j->next)
33 argv_array_push(&args, merge_argument(j->item));
35 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
36 argv_array_clear(&args);
38 discard_cache();
39 if (read_cache() < 0)
40 die(_("failed to read the cache"));
41 resolve_undo_clear();
43 return ret;
46 int checkout_fast_forward(const unsigned char *head,
47 const unsigned char *remote,
48 int overwrite_ignore)
50 struct tree *trees[MAX_UNPACK_TREES];
51 struct unpack_trees_options opts;
52 struct tree_desc t[MAX_UNPACK_TREES];
53 int i, nr_trees = 0;
54 struct dir_struct dir;
55 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
57 refresh_cache(REFRESH_QUIET);
59 hold_locked_index(lock_file, 1);
61 memset(&trees, 0, sizeof(trees));
62 memset(&opts, 0, sizeof(opts));
63 memset(&t, 0, sizeof(t));
64 if (overwrite_ignore) {
65 memset(&dir, 0, sizeof(dir));
66 dir.flags |= DIR_SHOW_IGNORED;
67 setup_standard_excludes(&dir);
68 opts.dir = &dir;
71 opts.head_idx = 1;
72 opts.src_index = &the_index;
73 opts.dst_index = &the_index;
74 opts.update = 1;
75 opts.verbose_update = 1;
76 opts.merge = 1;
77 opts.fn = twoway_merge;
78 setup_unpack_trees_porcelain(&opts, "merge");
80 trees[nr_trees] = parse_tree_indirect(head);
81 if (!trees[nr_trees++])
82 return -1;
83 trees[nr_trees] = parse_tree_indirect(remote);
84 if (!trees[nr_trees++])
85 return -1;
86 for (i = 0; i < nr_trees; i++) {
87 parse_tree(trees[i]);
88 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
90 if (unpack_trees(nr_trees, t, &opts))
91 return -1;
92 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
93 die(_("unable to write new index file"));
94 return 0;