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