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