A little fast cloc(Count Lines Of Code)

gocloc

GoDoc ci Go Report Card Docker Pulls Docker Image Size

A little fast cloc(Count Lines Of Code), written in Go. Inspired by tokei.

Installation

$ go get -u github.com/hhatto/gocloc/cmd/gocloc

Usage

Basic Usage

$ gocloc .
$ gocloc .
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Markdown                         3              8              0             18
Go                               1             29              1            323
-------------------------------------------------------------------------------
TOTAL                            4             37              1            341
-------------------------------------------------------------------------------

Via Docker

with dockerhub

$ docker run --rm -v "${PWD}":/workdir hhatto/gocloc .

with GitHub Packages on GitHub Actions

jobs:
  build:
    name: example of code measurement using gocloc
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@master

      - name: Login GitHub Registry
        run: docker login docker.pkg.github.com -u owner -p ${{ secrets.GITHUB_TOKEN }}

      - name: Run gocloc
        run: docker run --rm -v "${PWD}":/workdir docker.pkg.github.com/hhatto/gocloc/gocloc:latest .

Integration Jenkins CI

use SLOCCount Plugin.

$ cloc --by-file --output-type=sloccount . > sloccount.scc
$ cat sloccount.scc
398 Go      ./main.go
190 Go      ./language.go
132 Markdown        ./README.md
24  Go      ./xml.go
18  Go      ./file.go
15  Go      ./option.go

Support Languages

use --show-lang option

$ gocloc --show-lang

Performance

cloc

$ time cloc .
    5171 text files.
    5052 unique files.
     420 files ignored.

https://github.com/AlDanial/cloc v 1.66  T=23.31 s (204.0 files/s, 48203.3 lines/s)
-----------------------------------------------------------------------------------
Language                         files          blank        comment           code
-----------------------------------------------------------------------------------
Go                                4197         101140         125939         780280
Assembly                           331           6128          14654          40531
HTML                                41           4927            198          33316
C                                   90           1076            940           7390
Perl                                12            185            177           1135
Bourne Again Shell                  25            209            266            933
XML                                  4             85              9            623
Bourne Shell                         8             71            302            467
Python                               1            121             88            295
DOS Batch                            5             55              1            238
JavaScript                           4             48            122            231
C/C++ Header                        15             50            147            211
CSS                                  3             51              9            176
yacc                                 1             27             20            155
Protocol Buffers                     1              1              0            144
Windows Resource File                4             25              0            116
JSON                                 2              0              0             36
make                                 7             12             10             35
Fortran 90                           2              1              3              8
C++                                  1              3              5              7
awk                                  1              1              6              7
-----------------------------------------------------------------------------------
SUM:                              4755         114216         142896         866334
-----------------------------------------------------------------------------------
cloc .  13.57s user 7.89s system 105% cpu 20.413 total

tokei

$ time tokei --sort code .
-------------------------------------------------------------------------------
 Language            Files        Total       Blanks     Comments         Code
-------------------------------------------------------------------------------
 Go                   4272      1027537       103241       150411       773970
 Plain Text             28            0            0            0       137715
 Assembly              334        61318         6130            0        55188
 HTML                   41        38441         4927          204        33316
 C                      92         9436         1081          946         7409
 BASH                   27         2134          260          570         1304
 XML                     4          717           85            9          623
 Perl                   10         1255          151         1096          343
 Python                  1          504          121           56          327
 Batch                   5          294           55            0          239
 JavaScript              4          401           48          122          231
 C Header               15          408           50          147          211
 CSS                     3          236           51            9          176
 Protocol Buffers        1          145            1            0          144
 Markdown                3            0            0            0          115
 JSON                    2            0            0            0           36
 Makefile                7           57           13           10           34
 FORTRAN Modern          2           12            1            3            8
 C++                     1           15            3            5            7
-------------------------------------------------------------------------------
 Total                4852      1142910       116218       153588      1011396
-------------------------------------------------------------------------------
tokei --sort code .  1.27s user 0.06s system 99% cpu 1.328 total

gocloc

$ time gocloc --exclude-ext=txt .
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Go                            4197         101140         133703         773058
Assembly                       331           6128          14694          40510
HTML                            41           4927            198          33316
C                               90           1076            991           7389
BASH                            28            257            539           1330
Perl                            12            185            177           1135
XML                              4             85              9            623
Python                           1            121             88            295
Batch                            5             55              1            238
JavaScript                       4             48            122            231
C Header                        15             50            147            211
CSS                              3             51              9            176
Yacc                             1             27             20            155
Protocol Buffers                 1              1              0            144
Markdown                         3             29              0             86
Plan9 Shell                      4             25             42             74
Bourne Shell                     5             23             29             70
JSON                             2              0              0             36
make                             7             13             10             34
FORTRAN Modern                   2              1              3              8
Awk                              1              1              6              7
C++                              1              3              5              7
-------------------------------------------------------------------------------
TOTAL                         4758         114246         150793         859133
-------------------------------------------------------------------------------
gocloc --exclude-ext=txt .  0.76s user 0.15s system 116% cpu 0.775 total

