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