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