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