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