merge: pass aggressive when rename detection is turned off
[git/git.git] / merge.c
CommitLineData
db699a8a 1#include "cache.h"
b101793c
EN
2#include "diff.h"
3#include "diffcore.h"
697cc8ef 4#include "lockfile.h"
db699a8a
NTND
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)
f2fd0760 15 return oid_to_hex(&commit->object.oid);
db699a8a
NTND
16 else
17 return EMPTY_TREE_SHA1_HEX;
18}
19
b101793c
EN
20int index_has_changes(struct strbuf *sb)
21{
22 struct object_id head;
23 int i;
24
25 if (!get_oid_tree("HEAD", &head)) {
26 struct diff_options opt;
27
28 diff_setup(&opt);
b6825b5c 29 opt.flags.exit_with_status = 1;
b101793c 30 if (!sb)
b6825b5c 31 opt.flags.quick = 1;
b101793c
EN
32 do_diff_cache(&head, &opt);
33 diffcore_std(&opt);
34 for (i = 0; sb && i < diff_queued_diff.nr; i++) {
35 if (i)
36 strbuf_addch(sb, ' ');
37 strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
38 }
39 diff_flush(&opt);
b6825b5c 40 return opt.flags.has_changes != 0;
b101793c
EN
41 } else {
42 for (i = 0; sb && i < active_nr; i++) {
43 if (i)
44 strbuf_addch(sb, ' ');
45 strbuf_addstr(sb, active_cache[i]->name);
46 }
47 return !!active_nr;
48 }
49}
50
db699a8a
NTND
51int try_merge_command(const char *strategy, size_t xopts_nr,
52 const char **xopts, struct commit_list *common,
53 const char *head_arg, struct commit_list *remotes)
54{
5c1753b1
JK
55 struct argv_array args = ARGV_ARRAY_INIT;
56 int i, ret;
db699a8a 57 struct commit_list *j;
db699a8a 58
5c1753b1
JK
59 argv_array_pushf(&args, "merge-%s", strategy);
60 for (i = 0; i < xopts_nr; i++)
61 argv_array_pushf(&args, "--%s", xopts[i]);
db699a8a 62 for (j = common; j; j = j->next)
5c1753b1
JK
63 argv_array_push(&args, merge_argument(j->item));
64 argv_array_push(&args, "--");
65 argv_array_push(&args, head_arg);
db699a8a 66 for (j = remotes; j; j = j->next)
5c1753b1
JK
67 argv_array_push(&args, merge_argument(j->item));
68
69 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
70 argv_array_clear(&args);
71
db699a8a
NTND
72 discard_cache();
73 if (read_cache() < 0)
74 die(_("failed to read the cache"));
75 resolve_undo_clear();
76
77 return ret;
78}
79
f06e90da 80int checkout_fast_forward(const struct object_id *head,
81 const struct object_id *remote,
db699a8a
NTND
82 int overwrite_ignore)
83{
84 struct tree *trees[MAX_UNPACK_TREES];
85 struct unpack_trees_options opts;
86 struct tree_desc t[MAX_UNPACK_TREES];
03b86647 87 int i, nr_trees = 0;
db699a8a 88 struct dir_struct dir;
837e34eb 89 struct lock_file lock_file = LOCK_INIT;
db699a8a
NTND
90
91 refresh_cache(REFRESH_QUIET);
92
837e34eb 93 if (hold_locked_index(&lock_file, LOCK_REPORT_ON_ERROR) < 0)
55f5704d 94 return -1;
db699a8a
NTND
95
96 memset(&trees, 0, sizeof(trees));
97 memset(&opts, 0, sizeof(opts));
98 memset(&t, 0, sizeof(t));
99 if (overwrite_ignore) {
100 memset(&dir, 0, sizeof(dir));
101 dir.flags |= DIR_SHOW_IGNORED;
102 setup_standard_excludes(&dir);
103 opts.dir = &dir;
104 }
105
106 opts.head_idx = 1;
107 opts.src_index = &the_index;
108 opts.dst_index = &the_index;
109 opts.update = 1;
110 opts.verbose_update = 1;
111 opts.merge = 1;
112 opts.fn = twoway_merge;
113 setup_unpack_trees_porcelain(&opts, "merge");
114
a9dbc179 115 trees[nr_trees] = parse_tree_indirect(head);
5790d258
116 if (!trees[nr_trees++]) {
117 rollback_lock_file(&lock_file);
db699a8a 118 return -1;
5790d258 119 }
a9dbc179 120 trees[nr_trees] = parse_tree_indirect(remote);
5790d258
121 if (!trees[nr_trees++]) {
122 rollback_lock_file(&lock_file);
db699a8a 123 return -1;
5790d258 124 }
db699a8a
NTND
125 for (i = 0; i < nr_trees; i++) {
126 parse_tree(trees[i]);
127 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
128 }
5790d258
129 if (unpack_trees(nr_trees, t, &opts)) {
130 rollback_lock_file(&lock_file);
db699a8a 131 return -1;
5790d258 132 }
df60cf57 133 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
55f5704d 134 return error(_("unable to write new index file"));
db699a8a
NTND
135 return 0;
136}