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
21BEGIN {
22 CGI->compile() if $ENV{'MOD_PERL'};
23}
24
25our $cgi = new CGI;
26our $version = "++GIT_VERSION++";
27our $my_url = $cgi->url();
28our $my_uri = $cgi->url(-absolute => 1);
29
30# core git executable to use
31# this can just be "git" if your webserver has a sensible PATH
32our $GIT = "++GIT_BINDIR++/git";
33
34# absolute fs-path which will be prepended to the project path
35#our $projectroot = "/pub/scm";
36our $projectroot = "++GITWEB_PROJECTROOT++";
37
38# fs traversing limit for getting project list
39# the number is relative to the projectroot
40our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
41
42# target of the home link on top of all pages
43our $home_link = $my_uri || "/";
44
45# string of the home link on top of all pages
46our $home_link_str = "++GITWEB_HOME_LINK_STR++";
47
48# name of your site or organization to appear in page titles
49# replace this with something more descriptive for clearer bookmarks
50our $site_name = "++GITWEB_SITENAME++"
51 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
52
53# filename of html text to include at top of each page
54our $site_header = "++GITWEB_SITE_HEADER++";
55# html text to include at home page
56our $home_text = "++GITWEB_HOMETEXT++";
57# filename of html text to include at bottom of each page
58our $site_footer = "++GITWEB_SITE_FOOTER++";
59
60# URI of stylesheets
61our @stylesheets = ("++GITWEB_CSS++");
62# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
63our $stylesheet = undef;
64# URI of GIT logo (72x27 size)
65our $logo = "++GITWEB_LOGO++";
66# URI of GIT favicon, assumed to be image/png type
67our $favicon = "++GITWEB_FAVICON++";
68
69# URI and label (title) of GIT logo link
70#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
71#our $logo_label = "git documentation";
72our $logo_url = "http://git.or.cz/";
73our $logo_label = "git homepage";
74
75# source of projects list
76our $projects_list = "++GITWEB_LIST++";
77
78# the width (in characters) of the projects list "Description" column
79our $projects_list_description_width = 25;
80
81# default order of projects list
82# valid values are none, project, descr, owner, and age
83our $default_projects_order = "project";
84
85# show repository only if this file exists
86# (only effective if this variable evaluates to true)
87our $export_ok = "++GITWEB_EXPORT_OK++";
88
89# only allow viewing of repositories also shown on the overview page
90our $strict_export = "++GITWEB_STRICT_EXPORT++";
91
92# list of git base URLs used for URL to where fetch project from,
93# i.e. full URL is "$git_base_url/$project"
94our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
95
96# default blob_plain mimetype and default charset for text/plain blob
97our $default_blob_plain_mimetype = 'text/plain';
98our $default_text_plain_charset = undef;
99
100# file to use for guessing MIME types before trying /etc/mime.types
101# (relative to the current git repository)
102our $mimetypes_file = undef;
103
104# assume this charset if line contains non-UTF-8 characters;
105# it should be valid encoding (see Encoding::Supported(3pm) for list),
106# for which encoding all byte sequences are valid, for example
107# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
108# could be even 'utf-8' for the old behavior)
109our $fallback_encoding = 'latin1';
110
111# rename detection options for git-diff and git-diff-tree
112# - default is '-M', with the cost proportional to
113# (number of removed files) * (number of new files).
114# - more costly is '-C' (which implies '-M'), with the cost proportional to
115# (number of changed files + number of removed files) * (number of new files)
116# - even more costly is '-C', '--find-copies-harder' with cost
117# (number of files in the original tree) * (number of new files)
118# - one might want to include '-B' option, e.g. '-B', '-M'
119our @diff_opts = ('-M'); # taken from git_commit
120
121# information about snapshot formats that gitweb is capable of serving
122our %known_snapshot_formats = (
123 # name => {
124 # 'display' => display name,
125 # 'type' => mime type,
126 # 'suffix' => filename suffix,
127 # 'format' => --format for git-archive,
128 # 'compressor' => [compressor command and arguments]
129 # (array reference, optional)}
130 #
131 'tgz' => {
132 'display' => 'tar.gz',
133 'type' => 'application/x-gzip',
134 'suffix' => '.tar.gz',
135 'format' => 'tar',
136 'compressor' => ['gzip']},
137
138 'tbz2' => {
139 'display' => 'tar.bz2',
140 'type' => 'application/x-bzip2',
141 'suffix' => '.tar.bz2',
142 'format' => 'tar',
143 'compressor' => ['bzip2']},
144
145 'zip' => {
146 'display' => 'zip',
147 'type' => 'application/x-zip',
148 'suffix' => '.zip',
149 'format' => 'zip'},
150);
151
152# Aliases so we understand old gitweb.snapshot values in repository
153# configuration.
154our %known_snapshot_format_aliases = (
155 'gzip' => 'tgz',
156 'bzip2' => 'tbz2',
157
158 # backward compatibility: legacy gitweb config support
159 'x-gzip' => undef, 'gz' => undef,
160 'x-bzip2' => undef, 'bz2' => undef,
161 'x-zip' => undef, '' => undef,
162);
163
164# You define site-wide feature defaults here; override them with
165# $GITWEB_CONFIG as necessary.
166our %feature = (
167 # feature => {
168 # 'sub' => feature-sub (subroutine),
169 # 'override' => allow-override (boolean),
170 # 'default' => [ default options...] (array reference)}
171 #
172 # if feature is overridable (it means that allow-override has true value),
173 # then feature-sub will be called with default options as parameters;
174 # return value of feature-sub indicates if to enable specified feature
175 #
176 # if there is no 'sub' key (no feature-sub), then feature cannot be
177 # overriden
178 #
179 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
180
181 # Enable the 'blame' blob view, showing the last commit that modified
182 # each line in the file. This can be very CPU-intensive.
183
184 # To enable system wide have in $GITWEB_CONFIG
185 # $feature{'blame'}{'default'} = [1];
186 # To have project specific config enable override in $GITWEB_CONFIG
187 # $feature{'blame'}{'override'} = 1;
188 # and in project config gitweb.blame = 0|1;
189 'blame' => {
190 'sub' => \&feature_blame,
191 'override' => 0,
192 'default' => [0]},
193
194 # Enable the 'snapshot' link, providing a compressed archive of any
195 # tree. This can potentially generate high traffic if you have large
196 # project.
197
198 # Value is a list of formats defined in %known_snapshot_formats that
199 # you wish to offer.
200 # To disable system wide have in $GITWEB_CONFIG
201 # $feature{'snapshot'}{'default'} = [];
202 # To have project specific config enable override in $GITWEB_CONFIG
203 # $feature{'snapshot'}{'override'} = 1;
204 # and in project config, a comma-separated list of formats or "none"
205 # to disable. Example: gitweb.snapshot = tbz2,zip;
206 'snapshot' => {
207 'sub' => \&feature_snapshot,
208 'override' => 0,
209 'default' => ['tgz']},
210
211 # Enable text search, which will list the commits which match author,
212 # committer or commit text to a given string. Enabled by default.
213 # Project specific override is not supported.
214 'search' => {
215 'override' => 0,
216 'default' => [1]},
217
218 # Enable grep search, which will list the files in currently selected
219 # tree containing the given string. Enabled by default. This can be
220 # potentially CPU-intensive, of course.
221
222 # To enable system wide have in $GITWEB_CONFIG
223 # $feature{'grep'}{'default'} = [1];
224 # To have project specific config enable override in $GITWEB_CONFIG
225 # $feature{'grep'}{'override'} = 1;
226 # and in project config gitweb.grep = 0|1;
227 'grep' => {
228 'override' => 0,
229 'default' => [1]},
230
231 # Enable the pickaxe search, which will list the commits that modified
232 # a given string in a file. This can be practical and quite faster
233 # alternative to 'blame', but still potentially CPU-intensive.
234
235 # To enable system wide have in $GITWEB_CONFIG
236 # $feature{'pickaxe'}{'default'} = [1];
237 # To have project specific config enable override in $GITWEB_CONFIG
238 # $feature{'pickaxe'}{'override'} = 1;
239 # and in project config gitweb.pickaxe = 0|1;
240 'pickaxe' => {
241 'sub' => \&feature_pickaxe,
242 'override' => 0,
243 'default' => [1]},
244
245 # Make gitweb use an alternative format of the URLs which can be
246 # more readable and natural-looking: project name is embedded
247 # directly in the path and the query string contains other
248 # auxiliary information. All gitweb installations recognize
249 # URL in either format; this configures in which formats gitweb
250 # generates links.
251
252 # To enable system wide have in $GITWEB_CONFIG
253 # $feature{'pathinfo'}{'default'} = [1];
254 # Project specific override is not supported.
255
256 # Note that you will need to change the default location of CSS,
257 # favicon, logo and possibly other files to an absolute URL. Also,
258 # if gitweb.cgi serves as your indexfile, you will need to force
259 # $my_uri to contain the script name in your $GITWEB_CONFIG.
260 'pathinfo' => {
261 'override' => 0,
262 'default' => [0]},
263
264 # Make gitweb consider projects in project root subdirectories
265 # to be forks of existing projects. Given project $projname.git,
266 # projects matching $projname/*.git will not be shown in the main
267 # projects list, instead a '+' mark will be added to $projname
268 # there and a 'forks' view will be enabled for the project, listing
269 # all the forks. If project list is taken from a file, forks have
270 # to be listed after the main project.
271
272 # To enable system wide have in $GITWEB_CONFIG
273 # $feature{'forks'}{'default'} = [1];
274 # Project specific override is not supported.
275 'forks' => {
276 'override' => 0,
277 'default' => [0]},
278);
279
280sub gitweb_check_feature {
281 my ($name) = @_;
282 return unless exists $feature{$name};
283 my ($sub, $override, @defaults) = (
284 $feature{$name}{'sub'},
285 $feature{$name}{'override'},
286 @{$feature{$name}{'default'}});
287 if (!$override) { return @defaults; }
288 if (!defined $sub) {
289 warn "feature $name is not overrideable";
290 return @defaults;
291 }
292 return $sub->(@defaults);
293}
294
295sub feature_blame {
296 my ($val) = git_get_project_config('blame', '--bool');
297
298 if ($val eq 'true') {
299 return 1;
300 } elsif ($val eq 'false') {
301 return 0;
302 }
303
304 return $_[0];
305}
306
307sub feature_snapshot {
308 my (@fmts) = @_;
309
310 my ($val) = git_get_project_config('snapshot');
311
312 if ($val) {
313 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
314 }
315
316 return @fmts;
317}
318
319sub feature_grep {
320 my ($val) = git_get_project_config('grep', '--bool');
321
322 if ($val eq 'true') {
323 return (1);
324 } elsif ($val eq 'false') {
325 return (0);
326 }
327
328 return ($_[0]);
329}
330
331sub feature_pickaxe {
332 my ($val) = git_get_project_config('pickaxe', '--bool');
333
334 if ($val eq 'true') {
335 return (1);
336 } elsif ($val eq 'false') {
337 return (0);
338 }
339
340 return ($_[0]);
341}
342
343# checking HEAD file with -e is fragile if the repository was
344# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
345# and then pruned.
346sub check_head_link {
347 my ($dir) = @_;
348 my $headfile = "$dir/HEAD";
349 return ((-e $headfile) ||
350 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
351}
352
353sub check_export_ok {
354 my ($dir) = @_;
355 return (check_head_link($dir) &&
356 (!$export_ok || -e "$dir/$export_ok"));
357}
358
359# process alternate names for backward compatibility
360# filter out unsupported (unknown) snapshot formats
361sub filter_snapshot_fmts {
362 my @fmts = @_;
363
364 @fmts = map {
365 exists $known_snapshot_format_aliases{$_} ?
366 $known_snapshot_format_aliases{$_} : $_} @fmts;
367 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
368
369}
370
371our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
372do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
373
374# version of the core git binary
375our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
376
377$projects_list ||= $projectroot;
378
379# ======================================================================
380# input validation and dispatch
381our $action = $cgi->param('a');
382if (defined $action) {
383 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
384 die_error(undef, "Invalid action parameter");
385 }
386}
387
388# parameters which are pathnames
389our $project = $cgi->param('p');
390if (defined $project) {
391 if (!validate_pathname($project) ||
392 !(-d "$projectroot/$project") ||
393 !check_head_link("$projectroot/$project") ||
394 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
395 ($strict_export && !project_in_list($project))) {
396 undef $project;
397 die_error(undef, "No such project");
398 }
399}
400
401our $file_name = $cgi->param('f');
402if (defined $file_name) {
403 if (!validate_pathname($file_name)) {
404 die_error(undef, "Invalid file parameter");
405 }
406}
407
408our $file_parent = $cgi->param('fp');
409if (defined $file_parent) {
410 if (!validate_pathname($file_parent)) {
411 die_error(undef, "Invalid file parent parameter");
412 }
413}
414
415# parameters which are refnames
416our $hash = $cgi->param('h');
417if (defined $hash) {
418 if (!validate_refname($hash)) {
419 die_error(undef, "Invalid hash parameter");
420 }
421}
422
423our $hash_parent = $cgi->param('hp');
424if (defined $hash_parent) {
425 if (!validate_refname($hash_parent)) {
426 die_error(undef, "Invalid hash parent parameter");
427 }
428}
429
430our $hash_base = $cgi->param('hb');
431if (defined $hash_base) {
432 if (!validate_refname($hash_base)) {
433 die_error(undef, "Invalid hash base parameter");
434 }
435}
436
437my %allowed_options = (
438 "--no-merges" => [ qw(rss atom log shortlog history) ],
439);
440
441our @extra_options = $cgi->param('opt');
442if (defined @extra_options) {
443 foreach my $opt (@extra_options) {
444 if (not exists $allowed_options{$opt}) {
445 die_error(undef, "Invalid option parameter");
446 }
447 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
448 die_error(undef, "Invalid option parameter for this action");
449 }
450 }
451}
452
453our $hash_parent_base = $cgi->param('hpb');
454if (defined $hash_parent_base) {
455 if (!validate_refname($hash_parent_base)) {
456 die_error(undef, "Invalid hash parent base parameter");
457 }
458}
459
460# other parameters
461our $page = $cgi->param('pg');
462if (defined $page) {
463 if ($page =~ m/[^0-9]/) {
464 die_error(undef, "Invalid page parameter");
465 }
466}
467
468our $searchtype = $cgi->param('st');
469if (defined $searchtype) {
470 if ($searchtype =~ m/[^a-z]/) {
471 die_error(undef, "Invalid searchtype parameter");
472 }
473}
474
475our $searchtext = $cgi->param('s');
476our $search_regexp;
477if (defined $searchtext) {
478 if (length($searchtext) < 2) {
479 die_error(undef, "At least two characters are required for search parameter");
480 }
481 $search_regexp = quotemeta $searchtext;
482}
483
484# now read PATH_INFO and use it as alternative to parameters
485sub evaluate_path_info {
486 return if defined $project;
487 my $path_info = $ENV{"PATH_INFO"};
488 return if !$path_info;
489 $path_info =~ s,^/+,,;
490 return if !$path_info;
491 # find which part of PATH_INFO is project
492 $project = $path_info;
493 $project =~ s,/+$,,;
494 while ($project && !check_head_link("$projectroot/$project")) {
495 $project =~ s,/*[^/]*$,,;
496 }
497 # validate project
498 $project = validate_pathname($project);
499 if (!$project ||
500 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
501 ($strict_export && !project_in_list($project))) {
502 undef $project;
503 return;
504 }
505 # do not change any parameters if an action is given using the query string
506 return if $action;
507 $path_info =~ s,^$project/*,,;
508 my ($refname, $pathname) = split(/:/, $path_info, 2);
509 if (defined $pathname) {
510 # we got "project.git/branch:filename" or "project.git/branch:dir/"
511 # we could use git_get_type(branch:pathname), but it needs $git_dir
512 $pathname =~ s,^/+,,;
513 if (!$pathname || substr($pathname, -1) eq "/") {
514 $action ||= "tree";
515 $pathname =~ s,/$,,;
516 } else {
517 $action ||= "blob_plain";
518 }
519 $hash_base ||= validate_refname($refname);
520 $file_name ||= validate_pathname($pathname);
521 } elsif (defined $refname) {
522 # we got "project.git/branch"
523 $action ||= "shortlog";
524 $hash ||= validate_refname($refname);
525 }
526}
527evaluate_path_info();
528
529# path to the current git repository
530our $git_dir;
531$git_dir = "$projectroot/$project" if $project;
532
533# dispatch
534my %actions = (
535 "blame" => \&git_blame2,
536 "blobdiff" => \&git_blobdiff,
537 "blobdiff_plain" => \&git_blobdiff_plain,
538 "blob" => \&git_blob,
539 "blob_plain" => \&git_blob_plain,
540 "commitdiff" => \&git_commitdiff,
541 "commitdiff_plain" => \&git_commitdiff_plain,
542 "commit" => \&git_commit,
543 "forks" => \&git_forks,
544 "heads" => \&git_heads,
545 "history" => \&git_history,
546 "log" => \&git_log,
547 "rss" => \&git_rss,
548 "atom" => \&git_atom,
549 "search" => \&git_search,
550 "search_help" => \&git_search_help,
551 "shortlog" => \&git_shortlog,
552 "summary" => \&git_summary,
553 "tag" => \&git_tag,
554 "tags" => \&git_tags,
555 "tree" => \&git_tree,
556 "snapshot" => \&git_snapshot,
557 "object" => \&git_object,
558 # those below don't need $project
559 "opml" => \&git_opml,
560 "project_list" => \&git_project_list,
561 "project_index" => \&git_project_index,
562);
563
564if (!defined $action) {
565 if (defined $hash) {
566 $action = git_get_type($hash);
567 } elsif (defined $hash_base && defined $file_name) {
568 $action = git_get_type("$hash_base:$file_name");
569 } elsif (defined $project) {
570 $action = 'summary';
571 } else {
572 $action = 'project_list';
573 }
574}
575if (!defined($actions{$action})) {
576 die_error(undef, "Unknown action");
577}
578if ($action !~ m/^(opml|project_list|project_index)$/ &&
579 !$project) {
580 die_error(undef, "Project needed");
581}
582$actions{$action}->();
583exit;
584
585## ======================================================================
586## action links
587
588sub href(%) {
589 my %params = @_;
590 # default is to use -absolute url() i.e. $my_uri
591 my $href = $params{-full} ? $my_url : $my_uri;
592
593 # XXX: Warning: If you touch this, check the search form for updating,
594 # too.
595
596 my @mapping = (
597 project => "p",
598 action => "a",
599 file_name => "f",
600 file_parent => "fp",
601 hash => "h",
602 hash_parent => "hp",
603 hash_base => "hb",
604 hash_parent_base => "hpb",
605 page => "pg",
606 order => "o",
607 searchtext => "s",
608 searchtype => "st",
609 snapshot_format => "sf",
610 extra_options => "opt",
611 );
612 my %mapping = @mapping;
613
614 $params{'project'} = $project unless exists $params{'project'};
615
616 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
617 if ($use_pathinfo) {
618 # use PATH_INFO for project name
619 $href .= "/$params{'project'}" if defined $params{'project'};
620 delete $params{'project'};
621
622 # Summary just uses the project path URL
623 if (defined $params{'action'} && $params{'action'} eq 'summary') {
624 delete $params{'action'};
625 }
626 }
627
628 # now encode the parameters explicitly
629 my @result = ();
630 for (my $i = 0; $i < @mapping; $i += 2) {
631 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
632 if (defined $params{$name}) {
633 if (ref($params{$name}) eq "ARRAY") {
634 foreach my $par (@{$params{$name}}) {
635 push @result, $symbol . "=" . esc_param($par);
636 }
637 } else {
638 push @result, $symbol . "=" . esc_param($params{$name});
639 }
640 }
641 }
642 $href .= "?" . join(';', @result) if scalar @result;
643
644 return $href;
645}
646
647
648## ======================================================================
649## validation, quoting/unquoting and escaping
650
651sub validate_pathname {
652 my $input = shift || return undef;
653
654 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
655 # at the beginning, at the end, and between slashes.
656 # also this catches doubled slashes
657 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
658 return undef;
659 }
660 # no null characters
661 if ($input =~ m!\0!) {
662 return undef;
663 }
664 return $input;
665}
666
667sub validate_refname {
668 my $input = shift || return undef;
669
670 # textual hashes are O.K.
671 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
672 return $input;
673 }
674 # it must be correct pathname
675 $input = validate_pathname($input)
676 or return undef;
677 # restrictions on ref name according to git-check-ref-format
678 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
679 return undef;
680 }
681 return $input;
682}
683
684# decode sequences of octets in utf8 into Perl's internal form,
685# which is utf-8 with utf8 flag set if needed. gitweb writes out
686# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
687sub to_utf8 {
688 my $str = shift;
689 my $res;
690 eval { $res = decode_utf8($str, Encode::FB_CROAK); };
691 if (defined $res) {
692 return $res;
693 } else {
694 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
695 }
696}
697
698# quote unsafe chars, but keep the slash, even when it's not
699# correct, but quoted slashes look too horrible in bookmarks
700sub esc_param {
701 my $str = shift;
702 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
703 $str =~ s/\+/%2B/g;
704 $str =~ s/ /\+/g;
705 return $str;
706}
707
708# quote unsafe chars in whole URL, so some charactrs cannot be quoted
709sub esc_url {
710 my $str = shift;
711 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
712 $str =~ s/\+/%2B/g;
713 $str =~ s/ /\+/g;
714 return $str;
715}
716
717# replace invalid utf8 character with SUBSTITUTION sequence
718sub esc_html ($;%) {
719 my $str = shift;
720 my %opts = @_;
721
722 $str = to_utf8($str);
723 $str = $cgi->escapeHTML($str);
724 if ($opts{'-nbsp'}) {
725 $str =~ s/ / /g;
726 }
727 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
728 return $str;
729}
730
731# quote control characters and escape filename to HTML
732sub esc_path {
733 my $str = shift;
734 my %opts = @_;
735
736 $str = to_utf8($str);
737 $str = $cgi->escapeHTML($str);
738 if ($opts{'-nbsp'}) {
739 $str =~ s/ / /g;
740 }
741 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
742 return $str;
743}
744
745# Make control characters "printable", using character escape codes (CEC)
746sub quot_cec {
747 my $cntrl = shift;
748 my %es = ( # character escape codes, aka escape sequences
749 "\t" => '\t', # tab (HT)
750 "\n" => '\n', # line feed (LF)
751 "\r" => '\r', # carrige return (CR)
752 "\f" => '\f', # form feed (FF)
753 "\b" => '\b', # backspace (BS)
754 "\a" => '\a', # alarm (bell) (BEL)
755 "\e" => '\e', # escape (ESC)
756 "\013" => '\v', # vertical tab (VT)
757 "\000" => '\0', # nul character (NUL)
758 );
759 my $chr = ( (exists $es{$cntrl})
760 ? $es{$cntrl}
761 : sprintf('\%03o', ord($cntrl)) );
762 return "<span class=\"cntrl\">$chr</span>";
763}
764
765# Alternatively use unicode control pictures codepoints,
766# Unicode "printable representation" (PR)
767sub quot_upr {
768 my $cntrl = shift;
769 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
770 return "<span class=\"cntrl\">$chr</span>";
771}
772
773# git may return quoted and escaped filenames
774sub unquote {
775 my $str = shift;
776
777 sub unq {
778 my $seq = shift;
779 my %es = ( # character escape codes, aka escape sequences
780 't' => "\t", # tab (HT, TAB)
781 'n' => "\n", # newline (NL)
782 'r' => "\r", # return (CR)
783 'f' => "\f", # form feed (FF)
784 'b' => "\b", # backspace (BS)
785 'a' => "\a", # alarm (bell) (BEL)
786 'e' => "\e", # escape (ESC)
787 'v' => "\013", # vertical tab (VT)
788 );
789
790 if ($seq =~ m/^[0-7]{1,3}$/) {
791 # octal char sequence
792 return chr(oct($seq));
793 } elsif (exists $es{$seq}) {
794 # C escape sequence, aka character escape code
795 return $es{$seq}
796 }
797 # quoted ordinary character
798 return $seq;
799 }
800
801 if ($str =~ m/^"(.*)"$/) {
802 # needs unquoting
803 $str = $1;
804 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
805 }
806 return $str;
807}
808
809# escape tabs (convert tabs to spaces)
810sub untabify {
811 my $line = shift;
812
813 while ((my $pos = index($line, "\t")) != -1) {
814 if (my $count = (8 - ($pos % 8))) {
815 my $spaces = ' ' x $count;
816 $line =~ s/\t/$spaces/;
817 }
818 }
819
820 return $line;
821}
822
823sub project_in_list {
824 my $project = shift;
825 my @list = git_get_projects_list();
826 return @list && scalar(grep { $_->{'path'} eq $project } @list);
827}
828
829## ----------------------------------------------------------------------
830## HTML aware string manipulation
831
832sub chop_str {
833 my $str = shift;
834 my $len = shift;
835 my $add_len = shift || 10;
836
837 # allow only $len chars, but don't cut a word if it would fit in $add_len
838 # if it doesn't fit, cut it if it's still longer than the dots we would add
839 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
840 my $body = $1;
841 my $tail = $2;
842 if (length($tail) > 4) {
843 $tail = " ...";
844 $body =~ s/&[^;]*$//; # remove chopped character entities
845 }
846 return "$body$tail";
847}
848
849# takes the same arguments as chop_str, but also wraps a <span> around the
850# result with a title attribute if it does get chopped. Additionally, the
851# string is HTML-escaped.
852sub chop_and_escape_str {
853 my $str = shift;
854 my $len = shift;
855 my $add_len = shift || 10;
856
857 my $chopped = chop_str($str, $len, $add_len);
858 if ($chopped eq $str) {
859 return esc_html($chopped);
860 } else {
861 return qq{<span title="} . esc_html($str) . qq{">} .
862 esc_html($chopped) . qq{</span>};
863 }
864}
865
866## ----------------------------------------------------------------------
867## functions returning short strings
868
869# CSS class for given age value (in seconds)
870sub age_class {
871 my $age = shift;
872
873 if (!defined $age) {
874 return "noage";
875 } elsif ($age < 60*60*2) {
876 return "age0";
877 } elsif ($age < 60*60*24*2) {
878 return "age1";
879 } else {
880 return "age2";
881 }
882}
883
884# convert age in seconds to "nn units ago" string
885sub age_string {
886 my $age = shift;
887 my $age_str;
888
889 if ($age > 60*60*24*365*2) {
890 $age_str = (int $age/60/60/24/365);
891 $age_str .= " years ago";
892 } elsif ($age > 60*60*24*(365/12)*2) {
893 $age_str = int $age/60/60/24/(365/12);
894 $age_str .= " months ago";
895 } elsif ($age > 60*60*24*7*2) {
896 $age_str = int $age/60/60/24/7;
897 $age_str .= " weeks ago";
898 } elsif ($age > 60*60*24*2) {
899 $age_str = int $age/60/60/24;
900 $age_str .= " days ago";
901 } elsif ($age > 60*60*2) {
902 $age_str = int $age/60/60;
903 $age_str .= " hours ago";
904 } elsif ($age > 60*2) {
905 $age_str = int $age/60;
906 $age_str .= " min ago";
907 } elsif ($age > 2) {
908 $age_str = int $age;
909 $age_str .= " sec ago";
910 } else {
911 $age_str .= " right now";
912 }
913 return $age_str;
914}
915
916use constant {
917 S_IFINVALID => 0030000,
918 S_IFGITLINK => 0160000,
919};
920
921# submodule/subproject, a commit object reference
922sub S_ISGITLINK($) {
923 my $mode = shift;
924
925 return (($mode & S_IFMT) == S_IFGITLINK)
926}
927
928# convert file mode in octal to symbolic file mode string
929sub mode_str {
930 my $mode = oct shift;
931
932 if (S_ISGITLINK($mode)) {
933 return 'm---------';
934 } elsif (S_ISDIR($mode & S_IFMT)) {
935 return 'drwxr-xr-x';
936 } elsif (S_ISLNK($mode)) {
937 return 'lrwxrwxrwx';
938 } elsif (S_ISREG($mode)) {
939 # git cares only about the executable bit
940 if ($mode & S_IXUSR) {
941 return '-rwxr-xr-x';
942 } else {
943 return '-rw-r--r--';
944 };
945 } else {
946 return '----------';
947 }
948}
949
950# convert file mode in octal to file type string
951sub file_type {
952 my $mode = shift;
953
954 if ($mode !~ m/^[0-7]+$/) {
955 return $mode;
956 } else {
957 $mode = oct $mode;
958 }
959
960 if (S_ISGITLINK($mode)) {
961 return "submodule";
962 } elsif (S_ISDIR($mode & S_IFMT)) {
963 return "directory";
964 } elsif (S_ISLNK($mode)) {
965 return "symlink";
966 } elsif (S_ISREG($mode)) {
967 return "file";
968 } else {
969 return "unknown";
970 }
971}
972
973# convert file mode in octal to file type description string
974sub file_type_long {
975 my $mode = shift;
976
977 if ($mode !~ m/^[0-7]+$/) {
978 return $mode;
979 } else {
980 $mode = oct $mode;
981 }
982
983 if (S_ISGITLINK($mode)) {
984 return "submodule";
985 } elsif (S_ISDIR($mode & S_IFMT)) {
986 return "directory";
987 } elsif (S_ISLNK($mode)) {
988 return "symlink";
989 } elsif (S_ISREG($mode)) {
990 if ($mode & S_IXUSR) {
991 return "executable";
992 } else {
993 return "file";
994 };
995 } else {
996 return "unknown";
997 }
998}
999
1000
1001## ----------------------------------------------------------------------
1002## functions returning short HTML fragments, or transforming HTML fragments
1003## which don't belong to other sections
1004
1005# format line of commit message.
1006sub format_log_line_html {
1007 my $line = shift;
1008
1009 $line = esc_html($line, -nbsp=>1);
1010 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1011 my $hash_text = $1;
1012 my $link =
1013 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1014 -class => "text"}, $hash_text);
1015 $line =~ s/$hash_text/$link/;
1016 }
1017 return $line;
1018}
1019
1020# format marker of refs pointing to given object
1021sub format_ref_marker {
1022 my ($refs, $id) = @_;
1023 my $markers = '';
1024
1025 if (defined $refs->{$id}) {
1026 foreach my $ref (@{$refs->{$id}}) {
1027 my ($type, $name) = qw();
1028 # e.g. tags/v2.6.11 or heads/next
1029 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1030 $type = $1;
1031 $name = $2;
1032 } else {
1033 $type = "ref";
1034 $name = $ref;
1035 }
1036
1037 $markers .= " <span class=\"$type\" title=\"$ref\">" .
1038 esc_html($name) . "</span>";
1039 }
1040 }
1041
1042 if ($markers) {
1043 return ' <span class="refs">'. $markers . '</span>';
1044 } else {
1045 return "";
1046 }
1047}
1048
1049# format, perhaps shortened and with markers, title line
1050sub format_subject_html {
1051 my ($long, $short, $href, $extra) = @_;
1052 $extra = '' unless defined($extra);
1053
1054 if (length($short) < length($long)) {
1055 return $cgi->a({-href => $href, -class => "list subject",
1056 -title => to_utf8($long)},
1057 esc_html($short) . $extra);
1058 } else {
1059 return $cgi->a({-href => $href, -class => "list subject"},
1060 esc_html($long) . $extra);
1061 }
1062}
1063
1064# format git diff header line, i.e. "diff --(git|combined|cc) ..."
1065sub format_git_diff_header_line {
1066 my $line = shift;
1067 my $diffinfo = shift;
1068 my ($from, $to) = @_;
1069
1070 if ($diffinfo->{'nparents'}) {
1071 # combined diff
1072 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1073 if ($to->{'href'}) {
1074 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1075 esc_path($to->{'file'}));
1076 } else { # file was deleted (no href)
1077 $line .= esc_path($to->{'file'});
1078 }
1079 } else {
1080 # "ordinary" diff
1081 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1082 if ($from->{'href'}) {
1083 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1084 'a/' . esc_path($from->{'file'}));
1085 } else { # file was added (no href)
1086 $line .= 'a/' . esc_path($from->{'file'});
1087 }
1088 $line .= ' ';
1089 if ($to->{'href'}) {
1090 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1091 'b/' . esc_path($to->{'file'}));
1092 } else { # file was deleted
1093 $line .= 'b/' . esc_path($to->{'file'});
1094 }
1095 }
1096
1097 return "<div class=\"diff header\">$line</div>\n";
1098}
1099
1100# format extended diff header line, before patch itself
1101sub format_extended_diff_header_line {
1102 my $line = shift;
1103 my $diffinfo = shift;
1104 my ($from, $to) = @_;
1105
1106 # match <path>
1107 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1108 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1109 esc_path($from->{'file'}));
1110 }
1111 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1112 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1113 esc_path($to->{'file'}));
1114 }
1115 # match single <mode>
1116 if ($line =~ m/\s(\d{6})$/) {
1117 $line .= '<span class="info"> (' .
1118 file_type_long($1) .
1119 ')</span>';
1120 }
1121 # match <hash>
1122 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1123 # can match only for combined diff
1124 $line = 'index ';
1125 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1126 if ($from->{'href'}[$i]) {
1127 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1128 -class=>"hash"},
1129 substr($diffinfo->{'from_id'}[$i],0,7));
1130 } else {
1131 $line .= '0' x 7;
1132 }
1133 # separator
1134 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1135 }
1136 $line .= '..';
1137 if ($to->{'href'}) {
1138 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1139 substr($diffinfo->{'to_id'},0,7));
1140 } else {
1141 $line .= '0' x 7;
1142 }
1143
1144 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1145 # can match only for ordinary diff
1146 my ($from_link, $to_link);
1147 if ($from->{'href'}) {
1148 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1149 substr($diffinfo->{'from_id'},0,7));
1150 } else {
1151 $from_link = '0' x 7;
1152 }
1153 if ($to->{'href'}) {
1154 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1155 substr($diffinfo->{'to_id'},0,7));
1156 } else {
1157 $to_link = '0' x 7;
1158 }
1159 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1160 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1161 }
1162
1163 return $line . "<br/>\n";
1164}
1165
1166# format from-file/to-file diff header
1167sub format_diff_from_to_header {
1168 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1169 my $line;
1170 my $result = '';
1171
1172 $line = $from_line;
1173 #assert($line =~ m/^---/) if DEBUG;
1174 # no extra formatting for "^--- /dev/null"
1175 if (! $diffinfo->{'nparents'}) {
1176 # ordinary (single parent) diff
1177 if ($line =~ m!^--- "?a/!) {
1178 if ($from->{'href'}) {
1179 $line = '--- a/' .
1180 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1181 esc_path($from->{'file'}));
1182 } else {
1183 $line = '--- a/' .
1184 esc_path($from->{'file'});
1185 }
1186 }
1187 $result .= qq!<div class="diff from_file">$line</div>\n!;
1188
1189 } else {
1190 # combined diff (merge commit)
1191 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1192 if ($from->{'href'}[$i]) {
1193 $line = '--- ' .
1194 $cgi->a({-href=>href(action=>"blobdiff",
1195 hash_parent=>$diffinfo->{'from_id'}[$i],
1196 hash_parent_base=>$parents[$i],
1197 file_parent=>$from->{'file'}[$i],
1198 hash=>$diffinfo->{'to_id'},
1199 hash_base=>$hash,
1200 file_name=>$to->{'file'}),
1201 -class=>"path",
1202 -title=>"diff" . ($i+1)},
1203 $i+1) .
1204 '/' .
1205 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1206 esc_path($from->{'file'}[$i]));
1207 } else {
1208 $line = '--- /dev/null';
1209 }
1210 $result .= qq!<div class="diff from_file">$line</div>\n!;
1211 }
1212 }
1213
1214 $line = $to_line;
1215 #assert($line =~ m/^\+\+\+/) if DEBUG;
1216 # no extra formatting for "^+++ /dev/null"
1217 if ($line =~ m!^\+\+\+ "?b/!) {
1218 if ($to->{'href'}) {
1219 $line = '+++ b/' .
1220 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1221 esc_path($to->{'file'}));
1222 } else {
1223 $line = '+++ b/' .
1224 esc_path($to->{'file'});
1225 }
1226 }
1227 $result .= qq!<div class="diff to_file">$line</div>\n!;
1228
1229 return $result;
1230}
1231
1232# create note for patch simplified by combined diff
1233sub format_diff_cc_simplified {
1234 my ($diffinfo, @parents) = @_;
1235 my $result = '';
1236
1237 $result .= "<div class=\"diff header\">" .
1238 "diff --cc ";
1239 if (!is_deleted($diffinfo)) {
1240 $result .= $cgi->a({-href => href(action=>"blob",
1241 hash_base=>$hash,
1242 hash=>$diffinfo->{'to_id'},
1243 file_name=>$diffinfo->{'to_file'}),
1244 -class => "path"},
1245 esc_path($diffinfo->{'to_file'}));
1246 } else {
1247 $result .= esc_path($diffinfo->{'to_file'});
1248 }
1249 $result .= "</div>\n" . # class="diff header"
1250 "<div class=\"diff nodifferences\">" .
1251 "Simple merge" .
1252 "</div>\n"; # class="diff nodifferences"
1253
1254 return $result;
1255}
1256
1257# format patch (diff) line (not to be used for diff headers)
1258sub format_diff_line {
1259 my $line = shift;
1260 my ($from, $to) = @_;
1261 my $diff_class = "";
1262
1263 chomp $line;
1264
1265 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1266 # combined diff
1267 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1268 if ($line =~ m/^\@{3}/) {
1269 $diff_class = " chunk_header";
1270 } elsif ($line =~ m/^\\/) {
1271 $diff_class = " incomplete";
1272 } elsif ($prefix =~ tr/+/+/) {
1273 $diff_class = " add";
1274 } elsif ($prefix =~ tr/-/-/) {
1275 $diff_class = " rem";
1276 }
1277 } else {
1278 # assume ordinary diff
1279 my $char = substr($line, 0, 1);
1280 if ($char eq '+') {
1281 $diff_class = " add";
1282 } elsif ($char eq '-') {
1283 $diff_class = " rem";
1284 } elsif ($char eq '@') {
1285 $diff_class = " chunk_header";
1286 } elsif ($char eq "\\") {
1287 $diff_class = " incomplete";
1288 }
1289 }
1290 $line = untabify($line);
1291 if ($from && $to && $line =~ m/^\@{2} /) {
1292 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1293 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1294
1295 $from_lines = 0 unless defined $from_lines;
1296 $to_lines = 0 unless defined $to_lines;
1297
1298 if ($from->{'href'}) {
1299 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1300 -class=>"list"}, $from_text);
1301 }
1302 if ($to->{'href'}) {
1303 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1304 -class=>"list"}, $to_text);
1305 }
1306 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1307 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1308 return "<div class=\"diff$diff_class\">$line</div>\n";
1309 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1310 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1311 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1312
1313 @from_text = split(' ', $ranges);
1314 for (my $i = 0; $i < @from_text; ++$i) {
1315 ($from_start[$i], $from_nlines[$i]) =
1316 (split(',', substr($from_text[$i], 1)), 0);
1317 }
1318
1319 $to_text = pop @from_text;
1320 $to_start = pop @from_start;
1321 $to_nlines = pop @from_nlines;
1322
1323 $line = "<span class=\"chunk_info\">$prefix ";
1324 for (my $i = 0; $i < @from_text; ++$i) {
1325 if ($from->{'href'}[$i]) {
1326 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1327 -class=>"list"}, $from_text[$i]);
1328 } else {
1329 $line .= $from_text[$i];
1330 }
1331 $line .= " ";
1332 }
1333 if ($to->{'href'}) {
1334 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1335 -class=>"list"}, $to_text);
1336 } else {
1337 $line .= $to_text;
1338 }
1339 $line .= " $prefix</span>" .
1340 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1341 return "<div class=\"diff$diff_class\">$line</div>\n";
1342 }
1343 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1344}
1345
1346# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1347# linked. Pass the hash of the tree/commit to snapshot.
1348sub format_snapshot_links {
1349 my ($hash) = @_;
1350 my @snapshot_fmts = gitweb_check_feature('snapshot');
1351 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1352 my $num_fmts = @snapshot_fmts;
1353 if ($num_fmts > 1) {
1354 # A parenthesized list of links bearing format names.
1355 # e.g. "snapshot (_tar.gz_ _zip_)"
1356 return "snapshot (" . join(' ', map
1357 $cgi->a({
1358 -href => href(
1359 action=>"snapshot",
1360 hash=>$hash,
1361 snapshot_format=>$_
1362 )
1363 }, $known_snapshot_formats{$_}{'display'})
1364 , @snapshot_fmts) . ")";
1365 } elsif ($num_fmts == 1) {
1366 # A single "snapshot" link whose tooltip bears the format name.
1367 # i.e. "_snapshot_"
1368 my ($fmt) = @snapshot_fmts;
1369 return
1370 $cgi->a({
1371 -href => href(
1372 action=>"snapshot",
1373 hash=>$hash,
1374 snapshot_format=>$fmt
1375 ),
1376 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1377 }, "snapshot");
1378 } else { # $num_fmts == 0
1379 return undef;
1380 }
1381}
1382
1383## ----------------------------------------------------------------------
1384## git utility subroutines, invoking git commands
1385
1386# returns path to the core git executable and the --git-dir parameter as list
1387sub git_cmd {
1388 return $GIT, '--git-dir='.$git_dir;
1389}
1390
1391# returns path to the core git executable and the --git-dir parameter as string
1392sub git_cmd_str {
1393 return join(' ', git_cmd());
1394}
1395
1396# get HEAD ref of given project as hash
1397sub git_get_head_hash {
1398 my $project = shift;
1399 my $o_git_dir = $git_dir;
1400 my $retval = undef;
1401 $git_dir = "$projectroot/$project";
1402 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1403 my $head = <$fd>;
1404 close $fd;
1405 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1406 $retval = $1;
1407 }
1408 }
1409 if (defined $o_git_dir) {
1410 $git_dir = $o_git_dir;
1411 }
1412 return $retval;
1413}
1414
1415# get type of given object
1416sub git_get_type {
1417 my $hash = shift;
1418
1419 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1420 my $type = <$fd>;
1421 close $fd or return;
1422 chomp $type;
1423 return $type;
1424}
1425
1426sub git_get_project_config {
1427 my ($key, $type) = @_;
1428
1429 return unless ($key);
1430 $key =~ s/^gitweb\.//;
1431 return if ($key =~ m/\W/);
1432
1433 my @x = (git_cmd(), 'config');
1434 if (defined $type) { push @x, $type; }
1435 push @x, "--get";
1436 push @x, "gitweb.$key";
1437 my $val = qx(@x);
1438 chomp $val;
1439 return ($val);
1440}
1441
1442# get hash of given path at given ref
1443sub git_get_hash_by_path {
1444 my $base = shift;
1445 my $path = shift || return undef;
1446 my $type = shift;
1447
1448 $path =~ s,/+$,,;
1449
1450 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1451 or die_error(undef, "Open git-ls-tree failed");
1452 my $line = <$fd>;
1453 close $fd or return undef;
1454
1455 if (!defined $line) {
1456 # there is no tree or hash given by $path at $base
1457 return undef;
1458 }
1459
1460 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1461 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1462 if (defined $type && $type ne $2) {
1463 # type doesn't match
1464 return undef;
1465 }
1466 return $3;
1467}
1468
1469# get path of entry with given hash at given tree-ish (ref)
1470# used to get 'from' filename for combined diff (merge commit) for renames
1471sub git_get_path_by_hash {
1472 my $base = shift || return;
1473 my $hash = shift || return;
1474
1475 local $/ = "\0";
1476
1477 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1478 or return undef;
1479 while (my $line = <$fd>) {
1480 chomp $line;
1481
1482 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1483 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1484 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1485 close $fd;
1486 return $1;
1487 }
1488 }
1489 close $fd;
1490 return undef;
1491}
1492
1493## ......................................................................
1494## git utility functions, directly accessing git repository
1495
1496sub git_get_project_description {
1497 my $path = shift;
1498
1499 open my $fd, "$projectroot/$path/description" or return undef;
1500 my $descr = <$fd>;
1501 close $fd;
1502 if (defined $descr) {
1503 chomp $descr;
1504 }
1505 return $descr;
1506}
1507
1508sub git_get_project_url_list {
1509 my $path = shift;
1510
1511 open my $fd, "$projectroot/$path/cloneurl" or return;
1512 my @git_project_url_list = map { chomp; $_ } <$fd>;
1513 close $fd;
1514
1515 return wantarray ? @git_project_url_list : \@git_project_url_list;
1516}
1517
1518sub git_get_projects_list {
1519 my ($filter) = @_;
1520 my @list;
1521
1522 $filter ||= '';
1523 $filter =~ s/\.git$//;
1524
1525 my ($check_forks) = gitweb_check_feature('forks');
1526
1527 if (-d $projects_list) {
1528 # search in directory
1529 my $dir = $projects_list . ($filter ? "/$filter" : '');
1530 # remove the trailing "/"
1531 $dir =~ s!/+$!!;
1532 my $pfxlen = length("$dir");
1533 my $pfxdepth = ($dir =~ tr!/!!);
1534
1535 File::Find::find({
1536 follow_fast => 1, # follow symbolic links
1537 follow_skip => 2, # ignore duplicates
1538 dangling_symlinks => 0, # ignore dangling symlinks, silently
1539 wanted => sub {
1540 # skip project-list toplevel, if we get it.
1541 return if (m!^[/.]$!);
1542 # only directories can be git repositories
1543 return unless (-d $_);
1544 # don't traverse too deep (Find is super slow on os x)
1545 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
1546 $File::Find::prune = 1;
1547 return;
1548 }
1549
1550 my $subdir = substr($File::Find::name, $pfxlen + 1);
1551 # we check related file in $projectroot
1552 if ($check_forks and $subdir =~ m#/.#) {
1553 $File::Find::prune = 1;
1554 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1555 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1556 $File::Find::prune = 1;
1557 }
1558 },
1559 }, "$dir");
1560
1561 } elsif (-f $projects_list) {
1562 # read from file(url-encoded):
1563 # 'git%2Fgit.git Linus+Torvalds'
1564 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1565 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1566 my %paths;
1567 open my ($fd), $projects_list or return;
1568 PROJECT:
1569 while (my $line = <$fd>) {
1570 chomp $line;
1571 my ($path, $owner) = split ' ', $line;
1572 $path = unescape($path);
1573 $owner = unescape($owner);
1574 if (!defined $path) {
1575 next;
1576 }
1577 if ($filter ne '') {
1578 # looking for forks;
1579 my $pfx = substr($path, 0, length($filter));
1580 if ($pfx ne $filter) {
1581 next PROJECT;
1582 }
1583 my $sfx = substr($path, length($filter));
1584 if ($sfx !~ /^\/.*\.git$/) {
1585 next PROJECT;
1586 }
1587 } elsif ($check_forks) {
1588 PATH:
1589 foreach my $filter (keys %paths) {
1590 # looking for forks;
1591 my $pfx = substr($path, 0, length($filter));
1592 if ($pfx ne $filter) {
1593 next PATH;
1594 }
1595 my $sfx = substr($path, length($filter));
1596 if ($sfx !~ /^\/.*\.git$/) {
1597 next PATH;
1598 }
1599 # is a fork, don't include it in
1600 # the list
1601 next PROJECT;
1602 }
1603 }
1604 if (check_export_ok("$projectroot/$path")) {
1605 my $pr = {
1606 path => $path,
1607 owner => to_utf8($owner),
1608 };
1609 push @list, $pr;
1610 (my $forks_path = $path) =~ s/\.git$//;
1611 $paths{$forks_path}++;
1612 }
1613 }
1614 close $fd;
1615 }
1616 return @list;
1617}
1618
1619our $gitweb_project_owner = undef;
1620sub git_get_project_list_from_file {
1621
1622 return if (defined $gitweb_project_owner);
1623
1624 $gitweb_project_owner = {};
1625 # read from file (url-encoded):
1626 # 'git%2Fgit.git Linus+Torvalds'
1627 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1628 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1629 if (-f $projects_list) {
1630 open (my $fd , $projects_list);
1631 while (my $line = <$fd>) {
1632 chomp $line;
1633 my ($pr, $ow) = split ' ', $line;
1634 $pr = unescape($pr);
1635 $ow = unescape($ow);
1636 $gitweb_project_owner->{$pr} = to_utf8($ow);
1637 }
1638 close $fd;
1639 }
1640}
1641
1642sub git_get_project_owner {
1643 my $project = shift;
1644 my $owner;
1645
1646 return undef unless $project;
1647
1648 if (!defined $gitweb_project_owner) {
1649 git_get_project_list_from_file();
1650 }
1651
1652 if (exists $gitweb_project_owner->{$project}) {
1653 $owner = $gitweb_project_owner->{$project};
1654 }
1655 if (!defined $owner) {
1656 $owner = get_file_owner("$projectroot/$project");
1657 }
1658
1659 return $owner;
1660}
1661
1662sub git_get_last_activity {
1663 my ($path) = @_;
1664 my $fd;
1665
1666 $git_dir = "$projectroot/$path";
1667 open($fd, "-|", git_cmd(), 'for-each-ref',
1668 '--format=%(committer)',
1669 '--sort=-committerdate',
1670 '--count=1',
1671 'refs/heads') or return;
1672 my $most_recent = <$fd>;
1673 close $fd or return;
1674 if (defined $most_recent &&
1675 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1676 my $timestamp = $1;
1677 my $age = time - $timestamp;
1678 return ($age, age_string($age));
1679 }
1680 return (undef, undef);
1681}
1682
1683sub git_get_references {
1684 my $type = shift || "";
1685 my %refs;
1686 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1687 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1688 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1689 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1690 or return;
1691
1692 while (my $line = <$fd>) {
1693 chomp $line;
1694 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1695 if (defined $refs{$1}) {
1696 push @{$refs{$1}}, $2;
1697 } else {
1698 $refs{$1} = [ $2 ];
1699 }
1700 }
1701 }
1702 close $fd or return;
1703 return \%refs;
1704}
1705
1706sub git_get_rev_name_tags {
1707 my $hash = shift || return undef;
1708
1709 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1710 or return;
1711 my $name_rev = <$fd>;
1712 close $fd;
1713
1714 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1715 return $1;
1716 } else {
1717 # catches also '$hash undefined' output
1718 return undef;
1719 }
1720}
1721
1722## ----------------------------------------------------------------------
1723## parse to hash functions
1724
1725sub parse_date {
1726 my $epoch = shift;
1727 my $tz = shift || "-0000";
1728
1729 my %date;
1730 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1731 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1732 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1733 $date{'hour'} = $hour;
1734 $date{'minute'} = $min;
1735 $date{'mday'} = $mday;
1736 $date{'day'} = $days[$wday];
1737 $date{'month'} = $months[$mon];
1738 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1739 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1740 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1741 $mday, $months[$mon], $hour ,$min;
1742 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1743 1900+$year, $mon, $mday, $hour ,$min, $sec;
1744
1745 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1746 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1747 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1748 $date{'hour_local'} = $hour;
1749 $date{'minute_local'} = $min;
1750 $date{'tz_local'} = $tz;
1751 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1752 1900+$year, $mon+1, $mday,
1753 $hour, $min, $sec, $tz);
1754 return %date;
1755}
1756
1757sub parse_tag {
1758 my $tag_id = shift;
1759 my %tag;
1760 my @comment;
1761
1762 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1763 $tag{'id'} = $tag_id;
1764 while (my $line = <$fd>) {
1765 chomp $line;
1766 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1767 $tag{'object'} = $1;
1768 } elsif ($line =~ m/^type (.+)$/) {
1769 $tag{'type'} = $1;
1770 } elsif ($line =~ m/^tag (.+)$/) {
1771 $tag{'name'} = $1;
1772 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1773 $tag{'author'} = $1;
1774 $tag{'epoch'} = $2;
1775 $tag{'tz'} = $3;
1776 } elsif ($line =~ m/--BEGIN/) {
1777 push @comment, $line;
1778 last;
1779 } elsif ($line eq "") {
1780 last;
1781 }
1782 }
1783 push @comment, <$fd>;
1784 $tag{'comment'} = \@comment;
1785 close $fd or return;
1786 if (!defined $tag{'name'}) {
1787 return
1788 };
1789 return %tag
1790}
1791
1792sub parse_commit_text {
1793 my ($commit_text, $withparents) = @_;
1794 my @commit_lines = split '\n', $commit_text;
1795 my %co;
1796
1797 pop @commit_lines; # Remove '\0'
1798
1799 if (! @commit_lines) {
1800 return;
1801 }
1802
1803 my $header = shift @commit_lines;
1804 if ($header !~ m/^[0-9a-fA-F]{40}/) {
1805 return;
1806 }
1807 ($co{'id'}, my @parents) = split ' ', $header;
1808 while (my $line = shift @commit_lines) {
1809 last if $line eq "\n";
1810 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1811 $co{'tree'} = $1;
1812 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1813 push @parents, $1;
1814 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1815 $co{'author'} = $1;
1816 $co{'author_epoch'} = $2;
1817 $co{'author_tz'} = $3;
1818 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1819 $co{'author_name'} = $1;
1820 $co{'author_email'} = $2;
1821 } else {
1822 $co{'author_name'} = $co{'author'};
1823 }
1824 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1825 $co{'committer'} = $1;
1826 $co{'committer_epoch'} = $2;
1827 $co{'committer_tz'} = $3;
1828 $co{'committer_name'} = $co{'committer'};
1829 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1830 $co{'committer_name'} = $1;
1831 $co{'committer_email'} = $2;
1832 } else {
1833 $co{'committer_name'} = $co{'committer'};
1834 }
1835 }
1836 }
1837 if (!defined $co{'tree'}) {
1838 return;
1839 };
1840 $co{'parents'} = \@parents;
1841 $co{'parent'} = $parents[0];
1842
1843 foreach my $title (@commit_lines) {
1844 $title =~ s/^ //;
1845 if ($title ne "") {
1846 $co{'title'} = chop_str($title, 80, 5);
1847 # remove leading stuff of merges to make the interesting part visible
1848 if (length($title) > 50) {
1849 $title =~ s/^Automatic //;
1850 $title =~ s/^merge (of|with) /Merge ... /i;
1851 if (length($title) > 50) {
1852 $title =~ s/(http|rsync):\/\///;
1853 }
1854 if (length($title) > 50) {
1855 $title =~ s/(master|www|rsync)\.//;
1856 }
1857 if (length($title) > 50) {
1858 $title =~ s/kernel.org:?//;
1859 }
1860 if (length($title) > 50) {
1861 $title =~ s/\/pub\/scm//;
1862 }
1863 }
1864 $co{'title_short'} = chop_str($title, 50, 5);
1865 last;
1866 }
1867 }
1868 if ($co{'title'} eq "") {
1869 $co{'title'} = $co{'title_short'} = '(no commit message)';
1870 }
1871 # remove added spaces
1872 foreach my $line (@commit_lines) {
1873 $line =~ s/^ //;
1874 }
1875 $co{'comment'} = \@commit_lines;
1876
1877 my $age = time - $co{'committer_epoch'};
1878 $co{'age'} = $age;
1879 $co{'age_string'} = age_string($age);
1880 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1881 if ($age > 60*60*24*7*2) {
1882 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1883 $co{'age_string_age'} = $co{'age_string'};
1884 } else {
1885 $co{'age_string_date'} = $co{'age_string'};
1886 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1887 }
1888 return %co;
1889}
1890
1891sub parse_commit {
1892 my ($commit_id) = @_;
1893 my %co;
1894
1895 local $/ = "\0";
1896
1897 open my $fd, "-|", git_cmd(), "rev-list",
1898 "--parents",
1899 "--header",
1900 "--max-count=1",
1901 $commit_id,
1902 "--",
1903 or die_error(undef, "Open git-rev-list failed");
1904 %co = parse_commit_text(<$fd>, 1);
1905 close $fd;
1906
1907 return %co;
1908}
1909
1910sub parse_commits {
1911 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1912 my @cos;
1913
1914 $maxcount ||= 1;
1915 $skip ||= 0;
1916
1917 local $/ = "\0";
1918
1919 open my $fd, "-|", git_cmd(), "rev-list",
1920 "--header",
1921 ($arg ? ($arg) : ()),
1922 ("--max-count=" . $maxcount),
1923 ("--skip=" . $skip),
1924 @extra_options,
1925 $commit_id,
1926 "--",
1927 ($filename ? ($filename) : ())
1928 or die_error(undef, "Open git-rev-list failed");
1929 while (my $line = <$fd>) {
1930 my %co = parse_commit_text($line);
1931 push @cos, \%co;
1932 }
1933 close $fd;
1934
1935 return wantarray ? @cos : \@cos;
1936}
1937
1938# parse ref from ref_file, given by ref_id, with given type
1939sub parse_ref {
1940 my $ref_file = shift;
1941 my $ref_id = shift;
1942 my $type = shift || git_get_type($ref_id);
1943 my %ref_item;
1944
1945 $ref_item{'type'} = $type;
1946 $ref_item{'id'} = $ref_id;
1947 $ref_item{'epoch'} = 0;
1948 $ref_item{'age'} = "unknown";
1949 if ($type eq "tag") {
1950 my %tag = parse_tag($ref_id);
1951 $ref_item{'comment'} = $tag{'comment'};
1952 if ($tag{'type'} eq "commit") {
1953 my %co = parse_commit($tag{'object'});
1954 $ref_item{'epoch'} = $co{'committer_epoch'};
1955 $ref_item{'age'} = $co{'age_string'};
1956 } elsif (defined($tag{'epoch'})) {
1957 my $age = time - $tag{'epoch'};
1958 $ref_item{'epoch'} = $tag{'epoch'};
1959 $ref_item{'age'} = age_string($age);
1960 }
1961 $ref_item{'reftype'} = $tag{'type'};
1962 $ref_item{'name'} = $tag{'name'};
1963 $ref_item{'refid'} = $tag{'object'};
1964 } elsif ($type eq "commit"){
1965 my %co = parse_commit($ref_id);
1966 $ref_item{'reftype'} = "commit";
1967 $ref_item{'name'} = $ref_file;
1968 $ref_item{'title'} = $co{'title'};
1969 $ref_item{'refid'} = $ref_id;
1970 $ref_item{'epoch'} = $co{'committer_epoch'};
1971 $ref_item{'age'} = $co{'age_string'};
1972 } else {
1973 $ref_item{'reftype'} = $type;
1974 $ref_item{'name'} = $ref_file;
1975 $ref_item{'refid'} = $ref_id;
1976 }
1977
1978 return %ref_item;
1979}
1980
1981# parse line of git-diff-tree "raw" output
1982sub parse_difftree_raw_line {
1983 my $line = shift;
1984 my %res;
1985
1986 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1987 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1988 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1989 $res{'from_mode'} = $1;
1990 $res{'to_mode'} = $2;
1991 $res{'from_id'} = $3;
1992 $res{'to_id'} = $4;
1993 $res{'status'} = $5;
1994 $res{'similarity'} = $6;
1995 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1996 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1997 } else {
1998 $res{'file'} = unquote($7);
1999 }
2000 }
2001 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2002 # combined diff (for merge commit)
2003 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2004 $res{'nparents'} = length($1);
2005 $res{'from_mode'} = [ split(' ', $2) ];
2006 $res{'to_mode'} = pop @{$res{'from_mode'}};
2007 $res{'from_id'} = [ split(' ', $3) ];
2008 $res{'to_id'} = pop @{$res{'from_id'}};
2009 $res{'status'} = [ split('', $4) ];
2010 $res{'to_file'} = unquote($5);
2011 }
2012 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2013 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2014 $res{'commit'} = $1;
2015 }
2016
2017 return wantarray ? %res : \%res;
2018}
2019
2020# parse line of git-ls-tree output
2021sub parse_ls_tree_line ($;%) {
2022 my $line = shift;
2023 my %opts = @_;
2024 my %res;
2025
2026 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2027 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2028
2029 $res{'mode'} = $1;
2030 $res{'type'} = $2;
2031 $res{'hash'} = $3;
2032 if ($opts{'-z'}) {
2033 $res{'name'} = $4;
2034 } else {
2035 $res{'name'} = unquote($4);
2036 }
2037
2038 return wantarray ? %res : \%res;
2039}
2040
2041# generates _two_ hashes, references to which are passed as 2 and 3 argument
2042sub parse_from_to_diffinfo {
2043 my ($diffinfo, $from, $to, @parents) = @_;
2044
2045 if ($diffinfo->{'nparents'}) {
2046 # combined diff
2047 $from->{'file'} = [];
2048 $from->{'href'} = [];
2049 fill_from_file_info($diffinfo, @parents)
2050 unless exists $diffinfo->{'from_file'};
2051 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2052 $from->{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
2053 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2054 $from->{'href'}[$i] = href(action=>"blob",
2055 hash_base=>$parents[$i],
2056 hash=>$diffinfo->{'from_id'}[$i],
2057 file_name=>$from->{'file'}[$i]);
2058 } else {
2059 $from->{'href'}[$i] = undef;
2060 }
2061 }
2062 } else {
2063 $from->{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2064 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2065 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2066 hash=>$diffinfo->{'from_id'},
2067 file_name=>$from->{'file'});
2068 } else {
2069 delete $from->{'href'};
2070 }
2071 }
2072
2073 $to->{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2074 if (!is_deleted($diffinfo)) { # file exists in result
2075 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2076 hash=>$diffinfo->{'to_id'},
2077 file_name=>$to->{'file'});
2078 } else {
2079 delete $to->{'href'};
2080 }
2081}
2082
2083## ......................................................................
2084## parse to array of hashes functions
2085
2086sub git_get_heads_list {
2087 my $limit = shift;
2088 my @headslist;
2089
2090 open my $fd, '-|', git_cmd(), 'for-each-ref',
2091 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2092 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2093 'refs/heads'
2094 or return;
2095 while (my $line = <$fd>) {
2096 my %ref_item;
2097
2098 chomp $line;
2099 my ($refinfo, $committerinfo) = split(/\0/, $line);
2100 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2101 my ($committer, $epoch, $tz) =
2102 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2103 $name =~ s!^refs/heads/!!;
2104
2105 $ref_item{'name'} = $name;
2106 $ref_item{'id'} = $hash;
2107 $ref_item{'title'} = $title || '(no commit message)';
2108 $ref_item{'epoch'} = $epoch;
2109 if ($epoch) {
2110 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2111 } else {
2112 $ref_item{'age'} = "unknown";
2113 }
2114
2115 push @headslist, \%ref_item;
2116 }
2117 close $fd;
2118
2119 return wantarray ? @headslist : \@headslist;
2120}
2121
2122sub git_get_tags_list {
2123 my $limit = shift;
2124 my @tagslist;
2125
2126 open my $fd, '-|', git_cmd(), 'for-each-ref',
2127 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2128 '--format=%(objectname) %(objecttype) %(refname) '.
2129 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2130 'refs/tags'
2131 or return;
2132 while (my $line = <$fd>) {
2133 my %ref_item;
2134
2135 chomp $line;
2136 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2137 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2138 my ($creator, $epoch, $tz) =
2139 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2140 $name =~ s!^refs/tags/!!;
2141
2142 $ref_item{'type'} = $type;
2143 $ref_item{'id'} = $id;
2144 $ref_item{'name'} = $name;
2145 if ($type eq "tag") {
2146 $ref_item{'subject'} = $title;
2147 $ref_item{'reftype'} = $reftype;
2148 $ref_item{'refid'} = $refid;
2149 } else {
2150 $ref_item{'reftype'} = $type;
2151 $ref_item{'refid'} = $id;
2152 }
2153
2154 if ($type eq "tag" || $type eq "commit") {
2155 $ref_item{'epoch'} = $epoch;
2156 if ($epoch) {
2157 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2158 } else {
2159 $ref_item{'age'} = "unknown";
2160 }
2161 }
2162
2163 push @tagslist, \%ref_item;
2164 }
2165 close $fd;
2166
2167 return wantarray ? @tagslist : \@tagslist;
2168}
2169
2170## ----------------------------------------------------------------------
2171## filesystem-related functions
2172
2173sub get_file_owner {
2174 my $path = shift;
2175
2176 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2177 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2178 if (!defined $gcos) {
2179 return undef;
2180 }
2181 my $owner = $gcos;
2182 $owner =~ s/[,;].*$//;
2183 return to_utf8($owner);
2184}
2185
2186## ......................................................................
2187## mimetype related functions
2188
2189sub mimetype_guess_file {
2190 my $filename = shift;
2191 my $mimemap = shift;
2192 -r $mimemap or return undef;
2193
2194 my %mimemap;
2195 open(MIME, $mimemap) or return undef;
2196 while (<MIME>) {
2197 next if m/^#/; # skip comments
2198 my ($mime, $exts) = split(/\t+/);
2199 if (defined $exts) {
2200 my @exts = split(/\s+/, $exts);
2201 foreach my $ext (@exts) {
2202 $mimemap{$ext} = $mime;
2203 }
2204 }
2205 }
2206 close(MIME);
2207
2208 $filename =~ /\.([^.]*)$/;
2209 return $mimemap{$1};
2210}
2211
2212sub mimetype_guess {
2213 my $filename = shift;
2214 my $mime;
2215 $filename =~ /\./ or return undef;
2216
2217 if ($mimetypes_file) {
2218 my $file = $mimetypes_file;
2219 if ($file !~ m!^/!) { # if it is relative path
2220 # it is relative to project
2221 $file = "$projectroot/$project/$file";
2222 }
2223 $mime = mimetype_guess_file($filename, $file);
2224 }
2225 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2226 return $mime;
2227}
2228
2229sub blob_mimetype {
2230 my $fd = shift;
2231 my $filename = shift;
2232
2233 if ($filename) {
2234 my $mime = mimetype_guess($filename);
2235 $mime and return $mime;
2236 }
2237
2238 # just in case
2239 return $default_blob_plain_mimetype unless $fd;
2240
2241 if (-T $fd) {
2242 return 'text/plain' .
2243 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
2244 } elsif (! $filename) {
2245 return 'application/octet-stream';
2246 } elsif ($filename =~ m/\.png$/i) {
2247 return 'image/png';
2248 } elsif ($filename =~ m/\.gif$/i) {
2249 return 'image/gif';
2250 } elsif ($filename =~ m/\.jpe?g$/i) {
2251 return 'image/jpeg';
2252 } else {
2253 return 'application/octet-stream';
2254 }
2255}
2256
2257## ======================================================================
2258## functions printing HTML: header, footer, error page
2259
2260sub git_header_html {
2261 my $status = shift || "200 OK";
2262 my $expires = shift;
2263
2264 my $title = "$site_name";
2265 if (defined $project) {
2266 $title .= " - " . to_utf8($project);
2267 if (defined $action) {
2268 $title .= "/$action";
2269 if (defined $file_name) {
2270 $title .= " - " . esc_path($file_name);
2271 if ($action eq "tree" && $file_name !~ m|/$|) {
2272 $title .= "/";
2273 }
2274 }
2275 }
2276 }
2277 my $content_type;
2278 # require explicit support from the UA if we are to send the page as
2279 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2280 # we have to do this because MSIE sometimes globs '*/*', pretending to
2281 # support xhtml+xml but choking when it gets what it asked for.
2282 if (defined $cgi->http('HTTP_ACCEPT') &&
2283 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2284 $cgi->Accept('application/xhtml+xml') != 0) {
2285 $content_type = 'application/xhtml+xml';
2286 } else {
2287 $content_type = 'text/html';
2288 }
2289 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2290 -status=> $status, -expires => $expires);
2291 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2292 print <<EOF;
2293<?xml version="1.0" encoding="utf-8"?>
2294<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2295<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2296<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2297<!-- git core binaries version $git_version -->
2298<head>
2299<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2300<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2301<meta name="robots" content="index, nofollow"/>
2302<title>$title</title>
2303EOF
2304# print out each stylesheet that exist
2305 if (defined $stylesheet) {
2306#provides backwards capability for those people who define style sheet in a config file
2307 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2308 } else {
2309 foreach my $stylesheet (@stylesheets) {
2310 next unless $stylesheet;
2311 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2312 }
2313 }
2314 if (defined $project) {
2315 printf('<link rel="alternate" title="%s log RSS feed" '.
2316 'href="%s" type="application/rss+xml" />'."\n",
2317 esc_param($project), href(action=>"rss"));
2318 printf('<link rel="alternate" title="%s log RSS feed (no merges)" '.
2319 'href="%s" type="application/rss+xml" />'."\n",
2320 esc_param($project), href(action=>"rss",
2321 extra_options=>"--no-merges"));
2322 printf('<link rel="alternate" title="%s log Atom feed" '.
2323 'href="%s" type="application/atom+xml" />'."\n",
2324 esc_param($project), href(action=>"atom"));
2325 printf('<link rel="alternate" title="%s log Atom feed (no merges)" '.
2326 'href="%s" type="application/atom+xml" />'."\n",
2327 esc_param($project), href(action=>"atom",
2328 extra_options=>"--no-merges"));
2329 } else {
2330 printf('<link rel="alternate" title="%s projects list" '.
2331 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
2332 $site_name, href(project=>undef, action=>"project_index"));
2333 printf('<link rel="alternate" title="%s projects feeds" '.
2334 'href="%s" type="text/x-opml"/>'."\n",
2335 $site_name, href(project=>undef, action=>"opml"));
2336 }
2337 if (defined $favicon) {
2338 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
2339 }
2340
2341 print "</head>\n" .
2342 "<body>\n";
2343
2344 if (-f $site_header) {
2345 open (my $fd, $site_header);
2346 print <$fd>;
2347 close $fd;
2348 }
2349
2350 print "<div class=\"page_header\">\n" .
2351 $cgi->a({-href => esc_url($logo_url),
2352 -title => $logo_label},
2353 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2354 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2355 if (defined $project) {
2356 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2357 if (defined $action) {
2358 print " / $action";
2359 }
2360 print "\n";
2361 }
2362 print "</div>\n";
2363
2364 my ($have_search) = gitweb_check_feature('search');
2365 if ((defined $project) && ($have_search)) {
2366 if (!defined $searchtext) {
2367 $searchtext = "";
2368 }
2369 my $search_hash;
2370 if (defined $hash_base) {
2371 $search_hash = $hash_base;
2372 } elsif (defined $hash) {
2373 $search_hash = $hash;
2374 } else {
2375 $search_hash = "HEAD";
2376 }
2377 my $action = $my_uri;
2378 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2379 if ($use_pathinfo) {
2380 $action .= "/$project";
2381 } else {
2382 $cgi->param("p", $project);
2383 }
2384 $cgi->param("a", "search");
2385 $cgi->param("h", $search_hash);
2386 print $cgi->startform(-method => "get", -action => $action) .
2387 "<div class=\"search\">\n" .
2388 (!$use_pathinfo && $cgi->hidden(-name => "p") . "\n") .
2389 $cgi->hidden(-name => "a") . "\n" .
2390 $cgi->hidden(-name => "h") . "\n" .
2391 $cgi->popup_menu(-name => 'st', -default => 'commit',
2392 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2393 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2394 " search:\n",
2395 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2396 "</div>" .
2397 $cgi->end_form() . "\n";
2398 }
2399}
2400
2401sub git_footer_html {
2402 print "<div class=\"page_footer\">\n";
2403 if (defined $project) {
2404 my $descr = git_get_project_description($project);
2405 if (defined $descr) {
2406 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2407 }
2408 print $cgi->a({-href => href(action=>"rss"),
2409 -class => "rss_logo"}, "RSS") . " ";
2410 print $cgi->a({-href => href(action=>"atom"),
2411 -class => "rss_logo"}, "Atom") . "\n";
2412 } else {
2413 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2414 -class => "rss_logo"}, "OPML") . " ";
2415 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2416 -class => "rss_logo"}, "TXT") . "\n";
2417 }
2418 print "</div>\n" ;
2419
2420 if (-f $site_footer) {
2421 open (my $fd, $site_footer);
2422 print <$fd>;
2423 close $fd;
2424 }
2425
2426 print "</body>\n" .
2427 "</html>";
2428}
2429
2430sub die_error {
2431 my $status = shift || "403 Forbidden";
2432 my $error = shift || "Malformed query, file missing or permission denied";
2433
2434 git_header_html($status);
2435 print <<EOF;
2436<div class="page_body">
2437<br /><br />
2438$status - $error
2439<br />
2440</div>
2441EOF
2442 git_footer_html();
2443 exit;
2444}
2445
2446## ----------------------------------------------------------------------
2447## functions printing or outputting HTML: navigation
2448
2449sub git_print_page_nav {
2450 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2451 $extra = '' if !defined $extra; # pager or formats
2452
2453 my @navs = qw(summary shortlog log commit commitdiff tree);
2454 if ($suppress) {
2455 @navs = grep { $_ ne $suppress } @navs;
2456 }
2457
2458 my %arg = map { $_ => {action=>$_} } @navs;
2459 if (defined $head) {
2460 for (qw(commit commitdiff)) {
2461 $arg{$_}{'hash'} = $head;
2462 }
2463 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2464 for (qw(shortlog log)) {
2465 $arg{$_}{'hash'} = $head;
2466 }
2467 }
2468 }
2469 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2470 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2471
2472 print "<div class=\"page_nav\">\n" .
2473 (join " | ",
2474 map { $_ eq $current ?
2475 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2476 } @navs);
2477 print "<br/>\n$extra<br/>\n" .
2478 "</div>\n";
2479}
2480
2481sub format_paging_nav {
2482 my ($action, $hash, $head, $page, $nrevs) = @_;
2483 my $paging_nav;
2484
2485
2486 if ($hash ne $head || $page) {
2487 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2488 } else {
2489 $paging_nav .= "HEAD";
2490 }
2491
2492 if ($page > 0) {
2493 $paging_nav .= " ⋅ " .
2494 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
2495 -accesskey => "p", -title => "Alt-p"}, "prev");
2496 } else {
2497 $paging_nav .= " ⋅ prev";
2498 }
2499
2500 if ($nrevs >= (100 * ($page+1)-1)) {
2501 $paging_nav .= " ⋅ " .
2502 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
2503 -accesskey => "n", -title => "Alt-n"}, "next");
2504 } else {
2505 $paging_nav .= " ⋅ next";
2506 }
2507
2508 return $paging_nav;
2509}
2510
2511## ......................................................................
2512## functions printing or outputting HTML: div
2513
2514sub git_print_header_div {
2515 my ($action, $title, $hash, $hash_base) = @_;
2516 my %args = ();
2517
2518 $args{'action'} = $action;
2519 $args{'hash'} = $hash if $hash;
2520 $args{'hash_base'} = $hash_base if $hash_base;
2521
2522 print "<div class=\"header\">\n" .
2523 $cgi->a({-href => href(%args), -class => "title"},
2524 $title ? $title : $action) .
2525 "\n</div>\n";
2526}
2527
2528#sub git_print_authorship (\%) {
2529sub git_print_authorship {
2530 my $co = shift;
2531
2532 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2533 print "<div class=\"author_date\">" .
2534 esc_html($co->{'author_name'}) .
2535 " [$ad{'rfc2822'}";
2536 if ($ad{'hour_local'} < 6) {
2537 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2538 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2539 } else {
2540 printf(" (%02d:%02d %s)",
2541 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2542 }
2543 print "]</div>\n";
2544}
2545
2546sub git_print_page_path {
2547 my $name = shift;
2548 my $type = shift;
2549 my $hb = shift;
2550
2551
2552 print "<div class=\"page_path\">";
2553 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2554 -title => 'tree root'}, to_utf8("[$project]"));
2555 print " / ";
2556 if (defined $name) {
2557 my @dirname = split '/', $name;
2558 my $basename = pop @dirname;
2559 my $fullname = '';
2560
2561 foreach my $dir (@dirname) {
2562 $fullname .= ($fullname ? '/' : '') . $dir;
2563 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2564 hash_base=>$hb),
2565 -title => $fullname}, esc_path($dir));
2566 print " / ";
2567 }
2568 if (defined $type && $type eq 'blob') {
2569 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2570 hash_base=>$hb),
2571 -title => $name}, esc_path($basename));
2572 } elsif (defined $type && $type eq 'tree') {
2573 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2574 hash_base=>$hb),
2575 -title => $name}, esc_path($basename));
2576 print " / ";
2577 } else {
2578 print esc_path($basename);
2579 }
2580 }
2581 print "<br/></div>\n";
2582}
2583
2584# sub git_print_log (\@;%) {
2585sub git_print_log ($;%) {
2586 my $log = shift;
2587 my %opts = @_;
2588
2589 if ($opts{'-remove_title'}) {
2590 # remove title, i.e. first line of log
2591 shift @$log;
2592 }
2593 # remove leading empty lines
2594 while (defined $log->[0] && $log->[0] eq "") {
2595 shift @$log;
2596 }
2597
2598 # print log
2599 my $signoff = 0;
2600 my $empty = 0;
2601 foreach my $line (@$log) {
2602 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2603 $signoff = 1;
2604 $empty = 0;
2605 if (! $opts{'-remove_signoff'}) {
2606 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2607 next;
2608 } else {
2609 # remove signoff lines
2610 next;
2611 }
2612 } else {
2613 $signoff = 0;
2614 }
2615
2616 # print only one empty line
2617 # do not print empty line after signoff
2618 if ($line eq "") {
2619 next if ($empty || $signoff);
2620 $empty = 1;
2621 } else {
2622 $empty = 0;
2623 }
2624
2625 print format_log_line_html($line) . "<br/>\n";
2626 }
2627
2628 if ($opts{'-final_empty_line'}) {
2629 # end with single empty line
2630 print "<br/>\n" unless $empty;
2631 }
2632}
2633
2634# return link target (what link points to)
2635sub git_get_link_target {
2636 my $hash = shift;
2637 my $link_target;
2638
2639 # read link
2640 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2641 or return;
2642 {
2643 local $/;
2644 $link_target = <$fd>;
2645 }
2646 close $fd
2647 or return;
2648
2649 return $link_target;
2650}
2651
2652# given link target, and the directory (basedir) the link is in,
2653# return target of link relative to top directory (top tree);
2654# return undef if it is not possible (including absolute links).
2655sub normalize_link_target {
2656 my ($link_target, $basedir, $hash_base) = @_;
2657
2658 # we can normalize symlink target only if $hash_base is provided
2659 return unless $hash_base;
2660
2661 # absolute symlinks (beginning with '/') cannot be normalized
2662 return if (substr($link_target, 0, 1) eq '/');
2663
2664 # normalize link target to path from top (root) tree (dir)
2665 my $path;
2666 if ($basedir) {
2667 $path = $basedir . '/' . $link_target;
2668 } else {
2669 # we are in top (root) tree (dir)
2670 $path = $link_target;
2671 }
2672
2673 # remove //, /./, and /../
2674 my @path_parts;
2675 foreach my $part (split('/', $path)) {
2676 # discard '.' and ''
2677 next if (!$part || $part eq '.');
2678 # handle '..'
2679 if ($part eq '..') {
2680 if (@path_parts) {
2681 pop @path_parts;
2682 } else {
2683 # link leads outside repository (outside top dir)
2684 return;
2685 }
2686 } else {
2687 push @path_parts, $part;
2688 }
2689 }
2690 $path = join('/', @path_parts);
2691
2692 return $path;
2693}
2694
2695# print tree entry (row of git_tree), but without encompassing <tr> element
2696sub git_print_tree_entry {
2697 my ($t, $basedir, $hash_base, $have_blame) = @_;
2698
2699 my %base_key = ();
2700 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2701
2702 # The format of a table row is: mode list link. Where mode is
2703 # the mode of the entry, list is the name of the entry, an href,
2704 # and link is the action links of the entry.
2705
2706 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2707 if ($t->{'type'} eq "blob") {
2708 print "<td class=\"list\">" .
2709 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2710 file_name=>"$basedir$t->{'name'}", %base_key),
2711 -class => "list"}, esc_path($t->{'name'}));
2712 if (S_ISLNK(oct $t->{'mode'})) {
2713 my $link_target = git_get_link_target($t->{'hash'});
2714 if ($link_target) {
2715 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2716 if (defined $norm_target) {
2717 print " -> " .
2718 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2719 file_name=>$norm_target),
2720 -title => $norm_target}, esc_path($link_target));
2721 } else {
2722 print " -> " . esc_path($link_target);
2723 }
2724 }
2725 }
2726 print "</td>\n";
2727 print "<td class=\"link\">";
2728 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2729 file_name=>"$basedir$t->{'name'}", %base_key)},
2730 "blob");
2731 if ($have_blame) {
2732 print " | " .
2733 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2734 file_name=>"$basedir$t->{'name'}", %base_key)},
2735 "blame");
2736 }
2737 if (defined $hash_base) {
2738 print " | " .
2739 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2740 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2741 "history");
2742 }
2743 print " | " .
2744 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2745 file_name=>"$basedir$t->{'name'}")},
2746 "raw");
2747 print "</td>\n";
2748
2749 } elsif ($t->{'type'} eq "tree") {
2750 print "<td class=\"list\">";
2751 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2752 file_name=>"$basedir$t->{'name'}", %base_key)},
2753 esc_path($t->{'name'}));
2754 print "</td>\n";
2755 print "<td class=\"link\">";
2756 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2757 file_name=>"$basedir$t->{'name'}", %base_key)},
2758 "tree");
2759 if (defined $hash_base) {
2760 print " | " .
2761 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2762 file_name=>"$basedir$t->{'name'}")},
2763 "history");
2764 }
2765 print "</td>\n";
2766 } else {
2767 # unknown object: we can only present history for it
2768 # (this includes 'commit' object, i.e. submodule support)
2769 print "<td class=\"list\">" .
2770 esc_path($t->{'name'}) .
2771 "</td>\n";
2772 print "<td class=\"link\">";
2773 if (defined $hash_base) {
2774 print $cgi->a({-href => href(action=>"history",
2775 hash_base=>$hash_base,
2776 file_name=>"$basedir$t->{'name'}")},
2777 "history");
2778 }
2779 print "</td>\n";
2780 }
2781}
2782
2783## ......................................................................
2784## functions printing large fragments of HTML
2785
2786sub fill_from_file_info {
2787 my ($diff, @parents) = @_;
2788
2789 $diff->{'from_file'} = [ ];
2790 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2791 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2792 if ($diff->{'status'}[$i] eq 'R' ||
2793 $diff->{'status'}[$i] eq 'C') {
2794 $diff->{'from_file'}[$i] =
2795 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2796 }
2797 }
2798
2799 return $diff;
2800}
2801
2802# parameters can be strings, or references to arrays of strings
2803sub from_ids_eq {
2804 my ($a, $b) = @_;
2805
2806 if (ref($a) eq "ARRAY" && ref($b) eq "ARRAY" && @$a == @$b) {
2807 for (my $i = 0; $i < @$a; ++$i) {
2808 return 0 unless ($a->[$i] eq $b->[$i]);
2809 }
2810 return 1;
2811 } elsif (!ref($a) && !ref($b)) {
2812 return $a eq $b;
2813 } else {
2814 return 0;
2815 }
2816}
2817
2818sub is_deleted {
2819 my $diffinfo = shift;
2820
2821 return $diffinfo->{'to_id'} eq ('0' x 40);
2822}
2823
2824sub git_difftree_body {
2825 my ($difftree, $hash, @parents) = @_;
2826 my ($parent) = $parents[0];
2827 my ($have_blame) = gitweb_check_feature('blame');
2828 print "<div class=\"list_head\">\n";
2829 if ($#{$difftree} > 10) {
2830 print(($#{$difftree} + 1) . " files changed:\n");
2831 }
2832 print "</div>\n";
2833
2834 print "<table class=\"" .
2835 (@parents > 1 ? "combined " : "") .
2836 "diff_tree\">\n";
2837
2838 # header only for combined diff in 'commitdiff' view
2839 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
2840 if ($has_header) {
2841 # table header
2842 print "<thead><tr>\n" .
2843 "<th></th><th></th>\n"; # filename, patchN link
2844 for (my $i = 0; $i < @parents; $i++) {
2845 my $par = $parents[$i];
2846 print "<th>" .
2847 $cgi->a({-href => href(action=>"commitdiff",
2848 hash=>$hash, hash_parent=>$par),
2849 -title => 'commitdiff to parent number ' .
2850 ($i+1) . ': ' . substr($par,0,7)},
2851 $i+1) .
2852 " </th>\n";
2853 }
2854 print "</tr></thead>\n<tbody>\n";
2855 }
2856
2857 my $alternate = 1;
2858 my $patchno = 0;
2859 foreach my $line (@{$difftree}) {
2860 my $diff;
2861 if (ref($line) eq "HASH") {
2862 # pre-parsed (or generated by hand)
2863 $diff = $line;
2864 } else {
2865 $diff = parse_difftree_raw_line($line);
2866 }
2867
2868 if ($alternate) {
2869 print "<tr class=\"dark\">\n";
2870 } else {
2871 print "<tr class=\"light\">\n";
2872 }
2873 $alternate ^= 1;
2874
2875 if (exists $diff->{'nparents'}) { # combined diff
2876
2877 fill_from_file_info($diff, @parents)
2878 unless exists $diff->{'from_file'};
2879
2880 if (!is_deleted($diff)) {
2881 # file exists in the result (child) commit
2882 print "<td>" .
2883 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2884 file_name=>$diff->{'to_file'},
2885 hash_base=>$hash),
2886 -class => "list"}, esc_path($diff->{'to_file'})) .
2887 "</td>\n";
2888 } else {
2889 print "<td>" .
2890 esc_path($diff->{'to_file'}) .
2891 "</td>\n";
2892 }
2893
2894 if ($action eq 'commitdiff') {
2895 # link to patch
2896 $patchno++;
2897 print "<td class=\"link\">" .
2898 $cgi->a({-href => "#patch$patchno"}, "patch") .
2899 " | " .
2900 "</td>\n";
2901 }
2902
2903 my $has_history = 0;
2904 my $not_deleted = 0;
2905 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2906 my $hash_parent = $parents[$i];
2907 my $from_hash = $diff->{'from_id'}[$i];
2908 my $from_path = $diff->{'from_file'}[$i];
2909 my $status = $diff->{'status'}[$i];
2910
2911 $has_history ||= ($status ne 'A');
2912 $not_deleted ||= ($status ne 'D');
2913
2914 if ($status eq 'A') {
2915 print "<td class=\"link\" align=\"right\"> | </td>\n";
2916 } elsif ($status eq 'D') {
2917 print "<td class=\"link\">" .
2918 $cgi->a({-href => href(action=>"blob",
2919 hash_base=>$hash,
2920 hash=>$from_hash,
2921 file_name=>$from_path)},
2922 "blob" . ($i+1)) .
2923 " | </td>\n";
2924 } else {
2925 if ($diff->{'to_id'} eq $from_hash) {
2926 print "<td class=\"link nochange\">";
2927 } else {
2928 print "<td class=\"link\">";
2929 }
2930 print $cgi->a({-href => href(action=>"blobdiff",
2931 hash=>$diff->{'to_id'},
2932 hash_parent=>$from_hash,
2933 hash_base=>$hash,
2934 hash_parent_base=>$hash_parent,
2935 file_name=>$diff->{'to_file'},
2936 file_parent=>$from_path)},
2937 "diff" . ($i+1)) .
2938 " | </td>\n";
2939 }
2940 }
2941
2942 print "<td class=\"link\">";
2943 if ($not_deleted) {
2944 print $cgi->a({-href => href(action=>"blob",
2945 hash=>$diff->{'to_id'},
2946 file_name=>$diff->{'to_file'},
2947 hash_base=>$hash)},
2948 "blob");
2949 print " | " if ($has_history);
2950 }
2951 if ($has_history) {
2952 print $cgi->a({-href => href(action=>"history",
2953 file_name=>$diff->{'to_file'},
2954 hash_base=>$hash)},
2955 "history");
2956 }
2957 print "</td>\n";
2958
2959 print "</tr>\n";
2960 next; # instead of 'else' clause, to avoid extra indent
2961 }
2962 # else ordinary diff
2963
2964 my ($to_mode_oct, $to_mode_str, $to_file_type);
2965 my ($from_mode_oct, $from_mode_str, $from_file_type);
2966 if ($diff->{'to_mode'} ne ('0' x 6)) {
2967 $to_mode_oct = oct $diff->{'to_mode'};
2968 if (S_ISREG($to_mode_oct)) { # only for regular file
2969 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2970 }
2971 $to_file_type = file_type($diff->{'to_mode'});
2972 }
2973 if ($diff->{'from_mode'} ne ('0' x 6)) {
2974 $from_mode_oct = oct $diff->{'from_mode'};
2975 if (S_ISREG($to_mode_oct)) { # only for regular file
2976 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2977 }
2978 $from_file_type = file_type($diff->{'from_mode'});
2979 }
2980
2981 if ($diff->{'status'} eq "A") { # created
2982 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2983 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2984 $mode_chng .= "]</span>";
2985 print "<td>";
2986 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2987 hash_base=>$hash, file_name=>$diff->{'file'}),
2988 -class => "list"}, esc_path($diff->{'file'}));
2989 print "</td>\n";
2990 print "<td>$mode_chng</td>\n";
2991 print "<td class=\"link\">";
2992 if ($action eq 'commitdiff') {
2993 # link to patch
2994 $patchno++;
2995 print $cgi->a({-href => "#patch$patchno"}, "patch");
2996 print " | ";
2997 }
2998 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2999 hash_base=>$hash, file_name=>$diff->{'file'})},
3000 "blob");
3001 print "</td>\n";
3002
3003 } elsif ($diff->{'status'} eq "D") { # deleted
3004 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3005 print "<td>";
3006 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3007 hash_base=>$parent, file_name=>$diff->{'file'}),
3008 -class => "list"}, esc_path($diff->{'file'}));
3009 print "</td>\n";
3010 print "<td>$mode_chng</td>\n";
3011 print "<td class=\"link\">";
3012 if ($action eq 'commitdiff') {
3013 # link to patch
3014 $patchno++;
3015 print $cgi->a({-href => "#patch$patchno"}, "patch");
3016 print " | ";
3017 }
3018 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3019 hash_base=>$parent, file_name=>$diff->{'file'})},
3020 "blob") . " | ";
3021 if ($have_blame) {
3022 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3023 file_name=>$diff->{'file'})},
3024 "blame") . " | ";
3025 }
3026 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3027 file_name=>$diff->{'file'})},
3028 "history");
3029 print "</td>\n";
3030
3031 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3032 my $mode_chnge = "";
3033 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3034 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3035 if ($from_file_type ne $to_file_type) {
3036 $mode_chnge .= " from $from_file_type to $to_file_type";
3037 }
3038 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3039 if ($from_mode_str && $to_mode_str) {
3040 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3041 } elsif ($to_mode_str) {
3042 $mode_chnge .= " mode: $to_mode_str";
3043 }
3044 }
3045 $mode_chnge .= "]</span>\n";
3046 }
3047 print "<td>";
3048 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3049 hash_base=>$hash, file_name=>$diff->{'file'}),
3050 -class => "list"}, esc_path($diff->{'file'}));
3051 print "</td>\n";
3052 print "<td>$mode_chnge</td>\n";
3053 print "<td class=\"link\">";
3054 if ($action eq 'commitdiff') {
3055 # link to patch
3056 $patchno++;
3057 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3058 " | ";
3059 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3060 # "commit" view and modified file (not onlu mode changed)
3061 print $cgi->a({-href => href(action=>"blobdiff",
3062 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3063 hash_base=>$hash, hash_parent_base=>$parent,
3064 file_name=>$diff->{'file'})},
3065 "diff") .
3066 " | ";
3067 }
3068 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3069 hash_base=>$hash, file_name=>$diff->{'file'})},
3070 "blob") . " | ";
3071 if ($have_blame) {
3072 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3073 file_name=>$diff->{'file'})},
3074 "blame") . " | ";
3075 }
3076 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3077 file_name=>$diff->{'file'})},
3078 "history");
3079 print "</td>\n";
3080
3081 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3082 my %status_name = ('R' => 'moved', 'C' => 'copied');
3083 my $nstatus = $status_name{$diff->{'status'}};
3084 my $mode_chng = "";
3085 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3086 # mode also for directories, so we cannot use $to_mode_str
3087 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3088 }
3089 print "<td>" .
3090 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3091 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3092 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3093 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3094 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3095 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3096 -class => "list"}, esc_path($diff->{'from_file'})) .
3097 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3098 "<td class=\"link\">";
3099 if ($action eq 'commitdiff') {
3100 # link to patch
3101 $patchno++;
3102 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3103 " | ";
3104 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3105 # "commit" view and modified file (not only pure rename or copy)
3106 print $cgi->a({-href => href(action=>"blobdiff",
3107 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3108 hash_base=>$hash, hash_parent_base=>$parent,
3109 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3110 "diff") .
3111 " | ";
3112 }
3113 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3114 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3115 "blob") . " | ";
3116 if ($have_blame) {
3117 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3118 file_name=>$diff->{'to_file'})},
3119 "blame") . " | ";
3120 }
3121 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3122 file_name=>$diff->{'to_file'})},
3123 "history");
3124 print "</td>\n";
3125
3126 } # we should not encounter Unmerged (U) or Unknown (X) status
3127 print "</tr>\n";
3128 }
3129 print "</tbody>" if $has_header;
3130 print "</table>\n";
3131}
3132
3133sub git_patchset_body {
3134 my ($fd, $difftree, $hash, @hash_parents) = @_;
3135 my ($hash_parent) = $hash_parents[0];
3136
3137 my $patch_idx = 0;
3138 my $patch_number = 0;
3139 my $patch_line;
3140 my $diffinfo;
3141 my (%from, %to);
3142
3143 print "<div class=\"patchset\">\n";
3144
3145 # skip to first patch
3146 while ($patch_line = <$fd>) {
3147 chomp $patch_line;
3148
3149 last if ($patch_line =~ m/^diff /);
3150 }
3151
3152 PATCH:
3153 while ($patch_line) {
3154 my @diff_header;
3155 my ($from_id, $to_id);
3156
3157 # git diff header
3158 #assert($patch_line =~ m/^diff /) if DEBUG;
3159 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3160 $patch_number++;
3161 push @diff_header, $patch_line;
3162
3163 # extended diff header
3164 EXTENDED_HEADER:
3165 while ($patch_line = <$fd>) {
3166 chomp $patch_line;
3167
3168 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3169
3170 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
3171 $from_id = $1;
3172 $to_id = $2;
3173 } elsif ($patch_line =~ m/^index ((?:[0-9a-fA-F]{40},)+[0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
3174 $from_id = [ split(',', $1) ];
3175 $to_id = $2;
3176 }
3177
3178 push @diff_header, $patch_line;
3179 }
3180 my $last_patch_line = $patch_line;
3181
3182 # check if current patch belong to current raw line
3183 # and parse raw git-diff line if needed
3184 if (defined $diffinfo &&
3185 defined $from_id && defined $to_id &&
3186 from_ids_eq($diffinfo->{'from_id'}, $from_id) &&
3187 $diffinfo->{'to_id'} eq $to_id) {
3188 # this is continuation of a split patch
3189 print "<div class=\"patch cont\">\n";
3190 } else {
3191 # advance raw git-diff output if needed
3192 $patch_idx++ if defined $diffinfo;
3193
3194 # compact combined diff output can have some patches skipped
3195 # find which patch (using pathname of result) we are at now
3196 my $to_name;
3197 if ($diff_header[0] =~ m!^diff --cc "?(.*)"?$!) {
3198 $to_name = $1;
3199 }
3200
3201 do {
3202 # read and prepare patch information
3203 if (ref($difftree->[$patch_idx]) eq "HASH") {
3204 # pre-parsed (or generated by hand)
3205 $diffinfo = $difftree->[$patch_idx];
3206 } else {
3207 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
3208 }
3209
3210 # check if current raw line has no patch (it got simplified)
3211 if (defined $to_name && $to_name ne $diffinfo->{'to_file'}) {
3212 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3213 format_diff_cc_simplified($diffinfo, @hash_parents) .
3214 "</div>\n"; # class="patch"
3215
3216 $patch_idx++;
3217 $patch_number++;
3218 }
3219 } until (!defined $to_name || $to_name eq $diffinfo->{'to_file'} ||
3220 $patch_idx > $#$difftree);
3221
3222 # modifies %from, %to hashes
3223 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3224
3225 # this is first patch for raw difftree line with $patch_idx index
3226 # we index @$difftree array from 0, but number patches from 1
3227 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3228 }
3229
3230 # print "git diff" header
3231 $patch_line = shift @diff_header;
3232 print format_git_diff_header_line($patch_line, $diffinfo,
3233 \%from, \%to);
3234
3235 # print extended diff header
3236 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
3237 EXTENDED_HEADER:
3238 foreach $patch_line (@diff_header) {
3239 print format_extended_diff_header_line($patch_line, $diffinfo,
3240 \%from, \%to);
3241 }
3242 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
3243
3244 # from-file/to-file diff header
3245 $patch_line = $last_patch_line;
3246 if (! $patch_line) {
3247 print "</div>\n"; # class="patch"
3248 last PATCH;
3249 }
3250 next PATCH if ($patch_line =~ m/^diff /);
3251 #assert($patch_line =~ m/^---/) if DEBUG;
3252 #assert($patch_line eq $last_patch_line) if DEBUG;
3253
3254 $patch_line = <$fd>;
3255 chomp $patch_line;
3256 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3257
3258 print format_diff_from_to_header($last_patch_line, $patch_line,
3259 $diffinfo, \%from, \%to,
3260 @hash_parents);
3261
3262 # the patch itself
3263 LINE:
3264 while ($patch_line = <$fd>) {
3265 chomp $patch_line;
3266
3267 next PATCH if ($patch_line =~ m/^diff /);
3268
3269 print format_diff_line($patch_line, \%from, \%to);
3270 }
3271
3272 } continue {
3273 print "</div>\n"; # class="patch"
3274 }
3275
3276 # for compact combined (--cc) format, with chunk and patch simpliciaction
3277 # patchset might be empty, but there might be unprocessed raw lines
3278 for ($patch_idx++ if $patch_number > 0;
3279 $patch_idx < @$difftree;
3280 $patch_idx++) {
3281 # read and prepare patch information
3282 if (ref($difftree->[$patch_idx]) eq "HASH") {
3283 # pre-parsed (or generated by hand)
3284 $diffinfo = $difftree->[$patch_idx];
3285 } else {
3286 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
3287 }
3288
3289 # generate anchor for "patch" links in difftree / whatchanged part
3290 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3291 format_diff_cc_simplified($diffinfo, @hash_parents) .
3292 "</div>\n"; # class="patch"
3293
3294 $patch_number++;
3295 }
3296
3297 if ($patch_number == 0) {
3298 if (@hash_parents > 1) {
3299 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3300 } else {
3301 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3302 }
3303 }
3304
3305 print "</div>\n"; # class="patchset"
3306}
3307
3308# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3309
3310sub git_project_list_body {
3311 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3312
3313 my ($check_forks) = gitweb_check_feature('forks');
3314
3315 my @projects;
3316 foreach my $pr (@$projlist) {
3317 my (@aa) = git_get_last_activity($pr->{'path'});
3318 unless (@aa) {
3319 next;
3320 }
3321 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
3322 if (!defined $pr->{'descr'}) {
3323 my $descr = git_get_project_description($pr->{'path'}) || "";
3324 $pr->{'descr_long'} = to_utf8($descr);
3325 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3326 }
3327 if (!defined $pr->{'owner'}) {
3328 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3329 }
3330 if ($check_forks) {
3331 my $pname = $pr->{'path'};
3332 if (($pname =~ s/\.git$//) &&
3333 ($pname !~ /\/$/) &&
3334 (-d "$projectroot/$pname")) {
3335 $pr->{'forks'} = "-d $projectroot/$pname";
3336 }
3337 else {
3338 $pr->{'forks'} = 0;
3339 }
3340 }
3341 push @projects, $pr;
3342 }
3343
3344 $order ||= $default_projects_order;
3345 $from = 0 unless defined $from;
3346 $to = $#projects if (!defined $to || $#projects < $to);
3347
3348 print "<table class=\"project_list\">\n";
3349 unless ($no_header) {
3350 print "<tr>\n";
3351 if ($check_forks) {
3352 print "<th></th>\n";
3353 }
3354 if ($order eq "project") {
3355 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3356 print "<th>Project</th>\n";
3357 } else {
3358 print "<th>" .
3359 $cgi->a({-href => href(project=>undef, order=>'project'),
3360 -class => "header"}, "Project") .
3361 "</th>\n";
3362 }
3363 if ($order eq "descr") {
3364 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3365 print "<th>Description</th>\n";
3366 } else {
3367 print "<th>" .
3368 $cgi->a({-href => href(project=>undef, order=>'descr'),
3369 -class => "header"}, "Description") .
3370 "</th>\n";
3371 }
3372 if ($order eq "owner") {
3373 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3374 print "<th>Owner</th>\n";
3375 } else {
3376 print "<th>" .
3377 $cgi->a({-href => href(project=>undef, order=>'owner'),
3378 -class => "header"}, "Owner") .
3379 "</th>\n";
3380 }
3381 if ($order eq "age") {
3382 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3383 print "<th>Last Change</th>\n";
3384 } else {
3385 print "<th>" .
3386 $cgi->a({-href => href(project=>undef, order=>'age'),
3387 -class => "header"}, "Last Change") .
3388 "</th>\n";
3389 }
3390 print "<th></th>\n" .
3391 "</tr>\n";
3392 }
3393 my $alternate = 1;
3394 for (my $i = $from; $i <= $to; $i++) {
3395 my $pr = $projects[$i];
3396 if ($alternate) {
3397 print "<tr class=\"dark\">\n";
3398 } else {
3399 print "<tr class=\"light\">\n";
3400 }
3401 $alternate ^= 1;
3402 if ($check_forks) {
3403 print "<td>";
3404 if ($pr->{'forks'}) {
3405 print "<!-- $pr->{'forks'} -->\n";
3406 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3407 }
3408 print "</td>\n";
3409 }
3410 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3411 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3412 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3413 -class => "list", -title => $pr->{'descr_long'}},
3414 esc_html($pr->{'descr'})) . "</td>\n" .
3415 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3416 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3417 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3418 "<td class=\"link\">" .
3419 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3420 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3421 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3422 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3423 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3424 "</td>\n" .
3425 "</tr>\n";
3426 }
3427 if (defined $extra) {
3428 print "<tr>\n";
3429 if ($check_forks) {
3430 print "<td></td>\n";
3431 }
3432 print "<td colspan=\"5\">$extra</td>\n" .
3433 "</tr>\n";
3434 }
3435 print "</table>\n";
3436}
3437
3438sub git_shortlog_body {
3439 # uses global variable $project
3440 my ($commitlist, $from, $to, $refs, $extra) = @_;
3441
3442 $from = 0 unless defined $from;
3443 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3444
3445 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
3446 my $alternate = 1;
3447 for (my $i = $from; $i <= $to; $i++) {
3448 my %co = %{$commitlist->[$i]};
3449 my $commit = $co{'id'};
3450 my $ref = format_ref_marker($refs, $commit);
3451 if ($alternate) {
3452 print "<tr class=\"dark\">\n";
3453 } else {
3454 print "<tr class=\"light\">\n";
3455 }
3456 $alternate ^= 1;
3457 my $author = chop_and_escape_str($co{'author_name'}, 10);
3458 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3459 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3460 "<td><i>" . $author . "</i></td>\n" .
3461 "<td>";
3462 print format_subject_html($co{'title'}, $co{'title_short'},
3463 href(action=>"commit", hash=>$commit), $ref);
3464 print "</td>\n" .
3465 "<td class=\"link\">" .
3466 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3467 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3468 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3469 my $snapshot_links = format_snapshot_links($commit);
3470 if (defined $snapshot_links) {
3471 print " | " . $snapshot_links;
3472 }
3473 print "</td>\n" .
3474 "</tr>\n";
3475 }
3476 if (defined $extra) {
3477 print "<tr>\n" .
3478 "<td colspan=\"4\">$extra</td>\n" .
3479 "</tr>\n";
3480 }
3481 print "</table>\n";
3482}
3483
3484sub git_history_body {
3485 # Warning: assumes constant type (blob or tree) during history
3486 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3487
3488 $from = 0 unless defined $from;
3489 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3490
3491 print "<table class=\"history\" cellspacing=\"0\">\n";
3492 my $alternate = 1;
3493 for (my $i = $from; $i <= $to; $i++) {
3494 my %co = %{$commitlist->[$i]};
3495 if (!%co) {
3496 next;
3497 }
3498 my $commit = $co{'id'};
3499
3500 my $ref = format_ref_marker($refs, $commit);
3501
3502 if ($alternate) {
3503 print "<tr class=\"dark\">\n";
3504 } else {
3505 print "<tr class=\"light\">\n";
3506 }
3507 $alternate ^= 1;
3508 # shortlog uses chop_str($co{'author_name'}, 10)
3509 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
3510 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3511 "<td><i>" . $author . "</i></td>\n" .
3512 "<td>";
3513 # originally git_history used chop_str($co{'title'}, 50)
3514 print format_subject_html($co{'title'}, $co{'title_short'},
3515 href(action=>"commit", hash=>$commit), $ref);
3516 print "</td>\n" .
3517 "<td class=\"link\">" .
3518 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3519 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3520
3521 if ($ftype eq 'blob') {
3522 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3523 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3524 if (defined $blob_current && defined $blob_parent &&
3525 $blob_current ne $blob_parent) {
3526 print " | " .
3527 $cgi->a({-href => href(action=>"blobdiff",
3528 hash=>$blob_current, hash_parent=>$blob_parent,
3529 hash_base=>$hash_base, hash_parent_base=>$commit,
3530 file_name=>$file_name)},
3531 "diff to current");
3532 }
3533 }
3534 print "</td>\n" .
3535 "</tr>\n";
3536 }
3537 if (defined $extra) {
3538 print "<tr>\n" .
3539 "<td colspan=\"4\">$extra</td>\n" .
3540 "</tr>\n";
3541 }
3542 print "</table>\n";
3543}
3544
3545sub git_tags_body {
3546 # uses global variable $project
3547 my ($taglist, $from, $to, $extra) = @_;
3548 $from = 0 unless defined $from;
3549 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3550
3551 print "<table class=\"tags\" cellspacing=\"0\">\n";
3552 my $alternate = 1;
3553 for (my $i = $from; $i <= $to; $i++) {
3554 my $entry = $taglist->[$i];
3555 my %tag = %$entry;
3556 my $comment = $tag{'subject'};
3557 my $comment_short;
3558 if (defined $comment) {
3559 $comment_short = chop_str($comment, 30, 5);
3560 }
3561 if ($alternate) {
3562 print "<tr class=\"dark\">\n";
3563 } else {
3564 print "<tr class=\"light\">\n";
3565 }
3566 $alternate ^= 1;
3567 if (defined $tag{'age'}) {
3568 print "<td><i>$tag{'age'}</i></td>\n";
3569 } else {
3570 print "<td></td>\n";
3571 }
3572 print "<td>" .
3573 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3574 -class => "list name"}, esc_html($tag{'name'})) .
3575 "</td>\n" .
3576 "<td>";
3577 if (defined $comment) {
3578 print format_subject_html($comment, $comment_short,
3579 href(action=>"tag", hash=>$tag{'id'}));
3580 }
3581 print "</td>\n" .
3582 "<td class=\"selflink\">";
3583 if ($tag{'type'} eq "tag") {
3584 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3585 } else {
3586 print " ";
3587 }
3588 print "</td>\n" .
3589 "<td class=\"link\">" . " | " .
3590 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3591 if ($tag{'reftype'} eq "commit") {
3592 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
3593 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
3594 } elsif ($tag{'reftype'} eq "blob") {
3595 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3596 }
3597 print "</td>\n" .
3598 "</tr>";
3599 }
3600 if (defined $extra) {
3601 print "<tr>\n" .
3602 "<td colspan=\"5\">$extra</td>\n" .
3603 "</tr>\n";
3604 }
3605 print "</table>\n";
3606}
3607
3608sub git_heads_body {
3609 # uses global variable $project
3610 my ($headlist, $head, $from, $to, $extra) = @_;
3611 $from = 0 unless defined $from;
3612 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3613
3614 print "<table class=\"heads\" cellspacing=\"0\">\n";
3615 my $alternate = 1;
3616 for (my $i = $from; $i <= $to; $i++) {
3617 my $entry = $headlist->[$i];
3618 my %ref = %$entry;
3619 my $curr = $ref{'id'} eq $head;
3620 if ($alternate) {
3621 print "<tr class=\"dark\">\n";
3622 } else {
3623 print "<tr class=\"light\">\n";
3624 }
3625 $alternate ^= 1;
3626 print "<td><i>$ref{'age'}</i></td>\n" .
3627 ($curr ? "<td class=\"current_head\">" : "<td>") .
3628 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3629 -class => "list name"},esc_html($ref{'name'})) .
3630 "</td>\n" .
3631 "<td class=\"link\">" .
3632 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3633 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3634 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
3635 "</td>\n" .
3636 "</tr>";
3637 }
3638 if (defined $extra) {
3639 print "<tr>\n" .
3640 "<td colspan=\"3\">$extra</td>\n" .
3641 "</tr>\n";
3642 }
3643 print "</table>\n";
3644}
3645
3646sub git_search_grep_body {
3647 my ($commitlist, $from, $to, $extra) = @_;
3648 $from = 0 unless defined $from;
3649 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3650
3651 print "<table class=\"grep\" cellspacing=\"0\">\n";
3652 my $alternate = 1;
3653 for (my $i = $from; $i <= $to; $i++) {
3654 my %co = %{$commitlist->[$i]};
3655 if (!%co) {
3656 next;
3657 }
3658 my $commit = $co{'id'};
3659 if ($alternate) {
3660 print "<tr class=\"dark\">\n";
3661 } else {
3662 print "<tr class=\"light\">\n";
3663 }
3664 $alternate ^= 1;
3665 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
3666 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3667 "<td><i>" . $author . "</i></td>\n" .
3668 "<td>" .
3669 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3670 chop_and_escape_str($co{'title'}, 50) . "<br/>");
3671 my $comment = $co{'comment'};
3672 foreach my $line (@$comment) {
3673 if ($line =~ m/^(.*)($search_regexp)(.*)$/i) {
3674 my $lead = esc_html($1) || "";
3675 $lead = chop_str($lead, 30, 10);
3676 my $match = esc_html($2) || "";
3677 my $trail = esc_html($3) || "";
3678 $trail = chop_str($trail, 30, 10);
3679 my $text = "$lead<span class=\"match\">$match</span>$trail";
3680 print chop_str($text, 80, 5) . "<br/>\n";
3681 }
3682 }
3683 print "</td>\n" .
3684 "<td class=\"link\">" .
3685 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3686 " | " .
3687 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3688 print "</td>\n" .
3689 "</tr>\n";
3690 }
3691 if (defined $extra) {
3692 print "<tr>\n" .
3693 "<td colspan=\"3\">$extra</td>\n" .
3694 "</tr>\n";
3695 }
3696 print "</table>\n";
3697}
3698
3699## ======================================================================
3700## ======================================================================
3701## actions
3702
3703sub git_project_list {
3704 my $order = $cgi->param('o');
3705 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3706 die_error(undef, "Unknown order parameter");
3707 }
3708
3709 my @list = git_get_projects_list();
3710 if (!@list) {
3711 die_error(undef, "No projects found");
3712 }
3713
3714 git_header_html();
3715 if (-f $home_text) {
3716 print "<div class=\"index_include\">\n";
3717 open (my $fd, $home_text);
3718 print <$fd>;
3719 close $fd;
3720 print "</div>\n";
3721 }
3722 git_project_list_body(\@list, $order);
3723 git_footer_html();
3724}
3725
3726sub git_forks {
3727 my $order = $cgi->param('o');
3728 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3729 die_error(undef, "Unknown order parameter");
3730 }
3731
3732 my @list = git_get_projects_list($project);
3733 if (!@list) {
3734 die_error(undef, "No forks found");
3735 }
3736
3737 git_header_html();
3738 git_print_page_nav('','');
3739 git_print_header_div('summary', "$project forks");
3740 git_project_list_body(\@list, $order);
3741 git_footer_html();
3742}
3743
3744sub git_project_index {
3745 my @projects = git_get_projects_list($project);
3746
3747 print $cgi->header(
3748 -type => 'text/plain',
3749 -charset => 'utf-8',
3750 -content_disposition => 'inline; filename="index.aux"');
3751
3752 foreach my $pr (@projects) {
3753 if (!exists $pr->{'owner'}) {
3754 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
3755 }
3756
3757 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3758 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3759 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3760 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3761 $path =~ s/ /\+/g;
3762 $owner =~ s/ /\+/g;
3763
3764 print "$path $owner\n";
3765 }
3766}
3767
3768sub git_summary {
3769 my $descr = git_get_project_description($project) || "none";
3770 my %co = parse_commit("HEAD");
3771 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3772 my $head = $co{'id'};
3773
3774 my $owner = git_get_project_owner($project);
3775
3776 my $refs = git_get_references();
3777 # These get_*_list functions return one more to allow us to see if
3778 # there are more ...
3779 my @taglist = git_get_tags_list(16);
3780 my @headlist = git_get_heads_list(16);
3781 my @forklist;
3782 my ($check_forks) = gitweb_check_feature('forks');
3783
3784 if ($check_forks) {
3785 @forklist = git_get_projects_list($project);
3786 }
3787
3788 git_header_html();
3789 git_print_page_nav('summary','', $head);
3790
3791 print "<div class=\"title\"> </div>\n";
3792 print "<table cellspacing=\"0\">\n" .
3793 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3794 "<tr><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
3795 if (defined $cd{'rfc2822'}) {
3796 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3797 }
3798
3799 # use per project git URL list in $projectroot/$project/cloneurl
3800 # or make project git URL from git base URL and project name
3801 my $url_tag = "URL";
3802 my @url_list = git_get_project_url_list($project);
3803 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3804 foreach my $git_url (@url_list) {
3805 next unless $git_url;
3806 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3807 $url_tag = "";
3808 }
3809 print "</table>\n";
3810
3811 if (-s "$projectroot/$project/README.html") {
3812 if (open my $fd, "$projectroot/$project/README.html") {
3813 print "<div class=\"title\">readme</div>\n";
3814 print $_ while (<$fd>);
3815 close $fd;
3816 }
3817 }
3818
3819 # we need to request one more than 16 (0..15) to check if
3820 # those 16 are all
3821 my @commitlist = $head ? parse_commits($head, 17) : ();
3822 if (@commitlist) {
3823 git_print_header_div('shortlog');
3824 git_shortlog_body(\@commitlist, 0, 15, $refs,
3825 $#commitlist <= 15 ? undef :
3826 $cgi->a({-href => href(action=>"shortlog")}, "..."));
3827 }
3828
3829 if (@taglist) {
3830 git_print_header_div('tags');
3831 git_tags_body(\@taglist, 0, 15,
3832 $#taglist <= 15 ? undef :
3833 $cgi->a({-href => href(action=>"tags")}, "..."));
3834 }
3835
3836 if (@headlist) {
3837 git_print_header_div('heads');
3838 git_heads_body(\@headlist, $head, 0, 15,
3839 $#headlist <= 15 ? undef :
3840 $cgi->a({-href => href(action=>"heads")}, "..."));
3841 }
3842
3843 if (@forklist) {
3844 git_print_header_div('forks');
3845 git_project_list_body(\@forklist, undef, 0, 15,
3846 $#forklist <= 15 ? undef :
3847 $cgi->a({-href => href(action=>"forks")}, "..."),
3848 'noheader');
3849 }
3850
3851 git_footer_html();
3852}
3853
3854sub git_tag {
3855 my $head = git_get_head_hash($project);
3856 git_header_html();
3857 git_print_page_nav('','', $head,undef,$head);
3858 my %tag = parse_tag($hash);
3859
3860 if (! %tag) {
3861 die_error(undef, "Unknown tag object");
3862 }
3863
3864 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3865 print "<div class=\"title_text\">\n" .
3866 "<table cellspacing=\"0\">\n" .
3867 "<tr>\n" .
3868 "<td>object</td>\n" .
3869 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3870 $tag{'object'}) . "</td>\n" .
3871 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3872 $tag{'type'}) . "</td>\n" .
3873 "</tr>\n";
3874 if (defined($tag{'author'})) {
3875 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3876 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3877 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3878 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3879 "</td></tr>\n";
3880 }
3881 print "</table>\n\n" .
3882 "</div>\n";
3883 print "<div class=\"page_body\">";
3884 my $comment = $tag{'comment'};
3885 foreach my $line (@$comment) {
3886 chomp $line;
3887 print esc_html($line, -nbsp=>1) . "<br/>\n";
3888 }
3889 print "</div>\n";
3890 git_footer_html();
3891}
3892
3893sub git_blame2 {
3894 my $fd;
3895 my $ftype;
3896
3897 my ($have_blame) = gitweb_check_feature('blame');
3898 if (!$have_blame) {
3899 die_error('403 Permission denied', "Permission denied");
3900 }
3901 die_error('404 Not Found', "File name not defined") if (!$file_name);
3902 $hash_base ||= git_get_head_hash($project);
3903 die_error(undef, "Couldn't find base commit") unless ($hash_base);
3904 my %co = parse_commit($hash_base)
3905 or die_error(undef, "Reading commit failed");
3906 if (!defined $hash) {
3907 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3908 or die_error(undef, "Error looking up file");
3909 }
3910 $ftype = git_get_type($hash);
3911 if ($ftype !~ "blob") {
3912 die_error('400 Bad Request', "Object is not a blob");
3913 }
3914 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3915 $file_name, $hash_base)
3916 or die_error(undef, "Open git-blame failed");
3917 git_header_html();
3918 my $formats_nav =
3919 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3920 "blob") .
3921 " | " .
3922 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3923 "history") .
3924 " | " .
3925 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3926 "HEAD");
3927 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3928 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3929 git_print_page_path($file_name, $ftype, $hash_base);
3930 my @rev_color = (qw(light2 dark2));
3931 my $num_colors = scalar(@rev_color);
3932 my $current_color = 0;
3933 my $last_rev;
3934 print <<HTML;
3935<div class="page_body">
3936<table class="blame">
3937<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3938HTML
3939 my %metainfo = ();
3940 while (1) {
3941 $_ = <$fd>;
3942 last unless defined $_;
3943 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3944 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3945 if (!exists $metainfo{$full_rev}) {
3946 $metainfo{$full_rev} = {};
3947 }
3948 my $meta = $metainfo{$full_rev};
3949 while (<$fd>) {
3950 last if (s/^\t//);
3951 if (/^(\S+) (.*)$/) {
3952 $meta->{$1} = $2;
3953 }
3954 }
3955 my $data = $_;
3956 chomp $data;
3957 my $rev = substr($full_rev, 0, 8);
3958 my $author = $meta->{'author'};
3959 my %date = parse_date($meta->{'author-time'},
3960 $meta->{'author-tz'});
3961 my $date = $date{'iso-tz'};
3962 if ($group_size) {
3963 $current_color = ++$current_color % $num_colors;
3964 }
3965 print "<tr class=\"$rev_color[$current_color]\">\n";
3966 if ($group_size) {
3967 print "<td class=\"sha1\"";
3968 print " title=\"". esc_html($author) . ", $date\"";
3969 print " rowspan=\"$group_size\"" if ($group_size > 1);
3970 print ">";
3971 print $cgi->a({-href => href(action=>"commit",
3972 hash=>$full_rev,
3973 file_name=>$file_name)},
3974 esc_html($rev));
3975 print "</td>\n";
3976 }
3977 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
3978 or die_error(undef, "Open git-rev-parse failed");
3979 my $parent_commit = <$dd>;
3980 close $dd;
3981 chomp($parent_commit);
3982 my $blamed = href(action => 'blame',
3983 file_name => $meta->{'filename'},
3984 hash_base => $parent_commit);
3985 print "<td class=\"linenr\">";
3986 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3987 -id => "l$lineno",
3988 -class => "linenr" },
3989 esc_html($lineno));
3990 print "</td>";
3991 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3992 print "</tr>\n";
3993 }
3994 print "</table>\n";
3995 print "</div>";
3996 close $fd
3997 or print "Reading blob failed\n";
3998 git_footer_html();
3999}
4000
4001sub git_blame {
4002 my $fd;
4003
4004 my ($have_blame) = gitweb_check_feature('blame');
4005 if (!$have_blame) {
4006 die_error('403 Permission denied', "Permission denied");
4007 }
4008 die_error('404 Not Found', "File name not defined") if (!$file_name);
4009 $hash_base ||= git_get_head_hash($project);
4010 die_error(undef, "Couldn't find base commit") unless ($hash_base);
4011 my %co = parse_commit($hash_base)
4012 or die_error(undef, "Reading commit failed");
4013 if (!defined $hash) {
4014 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4015 or die_error(undef, "Error lookup file");
4016 }
4017 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
4018 or die_error(undef, "Open git-annotate failed");
4019 git_header_html();
4020 my $formats_nav =
4021 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4022 "blob") .
4023 " | " .
4024 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4025 "history") .
4026 " | " .
4027 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4028 "HEAD");
4029 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4030 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4031 git_print_page_path($file_name, 'blob', $hash_base);
4032 print "<div class=\"page_body\">\n";
4033 print <<HTML;
4034<table class="blame">
4035 <tr>
4036 <th>Commit</th>
4037 <th>Age</th>
4038 <th>Author</th>
4039 <th>Line</th>
4040 <th>Data</th>
4041 </tr>
4042HTML
4043 my @line_class = (qw(light dark));
4044 my $line_class_len = scalar (@line_class);
4045 my $line_class_num = $#line_class;
4046 while (my $line = <$fd>) {
4047 my $long_rev;
4048 my $short_rev;
4049 my $author;
4050 my $time;
4051 my $lineno;
4052 my $data;
4053 my $age;
4054 my $age_str;
4055 my $age_class;
4056
4057 chomp $line;
4058 $line_class_num = ($line_class_num + 1) % $line_class_len;
4059
4060 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
4061 $long_rev = $1;
4062 $author = $2;
4063 $time = $3;
4064 $lineno = $4;
4065 $data = $5;
4066 } else {
4067 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
4068 next;
4069 }
4070 $short_rev = substr ($long_rev, 0, 8);
4071 $age = time () - $time;
4072 $age_str = age_string ($age);
4073 $age_str =~ s/ / /g;
4074 $age_class = age_class($age);
4075 $author = esc_html ($author);
4076 $author =~ s/ / /g;
4077
4078 $data = untabify($data);
4079 $data = esc_html ($data);
4080
4081 print <<HTML;
4082 <tr class="$line_class[$line_class_num]">
4083 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
4084 <td class="$age_class">$age_str</td>
4085 <td>$author</td>
4086 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
4087 <td class="pre">$data</td>
4088 </tr>
4089HTML
4090 } # while (my $line = <$fd>)
4091 print "</table>\n\n";
4092 close $fd
4093 or print "Reading blob failed.\n";
4094 print "</div>";
4095 git_footer_html();
4096}
4097
4098sub git_tags {
4099 my $head = git_get_head_hash($project);
4100 git_header_html();
4101 git_print_page_nav('','', $head,undef,$head);
4102 git_print_header_div('summary', $project);
4103
4104 my @tagslist = git_get_tags_list();
4105 if (@tagslist) {
4106 git_tags_body(\@tagslist);
4107 }
4108 git_footer_html();
4109}
4110
4111sub git_heads {
4112 my $head = git_get_head_hash($project);
4113 git_header_html();
4114 git_print_page_nav('','', $head,undef,$head);
4115 git_print_header_div('summary', $project);
4116
4117 my @headslist = git_get_heads_list();
4118 if (@headslist) {
4119 git_heads_body(\@headslist, $head);
4120 }
4121 git_footer_html();
4122}
4123
4124sub git_blob_plain {
4125 my $expires;
4126
4127 if (!defined $hash) {
4128 if (defined $file_name) {
4129 my $base = $hash_base || git_get_head_hash($project);
4130 $hash = git_get_hash_by_path($base, $file_name, "blob")
4131 or die_error(undef, "Error lookup file");
4132 } else {
4133 die_error(undef, "No file name defined");
4134 }
4135 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4136 # blobs defined by non-textual hash id's can be cached
4137 $expires = "+1d";
4138 }
4139
4140 my $type = shift;
4141 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4142 or die_error(undef, "Couldn't cat $file_name, $hash");
4143
4144 $type ||= blob_mimetype($fd, $file_name);
4145
4146 # save as filename, even when no $file_name is given
4147 my $save_as = "$hash";
4148 if (defined $file_name) {
4149 $save_as = $file_name;
4150 } elsif ($type =~ m/^text\//) {
4151 $save_as .= '.txt';
4152 }
4153
4154 print $cgi->header(
4155 -type => "$type",
4156 -expires=>$expires,
4157 -content_disposition => 'inline; filename="' . "$save_as" . '"');
4158 undef $/;
4159 binmode STDOUT, ':raw';
4160 print <$fd>;
4161 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4162 $/ = "\n";
4163 close $fd;
4164}
4165
4166sub git_blob {
4167 my $expires;
4168
4169 if (!defined $hash) {
4170 if (defined $file_name) {
4171 my $base = $hash_base || git_get_head_hash($project);
4172 $hash = git_get_hash_by_path($base, $file_name, "blob")
4173 or die_error(undef, "Error lookup file");
4174 } else {
4175 die_error(undef, "No file name defined");
4176 }
4177 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4178 # blobs defined by non-textual hash id's can be cached
4179 $expires = "+1d";
4180 }
4181
4182 my ($have_blame) = gitweb_check_feature('blame');
4183 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4184 or die_error(undef, "Couldn't cat $file_name, $hash");
4185 my $mimetype = blob_mimetype($fd, $file_name);
4186 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
4187 close $fd;
4188 return git_blob_plain($mimetype);
4189 }
4190 # we can have blame only for text/* mimetype
4191 $have_blame &&= ($mimetype =~ m!^text/!);
4192
4193 git_header_html(undef, $expires);
4194 my $formats_nav = '';
4195 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4196 if (defined $file_name) {
4197 if ($have_blame) {
4198 $formats_nav .=
4199 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
4200 hash=>$hash, file_name=>$file_name)},
4201 "blame") .
4202 " | ";
4203 }
4204 $formats_nav .=
4205 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4206 hash=>$hash, file_name=>$file_name)},
4207 "history") .
4208 " | " .
4209 $cgi->a({-href => href(action=>"blob_plain",
4210 hash=>$hash, file_name=>$file_name)},
4211 "raw") .
4212 " | " .
4213 $cgi->a({-href => href(action=>"blob",
4214 hash_base=>"HEAD", file_name=>$file_name)},
4215 "HEAD");
4216 } else {
4217 $formats_nav .=
4218 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
4219 }
4220 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4221 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4222 } else {
4223 print "<div class=\"page_nav\">\n" .
4224 "<br/><br/></div>\n" .
4225 "<div class=\"title\">$hash</div>\n";
4226 }
4227 git_print_page_path($file_name, "blob", $hash_base);
4228 print "<div class=\"page_body\">\n";
4229 if ($mimetype =~ m!^text/!) {
4230 my $nr;
4231 while (my $line = <$fd>) {
4232 chomp $line;
4233 $nr++;
4234 $line = untabify($line);
4235 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4236 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4237 }
4238 } elsif ($mimetype =~ m!^image/!) {
4239 print qq!<img type="$mimetype"!;
4240 if ($file_name) {
4241 print qq! alt="$file_name" title="$file_name"!;
4242 }
4243 print qq! src="! .
4244 href(action=>"blob_plain", hash=>$hash,
4245 hash_base=>$hash_base, file_name=>$file_name) .
4246 qq!" />\n!;
4247 }
4248 close $fd
4249 or print "Reading blob failed.\n";
4250 print "</div>";
4251 git_footer_html();
4252}
4253
4254sub git_tree {
4255 if (!defined $hash_base) {
4256 $hash_base = "HEAD";
4257 }
4258 if (!defined $hash) {
4259 if (defined $file_name) {
4260 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4261 } else {
4262 $hash = $hash_base;
4263 }
4264 }
4265 $/ = "\0";
4266 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4267 or die_error(undef, "Open git-ls-tree failed");
4268 my @entries = map { chomp; $_ } <$fd>;
4269 close $fd or die_error(undef, "Reading tree failed");
4270 $/ = "\n";
4271
4272 my $refs = git_get_references();
4273 my $ref = format_ref_marker($refs, $hash_base);
4274 git_header_html();
4275 my $basedir = '';
4276 my ($have_blame) = gitweb_check_feature('blame');
4277 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4278 my @views_nav = ();
4279 if (defined $file_name) {
4280 push @views_nav,
4281 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4282 hash=>$hash, file_name=>$file_name)},
4283 "history"),
4284 $cgi->a({-href => href(action=>"tree",
4285 hash_base=>"HEAD", file_name=>$file_name)},
4286 "HEAD"),
4287 }
4288 my $snapshot_links = format_snapshot_links($hash);
4289 if (defined $snapshot_links) {
4290 # FIXME: Should be available when we have no hash base as well.
4291 push @views_nav, $snapshot_links;
4292 }
4293 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4294 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4295 } else {
4296 undef $hash_base;
4297 print "<div class=\"page_nav\">\n";
4298 print "<br/><br/></div>\n";
4299 print "<div class=\"title\">$hash</div>\n";
4300 }
4301 if (defined $file_name) {
4302 $basedir = $file_name;
4303 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4304 $basedir .= '/';
4305 }
4306 }
4307 git_print_page_path($file_name, 'tree', $hash_base);
4308 print "<div class=\"page_body\">\n";
4309 print "<table cellspacing=\"0\">\n";
4310 my $alternate = 1;
4311 # '..' (top directory) link if possible
4312 if (defined $hash_base &&
4313 defined $file_name && $file_name =~ m![^/]+$!) {
4314 if ($alternate) {
4315 print "<tr class=\"dark\">\n";
4316 } else {
4317 print "<tr class=\"light\">\n";
4318 }
4319 $alternate ^= 1;
4320
4321 my $up = $file_name;
4322 $up =~ s!/?[^/]+$!!;
4323 undef $up unless $up;
4324 # based on git_print_tree_entry
4325 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4326 print '<td class="list">';
4327 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4328 file_name=>$up)},
4329 "..");
4330 print "</td>\n";
4331 print "<td class=\"link\"></td>\n";
4332
4333 print "</tr>\n";
4334 }
4335 foreach my $line (@entries) {
4336 my %t = parse_ls_tree_line($line, -z => 1);
4337
4338 if ($alternate) {
4339 print "<tr class=\"dark\">\n";
4340 } else {
4341 print "<tr class=\"light\">\n";
4342 }
4343 $alternate ^= 1;
4344
4345 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4346
4347 print "</tr>\n";
4348 }
4349 print "</table>\n" .
4350 "</div>";
4351 git_footer_html();
4352}
4353
4354sub git_snapshot {
4355 my @supported_fmts = gitweb_check_feature('snapshot');
4356 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4357
4358 my $format = $cgi->param('sf');
4359 if (!@supported_fmts) {
4360 die_error('403 Permission denied', "Permission denied");
4361 }
4362 # default to first supported snapshot format
4363 $format ||= $supported_fmts[0];
4364 if ($format !~ m/^[a-z0-9]+$/) {
4365 die_error(undef, "Invalid snapshot format parameter");
4366 } elsif (!exists($known_snapshot_formats{$format})) {
4367 die_error(undef, "Unknown snapshot format");
4368 } elsif (!grep($_ eq $format, @supported_fmts)) {
4369 die_error(undef, "Unsupported snapshot format");
4370 }
4371
4372 if (!defined $hash) {
4373 $hash = git_get_head_hash($project);
4374 }
4375
4376 my $git_command = git_cmd_str();
4377 my $name = $project;
4378 $name =~ s,([^/])/*\.git$,$1,;
4379 $name = basename($name);
4380 my $filename = to_utf8($name);
4381 $name =~ s/\047/\047\\\047\047/g;
4382 my $cmd;
4383 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4384 $cmd = "$git_command archive " .
4385 "--format=$known_snapshot_formats{$format}{'format'} " .
4386 "--prefix=\'$name\'/ $hash";
4387 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4388 $cmd .= ' | ' . join ' ', @{$known_snapshot_formats{$format}{'compressor'}};
4389 }
4390
4391 print $cgi->header(
4392 -type => $known_snapshot_formats{$format}{'type'},
4393 -content_disposition => 'inline; filename="' . "$filename" . '"',
4394 -status => '200 OK');
4395
4396 open my $fd, "-|", $cmd
4397 or die_error(undef, "Execute git-archive failed");
4398 binmode STDOUT, ':raw';
4399 print <$fd>;
4400 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4401 close $fd;
4402}
4403
4404sub git_log {
4405 my $head = git_get_head_hash($project);
4406 if (!defined $hash) {
4407 $hash = $head;
4408 }
4409 if (!defined $page) {
4410 $page = 0;
4411 }
4412 my $refs = git_get_references();
4413
4414 my @commitlist = parse_commits($hash, 101, (100 * $page));
4415
4416 my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
4417
4418 git_header_html();
4419 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4420
4421 if (!@commitlist) {
4422 my %co = parse_commit($hash);
4423
4424 git_print_header_div('summary', $project);
4425 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4426 }
4427 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4428 for (my $i = 0; $i <= $to; $i++) {
4429 my %co = %{$commitlist[$i]};
4430 next if !%co;
4431 my $commit = $co{'id'};
4432 my $ref = format_ref_marker($refs, $commit);
4433 my %ad = parse_date($co{'author_epoch'});
4434 git_print_header_div('commit',
4435 "<span class=\"age\">$co{'age_string'}</span>" .
4436 esc_html($co{'title'}) . $ref,
4437 $commit);
4438 print "<div class=\"title_text\">\n" .
4439 "<div class=\"log_link\">\n" .
4440 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4441 " | " .
4442 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4443 " | " .
4444 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4445 "<br/>\n" .
4446 "</div>\n" .
4447 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4448 "</div>\n";
4449
4450 print "<div class=\"log_body\">\n";
4451 git_print_log($co{'comment'}, -final_empty_line=> 1);
4452 print "</div>\n";
4453 }
4454 if ($#commitlist >= 100) {
4455 print "<div class=\"page_nav\">\n";
4456 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
4457 -accesskey => "n", -title => "Alt-n"}, "next");
4458 print "</div>\n";
4459 }
4460 git_footer_html();
4461}
4462
4463sub git_commit {
4464 $hash ||= $hash_base || "HEAD";
4465 my %co = parse_commit($hash);
4466 if (!%co) {
4467 die_error(undef, "Unknown commit object");
4468 }
4469 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4470 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4471
4472 my $parent = $co{'parent'};
4473 my $parents = $co{'parents'}; # listref
4474
4475 # we need to prepare $formats_nav before any parameter munging
4476 my $formats_nav;
4477 if (!defined $parent) {
4478 # --root commitdiff
4479 $formats_nav .= '(initial)';
4480 } elsif (@$parents == 1) {
4481 # single parent commit
4482 $formats_nav .=
4483 '(parent: ' .
4484 $cgi->a({-href => href(action=>"commit",
4485 hash=>$parent)},
4486 esc_html(substr($parent, 0, 7))) .
4487 ')';
4488 } else {
4489 # merge commit
4490 $formats_nav .=
4491 '(merge: ' .
4492 join(' ', map {
4493 $cgi->a({-href => href(action=>"commit",
4494 hash=>$_)},
4495 esc_html(substr($_, 0, 7)));
4496 } @$parents ) .
4497 ')';
4498 }
4499
4500 if (!defined $parent) {
4501 $parent = "--root";
4502 }
4503 my @difftree;
4504 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4505 @diff_opts,
4506 (@$parents <= 1 ? $parent : '-c'),
4507 $hash, "--"
4508 or die_error(undef, "Open git-diff-tree failed");
4509 @difftree = map { chomp; $_ } <$fd>;
4510 close $fd or die_error(undef, "Reading git-diff-tree failed");
4511
4512 # non-textual hash id's can be cached
4513 my $expires;
4514 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4515 $expires = "+1d";
4516 }
4517 my $refs = git_get_references();
4518 my $ref = format_ref_marker($refs, $co{'id'});
4519
4520 git_header_html(undef, $expires);
4521 git_print_page_nav('commit', '',
4522 $hash, $co{'tree'}, $hash,
4523 $formats_nav);
4524
4525 if (defined $co{'parent'}) {
4526 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4527 } else {
4528 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4529 }
4530 print "<div class=\"title_text\">\n" .
4531 "<table cellspacing=\"0\">\n";
4532 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4533 "<tr>" .
4534 "<td></td><td> $ad{'rfc2822'}";
4535 if ($ad{'hour_local'} < 6) {
4536 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4537 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4538 } else {
4539 printf(" (%02d:%02d %s)",
4540 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4541 }
4542 print "</td>" .
4543 "</tr>\n";
4544 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4545 print "<tr><td></td><td> $cd{'rfc2822'}" .
4546 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4547 "</td></tr>\n";
4548 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4549 print "<tr>" .
4550 "<td>tree</td>" .
4551 "<td class=\"sha1\">" .
4552 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4553 class => "list"}, $co{'tree'}) .
4554 "</td>" .
4555 "<td class=\"link\">" .
4556 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4557 "tree");
4558 my $snapshot_links = format_snapshot_links($hash);
4559 if (defined $snapshot_links) {
4560 print " | " . $snapshot_links;
4561 }
4562 print "</td>" .
4563 "</tr>\n";
4564
4565 foreach my $par (@$parents) {
4566 print "<tr>" .
4567 "<td>parent</td>" .
4568 "<td class=\"sha1\">" .
4569 $cgi->a({-href => href(action=>"commit", hash=>$par),
4570 class => "list"}, $par) .
4571 "</td>" .
4572 "<td class=\"link\">" .
4573 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4574 " | " .
4575 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4576 "</td>" .
4577 "</tr>\n";
4578 }
4579 print "</table>".
4580 "</div>\n";
4581
4582 print "<div class=\"page_body\">\n";
4583 git_print_log($co{'comment'});
4584 print "</div>\n";
4585
4586 git_difftree_body(\@difftree, $hash, @$parents);
4587
4588 git_footer_html();
4589}
4590
4591sub git_object {
4592 # object is defined by:
4593 # - hash or hash_base alone
4594 # - hash_base and file_name
4595 my $type;
4596
4597 # - hash or hash_base alone
4598 if ($hash || ($hash_base && !defined $file_name)) {
4599 my $object_id = $hash || $hash_base;
4600
4601 my $git_command = git_cmd_str();
4602 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4603 or die_error('404 Not Found', "Object does not exist");
4604 $type = <$fd>;
4605 chomp $type;
4606 close $fd
4607 or die_error('404 Not Found', "Object does not exist");
4608
4609 # - hash_base and file_name
4610 } elsif ($hash_base && defined $file_name) {
4611 $file_name =~ s,/+$,,;
4612
4613 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4614 or die_error('404 Not Found', "Base object does not exist");
4615
4616 # here errors should not hapen
4617 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4618 or die_error(undef, "Open git-ls-tree failed");
4619 my $line = <$fd>;
4620 close $fd;
4621
4622 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4623 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4624 die_error('404 Not Found', "File or directory for given base does not exist");
4625 }
4626 $type = $2;
4627 $hash = $3;
4628 } else {
4629 die_error('404 Not Found', "Not enough information to find object");
4630 }
4631
4632 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4633 hash=>$hash, hash_base=>$hash_base,
4634 file_name=>$file_name),
4635 -status => '302 Found');
4636}
4637
4638sub git_blobdiff {
4639 my $format = shift || 'html';
4640
4641 my $fd;
4642 my @difftree;
4643 my %diffinfo;
4644 my $expires;
4645
4646 # preparing $fd and %diffinfo for git_patchset_body
4647 # new style URI
4648 if (defined $hash_base && defined $hash_parent_base) {
4649 if (defined $file_name) {
4650 # read raw output
4651 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4652 $hash_parent_base, $hash_base,
4653 "--", (defined $file_parent ? $file_parent : ()), $file_name
4654 or die_error(undef, "Open git-diff-tree failed");
4655 @difftree = map { chomp; $_ } <$fd>;
4656 close $fd
4657 or die_error(undef, "Reading git-diff-tree failed");
4658 @difftree
4659 or die_error('404 Not Found', "Blob diff not found");
4660
4661 } elsif (defined $hash &&
4662 $hash =~ /[0-9a-fA-F]{40}/) {
4663 # try to find filename from $hash
4664
4665 # read filtered raw output
4666 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4667 $hash_parent_base, $hash_base, "--"
4668 or die_error(undef, "Open git-diff-tree failed");
4669 @difftree =
4670 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4671 # $hash == to_id
4672 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4673 map { chomp; $_ } <$fd>;
4674 close $fd
4675 or die_error(undef, "Reading git-diff-tree failed");
4676 @difftree
4677 or die_error('404 Not Found', "Blob diff not found");
4678
4679 } else {
4680 die_error('404 Not Found', "Missing one of the blob diff parameters");
4681 }
4682
4683 if (@difftree > 1) {
4684 die_error('404 Not Found', "Ambiguous blob diff specification");
4685 }
4686
4687 %diffinfo = parse_difftree_raw_line($difftree[0]);
4688 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
4689 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
4690
4691 $hash_parent ||= $diffinfo{'from_id'};
4692 $hash ||= $diffinfo{'to_id'};
4693
4694 # non-textual hash id's can be cached
4695 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4696 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4697 $expires = '+1d';
4698 }
4699
4700 # open patch output
4701 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4702 '-p', ($format eq 'html' ? "--full-index" : ()),
4703 $hash_parent_base, $hash_base,
4704 "--", (defined $file_parent ? $file_parent : ()), $file_name
4705 or die_error(undef, "Open git-diff-tree failed");
4706 }
4707
4708 # old/legacy style URI
4709 if (!%diffinfo && # if new style URI failed
4710 defined $hash && defined $hash_parent) {
4711 # fake git-diff-tree raw output
4712 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4713 $diffinfo{'from_id'} = $hash_parent;
4714 $diffinfo{'to_id'} = $hash;
4715 if (defined $file_name) {
4716 if (defined $file_parent) {
4717 $diffinfo{'status'} = '2';
4718 $diffinfo{'from_file'} = $file_parent;
4719 $diffinfo{'to_file'} = $file_name;
4720 } else { # assume not renamed
4721 $diffinfo{'status'} = '1';
4722 $diffinfo{'from_file'} = $file_name;
4723 $diffinfo{'to_file'} = $file_name;
4724 }
4725 } else { # no filename given
4726 $diffinfo{'status'} = '2';
4727 $diffinfo{'from_file'} = $hash_parent;
4728 $diffinfo{'to_file'} = $hash;
4729 }
4730
4731 # non-textual hash id's can be cached
4732 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4733 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4734 $expires = '+1d';
4735 }
4736
4737 # open patch output
4738 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4739 '-p', ($format eq 'html' ? "--full-index" : ()),
4740 $hash_parent, $hash, "--"
4741 or die_error(undef, "Open git-diff failed");
4742 } else {
4743 die_error('404 Not Found', "Missing one of the blob diff parameters")
4744 unless %diffinfo;
4745 }
4746
4747 # header
4748 if ($format eq 'html') {
4749 my $formats_nav =
4750 $cgi->a({-href => href(action=>"blobdiff_plain",
4751 hash=>$hash, hash_parent=>$hash_parent,
4752 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4753 file_name=>$file_name, file_parent=>$file_parent)},
4754 "raw");
4755 git_header_html(undef, $expires);
4756 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4757 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4758 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4759 } else {
4760 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4761 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4762 }
4763 if (defined $file_name) {
4764 git_print_page_path($file_name, "blob", $hash_base);
4765 } else {
4766 print "<div class=\"page_path\"></div>\n";
4767 }
4768
4769 } elsif ($format eq 'plain') {
4770 print $cgi->header(
4771 -type => 'text/plain',
4772 -charset => 'utf-8',
4773 -expires => $expires,
4774 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4775
4776 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4777
4778 } else {
4779 die_error(undef, "Unknown blobdiff format");
4780 }
4781
4782 # patch
4783 if ($format eq 'html') {
4784 print "<div class=\"page_body\">\n";
4785
4786 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4787 close $fd;
4788
4789 print "</div>\n"; # class="page_body"
4790 git_footer_html();
4791
4792 } else {
4793 while (my $line = <$fd>) {
4794 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4795 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4796
4797 print $line;
4798
4799 last if $line =~ m!^\+\+\+!;
4800 }
4801 local $/ = undef;
4802 print <$fd>;
4803 close $fd;
4804 }
4805}
4806
4807sub git_blobdiff_plain {
4808 git_blobdiff('plain');
4809}
4810
4811sub git_commitdiff {
4812 my $format = shift || 'html';
4813 $hash ||= $hash_base || "HEAD";
4814 my %co = parse_commit($hash);
4815 if (!%co) {
4816 die_error(undef, "Unknown commit object");
4817 }
4818
4819 # choose format for commitdiff for merge
4820 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
4821 $hash_parent = '--cc';
4822 }
4823 # we need to prepare $formats_nav before almost any parameter munging
4824 my $formats_nav;
4825 if ($format eq 'html') {
4826 $formats_nav =
4827 $cgi->a({-href => href(action=>"commitdiff_plain",
4828 hash=>$hash, hash_parent=>$hash_parent)},
4829 "raw");
4830
4831 if (defined $hash_parent &&
4832 $hash_parent ne '-c' && $hash_parent ne '--cc') {
4833 # commitdiff with two commits given
4834 my $hash_parent_short = $hash_parent;
4835 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4836 $hash_parent_short = substr($hash_parent, 0, 7);
4837 }
4838 $formats_nav .=
4839 ' (from';
4840 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
4841 if ($co{'parents'}[$i] eq $hash_parent) {
4842 $formats_nav .= ' parent ' . ($i+1);
4843 last;
4844 }
4845 }
4846 $formats_nav .= ': ' .
4847 $cgi->a({-href => href(action=>"commitdiff",
4848 hash=>$hash_parent)},
4849 esc_html($hash_parent_short)) .
4850 ')';
4851 } elsif (!$co{'parent'}) {
4852 # --root commitdiff
4853 $formats_nav .= ' (initial)';
4854 } elsif (scalar @{$co{'parents'}} == 1) {
4855 # single parent commit
4856 $formats_nav .=
4857 ' (parent: ' .
4858 $cgi->a({-href => href(action=>"commitdiff",
4859 hash=>$co{'parent'})},
4860 esc_html(substr($co{'parent'}, 0, 7))) .
4861 ')';
4862 } else {
4863 # merge commit
4864 if ($hash_parent eq '--cc') {
4865 $formats_nav .= ' | ' .
4866 $cgi->a({-href => href(action=>"commitdiff",
4867 hash=>$hash, hash_parent=>'-c')},
4868 'combined');
4869 } else { # $hash_parent eq '-c'
4870 $formats_nav .= ' | ' .
4871 $cgi->a({-href => href(action=>"commitdiff",
4872 hash=>$hash, hash_parent=>'--cc')},
4873 'compact');
4874 }
4875 $formats_nav .=
4876 ' (merge: ' .
4877 join(' ', map {
4878 $cgi->a({-href => href(action=>"commitdiff",
4879 hash=>$_)},
4880 esc_html(substr($_, 0, 7)));
4881 } @{$co{'parents'}} ) .
4882 ')';
4883 }
4884 }
4885
4886 my $hash_parent_param = $hash_parent;
4887 if (!defined $hash_parent_param) {
4888 # --cc for multiple parents, --root for parentless
4889 $hash_parent_param =
4890 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
4891 }
4892
4893 # read commitdiff
4894 my $fd;
4895 my @difftree;
4896 if ($format eq 'html') {
4897 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4898 "--no-commit-id", "--patch-with-raw", "--full-index",
4899 $hash_parent_param, $hash, "--"
4900 or die_error(undef, "Open git-diff-tree failed");
4901
4902 while (my $line = <$fd>) {
4903 chomp $line;
4904 # empty line ends raw part of diff-tree output
4905 last unless $line;
4906 push @difftree, scalar parse_difftree_raw_line($line);
4907 }
4908
4909 } elsif ($format eq 'plain') {
4910 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4911 '-p', $hash_parent_param, $hash, "--"
4912 or die_error(undef, "Open git-diff-tree failed");
4913
4914 } else {
4915 die_error(undef, "Unknown commitdiff format");
4916 }
4917
4918 # non-textual hash id's can be cached
4919 my $expires;
4920 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4921 $expires = "+1d";
4922 }
4923
4924 # write commit message
4925 if ($format eq 'html') {
4926 my $refs = git_get_references();
4927 my $ref = format_ref_marker($refs, $co{'id'});
4928
4929 git_header_html(undef, $expires);
4930 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4931 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4932 git_print_authorship(\%co);
4933 print "<div class=\"page_body\">\n";
4934 if (@{$co{'comment'}} > 1) {
4935 print "<div class=\"log\">\n";
4936 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4937 print "</div>\n"; # class="log"
4938 }
4939
4940 } elsif ($format eq 'plain') {
4941 my $refs = git_get_references("tags");
4942 my $tagname = git_get_rev_name_tags($hash);
4943 my $filename = basename($project) . "-$hash.patch";
4944
4945 print $cgi->header(
4946 -type => 'text/plain',
4947 -charset => 'utf-8',
4948 -expires => $expires,
4949 -content_disposition => 'inline; filename="' . "$filename" . '"');
4950 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4951 print <<TEXT;
4952From: $co{'author'}
4953Date: $ad{'rfc2822'} ($ad{'tz_local'})
4954Subject: $co{'title'}
4955TEXT
4956 print "X-Git-Tag: $tagname\n" if $tagname;
4957 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4958
4959 foreach my $line (@{$co{'comment'}}) {
4960 print "$line\n";
4961 }
4962 print "---\n\n";
4963 }
4964
4965 # write patch
4966 if ($format eq 'html') {
4967 my $use_parents = !defined $hash_parent ||
4968 $hash_parent eq '-c' || $hash_parent eq '--cc';
4969 git_difftree_body(\@difftree, $hash,
4970 $use_parents ? @{$co{'parents'}} : $hash_parent);
4971 print "<br/>\n";
4972
4973 git_patchset_body($fd, \@difftree, $hash,
4974 $use_parents ? @{$co{'parents'}} : $hash_parent);
4975 close $fd;
4976 print "</div>\n"; # class="page_body"
4977 git_footer_html();
4978
4979 } elsif ($format eq 'plain') {
4980 local $/ = undef;
4981 print <$fd>;
4982 close $fd
4983 or print "Reading git-diff-tree failed\n";
4984 }
4985}
4986
4987sub git_commitdiff_plain {
4988 git_commitdiff('plain');
4989}
4990
4991sub git_history {
4992 if (!defined $hash_base) {
4993 $hash_base = git_get_head_hash($project);
4994 }
4995 if (!defined $page) {
4996 $page = 0;
4997 }
4998 my $ftype;
4999 my %co = parse_commit($hash_base);
5000 if (!%co) {
5001 die_error(undef, "Unknown commit object");
5002 }
5003
5004 my $refs = git_get_references();
5005 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5006
5007 if (!defined $hash && defined $file_name) {
5008 $hash = git_get_hash_by_path($hash_base, $file_name);
5009 }
5010 if (defined $hash) {
5011 $ftype = git_get_type($hash);
5012 }
5013
5014 my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
5015
5016 my $paging_nav = '';
5017 if ($page > 0) {
5018 $paging_nav .=
5019 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5020 file_name=>$file_name)},
5021 "first");
5022 $paging_nav .= " ⋅ " .
5023 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5024 file_name=>$file_name, page=>$page-1),
5025 -accesskey => "p", -title => "Alt-p"}, "prev");
5026 } else {
5027 $paging_nav .= "first";
5028 $paging_nav .= " ⋅ prev";
5029 }
5030 if ($#commitlist >= 100) {
5031 $paging_nav .= " ⋅ " .
5032 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5033 file_name=>$file_name, page=>$page+1),
5034 -accesskey => "n", -title => "Alt-n"}, "next");
5035 } else {
5036 $paging_nav .= " ⋅ next";
5037 }
5038 my $next_link = '';
5039 if ($#commitlist >= 100) {
5040 $next_link =
5041 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5042 file_name=>$file_name, page=>$page+1),
5043 -accesskey => "n", -title => "Alt-n"}, "next");
5044 }
5045
5046 git_header_html();
5047 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5048 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5049 git_print_page_path($file_name, $ftype, $hash_base);
5050
5051 git_history_body(\@commitlist, 0, 99,
5052 $refs, $hash_base, $ftype, $next_link);
5053
5054 git_footer_html();
5055}
5056
5057sub git_search {
5058 my ($have_search) = gitweb_check_feature('search');
5059 if (!$have_search) {
5060 die_error('403 Permission denied', "Permission denied");
5061 }
5062 if (!defined $searchtext) {
5063 die_error(undef, "Text field empty");
5064 }
5065 if (!defined $hash) {
5066 $hash = git_get_head_hash($project);
5067 }
5068 my %co = parse_commit($hash);
5069 if (!%co) {
5070 die_error(undef, "Unknown commit object");
5071 }
5072 if (!defined $page) {
5073 $page = 0;
5074 }
5075
5076 $searchtype ||= 'commit';
5077 if ($searchtype eq 'pickaxe') {
5078 # pickaxe may take all resources of your box and run for several minutes
5079 # with every query - so decide by yourself how public you make this feature
5080 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5081 if (!$have_pickaxe) {
5082 die_error('403 Permission denied', "Permission denied");
5083 }
5084 }
5085 if ($searchtype eq 'grep') {
5086 my ($have_grep) = gitweb_check_feature('grep');
5087 if (!$have_grep) {
5088 die_error('403 Permission denied', "Permission denied");
5089 }
5090 }
5091
5092 git_header_html();
5093
5094 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5095 my $greptype;
5096 if ($searchtype eq 'commit') {
5097 $greptype = "--grep=";
5098 } elsif ($searchtype eq 'author') {
5099 $greptype = "--author=";
5100 } elsif ($searchtype eq 'committer') {
5101 $greptype = "--committer=";
5102 }
5103 $greptype .= $search_regexp;
5104 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
5105
5106 my $paging_nav = '';
5107 if ($page > 0) {
5108 $paging_nav .=
5109 $cgi->a({-href => href(action=>"search", hash=>$hash,
5110 searchtext=>$searchtext, searchtype=>$searchtype)},
5111 "first");
5112 $paging_nav .= " ⋅ " .
5113 $cgi->a({-href => href(action=>"search", hash=>$hash,
5114 searchtext=>$searchtext, searchtype=>$searchtype,
5115 page=>$page-1),
5116 -accesskey => "p", -title => "Alt-p"}, "prev");
5117 } else {
5118 $paging_nav .= "first";
5119 $paging_nav .= " ⋅ prev";
5120 }
5121 if ($#commitlist >= 100) {
5122 $paging_nav .= " ⋅ " .
5123 $cgi->a({-href => href(action=>"search", hash=>$hash,
5124 searchtext=>$searchtext, searchtype=>$searchtype,
5125 page=>$page+1),
5126 -accesskey => "n", -title => "Alt-n"}, "next");
5127 } else {
5128 $paging_nav .= " ⋅ next";
5129 }
5130 my $next_link = '';
5131 if ($#commitlist >= 100) {
5132 $next_link =
5133 $cgi->a({-href => href(action=>"search", hash=>$hash,
5134 searchtext=>$searchtext, searchtype=>$searchtype,
5135 page=>$page+1),
5136 -accesskey => "n", -title => "Alt-n"}, "next");
5137 }
5138
5139 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5140 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5141 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5142 }
5143
5144 if ($searchtype eq 'pickaxe') {
5145 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5146 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5147
5148 print "<table cellspacing=\"0\">\n";
5149 my $alternate = 1;
5150 $/ = "\n";
5151 my $git_command = git_cmd_str();
5152 my $searchqtext = $searchtext;
5153 $searchqtext =~ s/'/'\\''/;
5154 open my $fd, "-|", "$git_command rev-list $hash | " .
5155 "$git_command diff-tree -r --stdin -S\'$searchqtext\'";
5156 undef %co;
5157 my @files;
5158 while (my $line = <$fd>) {
5159 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
5160 my %set;
5161 $set{'file'} = $6;
5162 $set{'from_id'} = $3;
5163 $set{'to_id'} = $4;
5164 $set{'id'} = $set{'to_id'};
5165 if ($set{'id'} =~ m/0{40}/) {
5166 $set{'id'} = $set{'from_id'};
5167 }
5168 if ($set{'id'} =~ m/0{40}/) {
5169 next;
5170 }
5171 push @files, \%set;
5172 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
5173 if (%co) {
5174 if ($alternate) {
5175 print "<tr class=\"dark\">\n";
5176 } else {
5177 print "<tr class=\"light\">\n";
5178 }
5179 $alternate ^= 1;
5180 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5181 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5182 "<td><i>" . $author . "</i></td>\n" .
5183 "<td>" .
5184 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5185 -class => "list subject"},
5186 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5187 while (my $setref = shift @files) {
5188 my %set = %$setref;
5189 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5190 hash=>$set{'id'}, file_name=>$set{'file'}),
5191 -class => "list"},
5192 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5193 "<br/>\n";
5194 }
5195 print "</td>\n" .
5196 "<td class=\"link\">" .
5197 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5198 " | " .
5199 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5200 print "</td>\n" .
5201 "</tr>\n";
5202 }
5203 %co = parse_commit($1);
5204 }
5205 }
5206 close $fd;
5207
5208 print "</table>\n";
5209 }
5210
5211 if ($searchtype eq 'grep') {
5212 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5213 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5214
5215 print "<table cellspacing=\"0\">\n";
5216 my $alternate = 1;
5217 my $matches = 0;
5218 $/ = "\n";
5219 open my $fd, "-|", git_cmd(), 'grep', '-n', '-i', '-E', $searchtext, $co{'tree'};
5220 my $lastfile = '';
5221 while (my $line = <$fd>) {
5222 chomp $line;
5223 my ($file, $lno, $ltext, $binary);
5224 last if ($matches++ > 1000);
5225 if ($line =~ /^Binary file (.+) matches$/) {
5226 $file = $1;
5227 $binary = 1;
5228 } else {
5229 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5230 }
5231 if ($file ne $lastfile) {
5232 $lastfile and print "</td></tr>\n";
5233 if ($alternate++) {
5234 print "<tr class=\"dark\">\n";
5235 } else {
5236 print "<tr class=\"light\">\n";
5237 }
5238 print "<td class=\"list\">".
5239 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5240 file_name=>"$file"),
5241 -class => "list"}, esc_path($file));
5242 print "</td><td>\n";
5243 $lastfile = $file;
5244 }
5245 if ($binary) {
5246 print "<div class=\"binary\">Binary file</div>\n";
5247 } else {
5248 $ltext = untabify($ltext);
5249 if ($ltext =~ m/^(.*)($searchtext)(.*)$/i) {
5250 $ltext = esc_html($1, -nbsp=>1);
5251 $ltext .= '<span class="match">';
5252 $ltext .= esc_html($2, -nbsp=>1);
5253 $ltext .= '</span>';
5254 $ltext .= esc_html($3, -nbsp=>1);
5255 } else {
5256 $ltext = esc_html($ltext, -nbsp=>1);
5257 }
5258 print "<div class=\"pre\">" .
5259 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5260 file_name=>"$file").'#l'.$lno,
5261 -class => "linenr"}, sprintf('%4i', $lno))
5262 . ' ' . $ltext . "</div>\n";
5263 }
5264 }
5265 if ($lastfile) {
5266 print "</td></tr>\n";
5267 if ($matches > 1000) {
5268 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5269 }
5270 } else {
5271 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5272 }
5273 close $fd;
5274
5275 print "</table>\n";
5276 }
5277 git_footer_html();
5278}
5279
5280sub git_search_help {
5281 git_header_html();
5282 git_print_page_nav('','', $hash,$hash,$hash);
5283 print <<EOT;
5284<dl>
5285<dt><b>commit</b></dt>
5286<dd>The commit messages and authorship information will be scanned for the given string.</dd>
5287EOT
5288 my ($have_grep) = gitweb_check_feature('grep');
5289 if ($have_grep) {
5290 print <<EOT;
5291<dt><b>grep</b></dt>
5292<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5293 a different one) are searched for the given
5294<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>
5295(POSIX extended) and the matches are listed. On large
5296trees, this search can take a while and put some strain on the server, so please use it with
5297some consideration.</dd>
5298EOT
5299 }
5300 print <<EOT;
5301<dt><b>author</b></dt>
5302<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
5303<dt><b>committer</b></dt>
5304<dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
5305EOT
5306 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5307 if ($have_pickaxe) {
5308 print <<EOT;
5309<dt><b>pickaxe</b></dt>
5310<dd>All commits that caused the string to appear or disappear from any file (changes that
5311added, removed or "modified" the string) will be listed. This search can take a while and
5312takes a lot of strain on the server, so please use it wisely.</dd>
5313EOT
5314 }
5315 print "</dl>\n";
5316 git_footer_html();
5317}
5318
5319sub git_shortlog {
5320 my $head = git_get_head_hash($project);
5321 if (!defined $hash) {
5322 $hash = $head;
5323 }
5324 if (!defined $page) {
5325 $page = 0;
5326 }
5327 my $refs = git_get_references();
5328
5329 my @commitlist = parse_commits($hash, 101, (100 * $page));
5330
5331 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
5332 my $next_link = '';
5333 if ($#commitlist >= 100) {
5334 $next_link =
5335 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
5336 -accesskey => "n", -title => "Alt-n"}, "next");
5337 }
5338
5339 git_header_html();
5340 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5341 git_print_header_div('summary', $project);
5342
5343 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5344
5345 git_footer_html();
5346}
5347
5348## ......................................................................
5349## feeds (RSS, Atom; OPML)
5350
5351sub git_feed {
5352 my $format = shift || 'atom';
5353 my ($have_blame) = gitweb_check_feature('blame');
5354
5355 # Atom: http://www.atomenabled.org/developers/syndication/
5356 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5357 if ($format ne 'rss' && $format ne 'atom') {
5358 die_error(undef, "Unknown web feed format");
5359 }
5360
5361 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5362 my $head = $hash || 'HEAD';
5363 my @commitlist = parse_commits($head, 150, 0, undef, $file_name);
5364
5365 my %latest_commit;
5366 my %latest_date;
5367 my $content_type = "application/$format+xml";
5368 if (defined $cgi->http('HTTP_ACCEPT') &&
5369 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5370 # browser (feed reader) prefers text/xml
5371 $content_type = 'text/xml';
5372 }
5373 if (defined($commitlist[0])) {
5374 %latest_commit = %{$commitlist[0]};
5375 %latest_date = parse_date($latest_commit{'author_epoch'});
5376 print $cgi->header(
5377 -type => $content_type,
5378 -charset => 'utf-8',
5379 -last_modified => $latest_date{'rfc2822'});
5380 } else {
5381 print $cgi->header(
5382 -type => $content_type,
5383 -charset => 'utf-8');
5384 }
5385
5386 # Optimization: skip generating the body if client asks only
5387 # for Last-Modified date.
5388 return if ($cgi->request_method() eq 'HEAD');
5389
5390 # header variables
5391 my $title = "$site_name - $project/$action";
5392 my $feed_type = 'log';
5393 if (defined $hash) {
5394 $title .= " - '$hash'";
5395 $feed_type = 'branch log';
5396 if (defined $file_name) {
5397 $title .= " :: $file_name";
5398 $feed_type = 'history';
5399 }
5400 } elsif (defined $file_name) {
5401 $title .= " - $file_name";
5402 $feed_type = 'history';
5403 }
5404 $title .= " $feed_type";
5405 my $descr = git_get_project_description($project);
5406 if (defined $descr) {
5407 $descr = esc_html($descr);
5408 } else {
5409 $descr = "$project " .
5410 ($format eq 'rss' ? 'RSS' : 'Atom') .
5411 " feed";
5412 }
5413 my $owner = git_get_project_owner($project);
5414 $owner = esc_html($owner);
5415
5416 #header
5417 my $alt_url;
5418 if (defined $file_name) {
5419 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5420 } elsif (defined $hash) {
5421 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5422 } else {
5423 $alt_url = href(-full=>1, action=>"summary");
5424 }
5425 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5426 if ($format eq 'rss') {
5427 print <<XML;
5428<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5429<channel>
5430XML
5431 print "<title>$title</title>\n" .
5432 "<link>$alt_url</link>\n" .
5433 "<description>$descr</description>\n" .
5434 "<language>en</language>\n";
5435 } elsif ($format eq 'atom') {
5436 print <<XML;
5437<feed xmlns="http://www.w3.org/2005/Atom">
5438XML
5439 print "<title>$title</title>\n" .
5440 "<subtitle>$descr</subtitle>\n" .
5441 '<link rel="alternate" type="text/html" href="' .
5442 $alt_url . '" />' . "\n" .
5443 '<link rel="self" type="' . $content_type . '" href="' .
5444 $cgi->self_url() . '" />' . "\n" .
5445 "<id>" . href(-full=>1) . "</id>\n" .
5446 # use project owner for feed author
5447 "<author><name>$owner</name></author>\n";
5448 if (defined $favicon) {
5449 print "<icon>" . esc_url($favicon) . "</icon>\n";
5450 }
5451 if (defined $logo_url) {
5452 # not twice as wide as tall: 72 x 27 pixels
5453 print "<logo>" . esc_url($logo) . "</logo>\n";
5454 }
5455 if (! %latest_date) {
5456 # dummy date to keep the feed valid until commits trickle in:
5457 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5458 } else {
5459 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5460 }
5461 }
5462
5463 # contents
5464 for (my $i = 0; $i <= $#commitlist; $i++) {
5465 my %co = %{$commitlist[$i]};
5466 my $commit = $co{'id'};
5467 # we read 150, we always show 30 and the ones more recent than 48 hours
5468 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5469 last;
5470 }
5471 my %cd = parse_date($co{'author_epoch'});
5472
5473 # get list of changed files
5474 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5475 $co{'parent'} || "--root",
5476 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5477 or next;
5478 my @difftree = map { chomp; $_ } <$fd>;
5479 close $fd
5480 or next;
5481
5482 # print element (entry, item)
5483 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
5484 if ($format eq 'rss') {
5485 print "<item>\n" .
5486 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5487 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5488 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5489 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5490 "<link>$co_url</link>\n" .
5491 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5492 "<content:encoded>" .
5493 "<![CDATA[\n";
5494 } elsif ($format eq 'atom') {
5495 print "<entry>\n" .
5496 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5497 "<updated>$cd{'iso-8601'}</updated>\n" .
5498 "<author>\n" .
5499 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5500 if ($co{'author_email'}) {
5501 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5502 }
5503 print "</author>\n" .
5504 # use committer for contributor
5505 "<contributor>\n" .
5506 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5507 if ($co{'committer_email'}) {
5508 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5509 }
5510 print "</contributor>\n" .
5511 "<published>$cd{'iso-8601'}</published>\n" .
5512 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5513 "<id>$co_url</id>\n" .
5514 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5515 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5516 }
5517 my $comment = $co{'comment'};
5518 print "<pre>\n";
5519 foreach my $line (@$comment) {
5520 $line = esc_html($line);
5521 print "$line\n";
5522 }
5523 print "</pre><ul>\n";
5524 foreach my $difftree_line (@difftree) {
5525 my %difftree = parse_difftree_raw_line($difftree_line);
5526 next if !$difftree{'from_id'};
5527
5528 my $file = $difftree{'file'} || $difftree{'to_file'};
5529
5530 print "<li>" .
5531 "[" .
5532 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5533 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5534 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5535 file_name=>$file, file_parent=>$difftree{'from_file'}),
5536 -title => "diff"}, 'D');
5537 if ($have_blame) {
5538 print $cgi->a({-href => href(-full=>1, action=>"blame",
5539 file_name=>$file, hash_base=>$commit),
5540 -title => "blame"}, 'B');
5541 }
5542 # if this is not a feed of a file history
5543 if (!defined $file_name || $file_name ne $file) {
5544 print $cgi->a({-href => href(-full=>1, action=>"history",
5545 file_name=>$file, hash=>$commit),
5546 -title => "history"}, 'H');
5547 }
5548 $file = esc_path($file);
5549 print "] ".
5550 "$file</li>\n";
5551 }
5552 if ($format eq 'rss') {
5553 print "</ul>]]>\n" .
5554 "</content:encoded>\n" .
5555 "</item>\n";
5556 } elsif ($format eq 'atom') {
5557 print "</ul>\n</div>\n" .
5558 "</content>\n" .
5559 "</entry>\n";
5560 }
5561 }
5562
5563 # end of feed
5564 if ($format eq 'rss') {
5565 print "</channel>\n</rss>\n";
5566 } elsif ($format eq 'atom') {
5567 print "</feed>\n";
5568 }
5569}
5570
5571sub git_rss {
5572 git_feed('rss');
5573}
5574
5575sub git_atom {
5576 git_feed('atom');
5577}
5578
5579sub git_opml {
5580 my @list = git_get_projects_list();
5581
5582 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5583 print <<XML;
5584<?xml version="1.0" encoding="utf-8"?>
5585<opml version="1.0">
5586<head>
5587 <title>$site_name OPML Export</title>
5588</head>
5589<body>
5590<outline text="git RSS feeds">
5591XML
5592
5593 foreach my $pr (@list) {
5594 my %proj = %$pr;
5595 my $head = git_get_head_hash($proj{'path'});
5596 if (!defined $head) {
5597 next;
5598 }
5599 $git_dir = "$projectroot/$proj{'path'}";
5600 my %co = parse_commit($head);
5601 if (!%co) {
5602 next;
5603 }
5604
5605 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5606 my $rss = "$my_url?p=$proj{'path'};a=rss";
5607 my $html = "$my_url?p=$proj{'path'};a=summary";
5608 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5609 }
5610 print <<XML;
5611</outline>
5612</body>
5613</opml>
5614XML
5615}