1#!/usr/bin/perl
2
3# gitweb - simple web interface to track changes in git repositories
4#
5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6# (C) 2005, Christian Gierke
7#
8# This program is licensed under the GPLv2
9
10use strict;
11use warnings;
12use CGI qw(:standard :escapeHTML -nosticky);
13use CGI::Util qw(unescape);
14use CGI::Carp qw(fatalsToBrowser);
15use Encode;
16use Fcntl ':mode';
17use File::Find qw();
18use File::Basename qw(basename);
19binmode STDOUT, ':utf8';
20
21our $cgi = new CGI;
22our $version = "++GIT_VERSION++";
23our $my_url = $cgi->url();
24our $my_uri = $cgi->url(-absolute => 1);
25
26# core git executable to use
27# this can just be "git" if your webserver has a sensible PATH
28our $GIT = "++GIT_BINDIR++/git";
29
30# absolute fs-path which will be prepended to the project path
31#our $projectroot = "/pub/scm";
32our $projectroot = "++GITWEB_PROJECTROOT++";
33
34# target of the home link on top of all pages
35our $home_link = $my_uri || "/";
36
37# string of the home link on top of all pages
38our $home_link_str = "++GITWEB_HOME_LINK_STR++";
39
40# name of your site or organization to appear in page titles
41# replace this with something more descriptive for clearer bookmarks
42our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
43
44# filename of html text to include at top of each page
45our $site_header = "++GITWEB_SITE_HEADER++";
46# html text to include at home page
47our $home_text = "++GITWEB_HOMETEXT++";
48# filename of html text to include at bottom of each page
49our $site_footer = "++GITWEB_SITE_FOOTER++";
50
51# URI of stylesheets
52our @stylesheets = ("++GITWEB_CSS++");
53our $stylesheet;
54# default is not to define style sheet, but it can be overwritten later
55undef $stylesheet;
56
57# URI of GIT logo
58our $logo = "++GITWEB_LOGO++";
59# URI of GIT favicon, assumed to be image/png type
60our $favicon = "++GITWEB_FAVICON++";
61
62our $githelp_url = "http://git.or.cz/";
63our $githelp_label = "git homepage";
64
65# source of projects list
66our $projects_list = "++GITWEB_LIST++";
67
68# show repository only if this file exists
69# (only effective if this variable evaluates to true)
70our $export_ok = "++GITWEB_EXPORT_OK++";
71
72# only allow viewing of repositories also shown on the overview page
73our $strict_export = "++GITWEB_STRICT_EXPORT++";
74
75# list of git base URLs used for URL to where fetch project from,
76# i.e. full URL is "$git_base_url/$project"
77our @git_base_url_list = ("++GITWEB_BASE_URL++");
78
79# default blob_plain mimetype and default charset for text/plain blob
80our $default_blob_plain_mimetype = 'text/plain';
81our $default_text_plain_charset = undef;
82
83# file to use for guessing MIME types before trying /etc/mime.types
84# (relative to the current git repository)
85our $mimetypes_file = undef;
86
87# You define site-wide feature defaults here; override them with
88# $GITWEB_CONFIG as necessary.
89our %feature = (
90 # feature => {
91 # 'sub' => feature-sub (subroutine),
92 # 'override' => allow-override (boolean),
93 # 'default' => [ default options...] (array reference)}
94 #
95 # if feature is overridable (it means that allow-override has true value,
96 # then feature-sub will be called with default options as parameters;
97 # return value of feature-sub indicates if to enable specified feature
98 #
99 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
100
101 'blame' => {
102 'sub' => \&feature_blame,
103 'override' => 0,
104 'default' => [0]},
105
106 'snapshot' => {
107 'sub' => \&feature_snapshot,
108 'override' => 0,
109 # => [content-encoding, suffix, program]
110 'default' => ['x-gzip', 'gz', 'gzip']},
111
112 'pickaxe' => {
113 'sub' => \&feature_pickaxe,
114 'override' => 0,
115 'default' => [1]},
116);
117
118sub gitweb_check_feature {
119 my ($name) = @_;
120 return unless exists $feature{$name};
121 my ($sub, $override, @defaults) = (
122 $feature{$name}{'sub'},
123 $feature{$name}{'override'},
124 @{$feature{$name}{'default'}});
125 if (!$override) { return @defaults; }
126 return $sub->(@defaults);
127}
128
129# To enable system wide have in $GITWEB_CONFIG
130# $feature{'blame'}{'default'} = [1];
131# To have project specific config enable override in $GITWEB_CONFIG
132# $feature{'blame'}{'override'} = 1;
133# and in project config gitweb.blame = 0|1;
134
135sub feature_blame {
136 my ($val) = git_get_project_config('blame', '--bool');
137
138 if ($val eq 'true') {
139 return 1;
140 } elsif ($val eq 'false') {
141 return 0;
142 }
143
144 return $_[0];
145}
146
147# To disable system wide have in $GITWEB_CONFIG
148# $feature{'snapshot'}{'default'} = [undef];
149# To have project specific config enable override in $GITWEB_CONFIG
150# $feature{'blame'}{'override'} = 1;
151# and in project config gitweb.snapshot = none|gzip|bzip2
152
153sub feature_snapshot {
154 my ($ctype, $suffix, $command) = @_;
155
156 my ($val) = git_get_project_config('snapshot');
157
158 if ($val eq 'gzip') {
159 return ('x-gzip', 'gz', 'gzip');
160 } elsif ($val eq 'bzip2') {
161 return ('x-bzip2', 'bz2', 'bzip2');
162 } elsif ($val eq 'none') {
163 return ();
164 }
165
166 return ($ctype, $suffix, $command);
167}
168
169sub gitweb_have_snapshot {
170 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
171 my $have_snapshot = (defined $ctype && defined $suffix);
172
173 return $have_snapshot;
174}
175
176# To enable system wide have in $GITWEB_CONFIG
177# $feature{'pickaxe'}{'default'} = [1];
178# To have project specific config enable override in $GITWEB_CONFIG
179# $feature{'pickaxe'}{'override'} = 1;
180# and in project config gitweb.pickaxe = 0|1;
181
182sub feature_pickaxe {
183 my ($val) = git_get_project_config('pickaxe', '--bool');
184
185 if ($val eq 'true') {
186 return (1);
187 } elsif ($val eq 'false') {
188 return (0);
189 }
190
191 return ($_[0]);
192}
193
194# checking HEAD file with -e is fragile if the repository was
195# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
196# and then pruned.
197sub check_head_link {
198 my ($dir) = @_;
199 my $headfile = "$dir/HEAD";
200 return ((-e $headfile) ||
201 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
202}
203
204sub check_export_ok {
205 my ($dir) = @_;
206 return (check_head_link($dir) &&
207 (!$export_ok || -e "$dir/$export_ok"));
208}
209
210# rename detection options for git-diff and git-diff-tree
211# - default is '-M', with the cost proportional to
212# (number of removed files) * (number of new files).
213# - more costly is '-C' (or '-C', '-M'), with the cost proportional to
214# (number of changed files + number of removed files) * (number of new files)
215# - even more costly is '-C', '--find-copies-harder' with cost
216# (number of files in the original tree) * (number of new files)
217# - one might want to include '-B' option, e.g. '-B', '-M'
218our @diff_opts = ('-M'); # taken from git_commit
219
220our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
221do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
222
223# version of the core git binary
224our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
225
226$projects_list ||= $projectroot;
227
228# ======================================================================
229# input validation and dispatch
230our $action = $cgi->param('a');
231if (defined $action) {
232 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
233 die_error(undef, "Invalid action parameter");
234 }
235}
236
237# parameters which are pathnames
238our $project = $cgi->param('p');
239if (defined $project) {
240 if (!validate_pathname($project) ||
241 !(-d "$projectroot/$project") ||
242 !check_head_link("$projectroot/$project") ||
243 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
244 ($strict_export && !project_in_list($project))) {
245 undef $project;
246 die_error(undef, "No such project");
247 }
248}
249
250our $file_name = $cgi->param('f');
251if (defined $file_name) {
252 if (!validate_pathname($file_name)) {
253 die_error(undef, "Invalid file parameter");
254 }
255}
256
257our $file_parent = $cgi->param('fp');
258if (defined $file_parent) {
259 if (!validate_pathname($file_parent)) {
260 die_error(undef, "Invalid file parent parameter");
261 }
262}
263
264# parameters which are refnames
265our $hash = $cgi->param('h');
266if (defined $hash) {
267 if (!validate_refname($hash)) {
268 die_error(undef, "Invalid hash parameter");
269 }
270}
271
272our $hash_parent = $cgi->param('hp');
273if (defined $hash_parent) {
274 if (!validate_refname($hash_parent)) {
275 die_error(undef, "Invalid hash parent parameter");
276 }
277}
278
279our $hash_base = $cgi->param('hb');
280if (defined $hash_base) {
281 if (!validate_refname($hash_base)) {
282 die_error(undef, "Invalid hash base parameter");
283 }
284}
285
286our $hash_parent_base = $cgi->param('hpb');
287if (defined $hash_parent_base) {
288 if (!validate_refname($hash_parent_base)) {
289 die_error(undef, "Invalid hash parent base parameter");
290 }
291}
292
293# other parameters
294our $page = $cgi->param('pg');
295if (defined $page) {
296 if ($page =~ m/[^0-9]/) {
297 die_error(undef, "Invalid page parameter");
298 }
299}
300
301our $searchtext = $cgi->param('s');
302if (defined $searchtext) {
303 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
304 die_error(undef, "Invalid search parameter");
305 }
306 $searchtext = quotemeta $searchtext;
307}
308
309# now read PATH_INFO and use it as alternative to parameters
310sub evaluate_path_info {
311 return if defined $project;
312 my $path_info = $ENV{"PATH_INFO"};
313 return if !$path_info;
314 $path_info =~ s,^/+,,;
315 return if !$path_info;
316 # find which part of PATH_INFO is project
317 $project = $path_info;
318 $project =~ s,/+$,,;
319 while ($project && !check_head_link("$projectroot/$project")) {
320 $project =~ s,/*[^/]*$,,;
321 }
322 # validate project
323 $project = validate_pathname($project);
324 if (!$project ||
325 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
326 ($strict_export && !project_in_list($project))) {
327 undef $project;
328 return;
329 }
330 # do not change any parameters if an action is given using the query string
331 return if $action;
332 $path_info =~ s,^$project/*,,;
333 my ($refname, $pathname) = split(/:/, $path_info, 2);
334 if (defined $pathname) {
335 # we got "project.git/branch:filename" or "project.git/branch:dir/"
336 # we could use git_get_type(branch:pathname), but it needs $git_dir
337 $pathname =~ s,^/+,,;
338 if (!$pathname || substr($pathname, -1) eq "/") {
339 $action ||= "tree";
340 $pathname =~ s,/$,,;
341 } else {
342 $action ||= "blob_plain";
343 }
344 $hash_base ||= validate_refname($refname);
345 $file_name ||= validate_pathname($pathname);
346 } elsif (defined $refname) {
347 # we got "project.git/branch"
348 $action ||= "shortlog";
349 $hash ||= validate_refname($refname);
350 }
351}
352evaluate_path_info();
353
354# path to the current git repository
355our $git_dir;
356$git_dir = "$projectroot/$project" if $project;
357
358# dispatch
359my %actions = (
360 "blame" => \&git_blame2,
361 "blobdiff" => \&git_blobdiff,
362 "blobdiff_plain" => \&git_blobdiff_plain,
363 "blob" => \&git_blob,
364 "blob_plain" => \&git_blob_plain,
365 "commitdiff" => \&git_commitdiff,
366 "commitdiff_plain" => \&git_commitdiff_plain,
367 "commit" => \&git_commit,
368 "heads" => \&git_heads,
369 "history" => \&git_history,
370 "log" => \&git_log,
371 "rss" => \&git_rss,
372 "search" => \&git_search,
373 "shortlog" => \&git_shortlog,
374 "summary" => \&git_summary,
375 "tag" => \&git_tag,
376 "tags" => \&git_tags,
377 "tree" => \&git_tree,
378 "snapshot" => \&git_snapshot,
379 # those below don't need $project
380 "opml" => \&git_opml,
381 "project_list" => \&git_project_list,
382 "project_index" => \&git_project_index,
383);
384
385if (defined $project) {
386 $action ||= 'summary';
387} else {
388 $action ||= 'project_list';
389}
390if (!defined($actions{$action})) {
391 die_error(undef, "Unknown action");
392}
393if ($action !~ m/^(opml|project_list|project_index)$/ &&
394 !$project) {
395 die_error(undef, "Project needed");
396}
397$actions{$action}->();
398exit;
399
400## ======================================================================
401## action links
402
403sub href(%) {
404 my %params = @_;
405
406 my @mapping = (
407 project => "p",
408 action => "a",
409 file_name => "f",
410 file_parent => "fp",
411 hash => "h",
412 hash_parent => "hp",
413 hash_base => "hb",
414 hash_parent_base => "hpb",
415 page => "pg",
416 order => "o",
417 searchtext => "s",
418 );
419 my %mapping = @mapping;
420
421 $params{'project'} = $project unless exists $params{'project'};
422
423 my @result = ();
424 for (my $i = 0; $i < @mapping; $i += 2) {
425 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
426 if (defined $params{$name}) {
427 push @result, $symbol . "=" . esc_param($params{$name});
428 }
429 }
430 return "$my_uri?" . join(';', @result);
431}
432
433
434## ======================================================================
435## validation, quoting/unquoting and escaping
436
437sub validate_pathname {
438 my $input = shift || return undef;
439
440 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
441 # at the beginning, at the end, and between slashes.
442 # also this catches doubled slashes
443 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
444 return undef;
445 }
446 # no null characters
447 if ($input =~ m!\0!) {
448 return undef;
449 }
450 return $input;
451}
452
453sub validate_refname {
454 my $input = shift || return undef;
455
456 # textual hashes are O.K.
457 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
458 return $input;
459 }
460 # it must be correct pathname
461 $input = validate_pathname($input)
462 or return undef;
463 # restrictions on ref name according to git-check-ref-format
464 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
465 return undef;
466 }
467 return $input;
468}
469
470# quote unsafe chars, but keep the slash, even when it's not
471# correct, but quoted slashes look too horrible in bookmarks
472sub esc_param {
473 my $str = shift;
474 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
475 $str =~ s/\+/%2B/g;
476 $str =~ s/ /\+/g;
477 return $str;
478}
479
480# quote unsafe chars in whole URL, so some charactrs cannot be quoted
481sub esc_url {
482 my $str = shift;
483 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
484 $str =~ s/\+/%2B/g;
485 $str =~ s/ /\+/g;
486 return $str;
487}
488
489# replace invalid utf8 character with SUBSTITUTION sequence
490sub esc_html {
491 my $str = shift;
492 $str = decode("utf8", $str, Encode::FB_DEFAULT);
493 $str = escapeHTML($str);
494 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
495 $str =~ s/\033/^[/g; # "escape" ESCAPE (\e) character (e.g. commit 20a3847d8a5032ce41f90dcc68abfb36e6fee9b1)
496 return $str;
497}
498
499# git may return quoted and escaped filenames
500sub unquote {
501 my $str = shift;
502 if ($str =~ m/^"(.*)"$/) {
503 $str = $1;
504 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
505 }
506 return $str;
507}
508
509# escape tabs (convert tabs to spaces)
510sub untabify {
511 my $line = shift;
512
513 while ((my $pos = index($line, "\t")) != -1) {
514 if (my $count = (8 - ($pos % 8))) {
515 my $spaces = ' ' x $count;
516 $line =~ s/\t/$spaces/;
517 }
518 }
519
520 return $line;
521}
522
523sub project_in_list {
524 my $project = shift;
525 my @list = git_get_projects_list();
526 return @list && scalar(grep { $_->{'path'} eq $project } @list);
527}
528
529## ----------------------------------------------------------------------
530## HTML aware string manipulation
531
532sub chop_str {
533 my $str = shift;
534 my $len = shift;
535 my $add_len = shift || 10;
536
537 # allow only $len chars, but don't cut a word if it would fit in $add_len
538 # if it doesn't fit, cut it if it's still longer than the dots we would add
539 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
540 my $body = $1;
541 my $tail = $2;
542 if (length($tail) > 4) {
543 $tail = " ...";
544 $body =~ s/&[^;]*$//; # remove chopped character entities
545 }
546 return "$body$tail";
547}
548
549## ----------------------------------------------------------------------
550## functions returning short strings
551
552# CSS class for given age value (in seconds)
553sub age_class {
554 my $age = shift;
555
556 if ($age < 60*60*2) {
557 return "age0";
558 } elsif ($age < 60*60*24*2) {
559 return "age1";
560 } else {
561 return "age2";
562 }
563}
564
565# convert age in seconds to "nn units ago" string
566sub age_string {
567 my $age = shift;
568 my $age_str;
569
570 if ($age > 60*60*24*365*2) {
571 $age_str = (int $age/60/60/24/365);
572 $age_str .= " years ago";
573 } elsif ($age > 60*60*24*(365/12)*2) {
574 $age_str = int $age/60/60/24/(365/12);
575 $age_str .= " months ago";
576 } elsif ($age > 60*60*24*7*2) {
577 $age_str = int $age/60/60/24/7;
578 $age_str .= " weeks ago";
579 } elsif ($age > 60*60*24*2) {
580 $age_str = int $age/60/60/24;
581 $age_str .= " days ago";
582 } elsif ($age > 60*60*2) {
583 $age_str = int $age/60/60;
584 $age_str .= " hours ago";
585 } elsif ($age > 60*2) {
586 $age_str = int $age/60;
587 $age_str .= " min ago";
588 } elsif ($age > 2) {
589 $age_str = int $age;
590 $age_str .= " sec ago";
591 } else {
592 $age_str .= " right now";
593 }
594 return $age_str;
595}
596
597# convert file mode in octal to symbolic file mode string
598sub mode_str {
599 my $mode = oct shift;
600
601 if (S_ISDIR($mode & S_IFMT)) {
602 return 'drwxr-xr-x';
603 } elsif (S_ISLNK($mode)) {
604 return 'lrwxrwxrwx';
605 } elsif (S_ISREG($mode)) {
606 # git cares only about the executable bit
607 if ($mode & S_IXUSR) {
608 return '-rwxr-xr-x';
609 } else {
610 return '-rw-r--r--';
611 };
612 } else {
613 return '----------';
614 }
615}
616
617# convert file mode in octal to file type string
618sub file_type {
619 my $mode = shift;
620
621 if ($mode !~ m/^[0-7]+$/) {
622 return $mode;
623 } else {
624 $mode = oct $mode;
625 }
626
627 if (S_ISDIR($mode & S_IFMT)) {
628 return "directory";
629 } elsif (S_ISLNK($mode)) {
630 return "symlink";
631 } elsif (S_ISREG($mode)) {
632 return "file";
633 } else {
634 return "unknown";
635 }
636}
637
638## ----------------------------------------------------------------------
639## functions returning short HTML fragments, or transforming HTML fragments
640## which don't beling to other sections
641
642# format line of commit message or tag comment
643sub format_log_line_html {
644 my $line = shift;
645
646 $line = esc_html($line);
647 $line =~ s/ / /g;
648 if ($line =~ m/([0-9a-fA-F]{40})/) {
649 my $hash_text = $1;
650 if (git_get_type($hash_text) eq "commit") {
651 my $link =
652 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
653 -class => "text"}, $hash_text);
654 $line =~ s/$hash_text/$link/;
655 }
656 }
657 return $line;
658}
659
660# format marker of refs pointing to given object
661sub format_ref_marker {
662 my ($refs, $id) = @_;
663 my $markers = '';
664
665 if (defined $refs->{$id}) {
666 foreach my $ref (@{$refs->{$id}}) {
667 my ($type, $name) = qw();
668 # e.g. tags/v2.6.11 or heads/next
669 if ($ref =~ m!^(.*?)s?/(.*)$!) {
670 $type = $1;
671 $name = $2;
672 } else {
673 $type = "ref";
674 $name = $ref;
675 }
676
677 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
678 }
679 }
680
681 if ($markers) {
682 return ' <span class="refs">'. $markers . '</span>';
683 } else {
684 return "";
685 }
686}
687
688# format, perhaps shortened and with markers, title line
689sub format_subject_html {
690 my ($long, $short, $href, $extra) = @_;
691 $extra = '' unless defined($extra);
692
693 if (length($short) < length($long)) {
694 return $cgi->a({-href => $href, -class => "list subject",
695 -title => decode("utf8", $long, Encode::FB_DEFAULT)},
696 esc_html($short) . $extra);
697 } else {
698 return $cgi->a({-href => $href, -class => "list subject"},
699 esc_html($long) . $extra);
700 }
701}
702
703sub format_diff_line {
704 my $line = shift;
705 my $char = substr($line, 0, 1);
706 my $diff_class = "";
707
708 chomp $line;
709
710 if ($char eq '+') {
711 $diff_class = " add";
712 } elsif ($char eq "-") {
713 $diff_class = " rem";
714 } elsif ($char eq "@") {
715 $diff_class = " chunk_header";
716 } elsif ($char eq "\\") {
717 $diff_class = " incomplete";
718 }
719 $line = untabify($line);
720 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
721}
722
723## ----------------------------------------------------------------------
724## git utility subroutines, invoking git commands
725
726# returns path to the core git executable and the --git-dir parameter as list
727sub git_cmd {
728 return $GIT, '--git-dir='.$git_dir;
729}
730
731# returns path to the core git executable and the --git-dir parameter as string
732sub git_cmd_str {
733 return join(' ', git_cmd());
734}
735
736# get HEAD ref of given project as hash
737sub git_get_head_hash {
738 my $project = shift;
739 my $o_git_dir = $git_dir;
740 my $retval = undef;
741 $git_dir = "$projectroot/$project";
742 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
743 my $head = <$fd>;
744 close $fd;
745 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
746 $retval = $1;
747 }
748 }
749 if (defined $o_git_dir) {
750 $git_dir = $o_git_dir;
751 }
752 return $retval;
753}
754
755# get type of given object
756sub git_get_type {
757 my $hash = shift;
758
759 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
760 my $type = <$fd>;
761 close $fd or return;
762 chomp $type;
763 return $type;
764}
765
766sub git_get_project_config {
767 my ($key, $type) = @_;
768
769 return unless ($key);
770 $key =~ s/^gitweb\.//;
771 return if ($key =~ m/\W/);
772
773 my @x = (git_cmd(), 'repo-config');
774 if (defined $type) { push @x, $type; }
775 push @x, "--get";
776 push @x, "gitweb.$key";
777 my $val = qx(@x);
778 chomp $val;
779 return ($val);
780}
781
782# get hash of given path at given ref
783sub git_get_hash_by_path {
784 my $base = shift;
785 my $path = shift || return undef;
786 my $type = shift;
787
788 $path =~ s,/+$,,;
789
790 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
791 or die_error(undef, "Open git-ls-tree failed");
792 my $line = <$fd>;
793 close $fd or return undef;
794
795 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
796 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
797 if (defined $type && $type ne $2) {
798 # type doesn't match
799 return undef;
800 }
801 return $3;
802}
803
804## ......................................................................
805## git utility functions, directly accessing git repository
806
807sub git_get_project_description {
808 my $path = shift;
809
810 open my $fd, "$projectroot/$path/description" or return undef;
811 my $descr = <$fd>;
812 close $fd;
813 chomp $descr;
814 return $descr;
815}
816
817sub git_get_project_url_list {
818 my $path = shift;
819
820 open my $fd, "$projectroot/$path/cloneurl" or return;
821 my @git_project_url_list = map { chomp; $_ } <$fd>;
822 close $fd;
823
824 return wantarray ? @git_project_url_list : \@git_project_url_list;
825}
826
827sub git_get_projects_list {
828 my @list;
829
830 if (-d $projects_list) {
831 # search in directory
832 my $dir = $projects_list;
833 my $pfxlen = length("$dir");
834
835 File::Find::find({
836 follow_fast => 1, # follow symbolic links
837 dangling_symlinks => 0, # ignore dangling symlinks, silently
838 wanted => sub {
839 # skip project-list toplevel, if we get it.
840 return if (m!^[/.]$!);
841 # only directories can be git repositories
842 return unless (-d $_);
843
844 my $subdir = substr($File::Find::name, $pfxlen + 1);
845 # we check related file in $projectroot
846 if (check_export_ok("$projectroot/$subdir")) {
847 push @list, { path => $subdir };
848 $File::Find::prune = 1;
849 }
850 },
851 }, "$dir");
852
853 } elsif (-f $projects_list) {
854 # read from file(url-encoded):
855 # 'git%2Fgit.git Linus+Torvalds'
856 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
857 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
858 open my ($fd), $projects_list or return;
859 while (my $line = <$fd>) {
860 chomp $line;
861 my ($path, $owner) = split ' ', $line;
862 $path = unescape($path);
863 $owner = unescape($owner);
864 if (!defined $path) {
865 next;
866 }
867 if (check_export_ok("$projectroot/$path")) {
868 my $pr = {
869 path => $path,
870 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
871 };
872 push @list, $pr
873 }
874 }
875 close $fd;
876 }
877 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
878 return @list;
879}
880
881sub git_get_project_owner {
882 my $project = shift;
883 my $owner;
884
885 return undef unless $project;
886
887 # read from file (url-encoded):
888 # 'git%2Fgit.git Linus+Torvalds'
889 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
890 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
891 if (-f $projects_list) {
892 open (my $fd , $projects_list);
893 while (my $line = <$fd>) {
894 chomp $line;
895 my ($pr, $ow) = split ' ', $line;
896 $pr = unescape($pr);
897 $ow = unescape($ow);
898 if ($pr eq $project) {
899 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
900 last;
901 }
902 }
903 close $fd;
904 }
905 if (!defined $owner) {
906 $owner = get_file_owner("$projectroot/$project");
907 }
908
909 return $owner;
910}
911
912sub git_get_references {
913 my $type = shift || "";
914 my %refs;
915 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
916 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
917 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
918 or return;
919
920 while (my $line = <$fd>) {
921 chomp $line;
922 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
923 if (defined $refs{$1}) {
924 push @{$refs{$1}}, $2;
925 } else {
926 $refs{$1} = [ $2 ];
927 }
928 }
929 }
930 close $fd or return;
931 return \%refs;
932}
933
934sub git_get_rev_name_tags {
935 my $hash = shift || return undef;
936
937 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
938 or return;
939 my $name_rev = <$fd>;
940 close $fd;
941
942 if ($name_rev =~ m|^$hash tags/(.*)$|) {
943 return $1;
944 } else {
945 # catches also '$hash undefined' output
946 return undef;
947 }
948}
949
950## ----------------------------------------------------------------------
951## parse to hash functions
952
953sub parse_date {
954 my $epoch = shift;
955 my $tz = shift || "-0000";
956
957 my %date;
958 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
959 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
960 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
961 $date{'hour'} = $hour;
962 $date{'minute'} = $min;
963 $date{'mday'} = $mday;
964 $date{'day'} = $days[$wday];
965 $date{'month'} = $months[$mon];
966 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
967 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
968 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
969 $mday, $months[$mon], $hour ,$min;
970
971 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
972 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
973 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
974 $date{'hour_local'} = $hour;
975 $date{'minute_local'} = $min;
976 $date{'tz_local'} = $tz;
977 $date{'iso-tz'} = sprintf ("%04d-%02d-%02d %02d:%02d:%02d %s",
978 1900+$year, $mon+1, $mday,
979 $hour, $min, $sec, $tz);
980 return %date;
981}
982
983sub parse_tag {
984 my $tag_id = shift;
985 my %tag;
986 my @comment;
987
988 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
989 $tag{'id'} = $tag_id;
990 while (my $line = <$fd>) {
991 chomp $line;
992 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
993 $tag{'object'} = $1;
994 } elsif ($line =~ m/^type (.+)$/) {
995 $tag{'type'} = $1;
996 } elsif ($line =~ m/^tag (.+)$/) {
997 $tag{'name'} = $1;
998 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
999 $tag{'author'} = $1;
1000 $tag{'epoch'} = $2;
1001 $tag{'tz'} = $3;
1002 } elsif ($line =~ m/--BEGIN/) {
1003 push @comment, $line;
1004 last;
1005 } elsif ($line eq "") {
1006 last;
1007 }
1008 }
1009 push @comment, <$fd>;
1010 $tag{'comment'} = \@comment;
1011 close $fd or return;
1012 if (!defined $tag{'name'}) {
1013 return
1014 };
1015 return %tag
1016}
1017
1018sub parse_commit {
1019 my $commit_id = shift;
1020 my $commit_text = shift;
1021
1022 my @commit_lines;
1023 my %co;
1024
1025 if (defined $commit_text) {
1026 @commit_lines = @$commit_text;
1027 } else {
1028 $/ = "\0";
1029 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
1030 or return;
1031 @commit_lines = split '\n', <$fd>;
1032 close $fd or return;
1033 $/ = "\n";
1034 pop @commit_lines;
1035 }
1036 my $header = shift @commit_lines;
1037 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1038 return;
1039 }
1040 ($co{'id'}, my @parents) = split ' ', $header;
1041 $co{'parents'} = \@parents;
1042 $co{'parent'} = $parents[0];
1043 while (my $line = shift @commit_lines) {
1044 last if $line eq "\n";
1045 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1046 $co{'tree'} = $1;
1047 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1048 $co{'author'} = $1;
1049 $co{'author_epoch'} = $2;
1050 $co{'author_tz'} = $3;
1051 if ($co{'author'} =~ m/^([^<]+) </) {
1052 $co{'author_name'} = $1;
1053 } else {
1054 $co{'author_name'} = $co{'author'};
1055 }
1056 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1057 $co{'committer'} = $1;
1058 $co{'committer_epoch'} = $2;
1059 $co{'committer_tz'} = $3;
1060 $co{'committer_name'} = $co{'committer'};
1061 $co{'committer_name'} =~ s/ <.*//;
1062 }
1063 }
1064 if (!defined $co{'tree'}) {
1065 return;
1066 };
1067
1068 foreach my $title (@commit_lines) {
1069 $title =~ s/^ //;
1070 if ($title ne "") {
1071 $co{'title'} = chop_str($title, 80, 5);
1072 # remove leading stuff of merges to make the interesting part visible
1073 if (length($title) > 50) {
1074 $title =~ s/^Automatic //;
1075 $title =~ s/^merge (of|with) /Merge ... /i;
1076 if (length($title) > 50) {
1077 $title =~ s/(http|rsync):\/\///;
1078 }
1079 if (length($title) > 50) {
1080 $title =~ s/(master|www|rsync)\.//;
1081 }
1082 if (length($title) > 50) {
1083 $title =~ s/kernel.org:?//;
1084 }
1085 if (length($title) > 50) {
1086 $title =~ s/\/pub\/scm//;
1087 }
1088 }
1089 $co{'title_short'} = chop_str($title, 50, 5);
1090 last;
1091 }
1092 }
1093 # remove added spaces
1094 foreach my $line (@commit_lines) {
1095 $line =~ s/^ //;
1096 }
1097 $co{'comment'} = \@commit_lines;
1098
1099 my $age = time - $co{'committer_epoch'};
1100 $co{'age'} = $age;
1101 $co{'age_string'} = age_string($age);
1102 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1103 if ($age > 60*60*24*7*2) {
1104 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1105 $co{'age_string_age'} = $co{'age_string'};
1106 } else {
1107 $co{'age_string_date'} = $co{'age_string'};
1108 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1109 }
1110 return %co;
1111}
1112
1113# parse ref from ref_file, given by ref_id, with given type
1114sub parse_ref {
1115 my $ref_file = shift;
1116 my $ref_id = shift;
1117 my $type = shift || git_get_type($ref_id);
1118 my %ref_item;
1119
1120 $ref_item{'type'} = $type;
1121 $ref_item{'id'} = $ref_id;
1122 $ref_item{'epoch'} = 0;
1123 $ref_item{'age'} = "unknown";
1124 if ($type eq "tag") {
1125 my %tag = parse_tag($ref_id);
1126 $ref_item{'comment'} = $tag{'comment'};
1127 if ($tag{'type'} eq "commit") {
1128 my %co = parse_commit($tag{'object'});
1129 $ref_item{'epoch'} = $co{'committer_epoch'};
1130 $ref_item{'age'} = $co{'age_string'};
1131 } elsif (defined($tag{'epoch'})) {
1132 my $age = time - $tag{'epoch'};
1133 $ref_item{'epoch'} = $tag{'epoch'};
1134 $ref_item{'age'} = age_string($age);
1135 }
1136 $ref_item{'reftype'} = $tag{'type'};
1137 $ref_item{'name'} = $tag{'name'};
1138 $ref_item{'refid'} = $tag{'object'};
1139 } elsif ($type eq "commit"){
1140 my %co = parse_commit($ref_id);
1141 $ref_item{'reftype'} = "commit";
1142 $ref_item{'name'} = $ref_file;
1143 $ref_item{'title'} = $co{'title'};
1144 $ref_item{'refid'} = $ref_id;
1145 $ref_item{'epoch'} = $co{'committer_epoch'};
1146 $ref_item{'age'} = $co{'age_string'};
1147 } else {
1148 $ref_item{'reftype'} = $type;
1149 $ref_item{'name'} = $ref_file;
1150 $ref_item{'refid'} = $ref_id;
1151 }
1152
1153 return %ref_item;
1154}
1155
1156# parse line of git-diff-tree "raw" output
1157sub parse_difftree_raw_line {
1158 my $line = shift;
1159 my %res;
1160
1161 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1162 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1163 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1164 $res{'from_mode'} = $1;
1165 $res{'to_mode'} = $2;
1166 $res{'from_id'} = $3;
1167 $res{'to_id'} = $4;
1168 $res{'status'} = $5;
1169 $res{'similarity'} = $6;
1170 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1171 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1172 } else {
1173 $res{'file'} = unquote($7);
1174 }
1175 }
1176 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1177 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1178 $res{'commit'} = $1;
1179 }
1180
1181 return wantarray ? %res : \%res;
1182}
1183
1184# parse line of git-ls-tree output
1185sub parse_ls_tree_line ($;%) {
1186 my $line = shift;
1187 my %opts = @_;
1188 my %res;
1189
1190 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1191 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1192
1193 $res{'mode'} = $1;
1194 $res{'type'} = $2;
1195 $res{'hash'} = $3;
1196 if ($opts{'-z'}) {
1197 $res{'name'} = $4;
1198 } else {
1199 $res{'name'} = unquote($4);
1200 }
1201
1202 return wantarray ? %res : \%res;
1203}
1204
1205## ......................................................................
1206## parse to array of hashes functions
1207
1208sub git_get_refs_list {
1209 my $type = shift || "";
1210 my %refs;
1211 my @reflist;
1212
1213 my @refs;
1214 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1215 or return;
1216 while (my $line = <$fd>) {
1217 chomp $line;
1218 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1219 if (defined $refs{$1}) {
1220 push @{$refs{$1}}, $2;
1221 } else {
1222 $refs{$1} = [ $2 ];
1223 }
1224
1225 if (! $4) { # unpeeled, direct reference
1226 push @refs, { hash => $1, name => $3 }; # without type
1227 } elsif ($3 eq $refs[-1]{'name'}) {
1228 # most likely a tag is followed by its peeled
1229 # (deref) one, and when that happens we know the
1230 # previous one was of type 'tag'.
1231 $refs[-1]{'type'} = "tag";
1232 }
1233 }
1234 }
1235 close $fd;
1236
1237 foreach my $ref (@refs) {
1238 my $ref_file = $ref->{'name'};
1239 my $ref_id = $ref->{'hash'};
1240
1241 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1242 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1243
1244 push @reflist, \%ref_item;
1245 }
1246 # sort refs by age
1247 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1248 return (\@reflist, \%refs);
1249}
1250
1251## ----------------------------------------------------------------------
1252## filesystem-related functions
1253
1254sub get_file_owner {
1255 my $path = shift;
1256
1257 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1258 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1259 if (!defined $gcos) {
1260 return undef;
1261 }
1262 my $owner = $gcos;
1263 $owner =~ s/[,;].*$//;
1264 return decode("utf8", $owner, Encode::FB_DEFAULT);
1265}
1266
1267## ......................................................................
1268## mimetype related functions
1269
1270sub mimetype_guess_file {
1271 my $filename = shift;
1272 my $mimemap = shift;
1273 -r $mimemap or return undef;
1274
1275 my %mimemap;
1276 open(MIME, $mimemap) or return undef;
1277 while (<MIME>) {
1278 next if m/^#/; # skip comments
1279 my ($mime, $exts) = split(/\t+/);
1280 if (defined $exts) {
1281 my @exts = split(/\s+/, $exts);
1282 foreach my $ext (@exts) {
1283 $mimemap{$ext} = $mime;
1284 }
1285 }
1286 }
1287 close(MIME);
1288
1289 $filename =~ /\.([^.]*)$/;
1290 return $mimemap{$1};
1291}
1292
1293sub mimetype_guess {
1294 my $filename = shift;
1295 my $mime;
1296 $filename =~ /\./ or return undef;
1297
1298 if ($mimetypes_file) {
1299 my $file = $mimetypes_file;
1300 if ($file !~ m!^/!) { # if it is relative path
1301 # it is relative to project
1302 $file = "$projectroot/$project/$file";
1303 }
1304 $mime = mimetype_guess_file($filename, $file);
1305 }
1306 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1307 return $mime;
1308}
1309
1310sub blob_mimetype {
1311 my $fd = shift;
1312 my $filename = shift;
1313
1314 if ($filename) {
1315 my $mime = mimetype_guess($filename);
1316 $mime and return $mime;
1317 }
1318
1319 # just in case
1320 return $default_blob_plain_mimetype unless $fd;
1321
1322 if (-T $fd) {
1323 return 'text/plain' .
1324 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1325 } elsif (! $filename) {
1326 return 'application/octet-stream';
1327 } elsif ($filename =~ m/\.png$/i) {
1328 return 'image/png';
1329 } elsif ($filename =~ m/\.gif$/i) {
1330 return 'image/gif';
1331 } elsif ($filename =~ m/\.jpe?g$/i) {
1332 return 'image/jpeg';
1333 } else {
1334 return 'application/octet-stream';
1335 }
1336}
1337
1338## ======================================================================
1339## functions printing HTML: header, footer, error page
1340
1341sub git_header_html {
1342 my $status = shift || "200 OK";
1343 my $expires = shift;
1344
1345 my $title = "$site_name git";
1346 if (defined $project) {
1347 $title .= " - $project";
1348 if (defined $action) {
1349 $title .= "/$action";
1350 if (defined $file_name) {
1351 $title .= " - " . esc_html($file_name);
1352 if ($action eq "tree" && $file_name !~ m|/$|) {
1353 $title .= "/";
1354 }
1355 }
1356 }
1357 }
1358 my $content_type;
1359 # require explicit support from the UA if we are to send the page as
1360 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1361 # we have to do this because MSIE sometimes globs '*/*', pretending to
1362 # support xhtml+xml but choking when it gets what it asked for.
1363 if (defined $cgi->http('HTTP_ACCEPT') &&
1364 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1365 $cgi->Accept('application/xhtml+xml') != 0) {
1366 $content_type = 'application/xhtml+xml';
1367 } else {
1368 $content_type = 'text/html';
1369 }
1370 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1371 -status=> $status, -expires => $expires);
1372 print <<EOF;
1373<?xml version="1.0" encoding="utf-8"?>
1374<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1375<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1376<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1377<!-- git core binaries version $git_version -->
1378<head>
1379<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1380<meta name="generator" content="gitweb/$version git/$git_version"/>
1381<meta name="robots" content="index, nofollow"/>
1382<title>$title</title>
1383EOF
1384# print out each stylesheet that exist
1385 if (defined $stylesheet) {
1386#provides backwards capability for those people who define style sheet in a config file
1387 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1388 } else {
1389 foreach my $stylesheet (@stylesheets) {
1390 next unless $stylesheet;
1391 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1392 }
1393 }
1394 if (defined $project) {
1395 printf('<link rel="alternate" title="%s log" '.
1396 'href="%s" type="application/rss+xml"/>'."\n",
1397 esc_param($project), href(action=>"rss"));
1398 } else {
1399 printf('<link rel="alternate" title="%s projects list" '.
1400 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1401 $site_name, href(project=>undef, action=>"project_index"));
1402 printf('<link rel="alternate" title="%s projects logs" '.
1403 'href="%s" type="text/x-opml"/>'."\n",
1404 $site_name, href(project=>undef, action=>"opml"));
1405 }
1406 if (defined $favicon) {
1407 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1408 }
1409
1410 print "</head>\n" .
1411 "<body>\n";
1412
1413 if (-f $site_header) {
1414 open (my $fd, $site_header);
1415 print <$fd>;
1416 close $fd;
1417 }
1418
1419 print "<div class=\"page_header\">\n" .
1420 "<a href=\"" . esc_html($githelp_url) .
1421 "\" title=\"" . esc_html($githelp_label) .
1422 "\">" .
1423 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1424 "</a>\n";
1425 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1426 if (defined $project) {
1427 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1428 if (defined $action) {
1429 print " / $action";
1430 }
1431 print "\n";
1432 if (!defined $searchtext) {
1433 $searchtext = "";
1434 }
1435 my $search_hash;
1436 if (defined $hash_base) {
1437 $search_hash = $hash_base;
1438 } elsif (defined $hash) {
1439 $search_hash = $hash;
1440 } else {
1441 $search_hash = "HEAD";
1442 }
1443 $cgi->param("a", "search");
1444 $cgi->param("h", $search_hash);
1445 print $cgi->startform(-method => "get", -action => $my_uri) .
1446 "<div class=\"search\">\n" .
1447 $cgi->hidden(-name => "p") . "\n" .
1448 $cgi->hidden(-name => "a") . "\n" .
1449 $cgi->hidden(-name => "h") . "\n" .
1450 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1451 "</div>" .
1452 $cgi->end_form() . "\n";
1453 }
1454 print "</div>\n";
1455}
1456
1457sub git_footer_html {
1458 print "<div class=\"page_footer\">\n";
1459 if (defined $project) {
1460 my $descr = git_get_project_description($project);
1461 if (defined $descr) {
1462 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1463 }
1464 print $cgi->a({-href => href(action=>"rss"),
1465 -class => "rss_logo"}, "RSS") . "\n";
1466 } else {
1467 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1468 -class => "rss_logo"}, "OPML") . " ";
1469 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1470 -class => "rss_logo"}, "TXT") . "\n";
1471 }
1472 print "</div>\n" ;
1473
1474 if (-f $site_footer) {
1475 open (my $fd, $site_footer);
1476 print <$fd>;
1477 close $fd;
1478 }
1479
1480 print "</body>\n" .
1481 "</html>";
1482}
1483
1484sub die_error {
1485 my $status = shift || "403 Forbidden";
1486 my $error = shift || "Malformed query, file missing or permission denied";
1487
1488 git_header_html($status);
1489 print <<EOF;
1490<div class="page_body">
1491<br /><br />
1492$status - $error
1493<br />
1494</div>
1495EOF
1496 git_footer_html();
1497 exit;
1498}
1499
1500## ----------------------------------------------------------------------
1501## functions printing or outputting HTML: navigation
1502
1503sub git_print_page_nav {
1504 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1505 $extra = '' if !defined $extra; # pager or formats
1506
1507 my @navs = qw(summary shortlog log commit commitdiff tree);
1508 if ($suppress) {
1509 @navs = grep { $_ ne $suppress } @navs;
1510 }
1511
1512 my %arg = map { $_ => {action=>$_} } @navs;
1513 if (defined $head) {
1514 for (qw(commit commitdiff)) {
1515 $arg{$_}{hash} = $head;
1516 }
1517 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1518 for (qw(shortlog log)) {
1519 $arg{$_}{hash} = $head;
1520 }
1521 }
1522 }
1523 $arg{tree}{hash} = $treehead if defined $treehead;
1524 $arg{tree}{hash_base} = $treebase if defined $treebase;
1525
1526 print "<div class=\"page_nav\">\n" .
1527 (join " | ",
1528 map { $_ eq $current ?
1529 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1530 } @navs);
1531 print "<br/>\n$extra<br/>\n" .
1532 "</div>\n";
1533}
1534
1535sub format_paging_nav {
1536 my ($action, $hash, $head, $page, $nrevs) = @_;
1537 my $paging_nav;
1538
1539
1540 if ($hash ne $head || $page) {
1541 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1542 } else {
1543 $paging_nav .= "HEAD";
1544 }
1545
1546 if ($page > 0) {
1547 $paging_nav .= " ⋅ " .
1548 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1549 -accesskey => "p", -title => "Alt-p"}, "prev");
1550 } else {
1551 $paging_nav .= " ⋅ prev";
1552 }
1553
1554 if ($nrevs >= (100 * ($page+1)-1)) {
1555 $paging_nav .= " ⋅ " .
1556 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1557 -accesskey => "n", -title => "Alt-n"}, "next");
1558 } else {
1559 $paging_nav .= " ⋅ next";
1560 }
1561
1562 return $paging_nav;
1563}
1564
1565## ......................................................................
1566## functions printing or outputting HTML: div
1567
1568sub git_print_header_div {
1569 my ($action, $title, $hash, $hash_base) = @_;
1570 my %args = ();
1571
1572 $args{action} = $action;
1573 $args{hash} = $hash if $hash;
1574 $args{hash_base} = $hash_base if $hash_base;
1575
1576 print "<div class=\"header\">\n" .
1577 $cgi->a({-href => href(%args), -class => "title"},
1578 $title ? $title : $action) .
1579 "\n</div>\n";
1580}
1581
1582#sub git_print_authorship (\%) {
1583sub git_print_authorship {
1584 my $co = shift;
1585
1586 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1587 print "<div class=\"author_date\">" .
1588 esc_html($co->{'author_name'}) .
1589 " [$ad{'rfc2822'}";
1590 if ($ad{'hour_local'} < 6) {
1591 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1592 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1593 } else {
1594 printf(" (%02d:%02d %s)",
1595 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1596 }
1597 print "]</div>\n";
1598}
1599
1600sub git_print_page_path {
1601 my $name = shift;
1602 my $type = shift;
1603 my $hb = shift;
1604
1605 if (!defined $name) {
1606 print "<div class=\"page_path\">/</div>\n";
1607 } else {
1608 my @dirname = split '/', $name;
1609 my $basename = pop @dirname;
1610 my $fullname = '';
1611
1612 print "<div class=\"page_path\">";
1613 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1614 -title => 'tree root'}, "[$project]");
1615 print " / ";
1616 foreach my $dir (@dirname) {
1617 $fullname .= ($fullname ? '/' : '') . $dir;
1618 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1619 hash_base=>$hb),
1620 -title => $fullname}, esc_html($dir));
1621 print " / ";
1622 }
1623 if (defined $type && $type eq 'blob') {
1624 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1625 hash_base=>$hb),
1626 -title => $name}, esc_html($basename));
1627 } elsif (defined $type && $type eq 'tree') {
1628 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1629 hash_base=>$hb),
1630 -title => $name}, esc_html($basename));
1631 } else {
1632 print esc_html($basename);
1633 }
1634 print "<br/></div>\n";
1635 }
1636}
1637
1638# sub git_print_log (\@;%) {
1639sub git_print_log ($;%) {
1640 my $log = shift;
1641 my %opts = @_;
1642
1643 if ($opts{'-remove_title'}) {
1644 # remove title, i.e. first line of log
1645 shift @$log;
1646 }
1647 # remove leading empty lines
1648 while (defined $log->[0] && $log->[0] eq "") {
1649 shift @$log;
1650 }
1651
1652 # print log
1653 my $signoff = 0;
1654 my $empty = 0;
1655 foreach my $line (@$log) {
1656 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1657 $signoff = 1;
1658 $empty = 0;
1659 if (! $opts{'-remove_signoff'}) {
1660 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1661 next;
1662 } else {
1663 # remove signoff lines
1664 next;
1665 }
1666 } else {
1667 $signoff = 0;
1668 }
1669
1670 # print only one empty line
1671 # do not print empty line after signoff
1672 if ($line eq "") {
1673 next if ($empty || $signoff);
1674 $empty = 1;
1675 } else {
1676 $empty = 0;
1677 }
1678
1679 print format_log_line_html($line) . "<br/>\n";
1680 }
1681
1682 if ($opts{'-final_empty_line'}) {
1683 # end with single empty line
1684 print "<br/>\n" unless $empty;
1685 }
1686}
1687
1688sub git_print_simplified_log {
1689 my $log = shift;
1690 my $remove_title = shift;
1691
1692 git_print_log($log,
1693 -final_empty_line=> 1,
1694 -remove_title => $remove_title);
1695}
1696
1697# print tree entry (row of git_tree), but without encompassing <tr> element
1698sub git_print_tree_entry {
1699 my ($t, $basedir, $hash_base, $have_blame) = @_;
1700
1701 my %base_key = ();
1702 $base_key{hash_base} = $hash_base if defined $hash_base;
1703
1704 # The format of a table row is: mode list link. Where mode is
1705 # the mode of the entry, list is the name of the entry, an href,
1706 # and link is the action links of the entry.
1707
1708 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1709 if ($t->{'type'} eq "blob") {
1710 print "<td class=\"list\">" .
1711 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1712 file_name=>"$basedir$t->{'name'}", %base_key),
1713 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1714 print "<td class=\"link\">";
1715 if ($have_blame) {
1716 print $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1717 file_name=>"$basedir$t->{'name'}", %base_key)},
1718 "blame");
1719 }
1720 if (defined $hash_base) {
1721 if ($have_blame) {
1722 print " | ";
1723 }
1724 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1725 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1726 "history");
1727 }
1728 print " | " .
1729 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
1730 file_name=>"$basedir$t->{'name'}")},
1731 "raw");
1732 print "</td>\n";
1733
1734 } elsif ($t->{'type'} eq "tree") {
1735 print "<td class=\"list\">";
1736 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1737 file_name=>"$basedir$t->{'name'}", %base_key)},
1738 esc_html($t->{'name'}));
1739 print "</td>\n";
1740 print "<td class=\"link\">";
1741 if (defined $hash_base) {
1742 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1743 file_name=>"$basedir$t->{'name'}")},
1744 "history");
1745 }
1746 print "</td>\n";
1747 }
1748}
1749
1750## ......................................................................
1751## functions printing large fragments of HTML
1752
1753sub git_difftree_body {
1754 my ($difftree, $hash, $parent) = @_;
1755
1756 print "<div class=\"list_head\">\n";
1757 if ($#{$difftree} > 10) {
1758 print(($#{$difftree} + 1) . " files changed:\n");
1759 }
1760 print "</div>\n";
1761
1762 print "<table class=\"diff_tree\">\n";
1763 my $alternate = 1;
1764 my $patchno = 0;
1765 foreach my $line (@{$difftree}) {
1766 my %diff = parse_difftree_raw_line($line);
1767
1768 if ($alternate) {
1769 print "<tr class=\"dark\">\n";
1770 } else {
1771 print "<tr class=\"light\">\n";
1772 }
1773 $alternate ^= 1;
1774
1775 my ($to_mode_oct, $to_mode_str, $to_file_type);
1776 my ($from_mode_oct, $from_mode_str, $from_file_type);
1777 if ($diff{'to_mode'} ne ('0' x 6)) {
1778 $to_mode_oct = oct $diff{'to_mode'};
1779 if (S_ISREG($to_mode_oct)) { # only for regular file
1780 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1781 }
1782 $to_file_type = file_type($diff{'to_mode'});
1783 }
1784 if ($diff{'from_mode'} ne ('0' x 6)) {
1785 $from_mode_oct = oct $diff{'from_mode'};
1786 if (S_ISREG($to_mode_oct)) { # only for regular file
1787 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1788 }
1789 $from_file_type = file_type($diff{'from_mode'});
1790 }
1791
1792 if ($diff{'status'} eq "A") { # created
1793 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1794 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1795 $mode_chng .= "]</span>";
1796 print "<td>";
1797 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1798 hash_base=>$hash, file_name=>$diff{'file'}),
1799 -class => "list"}, esc_html($diff{'file'}));
1800 print "</td>\n";
1801 print "<td>$mode_chng</td>\n";
1802 print "<td class=\"link\">";
1803 if ($action eq 'commitdiff') {
1804 # link to patch
1805 $patchno++;
1806 print $cgi->a({-href => "#patch$patchno"}, "patch");
1807 }
1808 print "</td>\n";
1809
1810 } elsif ($diff{'status'} eq "D") { # deleted
1811 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1812 print "<td>";
1813 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1814 hash_base=>$parent, file_name=>$diff{'file'}),
1815 -class => "list"}, esc_html($diff{'file'}));
1816 print "</td>\n";
1817 print "<td>$mode_chng</td>\n";
1818 print "<td class=\"link\">";
1819 if ($action eq 'commitdiff') {
1820 # link to patch
1821 $patchno++;
1822 print $cgi->a({-href => "#patch$patchno"}, "patch");
1823 print " | ";
1824 }
1825 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1826 file_name=>$diff{'file'})},
1827 "blame") . " | ";
1828 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1829 file_name=>$diff{'file'})},
1830 "history");
1831 print "</td>\n";
1832
1833 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1834 my $mode_chnge = "";
1835 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1836 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1837 if ($from_file_type != $to_file_type) {
1838 $mode_chnge .= " from $from_file_type to $to_file_type";
1839 }
1840 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1841 if ($from_mode_str && $to_mode_str) {
1842 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1843 } elsif ($to_mode_str) {
1844 $mode_chnge .= " mode: $to_mode_str";
1845 }
1846 }
1847 $mode_chnge .= "]</span>\n";
1848 }
1849 print "<td>";
1850 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1851 hash_base=>$hash, file_name=>$diff{'file'}),
1852 -class => "list"}, esc_html($diff{'file'}));
1853 print "</td>\n";
1854 print "<td>$mode_chnge</td>\n";
1855 print "<td class=\"link\">";
1856 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1857 if ($action eq 'commitdiff') {
1858 # link to patch
1859 $patchno++;
1860 print $cgi->a({-href => "#patch$patchno"}, "patch");
1861 } else {
1862 print $cgi->a({-href => href(action=>"blobdiff",
1863 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1864 hash_base=>$hash, hash_parent_base=>$parent,
1865 file_name=>$diff{'file'})},
1866 "diff");
1867 }
1868 print " | ";
1869 }
1870 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
1871 file_name=>$diff{'file'})},
1872 "blame") . " | ";
1873 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
1874 file_name=>$diff{'file'})},
1875 "history");
1876 print "</td>\n";
1877
1878 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1879 my %status_name = ('R' => 'moved', 'C' => 'copied');
1880 my $nstatus = $status_name{$diff{'status'}};
1881 my $mode_chng = "";
1882 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1883 # mode also for directories, so we cannot use $to_mode_str
1884 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1885 }
1886 print "<td>" .
1887 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1888 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1889 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1890 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1891 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1892 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1893 -class => "list"}, esc_html($diff{'from_file'})) .
1894 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1895 "<td class=\"link\">";
1896 if ($diff{'to_id'} ne $diff{'from_id'}) {
1897 if ($action eq 'commitdiff') {
1898 # link to patch
1899 $patchno++;
1900 print $cgi->a({-href => "#patch$patchno"}, "patch");
1901 } else {
1902 print $cgi->a({-href => href(action=>"blobdiff",
1903 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1904 hash_base=>$hash, hash_parent_base=>$parent,
1905 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1906 "diff");
1907 }
1908 print " | ";
1909 }
1910 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1911 file_name=>$diff{'from_file'})},
1912 "blame") . " | ";
1913 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1914 file_name=>$diff{'from_file'})},
1915 "history");
1916 print "</td>\n";
1917
1918 } # we should not encounter Unmerged (U) or Unknown (X) status
1919 print "</tr>\n";
1920 }
1921 print "</table>\n";
1922}
1923
1924sub git_patchset_body {
1925 my ($fd, $difftree, $hash, $hash_parent) = @_;
1926
1927 my $patch_idx = 0;
1928 my $in_header = 0;
1929 my $patch_found = 0;
1930 my $diffinfo;
1931
1932 print "<div class=\"patchset\">\n";
1933
1934 LINE:
1935 while (my $patch_line = <$fd>) {
1936 chomp $patch_line;
1937
1938 if ($patch_line =~ m/^diff /) { # "git diff" header
1939 # beginning of patch (in patchset)
1940 if ($patch_found) {
1941 # close previous patch
1942 print "</div>\n"; # class="patch"
1943 } else {
1944 # first patch in patchset
1945 $patch_found = 1;
1946 }
1947 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
1948
1949 if (ref($difftree->[$patch_idx]) eq "HASH") {
1950 $diffinfo = $difftree->[$patch_idx];
1951 } else {
1952 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1953 }
1954 $patch_idx++;
1955
1956 # for now, no extended header, hence we skip empty patches
1957 # companion to next LINE if $in_header;
1958 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
1959 $in_header = 1;
1960 next LINE;
1961 }
1962
1963 if ($diffinfo->{'status'} eq "A") { # added
1964 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
1965 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1966 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1967 $diffinfo->{'to_id'}) . "(new)" .
1968 "</div>\n"; # class="diff_info"
1969
1970 } elsif ($diffinfo->{'status'} eq "D") { # deleted
1971 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
1972 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1973 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1974 $diffinfo->{'from_id'}) . "(deleted)" .
1975 "</div>\n"; # class="diff_info"
1976
1977 } elsif ($diffinfo->{'status'} eq "R" || # renamed
1978 $diffinfo->{'status'} eq "C" || # copied
1979 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
1980 print "<div class=\"diff_info\">" .
1981 file_type($diffinfo->{'from_mode'}) . ":" .
1982 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1983 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1984 $diffinfo->{'from_id'}) .
1985 " -> " .
1986 file_type($diffinfo->{'to_mode'}) . ":" .
1987 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1988 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1989 $diffinfo->{'to_id'});
1990 print "</div>\n"; # class="diff_info"
1991
1992 } else { # modified, mode changed, ...
1993 print "<div class=\"diff_info\">" .
1994 file_type($diffinfo->{'from_mode'}) . ":" .
1995 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1996 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1997 $diffinfo->{'from_id'}) .
1998 " -> " .
1999 file_type($diffinfo->{'to_mode'}) . ":" .
2000 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2001 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2002 $diffinfo->{'to_id'});
2003 print "</div>\n"; # class="diff_info"
2004 }
2005
2006 #print "<div class=\"diff extended_header\">\n";
2007 $in_header = 1;
2008 next LINE;
2009 } # start of patch in patchset
2010
2011
2012 if ($in_header && $patch_line =~ m/^---/) {
2013 #print "</div>\n"; # class="diff extended_header"
2014 $in_header = 0;
2015
2016 my $file = $diffinfo->{'from_file'};
2017 $file ||= $diffinfo->{'file'};
2018 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2019 hash=>$diffinfo->{'from_id'}, file_name=>$file),
2020 -class => "list"}, esc_html($file));
2021 $patch_line =~ s|a/.*$|a/$file|g;
2022 print "<div class=\"diff from_file\">$patch_line</div>\n";
2023
2024 $patch_line = <$fd>;
2025 chomp $patch_line;
2026
2027 #$patch_line =~ m/^+++/;
2028 $file = $diffinfo->{'to_file'};
2029 $file ||= $diffinfo->{'file'};
2030 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2031 hash=>$diffinfo->{'to_id'}, file_name=>$file),
2032 -class => "list"}, esc_html($file));
2033 $patch_line =~ s|b/.*|b/$file|g;
2034 print "<div class=\"diff to_file\">$patch_line</div>\n";
2035
2036 next LINE;
2037 }
2038 next LINE if $in_header;
2039
2040 print format_diff_line($patch_line);
2041 }
2042 print "</div>\n" if $patch_found; # class="patch"
2043
2044 print "</div>\n"; # class="patchset"
2045}
2046
2047# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2048
2049sub git_shortlog_body {
2050 # uses global variable $project
2051 my ($revlist, $from, $to, $refs, $extra) = @_;
2052
2053 $from = 0 unless defined $from;
2054 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2055
2056 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2057 my $alternate = 1;
2058 for (my $i = $from; $i <= $to; $i++) {
2059 my $commit = $revlist->[$i];
2060 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2061 my $ref = format_ref_marker($refs, $commit);
2062 my %co = parse_commit($commit);
2063 if ($alternate) {
2064 print "<tr class=\"dark\">\n";
2065 } else {
2066 print "<tr class=\"light\">\n";
2067 }
2068 $alternate ^= 1;
2069 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2070 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2071 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2072 "<td>";
2073 print format_subject_html($co{'title'}, $co{'title_short'},
2074 href(action=>"commit", hash=>$commit), $ref);
2075 print "</td>\n" .
2076 "<td class=\"link\">" .
2077 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2078 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") . " | " .
2079 $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2080 print "</td>\n" .
2081 "</tr>\n";
2082 }
2083 if (defined $extra) {
2084 print "<tr>\n" .
2085 "<td colspan=\"4\">$extra</td>\n" .
2086 "</tr>\n";
2087 }
2088 print "</table>\n";
2089}
2090
2091sub git_history_body {
2092 # Warning: assumes constant type (blob or tree) during history
2093 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2094
2095 $from = 0 unless defined $from;
2096 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2097
2098 print "<table class=\"history\" cellspacing=\"0\">\n";
2099 my $alternate = 1;
2100 for (my $i = $from; $i <= $to; $i++) {
2101 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2102 next;
2103 }
2104
2105 my $commit = $1;
2106 my %co = parse_commit($commit);
2107 if (!%co) {
2108 next;
2109 }
2110
2111 my $ref = format_ref_marker($refs, $commit);
2112
2113 if ($alternate) {
2114 print "<tr class=\"dark\">\n";
2115 } else {
2116 print "<tr class=\"light\">\n";
2117 }
2118 $alternate ^= 1;
2119 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2120 # shortlog uses chop_str($co{'author_name'}, 10)
2121 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2122 "<td>";
2123 # originally git_history used chop_str($co{'title'}, 50)
2124 print format_subject_html($co{'title'}, $co{'title_short'},
2125 href(action=>"commit", hash=>$commit), $ref);
2126 print "</td>\n" .
2127 "<td class=\"link\">" .
2128 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2129 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2130
2131 if ($ftype eq 'blob') {
2132 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2133 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2134 if (defined $blob_current && defined $blob_parent &&
2135 $blob_current ne $blob_parent) {
2136 print " | " .
2137 $cgi->a({-href => href(action=>"blobdiff",
2138 hash=>$blob_current, hash_parent=>$blob_parent,
2139 hash_base=>$hash_base, hash_parent_base=>$commit,
2140 file_name=>$file_name)},
2141 "diff to current");
2142 }
2143 }
2144 print "</td>\n" .
2145 "</tr>\n";
2146 }
2147 if (defined $extra) {
2148 print "<tr>\n" .
2149 "<td colspan=\"4\">$extra</td>\n" .
2150 "</tr>\n";
2151 }
2152 print "</table>\n";
2153}
2154
2155sub git_tags_body {
2156 # uses global variable $project
2157 my ($taglist, $from, $to, $extra) = @_;
2158 $from = 0 unless defined $from;
2159 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2160
2161 print "<table class=\"tags\" cellspacing=\"0\">\n";
2162 my $alternate = 1;
2163 for (my $i = $from; $i <= $to; $i++) {
2164 my $entry = $taglist->[$i];
2165 my %tag = %$entry;
2166 my $comment_lines = $tag{'comment'};
2167 my $comment = shift @$comment_lines;
2168 my $comment_short;
2169 if (defined $comment) {
2170 $comment_short = chop_str($comment, 30, 5);
2171 }
2172 if ($alternate) {
2173 print "<tr class=\"dark\">\n";
2174 } else {
2175 print "<tr class=\"light\">\n";
2176 }
2177 $alternate ^= 1;
2178 print "<td><i>$tag{'age'}</i></td>\n" .
2179 "<td>" .
2180 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2181 -class => "list name"}, esc_html($tag{'name'})) .
2182 "</td>\n" .
2183 "<td>";
2184 if (defined $comment) {
2185 print format_subject_html($comment, $comment_short,
2186 href(action=>"tag", hash=>$tag{'id'}));
2187 }
2188 print "</td>\n" .
2189 "<td class=\"selflink\">";
2190 if ($tag{'type'} eq "tag") {
2191 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2192 } else {
2193 print " ";
2194 }
2195 print "</td>\n" .
2196 "<td class=\"link\">" . " | " .
2197 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2198 if ($tag{'reftype'} eq "commit") {
2199 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2200 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2201 } elsif ($tag{'reftype'} eq "blob") {
2202 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2203 }
2204 print "</td>\n" .
2205 "</tr>";
2206 }
2207 if (defined $extra) {
2208 print "<tr>\n" .
2209 "<td colspan=\"5\">$extra</td>\n" .
2210 "</tr>\n";
2211 }
2212 print "</table>\n";
2213}
2214
2215sub git_heads_body {
2216 # uses global variable $project
2217 my ($headlist, $head, $from, $to, $extra) = @_;
2218 $from = 0 unless defined $from;
2219 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2220
2221 print "<table class=\"heads\" cellspacing=\"0\">\n";
2222 my $alternate = 1;
2223 for (my $i = $from; $i <= $to; $i++) {
2224 my $entry = $headlist->[$i];
2225 my %tag = %$entry;
2226 my $curr = $tag{'id'} eq $head;
2227 if ($alternate) {
2228 print "<tr class=\"dark\">\n";
2229 } else {
2230 print "<tr class=\"light\">\n";
2231 }
2232 $alternate ^= 1;
2233 print "<td><i>$tag{'age'}</i></td>\n" .
2234 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2235 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2236 -class => "list name"},esc_html($tag{'name'})) .
2237 "</td>\n" .
2238 "<td class=\"link\">" .
2239 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2240 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2241 $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
2242 "</td>\n" .
2243 "</tr>";
2244 }
2245 if (defined $extra) {
2246 print "<tr>\n" .
2247 "<td colspan=\"3\">$extra</td>\n" .
2248 "</tr>\n";
2249 }
2250 print "</table>\n";
2251}
2252
2253## ======================================================================
2254## ======================================================================
2255## actions
2256
2257sub git_project_list {
2258 my $order = $cgi->param('o');
2259 if (defined $order && $order !~ m/project|descr|owner|age/) {
2260 die_error(undef, "Unknown order parameter");
2261 }
2262
2263 my @list = git_get_projects_list();
2264 my @projects;
2265 if (!@list) {
2266 die_error(undef, "No projects found");
2267 }
2268 foreach my $pr (@list) {
2269 my $head = git_get_head_hash($pr->{'path'});
2270 if (!defined $head) {
2271 next;
2272 }
2273 $git_dir = "$projectroot/$pr->{'path'}";
2274 my %co = parse_commit($head);
2275 if (!%co) {
2276 next;
2277 }
2278 $pr->{'commit'} = \%co;
2279 if (!defined $pr->{'descr'}) {
2280 my $descr = git_get_project_description($pr->{'path'}) || "";
2281 $pr->{'descr'} = chop_str($descr, 25, 5);
2282 }
2283 if (!defined $pr->{'owner'}) {
2284 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2285 }
2286 push @projects, $pr;
2287 }
2288
2289 git_header_html();
2290 if (-f $home_text) {
2291 print "<div class=\"index_include\">\n";
2292 open (my $fd, $home_text);
2293 print <$fd>;
2294 close $fd;
2295 print "</div>\n";
2296 }
2297 print "<table class=\"project_list\">\n" .
2298 "<tr>\n";
2299 $order ||= "project";
2300 if ($order eq "project") {
2301 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2302 print "<th>Project</th>\n";
2303 } else {
2304 print "<th>" .
2305 $cgi->a({-href => href(project=>undef, order=>'project'),
2306 -class => "header"}, "Project") .
2307 "</th>\n";
2308 }
2309 if ($order eq "descr") {
2310 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2311 print "<th>Description</th>\n";
2312 } else {
2313 print "<th>" .
2314 $cgi->a({-href => href(project=>undef, order=>'descr'),
2315 -class => "header"}, "Description") .
2316 "</th>\n";
2317 }
2318 if ($order eq "owner") {
2319 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2320 print "<th>Owner</th>\n";
2321 } else {
2322 print "<th>" .
2323 $cgi->a({-href => href(project=>undef, order=>'owner'),
2324 -class => "header"}, "Owner") .
2325 "</th>\n";
2326 }
2327 if ($order eq "age") {
2328 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2329 print "<th>Last Change</th>\n";
2330 } else {
2331 print "<th>" .
2332 $cgi->a({-href => href(project=>undef, order=>'age'),
2333 -class => "header"}, "Last Change") .
2334 "</th>\n";
2335 }
2336 print "<th></th>\n" .
2337 "</tr>\n";
2338 my $alternate = 1;
2339 foreach my $pr (@projects) {
2340 if ($alternate) {
2341 print "<tr class=\"dark\">\n";
2342 } else {
2343 print "<tr class=\"light\">\n";
2344 }
2345 $alternate ^= 1;
2346 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2347 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2348 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2349 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2350 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2351 $pr->{'commit'}{'age_string'} . "</td>\n" .
2352 "<td class=\"link\">" .
2353 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2354 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2355 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2356 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2357 "</td>\n" .
2358 "</tr>\n";
2359 }
2360 print "</table>\n";
2361 git_footer_html();
2362}
2363
2364sub git_project_index {
2365 my @projects = git_get_projects_list();
2366
2367 print $cgi->header(
2368 -type => 'text/plain',
2369 -charset => 'utf-8',
2370 -content_disposition => 'inline; filename="index.aux"');
2371
2372 foreach my $pr (@projects) {
2373 if (!exists $pr->{'owner'}) {
2374 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2375 }
2376
2377 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2378 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2379 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2380 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2381 $path =~ s/ /\+/g;
2382 $owner =~ s/ /\+/g;
2383
2384 print "$path $owner\n";
2385 }
2386}
2387
2388sub git_summary {
2389 my $descr = git_get_project_description($project) || "none";
2390 my $head = git_get_head_hash($project);
2391 my %co = parse_commit($head);
2392 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2393
2394 my $owner = git_get_project_owner($project);
2395
2396 my ($reflist, $refs) = git_get_refs_list();
2397
2398 my @taglist;
2399 my @headlist;
2400 foreach my $ref (@$reflist) {
2401 if ($ref->{'name'} =~ s!^heads/!!) {
2402 push @headlist, $ref;
2403 } else {
2404 $ref->{'name'} =~ s!^tags/!!;
2405 push @taglist, $ref;
2406 }
2407 }
2408
2409 git_header_html();
2410 git_print_page_nav('summary','', $head);
2411
2412 print "<div class=\"title\"> </div>\n";
2413 print "<table cellspacing=\"0\">\n" .
2414 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2415 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2416 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2417 # use per project git URL list in $projectroot/$project/cloneurl
2418 # or make project git URL from git base URL and project name
2419 my $url_tag = "URL";
2420 my @url_list = git_get_project_url_list($project);
2421 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2422 foreach my $git_url (@url_list) {
2423 next unless $git_url;
2424 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2425 $url_tag = "";
2426 }
2427 print "</table>\n";
2428
2429 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2430 git_get_head_hash($project)
2431 or die_error(undef, "Open git-rev-list failed");
2432 my @revlist = map { chomp; $_ } <$fd>;
2433 close $fd;
2434 git_print_header_div('shortlog');
2435 git_shortlog_body(\@revlist, 0, 15, $refs,
2436 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2437
2438 if (@taglist) {
2439 git_print_header_div('tags');
2440 git_tags_body(\@taglist, 0, 15,
2441 $cgi->a({-href => href(action=>"tags")}, "..."));
2442 }
2443
2444 if (@headlist) {
2445 git_print_header_div('heads');
2446 git_heads_body(\@headlist, $head, 0, 15,
2447 $cgi->a({-href => href(action=>"heads")}, "..."));
2448 }
2449
2450 git_footer_html();
2451}
2452
2453sub git_tag {
2454 my $head = git_get_head_hash($project);
2455 git_header_html();
2456 git_print_page_nav('','', $head,undef,$head);
2457 my %tag = parse_tag($hash);
2458 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2459 print "<div class=\"title_text\">\n" .
2460 "<table cellspacing=\"0\">\n" .
2461 "<tr>\n" .
2462 "<td>object</td>\n" .
2463 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2464 $tag{'object'}) . "</td>\n" .
2465 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2466 $tag{'type'}) . "</td>\n" .
2467 "</tr>\n";
2468 if (defined($tag{'author'})) {
2469 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2470 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2471 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2472 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2473 "</td></tr>\n";
2474 }
2475 print "</table>\n\n" .
2476 "</div>\n";
2477 print "<div class=\"page_body\">";
2478 my $comment = $tag{'comment'};
2479 foreach my $line (@$comment) {
2480 print esc_html($line) . "<br/>\n";
2481 }
2482 print "</div>\n";
2483 git_footer_html();
2484}
2485
2486sub git_blame2 {
2487 my $fd;
2488 my $ftype;
2489
2490 my ($have_blame) = gitweb_check_feature('blame');
2491 if (!$have_blame) {
2492 die_error('403 Permission denied', "Permission denied");
2493 }
2494 die_error('404 Not Found', "File name not defined") if (!$file_name);
2495 $hash_base ||= git_get_head_hash($project);
2496 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2497 my %co = parse_commit($hash_base)
2498 or die_error(undef, "Reading commit failed");
2499 if (!defined $hash) {
2500 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2501 or die_error(undef, "Error looking up file");
2502 }
2503 $ftype = git_get_type($hash);
2504 if ($ftype !~ "blob") {
2505 die_error("400 Bad Request", "Object is not a blob");
2506 }
2507 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
2508 $file_name, $hash_base)
2509 or die_error(undef, "Open git-blame failed");
2510 git_header_html();
2511 my $formats_nav =
2512 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2513 "blob") .
2514 " | " .
2515 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2516 "history") .
2517 " | " .
2518 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2519 "HEAD");
2520 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2521 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2522 git_print_page_path($file_name, $ftype, $hash_base);
2523 my @rev_color = (qw(light2 dark2));
2524 my $num_colors = scalar(@rev_color);
2525 my $current_color = 0;
2526 my $last_rev;
2527 print <<HTML;
2528<div class="page_body">
2529<table class="blame">
2530<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2531HTML
2532 my %metainfo = ();
2533 while (1) {
2534 $_ = <$fd>;
2535 last unless defined $_;
2536 my ($full_rev, $orig_lineno, $lineno, $group_size) =
2537 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
2538 if (!exists $metainfo{$full_rev}) {
2539 $metainfo{$full_rev} = {};
2540 }
2541 my $meta = $metainfo{$full_rev};
2542 while (<$fd>) {
2543 last if (s/^\t//);
2544 if (/^(\S+) (.*)$/) {
2545 $meta->{$1} = $2;
2546 }
2547 }
2548 my $data = $_;
2549 my $rev = substr($full_rev, 0, 8);
2550 my $author = $meta->{'author'};
2551 my %date = parse_date($meta->{'author-time'},
2552 $meta->{'author-tz'});
2553 my $date = $date{'iso-tz'};
2554 if ($group_size) {
2555 $current_color = ++$current_color % $num_colors;
2556 }
2557 print "<tr class=\"$rev_color[$current_color]\">\n";
2558 if ($group_size) {
2559 print "<td class=\"sha1\"";
2560 print " title=\"$author, $date\"";
2561 print " rowspan=\"$group_size\"" if ($group_size > 1);
2562 print ">";
2563 print $cgi->a({-href => href(action=>"commit",
2564 hash=>$full_rev,
2565 file_name=>$file_name)},
2566 esc_html($rev));
2567 print "</td>\n";
2568 }
2569 my $blamed = href(action => 'blame',
2570 file_name => $meta->{'filename'},
2571 hash_base => $full_rev);
2572 print "<td class=\"linenr\">";
2573 print $cgi->a({ -href => "$blamed#l$orig_lineno",
2574 -id => "l$lineno",
2575 -class => "linenr" },
2576 esc_html($lineno));
2577 print "</td>";
2578 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2579 print "</tr>\n";
2580 }
2581 print "</table>\n";
2582 print "</div>";
2583 close $fd
2584 or print "Reading blob failed\n";
2585 git_footer_html();
2586}
2587
2588sub git_blame {
2589 my $fd;
2590
2591 my ($have_blame) = gitweb_check_feature('blame');
2592 if (!$have_blame) {
2593 die_error('403 Permission denied', "Permission denied");
2594 }
2595 die_error('404 Not Found', "File name not defined") if (!$file_name);
2596 $hash_base ||= git_get_head_hash($project);
2597 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2598 my %co = parse_commit($hash_base)
2599 or die_error(undef, "Reading commit failed");
2600 if (!defined $hash) {
2601 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2602 or die_error(undef, "Error lookup file");
2603 }
2604 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2605 or die_error(undef, "Open git-annotate failed");
2606 git_header_html();
2607 my $formats_nav =
2608 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2609 "blob") .
2610 " | " .
2611 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2612 "history") .
2613 " | " .
2614 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2615 "HEAD");
2616 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2617 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2618 git_print_page_path($file_name, 'blob', $hash_base);
2619 print "<div class=\"page_body\">\n";
2620 print <<HTML;
2621<table class="blame">
2622 <tr>
2623 <th>Commit</th>
2624 <th>Age</th>
2625 <th>Author</th>
2626 <th>Line</th>
2627 <th>Data</th>
2628 </tr>
2629HTML
2630 my @line_class = (qw(light dark));
2631 my $line_class_len = scalar (@line_class);
2632 my $line_class_num = $#line_class;
2633 while (my $line = <$fd>) {
2634 my $long_rev;
2635 my $short_rev;
2636 my $author;
2637 my $time;
2638 my $lineno;
2639 my $data;
2640 my $age;
2641 my $age_str;
2642 my $age_class;
2643
2644 chomp $line;
2645 $line_class_num = ($line_class_num + 1) % $line_class_len;
2646
2647 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2648 $long_rev = $1;
2649 $author = $2;
2650 $time = $3;
2651 $lineno = $4;
2652 $data = $5;
2653 } else {
2654 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2655 next;
2656 }
2657 $short_rev = substr ($long_rev, 0, 8);
2658 $age = time () - $time;
2659 $age_str = age_string ($age);
2660 $age_str =~ s/ / /g;
2661 $age_class = age_class($age);
2662 $author = esc_html ($author);
2663 $author =~ s/ / /g;
2664
2665 $data = untabify($data);
2666 $data = esc_html ($data);
2667
2668 print <<HTML;
2669 <tr class="$line_class[$line_class_num]">
2670 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2671 <td class="$age_class">$age_str</td>
2672 <td>$author</td>
2673 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2674 <td class="pre">$data</td>
2675 </tr>
2676HTML
2677 } # while (my $line = <$fd>)
2678 print "</table>\n\n";
2679 close $fd
2680 or print "Reading blob failed.\n";
2681 print "</div>";
2682 git_footer_html();
2683}
2684
2685sub git_tags {
2686 my $head = git_get_head_hash($project);
2687 git_header_html();
2688 git_print_page_nav('','', $head,undef,$head);
2689 git_print_header_div('summary', $project);
2690
2691 my ($taglist) = git_get_refs_list("tags");
2692 if (@$taglist) {
2693 git_tags_body($taglist);
2694 }
2695 git_footer_html();
2696}
2697
2698sub git_heads {
2699 my $head = git_get_head_hash($project);
2700 git_header_html();
2701 git_print_page_nav('','', $head,undef,$head);
2702 git_print_header_div('summary', $project);
2703
2704 my ($headlist) = git_get_refs_list("heads");
2705 if (@$headlist) {
2706 git_heads_body($headlist, $head);
2707 }
2708 git_footer_html();
2709}
2710
2711sub git_blob_plain {
2712 my $expires;
2713
2714 if (!defined $hash) {
2715 if (defined $file_name) {
2716 my $base = $hash_base || git_get_head_hash($project);
2717 $hash = git_get_hash_by_path($base, $file_name, "blob")
2718 or die_error(undef, "Error lookup file");
2719 } else {
2720 die_error(undef, "No file name defined");
2721 }
2722 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2723 # blobs defined by non-textual hash id's can be cached
2724 $expires = "+1d";
2725 }
2726
2727 my $type = shift;
2728 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2729 or die_error(undef, "Couldn't cat $file_name, $hash");
2730
2731 $type ||= blob_mimetype($fd, $file_name);
2732
2733 # save as filename, even when no $file_name is given
2734 my $save_as = "$hash";
2735 if (defined $file_name) {
2736 $save_as = $file_name;
2737 } elsif ($type =~ m/^text\//) {
2738 $save_as .= '.txt';
2739 }
2740
2741 print $cgi->header(
2742 -type => "$type",
2743 -expires=>$expires,
2744 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2745 undef $/;
2746 binmode STDOUT, ':raw';
2747 print <$fd>;
2748 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2749 $/ = "\n";
2750 close $fd;
2751}
2752
2753sub git_blob {
2754 my $expires;
2755
2756 if (!defined $hash) {
2757 if (defined $file_name) {
2758 my $base = $hash_base || git_get_head_hash($project);
2759 $hash = git_get_hash_by_path($base, $file_name, "blob")
2760 or die_error(undef, "Error lookup file");
2761 } else {
2762 die_error(undef, "No file name defined");
2763 }
2764 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2765 # blobs defined by non-textual hash id's can be cached
2766 $expires = "+1d";
2767 }
2768
2769 my ($have_blame) = gitweb_check_feature('blame');
2770 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2771 or die_error(undef, "Couldn't cat $file_name, $hash");
2772 my $mimetype = blob_mimetype($fd, $file_name);
2773 if ($mimetype !~ m/^text\//) {
2774 close $fd;
2775 return git_blob_plain($mimetype);
2776 }
2777 git_header_html(undef, $expires);
2778 my $formats_nav = '';
2779 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2780 if (defined $file_name) {
2781 if ($have_blame) {
2782 $formats_nav .=
2783 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2784 hash=>$hash, file_name=>$file_name)},
2785 "blame") .
2786 " | ";
2787 }
2788 $formats_nav .=
2789 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2790 hash=>$hash, file_name=>$file_name)},
2791 "history") .
2792 " | " .
2793 $cgi->a({-href => href(action=>"blob_plain",
2794 hash=>$hash, file_name=>$file_name)},
2795 "raw") .
2796 " | " .
2797 $cgi->a({-href => href(action=>"blob",
2798 hash_base=>"HEAD", file_name=>$file_name)},
2799 "HEAD");
2800 } else {
2801 $formats_nav .=
2802 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
2803 }
2804 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2805 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2806 } else {
2807 print "<div class=\"page_nav\">\n" .
2808 "<br/><br/></div>\n" .
2809 "<div class=\"title\">$hash</div>\n";
2810 }
2811 git_print_page_path($file_name, "blob", $hash_base);
2812 print "<div class=\"page_body\">\n";
2813 my $nr;
2814 while (my $line = <$fd>) {
2815 chomp $line;
2816 $nr++;
2817 $line = untabify($line);
2818 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2819 $nr, $nr, $nr, esc_html($line);
2820 }
2821 close $fd
2822 or print "Reading blob failed.\n";
2823 print "</div>";
2824 git_footer_html();
2825}
2826
2827sub git_tree {
2828 my $have_snapshot = gitweb_have_snapshot();
2829
2830 if (!defined $hash_base) {
2831 $hash_base = "HEAD";
2832 }
2833 if (!defined $hash) {
2834 if (defined $file_name) {
2835 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
2836 } else {
2837 $hash = $hash_base;
2838 }
2839 }
2840 $/ = "\0";
2841 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2842 or die_error(undef, "Open git-ls-tree failed");
2843 my @entries = map { chomp; $_ } <$fd>;
2844 close $fd or die_error(undef, "Reading tree failed");
2845 $/ = "\n";
2846
2847 my $refs = git_get_references();
2848 my $ref = format_ref_marker($refs, $hash_base);
2849 git_header_html();
2850 my $base = "";
2851 my ($have_blame) = gitweb_check_feature('blame');
2852 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2853 my @views_nav = ();
2854 if (defined $file_name) {
2855 push @views_nav,
2856 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2857 hash=>$hash, file_name=>$file_name)},
2858 "history"),
2859 $cgi->a({-href => href(action=>"tree",
2860 hash_base=>"HEAD", file_name=>$file_name)},
2861 "HEAD"),
2862 }
2863 if ($have_snapshot) {
2864 # FIXME: Should be available when we have no hash base as well.
2865 push @views_nav,
2866 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
2867 "snapshot");
2868 }
2869 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
2870 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2871 } else {
2872 undef $hash_base;
2873 print "<div class=\"page_nav\">\n";
2874 print "<br/><br/></div>\n";
2875 print "<div class=\"title\">$hash</div>\n";
2876 }
2877 if (defined $file_name) {
2878 $base = esc_html("$file_name/");
2879 }
2880 git_print_page_path($file_name, 'tree', $hash_base);
2881 print "<div class=\"page_body\">\n";
2882 print "<table cellspacing=\"0\">\n";
2883 my $alternate = 1;
2884 foreach my $line (@entries) {
2885 my %t = parse_ls_tree_line($line, -z => 1);
2886
2887 if ($alternate) {
2888 print "<tr class=\"dark\">\n";
2889 } else {
2890 print "<tr class=\"light\">\n";
2891 }
2892 $alternate ^= 1;
2893
2894 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2895
2896 print "</tr>\n";
2897 }
2898 print "</table>\n" .
2899 "</div>";
2900 git_footer_html();
2901}
2902
2903sub git_snapshot {
2904 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2905 my $have_snapshot = (defined $ctype && defined $suffix);
2906 if (!$have_snapshot) {
2907 die_error('403 Permission denied', "Permission denied");
2908 }
2909
2910 if (!defined $hash) {
2911 $hash = git_get_head_hash($project);
2912 }
2913
2914 my $filename = basename($project) . "-$hash.tar.$suffix";
2915
2916 print $cgi->header(
2917 -type => 'application/x-tar',
2918 -content_encoding => $ctype,
2919 -content_disposition => 'inline; filename="' . "$filename" . '"',
2920 -status => '200 OK');
2921
2922 my $git = git_cmd_str();
2923 my $name = $project;
2924 $name =~ s/\047/\047\\\047\047/g;
2925 open my $fd, "-|",
2926 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
2927 or die_error(undef, "Execute git-tar-tree failed.");
2928 binmode STDOUT, ':raw';
2929 print <$fd>;
2930 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2931 close $fd;
2932
2933}
2934
2935sub git_log {
2936 my $head = git_get_head_hash($project);
2937 if (!defined $hash) {
2938 $hash = $head;
2939 }
2940 if (!defined $page) {
2941 $page = 0;
2942 }
2943 my $refs = git_get_references();
2944
2945 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2946 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
2947 or die_error(undef, "Open git-rev-list failed");
2948 my @revlist = map { chomp; $_ } <$fd>;
2949 close $fd;
2950
2951 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2952
2953 git_header_html();
2954 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2955
2956 if (!@revlist) {
2957 my %co = parse_commit($hash);
2958
2959 git_print_header_div('summary', $project);
2960 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2961 }
2962 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2963 my $commit = $revlist[$i];
2964 my $ref = format_ref_marker($refs, $commit);
2965 my %co = parse_commit($commit);
2966 next if !%co;
2967 my %ad = parse_date($co{'author_epoch'});
2968 git_print_header_div('commit',
2969 "<span class=\"age\">$co{'age_string'}</span>" .
2970 esc_html($co{'title'}) . $ref,
2971 $commit);
2972 print "<div class=\"title_text\">\n" .
2973 "<div class=\"log_link\">\n" .
2974 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2975 " | " .
2976 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2977 " | " .
2978 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
2979 "<br/>\n" .
2980 "</div>\n" .
2981 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
2982 "</div>\n";
2983
2984 print "<div class=\"log_body\">\n";
2985 git_print_simplified_log($co{'comment'});
2986 print "</div>\n";
2987 }
2988 git_footer_html();
2989}
2990
2991sub git_commit {
2992 my %co = parse_commit($hash);
2993 if (!%co) {
2994 die_error(undef, "Unknown commit object");
2995 }
2996 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2997 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2998
2999 my $parent = $co{'parent'};
3000 if (!defined $parent) {
3001 $parent = "--root";
3002 }
3003 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
3004 or die_error(undef, "Open git-diff-tree failed");
3005 my @difftree = map { chomp; $_ } <$fd>;
3006 close $fd or die_error(undef, "Reading git-diff-tree failed");
3007
3008 # non-textual hash id's can be cached
3009 my $expires;
3010 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3011 $expires = "+1d";
3012 }
3013 my $refs = git_get_references();
3014 my $ref = format_ref_marker($refs, $co{'id'});
3015
3016 my $have_snapshot = gitweb_have_snapshot();
3017
3018 my @views_nav = ();
3019 if (defined $file_name && defined $co{'parent'}) {
3020 push @views_nav,
3021 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
3022 "blame");
3023 }
3024 git_header_html(undef, $expires);
3025 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
3026 $hash, $co{'tree'}, $hash,
3027 join (' | ', @views_nav));
3028
3029 if (defined $co{'parent'}) {
3030 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3031 } else {
3032 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3033 }
3034 print "<div class=\"title_text\">\n" .
3035 "<table cellspacing=\"0\">\n";
3036 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3037 "<tr>" .
3038 "<td></td><td> $ad{'rfc2822'}";
3039 if ($ad{'hour_local'} < 6) {
3040 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3041 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3042 } else {
3043 printf(" (%02d:%02d %s)",
3044 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3045 }
3046 print "</td>" .
3047 "</tr>\n";
3048 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3049 print "<tr><td></td><td> $cd{'rfc2822'}" .
3050 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3051 "</td></tr>\n";
3052 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3053 print "<tr>" .
3054 "<td>tree</td>" .
3055 "<td class=\"sha1\">" .
3056 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3057 class => "list"}, $co{'tree'}) .
3058 "</td>" .
3059 "<td class=\"link\">" .
3060 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3061 "tree");
3062 if ($have_snapshot) {
3063 print " | " .
3064 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3065 }
3066 print "</td>" .
3067 "</tr>\n";
3068 my $parents = $co{'parents'};
3069 foreach my $par (@$parents) {
3070 print "<tr>" .
3071 "<td>parent</td>" .
3072 "<td class=\"sha1\">" .
3073 $cgi->a({-href => href(action=>"commit", hash=>$par),
3074 class => "list"}, $par) .
3075 "</td>" .
3076 "<td class=\"link\">" .
3077 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3078 " | " .
3079 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3080 "</td>" .
3081 "</tr>\n";
3082 }
3083 print "</table>".
3084 "</div>\n";
3085
3086 print "<div class=\"page_body\">\n";
3087 git_print_log($co{'comment'});
3088 print "</div>\n";
3089
3090 git_difftree_body(\@difftree, $hash, $parent);
3091
3092 git_footer_html();
3093}
3094
3095sub git_blobdiff {
3096 my $format = shift || 'html';
3097
3098 my $fd;
3099 my @difftree;
3100 my %diffinfo;
3101 my $expires;
3102
3103 # preparing $fd and %diffinfo for git_patchset_body
3104 # new style URI
3105 if (defined $hash_base && defined $hash_parent_base) {
3106 if (defined $file_name) {
3107 # read raw output
3108 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
3109 "--", $file_name
3110 or die_error(undef, "Open git-diff-tree failed");
3111 @difftree = map { chomp; $_ } <$fd>;
3112 close $fd
3113 or die_error(undef, "Reading git-diff-tree failed");
3114 @difftree
3115 or die_error('404 Not Found', "Blob diff not found");
3116
3117 } elsif (defined $hash &&
3118 $hash =~ /[0-9a-fA-F]{40}/) {
3119 # try to find filename from $hash
3120
3121 # read filtered raw output
3122 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
3123 or die_error(undef, "Open git-diff-tree failed");
3124 @difftree =
3125 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3126 # $hash == to_id
3127 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3128 map { chomp; $_ } <$fd>;
3129 close $fd
3130 or die_error(undef, "Reading git-diff-tree failed");
3131 @difftree
3132 or die_error('404 Not Found', "Blob diff not found");
3133
3134 } else {
3135 die_error('404 Not Found', "Missing one of the blob diff parameters");
3136 }
3137
3138 if (@difftree > 1) {
3139 die_error('404 Not Found', "Ambiguous blob diff specification");
3140 }
3141
3142 %diffinfo = parse_difftree_raw_line($difftree[0]);
3143 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3144 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3145
3146 $hash_parent ||= $diffinfo{'from_id'};
3147 $hash ||= $diffinfo{'to_id'};
3148
3149 # non-textual hash id's can be cached
3150 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3151 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3152 $expires = '+1d';
3153 }
3154
3155 # open patch output
3156 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3157 '-p', $hash_parent_base, $hash_base,
3158 "--", $file_name
3159 or die_error(undef, "Open git-diff-tree failed");
3160 }
3161
3162 # old/legacy style URI
3163 if (!%diffinfo && # if new style URI failed
3164 defined $hash && defined $hash_parent) {
3165 # fake git-diff-tree raw output
3166 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3167 $diffinfo{'from_id'} = $hash_parent;
3168 $diffinfo{'to_id'} = $hash;
3169 if (defined $file_name) {
3170 if (defined $file_parent) {
3171 $diffinfo{'status'} = '2';
3172 $diffinfo{'from_file'} = $file_parent;
3173 $diffinfo{'to_file'} = $file_name;
3174 } else { # assume not renamed
3175 $diffinfo{'status'} = '1';
3176 $diffinfo{'from_file'} = $file_name;
3177 $diffinfo{'to_file'} = $file_name;
3178 }
3179 } else { # no filename given
3180 $diffinfo{'status'} = '2';
3181 $diffinfo{'from_file'} = $hash_parent;
3182 $diffinfo{'to_file'} = $hash;
3183 }
3184
3185 # non-textual hash id's can be cached
3186 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3187 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3188 $expires = '+1d';
3189 }
3190
3191 # open patch output
3192 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
3193 or die_error(undef, "Open git-diff failed");
3194 } else {
3195 die_error('404 Not Found', "Missing one of the blob diff parameters")
3196 unless %diffinfo;
3197 }
3198
3199 # header
3200 if ($format eq 'html') {
3201 my $formats_nav =
3202 $cgi->a({-href => href(action=>"blobdiff_plain",
3203 hash=>$hash, hash_parent=>$hash_parent,
3204 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3205 file_name=>$file_name, file_parent=>$file_parent)},
3206 "raw");
3207 git_header_html(undef, $expires);
3208 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3209 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3210 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3211 } else {
3212 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3213 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3214 }
3215 if (defined $file_name) {
3216 git_print_page_path($file_name, "blob", $hash_base);
3217 } else {
3218 print "<div class=\"page_path\"></div>\n";
3219 }
3220
3221 } elsif ($format eq 'plain') {
3222 print $cgi->header(
3223 -type => 'text/plain',
3224 -charset => 'utf-8',
3225 -expires => $expires,
3226 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3227
3228 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3229
3230 } else {
3231 die_error(undef, "Unknown blobdiff format");
3232 }
3233
3234 # patch
3235 if ($format eq 'html') {
3236 print "<div class=\"page_body\">\n";
3237
3238 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3239 close $fd;
3240
3241 print "</div>\n"; # class="page_body"
3242 git_footer_html();
3243
3244 } else {
3245 while (my $line = <$fd>) {
3246 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3247 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3248
3249 print $line;
3250
3251 last if $line =~ m!^\+\+\+!;
3252 }
3253 local $/ = undef;
3254 print <$fd>;
3255 close $fd;
3256 }
3257}
3258
3259sub git_blobdiff_plain {
3260 git_blobdiff('plain');
3261}
3262
3263sub git_commitdiff {
3264 my $format = shift || 'html';
3265 my %co = parse_commit($hash);
3266 if (!%co) {
3267 die_error(undef, "Unknown commit object");
3268 }
3269 if (!defined $hash_parent) {
3270 $hash_parent = $co{'parent'} || '--root';
3271 }
3272
3273 # read commitdiff
3274 my $fd;
3275 my @difftree;
3276 if ($format eq 'html') {
3277 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3278 "--patch-with-raw", "--full-index", $hash_parent, $hash
3279 or die_error(undef, "Open git-diff-tree failed");
3280
3281 while (chomp(my $line = <$fd>)) {
3282 # empty line ends raw part of diff-tree output
3283 last unless $line;
3284 push @difftree, $line;
3285 }
3286
3287 } elsif ($format eq 'plain') {
3288 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3289 '-p', $hash_parent, $hash
3290 or die_error(undef, "Open git-diff-tree failed");
3291
3292 } else {
3293 die_error(undef, "Unknown commitdiff format");
3294 }
3295
3296 # non-textual hash id's can be cached
3297 my $expires;
3298 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3299 $expires = "+1d";
3300 }
3301
3302 # write commit message
3303 if ($format eq 'html') {
3304 my $refs = git_get_references();
3305 my $ref = format_ref_marker($refs, $co{'id'});
3306 my $formats_nav =
3307 $cgi->a({-href => href(action=>"commitdiff_plain",
3308 hash=>$hash, hash_parent=>$hash_parent)},
3309 "raw");
3310
3311 git_header_html(undef, $expires);
3312 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3313 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3314 git_print_authorship(\%co);
3315 print "<div class=\"page_body\">\n";
3316 print "<div class=\"log\">\n";
3317 git_print_simplified_log($co{'comment'}, 1); # skip title
3318 print "</div>\n"; # class="log"
3319
3320 } elsif ($format eq 'plain') {
3321 my $refs = git_get_references("tags");
3322 my $tagname = git_get_rev_name_tags($hash);
3323 my $filename = basename($project) . "-$hash.patch";
3324
3325 print $cgi->header(
3326 -type => 'text/plain',
3327 -charset => 'utf-8',
3328 -expires => $expires,
3329 -content_disposition => 'inline; filename="' . "$filename" . '"');
3330 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3331 print <<TEXT;
3332From: $co{'author'}
3333Date: $ad{'rfc2822'} ($ad{'tz_local'})
3334Subject: $co{'title'}
3335TEXT
3336 print "X-Git-Tag: $tagname\n" if $tagname;
3337 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3338
3339 foreach my $line (@{$co{'comment'}}) {
3340 print "$line\n";
3341 }
3342 print "---\n\n";
3343 }
3344
3345 # write patch
3346 if ($format eq 'html') {
3347 git_difftree_body(\@difftree, $hash, $hash_parent);
3348 print "<br/>\n";
3349
3350 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3351 close $fd;
3352 print "</div>\n"; # class="page_body"
3353 git_footer_html();
3354
3355 } elsif ($format eq 'plain') {
3356 local $/ = undef;
3357 print <$fd>;
3358 close $fd
3359 or print "Reading git-diff-tree failed\n";
3360 }
3361}
3362
3363sub git_commitdiff_plain {
3364 git_commitdiff('plain');
3365}
3366
3367sub git_history {
3368 if (!defined $hash_base) {
3369 $hash_base = git_get_head_hash($project);
3370 }
3371 if (!defined $page) {
3372 $page = 0;
3373 }
3374 my $ftype;
3375 my %co = parse_commit($hash_base);
3376 if (!%co) {
3377 die_error(undef, "Unknown commit object");
3378 }
3379
3380 my $refs = git_get_references();
3381 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3382
3383 if (!defined $hash && defined $file_name) {
3384 $hash = git_get_hash_by_path($hash_base, $file_name);
3385 }
3386 if (defined $hash) {
3387 $ftype = git_get_type($hash);
3388 }
3389
3390 open my $fd, "-|",
3391 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3392 or die_error(undef, "Open git-rev-list-failed");
3393 my @revlist = map { chomp; $_ } <$fd>;
3394 close $fd
3395 or die_error(undef, "Reading git-rev-list failed");
3396
3397 my $paging_nav = '';
3398 if ($page > 0) {
3399 $paging_nav .=
3400 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3401 file_name=>$file_name)},
3402 "first");
3403 $paging_nav .= " ⋅ " .
3404 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3405 file_name=>$file_name, page=>$page-1),
3406 -accesskey => "p", -title => "Alt-p"}, "prev");
3407 } else {
3408 $paging_nav .= "first";
3409 $paging_nav .= " ⋅ prev";
3410 }
3411 if ($#revlist >= (100 * ($page+1)-1)) {
3412 $paging_nav .= " ⋅ " .
3413 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3414 file_name=>$file_name, page=>$page+1),
3415 -accesskey => "n", -title => "Alt-n"}, "next");
3416 } else {
3417 $paging_nav .= " ⋅ next";
3418 }
3419 my $next_link = '';
3420 if ($#revlist >= (100 * ($page+1)-1)) {
3421 $next_link =
3422 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3423 file_name=>$file_name, page=>$page+1),
3424 -title => "Alt-n"}, "next");
3425 }
3426
3427 git_header_html();
3428 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3429 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3430 git_print_page_path($file_name, $ftype, $hash_base);
3431
3432 git_history_body(\@revlist, ($page * 100), $#revlist,
3433 $refs, $hash_base, $ftype, $next_link);
3434
3435 git_footer_html();
3436}
3437
3438sub git_search {
3439 if (!defined $searchtext) {
3440 die_error(undef, "Text field empty");
3441 }
3442 if (!defined $hash) {
3443 $hash = git_get_head_hash($project);
3444 }
3445 my %co = parse_commit($hash);
3446 if (!%co) {
3447 die_error(undef, "Unknown commit object");
3448 }
3449
3450 my $commit_search = 1;
3451 my $author_search = 0;
3452 my $committer_search = 0;
3453 my $pickaxe_search = 0;
3454 if ($searchtext =~ s/^author\\://i) {
3455 $author_search = 1;
3456 } elsif ($searchtext =~ s/^committer\\://i) {
3457 $committer_search = 1;
3458 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3459 $commit_search = 0;
3460 $pickaxe_search = 1;
3461
3462 # pickaxe may take all resources of your box and run for several minutes
3463 # with every query - so decide by yourself how public you make this feature
3464 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3465 if (!$have_pickaxe) {
3466 die_error('403 Permission denied', "Permission denied");
3467 }
3468 }
3469 git_header_html();
3470 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3471 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3472
3473 print "<table cellspacing=\"0\">\n";
3474 my $alternate = 1;
3475 if ($commit_search) {
3476 $/ = "\0";
3477 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3478 while (my $commit_text = <$fd>) {
3479 if (!grep m/$searchtext/i, $commit_text) {
3480 next;
3481 }
3482 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3483 next;
3484 }
3485 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3486 next;
3487 }
3488 my @commit_lines = split "\n", $commit_text;
3489 my %co = parse_commit(undef, \@commit_lines);
3490 if (!%co) {
3491 next;
3492 }
3493 if ($alternate) {
3494 print "<tr class=\"dark\">\n";
3495 } else {
3496 print "<tr class=\"light\">\n";
3497 }
3498 $alternate ^= 1;
3499 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3500 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3501 "<td>" .
3502 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3503 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3504 my $comment = $co{'comment'};
3505 foreach my $line (@$comment) {
3506 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3507 my $lead = esc_html($1) || "";
3508 $lead = chop_str($lead, 30, 10);
3509 my $match = esc_html($2) || "";
3510 my $trail = esc_html($3) || "";
3511 $trail = chop_str($trail, 30, 10);
3512 my $text = "$lead<span class=\"match\">$match</span>$trail";
3513 print chop_str($text, 80, 5) . "<br/>\n";
3514 }
3515 }
3516 print "</td>\n" .
3517 "<td class=\"link\">" .
3518 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3519 " | " .
3520 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3521 print "</td>\n" .
3522 "</tr>\n";
3523 }
3524 close $fd;
3525 }
3526
3527 if ($pickaxe_search) {
3528 $/ = "\n";
3529 my $git_command = git_cmd_str();
3530 open my $fd, "-|", "$git_command rev-list $hash | " .
3531 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3532 undef %co;
3533 my @files;
3534 while (my $line = <$fd>) {
3535 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3536 my %set;
3537 $set{'file'} = $6;
3538 $set{'from_id'} = $3;
3539 $set{'to_id'} = $4;
3540 $set{'id'} = $set{'to_id'};
3541 if ($set{'id'} =~ m/0{40}/) {
3542 $set{'id'} = $set{'from_id'};
3543 }
3544 if ($set{'id'} =~ m/0{40}/) {
3545 next;
3546 }
3547 push @files, \%set;
3548 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3549 if (%co) {
3550 if ($alternate) {
3551 print "<tr class=\"dark\">\n";
3552 } else {
3553 print "<tr class=\"light\">\n";
3554 }
3555 $alternate ^= 1;
3556 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3557 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3558 "<td>" .
3559 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3560 -class => "list subject"},
3561 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3562 while (my $setref = shift @files) {
3563 my %set = %$setref;
3564 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3565 hash=>$set{'id'}, file_name=>$set{'file'}),
3566 -class => "list"},
3567 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3568 "<br/>\n";
3569 }
3570 print "</td>\n" .
3571 "<td class=\"link\">" .
3572 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3573 " | " .
3574 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3575 print "</td>\n" .
3576 "</tr>\n";
3577 }
3578 %co = parse_commit($1);
3579 }
3580 }
3581 close $fd;
3582 }
3583 print "</table>\n";
3584 git_footer_html();
3585}
3586
3587sub git_shortlog {
3588 my $head = git_get_head_hash($project);
3589 if (!defined $hash) {
3590 $hash = $head;
3591 }
3592 if (!defined $page) {
3593 $page = 0;
3594 }
3595 my $refs = git_get_references();
3596
3597 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3598 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3599 or die_error(undef, "Open git-rev-list failed");
3600 my @revlist = map { chomp; $_ } <$fd>;
3601 close $fd;
3602
3603 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3604 my $next_link = '';
3605 if ($#revlist >= (100 * ($page+1)-1)) {
3606 $next_link =
3607 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3608 -title => "Alt-n"}, "next");
3609 }
3610
3611
3612 git_header_html();
3613 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3614 git_print_header_div('summary', $project);
3615
3616 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3617
3618 git_footer_html();
3619}
3620
3621## ......................................................................
3622## feeds (RSS, OPML)
3623
3624sub git_rss {
3625 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3626 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3627 or die_error(undef, "Open git-rev-list failed");
3628 my @revlist = map { chomp; $_ } <$fd>;
3629 close $fd or die_error(undef, "Reading git-rev-list failed");
3630 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3631 print <<XML;
3632<?xml version="1.0" encoding="utf-8"?>
3633<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3634<channel>
3635<title>$project $my_uri $my_url</title>
3636<link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3637<description>$project log</description>
3638<language>en</language>
3639XML
3640
3641 for (my $i = 0; $i <= $#revlist; $i++) {
3642 my $commit = $revlist[$i];
3643 my %co = parse_commit($commit);
3644 # we read 150, we always show 30 and the ones more recent than 48 hours
3645 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3646 last;
3647 }
3648 my %cd = parse_date($co{'committer_epoch'});
3649 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3650 $co{'parent'}, $co{'id'}
3651 or next;
3652 my @difftree = map { chomp; $_ } <$fd>;
3653 close $fd
3654 or next;
3655 print "<item>\n" .
3656 "<title>" .
3657 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3658 "</title>\n" .
3659 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3660 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3661 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3662 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3663 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3664 "<content:encoded>" .
3665 "<![CDATA[\n";
3666 my $comment = $co{'comment'};
3667 foreach my $line (@$comment) {
3668 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3669 print "$line<br/>\n";
3670 }
3671 print "<br/>\n";
3672 foreach my $line (@difftree) {
3673 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3674 next;
3675 }
3676 my $file = esc_html(unquote($7));
3677 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3678 print "$file<br/>\n";
3679 }
3680 print "]]>\n" .
3681 "</content:encoded>\n" .
3682 "</item>\n";
3683 }
3684 print "</channel></rss>";
3685}
3686
3687sub git_opml {
3688 my @list = git_get_projects_list();
3689
3690 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3691 print <<XML;
3692<?xml version="1.0" encoding="utf-8"?>
3693<opml version="1.0">
3694<head>
3695 <title>$site_name Git OPML Export</title>
3696</head>
3697<body>
3698<outline text="git RSS feeds">
3699XML
3700
3701 foreach my $pr (@list) {
3702 my %proj = %$pr;
3703 my $head = git_get_head_hash($proj{'path'});
3704 if (!defined $head) {
3705 next;
3706 }
3707 $git_dir = "$projectroot/$proj{'path'}";
3708 my %co = parse_commit($head);
3709 if (!%co) {
3710 next;
3711 }
3712
3713 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3714 my $rss = "$my_url?p=$proj{'path'};a=rss";
3715 my $html = "$my_url?p=$proj{'path'};a=summary";
3716 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3717 }
3718 print <<XML;
3719</outline>
3720</body>
3721</opml>
3722XML
3723}