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