summaryrefslogtreecommitdiff
path: root/worktree.c
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2016-11-28 09:36:56 (GMT)
committerJunio C Hamano <gitster@pobox.com>2016-11-28 21:18:51 (GMT)
commit4df1d4d4666eb26b420d5b386010470729846b8c (patch)
tree3326149c4a7e8c3927d56e1f2a0e04ba0f19c1ee /worktree.c
parent4fff1ef7ffe0e370459242cf08c51177eeb4181f (diff)
downloadgit-4df1d4d4666eb26b420d5b386010470729846b8c.zip
git-4df1d4d4666eb26b420d5b386010470729846b8c.tar.gz
git-4df1d4d4666eb26b420d5b386010470729846b8c.tar.bz2
worktree list: keep the list sorted
It makes it easier to write tests for. But it should also be good for the user since locating a worktree by eye would be easier once they notice this. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'worktree.c')
-rw-r--r--worktree.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/worktree.c b/worktree.c
index ead088e..eb61212 100644
--- a/worktree.c
+++ b/worktree.c
@@ -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;
}