我如何在mercurial中创建一个补丁文件,其中也包含来自包含的subrepo的更改

时间:2011-10-27 13:27:09

标签: mercurial

我们正在与同一个项目的几个人合作,并使用Mercurial作为我们的DVCS。我们的项目确实有几个子项目。 我们确实需要通过邮件向对方发送补丁,因为此时无法从主回购中推送和拉出。 导出命令 - 如果在主服务器上执行,则只会为主服务器创建补丁,而不会为子服务器创建补丁。我们可以手动为这些创建pathes,但我们想知道是否有更简单的方法来做到这一点?

3 个答案:

答案 0 :(得分:1)

正如您所注意到的那样,没有内置支持。

也许你可以使用我写的onsub extension。这使得为​​子目录生成补丁变得更容易一些:

$ hg onsub 'hg export -r tip -o $HG_REPO/sub-$HG_SUBPATH.diff' 

HG_SUBPATH变量将替换为subrepo的路径。

如果您有嵌套子项目,则失败,因为hg export无法写入补丁名称sub-foo/bar/baz.diff。因此,您需要按摩路径并将/替换为-或类似内容。您应该能够使用类似的hg onsub电话导入补丁。

如果你正常工作,请随时在维基页面上添加关于它的说明。您也可以看看是否可以扩展onsub扩展以使这更容易,也许通过添加一个选项来使onsub在顶级repo上运行,以及一个可以直接使用而不是munging的新变量{{ 1}}。

答案 1 :(得分:0)

  • hg archive知道subrepo:你可以从单个变更集导出变更(并且如果在提交中影响子排行,它们会递归地行走)
  • hg diff也知道subrepos
  • 如果hg commit --subrepos来自subrepos的此commit must include all affected files,那么 - 导出也将导出(?)这些文件

答案 2 :(得分:0)

正如马丁所说,我写了自己的扩展。我会在这里发布代码,希望有人能改进它,或者在mercurial中使用defaulf ...

感谢Martin,我基于你的onsub扩展,是的,我知道它有一些问题,但是现在,它起到了它的作用。 (超过10个subrepos和超过1级嵌套的问题)

"""execute the Bundle command in a repository and each subrepository"""

# bundlesb.py - execute the Bundle command in a repository and each subrepository
#
# Copyright 2012 Johan G.
#
# This software may be used and distributed according to the terms of
# the GNU General Public License version 2 or any later version.

import os
import zipfile
from mercurial.i18n import _
from mercurial import extensions, subrepo, util

def bundlesb(ui, repo, *args, **opts):
    """execute the Bundle command in a repository and each subrepository

    Creates a combined bundle (with hgs extention) for the current 
    repository. 

    Because the revision numbers of the root and the subrepos will differ,
    we cannot reliably choose a revision to start from. A date to start
    from should be nice, but i have not taken the time to implement this.
    Instead i choose to take the N (default=10) last changesets into account.
    This seems to work well in our environment. Instead of providing the
    number of changesets for the operation, you can also specify -a to
    include all changesets. 

    Use --verbose/-v to print information and the subrepo
    name for each subrepo. 
    """
    ui.status("Starting operation\n")
    zipname = os.path.splitext(' '.join(args))[0]
    if (zipname==''):
       zipname = os.path.join(repo.root, os.path.split(repo.root)[1])
       #raise ValueError("FILE cannot be empty")
    zipname= zipname + '.hgs'
    allchangesets=opts.get('all')
    changesets=opts.get('changesets')
    ui.debug(_("input filename=%s ; AllChangesets=%s ; Changesets=%s \n") % (zipname, allchangesets, changesets))
    files=[]

    #work on the root repository
    runcmd(ui, repo.root, files, "0Root", repo['.'].rev(), ".", changesets, allchangesets)

    #do the same for each subrepository
    foreach(ui, repo, files, changesets, allchangesets)

    # open the zip file for writing, and write stuff to it
    ui.status("creating file: " + zipname + "\n\n")

    file = zipfile.ZipFile(zipname, "w" )
    for name in files:
        file.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED)
    file.close()

    # open the file again, to see what's in it

    file = zipfile.ZipFile(zipname, "r")
    for info in file.infolist():
        ui.debug(info.filename + " " + str(info.date_time) + " " + str(info.file_size) + " " + str(info.compress_size) +"\n")
        #delete all the compressed .hg files
    os.remove(os.path.join(repo.root, info.filename)) 

    ui.status("\nOperation complete\n")


