{"thread":{"id":"57473","subject":"\"Git worktree list\" on paths with newlines","startedAt":"2022-02-25T08:32:51Z","lastAt":"2022-02-28T08:43:58Z","messageCount":3,"participants":["Kang-Che Sung","Junio C Hamano","Eric Sunshine"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"449554","messageId":"CADDzAfNTuuAWn1ynswTayRqgNNcPn3ou=v6c+z_tRsu5uoqJZA@mail.gmail.com","threadId":"57473","inReplyTo":null,"subject":"\"Git worktree list\" on paths with newlines","fromName":"Kang-Che Sung","fromEmail":"explorer09@gmail.com","sentAt":"2022-02-25T08:32:37Z","receivedAt":"2022-02-25T08:32:51Z","isPatch":false,"sender":{"key":"explorer09@gmail.com","avatar":null},"body":"Dear Git developers,\n\nI'm not sure if this is a good place to ask a question about the Git worktree\nfeature.\n\nI'm writing a shell script that parses the \"git worktree list --porcelain\"\nformat, and I've run into trouble on determining the end of the worktree path.\n\nConsider a repository with a \"test1\" branch, and I create a worktree through a\ncommand like this:\n\n$ git worktree add \"$(printf 'directory\\nHEAD\\nbranch\\n\\nxyz')\" test1\n\nGit does allow me to create a worktree with newlines in its name (in a Unix\nfile system, of course). After that, \"git worktree list --porcelain\" would show\na somewhat tricked output, and it would break my parsing script.\n\n(What my script does is find out what worktrees check out branches of a\nspecific pattern, and remove the worktrees found )\n\nI wish \"git worktree list --porcelain\" would show directory names with some\nquoting or escaping so that tricky names can be handled easily for any program\nthat reads the porcelain output. But I didn't see any command line option that\nenables quoting or escaping of file names.\n\nDoes anyone have an idea on what I could do?\n"},{"id":"449608","messageId":"xmqqilt2g9cb.fsf@gitster.g","threadId":"57473","inReplyTo":"CADDzAfNTuuAWn1ynswTayRqgNNcPn3ou=v6c+z_tRsu5uoqJZA@mail.gmail.com","subject":"Re: \"Git worktree list\" on paths with newlines","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-02-25T17:11:16Z","receivedAt":"2022-02-25T17:11:22Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Kang-Che Sung <explorer09@gmail.com> writes:\n\n> I wish \"git worktree list --porcelain\" would show directory names with some\n> quoting or escaping so that tricky names can be handled easily for any program\n> that reads the porcelain output. But I didn't see any command line option that\n> enables quoting or escaping of file names.\n>\n> Does anyone have an idea on what I could do?\n\nI agree that an output mode that claims to be --porcelain should\nproduce output that is machine-parseable without ambiguity.\n\nThe usual practice is to use quote_c_style() for output without\n\"-z\", and with \"-z\", just use the NUL as the termination character,\nas you shouldn't be listing strings with embedded NUL.\n\n"},{"id":"449730","messageId":"CAPig+cT9H=rcQz8GhAL5RQP+iXYzYc56yn2eWpQPJz9K0P-zLw@mail.gmail.com","threadId":"57473","inReplyTo":"xmqqilt2g9cb.fsf@gitster.g","subject":"Re: \"Git worktree list\" on paths with newlines","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2022-02-28T08:43:45Z","receivedAt":"2022-02-28T08:43:58Z","isPatch":false,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, Feb 25, 2022 at 3:47 PM Junio C Hamano <gitster@pobox.com> wrote:\n> Kang-Che Sung <explorer09@gmail.com> writes:\n> > I wish \"git worktree list --porcelain\" would show directory names with some\n> > quoting or escaping so that tricky names can be handled easily for any program\n> > that reads the porcelain output. But I didn't see any command line option that\n> > enables quoting or escaping of file names.\n> >\n> > Does anyone have an idea on what I could do?\n>\n> I agree that an output mode that claims to be --porcelain should\n> produce output that is machine-parseable without ambiguity.\n>\n> The usual practice is to use quote_c_style() for output without\n> \"-z\", and with \"-z\", just use the NUL as the termination character,\n> as you shouldn't be listing strings with embedded NUL.\n\nA couple additional comments...\n\nPhillip has just submitted a patch to add a `-z` mode to `git worktree\nlist --porcelain`. You can see it at [1].\n\nPorcelain mode should indeed be quoting the path using\nquote_c_style(); the fact that it doesn't is a bug which would be nice\nto fix. This has been discussed previously[2], however, there may be\nbackward compatibility concerns[3].\n\n[1]: https://lore.kernel.org/git/pull.1164.git.1645801727732.gitgitgadget@gmail.com/\n[2]: https://lore.kernel.org/git/CAPig+cQq_RnanDQ3jHfNz_L58WyzmsUJBhtdrLxa=H0v_io+WA@mail.gmail.com/\n[3]: https://lore.kernel.org/git/936f9b7c-6d54-00bc-f136-4cb4c2836eb6@gmail.com/\n"}]}