diff options
author | Eric Sunshine <sunshine@sunshineco.com> | 2020-06-19 19:35:43 -0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2020-06-22 10:30:29 -0700 |
commit | d9c54c2bbf684ca3903bc2227c81f657a557d8b8 (patch) | |
tree | f92b97425460ce1f035f4c4839e16e46dd441931 /builtin/worktree.c | |
parent | worktree: make "move" refuse to move atop missing registered worktree (diff) | |
download | tgif-d9c54c2bbf684ca3903bc2227c81f657a557d8b8.tar.xz |
worktree: drop get_worktrees() special-purpose sorting option
Of all the clients of get_worktrees(), only "git worktree list" wants
the list sorted in a very specific way; other clients simply don't care
about the order. Rather than imbuing get_worktrees() with special
knowledge about how various clients -- now and in the future -- may want
the list sorted, drop the sorting capability altogether and make it the
client's responsibility to sort the list if needed.
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/worktree.c')
-rw-r--r-- | builtin/worktree.c | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/builtin/worktree.c b/builtin/worktree.c index 4dec3951dc..c20e4ce089 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -694,6 +694,23 @@ static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen) } } +static int pathcmp(const void *a_, const void *b_) +{ + const struct worktree *const *a = a_; + const struct worktree *const *b = b_; + return fspathcmp((*a)->path, (*b)->path); +} + +static void pathsort(struct worktree **wt) +{ + int n = 0; + struct worktree **p = wt; + + while (*p++) + n++; + QSORT(wt, n, pathcmp); +} + static int list(int ac, const char **av, const char *prefix) { int porcelain = 0; @@ -707,9 +724,12 @@ static int list(int ac, const char **av, const char *prefix) if (ac) usage_with_options(worktree_usage, options); else { - struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED); + struct worktree **worktrees = get_worktrees(0); int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i; + /* sort worktrees by path but keep main worktree at top */ + pathsort(worktrees + 1); + if (!porcelain) measure_widths(worktrees, &abbrev, &path_maxlen); |