branches names parsing

Matt Mackall mpm at selenic.com
Tue Jan 13 08:04:35 UTC 2015


On Mon, 2015-01-12 at 16:53 +0600, Roman Odyshew wrote:
> Hello, my name is Roman.
> I has one trouble, I need get list of all branches names by programm code.
> So, I use command hg branches -c, and I get all branches, in format:
> <branch name> <changeset> <branch status>
> But branch name can has any characters and whitespaces, so how I can in
> programm parse branch names from lists? Are there rules of format - hg
> branches -c command?

The python-hglib library does it thusly:

        for line in out.rstrip().splitlines():
            namerev, node = line.rsplit(':', 1)
            name, rev = namerev.rsplit(' ', 1)
            name = name.rstrip()
            node = node.split()[0] # get rid of ' (inactive)'                   
            branches.append((name, int(rev), node))

..namely parsing from right to left, because the right side is less
ambiguous.

Other options exist, including:

$ hg branches -cq
default
stable

and

$ hg branches -T json
[
 {
  "active": true,
  "branch": "default",
  "closed": false,
  "current": true,
  "node": "345042e024dcd8532216d30c70fd78338fb22e3a",
  "rev": 26216
 },
 {
  "active": false,
  "branch": "stable",
  "closed": false,
  "current": false,
  "node": "f4e6475950f18a08663c2f4ea8f6352a1b0b55fa",
  "rev": 26105
 }
]

-- 
Mathematics is the supreme nostalgia of our time.





More information about the Mercurial mailing list