]> git.ipfire.org Git - thirdparty/git.git/commitdiff
worktree: drop get_worktrees() special-purpose sorting option
authorEric Sunshine <sunshine@sunshineco.com>
Fri, 19 Jun 2020 23:35:43 +0000 (19:35 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 22 Jun 2020 17:30:29 +0000 (10:30 -0700)
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>
builtin/worktree.c
worktree.c
worktree.h

index 4dec3951dc2cacf72337f9500ecc7c3480f72b84..c20e4ce08925cb82ab6e1767490404a910d7634f 100644 (file)
@@ -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);
 
index eba4fd3a03812f046dadc1baf9d7644ba8dfcce9..42cabc5403281fe8e4bee87b77799612fbebb674 100644 (file)
@@ -123,13 +123,6 @@ static void mark_current_worktree(struct worktree **worktrees)
        free(git_dir);
 }
 
-static int compare_worktree(const void *a_, const void *b_)
-{
-       const struct worktree *const *a = a_;
-       const struct worktree *const *b = b_;
-       return fspathcmp((*a)->path, (*b)->path);
-}
-
 struct worktree **get_worktrees(unsigned flags)
 {
        struct worktree **list = NULL;
@@ -161,13 +154,6 @@ struct worktree **get_worktrees(unsigned flags)
        ALLOC_GROW(list, counter + 1, alloc);
        list[counter] = NULL;
 
-       if (flags & GWT_SORT_LINKED)
-               /*
-                * don't sort the first item (main worktree), which will
-                * always be the first
-                */
-               QSORT(list + 1, counter - 1, compare_worktree);
-
        mark_current_worktree(list);
        return list;
 }
index d242a6e71c0333f80b76e44976f0c4356f99bec3..bd2235abe06ef501001e508f116042a546710786 100644 (file)
@@ -18,17 +18,12 @@ struct worktree {
        int lock_reason_valid; /* private */
 };
 
-/* Functions for acting on the information about worktrees. */
-
-#define GWT_SORT_LINKED (1 << 0) /* keeps linked worktrees sorted */
-
 /*
  * Get the worktrees.  The primary worktree will always be the first returned,
- * and linked worktrees will be pointed to by 'next' in each subsequent
- * worktree.  No specific ordering is done on the linked worktrees.
+ * and linked worktrees will follow in no particular order.
  *
  * The caller is responsible for freeing the memory from the returned
- * worktree(s).
+ * worktrees by calling free_worktrees().
  */
 struct worktree **get_worktrees(unsigned flags);