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