我如何计算远程git存储库的总行数(How do I count total lines of remote git repository)

编程入门 行业动态 更新时间:2024-10-26 10:32:46
我如何计算远程git存储库的总行数(How do I count total lines of remote git repository)

我想要计入git存储库中的总代码行数。 我在谷歌找到了答案。

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

它在本地存储库中运行良好。

但。 我想要计入远程存储库。

所以,我试过了。

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

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

所有命令都失败了

有谁知道如何计算总代码行数?

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?

最满意答案

虽然VonC对于命令失败的原因是正确的,但仍有一种方法可以计算存储库中的行,即使此存储库是裸的。

为了实现这一点,你必须使用git show从git打印特定版本的文件内容。

假设您当前在存储库中,这样的命令可能看起来像这样。

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

如果要从其他文件夹执行命令,可以在git关键字后使用--git-dir 。

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
 

我们使用git ls-tree列出存储库中的所有文件,原因是ls-files在裸存储库中不起作用。 然后我们使用git show和特定版本打印文件的内容。

您可以查看ls-tree文档和show文档,以准确了解命令正在执行的操作。

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.

更多推荐

本文发布于:2023-04-29 09:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335870.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:行数   count   git   total   repository

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!