(Edit: see Proper Usage section on the bottom.)
Main Question
How do you get cloc
to use its --exclude-list-file=<file>
option? Essentially, I'm trying to feed it a .clocignore
file.
Expected Behavior
cloc
documentation says the following:
--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.
Attempts
The following command works as expected:
cloc --exclude-dir=node_modules .
But this command doesn't exclude anything:
cloc --exclude-list-file=myignorefile .
This is the contents of 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/**
cloc
does not error if myignorefile
doesn't exist, so I have no feedback on what it's doing.
(I'm running OS X and installed cloc
v1.60 via Homebrew.)
tl;dr -- The method specified in @Raman's answer both requires less to be specified in .clocignore
and runs considerably faster.
Spurred on by @Raman's answer, I investigated the source code: cloc
does in fact respect --exclude-list-file
but processes it differently than --exclude-dir
in two important ways.
First, while --exclude-dir
will ignore any files whose paths contain the specified strings, --exclude-list-file
will only exclude the exact files or directories specified in .clocignore
.
If you have a directory structure like this:
.clocignore node_modules/foo/first.js app/node_modules/bar/second.js
And the contents of .clocignore
is just
node_modules
Then cloc --exclude-list-file=.clocignore .
will successfully ignore first.js
but count second.js
. Whereas cloc --exclude-dir=node_modules .
will ignore both.
To deal with this, .clocignore
needs to contain this:
node_modules app/node_modules
Second, the source code for cloc
appears to add the directories specified in --exlude-dir
to a list which is consulted before counting the files. Whereas the list of directories discovered by --exclude-list-file
is consulted after counting the files.
Meaning, --exclude-list-file
still processes the files, which can be slow, before ignoring their results in the final report. This is borne out by experiment: in an example codebase, it took half a second to run cloc
with --exclude-dir
, and 11 seconds to run with an equivalent --exclude-list-file
.
The best workaround I've found is to feed the contents of .clocignore
directly to --exclude-dir
. For example, if you are using bash
and have tr
available:
cloc --exclude-dir=$(tr '\n' ',' < .clocignore) .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With