Massive tree action speedup
[catagits/Gitalist.git] / lib / Gitalist / Utils.pm
CommitLineData
c113db92 1package Gitalist::Utils;
2use strict;
3use warnings;
4use Exporter qw/import/;
5
6our @EXPORT_OK = qw/
7 age_string
eb8ee28a 8 mode_string
c113db92 9/;
10
11sub 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
53a9d6de 49sub is_binary {
b30f0e2e 50 # Crappy heuristic - does the first line or so look printable?
51 return $_[0] !~ /^[[:print:]]+$ (?: \s ^[[:print:]]+$ )?/mx;
53a9d6de 52}
53
eb8ee28a 54# via gitweb.pm circa line 1305
55use Fcntl ':mode';
56use constant {
57 S_IFINVALID => 0030000,
58 S_IFGITLINK => 0160000,
59};
60
61# submodule/subrepository, a commit object reference
62sub S_ISGITLINK($) {
63 return (($_[0] & S_IFMT) == S_IFGITLINK)
64}
65
66# convert file mode in octal to symbolic file mode string
67sub 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
c113db92 881;
89
90__END__
91
92=head1 NAME
93
94Gitalist::Utils - trivial utils for Gitalist
95
96=head2 FUNCTIONS
97
98=head2 age_string
99
53a9d6de 100Turns an integer number of seconds into a string.
101
102=head2 is_binary
103
104Check whether a string is binary according to C<-B>.
c113db92 105
106=head1 AUTHORS
107
108See L<Gitalist> for authors.
109
110=head1 LICENSE
111
112See L<Gitalist> for the license.
113
114=cut