How do I run a code formatter over my source without modifying git history?

后端 未结 5 1748
日久生厌
日久生厌 2020-11-30 10:35

I am trying to format an entire repo using a code formatter tool. In doing so, I want to keep information about who committed which line, so that commands like git bla

5条回答
  •  情深已故
    2020-11-30 10:51

    git filter-branch --tree-filter "find < dir > -regex '.*.(cpp\|h\|c\|< etc >)' -exec < formatter-command > {} \;" -- --all

    < dir > : directory of concerned, since above needs to be run from the root dir, but you may want to format only certain sub-dir under the root git dir.

    < etc > : other file formats.

    < formatter-command > : the command which you can run for a single file and it would format that file.

    --all at the end means to do this for all git branches (overall 4 dashes)

    E.g. this is what I have, wherein my git contains src directory (apart from tests, tools, etc)

    git filter-branch --tree-filter "find src -regex '.*.(cpp\|h\|cu\|inl)' -exec clang-format -style=google -i {} \;" -- --all

    Above will rewrite each git commit, but not change the git annotation. Since this modifies git history, everyone would have to reclone once this is pushed.

提交回复
热议问题