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