[darcs-users] Patch hash calculation

Quag quaggy at gmail.com
Sat Nov 6 01:03:13 UTC 2004


On Thu, 4 Nov 2004 06:23:03 -0500, David Roundy <droundy at abridgegame.org> wrote:
> On Thu, Nov 04, 2004 at 08:00:11AM +0100, Tomasz Zielonka wrote:
> 
> 
> > On Wed, Nov 03, 2004 at 02:44:29PM -0800, Kannan Goundan wrote:
> > > Looking at the sources it looks like SHA1 is being used.  I tried
> > > giving the "sha1sum" utility various subsets of the patch text but
> > > couldn't match it up with Darcs's checksum.  Does anybody know how to
> > > calculate the checksum for a patch text?
> >
> > SHA1 is calculated only from the "patch info" part of the patch, but
> > looking at the code I think I will be difficult to prepare the input
> > for SHA1 by hand.
> 
> Anthony Towns has written perl code to do the hash calculation, which is
> part of his "darcs-repo".  You can search (or browse) the archives for the
> URL from which to darcs get.

For those that are interested, here is the relevant function from
Anthony's script:

use Digest::SHA1 qw(sha1_hex);

sub getpatchname {
        my ($email, $desc, $log, $date, $inv) = @_;

        return sprintf("%s-%s-%s", $date, substr(sha1_hex($email), 0, 5),
                sha1_hex("$desc$email$date$log$inv"));
}

I won't insult you all by trying to provide an example of how to call
this function. My perl is rather shameful.

The $inv argument is set to either "t" or "f".

Since I'm not a perl speaker, I recoded the hash calculation in python
to check that I understood the perl implementation. I include the
python version here for the few of you who find it easier to
understand python over perl. ;-)

import sha

def sha1_hex(s):
    e = sha.new()
    e.update(s)
    return e.hexdigest()

def patchname(email, desc, log, date, inv='*'):
    return "%s-%s-%s" % (date, sha1_hex(email)[:5], sha1_hex(desc +
email + date + log + {'-':'t', '*':'f'}[inv]))

print patchname(email="quaggy at gmail.com", desc="initial", log="",
date="20041101074140")

I hope this is useful to someone,
Jonathan.




More information about the darcs-users mailing list