Last active
September 4, 2020 10:31
-
-
Save alecthegeek/333663 to your computer and use it in GitHub Desktop.
Calculate Git sha1 for a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(echo -en "blob $(wc -c < $file)\00";cat $file)|sha1sum -b | cut -d " " -f 1 | |
or of course | |
git hash-object $file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
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
For a file, this should be actually:
(echo -en "blob $(cat $file|wc -c)\00";cat $file)|sha1sum -b | cut -d " " -f 1