如果比較的文件大小是所有你需要做的,沒有進一步看比svnlook filesize
命令。默認的調用 - svnlook filesize repo path
- 會給你path
的HEAD版本的大小。要獲取傳入提交中路徑的大小,請使用svnlook filesize repo path -t argv[2]
。
儘管如此,還是列出版本化路徑的所有版本(除了傳入路徑之外,因爲這是預掛鉤鉤子)的示例。
#!/usr/bin/env python
from sys import argv, stderr, exit
from subprocess import check_output
repo = argv[1]
transaction = argv[2]
def path_history(path, limit=5):
path = '/%s' % path
cmd = ('svnlook', 'history', '-l', str(limit), repo, path)
out = check_output(cmd).splitlines()[2:]
for rev, _path in (i.split() for i in out):
if _path == path:
yield rev
def commit_changes():
cmd = ('svnlook', 'changed', repo, '-t', transaction)
out = check_output(cmd).splitlines()
for line in out:
yield line.split()
def filesize(path, rev=None, trans=None):
cmd = ['svnlook', 'filesize', repo, path]
if rev: cmd.extend(('-r', str(rev)))
elif trans: cmd.extend(('-t', str(trans)))
out = check_output(cmd)
return out.rstrip()
def filesize_catwc(path, rev=None, trans=None):
'''A `svnlook filesize` substitute for older versions of svn.
Uses `svnlook cat ... | wc -c` and should be very inefficient
for large files.'''
arg = '-r %s' % rev if rev else '-t %s' % trans
cmd = 'svnlook cat %s %s %s | wc -c' % (arg, repo, path)
out = check_output(cmd, shell=True)
return out.rstrip()
for status, path in commit_changes():
if status in ('A', 'M', 'U'):
# get the last 5 revisions of the added/modified path
revisions = list(path_history(path))
headrev = revisions[0]
oldsize = filesize(path, rev=headrev)
newsize = filesize(path, trans=transaction)
非常感謝您的好評。我可以問一下,參考 「svnlook filesize repo path -t argv [2]」,這是什麼最後一個參數,它應該從哪裏傳入? – EthanML
等待 - 太好了,我剛剛發現運行的服務器使用的是舊版SVN,不包含filesize命令-_-任何良好的解決方法? – EthanML
Ethan,'sys.argv [2]'是第二個命令行參數,對於預提交鉤子,它對應於事務ID(大多數'svnlook'命令接受修訂或事務)。嗯,我想你可以使用'svnadmin cat'來獲取文件內容並自己計算字節數 - 我會添加回答。 – gvalkov