add got ref -t option to sort listed references by modification time
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
diff --git a/got/got.1 b/got/got.1
index 2fae628..f978e93 100644
--- a/got/got.1
+++ b/got/got.1
@@ -986,7 +986,7 @@ Show object IDs of files (blob objects) and directories (tree objects).
.It Fl R
Recurse into sub-directories in the repository.
.El
-.It Cm ref Oo Fl r Ar repository-path Oc Oo Fl l Oc Oo Fl c Ar object Oc Oo Fl s Ar reference Oc Oo Fl d Oc Op Ar name
+.It Cm ref Oo Fl r Ar repository-path Oc Oo Fl l Oc Oo Fl t Oc Oo Fl c Ar object Oc Oo Fl s Ar reference Oc Oo Fl d Oc Op Ar name
Manage references in a repository.
.Pp
References may be listed, created, deleted, and changed.
@@ -1017,7 +1017,15 @@ is a reference namespace, list all references in this namespace.
Otherwise, show only the reference with the given
.Ar name .
Cannot be used together with any other options except
-.Fl r .
+.Fl r
+and
+.Fl t .
+.It Fl t
+Sort listed references by modification time (most recently modified first)
+instead of sorting by lexicographical order.
+Use of this option requires the
+.Fl l
+option to be used as well.
.It Fl c Ar object
Create a reference or change an existing reference.
The reference with the specified
diff --git a/got/got.c b/got/got.c
index 79b1186..34fa60a 100644
--- a/got/got.c
+++ b/got/got.c
@@ -5596,21 +5596,23 @@ __dead static void
usage_ref(void)
{
fprintf(stderr,
- "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
- "[-d] [name]\n",
+ "usage: %s ref [-r repository] [-l] [-t] [-c object] "
+ "[-s reference] [-d] [name]\n",
getprogname());
exit(1);
}
static const struct got_error *
-list_refs(struct got_repository *repo, const char *refname)
+list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
{
static const struct got_error *err = NULL;
struct got_reflist_head refs;
struct got_reflist_entry *re;
TAILQ_INIT(&refs);
- err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
+ err = got_ref_list(&refs, repo, refname, sort_by_time ?
+ got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
+ repo);
if (err)
return err;
@@ -5724,11 +5726,11 @@ cmd_ref(int argc, char *argv[])
struct got_repository *repo = NULL;
struct got_worktree *worktree = NULL;
char *cwd = NULL, *repo_path = NULL;
- int ch, do_list = 0, do_delete = 0;
+ int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
const char *obj_arg = NULL, *symref_target= NULL;
char *refname = NULL;
- while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
+ while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
switch (ch) {
case 'c':
obj_arg = optarg;
@@ -5749,6 +5751,9 @@ cmd_ref(int argc, char *argv[])
case 's':
symref_target = optarg;
break;
+ case 't':
+ sort_by_time = 1;
+ break;
default:
usage_ref();
/* NOTREACHED */
@@ -5767,6 +5772,8 @@ cmd_ref(int argc, char *argv[])
option_conflict('s', 'l');
if (do_delete && do_list)
option_conflict('d', 'l');
+ if (sort_by_time && !do_list)
+ errx(1, "-t option requires -l option");
argc -= optind;
argv += optind;
@@ -5843,7 +5850,7 @@ cmd_ref(int argc, char *argv[])
goto done;
if (do_list)
- error = list_refs(repo, refname);
+ error = list_refs(repo, refname, sort_by_time);
else if (do_delete)
error = delete_ref_by_name(repo, refname);
else if (symref_target)