Skip to content

Commit 5988cf3

Browse files
tiennoupks-t
authored andcommitted
commit_list: unify commit information parsing
1 parent 1c847a6 commit 5988cf3

File tree

1 file changed

+23
-77
lines changed

1 file changed

+23
-77
lines changed

src/commit_list.c

Lines changed: 23 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "revwalk.h"
1111
#include "pool.h"
1212
#include "odb.h"
13+
#include "commit.h"
1314

1415
int git_commit_list_time_cmp(const void *a, const void *b)
1516
{
@@ -55,17 +56,6 @@ git_commit_list_node *git_commit_list_alloc_node(git_revwalk *walk)
5556
return (git_commit_list_node *)git_pool_mallocz(&walk->commit_pool, 1);
5657
}
5758

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-
git_error_set(GIT_ERROR_ODB, "failed to parse commit %s - %s", commit_oid, msg);
65-
66-
return -1;
67-
}
68-
6959
static git_commit_list_node **alloc_parents(
7060
git_revwalk *walk, git_commit_list_node *commit, size_t n_parents)
7161
{
@@ -107,77 +97,36 @@ git_commit_list_node *git_commit_list_pop(git_commit_list **stack)
10797

10898
static int commit_quick_parse(
10999
git_revwalk *walk,
110-
git_commit_list_node *commit,
111-
const uint8_t *buffer,
112-
size_t buffer_len)
100+
git_commit_list_node *node,
101+
git_odb_object *obj)
113102
{
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-
GIT_ERROR_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;
103+
git_oid *parent_oid;
104+
git_commit *commit;
105+
int error;
106+
size_t i;
137107

138-
commit->parents[i] = git_revwalk__commit_lookup(walk, &oid);
139-
if (commit->parents[i] == NULL)
140-
return -1;
108+
commit = git__calloc(1, sizeof(*commit));
109+
GIT_ERROR_CHECK_ALLOC(commit);
110+
commit->object.repo = walk->repo;
141111

142-
buffer += parent_len;
112+
if ((error = git_commit__parse_ext(commit, obj, GIT_COMMIT_PARSE_QUICK)) < 0) {
113+
git__free(commit);
114+
return error;
143115
}
144116

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--;
117+
node->time = commit->committer->when.time;
118+
node->out_degree = git_array_size(commit->parent_ids);
119+
node->parents = alloc_parents(walk, node, node->out_degree);
120+
GIT_ERROR_CHECK_ALLOC(node->parents);
158121

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--;
122+
git_array_foreach(commit->parent_ids, i, parent_oid) {
123+
node->parents[i] = git_revwalk__commit_lookup(walk, parent_oid);
172124
}
173125

174-
if ((buffer == committer_start) ||
175-
(git__strntol64(&commit_time, (char *)(buffer + 1),
176-
buffer_end - buffer + 1, NULL, 10) < 0))
177-
return commit_error(commit, "cannot parse commit time");
126+
git_commit__free(commit);
127+
128+
node->parsed = 1;
178129

179-
commit->time = commit_time;
180-
commit->parsed = 1;
181130
return 0;
182131
}
183132

@@ -196,10 +145,7 @@ int git_commit_list_parse(git_revwalk *walk, git_commit_list_node *commit)
196145
git_error_set(GIT_ERROR_INVALID, "object is no commit object");
197146
error = -1;
198147
} else
199-
error = commit_quick_parse(
200-
walk, commit,
201-
(const uint8_t *)git_odb_object_data(obj),
202-
git_odb_object_size(obj));
148+
error = commit_quick_parse(walk, commit, obj);
203149

204150
git_odb_object_free(obj);
205151
return error;

0 commit comments

Comments
 (0)