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