git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	rappazzo@gmail.com, "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH v2 5/5] worktree list: keep the list sorted
Date: Mon, 28 Nov 2016 16:36:56 +0700	[thread overview]
Message-ID: <20161128093656.15744-6-pclouds@gmail.com> (raw)
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


  parent reply	other threads:[~2016-11-28  9:38 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-22 10:00 [PATCH 0/3] Minor fixes on 'git worktree' Nguyễn Thái Ngọc Duy
2016-11-22 10:00 ` [PATCH 1/3] worktree.c: zero new 'struct worktree' on allocation Nguyễn Thái Ngọc Duy
2016-11-23 16:52   ` Junio C Hamano
2016-11-22 10:00 ` [PATCH 2/3] get_worktrees() must return main worktree as first item even on error Nguyễn Thái Ngọc Duy
2016-11-23 17:06   ` Junio C Hamano
2016-11-22 10:00 ` [PATCH 3/3] worktree list: keep the list sorted Nguyễn Thái Ngọc Duy
2016-11-23 17:16   ` Junio C Hamano
2016-11-25 12:19     ` Duy Nguyen
2016-11-23 16:52 ` [PATCH 0/3] Minor fixes on 'git worktree' Junio C Hamano
2016-11-25 12:24   ` Duy Nguyen
2016-11-25 13:44     ` Duy Nguyen
2016-11-28 19:36       ` Junio C Hamano
2016-11-28  9:36 ` [PATCH v2 0/5] nd/worktree-list-fixup Nguyễn Thái Ngọc Duy
2016-11-28  9:36   ` [PATCH v2 1/5] worktree.c: zero new 'struct worktree' on allocation Nguyễn Thái Ngọc Duy
2016-11-28  9:36   ` [PATCH v2 2/5] worktree: reorder an if statement Nguyễn Thái Ngọc Duy
2016-11-28  9:36   ` [PATCH v2 3/5] get_worktrees() must return main worktree as first item even on error Nguyễn Thái Ngọc Duy
2016-11-28  9:36   ` [PATCH v2 4/5] worktree.c: get_worktrees() takes a new flag argument Nguyễn Thái Ngọc Duy
2016-11-28  9:36   ` Nguyễn Thái Ngọc Duy [this message]
2016-11-28 21:25   ` [PATCH v2 0/5] nd/worktree-list-fixup Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161128093656.15744-6-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=rappazzo@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).