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
;
41 obj
->type
= commit_type
;
42 return check_commit(obj
, sha1
);
45 static unsigned long parse_commit_date(const char *buf
)
49 if (memcmp(buf
, "author", 6))
51 while (*buf
++ != '\n')
53 if (memcmp(buf
, "committer", 9))
57 date
= strtoul(buf
, NULL
, 10);
58 if (date
== ULONG_MAX
)
63 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
65 void *bufptr
= buffer
;
66 unsigned char parent
[20];
68 if (item
->object
.parsed
)
70 item
->object
.parsed
= 1;
71 get_sha1_hex(bufptr
+ 5, parent
);
72 item
->tree
= lookup_tree(parent
);
74 add_ref(&item
->object
, &item
->tree
->object
);
75 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
76 while (!memcmp(bufptr
, "parent ", 7) &&
77 !get_sha1_hex(bufptr
+ 7, parent
)) {
78 struct commit
*new_parent
= lookup_commit(parent
);
80 commit_list_insert(new_parent
, &item
->parents
);
81 add_ref(&item
->object
, &new_parent
->object
);
85 item
->date
= parse_commit_date(bufptr
);
89 int parse_commit(struct commit
*item
)
96 if (item
->object
.parsed
)
98 buffer
= read_sha1_file(item
->object
.sha1
, type
, &size
);
100 return error("Could not read %s",
101 sha1_to_hex(item
->object
.sha1
));
102 if (strcmp(type
, commit_type
)) {
104 return error("Object %s not a commit",
105 sha1_to_hex(item
->object
.sha1
));
107 ret
= parse_commit_buffer(item
, buffer
, size
);
112 void commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
114 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
115 new_list
->item
= item
;
116 new_list
->next
= *list_p
;
120 void free_commit_list(struct commit_list
*list
)
123 struct commit_list
*temp
= list
;
129 static void insert_by_date(struct commit_list
**list
, struct commit
*item
)
131 struct commit_list
**pp
= list
;
132 struct commit_list
*p
;
133 while ((p
= *pp
) != NULL
) {
134 if (p
->item
->date
< item
->date
) {
139 commit_list_insert(item
, pp
);
143 void sort_by_date(struct commit_list
**list
)
145 struct commit_list
*ret
= NULL
;
147 insert_by_date(&ret
, (*list
)->item
);
148 *list
= (*list
)->next
;
153 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
156 struct commit
*ret
= (*list
)->item
;
157 struct commit_list
*parents
= ret
->parents
;
158 struct commit_list
*old
= *list
;
160 *list
= (*list
)->next
;
164 struct commit
*commit
= parents
->item
;
165 parse_commit(commit
);
166 if (!(commit
->object
.flags
& mark
)) {
167 commit
->object
.flags
|= mark
;
168 insert_by_date(list
, commit
);
170 parents
= parents
->next
;