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