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