Search code examples
gitmercurialdvcs

Will commits in git and mercurial repositories with the same history have the same hash?


When using git to clone an hg repository, or when migrating from hg to git, will the hashes remain the same?


Solution

  • By inspection, the answer appears to be "no".

    hg clone ssh://some.server.com/path/hgproject/

    git clone hg::ssh://some.server.com/path/hgproject/

    Followed by:

    hg log -l 5

    git log -n 5

    Show different commit hashes for the same commits.

    While both git and hg use SHA-1 hashes, there must be a difference in what they are hashing, perhaps the metadata.

    Any tooling dependent on the hashes will require history rewriting for a migration.

    More in depth context

    Per comment by @ngoldbaum & https://www.mercurial-scm.org/wiki/Nodeid:

    nodeid = sha1( min(parent1, parent2) + max(parent1, parent2) + contents )
    

    Whereas git computes it as:

    sha1(
        meta data
            commit message
            committer
            commit date
            author
            authoring date
        hash-of-tree-object (effectively the working directory)
    )
    

    Mapping

    if using git-remote-hg to do the view or conversion you can find enough information to create a mapping in .git/hg/origin/marks-{hg,git}