il_mix
il_mix

Reputation: 571

Get Mercurial next commit hash

I am at revision 56, hash 6af16aa3edf8. Next revision will be 57, with hash ???. Is there a way to know the hash of revision 57? I need it in a pre-commit hook.

WHY THO?

I developed a script, called via pre-commit hook, that update some version files. That way, the compiled executables can give all info about the revision they are build from. I'm adding the revision number of the current commit in my version file, simply retrieved with "parent revision number + 1". Since the revision number is not reliable when collaborating with other people on the same repository, I prefer to add the hash, too. Don't know how to retrieve it...

Upvotes: 3

Views: 1011

Answers (1)

planetmaker
planetmaker

Reputation: 6044

No, you cannot predict the next hash even when you know its changeset completely. The commit time also plays a role there:

~/hg-test $ hg ci -m "b in foo"
~/hg-test $ hg id
d65d61e6898a tip
~/hg-test $ hg rollback
~/hg-test $ hg ci -m "b in foo"
~/hg-test $ hg id
c7f5ff744e43 tip

https://www.mercurial-scm.org/wiki/Nodeid

I suggest to solve your problem such: In your build tools, query whether the project is built from a repository. If so: retrieve the repository information. E.g.

ver = $(hg log -r. -T"{node|short} from {date|isodate}")

will give you

c7f5ff744e43 from 2017-07-26 14:05 +0200

Generate the version file from that information in your build chain on the fly

For distribution purposes, generate and amend this file to the package, so that the build process, when it finds it is not started from a repository checkout, still has a version file it can make use of.

Upvotes: 5

Related Questions