aboutsummaryrefslogtreecommitdiff
path: root/time-getter.pl
blob: 7daafa0ba8f7334949741a2d6c6d8e7577c38e30 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env perl

use strict;

open (TREES, 'git ls-tree -r HEAD|');

my %blob_hash;
my %files_should_print;

# make the HEAD tree info
while (<TREES>) {
    my @info = split /\s+/;

    my $filename = $info[-1];
    my $hash     = $info[-2];

    $blob_hash{$filename} = $hash;
    $files_should_print{$filename} = 1;
}

close(TREES);

$? and die;

open (COMMITS, 'git rev-list HEAD |');

my $prev_timestamp;
while (<COMMITS>) {
    (keys %files_should_print) or last;

    my $commit = $_;

    open (COMMIT_CONTENT, 'git cat-file -p ' . $commit . '|');
    my $tree;
    my $timestamp;
    while (<COMMIT_CONTENT>) {
        if (/tree/) {
            my @tmp = split /\s+/;
            $tree = $tmp[1];
            next
        }
        if (/committer/) {
            my @tmp = split /\s+/;
            $timestamp = $tmp[-2];
            last;
        }
    }
    close(COMMIT_CONTENT);

    open (BLOBS, "git ls-tree -r $tree |");

    my %remained = %files_should_print;
    while (<BLOBS>) {
        my @tmp = split(/\s+/);
        my $filename = $tmp[-1];
        my $blobhash = $tmp[-2];

        if ($remained{$filename}) {
            delete $remained{$filename};
        }

        if (!$files_should_print{$filename}) {
            next;
        }


        if ($blob_hash{$filename} !~ /$blobhash/) {
            do_print($filename, $prev_timestamp);
        }
    }

    foreach my $filename (keys(%remained)) {
        do_print($filename, $prev_timestamp);
    }

    $prev_timestamp = $timestamp;

    close (BLOBS);
}

# For files that never changed from the root commit.
foreach my $filename (keys(%files_should_print)) {
    do_print($filename, $prev_timestamp);
}


sub do_print() {
    my ($filename, $timestamp) = @_;
    print $filename . "\t" . $timestamp . " \n";
    delete ($files_should_print{$filename});
}

close (COMMITS);