From 03d052da5001caeb6427b08feebdd35dba8cfe06 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Wed, 22 Apr 2015 12:15:11 -0700 Subject: [PATCH] gitweb: refactor age_string computation To facilitate caching, the age_string function needs to take as its arguments not the actual age in seconds, but the actual unix epoch creation time and the unix epoch to compute the age string against (which defaults to now) instead. To get the old behavior, passing in 0 for the first argument and the age in seconds as the second will suffice. Signed-off-by: Kyle J. McKay --- gitweb/gitweb.perl | 86 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 28 deletions(-) diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl index 1817c85826..9defb71c30 100755 --- a/gitweb/gitweb.perl +++ b/gitweb/gitweb.perl @@ -1936,13 +1936,15 @@ sub esc_html_match_hl_chopped { ## ---------------------------------------------------------------------- ## functions returning short strings -# CSS class for given age value (in seconds) +# CSS class for given age epoch value (in seconds) +# and reference time (optional, defaults to now) as second value sub age_class { - my $age = shift; + my ($age_epoch, $time_now) = @_; + return "noage" unless defined $age_epoch; + defined $time_now or $time_now = time; + my $age = $time_now - $age_epoch; - if (!defined $age) { - return "noage"; - } elsif ($age < 60*60*2) { + if ($age < 60*60*2) { return "age0"; } elsif ($age < 60*60*24*2) { return "age1"; @@ -1951,9 +1953,15 @@ sub age_class { } } -# convert age in seconds to "nn units ago" string +# convert age epoch in seconds to "nn units ago" string +# reference time used is now unless second argument passed in +# to get the old behavior, pass 0 as the first argument and +# the time in seconds as the second sub age_string { - my $age = shift; + my ($age_epoch, $time_now) = @_; + return "unknown" unless defined $age_epoch; + defined $time_now or $time_now = time; + my $age = $time_now - $age_epoch; my $age_str; if ($age > 60*60*24*365*2) { @@ -1983,6 +1991,38 @@ sub age_string { return $age_str; } +# returns age_string if the age is <= 2 weeks otherwise an absolute date +# this is typically shown to the user directly with the age_string_age as a title +sub age_string_date { + my ($age_epoch, $time_now) = @_; + return "unknown" unless defined $age_epoch; + defined $time_now or $time_now = time; + my $age = $time_now - $age_epoch; + + if ($age > 60*60*24*7*2) { + my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($age_epoch); + return sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday; + } else { + return age_string($age_epoch, $time_now); + } +} + +# returns an absolute date if the age is <= 2 weeks otherwise age_string +# this is typically used for the 'title' attribute so it will show as a tooltip +sub age_string_age { + my ($age_epoch, $time_now) = @_; + return "unknown" unless defined $age_epoch; + defined $time_now or $time_now = time; + my $age = $time_now - $age_epoch; + + if ($age > 60*60*24*7*2) { + return age_string($age_epoch, $time_now); + } else { + my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($age_epoch); + return sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday; + } +} + use constant { S_IFINVALID => 0030000, S_IFGITLINK => 0160000, @@ -3587,17 +3627,12 @@ sub parse_commit_text { } $co{'comment'} = \@commit_lines; - my $age = time - $co{'committer_epoch'}; - $co{'age'} = $age; - $co{'age_string'} = age_string($age); - my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'}); - if ($age > 60*60*24*7*2) { - $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday; - $co{'age_string_age'} = $co{'age_string'}; - } else { - $co{'age_string_date'} = $co{'age_string'}; - $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday; - } + my $age_epoch = time - $co{'committer_epoch'}; + $co{'age_epoch'} = $age_epoch; + my $time_now = time; + $co{'age_string'} = age_string($age_epoch, $time_now); + $co{'age_string_date'} = age_string_date($age_epoch, $time_now); + $co{'age_string_age'} = age_string_age($age_epoch, $time_now); return %co; } @@ -3819,7 +3854,7 @@ sub git_get_heads_list { $ref_item{'title'} = $title || '(no commit message)'; $ref_item{'epoch'} = $epoch; if ($epoch) { - $ref_item{'age'} = age_string(time - $ref_item{'epoch'}); + $ref_item{'age'} = age_string($ref_item{'epoch'}); } else { $ref_item{'age'} = "unknown"; } @@ -3867,7 +3902,7 @@ sub git_get_tags_list { if ($type eq "tag" || $type eq "commit") { $ref_item{'epoch'} = $epoch; if ($epoch) { - $ref_item{'age'} = age_string(time - $ref_item{'epoch'}); + $ref_item{'age'} = age_string($ref_item{'epoch'}); } else { $ref_item{'age'} = "unknown"; } @@ -5918,14 +5953,9 @@ sub git_project_list_rows { print "" . chop_and_escape_str($pr->{'owner'}, 15) . "\n"; } unless ($omit_age_column) { - my ($age, $age_string, $age_epoch); - if (defined($age_epoch = $pr->{'age_epoch'})) { - $age = $now - $age_epoch; - $age_string = age_string($age); - } else { - $age_string = "No commits"; - } - print "" . $age_string . "\n"; + my ($age_epoch, $age_string) = ($pr->{'age_epoch'}); + $age_string = defined $age_epoch ? age_string($age_epoch, $now) : "No commits"; + print "" . $age_string . "\n"; } print"" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " . -- 2.11.4.GIT