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>
This commit is contained in:

committed by
Junio C Hamano

parent
4fff1ef7ff
commit
4df1d4d466
@ -447,7 +447,7 @@ static int list(int ac, const char **av, const char *prefix)
|
|||||||
if (ac)
|
if (ac)
|
||||||
usage_with_options(worktree_usage, options);
|
usage_with_options(worktree_usage, options);
|
||||||
else {
|
else {
|
||||||
struct worktree **worktrees = get_worktrees(0);
|
struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
|
||||||
int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
|
int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
|
||||||
|
|
||||||
if (!porcelain)
|
if (!porcelain)
|
||||||
|
@ -117,4 +117,23 @@ test_expect_success 'broken main worktree still at the top' '
|
|||||||
)
|
)
|
||||||
'
|
'
|
||||||
|
|
||||||
|
test_expect_success 'linked worktrees are sorted' '
|
||||||
|
mkdir sorted &&
|
||||||
|
git init sorted/main &&
|
||||||
|
(
|
||||||
|
cd sorted/main &&
|
||||||
|
test_tick &&
|
||||||
|
test_commit new &&
|
||||||
|
git worktree add ../first &&
|
||||||
|
git worktree add ../second &&
|
||||||
|
git worktree list --porcelain | grep ^worktree >actual
|
||||||
|
) &&
|
||||||
|
cat >expected <<-EOF &&
|
||||||
|
worktree $(pwd)/sorted/main
|
||||||
|
worktree $(pwd)/sorted/first
|
||||||
|
worktree $(pwd)/sorted/second
|
||||||
|
EOF
|
||||||
|
test_cmp expected sorted/main/actual
|
||||||
|
'
|
||||||
|
|
||||||
test_done
|
test_done
|
||||||
|
14
worktree.c
14
worktree.c
@ -160,6 +160,13 @@ static void mark_current_worktree(struct worktree **worktrees)
|
|||||||
free(git_dir);
|
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 **get_worktrees(unsigned flags)
|
||||||
{
|
{
|
||||||
struct worktree **list = NULL;
|
struct worktree **list = NULL;
|
||||||
@ -191,6 +198,13 @@ struct worktree **get_worktrees(unsigned flags)
|
|||||||
ALLOC_GROW(list, counter + 1, alloc);
|
ALLOC_GROW(list, counter + 1, alloc);
|
||||||
list[counter] = NULL;
|
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);
|
mark_current_worktree(list);
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,8 @@ struct worktree {
|
|||||||
|
|
||||||
/* Functions for acting on the information about worktrees. */
|
/* 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,
|
* Get the worktrees. The primary worktree will always be the first returned,
|
||||||
* and linked worktrees will be pointed to by 'next' in each subsequent
|
* and linked worktrees will be pointed to by 'next' in each subsequent
|
||||||
|
Reference in New Issue
Block a user