简体   繁体   中英

How do I count total lines of remote git repository

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM