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