Commit 6e790f45d64975dc0b774d672fa3577dfcfafdc9

Stefan Sperling 2018-06-11T00:50:26

move parse_commit_time() to object.c and make public API

diff --git a/include/got_object.h b/include/got_object.h
index 54d808e..6a104bb 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -122,6 +122,14 @@ 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 7dd5b8b..5d6edcf 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -135,53 +135,17 @@ is_root_node(struct got_commit_graph_node *node)
 }
 
 static const struct got_error *
-parse_commit_time(int64_t *time, struct got_commit_object *commit)
-{
-	const struct got_error *err = NULL;
-	const char *errstr;
-	char *committer, *space;
-
-	*time = 0;
-
-	committer = strdup(commit->committer);
-	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;
-}
-
-static const struct got_error *
 compare_commits(int *cmp, struct got_commit_object *c1,
     struct got_commit_object *c2)
 {
 	const struct got_error *err;
 	int64_t t1, t2;
 
-	err = parse_commit_time(&t1, c1);
+	err = got_object_commit_get_committer_time(&t1, c1);
 	if (err)
 		return err;
-	err = parse_commit_time(&t2, c2);
+	err = got_object_commit_get_committer_time(&t2, c2);
+
 	if (err)
 		return err;
 
diff --git a/lib/object.c b/lib/object.c
index fd3ef97..679abec 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -581,6 +581,57 @@ 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)
 {