记录 pysvn 更新

3 投票
3 回答
4596 浏览
提问于 2025-04-15 16:08

我想知道,在使用pysvn运行svn update的时候,怎么能获取到哪些文件被添加、删除或者更新了等信息?我想把这些信息写到一个日志文件里。

3 个回答

1

我知道这个问题有点老了,但没有一个被认可的答案。我在找关于 node_kind 的信息时偶然发现了这个。

import pysvn

tmpFile = open('your.log', 'w')

repository = sys.argv[1]
transactionId = sys.argv[2]
transaction = pysvn.Transaction(repository, transactionId)

#
#   transaction.changed()
#
#   {
#       u'some.file': ('R', <node_kind.file>, 1, 0),
#       u'another.file': ('R', <node_kind.file>, 1, 0),
#       u'aDirectory/a.file': ('R', <node_kind.file>, 1, 0),
#       u'anotherDirectory': ('A', <node_kind.dir>, 0, 0),
#       u'junk.file': ('D', <node_kind.file>, 0, 0)
#   }
#

for changedFile in transaction.changed():
    tmpFile.writelines(transaction.cat(changedFile))

    # if you need to check data in the .changed() dict...
    # if ('A' or 'R') in transaction.changed()[changedFile][0]

我在 SVN 的预提交钩子脚本中用 Transaction 的方式和上面类似。

关于字典的详细信息可以查看文档:
http://pysvn.tigris.org/docs/pysvn_prog_ref.html#pysvn_transaction
http://pysvn.tigris.org/docs/pysvn_prog_ref.html#pysvn_transaction_changed

另外,虽然我没有使用 Transaction 的 list() 方法,但这也可能对你有帮助:
http://pysvn.tigris.org/docs/pysvn_prog_ref.html#pysvn_transaction

1

当你创建客户端对象时,记得添加一个 通知回调。这个回调就是一个函数,它会接收一个包含事件信息的 dict(字典)。

import pysvn
import pprint

def notify(event_dict):
    pprint.pprint(event_dict)

client = pysvn.Client()
client.callback_notify = notify

# Perform actions with client
2

你可以保存原始版本和更新后的版本,然后使用 diff_summarize 来获取更新的文件。(可以查看 pysvn 程序员参考

这里有个例子:

import time
import pysvn

work_path = '.'

client = pysvn.Client()

entry = client.info(work_path)
old_rev = entry.revision.number

revs = client.update(work_path)
new_rev = revs[-1].number
print 'updated from %s to %s.\n' % (old_rev, new_rev)

head = pysvn.Revision(pysvn.opt_revision_kind.number, old_rev)
end = pysvn.Revision(pysvn.opt_revision_kind.number, new_rev)

log_messages = client.log(work_path, revision_start=head, revision_end=end,
        limit=0)
for log in log_messages:
    timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(log.date))
    print '[%s]\t%s\t%s\n  %s\n' % (log.revision.number, timestamp,
            log.author, log.message)
print

FILE_CHANGE_INFO = {
        pysvn.diff_summarize_kind.normal: ' ',
        pysvn.diff_summarize_kind.modified: 'M',
        pysvn.diff_summarize_kind.delete: 'D',
        pysvn.diff_summarize_kind.added: 'A',
        }

print 'file changed:'
summary = client.diff_summarize(work_path, head, work_path, end)
for info in summary:
    path = info.path
    if info.node_kind == pysvn.node_kind.dir:
        path += '/'
    file_changed = FILE_CHANGE_INFO[info.summarize_kind]
    prop_changed = ' '
    if info.prop_changed:
        prop_changed = 'M'
    print file_changed + prop_changed, path
print

撰写回答