parse commit timestamps when opening commits
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
diff --git a/include/got_object.h b/include/got_object.h
index 6a104bb..1e65e1d 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -40,7 +40,9 @@ struct got_commit_object {
unsigned int nparents;
SIMPLEQ_HEAD(, got_parent_id) parent_ids;
char *author;
+ time_t author_time; /* UTC */
char *committer;
+ time_t committer_time; /* UTC */
char *logmsg;
};
@@ -122,14 +124,6 @@ const struct got_error *got_object_commit_open(struct got_commit_object **,
/* Dispose of a commit object. */
void got_object_commit_close(struct got_commit_object *);
-/* Get the commit's committer timestamp (in UTC). */
-const struct got_error *got_object_commit_get_committer_time(time_t *,
- struct got_commit_object *);
-
-/* Get the commit's author timestamp (in UTC). */
-const struct got_error *got_object_commit_get_committer_time(time_t *,
- struct got_commit_object *);
-
/*
* Attempt to open a tree object in a repository.
* The provided object must be of type GOT_OBJ_TYPE_TREE.
diff --git a/lib/commit_graph.c b/lib/commit_graph.c
index 5d6edcf..2743d38 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -134,32 +134,21 @@ is_root_node(struct got_commit_graph_node *node)
return node->commit->nparents == 0;
}
-static const struct got_error *
-compare_commits(int *cmp, struct got_commit_object *c1,
- struct got_commit_object *c2)
+static int
+compare_commits(struct got_commit_object *c1, struct got_commit_object *c2)
{
- const struct got_error *err;
- int64_t t1, t2;
-
- err = got_object_commit_get_committer_time(&t1, c1);
- if (err)
- return err;
- err = got_object_commit_get_committer_time(&t2, c2);
-
- if (err)
- return err;
+ time_t t1, t2;
+ t1 = c1->committer_time;
+ t2 = c2->committer_time;
if (t1 == t2)
- *cmp = 0;
+ return 0;
else if (t1 < t2)
- *cmp = -1;
- else
- *cmp = 1;
-
- return NULL;
+ return -1;
+ return 1;
}
-static const struct got_error *
+static void
add_iteration_candidate(struct got_commit_graph *graph,
struct got_commit_graph_node *node)
{
@@ -167,15 +156,11 @@ add_iteration_candidate(struct got_commit_graph *graph,
if (TAILQ_EMPTY(&graph->iter_candidates)) {
TAILQ_INSERT_TAIL(&graph->iter_candidates, node, entry);
- return NULL;
+ return;
}
TAILQ_FOREACH(n, &graph->iter_candidates, entry) {
- const struct got_error *err;
- int cmp;
- err = compare_commits(&cmp, node->commit, n->commit);
- if (err)
- return err;
+ int cmp = compare_commits(node->commit, n->commit);
if (cmp < 0) {
next = TAILQ_NEXT(n, entry);
if (next == NULL) {
@@ -183,9 +168,7 @@ add_iteration_candidate(struct got_commit_graph *graph,
node, entry);
break;
}
- err = compare_commits(&cmp, node->commit, next->commit);
- if (err)
- return err;
+ cmp = compare_commits(node->commit, next->commit);
if (cmp >= 0) {
TAILQ_INSERT_BEFORE(next, node, entry);
break;
@@ -195,8 +178,6 @@ add_iteration_candidate(struct got_commit_graph *graph,
break;
}
}
-
- return NULL;
}
static const struct got_error *
@@ -222,10 +203,7 @@ add_node(struct got_commit_graph_node **new_node,
if (err == NULL) {
struct got_parent_id *pid;
- err = add_iteration_candidate(graph, node);
- if (err)
- return err;
-
+ add_iteration_candidate(graph, node);
err = got_object_idset_remove(graph->open_branches, commit_id);
if (err && err->code != GOT_ERR_NO_OBJ)
return err;
@@ -476,12 +454,8 @@ got_commit_graph_iter_start(struct got_commit_graph *graph,
/* Put all known parents of this commit on the candidate list. */
SIMPLEQ_FOREACH(pid, &start_node->commit->parent_ids, entry) {
node = got_object_idset_get(graph->node_ids, pid->id);
- if (node) {
- const struct got_error *err;
- err = add_iteration_candidate(graph, node);
- if (err)
- return err;
- }
+ if (node)
+ add_iteration_candidate(graph, node);
}
return NULL;
diff --git a/lib/object.c b/lib/object.c
index 679abec..71c594e 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -472,6 +472,34 @@ got_object_commit_add_parent(struct got_commit_object *commit,
}
static const struct got_error *
+parse_commit_time(time_t *time, char *committer)
+{
+ const char *errstr;
+ char *space;
+
+ *time = 0;
+
+ /* Strip off trailing timezone indicator. */
+ space = strrchr(committer, ' ');
+ if (space == NULL)
+ return got_error(GOT_ERR_BAD_OBJ_DATA);
+ *space = '\0';
+
+ /* Timestamp is separated from committer name + email by space. */
+ space = strrchr(committer, ' ');
+ if (space == NULL)
+ return got_error(GOT_ERR_BAD_OBJ_DATA);
+
+ *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
+ if (errstr)
+ return got_error(GOT_ERR_BAD_OBJ_DATA);
+
+ /* Strip off parsed time information, leaving just author and email. */
+ *space = '\0';
+ return NULL;
+}
+
+static const struct got_error *
parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
{
const struct got_error *err = NULL;
@@ -521,6 +549,7 @@ parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
char *p;
+ size_t slen;
remain -= tlen;
if (remain <= 0) {
@@ -534,18 +563,23 @@ parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
goto done;
}
*p = '\0';
+ slen = strlen(s);
+ err = parse_commit_time(&(*commit)->author_time, s);
+ if (err)
+ goto done;
(*commit)->author = strdup(s);
if ((*commit)->author == NULL) {
err = got_error_from_errno();
goto done;
}
- s += strlen((*commit)->author) + 1;
- remain -= strlen((*commit)->author) + 1;
+ s += slen + 1;
+ remain -= slen + 1;
}
tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
char *p;
+ size_t slen;
remain -= tlen;
if (remain <= 0) {
@@ -559,13 +593,17 @@ parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
goto done;
}
*p = '\0';
+ slen = strlen(s);
+ err = parse_commit_time(&(*commit)->committer_time, s);
+ if (err)
+ goto done;
(*commit)->committer = strdup(s);
if ((*commit)->committer == NULL) {
err = got_error_from_errno();
goto done;
}
- s += strlen((*commit)->committer) + 1;
- remain -= strlen((*commit)->committer) + 1;
+ s += slen + 1;
+ remain -= slen + 1;
}
(*commit)->logmsg = strndup(s, remain);
@@ -581,57 +619,6 @@ done:
return err;
}
-static const struct got_error *
-parse_commit_time(time_t *time, const char *author_str)
-{
- const struct got_error *err = NULL;
- const char *errstr;
- char *committer, *space;
-
- *time = 0;
-
- committer = strdup(author_str);
- if (committer == NULL)
- return got_error_from_errno();
-
- /* Strip off trailing timezone indicator. */
- space = strrchr(committer, ' ');
- if (space == NULL) {
- err = got_error(GOT_ERR_BAD_OBJ_DATA);
- goto done;
- }
- *space = '\0';
-
- /* Timestamp is separated from committer name + email by space. */
- space = strrchr(committer, ' ');
- if (space == NULL) {
- err = got_error(GOT_ERR_BAD_OBJ_DATA);
- goto done;
- }
-
- *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
- if (errstr)
- err = got_error(GOT_ERR_BAD_OBJ_DATA);
-
-done:
- free(committer);
- return err;
-}
-
-const struct got_error *
-got_object_commit_get_committer_time(time_t *time,
- struct got_commit_object *commit)
-{
- return parse_commit_time(time, commit->committer);
-}
-
-const struct got_error *
-got_object_commit_get_author_time(time_t *time,
- struct got_commit_object *commit)
-{
- return parse_commit_time(time, commit->committer);
-}
-
static void
tree_entry_close(struct got_tree_entry *te)
{