LivBanana
LivBanana

Reputation: 381

Why I am not getting the same SHA-1?

I tried the following two commands:

philip@X230:~/Desktop/learn_git$ echo 'what is up, doc?' | git hash-object --stdin
7108f7ecb345ee9d0084193f147cdad4d2998293

philip@X230:~/Desktop/learn_git$ echo 'blob 16\u0000what is up, doc?' | openssl sha1
(stdin)= 5bc40a1cd865de7a0a4641d3d059b1216ed9042c

I am wondering why I am not getting te same SHA-1

Upvotes: 3

Views: 354

Answers (2)

bk2204
bk2204

Reputation: 76409

echo appends a newline to its output, so your contents are actually 17 bytes long, not 16. Try to use printf, which is more standardized and doesn't do that:

$ printf '%s' 'what is up, doc?' | git hash-object --stdin
bd9dbf5aae1a3862dd1526723246b20206e5fc37
$ printf 'blob 16\0%s' 'what is up, doc?' | sha1sum
bd9dbf5aae1a3862dd1526723246b20206e5fc37  -

Upvotes: 4

hobbs
hobbs

Reputation: 239652

Two things:

  1. echo 'what is up, doc?' outputs 17 characters, including the newline at the end. You should use echo -n (assuming bash) both times to remove the newline at the end, or adjust the length of your manually-constructed blob to 17.

  2. Your \u0000 escape is ineffective (test it, run echo 'blob 16\u0000what is up, doc?' to the console without piping into sha1). You need the -e option (again assuming bash) to make echo interpret any escapes.

If you do

echo -e 'blob 17\u0000what is up, doc?' | openssl sha1

the result is

(stdin)= 7108f7ecb345ee9d0084193f147cdad4d2998293

which matches git hash-object nicely.

Upvotes: 7

Related Questions