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