commit 2bf0fa54a55e054ffe4c688f49ecbd6a28101fe0 from: Stefan Sperling via: Thomas Adam date: Sat Nov 20 22:47:47 2021 UTC add got ref -t option to sort listed references by modification time commit - c591440fff37d0195c98e6a7357f881ec9d5a8df commit + 2bf0fa54a55e054ffe4c688f49ecbd6a28101fe0 blob - 2fae628addc1982ebd45678a1abce970b074a17a blob + f978e93058d0e2a0ba0fc2fe42c3385a24fbf75c --- got/got.1 +++ got/got.1 @@ -986,7 +986,7 @@ Show object IDs of files (blob objects) and directorie .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 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 blob - 389f9d08088c16749e26653373e02b47dbea55f0 blob + 3bdebeb287d1e246a565d6e1060713d35dcd05f7 --- got/got.c +++ 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; @@ -5748,6 +5750,9 @@ cmd_ref(int argc, char *argv[]) break; case 's': symref_target = optarg; + break; + case 't': + sort_by_time = 1; break; default: usage_ref(); @@ -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)