From 5d9a4e18c8d5fe7f80e302a5f2439f219d50a39d Mon Sep 17 00:00:00 2001 From: Nao Ueda Date: Sat, 6 Feb 2021 09:20:58 +0900 Subject: refactoring --- time-getter.pl | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/time-getter.pl b/time-getter.pl index 7a29ba5..7daafa0 100644 --- a/time-getter.pl +++ b/time-getter.pl @@ -2,39 +2,38 @@ use strict; -open (F, 'git ls-tree -r HEAD|'); +open (TREES, 'git ls-tree -r HEAD|'); -my %list; -my %all_file; +my %blob_hash; +my %files_should_print; -# 辞書作る -while () { +# make the HEAD tree info +while () { my @info = split /\s+/; my $filename = $info[-1]; my $hash = $info[-2]; - $list{$filename} = $hash; - $all_file{$filename} = 1; + $blob_hash{$filename} = $hash; + $files_should_print{$filename} = 1; } -close(F); +close(TREES); $? and die; open (COMMITS, 'git rev-list HEAD |'); -# timestamp 取りつつ tree を get -my $prevtimestamp; -my $timestamp; +my $prev_timestamp; while () { - my $commit = $_; + (keys %files_should_print) or last; - (keys %list) or last; + my $commit = $_; - open (TMP, 'git cat-file -p ' . $commit . '|'); + open (COMMIT_CONTENT, 'git cat-file -p ' . $commit . '|'); my $tree; - while () { + my $timestamp; + while () { if (/tree/) { my @tmp = split /\s+/; $tree = $tmp[1]; @@ -46,11 +45,11 @@ while () { last; } } - close(TMP); + close(COMMIT_CONTENT); open (BLOBS, "git ls-tree -r $tree |"); - my %remained = %all_file; + my %remained = %files_should_print; while () { my @tmp = split(/\s+/); my $filename = $tmp[-1]; @@ -60,36 +59,35 @@ while () { delete $remained{$filename}; } - if (!$list{$filename}) { + if (!$files_should_print{$filename}) { next; } - if ($list{$filename} !~ /$blobhash/) { - do_print($filename, $prevtimestamp); + if ($blob_hash{$filename} !~ /$blobhash/) { + do_print($filename, $prev_timestamp); } } foreach my $filename (keys(%remained)) { - do_print($filename, $prevtimestamp); + do_print($filename, $prev_timestamp); } - $prevtimestamp = $timestamp; + $prev_timestamp = $timestamp; close (BLOBS); } # For files that never changed from the root commit. -foreach my $filename (keys(%all_file)) { - do_print($filename, $prevtimestamp); +foreach my $filename (keys(%files_should_print)) { + do_print($filename, $prev_timestamp); } sub do_print() { my ($filename, $timestamp) = @_; print $filename . "\t" . $timestamp . " \n"; - delete ($list{$filename}); - delete ($all_file{$filename}); + delete ($files_should_print{$filename}); } close (COMMITS); -- cgit v1.2.3-54-g00ecf