]>
Commit | Line | Data |
---|---|---|
4ff192d3 BS |
1 | /* |
2 | * Copyright (C) 2009-2012 the libgit2 contributors | |
3 | * | |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with | |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
6 | */ | |
7 | ||
8 | #include "commit_list.h" | |
9 | #include "common.h" | |
10 | #include "revwalk.h" | |
11 | #include "pool.h" | |
12 | #include "odb.h" | |
13 | ||
14 | int git_commit_list_time_cmp(void *a, void *b) | |
15 | { | |
16 | git_commit_list_node *commit_a = (git_commit_list_node *)a; | |
17 | git_commit_list_node *commit_b = (git_commit_list_node *)b; | |
18 | ||
19 | return (commit_a->time < commit_b->time); | |
20 | } | |
21 | ||
22 | git_commit_list *git_commit_list_insert(git_commit_list_node *item, git_commit_list **list_p) | |
23 | { | |
24 | git_commit_list *new_list = git__malloc(sizeof(git_commit_list)); | |
25 | if (new_list != NULL) { | |
26 | new_list->item = item; | |
27 | new_list->next = *list_p; | |
28 | } | |
29 | *list_p = new_list; | |
30 | return new_list; | |
31 | } | |
32 | ||
33 | git_commit_list *git_commit_list_insert_by_date(git_commit_list_node *item, git_commit_list **list_p) | |
34 | { | |
35 | git_commit_list **pp = list_p; | |
36 | git_commit_list *p; | |
37 | ||
38 | while ((p = *pp) != NULL) { | |
39 | if (git_commit_list_time_cmp(p->item, item) < 0) | |
40 | break; | |
41 | ||
42 | pp = &p->next; | |
43 | } | |
44 | ||
45 | return git_commit_list_insert(item, pp); | |
46 | } | |
47 | ||
48 | git_commit_list_node *git_commit_list_alloc_node(git_revwalk *walk) | |
49 | { | |
50 | return (git_commit_list_node *)git_pool_malloc(&walk->commit_pool, COMMIT_ALLOC); | |
51 | } | |
52 | ||
53 | static int commit_error(git_commit_list_node *commit, const char *msg) | |
54 | { | |
55 | char commit_oid[GIT_OID_HEXSZ + 1]; | |
56 | git_oid_fmt(commit_oid, &commit->oid); | |
57 | commit_oid[GIT_OID_HEXSZ] = '\0'; | |
58 | ||
59 | giterr_set(GITERR_ODB, "Failed to parse commit %s - %s", commit_oid, msg); | |
60 | ||
61 | return -1; | |
62 | } | |
63 | ||
64 | static git_commit_list_node **alloc_parents( | |
65 | git_revwalk *walk, git_commit_list_node *commit, size_t n_parents) | |
66 | { | |
67 | if (n_parents <= PARENTS_PER_COMMIT) | |
68 | return (git_commit_list_node **)((char *)commit + sizeof(git_commit_list_node)); | |
69 | ||
70 | return (git_commit_list_node **)git_pool_malloc( | |
71 | &walk->commit_pool, (uint32_t)(n_parents * sizeof(git_commit_list_node *))); | |
72 | } | |
73 | ||
74 | ||
75 | void git_commit_list_free(git_commit_list **list_p) | |
76 | { | |
77 | git_commit_list *list = *list_p; | |
78 | ||
79 | if (list == NULL) | |
80 | return; | |
81 | ||
82 | while (list) { | |
83 | git_commit_list *temp = list; | |
84 | list = temp->next; | |
85 | git__free(temp); | |
86 | } | |
87 | ||
88 | *list_p = NULL; | |
89 | } | |
90 | ||
91 | git_commit_list_node *git_commit_list_pop(git_commit_list **stack) | |
92 | { | |
93 | git_commit_list *top = *stack; | |
94 | git_commit_list_node *item = top ? top->item : NULL; | |
95 | ||
96 | if (top) { | |
97 | *stack = top->next; | |
98 | git__free(top); | |
99 | } | |
100 | return item; | |
101 | } | |
102 | ||
103 | static int commit_quick_parse(git_revwalk *walk, git_commit_list_node *commit, git_rawobj *raw) | |
104 | { | |
105 | const size_t parent_len = strlen("parent ") + GIT_OID_HEXSZ + 1; | |
106 | unsigned char *buffer = raw->data; | |
107 | unsigned char *buffer_end = buffer + raw->len; | |
108 | unsigned char *parents_start, *committer_start; | |
109 | int i, parents = 0; | |
110 | int commit_time; | |
111 | ||
112 | buffer += strlen("tree ") + GIT_OID_HEXSZ + 1; | |
113 | ||
114 | parents_start = buffer; | |
115 | while (buffer + parent_len < buffer_end && memcmp(buffer, "parent ", strlen("parent ")) == 0) { | |
116 | parents++; | |
117 | buffer += parent_len; | |
118 | } | |
119 | ||
120 | commit->parents = alloc_parents(walk, commit, parents); | |
121 | GITERR_CHECK_ALLOC(commit->parents); | |
122 | ||
123 | buffer = parents_start; | |
124 | for (i = 0; i < parents; ++i) { | |
125 | git_oid oid; | |
126 | ||
127 | if (git_oid_fromstr(&oid, (char *)buffer + strlen("parent ")) < 0) | |
128 | return -1; | |
129 | ||
d5e44d84 | 130 | commit->parents[i] = git_revwalk__commit_lookup(walk, &oid); |
4ff192d3 BS |
131 | if (commit->parents[i] == NULL) |
132 | return -1; | |
133 | ||
134 | buffer += parent_len; | |
135 | } | |
136 | ||
137 | commit->out_degree = (unsigned short)parents; | |
138 | ||
139 | if ((committer_start = buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL) | |
140 | return commit_error(commit, "object is corrupted"); | |
141 | ||
142 | buffer++; | |
143 | ||
144 | if ((buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL) | |
145 | return commit_error(commit, "object is corrupted"); | |
146 | ||
147 | /* Skip trailing spaces */ | |
148 | while (buffer > committer_start && git__isspace(*buffer)) | |
149 | buffer--; | |
150 | ||
151 | /* Seek for the begining of the pack of digits */ | |
152 | while (buffer > committer_start && git__isdigit(*buffer)) | |
153 | buffer--; | |
154 | ||
155 | /* Skip potential timezone offset */ | |
156 | if ((buffer > committer_start) && (*buffer == '+' || *buffer == '-')) { | |
157 | buffer--; | |
158 | ||
159 | while (buffer > committer_start && git__isspace(*buffer)) | |
160 | buffer--; | |
161 | ||
162 | while (buffer > committer_start && git__isdigit(*buffer)) | |
163 | buffer--; | |
164 | } | |
165 | ||
166 | if ((buffer == committer_start) || (git__strtol32(&commit_time, (char *)(buffer + 1), NULL, 10) < 0)) | |
167 | return commit_error(commit, "cannot parse commit time"); | |
168 | ||
169 | commit->time = (time_t)commit_time; | |
170 | commit->parsed = 1; | |
171 | return 0; | |
172 | } | |
173 | ||
174 | int git_commit_list_parse(git_revwalk *walk, git_commit_list_node *commit) | |
175 | { | |
176 | git_odb_object *obj; | |
177 | int error; | |
178 | ||
179 | if (commit->parsed) | |
180 | return 0; | |
181 | ||
182 | if ((error = git_odb_read(&obj, walk->odb, &commit->oid)) < 0) | |
183 | return error; | |
4ff192d3 | 184 | |
bdf3e6df | 185 | if (obj->raw.type != GIT_OBJ_COMMIT) { |
d5e44d84 RB |
186 | giterr_set(GITERR_INVALID, "Object is no commit object"); |
187 | error = -1; | |
188 | } else | |
189 | error = commit_quick_parse(walk, commit, &obj->raw); | |
190 | ||
4ff192d3 BS |
191 | git_odb_object_free(obj); |
192 | return error; | |
193 | } | |
194 |