committing a single file using python-hglib

1.2k views Asked by At

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

3

There are 3 answers

2
kwarunek On BEST ANSWER

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.

0
MrPat On

Committing a single file using hglib.commit is not supported, but you can use hglib.rawcommand, which uses the same syntax as hg on the command line:

repo = hglib.open(path_to_your_repository)
repo.rawcommand(args=['commit', 'filename'])

The first element in args must be an hg command name. The remaining elements are any options you'd use with that command. In my actual code, I have:

repo.rawcommand(['commit','-m '+commit_msg, file_name)])
0
Vucar Timnärakrul On

If I understand you correctly, you want to commit a single file, no matter how many files in your working copy might have changed, just as if you’d do on the command line:

hg commit ${file}

hglib’s commit method does not seem to provide for this:

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
    """

Committing just a single file doesn’t seem to be supported. hglib is supposed to wrap Mercurial’s commandline in some fashion, and the hg command can commit a single changed file, so this is weird. Then again, the documentation is scarce to the point of being non-existent, so it’s unclear if that’s by design or not.

I’d file a bug for this.