Skip to content

Commit 364788e

Browse files
author
Vicent Marti
committed
Refactor parsing methods
The 'parse_oid' and 'parse_person' methods which were used by the commit parser are now global so they can be used when parsing other objects. The 'git_commit_person' struct has been changed to a generic 'git_person'. Signed-off-by: Vicent Marti <tanoku@gmail.com>
1 parent 7e4f56a commit 364788e

File tree

6 files changed

+33
-33
lines changed

6 files changed

+33
-33
lines changed

src/commit.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ git_commit *git_commit_lookup(git_revpool *pool, const git_oid *id)
161161
return commit;
162162
}
163163

164-
int git_commit__parse_person(git_commit_person *person, char **buffer_out,
164+
int git__parse_person(git_person *person, char **buffer_out,
165165
const char *buffer_end, const char *header)
166166
{
167167
const size_t header_len = strlen(header);
@@ -220,7 +220,7 @@ int git_commit__parse_person(git_commit_person *person, char **buffer_out,
220220
return 0;
221221
}
222222

223-
int git_commit__parse_oid(git_oid *oid, char **buffer_out,
223+
int git__parse_oid(git_oid *oid, char **buffer_out,
224224
const char *buffer_end, const char *header)
225225
{
226226
const size_t sha_len = GIT_OID_HEXSZ;
@@ -251,9 +251,9 @@ int git_commit__parse_buffer(git_commit *commit, void *data, size_t len, unsigne
251251
const char *buffer_end = (char *)data + len;
252252

253253
git_oid oid;
254-
git_commit_person person;
254+
git_person person;
255255

256-
if (git_commit__parse_oid(&oid, &buffer, buffer_end, "tree ") < 0)
256+
if (git__parse_oid(&oid, &buffer, buffer_end, "tree ") < 0)
257257
return GIT_EOBJCORRUPTED;
258258

259259
if (parse_flags & GIT_COMMIT_TREE)
@@ -266,7 +266,7 @@ int git_commit__parse_buffer(git_commit *commit, void *data, size_t len, unsigne
266266
if (parse_flags & GIT_COMMIT_PARENTS)
267267
git_commit_list_clear(&commit->parents, 0);
268268

269-
while (git_commit__parse_oid(&oid, &buffer, buffer_end, "parent ") == 0) {
269+
while (git__parse_oid(&oid, &buffer, buffer_end, "parent ") == 0) {
270270
git_commit *parent;
271271

272272
if ((parse_flags & GIT_COMMIT_PARENTS) == 0)
@@ -283,18 +283,18 @@ int git_commit__parse_buffer(git_commit *commit, void *data, size_t len, unsigne
283283
return GIT_ENOMEM;
284284
}
285285

286-
if (git_commit__parse_person(&person, &buffer, buffer_end, "author ") < 0)
286+
if (git__parse_person(&person, &buffer, buffer_end, "author ") < 0)
287287
return GIT_EOBJCORRUPTED;
288288

289289
if (parse_flags & GIT_COMMIT_AUTHOR) {
290290
if (commit->author)
291291
free(commit->author);
292292

293-
commit->author = git__malloc(sizeof(git_commit_person));
294-
memcpy(commit->author, &person, sizeof(git_commit_person));
293+
commit->author = git__malloc(sizeof(git_person));
294+
memcpy(commit->author, &person, sizeof(git_person));
295295
}
296296

297-
if (git_commit__parse_person(&person, &buffer, buffer_end, "committer ") < 0)
297+
if (git__parse_person(&person, &buffer, buffer_end, "committer ") < 0)
298298
return GIT_EOBJCORRUPTED;
299299

300300
if (parse_flags & GIT_COMMIT_TIME)
@@ -304,8 +304,8 @@ int git_commit__parse_buffer(git_commit *commit, void *data, size_t len, unsigne
304304
if (commit->committer)
305305
free(commit->committer);
306306

307-
commit->committer = git__malloc(sizeof(git_commit_person));
308-
memcpy(commit->committer, &person, sizeof(git_commit_person));
307+
commit->committer = git__malloc(sizeof(git_person));
308+
memcpy(commit->committer, &person, sizeof(git_person));
309309
}
310310

311311
/* parse commit message */
@@ -347,7 +347,7 @@ const git_tree *git_commit_tree(git_commit *commit)
347347
return commit->tree;
348348
}
349349

350-
const git_commit_person *git_commit_author(git_commit *commit)
350+
const git_person *git_commit_author(git_commit *commit)
351351
{
352352
if (commit->author)
353353
return commit->author;
@@ -356,7 +356,7 @@ const git_commit_person *git_commit_author(git_commit *commit)
356356
return commit->author;
357357
}
358358

359-
const git_commit_person *git_commit_committer(git_commit *commit)
359+
const git_person *git_commit_committer(git_commit *commit)
360360
{
361361
if (commit->committer)
362362
return commit->committer;

src/commit.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ struct git_commit {
4040
git_commit_list parents;
4141

4242
git_tree *tree;
43-
git_commit_person *author;
44-
git_commit_person *committer;
43+
git_person *author;
44+
git_person *committer;
4545

4646
char *message;
4747
char *message_short;
@@ -58,11 +58,11 @@ struct git_commit {
5858
void git_commit__free(git_commit *c);
5959
int git_commit__parse(git_commit *commit, unsigned int flags, int close_odb);
6060
int git_commit__parse_basic(git_commit *commit);
61-
int git_commit__parse_oid(git_oid *oid, char **buffer_out, const char *buffer_end, const char *header);
6261
int git_commit__parse_buffer(git_commit *commit, void *data, size_t len, unsigned int parse_flags);
63-
int git_commit__parse_person(git_commit_person *person, char **buffer_out, const char *buffer_end, const char *header);
6462
void git_commit__mark_uninteresting(git_commit *commit);
6563

64+
int git__parse_oid(git_oid *oid, char **buffer_out, const char *buffer_end, const char *header);
65+
int git__parse_person(git_person *person, char **buffer_out, const char *buffer_end, const char *header);
6666

6767
int git_commit_list_push_back(git_commit_list *list, git_commit *commit);
6868
int git_commit_list_push_front(git_commit_list *list, git_commit *commit);

src/git/commit.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ GIT_BEGIN_DECL
1717
/** Parsed representation of a commit object. */
1818
typedef struct git_commit git_commit;
1919

20-
/** Parsed representation of an author/committer of a commit */
21-
typedef struct git_commit_person {
22-
char name[64]; /**< Full name */
23-
char email[64]; /**< Email address */
24-
time_t time; /**< Time when this person commited the change */
25-
} git_commit_person;
26-
2720
/**
2821
* Locate a reference to a commit without loading it.
2922
* The generated commit object is owned by the revision
@@ -84,14 +77,14 @@ GIT_EXTERN(time_t) git_commit_time(git_commit *commit);
8477
* @param commit a previously loaded commit.
8578
* @return the committer of a commit
8679
*/
87-
GIT_EXTERN(const git_commit_person *) git_commit_committer(git_commit *commit);
80+
GIT_EXTERN(const git_person *) git_commit_committer(git_commit *commit);
8881

8982
/**
9083
* Get the author of a commit.
9184
* @param commit a previously loaded commit.
9285
* @return the author of a commit
9386
*/
94-
GIT_EXTERN(const git_commit_person *) git_commit_author(git_commit *commit);
87+
GIT_EXTERN(const git_person *) git_commit_author(git_commit *commit);
9588

9689
/**
9790
* Get the tree pointed to by a commit.

src/git/common.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ GIT_BEGIN_DECL
8888
/** A revision traversal pool. */
8989
typedef struct git_revpool git_revpool;
9090

91+
/** Parsed representation of a person */
92+
typedef struct git_person {
93+
char name[64]; /**< Full name */
94+
char email[64]; /**< Email address */
95+
time_t time; /**< Time when this person commited the change */
96+
} git_person;
97+
9198
/** @} */
9299
GIT_END_DECL
93100
#endif

tests/t0401-parse.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ BEGIN_TEST(parse_oid_test)
8787
char *ptr = string;\
8888
char *ptr_original = ptr;\
8989
size_t len = strlen(ptr);\
90-
must_pass(git_commit__parse_oid(&oid, &ptr, ptr + len, header));\
90+
must_pass(git__parse_oid(&oid, &ptr, ptr + len, header));\
9191
must_be_true(ptr == ptr_original + len);\
9292
}
9393

9494
#define TEST_OID_FAIL(string, header) { \
9595
char *ptr = string;\
9696
size_t len = strlen(ptr);\
97-
must_fail(git_commit__parse_oid(&oid, &ptr, ptr + len, header));\
97+
must_fail(git__parse_oid(&oid, &ptr, ptr + len, header));\
9898
}
9999

100100
TEST_OID_PASS("parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "parent ");
@@ -126,8 +126,8 @@ BEGIN_TEST(parse_person_test)
126126
#define TEST_PERSON_PASS(_string, _header, _name, _email, _time) { \
127127
char *ptr = _string; \
128128
size_t len = strlen(_string);\
129-
git_commit_person person; \
130-
must_pass(git_commit__parse_person(&person, &ptr, ptr + len, _header));\
129+
git_person person; \
130+
must_pass(git__parse_person(&person, &ptr, ptr + len, _header));\
131131
must_be_true(strncmp(_name, person.name, 63) == 0);\
132132
must_be_true(strncmp(_email, person.email, 63) == 0);\
133133
must_be_true(_time == person.time);\
@@ -136,8 +136,8 @@ BEGIN_TEST(parse_person_test)
136136
#define TEST_PERSON_FAIL(_string, _header) { \
137137
char *ptr = _string; \
138138
size_t len = strlen(_string);\
139-
git_commit_person person; \
140-
must_fail(git_commit__parse_person(&person, &ptr, ptr + len, _header));\
139+
git_person person; \
140+
must_fail(git__parse_person(&person, &ptr, ptr + len, _header));\
141141
}
142142

143143
TEST_PERSON_PASS(

tests/t0402-details.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ BEGIN_TEST(query_details_test)
3232
git_oid id;
3333
git_commit *commit;
3434

35-
const git_commit_person *author, *committer;
35+
const git_person *author, *committer;
3636
const char *message, *message_short;
3737
time_t commit_time;
3838

0 commit comments

Comments
 (0)