Implementing hash-object

I’ve spent now 2 small sessions on the second part of the “Build your own git” project on CodeCrafters and have come up with the following implementation for the hash-object functionality of git:

An implementation of the hash-object functionality that is … wrong.

For the test prompt of

"strawberry pineapple mango apple pear raspberry" > pear.txt

I received the following test failure output:

Expected a 40-char SHA ("a882c95db4a3524e5f04815145ec82980a79f9b2") as output. Got: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

So my hash is 60 characters long…. for some reason. At this point the very first thing I want to check on is what the header is looking like so I slap a print statement on it and guess what.

Yeah ok well that would present a problem.

I’ve got the right first word and then nothing else right. Classic.

Immediately from that output though I can tell 2 things: that I am grabbing the wrong index from the inputted list of args and that I have lost my null byte somewhere along the way. The second issue of missing the null byte was easy. I forgot to use an escape character in building the string out. So I just had to update the below line:

String header = "blob " + object.length() + "\0" + object;

to this:

String header = "blob " + object.length() + "\\0" + object;

Easy enough and now I have different (still wrong) output that contains the null byte. PROGRESS!

A slightly less glaringly wrong but still very easy to fix issue was the culprit for seemingly grabbing the wrong argument. But this I leave as an exercise to the reader… can you find what went wrong? Hint: it’s on line 58 in the above image of code. It really brought me back to when I used to be a Teaching Assistant for our intro to object oriented programming class and had to explain to students the difference between .equals and == in code without ever bringing up pointers. It’s tough.

And even after all that I was still getting a 60 character output. At which point I started looking in depth at everything I was doing thinking I must be doing something wrong with the hex conversion until it suddenly dawned on me: I was using SHA-256. That would in fact do it and after changing the encryption to SHA-1 I was ready to experience my next error.

Ok I don’t know how Zlib-compression works and I’m hitting a wall of tiredness at a tender 8:30pm. Time well spent getting this slowly up to speed and I’ll figure out what ZLib-Compression is tomorrow.

Previous
Previous

Implementing hash-object part 2: The hashening

Next
Next

What language should you learn? And why isn’t it Python?