1

I am trying to write a small Mercurial extension, which, given the path to an object stored within the repository, it will tell you the revision it's at. So far, I'm working on the code from the WritingExtensions article, and I have something like this:

cmdtable = {
    # cmd name        function call
    "whichrev": (whichrev,[],"hg whichrev FILE")
}

and the whichrev function has almost no code:

def whichrev(ui, repo, node, **opts):
    # node will be the file chosen at the command line
    pass

So , for example:

hg whichrev text_file.txt

Will call the whichrev function with node being set to text_file.txt. With the use of the debugger, I found that I can access a filelog object, by using this:

repo.file("text_file.txt")

But I don't know what I should access in order to get to the sha1 of the file.I have a feeling I may not be working with the right function.

Given a path to a tracked file ( the file may or may not appear as modified under hg status ), how can I get it's sha1 from my extension?

Geo
  • 93,257
  • 117
  • 344
  • 520

1 Answers1

1

A filelog object is pretty low level, you probably want a filectx:

A filecontext object makes access to data related to a particular filerevision convenient.

You can get one through a changectx:

ctx = repo['.']
fooctx = ctx['foo']
print fooctx.filenode()

Or directly through the repo:

fooctx = repo.filectx('foo', '.')

Pass None instead of . to get the working copy ones.

Idan K
  • 20,443
  • 10
  • 63
  • 83
  • What do you mean by `Pass None instead of . to get the working copy ones.`? Isn't foo the path to a file? – Geo Aug 29 '11 at 19:08
  • Geo: I was referring to the `changeid`. When you pass `None`, you get back an object that reflects the working copy. Where as `.` is the first parent of your working copy. – Idan K Aug 29 '11 at 19:28
  • And how would I refer to a specific revision? N-1 dots? – Geo Aug 29 '11 at 19:37
  • Geo: no, `.` is special in that sense (at least here, revsets are processed in a higher layer). Just pass an identifier to it: SHA1, revision number, tag, branch... So if you want file `foo` in the first revision of the repository, you would `repo[0]['foo']` – Idan K Aug 29 '11 at 19:43