简体   繁体   English

如何绘制git repo的代码行历史?

[英]How can I graph the Lines of Code history for git repo?

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来运行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) 我玩过.lines() '库,但我发现最接近的是在diff上使用.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文件。

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: 在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))

The first thing that jumps to mind is the possibility of your git history having a nonlinear history. 跳到脑海的第一件事是你的git历史可能具有非线性历史。 You might have difficulty determining a sensible sequence of commits. 您可能难以确定合理的提交序列。

Having said that, it seems like you could keep a log of commit ids and the corresponding lines of code in that commit. 话虽如此,您似乎可以在该提交中保留提交ID和相应代码行的日志。 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. 在后提交钩子中,从HEAD修订版开始,向后工作(如果需要,分支到多个父级),直到所有路径都达到您之前已经看到的提交。 That should give you the total lines of code for each commit id. 这应该为您提供每个提交ID的总代码行数。

Does that help any? 这对你有帮助吗? I have a feeling that I've misunderstood something about your question. 我有一种感觉,我误解了你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM