首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CLOC忽略/排除列表文件(.clocignore)

CLOC忽略/排除列表文件(.clocignore)
EN

Stack Overflow用户
提问于 2014-10-01 22:58:02
回答 4查看 31.2K关注 0票数 46

(编辑:请参阅底部的适当用法部分。)

主要问题

如何让cloc使用其--exclude-list-file=<file>选项?本质上,我试图将它作为一个.clocignore文件。

期望行为

cloc文档说明如下:

代码语言:javascript
复制
--exclude-list-file=<file>  Ignore files and/or directories whose names
                          appear in <file>.  <file> should have one entry
                          per line.  Relative path names will be resolved
                          starting from the directory where cloc is
                          invoked.  See also --list-file.

尝试

以下命令按预期工作:

代码语言:javascript
复制
cloc --exclude-dir=node_modules .

但是这个命令并不排除任何东西:

代码语言:javascript
复制
cloc --exclude-list-file=myignorefile .

这是myignorefile的内容

代码语言:javascript
复制
node_modules
node_modules/
node_modules/*
node_modules/**
./node_modules
./node_modules/
./node_modules/*
./node_modules/**
/full/path/to/current/directory/node_modules
/full/path/to/current/directory/node_modules/
/full/path/to/current/directory/node_modules/*
/full/path/to/current/directory/node_modules/**

如果cloc不存在,myignorefile不会出错,所以我对它所做的事情没有任何反馈。

(我正在运行OS,并通过国产安装了cloc v1.60。)

正确使用

tl;dr -- @Raman的答案中指定的方法既要求在.clocignore中指定的方法少,又运行得快得多。

受@Raman回答的启发,我研究了源代码:cloc实际上尊重--exclude-list-file,但在两种重要的方式上处理它与--exclude-dir不同。

确切的文件名与“部分路径”

首先,虽然--exclude-dir将忽略路径包含指定字符串的任何文件,但--exclude-list-file只会排除.clocignore中指定的确切文件或目录。

如果您有这样一个目录结构

代码语言:javascript
复制
.clocignore
node_modules/foo/first.js
app/node_modules/bar/second.js

.clocignore的内容

代码语言:javascript
复制
node_modules

然后cloc --exclude-list-file=.clocignore .将成功地忽略first.js,但计数second.js。而cloc --exclude-dir=node_modules .会忽略两者。

要解决这个问题,.clocignore需要包含以下内容:

代码语言:javascript
复制
node_modules
app/node_modules

性能

第二,cloc的源代码似乎将--exlude-dir中指定的目录添加到列表中,在计算文件之前会参考列表。然而,--exclude-list-file发现的目录列表是在对文件进行计数后查阅的。

这意味着,--exclude-list-file在忽略最终报告中的结果之前,仍然会处理这些文件,这可能是缓慢的。实验证明了这一点:在一个例子代码库中,用--exclude-dir运行cloc需要半秒钟,用等效的--exclude-list-file运行11秒。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-10-31 16:13:31

我发现的最佳解决方法是将.clocignore的内容直接提供给--exclude-dir。例如,如果您正在使用bash并使tr可用:

代码语言:javascript
复制
cloc --exclude-dir=$(tr '\n' ',' < .clocignore) .
票数 36
EN

Stack Overflow用户

发布于 2020-05-22 22:29:17

接受的答案对我无效,因为我也想指定子目录,只有使用--not-match-d="" regex参数才有可能。因此,我创建了一个PHP文件,该文件使用.clocignore文件生成整个CLOC命令(示例输出)

代码语言:javascript
复制
$ php cloc.php

cloc --fullpath --not-match-d="(node_modules|App/ios|App/android)" --not-match-f="(yarn\.lock|package\.json|package\-lock\.json)" .

该脚本基本上将目录路径内爆为一个regex字符串,并输出完整的cloc命令以便于复制。如果有人觉得有用,我就把它放在gist上:)

https://gist.github.com/Lukakva/a2ef7626724a809ff2859e7203accf53

票数 13
EN

Stack Overflow用户

发布于 2020-04-20 06:33:42

--not-match-d--not-match-f也可以满足您的需要。

--not-match-d=REGEX Count all files except in directories matching the Perl regex. Only the trailing directory name is compared, for example, when counting in "/usr/local/lib", only "lib" is compared to the regex. Add --fullpath to compare parent directories to the regex. Do not include file path separators at the beginning or end of the regex. --match-f=REGEX Only count files whose basenames match the Perl regex. For example this only counts files at start with Widget or widget: --match-f='^[Ww]idget' Add --fullpath to include parent directories in the regex instead of just the basename. --not-match-f=REGEX Count all files except those whose basenames match the Perl regex. Add --fullpath to include parent directories in the regex instead of just the basename.

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26152014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档