2008-08-23 38 views
40

基本上我想在每次提交之後獲取版本庫中的代碼行數。如何繪製git repo的代碼行歷史記錄?

我發現的唯一的(真糟糕)的方法是使用git filter-branch運行wc -l *,而且每次提交運行git reset --hard的腳本,然後運行wc -l

爲了使它更清楚一點,當刀具運行時,它會輸出第一次提交的代碼行,然後輸出第二次提交的代碼行等等。這就是我想要的工具輸出(作爲一個例子):

[email protected]:~/$ gitsloc --branch master 
10 
48 
153 
450 
1734 
1542 

我和紅寶石「混帳」庫玩耍了,但我發現用在一個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 

回答

4

跳轉想到的第一件事是具有非線性歷史的git的歷史的可能性。您可能難以確定合理的提交順序。

話雖如此,好像你可以在提交中保存提交id和相應代碼行的日誌。在一個post-commit鉤子中,從HEAD修訂開始,向後工作(如果需要的話,分支到多個父項),直到所有路徑達到您之前已經見過的提交。這應該爲您提供每個提交ID的代碼總行數。

這有幫助嗎?我有一種感覺,我誤解了你的問題。

23

您可能會同時添加和刪除的那些行用git的日誌,如:

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

由此看來,你可以寫一個類似的腳本來你沒有使用這個信息的人。在蟒蛇:

#!/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)) 
+0

在你的代碼中`err`將總是`None`。 – jfs 2009-01-14 13:06:03

+0

`如果不是line.strip():continue`可能會更健壯。 – jfs 2009-01-14 13:08:22

相關問題