Skip to content

Commit 7c6d8ee

Browse files
committed
Merge branch 'pw/worktree-list-with-z'
"git worktree list --porcelain" did not c-quote pathnames and lock reasons with unsafe bytes correctly, which is worked around by introducing NUL terminated output format with "-z". * pw/worktree-list-with-z: worktree: add -z option for list subcommand
2 parents 77ceb11 + d97eb30 commit 7c6d8ee

File tree

3 files changed

+55
-20
lines changed

3 files changed

+55
-20
lines changed

Documentation/git-worktree.txt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SYNOPSIS
1010
--------
1111
[verse]
1212
'git worktree add' [-f] [--detach] [--checkout] [--lock [--reason <string>]] [-b <new-branch>] <path> [<commit-ish>]
13-
'git worktree list' [-v | --porcelain]
13+
'git worktree list' [-v | --porcelain [-z]]
1414
'git worktree lock' [--reason <string>] <worktree>
1515
'git worktree move' <worktree> <new-path>
1616
'git worktree prune' [-n] [-v] [--expire <expire>]
@@ -224,7 +224,14 @@ This can also be set up as the default behaviour by using the
224224
--porcelain::
225225
With `list`, output in an easy-to-parse format for scripts.
226226
This format will remain stable across Git versions and regardless of user
227-
configuration. See below for details.
227+
configuration. It is recommended to combine this with `-z`.
228+
See below for details.
229+
230+
-z::
231+
Terminate each line with a NUL rather than a newline when
232+
`--porcelain` is specified with `list`. This makes it possible
233+
to parse the output when a worktree path contains a newline
234+
character.
228235

229236
-q::
230237
--quiet::
@@ -416,7 +423,8 @@ worktree itself.
416423

417424
Porcelain Format
418425
~~~~~~~~~~~~~~~~
419-
The porcelain format has a line per attribute. Attributes are listed with a
426+
The porcelain format has a line per attribute. If `-z` is given then the lines
427+
are terminated with NUL rather than a newline. Attributes are listed with a
420428
label and value separated by a single space. Boolean attributes (like `bare`
421429
and `detached`) are listed as a label only, and are present only
422430
if the value is true. Some attributes (like `locked`) can be listed as a label
@@ -454,7 +462,7 @@ prunable gitdir file points to non-existent location
454462

455463
------------
456464

457-
If the lock reason contains "unusual" characters such as newline, they
465+
Unless `-z` is used any "unusual" characters in the lock reason such as newlines
458466
are escaped and the entire reason is quoted as explained for the
459467
configuration variable `core.quotePath` (see linkgit:git-config[1]).
460468
For Example:

builtin/worktree.c

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -651,35 +651,37 @@ static int add(int ac, const char **av, const char *prefix)
651651
return add_worktree(path, branch, &opts);
652652
}
653653

654-
static void show_worktree_porcelain(struct worktree *wt)
654+
static void show_worktree_porcelain(struct worktree *wt, int line_terminator)
655655
{
656656
const char *reason;
657657

658-
printf("worktree %s\n", wt->path);
658+
printf("worktree %s%c", wt->path, line_terminator);
659659
if (wt->is_bare)
660-
printf("bare\n");
660+
printf("bare%c", line_terminator);
661661
else {
662-
printf("HEAD %s\n", oid_to_hex(&wt->head_oid));
662+
printf("HEAD %s%c", oid_to_hex(&wt->head_oid), line_terminator);
663663
if (wt->is_detached)
664-
printf("detached\n");
664+
printf("detached%c", line_terminator);
665665
else if (wt->head_ref)
666-
printf("branch %s\n", wt->head_ref);
666+
printf("branch %s%c", wt->head_ref, line_terminator);
667667
}
668668

669669
reason = worktree_lock_reason(wt);
670-
if (reason && *reason) {
671-
struct strbuf sb = STRBUF_INIT;
672-
quote_c_style(reason, &sb, NULL, 0);
673-
printf("locked %s\n", sb.buf);
674-
strbuf_release(&sb);
675-
} else if (reason)
676-
printf("locked\n");
670+
if (reason) {
671+
fputs("locked", stdout);
672+
if (*reason) {
673+
fputc(' ', stdout);
674+
write_name_quoted(reason, stdout, line_terminator);
675+
} else {
676+
fputc(line_terminator, stdout);
677+
}
678+
}
677679

678680
reason = worktree_prune_reason(wt, expire);
679681
if (reason)
680-
printf("prunable %s\n", reason);
682+
printf("prunable %s%c", reason, line_terminator);
681683

682-
printf("\n");
684+
fputc(line_terminator, stdout);
683685
}
684686

685687
static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
@@ -757,12 +759,15 @@ static void pathsort(struct worktree **wt)
757759
static int list(int ac, const char **av, const char *prefix)
758760
{
759761
int porcelain = 0;
762+
int line_terminator = '\n';
760763

761764
struct option options[] = {
762765
OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
763766
OPT__VERBOSE(&verbose, N_("show extended annotations and reasons, if available")),
764767
OPT_EXPIRY_DATE(0, "expire", &expire,
765768
N_("add 'prunable' annotation to worktrees older than <time>")),
769+
OPT_SET_INT('z', NULL, &line_terminator,
770+
N_("terminate records with a NUL character"), '\0'),
766771
OPT_END()
767772
};
768773

@@ -772,6 +777,8 @@ static int list(int ac, const char **av, const char *prefix)
772777
usage_with_options(worktree_usage, options);
773778
else if (verbose && porcelain)
774779
die(_("options '%s' and '%s' cannot be used together"), "--verbose", "--porcelain");
780+
else if (!line_terminator && !porcelain)
781+
die(_("the option '%s' requires '%s'"), "-z", "--porcelain");
775782
else {
776783
struct worktree **worktrees = get_worktrees();
777784
int path_maxlen = 0, abbrev = DEFAULT_ABBREV, i;
@@ -784,7 +791,8 @@ static int list(int ac, const char **av, const char *prefix)
784791

785792
for (i = 0; worktrees[i]; i++) {
786793
if (porcelain)
787-
show_worktree_porcelain(worktrees[i]);
794+
show_worktree_porcelain(worktrees[i],
795+
line_terminator);
788796
else
789797
show_worktree(worktrees[i], path_maxlen, abbrev);
790798
}

t/t2402-worktree-list.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,25 @@ test_expect_success '"list" all worktrees --porcelain' '
6464
test_cmp expect actual
6565
'
6666

67+
test_expect_success '"list" all worktrees --porcelain -z' '
68+
test_when_finished "rm -rf here _actual actual expect &&
69+
git worktree prune" &&
70+
printf "worktree %sQHEAD %sQbranch %sQQ" \
71+
"$(git rev-parse --show-toplevel)" \
72+
$(git rev-parse HEAD --symbolic-full-name HEAD) >expect &&
73+
git worktree add --detach here main &&
74+
printf "worktree %sQHEAD %sQdetachedQQ" \
75+
"$(git -C here rev-parse --show-toplevel)" \
76+
"$(git rev-parse HEAD)" >>expect &&
77+
git worktree list --porcelain -z >_actual &&
78+
nul_to_q <_actual >actual &&
79+
test_cmp expect actual
80+
'
81+
82+
test_expect_success '"list" -z fails without --porcelain' '
83+
test_must_fail git worktree list -z
84+
'
85+
6786
test_expect_success '"list" all worktrees with locked annotation' '
6887
test_when_finished "rm -rf locked unlocked out && git worktree prune" &&
6988
git worktree add --detach locked main &&

0 commit comments

Comments
 (0)