diff: remove lazy config loading
[git/dscho.git] / commit.c
blobf074811edc8c5ba41351f50c48a6cda7614e8f8e
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)
53 unsigned long date;
55 if (memcmp(buf, "author", 6))
56 return 0;
57 while (*buf++ != '\n')
58 /* nada */;
59 if (memcmp(buf, "committer", 9))
60 return 0;
61 while (*buf++ != '>')
62 /* nada */;
63 date = strtoul(buf, NULL, 10);
64 if (date == ULONG_MAX)
65 date = 0;
66 return date;
69 static struct commit_graft **commit_graft;
70 static int commit_graft_alloc, commit_graft_nr;
72 static int commit_graft_pos(const unsigned char *sha1)
74 int lo, hi;
75 lo = 0;
76 hi = commit_graft_nr;
77 while (lo < hi) {
78 int mi = (lo + hi) / 2;
79 struct commit_graft *graft = commit_graft[mi];
80 int cmp = hashcmp(sha1, graft->sha1);
81 if (!cmp)
82 return mi;
83 if (cmp < 0)
84 hi = mi;
85 else
86 lo = mi + 1;
88 return -lo - 1;
91 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
93 int pos = commit_graft_pos(graft->sha1);
95 if (0 <= pos) {
96 if (ignore_dups)
97 free(graft);
98 else {
99 free(commit_graft[pos]);
100 commit_graft[pos] = graft;
102 return 1;
104 pos = -pos - 1;
105 if (commit_graft_alloc <= ++commit_graft_nr) {
106 commit_graft_alloc = alloc_nr(commit_graft_alloc);
107 commit_graft = xrealloc(commit_graft,
108 sizeof(*commit_graft) *
109 commit_graft_alloc);
111 if (pos < commit_graft_nr)
112 memmove(commit_graft + pos + 1,
113 commit_graft + pos,
114 (commit_graft_nr - pos - 1) *
115 sizeof(*commit_graft));
116 commit_graft[pos] = graft;
117 return 0;
120 struct commit_graft *read_graft_line(char *buf, int len)
122 /* The format is just "Commit Parent1 Parent2 ...\n" */
123 int i;
124 struct commit_graft *graft = NULL;
126 if (buf[len-1] == '\n')
127 buf[--len] = 0;
128 if (buf[0] == '#' || buf[0] == '\0')
129 return NULL;
130 if ((len + 1) % 41) {
131 bad_graft_data:
132 error("bad graft data: %s", buf);
133 free(graft);
134 return NULL;
136 i = (len + 1) / 41 - 1;
137 graft = xmalloc(sizeof(*graft) + 20 * i);
138 graft->nr_parent = i;
139 if (get_sha1_hex(buf, graft->sha1))
140 goto bad_graft_data;
141 for (i = 40; i < len; i += 41) {
142 if (buf[i] != ' ')
143 goto bad_graft_data;
144 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
145 goto bad_graft_data;
147 return graft;
150 int read_graft_file(const char *graft_file)
152 FILE *fp = fopen(graft_file, "r");
153 char buf[1024];
154 if (!fp)
155 return -1;
156 while (fgets(buf, sizeof(buf), fp)) {
157 /* The format is just "Commit Parent1 Parent2 ...\n" */
158 int len = strlen(buf);
159 struct commit_graft *graft = read_graft_line(buf, len);
160 if (!graft)
161 continue;
162 if (register_commit_graft(graft, 1))
163 error("duplicate graft data: %s", buf);
165 fclose(fp);
166 return 0;
169 static void prepare_commit_graft(void)
171 static int commit_graft_prepared;
172 char *graft_file;
174 if (commit_graft_prepared)
175 return;
176 graft_file = get_graft_file();
177 read_graft_file(graft_file);
178 /* make sure shallows are read */
179 is_repository_shallow();
180 commit_graft_prepared = 1;
183 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
185 int pos;
186 prepare_commit_graft();
187 pos = commit_graft_pos(sha1);
188 if (pos < 0)
189 return NULL;
190 return commit_graft[pos];
193 int write_shallow_commits(int fd, int use_pack_protocol)
195 int i, count = 0;
196 for (i = 0; i < commit_graft_nr; i++)
197 if (commit_graft[i]->nr_parent < 0) {
198 const char *hex =
199 sha1_to_hex(commit_graft[i]->sha1);
200 count++;
201 if (use_pack_protocol)
202 packet_write(fd, "shallow %s", hex);
203 else {
204 if (write_in_full(fd, hex, 40) != 40)
205 break;
206 if (write_in_full(fd, "\n", 1) != 1)
207 break;
210 return count;
213 int unregister_shallow(const unsigned char *sha1)
215 int pos = commit_graft_pos(sha1);
216 if (pos < 0)
217 return -1;
218 if (pos + 1 < commit_graft_nr)
219 memcpy(commit_graft + pos, commit_graft + pos + 1,
220 sizeof(struct commit_graft *)
221 * (commit_graft_nr - pos - 1));
222 commit_graft_nr--;
223 return 0;
226 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
228 char *tail = buffer;
229 char *bufptr = buffer;
230 unsigned char parent[20];
231 struct commit_list **pptr;
232 struct commit_graft *graft;
233 unsigned n_refs = 0;
235 if (item->object.parsed)
236 return 0;
237 item->object.parsed = 1;
238 tail += size;
239 if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5))
240 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
241 if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0)
242 return error("bad tree pointer in commit %s",
243 sha1_to_hex(item->object.sha1));
244 item->tree = lookup_tree(parent);
245 if (item->tree)
246 n_refs++;
247 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
248 pptr = &item->parents;
250 graft = lookup_commit_graft(item->object.sha1);
251 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
252 struct commit *new_parent;
254 if (tail <= bufptr + 48 ||
255 get_sha1_hex(bufptr + 7, parent) ||
256 bufptr[47] != '\n')
257 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
258 bufptr += 48;
259 if (graft)
260 continue;
261 new_parent = lookup_commit(parent);
262 if (new_parent) {
263 pptr = &commit_list_insert(new_parent, pptr)->next;
264 n_refs++;
267 if (graft) {
268 int i;
269 struct commit *new_parent;
270 for (i = 0; i < graft->nr_parent; i++) {
271 new_parent = lookup_commit(graft->parent[i]);
272 if (!new_parent)
273 continue;
274 pptr = &commit_list_insert(new_parent, pptr)->next;
275 n_refs++;
278 item->date = parse_commit_date(bufptr);
280 if (track_object_refs) {
281 unsigned i = 0;
282 struct commit_list *p;
283 struct object_refs *refs = alloc_object_refs(n_refs);
284 if (item->tree)
285 refs->ref[i++] = &item->tree->object;
286 for (p = item->parents; p; p = p->next)
287 refs->ref[i++] = &p->item->object;
288 set_object_refs(&item->object, refs);
291 return 0;
294 int parse_commit(struct commit *item)
296 enum object_type type;
297 void *buffer;
298 unsigned long size;
299 int ret;
301 if (item->object.parsed)
302 return 0;
303 buffer = read_sha1_file(item->object.sha1, &type, &size);
304 if (!buffer)
305 return error("Could not read %s",
306 sha1_to_hex(item->object.sha1));
307 if (type != OBJ_COMMIT) {
308 free(buffer);
309 return error("Object %s not a commit",
310 sha1_to_hex(item->object.sha1));
312 ret = parse_commit_buffer(item, buffer, size);
313 if (save_commit_buffer && !ret) {
314 item->buffer = buffer;
315 return 0;
317 free(buffer);
318 return ret;
321 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
323 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
324 new_list->item = item;
325 new_list->next = *list_p;
326 *list_p = new_list;
327 return new_list;
330 void free_commit_list(struct commit_list *list)
332 while (list) {
333 struct commit_list *temp = list;
334 list = temp->next;
335 free(temp);
339 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
341 struct commit_list **pp = list;
342 struct commit_list *p;
343 while ((p = *pp) != NULL) {
344 if (p->item->date < item->date) {
345 break;
347 pp = &p->next;
349 return commit_list_insert(item, pp);
353 void sort_by_date(struct commit_list **list)
355 struct commit_list *ret = NULL;
356 while (*list) {
357 insert_by_date((*list)->item, &ret);
358 *list = (*list)->next;
360 *list = ret;
363 struct commit *pop_most_recent_commit(struct commit_list **list,
364 unsigned int mark)
366 struct commit *ret = (*list)->item;
367 struct commit_list *parents = ret->parents;
368 struct commit_list *old = *list;
370 *list = (*list)->next;
371 free(old);
373 while (parents) {
374 struct commit *commit = parents->item;
375 parse_commit(commit);
376 if (!(commit->object.flags & mark)) {
377 commit->object.flags |= mark;
378 insert_by_date(commit, list);
380 parents = parents->next;
382 return ret;
385 void clear_commit_marks(struct commit *commit, unsigned int mark)
387 while (commit) {
388 struct commit_list *parents;
390 if (!(mark & commit->object.flags))
391 return;
393 commit->object.flags &= ~mark;
395 parents = commit->parents;
396 if (!parents)
397 return;
399 while ((parents = parents->next))
400 clear_commit_marks(parents->item, mark);
402 commit = commit->parents->item;
406 struct commit *pop_commit(struct commit_list **stack)
408 struct commit_list *top = *stack;
409 struct commit *item = top ? top->item : NULL;
411 if (top) {
412 *stack = top->next;
413 free(top);
415 return item;
419 * Performs an in-place topological sort on the list supplied.
421 void sort_in_topological_order(struct commit_list ** list, int lifo)
423 struct commit_list *next, *orig = *list;
424 struct commit_list *work, **insert;
425 struct commit_list **pptr;
427 if (!orig)
428 return;
429 *list = NULL;
431 /* Mark them and clear the indegree */
432 for (next = orig; next; next = next->next) {
433 struct commit *commit = next->item;
434 commit->object.flags |= TOPOSORT;
435 commit->indegree = 0;
438 /* update the indegree */
439 for (next = orig; next; next = next->next) {
440 struct commit_list * parents = next->item->parents;
441 while (parents) {
442 struct commit *parent = parents->item;
444 if (parent->object.flags & TOPOSORT)
445 parent->indegree++;
446 parents = parents->next;
451 * find the tips
453 * tips are nodes not reachable from any other node in the list
455 * the tips serve as a starting set for the work queue.
457 work = NULL;
458 insert = &work;
459 for (next = orig; next; next = next->next) {
460 struct commit *commit = next->item;
462 if (!commit->indegree)
463 insert = &commit_list_insert(commit, insert)->next;
466 /* process the list in topological order */
467 if (!lifo)
468 sort_by_date(&work);
470 pptr = list;
471 *list = NULL;
472 while (work) {
473 struct commit *commit;
474 struct commit_list *parents, *work_item;
476 work_item = work;
477 work = work_item->next;
478 work_item->next = NULL;
480 commit = work_item->item;
481 for (parents = commit->parents; parents ; parents = parents->next) {
482 struct commit *parent=parents->item;
484 if (!(parent->object.flags & TOPOSORT))
485 continue;
488 * parents are only enqueued for emission
489 * when all their children have been emitted thereby
490 * guaranteeing topological order.
492 if (!--parent->indegree) {
493 if (!lifo)
494 insert_by_date(parent, &work);
495 else
496 commit_list_insert(parent, &work);
500 * work_item is a commit all of whose children
501 * have already been emitted. we can emit it now.
503 commit->object.flags &= ~TOPOSORT;
504 *pptr = work_item;
505 pptr = &work_item->next;
509 /* merge-base stuff */
511 /* bits #0..15 in revision.h */
512 #define PARENT1 (1u<<16)
513 #define PARENT2 (1u<<17)
514 #define STALE (1u<<18)
515 #define RESULT (1u<<19)
517 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
519 static struct commit *interesting(struct commit_list *list)
521 while (list) {
522 struct commit *commit = list->item;
523 list = list->next;
524 if (commit->object.flags & STALE)
525 continue;
526 return commit;
528 return NULL;
531 static struct commit_list *merge_bases(struct commit *one, struct commit *two)
533 struct commit_list *list = NULL;
534 struct commit_list *result = NULL;
536 if (one == two)
537 /* We do not mark this even with RESULT so we do not
538 * have to clean it up.
540 return commit_list_insert(one, &result);
542 parse_commit(one);
543 parse_commit(two);
545 one->object.flags |= PARENT1;
546 two->object.flags |= PARENT2;
547 insert_by_date(one, &list);
548 insert_by_date(two, &list);
550 while (interesting(list)) {
551 struct commit *commit;
552 struct commit_list *parents;
553 struct commit_list *n;
554 int flags;
556 commit = list->item;
557 n = list->next;
558 free(list);
559 list = n;
561 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
562 if (flags == (PARENT1 | PARENT2)) {
563 if (!(commit->object.flags & RESULT)) {
564 commit->object.flags |= RESULT;
565 insert_by_date(commit, &result);
567 /* Mark parents of a found merge stale */
568 flags |= STALE;
570 parents = commit->parents;
571 while (parents) {
572 struct commit *p = parents->item;
573 parents = parents->next;
574 if ((p->object.flags & flags) == flags)
575 continue;
576 parse_commit(p);
577 p->object.flags |= flags;
578 insert_by_date(p, &list);
582 /* Clean up the result to remove stale ones */
583 free_commit_list(list);
584 list = result; result = NULL;
585 while (list) {
586 struct commit_list *n = list->next;
587 if (!(list->item->object.flags & STALE))
588 insert_by_date(list->item, &result);
589 free(list);
590 list = n;
592 return result;
595 struct commit_list *get_merge_bases(struct commit *one,
596 struct commit *two, int cleanup)
598 struct commit_list *list;
599 struct commit **rslt;
600 struct commit_list *result;
601 int cnt, i, j;
603 result = merge_bases(one, two);
604 if (one == two)
605 return result;
606 if (!result || !result->next) {
607 if (cleanup) {
608 clear_commit_marks(one, all_flags);
609 clear_commit_marks(two, all_flags);
611 return result;
614 /* There are more than one */
615 cnt = 0;
616 list = result;
617 while (list) {
618 list = list->next;
619 cnt++;
621 rslt = xcalloc(cnt, sizeof(*rslt));
622 for (list = result, i = 0; list; list = list->next)
623 rslt[i++] = list->item;
624 free_commit_list(result);
626 clear_commit_marks(one, all_flags);
627 clear_commit_marks(two, all_flags);
628 for (i = 0; i < cnt - 1; i++) {
629 for (j = i+1; j < cnt; j++) {
630 if (!rslt[i] || !rslt[j])
631 continue;
632 result = merge_bases(rslt[i], rslt[j]);
633 clear_commit_marks(rslt[i], all_flags);
634 clear_commit_marks(rslt[j], all_flags);
635 for (list = result; list; list = list->next) {
636 if (rslt[i] == list->item)
637 rslt[i] = NULL;
638 if (rslt[j] == list->item)
639 rslt[j] = NULL;
644 /* Surviving ones in rslt[] are the independent results */
645 result = NULL;
646 for (i = 0; i < cnt; i++) {
647 if (rslt[i])
648 insert_by_date(rslt[i], &result);
650 free(rslt);
651 return result;
654 int in_merge_bases(struct commit *commit, struct commit **reference, int num)
656 struct commit_list *bases, *b;
657 int ret = 0;
659 if (num == 1)
660 bases = get_merge_bases(commit, *reference, 1);
661 else
662 die("not yet");
663 for (b = bases; b; b = b->next) {
664 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
665 ret = 1;
666 break;
670 free_commit_list(bases);
671 return ret;