Skip to content

Commit f958150

Browse files
committed
Move helper functions from corpus test into module
1 parent dc44fec commit f958150

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

Local/Utils.pm

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package Local::Utils;
2+
3+
use strict;
4+
use warnings;
5+
use parent 'Exporter';
6+
7+
use JSON qw(decode_json);
8+
9+
my $GIT_LS_TREE = qr{
10+
\A
11+
(?<permissions>\d+)
12+
\s+
13+
(?<object_type>blob|tree)
14+
\s+
15+
(?<object_id>[a-fA-F0-9]{40})
16+
\s+
17+
(?<filename>\S+)
18+
\z
19+
}x;
20+
21+
sub get_blob_iterator {
22+
my ( $tree, $starting_path ) = @_;
23+
24+
my @paths = split('/', $starting_path);
25+
26+
while(@paths) {
27+
my $directory = shift @paths;
28+
29+
my $found_tree;
30+
31+
open my $pipe, '-|', 'git', 'ls-tree', $tree;
32+
while(<$pipe>) {
33+
chomp;
34+
35+
if(/$GIT_LS_TREE/) {
36+
if($directory eq $+{'filename'}) {
37+
$found_tree = 1;
38+
$tree = $+{'object_id'};
39+
last;
40+
}
41+
} else {
42+
die "Invalid output from git-ls-tree: $_";
43+
}
44+
}
45+
close $pipe;
46+
47+
unless($found_tree) {
48+
die "Unable to find path component '$directory'";
49+
}
50+
}
51+
52+
open my $pipe, '-|', 'git', 'ls-tree', '-r', $tree;
53+
54+
return sub {
55+
my $line = <$pipe>;
56+
57+
unless(defined($line)) {
58+
close $pipe;
59+
return;
60+
}
61+
62+
chomp $line;
63+
64+
if($line =~ /$GIT_LS_TREE/) {
65+
my ( $object_id, $filename ) = @+{qw/object_id filename/};
66+
67+
open my $other_pipe, '-|', 'git', 'cat-file', 'blob', $object_id;
68+
my $content = do {
69+
local $/;
70+
<$other_pipe>;
71+
};
72+
close $other_pipe;
73+
74+
return ( $starting_path . '/' . $filename, $content );
75+
} else {
76+
die "Invalid output from git-ls-tree: $line";
77+
}
78+
};
79+
}
80+
81+
sub get_corpus_contents {
82+
my ( $filename ) = @_;
83+
84+
open my $pipe, '-|', 'git', 'show', 'p5-corpus:' . $filename;
85+
my $content = do {
86+
local $/;
87+
<$pipe>
88+
};
89+
close $pipe;
90+
return $content;
91+
}
92+
93+
sub get_html_output_for {
94+
my ( $filename ) = @_;
95+
96+
$filename =~ s{\Acorpus/}{corpus_html/};
97+
$filename .= '.html';
98+
99+
return get_corpus_contents($filename);
100+
}
101+
102+
sub get_folds_for {
103+
my ( $filename ) = @_;
104+
105+
$filename =~ s{\Acorpus/}{corpus_html/};
106+
$filename .= '-folds.json';
107+
108+
my $contents = get_corpus_contents($filename);
109+
110+
return @{ decode_json($contents) };
111+
}
112+
113+
our @EXPORT = qw(get_blob_iterator get_corpus_contents get_html_output_for get_folds_for);
114+
115+
1;

0 commit comments

Comments
 (0)