1#include "cache.h"
2#include "diff.h"
3#include "diffcore.h"
4#include "lockfile.h"
5#include "commit.h"
6#include "run-command.h"
7#include "resolve-undo.h"
8#include "tree-walk.h"
9#include "unpack-trees.h"
10#include "dir.h"
11
12static const char *merge_argument(struct commit *commit)
13{
14 if (commit)
15 return oid_to_hex(&commit->object.oid);
16 else
17 return EMPTY_TREE_SHA1_HEX;
18}
19
20int try_merge_command(const char *strategy, size_t xopts_nr,
21 const char **xopts, struct commit_list *common,
22 const char *head_arg, struct commit_list *remotes)
23{
24 struct argv_array args = ARGV_ARRAY_INIT;
25 int i, ret;
26 struct commit_list *j;
27
28 argv_array_pushf(&args, "merge-%s", strategy);
29 for (i = 0; i < xopts_nr; i++)
30 argv_array_pushf(&args, "--%s", xopts[i]);
31 for (j = common; j; j = j->next)
32 argv_array_push(&args, merge_argument(j->item));
33 argv_array_push(&args, "--");
34 argv_array_push(&args, head_arg);
35 for (j = remotes; j; j = j->next)
36 argv_array_push(&args, merge_argument(j->item));
37
38 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
39 argv_array_clear(&args);
40
41 discard_cache();
42 if (read_cache() < 0)
43 die(_("failed to read the cache"));
44 resolve_undo_clear();
45
46 return ret;
47}
48
49int checkout_fast_forward(const struct object_id *head,
50 const struct object_id *remote,
51 int overwrite_ignore)
52{
53 struct tree *trees[MAX_UNPACK_TREES];
54 struct unpack_trees_options opts;
55 struct tree_desc t[MAX_UNPACK_TREES];
56 int i, nr_trees = 0;
57 struct dir_struct dir;
58 struct lock_file lock_file = LOCK_INIT;
59
60 refresh_cache(REFRESH_QUIET);
61
62 if (hold_locked_index(&lock_file, LOCK_REPORT_ON_ERROR) < 0)
63 return -1;
64
65 memset(&trees, 0, sizeof(trees));
66 memset(&opts, 0, sizeof(opts));
67 memset(&t, 0, sizeof(t));
68 if (overwrite_ignore) {
69 memset(&dir, 0, sizeof(dir));
70 dir.flags |= DIR_SHOW_IGNORED;
71 setup_standard_excludes(&dir);
72 opts.dir = &dir;
73 }
74
75 opts.head_idx = 1;
76 opts.src_index = &the_index;
77 opts.dst_index = &the_index;
78 opts.update = 1;
79 opts.verbose_update = 1;
80 opts.merge = 1;
81 opts.fn = twoway_merge;
82 setup_unpack_trees_porcelain(&opts, "merge");
83
84 trees[nr_trees] = parse_tree_indirect(head);
85 if (!trees[nr_trees++]) {
86 rollback_lock_file(&lock_file);
87 return -1;
88 }
89 trees[nr_trees] = parse_tree_indirect(remote);
90 if (!trees[nr_trees++]) {
91 rollback_lock_file(&lock_file);
92 return -1;
93 }
94 for (i = 0; i < nr_trees; i++) {
95 parse_tree(trees[i]);
96 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
97 }
98 if (unpack_trees(nr_trees, t, &opts)) {
99 rollback_lock_file(&lock_file);
100 return -1;
101 }
102 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
103 return error(_("unable to write new index file"));
104 return 0;
105}