gitweb: Move git-ls-tree output parsing to parse_ls_tree_line
[gitweb.git] / gitweb / gitweb.perl
index ba21a4770da757b7d7a5b370c95cc1ea917b226b..758032af64a0ce5a54b87dec449e8b1ed7d65dc6 100755 (executable)
@@ -152,7 +152,7 @@ sub feature_snapshot {
 our @diff_opts = ('-M'); # taken from git_commit
 
 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
-require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
+do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
 
 # version of the core git binary
 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
@@ -1034,6 +1034,27 @@ sub parse_difftree_raw_line {
        return wantarray ? %res : \%res;
 }
 
+# parse line of git-ls-tree output
+sub parse_ls_tree_line ($;%) {
+       my $line = shift;
+       my %opts = @_;
+       my %res;
+
+       #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
+       $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
+
+       $res{'mode'} = $1;
+       $res{'type'} = $2;
+       $res{'hash'} = $3;
+       if ($opts{'-z'}) {
+               $res{'name'} = $4;
+       } else {
+               $res{'name'} = unquote($4);
+       }
+
+       return wantarray ? %res : \%res;
+}
+
 ## ......................................................................
 ## parse to array of hashes functions
 
@@ -1354,6 +1375,24 @@ sub git_print_header_div {
              "\n</div>\n";
 }
 
+#sub git_print_authorship (\%) {
+sub git_print_authorship {
+       my $co = shift;
+
+       my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
+       print "<div class=\"author_date\">" .
+             esc_html($co->{'author_name'}) .
+             " [$ad{'rfc2822'}";
+       if ($ad{'hour_local'} < 6) {
+               printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
+                      $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
+       } else {
+               printf(" (%02d:%02d %s)",
+                      $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
+       }
+       print "]</div>\n";
+}
+
 sub git_print_page_path {
        my $name = shift;
        my $type = shift;
@@ -1397,6 +1436,7 @@ ($;%)
        foreach my $line (@$log) {
                if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
                        $signoff = 1;
+                       $empty = 0;
                        if (! $opts{'-remove_signoff'}) {
                                print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
                                next;
@@ -1432,7 +1472,6 @@ sub git_print_simplified_log {
 
        git_print_log($log,
                -final_empty_line=> 1,
-               -remove_signoff => 1,
                -remove_title => $remove_title);
 }
 
@@ -1450,6 +1489,7 @@ sub git_difftree_body {
 
        print "<table class=\"diff_tree\">\n";
        my $alternate = 0;
+       my $patchno = 0;
        foreach my $line (@{$difftree}) {
                my %diff = parse_difftree_raw_line($line);
 
@@ -1490,8 +1530,14 @@ sub git_difftree_body {
                              "<td class=\"link\">" .
                              $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
                                                     hash_base=>$hash, file_name=>$diff{'file'})},
-                                     "blob") .
-                             "</td>\n";
+                                     "blob");
+                       if ($action == "commitdiff") {
+                               # link to patch
+                               $patchno++;
+                               print " | " .
+                                     $cgi->a({-href => "#patch$patchno"}, "patch");
+                       }
+                       print "</td>\n";
 
                } elsif ($diff{'status'} eq "D") { # deleted
                        my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
@@ -1505,8 +1551,14 @@ sub git_difftree_body {
                              $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
                                                     hash_base=>$parent, file_name=>$diff{'file'})},
                                      "blob") .
-                             " | " .
-                             $cgi->a({-href => href(action=>"history", hash_base=>$parent,
+                             " | ";
+                       if ($action == "commitdiff") {
+                               # link to patch
+                               $patchno++;
+                               print " | " .
+                                     $cgi->a({-href => "#patch$patchno"}, "patch");
+                       }
+                       print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
                                                     file_name=>$diff{'file'})},
                                      "history") .
                              "</td>\n";
@@ -1542,16 +1594,23 @@ sub git_difftree_body {
                        print "</td>\n" .
                              "<td>$mode_chnge</td>\n" .
                              "<td class=\"link\">" .
-                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
-                                                      hash_base=>$hash, file_name=>$diff{'file'})},
-                                       "blob");
+                             $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
+                                                    hash_base=>$hash, file_name=>$diff{'file'})},
+                                     "blob");
                        if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
