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]);