Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I count total lines of remote git repository

Tags:

git

linux

bash

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?

like image 291
chris Avatar asked Sep 25 '14 11:09

chris


People also ask

Can GitHub count lines of code?

This API uses COUNT LOC API derived from the Github API to calculate the Lines of Code, Total Number of files, Total Languages used and Total Repositories and creates data in JSON format for the same.

How do you count lines of code?

Source Lines of Code The most direct way to count lines of code (LOC) is to, well, count lines of code assuming each line corresponds to a line feed sequence ( \n or \r\n ). Our IDE tells us how many lines of text a file has and displays a count in one of the margins.

How do you list your remote repositories git?

You can list the remote branches associated with a repository using the git branch -r, the git branch -a command or the git remote show command. To see local branches, use the git branch command. The git branch command lets you see a list of all the branches stored in your local version of a repository.


1 Answers

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.

like image 193
Sascha Wolf Avatar answered Oct 05 '22 22:10

Sascha Wolf