lib/Gitalist/Utils.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 90 CC 13 SCAR 36.9
CC
13
Cov
0%
age_string
72.0
mode_string
42.0
is_binary
2.0
CRAP 40.1
1 package Gitalist::Utils;
2 use strict;
3 use warnings;
4 use Exporter qw/import/;
5  
6 our @EXPORT_OK = qw/
7 age_string
8 mode_string
9 /;
10  
11 sub age_string {
12 my $age = shift;
13 my $age_str;
14  
15 if ( $age > 60 * 60 * 24 * 365 * 2 ) {
16 $age_str = ( int $age / 60 / 60 / 24 / 365 );
17 $age_str .= " years ago";
18 }
19 elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
20 $age_str = int $age / 60 / 60 / 24 / ( 365 / 12 );
21 $age_str .= " months ago";
22 }
23 elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
24 $age_str = int $age / 60 / 60 / 24 / 7;
25 $age_str .= " weeks ago";
26 }
27 elsif ( $age > 60 * 60 * 24 * 2 ) {
28 $age_str = int $age / 60 / 60 / 24;
29 $age_str .= " days ago";
30 }
31 elsif ( $age > 60 * 60 * 2 ) {
32 $age_str = int $age / 60 / 60;
33 $age_str .= " hours ago";
34 }
35 elsif ( $age > 60 * 2 ) {
36 $age_str = int $age / 60;
37 $age_str .= " min ago";
38 }
39 elsif ( $age > 2 ) {
40 $age_str = int $age;
41 $age_str .= " sec ago";
42 }
43 else {
44 $age_str .= " right now";
45 }
46 return $age_str;
47 }
48  
49 sub is_binary {
50 # Crappy heuristic - does the first line or so look printable?
51 return $_[0] !~ /^[[:print:]]+$ (?: \s ^[[:print:]]+$ )?/mx;
52 }
53  
54 # via gitweb.pm circa line 1305
55 use Fcntl ':mode';
56 use constant {
57 S_IFINVALID => 0030000,
58 S_IFGITLINK => 0160000,
59 };
60  
61 # submodule/subrepository, a commit object reference
62 sub S_ISGITLINK($) {
63 return (($_[0] & S_IFMT) == S_IFGITLINK)
64 }
65  
66 # convert file mode in octal to symbolic file mode string
67 sub mode_string {
68 my $mode = shift;
69  
70 if (S_ISGITLINK($mode)) {
71 return 'm---------';
72 } elsif (S_ISDIR($mode & S_IFMT)) {
73 return 'drwxr-xr-x';
74 } elsif ($^O ne 'MSWin32' and S_ISLNK($mode)) { # this is ENOLINKS country, we can't stop here!
75 return 'lrwxrwxrwx';
76 } elsif (S_ISREG($mode)) {
77 # git cares only about the executable bit
78 if ($mode & S_IXUSR) {
79 return '-rwxr-xr-x';
80 } else {
81 return '-rw-r--r--';
82 }
83 } else {
84 return '----------';
85 }
86 }
87  
88 1;
89  
90 __END__