]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Feed.pm
thread: common sorting code
[public-inbox.git] / lib / PublicInbox / Feed.pm
index dd9915324af33995205bb6509cb80dd08df75ff2..b53255975a3f309be44059fcc6fc7ec4436d86c8 100644 (file)
@@ -61,13 +61,7 @@ sub generate_html_index {
        my $git_dir = $args->{git_dir};
        my $git = PublicInbox::GitCatFile->new($git_dir);
        my ($first, $last) = each_recent_blob($args, sub {
-               my $mime = do_cat_mail($git, $_[0]) or return 0;
-
-               my $t = eval { str2time($mime->header('Date')) };
-               defined($t) or $t = 0;
-               $mime->header_set('X-PI-Date', $t);
-               push @messages, $mime;
-               1;
+               mime_load_for_sort($git, $_[0], \@messages);
        });
        $git = undef; # destroy pipes.
 
@@ -79,19 +73,12 @@ sub generate_html_index {
                '</head><body>' . PRE_WRAP;
 
        # sort child messages in chronological order
-       $th->order(sub {
-               sort {
-                       $a->topmost->message->header('X-PI-Date') <=>
-                       $b->topmost->message->header('X-PI-Date')
-               } @_;
-       });
+       $th->order(*PublicInbox::Thread::sort_ts);
 
        # except we sort top-level messages reverse chronologically
        my $state = [ time, {}, $first, 0 ];
-       for (sort { (eval { $b->message->header('X-PI-Date') } || 0) <=>
-                   (eval { $a->message->header('X-PI-Date') } || 0)
-                 } $th->rootset) {
-               dump_msg($_, 0, \$html, $state);
+       for (PublicInbox::Thread::rsort_ts($th->rootset)) {
+               dump_msg($_, 0, \$html, $state)
        }
        Email::Address->purge_cache;
 
@@ -168,14 +155,14 @@ sub each_recent_blob {
                        }
                } elsif ($line =~ /$delmsg/o) {
                        $deleted{$1} = 1;
-               } elsif ($line =~ /^commit (${hex}{7,40})/) {
+               } elsif ($line =~ /^commit (${hex}{7,40})/o) {
                        push @commits, $1;
                }
        }
 
        if ($last) {
                while (my $line = <$log>) {
-                       if ($line =~ /^commit (${hex}{7,40})/) {
+                       if ($line =~ /^commit (${hex}{7,40})/o) {
                                push @commits, $1;
                                last;
                        }
@@ -308,4 +295,15 @@ sub do_cat_mail {
        $@ ? undef : $mime;
 }
 
+sub mime_load_for_sort {
+       my ($git, $path, $messages) = @_;
+       my $mime = do_cat_mail($git, $path) or return 0;
+
+       my $t = eval { str2time($mime->header('Date')) };
+       defined($t) or $t = 0;
+       $mime->header_set('X-PI-TS', $t);
+       push @$messages, $mime;
+       1;
+}
+
 1;