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