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