Git:在回购中获取未完成行数的总数 [英] Git: Getting total numbers of uncomitted lines in a repo

查看:184
本文介绍了Git:在回购中获取未完成行数的总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有命令获取当前git repo中更改的总数。我希望考虑考虑分阶段文件和非分页文件文件。

这是我可以获得的最接近的数据。

  $ git diff --cached --shortstat 
1个文件已更改,1个插入(+),1个删除( - )

$ git diff --shortstat
1个文件已更改,1个插入(+)

但我必须执行两个命令然后解析(很容易出错,你永远不会知道所有的情况),找到已经改变的行数



如果不是git命令,bash / zsh函数也可以。

更新:

所以我的想法是在我的ZSH提示符下跟踪未提交的行(显示git工作目录的不清晰程度),例如:

 [〜/ dotfiles](master)✗[192] 
$ ...

所以感谢@ arco444的回答,我稍微修改了一下,现在我有了下面的内容,只要有人e希望实现相同的功能git_change_count {
local IS_INSIDE_REPO = $(git rev-parse --is-内部工作树2> / dev / null)
if [[$ IS_INSIDE_REPO ==true]];然后
{git diff --cached --numstat; git diff --numstat; } | awk'{a + =($ 1 + $ 2)} END {print a}'
fi
}

我添加了添加和删除行的行,而不是获取它们的差异。这基本上意味着编辑过的行显示为2,但是这样做可以涵盖两个不同的行被添加和删除并且由于减法而得到0的情况。

方案

如何:

  {git diff --cached --numstat; git diff --numstat; } | awk'{a + =($ 1- $ 2)} END {print a}'

<$ c
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b

您需要为stage和unstaged文件运行它,然后管道到 awk 来做算术。它会返回一个添加和删除行的总和,所以如果删除的行数多于已添加的行数,你会得到一个负面结果。


Is there a command to get just total number of lines that are changed in current git repo. I want to take count considering both staged and unstaged files.

This is the closest I could get

$ git diff --cached --shortstat
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git diff --shortstat
 1 file changed, 1 insertion(+)

But I have to execute two commands and then parse (quite error prone, you never know all cases) result to find number of lines that have changed.

If not a git command, a bash/zsh function would also do.

UPDATE:

So the idea was to track total uncommitted lines (showing approximate level of dirtiness of a git working directory) on my ZSH prompt, Something like:

[~/dotfiles] (master) ✗ [192]
$ ...

So thanks to @arco444's answer, which I slightly modified, I now have following, just if someone wants to achieve the same

function git_change_count {
    local IS_INSIDE_REPO=$(git rev-parse --is-inside-work-tree 2>/dev/null)
    if [[ $IS_INSIDE_REPO == "true" ]]; then
        { git diff --cached --numstat; git diff --numstat; } | awk '{ a+=($1+$2) } END {print a}'
    fi
}

I am adding the lines that were added and deleted lines, instead of getting their diff. Which essentially means edited lines shows up as 2 but doing so covers the scenario when two different lines were added and deleted and because of subtraction we get 0 as result.

解决方案

How about:

{ git diff --cached --numstat; git diff --numstat; } | awk '{ a+=($1-$2) } END {print a}'

The --numstat flag gives you:

#added #deleted #filename

You need to run it for both staged and unstaged files, then pipe to awk to do the arithmetic. It will return a sum of added and deleted lines, so you will get a negative result if more lines were removed than added.

这篇关于Git:在回购中获取未完成行数的总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