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