6

I wanna count to total lines of codes in git repository. I've found the answer in google.

git ls-files -z | xargs -0 cat | wc -l

It works well in local repository.

but. I want to count in remote repository.

so, I tried.

git ls-files -z /home/bjyoo/repositories/root/localfiletest.git | xargs -0 cat | wc -l

,

git ls-files -z --git-dir=/home/bjyoo/repositories/root/localfiletest.git | xargs -0 cat | wc -l

and

git --git-dir=/home/bjyoo/repositories/root/localfiletest.git --ls-files | xargs -0 cat | wc -l

all command failed.

does anyone knows how to count total lines of code?

chris
  • 83
  • 1
  • 7

2 Answers2

4

While VonC is correct on the reason why your commands are failing, there still is a way to count the lines in a repository, even if this repository is bare.

For this to work you have to make git print the content of the files from a specific revision which is possible using git show.

Such a command could look like this, assuming you are currently in the repository.

for file in $(git ls-tree --name-only -r HEAD); do
    git show HEAD:"$file"
done | wc -l

If you want to execute the command from a different folder you can use --git-dir after the git keyword.

for file in $(git --git-dir=<path-to-repo> ls-tree --name-only -r HEAD); do
    git --git-dir=<path-to-repo> show HEAD:"$file"
done | wc -l

We are using git ls-tree to list all files in the repository, the reason being that ls-files doesn't work in a bare repository. Then we print the content of the file using git show combined with a specific revision.

You can take a look at the ls-tree documentation and the show documentation to understand exactly what the commands are doing.

Sascha Wolf
  • 18,810
  • 4
  • 51
  • 73
0

git log --numstat --no-merges --pretty="format:" | grep -v -e '^$' | awk -F " " '{sum+=$1-$2} END {print sum}'

Zheng Gao
  • 1
  • 1