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