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

Re: [PATCH v9 5/5] worktree: add 'list' command

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 2, 2015, 20:33 UTC
Message-ID
<xmqqtwq93t9p.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<1443786935-81131-6-git-send-email-rappazzo@gmail.com>
Michael Rappazzo <rappazzo@gmail.com> writes:
Show 7 quoted lines
> +		if (!porcelain) {
> +			for (i = 0; worktrees[i]; i++) {
> +				int path_len = strlen(worktrees[i]->path);
> +				if (path_len > path_maxlen)
> +					path_maxlen = path_len;
> +				int sha1_len = strlen(
> +						find_unique_abbrev(worktrees[i]->head_sha1, DEFAULT_ABBREV));
decl-after-stmt.

If I were doing this, I'd probably do something like the attached using a small helper function to make the primary logic easier to see.

The first hunk below is unrelated but was to fix an obvious style breakage I happened to have noticed nearby.

Thanks.
 builtin/worktree.c | 40 +++++++++++++++++++++++-----------------
 1 file changed, 23 insertions(+), 17 deletions(-)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 268f9bf..3be8ec8 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -376,8 +376,8 @@ static void show_worktree_porcelain(struct worktree *worktree)
 	}
 	printf("\n");
 }
-static void show_worktree(
-		struct worktree *worktree, int path_maxlen, int abbrev_len)
+
+static void show_worktree(struct worktree *worktree, int path_maxlen, int abbrev_len)
 {
 	struct strbuf sb = STRBUF_INIT;
 	int cur_path_len = strlen(worktree->path);
@@ -399,6 +399,22 @@ static void show_worktree(
 	strbuf_release(&sb);
 }
 
+static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
+{
+	int i;
+
+	for (i = 0; wt[i]; i++) {
+		int sha1_len;
+		int path_len = strlen(wt[i]->path);
+
+		if (path_len > *maxlen)
+			*maxlen = path_len;
+		sha1_len = strlen(find_unique_abbrev(wt[i]->head_sha1, *abbrev));
+		if (sha1_len > *abbrev)
+			*abbrev = sha1_len;
+	}
+}
+
 static int list(int ac, const char **av, const char *prefix)
 {
 	int porcelain = 0;
@@ -413,21 +429,11 @@ static int list(int ac, const char **av, const char *prefix)
 		usage_with_options(worktree_usage, options);
 	else {
 		struct worktree **worktrees = get_worktrees();
-		int path_maxlen = 0;
-		int abbrev = 0;
-		int i;
-
-		if (!porcelain) {
-			for (i = 0; worktrees[i]; i++) {
-				int path_len = strlen(worktrees[i]->path);
-				if (path_len > path_maxlen)
-					path_maxlen = path_len;
-				int sha1_len = strlen(
-						find_unique_abbrev(worktrees[i]->head_sha1, DEFAULT_ABBREV));
-				if (sha1_len > abbrev)
-					abbrev = sha1_len;
-			}
-		}
+		int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
+
+		if (!porcelain)
+			measure_widths(worktrees, &abbrev, &path_maxlen);
+
 		for (i = 0; worktrees[i]; i++) {
 			if (porcelain)
 				show_worktree_porcelain(worktrees[i]);
Previous: Michael Rappazzo
Message 8 of 8 in “worktree: list functions and command”
  1. 0/5 worktree: list functions and commandMichael Rappazzo, Oct 2, 2015
  2. 1/5 worktree: add top-level worktree.cMichael Rappazzo, Oct 2, 2015
  3. 2/5 worktree: refactor find_linked_symref functionMichael Rappazzo, Oct 2, 2015
  4. Junio C HamanoOct 2, 2015
  5. 3/5 worktree: add a function to get worktree detailsMichael Rappazzo, Oct 2, 2015
  6. 4/5 worktree: add details to the worktree structMichael Rappazzo, Oct 2, 2015
  7. 5/5 worktree: add 'list' commandMichael Rappazzo, Oct 2, 2015
  8. Junio C HamanoOct 2, 2015

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.