简体   繁体   English

仅从“ git diff”获取静态代码分析器消息?

[英]Get static code analyzer messages only from 'git diff'?

In our build system (with sources under git version control), I'd like to get the static code analyzer ( pylint , in this case) messages for every build. 在我们的构建系统中(源代码在git版本控制下),我想为每个构建获取静态代码分析器(在这种情况下为pylint )消息。 And I want them incremental: in the new build report, only the messages introduced by new commits are shown. 我希望它们是增量的:在新的构建报告中,仅显示新提交引入的消息。

I can easily get the 'old' and 'new' commits. 我可以轻松获得“旧”和“新”提交。 Then, the general path is to run the analyzer on the 'new' commit, and then, for each source code line with a message, find whether that line is new/modified, or if it is intact from the 'old' commit. 然后,通常的路径是在“新”提交上运行分析器,然后对于带有消息的每个源代码行,查找该行是否是新的/已修改的,或者它是否与“旧”的提交完好无损。 Then, print only messages from new/modified lines. 然后,仅打印来自新行/修改行的消息。

Is there a simpler way of achieving this goal for pylint ? 有没有一种简单的方法可以实现pylint目标? Or is there an existing implementation of something similar? 还是有类似的现有实现?

You can get list of files changed between commits and pass it to pylint then. 您可以获取两次提交之间更改的文件列表,然后将其传递给pylint。 With github pullrequest workflow it looks like this: 使用github pullrequest工作流,它看起来像这样:

git --no-pager diff --name-only FETCH_HEAD $(git merge-base FETCH_HEAD master) | grep .py | xargs pylint

The git diff-tree command has a --diff-filter argument that can be used to filter for a specific type of modification: git diff-tree命令具有--diff-filter参数,可用于过滤特定类型的修改:

From the git diff-tree documentation : git diff-tree文档中

--diff-filter=[(A|C|D|M|R|T|U|X|B)…​[*]] --diff-filter = [(A | C | D | M | R | T | U | X | B)…[[*]]
Select only files that are Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), have their type (ie regular file, symlink, submodule, …​) changed (T), are Unmerged (U), are Unknown (X), or have had their pairing Broken (B). 仅选择已更改(T)的已添加(A),已复制(C),已删除(D),已修改(M),已重命名(R),其类型(即常规文件,符号链接,子模块等)的文件,是未合并(U),是未知(X)或配对已损坏(B)。 Any combination of the filter characters (including none) can be used. 可以使用过滤器字符的任何组合(包括无过滤器字符)。

If you want to get a list of the names of all added files you could use a command like this: 如果要获取所有已添加文件的名称的列表,可以使用如下命令:

git diff-tree -r --name-only --diff-filter A <oldCommit>..<newCommit>

The -r option in this case means to diff the trees recursively. 在这种情况下, -r选项意味着以递归方式区分树。 The --name-only option causes only the file names to be printed, without their status and prevents printing of the diff header. --name-only选项仅导致文件名被打印而不显示其状态,并防止打印diff标头。

Since this is one of the git diff commands, it also takes additional arguments that allow filtering the file list. 由于这是git diff命令之一,因此它还采用了允许过滤文件列表的其他参数。 If you are only interested in files ending in '.py' at a certain path you could add -- 'path/*.py' to the command. 如果您只对在某个路径下以'.py'结尾的文件感兴趣,则可以在命令中添加-- 'path/*.py' 。py'。

If the list of files is sent further to a tool like xargs , it may make sense to add the -z option. 如果将文件列表进一步发送给xargs类的工具,则添加-z选项可能很有意义。 This option causes the list of filenames to be delimited by a null byte instead of a newline. 此选项使文件名列表由空字节而不是换行符分隔。 This makes the command robust against various special characters that may occur in file names (including spaces). 这使得该命令可以抵抗文件名(包括空格)中可能出现的各种特殊字符。 The receiving end then needs to made aware of that. 然后,接收端需要意识到这一点。 For xargs the option to use would be --null . 对于xargs ,使用的选项为--null

A complete command that runs pylint on added files with the '.py' extension between two commits could therefore look like this: 因此,在两次提交之间以'.py'扩展名在添加的文件上运行pylint完整命令可能如下所示:

git diff-tree -r -z --name-only --diff-filter A <oldCommit>..<newCommit> -- '*.py' | xargs --null pylint

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM