diff: make sure work tree side is shown as 0{40} when different
[debian-git.git] / commit.c
blob22ce7768639465f896e52b08a73cf5605940700b
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "pkt-line.h"
5 #include "utf8.h"
6 #include "diff.h"
7 #include "revision.h"
9 int save_commit_buffer = 1;
11 const char *commit_type = "commit";
13 static struct commit *check_commit(struct object *obj,
14 const unsigned char *sha1,
15 int quiet)
17 if (obj->type != OBJ_COMMIT) {
18 if (!quiet)
19 error("Object %s is a %s, not a commit",
20 sha1_to_hex(sha1), typename(obj->type));
21 return NULL;
23 return (struct commit *) obj;
26 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
27 int quiet)
29 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
31 if (!obj)
32 return NULL;
33 return check_commit(obj, sha1, quiet);
36 struct commit *lookup_commit_reference(const unsigned char *sha1)
38 return lookup_commit_reference_gently(sha1, 0);
41 struct commit *lookup_commit(const unsigned char *sha1)
43 struct object *obj = lookup_object(sha1);
44 if (!obj)
45 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
46 if (!obj->type)
47 obj->type = OBJ_COMMIT;
48 return check_commit(obj, sha1, 0);
51 static unsigned long parse_commit_date(const char *buf, const char *tail)
53 unsigned long date;
54 const char *dateptr;
56 if (buf + 6 >= tail)
57 return 0;
58 if (memcmp(buf, "author", 6))
59 return 0;
60 while (buf < tail && *buf++ != '\n')
61 /* nada */;
62 if (buf + 9 >= tail)
63 return 0;
64 if (memcmp(buf, "committer", 9))
65 return 0;
66 while (buf < tail && *buf++ != '>')
67 /* nada */;
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);
77 if (date == ULONG_MAX)
78 date = 0;
79 return date;
82 static struct commit_graft **commit_graft;
83 static int commit_graft_alloc, commit_graft_nr;
85 static int commit_graft_pos(const unsigned char *sha1)
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];
93 int cmp = hashcmp(sha1, graft->sha1);
94 if (!cmp)
95 return mi;
96 if (cmp < 0)
97 hi = mi;
98 else
99 lo = mi + 1;
101 return -lo - 1;
104 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
106 int pos = commit_graft_pos(graft->sha1);
108 if (0 <= pos) {
109 if (ignore_dups)
110 free(graft);
111 else {
112 free(commit_graft[pos]);
113 commit_graft[pos] = graft;
115 return 1;
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);
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;
133 struct commit_graft *read_graft_line(char *buf, int len)
135 /* The format is just "Commit Parent1 Parent2 ...\n" */
136 int i;
137 struct commit_graft *graft = NULL;
139 if (buf[len-1] == '\n')
140 buf[--len] = 0;
141 if (buf[0] == '#' || buf[0] == '\0')
142 return NULL;
143 if ((len + 1) % 41) {
144 bad_graft_data:
145 error("bad graft data: %s", buf);
146 free(graft);
147 return NULL;
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;
160 return graft;
163 int read_graft_file(const char *graft_file)
165 FILE *fp = fopen(graft_file, "r");
166 char buf[1024];
167 if (!fp)
168 return -1;
169 while (fgets(buf, sizeof(buf), fp)) {
170 /* The format is just "Commit Parent1 Parent2 ...\n" */
171 int len = strlen(buf);
172 struct commit_graft *graft = read_graft_line(buf, len);
173 if (!graft)
174 continue;
175 if (register_commit_graft(graft, 1))
176 error("duplicate graft data: %s", buf);
178 fclose(fp);
179 return 0;
182 static void prepare_commit_graft(void)
184 static int commit_graft_prepared;
185 char *graft_file;
187 if (commit_graft_prepared)
188 return;
189 graft_file = get_graft_file();
190 read_graft_file(graft_file);
191 /* make sure shallows are read */
192 is_repository_shallow();
193 commit_graft_prepared = 1;
196 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
198 int pos;
199 prepare_commit_graft();
200 pos = commit_graft_pos(sha1);
201 if (pos < 0)
202 return NULL;
203 return commit_graft[pos];
206 int write_shallow_commits(int fd, int use_pack_protocol)
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 {
217 if (write_in_full(fd, hex, 40) != 40)
218 break;
219 if (write_in_full(fd, "\n", 1) != 1)
220 break;
223 return count;
226 int unregister_shallow(const unsigned char *sha1)
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;
239 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
241 char *tail = buffer;
242 char *bufptr = buffer;
243 unsigned char parent[20];
244 struct commit_list **pptr;
245 struct commit_graft *graft;
246 unsigned n_refs = 0;
248 if (item->object.parsed)
249 return 0;
250 item->object.parsed = 1;
251 tail += size;
252 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
253 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
254 if (get_sha1_hex(bufptr + 5, parent) < 0)
255 return error("bad tree pointer in commit %s",
256 sha1_to_hex(item->object.sha1));
257 item->tree = lookup_tree(parent);
258 if (item->tree)
259 n_refs++;
260 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
261 pptr = &item->parents;
263 graft = lookup_commit_graft(item->object.sha1);
264 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
265 struct commit *new_parent;
267 if (tail <= bufptr + 48 ||
268 get_sha1_hex(bufptr + 7, parent) ||
269 bufptr[47] != '\n')
270 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
271 bufptr += 48;
272 if (graft)
273 continue;
274 new_parent = lookup_commit(parent);
275 if (new_parent) {
276 pptr = &commit_list_insert(new_parent, pptr)->next;
277 n_refs++;
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;
288 n_refs++;
291 item->date = parse_commit_date(bufptr, tail);
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);
304 return 0;
307 int parse_commit(struct commit *item)
309 enum object_type type;
310 void *buffer;
311 unsigned long size;
312 int ret;
314 if (!item)
315 return -1;
316 if (item->object.parsed)
317 return 0;
318 buffer = read_sha1_file(item->object.sha1, &type, &size);
319 if (!buffer)
320 return error("Could not read %s",
321 sha1_to_hex(item->object.sha1));
322 if (type != OBJ_COMMIT) {
323 free(buffer);
324 return error("Object %s not a commit",
325 sha1_to_hex(item->object.sha1));
327 ret = parse_commit_buffer(item, buffer, size);
328 if (save_commit_buffer && !ret) {
329 item->buffer = buffer;
330 return 0;
332 free(buffer);
333 return ret;
336 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
338 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
339 new_list->item = item;
340 new_list->next = *list_p;
341 *list_p = new_list;
342 return new_list;
345 void free_commit_list(struct commit_list *list)
347 while (list) {
348 struct commit_list *temp = list;
349 list = temp->next;
350 free(temp);
354 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
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;
362 pp = &p->next;
364 return commit_list_insert(item, pp);
368 void sort_by_date(struct commit_list **list)
370 struct commit_list *ret = NULL;
371 while (*list) {
372 insert_by_date((*list)->item, &ret);
373 *list = (*list)->next;
375 *list = ret;
378 struct commit *pop_most_recent_commit(struct commit_list **list,
379 unsigned int mark)
381 struct commit *ret = (*list)->item;
382 struct commit_list *parents = ret->parents;
383 struct commit_list *old = *list;
385 *list = (*list)->next;
386 free(old);
388 while (parents) {
389 struct commit *commit = parents->item;
390 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
391 commit->object.flags |= mark;
392 insert_by_date(commit, list);
394 parents = parents->next;
396 return ret;
399 void clear_commit_marks(struct commit *commit, unsigned int mark)
401 while (commit) {
402 struct commit_list *parents;
404 if (!(mark & commit->object.flags))
405 return;
407 commit->object.flags &= ~mark;
409 parents = commit->parents;
410 if (!parents)
411 return;
413 while ((parents = parents->next))
414 clear_commit_marks(parents->item, mark);
416 commit = commit->parents->item;
420 struct commit *pop_commit(struct commit_list **stack)
422 struct commit_list *top = *stack;
423 struct commit *item = top ? top->item : NULL;
425 if (top) {
426 *stack = top->next;
427 free(top);
429 return item;
433 * Performs an in-place topological sort on the list supplied.
435 void sort_in_topological_order(struct commit_list ** list, int lifo)
437 struct commit_list *next, *orig = *list;
438 struct commit_list *work, **insert;
439 struct commit_list **pptr;
441 if (!orig)
442 return;
443 *list = NULL;
445 /* Mark them and clear the indegree */
446 for (next = orig; next; next = next->next) {
447 struct commit *commit = next->item;
448 commit->object.flags |= TOPOSORT;
449 commit->indegree = 0;
452 /* update the indegree */
453 for (next = orig; next; next = next->next) {
454 struct commit_list * parents = next->item->parents;
455 while (parents) {
456 struct commit *parent = parents->item;
458 if (parent->object.flags & TOPOSORT)
459 parent->indegree++;
460 parents = parents->next;
465 * find the tips
467 * tips are nodes not reachable from any other node in the list
469 * the tips serve as a starting set for the work queue.
471 work = NULL;
472 insert = &work;
473 for (next = orig; next; next = next->next) {
474 struct commit *commit = next->item;
476 if (!commit->indegree)
477 insert = &commit_list_insert(commit, insert)->next;
480 /* process the list in topological order */
481 if (!lifo)
482 sort_by_date(&work);
484 pptr = list;
485 *list = NULL;
486 while (work) {
487 struct commit *commit;
488 struct commit_list *parents, *work_item;
490 work_item = work;
491 work = work_item->next;
492 work_item->next = NULL;
494 commit = work_item->item;
495 for (parents = commit->parents; parents ; parents = parents->next) {
496 struct commit *parent=parents->item;
498 if (!(parent->object.flags & TOPOSORT))
499 continue;
502 * parents are only enqueued for emission
503 * when all their children have been emitted thereby
504 * guaranteeing topological order.
506 if (!--parent->indegree) {
507 if (!lifo)
508 insert_by_date(parent, &work);
509 else
510 commit_list_insert(parent, &work);
514 * work_item is a commit all of whose children
515 * have already been emitted. we can emit it now.
517 commit->object.flags &= ~TOPOSORT;
518 *pptr = work_item;
519 pptr = &work_item->next;
523 /* merge-base stuff */
525 /* bits #0..15 in revision.h */
526 #define PARENT1 (1u<<16)
527 #define PARENT2 (1u<<17)
528 #define STALE (1u<<18)
529 #define RESULT (1u<<19)
531 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
533 static struct commit *interesting(struct commit_list *list)
535 while (list) {
536 struct commit *commit = list->item;
537 list = list->next;
538 if (commit->object.flags & STALE)
539 continue;
540 return commit;
542 return NULL;
545 static struct commit_list *merge_bases(struct commit *one, struct commit *two)
547 struct commit_list *list = NULL;
548 struct commit_list *result = NULL;
550 if (one == two)
551 /* We do not mark this even with RESULT so we do not
552 * have to clean it up.
554 return commit_list_insert(one, &result);
556 if (parse_commit(one))
557 return NULL;
558 if (parse_commit(two))
559 return NULL;
561 one->object.flags |= PARENT1;
562 two->object.flags |= PARENT2;
563 insert_by_date(one, &list);
564 insert_by_date(two, &list);
566 while (interesting(list)) {
567 struct commit *commit;
568 struct commit_list *parents;
569 struct commit_list *n;
570 int flags;
572 commit = list->item;
573 n = list->next;
574 free(list);
575 list = n;
577 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
578 if (flags == (PARENT1 | PARENT2)) {
579 if (!(commit->object.flags & RESULT)) {
580 commit->object.flags |= RESULT;
581 insert_by_date(commit, &result);
583 /* Mark parents of a found merge stale */
584 flags |= STALE;
586 parents = commit->parents;
587 while (parents) {
588 struct commit *p = parents->item;
589 parents = parents->next;
590 if ((p->object.flags & flags) == flags)
591 continue;
592 if (parse_commit(p))
593 return NULL;
594 p->object.flags |= flags;
595 insert_by_date(p, &list);
599 /* Clean up the result to remove stale ones */
600 free_commit_list(list);
601 list = result; result = NULL;
602 while (list) {
603 struct commit_list *n = list->next;
604 if (!(list->item->object.flags & STALE))
605 insert_by_date(list->item, &result);
606 free(list);
607 list = n;
609 return result;
612 struct commit_list *get_merge_bases(struct commit *one,
613 struct commit *two, int cleanup)
615 struct commit_list *list;
616 struct commit **rslt;
617 struct commit_list *result;
618 int cnt, i, j;
620 result = merge_bases(one, two);
621 if (one == two)
622 return result;
623 if (!result || !result->next) {
624 if (cleanup) {
625 clear_commit_marks(one, all_flags);
626 clear_commit_marks(two, all_flags);
628 return result;
631 /* There are more than one */
632 cnt = 0;
633 list = result;
634 while (list) {
635 list = list->next;
636 cnt++;
638 rslt = xcalloc(cnt, sizeof(*rslt));
639 for (list = result, i = 0; list; list = list->next)
640 rslt[i++] = list->item;
641 free_commit_list(result);
643 clear_commit_marks(one, all_flags);
644 clear_commit_marks(two, all_flags);
645 for (i = 0; i < cnt - 1; i++) {
646 for (j = i+1; j < cnt; j++) {
647 if (!rslt[i] || !rslt[j])
648 continue;
649 result = merge_bases(rslt[i], rslt[j]);
650 clear_commit_marks(rslt[i], all_flags);
651 clear_commit_marks(rslt[j], all_flags);
652 for (list = result; list; list = list->next) {
653 if (rslt[i] == list->item)
654 rslt[i] = NULL;
655 if (rslt[j] == list->item)
656 rslt[j] = NULL;
661 /* Surviving ones in rslt[] are the independent results */
662 result = NULL;
663 for (i = 0; i < cnt; i++) {
664 if (rslt[i])
665 insert_by_date(rslt[i], &result);
667 free(rslt);
668 return result;
671 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
673 struct commit_list *bases, *b;
674 int ret = 0;
676 if (num == 1)
677 bases = get_merge_bases(commit, *reference, 1);
678 else
679 die("not yet");
680 for (b = bases; b; b = b->next) {
681 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
682 ret = 1;
683 break;
687 free_commit_list(bases);
688 return ret;