git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 5/5] worktree list: keep the list sorted

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Nov 28, 2016, 09:36 UTC
Message-ID
<20161128093656.15744-6-pclouds@gmail.com>
In-Reply-To
<20161128093656.15744-1-pclouds@gmail.com>

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>
---
 builtin/worktree.c       |  2 +-
 t/t2027-worktree-list.sh | 19 +++++++++++++++++++
 worktree.c               | 14 ++++++++++++++
 worktree.h               |  2 ++
 4 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index d7d195c..9a97e37 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -447,7 +447,7 @@ static int list(int ac, const char **av, const char *prefix)
 	if (ac)
 		usage_with_options(worktree_usage, options);
 	else {
-		struct worktree **worktrees = get_worktrees(0);
+		struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
 		int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
 
 		if (!porcelain)
diff --git a/t/t2027-worktree-list.sh b/t/t2027-worktree-list.sh
index 98b5f34..465eeea 100755
--- a/t/t2027-worktree-list.sh
+++ b/t/t2027-worktree-list.sh
@@ -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
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;
 }
diff --git a/worktree.h b/worktree.h
index 2e68d4a..d59ce1f 100644
--- a/worktree.h
+++ b/worktree.h
@@ -15,6 +15,8 @@ struct worktree {
 
 /* 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,
  * and linked worktrees will be pointed to by 'next' in each subsequent
-- 
2.8.2.524.g6ff3d78
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 16 of 19 in “Minor fixes on 'git worktree'”
  1. 0/3 Minor fixes on 'git worktree'Nguyễn Thái Ngọc Duy, Nov 22, 2016
  2. 1/3 worktree.c: zero new 'struct worktree' on allocationNguyễn Thái Ngọc Duy, Nov 22, 2016
  3. Junio C HamanoNov 23, 2016
  4. 2/3 get_worktrees() must return main worktree as first item even on errorNguyễn Thái Ngọc Duy, Nov 22, 2016
  5. Junio C HamanoNov 23, 2016
  6. 3/3 worktree list: keep the list sortedNguyễn Thái Ngọc Duy, Nov 22, 2016
  7. Junio C HamanoNov 23, 2016
  8. Duy NguyenNov 25, 2016
  9. Junio C HamanoNov 23, 2016
  10. Duy NguyenNov 25, 2016
  11. Duy NguyenNov 25, 2016
  12. Junio C HamanoNov 28, 2016
  13. 0/5 nd/worktree-list-fixupNguyễn Thái Ngọc Duy, Nov 28, 2016
  14. 1/5 worktree.c: zero new 'struct worktree' on allocationNguyễn Thái Ngọc Duy, Nov 28, 2016
  15. 2/5 worktree: reorder an if statementNguyễn Thái Ngọc Duy, Nov 28, 2016
  16. 5/5 worktree list: keep the list sortedNguyễn Thái Ngọc Duy, Nov 28, 2016
  17. 3/5 get_worktrees() must return main worktree as first item even on errorNguyễn Thái Ngọc Duy, Nov 28, 2016
  18. 4/5 worktree.c: get_worktrees() takes a new flag argumentNguyễn Thái Ngọc Duy, Nov 28, 2016
  19. Junio C HamanoNov 28, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.