From: Phillip Wood via GitGitGadget Date: Thu, 31 Mar 2022 16:21:28 GMT Subject: [PATCH v2] worktree: add -z option for list subcommand Message-ID: In-Reply-To: From: Phillip Wood Add a -z option to be used in conjunction with --porcelain that gives NUL-terminated output. As 'worktree list --porcelain' does not quote worktree paths this enables it to handle worktree paths that contain newlines. Signed-off-by: Phillip Wood --- worktree: add -z option for list subcommand Thanks to Eric, Jean-Noël and Junio for their comments on V1. I've reworded the docs and option help and tweaked the tests as suggested by Eric, fixed the error messages as suggested by Eric/Jean-Noël and changed the implementation to use write_name_quoted() as suggested by Junio. I've punted doing anything about quoting the output without -z for now, I'll fix that with and without --porcelain in another series. V1 Cover Letter: Add a -z option to be used in conjunction with --porcelain that gives NUL-terminated output. This enables 'worktree list --porcelain' to handle worktree paths that contain newlines. For a previous discussion of the merits of adding a -z option vs quoting the worktree path see https://lore.kernel.org/git/CAPig+cT-9sjmkdWFEcFS=rg9ziV9b6uWNMpQ8BTYP-a258La6Q@mail.gmail.com/ Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1164%2Fphillipwood%2Fwip%2Fworktree-list-nul-termination-v2 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1164/phillipwood/wip/worktree-list-nul-termination-v2 Pull-Request: https://github.com/gitgitgadget/git/pull/1164 Range-diff vs v1: 1: b954579189b ! 1: 5f0e0213583 worktree: add -z option for list subcommand @@ Commit message worktree: add -z option for list subcommand Add a -z option to be used in conjunction with --porcelain that gives - NUL-terminated output. This enables 'worktree list --porcelain' to - handle worktree paths that contain newlines. + NUL-terminated output. As 'worktree list --porcelain' does not quote + worktree paths this enables it to handle worktree paths that contain + newlines. Signed-off-by: Phillip Wood @@ Documentation/git-worktree.txt: This can also be set up as the default behaviour + See below for details. + +-z:: -+ When `--porcelain` is specified with `list` terminate each line with a -+ NUL rather than a newline. This makes it possible to parse the output -+ when a worktree path contains a newline character. ++ Terminate each line with a NUL rather than a newline when ++ `--porcelain` is specified with `list`. This makes it possible ++ to parse the output when a worktree path contains a newline ++ character. -q:: --quiet:: @@ builtin/worktree.c: static int add(int ac, const char **av, const char *prefix) } reason = worktree_lock_reason(wt); - if (reason && *reason) { - struct strbuf sb = STRBUF_INIT; +- if (reason && *reason) { +- struct strbuf sb = STRBUF_INIT; - quote_c_style(reason, &sb, NULL, 0); - printf("locked %s\n", sb.buf); -+ if (line_terminator) { -+ quote_c_style(reason, &sb, NULL, 0); -+ reason = sb.buf; -+ } -+ printf("locked %s%c", reason, line_terminator); - strbuf_release(&sb); - } else if (reason) +- strbuf_release(&sb); +- } else if (reason) - printf("locked\n"); -+ printf("locked%c", line_terminator); ++ if (reason) { ++ fputs("locked", stdout); ++ if (*reason) { ++ fputc(' ', stdout); ++ write_name_quoted(reason, stdout, line_terminator); ++ } else { ++ fputc(line_terminator, stdout); ++ } ++ } reason = worktree_prune_reason(wt, expire); if (reason) @@ builtin/worktree.c: static void pathsort(struct worktree **wt) OPT_EXPIRY_DATE(0, "expire", &expire, N_("add 'prunable' annotation to worktrees older than