Search code examples
mercurialmercurial-api

Access the active bookmark name without parsing a command output?


I'd like to automatically add the active bookmark name (if any) to the commit message.

I found this method to do something similar as a pre-commit hook. However it uses the branch name, which is redundant since named branches are part of metadata. I would like the active bookmark instead.

The internal API context used in this example does not seem to hold bookmark info (see MercurialApi). Using hglib I could get the result of hg bookmarks, then parse it, find the line with *, trim to the right column... that's ugly.

I understand that hg lacks the equivalent of git's "plumbing" commands, but I can't even find a python API that offers what I'm looking for.

Are bookmarks managed by the internal API (if so, where is the doc?) or how can I avoid the parsing solution?


Solution

  • I believe you can use python-hglib:

    import hglib
    client = hglib.open('.')
    bookmarks, active = client.bookmarks()
    if active == -1:
        print 'no active bookmark'
    else:
        print 'active bookmark:', bookmarks[active][0]
    

    The confusion might be that the API documented on the MercurialAPI wiki page is the internal API. The API provided by python-hglib is apparently not really documented anywhere except in the code of the library. The bookmarks method is documented, for example.