使用Mercurial API获取特定变更集的仓库变更

5 投票
1 回答
3269 浏览
提问于 2025-04-15 19:50

我想知道怎么用Mercurial的API来查看每个变更集(changeset)对一个仓库做了哪些改动。我可以得到某个特定版本相关的文件列表,但我不知道怎么判断这些文件发生了什么变化。

我想回答以下关于每个变更集中文件的问题:

  • 这个文件是新增的吗?
  • 这个文件被删除了吗?
  • 这个文件被修改了吗?

文件上下文中有没有什么属性可以告诉我这些信息(如果有的话,我找不到),或者有没有其他方法可以弄明白这些?

这是我的代码:

def index(request):
    u = ui.ui()
    repo = hg.repository(ui.ui(), '/path/to/repo')
    changes = repo.changelog
    changesets = []

    for change in changes:
        ctx = repo.changectx(change)
        fileCtxs = []
        for aFile in ctx.files():
            if aFile in ctx:
                for status in repo.status(None, ctx.node()):
                    # I'm hoping this could return A, M, D, ? etc
                    fileCtxs.append(status)

        changeset = {
            'files':ctx.files(),
            'rev':str(ctx.rev()),
            'desc':ctx.description(),
            'user':ctx.user(),
            'filectxs':fileCtxs,
        }
        changesets.append(changeset)

    c = Context({
        'changesets': changesets,
    })

    tmplt = loader.get_template('web/index.html')
    return HttpResponse(tmplt.render(c))

1 个回答

5

localrepo.status() 这个函数可以接收一些上下文作为参数,比如 node1node2

你可以查看这个链接了解更多信息:http://hg.intevation.org/mercurial/crew/file/6505773080e4/mercurial/localrepo.py#l973

撰写回答