Skip to content

[ClangFormat] Print paths with null (git) #123921

@createyourpersonalaccount

Description

I use clang-format in my git pre-commit hook to format my diffs:

git clang-format --staged \
  | tail -n +2 \
  | xargs git add

This is not a secure git hook due to tricky filenames.

The issue with git clang-format --staged is that it lacks an option to print the affected paths with nulls, hence it is non-composable in the shell, see e.g. https://dwheeler.com/essays/filenames-in-shell.html.

The way to fix this is to provide an option like -z (xargs has -0, find has -z, etc) that will make the affected paths be printed with null bytes at their end. Then the above command could be changed to:

git clang-format --staged -z \
  | xargs -0 git add

and there would not be any issues with the parsing of the filenames by xargs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang-formatwontfixIssue is real, but we can't or won't fix it. Not invalid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions