move cmp_tags() into the library
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
diff --git a/got/got.c b/got/got.c
index 8557a12..393b9c5 100644
--- a/got/got.c
+++ b/got/got.c
@@ -3662,70 +3662,6 @@ done:
#endif
static const struct got_error *
-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 *
list_tags(struct got_repository *repo, struct got_worktree *worktree)
{
static const struct got_error *err = NULL;
@@ -3734,7 +3670,7 @@ list_tags(struct got_repository *repo, struct got_worktree *worktree)
SIMPLEQ_INIT(&refs);
- err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
+ err = got_ref_list(&refs, repo, "refs/tags", got_repo_cmp_tags, repo);
if (err)
return err;
diff --git a/include/got_repository.h b/include/got_repository.h
index f54c896..041be7b 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -116,3 +116,7 @@ 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/repository.c b/lib/repository.c
index 694024d..bfb1d44 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -1685,3 +1685,67 @@ 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;
+}