gitweb: Move git-ls-tree output parsing to parse_ls_tree_line
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/4 gitweb: Move git-ls-tree output parsing to parse_ls_tree_line
Add new subroutine parse_ls_tree_line and use it in git_tree.
---
This patch could be applied regardless of the fact that this "tree_blame" view series is proof-of-concept series.
gitweb/gitweb.perl | 62 ++++++++++++++++++++++++++++++++++++---------------- 1 files changed, 43 insertions(+), 19 deletions(-)
gitweb/gitweb.perl +43 -19
@@ -1021,6 +1021,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
@@ -2498,51 +2519,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"; }
Signature · 1 line
1.4.1.1
PATCH 2/4 gitweb: Separate printing of git_tree row into git_print_tree_entry
In reply to a message not in the archiveThis is preparation for "tree blame" (similar to what ViewVC shows) output, i.e. for each entry give commit where it was changed.
---
This could be applied even though it is proof-of-concept series, but I think it would be better to separate git_tree_body rather, although perhaps something like git_print_tree_entry subroutine is a good idea.
gitweb/gitweb.perl | 103 ++++++++++++++++++++++++++++++---------------------- 1 files changed, 59 insertions(+), 44 deletions(-)
gitweb/gitweb.perl +59 -44
@@ -1462,6 +1462,62 @@ sub git_print_simplified_log { -remove_title => $remove_title); } +# print tree entry (row of git_tree), but without encompassing <tr> element +sub git_print_tree_entry { + my ($t, $basedir, $hash_base, $have_blame) = @_; + + my %base_key = (); + $base_key{hash_base} = $hash_base if defined $hash_base; + + 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=>"$basedir$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=>"$basedir$t->{'name'}", %base_key)}, + "blob"); + if ($have_blame) { + print " | " . + $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'}, + file_name=>"$basedir$t->{'name'}", %base_key)}, + "blame"); + } + if (defined $hash_base) { + print " | " . + $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, + hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")}, + "history"); + } + print " | " . + $cgi->a({-href => href(action=>"blob_plain", + hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")}, + "raw") . + "</td>\n"; + + } elsif ($t->{'type'} eq "tree") { + print "<td class=\"list\">" . + $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'}, + file_name=>"$basedir$t->{'name'}", %base_key)}, + esc_html($t->{'name'})) . + "</td>\n" . + "<td class=\"link\">" . + $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'}, + file_name=>"$basedir$t->{'name'}", %base_key)}, + "tree"); + if (defined $hash_base) { + print " | " . + $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, + file_name=>"$basedir$t->{'name'}")}, + "history"); + } + print "</td>\n"; + } +} + ## ...................................................................... ## functions printing large fragments of HTML
@@ -2499,14 +2555,13 @@ sub git_tree { my $refs = git_get_references(); my $ref = format_ref_marker($refs, $hash_base); git_header_html(); - my %base_key = (); my $base = ""; my $have_blame = gitweb_check_feature('blame'); if (defined $hash_base && (my %co = parse_commit($hash_base))) { - $base_key{hash_base} = $hash_base; git_print_page_nav('tree','', $hash_base); git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base); } else { + undef $hash_base; print "<div class=\"page_nav\">\n"; print "<br/><br/></div>\n"; print "<div class=\"title\">$hash</div>\n";
@@ -2528,48 +2583,8 @@ sub git_tree { } $alternate ^= 1; - 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'})) . - "</td>\n" . - "<td class=\"link\">" . - $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)}, - "blame"); - } - print " | " . - $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, - hash=>$t{'hash'}, file_name=>"$base$t{'name'}")}, - "history") . - " | " . - $cgi->a({-href => href(action=>"blob_plain", - hash=>$t{'hash'}, file_name=>"$base$t{'name'}")}, - "raw") . - "</td>\n"; - } 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'})) . - "</td>\n" . - "<td class=\"link\">" . - $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'}")}, - "history") . - "</td>\n"; - } + git_print_tree_entry(\%t, $base, $hash_base, $have_blame); + print "</tr>\n"; } print "</table>\n" .
Signature · 1 line
1.4.1.1
PATCH 3/4 gitweb: Extend parse_difftree_raw_line to save commit info
In reply to a message not in the archiveExtend parse_difftree_raw_line to save commit info from when git-diff-tree is given only one <tree-ish>, for example when fed from git-rev-list using --stdin option.
git-diff-tree outputs a line with the commit ID when applicable.
---
This can be applied regardless that it is proof-of-concept series. Need checking if it doesn't break anything.
gitweb/gitweb.perl | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
gitweb/gitweb.perl +3 -3
@@ -1014,9 +1014,9 @@ sub parse_difftree_raw_line { } } # 'c512b523472485aef4fff9e57b229d9d243c967f' - #elsif ($line =~ m/^([0-9a-fA-F]{40})$/) { - # $res{'commit'} = $1; - #} + elsif ($line =~ m/^([0-9a-fA-F]{40})$/) { + $res{'commit'} = $1; + } return wantarray ? %res : \%res; }
Signature · 1 line
1.4.1.1
PATCH 4/4 gitweb: Add "tree_blame" view (WIP)
In reply to a message not in the archiveAdds git_tree_blame subroutine, for the "tree_blame" view, similar to the ViewVC output. It means having the commit, date and author of last change to the file (blob) or directory (tree) in given tree in addition to the default "tree" view info.
The git_tree_blame subroutine contains 3 implementations: one using git-diff-tree to detect when file was changed (works only on blobs, and not on trees/directories), one comparing output of git-ls-tree to check what changed between revisions, and one (default used) looking at two first commits in the blob or tree history.
While at it, add 'year' field to the parse_date output; add "blame" link to the list of links for tree entries in directory listing in git_print_tree_entry; add support for 'order' parameter to href().
--- gitweb/gitweb.perl | 344 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 344 insertions(+), 0 deletions(-)
gitweb/gitweb.perl +344 -0
@@ -264,6 +264,7 @@ my %actions = ( "tag" => \&git_tag, "tags" => \&git_tags, "tree" => \&git_tree, + "tree_blame" => \&git_tree_blame, "snapshot" => \&git_snapshot, # those below don't need $project "opml" => \&git_opml,
@@ -298,6 +299,7 @@ sub href(%) { hash_parent_base => "hpb", page => "pg", searchtext => "s", + order => "o", ); my %mapping = @mapping;
@@ -804,6 +806,7 @@ sub parse_date { $date{'hour'} = $hour; $date{'minute'} = $min; $date{'mday'} = $mday; + $date{'year'} = 1900+$year; $date{'day'} = $days[$wday]; $date{'month'} = $months[$mon]; $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
@@ -1508,6 +1511,12 @@ sub git_print_tree_entry { $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}", %base_key)}, "tree"); + if ($have_blame) { + print " | " . + $cgi->a({-href => href(action=>"tree_blame", hash=>$t->{'hash'}, + file_name=>"$basedir$t->{'name'}", %base_key)}, + "blame"); + } if (defined $hash_base) { print " | " . $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
@@ -2592,6 +2601,341 @@ sub git_tree { git_footer_html(); } +sub git_tree_blame { + my $version = shift || 'rev-list'; + + # check permission + my $have_blame = gitweb_check_feature('blame'); + if (!$have_blame) { + die_error('403 Permission denied', "Permission denied"); + } + # get sort order parameter + my $order = $cgi->param('o'); + if (defined $order && $order !~ m/[+-]?age|[+-]?author|[+-]?mode|[+-]?name/) { + die_error(undef, "Unknown order parameter"); + } + # check hash + if (!defined $hash) { + $hash = git_get_head_hash($project); + if (defined $file_name) { + my $base = $hash_base || $hash; + $hash = git_get_hash_by_path($base, $file_name, "tree"); + } + } + # check hash_base + if (!defined $hash_base) { + $hash_base = $hash; + } + # check if we have starting (base) commit + my %co = parse_commit($hash_base); + if (!%co) { + die_error(undef, "Base commit not defined"); + } + # set basedir (dirname for entries) + my $basedir; + if (defined $file_name) { + $basedir = "$file_name/"; + } + + # read tree + $/ = "\0"; + open my $fd, "-|", $GIT, "ls-tree", '-z', $hash + or die_error(undef, "Open git-ls-tree failed"); + my @entries = map { chomp; $_ } <$fd>; + close $fd + or die_error(undef, "Reading git-ls-tree failed"); + $/ = "\n"; + + # parse tree + @entries = map { + scalar parse_ls_tree_line($_, -z => 1); + } @entries; + + ## !!! two versions: one using git-diff-tree, without directories, + ## !!! second using git-ls-files, with directories, slower. + + # blame + my $tofind = scalar @entries; + my %idx; + for (my $i = 0; $i < @entries; $i++) { + $idx{"$basedir$entries[$i]{'name'}"} = $i; + } + my $commit = $hash_base; + + if ($version eq 'diff-tree') { + open $fd, "-|", "$GIT rev-list $commit -- $basedir/" . + "| $GIT diff-tree -r -- $basedir/" + or die_error(undef, "Open git-rev-list or git-diff-tree failed"); + + while ($tofind > 0 && (my $line = <$fd>)) { + chomp $line; + my %diffinfo = parse_difftree_raw_line($line); + if ($diffinfo{'commit'}) { + $commit = $diffinfo{'commit'}; + } else { + my $name = $diffinfo{'to_name'} || $diffinfo{'name'}; + my $fullname = $basedir . $name; + if (!exists $entries[$idx{$fullname}]{'commit'}) { + $entries[$idx{$fullname}]{'commit'} = $commit; + $entries[$idx{$fullname}]{'added'} = + ($diffinfo{'status'} eq 'A'); + $tofind--; + } + } + } + + } elsif ($version eq 'ls-tree') { + + TOFIND: + while ($tofind > 0) { + # workaround the lack of git-ls-tree --stdin + open $fd, "-|", $GIT, "rev-list", "--max-count=100", "--remove-empty", + "$commit^", "--", $basedir + or die_error(undef, "Open git-rev-list failed"); + my @revlist = map { chomp; $_ } <$fd>; + close $fd + or die_error(undef, "Reading git-rev-list failed"); + + # exit if no revisions found + last unless @revlist; + + $/ = "\0"; + my $prev_commit = $commit; + COMMIT: + foreach $commit (@revlist) { + # read tree + open my $fd, "-|", $GIT, "ls-tree", '-z', $commit, "--", $basedir + or die_error(undef, "Open git-ls-tree failed"); + my @ls_tree = map { chomp; $_ } <$fd>; + close $fd + or die_error(undef, "Reading git-ls-tree failed"); + # parse tree + @ls_tree = map { + scalar parse_ls_tree_line($_, -z => 1); + } @ls_tree; + + # find unchanged + my %unchanged; + my %changed; + foreach my $entry (@ls_tree) { + if (exists $idx{"$entry->{'name'}"} && + $entry->{'hash'} eq $entries[$idx{"$entry->{'name'}"}]{'hash'}) { + $unchanged{"$entry->{'name'}"} = 1; + } + if (exists $idx{"$entry->{'name'}"} && + $entry->{'hash'} ne $entries[$idx{"$entry->{'name'}"}]{'hash'}) { + $changed{"$entry->{'name'}"} = 1; + } + } + + # find those with changed id, and those that vanished + foreach my $entry (@entries) { + my $fullname = "$basedir$entry->{'name'}"; + if (!$unchanged{$fullname} && + !exists $entry->{'commit'}) { + $entry->{'commit'} = $prev_commit; + $entry->{'added'} = + !$changed{$fullname}; + $tofind--; + } + } + last TOFIND if $tofind <= 0; + + } continue { + $prev_commit = $commit; + } + $/ = "\n"; + + $commit = $revlist[-1]; + } + + # 'import' case: we attribute 'not found yet' to the last commit + if ($tofind > 0) { + foreach my $entry (@entries) { + if (!exists $entry->{'commit'}) { + $entry->{'commit'} = $commit; + $entry->{'added'} = 1; + } + } + } + + } elsif ($version eq 'rev-list') { + + foreach my $entry (@entries) { + my $fullname = "$basedir$entry->{'name'}"; + + open $fd, "-|", $GIT, "rev-list", "--max-count=2", + $hash_base, "--", $fullname + or die_error(undef, "Open git-rev-list failed"); + my @revlist = map { chomp; $_ } <$fd>; + close $fd + or die_error(undef, "Reading git-rev-list failed"); + + if ($#revlist >= 0) { + $entry->{'commit'} = $revlist[0]; + $entry->{'added'} = 1 + if (@revlist == 1); + } + } + + } else { + die_error(undef, "Unknown tree_blame version parameter"); + } + + # blame postprocessing + foreach my $entry (@entries) { + if (my %entry_co = parse_commit($entry->{'commit'})) { + $entry->{'title'} = $entry_co{'title'}; + $entry->{'epoch'} = $entry_co{'author_epoch'}; + $entry->{'author'} = $entry_co{'author_name'}; + if ($entry_co{'author'} =~ m/<([^@>]+)\@/) { + $entry->{'author'} = $1; + } + } + } + + + ### ---------------------------------------------- + # page header + my $refs = git_get_references(); + my $ref = format_ref_marker($refs, $hash_base); + git_header_html(); + git_print_page_nav('tree_blame','', $hash_base, $hash, $hash_base); + git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base); + git_print_page_path($file_name, 'tree', $hash_base); + print "<div class=\"page_body\">\n"; + print "<table class=\"tree blame\" cellspacing=\"0\">\n"; + + # sort and print table header + print "<thead>\n" . + "<tr>\n"; + print "<th>Commit</th>\n"; + if ($order =~ m/^([-+]?)age$/) { + my $sign = "$1" ? "${1}1" : -1; + @entries = sort { + if (exists $a->{'commit'} && exists $b->{'commit'}) { + $sign*($a->{'epoch'} <=> $b->{'epoch'}); + } else { + 0; + } + } @entries; + print "<th>Date</th>\n"; + } else { + print "<th>" . + $cgi->a({-href => href(action=>$action, hash=>$hash, + hash_base=>$hash_base, file_name=>$file_name, + order=>'age'), + -class => "header"}, "Date") . + "</th>\n"; + } + if ($order =~ m/^([-+]?)author$/) { + my $sign = "${1}1"; + @entries = sort { + if (exists $a->{'commit'} && exists $b->{'commit'}) { + $sign*($a->{'author'} cmp $b->{'author'}); + } else { + 0; + } + } @entries; + print "<th>Author</th>\n"; + } else { + print "<th>" . + $cgi->a({-href => href(action=>$action, hash=>$hash, + hash_base=>$hash_base, file_name=>$file_name, + order=>'author'), + -class => "header"}, "Author") . + "</th>\n"; + } + if ($order =~ m/^([-+]?)mode$/) { + my $sign = "${1}1"; + @entries = sort { + if (exists $a->{'commit'} && exists $b->{'commit'}) { + $sign*($a->{'mode'} <=> $b->{'mode'}); + } else { + 0; + } + } @entries; + print "<th>Mode</th>\n"; + } else { + print "<th>" . + $cgi->a({-href => href(action=>$action, hash=>$hash, + hash_base=>$hash_base, file_name=>$file_name, + order=>'mode'), + -class => "header"}, "Mode") . + "</th>\n"; + } + if ($order =~ m/^([-+]?)name$/) { + my $sign = "${1}1"; + @entries = sort { + if (exists $a->{'commit'} && exists $b->{'commit'}) { + $sign*($a->{'name'} cmp $b->{'name'}); + } else { + 0; + } + } @entries; + print "<th>Filename</th>\n"; + } else { + print "<th>" . + $cgi->a({-href => href(action=>$action, hash=>$hash, + hash_base=>$hash_base, file_name=>$file_name, + order=>'name'), + -class => "header"}, "Filename") . + "</th>\n"; + } + print <<HTML; +<th>Link</th> +</tr> +</thead> +<tbody> +HTML + + # print tree + my $alternate = 0; + foreach my $entry (@entries) { + if ($alternate) { + print "<tr class=\"dark\">\n"; + } else { + print "<tr class=\"light\">\n"; + } + $alternate ^= 1; + + if ($entry->{'commit'}) { + print $cgi->start_td({-class => "sha1", -title => $entry->{'title'}}) . + ($entry->{'added'} ? "<strike>" : "") . + $cgi->a({-href => href(action=>"commit", hash=>$entry->{'commit'}, + file_name=>$entry->{'name'})}, + esc_html(substr($entry->{'commit'}, 0, 8))) . + ($entry->{'added'} ? "</strike>" : "") . + "</td>"; + my %ad = parse_date($entry->{'epoch'}); + my (undef,undef,undef,undef,undef,$current_year) = gmtime(); + my $date_str; + if ($ad{'year'} == 1900+$current_year) { + $date_str = sprintf "%3s % 2u %02d:%02d", + $ad{'month'}, $ad{'mday'}, $ad{'hour'}, $ad{'minute'}; + } else { + $date_str = sprintf "%3s % 2u % 5d", + $ad{'month'}, $ad{'mday'}, $ad{'year'}; + } + $date_str =~ s/ / /g; + print "<td>$date_str</td>\n"; + print "<td>$entry->{'author'}</td>\n"; + } else { + print "<td></td>\n" . + "<td></td>\n" . + "<td></td>\n"; + } + git_print_tree_entry($entry, $basedir, $hash_base, + $have_blame && defined $hash_base); + + print "</tr>\n"; + } + print "</tbody>\n" . + "</table>\n" . + "</div>"; + git_footer_html(); +} + sub git_snapshot { my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
Signature · 1 line
1.4.1.1