-                               print " | " .
-                                       $cgi->a({-href => href(action=>"blobdiff",
-                                                              hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
-                                                              hash_base=>$hash, hash_parent_base=>$parent,
-                                                              file_name=>$diff{'file'})},
-                                               "diff");
+                               if ($action == "commitdiff") {
+                                       # link to patch
+                                       $patchno++;
+                                       print " | " .
+                                               $cgi->a({-href => "#patch$patchno"}, "patch");
+                               } else {
+                                       print " | " .
+                                               $cgi->a({-href => href(action=>"blobdiff",
+                                                                      hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
+                                                                      hash_base=>$hash, hash_parent_base=>$parent,
+                                                                      file_name=>$diff{'file'})},
+                                                       "diff");
+                               }
                        }
                        print " | " .
                                $cgi->a({-href => href(action=>"history",
@@ -1581,12 +1640,19 @@ sub git_difftree_body {
                                                     hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
                                      "blob");
                        if ($diff{'to_id'} ne $diff{'from_id'}) {
-                               print " | " .
-                                       $cgi->a({-href => href(action=>"blobdiff",
-                                                              hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
-                                                              hash_base=>$hash, hash_parent_base=>$parent,
-                                                              file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
-                                               "diff");
+                               if ($action == "commitdiff") {
+                                       # link to patch
+                                       $patchno++;
+                                       print " | " .
+                                               $cgi->a({-href => "#patch$patchno"}, "patch");
+                               } else {
+                                       print " | " .
+                                               $cgi->a({-href => href(action=>"blobdiff",
+                                                                      hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
+                                                                      hash_base=>$hash, hash_parent_base=>$parent,
+                                                                      file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
+                                                       "diff");
+                               }
                        }
                        print "</td>\n";
 
@@ -1619,7 +1685,7 @@ sub git_patchset_body {
                                # first patch in patchset
                                $patch_found = 1;
                        }
-                       print "<div class=\"patch\">\n";
+                       print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
 
                        if (ref($difftree->[$patch_idx]) eq "HASH") {
                                $diffinfo = $difftree->[$patch_idx];
@@ -2467,51 +2533,54 @@ sub git_tree {
        print "<table cellspacing=\"0\">\n";
        my $alternate = 0;
        foreach my $line (@entries) {
-               #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
-               $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
-               my $t_mode = $1;
-               my $t_type = $2;
-               my $t_hash = $3;
-               my $t_name = validate_input($4);
+               my %t = parse_ls_tree_line($line, -z => 1);
+
                if ($alternate) {
                        print "<tr class=\"dark\">\n";
                } else {
                        print "<tr class=\"light\">\n";
                }
                $alternate ^= 1;
-               print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
-               if ($t_type eq "blob") {
+
+               print "<td class=\"mode\">" . mode_str($t{'mode'}) . "</td>\n";
+               if ($t{'type'} eq "blob") {
                        print "<td class=\"list\">" .
-                             $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key),
-                                     -class => "list"}, esc_html($t_name)) .
+                             $cgi->a({-href => href(action=>"blob", hash=>$t{'hash'},
+                                                    file_name=>"$base$t{'name'}", %base_key),
+                                     -class => "list"}, esc_html($t{'name'})) .
                              "</td>\n" .
                              "<td class=\"link\">" .
-                             $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
+                             $cgi->a({-href => href(action=>"blob", hash=>$t{'hash'},
+                                                    file_name=>"$base$t{'name'}", %base_key)},
                                      "blob");
                        if ($have_blame) {
                                print " | " .
-                                       $cgi->a({-href => href(action=>"blame", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
+                                       $cgi->a({-href => href(action=>"blame", hash=>$t{'hash'},
+                                                              file_name=>"$base$t{'name'}", %base_key)},
                                                "blame");
                        }
                        print " | " .
                              $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
-                                                    hash=>$t_hash, file_name=>"$base$t_name")},
+                                                    hash=>$t{'hash'}, file_name=>"$base$t{'name'}")},
                                      "history") .
                              " | " .
                              $cgi->a({-href => href(action=>"blob_plain",
-                                                    hash=>$t_hash, file_name=>"$base$t_name")},
+                                                    hash=>$t{'hash'}, file_name=>"$base$t{'name'}")},
                                      "raw") .
                              "</td>\n";
-               } elsif ($t_type eq "tree") {
+               } elsif ($t{'type'} eq "tree") {
                        print "<td class=\"list\">" .
-                             $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
-                                     esc_html($t_name)) .
+                             $cgi->a({-href => href(action=>"tree", hash=>$t{'hash'},
+                                                    file_name=>"$base$t{'name'}", %base_key)},
+                                     esc_html($t{'name'})) .
                              "</td>\n" .
                              "<td class=\"link\">" .
-                             $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
+                             $cgi->a({-href => href(action=>"tree", hash=>$t{'hash'},
+                                                    file_name=>"$base$t{'name'}", %base_key)},
                                      "tree") .
                              " | " .
-                             $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, file_name=>"$base$t_name")},
+                             $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
+                                                    file_name=>"$base$t{'name'}")},
                                      "history") .
                              "</td>\n";
                }
@@ -2733,10 +2802,6 @@ sub git_blobdiff {
                        @difftree
                                or die_error('404 Not Found', "Blob diff not found");
 
-               } elsif (defined $hash) { # try to find filename from $hash
-                       if ($hash !~ /[0-9a-fA-F]{40}/) {
-                               $hash = git_to_hash($hash);
-                       }
                } elsif (defined $hash &&
                         $hash =~ /[0-9a-fA-F]{40}/) {
                        # try to find filename from $hash
@@ -2934,6 +2999,7 @@ sub git_commitdiff {
                git_header_html(undef, $expires);
                git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
                git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
+               git_print_authorship(\%co);
                print "<div class=\"page_body\">\n";
                print "<div class=\"log\">\n";
                git_print_simplified_log($co{'comment'}, 1); # skip title
@@ -2966,8 +3032,8 @@ sub git_commitdiff {
 
        # write patch
        if ($format eq 'html') {
-               #git_difftree_body(\@difftree, $hash, $hash_parent);
-               #print "<br/>\n";
+               git_difftree_body(\@difftree, $hash, $hash_parent);
+               print "<br/>\n";
 
                git_patchset_body($fd, \@difftree, $hash, $hash_parent);
                close $fd;