Skip to content

Commit d9c54c2

Browse files
sunshinecogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 810382e commit d9c54c2

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

builtin/worktree.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,23 @@ static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
694694
}
695695
}
696696

697+
static int pathcmp(const void *a_, const void *b_)
698+
{
699+
const struct worktree *const *a = a_;
700+
const struct worktree *const *b = b_;
701+
return fspathcmp((*a)->path, (*b)->path);
702+
}
703+
704+
static void pathsort(struct worktree **wt)
705+
{
706+
int n = 0;
707+
struct worktree **p = wt;
708+
709+
while (*p++)
710+
n++;
711+
QSORT(wt, n, pathcmp);
712+
}
713+
697714
static int list(int ac, const char **av, const char *prefix)
698715
{
699716
int porcelain = 0;
@@ -707,9 +724,12 @@ static int list(int ac, const char **av, const char *prefix)
707724
if (ac)
708725
usage_with_options(worktree_usage, options);
709726
else {
710-
struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
727+
struct worktree **worktrees = get_worktrees(0);
711728
int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
712729

730+
/* sort worktrees by path but keep main worktree at top */
731+
pathsort(worktrees + 1);
732+
713733
if (!porcelain)
714734
measure_widths(worktrees, &abbrev, &path_maxlen);
715735

worktree.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,6 @@ static void mark_current_worktree(struct worktree **worktrees)
123123
free(git_dir);
124124
}
125125

126-
static int compare_worktree(const void *a_, const void *b_)
127-
{
128-
const struct worktree *const *a = a_;
129-
const struct worktree *const *b = b_;
130-
return fspathcmp((*a)->path, (*b)->path);
131-
}
132-
133126
struct worktree **get_worktrees(unsigned flags)
134127
{
135128
struct worktree **list = NULL;
@@ -161,13 +154,6 @@ struct worktree **get_worktrees(unsigned flags)
161154
ALLOC_GROW(list, counter + 1, alloc);
162155
list[counter] = NULL;
163156

164-
if (flags & GWT_SORT_LINKED)
165-
/*
166-
* don't sort the first item (main worktree), which will
167-
* always be the first
168-
*/
169-
QSORT(list + 1, counter - 1, compare_worktree);
170-
171157
mark_current_worktree(list);
172158
return list;
173159
}

worktree.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,12 @@ struct worktree {
1818
int lock_reason_valid; /* private */
1919
};
2020

21-
/* Functions for acting on the information about worktrees. */
22-
23-
#define GWT_SORT_LINKED (1 << 0) /* keeps linked worktrees sorted */
24-
2521
/*
2622
* Get the worktrees. The primary worktree will always be the first returned,
27-
* and linked worktrees will be pointed to by 'next' in each subsequent
28-
* worktree. No specific ordering is done on the linked worktrees.
23+
* and linked worktrees will follow in no particular order.
2924
*
3025
* The caller is responsible for freeing the memory from the returned
31-
* worktree(s).
26+
* worktrees by calling free_worktrees().
3227
*/
3328
struct worktree **get_worktrees(unsigned flags);
3429

0 commit comments

Comments
 (0)