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

Shell CLOC忽略/排除列表文件(.clocignore),shell,cloc,Shell,Cloc,(编辑:请参见底部的正确用法部分。) 主要问题 如何使用它的--exclude list file=选项?本质上,我试图为它提供一个.clocignore文件 预期行为 cloc文档说明如下: --exclude-list-file=<file> Ignore files and/or directories whose names appear in <file>. <file> should have

(编辑:请参见底部的正确用法部分。)

主要问题

如何使用它的
--exclude list file=
选项?本质上,我试图为它提供一个
.clocignore
文件

预期行为

cloc
文档说明如下:

--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.
但此命令不排除任何内容:

cloc --exclude-list-file=myignorefile .
这是
myignorefile
的内容:

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/**
如果
myignorefile
不存在,
cloc
不会出错,因此我对它的功能没有任何反馈

(我正在运行OS X,并通过自制软件安装了
cloc
v1.60。)



正确使用 tl;dr--@Raman的答案中指定的方法在
.clocignore
中指定的次数更少,运行速度也更快


在@Raman的回答的激励下,我调查了源代码:
cloc
实际上尊重
--排除列表文件
,但在两个重要方面处理它与
--排除目录
不同

确切的文件名与“部分路径” 首先,
--exclude dir
将忽略路径包含指定字符串的任何文件,
--exclude list file
将只排除
.clocignore
中指定的确切文件或目录

如果您的目录结构如下所示:

.clocignore
node_modules/foo/first.js
app/node_modules/bar/second.js
.clocignore
的内容只是

node_modules
然后
cloc--exclude list file=.clocignore.
将成功忽略
first.js
,但count
second.js
。而
cloc--exclude dir=node\u模块。
将忽略这两个模块

要处理此问题,
.clocignore
需要包含以下内容:

node_modules
app/node_modules
演出 其次,
cloc
的源代码似乎将
--exlude dir
中指定的目录添加到一个列表中,该列表在计算文件之前会被查阅。而
--exclude list file
查找到的目录列表是在计算文件数后查询的


这意味着,
--排除列表文件
仍在处理文件,这可能会很慢,然后在最终报告中忽略其结果。实验证明了这一点:在一个示例代码库中,使用
--exclude dir
运行
cloc
需要半秒,使用等效的
--exclude list文件运行

需要11秒。我发现的最佳解决方法是将
.clocignore
的内容直接输入
--exclude dir
。例如,如果您正在使用
bash
并且
tr
可用:

cloc --exclude-dir=$(tr '\n' ',' < .clocignore) .
cloc--exclude dir=$(tr'\n'','<.clocignore)。
--not-match-d
--not-match-f
也可以满足您的需要


接受的答案对我不起作用,因为我也想指定子目录,这只能通过使用
--not-match-d=“”
regex参数来实现。因此,我创建了一个PHP文件,该文件使用.clocignore文件生成整个CLOC命令(示例输出)

该脚本基本上将目录路径内爆为单个正则表达式字符串,并输出完整的cloc命令以方便复制。如果有人觉得它有用,我会把它放在gist上:)


排除列表文件
对我的作用与
排除目录
不同,但我没有花任何时间找出原因。你可以看看这里的来源:谢谢。我进一步解释了为什么它在这个问题上似乎不起作用。希望它现在有意义。太好了,谢谢你的调查!太糟糕了,
——排除列表文件
不能像人们期望的那样工作。
cloc --exclude-dir=$(tr '\n' ',' < .clocignore) .
   --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.
$ php cloc.php

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