License

MIT

Owner
Comments
  • support output format options?

    support output format options?

    I am using this library. I want to convert the output format to csv, tsv in order to link with other aggregation devices, but do you plan to support the output format option?

  • Add Dockerfile with linting and testing

    Add Dockerfile with linting and testing

    Container size is minimized by the ldflags (which strip debugging symbols) and compressing via upx.

    After merging the PR, it would be nice if the dockerhub cloud builder could be activated to upload to image to Dockerhub for master=latest and $tags.

    This then allows to use gocloc e.g. on CI via: docker run -v ${PWD}:/workdir hhatto/gocloc . or docker run -v ${PWD}:/workdir hhatto/gocloc:$VERSION . (instead of wget $release && $hashcheck && tar -xf .. && chmod +x .. && gocloc .).

    Thanks!

  • go get instructions fail

    go get instructions fail

    go get complains trying to get gocloc.

    ~$ go get -u github.com/hhatto/gocloc/cmd/gocloc
    package github.com/src-d/enry: code in directory /home/martin/go/src/github.com/src-d/enry expects import "github.com/src-d/enry/v2"
    

    Not sure what it means for code to "expect" an import and why that is a problem.

  • Performance improvements

    Performance improvements

    Real time of gocloc github.com/spf13/hugo reduced from ~380ms to ~300ms on my laptop with this PR. Sorry for so non-detailed changes, but comments, questions and code reviews are welcome!

  • Question: how it works when use command

    Question: how it works when use command "gocloc ."

    It's quite useful to calculate how many codes are there in my project. After using it I try to implement a more simple one with a few same project to play. However something disturbed me a lot so I hope some one can help me. My question is how the command gocloc . call function to calculate lines of code. I knew that go get <this project address> download the package so I can use it in my project. But why gocloc . can invoke this package? What I know is only pass this argument with main args as go run main.go gocloc .. How this main function get the command info input at the terminal?

  • Installation failure on Windows

    Installation failure on Windows

    When I tried to install it on Windows without Go modules, I encountered the problem that is also mentioned in the repo's README (package github.com/src-d/enry: code in directory /PATH/TO/go/src/github.com/src-d/enry expects import "github.com/src-d/enry/v2").

    However, I could not install it using Go modules either. The error message is the following: go: cannot find main module; see 'go help modules'.

  • Unable to build gocloc

    Unable to build gocloc

    I get the following error when building gocloc.

    $ go get -u -ldflags '-s -w' github.com/hhatto/gocloc/cmd/gocloc
    package github.com/src-d/enry: code in directory /Users/jimi/go/src/github.com/src-d/enry expects import "github.com/src-d/enry/v2"
    
    $ go version
    go version go1.12.7 darwin/amd64
    
    $ sw_vers
    ProductName:	Mac OS X
    ProductVersion:	10.14.6
    BuildVersion:	18G84
    
  • Is it possible to not match multiple directories  --not-match-d

    Is it possible to not match multiple directories --not-match-d

    Hello, it's a nice project, I wanted to know if there was a way to exclude more than one folder ?

    If yes, how ? If no, will it be a big change to implement ?

  • proposal:  Split into a command and a library

    proposal: Split into a command and a library

    I would like to write some code to analyze a repository and write the stats into a backend time relational database and having the analysis from gocloc would be nice, but to write some custom code around it I would need it to be importable.

    I could fork but what do you think about creating a go package and separate cmd?

  • Incorrect comment count

    Incorrect comment count

    Consider this code:

    package main
    
    func main() {
    	var n string /*
    		comment
    		comment
    	*/
    }
    

    There are 4 code lines and 3 comment lines, but gocloc counts not as expected:

    -------------------------------------------------------------------------------
    Language                     files          blank        comment           code
    -------------------------------------------------------------------------------
    Go                               1              1              0              7
    -------------------------------------------------------------------------------
    TOTAL                            1              1              0              7
    -------------------------------------------------------------------------------
    
  • Make gocloc continue when encountering directory read errors

    Make gocloc continue when encountering directory read errors

    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
    -------------------------------------------------------------------------------
    
    1. 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.

  • Not covering most file types

    Not covering most file types

    Some files do not end with standard extensions and these files are not correctly recognized. Take the XML files as an example, only the files with the extension xml or XML are counted. In fact, many files that are not with these extensions may also be XML files, such as .fxml, .plist, .rss, .xproj and so on.

    refer here: https://gist.github.com/ppisarczyk/43962d06686722d26d176fad46879d41#file-programming_languages_extensions-json-L3109 https://github.com/AlDanial/cloc/blob/4d244d29a4b0f3c994ded12f7a0c57b8fe2906ef/cloc#L7581

    It is recommended to add these file extensions to improve accuracy.

  • \* Statistical exception

    \* Statistical exception

    gomod go get installed, version gocloc \ @ v0.3.1 Comprehensive code is regarded as a comment after return "/*" in go code

    gomod go get安装,版本[email protected] 在go代码中return "/*"后全面代码算为注释

    // getRouteByName 函数使用函数名称生成路由路径。
    func getRouteByName(name string) string {
    	names := splitName(name)
    	if checkAllowMethod(names[0]) {
    		names = names[1:]
    	}
    	if len(names) == 0 {
    		return "/*"
    	}
    	name = ""
    	for i := 0; i < len(names); i++ {
    		if names[i] == "By" {
    			name = name + "/:" + names[i+1]
    			i++
    		} else {
    			name = name + "/" + names[i]
    		}
    	}
    	return strings.ToLower(name)
    }
    
  • Some file types showing all lines as comments

    Some file types showing all lines as comments

    Steps to reproduce:

    go install github.com/hhatto/gocloc/cmd/gocloc git clone https://github.com/hhatto/gocloc.git cd gocloc gocloc Makefile

    produces:

    -------------------------------------------------------------------------------
    Language                     files          blank        comment           code
    -------------------------------------------------------------------------------
    Makefile                         1              4             11              0
    -------------------------------------------------------------------------------
    TOTAL                            1              4             11              0
    -------------------------------------------------------------------------------
    

    Relative to 'cloc':

           1 text file.
           1 unique file.                              
           0 files ignored.
    
    github.com/AlDanial/cloc v 1.82  T=0.02 s (61.8 files/s, 927.2 lines/s)
    -------------------------------------------------------------------------------
    Language                     files          blank        comment           code
    -------------------------------------------------------------------------------
    make                             1              4              0             11
    -------------------------------------------------------------------------------
    

    Suspected cause:

    Many of the Languages have the multiline quote definition of [][]string{{"", ""}} for language.multiLines

    But the test for marking as code is:

    if len(language.multiLines) == 0 {
      onCode(clocFile, opts, len(inComments) > 0, line, lineOrg)
      continue scannerloop
    }
    

    I've noticed this for Makefiles and bash scripts, so I think it's rather pervasive. I did notice some code to try to deal with Makefiles, not sure if that's part of this though. I think it's that the Language definition should be empty for these types of files that don't support multiline comments or the logic for detecting should deal with empty strings as if it were empty.

  • Show totals for each language

    Show totals for each language

    Currently, the totals are only displayed for each line type. It would be useful to see it for each language as well.

    For example, this:

    Language                     files          blank        comment       code
    -------------------------------------------------------------------------------
    Go                            16             306          238           1607
    XML                           7               0            0            209
    HCL                           5               19           0            134
    Bourne Shell                  1               6            21            1
    JSON                          2               0            15            0
    Markdown                      1              16            38            0 
    YAML                          2              10           352            0 
    -------------------------------------------------------------------------------
    TOTAL                         34            357           664           1951
    -------------------------------------------------------------------------------
    

    would become this:

    Language                     files          blank        comment       code        TOTAL
    -------------------------------------------------------------------------------------------
    Go                            16             306          238           1607       2167
    XML                           7               0            0            209        216
    HCL                           5               19           0            134        158
    Bourne Shell                  1               6            21            1         29
    JSON                          2               0            15            0         17
    Markdown                      1              16            38            0         55
    YAML                          2              10           352            0         364
    -------------------------------------------------------------------------------------------
    TOTAL                         34            357           664           1951      3006
    -------------------------------------------------------------------------------------------
    
  • Unable to build gocloc on raspberry pi

    Unable to build gocloc on raspberry pi

    I'm hitting this issue building gocloc on a raspberry pi.

    $ uname -sr
    Linux 4.19.55-Re4son-v8+
    
    $ go version
    go version go1.12.9 linux/arm64
    
    $ GO111MODULE=on go get -u -ldflags "-s -w" github.com/hhatto/gocloc/cmd/gocloc
    go: finding github.com/hhatto/gocloc/cmd/gocloc latest
    go: finding github.com/hhatto/gocloc/cmd latest
    go: finding gopkg.in/check.v1 latest
    go build github.com/src-d/enry/v2/data: /usr/lib/go-1.12/pkg/tool/linux_arm64/compile: signal: killed
    
