]> git.ipfire.org Git - thirdparty/git.git/blame - commit.c
Merge branch 'fixo/1.0'
[thirdparty/git.git] / commit.c
CommitLineData
8f1d2e6f 1#include "cache.h"
961784ee 2#include "tag.h"
175785e5 3#include "commit.h"
175785e5 4
60ab26de
LT
5int save_commit_buffer = 1;
6
ab580ace
JS
7struct sort_node
8{
9 /*
10 * the number of children of the associated commit
11 * that also occur in the list being sorted.
12 */
13 unsigned int indegree;
14
15 /*
16 * reference to original list item that we will re-use
17 * on output.
18 */
19 struct commit_list * list_item;
20
21};
22
175785e5
DB
23const char *commit_type = "commit";
24
9b66ec04
LT
25enum cmit_fmt get_commit_format(const char *arg)
26{
27 if (!*arg)
28 return CMIT_FMT_DEFAULT;
29 if (!strcmp(arg, "=raw"))
30 return CMIT_FMT_RAW;
31 if (!strcmp(arg, "=medium"))
32 return CMIT_FMT_MEDIUM;
33 if (!strcmp(arg, "=short"))
34 return CMIT_FMT_SHORT;
35 if (!strcmp(arg, "=full"))
36 return CMIT_FMT_FULL;
ff56fe1c
JH
37 if (!strcmp(arg, "=fuller"))
38 return CMIT_FMT_FULLER;
d87449c5
JH
39 if (!strcmp(arg, "=oneline"))
40 return CMIT_FMT_ONELINE;
9b66ec04
LT
41 die("invalid --pretty format");
42}
43
f76412ed
JH
44static struct commit *check_commit(struct object *obj,
45 const unsigned char *sha1,
46 int quiet)
961784ee
LT
47{
48 if (obj->type != commit_type) {
f76412ed
JH
49 if (!quiet)
50 error("Object %s is a %s, not a commit",
51 sha1_to_hex(sha1), obj->type);
961784ee
LT
52 return NULL;
53 }
54 return (struct commit *) obj;
55}
56
f76412ed
JH
57struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
58 int quiet)
961784ee 59{
9534f40b 60 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
961784ee
LT
61
62 if (!obj)
63 return NULL;
f76412ed
JH
64 return check_commit(obj, sha1, quiet);
65}
66
67struct commit *lookup_commit_reference(const unsigned char *sha1)
68{
69 return lookup_commit_reference_gently(sha1, 0);
961784ee
LT
70}
71
5d6ccf5c 72struct commit *lookup_commit(const unsigned char *sha1)
175785e5
DB
73{
74 struct object *obj = lookup_object(sha1);
75 if (!obj) {
812666c8 76 struct commit *ret = xmalloc(sizeof(struct commit));
175785e5
DB
77 memset(ret, 0, sizeof(struct commit));
78 created_object(sha1, &ret->object);
d32987be 79 ret->object.type = commit_type;
175785e5
DB
80 return ret;
81 }
d1af002d
NP
82 if (!obj->type)
83 obj->type = commit_type;
f76412ed 84 return check_commit(obj, sha1, 0);
175785e5
DB
85}
86
87static unsigned long parse_commit_date(const char *buf)
88{
89 unsigned long date;
90
91 if (memcmp(buf, "author", 6))
92 return 0;
93 while (*buf++ != '\n')
94 /* nada */;
95 if (memcmp(buf, "committer", 9))
96 return 0;
97 while (*buf++ != '>')
98 /* nada */;
99 date = strtoul(buf, NULL, 10);
100 if (date == ULONG_MAX)
101 date = 0;
102 return date;
103}
104
5da5c8f4
JH
105static struct commit_graft {
106 unsigned char sha1[20];
107 int nr_parent;
2c04662d 108 unsigned char parent[0][20]; /* more */
5da5c8f4
JH
109} **commit_graft;
110static int commit_graft_alloc, commit_graft_nr;
111
112static int commit_graft_pos(const unsigned char *sha1)
113{
114 int lo, hi;
115 lo = 0;
116 hi = commit_graft_nr;
117 while (lo < hi) {
118 int mi = (lo + hi) / 2;
119 struct commit_graft *graft = commit_graft[mi];
120 int cmp = memcmp(sha1, graft->sha1, 20);
121 if (!cmp)
122 return mi;
123 if (cmp < 0)
124 hi = mi;
125 else
126 lo = mi + 1;
127 }
128 return -lo - 1;
129}
130
131static void prepare_commit_graft(void)
132{
133 char *graft_file = get_graft_file();
134 FILE *fp = fopen(graft_file, "r");
135 char buf[1024];
136 if (!fp) {
137 commit_graft = (struct commit_graft **) "hack";
138 return;
139 }
140 while (fgets(buf, sizeof(buf), fp)) {
141 /* The format is just "Commit Parent1 Parent2 ...\n" */
142 int len = strlen(buf);
143 int i;
144 struct commit_graft *graft = NULL;
145
146 if (buf[len-1] == '\n')
147 buf[--len] = 0;
148 if (buf[0] == '#')
149 continue;
150 if ((len + 1) % 41) {
151 bad_graft_data:
152 error("bad graft data: %s", buf);
153 free(graft);
154 continue;
155 }
156 i = (len + 1) / 41 - 1;
157 graft = xmalloc(sizeof(*graft) + 20 * i);
158 graft->nr_parent = i;
159 if (get_sha1_hex(buf, graft->sha1))
160 goto bad_graft_data;
161 for (i = 40; i < len; i += 41) {
162 if (buf[i] != ' ')
163 goto bad_graft_data;
164 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
165 goto bad_graft_data;
166 }
167 i = commit_graft_pos(graft->sha1);
168 if (0 <= i) {
169 error("duplicate graft data: %s", buf);
170 free(graft);
171 continue;
172 }
173 i = -i - 1;
174 if (commit_graft_alloc <= ++commit_graft_nr) {
175 commit_graft_alloc = alloc_nr(commit_graft_alloc);
176 commit_graft = xrealloc(commit_graft,
177 sizeof(*commit_graft) *
178 commit_graft_alloc);
179 }
180 if (i < commit_graft_nr)
181 memmove(commit_graft + i + 1,
182 commit_graft + i,
183 (commit_graft_nr - i - 1) *
184 sizeof(*commit_graft));
185 commit_graft[i] = graft;
186 }
187 fclose(fp);
188}
189
190static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
191{
192 int pos;
193 if (!commit_graft)
194 prepare_commit_graft();
195 pos = commit_graft_pos(sha1);
196 if (pos < 0)
197 return NULL;
198 return commit_graft[pos];
199}
200
bd2c39f5 201int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
175785e5 202{
f7cc77d7 203 char *bufptr = buffer;
175785e5 204 unsigned char parent[20];
6c88be16 205 struct commit_list **pptr;
5da5c8f4 206 struct commit_graft *graft;
27dedf0c 207 unsigned n_refs = 0;
bd2c39f5 208
175785e5
DB
209 if (item->object.parsed)
210 return 0;
211 item->object.parsed = 1;
f7cc77d7
LT
212 if (memcmp(bufptr, "tree ", 5))
213 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
214 if (get_sha1_hex(bufptr + 5, parent) < 0)
215 return error("bad tree pointer in commit %s\n", sha1_to_hex(item->object.sha1));
175785e5 216 item->tree = lookup_tree(parent);
235ac407 217 if (item->tree)
27dedf0c 218 n_refs++;
175785e5 219 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
6c88be16 220 pptr = &item->parents;
5da5c8f4
JH
221
222 graft = lookup_commit_graft(item->object.sha1);
f7cc77d7
LT
223 while (!memcmp(bufptr, "parent ", 7)) {
224 struct commit *new_parent;
225
226 if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
227 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
5da5c8f4
JH
228 bufptr += 48;
229 if (graft)
230 continue;
f7cc77d7 231 new_parent = lookup_commit(parent);
235ac407 232 if (new_parent) {
6c88be16 233 pptr = &commit_list_insert(new_parent, pptr)->next;
27dedf0c 234 n_refs++;
235ac407 235 }
5da5c8f4
JH
236 }
237 if (graft) {
238 int i;
239 struct commit *new_parent;
240 for (i = 0; i < graft->nr_parent; i++) {
241 new_parent = lookup_commit(graft->parent[i]);
242 if (!new_parent)
243 continue;
244 pptr = &commit_list_insert(new_parent, pptr)->next;
27dedf0c 245 n_refs++;
5da5c8f4 246 }
175785e5
DB
247 }
248 item->date = parse_commit_date(bufptr);
27dedf0c
JH
249
250 if (track_object_refs) {
251 unsigned i = 0;
252 struct commit_list *p;
253 struct object_refs *refs = alloc_object_refs(n_refs);
254 if (item->tree)
255 refs->ref[i++] = &item->tree->object;
256 for (p = item->parents; p; p = p->next)
257 refs->ref[i++] = &p->item->object;
258 set_object_refs(&item->object, refs);
259 }
260
175785e5
DB
261 return 0;
262}
263
bd2c39f5
NP
264int parse_commit(struct commit *item)
265{
266 char type[20];
267 void *buffer;
268 unsigned long size;
269 int ret;
270
271 if (item->object.parsed)
272 return 0;
273 buffer = read_sha1_file(item->object.sha1, type, &size);
274 if (!buffer)
275 return error("Could not read %s",
276 sha1_to_hex(item->object.sha1));
277 if (strcmp(type, commit_type)) {
278 free(buffer);
279 return error("Object %s not a commit",
280 sha1_to_hex(item->object.sha1));
281 }
282 ret = parse_commit_buffer(item, buffer, size);
60ab26de 283 if (save_commit_buffer && !ret) {
3ff1fbbb
LT
284 item->buffer = buffer;
285 return 0;
286 }
bd2c39f5
NP
287 free(buffer);
288 return ret;
289}
290
ac5155ef 291struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
dd97f850 292{
812666c8 293 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
dd97f850
DB
294 new_list->item = item;
295 new_list->next = *list_p;
296 *list_p = new_list;
ac5155ef 297 return new_list;
dd97f850
DB
298}
299
175785e5
DB
300void free_commit_list(struct commit_list *list)
301{
302 while (list) {
303 struct commit_list *temp = list;
304 list = temp->next;
305 free(temp);
306 }
307}
dd97f850 308
f755494c 309struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
dd97f850
DB
310{
311 struct commit_list **pp = list;
312 struct commit_list *p;
313 while ((p = *pp) != NULL) {
314 if (p->item->date < item->date) {
315 break;
316 }
317 pp = &p->next;
318 }
f755494c 319 return commit_list_insert(item, pp);
dd97f850
DB
320}
321
322
323void sort_by_date(struct commit_list **list)
324{
325 struct commit_list *ret = NULL;
326 while (*list) {
f755494c 327 insert_by_date((*list)->item, &ret);
dd97f850
DB
328 *list = (*list)->next;
329 }
330 *list = ret;
331}
332
58e28af6
DB
333struct commit *pop_most_recent_commit(struct commit_list **list,
334 unsigned int mark)
dd97f850
DB
335{
336 struct commit *ret = (*list)->item;
337 struct commit_list *parents = ret->parents;
338 struct commit_list *old = *list;
339
340 *list = (*list)->next;
341 free(old);
342
343 while (parents) {
4056c091 344 struct commit *commit = parents->item;
58e28af6
DB
345 parse_commit(commit);
346 if (!(commit->object.flags & mark)) {
347 commit->object.flags |= mark;
f755494c 348 insert_by_date(commit, list);
4056c091 349 }
dd97f850
DB
350 parents = parents->next;
351 }
352 return ret;
353}
e3bc7a3b 354
f8f9c73c
JH
355void clear_commit_marks(struct commit *commit, unsigned int mark)
356{
357 struct commit_list *parents;
358
359 parents = commit->parents;
360 commit->object.flags &= ~mark;
361 while (parents) {
362 if (parents->item && parents->item->object.parsed)
363 clear_commit_marks(parents->item, mark);
364 parents = parents->next;
365 }
366}
367
e3bc7a3b
LT
368/*
369 * Generic support for pretty-printing the header
370 */
371static int get_one_line(const char *msg, unsigned long len)
372{
373 int ret = 0;
374
375 while (len--) {
376 char c = *msg++;
377 ret++;
378 if (c == '\n')
379 break;
380 if (!c)
381 return 0;
382 }
383 return ret;
384}
385
9b66ec04 386static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
e3bc7a3b
LT
387{
388 char *date;
5de36bfe 389 int namelen;
e3bc7a3b 390 unsigned long time;
000182ea 391 int tz, ret;
ff56fe1c 392 const char *filler = " ";
e3bc7a3b 393
d87449c5
JH
394 if (fmt == CMIT_FMT_ONELINE)
395 return 0;
e3bc7a3b
LT
396 date = strchr(line, '>');
397 if (!date)
398 return 0;
399 namelen = ++date - line;
400 time = strtoul(date, &date, 10);
401 tz = strtol(date, NULL, 10);
402
ff56fe1c
JH
403 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
404 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
405 filler, namelen, line);
406 switch (fmt) {
407 case CMIT_FMT_MEDIUM:
000182ea 408 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
ff56fe1c
JH
409 break;
410 case CMIT_FMT_FULLER:
411 ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
412 break;
413 default:
414 /* notin' */
415 break;
416 }
000182ea
LT
417 return ret;
418}
419
420static int is_empty_line(const char *line, int len)
421{
422 while (len && isspace(line[len-1]))
423 len--;
424 return !len;
e3bc7a3b
LT
425}
426
28342a5d
LT
427static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents)
428{
429 int offset = 0;
d87449c5
JH
430
431 if (fmt == CMIT_FMT_ONELINE)
432 return offset;
28342a5d
LT
433 switch (parents) {
434 case 1:
435 break;
436 case 2:
437 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
438 offset = sprintf(buf, "Merge: %.40s\n", line-41);
439 /* Fallthrough */
440 default:
441 /* Replace the previous '\n' with a space */
442 buf[offset-1] = ' ';
443 offset += sprintf(buf + offset, "%.40s\n", line+7);
444 }
445 return offset;
446}
447
000182ea 448unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space)
e3bc7a3b 449{
000182ea 450 int hdr = 1, body = 0;
e3bc7a3b 451 unsigned long offset = 0;
28342a5d 452 int parents = 0;
d87449c5 453 int indent = (fmt == CMIT_FMT_ONELINE) ? 0 : 4;
e3bc7a3b
LT
454
455 for (;;) {
456 const char *line = msg;
457 int linelen = get_one_line(msg, len);
458
459 if (!linelen)
460 break;
461
462 /*
463 * We want some slop for indentation and a possible
464 * final "...". Thus the "+ 20".
465 */
466 if (offset + linelen + 20 > space) {
467 memcpy(buf + offset, " ...\n", 8);
468 offset += 8;
469 break;
470 }
471
472 msg += linelen;
473 len -= linelen;
e3bc7a3b 474 if (hdr) {
000182ea
LT
475 if (linelen == 1) {
476 hdr = 0;
d87449c5
JH
477 if (fmt != CMIT_FMT_ONELINE)
478 buf[offset++] = '\n';
000182ea
LT
479 continue;
480 }
481 if (fmt == CMIT_FMT_RAW) {
482 memcpy(buf + offset, line, linelen);
483 offset += linelen;
484 continue;
485 }
28342a5d
LT
486 if (!memcmp(line, "parent ", 7)) {
487 if (linelen != 48)
488 die("bad parent line in commit");
489 offset += add_parent_info(fmt, buf + offset, line, ++parents);
490 }
ff56fe1c
JH
491
492 /*
493 * MEDIUM == DEFAULT shows only author with dates.
494 * FULL shows both authors but not dates.
495 * FULLER shows both authors and dates.
496 */
e3bc7a3b 497 if (!memcmp(line, "author ", 7))
ff56fe1c
JH
498 offset += add_user_info("Author", fmt,
499 buf + offset,
500 line + 7);
501 if (!memcmp(line, "committer ", 10) &&
502 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
503 offset += add_user_info("Commit", fmt,
504 buf + offset,
505 line + 10);
e3bc7a3b
LT
506 continue;
507 }
000182ea
LT
508
509 if (is_empty_line(line, linelen)) {
510 if (!body)
511 continue;
512 if (fmt == CMIT_FMT_SHORT)
513 break;
514 } else {
515 body = 1;
516 }
d87449c5
JH
517
518 memset(buf + offset, ' ', indent);
519 memcpy(buf + offset + indent, line, linelen);
520 offset += linelen + indent;
521 if (fmt == CMIT_FMT_ONELINE)
522 break;
523 }
524 if (fmt == CMIT_FMT_ONELINE) {
525 /* We do not want the terminating newline */
526 if (buf[offset - 1] == '\n')
527 offset--;
528 }
529 else {
530 /* Make sure there is an EOLN */
531 if (buf[offset - 1] != '\n')
532 buf[offset++] = '\n';
e3bc7a3b 533 }
e3bc7a3b
LT
534 buf[offset] = '\0';
535 return offset;
536}
a3437b8c
JS
537
538struct commit *pop_commit(struct commit_list **stack)
539{
540 struct commit_list *top = *stack;
541 struct commit *item = top ? top->item : NULL;
542
543 if (top) {
544 *stack = top->next;
545 free(top);
546 }
547 return item;
548}
549
550int count_parents(struct commit * commit)
551{
552 int count = 0;
553 struct commit_list * parents = commit->parents;
554 for (count=0;parents; parents=parents->next,count++)
555 ;
556 return count;
557}
558
ab580ace
JS
559/*
560 * Performs an in-place topological sort on the list supplied.
561 */
562void sort_in_topological_order(struct commit_list ** list)
563{
564 struct commit_list * next = *list;
2ed02887 565 struct commit_list * work = NULL, **insert;
ab580ace
JS
566 struct commit_list ** pptr = list;
567 struct sort_node * nodes;
568 struct sort_node * next_nodes;
569 int count = 0;
570
571 /* determine the size of the list */
572 while (next) {
573 next = next->next;
574 count++;
575 }
7d6fb370
EW
576
577 if (!count)
578 return;
ab580ace
JS
579 /* allocate an array to help sort the list */
580 nodes = xcalloc(count, sizeof(*nodes));
581 /* link the list to the array */
582 next_nodes = nodes;
583 next=*list;
584 while (next) {
585 next_nodes->list_item = next;
586 next->item->object.util = next_nodes;
587 next_nodes++;
588 next = next->next;
589 }
590 /* update the indegree */
591 next=*list;
592 while (next) {
593 struct commit_list * parents = next->item->parents;
594 while (parents) {
595 struct commit * parent=parents->item;
596 struct sort_node * pn = (struct sort_node *)parent->object.util;
597
598 if (pn)
599 pn->indegree++;
600 parents=parents->next;
601 }
602 next=next->next;
603 }
604 /*
605 * find the tips
606 *
607 * tips are nodes not reachable from any other node in the list
608 *
609 * the tips serve as a starting set for the work queue.
610 */
611 next=*list;
2ed02887 612 insert = &work;
ab580ace
JS
613 while (next) {
614 struct sort_node * node = (struct sort_node *)next->item->object.util;
615
616 if (node->indegree == 0) {
2ed02887 617 insert = &commit_list_insert(next->item, insert)->next;
ab580ace
JS
618 }
619 next=next->next;
620 }
621 /* process the list in topological order */
622 while (work) {
623 struct commit * work_item = pop_commit(&work);
624 struct sort_node * work_node = (struct sort_node *)work_item->object.util;
625 struct commit_list * parents = work_item->parents;
626
627 while (parents) {
628 struct commit * parent=parents->item;
629 struct sort_node * pn = (struct sort_node *)parent->object.util;
630
631 if (pn) {
632 /*
633 * parents are only enqueued for emission
634 * when all their children have been emitted thereby
635 * guaranteeing topological order.
636 */
637 pn->indegree--;
638 if (!pn->indegree)
639 commit_list_insert(parent, &work);
640 }
641 parents=parents->next;
642 }
643 /*
644 * work_item is a commit all of whose children
645 * have already been emitted. we can emit it now.
646 */
647 *pptr = work_node->list_item;
648 pptr = &(*pptr)->next;
649 *pptr = NULL;
650 work_item->object.util = NULL;
651 }
652 free(nodes);
653}