Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python hglib提交单个文件_Python_Mercurial_Hglib - Fatal编程技术网

使用python hglib提交单个文件

使用python hglib提交单个文件,python,mercurial,hglib,Python,Mercurial,Hglib,我正在尝试使用python hglib实现一个基本的scm。 到目前为止,我已经成功地连接到一个repo(本地),我想在多个文件中提交一个文件。 我不知道该怎么做。考虑以下事项: client = hglib.open(my_mercurial_repo) root_repo=hglib.client.hgclient.root(client) print "%s root" % root_repo rev, node =client.commit('Simple commit message'

我正在尝试使用python hglib实现一个基本的scm。
到目前为止,我已经成功地连接到一个repo(本地),我想在多个文件中提交一个文件。 我不知道该怎么做。考虑以下事项:

client = hglib.open(my_mercurial_repo)
root_repo=hglib.client.hgclient.root(client)
print "%s root" % root_repo
rev, node =client.commit('Simple commit message', addremove=False, user='user1')
这将成功连接到我的\u mercurial\u repo,但当我到达提交行时,会出现以下错误:

'hglib.error.CommandError'>,CommandError('commit','-m', '检查点'、'-u'、'我自己'、'-debug')

但是,如果我将其更改为:

rev,node=client.commit('Simple commit message',addremove=True, user='user1')

它很好用。查看文档,
addremove=True
会在提交之前将新文件/缺少的文件标记为已添加/已删除

所以我想我的问题是:如何使用PythonHGLIB在包含n个文件的存储库中提交单个文件

只是一个快速更新,感谢@kAlmAcetA的回复,我更新了我的代码,建议包括

client.add('/tmp/repo/somefile')
rev, node =client.commit('Simple commit message', addremove=False, user='user1')
当我这样做的时候,第一次执行commit时,错误就消失了。 如果我在打开的同一个文件上再次执行代码,仍然会出现错误。 所以也许我想做的是

  • 打开一个文件(我同意)
  • 在文件中添加一些文本(我同意)
  • 提交文件
  • 向同一文件添加更多文本(我同意)
  • 提交文件
我现在正努力为单个文件执行提交-->编辑-->提交循环


关于

您必须首先添加该文件以使用提交

您必须仅在第一次添加文件(成功后,您将获得
否则
),下一次修改只需要提交

注意:如果您试图添加相同的文件,不幸的是,您也会得到
True
,然后提交将失败,出现异常:

hglib.error.CommandError: (1, 'nothing changed', '')

使用
try…expect
很可能可以将提交包装起来。

如果我理解正确,您希望提交一个文件,无论工作副本中有多少文件可能已更改,就像您在命令行中所做的那样:

hg commit ${file}
repo = hglib.open(path_to_your_repository)
repo.rawcommand(args=['commit', 'filename'])
hglib的
commit
方法似乎没有提供:

def commit(self, message=None, logfile=None, addremove=False, closebranch=False,
           date=None, user=None, include=None, exclude=None):
    """
    Commit changes reported by status into the repository.

    message - the commit message
    logfile - read commit message from file
    addremove - mark new/missing files as added/removed before committing
    closebranch - mark a branch as closed, hiding it from the branch list
    date - record the specified date as commit date
    user - record the specified user as committer
    include - include names matching the given patterns
    exclude - exclude names matching the given patterns
    """
似乎不支持只提交一个文件。
hglib
应该以某种方式包装Mercurial的命令行,而
hg
命令可以提交一个更改过的文件,所以这很奇怪。再说一次,文档非常稀少,几乎不存在,因此不清楚这是否是出于设计


我会为此提交一个bug。

不支持使用hglib.commit提交单个文件,但您可以使用hglib.rawcommand,它使用与命令行上的hg相同的语法:

hg commit ${file}
repo = hglib.open(path_to_your_repository)
repo.rawcommand(args=['commit', 'filename'])
args中的第一个元素必须是hg命令名。其余元素是您将与该命令一起使用的任何选项。在我的实际代码中,我有:

repo.rawcommand(['commit','-m '+commit_msg, file_name)])

谢谢,当我执行client.add时,错误消失了,第一次执行commit时,如果我在打开的同一个文件上再次执行代码,我仍然会得到错误。我已经根据我的问题进行了编辑。您只需第一次将文件添加到repo,下次修改只需提交