Commit 364788e1 by Vicent Marti

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>
parent 7e4f56a5
......@@ -161,7 +161,7 @@ git_commit *git_commit_lookup(git_revpool *pool, const git_oid *id)
return commit;
}
int git_commit__parse_person(git_commit_person *person, char **buffer_out,
int git__parse_person(git_person *person, char **buffer_out,
const char *buffer_end, const char *header)
{
const size_t header_len = strlen(header);
......@@ -220,7 +220,7 @@ int git_commit__parse_person(git_commit_person *person, char **buffer_out,
return 0;
}
int git_commit__parse_oid(git_oid *oid, char **buffer_out,
int git__parse_oid(git_oid *oid, char **buffer_out,
const char *buffer_end, const char *header)
{
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
const char *buffer_end = (char *)data + len;
git_oid oid;
git_commit_person person;
git_person person;
if (git_commit__parse_oid(&oid, &buffer, buffer_end, "tree ") < 0)
if (git__parse_oid(&oid, &buffer, buffer_end, "tree ") < 0)
return GIT_EOBJCORRUPTED;
if (parse_flags & GIT_COMMIT_TREE)
......@@ -266,7 +266,7 @@ int git_commit__parse_buffer(git_commit *commit, void *data, size_t len, unsigne
if (parse_flags & GIT_COMMIT_PARENTS)
git_commit_list_clear(&commit->parents, 0);
while (git_commit__parse_oid(&oid, &buffer, buffer_end, "parent ") == 0) {
while (git__parse_oid(&oid, &buffer, buffer_end, "parent ") == 0) {
git_commit *parent;
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
return GIT_ENOMEM;
}
if (git_commit__parse_person(&person, &buffer, buffer_end, "author ") < 0)
if (git__parse_person(&person, &buffer, buffer_end, "author ") < 0)
return GIT_EOBJCORRUPTED;
if (parse_flags & GIT_COMMIT_AUTHOR) {
if (commit->author)
free(commit->author);
commit->author = git__malloc(sizeof(git_commit_person));
memcpy(commit->author, &person, sizeof(git_commit_person));
commit->author = git__malloc(sizeof(git_person));
memcpy(commit->author, &person, sizeof(git_person));
}
if (git_commit__parse_person(&person, &buffer, buffer_end, "committer ") < 0)
if (git__parse_person(&person, &buffer, buffer_end, "committer ") < 0)
return GIT_EOBJCORRUPTED;
if (parse_flags & GIT_COMMIT_TIME)
......@@ -304,8 +304,8 @@ int git_commit__parse_buffer(git_commit *commit, void *data, size_t len, unsigne
if (commit->committer)
free(commit->committer);
commit->committer = git__malloc(sizeof(git_commit_person));
memcpy(commit->committer, &person, sizeof(git_commit_person));
commit->committer = git__malloc(sizeof(git_person));
memcpy(commit->committer, &person, sizeof(git_person));
}
/* parse commit message */
......@@ -347,7 +347,7 @@ const git_tree *git_commit_tree(git_commit *commit)
return commit->tree;
}
const git_commit_person *git_commit_author(git_commit *commit)
const git_person *git_commit_author(git_commit *commit)
{
if (commit->author)
return commit->author;
......@@ -356,7 +356,7 @@ const git_commit_person *git_commit_author(git_commit *commit)
return commit->author;
}
const git_commit_person *git_commit_committer(git_commit *commit)
const git_person *git_commit_committer(git_commit *commit)
{
if (commit->committer)
return commit->committer;
......
......@@ -40,8 +40,8 @@ struct git_commit {
git_commit_list parents;
git_tree *tree;
git_commit_person *author;
git_commit_person *committer;
git_person *author;
git_person *committer;
char *message;
char *message_short;
......@@ -58,11 +58,11 @@ struct git_commit {
void git_commit__free(git_commit *c);
int git_commit__parse(git_commit *commit, unsigned int flags, int close_odb);
int git_commit__parse_basic(git_commit *commit);
int git_commit__parse_oid(git_oid *oid, char **buffer_out, const char *buffer_end, const char *header);
int git_commit__parse_buffer(git_commit *commit, void *data, size_t len, unsigned int parse_flags);
int git_commit__parse_person(git_commit_person *person, char **buffer_out, const char *buffer_end, const char *header);
void git_commit__mark_uninteresting(git_commit *commit);
int git__parse_oid(git_oid *oid, char **buffer_out, const char *buffer_end, const char *header);
int git__parse_person(git_person *person, char **buffer_out, const char *buffer_end, const char *header);
int git_commit_list_push_back(git_commit_list *list, git_commit *commit);
int git_commit_list_push_front(git_commit_list *list, git_commit *commit);
......
......@@ -17,13 +17,6 @@ GIT_BEGIN_DECL
/** Parsed representation of a commit object. */
typedef struct git_commit git_commit;
/** Parsed representation of an author/committer of a commit */
typedef struct git_commit_person {
char name[64]; /**< Full name */
char email[64]; /**< Email address */
time_t time; /**< Time when this person commited the change */
} git_commit_person;
/**
* Locate a reference to a commit without loading it.
* The generated commit object is owned by the revision
......@@ -84,14 +77,14 @@ GIT_EXTERN(time_t) git_commit_time(git_commit *commit);
* @param commit a previously loaded commit.
* @return the committer of a commit
*/
GIT_EXTERN(const git_commit_person *) git_commit_committer(git_commit *commit);
GIT_EXTERN(const git_person *) git_commit_committer(git_commit *commit);
/**
* Get the author of a commit.
* @param commit a previously loaded commit.
* @return the author of a commit
*/
GIT_EXTERN(const git_commit_person *) git_commit_author(git_commit *commit);
GIT_EXTERN(const git_person *) git_commit_author(git_commit *commit);
/**
* Get the tree pointed to by a commit.
......
......@@ -88,6 +88,13 @@ GIT_BEGIN_DECL
/** A revision traversal pool. */
typedef struct git_revpool git_revpool;
/** Parsed representation of a person */
typedef struct git_person {
char name[64]; /**< Full name */
char email[64]; /**< Email address */
time_t time; /**< Time when this person commited the change */
} git_person;
/** @} */
GIT_END_DECL
#endif
......@@ -87,14 +87,14 @@ BEGIN_TEST(parse_oid_test)
char *ptr = string;\
char *ptr_original = ptr;\
size_t len = strlen(ptr);\
must_pass(git_commit__parse_oid(&oid, &ptr, ptr + len, header));\
must_pass(git__parse_oid(&oid, &ptr, ptr + len, header));\
must_be_true(ptr == ptr_original + len);\
}
#define TEST_OID_FAIL(string, header) { \
char *ptr = string;\
size_t len = strlen(ptr);\
must_fail(git_commit__parse_oid(&oid, &ptr, ptr + len, header));\
must_fail(git__parse_oid(&oid, &ptr, ptr + len, header));\
}
TEST_OID_PASS("parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "parent ");
......@@ -126,8 +126,8 @@ BEGIN_TEST(parse_person_test)
#define TEST_PERSON_PASS(_string, _header, _name, _email, _time) { \
char *ptr = _string; \
size_t len = strlen(_string);\
git_commit_person person; \
must_pass(git_commit__parse_person(&person, &ptr, ptr + len, _header));\
git_person person; \
must_pass(git__parse_person(&person, &ptr, ptr + len, _header));\
must_be_true(strncmp(_name, person.name, 63) == 0);\
must_be_true(strncmp(_email, person.email, 63) == 0);\
must_be_true(_time == person.time);\
......@@ -136,8 +136,8 @@ BEGIN_TEST(parse_person_test)
#define TEST_PERSON_FAIL(_string, _header) { \
char *ptr = _string; \
size_t len = strlen(_string);\
git_commit_person person; \
must_fail(git_commit__parse_person(&person, &ptr, ptr + len, _header));\
git_person person; \
must_fail(git__parse_person(&person, &ptr, ptr + len, _header));\
}
TEST_PERSON_PASS(
......
......@@ -32,7 +32,7 @@ BEGIN_TEST(query_details_test)
git_oid id;
git_commit *commit;
const git_commit_person *author, *committer;
const git_person *author, *committer;
const char *message, *message_short;
time_t commit_time;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment