如何使用 clang-format 3.9 忽略文件或目录

2024-03-06

我目前正在使用 travis ci 在补丁进入 github 时检查补丁,并试图找出 clang-format 3.9 是否有(因为 travis ci 目前仅支持最新的 ubuntu 14.04)在扫描时忽略整个目录或文件变化。

我的 .travis.yml 文件:

language: c++
sudo: required
dist: trusty
install:
- sudo apt-get update
- sudo apt-get install clang-format-3.9 python3
- ./travisci/check_patch.py

我的 travisci/check_patch.py​​ 文件:

#!/usr/bin/env python3

from subprocess import Popen, PIPE, STDOUT

# Run clang to check if code changes cause a diff output and return 1 if so.
cmd = "git show origin/master..@ | clang-format-diff-3.9 -p 1 -style=file"
diff = Popen(cmd, stdout=PIPE, shell=True).communicate()[0]
if diff:
    print("Code formatting is not according to style guidelines. Read https://github.com/intel/IA-Hardware-Composer/wiki/Contributions#coding_style")
    exit(1)

exit(0)

个人files no, but 目录,是的.

As said here https://stackoverflow.com/a/56243326/2656413,你可以放一个新的.clang-format- 包含不被格式化的文件的文件夹内的文件。

示例:我有一个包含仅标头库的项目,例如cppzmq https://github.com/zeromq/cppzmq/我只想my更新库时要格式化的源文件以保持较小的差异。所以我创建了一个布局,例如:

project/
├ include/
│ ├ 3rdparty/
│ │ ├ .clang-format   (1)
│ │ └ zmq.hpp
│ └ my_app.hpp
├ src/
│ └ my_app.cpp
└ .clang-format       (2)

哪里的first .clang-format holds:

{
    "DisableFormat": true,
    "SortIncludes": "Never"  // with clang-format version < 13 use `false` here.
}

(DisableFormat似乎没有禁用包含排序,因此必须明确给出。)

The second .clang-format保存您常用的 clang-format 配置。

确保您的全局/项目级别 clang-formatstyle设置被设置为File.


Edit:如果您的 clang-format 抱怨第二行的值无效,请添加尾随逗号:

{
    "DisableFormat": true,
    "SortIncludes": "Never",
}

或者使用 YAML 语法而不是 JSON:

DisableFormat: true
SortIncludes: Never
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 clang-format 3.9 忽略文件或目录 的相关文章

随机推荐