# "Git worktree list" on paths with newlines

3 messages from 2022-02-25 to 2022-02-28. Participants: Kang-Che Sung, Junio C Hamano, Eric Sunshine.
Thread: https://gitlist.dev/t/57473

## Kang-Che Sung, 2022-02-25 08:32

Subject: "Git worktree list" on paths with newlines
Message-ID: <CADDzAfNTuuAWn1ynswTayRqgNNcPn3ou=v6c+z_tRsu5uoqJZA@mail.gmail.com>
URL: https://gitlist.dev/e/CADDzAfNTuuAWn1ynswTayRqgNNcPn3ou%3Dv6c%2Bz_tRsu5uoqJZA%40mail.gmail.com

```
Dear Git developers,

I'm not sure if this is a good place to ask a question about the Git worktree
feature.

I'm writing a shell script that parses the "git worktree list --porcelain"
format, and I've run into trouble on determining the end of the worktree path.

Consider a repository with a "test1" branch, and I create a worktree through a
command like this:

$ git worktree add "$(printf 'directory\nHEAD\nbranch\n\nxyz')" test1

Git does allow me to create a worktree with newlines in its name (in a Unix
file system, of course). After that, "git worktree list --porcelain" would show
a somewhat tricked output, and it would break my parsing script.

(What my script does is find out what worktrees check out branches of a
specific pattern, and remove the worktrees found )

I wish "git worktree list --porcelain" would show directory names with some
quoting or escaping so that tricky names can be handled easily for any program
that reads the porcelain output. But I didn't see any command line option that
enables quoting or escaping of file names.

Does anyone have an idea on what I could do?

```

## Junio C Hamano, 2022-02-25 17:11

Subject: Re: "Git worktree list" on paths with newlines
Message-ID: <xmqqilt2g9cb.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqilt2g9cb.fsf%40gitster.g
In-Reply-To: <CADDzAfNTuuAWn1ynswTayRqgNNcPn3ou=v6c+z_tRsu5uoqJZA@mail.gmail.com>

```
Kang-Che Sung <explorer09@gmail.com> writes:

> I wish "git worktree list --porcelain" would show directory names with some
> quoting or escaping so that tricky names can be handled easily for any program
> that reads the porcelain output. But I didn't see any command line option that
> enables quoting or escaping of file names.
>
> Does anyone have an idea on what I could do?

I agree that an output mode that claims to be --porcelain should
produce output that is machine-parseable without ambiguity.

The usual practice is to use quote_c_style() for output without
"-z", and with "-z", just use the NUL as the termination character,
as you shouldn't be listing strings with embedded NUL.


```

## Eric Sunshine, 2022-02-28 08:43

Subject: Re: "Git worktree list" on paths with newlines
Message-ID: <CAPig+cT9H=rcQz8GhAL5RQP+iXYzYc56yn2eWpQPJz9K0P-zLw@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcT9H%3DrcQz8GhAL5RQP%2BiXYzYc56yn2eWpQPJz9K0P-zLw%40mail.gmail.com
In-Reply-To: <xmqqilt2g9cb.fsf@gitster.g>

```
On Fri, Feb 25, 2022 at 3:47 PM Junio C Hamano <gitster@pobox.com> wrote:
> Kang-Che Sung <explorer09@gmail.com> writes:
> > I wish "git worktree list --porcelain" would show directory names with some
> > quoting or escaping so that tricky names can be handled easily for any program
> > that reads the porcelain output. But I didn't see any command line option that
> > enables quoting or escaping of file names.
> >
> > Does anyone have an idea on what I could do?
>
> I agree that an output mode that claims to be --porcelain should
> produce output that is machine-parseable without ambiguity.
>
> The usual practice is to use quote_c_style() for output without
> "-z", and with "-z", just use the NUL as the termination character,
> as you shouldn't be listing strings with embedded NUL.

A couple additional comments...

Phillip has just submitted a patch to add a `-z` mode to `git worktree
list --porcelain`. You can see it at [1].

Porcelain mode should indeed be quoting the path using
quote_c_style(); the fact that it doesn't is a bug which would be nice
to fix. This has been discussed previously[2], however, there may be
backward compatibility concerns[3].

[1]: https://lore.kernel.org/git/pull.1164.git.1645801727732.gitgitgadget@gmail.com/
[2]: https://lore.kernel.org/git/CAPig+cQq_RnanDQ3jHfNz_L58WyzmsUJBhtdrLxa=H0v_io+WA@mail.gmail.com/
[3]: https://lore.kernel.org/git/936f9b7c-6d54-00bc-f136-4cb4c2836eb6@gmail.com/

```
