Get file hash in specific changeset in Mercurial - mercurial

Is there a way to get file hash in a specific changeset/revision in Mercurial?
Similar to svn proplist command in Subversion.
Thanks

I don't think so, since for mercurial the atomic unit is the changeset, not a single file (and rightly so!).
On the other hand, you can use hg cat and pipe to any hash utility as follows
$ hg cat -r REV FILE | md5
$ hg cat -r REV FILE | shasum
...
But again, are you sure focusing on one file (as opposed to the changeset) is the right approach?

Related

How can I query Mercurial for the the revision which introduced a given file?

Given a file name, how can I query Mercurial for the revision of the change set which introduced this file?
You want to work with revsets!
hg log -r "adds(filename.ext)"
The description from the help:
"adds(pattern)":
Changesets that add a file matching pattern.
The pattern without explicit kind like "glob:" is expected to be relative to the current directory and match against a file or a directory.
This should work:
hg log -r : -l 1 filepath
hg log -r : prints the history in reverse order, the -l 1 switch limits the changes displayed to the first one.

Mercurial, check if a particular version of a file has existed in the history before?

To somewhat elaborate on the title question, is there a way to have Mercurial search through the repository history for a particular version of a given file, and show all of the revisions (or just the most recent one) that contain that version?
For example, let's say that the current working revision is, say, 300 and a file was reverted to an earlier version (say, revision 200). I don't know this - all I can easily see is "how different" the new file is from the 300 version. How can I find out all of the possible revisions that it could have been reverted to?
And if Mercurial cannot do this natively, is there another tool that can? (TortoiseHG?)
I don't think this is possible with any of hg's built-in functionality, but you can get something like it with judicious xargs and md5sum application:
hg log --template "{rev}\\n" | xargs -I "{}" /bin/bash -c 'echo $(hg cat -r {} <filename> | md5sum) {}'
This will give you a list of md5 checksums with the revision number, which when you sort it will give you the revisions sharing versions of the file.

Mercurial: get contents of a specific revision of a file

I need to get contents of a specific revision/node of a file in a local repository and write it to a temporary file.
I know it is possible to do through the internal Mercurial API.
Is there a built-in command or an extension?
You can use hg cat:
hg cat -r revisionid filename > tmpfile
The fastest, large and/or binary file friendly way to do this is:
hg cat -r revisionid repoRelativeFilePath -o tempFilePath
The tempFilePath, unless absolutely rooted (ex. 'C:\') will be relative to the repo's root

In Mercurial (hg), how do you see a list of files that will be pushed if an "hg push" is issued?

We can see all the changesets and the files involved using
hg outgoing -v
but the filenames are all scattered in the list of changesets.
Is there a way to just see a list of all the files that will go out if hg push is issued?
First, create a file with this content:
changeset = "{files}"
file = "{file}\n"
Let's say you call it out-style.txt and put it in your home directory. Then you can give this command:
hg -q outgoing --style ~/out-style.txt | sort -u
A somewhat under-appreciated feature: hg status can show information about changes in file status between arbitrary changesets. This can be used to get a list of files changed between revisions X and Y:
hg status --rev X:Y
In this case, we can use hg outgoing, to find the first outgoing changeset X and then do
hg status --rev X:
to see the files changes since revision X. You can combine this into a single line in your shell:
hg status --rev $(hg outgoing -q --template '{node}' -l 1):
I usually use
hg outgoing -v | grep files
It makes the listing shorter, but doesnt sort. But thus far I havent been in a situation where I want to push so much (and at the same time check the files) that its been a problem.
[Edit]
To do what you want:
Use cut to remove the files: part
For changesets with more than one touched file, use tr to put them on separate lines
Finally sort the resulting output with sort
Like so:
hg outgoing -v |grep files: |cut -c 14- |tr ' ' '\n' |sort -u
You can put this in ~/outgoingfiles.sh or something to have it nice and ready.
I use Torgoise Hg, which is a shell extension that has a "synchronize" view allowing you to see outgoing files before you push them. It's convenient for commits as well, and other things.
A simple hg out will also solve this.
It will list all committed but yet to push checkins.

How do I get the current mercurial revision without calling hg?

In Git the current revision hash is stored in
.git/refs/heads/master
Is there an equivalent in Mercurial that doesn't require me making a call to hg log -l1? I know I can get the current branch in .hg/branch.
This is to "display" the current hg hash on screen when browsing a web page.
$ hg parents --template="{node}\n"
52b8cee1e59c91b9147635b7f44a3a8896ee0b00
$ hexdump -n 20 -e '1/1 "%02x"' .hg/dirstate
52b8cee1e59c91b9147635b7f44a3a8896ee0b00
But why can't you just call hg parents --template="{node}\n"?
hg id --debug -i -r .
I'm not a mercurial expert, but taking the sledgehammer approach and doing a grep for the current revision hash in .hg yields only one possible, and that is .hg/branchheads.cache.
I believe this caches all the heads of the repository, so it may have multiple entries. By default, I think it will always have two entries, one for the default branch and one for the tip revision number.
I think that branchheads.cache is rebuilt whenever new changesets arrive, so it should always have the correct current revision hash in it.