基本上我想在每次提交后获取存储库中的代码行数。

我发现的唯一(真的很糟糕)的方法是使用 git filter-branch 跑步 wc -l *, ,以及运行的脚本 git reset --hard 在每次提交时,然后运行 wc -l

为了更清楚一点,当该工具运行时,它将输出第一次提交的代码行,然后是第二次提交的代码行,依此类推。这就是我希望该工具输出的内容(作为示例):

me@something:~/$ gitsloc --branch master
10
48
153
450
1734
1542

我玩过 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
有帮助吗?

解决方案

您也可以考虑 gitstats, ,它会将此图表生成为 html 文件。

其他提示

您可能会通过 git log 获得添加和删除的行,例如:

git log --shortstat --reverse --pretty=oneline

由此,您可以编写一个与使用此信息所做的脚本类似的脚本。在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))

http://github.com/ITikhonov/git-loc 对我来说开箱即用。

首先想到的是你的 git 历史记录可能具有非线性历史记录。您可能难以确定合理的提交顺序。

话虽如此,您似乎可以保留提交 ID 的日志以及该提交中相应的代码行。在提交后挂钩中,从 HEAD 修订版开始向后工作(如有必要,分支到多个父级),直到所有路径都到达您之前已经见过的提交。这将为您提供每个提交 ID 的总代码行数。

这有帮助吗?我有一种感觉,我误解了你的问题。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top