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