]> Lady’s Gitweb - Gitweb/blob - gitweb.perl
gitweb: Better support for non-CSS aware web browsers
[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 our $cgi = new CGI;
22 our $version = "++GIT_VERSION++";
23 our $my_url = $cgi->url();
24 our $my_uri = $cgi->url(-absolute => 1);
25
26 # core git executable to use
27 # this can just be "git" if your webserver has a sensible PATH
28 our $GIT = "++GIT_BINDIR++/git";
29
30 # absolute fs-path which will be prepended to the project path
31 #our $projectroot = "/pub/scm";
32 our $projectroot = "++GITWEB_PROJECTROOT++";
33
34 # target of the home link on top of all pages
35 our $home_link = $my_uri || "/";
36
37 # string of the home link on top of all pages
38 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
39
40 # name of your site or organization to appear in page titles
41 # replace this with something more descriptive for clearer bookmarks
42 our $site_name = "++GITWEB_SITENAME++"
43 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
44
45 # filename of html text to include at top of each page
46 our $site_header = "++GITWEB_SITE_HEADER++";
47 # html text to include at home page
48 our $home_text = "++GITWEB_HOMETEXT++";
49 # filename of html text to include at bottom of each page
50 our $site_footer = "++GITWEB_SITE_FOOTER++";
51
52 # URI of stylesheets
53 our @stylesheets = ("++GITWEB_CSS++");
54 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
55 our $stylesheet = undef;
56
57 # URI of GIT logo (72x27 size)
58 our $logo = "++GITWEB_LOGO++";
59 # URI of GIT favicon, assumed to be image/png type
60 our $favicon = "++GITWEB_FAVICON++";
61
62 # URI and label (title) of GIT logo link
63 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
64 #our $logo_label = "git documentation";
65 our $logo_url = "http://git.or.cz/";
66 our $logo_label = "git homepage";
67
68 # source of projects list
69 our $projects_list = "++GITWEB_LIST++";
70
71 # show repository only if this file exists
72 # (only effective if this variable evaluates to true)
73 our $export_ok = "++GITWEB_EXPORT_OK++";
74
75 # only allow viewing of repositories also shown on the overview page
76 our $strict_export = "++GITWEB_STRICT_EXPORT++";
77
78 # list of git base URLs used for URL to where fetch project from,
79 # i.e. full URL is "$git_base_url/$project"
80 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
81
82 # default blob_plain mimetype and default charset for text/plain blob
83 our $default_blob_plain_mimetype = 'text/plain';
84 our $default_text_plain_charset = undef;
85
86 # file to use for guessing MIME types before trying /etc/mime.types
87 # (relative to the current git repository)
88 our $mimetypes_file = undef;
89
90 # You define site-wide feature defaults here; override them with
91 # $GITWEB_CONFIG as necessary.
92 our %feature = (
93 # feature => {
94 # 'sub' => feature-sub (subroutine),
95 # 'override' => allow-override (boolean),
96 # 'default' => [ default options...] (array reference)}
97 #
98 # if feature is overridable (it means that allow-override has true value,
99 # then feature-sub will be called with default options as parameters;
100 # return value of feature-sub indicates if to enable specified feature
101 #
102 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
103
104 # Enable the 'blame' blob view, showing the last commit that modified
105 # each line in the file. This can be very CPU-intensive.
106
107 # To enable system wide have in $GITWEB_CONFIG
108 # $feature{'blame'}{'default'} = [1];
109 # To have project specific config enable override in $GITWEB_CONFIG
110 # $feature{'blame'}{'override'} = 1;
111 # and in project config gitweb.blame = 0|1;
112 'blame' => {
113 'sub' => \&feature_blame,
114 'override' => 0,
115 'default' => [0]},
116
117 # Enable the 'snapshot' link, providing a compressed tarball of any
118 # tree. This can potentially generate high traffic if you have large
119 # project.
120
121 # To disable system wide have in $GITWEB_CONFIG
122 # $feature{'snapshot'}{'default'} = [undef];
123 # To have project specific config enable override in $GITWEB_CONFIG
124 # $feature{'blame'}{'override'} = 1;
125 # and in project config gitweb.snapshot = none|gzip|bzip2;
126 'snapshot' => {
127 'sub' => \&feature_snapshot,
128 'override' => 0,
129 # => [content-encoding, suffix, program]
130 'default' => ['x-gzip', 'gz', 'gzip']},
131
132 # Enable the pickaxe search, which will list the commits that modified
133 # a given string in a file. This can be practical and quite faster
134 # alternative to 'blame', but still potentially CPU-intensive.
135
136 # To enable system wide have in $GITWEB_CONFIG
137 # $feature{'pickaxe'}{'default'} = [1];
138 # To have project specific config enable override in $GITWEB_CONFIG
139 # $feature{'pickaxe'}{'override'} = 1;
140 # and in project config gitweb.pickaxe = 0|1;
141 'pickaxe' => {
142 'sub' => \&feature_pickaxe,
143 'override' => 0,
144 'default' => [1]},
145
146 # Make gitweb use an alternative format of the URLs which can be
147 # more readable and natural-looking: project name is embedded
148 # directly in the path and the query string contains other
149 # auxiliary information. All gitweb installations recognize
150 # URL in either format; this configures in which formats gitweb
151 # generates links.
152
153 # To enable system wide have in $GITWEB_CONFIG
154 # $feature{'pathinfo'}{'default'} = [1];
155 # Project specific override is not supported.
156
157 # Note that you will need to change the default location of CSS,
158 # favicon, logo and possibly other files to an absolute URL. Also,
159 # if gitweb.cgi serves as your indexfile, you will need to force
160 # $my_uri to contain the script name in your $GITWEB_CONFIG.
161 'pathinfo' => {
162 'override' => 0,
163 'default' => [0]},
164
165 # Make gitweb consider projects in project root subdirectories
166 # to be forks of existing projects. Given project $projname.git,
167 # projects matching $projname/*.git will not be shown in the main
168 # projects list, instead a '+' mark will be added to $projname
169 # there and a 'forks' view will be enabled for the project, listing
170 # all the forks. This feature is supported only if project list
171 # is taken from a directory, not file.
172
173 # To enable system wide have in $GITWEB_CONFIG
174 # $feature{'forks'}{'default'} = [1];
175 # Project specific override is not supported.
176 'forks' => {
177 'override' => 0,
178 'default' => [0]},
179 );
180
181 sub gitweb_check_feature {
182 my ($name) = @_;
183 return unless exists $feature{$name};
184 my ($sub, $override, @defaults) = (
185 $feature{$name}{'sub'},
186 $feature{$name}{'override'},
187 @{$feature{$name}{'default'}});
188 if (!$override) { return @defaults; }
189 if (!defined $sub) {
190 warn "feature $name is not overrideable";
191 return @defaults;
192 }
193 return $sub->(@defaults);
194 }
195
196 sub feature_blame {
197 my ($val) = git_get_project_config('blame', '--bool');
198
199 if ($val eq 'true') {
200 return 1;
201 } elsif ($val eq 'false') {
202 return 0;
203 }
204
205 return $_[0];
206 }
207
208 sub feature_snapshot {
209 my ($ctype, $suffix, $command) = @_;
210
211 my ($val) = git_get_project_config('snapshot');
212
213 if ($val eq 'gzip') {
214 return ('x-gzip', 'gz', 'gzip');
215 } elsif ($val eq 'bzip2') {
216 return ('x-bzip2', 'bz2', 'bzip2');
217 } elsif ($val eq 'none') {
218 return ();
219 }
220
221 return ($ctype, $suffix, $command);
222 }
223
224 sub gitweb_have_snapshot {
225 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
226 my $have_snapshot = (defined $ctype && defined $suffix);
227
228 return $have_snapshot;
229 }
230
231 sub feature_pickaxe {
232 my ($val) = git_get_project_config('pickaxe', '--bool');
233
234 if ($val eq 'true') {
235 return (1);
236 } elsif ($val eq 'false') {
237 return (0);
238 }
239
240 return ($_[0]);
241 }
242
243 # checking HEAD file with -e is fragile if the repository was
244 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
245 # and then pruned.
246 sub check_head_link {
247 my ($dir) = @_;
248 my $headfile = "$dir/HEAD";
249 return ((-e $headfile) ||
250 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
251 }
252
253 sub check_export_ok {
254 my ($dir) = @_;
255 return (check_head_link($dir) &&
256 (!$export_ok || -e "$dir/$export_ok"));
257 }
258
259 # rename detection options for git-diff and git-diff-tree
260 # - default is '-M', with the cost proportional to
261 # (number of removed files) * (number of new files).
262 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
263 # (number of changed files + number of removed files) * (number of new files)
264 # - even more costly is '-C', '--find-copies-harder' with cost
265 # (number of files in the original tree) * (number of new files)
266 # - one might want to include '-B' option, e.g. '-B', '-M'
267 our @diff_opts = ('-M'); # taken from git_commit
268
269 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
270 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
271
272 # version of the core git binary
273 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
274
275 $projects_list ||= $projectroot;
276
277 # ======================================================================
278 # input validation and dispatch
279 our $action = $cgi->param('a');
280 if (defined $action) {
281 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
282 die_error(undef, "Invalid action parameter");
283 }
284 }
285
286 # parameters which are pathnames
287 our $project = $cgi->param('p');
288 if (defined $project) {
289 if (!validate_pathname($project) ||
290 !(-d "$projectroot/$project") ||
291 !check_head_link("$projectroot/$project") ||
292 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
293 ($strict_export && !project_in_list($project))) {
294 undef $project;
295 die_error(undef, "No such project");
296 }
297 }
298
299 our $file_name = $cgi->param('f');
300 if (defined $file_name) {
301 if (!validate_pathname($file_name)) {
302 die_error(undef, "Invalid file parameter");
303 }
304 }
305
306 our $file_parent = $cgi->param('fp');
307 if (defined $file_parent) {
308 if (!validate_pathname($file_parent)) {
309 die_error(undef, "Invalid file parent parameter");
310 }
311 }
312
313 # parameters which are refnames
314 our $hash = $cgi->param('h');
315 if (defined $hash) {
316 if (!validate_refname($hash)) {
317 die_error(undef, "Invalid hash parameter");
318 }
319 }
320
321 our $hash_parent = $cgi->param('hp');
322 if (defined $hash_parent) {
323 if (!validate_refname($hash_parent)) {
324 die_error(undef, "Invalid hash parent parameter");
325 }
326 }
327
328 our $hash_base = $cgi->param('hb');
329 if (defined $hash_base) {
330 if (!validate_refname($hash_base)) {
331 die_error(undef, "Invalid hash base parameter");
332 }
333 }
334
335 our $hash_parent_base = $cgi->param('hpb');
336 if (defined $hash_parent_base) {
337 if (!validate_refname($hash_parent_base)) {
338 die_error(undef, "Invalid hash parent base parameter");
339 }
340 }
341
342 # other parameters
343 our $page = $cgi->param('pg');
344 if (defined $page) {
345 if ($page =~ m/[^0-9]/) {
346 die_error(undef, "Invalid page parameter");
347 }
348 }
349
350 our $searchtext = $cgi->param('s');
351 if (defined $searchtext) {
352 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
353 die_error(undef, "Invalid search parameter");
354 }
355 $searchtext = quotemeta $searchtext;
356 }
357
358 our $searchtype = $cgi->param('st');
359 if (defined $searchtype) {
360 if ($searchtype =~ m/[^a-z]/) {
361 die_error(undef, "Invalid searchtype parameter");
362 }
363 }
364
365 # now read PATH_INFO and use it as alternative to parameters
366 sub evaluate_path_info {
367 return if defined $project;
368 my $path_info = $ENV{"PATH_INFO"};
369 return if !$path_info;
370 $path_info =~ s,^/+,,;
371 return if !$path_info;
372 # find which part of PATH_INFO is project
373 $project = $path_info;
374 $project =~ s,/+$,,;
375 while ($project && !check_head_link("$projectroot/$project")) {
376 $project =~ s,/*[^/]*$,,;
377 }
378 # validate project
379 $project = validate_pathname($project);
380 if (!$project ||
381 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
382 ($strict_export && !project_in_list($project))) {
383 undef $project;
384 return;
385 }
386 # do not change any parameters if an action is given using the query string
387 return if $action;
388 $path_info =~ s,^$project/*,,;
389 my ($refname, $pathname) = split(/:/, $path_info, 2);
390 if (defined $pathname) {
391 # we got "project.git/branch:filename" or "project.git/branch:dir/"
392 # we could use git_get_type(branch:pathname), but it needs $git_dir
393 $pathname =~ s,^/+,,;
394 if (!$pathname || substr($pathname, -1) eq "/") {
395 $action ||= "tree";
396 $pathname =~ s,/$,,;
397 } else {
398 $action ||= "blob_plain";
399 }
400 $hash_base ||= validate_refname($refname);
401 $file_name ||= validate_pathname($pathname);
402 } elsif (defined $refname) {
403 # we got "project.git/branch"
404 $action ||= "shortlog";
405 $hash ||= validate_refname($refname);
406 }
407 }
408 evaluate_path_info();
409
410 # path to the current git repository
411 our $git_dir;
412 $git_dir = "$projectroot/$project" if $project;
413
414 # dispatch
415 my %actions = (
416 "blame" => \&git_blame2,
417 "blobdiff" => \&git_blobdiff,
418 "blobdiff_plain" => \&git_blobdiff_plain,
419 "blob" => \&git_blob,
420 "blob_plain" => \&git_blob_plain,
421 "commitdiff" => \&git_commitdiff,
422 "commitdiff_plain" => \&git_commitdiff_plain,
423 "commit" => \&git_commit,
424 "forks" => \&git_forks,
425 "heads" => \&git_heads,
426 "history" => \&git_history,
427 "log" => \&git_log,
428 "rss" => \&git_rss,
429 "search" => \&git_search,
430 "search_help" => \&git_search_help,
431 "shortlog" => \&git_shortlog,
432 "summary" => \&git_summary,
433 "tag" => \&git_tag,
434 "tags" => \&git_tags,
435 "tree" => \&git_tree,
436 "snapshot" => \&git_snapshot,
437 # those below don't need $project
438 "opml" => \&git_opml,
439 "project_list" => \&git_project_list,
440 "project_index" => \&git_project_index,
441 );
442
443 if (defined $project) {
444 $action ||= 'summary';
445 } else {
446 $action ||= 'project_list';
447 }
448 if (!defined($actions{$action})) {
449 die_error(undef, "Unknown action");
450 }
451 if ($action !~ m/^(opml|project_list|project_index)$/ &&
452 !$project) {
453 die_error(undef, "Project needed");
454 }
455 $actions{$action}->();
456 exit;
457
458 ## ======================================================================
459 ## action links
460
461 sub href(%) {
462 my %params = @_;
463 my $href = $my_uri;
464
465 # XXX: Warning: If you touch this, check the search form for updating,
466 # too.
467
468 my @mapping = (
469 project => "p",
470 action => "a",
471 file_name => "f",
472 file_parent => "fp",
473 hash => "h",
474 hash_parent => "hp",
475 hash_base => "hb",
476 hash_parent_base => "hpb",
477 page => "pg",
478 order => "o",
479 searchtext => "s",
480 searchtype => "st",
481 );
482 my %mapping = @mapping;
483
484 $params{'project'} = $project unless exists $params{'project'};
485
486 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
487 if ($use_pathinfo) {
488 # use PATH_INFO for project name
489 $href .= "/$params{'project'}" if defined $params{'project'};
490 delete $params{'project'};
491
492 # Summary just uses the project path URL
493 if (defined $params{'action'} && $params{'action'} eq 'summary') {
494 delete $params{'action'};
495 }
496 }
497
498 # now encode the parameters explicitly
499 my @result = ();
500 for (my $i = 0; $i < @mapping; $i += 2) {
501 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
502 if (defined $params{$name}) {
503 push @result, $symbol . "=" . esc_param($params{$name});
504 }
505 }
506 $href .= "?" . join(';', @result) if scalar @result;
507
508 return $href;
509 }
510
511
512 ## ======================================================================
513 ## validation, quoting/unquoting and escaping
514
515 sub validate_pathname {
516 my $input = shift || return undef;
517
518 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
519 # at the beginning, at the end, and between slashes.
520 # also this catches doubled slashes
521 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
522 return undef;
523 }
524 # no null characters
525 if ($input =~ m!\0!) {
526 return undef;
527 }
528 return $input;
529 }
530
531 sub validate_refname {
532 my $input = shift || return undef;
533
534 # textual hashes are O.K.
535 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
536 return $input;
537 }
538 # it must be correct pathname
539 $input = validate_pathname($input)
540 or return undef;
541 # restrictions on ref name according to git-check-ref-format
542 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
543 return undef;
544 }
545 return $input;
546 }
547
548 # very thin wrapper for decode("utf8", $str, Encode::FB_DEFAULT);
549 sub to_utf8 {
550 my $str = shift;
551 return decode("utf8", $str, Encode::FB_DEFAULT);
552 }
553
554 # quote unsafe chars, but keep the slash, even when it's not
555 # correct, but quoted slashes look too horrible in bookmarks
556 sub esc_param {
557 my $str = shift;
558 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
559 $str =~ s/\+/%2B/g;
560 $str =~ s/ /\+/g;
561 return $str;
562 }
563
564 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
565 sub esc_url {
566 my $str = shift;
567 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
568 $str =~ s/\+/%2B/g;
569 $str =~ s/ /\+/g;
570 return $str;
571 }
572
573 # replace invalid utf8 character with SUBSTITUTION sequence
574 sub esc_html ($;%) {
575 my $str = shift;
576 my %opts = @_;
577
578 $str = to_utf8($str);
579 $str = escapeHTML($str);
580 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
581 $str =~ s/\033/^[/g; # "escape" ESCAPE (\e) character (e.g. commit 20a3847d8a5032ce41f90dcc68abfb36e6fee9b1)
582 if ($opts{'-nbsp'}) {
583 $str =~ s/ /&nbsp;/g;
584 }
585 return $str;
586 }
587
588 # git may return quoted and escaped filenames
589 sub unquote {
590 my $str = shift;
591 if ($str =~ m/^"(.*)"$/) {
592 $str = $1;
593 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
594 }
595 return $str;
596 }
597
598 # escape tabs (convert tabs to spaces)
599 sub untabify {
600 my $line = shift;
601
602 while ((my $pos = index($line, "\t")) != -1) {
603 if (my $count = (8 - ($pos % 8))) {
604 my $spaces = ' ' x $count;
605 $line =~ s/\t/$spaces/;
606 }
607 }
608
609 return $line;
610 }
611
612 sub project_in_list {
613 my $project = shift;
614 my @list = git_get_projects_list();
615 return @list && scalar(grep { $_->{'path'} eq $project } @list);
616 }
617
618 ## ----------------------------------------------------------------------
619 ## HTML aware string manipulation
620
621 sub chop_str {
622 my $str = shift;
623 my $len = shift;
624 my $add_len = shift || 10;
625
626 # allow only $len chars, but don't cut a word if it would fit in $add_len
627 # if it doesn't fit, cut it if it's still longer than the dots we would add
628 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
629 my $body = $1;
630 my $tail = $2;
631 if (length($tail) > 4) {
632 $tail = " ...";
633 $body =~ s/&[^;]*$//; # remove chopped character entities
634 }
635 return "$body$tail";
636 }
637
638 ## ----------------------------------------------------------------------
639 ## functions returning short strings
640
641 # CSS class for given age value (in seconds)
642 sub age_class {
643 my $age = shift;
644
645 if ($age < 60*60*2) {
646 return "age0";
647 } elsif ($age < 60*60*24*2) {
648 return "age1";
649 } else {
650 return "age2";
651 }
652 }
653
654 # convert age in seconds to "nn units ago" string
655 sub age_string {
656 my $age = shift;
657 my $age_str;
658
659 if ($age > 60*60*24*365*2) {
660 $age_str = (int $age/60/60/24/365);
661 $age_str .= " years ago";
662 } elsif ($age > 60*60*24*(365/12)*2) {
663 $age_str = int $age/60/60/24/(365/12);
664 $age_str .= " months ago";
665 } elsif ($age > 60*60*24*7*2) {
666 $age_str = int $age/60/60/24/7;
667 $age_str .= " weeks ago";
668 } elsif ($age > 60*60*24*2) {
669 $age_str = int $age/60/60/24;
670 $age_str .= " days ago";
671 } elsif ($age > 60*60*2) {
672 $age_str = int $age/60/60;
673 $age_str .= " hours ago";
674 } elsif ($age > 60*2) {
675 $age_str = int $age/60;
676 $age_str .= " min ago";
677 } elsif ($age > 2) {
678 $age_str = int $age;
679 $age_str .= " sec ago";
680 } else {
681 $age_str .= " right now";
682 }
683 return $age_str;
684 }
685
686 # convert file mode in octal to symbolic file mode string
687 sub mode_str {
688 my $mode = oct shift;
689
690 if (S_ISDIR($mode & S_IFMT)) {
691 return 'drwxr-xr-x';
692 } elsif (S_ISLNK($mode)) {
693 return 'lrwxrwxrwx';
694 } elsif (S_ISREG($mode)) {
695 # git cares only about the executable bit
696 if ($mode & S_IXUSR) {
697 return '-rwxr-xr-x';
698 } else {
699 return '-rw-r--r--';
700 };
701 } else {
702 return '----------';
703 }
704 }
705
706 # convert file mode in octal to file type string
707 sub file_type {
708 my $mode = shift;
709
710 if ($mode !~ m/^[0-7]+$/) {
711 return $mode;
712 } else {
713 $mode = oct $mode;
714 }
715
716 if (S_ISDIR($mode & S_IFMT)) {
717 return "directory";
718 } elsif (S_ISLNK($mode)) {
719 return "symlink";
720 } elsif (S_ISREG($mode)) {
721 return "file";
722 } else {
723 return "unknown";
724 }
725 }
726
727 ## ----------------------------------------------------------------------
728 ## functions returning short HTML fragments, or transforming HTML fragments
729 ## which don't beling to other sections
730
731 # format line of commit message or tag comment
732 sub format_log_line_html {
733 my $line = shift;
734
735 $line = esc_html($line);
736 $line =~ s/ /&nbsp;/g;
737 if ($line =~ m/([0-9a-fA-F]{40})/) {
738 my $hash_text = $1;
739 if (git_get_type($hash_text) eq "commit") {
740 my $link =
741 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
742 -class => "text"}, $hash_text);
743 $line =~ s/$hash_text/$link/;
744 }
745 }
746 return $line;
747 }
748
749 # format marker of refs pointing to given object
750 sub format_ref_marker {
751 my ($refs, $id) = @_;
752 my $markers = '';
753
754 if (defined $refs->{$id}) {
755 foreach my $ref (@{$refs->{$id}}) {
756 my ($type, $name) = qw();
757 # e.g. tags/v2.6.11 or heads/next
758 if ($ref =~ m!^(.*?)s?/(.*)$!) {
759 $type = $1;
760 $name = $2;
761 } else {
762 $type = "ref";
763 $name = $ref;
764 }
765
766 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
767 }
768 }
769
770 if ($markers) {
771 return ' <span class="refs">'. $markers . '</span>';
772 } else {
773 return "";
774 }
775 }
776
777 # format, perhaps shortened and with markers, title line
778 sub format_subject_html {
779 my ($long, $short, $href, $extra) = @_;
780 $extra = '' unless defined($extra);
781
782 if (length($short) < length($long)) {
783 return $cgi->a({-href => $href, -class => "list subject",
784 -title => to_utf8($long)},
785 esc_html($short) . $extra);
786 } else {
787 return $cgi->a({-href => $href, -class => "list subject"},
788 esc_html($long) . $extra);
789 }
790 }
791
792 sub format_diff_line {
793 my $line = shift;
794 my $char = substr($line, 0, 1);
795 my $diff_class = "";
796
797 chomp $line;
798
799 if ($char eq '+') {
800 $diff_class = " add";
801 } elsif ($char eq "-") {
802 $diff_class = " rem";
803 } elsif ($char eq "@") {
804 $diff_class = " chunk_header";
805 } elsif ($char eq "\\") {
806 $diff_class = " incomplete";
807 }
808 $line = untabify($line);
809 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
810 }
811
812 ## ----------------------------------------------------------------------
813 ## git utility subroutines, invoking git commands
814
815 # returns path to the core git executable and the --git-dir parameter as list
816 sub git_cmd {
817 return $GIT, '--git-dir='.$git_dir;
818 }
819
820 # returns path to the core git executable and the --git-dir parameter as string
821 sub git_cmd_str {
822 return join(' ', git_cmd());
823 }
824
825 # get HEAD ref of given project as hash
826 sub git_get_head_hash {
827 my $project = shift;
828 my $o_git_dir = $git_dir;
829 my $retval = undef;
830 $git_dir = "$projectroot/$project";
831 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
832 my $head = <$fd>;
833 close $fd;
834 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
835 $retval = $1;
836 }
837 }
838 if (defined $o_git_dir) {
839 $git_dir = $o_git_dir;
840 }
841 return $retval;
842 }
843
844 # get type of given object
845 sub git_get_type {
846 my $hash = shift;
847
848 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
849 my $type = <$fd>;
850 close $fd or return;
851 chomp $type;
852 return $type;
853 }
854
855 sub git_get_project_config {
856 my ($key, $type) = @_;
857
858 return unless ($key);
859 $key =~ s/^gitweb\.//;
860 return if ($key =~ m/\W/);
861
862 my @x = (git_cmd(), 'repo-config');
863 if (defined $type) { push @x, $type; }
864 push @x, "--get";
865 push @x, "gitweb.$key";
866 my $val = qx(@x);
867 chomp $val;
868 return ($val);
869 }
870
871 # get hash of given path at given ref
872 sub git_get_hash_by_path {
873 my $base = shift;
874 my $path = shift || return undef;
875 my $type = shift;
876
877 $path =~ s,/+$,,;
878
879 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
880 or die_error(undef, "Open git-ls-tree failed");
881 my $line = <$fd>;
882 close $fd or return undef;
883
884 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
885 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
886 if (defined $type && $type ne $2) {
887 # type doesn't match
888 return undef;
889 }
890 return $3;
891 }
892
893 ## ......................................................................
894 ## git utility functions, directly accessing git repository
895
896 sub git_get_project_description {
897 my $path = shift;
898
899 open my $fd, "$projectroot/$path/description" or return undef;
900 my $descr = <$fd>;
901 close $fd;
902 chomp $descr;
903 return $descr;
904 }
905
906 sub git_get_project_url_list {
907 my $path = shift;
908
909 open my $fd, "$projectroot/$path/cloneurl" or return;
910 my @git_project_url_list = map { chomp; $_ } <$fd>;
911 close $fd;
912
913 return wantarray ? @git_project_url_list : \@git_project_url_list;
914 }
915
916 sub git_get_projects_list {
917 my ($filter) = @_;
918 my @list;
919
920 $filter ||= '';
921 $filter =~ s/\.git$//;
922
923 if (-d $projects_list) {
924 # search in directory
925 my $dir = $projects_list . ($filter ? "/$filter" : '');
926 my $pfxlen = length("$dir");
927
928 my $check_forks = gitweb_check_feature('forks');
929
930 File::Find::find({
931 follow_fast => 1, # follow symbolic links
932 dangling_symlinks => 0, # ignore dangling symlinks, silently
933 wanted => sub {
934 # skip project-list toplevel, if we get it.
935 return if (m!^[/.]$!);
936 # only directories can be git repositories
937 return unless (-d $_);
938
939 my $subdir = substr($File::Find::name, $pfxlen + 1);
940 # we check related file in $projectroot
941 if ($check_forks and $subdir =~ m#/.#) {
942 $File::Find::prune = 1;
943 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
944 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
945 $File::Find::prune = 1;
946 }
947 },
948 }, "$dir");
949
950 } elsif (-f $projects_list) {
951 # read from file(url-encoded):
952 # 'git%2Fgit.git Linus+Torvalds'
953 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
954 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
955 open my ($fd), $projects_list or return;
956 while (my $line = <$fd>) {
957 chomp $line;
958 my ($path, $owner) = split ' ', $line;
959 $path = unescape($path);
960 $owner = unescape($owner);
961 if (!defined $path) {
962 next;
963 }
964 if (check_export_ok("$projectroot/$path")) {
965 my $pr = {
966 path => $path,
967 owner => to_utf8($owner),
968 };
969 push @list, $pr
970 }
971 }
972 close $fd;
973 }
974 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
975 return @list;
976 }
977
978 sub git_get_project_owner {
979 my $project = shift;
980 my $owner;
981
982 return undef unless $project;
983
984 # read from file (url-encoded):
985 # 'git%2Fgit.git Linus+Torvalds'
986 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
987 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
988 if (-f $projects_list) {
989 open (my $fd , $projects_list);
990 while (my $line = <$fd>) {
991 chomp $line;
992 my ($pr, $ow) = split ' ', $line;
993 $pr = unescape($pr);
994 $ow = unescape($ow);
995 if ($pr eq $project) {
996 $owner = to_utf8($ow);
997 last;
998 }
999 }
1000 close $fd;
1001 }
1002 if (!defined $owner) {
1003 $owner = get_file_owner("$projectroot/$project");
1004 }
1005
1006 return $owner;
1007 }
1008
1009 sub git_get_last_activity {
1010 my ($path) = @_;
1011 my $fd;
1012
1013 $git_dir = "$projectroot/$path";
1014 open($fd, "-|", git_cmd(), 'for-each-ref',
1015 '--format=%(refname) %(committer)',
1016 '--sort=-committerdate',
1017 'refs/heads') or return;
1018 my $most_recent = <$fd>;
1019 close $fd or return;
1020 if ($most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1021 my $timestamp = $1;
1022 my $age = time - $timestamp;
1023 return ($age, age_string($age));
1024 }
1025 }
1026
1027 sub git_get_references {
1028 my $type = shift || "";
1029 my %refs;
1030 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1031 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1032 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1033 or return;
1034
1035 while (my $line = <$fd>) {
1036 chomp $line;
1037 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
1038 if (defined $refs{$1}) {
1039 push @{$refs{$1}}, $2;
1040 } else {
1041 $refs{$1} = [ $2 ];
1042 }
1043 }
1044 }
1045 close $fd or return;
1046 return \%refs;
1047 }
1048
1049 sub git_get_rev_name_tags {
1050 my $hash = shift || return undef;
1051
1052 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1053 or return;
1054 my $name_rev = <$fd>;
1055 close $fd;
1056
1057 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1058 return $1;
1059 } else {
1060 # catches also '$hash undefined' output
1061 return undef;
1062 }
1063 }
1064
1065 ## ----------------------------------------------------------------------
1066 ## parse to hash functions
1067
1068 sub parse_date {
1069 my $epoch = shift;
1070 my $tz = shift || "-0000";
1071
1072 my %date;
1073 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1074 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1075 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1076 $date{'hour'} = $hour;
1077 $date{'minute'} = $min;
1078 $date{'mday'} = $mday;
1079 $date{'day'} = $days[$wday];
1080 $date{'month'} = $months[$mon];
1081 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1082 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1083 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1084 $mday, $months[$mon], $hour ,$min;
1085
1086 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1087 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1088 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1089 $date{'hour_local'} = $hour;
1090 $date{'minute_local'} = $min;
1091 $date{'tz_local'} = $tz;
1092 $date{'iso-tz'} = sprintf ("%04d-%02d-%02d %02d:%02d:%02d %s",
1093 1900+$year, $mon+1, $mday,
1094 $hour, $min, $sec, $tz);
1095 return %date;
1096 }
1097
1098 sub parse_tag {
1099 my $tag_id = shift;
1100 my %tag;
1101 my @comment;
1102
1103 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1104 $tag{'id'} = $tag_id;
1105 while (my $line = <$fd>) {
1106 chomp $line;
1107 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1108 $tag{'object'} = $1;
1109 } elsif ($line =~ m/^type (.+)$/) {
1110 $tag{'type'} = $1;
1111 } elsif ($line =~ m/^tag (.+)$/) {
1112 $tag{'name'} = $1;
1113 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1114 $tag{'author'} = $1;
1115 $tag{'epoch'} = $2;
1116 $tag{'tz'} = $3;
1117 } elsif ($line =~ m/--BEGIN/) {
1118 push @comment, $line;
1119 last;
1120 } elsif ($line eq "") {
1121 last;
1122 }
1123 }
1124 push @comment, <$fd>;
1125 $tag{'comment'} = \@comment;
1126 close $fd or return;
1127 if (!defined $tag{'name'}) {
1128 return
1129 };
1130 return %tag
1131 }
1132
1133 sub parse_commit {
1134 my $commit_id = shift;
1135 my $commit_text = shift;
1136
1137 my @commit_lines;
1138 my %co;
1139
1140 if (defined $commit_text) {
1141 @commit_lines = @$commit_text;
1142 } else {
1143 local $/ = "\0";
1144 open my $fd, "-|", git_cmd(), "rev-list",
1145 "--header", "--parents", "--max-count=1",
1146 $commit_id, "--"
1147 or return;
1148 @commit_lines = split '\n', <$fd>;
1149 close $fd or return;
1150 pop @commit_lines;
1151 }
1152 my $header = shift @commit_lines;
1153 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1154 return;
1155 }
1156 ($co{'id'}, my @parents) = split ' ', $header;
1157 $co{'parents'} = \@parents;
1158 $co{'parent'} = $parents[0];
1159 while (my $line = shift @commit_lines) {
1160 last if $line eq "\n";
1161 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1162 $co{'tree'} = $1;
1163 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1164 $co{'author'} = $1;
1165 $co{'author_epoch'} = $2;
1166 $co{'author_tz'} = $3;
1167 if ($co{'author'} =~ m/^([^<]+) </) {
1168 $co{'author_name'} = $1;
1169 } else {
1170 $co{'author_name'} = $co{'author'};
1171 }
1172 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1173 $co{'committer'} = $1;
1174 $co{'committer_epoch'} = $2;
1175 $co{'committer_tz'} = $3;
1176 $co{'committer_name'} = $co{'committer'};
1177 $co{'committer_name'} =~ s/ <.*//;
1178 }
1179 }
1180 if (!defined $co{'tree'}) {
1181 return;
1182 };
1183
1184 foreach my $title (@commit_lines) {
1185 $title =~ s/^ //;
1186 if ($title ne "") {
1187 $co{'title'} = chop_str($title, 80, 5);
1188 # remove leading stuff of merges to make the interesting part visible
1189 if (length($title) > 50) {
1190 $title =~ s/^Automatic //;
1191 $title =~ s/^merge (of|with) /Merge ... /i;
1192 if (length($title) > 50) {
1193 $title =~ s/(http|rsync):\/\///;
1194 }
1195 if (length($title) > 50) {
1196 $title =~ s/(master|www|rsync)\.//;
1197 }
1198 if (length($title) > 50) {
1199 $title =~ s/kernel.org:?//;
1200 }
1201 if (length($title) > 50) {
1202 $title =~ s/\/pub\/scm//;
1203 }
1204 }
1205 $co{'title_short'} = chop_str($title, 50, 5);
1206 last;
1207 }
1208 }
1209 if ($co{'title'} eq "") {
1210 $co{'title'} = $co{'title_short'} = '(no commit message)';
1211 }
1212 # remove added spaces
1213 foreach my $line (@commit_lines) {
1214 $line =~ s/^ //;
1215 }
1216 $co{'comment'} = \@commit_lines;
1217
1218 my $age = time - $co{'committer_epoch'};
1219 $co{'age'} = $age;
1220 $co{'age_string'} = age_string($age);
1221 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1222 if ($age > 60*60*24*7*2) {
1223 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1224 $co{'age_string_age'} = $co{'age_string'};
1225 } else {
1226 $co{'age_string_date'} = $co{'age_string'};
1227 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1228 }
1229 return %co;
1230 }
1231
1232 # parse ref from ref_file, given by ref_id, with given type
1233 sub parse_ref {
1234 my $ref_file = shift;
1235 my $ref_id = shift;
1236 my $type = shift || git_get_type($ref_id);
1237 my %ref_item;
1238
1239 $ref_item{'type'} = $type;
1240 $ref_item{'id'} = $ref_id;
1241 $ref_item{'epoch'} = 0;
1242 $ref_item{'age'} = "unknown";
1243 if ($type eq "tag") {
1244 my %tag = parse_tag($ref_id);
1245 $ref_item{'comment'} = $tag{'comment'};
1246 if ($tag{'type'} eq "commit") {
1247 my %co = parse_commit($tag{'object'});
1248 $ref_item{'epoch'} = $co{'committer_epoch'};
1249 $ref_item{'age'} = $co{'age_string'};
1250 } elsif (defined($tag{'epoch'})) {
1251 my $age = time - $tag{'epoch'};
1252 $ref_item{'epoch'} = $tag{'epoch'};
1253 $ref_item{'age'} = age_string($age);
1254 }
1255 $ref_item{'reftype'} = $tag{'type'};
1256 $ref_item{'name'} = $tag{'name'};
1257 $ref_item{'refid'} = $tag{'object'};
1258 } elsif ($type eq "commit"){
1259 my %co = parse_commit($ref_id);
1260 $ref_item{'reftype'} = "commit";
1261 $ref_item{'name'} = $ref_file;
1262 $ref_item{'title'} = $co{'title'};
1263 $ref_item{'refid'} = $ref_id;
1264 $ref_item{'epoch'} = $co{'committer_epoch'};
1265 $ref_item{'age'} = $co{'age_string'};
1266 } else {
1267 $ref_item{'reftype'} = $type;
1268 $ref_item{'name'} = $ref_file;
1269 $ref_item{'refid'} = $ref_id;
1270 }
1271
1272 return %ref_item;
1273 }
1274
1275 # parse line of git-diff-tree "raw" output
1276 sub parse_difftree_raw_line {
1277 my $line = shift;
1278 my %res;
1279
1280 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1281 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1282 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1283 $res{'from_mode'} = $1;
1284 $res{'to_mode'} = $2;
1285 $res{'from_id'} = $3;
1286 $res{'to_id'} = $4;
1287 $res{'status'} = $5;
1288 $res{'similarity'} = $6;
1289 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1290 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1291 } else {
1292 $res{'file'} = unquote($7);
1293 }
1294 }
1295 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1296 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1297 $res{'commit'} = $1;
1298 }
1299
1300 return wantarray ? %res : \%res;
1301 }
1302
1303 # parse line of git-ls-tree output
1304 sub parse_ls_tree_line ($;%) {
1305 my $line = shift;
1306 my %opts = @_;
1307 my %res;
1308
1309 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1310 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
1311
1312 $res{'mode'} = $1;
1313 $res{'type'} = $2;
1314 $res{'hash'} = $3;
1315 if ($opts{'-z'}) {
1316 $res{'name'} = $4;
1317 } else {
1318 $res{'name'} = unquote($4);
1319 }
1320
1321 return wantarray ? %res : \%res;
1322 }
1323
1324 ## ......................................................................
1325 ## parse to array of hashes functions
1326
1327 sub git_get_heads_list {
1328 my $limit = shift;
1329 my @headslist;
1330
1331 open my $fd, '-|', git_cmd(), 'for-each-ref',
1332 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
1333 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
1334 'refs/heads'
1335 or return;
1336 while (my $line = <$fd>) {
1337 my %ref_item;
1338
1339 chomp $line;
1340 my ($refinfo, $committerinfo) = split(/\0/, $line);
1341 my ($hash, $name, $title) = split(' ', $refinfo, 3);
1342 my ($committer, $epoch, $tz) =
1343 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
1344 $name =~ s!^refs/heads/!!;
1345
1346 $ref_item{'name'} = $name;
1347 $ref_item{'id'} = $hash;
1348 $ref_item{'title'} = $title || '(no commit message)';
1349 $ref_item{'epoch'} = $epoch;
1350 if ($epoch) {
1351 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1352 } else {
1353 $ref_item{'age'} = "unknown";
1354 }
1355
1356 push @headslist, \%ref_item;
1357 }
1358 close $fd;
1359
1360 return wantarray ? @headslist : \@headslist;
1361 }
1362
1363 sub git_get_tags_list {
1364 my $limit = shift;
1365 my @tagslist;
1366
1367 open my $fd, '-|', git_cmd(), 'for-each-ref',
1368 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
1369 '--format=%(objectname) %(objecttype) %(refname) '.
1370 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
1371 'refs/tags'
1372 or return;
1373 while (my $line = <$fd>) {
1374 my %ref_item;
1375
1376 chomp $line;
1377 my ($refinfo, $creatorinfo) = split(/\0/, $line);
1378 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
1379 my ($creator, $epoch, $tz) =
1380 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
1381 $name =~ s!^refs/tags/!!;
1382
1383 $ref_item{'type'} = $type;
1384 $ref_item{'id'} = $id;
1385 $ref_item{'name'} = $name;
1386 if ($type eq "tag") {
1387 $ref_item{'subject'} = $title;
1388 $ref_item{'reftype'} = $reftype;
1389 $ref_item{'refid'} = $refid;
1390 } else {
1391 $ref_item{'reftype'} = $type;
1392 $ref_item{'refid'} = $id;
1393 }
1394
1395 if ($type eq "tag" || $type eq "commit") {
1396 $ref_item{'epoch'} = $epoch;
1397 if ($epoch) {
1398 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1399 } else {
1400 $ref_item{'age'} = "unknown";
1401 }
1402 }
1403
1404 push @tagslist, \%ref_item;
1405 }
1406 close $fd;
1407
1408 return wantarray ? @tagslist : \@tagslist;
1409 }
1410
1411 ## ----------------------------------------------------------------------
1412 ## filesystem-related functions
1413
1414 sub get_file_owner {
1415 my $path = shift;
1416
1417 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1418 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1419 if (!defined $gcos) {
1420 return undef;
1421 }
1422 my $owner = $gcos;
1423 $owner =~ s/[,;].*$//;
1424 return to_utf8($owner);
1425 }
1426
1427 ## ......................................................................
1428 ## mimetype related functions
1429
1430 sub mimetype_guess_file {
1431 my $filename = shift;
1432 my $mimemap = shift;
1433 -r $mimemap or return undef;
1434
1435 my %mimemap;
1436 open(MIME, $mimemap) or return undef;
1437 while (<MIME>) {
1438 next if m/^#/; # skip comments
1439 my ($mime, $exts) = split(/\t+/);
1440 if (defined $exts) {
1441 my @exts = split(/\s+/, $exts);
1442 foreach my $ext (@exts) {
1443 $mimemap{$ext} = $mime;
1444 }
1445 }
1446 }
1447 close(MIME);
1448
1449 $filename =~ /\.([^.]*)$/;
1450 return $mimemap{$1};
1451 }
1452
1453 sub mimetype_guess {
1454 my $filename = shift;
1455 my $mime;
1456 $filename =~ /\./ or return undef;
1457
1458 if ($mimetypes_file) {
1459 my $file = $mimetypes_file;
1460 if ($file !~ m!^/!) { # if it is relative path
1461 # it is relative to project
1462 $file = "$projectroot/$project/$file";
1463 }
1464 $mime = mimetype_guess_file($filename, $file);
1465 }
1466 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1467 return $mime;
1468 }
1469
1470 sub blob_mimetype {
1471 my $fd = shift;
1472 my $filename = shift;
1473
1474 if ($filename) {
1475 my $mime = mimetype_guess($filename);
1476 $mime and return $mime;
1477 }
1478
1479 # just in case
1480 return $default_blob_plain_mimetype unless $fd;
1481
1482 if (-T $fd) {
1483 return 'text/plain' .
1484 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1485 } elsif (! $filename) {
1486 return 'application/octet-stream';
1487 } elsif ($filename =~ m/\.png$/i) {
1488 return 'image/png';
1489 } elsif ($filename =~ m/\.gif$/i) {
1490 return 'image/gif';
1491 } elsif ($filename =~ m/\.jpe?g$/i) {
1492 return 'image/jpeg';
1493 } else {
1494 return 'application/octet-stream';
1495 }
1496 }
1497
1498 ## ======================================================================
1499 ## functions printing HTML: header, footer, error page
1500
1501 sub git_header_html {
1502 my $status = shift || "200 OK";
1503 my $expires = shift;
1504
1505 my $title = "$site_name";
1506 if (defined $project) {
1507 $title .= " - $project";
1508 if (defined $action) {
1509 $title .= "/$action";
1510 if (defined $file_name) {
1511 $title .= " - " . esc_html($file_name);
1512 if ($action eq "tree" && $file_name !~ m|/$|) {
1513 $title .= "/";
1514 }
1515 }
1516 }
1517 }
1518 my $content_type;
1519 # require explicit support from the UA if we are to send the page as
1520 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1521 # we have to do this because MSIE sometimes globs '*/*', pretending to
1522 # support xhtml+xml but choking when it gets what it asked for.
1523 if (defined $cgi->http('HTTP_ACCEPT') &&
1524 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1525 $cgi->Accept('application/xhtml+xml') != 0) {
1526 $content_type = 'application/xhtml+xml';
1527 } else {
1528 $content_type = 'text/html';
1529 }
1530 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1531 -status=> $status, -expires => $expires);
1532 print <<EOF;
1533 <?xml version="1.0" encoding="utf-8"?>
1534 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1535 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1536 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1537 <!-- git core binaries version $git_version -->
1538 <head>
1539 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1540 <meta name="generator" content="gitweb/$version git/$git_version"/>
1541 <meta name="robots" content="index, nofollow"/>
1542 <title>$title</title>
1543 EOF
1544 # print out each stylesheet that exist
1545 if (defined $stylesheet) {
1546 #provides backwards capability for those people who define style sheet in a config file
1547 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1548 } else {
1549 foreach my $stylesheet (@stylesheets) {
1550 next unless $stylesheet;
1551 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1552 }
1553 }
1554 if (defined $project) {
1555 printf('<link rel="alternate" title="%s log" '.
1556 'href="%s" type="application/rss+xml"/>'."\n",
1557 esc_param($project), href(action=>"rss"));
1558 } else {
1559 printf('<link rel="alternate" title="%s projects list" '.
1560 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1561 $site_name, href(project=>undef, action=>"project_index"));
1562 printf('<link rel="alternate" title="%s projects logs" '.
1563 'href="%s" type="text/x-opml"/>'."\n",
1564 $site_name, href(project=>undef, action=>"opml"));
1565 }
1566 if (defined $favicon) {
1567 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1568 }
1569
1570 print "</head>\n" .
1571 "<body>\n";
1572
1573 if (-f $site_header) {
1574 open (my $fd, $site_header);
1575 print <$fd>;
1576 close $fd;
1577 }
1578
1579 print "<div class=\"page_header\">\n" .
1580 $cgi->a({-href => esc_url($logo_url),
1581 -title => $logo_label},
1582 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1583 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1584 if (defined $project) {
1585 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1586 if (defined $action) {
1587 print " / $action";
1588 }
1589 print "\n";
1590 if (!defined $searchtext) {
1591 $searchtext = "";
1592 }
1593 my $search_hash;
1594 if (defined $hash_base) {
1595 $search_hash = $hash_base;
1596 } elsif (defined $hash) {
1597 $search_hash = $hash;
1598 } else {
1599 $search_hash = "HEAD";
1600 }
1601 $cgi->param("a", "search");
1602 $cgi->param("h", $search_hash);
1603 $cgi->param("p", $project);
1604 print $cgi->startform(-method => "get", -action => $my_uri) .
1605 "<div class=\"search\">\n" .
1606 $cgi->hidden(-name => "p") . "\n" .
1607 $cgi->hidden(-name => "a") . "\n" .
1608 $cgi->hidden(-name => "h") . "\n" .
1609 $cgi->popup_menu(-name => 'st', -default => 'commit',
1610 -values => ['commit', 'author', 'committer', 'pickaxe']) .
1611 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
1612 " search:\n",
1613 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1614 "</div>" .
1615 $cgi->end_form() . "\n";
1616 }
1617 print "</div>\n";
1618 }
1619
1620 sub git_footer_html {
1621 print "<div class=\"page_footer\">\n";
1622 if (defined $project) {
1623 my $descr = git_get_project_description($project);
1624 if (defined $descr) {
1625 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1626 }
1627 print $cgi->a({-href => href(action=>"rss"),
1628 -class => "rss_logo"}, "RSS") . "\n";
1629 } else {
1630 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1631 -class => "rss_logo"}, "OPML") . " ";
1632 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1633 -class => "rss_logo"}, "TXT") . "\n";
1634 }
1635 print "</div>\n" ;
1636
1637 if (-f $site_footer) {
1638 open (my $fd, $site_footer);
1639 print <$fd>;
1640 close $fd;
1641 }
1642
1643 print "</body>\n" .
1644 "</html>";
1645 }
1646
1647 sub die_error {
1648 my $status = shift || "403 Forbidden";
1649 my $error = shift || "Malformed query, file missing or permission denied";
1650
1651 git_header_html($status);
1652 print <<EOF;
1653 <div class="page_body">
1654 <br /><br />
1655 $status - $error
1656 <br />
1657 </div>
1658 EOF
1659 git_footer_html();
1660 exit;
1661 }
1662
1663 ## ----------------------------------------------------------------------
1664 ## functions printing or outputting HTML: navigation
1665
1666 sub git_print_page_nav {
1667 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1668 $extra = '' if !defined $extra; # pager or formats
1669
1670 my @navs = qw(summary shortlog log commit commitdiff tree);
1671 if ($suppress) {
1672 @navs = grep { $_ ne $suppress } @navs;
1673 }
1674
1675 my %arg = map { $_ => {action=>$_} } @navs;
1676 if (defined $head) {
1677 for (qw(commit commitdiff)) {
1678 $arg{$_}{hash} = $head;
1679 }
1680 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1681 for (qw(shortlog log)) {
1682 $arg{$_}{hash} = $head;
1683 }
1684 }
1685 }
1686 $arg{tree}{hash} = $treehead if defined $treehead;
1687 $arg{tree}{hash_base} = $treebase if defined $treebase;
1688
1689 print "<div class=\"page_nav\">\n" .
1690 (join " | ",
1691 map { $_ eq $current ?
1692 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1693 } @navs);
1694 print "<br/>\n$extra<br/>\n" .
1695 "</div>\n";
1696 }
1697
1698 sub format_paging_nav {
1699 my ($action, $hash, $head, $page, $nrevs) = @_;
1700 my $paging_nav;
1701
1702
1703 if ($hash ne $head || $page) {
1704 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1705 } else {
1706 $paging_nav .= "HEAD";
1707 }
1708
1709 if ($page > 0) {
1710 $paging_nav .= " &sdot; " .
1711 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1712 -accesskey => "p", -title => "Alt-p"}, "prev");
1713 } else {
1714 $paging_nav .= " &sdot; prev";
1715 }
1716
1717 if ($nrevs >= (100 * ($page+1)-1)) {
1718 $paging_nav .= " &sdot; " .
1719 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1720 -accesskey => "n", -title => "Alt-n"}, "next");
1721 } else {
1722 $paging_nav .= " &sdot; next";
1723 }
1724
1725 return $paging_nav;
1726 }
1727
1728 ## ......................................................................
1729 ## functions printing or outputting HTML: div
1730
1731 sub git_print_header_div {
1732 my ($action, $title, $hash, $hash_base) = @_;
1733 my %args = ();
1734
1735 $args{action} = $action;
1736 $args{hash} = $hash if $hash;
1737 $args{hash_base} = $hash_base if $hash_base;
1738
1739 print "<div class=\"header\">\n" .
1740 $cgi->a({-href => href(%args), -class => "title"},
1741 $title ? $title : $action) .
1742 "\n</div>\n";
1743 }
1744
1745 #sub git_print_authorship (\%) {
1746 sub git_print_authorship {
1747 my $co = shift;
1748
1749 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1750 print "<div class=\"author_date\">" .
1751 esc_html($co->{'author_name'}) .
1752 " [$ad{'rfc2822'}";
1753 if ($ad{'hour_local'} < 6) {
1754 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1755 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1756 } else {
1757 printf(" (%02d:%02d %s)",
1758 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1759 }
1760 print "]</div>\n";
1761 }
1762
1763 sub git_print_page_path {
1764 my $name = shift;
1765 my $type = shift;
1766 my $hb = shift;
1767
1768
1769 print "<div class=\"page_path\">";
1770 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1771 -title => 'tree root'}, "[$project]");
1772 print " / ";
1773 if (defined $name) {
1774 my @dirname = split '/', $name;
1775 my $basename = pop @dirname;
1776 my $fullname = '';
1777
1778 foreach my $dir (@dirname) {
1779 $fullname .= ($fullname ? '/' : '') . $dir;
1780 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1781 hash_base=>$hb),
1782 -title => $fullname}, esc_html($dir));
1783 print " / ";
1784 }
1785 if (defined $type && $type eq 'blob') {
1786 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1787 hash_base=>$hb),
1788 -title => $name}, esc_html($basename));
1789 } elsif (defined $type && $type eq 'tree') {
1790 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1791 hash_base=>$hb),
1792 -title => $name}, esc_html($basename));
1793 print " / ";
1794 } else {
1795 print esc_html($basename);
1796 }
1797 }
1798 print "<br/></div>\n";
1799 }
1800
1801 # sub git_print_log (\@;%) {
1802 sub git_print_log ($;%) {
1803 my $log = shift;
1804 my %opts = @_;
1805
1806 if ($opts{'-remove_title'}) {
1807 # remove title, i.e. first line of log
1808 shift @$log;
1809 }
1810 # remove leading empty lines
1811 while (defined $log->[0] && $log->[0] eq "") {
1812 shift @$log;
1813 }
1814
1815 # print log
1816 my $signoff = 0;
1817 my $empty = 0;
1818 foreach my $line (@$log) {
1819 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1820 $signoff = 1;
1821 $empty = 0;
1822 if (! $opts{'-remove_signoff'}) {
1823 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1824 next;
1825 } else {
1826 # remove signoff lines
1827 next;
1828 }
1829 } else {
1830 $signoff = 0;
1831 }
1832
1833 # print only one empty line
1834 # do not print empty line after signoff
1835 if ($line eq "") {
1836 next if ($empty || $signoff);
1837 $empty = 1;
1838 } else {
1839 $empty = 0;
1840 }
1841
1842 print format_log_line_html($line) . "<br/>\n";
1843 }
1844
1845 if ($opts{'-final_empty_line'}) {
1846 # end with single empty line
1847 print "<br/>\n" unless $empty;
1848 }
1849 }
1850
1851 # print tree entry (row of git_tree), but without encompassing <tr> element
1852 sub git_print_tree_entry {
1853 my ($t, $basedir, $hash_base, $have_blame) = @_;
1854
1855 my %base_key = ();
1856 $base_key{hash_base} = $hash_base if defined $hash_base;
1857
1858 # The format of a table row is: mode list link. Where mode is
1859 # the mode of the entry, list is the name of the entry, an href,
1860 # and link is the action links of the entry.
1861
1862 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1863 if ($t->{'type'} eq "blob") {
1864 print "<td class=\"list\">" .
1865 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1866 file_name=>"$basedir$t->{'name'}", %base_key),
1867 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1868 print "<td class=\"link\">";
1869 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1870 file_name=>"$basedir$t->{'name'}", %base_key)},
1871 "blob");
1872 if ($have_blame) {
1873 print " | " .
1874 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1875 file_name=>"$basedir$t->{'name'}", %base_key)},
1876 "blame");
1877 }
1878 if (defined $hash_base) {
1879 print " | " .
1880 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1881 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1882 "history");
1883 }
1884 print " | " .
1885 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
1886 file_name=>"$basedir$t->{'name'}")},
1887 "raw");
1888 print "</td>\n";
1889
1890 } elsif ($t->{'type'} eq "tree") {
1891 print "<td class=\"list\">";
1892 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1893 file_name=>"$basedir$t->{'name'}", %base_key)},
1894 esc_html($t->{'name'}));
1895 print "</td>\n";
1896 print "<td class=\"link\">";
1897 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1898 file_name=>"$basedir$t->{'name'}", %base_key)},
1899 "tree");
1900 if (defined $hash_base) {
1901 print " | " .
1902 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1903 file_name=>"$basedir$t->{'name'}")},
1904 "history");
1905 }
1906 print "</td>\n";
1907 }
1908 }
1909
1910 ## ......................................................................
1911 ## functions printing large fragments of HTML
1912
1913 sub git_difftree_body {
1914 my ($difftree, $hash, $parent) = @_;
1915
1916 print "<div class=\"list_head\">\n";
1917 if ($#{$difftree} > 10) {
1918 print(($#{$difftree} + 1) . " files changed:\n");
1919 }
1920 print "</div>\n";
1921
1922 print "<table class=\"diff_tree\">\n";
1923 my $alternate = 1;
1924 my $patchno = 0;
1925 foreach my $line (@{$difftree}) {
1926 my %diff = parse_difftree_raw_line($line);
1927
1928 if ($alternate) {
1929 print "<tr class=\"dark\">\n";
1930 } else {
1931 print "<tr class=\"light\">\n";
1932 }
1933 $alternate ^= 1;
1934
1935 my ($to_mode_oct, $to_mode_str, $to_file_type);
1936 my ($from_mode_oct, $from_mode_str, $from_file_type);
1937 if ($diff{'to_mode'} ne ('0' x 6)) {
1938 $to_mode_oct = oct $diff{'to_mode'};
1939 if (S_ISREG($to_mode_oct)) { # only for regular file
1940 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1941 }
1942 $to_file_type = file_type($diff{'to_mode'});
1943 }
1944 if ($diff{'from_mode'} ne ('0' x 6)) {
1945 $from_mode_oct = oct $diff{'from_mode'};
1946 if (S_ISREG($to_mode_oct)) { # only for regular file
1947 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1948 }
1949 $from_file_type = file_type($diff{'from_mode'});
1950 }
1951
1952 if ($diff{'status'} eq "A") { # created
1953 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1954 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1955 $mode_chng .= "]</span>";
1956 print "<td>";
1957 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1958 hash_base=>$hash, file_name=>$diff{'file'}),
1959 -class => "list"}, esc_html($diff{'file'}));
1960 print "</td>\n";
1961 print "<td>$mode_chng</td>\n";
1962 print "<td class=\"link\">";
1963 if ($action eq 'commitdiff') {
1964 # link to patch
1965 $patchno++;
1966 print $cgi->a({-href => "#patch$patchno"}, "patch");
1967 }
1968 print "</td>\n";
1969
1970 } elsif ($diff{'status'} eq "D") { # deleted
1971 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1972 print "<td>";
1973 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1974 hash_base=>$parent, file_name=>$diff{'file'}),
1975 -class => "list"}, esc_html($diff{'file'}));
1976 print "</td>\n";
1977 print "<td>$mode_chng</td>\n";
1978 print "<td class=\"link\">";
1979 if ($action eq 'commitdiff') {
1980 # link to patch
1981 $patchno++;
1982 print $cgi->a({-href => "#patch$patchno"}, "patch");
1983 print " | ";
1984 }
1985 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1986 hash_base=>$parent, file_name=>$diff{'file'})},
1987 "blob") . " | ";
1988 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
1989 file_name=>$diff{'file'})},
1990 "blame") . " | ";
1991 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1992 file_name=>$diff{'file'})},
1993 "history");
1994 print "</td>\n";
1995
1996 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1997 my $mode_chnge = "";
1998 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1999 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
2000 if ($from_file_type != $to_file_type) {
2001 $mode_chnge .= " from $from_file_type to $to_file_type";
2002 }
2003 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2004 if ($from_mode_str && $to_mode_str) {
2005 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2006 } elsif ($to_mode_str) {
2007 $mode_chnge .= " mode: $to_mode_str";
2008 }
2009 }
2010 $mode_chnge .= "]</span>\n";
2011 }
2012 print "<td>";
2013 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2014 hash_base=>$hash, file_name=>$diff{'file'}),
2015 -class => "list"}, esc_html($diff{'file'}));
2016 print "</td>\n";
2017 print "<td>$mode_chnge</td>\n";
2018 print "<td class=\"link\">";
2019 if ($action eq 'commitdiff') {
2020 # link to patch
2021 $patchno++;
2022 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2023 " | ";
2024 } elsif ($diff{'to_id'} ne $diff{'from_id'}) {
2025 # "commit" view and modified file (not onlu mode changed)
2026 print $cgi->a({-href => href(action=>"blobdiff",
2027 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
2028 hash_base=>$hash, hash_parent_base=>$parent,
2029 file_name=>$diff{'file'})},
2030 "diff") .
2031 " | ";
2032 }
2033 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2034 hash_base=>$hash, file_name=>$diff{'file'})},
2035 "blob") . " | ";
2036 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2037 file_name=>$diff{'file'})},
2038 "blame") . " | ";
2039 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2040 file_name=>$diff{'file'})},
2041 "history");
2042 print "</td>\n";
2043
2044 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
2045 my %status_name = ('R' => 'moved', 'C' => 'copied');
2046 my $nstatus = $status_name{$diff{'status'}};
2047 my $mode_chng = "";
2048 if ($diff{'from_mode'} != $diff{'to_mode'}) {
2049 # mode also for directories, so we cannot use $to_mode_str
2050 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
2051 }
2052 print "<td>" .
2053 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2054 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
2055 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
2056 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
2057 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2058 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
2059 -class => "list"}, esc_html($diff{'from_file'})) .
2060 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
2061 "<td class=\"link\">";
2062 if ($action eq 'commitdiff') {
2063 # link to patch
2064 $patchno++;
2065 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2066 " | ";
2067 } elsif ($diff{'to_id'} ne $diff{'from_id'}) {
2068 # "commit" view and modified file (not only pure rename or copy)
2069 print $cgi->a({-href => href(action=>"blobdiff",
2070 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
2071 hash_base=>$hash, hash_parent_base=>$parent,
2072 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
2073 "diff") .
2074 " | ";
2075 }
2076 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
2077 hash_base=>$parent, file_name=>$diff{'from_file'})},
2078 "blob") . " | ";
2079 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2080 file_name=>$diff{'from_file'})},
2081 "blame") . " | ";
2082 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2083 file_name=>$diff{'from_file'})},
2084 "history");
2085 print "</td>\n";
2086
2087 } # we should not encounter Unmerged (U) or Unknown (X) status
2088 print "</tr>\n";
2089 }
2090 print "</table>\n";
2091 }
2092
2093 sub git_patchset_body {
2094 my ($fd, $difftree, $hash, $hash_parent) = @_;
2095
2096 my $patch_idx = 0;
2097 my $in_header = 0;
2098 my $patch_found = 0;
2099 my $diffinfo;
2100
2101 print "<div class=\"patchset\">\n";
2102
2103 LINE:
2104 while (my $patch_line = <$fd>) {
2105 chomp $patch_line;
2106
2107 if ($patch_line =~ m/^diff /) { # "git diff" header
2108 # beginning of patch (in patchset)
2109 if ($patch_found) {
2110 # close previous patch
2111 print "</div>\n"; # class="patch"
2112 } else {
2113 # first patch in patchset
2114 $patch_found = 1;
2115 }
2116 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2117
2118 if (ref($difftree->[$patch_idx]) eq "HASH") {
2119 $diffinfo = $difftree->[$patch_idx];
2120 } else {
2121 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2122 }
2123 $patch_idx++;
2124
2125 if ($diffinfo->{'status'} eq "A") { # added
2126 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
2127 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2128 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2129 $diffinfo->{'to_id'}) . " (new)" .
2130 "</div>\n"; # class="diff_info"
2131
2132 } elsif ($diffinfo->{'status'} eq "D") { # deleted
2133 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
2134 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2135 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2136 $diffinfo->{'from_id'}) . " (deleted)" .
2137 "</div>\n"; # class="diff_info"
2138
2139 } elsif ($diffinfo->{'status'} eq "R" || # renamed
2140 $diffinfo->{'status'} eq "C" || # copied
2141 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
2142 print "<div class=\"diff_info\">" .
2143 file_type($diffinfo->{'from_mode'}) . ":" .
2144 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2145 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
2146 $diffinfo->{'from_id'}) .
2147 " -> " .
2148 file_type($diffinfo->{'to_mode'}) . ":" .
2149 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2150 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
2151 $diffinfo->{'to_id'});
2152 print "</div>\n"; # class="diff_info"
2153
2154 } else { # modified, mode changed, ...
2155 print "<div class=\"diff_info\">" .
2156 file_type($diffinfo->{'from_mode'}) . ":" .
2157 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2158 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2159 $diffinfo->{'from_id'}) .
2160 " -> " .
2161 file_type($diffinfo->{'to_mode'}) . ":" .
2162 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2163 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2164 $diffinfo->{'to_id'});
2165 print "</div>\n"; # class="diff_info"
2166 }
2167
2168 #print "<div class=\"diff extended_header\">\n";
2169 $in_header = 1;
2170 next LINE;
2171 } # start of patch in patchset
2172
2173
2174 if ($in_header && $patch_line =~ m/^---/) {
2175 #print "</div>\n"; # class="diff extended_header"
2176 $in_header = 0;
2177
2178 my $file = $diffinfo->{'from_file'};
2179 $file ||= $diffinfo->{'file'};
2180 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2181 hash=>$diffinfo->{'from_id'}, file_name=>$file),
2182 -class => "list"}, esc_html($file));
2183 $patch_line =~ s|a/.*$|a/$file|g;
2184 print "<div class=\"diff from_file\">$patch_line</div>\n";
2185
2186 $patch_line = <$fd>;
2187 chomp $patch_line;
2188
2189 #$patch_line =~ m/^+++/;
2190 $file = $diffinfo->{'to_file'};
2191 $file ||= $diffinfo->{'file'};
2192 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2193 hash=>$diffinfo->{'to_id'}, file_name=>$file),
2194 -class => "list"}, esc_html($file));
2195 $patch_line =~ s|b/.*|b/$file|g;
2196 print "<div class=\"diff to_file\">$patch_line</div>\n";
2197
2198 next LINE;
2199 }
2200 next LINE if $in_header;
2201
2202 print format_diff_line($patch_line);
2203 }
2204 print "</div>\n" if $patch_found; # class="patch"
2205
2206 print "</div>\n"; # class="patchset"
2207 }
2208
2209 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2210
2211 sub git_project_list_body {
2212 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
2213
2214 my $check_forks = gitweb_check_feature('forks');
2215
2216 my @projects;
2217 foreach my $pr (@$projlist) {
2218 my (@aa) = git_get_last_activity($pr->{'path'});
2219 unless (@aa) {
2220 next;
2221 }
2222 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2223 if (!defined $pr->{'descr'}) {
2224 my $descr = git_get_project_description($pr->{'path'}) || "";
2225 $pr->{'descr'} = chop_str($descr, 25, 5);
2226 }
2227 if (!defined $pr->{'owner'}) {
2228 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2229 }
2230 if ($check_forks) {
2231 my $pname = $pr->{'path'};
2232 $pname =~ s/\.git$//;
2233 $pr->{'forks'} = -d "$projectroot/$pname";
2234 }
2235 push @projects, $pr;
2236 }
2237
2238 $order ||= "project";
2239 $from = 0 unless defined $from;
2240 $to = $#projects if (!defined $to || $#projects < $to);
2241
2242 print "<table class=\"project_list\">\n";
2243 unless ($no_header) {
2244 print "<tr>\n";
2245 if ($check_forks) {
2246 print "<th></th>\n";
2247 }
2248 if ($order eq "project") {
2249 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2250 print "<th>Project</th>\n";
2251 } else {
2252 print "<th>" .
2253 $cgi->a({-href => href(project=>undef, order=>'project'),
2254 -class => "header"}, "Project") .
2255 "</th>\n";
2256 }
2257 if ($order eq "descr") {
2258 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2259 print "<th>Description</th>\n";
2260 } else {
2261 print "<th>" .
2262 $cgi->a({-href => href(project=>undef, order=>'descr'),
2263 -class => "header"}, "Description") .
2264 "</th>\n";
2265 }
2266 if ($order eq "owner") {
2267 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2268 print "<th>Owner</th>\n";
2269 } else {
2270 print "<th>" .
2271 $cgi->a({-href => href(project=>undef, order=>'owner'),
2272 -class => "header"}, "Owner") .
2273 "</th>\n";
2274 }
2275 if ($order eq "age") {
2276 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2277 print "<th>Last Change</th>\n";
2278 } else {
2279 print "<th>" .
2280 $cgi->a({-href => href(project=>undef, order=>'age'),
2281 -class => "header"}, "Last Change") .
2282 "</th>\n";
2283 }
2284 print "<th></th>\n" .
2285 "</tr>\n";
2286 }
2287 my $alternate = 1;
2288 for (my $i = $from; $i <= $to; $i++) {
2289 my $pr = $projects[$i];
2290 if ($alternate) {
2291 print "<tr class=\"dark\">\n";
2292 } else {
2293 print "<tr class=\"light\">\n";
2294 }
2295 $alternate ^= 1;
2296 if ($check_forks) {
2297 print "<td>";
2298 if ($pr->{'forks'}) {
2299 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
2300 }
2301 print "</td>\n";
2302 }
2303 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2304 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2305 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2306 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2307 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
2308 $pr->{'age_string'} . "</td>\n" .
2309 "<td class=\"link\">" .
2310 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2311 $cgi->a({-href => '/git-browser/by-commit.html?r='.$pr->{'path'}}, "graphiclog") . " | " .
2312 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2313 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2314 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
2315 "</td>\n" .
2316 "</tr>\n";
2317 }
2318 if (defined $extra) {
2319 print "<tr>\n";
2320 if ($check_forks) {
2321 print "<td></td>\n";
2322 }
2323 print "<td colspan=\"5\">$extra</td>\n" .
2324 "</tr>\n";
2325 }
2326 print "</table>\n";
2327 }
2328
2329 sub git_shortlog_body {
2330 # uses global variable $project
2331 my ($revlist, $from, $to, $refs, $extra) = @_;
2332
2333 $from = 0 unless defined $from;
2334 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2335
2336 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2337 my $alternate = 1;
2338 for (my $i = $from; $i <= $to; $i++) {
2339 my $commit = $revlist->[$i];
2340 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2341 my $ref = format_ref_marker($refs, $commit);
2342 my %co = parse_commit($commit);
2343 if ($alternate) {
2344 print "<tr class=\"dark\">\n";
2345 } else {
2346 print "<tr class=\"light\">\n";
2347 }
2348 $alternate ^= 1;
2349 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2350 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2351 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2352 "<td>";
2353 print format_subject_html($co{'title'}, $co{'title_short'},
2354 href(action=>"commit", hash=>$commit), $ref);
2355 print "</td>\n" .
2356 "<td class=\"link\">" .
2357 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
2358 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2359 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
2360 if (gitweb_have_snapshot()) {
2361 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2362 }
2363 print "</td>\n" .
2364 "</tr>\n";
2365 }
2366 if (defined $extra) {
2367 print "<tr>\n" .
2368 "<td colspan=\"4\">$extra</td>\n" .
2369 "</tr>\n";
2370 }
2371 print "</table>\n";
2372 }
2373
2374 sub git_history_body {
2375 # Warning: assumes constant type (blob or tree) during history
2376 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2377
2378 $from = 0 unless defined $from;
2379 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2380
2381 print "<table class=\"history\" cellspacing=\"0\">\n";
2382 my $alternate = 1;
2383 for (my $i = $from; $i <= $to; $i++) {
2384 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2385 next;
2386 }
2387
2388 my $commit = $1;
2389 my %co = parse_commit($commit);
2390 if (!%co) {
2391 next;
2392 }
2393
2394 my $ref = format_ref_marker($refs, $commit);
2395
2396 if ($alternate) {
2397 print "<tr class=\"dark\">\n";
2398 } else {
2399 print "<tr class=\"light\">\n";
2400 }
2401 $alternate ^= 1;
2402 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2403 # shortlog uses chop_str($co{'author_name'}, 10)
2404 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2405 "<td>";
2406 # originally git_history used chop_str($co{'title'}, 50)
2407 print format_subject_html($co{'title'}, $co{'title_short'},
2408 href(action=>"commit", hash=>$commit), $ref);
2409 print "</td>\n" .
2410 "<td class=\"link\">" .
2411 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2412 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2413
2414 if ($ftype eq 'blob') {
2415 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2416 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2417 if (defined $blob_current && defined $blob_parent &&
2418 $blob_current ne $blob_parent) {
2419 print " | " .
2420 $cgi->a({-href => href(action=>"blobdiff",
2421 hash=>$blob_current, hash_parent=>$blob_parent,
2422 hash_base=>$hash_base, hash_parent_base=>$commit,
2423 file_name=>$file_name)},
2424 "diff to current");
2425 }
2426 }
2427 print "</td>\n" .
2428 "</tr>\n";
2429 }
2430 if (defined $extra) {
2431 print "<tr>\n" .
2432 "<td colspan=\"4\">$extra</td>\n" .
2433 "</tr>\n";
2434 }
2435 print "</table>\n";
2436 }
2437
2438 sub git_tags_body {
2439 # uses global variable $project
2440 my ($taglist, $from, $to, $extra) = @_;
2441 $from = 0 unless defined $from;
2442 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2443
2444 print "<table class=\"tags\" cellspacing=\"0\">\n";
2445 my $alternate = 1;
2446 for (my $i = $from; $i <= $to; $i++) {
2447 my $entry = $taglist->[$i];
2448 my %tag = %$entry;
2449 my $comment = $tag{'subject'};
2450 my $comment_short;
2451 if (defined $comment) {
2452 $comment_short = chop_str($comment, 30, 5);
2453 }
2454 if ($alternate) {
2455 print "<tr class=\"dark\">\n";
2456 } else {
2457 print "<tr class=\"light\">\n";
2458 }
2459 $alternate ^= 1;
2460 print "<td><i>$tag{'age'}</i></td>\n" .
2461 "<td>" .
2462 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2463 -class => "list name"}, esc_html($tag{'name'})) .
2464 "</td>\n" .
2465 "<td>";
2466 if (defined $comment) {
2467 print format_subject_html($comment, $comment_short,
2468 href(action=>"tag", hash=>$tag{'id'}));
2469 }
2470 print "</td>\n" .
2471 "<td class=\"selflink\">";
2472 if ($tag{'type'} eq "tag") {
2473 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2474 } else {
2475 print "&nbsp;";
2476 }
2477 print "</td>\n" .
2478 "<td class=\"link\">" . " | " .
2479 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2480 if ($tag{'reftype'} eq "commit") {
2481 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2482 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
2483 } elsif ($tag{'reftype'} eq "blob") {
2484 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2485 }
2486 print "</td>\n" .
2487 "</tr>";
2488 }
2489 if (defined $extra) {
2490 print "<tr>\n" .
2491 "<td colspan=\"5\">$extra</td>\n" .
2492 "</tr>\n";
2493 }
2494 print "</table>\n";
2495 }
2496
2497 sub git_heads_body {
2498 # uses global variable $project
2499 my ($headlist, $head, $from, $to, $extra) = @_;
2500 $from = 0 unless defined $from;
2501 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2502
2503 print "<table class=\"heads\" cellspacing=\"0\">\n";
2504 my $alternate = 1;
2505 for (my $i = $from; $i <= $to; $i++) {
2506 my $entry = $headlist->[$i];
2507 my %ref = %$entry;
2508 my $curr = $ref{'id'} eq $head;
2509 if ($alternate) {
2510 print "<tr class=\"dark\">\n";
2511 } else {
2512 print "<tr class=\"light\">\n";
2513 }
2514 $alternate ^= 1;
2515 print "<td><i>$ref{'age'}</i></td>\n" .
2516 ($curr ? "<td class=\"current_head\">" : "<td>") .
2517 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
2518 -class => "list name"},esc_html($ref{'name'})) .
2519 "</td>\n" .
2520 "<td class=\"link\">" .
2521 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
2522 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
2523 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
2524 "</td>\n" .
2525 "</tr>";
2526 }
2527 if (defined $extra) {
2528 print "<tr>\n" .
2529 "<td colspan=\"3\">$extra</td>\n" .
2530 "</tr>\n";
2531 }
2532 print "</table>\n";
2533 }
2534
2535 ## ======================================================================
2536 ## ======================================================================
2537 ## actions
2538
2539 sub git_project_list {
2540 my $order = $cgi->param('o');
2541 if (defined $order && $order !~ m/project|descr|owner|age/) {
2542 die_error(undef, "Unknown order parameter");
2543 }
2544
2545 my @list = git_get_projects_list();
2546 if (!@list) {
2547 die_error(undef, "No projects found");
2548 }
2549
2550 git_header_html();
2551 if (-f $home_text) {
2552 print "<div class=\"index_include\">\n";
2553 open (my $fd, $home_text);
2554 print <$fd>;
2555 close $fd;
2556 print "</div>\n";
2557 }
2558 git_project_list_body(\@list, $order);
2559 git_footer_html();
2560 }
2561
2562 sub git_forks {
2563 my $order = $cgi->param('o');
2564 if (defined $order && $order !~ m/project|descr|owner|age/) {
2565 die_error(undef, "Unknown order parameter");
2566 }
2567
2568 my @list = git_get_projects_list($project);
2569 if (!@list) {
2570 die_error(undef, "No forks found");
2571 }
2572
2573 git_header_html();
2574 git_print_page_nav('','');
2575 git_print_header_div('summary', "$project forks");
2576 git_project_list_body(\@list, $order);
2577 git_footer_html();
2578 }
2579
2580 sub git_project_index {
2581 my @projects = git_get_projects_list($project);
2582
2583 print $cgi->header(
2584 -type => 'text/plain',
2585 -charset => 'utf-8',
2586 -content_disposition => 'inline; filename="index.aux"');
2587
2588 foreach my $pr (@projects) {
2589 if (!exists $pr->{'owner'}) {
2590 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2591 }
2592
2593 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2594 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2595 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2596 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2597 $path =~ s/ /\+/g;
2598 $owner =~ s/ /\+/g;
2599
2600 print "$path $owner\n";
2601 }
2602 }
2603
2604 sub git_summary {
2605 my $descr = git_get_project_description($project) || "none";
2606 my $head = git_get_head_hash($project);
2607 my %co = parse_commit($head);
2608 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2609
2610 my $owner = git_get_project_owner($project);
2611
2612 my $refs = git_get_references();
2613 my @taglist = git_get_tags_list(15);
2614 my @headlist = git_get_heads_list(15);
2615 my @forklist;
2616 if (gitweb_check_feature('forks')) {
2617 @forklist = git_get_projects_list($project);
2618 }
2619
2620 git_header_html();
2621 git_print_page_nav('summary','', $head);
2622
2623 print "<div class=\"title\">&nbsp;</div>\n";
2624 print "<table cellspacing=\"0\">\n" .
2625 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2626 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2627 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2628 # use per project git URL list in $projectroot/$project/cloneurl
2629 # or make project git URL from git base URL and project name
2630 my $url_tag = "URL";
2631 my @url_list = git_get_project_url_list($project);
2632 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2633 foreach my $git_url (@url_list) {
2634 next unless $git_url;
2635 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2636 $url_tag = "";
2637 }
2638 print "</table>\n";
2639
2640 if (-s "$projectroot/$project/README.html") {
2641 if (open my $fd, "$projectroot/$project/README.html") {
2642 print "<div class=\"title\">readme</div>\n";
2643 print $_ while (<$fd>);
2644 close $fd;
2645 }
2646 }
2647
2648 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2649 git_get_head_hash($project), "--"
2650 or die_error(undef, "Open git-rev-list failed");
2651 my @revlist = map { chomp; $_ } <$fd>;
2652 close $fd;
2653 git_print_header_div('shortlog');
2654 git_shortlog_body(\@revlist, 0, 15, $refs,
2655 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2656
2657 if (@taglist) {
2658 git_print_header_div('tags');
2659 git_tags_body(\@taglist, 0, 15,
2660 $cgi->a({-href => href(action=>"tags")}, "..."));
2661 }
2662
2663 if (@headlist) {
2664 git_print_header_div('heads');
2665 git_heads_body(\@headlist, $head, 0, 15,
2666 $cgi->a({-href => href(action=>"heads")}, "..."));
2667 }
2668
2669 if (@forklist) {
2670 git_print_header_div('forks');
2671 git_project_list_body(\@forklist, undef, 0, 15,
2672 $cgi->a({-href => href(action=>"forks")}, "..."),
2673 'noheader');
2674 }
2675
2676 git_footer_html();
2677 }
2678
2679 sub git_tag {
2680 my $head = git_get_head_hash($project);
2681 git_header_html();
2682 git_print_page_nav('','', $head,undef,$head);
2683 my %tag = parse_tag($hash);
2684 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2685 print "<div class=\"title_text\">\n" .
2686 "<table cellspacing=\"0\">\n" .
2687 "<tr>\n" .
2688 "<td>object</td>\n" .
2689 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2690 $tag{'object'}) . "</td>\n" .
2691 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2692 $tag{'type'}) . "</td>\n" .
2693 "</tr>\n";
2694 if (defined($tag{'author'})) {
2695 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2696 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2697 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2698 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2699 "</td></tr>\n";
2700 }
2701 print "</table>\n\n" .
2702 "</div>\n";
2703 print "<div class=\"page_body\">";
2704 my $comment = $tag{'comment'};
2705 foreach my $line (@$comment) {
2706 print esc_html($line) . "<br/>\n";
2707 }
2708 print "</div>\n";
2709 git_footer_html();
2710 }
2711
2712 sub git_blame2 {
2713 my $fd;
2714 my $ftype;
2715
2716 my ($have_blame) = gitweb_check_feature('blame');
2717 if (!$have_blame) {
2718 die_error('403 Permission denied', "Permission denied");
2719 }
2720 die_error('404 Not Found', "File name not defined") if (!$file_name);
2721 $hash_base ||= git_get_head_hash($project);
2722 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2723 my %co = parse_commit($hash_base)
2724 or die_error(undef, "Reading commit failed");
2725 if (!defined $hash) {
2726 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2727 or die_error(undef, "Error looking up file");
2728 }
2729 $ftype = git_get_type($hash);
2730 if ($ftype !~ "blob") {
2731 die_error("400 Bad Request", "Object is not a blob");
2732 }
2733 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
2734 $file_name, $hash_base)
2735 or die_error(undef, "Open git-blame failed");
2736 git_header_html();
2737 my $formats_nav =
2738 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2739 "blob") .
2740 " | " .
2741 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2742 "history") .
2743 " | " .
2744 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2745 "HEAD");
2746 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2747 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2748 git_print_page_path($file_name, $ftype, $hash_base);
2749 my @rev_color = (qw(light2 dark2));
2750 my $num_colors = scalar(@rev_color);
2751 my $current_color = 0;
2752 my $last_rev;
2753 print <<HTML;
2754 <div class="page_body">
2755 <table class="blame">
2756 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2757 HTML
2758 my %metainfo = ();
2759 while (1) {
2760 $_ = <$fd>;
2761 last unless defined $_;
2762 my ($full_rev, $orig_lineno, $lineno, $group_size) =
2763 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
2764 if (!exists $metainfo{$full_rev}) {
2765 $metainfo{$full_rev} = {};
2766 }
2767 my $meta = $metainfo{$full_rev};
2768 while (<$fd>) {
2769 last if (s/^\t//);
2770 if (/^(\S+) (.*)$/) {
2771 $meta->{$1} = $2;
2772 }
2773 }
2774 my $data = $_;
2775 my $rev = substr($full_rev, 0, 8);
2776 my $author = $meta->{'author'};
2777 my %date = parse_date($meta->{'author-time'},
2778 $meta->{'author-tz'});
2779 my $date = $date{'iso-tz'};
2780 if ($group_size) {
2781 $current_color = ++$current_color % $num_colors;
2782 }
2783 print "<tr class=\"$rev_color[$current_color]\">\n";
2784 if ($group_size) {
2785 print "<td class=\"sha1\"";
2786 print " title=\"". esc_html($author) . ", $date\"";
2787 print " rowspan=\"$group_size\"" if ($group_size > 1);
2788 print ">";
2789 print $cgi->a({-href => href(action=>"commit",
2790 hash=>$full_rev,
2791 file_name=>$file_name)},
2792 esc_html($rev));
2793 print "</td>\n";
2794 }
2795 my $blamed = href(action => 'blame',
2796 file_name => $meta->{'filename'},
2797 hash_base => $full_rev);
2798 print "<td class=\"linenr\">";
2799 print $cgi->a({ -href => "$blamed#l$orig_lineno",
2800 -id => "l$lineno",
2801 -class => "linenr" },
2802 esc_html($lineno));
2803 print "</td>";
2804 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2805 print "</tr>\n";
2806 }
2807 print "</table>\n";
2808 print "</div>";
2809 close $fd
2810 or print "Reading blob failed\n";
2811 git_footer_html();
2812 }
2813
2814 sub git_blame {
2815 my $fd;
2816
2817 my ($have_blame) = gitweb_check_feature('blame');
2818 if (!$have_blame) {
2819 die_error('403 Permission denied', "Permission denied");
2820 }
2821 die_error('404 Not Found', "File name not defined") if (!$file_name);
2822 $hash_base ||= git_get_head_hash($project);
2823 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2824 my %co = parse_commit($hash_base)
2825 or die_error(undef, "Reading commit failed");
2826 if (!defined $hash) {
2827 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2828 or die_error(undef, "Error lookup file");
2829 }
2830 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2831 or die_error(undef, "Open git-annotate failed");
2832 git_header_html();
2833 my $formats_nav =
2834 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2835 "blob") .
2836 " | " .
2837 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2838 "history") .
2839 " | " .
2840 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2841 "HEAD");
2842 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2843 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2844 git_print_page_path($file_name, 'blob', $hash_base);
2845 print "<div class=\"page_body\">\n";
2846 print <<HTML;
2847 <table class="blame">
2848 <tr>
2849 <th>Commit</th>
2850 <th>Age</th>
2851 <th>Author</th>
2852 <th>Line</th>
2853 <th>Data</th>
2854 </tr>
2855 HTML
2856 my @line_class = (qw(light dark));
2857 my $line_class_len = scalar (@line_class);
2858 my $line_class_num = $#line_class;
2859 while (my $line = <$fd>) {
2860 my $long_rev;
2861 my $short_rev;
2862 my $author;
2863 my $time;
2864 my $lineno;
2865 my $data;
2866 my $age;
2867 my $age_str;
2868 my $age_class;
2869
2870 chomp $line;
2871 $line_class_num = ($line_class_num + 1) % $line_class_len;
2872
2873 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2874 $long_rev = $1;
2875 $author = $2;
2876 $time = $3;
2877 $lineno = $4;
2878 $data = $5;
2879 } else {
2880 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2881 next;
2882 }
2883 $short_rev = substr ($long_rev, 0, 8);
2884 $age = time () - $time;
2885 $age_str = age_string ($age);
2886 $age_str =~ s/ /&nbsp;/g;
2887 $age_class = age_class($age);
2888 $author = esc_html ($author);
2889 $author =~ s/ /&nbsp;/g;
2890
2891 $data = untabify($data);
2892 $data = esc_html ($data);
2893
2894 print <<HTML;
2895 <tr class="$line_class[$line_class_num]">
2896 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2897 <td class="$age_class">$age_str</td>
2898 <td>$author</td>
2899 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2900 <td class="pre">$data</td>
2901 </tr>
2902 HTML
2903 } # while (my $line = <$fd>)
2904 print "</table>\n\n";
2905 close $fd
2906 or print "Reading blob failed.\n";
2907 print "</div>";
2908 git_footer_html();
2909 }
2910
2911 sub git_tags {
2912 my $head = git_get_head_hash($project);
2913 git_header_html();
2914 git_print_page_nav('','', $head,undef,$head);
2915 git_print_header_div('summary', $project);
2916
2917 my @tagslist = git_get_tags_list();
2918 if (@tagslist) {
2919 git_tags_body(\@tagslist);
2920 }
2921 git_footer_html();
2922 }
2923
2924 sub git_heads {
2925 my $head = git_get_head_hash($project);
2926 git_header_html();
2927 git_print_page_nav('','', $head,undef,$head);
2928 git_print_header_div('summary', $project);
2929
2930 my @headslist = git_get_heads_list();
2931 if (@headslist) {
2932 git_heads_body(\@headslist, $head);
2933 }
2934 git_footer_html();
2935 }
2936
2937 sub git_blob_plain {
2938 my $expires;
2939
2940 if (!defined $hash) {
2941 if (defined $file_name) {
2942 my $base = $hash_base || git_get_head_hash($project);
2943 $hash = git_get_hash_by_path($base, $file_name, "blob")
2944 or die_error(undef, "Error lookup file");
2945 } else {
2946 die_error(undef, "No file name defined");
2947 }
2948 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2949 # blobs defined by non-textual hash id's can be cached
2950 $expires = "+1d";
2951 }
2952
2953 my $type = shift;
2954 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2955 or die_error(undef, "Couldn't cat $file_name, $hash");
2956
2957 $type ||= blob_mimetype($fd, $file_name);
2958
2959 # save as filename, even when no $file_name is given
2960 my $save_as = "$hash";
2961 if (defined $file_name) {
2962 $save_as = $file_name;
2963 } elsif ($type =~ m/^text\//) {
2964 $save_as .= '.txt';
2965 }
2966
2967 print $cgi->header(
2968 -type => "$type",
2969 -expires=>$expires,
2970 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2971 undef $/;
2972 binmode STDOUT, ':raw';
2973 print <$fd>;
2974 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2975 $/ = "\n";
2976 close $fd;
2977 }
2978
2979 sub git_blob {
2980 my $expires;
2981
2982 if (!defined $hash) {
2983 if (defined $file_name) {
2984 my $base = $hash_base || git_get_head_hash($project);
2985 $hash = git_get_hash_by_path($base, $file_name, "blob")
2986 or die_error(undef, "Error lookup file");
2987 } else {
2988 die_error(undef, "No file name defined");
2989 }
2990 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2991 # blobs defined by non-textual hash id's can be cached
2992 $expires = "+1d";
2993 }
2994
2995 my ($have_blame) = gitweb_check_feature('blame');
2996 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2997 or die_error(undef, "Couldn't cat $file_name, $hash");
2998 my $mimetype = blob_mimetype($fd, $file_name);
2999 if ($mimetype !~ m/^text\//) {
3000 close $fd;
3001 return git_blob_plain($mimetype);
3002 }
3003 git_header_html(undef, $expires);
3004 my $formats_nav = '';
3005 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3006 if (defined $file_name) {
3007 if ($have_blame) {
3008 $formats_nav .=
3009 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
3010 hash=>$hash, file_name=>$file_name)},
3011 "blame") .
3012 " | ";
3013 }
3014 $formats_nav .=
3015 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3016 hash=>$hash, file_name=>$file_name)},
3017 "history") .
3018 " | " .
3019 $cgi->a({-href => href(action=>"blob_plain",
3020 hash=>$hash, file_name=>$file_name)},
3021 "raw") .
3022 " | " .
3023 $cgi->a({-href => href(action=>"blob",
3024 hash_base=>"HEAD", file_name=>$file_name)},
3025 "HEAD");
3026 } else {
3027 $formats_nav .=
3028 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
3029 }
3030 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3031 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3032 } else {
3033 print "<div class=\"page_nav\">\n" .
3034 "<br/><br/></div>\n" .
3035 "<div class=\"title\">$hash</div>\n";
3036 }
3037 git_print_page_path($file_name, "blob", $hash_base);
3038 print "<div class=\"page_body\">\n";
3039 my $nr;
3040 while (my $line = <$fd>) {
3041 chomp $line;
3042 $nr++;
3043 $line = untabify($line);
3044 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
3045 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
3046 }
3047 close $fd
3048 or print "Reading blob failed.\n";
3049 print "</div>";
3050 git_footer_html();
3051 }
3052
3053 sub git_tree {
3054 my $have_snapshot = gitweb_have_snapshot();
3055
3056 if (!defined $hash_base) {
3057 $hash_base = "HEAD";
3058 }
3059 if (!defined $hash) {
3060 if (defined $file_name) {
3061 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
3062 } else {
3063 $hash = $hash_base;
3064 }
3065 }
3066 $/ = "\0";
3067 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
3068 or die_error(undef, "Open git-ls-tree failed");
3069 my @entries = map { chomp; $_ } <$fd>;
3070 close $fd or die_error(undef, "Reading tree failed");
3071 $/ = "\n";
3072
3073 my $refs = git_get_references();
3074 my $ref = format_ref_marker($refs, $hash_base);
3075 git_header_html();
3076 my $basedir = '';
3077 my ($have_blame) = gitweb_check_feature('blame');
3078 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3079 my @views_nav = ();
3080 if (defined $file_name) {
3081 push @views_nav,
3082 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3083 hash=>$hash, file_name=>$file_name)},
3084 "history"),
3085 $cgi->a({-href => href(action=>"tree",
3086 hash_base=>"HEAD", file_name=>$file_name)},
3087 "HEAD"),
3088 }
3089 if ($have_snapshot) {
3090 # FIXME: Should be available when we have no hash base as well.
3091 push @views_nav,
3092 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
3093 "snapshot");
3094 }
3095 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
3096 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
3097 } else {
3098 undef $hash_base;
3099 print "<div class=\"page_nav\">\n";
3100 print "<br/><br/></div>\n";
3101 print "<div class=\"title\">$hash</div>\n";
3102 }
3103 if (defined $file_name) {
3104 $basedir = $file_name;
3105 if ($basedir ne '' && substr($basedir, -1) ne '/') {
3106 $basedir .= '/';
3107 }
3108 }
3109 git_print_page_path($file_name, 'tree', $hash_base);
3110 print "<div class=\"page_body\">\n";
3111 print "<table cellspacing=\"0\">\n";
3112 my $alternate = 1;
3113 # '..' (top directory) link if possible
3114 if (defined $hash_base &&
3115 defined $file_name && $file_name =~ m![^/]+$!) {
3116 if ($alternate) {
3117 print "<tr class=\"dark\">\n";
3118 } else {
3119 print "<tr class=\"light\">\n";
3120 }
3121 $alternate ^= 1;
3122
3123 my $up = $file_name;
3124 $up =~ s!/?[^/]+$!!;
3125 undef $up unless $up;
3126 # based on git_print_tree_entry
3127 print '<td class="mode">' . mode_str('040000') . "</td>\n";
3128 print '<td class="list">';
3129 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
3130 file_name=>$up)},
3131 "..");
3132 print "</td>\n";
3133 print "<td class=\"link\"></td>\n";
3134
3135 print "</tr>\n";
3136 }
3137 foreach my $line (@entries) {
3138 my %t = parse_ls_tree_line($line, -z => 1);
3139
3140 if ($alternate) {
3141 print "<tr class=\"dark\">\n";
3142 } else {
3143 print "<tr class=\"light\">\n";
3144 }
3145 $alternate ^= 1;
3146
3147 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
3148
3149 print "</tr>\n";
3150 }
3151 print "</table>\n" .
3152 "</div>";
3153 git_footer_html();
3154 }
3155
3156 sub git_snapshot {
3157 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
3158 my $have_snapshot = (defined $ctype && defined $suffix);
3159 if (!$have_snapshot) {
3160 die_error('403 Permission denied', "Permission denied");
3161 }
3162
3163 if (!defined $hash) {
3164 $hash = git_get_head_hash($project);
3165 }
3166
3167 my $filename = basename($project) . "-$hash.tar.$suffix";
3168
3169 print $cgi->header(
3170 -type => 'application/x-tar',
3171 -content_encoding => $ctype,
3172 -content_disposition => 'inline; filename="' . "$filename" . '"',
3173 -status => '200 OK');
3174
3175 my $git = git_cmd_str();
3176 my $name = $project;
3177 $name =~ s/\047/\047\\\047\047/g;
3178 open my $fd, "-|",
3179 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3180 or die_error(undef, "Execute git-tar-tree failed.");
3181 binmode STDOUT, ':raw';
3182 print <$fd>;
3183 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3184 close $fd;
3185
3186 }
3187
3188 sub git_log {
3189 my $head = git_get_head_hash($project);
3190 if (!defined $hash) {
3191 $hash = $head;
3192 }
3193 if (!defined $page) {
3194 $page = 0;
3195 }
3196 my $refs = git_get_references();
3197
3198 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3199 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash, "--"
3200 or die_error(undef, "Open git-rev-list failed");
3201 my @revlist = map { chomp; $_ } <$fd>;
3202 close $fd;
3203
3204 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
3205
3206 git_header_html();
3207 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
3208
3209 if (!@revlist) {
3210 my %co = parse_commit($hash);
3211
3212 git_print_header_div('summary', $project);
3213 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
3214 }
3215 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
3216 my $commit = $revlist[$i];
3217 my $ref = format_ref_marker($refs, $commit);
3218 my %co = parse_commit($commit);
3219 next if !%co;
3220 my %ad = parse_date($co{'author_epoch'});
3221 git_print_header_div('commit',
3222 "<span class=\"age\">$co{'age_string'}</span>" .
3223 esc_html($co{'title'}) . $ref,
3224 $commit);
3225 print "<div class=\"title_text\">\n" .
3226 "<div class=\"log_link\">\n" .
3227 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
3228 " | " .
3229 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
3230 " | " .
3231 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
3232 "<br/>\n" .
3233 "</div>\n" .
3234 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
3235 "</div>\n";
3236
3237 print "<div class=\"log_body\">\n";
3238 git_print_log($co{'comment'}, -final_empty_line=> 1);
3239 print "</div>\n";
3240 }
3241 git_footer_html();
3242 }
3243
3244 sub git_commit {
3245 my %co = parse_commit($hash);
3246 if (!%co) {
3247 die_error(undef, "Unknown commit object");
3248 }
3249 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3250 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
3251
3252 my $parent = $co{'parent'};
3253 if (!defined $parent) {
3254 $parent = "--root";
3255 }
3256 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
3257 @diff_opts, $parent, $hash, "--"
3258 or die_error(undef, "Open git-diff-tree failed");
3259 my @difftree = map { chomp; $_ } <$fd>;
3260 close $fd or die_error(undef, "Reading git-diff-tree failed");
3261
3262 # non-textual hash id's can be cached
3263 my $expires;
3264 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3265 $expires = "+1d";
3266 }
3267 my $refs = git_get_references();
3268 my $ref = format_ref_marker($refs, $co{'id'});
3269
3270 my $have_snapshot = gitweb_have_snapshot();
3271
3272 my @views_nav = ();
3273 if (defined $file_name && defined $co{'parent'}) {
3274 push @views_nav,
3275 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
3276 "blame");
3277 }
3278 git_header_html(undef, $expires);
3279 git_print_page_nav('commit', '',
3280 $hash, $co{'tree'}, $hash,
3281 join (' | ', @views_nav));
3282
3283 if (defined $co{'parent'}) {
3284 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3285 } else {
3286 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3287 }
3288 print "<div class=\"title_text\">\n" .
3289 "<table cellspacing=\"0\">\n";
3290 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3291 "<tr>" .
3292 "<td></td><td> $ad{'rfc2822'}";
3293 if ($ad{'hour_local'} < 6) {
3294 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3295 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3296 } else {
3297 printf(" (%02d:%02d %s)",
3298 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3299 }
3300 print "</td>" .
3301 "</tr>\n";
3302 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3303 print "<tr><td></td><td> $cd{'rfc2822'}" .
3304 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3305 "</td></tr>\n";
3306 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3307 print "<tr>" .
3308 "<td>tree</td>" .
3309 "<td class=\"sha1\">" .
3310 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3311 class => "list"}, $co{'tree'}) .
3312 "</td>" .
3313 "<td class=\"link\">" .
3314 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3315 "tree");
3316 if ($have_snapshot) {
3317 print " | " .
3318 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3319 }
3320 print "</td>" .
3321 "</tr>\n";
3322 my $parents = $co{'parents'};
3323 foreach my $par (@$parents) {
3324 print "<tr>" .
3325 "<td>parent</td>" .
3326 "<td class=\"sha1\">" .
3327 $cgi->a({-href => href(action=>"commit", hash=>$par),
3328 class => "list"}, $par) .
3329 "</td>" .
3330 "<td class=\"link\">" .
3331 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3332 " | " .
3333 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3334 "</td>" .
3335 "</tr>\n";
3336 }
3337 print "</table>".
3338 "</div>\n";
3339
3340 print "<div class=\"page_body\">\n";
3341 git_print_log($co{'comment'});
3342 print "</div>\n";
3343
3344 git_difftree_body(\@difftree, $hash, $parent);
3345
3346 git_footer_html();
3347 }
3348
3349 sub git_blobdiff {
3350 my $format = shift || 'html';
3351
3352 my $fd;
3353 my @difftree;
3354 my %diffinfo;
3355 my $expires;
3356
3357 # preparing $fd and %diffinfo for git_patchset_body
3358 # new style URI
3359 if (defined $hash_base && defined $hash_parent_base) {
3360 if (defined $file_name) {
3361 # read raw output
3362 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3363 $hash_parent_base, $hash_base,
3364 "--", $file_name
3365 or die_error(undef, "Open git-diff-tree failed");
3366 @difftree = map { chomp; $_ } <$fd>;
3367 close $fd
3368 or die_error(undef, "Reading git-diff-tree failed");
3369 @difftree
3370 or die_error('404 Not Found', "Blob diff not found");
3371
3372 } elsif (defined $hash &&
3373 $hash =~ /[0-9a-fA-F]{40}/) {
3374 # try to find filename from $hash
3375
3376 # read filtered raw output
3377 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3378 $hash_parent_base, $hash_base, "--"
3379 or die_error(undef, "Open git-diff-tree failed");
3380 @difftree =
3381 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3382 # $hash == to_id
3383 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3384 map { chomp; $_ } <$fd>;
3385 close $fd
3386 or die_error(undef, "Reading git-diff-tree failed");
3387 @difftree
3388 or die_error('404 Not Found', "Blob diff not found");
3389
3390 } else {
3391 die_error('404 Not Found', "Missing one of the blob diff parameters");
3392 }
3393
3394 if (@difftree > 1) {
3395 die_error('404 Not Found', "Ambiguous blob diff specification");
3396 }
3397
3398 %diffinfo = parse_difftree_raw_line($difftree[0]);
3399 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3400 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3401
3402 $hash_parent ||= $diffinfo{'from_id'};
3403 $hash ||= $diffinfo{'to_id'};
3404
3405 # non-textual hash id's can be cached
3406 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3407 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3408 $expires = '+1d';
3409 }
3410
3411 # open patch output
3412 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3413 '-p', $hash_parent_base, $hash_base,
3414 "--", $file_name
3415 or die_error(undef, "Open git-diff-tree failed");
3416 }
3417
3418 # old/legacy style URI
3419 if (!%diffinfo && # if new style URI failed
3420 defined $hash && defined $hash_parent) {
3421 # fake git-diff-tree raw output
3422 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3423 $diffinfo{'from_id'} = $hash_parent;
3424 $diffinfo{'to_id'} = $hash;
3425 if (defined $file_name) {
3426 if (defined $file_parent) {
3427 $diffinfo{'status'} = '2';
3428 $diffinfo{'from_file'} = $file_parent;
3429 $diffinfo{'to_file'} = $file_name;
3430 } else { # assume not renamed
3431 $diffinfo{'status'} = '1';
3432 $diffinfo{'from_file'} = $file_name;
3433 $diffinfo{'to_file'} = $file_name;
3434 }
3435 } else { # no filename given
3436 $diffinfo{'status'} = '2';
3437 $diffinfo{'from_file'} = $hash_parent;
3438 $diffinfo{'to_file'} = $hash;
3439 }
3440
3441 # non-textual hash id's can be cached
3442 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3443 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3444 $expires = '+1d';
3445 }
3446
3447 # open patch output
3448 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts,
3449 $hash_parent, $hash, "--"
3450 or die_error(undef, "Open git-diff failed");
3451 } else {
3452 die_error('404 Not Found', "Missing one of the blob diff parameters")
3453 unless %diffinfo;
3454 }
3455
3456 # header
3457 if ($format eq 'html') {
3458 my $formats_nav =
3459 $cgi->a({-href => href(action=>"blobdiff_plain",
3460 hash=>$hash, hash_parent=>$hash_parent,
3461 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3462 file_name=>$file_name, file_parent=>$file_parent)},
3463 "raw");
3464 git_header_html(undef, $expires);
3465 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3466 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3467 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3468 } else {
3469 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3470 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3471 }
3472 if (defined $file_name) {
3473 git_print_page_path($file_name, "blob", $hash_base);
3474 } else {
3475 print "<div class=\"page_path\"></div>\n";
3476 }
3477
3478 } elsif ($format eq 'plain') {
3479 print $cgi->header(
3480 -type => 'text/plain',
3481 -charset => 'utf-8',
3482 -expires => $expires,
3483 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3484
3485 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3486
3487 } else {
3488 die_error(undef, "Unknown blobdiff format");
3489 }
3490
3491 # patch
3492 if ($format eq 'html') {
3493 print "<div class=\"page_body\">\n";
3494
3495 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3496 close $fd;
3497
3498 print "</div>\n"; # class="page_body"
3499 git_footer_html();
3500
3501 } else {
3502 while (my $line = <$fd>) {
3503 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3504 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3505
3506 print $line;
3507
3508 last if $line =~ m!^\+\+\+!;
3509 }
3510 local $/ = undef;
3511 print <$fd>;
3512 close $fd;
3513 }
3514 }
3515
3516 sub git_blobdiff_plain {
3517 git_blobdiff('plain');
3518 }
3519
3520 sub git_commitdiff {
3521 my $format = shift || 'html';
3522 my %co = parse_commit($hash);
3523 if (!%co) {
3524 die_error(undef, "Unknown commit object");
3525 }
3526
3527 # we need to prepare $formats_nav before any parameter munging
3528 my $formats_nav;
3529 if ($format eq 'html') {
3530 $formats_nav =
3531 $cgi->a({-href => href(action=>"commitdiff_plain",
3532 hash=>$hash, hash_parent=>$hash_parent)},
3533 "raw");
3534
3535 if (defined $hash_parent) {
3536 # commitdiff with two commits given
3537 my $hash_parent_short = $hash_parent;
3538 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3539 $hash_parent_short = substr($hash_parent, 0, 7);
3540 }
3541 $formats_nav .=
3542 ' (from: ' .
3543 $cgi->a({-href => href(action=>"commitdiff",
3544 hash=>$hash_parent)},
3545 esc_html($hash_parent_short)) .
3546 ')';
3547 } elsif (!$co{'parent'}) {
3548 # --root commitdiff
3549 $formats_nav .= ' (initial)';
3550 } elsif (scalar @{$co{'parents'}} == 1) {
3551 # single parent commit
3552 $formats_nav .=
3553 ' (parent: ' .
3554 $cgi->a({-href => href(action=>"commitdiff",
3555 hash=>$co{'parent'})},
3556 esc_html(substr($co{'parent'}, 0, 7))) .
3557 ')';
3558 } else {
3559 # merge commit
3560 $formats_nav .=
3561 ' (merge: ' .
3562 join(' ', map {
3563 $cgi->a({-href => href(action=>"commitdiff",
3564 hash=>$_)},
3565 esc_html(substr($_, 0, 7)));
3566 } @{$co{'parents'}} ) .
3567 ')';
3568 }
3569 }
3570
3571 if (!defined $hash_parent) {
3572 $hash_parent = $co{'parent'} || '--root';
3573 }
3574
3575 # read commitdiff
3576 my $fd;
3577 my @difftree;
3578 if ($format eq 'html') {
3579 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3580 "--no-commit-id", "--patch-with-raw", "--full-index",
3581 $hash_parent, $hash, "--"
3582 or die_error(undef, "Open git-diff-tree failed");
3583
3584 while (chomp(my $line = <$fd>)) {
3585 # empty line ends raw part of diff-tree output
3586 last unless $line;
3587 push @difftree, $line;
3588 }
3589
3590 } elsif ($format eq 'plain') {
3591 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3592 '-p', $hash_parent, $hash, "--"
3593 or die_error(undef, "Open git-diff-tree failed");
3594
3595 } else {
3596 die_error(undef, "Unknown commitdiff format");
3597 }
3598
3599 # non-textual hash id's can be cached
3600 my $expires;
3601 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3602 $expires = "+1d";
3603 }
3604
3605 # write commit message
3606 if ($format eq 'html') {
3607 my $refs = git_get_references();
3608 my $ref = format_ref_marker($refs, $co{'id'});
3609
3610 git_header_html(undef, $expires);
3611 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3612 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3613 git_print_authorship(\%co);
3614 print "<div class=\"page_body\">\n";
3615 if (@{$co{'comment'}} > 1) {
3616 print "<div class=\"log\">\n";
3617 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
3618 print "</div>\n"; # class="log"
3619 }
3620
3621 } elsif ($format eq 'plain') {
3622 my $refs = git_get_references("tags");
3623 my $tagname = git_get_rev_name_tags($hash);
3624 my $filename = basename($project) . "-$hash.patch";
3625
3626 print $cgi->header(
3627 -type => 'text/plain',
3628 -charset => 'utf-8',
3629 -expires => $expires,
3630 -content_disposition => 'inline; filename="' . "$filename" . '"');
3631 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3632 print <<TEXT;
3633 From: $co{'author'}
3634 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3635 Subject: $co{'title'}
3636 TEXT
3637 print "X-Git-Tag: $tagname\n" if $tagname;
3638 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3639
3640 foreach my $line (@{$co{'comment'}}) {
3641 print "$line\n";
3642 }
3643 print "---\n\n";
3644 }
3645
3646 # write patch
3647 if ($format eq 'html') {
3648 git_difftree_body(\@difftree, $hash, $hash_parent);
3649 print "<br/>\n";
3650
3651 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3652 close $fd;
3653 print "</div>\n"; # class="page_body"
3654 git_footer_html();
3655
3656 } elsif ($format eq 'plain') {
3657 local $/ = undef;
3658 print <$fd>;
3659 close $fd
3660 or print "Reading git-diff-tree failed\n";
3661 }
3662 }
3663
3664 sub git_commitdiff_plain {
3665 git_commitdiff('plain');
3666 }
3667
3668 sub git_history {
3669 if (!defined $hash_base) {
3670 $hash_base = git_get_head_hash($project);
3671 }
3672 if (!defined $page) {
3673 $page = 0;
3674 }
3675 my $ftype;
3676 my %co = parse_commit($hash_base);
3677 if (!%co) {
3678 die_error(undef, "Unknown commit object");
3679 }
3680
3681 my $refs = git_get_references();
3682 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3683
3684 if (!defined $hash && defined $file_name) {
3685 $hash = git_get_hash_by_path($hash_base, $file_name);
3686 }
3687 if (defined $hash) {
3688 $ftype = git_get_type($hash);
3689 }
3690
3691 open my $fd, "-|",
3692 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3693 or die_error(undef, "Open git-rev-list-failed");
3694 my @revlist = map { chomp; $_ } <$fd>;
3695 close $fd
3696 or die_error(undef, "Reading git-rev-list failed");
3697
3698 my $paging_nav = '';
3699 if ($page > 0) {
3700 $paging_nav .=
3701 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3702 file_name=>$file_name)},
3703 "first");
3704 $paging_nav .= " &sdot; " .
3705 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3706 file_name=>$file_name, page=>$page-1),
3707 -accesskey => "p", -title => "Alt-p"}, "prev");
3708 } else {
3709 $paging_nav .= "first";
3710 $paging_nav .= " &sdot; prev";
3711 }
3712 if ($#revlist >= (100 * ($page+1)-1)) {
3713 $paging_nav .= " &sdot; " .
3714 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3715 file_name=>$file_name, page=>$page+1),
3716 -accesskey => "n", -title => "Alt-n"}, "next");
3717 } else {
3718 $paging_nav .= " &sdot; next";
3719 }
3720 my $next_link = '';
3721 if ($#revlist >= (100 * ($page+1)-1)) {
3722 $next_link =
3723 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3724 file_name=>$file_name, page=>$page+1),
3725 -title => "Alt-n"}, "next");
3726 }
3727
3728 git_header_html();
3729 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3730 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3731 git_print_page_path($file_name, $ftype, $hash_base);
3732
3733 git_history_body(\@revlist, ($page * 100), $#revlist,
3734 $refs, $hash_base, $ftype, $next_link);
3735
3736 git_footer_html();
3737 }
3738
3739 sub git_search {
3740 if (!defined $searchtext) {
3741 die_error(undef, "Text field empty");
3742 }
3743 if (!defined $hash) {
3744 $hash = git_get_head_hash($project);
3745 }
3746 my %co = parse_commit($hash);
3747 if (!%co) {
3748 die_error(undef, "Unknown commit object");
3749 }
3750
3751 $searchtype ||= 'commit';
3752 if ($searchtype eq 'pickaxe') {
3753 # pickaxe may take all resources of your box and run for several minutes
3754 # with every query - so decide by yourself how public you make this feature
3755 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3756 if (!$have_pickaxe) {
3757 die_error('403 Permission denied', "Permission denied");
3758 }
3759 }
3760
3761 git_header_html();
3762 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3763 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3764
3765 print "<table cellspacing=\"0\">\n";
3766 my $alternate = 1;
3767 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
3768 $/ = "\0";
3769 open my $fd, "-|", git_cmd(), "rev-list",
3770 "--header", "--parents", $hash, "--"
3771 or next;
3772 while (my $commit_text = <$fd>) {
3773 if (!grep m/$searchtext/i, $commit_text) {
3774 next;
3775 }
3776 if ($searchtype eq 'author' && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3777 next;
3778 }
3779 if ($searchtype eq 'committer' && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3780 next;
3781 }
3782 my @commit_lines = split "\n", $commit_text;
3783 my %co = parse_commit(undef, \@commit_lines);
3784 if (!%co) {
3785 next;
3786 }
3787 if ($alternate) {
3788 print "<tr class=\"dark\">\n";
3789 } else {
3790 print "<tr class=\"light\">\n";
3791 }
3792 $alternate ^= 1;
3793 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3794 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3795 "<td>" .
3796 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3797 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3798 my $comment = $co{'comment'};
3799 foreach my $line (@$comment) {
3800 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3801 my $lead = esc_html($1) || "";
3802 $lead = chop_str($lead, 30, 10);
3803 my $match = esc_html($2) || "";
3804 my $trail = esc_html($3) || "";
3805 $trail = chop_str($trail, 30, 10);
3806 my $text = "$lead<span class=\"match\">$match</span>$trail";
3807 print chop_str($text, 80, 5) . "<br/>\n";
3808 }
3809 }
3810 print "</td>\n" .
3811 "<td class=\"link\">" .
3812 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3813 " | " .
3814 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3815 print "</td>\n" .
3816 "</tr>\n";
3817 }
3818 close $fd;
3819 }
3820
3821 if ($searchtype eq 'pickaxe') {
3822 $/ = "\n";
3823 my $git_command = git_cmd_str();
3824 open my $fd, "-|", "$git_command rev-list $hash | " .
3825 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3826 undef %co;
3827 my @files;
3828 while (my $line = <$fd>) {
3829 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3830 my %set;
3831 $set{'file'} = $6;
3832 $set{'from_id'} = $3;
3833 $set{'to_id'} = $4;
3834 $set{'id'} = $set{'to_id'};
3835 if ($set{'id'} =~ m/0{40}/) {
3836 $set{'id'} = $set{'from_id'};
3837 }
3838 if ($set{'id'} =~ m/0{40}/) {
3839 next;
3840 }
3841 push @files, \%set;
3842 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3843 if (%co) {
3844 if ($alternate) {
3845 print "<tr class=\"dark\">\n";
3846 } else {
3847 print "<tr class=\"light\">\n";
3848 }
3849 $alternate ^= 1;
3850 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3851 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3852 "<td>" .
3853 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3854 -class => "list subject"},
3855 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3856 while (my $setref = shift @files) {
3857 my %set = %$setref;
3858 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3859 hash=>$set{'id'}, file_name=>$set{'file'}),
3860 -class => "list"},
3861 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3862 "<br/>\n";
3863 }
3864 print "</td>\n" .
3865 "<td class=\"link\">" .
3866 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3867 " | " .
3868 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3869 print "</td>\n" .
3870 "</tr>\n";
3871 }
3872 %co = parse_commit($1);
3873 }
3874 }
3875 close $fd;
3876 }
3877 print "</table>\n";
3878 git_footer_html();
3879 }
3880
3881 sub git_search_help {
3882 git_header_html();
3883 git_print_page_nav('','', $hash,$hash,$hash);
3884 print <<EOT;
3885 <dl>
3886 <dt><b>commit</b></dt>
3887 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
3888 <dt><b>author</b></dt>
3889 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
3890 <dt><b>committer</b></dt>
3891 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
3892 EOT
3893 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3894 if ($have_pickaxe) {
3895 print <<EOT;
3896 <dt><b>pickaxe</b></dt>
3897 <dd>All commits that caused the string to appear or disappear from any file (changes that
3898 added, removed or "modified" the string) will be listed. This search can take a while and
3899 takes a lot of strain on the server, so please use it wisely.</dd>
3900 EOT
3901 }
3902 print "</dl>\n";
3903 git_footer_html();
3904 }
3905
3906 sub git_shortlog {
3907 my $head = git_get_head_hash($project);
3908 if (!defined $hash) {
3909 $hash = $head;
3910 }
3911 if (!defined $page) {
3912 $page = 0;
3913 }
3914 my $refs = git_get_references();
3915
3916 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3917 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash, "--"
3918 or die_error(undef, "Open git-rev-list failed");
3919 my @revlist = map { chomp; $_ } <$fd>;
3920 close $fd;
3921
3922 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3923 my $next_link = '';
3924 if ($#revlist >= (100 * ($page+1)-1)) {
3925 $next_link =
3926 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3927 -title => "Alt-n"}, "next");
3928 }
3929
3930
3931 git_header_html();
3932 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3933 git_print_header_div('summary', $project);
3934
3935 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3936
3937 git_footer_html();
3938 }
3939
3940 ## ......................................................................
3941 ## feeds (RSS, OPML)
3942
3943 sub git_rss {
3944 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3945 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150",
3946 git_get_head_hash($project), "--"
3947 or die_error(undef, "Open git-rev-list failed");
3948 my @revlist = map { chomp; $_ } <$fd>;
3949 close $fd or die_error(undef, "Reading git-rev-list failed");
3950 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3951 print <<XML;
3952 <?xml version="1.0" encoding="utf-8"?>
3953 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3954 <channel>
3955 <title>$project $my_uri $my_url</title>
3956 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3957 <description>$project log</description>
3958 <language>en</language>
3959 XML
3960
3961 for (my $i = 0; $i <= $#revlist; $i++) {
3962 my $commit = $revlist[$i];
3963 my %co = parse_commit($commit);
3964 # we read 150, we always show 30 and the ones more recent than 48 hours
3965 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3966 last;
3967 }
3968 my %cd = parse_date($co{'committer_epoch'});
3969 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3970 $co{'parent'}, $co{'id'}, "--"
3971 or next;
3972 my @difftree = map { chomp; $_ } <$fd>;
3973 close $fd
3974 or next;
3975 print "<item>\n" .
3976 "<title>" .
3977 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3978 "</title>\n" .
3979 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3980 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3981 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3982 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3983 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3984 "<content:encoded>" .
3985 "<![CDATA[\n";
3986 my $comment = $co{'comment'};
3987 foreach my $line (@$comment) {
3988 $line = to_utf8($line);
3989 print "$line<br/>\n";
3990 }
3991 print "<br/>\n";
3992 foreach my $line (@difftree) {
3993 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3994 next;
3995 }
3996 my $file = esc_html(unquote($7));
3997 $file = to_utf8($file);
3998 print "$file<br/>\n";
3999 }
4000 print "]]>\n" .
4001 "</content:encoded>\n" .
4002 "</item>\n";
4003 }
4004 print "</channel></rss>";
4005 }
4006
4007 sub git_opml {
4008 my @list = git_get_projects_list();
4009
4010 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
4011 print <<XML;
4012 <?xml version="1.0" encoding="utf-8"?>
4013 <opml version="1.0">
4014 <head>
4015 <title>$site_name OPML Export</title>
4016 </head>
4017 <body>
4018 <outline text="git RSS feeds">
4019 XML
4020
4021 foreach my $pr (@list) {
4022 my %proj = %$pr;
4023 my $head = git_get_head_hash($proj{'path'});
4024 if (!defined $head) {
4025 next;
4026 }
4027 $git_dir = "$projectroot/$proj{'path'}";
4028 my %co = parse_commit($head);
4029 if (!%co) {
4030 next;
4031 }
4032
4033 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
4034 my $rss = "$my_url?p=$proj{'path'};a=rss";
4035 my $html = "$my_url?p=$proj{'path'};a=summary";
4036 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
4037 }
4038 print <<XML;
4039 </outline>
4040 </body>
4041 </opml>
4042 XML
4043 }
This page took 3.836334 seconds and 5 git commands to generate.