]> git.ipfire.org Git - thirdparty/git.git/blame - commit.c
parse_commit: don't fail, if object is NULL
[thirdparty/git.git] / commit.c
CommitLineData
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"
175785e5 8
60ab26de
LT
9int save_commit_buffer = 1;
10
175785e5
DB
11const char *commit_type = "commit";
12
f76412ed
JH
13static struct commit *check_commit(struct object *obj,
14 const unsigned char *sha1,
15 int quiet)
961784ee 16{
1974632c 17 if (obj->type != OBJ_COMMIT) {
f76412ed
JH
18 if (!quiet)
19 error("Object %s is a %s, not a commit",
885a86ab 20 sha1_to_hex(sha1), typename(obj->type));
961784ee
LT
21 return NULL;
22 }
23 return (struct commit *) obj;
24}
25
f76412ed
JH
26struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
27 int quiet)
961784ee 28{
9534f40b 29 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
961784ee
LT
30
31 if (!obj)
32 return NULL;
f76412ed
JH
33 return check_commit(obj, sha1, quiet);
34}
35
36struct commit *lookup_commit_reference(const unsigned char *sha1)
37{
38 return lookup_commit_reference_gently(sha1, 0);
961784ee
LT
39}
40
5d6ccf5c 41struct commit *lookup_commit(const unsigned char *sha1)
175785e5
DB
42{
43 struct object *obj = lookup_object(sha1);
100c5f3b
LT
44 if (!obj)
45 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
d1af002d 46 if (!obj->type)
1974632c 47 obj->type = OBJ_COMMIT;
f76412ed 48 return check_commit(obj, sha1, 0);
175785e5
DB
49}
50
0a617799 51static unsigned long parse_commit_date(const char *buf, const char *tail)
175785e5
DB
52{
53 unsigned long date;
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 */
76 date = strtoul(dateptr, NULL, 10);
175785e5
DB
77 if (date == ULONG_MAX)
78 date = 0;
79 return date;
80}
81
5040f17e 82static struct commit_graft **commit_graft;
5da5c8f4
JH
83static int commit_graft_alloc, commit_graft_nr;
84
85static int commit_graft_pos(const unsigned char *sha1)
86{
87 int lo, hi;
88 lo = 0;
89 hi = commit_graft_nr;
90 while (lo < hi) {
91 int mi = (lo + hi) / 2;
92 struct commit_graft *graft = commit_graft[mi];
a89fccd2 93 int cmp = hashcmp(sha1, graft->sha1);
5da5c8f4
JH
94 if (!cmp)
95 return mi;
96 if (cmp < 0)
97 hi = mi;
98 else
99 lo = mi + 1;
100 }
101 return -lo - 1;
102}
103
5040f17e
JH
104int register_commit_graft(struct commit_graft *graft, int ignore_dups)
105{
106 int pos = commit_graft_pos(graft->sha1);
a6080a0a 107
5040f17e
JH
108 if (0 <= pos) {
109 if (ignore_dups)
110 free(graft);
111 else {
112 free(commit_graft[pos]);
113 commit_graft[pos] = graft;
114 }
115 return 1;
116 }
117 pos = -pos - 1;
118 if (commit_graft_alloc <= ++commit_graft_nr) {
119 commit_graft_alloc = alloc_nr(commit_graft_alloc);
120 commit_graft = xrealloc(commit_graft,
121 sizeof(*commit_graft) *
122 commit_graft_alloc);
123 }
124 if (pos < commit_graft_nr)
125 memmove(commit_graft + pos + 1,
126 commit_graft + pos,
127 (commit_graft_nr - pos - 1) *
128 sizeof(*commit_graft));
129 commit_graft[pos] = graft;
130 return 0;
131}
132
133struct commit_graft *read_graft_line(char *buf, int len)
134{
135 /* The format is just "Commit Parent1 Parent2 ...\n" */
136 int i;
137 struct commit_graft *graft = NULL;
138
139 if (buf[len-1] == '\n')
140 buf[--len] = 0;
360204c3 141 if (buf[0] == '#' || buf[0] == '\0')
5bc4ce58 142 return NULL;
5040f17e
JH
143 if ((len + 1) % 41) {
144 bad_graft_data:
145 error("bad graft data: %s", buf);
146 free(graft);
147 return NULL;
148 }
149 i = (len + 1) / 41 - 1;
150 graft = xmalloc(sizeof(*graft) + 20 * i);
151 graft->nr_parent = i;
152 if (get_sha1_hex(buf, graft->sha1))
153 goto bad_graft_data;
154 for (i = 40; i < len; i += 41) {
155 if (buf[i] != ' ')
156 goto bad_graft_data;
157 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
158 goto bad_graft_data;
159 }
160 return graft;
161}
162
163int read_graft_file(const char *graft_file)
5da5c8f4 164{
5da5c8f4
JH
165 FILE *fp = fopen(graft_file, "r");
166 char buf[1024];
5040f17e
JH
167 if (!fp)
168 return -1;
5da5c8f4
JH
169 while (fgets(buf, sizeof(buf), fp)) {
170 /* The format is just "Commit Parent1 Parent2 ...\n" */
171 int len = strlen(buf);
5040f17e 172 struct commit_graft *graft = read_graft_line(buf, len);
5bc4ce58
JH
173 if (!graft)
174 continue;
5040f17e 175 if (register_commit_graft(graft, 1))
5da5c8f4 176 error("duplicate graft data: %s", buf);
5da5c8f4
JH
177 }
178 fclose(fp);
5040f17e
JH
179 return 0;
180}
181
182static void prepare_commit_graft(void)
183{
184 static int commit_graft_prepared;
185 char *graft_file;
186
187 if (commit_graft_prepared)
188 return;
189 graft_file = get_graft_file();
190 read_graft_file(graft_file);
ed09aef0
JS
191 /* make sure shallows are read */
192 is_repository_shallow();
5040f17e 193 commit_graft_prepared = 1;
5da5c8f4
JH
194}
195
196static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
197{
198 int pos;
5040f17e 199 prepare_commit_graft();
5da5c8f4
JH
200 pos = commit_graft_pos(sha1);
201 if (pos < 0)
202 return NULL;
203 return commit_graft[pos];
204}
205
ed09aef0
JS
206int write_shallow_commits(int fd, int use_pack_protocol)
207{
208 int i, count = 0;
209 for (i = 0; i < commit_graft_nr; i++)
210 if (commit_graft[i]->nr_parent < 0) {
211 const char *hex =
212 sha1_to_hex(commit_graft[i]->sha1);
213 count++;
214 if (use_pack_protocol)
215 packet_write(fd, "shallow %s", hex);
216 else {
93822c22
AW
217 if (write_in_full(fd, hex, 40) != 40)
218 break;
219 if (write_in_full(fd, "\n", 1) != 1)
220 break;
ed09aef0
JS
221 }
222 }
223 return count;
224}
225
f53514bc
JS
226int unregister_shallow(const unsigned char *sha1)
227{
228 int pos = commit_graft_pos(sha1);
229 if (pos < 0)
230 return -1;
231 if (pos + 1 < commit_graft_nr)
232 memcpy(commit_graft + pos, commit_graft + pos + 1,
233 sizeof(struct commit_graft *)
234 * (commit_graft_nr - pos - 1));
235 commit_graft_nr--;
236 return 0;
237}
238
bd2c39f5 239int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
175785e5 240{
3b44f15a 241 char *tail = buffer;
f7cc77d7 242 char *bufptr = buffer;
175785e5 243 unsigned char parent[20];
6c88be16 244 struct commit_list **pptr;
5da5c8f4 245 struct commit_graft *graft;
27dedf0c 246 unsigned n_refs = 0;
bd2c39f5 247
175785e5
DB
248 if (item->object.parsed)
249 return 0;
250 item->object.parsed = 1;
3b44f15a 251 tail += size;
0a617799 252 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
f7cc77d7 253 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
0a617799 254 if (get_sha1_hex(bufptr + 5, parent) < 0)
bd2afde8
JH
255 return error("bad tree pointer in commit %s",
256 sha1_to_hex(item->object.sha1));
175785e5 257 item->tree = lookup_tree(parent);
235ac407 258 if (item->tree)
27dedf0c 259 n_refs++;
175785e5 260 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
6c88be16 261 pptr = &item->parents;
5da5c8f4
JH
262
263 graft = lookup_commit_graft(item->object.sha1);
3b44f15a 264 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
f7cc77d7
LT
265 struct commit *new_parent;
266
3b44f15a
JH
267 if (tail <= bufptr + 48 ||
268 get_sha1_hex(bufptr + 7, parent) ||
269 bufptr[47] != '\n')
f7cc77d7 270 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
5da5c8f4
JH
271 bufptr += 48;
272 if (graft)
273 continue;
f7cc77d7 274 new_parent = lookup_commit(parent);
235ac407 275 if (new_parent) {
6c88be16 276 pptr = &commit_list_insert(new_parent, pptr)->next;
27dedf0c 277 n_refs++;
235ac407 278 }
5da5c8f4
JH
279 }
280 if (graft) {
281 int i;
282 struct commit *new_parent;
283 for (i = 0; i < graft->nr_parent; i++) {
284 new_parent = lookup_commit(graft->parent[i]);
285 if (!new_parent)
286 continue;
287 pptr = &commit_list_insert(new_parent, pptr)->next;
27dedf0c 288 n_refs++;
5da5c8f4 289 }
175785e5 290 }
0a617799 291 item->date = parse_commit_date(bufptr, tail);
27dedf0c
JH
292
293 if (track_object_refs) {
294 unsigned i = 0;
295 struct commit_list *p;
296 struct object_refs *refs = alloc_object_refs(n_refs);
297 if (item->tree)
298 refs->ref[i++] = &item->tree->object;
299 for (p = item->parents; p; p = p->next)
300 refs->ref[i++] = &p->item->object;
301 set_object_refs(&item->object, refs);
302 }
303
175785e5
DB
304 return 0;
305}
306
bd2c39f5
NP
307int parse_commit(struct commit *item)
308{
21666f1a 309 enum object_type type;
bd2c39f5
NP
310 void *buffer;
311 unsigned long size;
312 int ret;
313
9786f68b
MK
314 if (!item)
315 return -1;
bd2c39f5
NP
316 if (item->object.parsed)
317 return 0;
21666f1a 318 buffer = read_sha1_file(item->object.sha1, &type, &size);
bd2c39f5
NP
319 if (!buffer)
320 return error("Could not read %s",
321 sha1_to_hex(item->object.sha1));
21666f1a 322 if (type != OBJ_COMMIT) {
bd2c39f5
NP
323 free(buffer);
324 return error("Object %s not a commit",
325 sha1_to_hex(item->object.sha1));
326 }
327 ret = parse_commit_buffer(item, buffer, size);
60ab26de 328 if (save_commit_buffer && !ret) {
3ff1fbbb
LT
329 item->buffer = buffer;
330 return 0;
331 }
bd2c39f5
NP
332 free(buffer);
333 return ret;
334}
335
ac5155ef 336struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
dd97f850 337{
812666c8 338 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
dd97f850
DB
339 new_list->item = item;
340 new_list->next = *list_p;
341 *list_p = new_list;
ac5155ef 342 return new_list;
dd97f850
DB
343}
344
175785e5
DB
345void free_commit_list(struct commit_list *list)
346{
347 while (list) {
348 struct commit_list *temp = list;
349 list = temp->next;
350 free(temp);
351 }
352}
dd97f850 353
f755494c 354struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
dd97f850
DB
355{
356 struct commit_list **pp = list;
357 struct commit_list *p;
358 while ((p = *pp) != NULL) {
359 if (p->item->date < item->date) {
360 break;
361 }
362 pp = &p->next;
363 }
f755494c 364 return commit_list_insert(item, pp);
dd97f850
DB
365}
366
a6080a0a 367
dd97f850
DB
368void sort_by_date(struct commit_list **list)
369{
370 struct commit_list *ret = NULL;
371 while (*list) {
f755494c 372 insert_by_date((*list)->item, &ret);
dd97f850
DB
373 *list = (*list)->next;
374 }
375 *list = ret;
376}
377
58e28af6
DB
378struct commit *pop_most_recent_commit(struct commit_list **list,
379 unsigned int mark)
dd97f850
DB
380{
381 struct commit *ret = (*list)->item;
382 struct commit_list *parents = ret->parents;
383 struct commit_list *old = *list;
384
385 *list = (*list)->next;
386 free(old);
387
388 while (parents) {
4056c091 389 struct commit *commit = parents->item;
58e28af6
DB
390 parse_commit(commit);
391 if (!(commit->object.flags & mark)) {
392 commit->object.flags |= mark;
f755494c 393 insert_by_date(commit, list);
4056c091 394 }
dd97f850
DB
395 parents = parents->next;
396 }
397 return ret;
398}
e3bc7a3b 399
f8f9c73c
JH
400void clear_commit_marks(struct commit *commit, unsigned int mark)
401{
60fcc2e6
JS
402 while (commit) {
403 struct commit_list *parents;
f8f9c73c 404
60fcc2e6
JS
405 if (!(mark & commit->object.flags))
406 return;
58ecf5c1 407
60fcc2e6
JS
408 commit->object.flags &= ~mark;
409
410 parents = commit->parents;
411 if (!parents)
412 return;
413
414 while ((parents = parents->next))
415 clear_commit_marks(parents->item, mark);
416
417 commit = commit->parents->item;
f8f9c73c
JH
418 }
419}
420
a3437b8c
JS
421struct commit *pop_commit(struct commit_list **stack)
422{
423 struct commit_list *top = *stack;
424 struct commit *item = top ? top->item : NULL;
425
426 if (top) {
427 *stack = top->next;
428 free(top);
429 }
430 return item;
431}
432
ab580ace
JS
433/*
434 * Performs an in-place topological sort on the list supplied.
435 */
4c8725f1 436void sort_in_topological_order(struct commit_list ** list, int lifo)
6b6dcfc2 437{
23c17d4a
LT
438 struct commit_list *next, *orig = *list;
439 struct commit_list *work, **insert;
440 struct commit_list **pptr;
a6080a0a 441
23c17d4a 442 if (!orig)
7d6fb370 443 return;
23c17d4a
LT
444 *list = NULL;
445
446 /* Mark them and clear the indegree */
447 for (next = orig; next; next = next->next) {
448 struct commit *commit = next->item;
449 commit->object.flags |= TOPOSORT;
450 commit->indegree = 0;
ab580ace 451 }
23c17d4a 452
ab580ace 453 /* update the indegree */
23c17d4a 454 for (next = orig; next; next = next->next) {
ab580ace
JS
455 struct commit_list * parents = next->item->parents;
456 while (parents) {
23c17d4a 457 struct commit *parent = parents->item;
6b6dcfc2 458
23c17d4a
LT
459 if (parent->object.flags & TOPOSORT)
460 parent->indegree++;
461 parents = parents->next;
ab580ace 462 }
ab580ace 463 }
23c17d4a 464
a6080a0a 465 /*
674d1727
PH
466 * find the tips
467 *
468 * tips are nodes not reachable from any other node in the list
469 *
470 * the tips serve as a starting set for the work queue.
471 */
23c17d4a 472 work = NULL;
2ed02887 473 insert = &work;
23c17d4a
LT
474 for (next = orig; next; next = next->next) {
475 struct commit *commit = next->item;
ab580ace 476
23c17d4a
LT
477 if (!commit->indegree)
478 insert = &commit_list_insert(commit, insert)->next;
ab580ace 479 }
4c8725f1 480
ab580ace 481 /* process the list in topological order */
4c8725f1
JH
482 if (!lifo)
483 sort_by_date(&work);
23c17d4a
LT
484
485 pptr = list;
486 *list = NULL;
ab580ace 487 while (work) {
23c17d4a
LT
488 struct commit *commit;
489 struct commit_list *parents, *work_item;
ab580ace 490
23c17d4a
LT
491 work_item = work;
492 work = work_item->next;
493 work_item->next = NULL;
494
495 commit = work_item->item;
496 for (parents = commit->parents; parents ; parents = parents->next) {
497 struct commit *parent=parents->item;
498
499 if (!(parent->object.flags & TOPOSORT))
500 continue;
501
502 /*
503 * parents are only enqueued for emission
504 * when all their children have been emitted thereby
505 * guaranteeing topological order.
506 */
507 if (!--parent->indegree) {
508 if (!lifo)
509 insert_by_date(parent, &work);
510 else
511 commit_list_insert(parent, &work);
ab580ace 512 }
ab580ace
JS
513 }
514 /*
674d1727
PH
515 * work_item is a commit all of whose children
516 * have already been emitted. we can emit it now.
517 */
23c17d4a
LT
518 commit->object.flags &= ~TOPOSORT;
519 *pptr = work_item;
520 pptr = &work_item->next;
ab580ace 521 }
ab580ace 522}
7c6f8aaf 523
1295228d 524/* merge-base stuff */
7c6f8aaf 525
577ed5c2
JH
526/* bits #0..15 in revision.h */
527#define PARENT1 (1u<<16)
528#define PARENT2 (1u<<17)
529#define STALE (1u<<18)
530#define RESULT (1u<<19)
7c6f8aaf 531
1295228d
JH
532static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
533
7c6f8aaf
JS
534static struct commit *interesting(struct commit_list *list)
535{
536 while (list) {
537 struct commit *commit = list->item;
538 list = list->next;
542ccefe 539 if (commit->object.flags & STALE)
7c6f8aaf
JS
540 continue;
541 return commit;
542 }
543 return NULL;
544}
545
f3249438 546static struct commit_list *merge_bases(struct commit *one, struct commit *two)
7c6f8aaf
JS
547{
548 struct commit_list *list = NULL;
549 struct commit_list *result = NULL;
7c6f8aaf 550
f3249438
JH
551 if (one == two)
552 /* We do not mark this even with RESULT so we do not
553 * have to clean it up.
554 */
555 return commit_list_insert(one, &result);
7c6f8aaf 556
172947e6
MK
557 if (parse_commit(one))
558 return NULL;
559 if (parse_commit(two))
560 return NULL;
7c6f8aaf 561
f3249438
JH
562 one->object.flags |= PARENT1;
563 two->object.flags |= PARENT2;
564 insert_by_date(one, &list);
565 insert_by_date(two, &list);
7c6f8aaf
JS
566
567 while (interesting(list)) {
f3249438 568 struct commit *commit;
7c6f8aaf 569 struct commit_list *parents;
f3249438
JH
570 struct commit_list *n;
571 int flags;
7c6f8aaf 572
f3249438
JH
573 commit = list->item;
574 n = list->next;
575 free(list);
576 list = n;
7c6f8aaf 577
f3249438
JH
578 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
579 if (flags == (PARENT1 | PARENT2)) {
580 if (!(commit->object.flags & RESULT)) {
581 commit->object.flags |= RESULT;
582 insert_by_date(commit, &result);
583 }
542ccefe
JH
584 /* Mark parents of a found merge stale */
585 flags |= STALE;
7c6f8aaf
JS
586 }
587 parents = commit->parents;
588 while (parents) {
589 struct commit *p = parents->item;
590 parents = parents->next;
591 if ((p->object.flags & flags) == flags)
592 continue;
172947e6
MK
593 if (parse_commit(p))
594 return NULL;
7c6f8aaf
JS
595 p->object.flags |= flags;
596 insert_by_date(p, &list);
597 }
598 }
599
f3249438 600 /* Clean up the result to remove stale ones */
1295228d 601 free_commit_list(list);
f3249438
JH
602 list = result; result = NULL;
603 while (list) {
604 struct commit_list *n = list->next;
605 if (!(list->item->object.flags & STALE))
606 insert_by_date(list->item, &result);
607 free(list);
608 list = n;
609 }
610 return result;
611}
7c6f8aaf 612
f3249438 613struct commit_list *get_merge_bases(struct commit *one,
674d1727 614 struct commit *two, int cleanup)
f3249438 615{
f3249438
JH
616 struct commit_list *list;
617 struct commit **rslt;
618 struct commit_list *result;
619 int cnt, i, j;
620
621 result = merge_bases(one, two);
622 if (one == two)
623 return result;
624 if (!result || !result->next) {
625 if (cleanup) {
626 clear_commit_marks(one, all_flags);
627 clear_commit_marks(two, all_flags);
7c6f8aaf 628 }
f3249438 629 return result;
7c6f8aaf
JS
630 }
631
f3249438
JH
632 /* There are more than one */
633 cnt = 0;
634 list = result;
635 while (list) {
636 list = list->next;
637 cnt++;
638 }
639 rslt = xcalloc(cnt, sizeof(*rslt));
640 for (list = result, i = 0; list; list = list->next)
641 rslt[i++] = list->item;
642 free_commit_list(result);
643
644 clear_commit_marks(one, all_flags);
645 clear_commit_marks(two, all_flags);
646 for (i = 0; i < cnt - 1; i++) {
647 for (j = i+1; j < cnt; j++) {
648 if (!rslt[i] || !rslt[j])
649 continue;
650 result = merge_bases(rslt[i], rslt[j]);
651 clear_commit_marks(rslt[i], all_flags);
652 clear_commit_marks(rslt[j], all_flags);
653 for (list = result; list; list = list->next) {
654 if (rslt[i] == list->item)
655 rslt[i] = NULL;
656 if (rslt[j] == list->item)
657 rslt[j] = NULL;
658 }
659 }
58ecf5c1 660 }
7c6f8aaf 661
f3249438
JH
662 /* Surviving ones in rslt[] are the independent results */
663 result = NULL;
664 for (i = 0; i < cnt; i++) {
665 if (rslt[i])
666 insert_by_date(rslt[i], &result);
667 }
668 free(rslt);
7c6f8aaf
JS
669 return result;
670}
2ecd2bbc 671
03840fc3 672int in_merge_bases(struct commit *commit, struct commit **reference, int num)
2ecd2bbc
JH
673{
674 struct commit_list *bases, *b;
675 int ret = 0;
676
03840fc3
JH
677 if (num == 1)
678 bases = get_merge_bases(commit, *reference, 1);
679 else
680 die("not yet");
2ecd2bbc 681 for (b = bases; b; b = b->next) {
03840fc3 682 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
2ecd2bbc
JH
683 ret = 1;
684 break;
685 }
686 }
687
688 free_commit_list(bases);
689 return ret;
690}