hhatto / gocloc

A little fast cloc(Count Lines Of Code)
MIT License
799 stars 79 forks source link

Make gocloc continue when encountering directory read errors #57

Closed k00l-beanz closed 2 years ago

k00l-beanz commented 2 years ago

As of right now, gocloc does not return any results if it encounters a directory it cannot read.

Here's my small PoC.

  1. Gocloc scanning a clean clone of 'thefuck' repository: https://github.com/nvbn/thefuck/
    $ ./gocloc ~/Documents/Repos/test_cases/gocloc/tests/thefuck/
    Results:
    -------------------------------------------------------------------------------
    Language                     files          blank        comment           code
    -------------------------------------------------------------------------------
    Python                         404           3591            733          11981
    Markdown                         3            138              0            541
    JSON                             1              3              0             36
    YAML                             1              3              0             22
    Plain Text                       1              0              0             11
    Bourne Shell                     1              1              0              3
    -------------------------------------------------------------------------------
    TOTAL                          411           3736            733          12594
    -------------------------------------------------------------------------------
  2. Change permissions of 'tests' directory
    # ls -la 
    drwxrwxr-x 8 test test   4096 Sep 13 11:46 tests
    # chown root:root tests
    # chmod 600 tests
    $ cd tests
    -bash: cd: tests/: Permission denied
    $ ./gocloc ~/Documents/Repos/test_cases/gocloc/tests/thefuck/
    fail gocloc analyze. error: open /home/maximillian/Documents/Repos/test_cases/gocloc/tests/thefuck/tests: permission denied

    It would be nice to have gocloc continue upon read errors. Something like:

    $ ./gocloc ~/Documents/Repos/test_cases/gocloc/tests/thefuck/
    open /home/test/Documents/Repos/test_cases/gocloc/tests/thefuck/tests: permission denied
    -------------------------------------------------------------------------------
    Language                     files          blank        comment           code
    -------------------------------------------------------------------------------
    Python                         209           1630            470           4658
    Markdown                         3            138              0            541
    JSON                             1              3              0             36
    YAML                             1              3              0             22
    Plain Text                       1              0              0             11
    Bourne Shell                     1              1              0              3
    -------------------------------------------------------------------------------
    TOTAL                          216           1775            470           5271
    -------------------------------------------------------------------------------

    Which now shows which directories are denied.

Not a problem with small projects. When using gocloc on larger projects this can cause gocloc to hang for a while before erroring out.

Sorry for hacky solution. I am not proficient in Go.

hhatto commented 2 years ago

Thanks for your contribution 👍