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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
diff --git a/src/commit.c b/src/commit.c
index e4e5611..4199e8e 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -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;
diff --git a/src/commit.h b/src/commit.h
index 36fd65c..028c837 100644
--- a/src/commit.h
+++ b/src/commit.h
@@ -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);
diff --git a/src/git/commit.h b/src/git/commit.h
index 6d34f01..89f0929 100644
--- a/src/git/commit.h
+++ b/src/git/commit.h
@@ -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.
diff --git a/src/git/common.h b/src/git/common.h
index 2506dae..09972aa 100644
--- a/src/git/common.h
+++ b/src/git/common.h
@@ -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
diff --git a/tests/t0401-parse.c b/tests/t0401-parse.c
index 3c6a4c0..f301182 100644
--- a/tests/t0401-parse.c
+++ b/tests/t0401-parse.c
@@ -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(
diff --git a/tests/t0402-details.c b/tests/t0402-details.c
index e412d5a..df4a074 100644
--- a/tests/t0402-details.c
+++ b/tests/t0402-details.c
@@ -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;