Skip to content

Commit 4df1d4d

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4fff1ef commit 4df1d4d

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

builtin/worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ static int list(int ac, const char **av, const char *prefix)
447447
if (ac)
448448
usage_with_options(worktree_usage, options);
449449
else {
450-
struct worktree **worktrees = get_worktrees(0);
450+
struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
451451
int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
452452

453453
if (!porcelain)

t/t2027-worktree-list.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,23 @@ test_expect_success 'broken main worktree still at the top' '
117117
)
118118
'
119119

120+
test_expect_success 'linked worktrees are sorted' '
121+
mkdir sorted &&
122+
git init sorted/main &&
123+
(
124+
cd sorted/main &&
125+
test_tick &&
126+
test_commit new &&
127+
git worktree add ../first &&
128+
git worktree add ../second &&
129+
git worktree list --porcelain | grep ^worktree >actual
130+
) &&
131+
cat >expected <<-EOF &&
132+
worktree $(pwd)/sorted/main
133+
worktree $(pwd)/sorted/first
134+
worktree $(pwd)/sorted/second
135+
EOF
136+
test_cmp expected sorted/main/actual
137+
'
138+
120139
test_done

worktree.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ static void mark_current_worktree(struct worktree **worktrees)
160160
free(git_dir);
161161
}
162162

163+
static int compare_worktree(const void *a_, const void *b_)
164+
{
165+
const struct worktree *const *a = a_;
166+
const struct worktree *const *b = b_;
167+
return fspathcmp((*a)->path, (*b)->path);
168+
}
169+
163170
struct worktree **get_worktrees(unsigned flags)
164171
{
165172
struct worktree **list = NULL;
@@ -191,6 +198,13 @@ struct worktree **get_worktrees(unsigned flags)
191198
ALLOC_GROW(list, counter + 1, alloc);
192199
list[counter] = NULL;
193200

201+
if (flags & GWT_SORT_LINKED)
202+
/*
203+
* don't sort the first item (main worktree), which will
204+
* always be the first
205+
*/
206+
QSORT(list + 1, counter - 1, compare_worktree);
207+
194208
mark_current_worktree(list);
195209
return list;
196210
}

worktree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ struct worktree {
1515

1616
/* Functions for acting on the information about worktrees. */
1717

18+
#define GWT_SORT_LINKED (1 << 0) /* keeps linked worktrees sorted */
19+
1820
/*
1921
* Get the worktrees. The primary worktree will always be the first returned,
2022
* and linked worktrees will be pointed to by 'next' in each subsequent

0 commit comments

Comments
 (0)