Skip to content

Instantly share code, notes, and snippets.

@alecthegeek
Last active September 4, 2020 10:31
Show Gist options
  • Save alecthegeek/333663 to your computer and use it in GitHub Desktop.
Save alecthegeek/333663 to your computer and use it in GitHub Desktop.
Calculate Git sha1 for a file
(echo -en "blob $(wc -c < $file)\00";cat $file)|sha1sum -b | cut -d " " -f 1
or of course
git hash-object $file
use strict;
use warnings;
use Digest::SHA1;
my @input = <>;
my $content = join("", @input);
my $git_blob = 'blob' . ' ' . length($content) . "\0" . $content;
my $sha1 = Digest::SHA1->new();
$sha1->add($git_blob);
print $sha1->hexdigest();
@mick-d
Copy link

mick-d commented Dec 30, 2019

For a file, this should be actually:

(echo -en "blob $(cat $file|wc -c)\00";cat $file)|sha1sum -b | cut -d " " -f 1

@alecthegeek
Copy link
Author

alecthegeek commented Dec 31, 2019

Oops. Forgot the redirect symbol and added an extra ")"

Many thanks @mick-d. Should be

(echo -en "blob $(wc -c < $file))\00";cat $file)|sha1sum -b | cut -d " " -f 1

Sorry about that. Now fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment