7 const char *commit_type = "commit";
9 static struct commit *check_commit(struct object *obj, unsigned char *sha1)
11 if (obj->type != commit_type) {
12 error("Object %s is a %s, not a commit",
13 sha1_to_hex(sha1), obj->type);
16 return (struct commit *) obj;
19 struct commit *lookup_commit_reference(unsigned char *sha1)
21 struct object *obj = parse_object(sha1);
25 if (obj->type == tag_type)
26 obj = ((struct tag *)obj)->tagged;
27 return check_commit(obj, sha1);
30 struct commit *lookup_commit(unsigned char *sha1)
32 struct object *obj = lookup_object(sha1);
34 struct commit *ret = xmalloc(sizeof(struct commit));
35 memset(ret, 0, sizeof(struct commit));
36 created_object(sha1, &ret->object);
37 ret->object.type = commit_type;
40 return check_commit(obj, sha1);
43 static unsigned long parse_commit_date(const char *buf)
47 if (memcmp(buf, "author", 6))
49 while (*buf++ != '\n')
51 if (memcmp(buf, "committer", 9))
55 date = strtoul(buf, NULL, 10);
56 if (date == ULONG_MAX)
61 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
63 void *bufptr = buffer;
64 unsigned char parent[20];
66 if (item->object.parsed)
68 item->object.parsed = 1;
69 get_sha1_hex(bufptr + 5, parent);
70 item->tree = lookup_tree(parent);
72 add_ref(&item->object, &item->tree->object);
73 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
74 while (!memcmp(bufptr, "parent ", 7) &&
75 !get_sha1_hex(bufptr + 7, parent)) {
76 struct commit *new_parent = lookup_commit(parent);
78 commit_list_insert(new_parent, &item->parents);
79 add_ref(&item->object, &new_parent->object);
83 item->date = parse_commit_date(bufptr);
87 int parse_commit(struct commit *item)
94 if (item->object.parsed)
96 buffer = read_sha1_file(item->object.sha1, type, &size);
98 return error("Could not read %s",
99 sha1_to_hex(item->object.sha1));
100 if (strcmp(type, commit_type)) {
102 return error("Object %s not a commit",
103 sha1_to_hex(item->object.sha1));
105 ret = parse_commit_buffer(item, buffer, size);
110 void commit_list_insert(struct commit *item, struct commit_list **list_p)
112 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
113 new_list->item = item;
114 new_list->next = *list_p;
118 void free_commit_list(struct commit_list *list)
121 struct commit_list *temp = list;
127 static void insert_by_date(struct commit_list **list, struct commit *item)
129 struct commit_list **pp = list;
130 struct commit_list *p;
131 while ((p = *pp) != NULL) {
132 if (p->item->date < item->date) {
137 commit_list_insert(item, pp);
141 void sort_by_date(struct commit_list **list)
143 struct commit_list *ret = NULL;
145 insert_by_date(&ret, (*list)->item);
146 *list = (*list)->next;
151 struct commit *pop_most_recent_commit(struct commit_list **list,
154 struct commit *ret = (*list)->item;
155 struct commit_list *parents = ret->parents;
156 struct commit_list *old = *list;
158 *list = (*list)->next;
162 struct commit *commit = parents->item;
163 parse_commit(commit);
164 if (!(commit->object.flags & mark)) {
165 commit->object.flags |= mark;
166 insert_by_date(list, commit);
168 parents = parents->next;