A golang formatter that fixes long lines

golines Golines is a golang formatter that shortens long lines, in addition to all of the formatting fixes done by gofmt. Motivation The standard gola

Jan 6, 2023
octocov is a tool for collecting code metrics (code coverage, code to test ratio and test execution time).

octocov is a tool for collecting code metrics (code coverage, code to test ratio and test execution time).

Jan 9, 2023
The most opinionated Go source code linter for code audit.
The most opinionated Go source code linter for code audit.

go-critic Highly extensible Go source code linter providing checks currently missing from other linters. There is never too much static code analysis.

Jan 6, 2023
🐶 Automated code review tool integrated with any code analysis tools regardless of programming language
🐶 Automated code review tool integrated with any code analysis tools regardless of programming language

reviewdog - A code review dog who keeps your codebase healthy. reviewdog provides a way to post review comments to code hosting service, such as GitHu

Jan 2, 2023
A Golang tool that does static analysis, unit testing, code review and generate code quality report.
A Golang tool that does static analysis, unit testing, code review and generate code quality report.

goreporter A Golang tool that does static analysis, unit testing, code review and generate code quality report. This is a tool that concurrently runs

Jan 8, 2023
🐶 Automated code review tool integrated with any code analysis tools regardless of programming language
🐶 Automated code review tool integrated with any code analysis tools regardless of programming language

