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