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