]> git.proxmox.com Git - libgit2.git/blob - src/commit_list.c
Merge remote-tracking branch 'origin/pr/3790' into win32_posix
[libgit2.git] / src / commit_list.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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(const void *a, const void *b)
15 {
16 int64_t time_a = ((git_commit_list_node *) a)->time;
17 int64_t time_b = ((git_commit_list_node *) b)->time;
18
19 if (time_a < time_b)
20 return 1;
21 if (time_a > time_b)
22 return -1;
23
24 return 0;
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) {
44 if (git_commit_list_time_cmp(p->item, item) > 0)
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 {
55 return (git_commit_list_node *)git_pool_mallocz(&walk->commit_pool, 1);
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
64 giterr_set(GITERR_ODB, "failed to parse commit %s - %s", commit_oid, msg);
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
108 static int commit_quick_parse(
109 git_revwalk *walk,
110 git_commit_list_node *commit,
111 const uint8_t *buffer,
112 size_t buffer_len)
113 {
114 const size_t parent_len = strlen("parent ") + GIT_OID_HEXSZ + 1;
115 const uint8_t *buffer_end = buffer + buffer_len;
116 const uint8_t *parents_start, *committer_start;
117 int i, parents = 0;
118 int64_t commit_time;
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
135 if (git_oid_fromstr(&oid, (const char *)buffer + strlen("parent ")) < 0)
136 return -1;
137
138 commit->parents[i] = git_revwalk__commit_lookup(walk, &oid);
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
159 /* Seek for the beginning of the pack of digits */
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
174 if ((buffer == committer_start) || (git__strtol64(&commit_time, (char *)(buffer + 1), NULL, 10) < 0))
175 return commit_error(commit, "cannot parse commit time");
176
177 commit->time = commit_time;
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;
192
193 if (obj->cached.type != GIT_OBJ_COMMIT) {
194 giterr_set(GITERR_INVALID, "object is no commit object");
195 error = -1;
196 } else
197 error = commit_quick_parse(
198 walk, commit,
199 (const uint8_t *)git_odb_object_data(obj),
200 git_odb_object_size(obj));
201
202 git_odb_object_free(obj);
203 return error;
204 }
205