git: fix leaking system paths
[git/gitster.git] / merge.c
blobfe3efa4b24b7d10514cda3c3dfa6a06093dfc1f4
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "gettext.h"
5 #include "hash.h"
6 #include "hex.h"
7 #include "lockfile.h"
8 #include "merge.h"
9 #include "commit.h"
10 #include "repository.h"
11 #include "run-command.h"
12 #include "resolve-undo.h"
13 #include "tree.h"
14 #include "tree-walk.h"
15 #include "unpack-trees.h"
17 static const char *merge_argument(struct commit *commit)
19 return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree);
22 int try_merge_command(struct repository *r,
23 const char *strategy, size_t xopts_nr,
24 const char **xopts, struct commit_list *common,
25 const char *head_arg, struct commit_list *remotes)
27 struct child_process cmd = CHILD_PROCESS_INIT;
28 int i, ret;
29 struct commit_list *j;
31 strvec_pushf(&cmd.args, "merge-%s", strategy);
32 for (i = 0; i < xopts_nr; i++)
33 strvec_pushf(&cmd.args, "--%s", xopts[i]);
34 for (j = common; j; j = j->next)
35 strvec_push(&cmd.args, merge_argument(j->item));
36 strvec_push(&cmd.args, "--");
37 strvec_push(&cmd.args, head_arg);
38 for (j = remotes; j; j = j->next)
39 strvec_push(&cmd.args, merge_argument(j->item));
41 cmd.git_cmd = 1;
42 ret = run_command(&cmd);
44 discard_index(r->index);
45 if (repo_read_index(r) < 0)
46 die(_("failed to read the cache"));
47 resolve_undo_clear_index(r->index);
49 return ret;
52 int checkout_fast_forward(struct repository *r,
53 const struct object_id *head,
54 const struct object_id *remote,
55 int overwrite_ignore)
57 struct tree *trees[MAX_UNPACK_TREES];
58 struct unpack_trees_options opts;
59 struct tree_desc t[MAX_UNPACK_TREES];
60 int i, nr_trees = 0;
61 struct lock_file lock_file = LOCK_INIT;
63 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
65 if (repo_hold_locked_index(r, &lock_file, LOCK_REPORT_ON_ERROR) < 0)
66 return -1;
68 memset(&trees, 0, sizeof(trees));
69 memset(&t, 0, sizeof(t));
71 trees[nr_trees] = parse_tree_indirect(head);
72 if (!trees[nr_trees++]) {
73 rollback_lock_file(&lock_file);
74 return -1;
76 trees[nr_trees] = parse_tree_indirect(remote);
77 if (!trees[nr_trees++]) {
78 rollback_lock_file(&lock_file);
79 return -1;
81 for (i = 0; i < nr_trees; i++) {
82 if (parse_tree(trees[i]) < 0) {
83 rollback_lock_file(&lock_file);
84 return -1;
86 init_tree_desc(t+i, &trees[i]->object.oid,
87 trees[i]->buffer, trees[i]->size);
90 memset(&opts, 0, sizeof(opts));
91 opts.preserve_ignored = !overwrite_ignore;
93 opts.head_idx = 1;
94 opts.src_index = r->index;
95 opts.dst_index = r->index;
96 opts.update = 1;
97 opts.verbose_update = 1;
98 opts.merge = 1;
99 opts.fn = twoway_merge;
100 init_checkout_metadata(&opts.meta, NULL, remote, NULL);
101 setup_unpack_trees_porcelain(&opts, "merge");
103 if (unpack_trees(nr_trees, t, &opts)) {
104 rollback_lock_file(&lock_file);
105 clear_unpack_trees_porcelain(&opts);
106 return -1;
108 clear_unpack_trees_porcelain(&opts);
110 if (write_locked_index(r->index, &lock_file, COMMIT_LOCK))
111 return error(_("unable to write new index file"));
112 return 0;