3

I'm writing a mercurial extension and I need to store a small amount of metadata. This is a dumb example, but enough to get something like this to work:

$ hg myextension --set a=2
$ hg myextension --get a
2

This data does not need to be copied if the repo is cloned, although if that's easy it would be cool to do that.

What is the proper way to do this in a mercurial extension? Is there a folder under .hg that I can just create arbitrary files in or something like that?

Philip
  • 4,128
  • 5
  • 31
  • 49

1 Answers1

4

There is no built-in mechanism for this — each extension decides how to best store the data. Extensions usually store their data in a file or directory named after themselves, so you could use

.hg/myextension/

as your root. You can use repo.opener to open files inside that directory:

fp = repo.opener('myextension/state.json')
data = json.load(fp)
fp.close()
Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
  • +1. So... still no git notes for Mercurial? (http://stackoverflow.com/a/2305977/6309) – VonC Jan 30 '14 at 07:25
  • @VonC no, nothing builtin. I see Git notes as something different, though. That is, I see how Git notes could be abused to store local state for an extension, but I don't think it's the best way to do it. For Mercurial it's perfectly feasible to extend the wire protocol to exchange arbitrary data (see the largefiles extension) and that way one can get the behavior one needs. – Martin Geisler Jan 31 '14 at 08:35