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