]> Lady’s Gitweb - Gitweb/blob - gitweb.perl
Update the gitweb/README file to include setting the GITWEB_CONFIG environment
[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_blame_flush_chunk {
2468 my ($name, $revdata, $color, $rev, @line) = @_;
2469 my $label = substr($rev, 0, 8);
2470 my $line = scalar(@line);
2471 my $cnt = 0;
2472 my $pop = '';
2473
2474 if ($revdata->{$rev} ne '') {
2475 $pop = ' title="' . esc_html($revdata->{$rev}) . '"';
2476 }
2477
2478 for (@line) {
2479 my ($lineno, $data) = @$_;
2480 $cnt++;
2481 print "<tr class=\"$color\">\n";
2482 if ($cnt == 1) {
2483 print "<td class=\"sha1\"$pop";
2484 if ($line > 1) {
2485 print " rowspan=\"$line\"";
2486 }
2487 print ">";
2488 print $cgi->a({-href => href(action=>"commit",
2489 hash=>$rev,
2490 file_name=>$name)},
2491 $label);
2492 print "</td>\n";
2493 }
2494 print "<td class=\"linenr\">".
2495 "<a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2496 esc_html($lineno) . "</a></td>\n";
2497 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2498 print "</tr>\n";
2499 }
2500 }
2501
2502 # We can have up to N*2 lines. If it is more than N lines, split it
2503 # into two to avoid orphans.
2504 sub git_blame_flush_chunk_1 {
2505 my ($chunk_cap, $name, $revdata, $color, $rev, @chunk) = @_;
2506 if ($chunk_cap < @chunk) {
2507 my @first = splice(@chunk, 0, @chunk/2);
2508 git_blame_flush_chunk($name,
2509 $revdata,
2510 $color,
2511 $rev,
2512 @first);
2513 }
2514 git_blame_flush_chunk($name,
2515 $revdata,
2516 $color,
2517 $rev,
2518 @chunk);
2519 }
2520
2521 sub git_blame2 {
2522 my $fd;
2523 my $ftype;
2524 my $chunk_cap = 20;
2525
2526 my ($have_blame) = gitweb_check_feature('blame');
2527 if (!$have_blame) {
2528 die_error('403 Permission denied', "Permission denied");
2529 }
2530 die_error('404 Not Found', "File name not defined") if (!$file_name);
2531 $hash_base ||= git_get_head_hash($project);
2532 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2533 my %co = parse_commit($hash_base)
2534 or die_error(undef, "Reading commit failed");
2535 if (!defined $hash) {
2536 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2537 or die_error(undef, "Error looking up file");
2538 }
2539 $ftype = git_get_type($hash);
2540 if ($ftype !~ "blob") {
2541 die_error("400 Bad Request", "Object is not a blob");
2542 }
2543 open ($fd, "-|", git_cmd(), "blame", '-l', '--', $file_name, $hash_base)
2544 or die_error(undef, "Open git-blame failed");
2545 git_header_html();
2546 my $formats_nav =
2547 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2548 "blob") .
2549 " | " .
2550 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2551 "history") .
2552 " | " .
2553 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2554 "HEAD");
2555 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2556 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2557 git_print_page_path($file_name, $ftype, $hash_base);
2558 my @rev_color = (qw(light2 dark2));
2559 my $num_colors = scalar(@rev_color);
2560 my $current_color = 0;
2561 my $last_rev;
2562 print <<HTML;
2563 <div class="page_body">
2564 <table class="blame">
2565 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2566 HTML
2567 my @chunk = ();
2568 my %revdata = ();
2569 while (<$fd>) {
2570 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2571 my ($full_rev, $author, $date, $lineno, $data) =
2572 /^([0-9a-f]{40}).*?\s\((.*?)\s+([-\d]+ [:\d]+ [-+\d]+)\s+(\d+)\)\s(.*)/;
2573 if (!exists $revdata{$full_rev}) {
2574 $revdata{$full_rev} = "$author, $date";
2575 }
2576 if (!defined $last_rev) {
2577 $last_rev = $full_rev;
2578 } elsif ($last_rev ne $full_rev) {
2579 git_blame_flush_chunk_1($chunk_cap,
2580 $file_name,
2581 \%revdata,
2582 $rev_color[$current_color],
2583 $last_rev, @chunk);
2584 @chunk = ();
2585 $last_rev = $full_rev;
2586 $current_color = ++$current_color % $num_colors;
2587 }
2588 elsif ($chunk_cap * 2 < @chunk) {
2589 # We have more than N*2 lines from the same
2590 # revision. Flush N lines and leave N lines
2591 # in @chunk to avoid orphaned lines.
2592 my @first = splice(@chunk, 0, $chunk_cap);
2593 git_blame_flush_chunk($file_name,
2594 \%revdata,
2595 $rev_color[$current_color],
2596 $last_rev, @first);
2597 }
2598 push @chunk, [$lineno, $data];
2599 }
2600 if (@chunk) {
2601 git_blame_flush_chunk_1($chunk_cap,
2602 $file_name,
2603 \%revdata,
2604 $rev_color[$current_color],
2605 $last_rev, @chunk);
2606 }
2607 print "</table>\n";
2608 print "</div>";
2609 close $fd
2610 or print "Reading blob failed\n";
2611 git_footer_html();
2612 }
2613
2614 sub git_blame {
2615 my $fd;
2616
2617 my ($have_blame) = gitweb_check_feature('blame');
2618 if (!$have_blame) {
2619 die_error('403 Permission denied', "Permission denied");
2620 }
2621 die_error('404 Not Found', "File name not defined") if (!$file_name);
2622 $hash_base ||= git_get_head_hash($project);
2623 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2624 my %co = parse_commit($hash_base)
2625 or die_error(undef, "Reading commit failed");
2626 if (!defined $hash) {
2627 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2628 or die_error(undef, "Error lookup file");
2629 }
2630 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2631 or die_error(undef, "Open git-annotate failed");
2632 git_header_html();
2633 my $formats_nav =
2634 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2635 "blob") .
2636 " | " .
2637 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2638 "history") .
2639 " | " .
2640 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2641 "HEAD");
2642 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2643 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2644 git_print_page_path($file_name, 'blob', $hash_base);
2645 print "<div class=\"page_body\">\n";
2646 print <<HTML;
2647 <table class="blame">
2648 <tr>
2649 <th>Commit</th>
2650 <th>Age</th>
2651 <th>Author</th>
2652 <th>Line</th>
2653 <th>Data</th>
2654 </tr>
2655 HTML
2656 my @line_class = (qw(light dark));
2657 my $line_class_len = scalar (@line_class);
2658 my $line_class_num = $#line_class;
2659 while (my $line = <$fd>) {
2660 my $long_rev;
2661 my $short_rev;
2662 my $author;
2663 my $time;
2664 my $lineno;
2665 my $data;
2666 my $age;
2667 my $age_str;
2668 my $age_class;
2669
2670 chomp $line;
2671 $line_class_num = ($line_class_num + 1) % $line_class_len;
2672
2673 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2674 $long_rev = $1;
2675 $author = $2;
2676 $time = $3;
2677 $lineno = $4;
2678 $data = $5;
2679 } else {
2680 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2681 next;
2682 }
2683 $short_rev = substr ($long_rev, 0, 8);
2684 $age = time () - $time;
2685 $age_str = age_string ($age);
2686 $age_str =~ s/ /&nbsp;/g;
2687 $age_class = age_class($age);
2688 $author = esc_html ($author);
2689 $author =~ s/ /&nbsp;/g;
2690
2691 $data = untabify($data);
2692 $data = esc_html ($data);
2693
2694 print <<HTML;
2695 <tr class="$line_class[$line_class_num]">
2696 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2697 <td class="$age_class">$age_str</td>
2698 <td>$author</td>
2699 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2700 <td class="pre">$data</td>
2701 </tr>
2702 HTML
2703 } # while (my $line = <$fd>)
2704 print "</table>\n\n";
2705 close $fd
2706 or print "Reading blob failed.\n";
2707 print "</div>";
2708 git_footer_html();
2709 }
2710
2711 sub git_tags {
2712 my $head = git_get_head_hash($project);
2713 git_header_html();
2714 git_print_page_nav('','', $head,undef,$head);
2715 git_print_header_div('summary', $project);
2716
2717 my ($taglist) = git_get_refs_list("tags");
2718 if (@$taglist) {
2719 git_tags_body($taglist);
2720 }
2721 git_footer_html();
2722 }
2723
2724 sub git_heads {
2725 my $head = git_get_head_hash($project);
2726 git_header_html();
2727 git_print_page_nav('','', $head,undef,$head);
2728 git_print_header_div('summary', $project);
2729
2730 my ($headlist) = git_get_refs_list("heads");
2731 if (@$headlist) {
2732 git_heads_body($headlist, $head);
2733 }
2734 git_footer_html();
2735 }
2736
2737 sub git_blob_plain {
2738 my $expires;
2739
2740 if (!defined $hash) {
2741 if (defined $file_name) {
2742 my $base = $hash_base || git_get_head_hash($project);
2743 $hash = git_get_hash_by_path($base, $file_name, "blob")
2744 or die_error(undef, "Error lookup file");
2745 } else {
2746 die_error(undef, "No file name defined");
2747 }
2748 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2749 # blobs defined by non-textual hash id's can be cached
2750 $expires = "+1d";
2751 }
2752
2753 my $type = shift;
2754 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2755 or die_error(undef, "Couldn't cat $file_name, $hash");
2756
2757 $type ||= blob_mimetype($fd, $file_name);
2758
2759 # save as filename, even when no $file_name is given
2760 my $save_as = "$hash";
2761 if (defined $file_name) {
2762 $save_as = $file_name;
2763 } elsif ($type =~ m/^text\//) {
2764 $save_as .= '.txt';
2765 }
2766
2767 print $cgi->header(
2768 -type => "$type",
2769 -expires=>$expires,
2770 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2771 undef $/;
2772 binmode STDOUT, ':raw';
2773 print <$fd>;
2774 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2775 $/ = "\n";
2776 close $fd;
2777 }
2778
2779 sub git_blob {
2780 my $expires;
2781
2782 if (!defined $hash) {
2783 if (defined $file_name) {
2784 my $base = $hash_base || git_get_head_hash($project);
2785 $hash = git_get_hash_by_path($base, $file_name, "blob")
2786 or die_error(undef, "Error lookup file");
2787 } else {
2788 die_error(undef, "No file name defined");
2789 }
2790 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2791 # blobs defined by non-textual hash id's can be cached
2792 $expires = "+1d";
2793 }
2794
2795 my ($have_blame) = gitweb_check_feature('blame');
2796 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2797 or die_error(undef, "Couldn't cat $file_name, $hash");
2798 my $mimetype = blob_mimetype($fd, $file_name);
2799 if ($mimetype !~ m/^text\//) {
2800 close $fd;
2801 return git_blob_plain($mimetype);
2802 }
2803 git_header_html(undef, $expires);
2804 my $formats_nav = '';
2805 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2806 if (defined $file_name) {
2807 if ($have_blame) {
2808 $formats_nav .=
2809 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2810 hash=>$hash, file_name=>$file_name)},
2811 "blame") .
2812 " | ";
2813 }
2814 $formats_nav .=
2815 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2816 hash=>$hash, file_name=>$file_name)},
2817 "history") .
2818 " | " .
2819 $cgi->a({-href => href(action=>"blob_plain",
2820 hash=>$hash, file_name=>$file_name)},
2821 "raw") .
2822 " | " .
2823 $cgi->a({-href => href(action=>"blob",
2824 hash_base=>"HEAD", file_name=>$file_name)},
2825 "HEAD");
2826 } else {
2827 $formats_nav .=
2828 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
2829 }
2830 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2831 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2832 } else {
2833 print "<div class=\"page_nav\">\n" .
2834 "<br/><br/></div>\n" .
2835 "<div class=\"title\">$hash</div>\n";
2836 }
2837 git_print_page_path($file_name, "blob", $hash_base);
2838 print "<div class=\"page_body\">\n";
2839 my $nr;
2840 while (my $line = <$fd>) {
2841 chomp $line;
2842 $nr++;
2843 $line = untabify($line);
2844 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2845 $nr, $nr, $nr, esc_html($line);
2846 }
2847 close $fd
2848 or print "Reading blob failed.\n";
2849 print "</div>";
2850 git_footer_html();
2851 }
2852
2853 sub git_tree {
2854 my $have_snapshot = gitweb_have_snapshot();
2855
2856 if (!defined $hash_base) {
2857 $hash_base = "HEAD";
2858 }
2859 if (!defined $hash) {
2860 if (defined $file_name) {
2861 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
2862 } else {
2863 $hash = $hash_base;
2864 }
2865 }
2866 $/ = "\0";
2867 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2868 or die_error(undef, "Open git-ls-tree failed");
2869 my @entries = map { chomp; $_ } <$fd>;
2870 close $fd or die_error(undef, "Reading tree failed");
2871 $/ = "\n";
2872
2873 my $refs = git_get_references();
2874 my $ref = format_ref_marker($refs, $hash_base);
2875 git_header_html();
2876 my $base = "";
2877 my ($have_blame) = gitweb_check_feature('blame');
2878 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2879 my @views_nav = ();
2880 if (defined $file_name) {
2881 push @views_nav,
2882 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2883 hash=>$hash, file_name=>$file_name)},
2884 "history"),
2885 $cgi->a({-href => href(action=>"tree",
2886 hash_base=>"HEAD", file_name=>$file_name)},
2887 "HEAD"),
2888 }
2889 if ($have_snapshot) {
2890 # FIXME: Should be available when we have no hash base as well.
2891 push @views_nav,
2892 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
2893 "snapshot");
2894 }
2895 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
2896 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2897 } else {
2898 undef $hash_base;
2899 print "<div class=\"page_nav\">\n";
2900 print "<br/><br/></div>\n";
2901 print "<div class=\"title\">$hash</div>\n";
2902 }
2903 if (defined $file_name) {
2904 $base = esc_html("$file_name/");
2905 }
2906 git_print_page_path($file_name, 'tree', $hash_base);
2907 print "<div class=\"page_body\">\n";
2908 print "<table cellspacing=\"0\">\n";
2909 my $alternate = 1;
2910 foreach my $line (@entries) {
2911 my %t = parse_ls_tree_line($line, -z => 1);
2912
2913 if ($alternate) {
2914 print "<tr class=\"dark\">\n";
2915 } else {
2916 print "<tr class=\"light\">\n";
2917 }
2918 $alternate ^= 1;
2919
2920 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2921
2922 print "</tr>\n";
2923 }
2924 print "</table>\n" .
2925 "</div>";
2926 git_footer_html();
2927 }
2928
2929 sub git_snapshot {
2930 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2931 my $have_snapshot = (defined $ctype && defined $suffix);
2932 if (!$have_snapshot) {
2933 die_error('403 Permission denied', "Permission denied");
2934 }
2935
2936 if (!defined $hash) {
2937 $hash = git_get_head_hash($project);
2938 }
2939
2940 my $filename = basename($project) . "-$hash.tar.$suffix";
2941
2942 print $cgi->header(
2943 -type => 'application/x-tar',
2944 -content_encoding => $ctype,
2945 -content_disposition => 'inline; filename="' . "$filename" . '"',
2946 -status => '200 OK');
2947
2948 my $git_command = git_cmd_str();
2949 open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
2950 die_error(undef, "Execute git-tar-tree failed.");
2951 binmode STDOUT, ':raw';
2952 print <$fd>;
2953 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2954 close $fd;
2955
2956 }
2957
2958 sub git_log {
2959 my $head = git_get_head_hash($project);
2960 if (!defined $hash) {
2961 $hash = $head;
2962 }
2963 if (!defined $page) {
2964 $page = 0;
2965 }
2966 my $refs = git_get_references();
2967
2968 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2969 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
2970 or die_error(undef, "Open git-rev-list failed");
2971 my @revlist = map { chomp; $_ } <$fd>;
2972 close $fd;
2973
2974 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2975
2976 git_header_html();
2977 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2978
2979 if (!@revlist) {
2980 my %co = parse_commit($hash);
2981
2982 git_print_header_div('summary', $project);
2983 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2984 }
2985 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2986 my $commit = $revlist[$i];
2987 my $ref = format_ref_marker($refs, $commit);
2988 my %co = parse_commit($commit);
2989 next if !%co;
2990 my %ad = parse_date($co{'author_epoch'});
2991 git_print_header_div('commit',
2992 "<span class=\"age\">$co{'age_string'}</span>" .
2993 esc_html($co{'title'}) . $ref,
2994 $commit);
2995 print "<div class=\"title_text\">\n" .
2996 "<div class=\"log_link\">\n" .
2997 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2998 " | " .
2999 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
3000 " | " .
3001 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
3002 "<br/>\n" .
3003 "</div>\n" .
3004 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
3005 "</div>\n";
3006
3007 print "<div class=\"log_body\">\n";
3008 git_print_simplified_log($co{'comment'});
3009 print "</div>\n";
3010 }
3011 git_footer_html();
3012 }
3013
3014 sub git_commit {
3015 my %co = parse_commit($hash);
3016 if (!%co) {
3017 die_error(undef, "Unknown commit object");
3018 }
3019 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3020 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
3021
3022 my $parent = $co{'parent'};
3023 if (!defined $parent) {
3024 $parent = "--root";
3025 }
3026 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
3027 or die_error(undef, "Open git-diff-tree failed");
3028 my @difftree = map { chomp; $_ } <$fd>;
3029 close $fd or die_error(undef, "Reading git-diff-tree failed");
3030
3031 # non-textual hash id's can be cached
3032 my $expires;
3033 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3034 $expires = "+1d";
3035 }
3036 my $refs = git_get_references();
3037 my $ref = format_ref_marker($refs, $co{'id'});
3038
3039 my $have_snapshot = gitweb_have_snapshot();
3040
3041 my @views_nav = ();
3042 if (defined $file_name && defined $co{'parent'}) {
3043 push @views_nav,
3044 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
3045 "blame");
3046 }
3047 if (defined $co{'parent'}) {
3048 push @views_nav,
3049 $cgi->a({-href => href(action=>"shortlog", hash=>$hash)}, "shortlog"),
3050 $cgi->a({-href => href(action=>"log", hash=>$hash)}, "log");
3051 }
3052 git_header_html(undef, $expires);
3053 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
3054 $hash, $co{'tree'}, $hash,
3055 join (' | ', @views_nav));
3056
3057 if (defined $co{'parent'}) {
3058 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3059 } else {
3060 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3061 }
3062 print "<div class=\"title_text\">\n" .
3063 "<table cellspacing=\"0\">\n";
3064 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3065 "<tr>" .
3066 "<td></td><td> $ad{'rfc2822'}";
3067 if ($ad{'hour_local'} < 6) {
3068 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3069 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3070 } else {
3071 printf(" (%02d:%02d %s)",
3072 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3073 }
3074 print "</td>" .
3075 "</tr>\n";
3076 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3077 print "<tr><td></td><td> $cd{'rfc2822'}" .
3078 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3079 "</td></tr>\n";
3080 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3081 print "<tr>" .
3082 "<td>tree</td>" .
3083 "<td class=\"sha1\">" .
3084 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3085 class => "list"}, $co{'tree'}) .
3086 "</td>" .
3087 "<td class=\"link\">" .
3088 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3089 "tree");
3090 if ($have_snapshot) {
3091 print " | " .
3092 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3093 }
3094 print "</td>" .
3095 "</tr>\n";
3096 my $parents = $co{'parents'};
3097 foreach my $par (@$parents) {
3098 print "<tr>" .
3099 "<td>parent</td>" .
3100 "<td class=\"sha1\">" .
3101 $cgi->a({-href => href(action=>"commit", hash=>$par),
3102 class => "list"}, $par) .
3103 "</td>" .
3104 "<td class=\"link\">" .
3105 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3106 " | " .
3107 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3108 "</td>" .
3109 "</tr>\n";
3110 }
3111 print "</table>".
3112 "</div>\n";
3113
3114 print "<div class=\"page_body\">\n";
3115 git_print_log($co{'comment'});
3116 print "</div>\n";
3117
3118 git_difftree_body(\@difftree, $hash, $parent);
3119
3120 git_footer_html();
3121 }
3122
3123 sub git_blobdiff {
3124 my $format = shift || 'html';
3125
3126 my $fd;
3127 my @difftree;
3128 my %diffinfo;
3129 my $expires;
3130
3131 # preparing $fd and %diffinfo for git_patchset_body
3132 # new style URI
3133 if (defined $hash_base && defined $hash_parent_base) {
3134 if (defined $file_name) {
3135 # read raw output
3136 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
3137 "--", $file_name
3138 or die_error(undef, "Open git-diff-tree failed");
3139 @difftree = map { chomp; $_ } <$fd>;
3140 close $fd
3141 or die_error(undef, "Reading git-diff-tree failed");
3142 @difftree
3143 or die_error('404 Not Found', "Blob diff not found");
3144
3145 } elsif (defined $hash &&
3146 $hash =~ /[0-9a-fA-F]{40}/) {
3147 # try to find filename from $hash
3148
3149 # read filtered raw output
3150 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
3151 or die_error(undef, "Open git-diff-tree failed");
3152 @difftree =
3153 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3154 # $hash == to_id
3155 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3156 map { chomp; $_ } <$fd>;
3157 close $fd
3158 or die_error(undef, "Reading git-diff-tree failed");
3159 @difftree
3160 or die_error('404 Not Found', "Blob diff not found");
3161
3162 } else {
3163 die_error('404 Not Found', "Missing one of the blob diff parameters");
3164 }
3165
3166 if (@difftree > 1) {
3167 die_error('404 Not Found', "Ambiguous blob diff specification");
3168 }
3169
3170 %diffinfo = parse_difftree_raw_line($difftree[0]);
3171 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3172 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3173
3174 $hash_parent ||= $diffinfo{'from_id'};
3175 $hash ||= $diffinfo{'to_id'};
3176
3177 # non-textual hash id's can be cached
3178 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3179 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3180 $expires = '+1d';
3181 }
3182
3183 # open patch output
3184 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3185 '-p', $hash_parent_base, $hash_base,
3186 "--", $file_name
3187 or die_error(undef, "Open git-diff-tree failed");
3188 }
3189
3190 # old/legacy style URI
3191 if (!%diffinfo && # if new style URI failed
3192 defined $hash && defined $hash_parent) {
3193 # fake git-diff-tree raw output
3194 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3195 $diffinfo{'from_id'} = $hash_parent;
3196 $diffinfo{'to_id'} = $hash;
3197 if (defined $file_name) {
3198 if (defined $file_parent) {
3199 $diffinfo{'status'} = '2';
3200 $diffinfo{'from_file'} = $file_parent;
3201 $diffinfo{'to_file'} = $file_name;
3202 } else { # assume not renamed
3203 $diffinfo{'status'} = '1';
3204 $diffinfo{'from_file'} = $file_name;
3205 $diffinfo{'to_file'} = $file_name;
3206 }
3207 } else { # no filename given
3208 $diffinfo{'status'} = '2';
3209 $diffinfo{'from_file'} = $hash_parent;
3210 $diffinfo{'to_file'} = $hash;
3211 }
3212
3213 # non-textual hash id's can be cached
3214 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3215 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3216 $expires = '+1d';
3217 }
3218
3219 # open patch output
3220 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
3221 or die_error(undef, "Open git-diff failed");
3222 } else {
3223 die_error('404 Not Found', "Missing one of the blob diff parameters")
3224 unless %diffinfo;
3225 }
3226
3227 # header
3228 if ($format eq 'html') {
3229 my $formats_nav =
3230 $cgi->a({-href => href(action=>"blobdiff_plain",
3231 hash=>$hash, hash_parent=>$hash_parent,
3232 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3233 file_name=>$file_name, file_parent=>$file_parent)},
3234 "raw");
3235 git_header_html(undef, $expires);
3236 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3237 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3238 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3239 } else {
3240 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3241 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3242 }
3243 if (defined $file_name) {
3244 git_print_page_path($file_name, "blob", $hash_base);
3245 } else {
3246 print "<div class=\"page_path\"></div>\n";
3247 }
3248
3249 } elsif ($format eq 'plain') {
3250 print $cgi->header(
3251 -type => 'text/plain',
3252 -charset => 'utf-8',
3253 -expires => $expires,
3254 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3255
3256 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3257
3258 } else {
3259 die_error(undef, "Unknown blobdiff format");
3260 }
3261
3262 # patch
3263 if ($format eq 'html') {
3264 print "<div class=\"page_body\">\n";
3265
3266 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3267 close $fd;
3268
3269 print "</div>\n"; # class="page_body"
3270 git_footer_html();
3271
3272 } else {
3273 while (my $line = <$fd>) {
3274 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3275 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3276
3277 print $line;
3278
3279 last if $line =~ m!^\+\+\+!;
3280 }
3281 local $/ = undef;
3282 print <$fd>;
3283 close $fd;
3284 }
3285 }
3286
3287 sub git_blobdiff_plain {
3288 git_blobdiff('plain');
3289 }
3290
3291 sub git_commitdiff {
3292 my $format = shift || 'html';
3293 my %co = parse_commit($hash);
3294 if (!%co) {
3295 die_error(undef, "Unknown commit object");
3296 }
3297 if (!defined $hash_parent) {
3298 $hash_parent = $co{'parent'} || '--root';
3299 }
3300
3301 # read commitdiff
3302 my $fd;
3303 my @difftree;
3304 if ($format eq 'html') {
3305 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3306 "--patch-with-raw", "--full-index", $hash_parent, $hash
3307 or die_error(undef, "Open git-diff-tree failed");
3308
3309 while (chomp(my $line = <$fd>)) {
3310 # empty line ends raw part of diff-tree output
3311 last unless $line;
3312 push @difftree, $line;
3313 }
3314
3315 } elsif ($format eq 'plain') {
3316 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3317 '-p', $hash_parent, $hash
3318 or die_error(undef, "Open git-diff-tree failed");
3319
3320 } else {
3321 die_error(undef, "Unknown commitdiff format");
3322 }
3323
3324 # non-textual hash id's can be cached
3325 my $expires;
3326 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3327 $expires = "+1d";
3328 }
3329
3330 # write commit message
3331 if ($format eq 'html') {
3332 my $refs = git_get_references();
3333 my $ref = format_ref_marker($refs, $co{'id'});
3334 my $formats_nav =
3335 $cgi->a({-href => href(action=>"commitdiff_plain",
3336 hash=>$hash, hash_parent=>$hash_parent)},
3337 "raw");
3338
3339 git_header_html(undef, $expires);
3340 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3341 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3342 git_print_authorship(\%co);
3343 print "<div class=\"page_body\">\n";
3344 print "<div class=\"log\">\n";
3345 git_print_simplified_log($co{'comment'}, 1); # skip title
3346 print "</div>\n"; # class="log"
3347
3348 } elsif ($format eq 'plain') {
3349 my $refs = git_get_references("tags");
3350 my $tagname = git_get_rev_name_tags($hash);
3351 my $filename = basename($project) . "-$hash.patch";
3352
3353 print $cgi->header(
3354 -type => 'text/plain',
3355 -charset => 'utf-8',
3356 -expires => $expires,
3357 -content_disposition => 'inline; filename="' . "$filename" . '"');
3358 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3359 print <<TEXT;
3360 From: $co{'author'}
3361 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3362 Subject: $co{'title'}
3363 TEXT
3364 print "X-Git-Tag: $tagname\n" if $tagname;
3365 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3366
3367 foreach my $line (@{$co{'comment'}}) {
3368 print "$line\n";
3369 }
3370 print "---\n\n";
3371 }
3372
3373 # write patch
3374 if ($format eq 'html') {
3375 git_difftree_body(\@difftree, $hash, $hash_parent);
3376 print "<br/>\n";
3377
3378 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3379 close $fd;
3380 print "</div>\n"; # class="page_body"
3381 git_footer_html();
3382
3383 } elsif ($format eq 'plain') {
3384 local $/ = undef;
3385 print <$fd>;
3386 close $fd
3387 or print "Reading git-diff-tree failed\n";
3388 }
3389 }
3390
3391 sub git_commitdiff_plain {
3392 git_commitdiff('plain');
3393 }
3394
3395 sub git_history {
3396 if (!defined $hash_base) {
3397 $hash_base = git_get_head_hash($project);
3398 }
3399 if (!defined $page) {
3400 $page = 0;
3401 }
3402 my $ftype;
3403 my %co = parse_commit($hash_base);
3404 if (!%co) {
3405 die_error(undef, "Unknown commit object");
3406 }
3407
3408 my $refs = git_get_references();
3409 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3410
3411 if (!defined $hash && defined $file_name) {
3412 $hash = git_get_hash_by_path($hash_base, $file_name);
3413 }
3414 if (defined $hash) {
3415 $ftype = git_get_type($hash);
3416 }
3417
3418 open my $fd, "-|",
3419 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3420 or die_error(undef, "Open git-rev-list-failed");
3421 my @revlist = map { chomp; $_ } <$fd>;
3422 close $fd
3423 or die_error(undef, "Reading git-rev-list failed");
3424
3425 my $paging_nav = '';
3426 if ($page > 0) {
3427 $paging_nav .=
3428 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3429 file_name=>$file_name)},
3430 "first");
3431 $paging_nav .= " &sdot; " .
3432 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3433 file_name=>$file_name, page=>$page-1),
3434 -accesskey => "p", -title => "Alt-p"}, "prev");
3435 } else {
3436 $paging_nav .= "first";
3437 $paging_nav .= " &sdot; prev";
3438 }
3439 if ($#revlist >= (100 * ($page+1)-1)) {
3440 $paging_nav .= " &sdot; " .
3441 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3442 file_name=>$file_name, page=>$page+1),
3443 -accesskey => "n", -title => "Alt-n"}, "next");
3444 } else {
3445 $paging_nav .= " &sdot; next";
3446 }
3447 my $next_link = '';
3448 if ($#revlist >= (100 * ($page+1)-1)) {
3449 $next_link =
3450 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3451 file_name=>$file_name, page=>$page+1),
3452 -title => "Alt-n"}, "next");
3453 }
3454
3455 git_header_html();
3456 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3457 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3458 git_print_page_path($file_name, $ftype, $hash_base);
3459
3460 git_history_body(\@revlist, ($page * 100), $#revlist,
3461 $refs, $hash_base, $ftype, $next_link);
3462
3463 git_footer_html();
3464 }
3465
3466 sub git_search {
3467 if (!defined $searchtext) {
3468 die_error(undef, "Text field empty");
3469 }
3470 if (!defined $hash) {
3471 $hash = git_get_head_hash($project);
3472 }
3473 my %co = parse_commit($hash);
3474 if (!%co) {
3475 die_error(undef, "Unknown commit object");
3476 }
3477
3478 my $commit_search = 1;
3479 my $author_search = 0;
3480 my $committer_search = 0;
3481 my $pickaxe_search = 0;
3482 if ($searchtext =~ s/^author\\://i) {
3483 $author_search = 1;
3484 } elsif ($searchtext =~ s/^committer\\://i) {
3485 $committer_search = 1;
3486 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3487 $commit_search = 0;
3488 $pickaxe_search = 1;
3489
3490 # pickaxe may take all resources of your box and run for several minutes
3491 # with every query - so decide by yourself how public you make this feature
3492 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3493 if (!$have_pickaxe) {
3494 die_error('403 Permission denied', "Permission denied");
3495 }
3496 }
3497 git_header_html();
3498 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3499 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3500
3501 print "<table cellspacing=\"0\">\n";
3502 my $alternate = 1;
3503 if ($commit_search) {
3504 $/ = "\0";
3505 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3506 while (my $commit_text = <$fd>) {
3507 if (!grep m/$searchtext/i, $commit_text) {
3508 next;
3509 }
3510 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3511 next;
3512 }
3513 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3514 next;
3515 }
3516 my @commit_lines = split "\n", $commit_text;
3517 my %co = parse_commit(undef, \@commit_lines);
3518 if (!%co) {
3519 next;
3520 }
3521 if ($alternate) {
3522 print "<tr class=\"dark\">\n";
3523 } else {
3524 print "<tr class=\"light\">\n";
3525 }
3526 $alternate ^= 1;
3527 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3528 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3529 "<td>" .
3530 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3531 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3532 my $comment = $co{'comment'};
3533 foreach my $line (@$comment) {
3534 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3535 my $lead = esc_html($1) || "";
3536 $lead = chop_str($lead, 30, 10);
3537 my $match = esc_html($2) || "";
3538 my $trail = esc_html($3) || "";
3539 $trail = chop_str($trail, 30, 10);
3540 my $text = "$lead<span class=\"match\">$match</span>$trail";
3541 print chop_str($text, 80, 5) . "<br/>\n";
3542 }
3543 }
3544 print "</td>\n" .
3545 "<td class=\"link\">" .
3546 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3547 " | " .
3548 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3549 print "</td>\n" .
3550 "</tr>\n";
3551 }
3552 close $fd;
3553 }
3554
3555 if ($pickaxe_search) {
3556 $/ = "\n";
3557 my $git_command = git_cmd_str();
3558 open my $fd, "-|", "$git_command rev-list $hash | " .
3559 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3560 undef %co;
3561 my @files;
3562 while (my $line = <$fd>) {
3563 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3564 my %set;
3565 $set{'file'} = $6;
3566 $set{'from_id'} = $3;
3567 $set{'to_id'} = $4;
3568 $set{'id'} = $set{'to_id'};
3569 if ($set{'id'} =~ m/0{40}/) {
3570 $set{'id'} = $set{'from_id'};
3571 }
3572 if ($set{'id'} =~ m/0{40}/) {
3573 next;
3574 }
3575 push @files, \%set;
3576 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3577 if (%co) {
3578 if ($alternate) {
3579 print "<tr class=\"dark\">\n";
3580 } else {
3581 print "<tr class=\"light\">\n";
3582 }
3583 $alternate ^= 1;
3584 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3585 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3586 "<td>" .
3587 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3588 -class => "list subject"},
3589 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3590 while (my $setref = shift @files) {
3591 my %set = %$setref;
3592 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3593 hash=>$set{'id'}, file_name=>$set{'file'}),
3594 -class => "list"},
3595 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3596 "<br/>\n";
3597 }
3598 print "</td>\n" .
3599 "<td class=\"link\">" .
3600 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3601 " | " .
3602 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3603 print "</td>\n" .
3604 "</tr>\n";
3605 }
3606 %co = parse_commit($1);
3607 }
3608 }
3609 close $fd;
3610 }
3611 print "</table>\n";
3612 git_footer_html();
3613 }
3614
3615 sub git_shortlog {
3616 my $head = git_get_head_hash($project);
3617 if (!defined $hash) {
3618 $hash = $head;
3619 }
3620 if (!defined $page) {
3621 $page = 0;
3622 }
3623 my $refs = git_get_references();
3624
3625 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3626 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3627 or die_error(undef, "Open git-rev-list failed");
3628 my @revlist = map { chomp; $_ } <$fd>;
3629 close $fd;
3630
3631 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3632 my $next_link = '';
3633 if ($#revlist >= (100 * ($page+1)-1)) {
3634 $next_link =
3635 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3636 -title => "Alt-n"}, "next");
3637 }
3638
3639
3640 git_header_html();
3641 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3642 git_print_header_div('summary', $project);
3643
3644 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3645
3646 git_footer_html();
3647 }
3648
3649 ## ......................................................................
3650 ## feeds (RSS, OPML)
3651
3652 sub git_rss {
3653 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3654 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3655 or die_error(undef, "Open git-rev-list failed");
3656 my @revlist = map { chomp; $_ } <$fd>;
3657 close $fd or die_error(undef, "Reading git-rev-list failed");
3658 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3659 print <<XML;
3660 <?xml version="1.0" encoding="utf-8"?>
3661 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3662 <channel>
3663 <title>$project $my_uri $my_url</title>
3664 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3665 <description>$project log</description>
3666 <language>en</language>
3667 XML
3668
3669 for (my $i = 0; $i <= $#revlist; $i++) {
3670 my $commit = $revlist[$i];
3671 my %co = parse_commit($commit);
3672 # we read 150, we always show 30 and the ones more recent than 48 hours
3673 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3674 last;
3675 }
3676 my %cd = parse_date($co{'committer_epoch'});
3677 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3678 $co{'parent'}, $co{'id'}
3679 or next;
3680 my @difftree = map { chomp; $_ } <$fd>;
3681 close $fd
3682 or next;
3683 print "<item>\n" .
3684 "<title>" .
3685 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3686 "</title>\n" .
3687 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3688 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3689 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3690 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3691 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3692 "<content:encoded>" .
3693 "<![CDATA[\n";
3694 my $comment = $co{'comment'};
3695 foreach my $line (@$comment) {
3696 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3697 print "$line<br/>\n";
3698 }
3699 print "<br/>\n";
3700 foreach my $line (@difftree) {
3701 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3702 next;
3703 }
3704 my $file = esc_html(unquote($7));
3705 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3706 print "$file<br/>\n";
3707 }
3708 print "]]>\n" .
3709 "</content:encoded>\n" .
3710 "</item>\n";
3711 }
3712 print "</channel></rss>";
3713 }
3714
3715 sub git_opml {
3716 my @list = git_get_projects_list();
3717
3718 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3719 print <<XML;
3720 <?xml version="1.0" encoding="utf-8"?>
3721 <opml version="1.0">
3722 <head>
3723 <title>$site_name Git OPML Export</title>
3724 </head>
3725 <body>
3726 <outline text="git RSS feeds">
3727 XML
3728
3729 foreach my $pr (@list) {
3730 my %proj = %$pr;
3731 my $head = git_get_head_hash($proj{'path'});
3732 if (!defined $head) {
3733 next;
3734 }
3735 $git_dir = "$projectroot/$proj{'path'}";
3736 my %co = parse_commit($head);
3737 if (!%co) {
3738 next;
3739 }
3740
3741 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3742 my $rss = "$my_url?p=$proj{'path'};a=rss";
3743 my $html = "$my_url?p=$proj{'path'};a=summary";
3744 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3745 }
3746 print <<XML;
3747 </outline>
3748 </body>
3749 </opml>
3750 XML
3751 }
This page took 1.767246 seconds and 5 git commands to generate.