def foreach(ui, repo, files, changesets, allchangesets):
    ctx = repo['.']
    work = [(1, ctx.sub(subpath)) for subpath in sorted(ctx.substate)]

    while work:
        (depth, sub) = work.pop(0)

        if hasattr(subrepo, 'relpath'):
            relpath = subrepo.relpath(sub)
        else:
            relpath = subrepo.subrelpath(sub)

        rev=sub._repo[sub._state[1]].rev()

        ui.debug(str(rev) + " " + str(sub._repo[sub._state[1]].user()) + " " + str(sub._repo[sub._state[1]].date()) + "\n")

        if depth>1:
            raise Exception("No support for nested levels deeper than 1 yet.")

    runcmd(ui, repo.root, files, str(depth) + relpath, rev, relpath, changesets, allchangesets)

        if isinstance(sub, subrepo.hgsubrepo):
            rev = sub._state[1]
            ctx = sub._repo[rev]
            w = [(depth + 1, ctx.sub(subpath)) 
                 for subpath in sorted(ctx.substate)]
            work.extend(w)

def runcmd(ui, root, files, name, revision, path, changesets, allchangesets):
    files.append(root + "/" + name + ".hg")
    if (revision<=changesets) or allchangesets:
       cmd="hg bundle -a " + root + "/" + name + ".hg"
    else:
       cmd="hg bundle --base " + str(revision-changesets)+ " "  + root + "/" + name + ".hg"
    ui.note(_("Working on '%s' in %s\n") % (path, root))
    ui.debug( "command line: "+ cmd +"\n")
    util.system(cmd, 
        cwd=os.path.join(root, path),
        onerr=util.Abort,
        errprefix=_('terminated bundlesub in %s') % path)


cmdtable = {
    "bundlesb":
        (bundlesb,
         [('c', 'changesets', 10, _('the number of recent changesets to include in the bundle'), 'N'),
          ('a', 'all', None, _('include all changesets in the bundle')),],
         _('[-c|-a] FILE...'))

}

反过来说:

 """execute the UnBundle command in a repository and each subrepository
           for a file created with BundleSb"""

# unbundlesub.py - execute the UnBundle command in a repository and each subrepository
#
# Copyright 2012 Johan G.
#
# This software may be used and distributed according to the terms of
# the GNU General Public License version 2 or any later version.

import os
import zipfile
#import glob
from mercurial.i18n import _
from mercurial import extensions, subrepo, util

def unbundlesb(ui, repo, *args, **opts):
    """execute the UnBundle command in a repository and each subrepository
        for a file created with BundleSb

    Updates the current repository from a combined bundle (with hgs extention). 

    Use --verbose/-v to print more detailed information during the operation. 
    """
    ui.status("Starting unbundle operation\n")

    update = opts.get('update')
    file = os.path.splitext(' '.join(args))[0] + '.hgs'
    if (file==''):
       raise ValueError("FILE cannot be empty")

    ui.debug("input filename=" + file + "\n")
    zfile = zipfile.ZipFile(file, "r" )
    for info in zfile.infolist():
        ui.debug(info.filename + " " + str(info.date_time) + " " + str(info.file_size) + " " + str(info.compress_size) +"\n")
    zfile.extract(info,repo.root)
        runcmd(ui, repo.root, info.filename, update)
        #delete all the compressed .hg files
    os.remove(os.path.join(repo.root, info.filename)) 

    zfile.close()

    ui.status("\nOperation complete\n")


def runcmd(ui, root, name, update):
    level=name[0]
    rep=name[1:len(name)-3]
    ui.debug(_("Detected level=%s for repository %s \n") % (level, rep))
    cmd="hg unbundle "
    if (update): cmd= cmd + "-u "
    cmd= cmd + root + "\\" + name 
    ui.note(_("Working on '%s' in %s\n") % (rep, root))
    if (level == '1'):
        wd=os.path.join(root, rep)
    elif (level=='0'):
        wd=root
    else:
       raise Exception("Do not know what to do with a level >1")

    ui.debug(_("command line: %s in working directory %s\n") % (cmd, wd))

    util.system(cmd, 
            cwd=wd,
            onerr=util.Abort,
            errprefix=_('terminated unbundlesub in %s') % rep)


cmdtable = {
    "unbundlesb":
        (unbundlesb,
         [('u', 'update', None,
           _('update to new branch head if changesets were unbundled'))],
         _('[-u] FILE...'))
}