]> Lady’s Gitweb - Gitweb/blob - gitweb.perl
gitweb: minimally fix "fork" support.
[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 # remove the trailing "/"
927 $dir =~ s!/+$!!;
928 my $pfxlen = length("$dir");
929
930 my ($check_forks) = gitweb_check_feature('forks');
931
932 File::Find::find({
933 follow_fast => 1, # follow symbolic links
934 dangling_symlinks => 0, # ignore dangling symlinks, silently
935 wanted => sub {
936 # skip project-list toplevel, if we get it.
937 return if (m!^[/.]$!);
938 # only directories can be git repositories
939 return unless (-d $_);
940
941 my $subdir = substr($File::Find::name, $pfxlen + 1);
942 # we check related file in $projectroot
943 if ($check_forks and $subdir =~ m#/.#) {
944 $File::Find::prune = 1;
945 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
946 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
947 $File::Find::prune = 1;
948 }
949 },
950 }, "$dir");
951
952 } elsif (-f $projects_list) {
953 # read from file(url-encoded):
954 # 'git%2Fgit.git Linus+Torvalds'
955 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
956 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
957 open my ($fd), $projects_list or return;
958 while (my $line = <$fd>) {
959 chomp $line;
960 my ($path, $owner) = split ' ', $line;
961 $path = unescape($path);
962 $owner = unescape($owner);
963 if (!defined $path) {
964 next;
965 }
966 if ($filter ne '') {
967 # looking for forks;
968 my $pfx = substr($path, 0, length($filter));
969 if ($pfx ne $filter) {
970 next;
971 }
972 my $sfx = substr($path, length($filter));
973 if ($sfx !~ /^\/.*\.git$/) {
974 next;
975 }
976 }
977 if (check_export_ok("$projectroot/$path")) {
978 my $pr = {
979 path => $path,
980 owner => to_utf8($owner),
981 };
982 push @list, $pr
983 }
984 }
985 close $fd;
986 }
987 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
988 return @list;
989 }
990
991 sub git_get_project_owner {
992 my $project = shift;
993 my $owner;
994
995 return undef unless $project;
996
997 # read from file (url-encoded):
998 # 'git%2Fgit.git Linus+Torvalds'
999 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1000 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1001 if (-f $projects_list) {
1002 open (my $fd , $projects_list);
1003 while (my $line = <$fd>) {
1004 chomp $line;
1005 my ($pr, $ow) = split ' ', $line;
1006 $pr = unescape($pr);
1007 $ow = unescape($ow);
1008 if ($pr eq $project) {
1009 $owner = to_utf8($ow);
1010 last;
1011 }
1012 }
1013 close $fd;
1014 }
1015 if (!defined $owner) {
1016 $owner = get_file_owner("$projectroot/$project");
1017 }
1018
1019 return $owner;
1020 }
1021
1022 sub git_get_last_activity {
1023 my ($path) = @_;
1024 my $fd;
1025
1026 $git_dir = "$projectroot/$path";
1027 open($fd, "-|", git_cmd(), 'for-each-ref',
1028 '--format=%(refname) %(committer)',
1029 '--sort=-committerdate',
1030 'refs/heads') or return;
1031 my $most_recent = <$fd>;
1032 close $fd or return;
1033 if ($most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1034 my $timestamp = $1;
1035 my $age = time - $timestamp;
1036 return ($age, age_string($age));
1037 }
1038 }
1039
1040 sub git_get_references {
1041 my $type = shift || "";
1042 my %refs;
1043 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1044 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1045 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1046 or return;
1047
1048 while (my $line = <$fd>) {
1049 chomp $line;
1050 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
1051 if (defined $refs{$1}) {
1052 push @{$refs{$1}}, $2;
1053 } else {
1054 $refs{$1} = [ $2 ];
1055 }
1056 }
1057 }
1058 close $fd or return;
1059 return \%refs;
1060 }
1061
1062 sub git_get_rev_name_tags {
1063 my $hash = shift || return undef;
1064
1065 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1066 or return;
1067 my $name_rev = <$fd>;
1068 close $fd;
1069
1070 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1071 return $1;
1072 } else {
1073 # catches also '$hash undefined' output
1074 return undef;
1075 }
1076 }
1077
1078 ## ----------------------------------------------------------------------
1079 ## parse to hash functions
1080
1081 sub parse_date {
1082 my $epoch = shift;
1083 my $tz = shift || "-0000";
1084
1085 my %date;
1086 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1087 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1088 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1089 $date{'hour'} = $hour;
1090 $date{'minute'} = $min;
1091 $date{'mday'} = $mday;
1092 $date{'day'} = $days[$wday];
1093 $date{'month'} = $months[$mon];
1094 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1095 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1096 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1097 $mday, $months[$mon], $hour ,$min;
1098
1099 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1100 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1101 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1102 $date{'hour_local'} = $hour;
1103 $date{'minute_local'} = $min;
1104 $date{'tz_local'} = $tz;
1105 $date{'iso-tz'} = sprintf ("%04d-%02d-%02d %02d:%02d:%02d %s",
1106 1900+$year, $mon+1, $mday,
1107 $hour, $min, $sec, $tz);
1108 return %date;
1109 }
1110
1111 sub parse_tag {
1112 my $tag_id = shift;
1113 my %tag;
1114 my @comment;
1115
1116 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1117 $tag{'id'} = $tag_id;
1118 while (my $line = <$fd>) {
1119 chomp $line;
1120 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1121 $tag{'object'} = $1;
1122 } elsif ($line =~ m/^type (.+)$/) {
1123 $tag{'type'} = $1;
1124 } elsif ($line =~ m/^tag (.+)$/) {
1125 $tag{'name'} = $1;
1126 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1127 $tag{'author'} = $1;
1128 $tag{'epoch'} = $2;
1129 $tag{'tz'} = $3;
1130 } elsif ($line =~ m/--BEGIN/) {
1131 push @comment, $line;
1132 last;
1133 } elsif ($line eq "") {
1134 last;
1135 }
1136 }
1137 push @comment, <$fd>;
1138 $tag{'comment'} = \@comment;
1139 close $fd or return;
1140 if (!defined $tag{'name'}) {
1141 return
1142 };
1143 return %tag
1144 }
1145
1146 sub parse_commit {
1147 my $commit_id = shift;
1148 my $commit_text = shift;
1149
1150 my @commit_lines;
1151 my %co;
1152
1153 if (defined $commit_text) {
1154 @commit_lines = @$commit_text;
1155 } else {
1156 local $/ = "\0";
1157 open my $fd, "-|", git_cmd(), "rev-list",
1158 "--header", "--parents", "--max-count=1",
1159 $commit_id, "--"
1160 or return;
1161 @commit_lines = split '\n', <$fd>;
1162 close $fd or return;
1163 pop @commit_lines;
1164 }
1165 my $header = shift @commit_lines;
1166 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1167 return;
1168 }
1169 ($co{'id'}, my @parents) = split ' ', $header;
1170 $co{'parents'} = \@parents;
1171 $co{'parent'} = $parents[0];
1172 while (my $line = shift @commit_lines) {
1173 last if $line eq "\n";
1174 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1175 $co{'tree'} = $1;
1176 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1177 $co{'author'} = $1;
1178 $co{'author_epoch'} = $2;
1179 $co{'author_tz'} = $3;
1180 if ($co{'author'} =~ m/^([^<]+) </) {
1181 $co{'author_name'} = $1;
1182 } else {
1183 $co{'author_name'} = $co{'author'};
1184 }
1185 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1186 $co{'committer'} = $1;
1187 $co{'committer_epoch'} = $2;
1188 $co{'committer_tz'} = $3;
1189 $co{'committer_name'} = $co{'committer'};
1190 $co{'committer_name'} =~ s/ <.*//;
1191 }
1192 }
1193 if (!defined $co{'tree'}) {
1194 return;
1195 };
1196
1197 foreach my $title (@commit_lines) {
1198 $title =~ s/^ //;
1199 if ($title ne "") {
1200 $co{'title'} = chop_str($title, 80, 5);
1201 # remove leading stuff of merges to make the interesting part visible
1202 if (length($title) > 50) {
1203 $title =~ s/^Automatic //;
1204 $title =~ s/^merge (of|with) /Merge ... /i;
1205 if (length($title) > 50) {
1206 $title =~ s/(http|rsync):\/\///;
1207 }
1208 if (length($title) > 50) {
1209 $title =~ s/(master|www|rsync)\.//;
1210 }
1211 if (length($title) > 50) {
1212 $title =~ s/kernel.org:?//;
1213 }
1214 if (length($title) > 50) {
1215 $title =~ s/\/pub\/scm//;
1216 }
1217 }
1218 $co{'title_short'} = chop_str($title, 50, 5);
1219 last;
1220 }
1221 }
1222 if ($co{'title'} eq "") {
1223 $co{'title'} = $co{'title_short'} = '(no commit message)';
1224 }
1225 # remove added spaces
1226 foreach my $line (@commit_lines) {
1227 $line =~ s/^ //;
1228 }
1229 $co{'comment'} = \@commit_lines;
1230
1231 my $age = time - $co{'committer_epoch'};
1232 $co{'age'} = $age;
1233 $co{'age_string'} = age_string($age);
1234 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1235 if ($age > 60*60*24*7*2) {
1236 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1237 $co{'age_string_age'} = $co{'age_string'};
1238 } else {
1239 $co{'age_string_date'} = $co{'age_string'};
1240 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1241 }
1242 return %co;
1243 }
1244
1245 # parse ref from ref_file, given by ref_id, with given type
1246 sub parse_ref {
1247 my $ref_file = shift;
1248 my $ref_id = shift;
1249 my $type = shift || git_get_type($ref_id);
1250 my %ref_item;
1251
1252 $ref_item{'type'} = $type;
1253 $ref_item{'id'} = $ref_id;
1254 $ref_item{'epoch'} = 0;
1255 $ref_item{'age'} = "unknown";
1256 if ($type eq "tag") {
1257 my %tag = parse_tag($ref_id);
1258 $ref_item{'comment'} = $tag{'comment'};
1259 if ($tag{'type'} eq "commit") {
1260 my %co = parse_commit($tag{'object'});
1261 $ref_item{'epoch'} = $co{'committer_epoch'};
1262 $ref_item{'age'} = $co{'age_string'};
1263 } elsif (defined($tag{'epoch'})) {
1264 my $age = time - $tag{'epoch'};
1265 $ref_item{'epoch'} = $tag{'epoch'};
1266 $ref_item{'age'} = age_string($age);
1267 }
1268 $ref_item{'reftype'} = $tag{'type'};
1269 $ref_item{'name'} = $tag{'name'};
1270 $ref_item{'refid'} = $tag{'object'};
1271 } elsif ($type eq "commit"){
1272 my %co = parse_commit($ref_id);
1273 $ref_item{'reftype'} = "commit";
1274 $ref_item{'name'} = $ref_file;
1275 $ref_item{'title'} = $co{'title'};
1276 $ref_item{'refid'} = $ref_id;
1277 $ref_item{'epoch'} = $co{'committer_epoch'};
1278 $ref_item{'age'} = $co{'age_string'};
1279 } else {
1280 $ref_item{'reftype'} = $type;
1281 $ref_item{'name'} = $ref_file;
1282 $ref_item{'refid'} = $ref_id;
1283 }
1284
1285 return %ref_item;
1286 }
1287
1288 # parse line of git-diff-tree "raw" output
1289 sub parse_difftree_raw_line {
1290 my $line = shift;
1291 my %res;
1292
1293 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1294 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1295 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1296 $res{'from_mode'} = $1;
1297 $res{'to_mode'} = $2;
1298 $res{'from_id'} = $3;
1299 $res{'to_id'} = $4;
1300 $res{'status'} = $5;
1301 $res{'similarity'} = $6;
1302 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1303 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1304 } else {
1305 $res{'file'} = unquote($7);
1306 }
1307 }
1308 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1309 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1310 $res{'commit'} = $1;
1311 }
1312
1313 return wantarray ? %res : \%res;
1314 }
1315
1316 # parse line of git-ls-tree output
1317 sub parse_ls_tree_line ($;%) {
1318 my $line = shift;
1319 my %opts = @_;
1320 my %res;
1321
1322 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1323 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
1324
1325 $res{'mode'} = $1;
1326 $res{'type'} = $2;
1327 $res{'hash'} = $3;
1328 if ($opts{'-z'}) {
1329 $res{'name'} = $4;
1330 } else {
1331 $res{'name'} = unquote($4);
1332 }
1333
1334 return wantarray ? %res : \%res;
1335 }
1336
1337 ## ......................................................................
1338 ## parse to array of hashes functions
1339
1340 sub git_get_heads_list {
1341 my $limit = shift;
1342 my @headslist;
1343
1344 open my $fd, '-|', git_cmd(), 'for-each-ref',
1345 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
1346 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
1347 'refs/heads'
1348 or return;
1349 while (my $line = <$fd>) {
1350 my %ref_item;
1351
1352 chomp $line;
1353 my ($refinfo, $committerinfo) = split(/\0/, $line);
1354 my ($hash, $name, $title) = split(' ', $refinfo, 3);
1355 my ($committer, $epoch, $tz) =
1356 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
1357 $name =~ s!^refs/heads/!!;
1358
1359 $ref_item{'name'} = $name;
1360 $ref_item{'id'} = $hash;
1361 $ref_item{'title'} = $title || '(no commit message)';
1362 $ref_item{'epoch'} = $epoch;
1363 if ($epoch) {
1364 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1365 } else {
1366 $ref_item{'age'} = "unknown";
1367 }
1368
1369 push @headslist, \%ref_item;
1370 }
1371 close $fd;
1372
1373 return wantarray ? @headslist : \@headslist;
1374 }
1375
1376 sub git_get_tags_list {
1377 my $limit = shift;
1378 my @tagslist;
1379
1380 open my $fd, '-|', git_cmd(), 'for-each-ref',
1381 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
1382 '--format=%(objectname) %(objecttype) %(refname) '.
1383 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
1384 'refs/tags'
1385 or return;
1386 while (my $line = <$fd>) {
1387 my %ref_item;
1388
1389 chomp $line;
1390 my ($refinfo, $creatorinfo) = split(/\0/, $line);
1391 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
1392 my ($creator, $epoch, $tz) =
1393 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
1394 $name =~ s!^refs/tags/!!;
1395
1396 $ref_item{'type'} = $type;
1397 $ref_item{'id'} = $id;
1398 $ref_item{'name'} = $name;
1399 if ($type eq "tag") {
1400 $ref_item{'subject'} = $title;
1401 $ref_item{'reftype'} = $reftype;
1402 $ref_item{'refid'} = $refid;
1403 } else {
1404 $ref_item{'reftype'} = $type;
1405 $ref_item{'refid'} = $id;
1406 }
1407
1408 if ($type eq "tag" || $type eq "commit") {
1409 $ref_item{'epoch'} = $epoch;
1410 if ($epoch) {
1411 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1412 } else {
1413 $ref_item{'age'} = "unknown";
1414 }
1415 }
1416
1417 push @tagslist, \%ref_item;
1418 }
1419 close $fd;
1420
1421 return wantarray ? @tagslist : \@tagslist;
1422 }
1423
1424 ## ----------------------------------------------------------------------
1425 ## filesystem-related functions
1426
1427 sub get_file_owner {
1428 my $path = shift;
1429
1430 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1431 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1432 if (!defined $gcos) {
1433 return undef;
1434 }
1435 my $owner = $gcos;
1436 $owner =~ s/[,;].*$//;
1437 return to_utf8($owner);
1438 }
1439
1440 ## ......................................................................
1441 ## mimetype related functions
1442
1443 sub mimetype_guess_file {
1444 my $filename = shift;
1445 my $mimemap = shift;
1446 -r $mimemap or return undef;
1447
1448 my %mimemap;
1449 open(MIME, $mimemap) or return undef;
1450 while (<MIME>) {
1451 next if m/^#/; # skip comments
1452 my ($mime, $exts) = split(/\t+/);
1453 if (defined $exts) {
1454 my @exts = split(/\s+/, $exts);
1455 foreach my $ext (@exts) {
1456 $mimemap{$ext} = $mime;
1457 }
1458 }
1459 }
1460 close(MIME);
1461
1462 $filename =~ /\.([^.]*)$/;
1463 return $mimemap{$1};
1464 }
1465
1466 sub mimetype_guess {
1467 my $filename = shift;
1468 my $mime;
1469 $filename =~ /\./ or return undef;
1470
1471 if ($mimetypes_file) {
1472 my $file = $mimetypes_file;
1473 if ($file !~ m!^/!) { # if it is relative path
1474 # it is relative to project
1475 $file = "$projectroot/$project/$file";
1476 }
1477 $mime = mimetype_guess_file($filename, $file);
1478 }
1479 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1480 return $mime;
1481 }
1482
1483 sub blob_mimetype {
1484 my $fd = shift;
1485 my $filename = shift;
1486
1487 if ($filename) {
1488 my $mime = mimetype_guess($filename);
1489 $mime and return $mime;
1490 }
1491
1492 # just in case
1493 return $default_blob_plain_mimetype unless $fd;
1494
1495 if (-T $fd) {
1496 return 'text/plain' .
1497 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1498 } elsif (! $filename) {
1499 return 'application/octet-stream';
1500 } elsif ($filename =~ m/\.png$/i) {
1501 return 'image/png';
1502 } elsif ($filename =~ m/\.gif$/i) {
1503 return 'image/gif';
1504 } elsif ($filename =~ m/\.jpe?g$/i) {
1505 return 'image/jpeg';
1506 } else {
1507 return 'application/octet-stream';
1508 }
1509 }
1510
1511 ## ======================================================================
1512 ## functions printing HTML: header, footer, error page
1513
1514 sub git_header_html {
1515 my $status = shift || "200 OK";
1516 my $expires = shift;
1517
1518 my $title = "$site_name";
1519 if (defined $project) {
1520 $title .= " - $project";
1521 if (defined $action) {
1522 $title .= "/$action";
1523 if (defined $file_name) {
1524 $title .= " - " . esc_html($file_name);
1525 if ($action eq "tree" && $file_name !~ m|/$|) {
1526 $title .= "/";
1527 }
1528 }
1529 }
1530 }
1531 my $content_type;
1532 # require explicit support from the UA if we are to send the page as
1533 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1534 # we have to do this because MSIE sometimes globs '*/*', pretending to
1535 # support xhtml+xml but choking when it gets what it asked for.
1536 if (defined $cgi->http('HTTP_ACCEPT') &&
1537 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1538 $cgi->Accept('application/xhtml+xml') != 0) {
1539 $content_type = 'application/xhtml+xml';
1540 } else {
1541 $content_type = 'text/html';
1542 }
1543 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1544 -status=> $status, -expires => $expires);
1545 print <<EOF;
1546 <?xml version="1.0" encoding="utf-8"?>
1547 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1548 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1549 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1550 <!-- git core binaries version $git_version -->
1551 <head>
1552 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1553 <meta name="generator" content="gitweb/$version git/$git_version"/>
1554 <meta name="robots" content="index, nofollow"/>
1555 <title>$title</title>
1556 EOF
1557 # print out each stylesheet that exist
1558 if (defined $stylesheet) {
1559 #provides backwards capability for those people who define style sheet in a config file
1560 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1561 } else {
1562 foreach my $stylesheet (@stylesheets) {
1563 next unless $stylesheet;
1564 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1565 }
1566 }
1567 if (defined $project) {
1568 printf('<link rel="alternate" title="%s log" '.
1569 'href="%s" type="application/rss+xml"/>'."\n",
1570 esc_param($project), href(action=>"rss"));
1571 } else {
1572 printf('<link rel="alternate" title="%s projects list" '.
1573 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1574 $site_name, href(project=>undef, action=>"project_index"));
1575 printf('<link rel="alternate" title="%s projects logs" '.
1576 'href="%s" type="text/x-opml"/>'."\n",
1577 $site_name, href(project=>undef, action=>"opml"));
1578 }
1579 if (defined $favicon) {
1580 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1581 }
1582
1583 print "</head>\n" .
1584 "<body>\n";
1585
1586 if (-f $site_header) {
1587 open (my $fd, $site_header);
1588 print <$fd>;
1589 close $fd;
1590 }
1591
1592 print "<div class=\"page_header\">\n" .
1593 $cgi->a({-href => esc_url($logo_url),
1594 -title => $logo_label},
1595 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1596 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1597 if (defined $project) {
1598 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1599 if (defined $action) {
1600 print " / $action";
1601 }
1602 print "\n";
1603 if (!defined $searchtext) {
1604 $searchtext = "";
1605 }
1606 my $search_hash;
1607 if (defined $hash_base) {
1608 $search_hash = $hash_base;
1609 } elsif (defined $hash) {
1610 $search_hash = $hash;
1611 } else {
1612 $search_hash = "HEAD";
1613 }
1614 $cgi->param("a", "search");
1615 $cgi->param("h", $search_hash);
1616 $cgi->param("p", $project);
1617 print $cgi->startform(-method => "get", -action => $my_uri) .
1618 "<div class=\"search\">\n" .
1619 $cgi->hidden(-name => "p") . "\n" .
1620 $cgi->hidden(-name => "a") . "\n" .
1621 $cgi->hidden(-name => "h") . "\n" .
1622 $cgi->popup_menu(-name => 'st', -default => 'commit',
1623 -values => ['commit', 'author', 'committer', 'pickaxe']) .
1624 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
1625 " search:\n",
1626 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1627 "</div>" .
1628 $cgi->end_form() . "\n";
1629 }
1630 print "</div>\n";
1631 }
1632
1633 sub git_footer_html {
1634 print "<div class=\"page_footer\">\n";
1635 if (defined $project) {
1636 my $descr = git_get_project_description($project);
1637 if (defined $descr) {
1638 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1639 }
1640 print $cgi->a({-href => href(action=>"rss"),
1641 -class => "rss_logo"}, "RSS") . "\n";
1642 } else {
1643 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1644 -class => "rss_logo"}, "OPML") . " ";
1645 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1646 -class => "rss_logo"}, "TXT") . "\n";
1647 }
1648 print "</div>\n" ;
1649
1650 if (-f $site_footer) {
1651 open (my $fd, $site_footer);
1652 print <$fd>;
1653 close $fd;
1654 }
1655
1656 print "</body>\n" .
1657 "</html>";
1658 }
1659
1660 sub die_error {
1661 my $status = shift || "403 Forbidden";
1662 my $error = shift || "Malformed query, file missing or permission denied";
1663
1664 git_header_html($status);
1665 print <<EOF;
1666 <div class="page_body">
1667 <br /><br />
1668 $status - $error
1669 <br />
1670 </div>
1671 EOF
1672 git_footer_html();
1673 exit;
1674 }
1675
1676 ## ----------------------------------------------------------------------
1677 ## functions printing or outputting HTML: navigation
1678
1679 sub git_print_page_nav {
1680 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1681 $extra = '' if !defined $extra; # pager or formats
1682
1683 my @navs = qw(summary shortlog log commit commitdiff tree);
1684 if ($suppress) {
1685 @navs = grep { $_ ne $suppress } @navs;
1686 }
1687
1688 my %arg = map { $_ => {action=>$_} } @navs;
1689 if (defined $head) {
1690 for (qw(commit commitdiff)) {
1691 $arg{$_}{hash} = $head;
1692 }
1693 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1694 for (qw(shortlog log)) {
1695 $arg{$_}{hash} = $head;
1696 }
1697 }
1698 }
1699 $arg{tree}{hash} = $treehead if defined $treehead;
1700 $arg{tree}{hash_base} = $treebase if defined $treebase;
1701
1702 print "<div class=\"page_nav\">\n" .
1703 (join " | ",
1704 map { $_ eq $current ?
1705 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1706 } @navs);
1707 print "<br/>\n$extra<br/>\n" .
1708 "</div>\n";
1709 }
1710
1711 sub format_paging_nav {
1712 my ($action, $hash, $head, $page, $nrevs) = @_;
1713 my $paging_nav;
1714
1715
1716 if ($hash ne $head || $page) {
1717 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1718 } else {
1719 $paging_nav .= "HEAD";
1720 }
1721
1722 if ($page > 0) {
1723 $paging_nav .= " &sdot; " .
1724 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1725 -accesskey => "p", -title => "Alt-p"}, "prev");
1726 } else {
1727 $paging_nav .= " &sdot; prev";
1728 }
1729
1730 if ($nrevs >= (100 * ($page+1)-1)) {
1731 $paging_nav .= " &sdot; " .
1732 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1733 -accesskey => "n", -title => "Alt-n"}, "next");
1734 } else {
1735 $paging_nav .= " &sdot; next";
1736 }
1737
1738 return $paging_nav;
1739 }
1740
1741 ## ......................................................................
1742 ## functions printing or outputting HTML: div
1743
1744 sub git_print_header_div {
1745 my ($action, $title, $hash, $hash_base) = @_;
1746 my %args = ();
1747
1748 $args{action} = $action;
1749 $args{hash} = $hash if $hash;
1750 $args{hash_base} = $hash_base if $hash_base;
1751
1752 print "<div class=\"header\">\n" .
1753 $cgi->a({-href => href(%args), -class => "title"},
1754 $title ? $title : $action) .
1755 "\n</div>\n";
1756 }
1757
1758 #sub git_print_authorship (\%) {
1759 sub git_print_authorship {
1760 my $co = shift;
1761
1762 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1763 print "<div class=\"author_date\">" .
1764 esc_html($co->{'author_name'}) .
1765 " [$ad{'rfc2822'}";
1766 if ($ad{'hour_local'} < 6) {
1767 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1768 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1769 } else {
1770 printf(" (%02d:%02d %s)",
1771 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1772 }
1773 print "]</div>\n";
1774 }
1775
1776 sub git_print_page_path {
1777 my $name = shift;
1778 my $type = shift;
1779 my $hb = shift;
1780
1781
1782 print "<div class=\"page_path\">";
1783 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
1784 -title => 'tree root'}, "[$project]");
1785 print " / ";
1786 if (defined $name) {
1787 my @dirname = split '/', $name;
1788 my $basename = pop @dirname;
1789 my $fullname = '';
1790
1791 foreach my $dir (@dirname) {
1792 $fullname .= ($fullname ? '/' : '') . $dir;
1793 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1794 hash_base=>$hb),
1795 -title => $fullname}, esc_html($dir));
1796 print " / ";
1797 }
1798 if (defined $type && $type eq 'blob') {
1799 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1800 hash_base=>$hb),
1801 -title => $name}, esc_html($basename));
1802 } elsif (defined $type && $type eq 'tree') {
1803 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1804 hash_base=>$hb),
1805 -title => $name}, esc_html($basename));
1806 print " / ";
1807 } else {
1808 print esc_html($basename);
1809 }
1810 }
1811 print "<br/></div>\n";
1812 }
1813
1814 # sub git_print_log (\@;%) {
1815 sub git_print_log ($;%) {
1816 my $log = shift;
1817 my %opts = @_;
1818
1819 if ($opts{'-remove_title'}) {
1820 # remove title, i.e. first line of log
1821 shift @$log;
1822 }
1823 # remove leading empty lines
1824 while (defined $log->[0] && $log->[0] eq "") {
1825 shift @$log;
1826 }
1827
1828 # print log
1829 my $signoff = 0;
1830 my $empty = 0;
1831 foreach my $line (@$log) {
1832 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1833 $signoff = 1;
1834 $empty = 0;
1835 if (! $opts{'-remove_signoff'}) {
1836 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1837 next;
1838 } else {
1839 # remove signoff lines
1840 next;
1841 }
1842 } else {
1843 $signoff = 0;
1844 }
1845
1846 # print only one empty line
1847 # do not print empty line after signoff
1848 if ($line eq "") {
1849 next if ($empty || $signoff);
1850 $empty = 1;
1851 } else {
1852 $empty = 0;
1853 }
1854
1855 print format_log_line_html($line) . "<br/>\n";
1856 }
1857
1858 if ($opts{'-final_empty_line'}) {
1859 # end with single empty line
1860 print "<br/>\n" unless $empty;
1861 }
1862 }
1863
1864 # print tree entry (row of git_tree), but without encompassing <tr> element
1865 sub git_print_tree_entry {
1866 my ($t, $basedir, $hash_base, $have_blame) = @_;
1867
1868 my %base_key = ();
1869 $base_key{hash_base} = $hash_base if defined $hash_base;
1870
1871 # The format of a table row is: mode list link. Where mode is
1872 # the mode of the entry, list is the name of the entry, an href,
1873 # and link is the action links of the entry.
1874
1875 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1876 if ($t->{'type'} eq "blob") {
1877 print "<td class=\"list\">" .
1878 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1879 file_name=>"$basedir$t->{'name'}", %base_key),
1880 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1881 print "<td class=\"link\">";
1882 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1883 file_name=>"$basedir$t->{'name'}", %base_key)},
1884 "blob");
1885 if ($have_blame) {
1886 print " | " .
1887 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1888 file_name=>"$basedir$t->{'name'}", %base_key)},
1889 "blame");
1890 }
1891 if (defined $hash_base) {
1892 print " | " .
1893 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1894 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1895 "history");
1896 }
1897 print " | " .
1898 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
1899 file_name=>"$basedir$t->{'name'}")},
1900 "raw");
1901 print "</td>\n";
1902
1903 } elsif ($t->{'type'} eq "tree") {
1904 print "<td class=\"list\">";
1905 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1906 file_name=>"$basedir$t->{'name'}", %base_key)},
1907 esc_html($t->{'name'}));
1908 print "</td>\n";
1909 print "<td class=\"link\">";
1910 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1911 file_name=>"$basedir$t->{'name'}", %base_key)},
1912 "tree");
1913 if (defined $hash_base) {
1914 print " | " .
1915 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1916 file_name=>"$basedir$t->{'name'}")},
1917 "history");
1918 }
1919 print "</td>\n";
1920 }
1921 }
1922
1923 ## ......................................................................
1924 ## functions printing large fragments of HTML
1925
1926 sub git_difftree_body {
1927 my ($difftree, $hash, $parent) = @_;
1928
1929 print "<div class=\"list_head\">\n";
1930 if ($#{$difftree} > 10) {
1931 print(($#{$difftree} + 1) . " files changed:\n");
1932 }
1933 print "</div>\n";
1934
1935 print "<table class=\"diff_tree\">\n";
1936 my $alternate = 1;
1937 my $patchno = 0;
1938 foreach my $line (@{$difftree}) {
1939 my %diff = parse_difftree_raw_line($line);
1940
1941 if ($alternate) {
1942 print "<tr class=\"dark\">\n";
1943 } else {
1944 print "<tr class=\"light\">\n";
1945 }
1946 $alternate ^= 1;
1947
1948 my ($to_mode_oct, $to_mode_str, $to_file_type);
1949 my ($from_mode_oct, $from_mode_str, $from_file_type);
1950 if ($diff{'to_mode'} ne ('0' x 6)) {
1951 $to_mode_oct = oct $diff{'to_mode'};
1952 if (S_ISREG($to_mode_oct)) { # only for regular file
1953 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1954 }
1955 $to_file_type = file_type($diff{'to_mode'});
1956 }
1957 if ($diff{'from_mode'} ne ('0' x 6)) {
1958 $from_mode_oct = oct $diff{'from_mode'};
1959 if (S_ISREG($to_mode_oct)) { # only for regular file
1960 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1961 }
1962 $from_file_type = file_type($diff{'from_mode'});
1963 }
1964
1965 if ($diff{'status'} eq "A") { # created
1966 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1967 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1968 $mode_chng .= "]</span>";
1969 print "<td>";
1970 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1971 hash_base=>$hash, file_name=>$diff{'file'}),
1972 -class => "list"}, esc_html($diff{'file'}));
1973 print "</td>\n";
1974 print "<td>$mode_chng</td>\n";
1975 print "<td class=\"link\">";
1976 if ($action eq 'commitdiff') {
1977 # link to patch
1978 $patchno++;
1979 print $cgi->a({-href => "#patch$patchno"}, "patch");
1980 }
1981 print "</td>\n";
1982
1983 } elsif ($diff{'status'} eq "D") { # deleted
1984 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1985 print "<td>";
1986 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1987 hash_base=>$parent, file_name=>$diff{'file'}),
1988 -class => "list"}, esc_html($diff{'file'}));
1989 print "</td>\n";
1990 print "<td>$mode_chng</td>\n";
1991 print "<td class=\"link\">";
1992 if ($action eq 'commitdiff') {
1993 # link to patch
1994 $patchno++;
1995 print $cgi->a({-href => "#patch$patchno"}, "patch");
1996 print " | ";
1997 }
1998 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1999 hash_base=>$parent, file_name=>$diff{'file'})},
2000 "blob") . " | ";
2001 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2002 file_name=>$diff{'file'})},
2003 "blame") . " | ";
2004 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2005 file_name=>$diff{'file'})},
2006 "history");
2007 print "</td>\n";
2008
2009 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
2010 my $mode_chnge = "";
2011 if ($diff{'from_mode'} != $diff{'to_mode'}) {
2012 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
2013 if ($from_file_type != $to_file_type) {
2014 $mode_chnge .= " from $from_file_type to $to_file_type";
2015 }
2016 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2017 if ($from_mode_str && $to_mode_str) {
2018 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2019 } elsif ($to_mode_str) {
2020 $mode_chnge .= " mode: $to_mode_str";
2021 }
2022 }
2023 $mode_chnge .= "]</span>\n";
2024 }
2025 print "<td>";
2026 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2027 hash_base=>$hash, file_name=>$diff{'file'}),
2028 -class => "list"}, esc_html($diff{'file'}));
2029 print "</td>\n";
2030 print "<td>$mode_chnge</td>\n";
2031 print "<td class=\"link\">";
2032 if ($action eq 'commitdiff') {
2033 # link to patch
2034 $patchno++;
2035 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2036 " | ";
2037 } elsif ($diff{'to_id'} ne $diff{'from_id'}) {
2038 # "commit" view and modified file (not onlu mode changed)
2039 print $cgi->a({-href => href(action=>"blobdiff",
2040 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
2041 hash_base=>$hash, hash_parent_base=>$parent,
2042 file_name=>$diff{'file'})},
2043 "diff") .
2044 " | ";
2045 }
2046 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2047 hash_base=>$hash, file_name=>$diff{'file'})},
2048 "blob") . " | ";
2049 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2050 file_name=>$diff{'file'})},
2051 "blame") . " | ";
2052 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2053 file_name=>$diff{'file'})},
2054 "history");
2055 print "</td>\n";
2056
2057 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
2058 my %status_name = ('R' => 'moved', 'C' => 'copied');
2059 my $nstatus = $status_name{$diff{'status'}};
2060 my $mode_chng = "";
2061 if ($diff{'from_mode'} != $diff{'to_mode'}) {
2062 # mode also for directories, so we cannot use $to_mode_str
2063 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
2064 }
2065 print "<td>" .
2066 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2067 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
2068 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
2069 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
2070 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2071 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
2072 -class => "list"}, esc_html($diff{'from_file'})) .
2073 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
2074 "<td class=\"link\">";
2075 if ($action eq 'commitdiff') {
2076 # link to patch
2077 $patchno++;
2078 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2079 " | ";
2080 } elsif ($diff{'to_id'} ne $diff{'from_id'}) {
2081 # "commit" view and modified file (not only pure rename or copy)
2082 print $cgi->a({-href => href(action=>"blobdiff",
2083 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
2084 hash_base=>$hash, hash_parent_base=>$parent,
2085 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
2086 "diff") .
2087 " | ";
2088 }
2089 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
2090 hash_base=>$parent, file_name=>$diff{'from_file'})},
2091 "blob") . " | ";
2092 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2093 file_name=>$diff{'from_file'})},
2094 "blame") . " | ";
2095 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2096 file_name=>$diff{'from_file'})},
2097 "history");
2098 print "</td>\n";
2099
2100 } # we should not encounter Unmerged (U) or Unknown (X) status
2101 print "</tr>\n";
2102 }
2103 print "</table>\n";
2104 }
2105
2106 sub git_patchset_body {
2107 my ($fd, $difftree, $hash, $hash_parent) = @_;
2108
2109 my $patch_idx = 0;
2110 my $in_header = 0;
2111 my $patch_found = 0;
2112 my $diffinfo;
2113
2114 print "<div class=\"patchset\">\n";
2115
2116 LINE:
2117 while (my $patch_line = <$fd>) {
2118 chomp $patch_line;
2119
2120 if ($patch_line =~ m/^diff /) { # "git diff" header
2121 # beginning of patch (in patchset)
2122 if ($patch_found) {
2123 # close previous patch
2124 print "</div>\n"; # class="patch"
2125 } else {
2126 # first patch in patchset
2127 $patch_found = 1;
2128 }
2129 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2130
2131 if (ref($difftree->[$patch_idx]) eq "HASH") {
2132 $diffinfo = $difftree->[$patch_idx];
2133 } else {
2134 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2135 }
2136 $patch_idx++;
2137
2138 if ($diffinfo->{'status'} eq "A") { # added
2139 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
2140 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2141 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2142 $diffinfo->{'to_id'}) . " (new)" .
2143 "</div>\n"; # class="diff_info"
2144
2145 } elsif ($diffinfo->{'status'} eq "D") { # deleted
2146 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
2147 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2148 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2149 $diffinfo->{'from_id'}) . " (deleted)" .
2150 "</div>\n"; # class="diff_info"
2151
2152 } elsif ($diffinfo->{'status'} eq "R" || # renamed
2153 $diffinfo->{'status'} eq "C" || # copied
2154 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
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->{'from_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->{'to_file'})},
2164 $diffinfo->{'to_id'});
2165 print "</div>\n"; # class="diff_info"
2166
2167 } else { # modified, mode changed, ...
2168 print "<div class=\"diff_info\">" .
2169 file_type($diffinfo->{'from_mode'}) . ":" .
2170 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2171 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
2172 $diffinfo->{'from_id'}) .
2173 " -> " .
2174 file_type($diffinfo->{'to_mode'}) . ":" .
2175 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2176 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
2177 $diffinfo->{'to_id'});
2178 print "</div>\n"; # class="diff_info"
2179 }
2180
2181 #print "<div class=\"diff extended_header\">\n";
2182 $in_header = 1;
2183 next LINE;
2184 } # start of patch in patchset
2185
2186
2187 if ($in_header && $patch_line =~ m/^---/) {
2188 #print "</div>\n"; # class="diff extended_header"
2189 $in_header = 0;
2190
2191 my $file = $diffinfo->{'from_file'};
2192 $file ||= $diffinfo->{'file'};
2193 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2194 hash=>$diffinfo->{'from_id'}, file_name=>$file),
2195 -class => "list"}, esc_html($file));
2196 $patch_line =~ s|a/.*$|a/$file|g;
2197 print "<div class=\"diff from_file\">$patch_line</div>\n";
2198
2199 $patch_line = <$fd>;
2200 chomp $patch_line;
2201
2202 #$patch_line =~ m/^+++/;
2203 $file = $diffinfo->{'to_file'};
2204 $file ||= $diffinfo->{'file'};
2205 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2206 hash=>$diffinfo->{'to_id'}, file_name=>$file),
2207 -class => "list"}, esc_html($file));
2208 $patch_line =~ s|b/.*|b/$file|g;
2209 print "<div class=\"diff to_file\">$patch_line</div>\n";
2210
2211 next LINE;
2212 }
2213 next LINE if $in_header;
2214
2215 print format_diff_line($patch_line);
2216 }
2217 print "</div>\n" if $patch_found; # class="patch"
2218
2219 print "</div>\n"; # class="patchset"
2220 }
2221
2222 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2223
2224 sub git_project_list_body {
2225 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
2226
2227 my ($check_forks) = gitweb_check_feature('forks');
2228
2229 my @projects;
2230 foreach my $pr (@$projlist) {
2231 my (@aa) = git_get_last_activity($pr->{'path'});
2232 unless (@aa) {
2233 next;
2234 }
2235 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2236 if (!defined $pr->{'descr'}) {
2237 my $descr = git_get_project_description($pr->{'path'}) || "";
2238 $pr->{'descr'} = chop_str($descr, 25, 5);
2239 }
2240 if (!defined $pr->{'owner'}) {
2241 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2242 }
2243 if ($check_forks) {
2244 my $pname = $pr->{'path'};
2245 if (($pname =~ s/\.git$//) &&
2246 ($pname !~ /\/$/) &&
2247 (-d "$projectroot/$pname")) {
2248 $pr->{'forks'} = "-d $projectroot/$pname";
2249 }
2250 else {
2251 $pr->{'forks'} = 0;
2252 }
2253 }
2254 push @projects, $pr;
2255 }
2256
2257 $order ||= "project";
2258 $from = 0 unless defined $from;
2259 $to = $#projects if (!defined $to || $#projects < $to);
2260
2261 print "<table class=\"project_list\">\n";
2262 unless ($no_header) {
2263 print "<tr>\n";
2264 if ($check_forks) {
2265 print "<th></th>\n";
2266 }
2267 if ($order eq "project") {
2268 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2269 print "<th>Project</th>\n";
2270 } else {
2271 print "<th>" .
2272 $cgi->a({-href => href(project=>undef, order=>'project'),
2273 -class => "header"}, "Project") .
2274 "</th>\n";
2275 }
2276 if ($order eq "descr") {
2277 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2278 print "<th>Description</th>\n";
2279 } else {
2280 print "<th>" .
2281 $cgi->a({-href => href(project=>undef, order=>'descr'),
2282 -class => "header"}, "Description") .
2283 "</th>\n";
2284 }
2285 if ($order eq "owner") {
2286 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2287 print "<th>Owner</th>\n";
2288 } else {
2289 print "<th>" .
2290 $cgi->a({-href => href(project=>undef, order=>'owner'),
2291 -class => "header"}, "Owner") .
2292 "</th>\n";
2293 }
2294 if ($order eq "age") {
2295 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2296 print "<th>Last Change</th>\n";
2297 } else {
2298 print "<th>" .
2299 $cgi->a({-href => href(project=>undef, order=>'age'),
2300 -class => "header"}, "Last Change") .
2301 "</th>\n";
2302 }
2303 print "<th></th>\n" .
2304 "</tr>\n";
2305 }
2306 my $alternate = 1;
2307 for (my $i = $from; $i <= $to; $i++) {
2308 my $pr = $projects[$i];
2309 if ($alternate) {
2310 print "<tr class=\"dark\">\n";
2311 } else {
2312 print "<tr class=\"light\">\n";
2313 }
2314 $alternate ^= 1;
2315 if ($check_forks) {
2316 print "<td>";
2317 if ($pr->{'forks'}) {
2318 print "<!-- $pr->{'forks'} -->\n";
2319 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
2320 }
2321 print "</td>\n";
2322 }
2323 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2324 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2325 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2326 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2327 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
2328 $pr->{'age_string'} . "</td>\n" .
2329 "<td class=\"link\">" .
2330 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2331 $cgi->a({-href => '/git-browser/by-commit.html?r='.$pr->{'path'}}, "graphiclog") . " | " .
2332 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2333 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2334 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
2335 "</td>\n" .
2336 "</tr>\n";
2337 }
2338 if (defined $extra) {
2339 print "<tr>\n";
2340 if ($check_forks) {
2341 print "<td></td>\n";
2342 }
2343 print "<td colspan=\"5\">$extra</td>\n" .
2344 "</tr>\n";
2345 }
2346 print "</table>\n";
2347 }
2348
2349 sub git_shortlog_body {
2350 # uses global variable $project
2351 my ($revlist, $from, $to, $refs, $extra) = @_;
2352
2353 $from = 0 unless defined $from;
2354 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
2355
2356 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2357 my $alternate = 1;
2358 for (my $i = $from; $i <= $to; $i++) {
2359 my $commit = $revlist->[$i];
2360 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2361 my $ref = format_ref_marker($refs, $commit);
2362 my %co = parse_commit($commit);
2363 if ($alternate) {
2364 print "<tr class=\"dark\">\n";
2365 } else {
2366 print "<tr class=\"light\">\n";
2367 }
2368 $alternate ^= 1;
2369 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2370 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2371 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2372 "<td>";
2373 print format_subject_html($co{'title'}, $co{'title_short'},
2374 href(action=>"commit", hash=>$commit), $ref);
2375 print "</td>\n" .
2376 "<td class=\"link\">" .
2377 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
2378 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2379 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
2380 if (gitweb_have_snapshot()) {
2381 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2382 }
2383 print "</td>\n" .
2384 "</tr>\n";
2385 }
2386 if (defined $extra) {
2387 print "<tr>\n" .
2388 "<td colspan=\"4\">$extra</td>\n" .
2389 "</tr>\n";
2390 }
2391 print "</table>\n";
2392 }
2393
2394 sub git_history_body {
2395 # Warning: assumes constant type (blob or tree) during history
2396 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2397
2398 $from = 0 unless defined $from;
2399 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
2400
2401 print "<table class=\"history\" cellspacing=\"0\">\n";
2402 my $alternate = 1;
2403 for (my $i = $from; $i <= $to; $i++) {
2404 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
2405 next;
2406 }
2407
2408 my $commit = $1;
2409 my %co = parse_commit($commit);
2410 if (!%co) {
2411 next;
2412 }
2413
2414 my $ref = format_ref_marker($refs, $commit);
2415
2416 if ($alternate) {
2417 print "<tr class=\"dark\">\n";
2418 } else {
2419 print "<tr class=\"light\">\n";
2420 }
2421 $alternate ^= 1;
2422 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2423 # shortlog uses chop_str($co{'author_name'}, 10)
2424 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2425 "<td>";
2426 # originally git_history used chop_str($co{'title'}, 50)
2427 print format_subject_html($co{'title'}, $co{'title_short'},
2428 href(action=>"commit", hash=>$commit), $ref);
2429 print "</td>\n" .
2430 "<td class=\"link\">" .
2431 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2432 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
2433
2434 if ($ftype eq 'blob') {
2435 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2436 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2437 if (defined $blob_current && defined $blob_parent &&
2438 $blob_current ne $blob_parent) {
2439 print " | " .
2440 $cgi->a({-href => href(action=>"blobdiff",
2441 hash=>$blob_current, hash_parent=>$blob_parent,
2442 hash_base=>$hash_base, hash_parent_base=>$commit,
2443 file_name=>$file_name)},
2444 "diff to current");
2445 }
2446 }
2447 print "</td>\n" .
2448 "</tr>\n";
2449 }
2450 if (defined $extra) {
2451 print "<tr>\n" .
2452 "<td colspan=\"4\">$extra</td>\n" .
2453 "</tr>\n";
2454 }
2455 print "</table>\n";
2456 }
2457
2458 sub git_tags_body {
2459 # uses global variable $project
2460 my ($taglist, $from, $to, $extra) = @_;
2461 $from = 0 unless defined $from;
2462 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2463
2464 print "<table class=\"tags\" cellspacing=\"0\">\n";
2465 my $alternate = 1;
2466 for (my $i = $from; $i <= $to; $i++) {
2467 my $entry = $taglist->[$i];
2468 my %tag = %$entry;
2469 my $comment = $tag{'subject'};
2470 my $comment_short;
2471 if (defined $comment) {
2472 $comment_short = chop_str($comment, 30, 5);
2473 }
2474 if ($alternate) {
2475 print "<tr class=\"dark\">\n";
2476 } else {
2477 print "<tr class=\"light\">\n";
2478 }
2479 $alternate ^= 1;
2480 print "<td><i>$tag{'age'}</i></td>\n" .
2481 "<td>" .
2482 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2483 -class => "list name"}, esc_html($tag{'name'})) .
2484 "</td>\n" .
2485 "<td>";
2486 if (defined $comment) {
2487 print format_subject_html($comment, $comment_short,
2488 href(action=>"tag", hash=>$tag{'id'}));
2489 }
2490 print "</td>\n" .
2491 "<td class=\"selflink\">";
2492 if ($tag{'type'} eq "tag") {
2493 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2494 } else {
2495 print "&nbsp;";
2496 }
2497 print "</td>\n" .
2498 "<td class=\"link\">" . " | " .
2499 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2500 if ($tag{'reftype'} eq "commit") {
2501 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2502 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
2503 } elsif ($tag{'reftype'} eq "blob") {
2504 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2505 }
2506 print "</td>\n" .
2507 "</tr>";
2508 }
2509 if (defined $extra) {
2510 print "<tr>\n" .
2511 "<td colspan=\"5\">$extra</td>\n" .
2512 "</tr>\n";
2513 }
2514 print "</table>\n";
2515 }
2516
2517 sub git_heads_body {
2518 # uses global variable $project
2519 my ($headlist, $head, $from, $to, $extra) = @_;
2520 $from = 0 unless defined $from;
2521 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
2522
2523 print "<table class=\"heads\" cellspacing=\"0\">\n";
2524 my $alternate = 1;
2525 for (my $i = $from; $i <= $to; $i++) {
2526 my $entry = $headlist->[$i];
2527 my %ref = %$entry;
2528 my $curr = $ref{'id'} eq $head;
2529 if ($alternate) {
2530 print "<tr class=\"dark\">\n";
2531 } else {
2532 print "<tr class=\"light\">\n";
2533 }
2534 $alternate ^= 1;
2535 print "<td><i>$ref{'age'}</i></td>\n" .
2536 ($curr ? "<td class=\"current_head\">" : "<td>") .
2537 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
2538 -class => "list name"},esc_html($ref{'name'})) .
2539 "</td>\n" .
2540 "<td class=\"link\">" .
2541 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
2542 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
2543 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
2544 "</td>\n" .
2545 "</tr>";
2546 }
2547 if (defined $extra) {
2548 print "<tr>\n" .
2549 "<td colspan=\"3\">$extra</td>\n" .
2550 "</tr>\n";
2551 }
2552 print "</table>\n";
2553 }
2554
2555 ## ======================================================================
2556 ## ======================================================================
2557 ## actions
2558
2559 sub git_project_list {
2560 my $order = $cgi->param('o');
2561 if (defined $order && $order !~ m/project|descr|owner|age/) {
2562 die_error(undef, "Unknown order parameter");
2563 }
2564
2565 my @list = git_get_projects_list();
2566 if (!@list) {
2567 die_error(undef, "No projects found");
2568 }
2569
2570 git_header_html();
2571 if (-f $home_text) {
2572 print "<div class=\"index_include\">\n";
2573 open (my $fd, $home_text);
2574 print <$fd>;
2575 close $fd;
2576 print "</div>\n";
2577 }
2578 git_project_list_body(\@list, $order);
2579 git_footer_html();
2580 }
2581
2582 sub git_forks {
2583 my $order = $cgi->param('o');
2584 if (defined $order && $order !~ m/project|descr|owner|age/) {
2585 die_error(undef, "Unknown order parameter");
2586 }
2587
2588 my @list = git_get_projects_list($project);
2589 if (!@list) {
2590 die_error(undef, "No forks found");
2591 }
2592
2593 git_header_html();
2594 git_print_page_nav('','');
2595 git_print_header_div('summary', "$project forks");
2596 git_project_list_body(\@list, $order);
2597 git_footer_html();
2598 }
2599
2600 sub git_project_index {
2601 my @projects = git_get_projects_list($project);
2602
2603 print $cgi->header(
2604 -type => 'text/plain',
2605 -charset => 'utf-8',
2606 -content_disposition => 'inline; filename="index.aux"');
2607
2608 foreach my $pr (@projects) {
2609 if (!exists $pr->{'owner'}) {
2610 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2611 }
2612
2613 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2614 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2615 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2616 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2617 $path =~ s/ /\+/g;
2618 $owner =~ s/ /\+/g;
2619
2620 print "$path $owner\n";
2621 }
2622 }
2623
2624 sub git_summary {
2625 my $descr = git_get_project_description($project) || "none";
2626 my $head = git_get_head_hash($project);
2627 my %co = parse_commit($head);
2628 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2629
2630 my $owner = git_get_project_owner($project);
2631
2632 my $refs = git_get_references();
2633 my @taglist = git_get_tags_list(15);
2634 my @headlist = git_get_heads_list(15);
2635 my @forklist;
2636 my ($check_forks) = gitweb_check_feature('forks');
2637
2638 if ($check_forks) {
2639 @forklist = git_get_projects_list($project);
2640 }
2641
2642 git_header_html();
2643 git_print_page_nav('summary','', $head);
2644
2645 print "<div class=\"title\">&nbsp;</div>\n";
2646 print "<table cellspacing=\"0\">\n" .
2647 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2648 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2649 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2650 # use per project git URL list in $projectroot/$project/cloneurl
2651 # or make project git URL from git base URL and project name
2652 my $url_tag = "URL";
2653 my @url_list = git_get_project_url_list($project);
2654 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2655 foreach my $git_url (@url_list) {
2656 next unless $git_url;
2657 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2658 $url_tag = "";
2659 }
2660 print "</table>\n";
2661
2662 if (-s "$projectroot/$project/README.html") {
2663 if (open my $fd, "$projectroot/$project/README.html") {
2664 print "<div class=\"title\">readme</div>\n";
2665 print $_ while (<$fd>);
2666 close $fd;
2667 }
2668 }
2669
2670 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2671 git_get_head_hash($project), "--"
2672 or die_error(undef, "Open git-rev-list failed");
2673 my @revlist = map { chomp; $_ } <$fd>;
2674 close $fd;
2675 git_print_header_div('shortlog');
2676 git_shortlog_body(\@revlist, 0, 15, $refs,
2677 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2678
2679 if (@taglist) {
2680 git_print_header_div('tags');
2681 git_tags_body(\@taglist, 0, 15,
2682 $cgi->a({-href => href(action=>"tags")}, "..."));
2683 }
2684
2685 if (@headlist) {
2686 git_print_header_div('heads');
2687 git_heads_body(\@headlist, $head, 0, 15,
2688 $cgi->a({-href => href(action=>"heads")}, "..."));
2689 }
2690
2691 if (@forklist) {
2692 git_print_header_div('forks');
2693 git_project_list_body(\@forklist, undef, 0, 15,
2694 $cgi->a({-href => href(action=>"forks")}, "..."),
2695 'noheader');
2696 }
2697
2698 git_footer_html();
2699 }
2700
2701 sub git_tag {
2702 my $head = git_get_head_hash($project);
2703 git_header_html();
2704 git_print_page_nav('','', $head,undef,$head);
2705 my %tag = parse_tag($hash);
2706 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2707 print "<div class=\"title_text\">\n" .
2708 "<table cellspacing=\"0\">\n" .
2709 "<tr>\n" .
2710 "<td>object</td>\n" .
2711 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2712 $tag{'object'}) . "</td>\n" .
2713 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2714 $tag{'type'}) . "</td>\n" .
2715 "</tr>\n";
2716 if (defined($tag{'author'})) {
2717 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2718 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2719 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2720 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2721 "</td></tr>\n";
2722 }
2723 print "</table>\n\n" .
2724 "</div>\n";
2725 print "<div class=\"page_body\">";
2726 my $comment = $tag{'comment'};
2727 foreach my $line (@$comment) {
2728 print esc_html($line) . "<br/>\n";
2729 }
2730 print "</div>\n";
2731 git_footer_html();
2732 }
2733
2734 sub git_blame2 {
2735 my $fd;
2736 my $ftype;
2737
2738 my ($have_blame) = gitweb_check_feature('blame');
2739 if (!$have_blame) {
2740 die_error('403 Permission denied', "Permission denied");
2741 }
2742 die_error('404 Not Found', "File name not defined") if (!$file_name);
2743 $hash_base ||= git_get_head_hash($project);
2744 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2745 my %co = parse_commit($hash_base)
2746 or die_error(undef, "Reading commit failed");
2747 if (!defined $hash) {
2748 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2749 or die_error(undef, "Error looking up file");
2750 }
2751 $ftype = git_get_type($hash);
2752 if ($ftype !~ "blob") {
2753 die_error("400 Bad Request", "Object is not a blob");
2754 }
2755 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
2756 $file_name, $hash_base)
2757 or die_error(undef, "Open git-blame failed");
2758 git_header_html();
2759 my $formats_nav =
2760 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2761 "blob") .
2762 " | " .
2763 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2764 "history") .
2765 " | " .
2766 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2767 "HEAD");
2768 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2769 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2770 git_print_page_path($file_name, $ftype, $hash_base);
2771 my @rev_color = (qw(light2 dark2));
2772 my $num_colors = scalar(@rev_color);
2773 my $current_color = 0;
2774 my $last_rev;
2775 print <<HTML;
2776 <div class="page_body">
2777 <table class="blame">
2778 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2779 HTML
2780 my %metainfo = ();
2781 while (1) {
2782 $_ = <$fd>;
2783 last unless defined $_;
2784 my ($full_rev, $orig_lineno, $lineno, $group_size) =
2785 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
2786 if (!exists $metainfo{$full_rev}) {
2787 $metainfo{$full_rev} = {};
2788 }
2789 my $meta = $metainfo{$full_rev};
2790 while (<$fd>) {
2791 last if (s/^\t//);
2792 if (/^(\S+) (.*)$/) {
2793 $meta->{$1} = $2;
2794 }
2795 }
2796 my $data = $_;
2797 my $rev = substr($full_rev, 0, 8);
2798 my $author = $meta->{'author'};
2799 my %date = parse_date($meta->{'author-time'},
2800 $meta->{'author-tz'});
2801 my $date = $date{'iso-tz'};
2802 if ($group_size) {
2803 $current_color = ++$current_color % $num_colors;
2804 }
2805 print "<tr class=\"$rev_color[$current_color]\">\n";
2806 if ($group_size) {
2807 print "<td class=\"sha1\"";
2808 print " title=\"". esc_html($author) . ", $date\"";
2809 print " rowspan=\"$group_size\"" if ($group_size > 1);
2810 print ">";
2811 print $cgi->a({-href => href(action=>"commit",
2812 hash=>$full_rev,
2813 file_name=>$file_name)},
2814 esc_html($rev));
2815 print "</td>\n";
2816 }
2817 my $blamed = href(action => 'blame',
2818 file_name => $meta->{'filename'},
2819 hash_base => $full_rev);
2820 print "<td class=\"linenr\">";
2821 print $cgi->a({ -href => "$blamed#l$orig_lineno",
2822 -id => "l$lineno",
2823 -class => "linenr" },
2824 esc_html($lineno));
2825 print "</td>";
2826 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2827 print "</tr>\n";
2828 }
2829 print "</table>\n";
2830 print "</div>";
2831 close $fd
2832 or print "Reading blob failed\n";
2833 git_footer_html();
2834 }
2835
2836 sub git_blame {
2837 my $fd;
2838
2839 my ($have_blame) = gitweb_check_feature('blame');
2840 if (!$have_blame) {
2841 die_error('403 Permission denied', "Permission denied");
2842 }
2843 die_error('404 Not Found', "File name not defined") if (!$file_name);
2844 $hash_base ||= git_get_head_hash($project);
2845 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2846 my %co = parse_commit($hash_base)
2847 or die_error(undef, "Reading commit failed");
2848 if (!defined $hash) {
2849 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2850 or die_error(undef, "Error lookup file");
2851 }
2852 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2853 or die_error(undef, "Open git-annotate failed");
2854 git_header_html();
2855 my $formats_nav =
2856 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2857 "blob") .
2858 " | " .
2859 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2860 "history") .
2861 " | " .
2862 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2863 "HEAD");
2864 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2865 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2866 git_print_page_path($file_name, 'blob', $hash_base);
2867 print "<div class=\"page_body\">\n";
2868 print <<HTML;
2869 <table class="blame">
2870 <tr>
2871 <th>Commit</th>
2872 <th>Age</th>
2873 <th>Author</th>
2874 <th>Line</th>
2875 <th>Data</th>
2876 </tr>
2877 HTML
2878 my @line_class = (qw(light dark));
2879 my $line_class_len = scalar (@line_class);
2880 my $line_class_num = $#line_class;
2881 while (my $line = <$fd>) {
2882 my $long_rev;
2883 my $short_rev;
2884 my $author;
2885 my $time;
2886 my $lineno;
2887 my $data;
2888 my $age;
2889 my $age_str;
2890 my $age_class;
2891
2892 chomp $line;
2893 $line_class_num = ($line_class_num + 1) % $line_class_len;
2894
2895 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2896 $long_rev = $1;
2897 $author = $2;
2898 $time = $3;
2899 $lineno = $4;
2900 $data = $5;
2901 } else {
2902 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2903 next;
2904 }
2905 $short_rev = substr ($long_rev, 0, 8);
2906 $age = time () - $time;
2907 $age_str = age_string ($age);
2908 $age_str =~ s/ /&nbsp;/g;
2909 $age_class = age_class($age);
2910 $author = esc_html ($author);
2911 $author =~ s/ /&nbsp;/g;
2912
2913 $data = untabify($data);
2914 $data = esc_html ($data);
2915
2916 print <<HTML;
2917 <tr class="$line_class[$line_class_num]">
2918 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2919 <td class="$age_class">$age_str</td>
2920 <td>$author</td>
2921 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2922 <td class="pre">$data</td>
2923 </tr>
2924 HTML
2925 } # while (my $line = <$fd>)
2926 print "</table>\n\n";
2927 close $fd
2928 or print "Reading blob failed.\n";
2929 print "</div>";
2930 git_footer_html();
2931 }
2932
2933 sub git_tags {
2934 my $head = git_get_head_hash($project);
2935 git_header_html();
2936 git_print_page_nav('','', $head,undef,$head);
2937 git_print_header_div('summary', $project);
2938
2939 my @tagslist = git_get_tags_list();
2940 if (@tagslist) {
2941 git_tags_body(\@tagslist);
2942 }
2943 git_footer_html();
2944 }
2945
2946 sub git_heads {
2947 my $head = git_get_head_hash($project);
2948 git_header_html();
2949 git_print_page_nav('','', $head,undef,$head);
2950 git_print_header_div('summary', $project);
2951
2952 my @headslist = git_get_heads_list();
2953 if (@headslist) {
2954 git_heads_body(\@headslist, $head);
2955 }
2956 git_footer_html();
2957 }
2958
2959 sub git_blob_plain {
2960 my $expires;
2961
2962 if (!defined $hash) {
2963 if (defined $file_name) {
2964 my $base = $hash_base || git_get_head_hash($project);
2965 $hash = git_get_hash_by_path($base, $file_name, "blob")
2966 or die_error(undef, "Error lookup file");
2967 } else {
2968 die_error(undef, "No file name defined");
2969 }
2970 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2971 # blobs defined by non-textual hash id's can be cached
2972 $expires = "+1d";
2973 }
2974
2975 my $type = shift;
2976 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2977 or die_error(undef, "Couldn't cat $file_name, $hash");
2978
2979 $type ||= blob_mimetype($fd, $file_name);
2980
2981 # save as filename, even when no $file_name is given
2982 my $save_as = "$hash";
2983 if (defined $file_name) {
2984 $save_as = $file_name;
2985 } elsif ($type =~ m/^text\//) {
2986 $save_as .= '.txt';
2987 }
2988
2989 print $cgi->header(
2990 -type => "$type",
2991 -expires=>$expires,
2992 -content_disposition => 'inline; filename="' . "$save_as" . '"');
2993 undef $/;
2994 binmode STDOUT, ':raw';
2995 print <$fd>;
2996 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2997 $/ = "\n";
2998 close $fd;
2999 }
3000
3001 sub git_blob {
3002 my $expires;
3003
3004 if (!defined $hash) {
3005 if (defined $file_name) {
3006 my $base = $hash_base || git_get_head_hash($project);
3007 $hash = git_get_hash_by_path($base, $file_name, "blob")
3008 or die_error(undef, "Error lookup file");
3009 } else {
3010 die_error(undef, "No file name defined");
3011 }
3012 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3013 # blobs defined by non-textual hash id's can be cached
3014 $expires = "+1d";
3015 }
3016
3017 my ($have_blame) = gitweb_check_feature('blame');
3018 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3019 or die_error(undef, "Couldn't cat $file_name, $hash");
3020 my $mimetype = blob_mimetype($fd, $file_name);
3021 if ($mimetype !~ m/^text\//) {
3022 close $fd;
3023 return git_blob_plain($mimetype);
3024 }
3025 git_header_html(undef, $expires);
3026 my $formats_nav = '';
3027 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3028 if (defined $file_name) {
3029 if ($have_blame) {
3030 $formats_nav .=
3031 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
3032 hash=>$hash, file_name=>$file_name)},
3033 "blame") .
3034 " | ";
3035 }
3036 $formats_nav .=
3037 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3038 hash=>$hash, file_name=>$file_name)},
3039 "history") .
3040 " | " .
3041 $cgi->a({-href => href(action=>"blob_plain",
3042 hash=>$hash, file_name=>$file_name)},
3043 "raw") .
3044 " | " .
3045 $cgi->a({-href => href(action=>"blob",
3046 hash_base=>"HEAD", file_name=>$file_name)},
3047 "HEAD");
3048 } else {
3049 $formats_nav .=
3050 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
3051 }
3052 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3053 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3054 } else {
3055 print "<div class=\"page_nav\">\n" .
3056 "<br/><br/></div>\n" .
3057 "<div class=\"title\">$hash</div>\n";
3058 }
3059 git_print_page_path($file_name, "blob", $hash_base);
3060 print "<div class=\"page_body\">\n";
3061 my $nr;
3062 while (my $line = <$fd>) {
3063 chomp $line;
3064 $nr++;
3065 $line = untabify($line);
3066 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
3067 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
3068 }
3069 close $fd
3070 or print "Reading blob failed.\n";
3071 print "</div>";
3072 git_footer_html();
3073 }
3074
3075 sub git_tree {
3076 my $have_snapshot = gitweb_have_snapshot();
3077
3078 if (!defined $hash_base) {
3079 $hash_base = "HEAD";
3080 }
3081 if (!defined $hash) {
3082 if (defined $file_name) {
3083 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
3084 } else {
3085 $hash = $hash_base;
3086 }
3087 }
3088 $/ = "\0";
3089 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
3090 or die_error(undef, "Open git-ls-tree failed");
3091 my @entries = map { chomp; $_ } <$fd>;
3092 close $fd or die_error(undef, "Reading tree failed");
3093 $/ = "\n";
3094
3095 my $refs = git_get_references();
3096 my $ref = format_ref_marker($refs, $hash_base);
3097 git_header_html();
3098 my $basedir = '';
3099 my ($have_blame) = gitweb_check_feature('blame');
3100 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3101 my @views_nav = ();
3102 if (defined $file_name) {
3103 push @views_nav,
3104 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3105 hash=>$hash, file_name=>$file_name)},
3106 "history"),
3107 $cgi->a({-href => href(action=>"tree",
3108 hash_base=>"HEAD", file_name=>$file_name)},
3109 "HEAD"),
3110 }
3111 if ($have_snapshot) {
3112 # FIXME: Should be available when we have no hash base as well.
3113 push @views_nav,
3114 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
3115 "snapshot");
3116 }
3117 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
3118 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
3119 } else {
3120 undef $hash_base;
3121 print "<div class=\"page_nav\">\n";
3122 print "<br/><br/></div>\n";
3123 print "<div class=\"title\">$hash</div>\n";
3124 }
3125 if (defined $file_name) {
3126 $basedir = $file_name;
3127 if ($basedir ne '' && substr($basedir, -1) ne '/') {
3128 $basedir .= '/';
3129 }
3130 }
3131 git_print_page_path($file_name, 'tree', $hash_base);
3132 print "<div class=\"page_body\">\n";
3133 print "<table cellspacing=\"0\">\n";
3134 my $alternate = 1;
3135 # '..' (top directory) link if possible
3136 if (defined $hash_base &&
3137 defined $file_name && $file_name =~ m![^/]+$!) {
3138 if ($alternate) {
3139 print "<tr class=\"dark\">\n";
3140 } else {
3141 print "<tr class=\"light\">\n";
3142 }
3143 $alternate ^= 1;
3144
3145 my $up = $file_name;
3146 $up =~ s!/?[^/]+$!!;
3147 undef $up unless $up;
3148 # based on git_print_tree_entry
3149 print '<td class="mode">' . mode_str('040000') . "</td>\n";
3150 print '<td class="list">';
3151 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
3152 file_name=>$up)},
3153 "..");
3154 print "</td>\n";
3155 print "<td class=\"link\"></td>\n";
3156
3157 print "</tr>\n";
3158 }
3159 foreach my $line (@entries) {
3160 my %t = parse_ls_tree_line($line, -z => 1);
3161
3162 if ($alternate) {
3163 print "<tr class=\"dark\">\n";
3164 } else {
3165 print "<tr class=\"light\">\n";
3166 }
3167 $alternate ^= 1;
3168
3169 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
3170
3171 print "</tr>\n";
3172 }
3173 print "</table>\n" .
3174 "</div>";
3175 git_footer_html();
3176 }
3177
3178 sub git_snapshot {
3179 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
3180 my $have_snapshot = (defined $ctype && defined $suffix);
3181 if (!$have_snapshot) {
3182 die_error('403 Permission denied', "Permission denied");
3183 }
3184
3185 if (!defined $hash) {
3186 $hash = git_get_head_hash($project);
3187 }
3188
3189 my $filename = basename($project) . "-$hash.tar.$suffix";
3190
3191 print $cgi->header(
3192 -type => 'application/x-tar',
3193 -content_encoding => $ctype,
3194 -content_disposition => 'inline; filename="' . "$filename" . '"',
3195 -status => '200 OK');
3196
3197 my $git = git_cmd_str();
3198 my $name = $project;
3199 $name =~ s/\047/\047\\\047\047/g;
3200 open my $fd, "-|",
3201 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3202 or die_error(undef, "Execute git-tar-tree failed.");
3203 binmode STDOUT, ':raw';
3204 print <$fd>;
3205 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3206 close $fd;
3207
3208 }
3209
3210 sub git_log {
3211 my $head = git_get_head_hash($project);
3212 if (!defined $hash) {
3213 $hash = $head;
3214 }
3215 if (!defined $page) {
3216 $page = 0;
3217 }
3218 my $refs = git_get_references();
3219
3220 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3221 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash, "--"
3222 or die_error(undef, "Open git-rev-list failed");
3223 my @revlist = map { chomp; $_ } <$fd>;
3224 close $fd;
3225
3226 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
3227
3228 git_header_html();
3229 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
3230
3231 if (!@revlist) {
3232 my %co = parse_commit($hash);
3233
3234 git_print_header_div('summary', $project);
3235 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
3236 }
3237 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
3238 my $commit = $revlist[$i];
3239 my $ref = format_ref_marker($refs, $commit);
3240 my %co = parse_commit($commit);
3241 next if !%co;
3242 my %ad = parse_date($co{'author_epoch'});
3243 git_print_header_div('commit',
3244 "<span class=\"age\">$co{'age_string'}</span>" .
3245 esc_html($co{'title'}) . $ref,
3246 $commit);
3247 print "<div class=\"title_text\">\n" .
3248 "<div class=\"log_link\">\n" .
3249 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
3250 " | " .
3251 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
3252 " | " .
3253 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
3254 "<br/>\n" .
3255 "</div>\n" .
3256 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
3257 "</div>\n";
3258
3259 print "<div class=\"log_body\">\n";
3260 git_print_log($co{'comment'}, -final_empty_line=> 1);
3261 print "</div>\n";
3262 }
3263 git_footer_html();
3264 }
3265
3266 sub git_commit {
3267 my %co = parse_commit($hash);
3268 if (!%co) {
3269 die_error(undef, "Unknown commit object");
3270 }
3271 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3272 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
3273
3274 my $parent = $co{'parent'};
3275 if (!defined $parent) {
3276 $parent = "--root";
3277 }
3278 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
3279 @diff_opts, $parent, $hash, "--"
3280 or die_error(undef, "Open git-diff-tree failed");
3281 my @difftree = map { chomp; $_ } <$fd>;
3282 close $fd or die_error(undef, "Reading git-diff-tree failed");
3283
3284 # non-textual hash id's can be cached
3285 my $expires;
3286 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3287 $expires = "+1d";
3288 }
3289 my $refs = git_get_references();
3290 my $ref = format_ref_marker($refs, $co{'id'});
3291
3292 my $have_snapshot = gitweb_have_snapshot();
3293
3294 my @views_nav = ();
3295 if (defined $file_name && defined $co{'parent'}) {
3296 push @views_nav,
3297 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
3298 "blame");
3299 }
3300 git_header_html(undef, $expires);
3301 git_print_page_nav('commit', '',
3302 $hash, $co{'tree'}, $hash,
3303 join (' | ', @views_nav));
3304
3305 if (defined $co{'parent'}) {
3306 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
3307 } else {
3308 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
3309 }
3310 print "<div class=\"title_text\">\n" .
3311 "<table cellspacing=\"0\">\n";
3312 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
3313 "<tr>" .
3314 "<td></td><td> $ad{'rfc2822'}";
3315 if ($ad{'hour_local'} < 6) {
3316 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3317 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3318 } else {
3319 printf(" (%02d:%02d %s)",
3320 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3321 }
3322 print "</td>" .
3323 "</tr>\n";
3324 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
3325 print "<tr><td></td><td> $cd{'rfc2822'}" .
3326 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
3327 "</td></tr>\n";
3328 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
3329 print "<tr>" .
3330 "<td>tree</td>" .
3331 "<td class=\"sha1\">" .
3332 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
3333 class => "list"}, $co{'tree'}) .
3334 "</td>" .
3335 "<td class=\"link\">" .
3336 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
3337 "tree");
3338 if ($have_snapshot) {
3339 print " | " .
3340 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
3341 }
3342 print "</td>" .
3343 "</tr>\n";
3344 my $parents = $co{'parents'};
3345 foreach my $par (@$parents) {
3346 print "<tr>" .
3347 "<td>parent</td>" .
3348 "<td class=\"sha1\">" .
3349 $cgi->a({-href => href(action=>"commit", hash=>$par),
3350 class => "list"}, $par) .
3351 "</td>" .
3352 "<td class=\"link\">" .
3353 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
3354 " | " .
3355 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
3356 "</td>" .
3357 "</tr>\n";
3358 }
3359 print "</table>".
3360 "</div>\n";
3361
3362 print "<div class=\"page_body\">\n";
3363 git_print_log($co{'comment'});
3364 print "</div>\n";
3365
3366 git_difftree_body(\@difftree, $hash, $parent);
3367
3368 git_footer_html();
3369 }
3370
3371 sub git_blobdiff {
3372 my $format = shift || 'html';
3373
3374 my $fd;
3375 my @difftree;
3376 my %diffinfo;
3377 my $expires;
3378
3379 # preparing $fd and %diffinfo for git_patchset_body
3380 # new style URI
3381 if (defined $hash_base && defined $hash_parent_base) {
3382 if (defined $file_name) {
3383 # read raw output
3384 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3385 $hash_parent_base, $hash_base,
3386 "--", $file_name
3387 or die_error(undef, "Open git-diff-tree failed");
3388 @difftree = map { chomp; $_ } <$fd>;
3389 close $fd
3390 or die_error(undef, "Reading git-diff-tree failed");
3391 @difftree
3392 or die_error('404 Not Found', "Blob diff not found");
3393
3394 } elsif (defined $hash &&
3395 $hash =~ /[0-9a-fA-F]{40}/) {
3396 # try to find filename from $hash
3397
3398 # read filtered raw output
3399 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3400 $hash_parent_base, $hash_base, "--"
3401 or die_error(undef, "Open git-diff-tree failed");
3402 @difftree =
3403 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3404 # $hash == to_id
3405 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3406 map { chomp; $_ } <$fd>;
3407 close $fd
3408 or die_error(undef, "Reading git-diff-tree failed");
3409 @difftree
3410 or die_error('404 Not Found', "Blob diff not found");
3411
3412 } else {
3413 die_error('404 Not Found', "Missing one of the blob diff parameters");
3414 }
3415
3416 if (@difftree > 1) {
3417 die_error('404 Not Found', "Ambiguous blob diff specification");
3418 }
3419
3420 %diffinfo = parse_difftree_raw_line($difftree[0]);
3421 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3422 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3423
3424 $hash_parent ||= $diffinfo{'from_id'};
3425 $hash ||= $diffinfo{'to_id'};
3426
3427 # non-textual hash id's can be cached
3428 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3429 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3430 $expires = '+1d';
3431 }
3432
3433 # open patch output
3434 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3435 '-p', $hash_parent_base, $hash_base,
3436 "--", $file_name
3437 or die_error(undef, "Open git-diff-tree failed");
3438 }
3439
3440 # old/legacy style URI
3441 if (!%diffinfo && # if new style URI failed
3442 defined $hash && defined $hash_parent) {
3443 # fake git-diff-tree raw output
3444 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3445 $diffinfo{'from_id'} = $hash_parent;
3446 $diffinfo{'to_id'} = $hash;
3447 if (defined $file_name) {
3448 if (defined $file_parent) {
3449 $diffinfo{'status'} = '2';
3450 $diffinfo{'from_file'} = $file_parent;
3451 $diffinfo{'to_file'} = $file_name;
3452 } else { # assume not renamed
3453 $diffinfo{'status'} = '1';
3454 $diffinfo{'from_file'} = $file_name;
3455 $diffinfo{'to_file'} = $file_name;
3456 }
3457 } else { # no filename given
3458 $diffinfo{'status'} = '2';
3459 $diffinfo{'from_file'} = $hash_parent;
3460 $diffinfo{'to_file'} = $hash;
3461 }
3462
3463 # non-textual hash id's can be cached
3464 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3465 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3466 $expires = '+1d';
3467 }
3468
3469 # open patch output
3470 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts,
3471 $hash_parent, $hash, "--"
3472 or die_error(undef, "Open git-diff failed");
3473 } else {
3474 die_error('404 Not Found', "Missing one of the blob diff parameters")
3475 unless %diffinfo;
3476 }
3477
3478 # header
3479 if ($format eq 'html') {
3480 my $formats_nav =
3481 $cgi->a({-href => href(action=>"blobdiff_plain",
3482 hash=>$hash, hash_parent=>$hash_parent,
3483 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3484 file_name=>$file_name, file_parent=>$file_parent)},
3485 "raw");
3486 git_header_html(undef, $expires);
3487 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3488 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3489 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3490 } else {
3491 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3492 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3493 }
3494 if (defined $file_name) {
3495 git_print_page_path($file_name, "blob", $hash_base);
3496 } else {
3497 print "<div class=\"page_path\"></div>\n";
3498 }
3499
3500 } elsif ($format eq 'plain') {
3501 print $cgi->header(
3502 -type => 'text/plain',
3503 -charset => 'utf-8',
3504 -expires => $expires,
3505 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
3506
3507 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3508
3509 } else {
3510 die_error(undef, "Unknown blobdiff format");
3511 }
3512
3513 # patch
3514 if ($format eq 'html') {
3515 print "<div class=\"page_body\">\n";
3516
3517 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3518 close $fd;
3519
3520 print "</div>\n"; # class="page_body"
3521 git_footer_html();
3522
3523 } else {
3524 while (my $line = <$fd>) {
3525 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3526 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
3527
3528 print $line;
3529
3530 last if $line =~ m!^\+\+\+!;
3531 }
3532 local $/ = undef;
3533 print <$fd>;
3534 close $fd;
3535 }
3536 }
3537
3538 sub git_blobdiff_plain {
3539 git_blobdiff('plain');
3540 }
3541
3542 sub git_commitdiff {
3543 my $format = shift || 'html';
3544 my %co = parse_commit($hash);
3545 if (!%co) {
3546 die_error(undef, "Unknown commit object");
3547 }
3548
3549 # we need to prepare $formats_nav before any parameter munging
3550 my $formats_nav;
3551 if ($format eq 'html') {
3552 $formats_nav =
3553 $cgi->a({-href => href(action=>"commitdiff_plain",
3554 hash=>$hash, hash_parent=>$hash_parent)},
3555 "raw");
3556
3557 if (defined $hash_parent) {
3558 # commitdiff with two commits given
3559 my $hash_parent_short = $hash_parent;
3560 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3561 $hash_parent_short = substr($hash_parent, 0, 7);
3562 }
3563 $formats_nav .=
3564 ' (from: ' .
3565 $cgi->a({-href => href(action=>"commitdiff",
3566 hash=>$hash_parent)},
3567 esc_html($hash_parent_short)) .
3568 ')';
3569 } elsif (!$co{'parent'}) {
3570 # --root commitdiff
3571 $formats_nav .= ' (initial)';
3572 } elsif (scalar @{$co{'parents'}} == 1) {
3573 # single parent commit
3574 $formats_nav .=
3575 ' (parent: ' .
3576 $cgi->a({-href => href(action=>"commitdiff",
3577 hash=>$co{'parent'})},
3578 esc_html(substr($co{'parent'}, 0, 7))) .
3579 ')';
3580 } else {
3581 # merge commit
3582 $formats_nav .=
3583 ' (merge: ' .
3584 join(' ', map {
3585 $cgi->a({-href => href(action=>"commitdiff",
3586 hash=>$_)},
3587 esc_html(substr($_, 0, 7)));
3588 } @{$co{'parents'}} ) .
3589 ')';
3590 }
3591 }
3592
3593 if (!defined $hash_parent) {
3594 $hash_parent = $co{'parent'} || '--root';
3595 }
3596
3597 # read commitdiff
3598 my $fd;
3599 my @difftree;
3600 if ($format eq 'html') {
3601 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3602 "--no-commit-id", "--patch-with-raw", "--full-index",
3603 $hash_parent, $hash, "--"
3604 or die_error(undef, "Open git-diff-tree failed");
3605
3606 while (chomp(my $line = <$fd>)) {
3607 # empty line ends raw part of diff-tree output
3608 last unless $line;
3609 push @difftree, $line;
3610 }
3611
3612 } elsif ($format eq 'plain') {
3613 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3614 '-p', $hash_parent, $hash, "--"
3615 or die_error(undef, "Open git-diff-tree failed");
3616
3617 } else {
3618 die_error(undef, "Unknown commitdiff format");
3619 }
3620
3621 # non-textual hash id's can be cached
3622 my $expires;
3623 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3624 $expires = "+1d";
3625 }
3626
3627 # write commit message
3628 if ($format eq 'html') {
3629 my $refs = git_get_references();
3630 my $ref = format_ref_marker($refs, $co{'id'});
3631
3632 git_header_html(undef, $expires);
3633 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3634 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3635 git_print_authorship(\%co);
3636 print "<div class=\"page_body\">\n";
3637 if (@{$co{'comment'}} > 1) {
3638 print "<div class=\"log\">\n";
3639 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
3640 print "</div>\n"; # class="log"
3641 }
3642
3643 } elsif ($format eq 'plain') {
3644 my $refs = git_get_references("tags");
3645 my $tagname = git_get_rev_name_tags($hash);
3646 my $filename = basename($project) . "-$hash.patch";
3647
3648 print $cgi->header(
3649 -type => 'text/plain',
3650 -charset => 'utf-8',
3651 -expires => $expires,
3652 -content_disposition => 'inline; filename="' . "$filename" . '"');
3653 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3654 print <<TEXT;
3655 From: $co{'author'}
3656 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3657 Subject: $co{'title'}
3658 TEXT
3659 print "X-Git-Tag: $tagname\n" if $tagname;
3660 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3661
3662 foreach my $line (@{$co{'comment'}}) {
3663 print "$line\n";
3664 }
3665 print "---\n\n";
3666 }
3667
3668 # write patch
3669 if ($format eq 'html') {
3670 git_difftree_body(\@difftree, $hash, $hash_parent);
3671 print "<br/>\n";
3672
3673 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3674 close $fd;
3675 print "</div>\n"; # class="page_body"
3676 git_footer_html();
3677
3678 } elsif ($format eq 'plain') {
3679 local $/ = undef;
3680 print <$fd>;
3681 close $fd
3682 or print "Reading git-diff-tree failed\n";
3683 }
3684 }
3685
3686 sub git_commitdiff_plain {
3687 git_commitdiff('plain');
3688 }
3689
3690 sub git_history {
3691 if (!defined $hash_base) {
3692 $hash_base = git_get_head_hash($project);
3693 }
3694 if (!defined $page) {
3695 $page = 0;
3696 }
3697 my $ftype;
3698 my %co = parse_commit($hash_base);
3699 if (!%co) {
3700 die_error(undef, "Unknown commit object");
3701 }
3702
3703 my $refs = git_get_references();
3704 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3705
3706 if (!defined $hash && defined $file_name) {
3707 $hash = git_get_hash_by_path($hash_base, $file_name);
3708 }
3709 if (defined $hash) {
3710 $ftype = git_get_type($hash);
3711 }
3712
3713 open my $fd, "-|",
3714 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3715 or die_error(undef, "Open git-rev-list-failed");
3716 my @revlist = map { chomp; $_ } <$fd>;
3717 close $fd
3718 or die_error(undef, "Reading git-rev-list failed");
3719
3720 my $paging_nav = '';
3721 if ($page > 0) {
3722 $paging_nav .=
3723 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3724 file_name=>$file_name)},
3725 "first");
3726 $paging_nav .= " &sdot; " .
3727 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3728 file_name=>$file_name, page=>$page-1),
3729 -accesskey => "p", -title => "Alt-p"}, "prev");
3730 } else {
3731 $paging_nav .= "first";
3732 $paging_nav .= " &sdot; prev";
3733 }
3734 if ($#revlist >= (100 * ($page+1)-1)) {
3735 $paging_nav .= " &sdot; " .
3736 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3737 file_name=>$file_name, page=>$page+1),
3738 -accesskey => "n", -title => "Alt-n"}, "next");
3739 } else {
3740 $paging_nav .= " &sdot; next";
3741 }
3742 my $next_link = '';
3743 if ($#revlist >= (100 * ($page+1)-1)) {
3744 $next_link =
3745 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3746 file_name=>$file_name, page=>$page+1),
3747 -title => "Alt-n"}, "next");
3748 }
3749
3750 git_header_html();
3751 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3752 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3753 git_print_page_path($file_name, $ftype, $hash_base);
3754
3755 git_history_body(\@revlist, ($page * 100), $#revlist,
3756 $refs, $hash_base, $ftype, $next_link);
3757
3758 git_footer_html();
3759 }
3760
3761 sub git_search {
3762 if (!defined $searchtext) {
3763 die_error(undef, "Text field empty");
3764 }
3765 if (!defined $hash) {
3766 $hash = git_get_head_hash($project);
3767 }
3768 my %co = parse_commit($hash);
3769 if (!%co) {
3770 die_error(undef, "Unknown commit object");
3771 }
3772
3773 $searchtype ||= 'commit';
3774 if ($searchtype eq 'pickaxe') {
3775 # pickaxe may take all resources of your box and run for several minutes
3776 # with every query - so decide by yourself how public you make this feature
3777 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3778 if (!$have_pickaxe) {
3779 die_error('403 Permission denied', "Permission denied");
3780 }
3781 }
3782
3783 git_header_html();
3784 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3785 git_print_header_div('commit', esc_html($co{'title'}), $hash);
3786
3787 print "<table cellspacing=\"0\">\n";
3788 my $alternate = 1;
3789 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
3790 $/ = "\0";
3791 open my $fd, "-|", git_cmd(), "rev-list",
3792 "--header", "--parents", $hash, "--"
3793 or next;
3794 while (my $commit_text = <$fd>) {
3795 if (!grep m/$searchtext/i, $commit_text) {
3796 next;
3797 }
3798 if ($searchtype eq 'author' && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3799 next;
3800 }
3801 if ($searchtype eq 'committer' && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3802 next;
3803 }
3804 my @commit_lines = split "\n", $commit_text;
3805 my %co = parse_commit(undef, \@commit_lines);
3806 if (!%co) {
3807 next;
3808 }
3809 if ($alternate) {
3810 print "<tr class=\"dark\">\n";
3811 } else {
3812 print "<tr class=\"light\">\n";
3813 }
3814 $alternate ^= 1;
3815 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3816 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3817 "<td>" .
3818 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3819 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3820 my $comment = $co{'comment'};
3821 foreach my $line (@$comment) {
3822 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3823 my $lead = esc_html($1) || "";
3824 $lead = chop_str($lead, 30, 10);
3825 my $match = esc_html($2) || "";
3826 my $trail = esc_html($3) || "";
3827 $trail = chop_str($trail, 30, 10);
3828 my $text = "$lead<span class=\"match\">$match</span>$trail";
3829 print chop_str($text, 80, 5) . "<br/>\n";
3830 }
3831 }
3832 print "</td>\n" .
3833 "<td class=\"link\">" .
3834 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3835 " | " .
3836 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3837 print "</td>\n" .
3838 "</tr>\n";
3839 }
3840 close $fd;
3841 }
3842
3843 if ($searchtype eq 'pickaxe') {
3844 $/ = "\n";
3845 my $git_command = git_cmd_str();
3846 open my $fd, "-|", "$git_command rev-list $hash | " .
3847 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3848 undef %co;
3849 my @files;
3850 while (my $line = <$fd>) {
3851 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3852 my %set;
3853 $set{'file'} = $6;
3854 $set{'from_id'} = $3;
3855 $set{'to_id'} = $4;
3856 $set{'id'} = $set{'to_id'};
3857 if ($set{'id'} =~ m/0{40}/) {
3858 $set{'id'} = $set{'from_id'};
3859 }
3860 if ($set{'id'} =~ m/0{40}/) {
3861 next;
3862 }
3863 push @files, \%set;
3864 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3865 if (%co) {
3866 if ($alternate) {
3867 print "<tr class=\"dark\">\n";
3868 } else {
3869 print "<tr class=\"light\">\n";
3870 }
3871 $alternate ^= 1;
3872 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3873 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3874 "<td>" .
3875 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3876 -class => "list subject"},
3877 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3878 while (my $setref = shift @files) {
3879 my %set = %$setref;
3880 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3881 hash=>$set{'id'}, file_name=>$set{'file'}),
3882 -class => "list"},
3883 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3884 "<br/>\n";
3885 }
3886 print "</td>\n" .
3887 "<td class=\"link\">" .
3888 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3889 " | " .
3890 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3891 print "</td>\n" .
3892 "</tr>\n";
3893 }
3894 %co = parse_commit($1);
3895 }
3896 }
3897 close $fd;
3898 }
3899 print "</table>\n";
3900 git_footer_html();
3901 }
3902
3903 sub git_search_help {
3904 git_header_html();
3905 git_print_page_nav('','', $hash,$hash,$hash);
3906 print <<EOT;
3907 <dl>
3908 <dt><b>commit</b></dt>
3909 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
3910 <dt><b>author</b></dt>
3911 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
3912 <dt><b>committer</b></dt>
3913 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
3914 EOT
3915 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3916 if ($have_pickaxe) {
3917 print <<EOT;
3918 <dt><b>pickaxe</b></dt>
3919 <dd>All commits that caused the string to appear or disappear from any file (changes that
3920 added, removed or "modified" the string) will be listed. This search can take a while and
3921 takes a lot of strain on the server, so please use it wisely.</dd>
3922 EOT
3923 }
3924 print "</dl>\n";
3925 git_footer_html();
3926 }
3927
3928 sub git_shortlog {
3929 my $head = git_get_head_hash($project);
3930 if (!defined $hash) {
3931 $hash = $head;
3932 }
3933 if (!defined $page) {
3934 $page = 0;
3935 }
3936 my $refs = git_get_references();
3937
3938 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3939 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash, "--"
3940 or die_error(undef, "Open git-rev-list failed");
3941 my @revlist = map { chomp; $_ } <$fd>;
3942 close $fd;
3943
3944 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3945 my $next_link = '';
3946 if ($#revlist >= (100 * ($page+1)-1)) {
3947 $next_link =
3948 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3949 -title => "Alt-n"}, "next");
3950 }
3951
3952
3953 git_header_html();
3954 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3955 git_print_header_div('summary', $project);
3956
3957 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3958
3959 git_footer_html();
3960 }
3961
3962 ## ......................................................................
3963 ## feeds (RSS, OPML)
3964
3965 sub git_rss {
3966 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3967 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150",
3968 git_get_head_hash($project), "--"
3969 or die_error(undef, "Open git-rev-list failed");
3970 my @revlist = map { chomp; $_ } <$fd>;
3971 close $fd or die_error(undef, "Reading git-rev-list failed");
3972 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3973 print <<XML;
3974 <?xml version="1.0" encoding="utf-8"?>
3975 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3976 <channel>
3977 <title>$project $my_uri $my_url</title>
3978 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3979 <description>$project log</description>
3980 <language>en</language>
3981 XML
3982
3983 for (my $i = 0; $i <= $#revlist; $i++) {
3984 my $commit = $revlist[$i];
3985 my %co = parse_commit($commit);
3986 # we read 150, we always show 30 and the ones more recent than 48 hours
3987 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3988 last;
3989 }
3990 my %cd = parse_date($co{'committer_epoch'});
3991 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3992 $co{'parent'}, $co{'id'}, "--"
3993 or next;
3994 my @difftree = map { chomp; $_ } <$fd>;
3995 close $fd
3996 or next;
3997 print "<item>\n" .
3998 "<title>" .
3999 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
4000 "</title>\n" .
4001 "<author>" . esc_html($co{'author'}) . "</author>\n" .
4002 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
4003 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
4004 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
4005 "<description>" . esc_html($co{'title'}) . "</description>\n" .
4006 "<content:encoded>" .
4007 "<![CDATA[\n";
4008 my $comment = $co{'comment'};
4009 foreach my $line (@$comment) {
4010 $line = to_utf8($line);
4011 print "$line<br/>\n";
4012 }
4013 print "<br/>\n";
4014 foreach my $line (@difftree) {
4015 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
4016 next;
4017 }
4018 my $file = esc_html(unquote($7));
4019 $file = to_utf8($file);
4020 print "$file<br/>\n";
4021 }
4022 print "]]>\n" .
4023 "</content:encoded>\n" .
4024 "</item>\n";
4025 }
4026 print "</channel></rss>";
4027 }
4028
4029 sub git_opml {
4030 my @list = git_get_projects_list();
4031
4032 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
4033 print <<XML;
4034 <?xml version="1.0" encoding="utf-8"?>
4035 <opml version="1.0">
4036 <head>
4037 <title>$site_name OPML Export</title>
4038 </head>
4039 <body>
4040 <outline text="git RSS feeds">
4041 XML
4042
4043 foreach my $pr (@list) {
4044 my %proj = %$pr;
4045 my $head = git_get_head_hash($proj{'path'});
4046 if (!defined $head) {
4047 next;
4048 }
4049 $git_dir = "$projectroot/$proj{'path'}";
4050 my %co = parse_commit($head);
4051 if (!%co) {
4052 next;
4053 }
4054
4055 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
4056 my $rss = "$my_url?p=$proj{'path'};a=rss";
4057 my $html = "$my_url?p=$proj{'path'};a=summary";
4058 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
4059 }
4060 print <<XML;
4061 </outline>
4062 </body>
4063 </opml>
4064 XML
4065 }
This page took 4.085876 seconds and 5 git commands to generate.