make 'got tag -l' list tags by time stamp in descending order
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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
diff --git a/got/got.c b/got/got.c
index 6148cc9..9ae1a22 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1763,7 +1763,7 @@ cmd_log(int argc, char *argv[])
path = in_repo_path;
}
- error = got_ref_list(&refs, repo, NULL);
+ error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
if (error)
goto done;
@@ -2825,7 +2825,7 @@ list_refs(struct got_repository *repo)
struct got_reflist_entry *re;
SIMPLEQ_INIT(&refs);
- err = got_ref_list(&refs, repo, NULL);
+ err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
if (err)
return err;
@@ -3065,7 +3065,8 @@ list_branches(struct got_repository *repo, struct got_worktree *worktree)
SIMPLEQ_INIT(&refs);
- err = got_ref_list(&refs, repo, "refs/heads");
+ err = got_ref_list(&refs, repo, "refs/heads",
+ got_ref_cmp_by_name, NULL);
if (err)
return err;
@@ -3304,6 +3305,109 @@ usage_tag(void)
exit(1);
}
+#if 0
+static const struct got_error *
+sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
+{
+ const struct got_error *err = NULL;
+ struct got_reflist_entry *re, *se, *new;
+ struct got_object_id *re_id, *se_id;
+ struct got_tag_object *re_tag, *se_tag;
+ time_t re_time, se_time;
+
+ SIMPLEQ_FOREACH(re, tags, entry) {
+ se = SIMPLEQ_FIRST(sorted);
+ if (se == NULL) {
+ err = got_reflist_entry_dup(&new, re);
+ if (err)
+ return err;
+ SIMPLEQ_INSERT_HEAD(sorted, new, entry);
+ continue;
+ } else {
+ err = got_ref_resolve(&re_id, repo, re->ref);
+ if (err)
+ break;
+ err = got_object_open_as_tag(&re_tag, repo, re_id);
+ free(re_id);
+ if (err)
+ break;
+ re_time = got_object_tag_get_tagger_time(re_tag);
+ got_object_tag_close(re_tag);
+ }
+
+ while (se) {
+ err = got_ref_resolve(&se_id, repo, re->ref);
+ if (err)
+ break;
+ err = got_object_open_as_tag(&se_tag, repo, se_id);
+ free(se_id);
+ if (err)
+ break;
+ se_time = got_object_tag_get_tagger_time(se_tag);
+ got_object_tag_close(se_tag);
+
+ if (se_time > re_time) {
+ err = got_reflist_entry_dup(&new, re);
+ if (err)
+ return err;
+ SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
+ break;
+ }
+ se = SIMPLEQ_NEXT(se, entry);
+ continue;
+ }
+ }
+done:
+ return err;
+}
+#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;
+ 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)
+ goto done;
+
+ err = got_ref_resolve(&id2, repo, ref2);
+ if (err)
+ goto done;
+ err = got_object_open_as_tag(&tag2, repo, id2);
+ if (err)
+ goto done;
+
+ time1 = got_object_tag_get_tagger_time(tag1);
+ 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);
+ return err;
+}
+
static const struct got_error *
list_tags(struct got_repository *repo, struct got_worktree *worktree)
{
@@ -3313,7 +3417,7 @@ list_tags(struct got_repository *repo, struct got_worktree *worktree)
SIMPLEQ_INIT(&refs);
- err = got_ref_list(&refs, repo, "refs/tags");
+ err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
if (err)
return err;
diff --git a/include/got_reference.h b/include/got_reference.h
index 67fa4cc..50ff767 100644
--- a/include/got_reference.h
+++ b/include/got_reference.h
@@ -83,13 +83,26 @@ struct got_reflist_entry {
};
SIMPLEQ_HEAD(got_reflist_head, got_reflist_entry);
+/* Duplicate a reference list entry. Caller must dispose of it with free(3). */
+const struct got_error *got_reflist_entry_dup(struct got_reflist_entry **,
+ struct got_reflist_entry *);
+
+/* A function which compares two references. Used with got_ref_list(). */
+typedef const struct got_error *(*got_ref_cmp_cb)(void *, int *,
+ struct got_reference *, struct got_reference *);
+
+/* An implementation of got_ref_cmp_cb which compares two references by name. */
+const struct got_error *got_ref_cmp_by_name(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
- * reference namespace.
+ * reference namespace. Sort the list with the provided reference comparison
+ * function, usually got_ref_cmp_by_name().
*/
const struct got_error *got_ref_list(struct got_reflist_head *,
- struct got_repository *, const char *);
+ struct got_repository *, const char *, got_ref_cmp_cb, void *);
/* Free all references on a ref list. */
void got_ref_list_free(struct got_reflist_head *);
diff --git a/lib/reference.c b/lib/reference.c
index 27bcf86..c4723d5 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -527,6 +527,38 @@ got_ref_dup(struct got_reference *ref)
return ret;
}
+const struct got_error *
+got_reflist_entry_dup(struct got_reflist_entry **newp,
+ struct got_reflist_entry *re)
+{
+ const struct got_error *err = NULL;
+ struct got_reflist_entry *new;
+
+ *newp = NULL;
+
+ new = malloc(sizeof(*new));
+ if (new == NULL)
+ return got_error_from_errno("malloc");
+
+ new->ref = got_ref_dup(re->ref);
+ if (new->ref == NULL) {
+ err = got_error_from_errno("got_ref_dup");
+ free(new);
+ return err;
+ }
+
+ new->id = got_object_id_dup(re->id);
+ if (new->id == NULL) {
+ err = got_error_from_errno("got_ref_dup");
+ free(new->id);
+ free(new);
+ return err;
+ }
+
+ *newp = new;
+ return NULL;
+}
+
static const struct got_error *
resolve_symbolic_ref(struct got_reference **resolved,
struct got_repository *repo, struct got_reference *ref)
@@ -620,9 +652,21 @@ got_ref_get_symref_target(struct got_reference *ref)
return NULL;
}
+const struct got_error *
+got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
+ struct got_reference* re2)
+{
+ const char *name1 = got_ref_get_name(re1);
+ const char *name2 = got_ref_get_name(re2);
+
+ *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
+ return NULL;
+}
+
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)
+ struct got_reference *ref, struct got_repository *repo,
+ got_ref_cmp_cb cmp_cb, void *cmp_arg)
{
const struct got_error *err;
struct got_object_id *id;
@@ -652,10 +696,9 @@ insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
*/
re = SIMPLEQ_FIRST(refs);
while (re) {
- const char *name = got_ref_get_name(re->ref);
- const char *new_name = got_ref_get_name(new->ref);
- cmp = got_path_cmp(name, new_name, strlen(name),
- strlen(new_name));
+ err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
+ if (err)
+ return err;
if (cmp == 0) {
/* duplicate */
free(new->id);
@@ -680,7 +723,8 @@ insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
static const struct got_error *
gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
- const char *subdir, struct got_repository *repo)
+ const char *subdir, struct got_repository *repo,
+ got_ref_cmp_cb cmp_cb, void *cmp_arg)
{
const struct got_error *err = NULL;
DIR *d = NULL;
@@ -714,7 +758,8 @@ gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
goto done;
if (ref) {
struct got_reflist_entry *new;
- err = insert_ref(&new, refs, ref, repo);
+ err = insert_ref(&new, refs, ref, repo,
+ cmp_cb, cmp_arg);
if (err || new == NULL /* duplicate */)
got_ref_close(ref);
if (err)
@@ -727,7 +772,8 @@ gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
err = got_error_from_errno("asprintf");
break;
}
- err = gather_on_disk_refs(refs, path_refs, child, repo);
+ err = gather_on_disk_refs(refs, path_refs, child, repo,
+ cmp_cb, cmp_arg);
free(child);
break;
default:
@@ -743,7 +789,7 @@ done:
const struct got_error *
got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
- const char *ref_namespace)
+ const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
{
const struct got_error *err;
char *packed_refs_path, *path_refs = NULL;
@@ -761,7 +807,7 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
if (err)
goto done;
- err = insert_ref(&new, refs, ref, repo);
+ err = insert_ref(&new, refs, ref, repo, cmp_cb, cmp_arg);
if (err || new == NULL /* duplicate */)
got_ref_close(ref);
if (err)
@@ -779,7 +825,7 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
goto done;
}
err = gather_on_disk_refs(refs, path_refs,
- ref_namespace ? ref_namespace : "", repo);
+ ref_namespace ? ref_namespace : "", repo, cmp_cb, cmp_arg);
if (err)
goto done;
@@ -821,7 +867,8 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
continue;
}
}
- err = insert_ref(&new, refs, ref, repo);
+ err = insert_ref(&new, refs, ref, repo,
+ cmp_cb, cmp_arg);
if (err || new == NULL /* duplicate */)
got_ref_close(ref);
if (err)
@@ -1051,7 +1098,8 @@ delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
continue;
}
- err = insert_ref(&new, &refs, ref, repo);
+ err = insert_ref(&new, &refs, ref, repo,
+ got_ref_cmp_by_name, NULL);
if (err || new == NULL /* duplicate */)
got_ref_close(ref);
if (err)
diff --git a/lib/repository.c b/lib/repository.c
index 4ed88ea..eeffa65 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -1128,7 +1128,7 @@ got_repo_object_match_tag(struct got_tag_object **tag, const char *name,
SIMPLEQ_INIT(&refs);
*tag = NULL;
- err = got_ref_list(&refs, repo, "refs/tags");
+ err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_by_name, NULL);
if (err)
return err;
diff --git a/regress/cmdline/tag.sh b/regress/cmdline/tag.sh
index dd28ecc..db9528a 100755
--- a/regress/cmdline/tag.sh
+++ b/regress/cmdline/tag.sh
@@ -146,7 +146,7 @@ function test_tag_list {
echo "-----------------------------------------------" \
> $testroot/stdout.expected
- echo "tag $tag $tag_id" >> $testroot/stdout.expected
+ echo "tag $tag2 $tag_id2" >> $testroot/stdout.expected
echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
echo "date: $d1" >> $testroot/stdout.expected
echo "object: commit $commit_id" >> $testroot/stdout.expected
@@ -155,7 +155,7 @@ function test_tag_list {
echo " " >> $testroot/stdout.expected
echo "-----------------------------------------------" \
>> $testroot/stdout.expected
- echo "tag $tag2 $tag_id2" >> $testroot/stdout.expected
+ echo "tag $tag $tag_id" >> $testroot/stdout.expected
echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
echo "date: $d2" >> $testroot/stdout.expected
echo "object: commit $commit_id" >> $testroot/stdout.expected
diff --git a/tog/tog.c b/tog/tog.c
index a1865a5..d5a10ac 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -2303,7 +2303,7 @@ cmd_log(int argc, char *argv[])
if (error != NULL)
goto done;
- error = got_ref_list(&refs, repo, NULL);
+ error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
if (error)
goto done;
@@ -2928,7 +2928,7 @@ cmd_diff(int argc, char *argv[])
if (error)
goto done;
- error = got_ref_list(&refs, repo, NULL);
+ error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
if (error)
goto done;
@@ -3832,7 +3832,7 @@ cmd_blame(int argc, char *argv[])
if (error != NULL)
goto done;
- error = got_ref_list(&refs, repo, NULL);
+ error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
if (error)
goto done;
@@ -4589,7 +4589,7 @@ cmd_tree(int argc, char *argv[])
if (error != NULL)
goto done;
- error = got_ref_list(&refs, repo, NULL);
+ error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
if (error)
goto done;