git - 在一系列 git 提交上运行 git-clang-format

我写了一系列的 git 提交,代码格式很糟糕。
在将它们推送到 github 之前,我想在每次提交时运行 git-clang-format,以在我的历史记录中获得格式良好的代码。

有没有 rebasegit-clang-format 的组合可以完成这个?

最佳答案

这看起来像是 git filter-branch 的工作,它可以重写你想要的提交。由于尚未推送这些提交,因此更改它们的内容(以及因此它们的 SHA1)并不是什么大问题。
其效果类似于 rebase 或cherry-picking 的效果,除了您可以为每个重放的提交运行任何命令。

您可以run a filter-branch over the last few commits :

参见“Reformatting Your Codebase with git filter-branch”,作者 Elliot Chance

git filter-branch --tree-filter 'git-clang-format' -- <SHA1>..HEAD

考虑到 git-clang-format syntax ,您只能将其应用于每次提交中更改的文件。
例如,对于 .cpp 文件:

git filter-branch --tree-filter 'git-clang-format $(\
  git diff-index --diff-filter=AM --name-only $GIT_COMMIT |\
    grep .cpp)' -- <SHA1>..HEAD

2017 年更新,使用 Git 2.14.x/2.15(2017 年第四季度)您有一个说明:

见 commit 2118805 , commit 6134de6 (2017 年 8 月 14 日)作者:Brandon Williams (mbrandonw) .
(由 Junio C Hamano -- gitster -- 在 commit a36f631 中 merge ,2017 年 9 月 25 日)

Makefile:添加样式构建规则

Add the 'style' build rule which will run git-clang-format on the diff between HEAD and the current worktree.
The result is a diff of suggested changes.

.PHONY: style
style:
    git clang-format --style file --diff --extensions c,h

https://stackoverflow.com/questions/43974371/

相关文章:

php - 定义跨平台money_format函数(Linux和Windows)

java - 在 String.format() 中选择参数

c# - 在 C# 中格式化大数

c# - JSON 序列化输出具有 C# 转义格式

python - 使用 Python 在 OpenOffice/Microsoft Word 中格式

java - 通过 HotKey 在 Eclipse 中防止代码格式化

c# - 将电话号码解析为其部分

regex - 正则表达式中的 C# 字符串格式占位符

c# - 使 Console.WriteLine() 换行而不是字母

c++ - 使用 boost::date_time 库以毫秒为单位格式化时间