Commit d1f166363b86f21241be730505c1182508dc9d2c

Stefan Sperling 2020-01-15T22:05:49

move got_repo_cmp_tags() to got_ref_cmp_tags()

diff --git a/got/got.c b/got/got.c
index 393b9c5..c053c61 100644
--- a/got/got.c
+++ b/got/got.c
@@ -3670,7 +3670,7 @@ list_tags(struct got_repository *repo, struct got_worktree *worktree)
 
 	SIMPLEQ_INIT(&refs);
 
-	err = got_ref_list(&refs, repo, "refs/tags", got_repo_cmp_tags, repo);
+	err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
 	if (err)
 		return err;
 
diff --git a/include/got_reference.h b/include/got_reference.h
index 50ff767..da0da7b 100644
--- a/include/got_reference.h
+++ b/include/got_reference.h
@@ -95,6 +95,10 @@ typedef const struct got_error *(*got_ref_cmp_cb)(void *, int *,
 const struct got_error *got_ref_cmp_by_name(void *, int *,
     struct got_reference *, struct got_reference *);
 
+/* An implementation of got_ref_cmp_cb which compares two tags. */
+const struct got_error *got_ref_cmp_tags(void *, int *,
+    struct got_reference *, struct got_reference *);
+
 /*
  * Append all known references to a caller-provided ref list head.
  * Optionally limit references returned to those within a given
diff --git a/include/got_repository.h b/include/got_repository.h
index 041be7b..f54c896 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -116,7 +116,3 @@ typedef const struct got_error *(*got_repo_import_cb)(void *, const char *);
 const struct got_error *got_repo_import(struct got_object_id **, const char *,
     const char *, const char *, struct got_pathlist_head *,
     struct got_repository *, got_repo_import_cb, void *);
-
-/* Attempt to compare two reference tags */
-const struct got_error *got_repo_cmp_tags(void *, int *,
-    struct got_reference *, struct got_reference *);
diff --git a/lib/reference.c b/lib/reference.c
index 439627e..398dfd5 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -672,6 +672,70 @@ got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
 	return NULL;
 }
 
+const struct got_error *
+got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
+    struct got_reference *ref2)
+{
+	const struct got_error *err = NULL;
+	struct got_repository *repo = arg;
+	struct got_object_id *id1, *id2 = NULL;
+	struct got_tag_object *tag1 = NULL, *tag2 = NULL;
+	struct got_commit_object *commit1 = NULL, *commit2 = NULL;
+	time_t time1, time2;
+
+	*cmp = 0;
+
+	err = got_ref_resolve(&id1, repo, ref1);
+	if (err)
+		return err;
+	err = got_object_open_as_tag(&tag1, repo, id1);
+	if (err) {
+		if (err->code != GOT_ERR_OBJ_TYPE)
+			goto done;
+		/* "lightweight" tag */
+		err = got_object_open_as_commit(&commit1, repo, id1);
+		if (err)
+			goto done;
+		time1 = got_object_commit_get_committer_time(commit1);
+	} else
+		time1 = got_object_tag_get_tagger_time(tag1);
+
+	err = got_ref_resolve(&id2, repo, ref2);
+	if (err)
+		goto done;
+	err = got_object_open_as_tag(&tag2, repo, id2);
+	if (err) {
+		if (err->code != GOT_ERR_OBJ_TYPE)
+			goto done;
+		/* "lightweight" tag */
+		err = got_object_open_as_commit(&commit2, repo, id2);
+		if (err)
+			goto done;
+		time2 = got_object_commit_get_committer_time(commit2);
+	} else
+		time2 = got_object_tag_get_tagger_time(tag2);
+
+	/* Put latest tags first. */
+	if (time1 < time2)
+		*cmp = 1;
+	else if (time1 > time2)
+		*cmp = -1;
+	else
+		err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
+done:
+	free(id1);
+	free(id2);
+	if (tag1)
+		got_object_tag_close(tag1);
+	if (tag2)
+		got_object_tag_close(tag2);
+	if (commit1)
+		got_object_commit_close(commit1);
+	if (commit2)
+		got_object_commit_close(commit2);
+	return err;
+}
+
 static const struct got_error *
 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
     struct got_reference *ref, struct got_repository *repo,
diff --git a/lib/repository.c b/lib/repository.c
index bfb1d44..694024d 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -1685,67 +1685,3 @@ got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
 	free(new_tree_id);
 	return err;
 }
-
-const struct got_error *
-got_repo_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
-    struct got_reference *ref2)
-{
-	const struct got_error *err = NULL;
-	struct got_repository *repo = arg;
-	struct got_object_id *id1, *id2 = NULL;
-	struct got_tag_object *tag1 = NULL, *tag2 = NULL;
-	struct got_commit_object *commit1 = NULL, *commit2 = NULL;
-	time_t time1, time2;
-
-	*cmp = 0;
-
-	err = got_ref_resolve(&id1, repo, ref1);
-	if (err)
-		return err;
-	err = got_object_open_as_tag(&tag1, repo, id1);
-	if (err) {
-		if (err->code != GOT_ERR_OBJ_TYPE)
-			goto done;
-		/* "lightweight" tag */
-		err = got_object_open_as_commit(&commit1, repo, id1);
-		if (err)
-			goto done;
-		time1 = got_object_commit_get_committer_time(commit1);
-	} else
-		time1 = got_object_tag_get_tagger_time(tag1);
-
-	err = got_ref_resolve(&id2, repo, ref2);
-	if (err)
-		goto done;
-	err = got_object_open_as_tag(&tag2, repo, id2);
-	if (err) {
-		if (err->code != GOT_ERR_OBJ_TYPE)
-			goto done;
-		/* "lightweight" tag */
-		err = got_object_open_as_commit(&commit2, repo, id2);
-		if (err)
-			goto done;
-		time2 = got_object_commit_get_committer_time(commit2);
-	} else
-		time2 = got_object_tag_get_tagger_time(tag2);
-
-	/* Put latest tags first. */
-	if (time1 < time2)
-		*cmp = 1;
-	else if (time1 > time2)
-		*cmp = -1;
-	else
-		err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
-done:
-	free(id1);
-	free(id2);
-	if (tag1)
-		got_object_tag_close(tag1);
-	if (tag2)
-		got_object_tag_close(tag2);
-	if (commit1)
-		got_object_commit_close(commit1);
-	if (commit2)
-		got_object_commit_close(commit2);
-	return err;
-}