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