美文网首页
Mac上使用cloc进行代码函数统计

Mac上使用cloc进行代码函数统计

作者: HanlyJiang | 来源:发表于2019-08-21 16:11 被阅读0次

cloc-github

安装

安装 cloc

brew install cloc
cloc --version 
# version is 1.82

安装 guntar,并配置cloc命令

brew install gun-tar

将如下shell函数保存为文件,并在 .bash_profile 中引入

gist-cloc_wrapper.sh

#  ---
# 由于Mac上的tar版本是BSD的tar,没有 -A(catenate)选项,需要使用guntar替换,但是在安装了guntar之后,并不想用guntar作为默认的tar,下面的函数即实现这个功能。
# cloc (https://github.com/AlDanial/cloc) version is 1.82 
# some links : https://superuser.com/questions/318809/linux-os-x-tar-incompatibility-tarballs-created-on-os-x-give-errors-when-unt
# ---
function cloc(){
    # U need install 'gtar' first.( Mac: brew install gnu-tar)
    # check whether gtar is used
    tar --version | grep -iq 'gnu'
    isGnuTar=$?
    if [ $isGnuTar -eq 0 ]
    then
        # if default tar is not gtar
        if [ ! -d ~/_tmp_bin ];then
            mkdir ~/_tmp_bin
        fi
        ln -s `which gtar` ~/_tmp_bin/tar
        export PATH=~/_tmp_bin:$PATH
    fi
    # exec cloc
    /usr/local/bin/cloc $@

    if [ -f ~/_tmp_bin/tar ];then
        rm ~/_tmp_bin/tar
    fi
}

用法

统计某个目录

cloc .

统计某个分支

cloc f32163eeeb838352c
cloc --git remotes/origin/customEmojiMove

统计分支/Tag/提交差异

cloc  --git --diff f32163eeeb838352c 19b87d09ebeab8f25
cloc  --git --diff v1.4.4 develop
cloc --git --diff HEAD^1 HEAD 

输出如下:

    2123 text files.
     851 text files.s
      67 files ignored.

github.com/AlDanial/cloc v 1.82  T=7.96 s (0.1 files/s, 0.1 lines/s)
--------------------------------------------------------------------------------
Language                      files          blank        comment           code
--------------------------------------------------------------------------------
Java
 same                           123           2234           2057          10616
 modified                         0              0              0              0
 added                          553          10925          10191          52390
 removed                       1588          31044          56872         148470
XML
 same                             1              2              0              3
 modified                         0              0              0              0
 added                          142            885            161           6906
 removed                        320           1473            572          15363
Gradle
 same                             1              9              7             93
 modified                         0              0              0              0
 added                            6             40             32            315
 removed                         16            100             38            663
--------------------------------------------------------------------------------
SUM:
 same                           128           2276           2064          10859
 modified                         0              0              0              0
 added                          707          11901          10486          59730
 removed                       1950          32702          57690         164777
--------------------------------------------------------------------------------

过滤

不统计node_modules目录和static目录

cloc --exclude-dir=node_modules,static

报错处理

tar -A 执行失败

报错信息如下,使用guntar替换默认的tar即可

tar -A -f /var/folders/ng/z6s8jfyj015_d7lnxcl1zlw80000gn/T/hqAyjrxX_g.tar /var/folders/ng/z6s8jfyj015_d7lnxcl1zlw80000gn/T/hqAyjrxX_g.tar_extra
Usage:
  List:    tar -tf <archive-filename>
  Extract: tar -xf <archive-filename>
  Create:  tar -cf <archive-filename> [filenames...]
  Help:    tar --help

相关文章

网友评论

      本文标题:Mac上使用cloc进行代码函数统计

      本文链接:https://www.haomeiwen.com/subject/arkisctx.html