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