4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

clocでgit管理下にあるファイルだけを対象とする

Posted at

clocコマンドを単に実行するとvendor/bundlenode_modulesなどgit管理下に無いファイルも集計してしまいます。

$ ls
.DS_Store        .dependabot/     .rubocop.yml     Procfile         bin/             db/              public/
.bundle/         .editorconfig    Gemfile          README.md        config/          lib/             test/
.byebug_history  .git/            Gemfile.lock     Rakefile         config.ru        log/             tmp/
.circleci/       .gitignore       Guardfile        app/             coverage/        package.json     vendor/

$ cloc .
   13862 text files.
   10857 unique files.
    4951 files ignored.

github.com/AlDanial/cloc v 1.80  T=34.00 s (265.3 files/s, 51360.0 lines/s)
-----------------------------------------------------------------------------------
Language                         files          blank        comment           code
-----------------------------------------------------------------------------------
Ruby                              6813         122342         178929         629290
JavaScript                          97          30244          22803         213124
C                                  342          12727          12804          60348
Markdown                           323          20352              0          51256
...
-----------------------------------------------------------------------------------
SUM:                              9021         228601         250093        1267480
-----------------------------------------------------------------------------------

clocではvcsオプションがあり、git配下にあるファイルのみを対象とすることができます。

$ cloc --vcs=git
     169 text files.
     163 unique files.
      36 files ignored.

github.com/AlDanial/cloc v 1.80  T=0.35 s (439.0 files/s, 10178.5 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Ruby                            89            310            388           1590
Haml                            38              3              2            315
Sass                             8             39             21            250
YAML                            10             52            105            220
HTML                             3             15              3            182
Markdown                         1             15              0             28
JSON                             1              0              0              5
JavaScript                       3              3             26              4
CSS                              1              0             15              0
CoffeeScript                     1              0              3              0
-------------------------------------------------------------------------------
SUM:                           155            437            563           2594
-------------------------------------------------------------------------------

clocのオプションを設定するよりはgitのサブコマンドをエイリアスで定義する方が楽チンです。

# ~/.gitconfig
[alias]
  cloc = !cloc $(git ls-files)

$ cloc --vcs=gitと同じ結果が出る。

$ git cloc
     169 text files.
     163 unique files.
      36 files ignored.

github.com/AlDanial/cloc v 1.80  T=0.10 s (1558.7 files/s, 36142.7 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Ruby                            89            310            388           1590
Haml                            38              3              2            315
...
-------------------------------------------------------------------------------
SUM:                           155            437            563           2594
-------------------------------------------------------------------------------
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?