File: annotate.sh

package info (click to toggle)
lcov 2.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,464 kB
  • sloc: perl: 27,911; sh: 7,320; xml: 6,982; python: 1,152; makefile: 597; cpp: 520; ansic: 176
file content (45 lines) | stat: -rwxr-xr-x 1,379 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl

use strict;
use warnings;
use DateTime;
use POSIX qw(strftime);

sub get_modify_time($)
{
    my $filename = shift;
    my @stat     = stat $filename;
    my $tz       = strftime("%z", localtime($stat[9]));
    $tz =~ s/([0-9][0-9])$/:$1/;
    return strftime("%Y-%m-%dT%H:%M:%S", localtime($stat[9])) . $tz;
}

my $file      = $ARGV[0];
my $annotated = $file . ".annotated";
my $now       = DateTime->now();

if (open(HANDLE, '<', $annotated)) {
    while (my $line = <HANDLE>) {
        chomp $line;
        $line =~ s/\r//g;    # remove CR from line-end
        my ($commit, $who, $days, $text) = split(/\|/, $line, 4);
        my $duration = DateTime::Duration->new(days => $days);
        my $date     = $now - $duration;

        printf("%s|%s|%s|%s\n", $commit, $who, $date, $text);
    }
    close(HANDLE) or die("unable to close $annotated: $!");
} elsif (open(HANDLE, $file)) {
    my $mtime = get_modify_time($file);    # when was the file last modified?
    my $owner =
        getpwuid((stat($file))[4]);    # who does the filesystem think owns it?
    while (my $line = <HANDLE>) {
        chomp $line;
        # Also remove CR from line-end
        $line =~ s/\015$//;
        printf("%s|%s|%s|%s\n", "NONE", $owner, $mtime, $line);
    }
    close(HANDLE) or die("unable to close $annotated: $!");
} else {
    die("unable to open $file: $!");
}