Add documentation for the rest of commands.
[git/dscho.git] / commit.c
blobb45118a544276797f6bc9c0f482c187647f0e88d
1 #include "commit.h"
2 #include "cache.h"
3 #include <string.h>
4 #include <limits.h>
6 const char *commit_type = "commit";
8 struct commit *lookup_commit(unsigned char *sha1)
10 struct object *obj = lookup_object(sha1);
11 if (!obj) {
12 struct commit *ret = xmalloc(sizeof(struct commit));
13 memset(ret, 0, sizeof(struct commit));
14 created_object(sha1, &ret->object);
15 ret->object.type = commit_type;
16 return ret;
18 if (obj->type != commit_type) {
19 error("Object %s is a %s, not a commit",
20 sha1_to_hex(sha1), obj->type);
21 return NULL;
23 return (struct commit *) obj;
26 static unsigned long parse_commit_date(const char *buf)
28 unsigned long date;
30 if (memcmp(buf, "author", 6))
31 return 0;
32 while (*buf++ != '\n')
33 /* nada */;
34 if (memcmp(buf, "committer", 9))
35 return 0;
36 while (*buf++ != '>')
37 /* nada */;
38 date = strtoul(buf, NULL, 10);
39 if (date == ULONG_MAX)
40 date = 0;
41 return date;
44 int parse_commit(struct commit *item)
46 char type[20];
47 void * buffer, *bufptr;
48 unsigned long size;
49 unsigned char parent[20];
50 if (item->object.parsed)
51 return 0;
52 item->object.parsed = 1;
53 buffer = bufptr = read_sha1_file(item->object.sha1, type, &size);
54 if (!buffer)
55 return error("Could not read %s",
56 sha1_to_hex(item->object.sha1));
57 if (strcmp(type, commit_type)) {
58 free(buffer);
59 return error("Object %s not a commit",
60 sha1_to_hex(item->object.sha1));
62 get_sha1_hex(bufptr + 5, parent);
63 item->tree = lookup_tree(parent);
64 if (item->tree)
65 add_ref(&item->object, &item->tree->object);
66 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
67 while (!memcmp(bufptr, "parent ", 7) &&
68 !get_sha1_hex(bufptr + 7, parent)) {
69 struct commit *new_parent = lookup_commit(parent);
70 if (new_parent) {
71 commit_list_insert(new_parent, &item->parents);
72 add_ref(&item->object, &new_parent->object);
74 bufptr += 48;
76 item->date = parse_commit_date(bufptr);
77 free(buffer);
78 return 0;
81 void commit_list_insert(struct commit *item, struct commit_list **list_p)
83 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
84 new_list->item = item;
85 new_list->next = *list_p;
86 *list_p = new_list;
89 void free_commit_list(struct commit_list *list)
91 while (list) {
92 struct commit_list *temp = list;
93 list = temp->next;
94 free(temp);
98 static void insert_by_date(struct commit_list **list, struct commit *item)
100 struct commit_list **pp = list;
101 struct commit_list *p;
102 while ((p = *pp) != NULL) {
103 if (p->item->date < item->date) {
104 break;
106 pp = &p->next;
108 commit_list_insert(item, pp);
112 void sort_by_date(struct commit_list **list)
114 struct commit_list *ret = NULL;
115 while (*list) {
116 insert_by_date(&ret, (*list)->item);
117 *list = (*list)->next;
119 *list = ret;
122 struct commit *pop_most_recent_commit(struct commit_list **list,
123 unsigned int mark)
125 struct commit *ret = (*list)->item;
126 struct commit_list *parents = ret->parents;
127 struct commit_list *old = *list;
129 *list = (*list)->next;
130 free(old);
132 while (parents) {
133 struct commit *commit = parents->item;
134 parse_commit(commit);
135 if (!(commit->object.flags & mark)) {
136 commit->object.flags |= mark;
137 insert_by_date(list, commit);
139 parents = parents->next;
141 return ret;