6 const char *commit_type = "commit";
8 struct commit *lookup_commit(unsigned char *sha1)
10 struct object *obj = lookup_object(sha1);
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;
18 if (obj->type != commit_type) {
19 error("Object %s is a %s, not a commit",
20 sha1_to_hex(sha1), obj->type);
23 return (struct commit *) obj;
26 static unsigned long parse_commit_date(const char *buf)
30 if (memcmp(buf, "author", 6))
32 while (*buf++ != '\n')
34 if (memcmp(buf, "committer", 9))
38 date = strtoul(buf, NULL, 10);
39 if (date == ULONG_MAX)
44 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
46 void *bufptr = buffer;
47 unsigned char parent[20];
49 if (item->object.parsed)
51 item->object.parsed = 1;
52 get_sha1_hex(bufptr + 5, parent);
53 item->tree = lookup_tree(parent);
55 add_ref(&item->object, &item->tree->object);
56 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
57 while (!memcmp(bufptr, "parent ", 7) &&
58 !get_sha1_hex(bufptr + 7, parent)) {
59 struct commit *new_parent = lookup_commit(parent);
61 commit_list_insert(new_parent, &item->parents);
62 add_ref(&item->object, &new_parent->object);
66 item->date = parse_commit_date(bufptr);
70 int parse_commit(struct commit *item)
77 if (item->object.parsed)
79 buffer = read_sha1_file(item->object.sha1, type, &size);
81 return error("Could not read %s",
82 sha1_to_hex(item->object.sha1));
83 if (strcmp(type, commit_type)) {
85 return error("Object %s not a commit",
86 sha1_to_hex(item->object.sha1));
88 ret = parse_commit_buffer(item, buffer, size);
93 void commit_list_insert(struct commit *item, struct commit_list **list_p)
95 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
96 new_list->item = item;
97 new_list->next = *list_p;
101 void free_commit_list(struct commit_list *list)
104 struct commit_list *temp = list;
110 static void insert_by_date(struct commit_list **list, struct commit *item)
112 struct commit_list **pp = list;
113 struct commit_list *p;
114 while ((p = *pp) != NULL) {
115 if (p->item->date < item->date) {
120 commit_list_insert(item, pp);
124 void sort_by_date(struct commit_list **list)
126 struct commit_list *ret = NULL;
128 insert_by_date(&ret, (*list)->item);
129 *list = (*list)->next;
134 struct commit *pop_most_recent_commit(struct commit_list **list,
137 struct commit *ret = (*list)->item;
138 struct commit_list *parents = ret->parents;
139 struct commit_list *old = *list;
141 *list = (*list)->next;
145 struct commit *commit = parents->item;
146 parse_commit(commit);
147 if (!(commit->object.flags & mark)) {
148 commit->object.flags |= mark;
149 insert_by_date(list, commit);
151 parents = parents->next;