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