Skip to content

Commit 10139fc

Browse files
committed
worktree list: quote paths
Signed-off-by: Phillip Wood <[email protected]>
1 parent 957f176 commit 10139fc

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

builtin/worktree.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,7 @@ static void show_worktree_porcelain(struct worktree *wt, int line_terminator)
980980
}
981981

982982
struct worktree_display {
983+
char *path;
983984
int width;
984985
};
985986

@@ -989,7 +990,7 @@ static void show_worktree(struct worktree *wt, struct worktree_display *display,
989990
struct strbuf sb = STRBUF_INIT;
990991
const char *reason;
991992

992-
strbuf_addf(&sb, "%s%*s", wt->path, 1 + path_maxwidth - display->width, "");
993+
strbuf_addf(&sb, "%s%*s", display->path, 1 + path_maxwidth - display->width, "");
993994
if (wt->is_bare)
994995
strbuf_addstr(&sb, "(bare)");
995996
else {
@@ -1028,11 +1029,14 @@ static void measure_widths(struct worktree **wt, int *abbrev,
10281029
{
10291030
int i, display_alloc = 0;
10301031
struct worktree_display *display = NULL;
1032+
struct strbuf buf = STRBUF_INIT;
10311033

10321034
for (i = 0; wt[i]; i++) {
10331035
int sha1_len;
10341036
ALLOC_GROW(display, i + 1, display_alloc);
1035-
display[i].width = utf8_strwidth(wt[i]->path);
1037+
quote_path(wt[i]->path, NULL, &buf, 0);
1038+
display[i].width = utf8_strwidth(buf.buf);
1039+
display[i].path = strbuf_detach(&buf, NULL);
10361040

10371041
if (display[i].width > *maxwidth)
10381042
*maxwidth = display[i].width;
@@ -1104,6 +1108,8 @@ static int list(int ac, const char **av, const char *prefix,
11041108
show_worktree(worktrees[i],
11051109
&display[i], path_maxwidth, abbrev);
11061110
}
1111+
for (i = 0; display && worktrees[i]; i++)
1112+
free(display[i].path);
11071113
free(display);
11081114
free_worktrees(worktrees);
11091115
}

t/t2402-worktree-list.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ test_expect_success 'rev-parse --git-path objects linked worktree' '
2929
test_cmp expect actual
3030
'
3131

32-
test_expect_success '"list" all worktrees from main' '
32+
test_expect_success '"list" all worktrees from main core.quotepath=false' '
33+
test_config core.quotepath false &&
3334
echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
3435
test_when_finished "rm -rf áááá out actual expect && git worktree prune" &&
3536
git worktree add --detach áááá main &&
@@ -38,7 +39,19 @@ test_expect_success '"list" all worktrees from main' '
3839
test_cmp expect actual
3940
'
4041

42+
test_expect_success '"list" all worktrees from main core.quotepath=true' '
43+
test_config core.quotepath true &&
44+
echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
45+
test_when_finished "rm -rf á out actual expect && git worktree prune" &&
46+
git worktree add --detach á main &&
47+
echo "\"$(git -C á rev-parse --show-toplevel)\" $(git rev-parse --short HEAD) (detached HEAD)" |
48+
sed s/á/\\\\303\\\\241/g >>expect &&
49+
git worktree list >actual &&
50+
test_cmp expect actual
51+
'
52+
4153
test_expect_success '"list" all worktrees from linked' '
54+
test_config core.quotepath false &&
4255
echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
4356
test_when_finished "rm -rf áááá out actual expect && git worktree prune" &&
4457
git worktree add --detach áááá main &&

0 commit comments

Comments
 (0)