1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
| | /*
* Builtin "git merge-octopus"
*
* Copyright (c) 2020 Alban Gruin
*
* Based on git-merge-octopus.sh, written by Junio C Hamano.
*
* Resolve two or more trees.
*/
#include "cache.h"
#include "cache-tree.h"
#include "builtin.h"
#include "commit-reach.h"
#include "lockfile.h"
#include "merge-strategies.h"
#include "unpack-trees.h"
static int fast_forward(const struct object_id *oids, int nr, int aggressive)
{
int i;
struct tree_desc t[MAX_UNPACK_TREES];
struct unpack_trees_options opts;
struct lock_file lock = LOCK_INIT;
repo_read_index_preload(the_repository, NULL, 0);
if (refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL))
return -1;
repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
memset(&opts, 0, sizeof(opts));
opts.head_idx = 1;
opts.src_index = the_repository->index;
opts.dst_index = the_repository->index;
opts.merge = 1;
opts.update = 1;
opts.aggressive = aggressive;
for (i = 0; i < nr; i++) {
struct tree *tree;
tree = parse_tree_indirect(oids + i);
if (parse_tree(tree))
return -1;
init_tree_desc(t + i, tree->buffer, tree->size);
}
if (nr == 1)
opts.fn = oneway_merge;
else if (nr == 2) {
opts.fn = twoway_merge;
opts.initial_checkout = is_index_unborn(the_repository->index);
} else if (nr >= 3) {
opts.fn = threeway_merge;
opts.head_idx = nr - 1;
}
if (unpack_trees(nr, t, &opts))
return -1;
if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK))
return error(_("unable to write new index file"));
return 0;
}
static int write_tree(struct tree **reference_tree)
{
struct object_id oid;
int ret;
ret = write_index_as_tree(&oid, the_repository->index,
the_repository->index_file, 0, NULL);
if (!ret)
*reference_tree = lookup_tree(the_repository, &oid);
return ret;
}
static int merge_octopus(struct commit_list *bases, const char *head_arg,
struct commit_list *remotes)
{
int non_ff_merge = 0, ret = 0, references = 1;
struct commit **reference_commit;
struct tree *reference_tree;
struct commit_list *j;
struct object_id head;
struct strbuf sb = STRBUF_INIT;
get_oid(head_arg, &head);
reference_commit = xcalloc(commit_list_count(remotes) + 1, sizeof(struct commit *));
reference_commit[0] = lookup_commit_reference(the_repository, &head);
reference_tree = get_commit_tree(reference_commit[0]);
if (repo_index_has_changes(the_repository, reference_tree, &sb)) {
error(_("Your local changes to the following files "
"would be overwritten by merge:\n %s"),
sb.buf);
strbuf_release(&sb);
ret = 2;
goto out;
}
for (j = remotes; j; j = j->next) {
struct commit *c = j->item;
struct object_id *oid = &c->object.oid;
struct commit_list *common, *k;
char *branch_name;
int can_ff = 1;
if (ret) {
puts(_("Automated merge did not work."));
puts(_("Should not be doing an octopus."));
ret = 2;
goto out;
}
branch_name = merge_get_better_branch_name(oid_to_hex(oid));
common = get_merge_bases_many(c, references, reference_commit);
if (!common)
die(_("Unable to find common commit with %s"), branch_name);
for (k = common; k && !oideq(&k->item->object.oid, oid); k = k->next);
if (k) {
printf(_("Already up to date with %s\n"), branch_name);
free(branch_name);
free_commit_list(common);
continue;
}
if (!non_ff_merge) {
int i;
for (i = 0, k = common; k && i < references && can_ff; k = k->next, i++) {
can_ff = oideq(&k->item->object.oid,
&reference_commit[i]->object.oid);
}
}
if (!non_ff_merge && can_ff) {
struct object_id oids[2];
printf(_("Fast-forwarding to: %s\n"), branch_name);
oidcpy(oids, &head);
oidcpy(oids + 1, oid);
ret = fast_forward(oids, 2, 0);
if (ret) {
free(branch_name);
free_commit_list(common);
goto out;
}
references = 0;
write_tree(&reference_tree);
} else {
int i = 0;
struct tree *next = NULL;
struct object_id oids[MAX_UNPACK_TREES];
non_ff_merge = 1;
printf(_("Trying simple merge with %s\n"), branch_name);
for (k = common; k; k = k->next)
oidcpy(oids + (i++), &k->item->object.oid);
oidcpy(oids + (i++), &reference_tree->object.oid);
oidcpy(oids + (i++), oid);
if (fast_forward(oids, i, 1)) {
ret = 2;
free(branch_name);
free_commit_list(common);
goto out;
}
if (write_tree(&next)) {
struct lock_file lock = LOCK_INIT;
puts(_("Simple merge did not work, trying automatic merge."));
repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
ret = !!merge_all(the_repository->index, 0, 0,
merge_one_file_cb, the_repository);
write_locked_index(the_repository->index, &lock, COMMIT_LOCK);
write_tree(&next);
}
reference_tree = next;
}
reference_commit[references++] = c;
free(branch_name);
free_commit_list(common);
}
out:
free(reference_commit);
return ret;
}
static const char builtin_merge_octopus_usage[] =
"git merge-octopus [<bases>...] -- <head> <remote1> <remote2> [<remotes>...]";
int cmd_merge_octopus(int argc, const char **argv, const char *prefix)
{
int i, sep_seen = 0;
struct commit_list *bases = NULL, *remotes = NULL;
struct commit_list **next_base = &bases, **next_remote = &remotes;
const char *head_arg = NULL;
if (argc < 5)
usage(builtin_merge_octopus_usage);
setup_work_tree();
if (repo_read_index(the_repository) < 0)
die("corrupted cache");
/* The first parameters up to -- are merge bases; the rest are
* heads. */
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "--") == 0)
sep_seen = 1;
else if (strcmp(argv[i], "-h") == 0)
usage(builtin_merge_octopus_usage);
else if (sep_seen && !head_arg)
head_arg = argv[i];
else {
struct object_id oid;
get_oid(argv[i], &oid);
if (!oideq(&oid, the_hash_algo->empty_tree)) {
struct commit *commit;
commit = lookup_commit_or_die(&oid, argv[i]);
if (sep_seen)
next_remote = commit_list_append(commit, next_remote);
else
next_base = commit_list_append(commit, next_base);
}
}
}
/* Reject if this is not an octopus -- resolve should be used
* instead. */
if (commit_list_count(remotes) < 2)
return 2;
return merge_octopus(bases, head_arg, remotes);
}
|