]>
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 | { |
5e2a29a7 CMN |
16 | int64_t time_a = ((git_commit_list_node *) a)->time; |
17 | int64_t time_b = ((git_commit_list_node *) b)->time; | |
4ff192d3 | 18 | |
5e2a29a7 CMN |
19 | if (time_a < time_b) |
20 | return 1; | |
21 | if (time_a > time_b) | |
22 | return -1; | |
23 | ||
24 | return 0; | |
4ff192d3 BS |
25 | } |
26 | ||
27 | git_commit_list *git_commit_list_insert(git_commit_list_node *item, git_commit_list **list_p) | |
28 | { | |
29 | git_commit_list *new_list = git__malloc(sizeof(git_commit_list)); | |
30 | if (new_list != NULL) { | |
31 | new_list->item = item; | |
32 | new_list->next = *list_p; | |
33 | } | |
34 | *list_p = new_list; | |
35 | return new_list; | |
36 | } | |
37 | ||
38 | git_commit_list *git_commit_list_insert_by_date(git_commit_list_node *item, git_commit_list **list_p) | |
39 | { | |
40 | git_commit_list **pp = list_p; | |
41 | git_commit_list *p; | |
42 | ||
43 | while ((p = *pp) != NULL) { | |
3736b64f | 44 | if (git_commit_list_time_cmp(p->item, item) > 0) |
4ff192d3 BS |
45 | break; |
46 | ||
47 | pp = &p->next; | |
48 | } | |
49 | ||
50 | return git_commit_list_insert(item, pp); | |
51 | } | |
52 | ||
53 | git_commit_list_node *git_commit_list_alloc_node(git_revwalk *walk) | |
54 | { | |
d3416dfe | 55 | return (git_commit_list_node *)git_pool_mallocz(&walk->commit_pool, 1); |
4ff192d3 BS |
56 | } |
57 | ||
58 | static int commit_error(git_commit_list_node *commit, const char *msg) | |
59 | { | |
60 | char commit_oid[GIT_OID_HEXSZ + 1]; | |
61 | git_oid_fmt(commit_oid, &commit->oid); | |
62 | commit_oid[GIT_OID_HEXSZ] = '\0'; | |
63 | ||
909d5494 | 64 | giterr_set(GITERR_ODB, "failed to parse commit %s - %s", commit_oid, msg); |
4ff192d3 BS |
65 | |
66 | return -1; | |
67 | } | |
68 | ||
69 | static git_commit_list_node **alloc_parents( | |
70 | git_revwalk *walk, git_commit_list_node *commit, size_t n_parents) | |
71 | { | |
72 | if (n_parents <= PARENTS_PER_COMMIT) | |
73 | return (git_commit_list_node **)((char *)commit + sizeof(git_commit_list_node)); | |
74 | ||
75 | return (git_commit_list_node **)git_pool_malloc( | |
76 | &walk->commit_pool, (uint32_t)(n_parents * sizeof(git_commit_list_node *))); | |
77 | } | |
78 | ||
79 | ||
80 | void git_commit_list_free(git_commit_list **list_p) | |
81 | { | |
82 | git_commit_list *list = *list_p; | |
83 | ||
84 | if (list == NULL) | |
85 | return; | |
86 | ||
87 | while (list) { | |
88 | git_commit_list *temp = list; | |
89 | list = temp->next; | |
90 | git__free(temp); | |
91 | } | |
92 | ||
93 | *list_p = NULL; | |
94 | } | |
95 | ||
96 | git_commit_list_node *git_commit_list_pop(git_commit_list **stack) | |
97 | { | |
98 | git_commit_list *top = *stack; | |
99 | git_commit_list_node *item = top ? top->item : NULL; | |
100 | ||
101 | if (top) { | |
102 | *stack = top->next; | |
103 | git__free(top); | |
104 | } | |
105 | return item; | |
106 | } | |
107 | ||
8842c75f VM |
108 | static int commit_quick_parse( |
109 | git_revwalk *walk, | |
110 | git_commit_list_node *commit, | |
badd85a6 | 111 | const uint8_t *buffer, |
8842c75f | 112 | size_t buffer_len) |
4ff192d3 BS |
113 | { |
114 | const size_t parent_len = strlen("parent ") + GIT_OID_HEXSZ + 1; | |
badd85a6 RB |
115 | const uint8_t *buffer_end = buffer + buffer_len; |
116 | const uint8_t *parents_start, *committer_start; | |
4ff192d3 | 117 | int i, parents = 0; |
5ffdea6f | 118 | int64_t commit_time; |
4ff192d3 BS |
119 | |
120 | buffer += strlen("tree ") + GIT_OID_HEXSZ + 1; | |
121 | ||
122 | parents_start = buffer; | |
123 | while (buffer + parent_len < buffer_end && memcmp(buffer, "parent ", strlen("parent ")) == 0) { | |
124 | parents++; | |
125 | buffer += parent_len; | |
126 | } | |
127 | ||
128 | commit->parents = alloc_parents(walk, commit, parents); | |
129 | GITERR_CHECK_ALLOC(commit->parents); | |
130 | ||
131 | buffer = parents_start; | |
132 | for (i = 0; i < parents; ++i) { | |
133 | git_oid oid; | |
134 | ||
badd85a6 | 135 | if (git_oid_fromstr(&oid, (const char *)buffer + strlen("parent ")) < 0) |
4ff192d3 BS |
136 | return -1; |
137 | ||
d5e44d84 | 138 | commit->parents[i] = git_revwalk__commit_lookup(walk, &oid); |
4ff192d3 BS |
139 | if (commit->parents[i] == NULL) |
140 | return -1; | |
141 | ||
142 | buffer += parent_len; | |
143 | } | |
144 | ||
145 | commit->out_degree = (unsigned short)parents; | |
146 | ||
147 | if ((committer_start = buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL) | |
148 | return commit_error(commit, "object is corrupted"); | |
149 | ||
150 | buffer++; | |
151 | ||
152 | if ((buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL) | |
153 | return commit_error(commit, "object is corrupted"); | |
154 | ||
155 | /* Skip trailing spaces */ | |
156 | while (buffer > committer_start && git__isspace(*buffer)) | |
157 | buffer--; | |
158 | ||
b874629b | 159 | /* Seek for the beginning of the pack of digits */ |
4ff192d3 BS |
160 | while (buffer > committer_start && git__isdigit(*buffer)) |
161 | buffer--; | |
162 | ||
163 | /* Skip potential timezone offset */ | |
164 | if ((buffer > committer_start) && (*buffer == '+' || *buffer == '-')) { | |
165 | buffer--; | |
166 | ||
167 | while (buffer > committer_start && git__isspace(*buffer)) | |
168 | buffer--; | |
169 | ||
170 | while (buffer > committer_start && git__isdigit(*buffer)) | |
171 | buffer--; | |
172 | } | |
173 | ||
5ffdea6f | 174 | if ((buffer == committer_start) || (git__strtol64(&commit_time, (char *)(buffer + 1), NULL, 10) < 0)) |
4ff192d3 BS |
175 | return commit_error(commit, "cannot parse commit time"); |
176 | ||
5ffdea6f | 177 | commit->time = commit_time; |
4ff192d3 BS |
178 | commit->parsed = 1; |
179 | return 0; | |
180 | } | |
181 | ||
182 | int git_commit_list_parse(git_revwalk *walk, git_commit_list_node *commit) | |
183 | { | |
184 | git_odb_object *obj; | |
185 | int error; | |
186 | ||
187 | if (commit->parsed) | |
188 | return 0; | |
189 | ||
190 | if ((error = git_odb_read(&obj, walk->odb, &commit->oid)) < 0) | |
191 | return error; | |
4ff192d3 | 192 | |
8842c75f | 193 | if (obj->cached.type != GIT_OBJ_COMMIT) { |
909d5494 | 194 | giterr_set(GITERR_INVALID, "object is no commit object"); |
d5e44d84 RB |
195 | error = -1; |
196 | } else | |
badd85a6 RB |
197 | error = commit_quick_parse( |
198 | walk, commit, | |
199 | (const uint8_t *)git_odb_object_data(obj), | |
200 | git_odb_object_size(obj)); | |
d5e44d84 | 201 | |
4ff192d3 BS |
202 | git_odb_object_free(obj); |
203 | return error; | |
204 | } | |
205 |