reviewdog - A code review dog who keeps your codebase healthy. reviewdog provides a way to post review comments to code hosting service, such as GitHu

Jan 7, 2023
Fast linters Runner for Go
Fast linters Runner for Go

golangci-lint Fast linters runner for Go golangci-lint is a fast Go linters runner. It runs linters in parallel, uses caching, supports yaml config, h

Jan 8, 2023
Fast division, modulus and divisibility checks in Go for divisors known only at runtime.

fastdiv Fast division, modulus and divisibility checks for divisors known only at runtime via the method of: "Faster Remainder by Direct Computation:

Jan 8, 2023
Bundle k6 with extensions as fast and easily as possible

xk6bundler xk6bundler is a CLI tool and GitHub Action makes bundle k6 with extensions as fast and easily as possible. Features Build for multiple targ

Nov 29, 2022
a tool for code clone detection

dupl dupl is a tool written in Go for finding code clones. So far it can find clones only in the Go source files. The method uses suffix tree for seri

Dec 12, 2022
[mirror] This is a linter for Go source code.

Golint is a linter for Go source code. Installation Golint requires a supported release of Go. go get -u golang.org/x/lint/golint To find out where g

Dec 23, 2022
Run linters from Go code -

Lint - run linters from Go Lint makes it easy to run linters from Go code. This allows lint checks to be part of a regular go build + go test workflow

Sep 27, 2022
depth is tool to retrieve and visualize Go source code dependency trees.

depth is tool to retrieve and visualize Go source code dependency trees. Install Download the appropriate binary for your platform from the Rele

Dec 30, 2022
A reference for the Go community that covers the fundamentals of writing clean code and discusses concrete refactoring examples specific to Go.

A reference for the Go community that covers the fundamentals of writing clean code and discusses concrete refactoring examples specific to Go.

Jan 1, 2023
Tool to populate your code with traceable and secure error codes

Essential part of any project, especially customer facing is proper and secure error handling. When error happens and customer reports it, it would be nice to know the context of the error and where it exactly occured.

Sep 28, 2022
A static code analyzer for annotated TODO comments
A static code analyzer for annotated TODO comments

todocheck todocheck is a static code analyzer for annotated TODO comments. It let's you create actionable TODOs by annotating them with issues from an

Dec 7, 2022
🔒🌍 Security scanner for your Terraform code
🔒🌍 Security scanner for your Terraform code

????tfsec uses static analysis of your terraform templates to spot potential security issues.

Dec 30, 2022
Know when GC runs from inside your golang code

gcnotifier gcnotifier provides a way to receive notifications after every run of the garbage collector (GC). Knowing when GC runs is useful to instruc

Dec 26, 2022
a Go code to detect leaks in JS files via regex patterns

a Go code to detect leaks in JS files via regex patterns

Nov 13, 2022