worktree list: keep the list sorted
[gitweb.git] / worktree.c
index ead088e43c674bcc5d1e2de9828e99ab73cc84c4..eb6121263b0d56b6b7ea7c5024e9ceb68256c1c5 100644 (file)
@@ -160,6 +160,13 @@ 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;
@@ -191,6 +198,13 @@ 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;
 }