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