]>
Commit | Line | Data |
---|---|---|
4ff192d3 | 1 | /* |
359fc2d2 | 2 | * Copyright (C) the libgit2 contributors. All rights reserved. |
4ff192d3 BS |
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 | ||
4075e060 | 14 | int git_commit_list_time_cmp(const void *a, const void *b) |
4ff192d3 | 15 | { |
4075e060 RB |
16 | const git_commit_list_node *commit_a = a; |
17 | const git_commit_list_node *commit_b = b; | |
4ff192d3 BS |
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) { | |
3736b64f | 39 | if (git_commit_list_time_cmp(p->item, item) > 0) |
4ff192d3 BS |
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 | { | |
d3416dfe | 50 | return (git_commit_list_node *)git_pool_mallocz(&walk->commit_pool, 1); |
4ff192d3 BS |
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 | ||
8842c75f VM |
103 | static int commit_quick_parse( |
104 | git_revwalk *walk, | |
105 | git_commit_list_node *commit, | |
badd85a6 | 106 | const uint8_t *buffer, |
8842c75f | 107 | size_t buffer_len) |
4ff192d3 BS |
108 | { |
109 | const size_t parent_len = strlen("parent ") + GIT_OID_HEXSZ + 1; | |
badd85a6 RB |
110 | const uint8_t *buffer_end = buffer + buffer_len; |
111 | const uint8_t *parents_start, *committer_start; | |
4ff192d3 | 112 | int i, parents = 0; |
5ffdea6f | 113 | int64_t commit_time; |
4ff192d3 BS |
114 | |
115 | buffer += strlen("tree ") + GIT_OID_HEXSZ + 1; | |
116 | ||
117 | parents_start = buffer; | |
118 | while (buffer + parent_len < buffer_end && memcmp(buffer, "parent ", strlen("parent ")) == 0) { | |
119 | parents++; | |
120 | buffer += parent_len; | |
121 | } | |
122 | ||
123 | commit->parents = alloc_parents(walk, commit, parents); | |
124 | GITERR_CHECK_ALLOC(commit->parents); | |
125 | ||
126 | buffer = parents_start; | |
127 | for (i = 0; i < parents; ++i) { | |
128 | git_oid oid; | |
129 | ||
badd85a6 | 130 | if (git_oid_fromstr(&oid, (const char *)buffer + strlen("parent ")) < 0) |
4ff192d3 BS |
131 | return -1; |
132 | ||
d5e44d84 | 133 | commit->parents[i] = git_revwalk__commit_lookup(walk, &oid); |
4ff192d3 BS |
134 | if (commit->parents[i] == NULL) |
135 | return -1; | |
136 | ||
137 | buffer += parent_len; | |
138 | } | |
139 | ||
140 | commit->out_degree = (unsigned short)parents; | |
141 | ||
142 | if ((committer_start = buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL) | |
143 | return commit_error(commit, "object is corrupted"); | |
144 | ||
145 | buffer++; | |
146 | ||
147 | if ((buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL) | |
148 | return commit_error(commit, "object is corrupted"); | |
149 | ||
150 | /* Skip trailing spaces */ | |
151 | while (buffer > committer_start && git__isspace(*buffer)) | |
152 | buffer--; | |
153 | ||
b874629b | 154 | /* Seek for the beginning of the pack of digits */ |
4ff192d3 BS |
155 | while (buffer > committer_start && git__isdigit(*buffer)) |
156 | buffer--; | |
157 | ||
158 | /* Skip potential timezone offset */ | |
159 | if ((buffer > committer_start) && (*buffer == '+' || *buffer == '-')) { | |
160 | buffer--; | |
161 | ||
162 | while (buffer > committer_start && git__isspace(*buffer)) | |
163 | buffer--; | |
164 | ||
165 | while (buffer > committer_start && git__isdigit(*buffer)) | |
166 | buffer--; | |
167 | } | |
168 | ||
5ffdea6f | 169 | if ((buffer == committer_start) || (git__strtol64(&commit_time, (char *)(buffer + 1), NULL, 10) < 0)) |
4ff192d3 BS |
170 | return commit_error(commit, "cannot parse commit time"); |
171 | ||
5ffdea6f | 172 | commit->time = commit_time; |
4ff192d3 BS |
173 | commit->parsed = 1; |
174 | return 0; | |
175 | } | |
176 | ||
177 | int git_commit_list_parse(git_revwalk *walk, git_commit_list_node *commit) | |
178 | { | |
179 | git_odb_object *obj; | |
180 | int error; | |
181 | ||
182 | if (commit->parsed) | |
183 | return 0; | |
184 | ||
185 | if ((error = git_odb_read(&obj, walk->odb, &commit->oid)) < 0) | |
186 | return error; | |
4ff192d3 | 187 | |
8842c75f | 188 | if (obj->cached.type != GIT_OBJ_COMMIT) { |
d5e44d84 RB |
189 | giterr_set(GITERR_INVALID, "Object is no commit object"); |
190 | error = -1; | |
191 | } else | |
badd85a6 RB |
192 | error = commit_quick_parse( |
193 | walk, commit, | |
194 | (const uint8_t *)git_odb_object_data(obj), | |
195 | git_odb_object_size(obj)); | |
d5e44d84 | 196 | |
4ff192d3 BS |
197 | git_odb_object_free(obj); |
198 | return error; | |
199 | } | |
200 |