\n";
git_patchset_body($fd, $diff_style,
[ \%diffinfo ], $hash_base, $hash_parent_base);
close $fd;
print "
\n"; # class="page_body"
git_footer_html();
} else {
while (my $line = <$fd>) {
$line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
$line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
print $line;
last if $line =~ m!^\+\+\+!;
}
local $/ = undef;
print <$fd>;
close $fd;
}
}
sub git_blobdiff_plain {
git_blobdiff('plain');
}
# assumes that it is added as later part of already existing navigation,
# so it returns "| foo | bar" rather than just "foo | bar"
sub diff_style_nav {
my ($diff_style, $is_combined) = @_;
$diff_style ||= 'inline';
return "" if ($is_combined);
my @styles = (inline => 'inline', 'sidebyside' => 'side by side');
my %styles = @styles;
@styles =
@styles[ map { $_ * 2 } 0..$#styles/2 ];
return join '',
map { " | ".$_ }
map {
$_ eq $diff_style ? $styles{$_} :
$cgi->a({-href => href(-replay=>1, diff_style => $_)}, $styles{$_})
} @styles;
}
sub git_commitdiff {
my %params = @_;
my $format = $params{-format} || 'html';
my $diff_style = $input_params{'diff_style'} || 'inline';
my ($patch_max) = gitweb_get_feature('patches');
if ($format eq 'patch') {
die_error(403, "Patch view not allowed") unless $patch_max;
}
$hash ||= $hash_base || "HEAD";
my %co = parse_commit($hash)
or die_error(404, "Unknown commit object");
# choose format for commitdiff for merge
if (! defined $hash_parent && @{$co{'parents'}} > 1) {
$hash_parent = '--cc';
}
# we need to prepare $formats_nav before almost any parameter munging
my $formats_nav;
if ($format eq 'html') {
$formats_nav =
$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
"raw");
if ($patch_max && @{$co{'parents'}} <= 1) {
$formats_nav .= " | " .
$cgi->a({-href => href(action=>"patch", -replay=>1)},
"patch");
}
$formats_nav .= diff_style_nav($diff_style, @{$co{'parents'}} > 1);
if (defined $hash_parent &&
$hash_parent ne '-c' && $hash_parent ne '--cc') {
# commitdiff with two commits given
my $hash_parent_short = $hash_parent;
if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
$hash_parent_short = substr($hash_parent, 0, 7);
}
$formats_nav .=
' (from';
for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
if ($co{'parents'}[$i] eq $hash_parent) {
$formats_nav .= ' parent ' . ($i+1);
last;
}
}
$formats_nav .= ': ' .
$cgi->a({-href => href(-replay=>1,
hash=>$hash_parent, hash_base=>undef)},
esc_html($hash_parent_short)) .
')';
} elsif (!$co{'parent'}) {
# --root commitdiff
$formats_nav .= ' (initial)';
} elsif (scalar @{$co{'parents'}} == 1) {
# single parent commit
$formats_nav .=
' (parent: ' .
$cgi->a({-href => href(-replay=>1,
hash=>$co{'parent'}, hash_base=>undef)},
esc_html(substr($co{'parent'}, 0, 7))) .
')';
} else {
# merge commit
if ($hash_parent eq '--cc') {
$formats_nav .= ' | ' .
$cgi->a({-href => href(-replay=>1,
hash=>$hash, hash_parent=>'-c')},
'combined');
} else { # $hash_parent eq '-c'
$formats_nav .= ' | ' .
$cgi->a({-href => href(-replay=>1,
hash=>$hash, hash_parent=>'--cc')},
'compact');
}
$formats_nav .=
' (merge: ' .
join(' ', map {
$cgi->a({-href => href(-replay=>1,
hash=>$_, hash_base=>undef)},
esc_html(substr($_, 0, 7)));
} @{$co{'parents'}} ) .
')';
}
}
my $hash_parent_param = $hash_parent;
if (!defined $hash_parent_param) {
# --cc for multiple parents, --root for parentless
$hash_parent_param =
@{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
}
# read commitdiff
my $fd;
my @difftree;
if ($format eq 'html') {
open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
"--no-commit-id", "--patch-with-raw", "--full-index",
$hash_parent_param, $hash, "--"
or die_error(500, "Open git-diff-tree failed");
while (my $line = <$fd>) {
chomp $line;
# empty line ends raw part of diff-tree output
last unless $line;
push @difftree, scalar parse_difftree_raw_line($line);
}
} elsif ($format eq 'plain') {
open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
'-p', $hash_parent_param, $hash, "--"
or die_error(500, "Open git-diff-tree failed");
} elsif ($format eq 'patch') {
# For commit ranges, we limit the output to the number of
# patches specified in the 'patches' feature.
# For single commits, we limit the output to a single patch,
# diverging from the git-format-patch default.
my @commit_spec = ();
if ($hash_parent) {
if ($patch_max > 0) {
push @commit_spec, "-$patch_max";
}
push @commit_spec, '-n', "$hash_parent..$hash";
} else {
if ($params{-single}) {
push @commit_spec, '-1';
} else {
if ($patch_max > 0) {
push @commit_spec, "-$patch_max";
}
push @commit_spec, "-n";
}
push @commit_spec, '--root', $hash;
}
open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
'--encoding=utf8', '--stdout', @commit_spec
or die_error(500, "Open git-format-patch failed");
} else {
die_error(400, "Unknown commitdiff format");
}
# non-textual hash id's can be cached
my $expires;
if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
$expires = "+1d";
}
# write commit message
if ($format eq 'html') {
my $refs = git_get_references();
my $ref = format_ref_marker($refs, $co{'id'});
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);
print "\n";
if (@{$co{'comment'}} > 1) {
print "
\n";
git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
print "
\n"; # class="log"
}
} elsif ($format eq 'plain') {
my $refs = git_get_references("tags");
my $tagname = git_get_rev_name_tags($hash);
my $filename = basename($project) . "-$hash.patch";
print $cgi->header(
-type => 'text/plain',
-charset => 'utf-8',
-expires => $expires,
-content_disposition => 'inline; filename="' . "$filename" . '"');
my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
print "From: " . to_utf8($co{'author'}) . "\n";
print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
print "Subject: " . to_utf8($co{'title'}) . "\n";
print "X-Git-Tag: $tagname\n" if $tagname;
print "X-Git-Url: " . $cgi->self_url() . "\n\n";
foreach my $line (@{$co{'comment'}}) {
print to_utf8($line) . "\n";
}
print "---\n\n";
} elsif ($format eq 'patch') {
my $filename = basename($project) . "-$hash.patch";
print $cgi->header(
-type => 'text/plain',
-charset => 'utf-8',
-expires => $expires,
-content_disposition => 'inline; filename="' . "$filename" . '"');
}
# write patch
if ($format eq 'html') {
my $use_parents = !defined $hash_parent ||
$hash_parent eq '-c' || $hash_parent eq '--cc';
git_difftree_body(\@difftree, $hash,
$use_parents ? @{$co{'parents'}} : $hash_parent);
print "
\n";
git_patchset_body($fd, $diff_style,
\@difftree, $hash,
$use_parents ? @{$co{'parents'}} : $hash_parent);
close $fd;
print "
\n"; # class="page_body"
git_footer_html();
} elsif ($format eq 'plain') {
local $/ = undef;
print <$fd>;
close $fd
or print "Reading git-diff-tree failed\n";
} elsif ($format eq 'patch') {
local $/ = undef;
print <$fd>;
close $fd
or print "Reading git-format-patch failed\n";
}
}
sub git_commitdiff_plain {
git_commitdiff(-format => 'plain');
}
# format-patch-style patches
sub git_patch {
git_commitdiff(-format => 'patch', -single => 1);
}
sub git_patches {
git_commitdiff(-format => 'patch');
}
sub git_history {
git_log_generic('history', \&git_history_body,
$hash_base, $hash_parent_base,
$file_name, $hash);
}
sub git_search {
$searchtype ||= 'commit';
# check if appropriate features are enabled
gitweb_check_feature('search')
or die_error(403, "Search is disabled");
if ($searchtype eq 'pickaxe') {
# pickaxe may take all resources of your box and run for several minutes
# with every query - so decide by yourself how public you make this feature
gitweb_check_feature('pickaxe')
or die_error(403, "Pickaxe search is disabled");
}
if ($searchtype eq 'grep') {
# grep search might be potentially CPU-intensive, too
gitweb_check_feature('grep')
or die_error(403, "Grep search is disabled");
}
if (!defined $searchtext) {
die_error(400, "Text field is empty");
}
if (!defined $hash) {
$hash = git_get_head_hash($project);
}
my %co = parse_commit($hash);
if (!%co) {
die_error(404, "Unknown commit object");
}
if (!defined $page) {
$page = 0;
}
if ($searchtype eq 'commit' ||
$searchtype eq 'author' ||
$searchtype eq 'committer') {
git_search_message(%co);
} elsif ($searchtype eq 'pickaxe') {
git_search_changes(%co);
} elsif ($searchtype eq 'grep') {
git_search_files(%co);
} e