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>
Eric Sunshine committed
Jun 19, 2020 at 19:35 UTC
d9c54c2bbf684ca3903bc2227c81f657a557d8b8
3 files changed
+23
-22
builtin/worktree.c
+21
-1
@@ -694,6 +694,23 @@ static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
694
}
695
}
696
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
+
714
static int list(int ac, const char **av, const char *prefix)
715
{
716
int porcelain = 0;
@@ -707,9 +724,12 @@ static int list(int ac, const char **av, const char *prefix)
724
if (ac)
725
usage_with_options(worktree_usage, options);
726
else {
710
- struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
727
+ struct worktree **worktrees = get_worktrees(0);
728
int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
729
730
+ /* sort worktrees by path but keep main worktree at top */
731
+ pathsort(worktrees + 1);
732
+
733
if (!porcelain)
734
measure_widths(worktrees, &abbrev, &path_maxlen);
735
worktree.c
-14
@@ -123,13 +123,6 @@ static void mark_current_worktree(struct worktree **worktrees)
123
free(git_dir);
124
}
125
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
-
126
struct worktree **get_worktrees(unsigned flags)
127
{
128
struct worktree **list = NULL;
@@ -161,13 +154,6 @@ struct worktree **get_worktrees(unsigned flags)
154
ALLOC_GROW(list, counter + 1, alloc);
155
list[counter] = NULL;
156
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
-
157
mark_current_worktree(list);
158
return list;
159
}
worktree.h
+2
-7
@@ -18,17 +18,12 @@ struct worktree {
18
int lock_reason_valid; /* private */
19
};
20
21
-/* Functions for acting on the information about worktrees. */
22
-
23
-#define GWT_SORT_LINKED (1 << 0) /* keeps linked worktrees sorted */
24
-
21
/*
22
* 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.
24
*
25
* The caller is responsible for freeing the memory from the returned
31
- * worktree(s).
26
+ * worktrees by calling free_worktrees().
27
*/
28
struct worktree **get_worktrees(unsigned flags);
29