Search code examples
pythonmercurialhglib

committing a single file using python-hglib


I am trying to implement a rudimentary scm using python-hglib.
So far I have managed to connect to a repo (local) and I want to commit a single file among many. I am not sure how to do this. Consider the following:

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')

This connects to my_mercurial_repo successfully, but when I get to the commit line I get this error:

'hglib.error.CommandError'>, CommandError('commit', '-m', 'Checkpoint', '-u', 'myself', '--debug')

However if I change it to:

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

It works fine. Looking at the documentation, addremove=True would mark new/missing files as added/removed before committing.

So I guess my question is: how do I commit a single file in a repository of n files using python-hglib?

Just a quick update, thanks to @kAlmAcetA's response I updated my code as suggested to include

client.add('/tmp/repo/somefile')
rev, node =client.commit('Simple commit message', addremove=False, user='user1')

when I did this, the error goes away, the FIRST time commit is executed. If I execute the code again on the same file that I had opened I still get the error. So maybe what I am looking to do is to

  • Open a file (i'm ok with this)
  • Add some text to the file (i'm ok with this)
  • Commit the file
  • Add more text to the same file (i'm ok with this)
  • Commit the file

I am now struggling to do the commit-->edit-->commit loop for a single file.

Regards


Solution

  • You must first add that file to commit using client's add method.

    ...
    client.add('/tmp/repo/somefile')
    rev, node =client.commit('Simple commit message', addremove=False, user='user1')
    ...
    

    You have to add file only for the very first time (on success you will get True otherwise False), next modifications requires only commit.

    Note: If you would try to add the same file, unfortunately you will get True as well, then commit will fail with exception:

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

    Is good to wrap commit with try...expect probably.