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