]>
Commit | Line | Data |
---|---|---|
8f1d2e6f | 1 | #include "cache.h" |
961784ee | 2 | #include "tag.h" |
175785e5 | 3 | #include "commit.h" |
ed09aef0 | 4 | #include "pkt-line.h" |
52883fbd | 5 | #include "utf8.h" |
199c45bf JH |
6 | #include "diff.h" |
7 | #include "revision.h" | |
a97a7468 | 8 | #include "notes.h" |
175785e5 | 9 | |
60ab26de LT |
10 | int save_commit_buffer = 1; |
11 | ||
175785e5 DB |
12 | const char *commit_type = "commit"; |
13 | ||
f76412ed JH |
14 | static struct commit *check_commit(struct object *obj, |
15 | const unsigned char *sha1, | |
16 | int quiet) | |
961784ee | 17 | { |
1974632c | 18 | if (obj->type != OBJ_COMMIT) { |
f76412ed JH |
19 | if (!quiet) |
20 | error("Object %s is a %s, not a commit", | |
885a86ab | 21 | sha1_to_hex(sha1), typename(obj->type)); |
961784ee LT |
22 | return NULL; |
23 | } | |
24 | return (struct commit *) obj; | |
25 | } | |
26 | ||
f76412ed JH |
27 | struct commit *lookup_commit_reference_gently(const unsigned char *sha1, |
28 | int quiet) | |
961784ee | 29 | { |
9534f40b | 30 | struct object *obj = deref_tag(parse_object(sha1), NULL, 0); |
961784ee LT |
31 | |
32 | if (!obj) | |
33 | return NULL; | |
f76412ed JH |
34 | return check_commit(obj, sha1, quiet); |
35 | } | |
36 | ||
37 | struct commit *lookup_commit_reference(const unsigned char *sha1) | |
38 | { | |
39 | return lookup_commit_reference_gently(sha1, 0); | |
961784ee LT |
40 | } |
41 | ||
5d6ccf5c | 42 | struct commit *lookup_commit(const unsigned char *sha1) |
175785e5 DB |
43 | { |
44 | struct object *obj = lookup_object(sha1); | |
100c5f3b LT |
45 | if (!obj) |
46 | return create_object(sha1, OBJ_COMMIT, alloc_commit_node()); | |
d1af002d | 47 | if (!obj->type) |
1974632c | 48 | obj->type = OBJ_COMMIT; |
f76412ed | 49 | return check_commit(obj, sha1, 0); |
175785e5 DB |
50 | } |
51 | ||
a6fa5992 PN |
52 | struct commit *lookup_commit_reference_by_name(const char *name) |
53 | { | |
54 | unsigned char sha1[20]; | |
55 | struct commit *commit; | |
56 | ||
57 | if (get_sha1(name, sha1)) | |
58 | return NULL; | |
59 | commit = lookup_commit_reference(sha1); | |
60 | if (!commit || parse_commit(commit)) | |
61 | return NULL; | |
62 | return commit; | |
63 | } | |
64 | ||
0a617799 | 65 | static unsigned long parse_commit_date(const char *buf, const char *tail) |
175785e5 | 66 | { |
0a617799 | 67 | const char *dateptr; |
175785e5 | 68 | |
0a617799 MK |
69 | if (buf + 6 >= tail) |
70 | return 0; | |
175785e5 DB |
71 | if (memcmp(buf, "author", 6)) |
72 | return 0; | |
0a617799 | 73 | while (buf < tail && *buf++ != '\n') |
175785e5 | 74 | /* nada */; |
0a617799 MK |
75 | if (buf + 9 >= tail) |
76 | return 0; | |
175785e5 DB |
77 | if (memcmp(buf, "committer", 9)) |
78 | return 0; | |
0a617799 | 79 | while (buf < tail && *buf++ != '>') |
175785e5 | 80 | /* nada */; |
0a617799 MK |
81 | if (buf >= tail) |
82 | return 0; | |
83 | dateptr = buf; | |
84 | while (buf < tail && *buf++ != '\n') | |
85 | /* nada */; | |
86 | if (buf >= tail) | |
87 | return 0; | |
88 | /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */ | |
f2909743 | 89 | return strtoul(dateptr, NULL, 10); |
175785e5 DB |
90 | } |
91 | ||
5040f17e | 92 | static struct commit_graft **commit_graft; |
5da5c8f4 JH |
93 | static int commit_graft_alloc, commit_graft_nr; |
94 | ||
95 | static int commit_graft_pos(const unsigned char *sha1) | |
96 | { | |
97 | int lo, hi; | |
98 | lo = 0; | |
99 | hi = commit_graft_nr; | |
100 | while (lo < hi) { | |
101 | int mi = (lo + hi) / 2; | |
102 | struct commit_graft *graft = commit_graft[mi]; | |
a89fccd2 | 103 | int cmp = hashcmp(sha1, graft->sha1); |
5da5c8f4 JH |
104 | if (!cmp) |
105 | return mi; | |
106 | if (cmp < 0) | |
107 | hi = mi; | |
108 | else | |
109 | lo = mi + 1; | |
110 | } | |
111 | return -lo - 1; | |
112 | } | |
113 | ||
5040f17e JH |
114 | int register_commit_graft(struct commit_graft *graft, int ignore_dups) |
115 | { | |
116 | int pos = commit_graft_pos(graft->sha1); | |
a6080a0a | 117 | |
5040f17e JH |
118 | if (0 <= pos) { |
119 | if (ignore_dups) | |
120 | free(graft); | |
121 | else { | |
122 | free(commit_graft[pos]); | |
123 | commit_graft[pos] = graft; | |
124 | } | |
125 | return 1; | |
126 | } | |
127 | pos = -pos - 1; | |
128 | if (commit_graft_alloc <= ++commit_graft_nr) { | |
129 | commit_graft_alloc = alloc_nr(commit_graft_alloc); | |
130 | commit_graft = xrealloc(commit_graft, | |
131 | sizeof(*commit_graft) * | |
132 | commit_graft_alloc); | |
133 | } | |
134 | if (pos < commit_graft_nr) | |
135 | memmove(commit_graft + pos + 1, | |
136 | commit_graft + pos, | |
137 | (commit_graft_nr - pos - 1) * | |
138 | sizeof(*commit_graft)); | |
139 | commit_graft[pos] = graft; | |
140 | return 0; | |
141 | } | |
142 | ||
143 | struct commit_graft *read_graft_line(char *buf, int len) | |
144 | { | |
145 | /* The format is just "Commit Parent1 Parent2 ...\n" */ | |
146 | int i; | |
147 | struct commit_graft *graft = NULL; | |
148 | ||
fe0a3cb2 JH |
149 | while (len && isspace(buf[len-1])) |
150 | buf[--len] = '\0'; | |
360204c3 | 151 | if (buf[0] == '#' || buf[0] == '\0') |
5bc4ce58 | 152 | return NULL; |
df5d43be RT |
153 | if ((len + 1) % 41) |
154 | goto bad_graft_data; | |
5040f17e JH |
155 | i = (len + 1) / 41 - 1; |
156 | graft = xmalloc(sizeof(*graft) + 20 * i); | |
157 | graft->nr_parent = i; | |
158 | if (get_sha1_hex(buf, graft->sha1)) | |
159 | goto bad_graft_data; | |
160 | for (i = 40; i < len; i += 41) { | |
161 | if (buf[i] != ' ') | |
162 | goto bad_graft_data; | |
163 | if (get_sha1_hex(buf + i + 1, graft->parent[i/41])) | |
164 | goto bad_graft_data; | |
165 | } | |
166 | return graft; | |
df5d43be RT |
167 | |
168 | bad_graft_data: | |
169 | error("bad graft data: %s", buf); | |
170 | free(graft); | |
171 | return NULL; | |
5040f17e JH |
172 | } |
173 | ||
c5ae6439 | 174 | static int read_graft_file(const char *graft_file) |
5da5c8f4 | 175 | { |
5da5c8f4 JH |
176 | FILE *fp = fopen(graft_file, "r"); |
177 | char buf[1024]; | |
5040f17e JH |
178 | if (!fp) |
179 | return -1; | |
5da5c8f4 JH |
180 | while (fgets(buf, sizeof(buf), fp)) { |
181 | /* The format is just "Commit Parent1 Parent2 ...\n" */ | |
182 | int len = strlen(buf); | |
5040f17e | 183 | struct commit_graft *graft = read_graft_line(buf, len); |
5bc4ce58 JH |
184 | if (!graft) |
185 | continue; | |
5040f17e | 186 | if (register_commit_graft(graft, 1)) |
5da5c8f4 | 187 | error("duplicate graft data: %s", buf); |
5da5c8f4 JH |
188 | } |
189 | fclose(fp); | |
5040f17e JH |
190 | return 0; |
191 | } | |
192 | ||
193 | static void prepare_commit_graft(void) | |
194 | { | |
195 | static int commit_graft_prepared; | |
196 | char *graft_file; | |
197 | ||
198 | if (commit_graft_prepared) | |
199 | return; | |
200 | graft_file = get_graft_file(); | |
201 | read_graft_file(graft_file); | |
ed09aef0 JS |
202 | /* make sure shallows are read */ |
203 | is_repository_shallow(); | |
5040f17e | 204 | commit_graft_prepared = 1; |
5da5c8f4 JH |
205 | } |
206 | ||
45163382 | 207 | struct commit_graft *lookup_commit_graft(const unsigned char *sha1) |
5da5c8f4 JH |
208 | { |
209 | int pos; | |
5040f17e | 210 | prepare_commit_graft(); |
5da5c8f4 JH |
211 | pos = commit_graft_pos(sha1); |
212 | if (pos < 0) | |
213 | return NULL; | |
214 | return commit_graft[pos]; | |
215 | } | |
216 | ||
09d46644 | 217 | int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data) |
ed09aef0 | 218 | { |
09d46644 NTND |
219 | int i, ret; |
220 | for (i = ret = 0; i < commit_graft_nr && !ret; i++) | |
221 | ret = fn(commit_graft[i], cb_data); | |
222 | return ret; | |
ed09aef0 JS |
223 | } |
224 | ||
f53514bc JS |
225 | int unregister_shallow(const unsigned char *sha1) |
226 | { | |
227 | int pos = commit_graft_pos(sha1); | |
228 | if (pos < 0) | |
229 | return -1; | |
230 | if (pos + 1 < commit_graft_nr) | |
65d41d48 | 231 | memmove(commit_graft + pos, commit_graft + pos + 1, |
f53514bc JS |
232 | sizeof(struct commit_graft *) |
233 | * (commit_graft_nr - pos - 1)); | |
234 | commit_graft_nr--; | |
235 | return 0; | |
236 | } | |
237 | ||
cf7b1cad | 238 | int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size) |
175785e5 | 239 | { |
cf7b1cad NTND |
240 | const char *tail = buffer; |
241 | const char *bufptr = buffer; | |
175785e5 | 242 | unsigned char parent[20]; |
6c88be16 | 243 | struct commit_list **pptr; |
5da5c8f4 | 244 | struct commit_graft *graft; |
bd2c39f5 | 245 | |
175785e5 DB |
246 | if (item->object.parsed) |
247 | return 0; | |
248 | item->object.parsed = 1; | |
3b44f15a | 249 | tail += size; |
0a617799 | 250 | if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n') |
f7cc77d7 | 251 | return error("bogus commit object %s", sha1_to_hex(item->object.sha1)); |
0a617799 | 252 | if (get_sha1_hex(bufptr + 5, parent) < 0) |
bd2afde8 JH |
253 | return error("bad tree pointer in commit %s", |
254 | sha1_to_hex(item->object.sha1)); | |
175785e5 | 255 | item->tree = lookup_tree(parent); |
175785e5 | 256 | bufptr += 46; /* "tree " + "hex sha1" + "\n" */ |
6c88be16 | 257 | pptr = &item->parents; |
5da5c8f4 JH |
258 | |
259 | graft = lookup_commit_graft(item->object.sha1); | |
3b44f15a | 260 | while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) { |
f7cc77d7 LT |
261 | struct commit *new_parent; |
262 | ||
3b44f15a JH |
263 | if (tail <= bufptr + 48 || |
264 | get_sha1_hex(bufptr + 7, parent) || | |
265 | bufptr[47] != '\n') | |
f7cc77d7 | 266 | return error("bad parents in commit %s", sha1_to_hex(item->object.sha1)); |
5da5c8f4 | 267 | bufptr += 48; |
7f3140cd JS |
268 | /* |
269 | * The clone is shallow if nr_parent < 0, and we must | |
270 | * not traverse its real parents even when we unhide them. | |
271 | */ | |
272 | if (graft && (graft->nr_parent < 0 || grafts_replace_parents)) | |
5da5c8f4 | 273 | continue; |
f7cc77d7 | 274 | new_parent = lookup_commit(parent); |
39468def | 275 | if (new_parent) |
6c88be16 | 276 | pptr = &commit_list_insert(new_parent, pptr)->next; |
5da5c8f4 JH |
277 | } |
278 | if (graft) { | |
279 | int i; | |
280 | struct commit *new_parent; | |
281 | for (i = 0; i < graft->nr_parent; i++) { | |
282 | new_parent = lookup_commit(graft->parent[i]); | |
283 | if (!new_parent) | |
284 | continue; | |
285 | pptr = &commit_list_insert(new_parent, pptr)->next; | |
5da5c8f4 | 286 | } |
175785e5 | 287 | } |
0a617799 | 288 | item->date = parse_commit_date(bufptr, tail); |
27dedf0c | 289 | |
175785e5 DB |
290 | return 0; |
291 | } | |
292 | ||
bd2c39f5 NP |
293 | int parse_commit(struct commit *item) |
294 | { | |
21666f1a | 295 | enum object_type type; |
bd2c39f5 NP |
296 | void *buffer; |
297 | unsigned long size; | |
298 | int ret; | |
299 | ||
9786f68b MK |
300 | if (!item) |
301 | return -1; | |
bd2c39f5 NP |
302 | if (item->object.parsed) |
303 | return 0; | |
21666f1a | 304 | buffer = read_sha1_file(item->object.sha1, &type, &size); |
bd2c39f5 NP |
305 | if (!buffer) |
306 | return error("Could not read %s", | |
307 | sha1_to_hex(item->object.sha1)); | |
21666f1a | 308 | if (type != OBJ_COMMIT) { |
bd2c39f5 NP |
309 | free(buffer); |
310 | return error("Object %s not a commit", | |
311 | sha1_to_hex(item->object.sha1)); | |
312 | } | |
313 | ret = parse_commit_buffer(item, buffer, size); | |
60ab26de | 314 | if (save_commit_buffer && !ret) { |
3ff1fbbb LT |
315 | item->buffer = buffer; |
316 | return 0; | |
317 | } | |
bd2c39f5 NP |
318 | free(buffer); |
319 | return ret; | |
320 | } | |
321 | ||
11af2aae CC |
322 | int find_commit_subject(const char *commit_buffer, const char **subject) |
323 | { | |
324 | const char *eol; | |
325 | const char *p = commit_buffer; | |
326 | ||
327 | while (*p && (*p != '\n' || p[1] != '\n')) | |
328 | p++; | |
329 | if (*p) { | |
330 | p += 2; | |
331 | for (eol = p; *eol && *eol != '\n'; eol++) | |
332 | ; /* do nothing */ | |
333 | } else | |
334 | eol = p; | |
335 | ||
336 | *subject = p; | |
337 | ||
338 | return eol - p; | |
339 | } | |
340 | ||
ac5155ef | 341 | struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p) |
dd97f850 | 342 | { |
812666c8 | 343 | struct commit_list *new_list = xmalloc(sizeof(struct commit_list)); |
dd97f850 DB |
344 | new_list->item = item; |
345 | new_list->next = *list_p; | |
346 | *list_p = new_list; | |
ac5155ef | 347 | return new_list; |
dd97f850 DB |
348 | } |
349 | ||
65319475 MV |
350 | unsigned commit_list_count(const struct commit_list *l) |
351 | { | |
352 | unsigned c = 0; | |
353 | for (; l; l = l->next ) | |
354 | c++; | |
355 | return c; | |
356 | } | |
357 | ||
175785e5 DB |
358 | void free_commit_list(struct commit_list *list) |
359 | { | |
360 | while (list) { | |
361 | struct commit_list *temp = list; | |
362 | list = temp->next; | |
363 | free(temp); | |
364 | } | |
365 | } | |
dd97f850 | 366 | |
47e44ed1 | 367 | struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list) |
dd97f850 DB |
368 | { |
369 | struct commit_list **pp = list; | |
370 | struct commit_list *p; | |
371 | while ((p = *pp) != NULL) { | |
372 | if (p->item->date < item->date) { | |
373 | break; | |
374 | } | |
375 | pp = &p->next; | |
376 | } | |
f755494c | 377 | return commit_list_insert(item, pp); |
dd97f850 DB |
378 | } |
379 | ||
a6080a0a | 380 | |
47e44ed1 | 381 | void commit_list_sort_by_date(struct commit_list **list) |
dd97f850 DB |
382 | { |
383 | struct commit_list *ret = NULL; | |
384 | while (*list) { | |
47e44ed1 | 385 | commit_list_insert_by_date((*list)->item, &ret); |
dd97f850 DB |
386 | *list = (*list)->next; |
387 | } | |
388 | *list = ret; | |
389 | } | |
390 | ||
58e28af6 DB |
391 | struct commit *pop_most_recent_commit(struct commit_list **list, |
392 | unsigned int mark) | |
dd97f850 DB |
393 | { |
394 | struct commit *ret = (*list)->item; | |
395 | struct commit_list *parents = ret->parents; | |
396 | struct commit_list *old = *list; | |
397 | ||
398 | *list = (*list)->next; | |
399 | free(old); | |
400 | ||
401 | while (parents) { | |
4056c091 | 402 | struct commit *commit = parents->item; |
dec38c81 | 403 | if (!parse_commit(commit) && !(commit->object.flags & mark)) { |
58e28af6 | 404 | commit->object.flags |= mark; |
47e44ed1 | 405 | commit_list_insert_by_date(commit, list); |
4056c091 | 406 | } |
dd97f850 DB |
407 | parents = parents->next; |
408 | } | |
409 | return ret; | |
410 | } | |
e3bc7a3b | 411 | |
f8f9c73c JH |
412 | void clear_commit_marks(struct commit *commit, unsigned int mark) |
413 | { | |
60fcc2e6 JS |
414 | while (commit) { |
415 | struct commit_list *parents; | |
f8f9c73c | 416 | |
60fcc2e6 JS |
417 | if (!(mark & commit->object.flags)) |
418 | return; | |
58ecf5c1 | 419 | |
60fcc2e6 JS |
420 | commit->object.flags &= ~mark; |
421 | ||
422 | parents = commit->parents; | |
423 | if (!parents) | |
424 | return; | |
425 | ||
426 | while ((parents = parents->next)) | |
427 | clear_commit_marks(parents->item, mark); | |
428 | ||
429 | commit = commit->parents->item; | |
f8f9c73c JH |
430 | } |
431 | } | |
432 | ||
a3437b8c JS |
433 | struct commit *pop_commit(struct commit_list **stack) |
434 | { | |
435 | struct commit_list *top = *stack; | |
436 | struct commit *item = top ? top->item : NULL; | |
437 | ||
438 | if (top) { | |
439 | *stack = top->next; | |
440 | free(top); | |
441 | } | |
442 | return item; | |
443 | } | |
444 | ||
ab580ace JS |
445 | /* |
446 | * Performs an in-place topological sort on the list supplied. | |
447 | */ | |
4c8725f1 | 448 | void sort_in_topological_order(struct commit_list ** list, int lifo) |
6b6dcfc2 | 449 | { |
23c17d4a LT |
450 | struct commit_list *next, *orig = *list; |
451 | struct commit_list *work, **insert; | |
452 | struct commit_list **pptr; | |
a6080a0a | 453 | |
23c17d4a | 454 | if (!orig) |
7d6fb370 | 455 | return; |
23c17d4a LT |
456 | *list = NULL; |
457 | ||
458 | /* Mark them and clear the indegree */ | |
459 | for (next = orig; next; next = next->next) { | |
460 | struct commit *commit = next->item; | |
e358f3c3 | 461 | commit->indegree = 1; |
ab580ace | 462 | } |
23c17d4a | 463 | |
ab580ace | 464 | /* update the indegree */ |
23c17d4a | 465 | for (next = orig; next; next = next->next) { |
ab580ace JS |
466 | struct commit_list * parents = next->item->parents; |
467 | while (parents) { | |
23c17d4a | 468 | struct commit *parent = parents->item; |
6b6dcfc2 | 469 | |
e358f3c3 | 470 | if (parent->indegree) |
23c17d4a LT |
471 | parent->indegree++; |
472 | parents = parents->next; | |
ab580ace | 473 | } |
ab580ace | 474 | } |
23c17d4a | 475 | |
a6080a0a | 476 | /* |
674d1727 PH |
477 | * find the tips |
478 | * | |
479 | * tips are nodes not reachable from any other node in the list | |
480 | * | |
481 | * the tips serve as a starting set for the work queue. | |
482 | */ | |
23c17d4a | 483 | work = NULL; |
2ed02887 | 484 | insert = &work; |
23c17d4a LT |
485 | for (next = orig; next; next = next->next) { |
486 | struct commit *commit = next->item; | |
ab580ace | 487 | |
e358f3c3 | 488 | if (commit->indegree == 1) |
23c17d4a | 489 | insert = &commit_list_insert(commit, insert)->next; |
ab580ace | 490 | } |
4c8725f1 | 491 | |
ab580ace | 492 | /* process the list in topological order */ |
4c8725f1 | 493 | if (!lifo) |
47e44ed1 | 494 | commit_list_sort_by_date(&work); |
23c17d4a LT |
495 | |
496 | pptr = list; | |
497 | *list = NULL; | |
ab580ace | 498 | while (work) { |
23c17d4a LT |
499 | struct commit *commit; |
500 | struct commit_list *parents, *work_item; | |
ab580ace | 501 | |
23c17d4a LT |
502 | work_item = work; |
503 | work = work_item->next; | |
504 | work_item->next = NULL; | |
505 | ||
506 | commit = work_item->item; | |
507 | for (parents = commit->parents; parents ; parents = parents->next) { | |
cd2b8ae9 | 508 | struct commit *parent = parents->item; |
23c17d4a | 509 | |
e358f3c3 | 510 | if (!parent->indegree) |
23c17d4a LT |
511 | continue; |
512 | ||
513 | /* | |
514 | * parents are only enqueued for emission | |
515 | * when all their children have been emitted thereby | |
516 | * guaranteeing topological order. | |
517 | */ | |
e358f3c3 | 518 | if (--parent->indegree == 1) { |
23c17d4a | 519 | if (!lifo) |
47e44ed1 | 520 | commit_list_insert_by_date(parent, &work); |
23c17d4a LT |
521 | else |
522 | commit_list_insert(parent, &work); | |
ab580ace | 523 | } |
ab580ace JS |
524 | } |
525 | /* | |
674d1727 PH |
526 | * work_item is a commit all of whose children |
527 | * have already been emitted. we can emit it now. | |
528 | */ | |
e358f3c3 | 529 | commit->indegree = 0; |
23c17d4a LT |
530 | *pptr = work_item; |
531 | pptr = &work_item->next; | |
ab580ace | 532 | } |
ab580ace | 533 | } |
7c6f8aaf | 534 | |
1295228d | 535 | /* merge-base stuff */ |
7c6f8aaf | 536 | |
577ed5c2 JH |
537 | /* bits #0..15 in revision.h */ |
538 | #define PARENT1 (1u<<16) | |
539 | #define PARENT2 (1u<<17) | |
540 | #define STALE (1u<<18) | |
541 | #define RESULT (1u<<19) | |
7c6f8aaf | 542 | |
1295228d JH |
543 | static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT); |
544 | ||
7c6f8aaf JS |
545 | static struct commit *interesting(struct commit_list *list) |
546 | { | |
547 | while (list) { | |
548 | struct commit *commit = list->item; | |
549 | list = list->next; | |
542ccefe | 550 | if (commit->object.flags & STALE) |
7c6f8aaf JS |
551 | continue; |
552 | return commit; | |
553 | } | |
554 | return NULL; | |
555 | } | |
556 | ||
6a938648 | 557 | static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos) |
7c6f8aaf JS |
558 | { |
559 | struct commit_list *list = NULL; | |
560 | struct commit_list *result = NULL; | |
6a938648 | 561 | int i; |
7c6f8aaf | 562 | |
6a938648 JH |
563 | for (i = 0; i < n; i++) { |
564 | if (one == twos[i]) | |
565 | /* | |
566 | * We do not mark this even with RESULT so we do not | |
567 | * have to clean it up. | |
568 | */ | |
569 | return commit_list_insert(one, &result); | |
570 | } | |
7c6f8aaf | 571 | |
172947e6 MK |
572 | if (parse_commit(one)) |
573 | return NULL; | |
6a938648 JH |
574 | for (i = 0; i < n; i++) { |
575 | if (parse_commit(twos[i])) | |
576 | return NULL; | |
577 | } | |
7c6f8aaf | 578 | |
f3249438 | 579 | one->object.flags |= PARENT1; |
47e44ed1 | 580 | commit_list_insert_by_date(one, &list); |
6a938648 JH |
581 | for (i = 0; i < n; i++) { |
582 | twos[i]->object.flags |= PARENT2; | |
47e44ed1 | 583 | commit_list_insert_by_date(twos[i], &list); |
6a938648 | 584 | } |
7c6f8aaf JS |
585 | |
586 | while (interesting(list)) { | |
f3249438 | 587 | struct commit *commit; |
7c6f8aaf | 588 | struct commit_list *parents; |
f203d696 | 589 | struct commit_list *next; |
f3249438 | 590 | int flags; |
7c6f8aaf | 591 | |
f3249438 | 592 | commit = list->item; |
f203d696 | 593 | next = list->next; |
f3249438 | 594 | free(list); |
f203d696 | 595 | list = next; |
7c6f8aaf | 596 | |
f3249438 JH |
597 | flags = commit->object.flags & (PARENT1 | PARENT2 | STALE); |
598 | if (flags == (PARENT1 | PARENT2)) { | |
599 | if (!(commit->object.flags & RESULT)) { | |
600 | commit->object.flags |= RESULT; | |
47e44ed1 | 601 | commit_list_insert_by_date(commit, &result); |
f3249438 | 602 | } |
542ccefe JH |
603 | /* Mark parents of a found merge stale */ |
604 | flags |= STALE; | |
7c6f8aaf JS |
605 | } |
606 | parents = commit->parents; | |
607 | while (parents) { | |
608 | struct commit *p = parents->item; | |
609 | parents = parents->next; | |
610 | if ((p->object.flags & flags) == flags) | |
611 | continue; | |
172947e6 MK |
612 | if (parse_commit(p)) |
613 | return NULL; | |
7c6f8aaf | 614 | p->object.flags |= flags; |
47e44ed1 | 615 | commit_list_insert_by_date(p, &list); |
7c6f8aaf JS |
616 | } |
617 | } | |
618 | ||
f3249438 | 619 | /* Clean up the result to remove stale ones */ |
1295228d | 620 | free_commit_list(list); |
f3249438 JH |
621 | list = result; result = NULL; |
622 | while (list) { | |
f203d696 | 623 | struct commit_list *next = list->next; |
f3249438 | 624 | if (!(list->item->object.flags & STALE)) |
47e44ed1 | 625 | commit_list_insert_by_date(list->item, &result); |
f3249438 | 626 | free(list); |
f203d696 | 627 | list = next; |
f3249438 JH |
628 | } |
629 | return result; | |
630 | } | |
7c6f8aaf | 631 | |
5240c9d7 MV |
632 | struct commit_list *get_octopus_merge_bases(struct commit_list *in) |
633 | { | |
634 | struct commit_list *i, *j, *k, *ret = NULL; | |
635 | struct commit_list **pptr = &ret; | |
636 | ||
637 | for (i = in; i; i = i->next) { | |
638 | if (!ret) | |
639 | pptr = &commit_list_insert(i->item, pptr)->next; | |
640 | else { | |
641 | struct commit_list *new = NULL, *end = NULL; | |
642 | ||
643 | for (j = ret; j; j = j->next) { | |
644 | struct commit_list *bases; | |
645 | bases = get_merge_bases(i->item, j->item, 1); | |
646 | if (!new) | |
647 | new = bases; | |
648 | else | |
649 | end->next = bases; | |
650 | for (k = bases; k; k = k->next) | |
651 | end = k; | |
652 | } | |
653 | ret = new; | |
654 | } | |
655 | } | |
656 | return ret; | |
657 | } | |
658 | ||
6a938648 JH |
659 | struct commit_list *get_merge_bases_many(struct commit *one, |
660 | int n, | |
661 | struct commit **twos, | |
662 | int cleanup) | |
f3249438 | 663 | { |
f3249438 JH |
664 | struct commit_list *list; |
665 | struct commit **rslt; | |
666 | struct commit_list *result; | |
667 | int cnt, i, j; | |
668 | ||
6a938648 JH |
669 | result = merge_bases_many(one, n, twos); |
670 | for (i = 0; i < n; i++) { | |
671 | if (one == twos[i]) | |
672 | return result; | |
673 | } | |
f3249438 JH |
674 | if (!result || !result->next) { |
675 | if (cleanup) { | |
676 | clear_commit_marks(one, all_flags); | |
6a938648 JH |
677 | for (i = 0; i < n; i++) |
678 | clear_commit_marks(twos[i], all_flags); | |
7c6f8aaf | 679 | } |
f3249438 | 680 | return result; |
7c6f8aaf JS |
681 | } |
682 | ||
f3249438 JH |
683 | /* There are more than one */ |
684 | cnt = 0; | |
685 | list = result; | |
686 | while (list) { | |
687 | list = list->next; | |
688 | cnt++; | |
689 | } | |
690 | rslt = xcalloc(cnt, sizeof(*rslt)); | |
691 | for (list = result, i = 0; list; list = list->next) | |
692 | rslt[i++] = list->item; | |
693 | free_commit_list(result); | |
694 | ||
695 | clear_commit_marks(one, all_flags); | |
6a938648 JH |
696 | for (i = 0; i < n; i++) |
697 | clear_commit_marks(twos[i], all_flags); | |
f3249438 JH |
698 | for (i = 0; i < cnt - 1; i++) { |
699 | for (j = i+1; j < cnt; j++) { | |
700 | if (!rslt[i] || !rslt[j]) | |
701 | continue; | |
6a938648 | 702 | result = merge_bases_many(rslt[i], 1, &rslt[j]); |
f3249438 JH |
703 | clear_commit_marks(rslt[i], all_flags); |
704 | clear_commit_marks(rslt[j], all_flags); | |
705 | for (list = result; list; list = list->next) { | |
706 | if (rslt[i] == list->item) | |
707 | rslt[i] = NULL; | |
708 | if (rslt[j] == list->item) | |
709 | rslt[j] = NULL; | |
710 | } | |
711 | } | |
58ecf5c1 | 712 | } |
7c6f8aaf | 713 | |
f3249438 JH |
714 | /* Surviving ones in rslt[] are the independent results */ |
715 | result = NULL; | |
716 | for (i = 0; i < cnt; i++) { | |
717 | if (rslt[i]) | |
47e44ed1 | 718 | commit_list_insert_by_date(rslt[i], &result); |
f3249438 JH |
719 | } |
720 | free(rslt); | |
7c6f8aaf JS |
721 | return result; |
722 | } | |
2ecd2bbc | 723 | |
6a938648 JH |
724 | struct commit_list *get_merge_bases(struct commit *one, struct commit *two, |
725 | int cleanup) | |
726 | { | |
727 | return get_merge_bases_many(one, 1, &two, cleanup); | |
728 | } | |
729 | ||
7fcdb36e JG |
730 | int is_descendant_of(struct commit *commit, struct commit_list *with_commit) |
731 | { | |
732 | if (!with_commit) | |
733 | return 1; | |
734 | while (with_commit) { | |
735 | struct commit *other; | |
736 | ||
737 | other = with_commit->item; | |
738 | with_commit = with_commit->next; | |
739 | if (in_merge_bases(other, &commit, 1)) | |
740 | return 1; | |
741 | } | |
742 | return 0; | |
743 | } | |
744 | ||
03840fc3 | 745 | int in_merge_bases(struct commit *commit, struct commit **reference, int num) |
2ecd2bbc JH |
746 | { |
747 | struct commit_list *bases, *b; | |
748 | int ret = 0; | |
749 | ||
03840fc3 JH |
750 | if (num == 1) |
751 | bases = get_merge_bases(commit, *reference, 1); | |
752 | else | |
753 | die("not yet"); | |
2ecd2bbc | 754 | for (b = bases; b; b = b->next) { |
03840fc3 | 755 | if (!hashcmp(commit->object.sha1, b->item->object.sha1)) { |
2ecd2bbc JH |
756 | ret = 1; |
757 | break; | |
758 | } | |
759 | } | |
760 | ||
761 | free_commit_list(bases); | |
762 | return ret; | |
763 | } | |
98cf9c3b JH |
764 | |
765 | struct commit_list *reduce_heads(struct commit_list *heads) | |
766 | { | |
767 | struct commit_list *p; | |
768 | struct commit_list *result = NULL, **tail = &result; | |
769 | struct commit **other; | |
770 | size_t num_head, num_other; | |
771 | ||
772 | if (!heads) | |
773 | return NULL; | |
774 | ||
775 | /* Avoid unnecessary reallocations */ | |
776 | for (p = heads, num_head = 0; p; p = p->next) | |
777 | num_head++; | |
778 | other = xcalloc(sizeof(*other), num_head); | |
779 | ||
780 | /* For each commit, see if it can be reached by others */ | |
781 | for (p = heads; p; p = p->next) { | |
782 | struct commit_list *q, *base; | |
783 | ||
711f6b29 JH |
784 | /* Do we already have this in the result? */ |
785 | for (q = result; q; q = q->next) | |
786 | if (p->item == q->item) | |
787 | break; | |
788 | if (q) | |
789 | continue; | |
790 | ||
98cf9c3b JH |
791 | num_other = 0; |
792 | for (q = heads; q; q = q->next) { | |
3d1dd472 | 793 | if (p->item == q->item) |
98cf9c3b JH |
794 | continue; |
795 | other[num_other++] = q->item; | |
796 | } | |
711f6b29 | 797 | if (num_other) |
98cf9c3b | 798 | base = get_merge_bases_many(p->item, num_other, other, 1); |
711f6b29 | 799 | else |
98cf9c3b JH |
800 | base = NULL; |
801 | /* | |
802 | * If p->item does not have anything common with other | |
803 | * commits, there won't be any merge base. If it is | |
804 | * reachable from some of the others, p->item will be | |
805 | * the merge base. If its history is connected with | |
806 | * others, but p->item is not reachable by others, we | |
807 | * will get something other than p->item back. | |
808 | */ | |
809 | if (!base || (base->item != p->item)) | |
810 | tail = &(commit_list_insert(p->item, tail)->next); | |
811 | free_commit_list(base); | |
812 | } | |
813 | free(other); | |
814 | return result; | |
815 | } | |
40d52ff7 JK |
816 | |
817 | static const char commit_utf8_warn[] = | |
818 | "Warning: commit message does not conform to UTF-8.\n" | |
819 | "You may want to amend it after fixing the message, or set the config\n" | |
820 | "variable i18n.commitencoding to the encoding your project uses.\n"; | |
821 | ||
822 | int commit_tree(const char *msg, unsigned char *tree, | |
823 | struct commit_list *parents, unsigned char *ret, | |
824 | const char *author) | |
825 | { | |
826 | int result; | |
827 | int encoding_is_utf8; | |
828 | struct strbuf buffer; | |
829 | ||
830 | assert_sha1_type(tree, OBJ_TREE); | |
831 | ||
832 | /* Not having i18n.commitencoding is the same as having utf-8 */ | |
833 | encoding_is_utf8 = is_encoding_utf8(git_commit_encoding); | |
834 | ||
835 | strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */ | |
836 | strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree)); | |
837 | ||
838 | /* | |
839 | * NOTE! This ordering means that the same exact tree merged with a | |
840 | * different order of parents will be a _different_ changeset even | |
841 | * if everything else stays the same. | |
842 | */ | |
843 | while (parents) { | |
844 | struct commit_list *next = parents->next; | |
845 | strbuf_addf(&buffer, "parent %s\n", | |
846 | sha1_to_hex(parents->item->object.sha1)); | |
847 | free(parents); | |
848 | parents = next; | |
849 | } | |
850 | ||
851 | /* Person/date information */ | |
852 | if (!author) | |
853 | author = git_author_info(IDENT_ERROR_ON_NO_NAME); | |
854 | strbuf_addf(&buffer, "author %s\n", author); | |
855 | strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME)); | |
856 | if (!encoding_is_utf8) | |
857 | strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding); | |
858 | strbuf_addch(&buffer, '\n'); | |
859 | ||
860 | /* And add the comment */ | |
861 | strbuf_addstr(&buffer, msg); | |
862 | ||
863 | /* And check the encoding */ | |
864 | if (encoding_is_utf8 && !is_utf8(buffer.buf)) | |
865 | fprintf(stderr, commit_utf8_warn); | |
866 | ||
867 | result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret); | |
868 | strbuf_release(&buffer); | |
869 | return result; | |
870 | } |