]>
Commit | Line | Data |
---|---|---|
961784ee | 1 | #include "tag.h" |
175785e5 DB |
2 | #include "commit.h" |
3 | #include "cache.h" | |
4 | #include <string.h> | |
b2c00718 | 5 | #include <limits.h> |
175785e5 DB |
6 | |
7 | const char *commit_type = "commit"; | |
8 | ||
961784ee LT |
9 | static struct commit *check_commit(struct object *obj, unsigned char *sha1) |
10 | { | |
11 | if (obj->type != commit_type) { | |
12 | error("Object %s is a %s, not a commit", | |
13 | sha1_to_hex(sha1), obj->type); | |
14 | return NULL; | |
15 | } | |
16 | return (struct commit *) obj; | |
17 | } | |
18 | ||
19 | struct commit *lookup_commit_reference(unsigned char *sha1) | |
20 | { | |
21 | struct object *obj = parse_object(sha1); | |
22 | ||
23 | if (!obj) | |
24 | return NULL; | |
25 | if (obj->type == tag_type) | |
26 | obj = ((struct tag *)obj)->tagged; | |
27 | return check_commit(obj, sha1); | |
28 | } | |
29 | ||
175785e5 DB |
30 | struct commit *lookup_commit(unsigned char *sha1) |
31 | { | |
32 | struct object *obj = lookup_object(sha1); | |
33 | if (!obj) { | |
812666c8 | 34 | struct commit *ret = xmalloc(sizeof(struct commit)); |
175785e5 DB |
35 | memset(ret, 0, sizeof(struct commit)); |
36 | created_object(sha1, &ret->object); | |
d32987be | 37 | ret->object.type = commit_type; |
175785e5 DB |
38 | return ret; |
39 | } | |
d1af002d NP |
40 | if (!obj->type) |
41 | obj->type = commit_type; | |
961784ee | 42 | return check_commit(obj, sha1); |
175785e5 DB |
43 | } |
44 | ||
45 | static unsigned long parse_commit_date(const char *buf) | |
46 | { | |
47 | unsigned long date; | |
48 | ||
49 | if (memcmp(buf, "author", 6)) | |
50 | return 0; | |
51 | while (*buf++ != '\n') | |
52 | /* nada */; | |
53 | if (memcmp(buf, "committer", 9)) | |
54 | return 0; | |
55 | while (*buf++ != '>') | |
56 | /* nada */; | |
57 | date = strtoul(buf, NULL, 10); | |
58 | if (date == ULONG_MAX) | |
59 | date = 0; | |
60 | return date; | |
61 | } | |
62 | ||
bd2c39f5 | 63 | int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size) |
175785e5 | 64 | { |
bd2c39f5 | 65 | void *bufptr = buffer; |
175785e5 | 66 | unsigned char parent[20]; |
bd2c39f5 | 67 | |
175785e5 DB |
68 | if (item->object.parsed) |
69 | return 0; | |
70 | item->object.parsed = 1; | |
175785e5 DB |
71 | get_sha1_hex(bufptr + 5, parent); |
72 | item->tree = lookup_tree(parent); | |
235ac407 LT |
73 | if (item->tree) |
74 | add_ref(&item->object, &item->tree->object); | |
175785e5 DB |
75 | bufptr += 46; /* "tree " + "hex sha1" + "\n" */ |
76 | while (!memcmp(bufptr, "parent ", 7) && | |
77 | !get_sha1_hex(bufptr + 7, parent)) { | |
dd97f850 | 78 | struct commit *new_parent = lookup_commit(parent); |
235ac407 LT |
79 | if (new_parent) { |
80 | commit_list_insert(new_parent, &item->parents); | |
81 | add_ref(&item->object, &new_parent->object); | |
82 | } | |
175785e5 DB |
83 | bufptr += 48; |
84 | } | |
85 | item->date = parse_commit_date(bufptr); | |
175785e5 DB |
86 | return 0; |
87 | } | |
88 | ||
bd2c39f5 NP |
89 | int parse_commit(struct commit *item) |
90 | { | |
91 | char type[20]; | |
92 | void *buffer; | |
93 | unsigned long size; | |
94 | int ret; | |
95 | ||
96 | if (item->object.parsed) | |
97 | return 0; | |
98 | buffer = read_sha1_file(item->object.sha1, type, &size); | |
99 | if (!buffer) | |
100 | return error("Could not read %s", | |
101 | sha1_to_hex(item->object.sha1)); | |
102 | if (strcmp(type, commit_type)) { | |
103 | free(buffer); | |
104 | return error("Object %s not a commit", | |
105 | sha1_to_hex(item->object.sha1)); | |
106 | } | |
107 | ret = parse_commit_buffer(item, buffer, size); | |
108 | free(buffer); | |
109 | return ret; | |
110 | } | |
111 | ||
dd97f850 DB |
112 | void commit_list_insert(struct commit *item, struct commit_list **list_p) |
113 | { | |
812666c8 | 114 | struct commit_list *new_list = xmalloc(sizeof(struct commit_list)); |
dd97f850 DB |
115 | new_list->item = item; |
116 | new_list->next = *list_p; | |
117 | *list_p = new_list; | |
118 | } | |
119 | ||
175785e5 DB |
120 | void free_commit_list(struct commit_list *list) |
121 | { | |
122 | while (list) { | |
123 | struct commit_list *temp = list; | |
124 | list = temp->next; | |
125 | free(temp); | |
126 | } | |
127 | } | |
dd97f850 DB |
128 | |
129 | static void insert_by_date(struct commit_list **list, struct commit *item) | |
130 | { | |
131 | struct commit_list **pp = list; | |
132 | struct commit_list *p; | |
133 | while ((p = *pp) != NULL) { | |
134 | if (p->item->date < item->date) { | |
135 | break; | |
136 | } | |
137 | pp = &p->next; | |
138 | } | |
139 | commit_list_insert(item, pp); | |
140 | } | |
141 | ||
142 | ||
143 | void sort_by_date(struct commit_list **list) | |
144 | { | |
145 | struct commit_list *ret = NULL; | |
146 | while (*list) { | |
147 | insert_by_date(&ret, (*list)->item); | |
148 | *list = (*list)->next; | |
149 | } | |
150 | *list = ret; | |
151 | } | |
152 | ||
58e28af6 DB |
153 | struct commit *pop_most_recent_commit(struct commit_list **list, |
154 | unsigned int mark) | |
dd97f850 DB |
155 | { |
156 | struct commit *ret = (*list)->item; | |
157 | struct commit_list *parents = ret->parents; | |
158 | struct commit_list *old = *list; | |
159 | ||
160 | *list = (*list)->next; | |
161 | free(old); | |
162 | ||
163 | while (parents) { | |
4056c091 | 164 | struct commit *commit = parents->item; |
58e28af6 DB |
165 | parse_commit(commit); |
166 | if (!(commit->object.flags & mark)) { | |
167 | commit->object.flags |= mark; | |
4056c091 LT |
168 | insert_by_date(list, commit); |
169 | } | |
dd97f850 DB |
170 | parents = parents->next; |
171 | } | |
172 | return ret; | |
173 | } |