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