Subject: Re: [PATCH 00/11] git worktree (re)move

From: Duy Nguyen <[email-masked]>
Date: Wed, 16 Nov 2016 20:05:34 +0700

On Sat, Nov 12, 2016 at 06:53:44PM -0800, Junio C Hamano wrote:
> not ok 12 - move worktree
> #	
> #		git worktree move source destination &&
> #		test_path_is_missing source &&
> #		git worktree list --porcelain | grep "^worktree" >actual &&
> #		cat <<-EOF >expected &&
> #		worktree $TRASH_DIRECTORY
> #		worktree $TRASH_DIRECTORY/destination
> #		worktree $TRASH_DIRECTORY/elsewhere
> #		EOF
> #		test_cmp expected actual &&
> #		git -C destination log --format=%s >actual2 &&
> #		echo init >expected2 &&
> #		test_cmp expected2 actual2

I think I've seen this (i.e. 'expected' and 'actual' differ only in
the order of items) once after a rebase and ignored it, assuming
something was changed during the rebase that caused this.

The following patch should fix it if that's the same thing you saw. I
could pile it on worktree-move series, or you can make it a separate
one-patch series. What's your preference?

-- 8< --
Subject: [PATCH] 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-masked]>
---
 worktree.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/worktree.c b/worktree.c
index f7869f8..fe92d6f 100644
--- a/worktree.c
+++ b/worktree.c
@@ -173,6 +173,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(void)
 {
 	struct worktree **list = NULL;
@@ -205,6 +212,11 @@ struct worktree **get_worktrees(void)
 	ALLOC_GROW(list, counter + 1, alloc);
 	list[counter] = NULL;
 
+	/*
+	 * don't sort the first item (main worktree), which will
+	 * always be the first
+	 */
+	qsort(list + 1, counter - 1, sizeof(*list), compare_worktree);
 	mark_current_worktree(list);
 	return list;
 }
-- 
2.8.2.524.g6ff3d78

-- 8< --