Make 'git diff --cached' synonymous to 'git diff --cached HEAD'.
[git/dscho.git] / commit.c
blobf735f981bb2d4d7594e416bcb728ac06d09ebd0c
1 #include <ctype.h>
2 #include "tag.h"
3 #include "commit.h"
4 #include "cache.h"
6 int save_commit_buffer = 1;
8 struct sort_node
11 * the number of children of the associated commit
12 * that also occur in the list being sorted.
14 unsigned int indegree;
17 * reference to original list item that we will re-use
18 * on output.
20 struct commit_list * list_item;
24 const char *commit_type = "commit";
26 enum cmit_fmt get_commit_format(const char *arg)
28 if (!*arg)
29 return CMIT_FMT_DEFAULT;
30 if (!strcmp(arg, "=raw"))
31 return CMIT_FMT_RAW;
32 if (!strcmp(arg, "=medium"))
33 return CMIT_FMT_MEDIUM;
34 if (!strcmp(arg, "=short"))
35 return CMIT_FMT_SHORT;
36 if (!strcmp(arg, "=full"))
37 return CMIT_FMT_FULL;
38 if (!strcmp(arg, "=oneline"))
39 return CMIT_FMT_ONELINE;
40 die("invalid --pretty format");
43 static struct commit *check_commit(struct object *obj,
44 const unsigned char *sha1,
45 int quiet)
47 if (obj->type != commit_type) {
48 if (!quiet)
49 error("Object %s is a %s, not a commit",
50 sha1_to_hex(sha1), obj->type);
51 return NULL;
53 return (struct commit *) obj;
56 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
57 int quiet)
59 struct object *obj = deref_tag(parse_object(sha1));
61 if (!obj)
62 return NULL;
63 return check_commit(obj, sha1, quiet);
66 struct commit *lookup_commit_reference(const unsigned char *sha1)
68 return lookup_commit_reference_gently(sha1, 0);
71 struct commit *lookup_commit(const unsigned char *sha1)
73 struct object *obj = lookup_object(sha1);
74 if (!obj) {
75 struct commit *ret = xmalloc(sizeof(struct commit));
76 memset(ret, 0, sizeof(struct commit));
77 created_object(sha1, &ret->object);
78 ret->object.type = commit_type;
79 return ret;
81 if (!obj->type)
82 obj->type = commit_type;
83 return check_commit(obj, sha1, 0);
86 static unsigned long parse_commit_date(const char *buf)
88 unsigned long date;
90 if (memcmp(buf, "author", 6))
91 return 0;
92 while (*buf++ != '\n')
93 /* nada */;
94 if (memcmp(buf, "committer", 9))
95 return 0;
96 while (*buf++ != '>')
97 /* nada */;
98 date = strtoul(buf, NULL, 10);
99 if (date == ULONG_MAX)
100 date = 0;
101 return date;
104 static struct commit_graft {
105 unsigned char sha1[20];
106 int nr_parent;
107 unsigned char parent[0][20]; /* more */
108 } **commit_graft;
109 static int commit_graft_alloc, commit_graft_nr;
111 static int commit_graft_pos(const unsigned char *sha1)
113 int lo, hi;
114 lo = 0;
115 hi = commit_graft_nr;
116 while (lo < hi) {
117 int mi = (lo + hi) / 2;
118 struct commit_graft *graft = commit_graft[mi];
119 int cmp = memcmp(sha1, graft->sha1, 20);
120 if (!cmp)
121 return mi;
122 if (cmp < 0)
123 hi = mi;
124 else
125 lo = mi + 1;
127 return -lo - 1;
130 static void prepare_commit_graft(void)
132 char *graft_file = get_graft_file();
133 FILE *fp = fopen(graft_file, "r");
134 char buf[1024];
135 if (!fp) {
136 commit_graft = (struct commit_graft **) "hack";
137 return;
139 while (fgets(buf, sizeof(buf), fp)) {
140 /* The format is just "Commit Parent1 Parent2 ...\n" */
141 int len = strlen(buf);
142 int i;
143 struct commit_graft *graft = NULL;
145 if (buf[len-1] == '\n')
146 buf[--len] = 0;
147 if (buf[0] == '#')
148 continue;
149 if ((len + 1) % 41) {
150 bad_graft_data:
151 error("bad graft data: %s", buf);
152 free(graft);
153 continue;
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;
166 i = commit_graft_pos(graft->sha1);
167 if (0 <= i) {
168 error("duplicate graft data: %s", buf);
169 free(graft);
170 continue;
172 i = -i - 1;
173 if (commit_graft_alloc <= ++commit_graft_nr) {
174 commit_graft_alloc = alloc_nr(commit_graft_alloc);
175 commit_graft = xrealloc(commit_graft,
176 sizeof(*commit_graft) *
177 commit_graft_alloc);
179 if (i < commit_graft_nr)
180 memmove(commit_graft + i + 1,
181 commit_graft + i,
182 (commit_graft_nr - i - 1) *
183 sizeof(*commit_graft));
184 commit_graft[i] = graft;
186 fclose(fp);
189 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
191 int pos;
192 if (!commit_graft)
193 prepare_commit_graft();
194 pos = commit_graft_pos(sha1);
195 if (pos < 0)
196 return NULL;
197 return commit_graft[pos];
200 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
202 char *bufptr = buffer;
203 unsigned char parent[20];
204 struct commit_list **pptr;
205 struct commit_graft *graft;
207 if (item->object.parsed)
208 return 0;
209 item->object.parsed = 1;
210 if (memcmp(bufptr, "tree ", 5))
211 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
212 if (get_sha1_hex(bufptr + 5, parent) < 0)
213 return error("bad tree pointer in commit %s\n", sha1_to_hex(item->object.sha1));
214 item->tree = lookup_tree(parent);
215 if (item->tree)
216 add_ref(&item->object, &item->tree->object);
217 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
218 pptr = &item->parents;
220 graft = lookup_commit_graft(item->object.sha1);
221 while (!memcmp(bufptr, "parent ", 7)) {
222 struct commit *new_parent;
224 if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
225 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
226 bufptr += 48;
227 if (graft)
228 continue;
229 new_parent = lookup_commit(parent);
230 if (new_parent) {
231 pptr = &commit_list_insert(new_parent, pptr)->next;
232 add_ref(&item->object, &new_parent->object);
235 if (graft) {
236 int i;
237 struct commit *new_parent;
238 for (i = 0; i < graft->nr_parent; i++) {
239 new_parent = lookup_commit(graft->parent[i]);
240 if (!new_parent)
241 continue;
242 pptr = &commit_list_insert(new_parent, pptr)->next;
243 add_ref(&item->object, &new_parent->object);
246 item->date = parse_commit_date(bufptr);
247 return 0;
250 int parse_commit(struct commit *item)
252 char type[20];
253 void *buffer;
254 unsigned long size;
255 int ret;
257 if (item->object.parsed)
258 return 0;
259 buffer = read_sha1_file(item->object.sha1, type, &size);
260 if (!buffer)
261 return error("Could not read %s",
262 sha1_to_hex(item->object.sha1));
263 if (strcmp(type, commit_type)) {
264 free(buffer);
265 return error("Object %s not a commit",
266 sha1_to_hex(item->object.sha1));
268 ret = parse_commit_buffer(item, buffer, size);
269 if (save_commit_buffer && !ret) {
270 item->buffer = buffer;
271 return 0;
273 free(buffer);
274 return ret;
277 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
279 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
280 new_list->item = item;
281 new_list->next = *list_p;
282 *list_p = new_list;
283 return new_list;
286 void free_commit_list(struct commit_list *list)
288 while (list) {
289 struct commit_list *temp = list;
290 list = temp->next;
291 free(temp);
295 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
297 struct commit_list **pp = list;
298 struct commit_list *p;
299 while ((p = *pp) != NULL) {
300 if (p->item->date < item->date) {
301 break;
303 pp = &p->next;
305 return commit_list_insert(item, pp);
309 void sort_by_date(struct commit_list **list)
311 struct commit_list *ret = NULL;
312 while (*list) {
313 insert_by_date((*list)->item, &ret);
314 *list = (*list)->next;
316 *list = ret;
319 struct commit *pop_most_recent_commit(struct commit_list **list,
320 unsigned int mark)
322 struct commit *ret = (*list)->item;
323 struct commit_list *parents = ret->parents;
324 struct commit_list *old = *list;
326 *list = (*list)->next;
327 free(old);
329 while (parents) {
330 struct commit *commit = parents->item;
331 parse_commit(commit);
332 if (!(commit->object.flags & mark)) {
333 commit->object.flags |= mark;
334 insert_by_date(commit, list);
336 parents = parents->next;
338 return ret;
342 * Generic support for pretty-printing the header
344 static int get_one_line(const char *msg, unsigned long len)
346 int ret = 0;
348 while (len--) {
349 char c = *msg++;
350 ret++;
351 if (c == '\n')
352 break;
353 if (!c)
354 return 0;
356 return ret;
359 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
361 char *date;
362 int namelen;
363 unsigned long time;
364 int tz, ret;
366 if (fmt == CMIT_FMT_ONELINE)
367 return 0;
368 date = strchr(line, '>');
369 if (!date)
370 return 0;
371 namelen = ++date - line;
372 time = strtoul(date, &date, 10);
373 tz = strtol(date, NULL, 10);
375 ret = sprintf(buf, "%s: %.*s\n", what, namelen, line);
376 if (fmt == CMIT_FMT_MEDIUM)
377 ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
378 return ret;
381 static int is_empty_line(const char *line, int len)
383 while (len && isspace(line[len-1]))
384 len--;
385 return !len;
388 static int add_parent_info(enum cmit_fmt fmt, char *buf, const char *line, int parents)
390 int offset = 0;
392 if (fmt == CMIT_FMT_ONELINE)
393 return offset;
394 switch (parents) {
395 case 1:
396 break;
397 case 2:
398 /* Go back to the previous line: 40 characters of previous parent, and one '\n' */
399 offset = sprintf(buf, "Merge: %.40s\n", line-41);
400 /* Fallthrough */
401 default:
402 /* Replace the previous '\n' with a space */
403 buf[offset-1] = ' ';
404 offset += sprintf(buf + offset, "%.40s\n", line+7);
406 return offset;
409 unsigned long pretty_print_commit(enum cmit_fmt fmt, const char *msg, unsigned long len, char *buf, unsigned long space)
411 int hdr = 1, body = 0;
412 unsigned long offset = 0;
413 int parents = 0;
414 int indent = (fmt == CMIT_FMT_ONELINE) ? 0 : 4;
416 for (;;) {
417 const char *line = msg;
418 int linelen = get_one_line(msg, len);
420 if (!linelen)
421 break;
424 * We want some slop for indentation and a possible
425 * final "...". Thus the "+ 20".
427 if (offset + linelen + 20 > space) {
428 memcpy(buf + offset, " ...\n", 8);
429 offset += 8;
430 break;
433 msg += linelen;
434 len -= linelen;
435 if (hdr) {
436 if (linelen == 1) {
437 hdr = 0;
438 if (fmt != CMIT_FMT_ONELINE)
439 buf[offset++] = '\n';
440 continue;
442 if (fmt == CMIT_FMT_RAW) {
443 memcpy(buf + offset, line, linelen);
444 offset += linelen;
445 continue;
447 if (!memcmp(line, "parent ", 7)) {
448 if (linelen != 48)
449 die("bad parent line in commit");
450 offset += add_parent_info(fmt, buf + offset, line, ++parents);
452 if (!memcmp(line, "author ", 7))
453 offset += add_user_info("Author", fmt, buf + offset, line + 7);
454 if (fmt == CMIT_FMT_FULL) {
455 if (!memcmp(line, "committer ", 10))
456 offset += add_user_info("Commit", fmt, buf + offset, line + 10);
458 continue;
461 if (is_empty_line(line, linelen)) {
462 if (!body)
463 continue;
464 if (fmt == CMIT_FMT_SHORT)
465 break;
466 } else {
467 body = 1;
470 memset(buf + offset, ' ', indent);
471 memcpy(buf + offset + indent, line, linelen);
472 offset += linelen + indent;
473 if (fmt == CMIT_FMT_ONELINE)
474 break;
476 if (fmt == CMIT_FMT_ONELINE) {
477 /* We do not want the terminating newline */
478 if (buf[offset - 1] == '\n')
479 offset--;
481 else {
482 /* Make sure there is an EOLN */
483 if (buf[offset - 1] != '\n')
484 buf[offset++] = '\n';
486 buf[offset] = '\0';
487 return offset;
490 struct commit *pop_commit(struct commit_list **stack)
492 struct commit_list *top = *stack;
493 struct commit *item = top ? top->item : NULL;
495 if (top) {
496 *stack = top->next;
497 free(top);
499 return item;
502 int count_parents(struct commit * commit)
504 int count = 0;
505 struct commit_list * parents = commit->parents;
506 for (count=0;parents; parents=parents->next,count++)
508 return count;
512 * Performs an in-place topological sort on the list supplied.
514 void sort_in_topological_order(struct commit_list ** list)
516 struct commit_list * next = *list;
517 struct commit_list * work = NULL;
518 struct commit_list ** pptr = list;
519 struct sort_node * nodes;
520 struct sort_node * next_nodes;
521 int count = 0;
523 /* determine the size of the list */
524 while (next) {
525 next = next->next;
526 count++;
528 /* allocate an array to help sort the list */
529 nodes = xcalloc(count, sizeof(*nodes));
530 /* link the list to the array */
531 next_nodes = nodes;
532 next=*list;
533 while (next) {
534 next_nodes->list_item = next;
535 next->item->object.util = next_nodes;
536 next_nodes++;
537 next = next->next;
539 /* update the indegree */
540 next=*list;
541 while (next) {
542 struct commit_list * parents = next->item->parents;
543 while (parents) {
544 struct commit * parent=parents->item;
545 struct sort_node * pn = (struct sort_node *)parent->object.util;
547 if (pn)
548 pn->indegree++;
549 parents=parents->next;
551 next=next->next;
554 * find the tips
556 * tips are nodes not reachable from any other node in the list
558 * the tips serve as a starting set for the work queue.
560 next=*list;
561 while (next) {
562 struct sort_node * node = (struct sort_node *)next->item->object.util;
564 if (node->indegree == 0) {
565 commit_list_insert(next->item, &work);
567 next=next->next;
569 /* process the list in topological order */
570 while (work) {
571 struct commit * work_item = pop_commit(&work);
572 struct sort_node * work_node = (struct sort_node *)work_item->object.util;
573 struct commit_list * parents = work_item->parents;
575 while (parents) {
576 struct commit * parent=parents->item;
577 struct sort_node * pn = (struct sort_node *)parent->object.util;
579 if (pn) {
581 * parents are only enqueued for emission
582 * when all their children have been emitted thereby
583 * guaranteeing topological order.
585 pn->indegree--;
586 if (!pn->indegree)
587 commit_list_insert(parent, &work);
589 parents=parents->next;
592 * work_item is a commit all of whose children
593 * have already been emitted. we can emit it now.
595 *pptr = work_node->list_item;
596 pptr = &(*pptr)->next;
597 *pptr = NULL;
598 work_item->object.util = NULL;
600 free(nodes);