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