如何绘制 git repo 的代码行历史记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I graph the Lines of Code history for git repo?
提问by dbr
Basically I want to get the number of lines-of-code in the repository after each commit.
基本上我想在每次提交后获取存储库中的代码行数。
The only (really crappy) ways I have found is to use git filter-branch
to run wc -l *
, and a script that runs git reset --hard
on each commit, then runs wc -l
我发现的唯一(非常糟糕的)方法是使用git filter-branch
run wc -l *
,以及git reset --hard
在每次提交时运行的脚本,然后运行wc -l
To make it a bit clearer, when the tool is run, it would output the lines of code of the very first commit, then the second and so on. This is what I want the tool to output (as an example):
为了更清楚一点,当工具运行时,它会输出第一次提交的代码行,然后是第二次,依此类推。这是我希望该工具输出的内容(例如):
me@something:~/$ gitsloc --branch master
10
48
153
450
1734
1542
I've played around with the ruby 'git' library, but the closest I found was using the .lines()
method on a diff, which seems like it should give the added lines (but does not: it returns 0 when you delete lines for example)
我玩过 ruby 'git' 库,但我发现最接近的是.lines()
在差异上使用该方法,这似乎应该提供添加的行(但不会:例如,当您删除行时它返回 0)
require 'rubygems'
require 'git'
total = 0
g = Git.open(working_dir = '/Users/dbr/Desktop/code_projects/tvdb_api')
last = nil
g.log.each do |cur|
diff = g.diff(last, cur)
total = total + diff.lines
puts total
last = cur
end
采纳答案by cboettig
回答by fserb
You may get both added and removed lines with git log, like:
您可能会使用 git log 获得添加和删除的行,例如:
git log --shortstat --reverse --pretty=oneline
From this, you can write a similar script to the one you did using this info. In python:
由此,您可以编写与使用此信息所做的脚本类似的脚本。在蟒蛇中:
#!/usr/bin/python
"""
Display the per-commit size of the current git branch.
"""
import subprocess
import re
import sys
def main(argv):
git = subprocess.Popen(["git", "log", "--shortstat", "--reverse",
"--pretty=oneline"], stdout=subprocess.PIPE)
out, err = git.communicate()
total_files, total_insertions, total_deletions = 0, 0, 0
for line in out.split('\n'):
if not line: continue
if line[0] != ' ':
# This is a description line
hash, desc = line.split(" ", 1)
else:
# This is a stat line
data = re.findall(
' (\d+) files changed, (\d+) insertions\(\+\), (\d+) deletions\(-\)',
line)
files, insertions, deletions = ( int(x) for x in data[0] )
total_files += files
total_insertions += insertions
total_deletions += deletions
print "%s: %d files, %d lines" % (hash, total_files,
total_insertions - total_deletions)
if __name__ == '__main__':
sys.exit(main(sys.argv))
回答by ma11hew28
http://github.com/ITikhonov/git-locworked right out of the box for me.
http://github.com/ITikhonov/git-loc对我来说开箱即用。
回答by Greg Hewgill
The first thing that jumps to mind is the possibility of your git history having a nonlinear history. You might have difficulty determining a sensible sequence of commits.
首先想到的是您的 git 历史可能具有非线性历史。您可能难以确定合理的提交顺序。
Having said that, it seems like you could keep a log of commit ids and the corresponding lines of code in that commit. In a post-commit hook, starting from the HEAD revision, work backwards (branching to multiple parents if necessary) until all paths reach a commit that you've already seen before. That should give you the total lines of code for each commit id.
话虽如此,您似乎可以在该提交中保留提交 ID 和相应代码行的日志。在提交后挂钩中,从 HEAD 修订版开始,向后工作(如有必要,分支到多个父级),直到所有路径都到达您之前已经见过的提交。这应该为您提供每个提交 ID 的总代码行数。
Does that help any? I have a feeling that I've misunderstood something about your question.
这有帮助吗?我有一种感觉,我误解了你的问题。