clang-format可以告诉我是否需要更改格式吗? [英] Can clang-format tell me if formatting changes are necessary?

查看:197
本文介绍了clang-format可以告诉我是否需要更改格式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在报告文件是否符合指定格式的模式下运行clang-format?一种空运行模式,它报告是否需要更改,但不进行更改.理想情况下,如果文件需要更改,我希望clang格式仅返回非零退出代码.或者,更理想的情况是,非零退出代码和标准输出中需要更改的文件列表.

Is there an way that you can run clang-format in a mode where it reports if the file meets the specified format? A kind of dry-run mode where it reports if a change is needed, but doesn't make the change. Ideally I'd like clang-format to just return a non-zero exit code if the file needs changes. Or, even more ideally, a non-zero exit code and a list of the files that need changes on standard output.

我正在尝试使问题通用,以便更多人可以回答,但是我正在尝试做的是编写一个git pre-commit钩子,该钩子将拒绝任何与预期的.clang-不匹配的提交.格式 .在索引中的文件列表上运行clang格式很容易.但是很难知道clang格式是否真的改变了任何东西.

I'm trying to keep the question generic, so that more people can answer, but what I am trying to do is write a git pre-commit hook that will reject any commits that don't match the expected .clang-format . It's easy to run clang-format on the list of files in the index. But it's hard to know if clang-format actually changed anything.

我有一个基于-output-replacements-xml的潜在解决方案(我将作为答案发布),但这是一个hack,我觉得这应该更简单.欢迎评论/建议,编辑,不同的答案/方法.

I have one potential solution based on -output-replacements-xml (that I will post as an answer), but it's a hack and I feel like this should be more straightforward. Comments/suggestions, edits, different answers/approaches are all welcome.

推荐答案

run-clang-format 是围绕clang-format的简单包装,专门用于钩子或连续集成脚本:它输出diff并以明智的状态退出.

run-clang-format is a simple wrapper around clang-format designed precisely to be used as a hook or as a continuous integration script: it outputs a diff and exits with a sensible status.

主页上给出的示例说明了一切:

The example given on the home page speaks for itself:

这篇关于clang-format可以告诉我是否需要更改格式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