-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-hash-self.pl
More file actions
52 lines (38 loc) · 1010 Bytes
/
git-hash-self.pl
File metadata and controls
52 lines (38 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# git hash-object __FILE__ equivalent
use strict;
use warnings;
use utf8;
# utf8 тест
sub with_FILE {
require Digest::SHA1;
open(my $fh, '<:raw', __FILE__) or die $!;
my $sha1 = Digest::SHA1->new;
$sha1->add('blob ');
$sha1->add(-s $fh);
$sha1->add("\x00");
$sha1->addfile($fh);
close($fh);
STDOUT->say($sha1->hexdigest);
}
sub with_DATA {
require Digest::SHA1;
require Fcntl;
my $fh = \*DATA;
unless (ref $fh eq 'GLOB') { return } # if there's no actual __DATA__ token in file, there will be no glob
my $sha1 = Digest::SHA1->new;
my $whence = $fh->tell();
$fh->seek(0, Fcntl::SEEK_SET());
$sha1->add('blob ');
$sha1->add(-s $fh);
$sha1->add("\x00");
$sha1->addfile($fh);
$fh->seek($whence, Fcntl::SEEK_SET());
STDOUT->say($sha1->hexdigest);
}
sub with_git {
system('git', 'hash-object', __FILE__);
}
with_FILE();
with_DATA();
with_git();
__DATA__