Reputation: 3
I am trying to work on a custom solution to connect to a git API, and when I do a shasum on content that starts with letters, the results match:
$ echo -e -n "blob 4\0test" | shasum
30d74d258442c7c65512eafab474568dd706c430 *-
$ echo -e -n "test" | git hash-object --stdin
30d74d258442c7c65512eafab474568dd706c430
It doesn't match when the content starts with a number though:
$ echo -e -n "blob 5\0test" | shasum
315a7861230f24fade469e87c0c548f0cc4bc8c8 *-
$ echo -e -n "0test" | git hash-object --stdin
475a3cf5e5dadf80fe51cc8748c9bfdabae29f07
I'm stumped as to why it's giving different results, anybody know why this is the case?
Upvotes: 0
Views: 108
Reputation: 76409
When you use echo -e
, you're providing an octal escape when you write \0
. Adding additional digits after that causes them to be interpreted as octal digits in the octal escape.
Since POSIX doesn't define echo -e
and it isn't portable, it's better to use printf
, since you know that the octal escape will be interpreted consistently and can contain at most three digits:
$ printf "blob 5\0000test" | shasum
475a3cf5e5dadf80fe51cc8748c9bfdabae29f07 -
$ printf "0test" | git hash-object --stdin
475a3cf5e5dadf80fe51cc8748c9bfdabae29f07
In the first invocation, the \0000
is the octal escape \000
(a NUL byte) plus the character 0
(decimal 48).
Upvotes: 1