如何使用 bzrlib 获取分支的修订历史记录

如何使用 bzrlib 获取分支的修订历史记录

我正在尝试获取 bzr 分支的提交者列表。我知道我可以通过命令行获取它,如下所示:

bzr log -n0 | grep committer | sed -e 's/^[[:space:]]*committer: //' | uniq

但是,我想用 以编程方式获取该列表bzrlib。查看了 bzrlib 文档后,我还是无法找到如何从我的分支获取完整的修订列表。

关于如何使用 bzrlib 获取分支中的完整修订历史记录,或者最终获取提交者列表,有什么提示吗?

答案1

我现在已经知道怎么做了。我正在添加代码来签出分支的本地副本,尽管这并非绝对必要(可以直接从签出的本地副本中读取修订信息)。重要的是all_revision_ids()get_revisions()get_apparent_authors()方法。

import os
from bzrlib.branch import Branch
from bzrlib.plugin import load_plugins

# The location on your file system where you want to check out
# the branch to get revisions for 
local_path = '/path/to/local/checkout'

# The name of the project you want to get the branch from
project_name = 'launchpad-project-name'

# Load the bzr plugins - the "launchpad" plugin 
# provides support for the "lp:" shortcut
load_plugins()

remote_branch_url = 'lp:{0}'.format(project_name)
remote_branch = Branch.open(remote_branch_url)

# Check out and get an instance of the branch
local_branch = remote_branch.bzrdir.sprout(
                   os.path.join(local_path,
                   project_name)).open_branch()

# Get all revisions from the branch
all_revision_ids = local_branch.repository.get_revisions(
    local_branch.repository.all_revision_ids())

# Set up a set of unique author names
authors = set()

# Iterate all revisions and get the list of authors
# without duplicates
for revision in all_revision_ids:
    for author in revision.get_apparent_authors():
        authors.add(author)

print 'Authors:', authors

答案2

另一个选择是安装该bzr-stats包并在分支的目录中运行以下命令:

bzr stats

相关内容