50e405f6fef49a8c099d44f8f3fce7b1d6f1bbca
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
10use strict;
11use warnings;
12use CGI qw(:standard :escapeHTML -nosticky);
13use CGI::Util qw(unescape);
14use CGI::Carp qw(fatalsToBrowser);
15use Encode;
16use Fcntl ':mode';
17use File::Find qw();
18use File::Basename qw(basename);
19binmode STDOUT, ':utf8';
20
21our $cgi = new CGI;
22our $version = "++GIT_VERSION++";
23our $my_url = $cgi->url();
24our $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
28our $GIT = "++GIT_BINDIR++/git";
29
30# absolute fs-path which will be prepended to the project path
31#our $projectroot = "/pub/scm";
32our $projectroot = "++GITWEB_PROJECTROOT++";
33
34# location for temporary files needed for diffs
35our $git_temp = "/tmp/gitweb";
36
37# target of the home link on top of all pages
38our $home_link = $my_uri || "/";
39
40# string of the home link on top of all pages
41our $home_link_str = "++GITWEB_HOME_LINK_STR++";
42
43# name of your site or organization to appear in page titles
44# replace this with something more descriptive for clearer bookmarks
45our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
46
47# html text to include at home page
48our $home_text = "++GITWEB_HOMETEXT++";
49
50# URI of default stylesheet
51our $stylesheet = "++GITWEB_CSS++";
52# URI of GIT logo
53our $logo = "++GITWEB_LOGO++";
54
55# source of projects list
56our $projects_list = "++GITWEB_LIST++";
57
58# list of git base URLs used for URL to where fetch project from,
59# i.e. full URL is "$git_base_url/$project"
60our @git_base_url_list = ("++GITWEB_BASE_URL++");
61
62# default blob_plain mimetype and default charset for text/plain blob
63our $default_blob_plain_mimetype = 'text/plain';
64our $default_text_plain_charset = undef;
65
66# file to use for guessing MIME types before trying /etc/mime.types
67# (relative to the current git repository)
68our $mimetypes_file = undef;
69
70# You define site-wide feature defaults here; override them with
71# $GITWEB_CONFIG as necessary.
72our %feature = (
73 # feature => {'sub' => feature-sub, 'override' => allow-override, 'default' => [ default options...]
74 # if feature is overridable, feature-sub will be called with default options;
75 # return value indicates if to enable specified feature
76
77 'blame' => {
78 'sub' => \&feature_blame,
79 'override' => 0,
80 'default' => [0]},
81
82 'snapshot' => {
83 'sub' => \&feature_snapshot,
84 'override' => 0,
85 # => [content-encoding, suffix, program]
86 'default' => ['x-gzip', 'gz', 'gzip']},
87);
88
89sub gitweb_check_feature {
90 my ($name) = @_;
91 return undef unless exists $feature{$name};
92 my ($sub, $override, @defaults) = (
93 $feature{$name}{'sub'},
94 $feature{$name}{'override'},
95 @{$feature{$name}{'default'}});
96 if (!$override) { return @defaults; }
97 return $sub->(@defaults);
98}
99
100# To enable system wide have in $GITWEB_CONFIG
101# $feature{'blame'}{'default'} = [1];
102# To have project specific config enable override in $GITWEB_CONFIG
103# $feature{'blame'}{'override'} = 1;
104# and in project config gitweb.blame = 0|1;
105
106sub feature_blame {
107 my ($val) = git_get_project_config('blame', '--bool');
108
109 if ($val eq 'true') {
110 return 1;
111 } elsif ($val eq 'false') {
112 return 0;
113 }
114
115 return $_[0];
116}
117
118# To disable system wide have in $GITWEB_CONFIG
119# $feature{'snapshot'}{'default'} = [undef];
120# To have project specific config enable override in $GITWEB_CONFIG
121# $feature{'blame'}{'override'} = 1;
122# and in project config gitweb.snapshot = none|gzip|bzip2
123
124sub feature_snapshot {
125 my ($ctype, $suffix, $command) = @_;
126
127 my ($val) = git_get_project_config('snapshot');
128
129 if ($val eq 'gzip') {
130 return ('x-gzip', 'gz', 'gzip');
131 } elsif ($val eq 'bzip2') {
132 return ('x-bzip2', 'bz2', 'bzip2');
133 } elsif ($val eq 'none') {
134 return ();
135 }
136
137 return ($ctype, $suffix, $command);
138}
139
140our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
141require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
142
143# version of the core git binary
144our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
145
146$projects_list ||= $projectroot;
147if (! -d $git_temp) {
148 mkdir($git_temp, 0700) || die_error(undef, "Couldn't mkdir $git_temp");
149}
150
151# ======================================================================
152# input validation and dispatch
153our $action = $cgi->param('a');
154if (defined $action) {
155 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
156 die_error(undef, "Invalid action parameter");
157 }
158}
159
160our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
161if (defined $project) {
162 $project =~ s|^/||;
163 $project =~ s|/$||;
164 $project = undef unless $project;
165}
166if (defined $project) {
167 if (!validate_input($project)) {
168 die_error(undef, "Invalid project parameter");
169 }
170 if (!(-d "$projectroot/$project")) {
171 die_error(undef, "No such directory");
172 }
173 if (!(-e "$projectroot/$project/HEAD")) {
174 die_error(undef, "No such project");
175 }
176 $ENV{'GIT_DIR'} = "$projectroot/$project";
177}
178
179our $file_name = $cgi->param('f');
180if (defined $file_name) {
181 if (!validate_input($file_name)) {
182 die_error(undef, "Invalid file parameter");
183 }
184}
185
186our $file_parent = $cgi->param('fp');
187if (defined $file_parent) {
188 if (!validate_input($file_parent)) {
189 die_error(undef, "Invalid file parent parameter");
190 }
191}
192
193our $hash = $cgi->param('h');
194if (defined $hash) {
195 if (!validate_input($hash)) {
196 die_error(undef, "Invalid hash parameter");
197 }
198}
199
200our $hash_parent = $cgi->param('hp');
201if (defined $hash_parent) {
202 if (!validate_input($hash_parent)) {
203 die_error(undef, "Invalid hash parent parameter");
204 }
205}
206
207our $hash_base = $cgi->param('hb');
208if (defined $hash_base) {
209 if (!validate_input($hash_base)) {
210 die_error(undef, "Invalid hash base parameter");
211 }
212}
213
214our $page = $cgi->param('pg');
215if (defined $page) {
216 if ($page =~ m/[^0-9]$/) {
217 die_error(undef, "Invalid page parameter");
218 }
219}
220
221our $searchtext = $cgi->param('s');
222if (defined $searchtext) {
223 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
224 die_error(undef, "Invalid search parameter");
225 }
226 $searchtext = quotemeta $searchtext;
227}
228
229# dispatch
230my %actions = (
231 "blame" => \&git_blame2,
232 "blobdiff" => \&git_blobdiff,
233 "blobdiff_plain" => \&git_blobdiff_plain,
234 "blob" => \&git_blob,
235 "blob_plain" => \&git_blob_plain,
236 "commitdiff" => \&git_commitdiff,
237 "commitdiff_plain" => \&git_commitdiff_plain,
238 "commit" => \&git_commit,
239 "heads" => \&git_heads,
240 "history" => \&git_history,
241 "log" => \&git_log,
242 "rss" => \&git_rss,
243 "search" => \&git_search,
244 "shortlog" => \&git_shortlog,
245 "summary" => \&git_summary,
246 "tag" => \&git_tag,
247 "tags" => \&git_tags,
248 "tree" => \&git_tree,
249 "snapshot" => \&git_snapshot,
250 # those below don't need $project
251 "opml" => \&git_opml,
252 "project_list" => \&git_project_list,
253);
254
255if (defined $project) {
256 $action ||= 'summary';
257} else {
258 $action ||= 'project_list';
259}
260if (!defined($actions{$action})) {
261 die_error(undef, "Unknown action");
262}
263$actions{$action}->();
264exit;
265
266## ======================================================================
267## action links
268
269sub href(%) {
270 my %params = @_;
271
272 my @mapping = (
273 action => "a",
274 project => "p",
275 file_name => "f",
276 file_parent => "fp",
277 hash => "h",
278 hash_parent => "hp",
279 hash_base => "hb",
280 page => "pg",
281 searchtext => "s",
282 );
283 my %mapping = @mapping;
284
285 $params{"project"} ||= $project;
286
287 my @result = ();
288 for (my $i = 0; $i < @mapping; $i += 2) {
289 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
290 if (defined $params{$name}) {
291 push @result, $symbol . "=" . esc_param($params{$name});
292 }
293 }
294 return "$my_uri?" . join(';', @result);
295}
296
297
298## ======================================================================
299## validation, quoting/unquoting and escaping
300
301sub validate_input {
302 my $input = shift;
303
304 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
305 return $input;
306 }
307 if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
308 return undef;
309 }
310 if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
311 return undef;
312 }
313 return $input;
314}
315
316# quote unsafe chars, but keep the slash, even when it's not
317# correct, but quoted slashes look too horrible in bookmarks
318sub esc_param {
319 my $str = shift;
320 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
321 $str =~ s/\+/%2B/g;
322 $str =~ s/ /\+/g;
323 return $str;
324}
325
326# replace invalid utf8 character with SUBSTITUTION sequence
327sub esc_html {
328 my $str = shift;
329 $str = decode("utf8", $str, Encode::FB_DEFAULT);
330 $str = escapeHTML($str);
331 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
332 return $str;
333}
334
335# git may return quoted and escaped filenames
336sub unquote {
337 my $str = shift;
338 if ($str =~ m/^"(.*)"$/) {
339 $str = $1;
340 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
341 }
342 return $str;
343}
344
345# escape tabs (convert tabs to spaces)
346sub untabify {
347 my $line = shift;
348
349 while ((my $pos = index($line, "\t")) != -1) {
350 if (my $count = (8 - ($pos % 8))) {
351 my $spaces = ' ' x $count;
352 $line =~ s/\t/$spaces/;
353 }
354 }
355
356 return $line;
357}
358
359## ----------------------------------------------------------------------
360## HTML aware string manipulation
361
362sub chop_str {
363 my $str = shift;
364 my $len = shift;
365 my $add_len = shift || 10;
366
367 # allow only $len chars, but don't cut a word if it would fit in $add_len
368 # if it doesn't fit, cut it if it's still longer than the dots we would add
369 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
370 my $body = $1;
371 my $tail = $2;
372 if (length($tail) > 4) {
373 $tail = " ...";
374 $body =~ s/&[^;]*$//; # remove chopped character entities
375 }
376 return "$body$tail";
377}
378
379## ----------------------------------------------------------------------
380## functions returning short strings
381
382# CSS class for given age value (in seconds)
383sub age_class {
384 my $age = shift;
385
386 if ($age < 60*60*2) {
387 return "age0";
388 } elsif ($age < 60*60*24*2) {
389 return "age1";
390 } else {
391 return "age2";
392 }
393}
394
395# convert age in seconds to "nn units ago" string
396sub age_string {
397 my $age = shift;
398 my $age_str;
399
400 if ($age > 60*60*24*365*2) {
401 $age_str = (int $age/60/60/24/365);
402 $age_str .= " years ago";
403 } elsif ($age > 60*60*24*(365/12)*2) {
404 $age_str = int $age/60/60/24/(365/12);
405 $age_str .= " months ago";
406 } elsif ($age > 60*60*24*7*2) {
407 $age_str = int $age/60/60/24/7;
408 $age_str .= " weeks ago";
409 } elsif ($age > 60*60*24*2) {
410 $age_str = int $age/60/60/24;
411 $age_str .= " days ago";
412 } elsif ($age > 60*60*2) {
413 $age_str = int $age/60/60;
414 $age_str .= " hours ago";
415 } elsif ($age > 60*2) {
416 $age_str = int $age/60;
417 $age_str .= " min ago";
418 } elsif ($age > 2) {
419 $age_str = int $age;
420 $age_str .= " sec ago";
421 } else {
422 $age_str .= " right now";
423 }
424 return $age_str;
425}
426
427# convert file mode in octal to symbolic file mode string
428sub mode_str {
429 my $mode = oct shift;
430
431 if (S_ISDIR($mode & S_IFMT)) {
432 return 'drwxr-xr-x';
433 } elsif (S_ISLNK($mode)) {
434 return 'lrwxrwxrwx';
435 } elsif (S_ISREG($mode)) {
436 # git cares only about the executable bit
437 if ($mode & S_IXUSR) {
438 return '-rwxr-xr-x';
439 } else {
440 return '-rw-r--r--';
441 };
442 } else {
443 return '----------';
444 }
445}
446
447# convert file mode in octal to file type string
448sub file_type {
449 my $mode = oct shift;
450
451 if (S_ISDIR($mode & S_IFMT)) {
452 return "directory";
453 } elsif (S_ISLNK($mode)) {
454 return "symlink";
455 } elsif (S_ISREG($mode)) {
456 return "file";
457 } else {
458 return "unknown";
459 }
460}
461
462## ----------------------------------------------------------------------
463## functions returning short HTML fragments, or transforming HTML fragments
464## which don't beling to other sections
465
466# format line of commit message or tag comment
467sub format_log_line_html {
468 my $line = shift;
469
470 $line = esc_html($line);
471 $line =~ s/ / /g;
472 if ($line =~ m/([0-9a-fA-F]{40})/) {
473 my $hash_text = $1;
474 if (git_get_type($hash_text) eq "commit") {
475 my $link =
476 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
477 -class => "text"}, $hash_text);
478 $line =~ s/$hash_text/$link/;
479 }
480 }
481 return $line;
482}
483
484# format marker of refs pointing to given object
485sub format_ref_marker {
486 my ($refs, $id) = @_;
487 my $markers = '';
488
489 if (defined $refs->{$id}) {
490 foreach my $ref (@{$refs->{$id}}) {
491 my ($type, $name) = qw();
492 # e.g. tags/v2.6.11 or heads/next
493 if ($ref =~ m!^(.*?)s?/(.*)$!) {
494 $type = $1;
495 $name = $2;
496 } else {
497 $type = "ref";
498 $name = $ref;
499 }
500
501 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
502 }
503 }
504
505 if ($markers) {
506 return ' <span class="refs">'. $markers . '</span>';
507 } else {
508 return "";
509 }
510}
511
512# format, perhaps shortened and with markers, title line
513sub format_subject_html {
514 my ($long, $short, $href, $extra) = @_;
515 $extra = '' unless defined($extra);
516
517 if (length($short) < length($long)) {
518 return $cgi->a({-href => $href, -class => "list subject",
519 -title => $long},
520 esc_html($short) . $extra);
521 } else {
522 return $cgi->a({-href => $href, -class => "list subject"},
523 esc_html($long) . $extra);
524 }
525}
526
527sub format_diff_line {
528 my $line = shift;
529 my $char = substr($line, 0, 1);
530 my $diff_class = "";
531
532 chomp $line;
533
534 if ($char eq '+') {
535 $diff_class = " add";
536 } elsif ($char eq "-") {
537 $diff_class = " rem";
538 } elsif ($char eq "@") {
539 $diff_class = " chunk_header";
540 } elsif ($char eq "\\") {
541 $diff_class = " incomplete";
542 }
543 $line = untabify($line);
544 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
545}
546
547## ----------------------------------------------------------------------
548## git utility subroutines, invoking git commands
549
550# get HEAD ref of given project as hash
551sub git_get_head_hash {
552 my $project = shift;
553 my $oENV = $ENV{'GIT_DIR'};
554 my $retval = undef;
555 $ENV{'GIT_DIR'} = "$projectroot/$project";
556 if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
557 my $head = <$fd>;
558 close $fd;
559 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
560 $retval = $1;
561 }
562 }
563 if (defined $oENV) {
564 $ENV{'GIT_DIR'} = $oENV;
565 }
566 return $retval;
567}
568
569# get type of given object
570sub git_get_type {
571 my $hash = shift;
572
573 open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
574 my $type = <$fd>;
575 close $fd or return;
576 chomp $type;
577 return $type;
578}
579
580sub git_get_project_config {
581 my ($key, $type) = @_;
582
583 return unless ($key);
584 $key =~ s/^gitweb\.//;
585 return if ($key =~ m/\W/);
586
587 my @x = ($GIT, 'repo-config');
588 if (defined $type) { push @x, $type; }
589 push @x, "--get";
590 push @x, "gitweb.$key";
591 my $val = qx(@x);
592 chomp $val;
593 return ($val);
594}
595
596# get hash of given path at given ref
597sub git_get_hash_by_path {
598 my $base = shift;
599 my $path = shift || return undef;
600
601 my $tree = $base;
602
603 open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
604 or die_error(undef, "Open git-ls-tree failed");
605 my $line = <$fd>;
606 close $fd or return undef;
607
608 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
609 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
610 return $3;
611}
612
613## ......................................................................
614## git utility functions, directly accessing git repository
615
616# assumes that PATH is not symref
617sub git_get_hash_by_ref {
618 my $path = shift;
619
620 open my $fd, "$projectroot/$path" or return undef;
621 my $head = <$fd>;
622 close $fd;
623 chomp $head;
624 if ($head =~ m/^[0-9a-fA-F]{40}$/) {
625 return $head;
626 }
627}
628
629sub git_get_project_description {
630 my $path = shift;
631
632 open my $fd, "$projectroot/$path/description" or return undef;
633 my $descr = <$fd>;
634 close $fd;
635 chomp $descr;
636 return $descr;
637}
638
639sub git_get_project_url_list {
640 my $path = shift;
641
642 open my $fd, "$projectroot/$path/cloneurl" or return undef;
643 my @git_project_url_list = map { chomp; $_ } <$fd>;
644 close $fd;
645
646 return wantarray ? @git_project_url_list : \@git_project_url_list;
647}
648
649sub git_get_projects_list {
650 my @list;
651
652 if (-d $projects_list) {
653 # search in directory
654 my $dir = $projects_list;
655 opendir my ($dh), $dir or return undef;
656 while (my $dir = readdir($dh)) {
657 if (-e "$projectroot/$dir/HEAD") {
658 my $pr = {
659 path => $dir,
660 };
661 push @list, $pr
662 }
663 }
664 closedir($dh);
665 } elsif (-f $projects_list) {
666 # read from file(url-encoded):
667 # 'git%2Fgit.git Linus+Torvalds'
668 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
669 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
670 open my ($fd), $projects_list or return undef;
671 while (my $line = <$fd>) {
672 chomp $line;
673 my ($path, $owner) = split ' ', $line;
674 $path = unescape($path);
675 $owner = unescape($owner);
676 if (!defined $path) {
677 next;
678 }
679 if (-e "$projectroot/$path/HEAD") {
680 my $pr = {
681 path => $path,
682 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
683 };
684 push @list, $pr
685 }
686 }
687 close $fd;
688 }
689 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
690 return @list;
691}
692
693sub git_get_project_owner {
694 my $project = shift;
695 my $owner;
696
697 return undef unless $project;
698
699 # read from file (url-encoded):
700 # 'git%2Fgit.git Linus+Torvalds'
701 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
702 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
703 if (-f $projects_list) {
704 open (my $fd , $projects_list);
705 while (my $line = <$fd>) {
706 chomp $line;
707 my ($pr, $ow) = split ' ', $line;
708 $pr = unescape($pr);
709 $ow = unescape($ow);
710 if ($pr eq $project) {
711 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
712 last;
713 }
714 }
715 close $fd;
716 }
717 if (!defined $owner) {
718 $owner = get_file_owner("$projectroot/$project");
719 }
720
721 return $owner;
722}
723
724sub git_get_references {
725 my $type = shift || "";
726 my %refs;
727 my $fd;
728 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
729 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
730 if (-f "$projectroot/$project/info/refs") {
731 open $fd, "$projectroot/$project/info/refs"
732 or return;
733 } else {
734 open $fd, "-|", $GIT, "ls-remote", "."
735 or return;
736 }
737
738 while (my $line = <$fd>) {
739 chomp $line;
740 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
741 if (defined $refs{$1}) {
742 push @{$refs{$1}}, $2;
743 } else {
744 $refs{$1} = [ $2 ];
745 }
746 }
747 }
748 close $fd or return;
749 return \%refs;
750}
751
752sub git_get_following_references {
753 my $hash = shift || return undef;
754 my $type = shift;
755 my $base = shift || $hash_base || "HEAD";
756
757 my $refs = git_get_references($type);
758 open my $fd, "-|", $GIT, "rev-list", $base
759 or return undef;
760 my @commits = map { chomp; $_ } <$fd>;
761 close $fd
762 or return undef;
763
764 my @reflist;
765 my $lastref;
766
767 foreach my $commit (@commits) {
768 foreach my $ref (@{$refs->{$commit}}) {
769 $lastref = $ref;
770 push @reflist, $lastref;
771 }
772 if ($commit eq $hash) {
773 last;
774 }
775 }
776
777 return wantarray ? @reflist : $lastref;
778}
779
780sub git_get_preceding_references {
781 my $hash = shift || return undef;
782 my $type = shift;
783
784 my $refs = git_get_references($type);
785 open my $fd, "-|", $GIT, "rev-list", $hash
786 or return undef;
787 my @commits = map { chomp; $_ } <$fd>;
788 close $fd
789 or return undef;
790
791 my @reflist;
792
793 foreach my $commit (@commits) {
794 foreach my $ref (@{$refs->{$commit}}) {
795 return $ref unless wantarray;
796 push @reflist, $ref;
797 }
798 }
799
800 return @reflist;
801}
802
803sub git_get_rev_name_tags {
804 my $hash = shift || return undef;
805
806 open my $fd, "-|", $GIT, "name-rev", "--tags", $hash
807 or return;
808 my $name_rev = <$fd>;
809 close $fd;
810
811 if ($name_rev =~ m|^$hash tags/(.*)$|) {
812 return $1;
813 } else {
814 # catches also '$hash undefined' output
815 return undef;
816 }
817}
818
819## ----------------------------------------------------------------------
820## parse to hash functions
821
822sub parse_date {
823 my $epoch = shift;
824 my $tz = shift || "-0000";
825
826 my %date;
827 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
828 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
829 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
830 $date{'hour'} = $hour;
831 $date{'minute'} = $min;
832 $date{'mday'} = $mday;
833 $date{'day'} = $days[$wday];
834 $date{'month'} = $months[$mon];
835 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
836 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
837 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
838 $mday, $months[$mon], $hour ,$min;
839
840 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
841 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
842 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
843 $date{'hour_local'} = $hour;
844 $date{'minute_local'} = $min;
845 $date{'tz_local'} = $tz;
846 return %date;
847}
848
849sub parse_tag {
850 my $tag_id = shift;
851 my %tag;
852 my @comment;
853
854 open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
855 $tag{'id'} = $tag_id;
856 while (my $line = <$fd>) {
857 chomp $line;
858 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
859 $tag{'object'} = $1;
860 } elsif ($line =~ m/^type (.+)$/) {
861 $tag{'type'} = $1;
862 } elsif ($line =~ m/^tag (.+)$/) {
863 $tag{'name'} = $1;
864 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
865 $tag{'author'} = $1;
866 $tag{'epoch'} = $2;
867 $tag{'tz'} = $3;
868 } elsif ($line =~ m/--BEGIN/) {
869 push @comment, $line;
870 last;
871 } elsif ($line eq "") {
872 last;
873 }
874 }
875 push @comment, <$fd>;
876 $tag{'comment'} = \@comment;
877 close $fd or return;
878 if (!defined $tag{'name'}) {
879 return
880 };
881 return %tag
882}
883
884sub parse_commit {
885 my $commit_id = shift;
886 my $commit_text = shift;
887
888 my @commit_lines;
889 my %co;
890
891 if (defined $commit_text) {
892 @commit_lines = @$commit_text;
893 } else {
894 $/ = "\0";
895 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id
896 or return;
897 @commit_lines = split '\n', <$fd>;
898 close $fd or return;
899 $/ = "\n";
900 pop @commit_lines;
901 }
902 my $header = shift @commit_lines;
903 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
904 return;
905 }
906 ($co{'id'}, my @parents) = split ' ', $header;
907 $co{'parents'} = \@parents;
908 $co{'parent'} = $parents[0];
909 while (my $line = shift @commit_lines) {
910 last if $line eq "\n";
911 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
912 $co{'tree'} = $1;
913 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
914 $co{'author'} = $1;
915 $co{'author_epoch'} = $2;
916 $co{'author_tz'} = $3;
917 if ($co{'author'} =~ m/^([^<]+) </) {
918 $co{'author_name'} = $1;
919 } else {
920 $co{'author_name'} = $co{'author'};
921 }
922 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
923 $co{'committer'} = $1;
924 $co{'committer_epoch'} = $2;
925 $co{'committer_tz'} = $3;
926 $co{'committer_name'} = $co{'committer'};
927 $co{'committer_name'} =~ s/ <.*//;
928 }
929 }
930 if (!defined $co{'tree'}) {
931 return;
932 };
933
934 foreach my $title (@commit_lines) {
935 $title =~ s/^ //;
936 if ($title ne "") {
937 $co{'title'} = chop_str($title, 80, 5);
938 # remove leading stuff of merges to make the interesting part visible
939 if (length($title) > 50) {
940 $title =~ s/^Automatic //;
941 $title =~ s/^merge (of|with) /Merge ... /i;
942 if (length($title) > 50) {
943 $title =~ s/(http|rsync):\/\///;
944 }
945 if (length($title) > 50) {
946 $title =~ s/(master|www|rsync)\.//;
947 }
948 if (length($title) > 50) {
949 $title =~ s/kernel.org:?//;
950 }
951 if (length($title) > 50) {
952 $title =~ s/\/pub\/scm//;
953 }
954 }
955 $co{'title_short'} = chop_str($title, 50, 5);
956 last;
957 }
958 }
959 # remove added spaces
960 foreach my $line (@commit_lines) {
961 $line =~ s/^ //;
962 }
963 $co{'comment'} = \@commit_lines;
964
965 my $age = time - $co{'committer_epoch'};
966 $co{'age'} = $age;
967 $co{'age_string'} = age_string($age);
968 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
969 if ($age > 60*60*24*7*2) {
970 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
971 $co{'age_string_age'} = $co{'age_string'};
972 } else {
973 $co{'age_string_date'} = $co{'age_string'};
974 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
975 }
976 return %co;
977}
978
979# parse ref from ref_file, given by ref_id, with given type
980sub parse_ref {
981 my $ref_file = shift;
982 my $ref_id = shift;
983 my $type = shift || git_get_type($ref_id);
984 my %ref_item;
985
986 $ref_item{'type'} = $type;
987 $ref_item{'id'} = $ref_id;
988 $ref_item{'epoch'} = 0;
989 $ref_item{'age'} = "unknown";
990 if ($type eq "tag") {
991 my %tag = parse_tag($ref_id);
992 $ref_item{'comment'} = $tag{'comment'};
993 if ($tag{'type'} eq "commit") {
994 my %co = parse_commit($tag{'object'});
995 $ref_item{'epoch'} = $co{'committer_epoch'};
996 $ref_item{'age'} = $co{'age_string'};
997 } elsif (defined($tag{'epoch'})) {
998 my $age = time - $tag{'epoch'};
999 $ref_item{'epoch'} = $tag{'epoch'};
1000 $ref_item{'age'} = age_string($age);
1001 }
1002 $ref_item{'reftype'} = $tag{'type'};
1003 $ref_item{'name'} = $tag{'name'};
1004 $ref_item{'refid'} = $tag{'object'};
1005 } elsif ($type eq "commit"){
1006 my %co = parse_commit($ref_id);
1007 $ref_item{'reftype'} = "commit";
1008 $ref_item{'name'} = $ref_file;
1009 $ref_item{'title'} = $co{'title'};
1010 $ref_item{'refid'} = $ref_id;
1011 $ref_item{'epoch'} = $co{'committer_epoch'};
1012 $ref_item{'age'} = $co{'age_string'};
1013 } else {
1014 $ref_item{'reftype'} = $type;
1015 $ref_item{'name'} = $ref_file;
1016 $ref_item{'refid'} = $ref_id;
1017 }
1018
1019 return %ref_item;
1020}
1021
1022# parse line of git-diff-tree "raw" output
1023sub parse_difftree_raw_line {
1024 my $line = shift;
1025 my %res;
1026
1027 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1028 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1029 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1030 $res{'from_mode'} = $1;
1031 $res{'to_mode'} = $2;
1032 $res{'from_id'} = $3;
1033 $res{'to_id'} = $4;
1034 $res{'status'} = $5;
1035 $res{'similarity'} = $6;
1036 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1037 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1038 } else {
1039 $res{'file'} = unquote($7);
1040 }
1041 }
1042 # 'c512b523472485aef4fff9e57b229d9d243c967f'
1043 #elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1044 # $res{'commit'} = $1;
1045 #}
1046
1047 return wantarray ? %res : \%res;
1048}
1049
1050## ......................................................................
1051## parse to array of hashes functions
1052
1053sub git_get_refs_list {
1054 my $ref_dir = shift;
1055 my @reflist;
1056
1057 my @refs;
1058 my $pfxlen = length("$projectroot/$project/$ref_dir");
1059 File::Find::find(sub {
1060 return if (/^\./);
1061 if (-f $_) {
1062 push @refs, substr($File::Find::name, $pfxlen + 1);
1063 }
1064 }, "$projectroot/$project/$ref_dir");
1065
1066 foreach my $ref_file (@refs) {
1067 my $ref_id = git_get_hash_by_ref("$project/$ref_dir/$ref_file");
1068 my $type = git_get_type($ref_id) || next;
1069 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1070
1071 push @reflist, \%ref_item;
1072 }
1073 # sort refs by age
1074 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1075 return \@reflist;
1076}
1077
1078## ----------------------------------------------------------------------
1079## filesystem-related functions
1080
1081sub get_file_owner {
1082 my $path = shift;
1083
1084 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1085 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1086 if (!defined $gcos) {
1087 return undef;
1088 }
1089 my $owner = $gcos;
1090 $owner =~ s/[,;].*$//;
1091 return decode("utf8", $owner, Encode::FB_DEFAULT);
1092}
1093
1094## ......................................................................
1095## mimetype related functions
1096
1097sub mimetype_guess_file {
1098 my $filename = shift;
1099 my $mimemap = shift;
1100 -r $mimemap or return undef;
1101
1102 my %mimemap;
1103 open(MIME, $mimemap) or return undef;
1104 while (<MIME>) {
1105 next if m/^#/; # skip comments
1106 my ($mime, $exts) = split(/\t+/);
1107 if (defined $exts) {
1108 my @exts = split(/\s+/, $exts);
1109 foreach my $ext (@exts) {
1110 $mimemap{$ext} = $mime;
1111 }
1112 }
1113 }
1114 close(MIME);
1115
1116 $filename =~ /\.(.*?)$/;
1117 return $mimemap{$1};
1118}
1119
1120sub mimetype_guess {
1121 my $filename = shift;
1122 my $mime;
1123 $filename =~ /\./ or return undef;
1124
1125 if ($mimetypes_file) {
1126 my $file = $mimetypes_file;
1127 if ($file !~ m!^/!) { # if it is relative path
1128 # it is relative to project
1129 $file = "$projectroot/$project/$file";
1130 }
1131 $mime = mimetype_guess_file($filename, $file);
1132 }
1133 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1134 return $mime;
1135}
1136
1137sub blob_mimetype {
1138 my $fd = shift;
1139 my $filename = shift;
1140
1141 if ($filename) {
1142 my $mime = mimetype_guess($filename);
1143 $mime and return $mime;
1144 }
1145
1146 # just in case
1147 return $default_blob_plain_mimetype unless $fd;
1148
1149 if (-T $fd) {
1150 return 'text/plain' .
1151 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1152 } elsif (! $filename) {
1153 return 'application/octet-stream';
1154 } elsif ($filename =~ m/\.png$/i) {
1155 return 'image/png';
1156 } elsif ($filename =~ m/\.gif$/i) {
1157 return 'image/gif';
1158 } elsif ($filename =~ m/\.jpe?g$/i) {
1159 return 'image/jpeg';
1160 } else {
1161 return 'application/octet-stream';
1162 }
1163}
1164
1165## ======================================================================
1166## functions printing HTML: header, footer, error page
1167
1168sub git_header_html {
1169 my $status = shift || "200 OK";
1170 my $expires = shift;
1171
1172 my $title = "$site_name git";
1173 if (defined $project) {
1174 $title .= " - $project";
1175 if (defined $action) {
1176 $title .= "/$action";
1177 if (defined $file_name) {
1178 $title .= " - $file_name";
1179 if ($action eq "tree" && $file_name !~ m|/$|) {
1180 $title .= "/";
1181 }
1182 }
1183 }
1184 }
1185 my $content_type;
1186 # require explicit support from the UA if we are to send the page as
1187 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1188 # we have to do this because MSIE sometimes globs '*/*', pretending to
1189 # support xhtml+xml but choking when it gets what it asked for.
1190 if (defined $cgi->http('HTTP_ACCEPT') &&
1191 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1192 $cgi->Accept('application/xhtml+xml') != 0) {
1193 $content_type = 'application/xhtml+xml';
1194 } else {
1195 $content_type = 'text/html';
1196 }
1197 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1198 -status=> $status, -expires => $expires);
1199 print <<EOF;
1200<?xml version="1.0" encoding="utf-8"?>
1201<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1202<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1203<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1204<!-- git core binaries version $git_version -->
1205<head>
1206<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1207<meta name="generator" content="gitweb/$version git/$git_version"/>
1208<meta name="robots" content="index, nofollow"/>
1209<title>$title</title>
1210<link rel="stylesheet" type="text/css" href="$stylesheet"/>
1211EOF
1212 if (defined $project) {
1213 printf('<link rel="alternate" title="%s log" '.
1214 'href="%s" type="application/rss+xml"/>'."\n",
1215 esc_param($project), href(action=>"rss"));
1216 }
1217
1218 print "</head>\n" .
1219 "<body>\n" .
1220 "<div class=\"page_header\">\n" .
1221 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1222 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1223 "</a>\n";
1224 print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
1225 if (defined $project) {
1226 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1227 if (defined $action) {
1228 print " / $action";
1229 }
1230 print "\n";
1231 if (!defined $searchtext) {
1232 $searchtext = "";
1233 }
1234 my $search_hash;
1235 if (defined $hash_base) {
1236 $search_hash = $hash_base;
1237 } elsif (defined $hash) {
1238 $search_hash = $hash;
1239 } else {
1240 $search_hash = "HEAD";
1241 }
1242 $cgi->param("a", "search");
1243 $cgi->param("h", $search_hash);
1244 print $cgi->startform(-method => "get", -action => $my_uri) .
1245 "<div class=\"search\">\n" .
1246 $cgi->hidden(-name => "p") . "\n" .
1247 $cgi->hidden(-name => "a") . "\n" .
1248 $cgi->hidden(-name => "h") . "\n" .
1249 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1250 "</div>" .
1251 $cgi->end_form() . "\n";
1252 }
1253 print "</div>\n";
1254}
1255
1256sub git_footer_html {
1257 print "<div class=\"page_footer\">\n";
1258 if (defined $project) {
1259 my $descr = git_get_project_description($project);
1260 if (defined $descr) {
1261 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1262 }
1263 print $cgi->a({-href => href(action=>"rss"), -class => "rss_logo"}, "RSS") . "\n";
1264 } else {
1265 print $cgi->a({-href => href(action=>"opml"), -class => "rss_logo"}, "OPML") . "\n";
1266 }
1267 print "</div>\n" .
1268 "</body>\n" .
1269 "</html>";
1270}
1271
1272sub die_error {
1273 my $status = shift || "403 Forbidden";
1274 my $error = shift || "Malformed query, file missing or permission denied";
1275
1276 git_header_html($status);
1277 print <<EOF;
1278<div class="page_body">
1279<br /><br />
1280$status - $error
1281<br />
1282</div>
1283EOF
1284 git_footer_html();
1285 exit;
1286}
1287
1288## ----------------------------------------------------------------------
1289## functions printing or outputting HTML: navigation
1290
1291sub git_print_page_nav {
1292 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1293 $extra = '' if !defined $extra; # pager or formats
1294
1295 my @navs = qw(summary shortlog log commit commitdiff tree);
1296 if ($suppress) {
1297 @navs = grep { $_ ne $suppress } @navs;
1298 }
1299
1300 my %arg = map { $_ => {action=>$_} } @navs;
1301 if (defined $head) {
1302 for (qw(commit commitdiff)) {
1303 $arg{$_}{hash} = $head;
1304 }
1305 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1306 for (qw(shortlog log)) {
1307 $arg{$_}{hash} = $head;
1308 }
1309 }
1310 }
1311 $arg{tree}{hash} = $treehead if defined $treehead;
1312 $arg{tree}{hash_base} = $treebase if defined $treebase;
1313
1314 print "<div class=\"page_nav\">\n" .
1315 (join " | ",
1316 map { $_ eq $current ?
1317 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1318 } @navs);
1319 print "<br/>\n$extra<br/>\n" .
1320 "</div>\n";
1321}
1322
1323sub format_paging_nav {
1324 my ($action, $hash, $head, $page, $nrevs) = @_;
1325 my $paging_nav;
1326
1327
1328 if ($hash ne $head || $page) {
1329 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1330 } else {
1331 $paging_nav .= "HEAD";
1332 }
1333
1334 if ($page > 0) {
1335 $paging_nav .= " ⋅ " .
1336 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1337 -accesskey => "p", -title => "Alt-p"}, "prev");
1338 } else {
1339 $paging_nav .= " ⋅ prev";
1340 }
1341
1342 if ($nrevs >= (100 * ($page+1)-1)) {
1343 $paging_nav .= " ⋅ " .
1344 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1345 -accesskey => "n", -title => "Alt-n"}, "next");
1346 } else {
1347 $paging_nav .= " ⋅ next";
1348 }
1349
1350 return $paging_nav;
1351}
1352
1353## ......................................................................
1354## functions printing or outputting HTML: div
1355
1356sub git_print_header_div {
1357 my ($action, $title, $hash, $hash_base) = @_;
1358 my %args = ();
1359
1360 $args{action} = $action;
1361 $args{hash} = $hash if $hash;
1362 $args{hash_base} = $hash_base if $hash_base;
1363
1364 print "<div class=\"header\">\n" .
1365 $cgi->a({-href => href(%args), -class => "title"},
1366 $title ? $title : $action) .
1367 "\n</div>\n";
1368}
1369
1370sub git_print_page_path {
1371 my $name = shift;
1372 my $type = shift;
1373 my $hb = shift;
1374
1375 if (!defined $name) {
1376 print "<div class=\"page_path\">/</div>\n";
1377 } elsif (defined $type && $type eq 'blob') {
1378 print "<div class=\"page_path\">";
1379 if (defined $hb) {
1380 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1381 hash_base=>$hb)},
1382 esc_html($name));
1383 } else {
1384 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name)},
1385 esc_html($name));
1386 }
1387 print "<br/></div>\n";
1388 } else {
1389 print "<div class=\"page_path\">" . esc_html($name) . "<br/></div>\n";
1390 }
1391}
1392
1393sub git_print_log {
1394 my $log = shift;
1395
1396 # remove leading empty lines
1397 while (defined $log->[0] && $log->[0] eq "") {
1398 shift @$log;
1399 }
1400
1401 # print log
1402 my $signoff = 0;
1403 my $empty = 0;
1404 foreach my $line (@$log) {
1405 # print only one empty line
1406 # do not print empty line after signoff
1407 if ($line eq "") {
1408 next if ($empty || $signoff);
1409 $empty = 1;
1410 } else {
1411 $empty = 0;
1412 }
1413 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1414 $signoff = 1;
1415 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1416 } else {
1417 $signoff = 0;
1418 print format_log_line_html($line) . "<br/>\n";
1419 }
1420 }
1421}
1422
1423sub git_print_simplified_log {
1424 my $log = shift;
1425 my $remove_title = shift;
1426
1427 shift @$log if $remove_title;
1428 # remove leading empty lines
1429 while (defined $log->[0] && $log->[0] eq "") {
1430 shift @$log;
1431 }
1432
1433 # simplify and print log
1434 my $empty = 0;
1435 foreach my $line (@$log) {
1436 # remove signoff lines
1437 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1438 next;
1439 }
1440 # print only one empty line
1441 if ($line eq "") {
1442 next if $empty;
1443 $empty = 1;
1444 } else {
1445 $empty = 0;
1446 }
1447 print format_log_line_html($line) . "<br/>\n";
1448 }
1449 # end with single empty line
1450 print "<br/>\n" unless $empty;
1451}
1452
1453## ......................................................................
1454## functions printing large fragments of HTML
1455
1456sub git_difftree_body {
1457 my ($difftree, $hash, $parent) = @_;
1458
1459 print "<div class=\"list_head\">\n";
1460 if ($#{$difftree} > 10) {
1461 print(($#{$difftree} + 1) . " files changed:\n");
1462 }
1463 print "</div>\n";
1464
1465 print "<table class=\"diff_tree\">\n";
1466 my $alternate = 0;
1467 foreach my $line (@{$difftree}) {
1468 my %diff = parse_difftree_raw_line($line);
1469
1470 if ($alternate) {
1471 print "<tr class=\"dark\">\n";
1472 } else {
1473 print "<tr class=\"light\">\n";
1474 }
1475 $alternate ^= 1;
1476
1477 my ($to_mode_oct, $to_mode_str, $to_file_type);
1478 my ($from_mode_oct, $from_mode_str, $from_file_type);
1479 if ($diff{'to_mode'} ne ('0' x 6)) {
1480 $to_mode_oct = oct $diff{'to_mode'};
1481 if (S_ISREG($to_mode_oct)) { # only for regular file
1482 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1483 }
1484 $to_file_type = file_type($diff{'to_mode'});
1485 }
1486 if ($diff{'from_mode'} ne ('0' x 6)) {
1487 $from_mode_oct = oct $diff{'from_mode'};
1488 if (S_ISREG($to_mode_oct)) { # only for regular file
1489 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1490 }
1491 $from_file_type = file_type($diff{'from_mode'});
1492 }
1493
1494 if ($diff{'status'} eq "A") { # created
1495 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1496 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1497 $mode_chng .= "]</span>";
1498 print "<td>" .
1499 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1500 hash_base=>$hash, file_name=>$diff{'file'}),
1501 -class => "list"}, esc_html($diff{'file'})) .
1502 "</td>\n" .
1503 "<td>$mode_chng</td>\n" .
1504 "<td class=\"link\">" .
1505 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1506 hash_base=>$hash, file_name=>$diff{'file'})},
1507 "blob") .
1508 "</td>\n";
1509
1510 } elsif ($diff{'status'} eq "D") { # deleted
1511 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1512 print "<td>" .
1513 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1514 hash_base=>$parent, file_name=>$diff{'file'}),
1515 -class => "list"}, esc_html($diff{'file'})) .
1516 "</td>\n" .
1517 "<td>$mode_chng</td>\n" .
1518 "<td class=\"link\">" .
1519 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1520 hash_base=>$parent, file_name=>$diff{'file'})},
1521 "blob") .
1522 " | " .
1523 $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1524 file_name=>$diff{'file'})},\
1525 "history") .
1526 "</td>\n";
1527
1528 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1529 my $mode_chnge = "";
1530 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1531 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1532 if ($from_file_type != $to_file_type) {
1533 $mode_chnge .= " from $from_file_type to $to_file_type";
1534 }
1535 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1536 if ($from_mode_str && $to_mode_str) {
1537 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1538 } elsif ($to_mode_str) {
1539 $mode_chnge .= " mode: $to_mode_str";
1540 }
1541 }
1542 $mode_chnge .= "]</span>\n";
1543 }
1544 print "<td>";
1545 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1546 print $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1547 hash_base=>$hash, file_name=>$diff{'file'}),
1548 -class => "list"}, esc_html($diff{'file'}));
1549 } else { # only mode changed
1550 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1551 hash_base=>$hash, file_name=>$diff{'file'}),
1552 -class => "list"}, esc_html($diff{'file'}));
1553 }
1554 print "</td>\n" .
1555 "<td>$mode_chnge</td>\n" .
1556 "<td class=\"link\">" .
1557 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1558 hash_base=>$hash, file_name=>$diff{'file'})},
1559 "blob");
1560 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1561 print " | " .
1562 $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1563 hash_base=>$hash, file_name=>$diff{'file'})},
1564 "diff");
1565 }
1566 print " | " .
1567 $cgi->a({-href => href(action=>"history",
1568 hash_base=>$hash, file_name=>$diff{'file'})},
1569 "history");
1570 print "</td>\n";
1571
1572 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1573 my %status_name = ('R' => 'moved', 'C' => 'copied');
1574 my $nstatus = $status_name{$diff{'status'}};
1575 my $mode_chng = "";
1576 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1577 # mode also for directories, so we cannot use $to_mode_str
1578 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1579 }
1580 print "<td>" .
1581 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1582 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1583 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1584 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1585 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1586 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1587 -class => "list"}, esc_html($diff{'from_file'})) .
1588 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1589 "<td class=\"link\">" .
1590 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1591 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1592 "blob");
1593 if ($diff{'to_id'} ne $diff{'from_id'}) {
1594 print " | " .
1595 $cgi->a({-href => href(action=>"blobdiff", hash_base=>$hash,
1596 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1597 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1598 "diff");
1599 }
1600 print "</td>\n";
1601
1602 } # we should not encounter Unmerged (U) or Unknown (X) status
1603 print "</tr>\n";
1604 }
1605 print "</table>\n";
1606}
1607
1608sub git_patchset_body {
1609 my ($fd, $difftree, $hash, $hash_parent) = @_;
1610
1611 my $patch_idx = 0;
1612 my $in_header = 0;
1613 my $patch_found = 0;
1614 my %diffinfo;
1615
1616 print "<div class=\"patchset\">\n";
1617
1618 LINE:
1619 while (my $patch_line @$fd>) {
1620 chomp $patch_line;
1621
1622 if ($patch_line =~ m/^diff /) { # "git diff" header
1623 # beginning of patch (in patchset)
1624 if ($patch_found) {
1625 # close previous patch
1626 print "</div>\n"; # class="patch"
1627 } else {
1628 # first patch in patchset
1629 $patch_found = 1;
1630 }
1631 print "<div class=\"patch\">\n";
1632
1633 %diffinfo = parse_difftree_raw_line($difftree->[$patch_idx++]);
1634
1635 # for now, no extended header, hence we skip empty patches
1636 # companion to next LINE if $in_header;
1637 if ($diffinfo{'from_id'} eq $diffinfo{'to_id'}) { # no change
1638 $in_header = 1;
1639 next LINE;
1640 }
1641
1642 if ($diffinfo{'status'} eq "A") { # added
1643 print "<div class=\"diff_info\">" . file_type($diffinfo{'to_mode'}) . ":" .
1644 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1645 hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'file'})},
1646 $diffinfo{'to_id'}) . "(new)" .
1647 "</div>\n"; # class="diff_info"
1648
1649 } elsif ($diffinfo{'status'} eq "D") { # deleted
1650 print "<div class=\"diff_info\">" . file_type($diffinfo{'from_mode'}) . ":" .
1651 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1652 hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'file'})},
1653 $diffinfo{'from_id'}) . "(deleted)" .
1654 "</div>\n"; # class="diff_info"
1655
1656 } elsif ($diffinfo{'status'} eq "R" || # renamed
1657 $diffinfo{'status'} eq "C") { # copied
1658 print "<div class=\"diff_info\">" .
1659 file_type($diffinfo{'from_mode'}) . ":" .
1660 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1661 hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'from_file'})},
1662 $diffinfo{'from_id'}) .
1663 " -> " .
1664 file_type($diffinfo{'to_mode'}) . ":" .
1665 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1666 hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'to_file'})},
1667 $diffinfo{'to_id'});
1668 print "</div>\n"; # class="diff_info"
1669
1670 } else { # modified, mode changed, ...
1671 print "<div class=\"diff_info\">" .
1672 file_type($diffinfo{'from_mode'}) . ":" .
1673 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1674 hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'file'})},
1675 $diffinfo{'from_id'}) .
1676 " -> " .
1677 file_type($diffinfo{'to_mode'}) . ":" .
1678 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1679 hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'file'})},
1680 $diffinfo{'to_id'});
1681 print "</div>\n"; # class="diff_info"
1682 }
1683
1684 #print "<div class=\"diff extended_header\">\n";
1685 $in_header = 1;
1686 next LINE;
1687 } # start of patch in patchset
1688
1689
1690 if ($in_header && $patch_line =~ m/^---/) {
1691 #print "</div>\n"
1692 $in_header = 0;
1693 }
1694 next LINE if $in_header;
1695
1696 print format_diff_line($patch_line);
1697 }
1698 print "</div>\n" if $patch_found; # class="patch"
1699
1700 print "</div>\n"; # class="patchset"
1701}
1702
1703# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1704
1705sub git_shortlog_body {
1706 # uses global variable $project
1707 my ($revlist, $from, $to, $refs, $extra) = @_;
1708
1709 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1710 my $have_snapshot = (defined $ctype && defined $suffix);
1711
1712 $from = 0 unless defined $from;
1713 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1714
1715 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1716 my $alternate = 0;
1717 for (my $i = $from; $i <= $to; $i++) {
1718 my $commit = $revlist->[$i];
1719 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1720 my $ref = format_ref_marker($refs, $commit);
1721 my %co = parse_commit($commit);
1722 if ($alternate) {
1723 print "<tr class=\"dark\">\n";
1724 } else {
1725 print "<tr class=\"light\">\n";
1726 }
1727 $alternate ^= 1;
1728 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1729 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1730 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1731 "<td>";
1732 print format_subject_html($co{'title'}, $co{'title_short'},
1733 href(action=>"commit", hash=>$commit), $ref);
1734 print "</td>\n" .
1735 "<td class=\"link\">" .
1736 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1737 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1738 if ($have_snapshot) {
1739 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1740 }
1741 print "</td>\n" .
1742 "</tr>\n";
1743 }
1744 if (defined $extra) {
1745 print "<tr>\n" .
1746 "<td colspan=\"4\">$extra</td>\n" .
1747 "</tr>\n";
1748 }
1749 print "</table>\n";
1750}
1751
1752sub git_history_body {
1753 # Warning: assumes constant type (blob or tree) during history
1754 my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
1755
1756 print "<table class=\"history\" cellspacing=\"0\">\n";
1757 my $alternate = 0;
1758 while (my $line = <$fd>) {
1759 if ($line !~ m/^([0-9a-fA-F]{40})/) {
1760 next;
1761 }
1762
1763 my $commit = $1;
1764 my %co = parse_commit($commit);
1765 if (!%co) {
1766 next;
1767 }
1768
1769 my $ref = format_ref_marker($refs, $commit);
1770
1771 if ($alternate) {
1772 print "<tr class=\"dark\">\n";
1773 } else {
1774 print "<tr class=\"light\">\n";
1775 }
1776 $alternate ^= 1;
1777 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1778 # shortlog uses chop_str($co{'author_name'}, 10)
1779 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1780 "<td>";
1781 # originally git_history used chop_str($co{'title'}, 50)
1782 print format_subject_html($co{'title'}, $co{'title_short'},
1783 href(action=>"commit", hash=>$commit), $ref);
1784 print "</td>\n" .
1785 "<td class=\"link\">" .
1786 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1787 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1788 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
1789
1790 if ($ftype eq 'blob') {
1791 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1792 my $blob_parent = git_get_hash_by_path($commit, $file_name);
1793 if (defined $blob_current && defined $blob_parent &&
1794 $blob_current ne $blob_parent) {
1795 print " | " .
1796 $cgi->a({-href => href(action=>"blobdiff", hash=>$blob_current, hash_parent=>$blob_parent,
1797 hash_base=>$commit, file_name=>$file_name)},
1798 "diff to current");
1799 }
1800 }
1801 print "</td>\n" .
1802 "</tr>\n";
1803 }
1804 if (defined $extra) {
1805 print "<tr>\n" .
1806 "<td colspan=\"4\">$extra</td>\n" .
1807 "</tr>\n";
1808 }
1809 print "</table>\n";
1810}
1811
1812sub git_tags_body {
1813 # uses global variable $project
1814 my ($taglist, $from, $to, $extra) = @_;
1815 $from = 0 unless defined $from;
1816 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1817
1818 print "<table class=\"tags\" cellspacing=\"0\">\n";
1819 my $alternate = 0;
1820 for (my $i = $from; $i <= $to; $i++) {
1821 my $entry = $taglist->[$i];
1822 my %tag = %$entry;
1823 my $comment_lines = $tag{'comment'};
1824 my $comment = shift @$comment_lines;
1825 my $comment_short;
1826 if (defined $comment) {
1827 $comment_short = chop_str($comment, 30, 5);
1828 }
1829 if ($alternate) {
1830 print "<tr class=\"dark\">\n";
1831 } else {
1832 print "<tr class=\"light\">\n";
1833 }
1834 $alternate ^= 1;
1835 print "<td><i>$tag{'age'}</i></td>\n" .
1836 "<td>" .
1837 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
1838 -class => "list name"}, esc_html($tag{'name'})) .
1839 "</td>\n" .
1840 "<td>";
1841 if (defined $comment) {
1842 print format_subject_html($comment, $comment_short,
1843 href(action=>"tag", hash=>$tag{'id'}));
1844 }
1845 print "</td>\n" .
1846 "<td class=\"selflink\">";
1847 if ($tag{'type'} eq "tag") {
1848 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
1849 } else {
1850 print " ";
1851 }
1852 print "</td>\n" .
1853 "<td class=\"link\">" . " | " .
1854 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
1855 if ($tag{'reftype'} eq "commit") {
1856 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
1857 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
1858 } elsif ($tag{'reftype'} eq "blob") {
1859 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
1860 }
1861 print "</td>\n" .
1862 "</tr>";
1863 }
1864 if (defined $extra) {
1865 print "<tr>\n" .
1866 "<td colspan=\"5\">$extra</td>\n" .
1867 "</tr>\n";
1868 }
1869 print "</table>\n";
1870}
1871
1872sub git_heads_body {
1873 # uses global variable $project
1874 my ($taglist, $head, $from, $to, $extra) = @_;
1875 $from = 0 unless defined $from;
1876 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1877
1878 print "<table class=\"heads\" cellspacing=\"0\">\n";
1879 my $alternate = 0;
1880 for (my $i = $from; $i <= $to; $i++) {
1881 my $entry = $taglist->[$i];
1882 my %tag = %$entry;
1883 my $curr = $tag{'id'} eq $head;
1884 if ($alternate) {
1885 print "<tr class=\"dark\">\n";
1886 } else {
1887 print "<tr class=\"light\">\n";
1888 }
1889 $alternate ^= 1;
1890 print "<td><i>$tag{'age'}</i></td>\n" .
1891 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1892 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
1893 -class => "list name"},esc_html($tag{'name'})) .
1894 "</td>\n" .
1895 "<td class=\"link\">" .
1896 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
1897 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
1898 "</td>\n" .
1899 "</tr>";
1900 }
1901 if (defined $extra) {
1902 print "<tr>\n" .
1903 "<td colspan=\"3\">$extra</td>\n" .
1904 "</tr>\n";
1905 }
1906 print "</table>\n";
1907}
1908
1909## ----------------------------------------------------------------------
1910## functions printing large fragments, format as one of arguments
1911
1912sub git_diff_print {
1913 my $from = shift;
1914 my $from_name = shift;
1915 my $to = shift;
1916 my $to_name = shift;
1917 my $format = shift || "html";
1918
1919 my $from_tmp = "/dev/null";
1920 my $to_tmp = "/dev/null";
1921 my $pid = $$;
1922
1923 # create tmp from-file
1924 if (defined $from) {
1925 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
1926 open my $fd2, "> $from_tmp";
1927 open my $fd, "-|", $GIT, "cat-file", "blob", $from;
1928 my @file = <$fd>;
1929 print $fd2 @file;
1930 close $fd2;
1931 close $fd;
1932 }
1933
1934 # create tmp to-file
1935 if (defined $to) {
1936 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
1937 open my $fd2, "> $to_tmp";
1938 open my $fd, "-|", $GIT, "cat-file", "blob", $to;
1939 my @file = <$fd>;
1940 print $fd2 @file;
1941 close $fd2;
1942 close $fd;
1943 }
1944
1945 open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
1946 if ($format eq "plain") {
1947 undef $/;
1948 print <$fd>;
1949 $/ = "\n";
1950 } else {
1951 while (my $line = <$fd>) {
1952 chomp $line;
1953 my $char = substr($line, 0, 1);
1954 my $diff_class = "";
1955 if ($char eq '+') {
1956 $diff_class = " add";
1957 } elsif ($char eq "-") {
1958 $diff_class = " rem";
1959 } elsif ($char eq "@") {
1960 $diff_class = " chunk_header";
1961 } elsif ($char eq "\\") {
1962 # skip errors
1963 next;
1964 }
1965 $line = untabify($line);
1966 print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
1967 }
1968 }
1969 close $fd;
1970
1971 if (defined $from) {
1972 unlink($from_tmp);
1973 }
1974 if (defined $to) {
1975 unlink($to_tmp);
1976 }
1977}
1978
1979
1980## ======================================================================
1981## ======================================================================
1982## actions
1983
1984sub git_project_list {
1985 my $order = $cgi->param('o');
1986 if (defined $order && $order !~ m/project|descr|owner|age/) {
1987 die_error(undef, "Unknown order parameter");
1988 }
1989
1990 my @list = git_get_projects_list();
1991 my @projects;
1992 if (!@list) {
1993 die_error(undef, "No projects found");
1994 }
1995 foreach my $pr (@list) {
1996 my $head = git_get_head_hash($pr->{'path'});
1997 if (!defined $head) {
1998 next;
1999 }
2000 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
2001 my %co = parse_commit($head);
2002 if (!%co) {
2003 next;
2004 }
2005 $pr->{'commit'} = \%co;
2006 if (!defined $pr->{'descr'}) {
2007 my $descr = git_get_project_description($pr->{'path'}) || "";
2008 $pr->{'descr'} = chop_str($descr, 25, 5);
2009 }
2010 if (!defined $pr->{'owner'}) {
2011 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2012 }
2013 push @projects, $pr;
2014 }
2015
2016 git_header_html();
2017 if (-f $home_text) {
2018 print "<div class=\"index_include\">\n";
2019 open (my $fd, $home_text);
2020 print <$fd>;
2021 close $fd;
2022 print "</div>\n";
2023 }
2024 print "<table class=\"project_list\">\n" .
2025 "<tr>\n";
2026 $order ||= "project";
2027 if ($order eq "project") {
2028 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2029 print "<th>Project</th>\n";
2030 } else {
2031 print "<th>" .
2032 $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
2033 -class => "header"}, "Project") .
2034 "</th>\n";
2035 }
2036 if ($order eq "descr") {
2037 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2038 print "<th>Description</th>\n";
2039 } else {
2040 print "<th>" .
2041 $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
2042 -class => "header"}, "Description") .
2043 "</th>\n";
2044 }
2045 if ($order eq "owner") {
2046 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2047 print "<th>Owner</th>\n";
2048 } else {
2049 print "<th>" .
2050 $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
2051 -class => "header"}, "Owner") .
2052 "</th>\n";
2053 }
2054 if ($order eq "age") {
2055 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2056 print "<th>Last Change</th>\n";
2057 } else {
2058 print "<th>" .
2059 $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
2060 -class => "header"}, "Last Change") .
2061 "</th>\n";
2062 }
2063 print "<th></th>\n" .
2064 "</tr>\n";
2065 my $alternate = 0;
2066 foreach my $pr (@projects) {
2067 if ($alternate) {
2068 print "<tr class=\"dark\">\n";
2069 } else {
2070 print "<tr class=\"light\">\n";
2071 }
2072 $alternate ^= 1;
2073 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2074 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2075 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2076 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2077 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2078 $pr->{'commit'}{'age_string'} . "</td>\n" .
2079 "<td class=\"link\">" .
2080 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2081 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2082 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
2083 "</td>\n" .
2084 "</tr>\n";
2085 }
2086 print "</table>\n";
2087 git_footer_html();
2088}
2089
2090sub git_summary {
2091 my $descr = git_get_project_description($project) || "none";
2092 my $head = git_get_head_hash($project);
2093 my %co = parse_commit($head);
2094 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2095
2096 my $owner = git_get_project_owner($project);
2097
2098 my $refs = git_get_references();
2099 git_header_html();
2100 git_print_page_nav('summary','', $head);
2101
2102 print "<div class=\"title\"> </div>\n";
2103 print "<table cellspacing=\"0\">\n" .
2104 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2105 "<tr><td>owner</td><td>$owner</td></tr>\n" .
2106 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2107 # use per project git URL list in $projectroot/$project/cloneurl
2108 # or make project git URL from git base URL and project name
2109 my $url_tag = "URL";
2110 my @url_list = git_get_project_url_list($project);
2111 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2112 foreach my $git_url (@url_list) {
2113 next unless $git_url;
2114 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2115 $url_tag = "";
2116 }
2117 print "</table>\n";
2118
2119 open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_get_head_hash($project)
2120 or die_error(undef, "Open git-rev-list failed");
2121 my @revlist = map { chomp; $_ } <$fd>;
2122 close $fd;
2123 git_print_header_div('shortlog');
2124 git_shortlog_body(\@revlist, 0, 15, $refs,
2125 $cgi->a({-href => href(action=>"shortlog")}, "..."));
2126
2127 my $taglist = git_get_refs_list("refs/tags");
2128 if (defined @$taglist) {
2129 git_print_header_div('tags');
2130 git_tags_body($taglist, 0, 15,
2131 $cgi->a({-href => href(action=>"tags")}, "..."));
2132 }
2133
2134 my $headlist = git_get_refs_list("refs/heads");
2135 if (defined @$headlist) {
2136 git_print_header_div('heads');
2137 git_heads_body($headlist, $head, 0, 15,
2138 $cgi->a({-href => href(action=>"heads")}, "..."));
2139 }
2140
2141 git_footer_html();
2142}
2143
2144sub git_tag {
2145 my $head = git_get_head_hash($project);
2146 git_header_html();
2147 git_print_page_nav('','', $head,undef,$head);
2148 my %tag = parse_tag($hash);
2149 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2150 print "<div class=\"title_text\">\n" .
2151 "<table cellspacing=\"0\">\n" .
2152 "<tr>\n" .
2153 "<td>object</td>\n" .
2154 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2155 $tag{'object'}) . "</td>\n" .
2156 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2157 $tag{'type'}) . "</td>\n" .
2158 "</tr>\n";
2159 if (defined($tag{'author'})) {
2160 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2161 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2162 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2163 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2164 "</td></tr>\n";
2165 }
2166 print "</table>\n\n" .
2167 "</div>\n";
2168 print "<div class=\"page_body\">";
2169 my $comment = $tag{'comment'};
2170 foreach my $line (@$comment) {
2171 print esc_html($line) . "<br/>\n";
2172 }
2173 print "</div>\n";
2174 git_footer_html();
2175}
2176
2177sub git_blame2 {
2178 my $fd;
2179 my $ftype;
2180
2181 if (!gitweb_check_feature('blame')) {
2182 die_error('403 Permission denied', "Permission denied");
2183 }
2184 die_error('404 Not Found', "File name not defined") if (!$file_name);
2185 $hash_base ||= git_get_head_hash($project);
2186 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2187 my %co = parse_commit($hash_base)
2188 or die_error(undef, "Reading commit failed");
2189 if (!defined $hash) {
2190 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2191 or die_error(undef, "Error looking up file");
2192 }
2193 $ftype = git_get_type($hash);
2194 if ($ftype !~ "blob") {
2195 die_error("400 Bad Request", "Object is not a blob");
2196 }
2197 open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
2198 or die_error(undef, "Open git-blame failed");
2199 git_header_html();
2200 my $formats_nav =
2201 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2202 "blob") .
2203 " | " .
2204 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2205 "head");
2206 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2207 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2208 git_print_page_path($file_name, $ftype, $hash_base);
2209 my @rev_color = (qw(light2 dark2));
2210 my $num_colors = scalar(@rev_color);
2211 my $current_color = 0;
2212 my $last_rev;
2213 print <<HTML;
2214<div class="page_body">
2215<table class="blame">
2216<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2217HTML
2218 while (<$fd>) {
2219 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2220 my $full_rev = $1;
2221 my $rev = substr($full_rev, 0, 8);
2222 my $lineno = $2;
2223 my $data = $3;
2224
2225 if (!defined $last_rev) {
2226 $last_rev = $full_rev;
2227 } elsif ($last_rev ne $full_rev) {
2228 $last_rev = $full_rev;
2229 $current_color = ++$current_color % $num_colors;
2230 }
2231 print "<tr class=\"$rev_color[$current_color]\">\n";
2232 print "<td class=\"sha1\">" .
2233 $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2234 esc_html($rev)) . "</td>\n";
2235 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2236 esc_html($lineno) . "</a></td>\n";
2237 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2238 print "</tr>\n";
2239 }
2240 print "</table>\n";
2241 print "</div>";
2242 close $fd
2243 or print "Reading blob failed\n";
2244 git_footer_html();
2245}
2246
2247sub git_blame {
2248 my $fd;
2249
2250 if (!gitweb_check_feature('blame')) {
2251 die_error('403 Permission denied', "Permission denied");
2252 }
2253 die_error('404 Not Found', "File name not defined") if (!$file_name);
2254 $hash_base ||= git_get_head_hash($project);
2255 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2256 my %co = parse_commit($hash_base)
2257 or die_error(undef, "Reading commit failed");
2258 if (!defined $hash) {
2259 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2260 or die_error(undef, "Error lookup file");
2261 }
2262 open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2263 or die_error(undef, "Open git-annotate failed");
2264 git_header_html();
2265 my $formats_nav =
2266 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2267 "blob") .
2268 " | " .
2269 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2270 "head");
2271 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2272 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2273 git_print_page_path($file_name, 'blob', $hash_base);
2274 print "<div class=\"page_body\">\n";
2275 print <<HTML;
2276<table class="blame">
2277 <tr>
2278 <th>Commit</th>
2279 <th>Age</th>
2280 <th>Author</th>
2281 <th>Line</th>
2282 <th>Data</th>
2283 </tr>
2284HTML
2285 my @line_class = (qw(light dark));
2286 my $line_class_len = scalar (@line_class);
2287 my $line_class_num = $#line_class;
2288 while (my $line = <$fd>) {
2289 my $long_rev;
2290 my $short_rev;
2291 my $author;
2292 my $time;
2293 my $lineno;
2294 my $data;
2295 my $age;
2296 my $age_str;
2297 my $age_class;
2298
2299 chomp $line;
2300 $line_class_num = ($line_class_num + 1) % $line_class_len;
2301
2302 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
2303 $long_rev = $1;
2304 $author = $2;
2305 $time = $3;
2306 $lineno = $4;
2307 $data = $5;
2308 } else {
2309 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2310 next;
2311 }
2312 $short_rev = substr ($long_rev, 0, 8);
2313 $age = time () - $time;
2314 $age_str = age_string ($age);
2315 $age_str =~ s/ / /g;
2316 $age_class = age_class($age);
2317 $author = esc_html ($author);
2318 $author =~ s/ / /g;
2319
2320 $data = untabify($data);
2321 $data = esc_html ($data);
2322
2323 print <<HTML;
2324 <tr class="$line_class[$line_class_num]">
2325 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2326 <td class="$age_class">$age_str</td>
2327 <td>$author</td>
2328 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2329 <td class="pre">$data</td>
2330 </tr>
2331HTML
2332 } # while (my $line = <$fd>)
2333 print "</table>\n\n";
2334 close $fd
2335 or print "Reading blob failed.\n";
2336 print "</div>";
2337 git_footer_html();
2338}
2339
2340sub git_tags {
2341 my $head = git_get_head_hash($project);
2342 git_header_html();
2343 git_print_page_nav('','', $head,undef,$head);
2344 git_print_header_div('summary', $project);
2345
2346 my $taglist = git_get_refs_list("refs/tags");
2347 if (defined @$taglist) {
2348 git_tags_body($taglist);
2349 }
2350 git_footer_html();
2351}
2352
2353sub git_heads {
2354 my $head = git_get_head_hash($project);
2355 git_header_html();
2356 git_print_page_nav('','', $head,undef,$head);
2357 git_print_header_div('summary', $project);
2358
2359 my $taglist = git_get_refs_list("refs/heads");
2360 if (defined @$taglist) {
2361 git_heads_body($taglist, $head);
2362 }
2363 git_footer_html();
2364}
2365
2366sub git_blob_plain {
2367 if (!defined $hash) {
2368 if (defined $file_name) {
2369 my $base = $hash_base || git_get_head_hash($project);
2370 $hash = git_get_hash_by_path($base, $file_name, "blob")
2371 or die_error(undef, "Error lookup file");
2372 } else {
2373 die_error(undef, "No file name defined");
2374 }
2375 }
2376 my $type = shift;
2377 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2378 or die_error(undef, "Couldn't cat $file_name, $hash");
2379
2380 $type ||= blob_mimetype($fd, $file_name);
2381
2382 # save as filename, even when no $file_name is given
2383 my $save_as = "$hash";
2384 if (defined $file_name) {
2385 $save_as = $file_name;
2386 } elsif ($type =~ m/^text\//) {
2387 $save_as .= '.txt';
2388 }
2389
2390 print $cgi->header(-type => "$type",
2391 -content_disposition => "inline; filename=\"$save_as\"");
2392 undef $/;
2393 binmode STDOUT, ':raw';
2394 print <$fd>;
2395 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2396 $/ = "\n";
2397 close $fd;
2398}
2399
2400sub git_blob {
2401 if (!defined $hash) {
2402 if (defined $file_name) {
2403 my $base = $hash_base || git_get_head_hash($project);
2404 $hash = git_get_hash_by_path($base, $file_name, "blob")
2405 or die_error(undef, "Error lookup file");
2406 } else {
2407 die_error(undef, "No file name defined");
2408 }
2409 }
2410 my $have_blame = gitweb_check_feature('blame');
2411 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2412 or die_error(undef, "Couldn't cat $file_name, $hash");
2413 my $mimetype = blob_mimetype($fd, $file_name);
2414 if ($mimetype !~ m/^text\//) {
2415 close $fd;
2416 return git_blob_plain($mimetype);
2417 }
2418 git_header_html();
2419 my $formats_nav = '';
2420 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2421 if (defined $file_name) {
2422 if ($have_blame) {
2423 $formats_nav .=
2424 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2425 hash=>$hash, file_name=>$file_name)},
2426 "blame") .
2427 " | ";
2428 }
2429 $formats_nav .=
2430 $cgi->a({-href => href(action=>"blob_plain",
2431 hash=>$hash, file_name=>$file_name)},
2432 "plain") .
2433 " | " .
2434 $cgi->a({-href => href(action=>"blob",
2435 hash_base=>"HEAD", file_name=>$file_name)},
2436 "head");
2437 } else {
2438 $formats_nav .=
2439 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
2440 }
2441 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2442 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2443 } else {
2444 print "<div class=\"page_nav\">\n" .
2445 "<br/><br/></div>\n" .
2446 "<div class=\"title\">$hash</div>\n";
2447 }
2448 git_print_page_path($file_name, "blob", $hash_base);
2449 print "<div class=\"page_body\">\n";
2450 my $nr;
2451 while (my $line = <$fd>) {
2452 chomp $line;
2453 $nr++;
2454 $line = untabify($line);
2455 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2456 $nr, $nr, $nr, esc_html($line);
2457 }
2458 close $fd
2459 or print "Reading blob failed.\n";
2460 print "</div>";
2461 git_footer_html();
2462}
2463
2464sub git_tree {
2465 if (!defined $hash) {
2466 $hash = git_get_head_hash($project);
2467 if (defined $file_name) {
2468 my $base = $hash_base || $hash;
2469 $hash = git_get_hash_by_path($base, $file_name, "tree");
2470 }
2471 if (!defined $hash_base) {
2472 $hash_base = $hash;
2473 }
2474 }
2475 $/ = "\0";
2476 open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
2477 or die_error(undef, "Open git-ls-tree failed");
2478 my @entries = map { chomp; $_ } <$fd>;
2479 close $fd or die_error(undef, "Reading tree failed");
2480 $/ = "\n";
2481
2482 my $refs = git_get_references();
2483 my $ref = format_ref_marker($refs, $hash_base);
2484 git_header_html();
2485 my %base_key = ();
2486 my $base = "";
2487 my $have_blame = gitweb_check_feature('blame');
2488 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2489 $base_key{hash_base} = $hash_base;
2490 git_print_page_nav('tree','', $hash_base);
2491 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2492 } else {
2493 print "<div class=\"page_nav\">\n";
2494 print "<br/><br/></div>\n";
2495 print "<div class=\"title\">$hash</div>\n";
2496 }
2497 if (defined $file_name) {
2498 $base = esc_html("$file_name/");
2499 }
2500 git_print_page_path($file_name, 'tree', $hash_base);
2501 print "<div class=\"page_body\">\n";
2502 print "<table cellspacing=\"0\">\n";
2503 my $alternate = 0;
2504 foreach my $line (@entries) {
2505 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2506 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
2507 my $t_mode = $1;
2508 my $t_type = $2;
2509 my $t_hash = $3;
2510 my $t_name = validate_input($4);
2511 if ($alternate) {
2512 print "<tr class=\"dark\">\n";
2513 } else {
2514 print "<tr class=\"light\">\n";
2515 }
2516 $alternate ^= 1;
2517 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
2518 if ($t_type eq "blob") {
2519 print "<td class=\"list\">" .
2520 $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key),
2521 -class => "list"}, esc_html($t_name)) .
2522 "</td>\n" .
2523 "<td class=\"link\">" .
2524 $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2525 "blob");
2526 if ($have_blame) {
2527 print " | " .
2528 $cgi->a({-href => href(action=>"blame", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2529 "blame");
2530 }
2531 print " | " .
2532 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2533 hash=>$t_hash, file_name=>"$base$t_name")},
2534 "history") .
2535 " | " .
2536 $cgi->a({-href => href(action=>"blob_plain",
2537 hash=>$t_hash, file_name=>"$base$t_name")},
2538 "raw") .
2539 "</td>\n";
2540 } elsif ($t_type eq "tree") {
2541 print "<td class=\"list\">" .
2542 $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2543 esc_html($t_name)) .
2544 "</td>\n" .
2545 "<td class=\"link\">" .
2546 $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2547 "tree") .
2548 " | " .
2549 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, file_name=>"$base$t_name")},
2550 "history") .
2551 "</td>\n";
2552 }
2553 print "</tr>\n";
2554 }
2555 print "</table>\n" .
2556 "</div>";
2557 git_footer_html();
2558}
2559
2560sub git_snapshot {
2561
2562 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2563 my $have_snapshot = (defined $ctype && defined $suffix);
2564 if (!$have_snapshot) {
2565 die_error('403 Permission denied', "Permission denied");
2566 }
2567
2568 if (!defined $hash) {
2569 $hash = git_get_head_hash($project);
2570 }
2571
2572 my $filename = basename($project) . "-$hash.tar.$suffix";
2573
2574 print $cgi->header(-type => 'application/x-tar',
2575 -content_encoding => $ctype,
2576 -content_disposition => "inline; filename=\"$filename\"",
2577 -status => '200 OK');
2578
2579 open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | $command" or
2580 die_error(undef, "Execute git-tar-tree failed.");
2581 binmode STDOUT, ':raw';
2582 print <$fd>;
2583 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2584 close $fd;
2585
2586}
2587
2588sub git_log {
2589 my $head = git_get_head_hash($project);
2590 if (!defined $hash) {
2591 $hash = $head;
2592 }
2593 if (!defined $page) {
2594 $page = 0;
2595 }
2596 my $refs = git_get_references();
2597
2598 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2599 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2600 or die_error(undef, "Open git-rev-list failed");
2601 my @revlist = map { chomp; $_ } <$fd>;
2602 close $fd;
2603
2604 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2605
2606 git_header_html();
2607 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2608
2609 if (!@revlist) {
2610 my %co = parse_commit($hash);
2611
2612 git_print_header_div('summary', $project);
2613 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2614 }
2615 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2616 my $commit = $revlist[$i];
2617 my $ref = format_ref_marker($refs, $commit);
2618 my %co = parse_commit($commit);
2619 next if !%co;
2620 my %ad = parse_date($co{'author_epoch'});
2621 git_print_header_div('commit',
2622 "<span class=\"age\">$co{'age_string'}</span>" .
2623 esc_html($co{'title'}) . $ref,
2624 $commit);
2625 print "<div class=\"title_text\">\n" .
2626 "<div class=\"log_link\">\n" .
2627 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2628 " | " .
2629 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2630 "<br/>\n" .
2631 "</div>\n" .
2632 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
2633 "</div>\n";
2634
2635 print "<div class=\"log_body\">\n";
2636 git_print_simplified_log($co{'comment'});
2637 print "</div>\n";
2638 }
2639 git_footer_html();
2640}
2641
2642sub git_commit {
2643 my %co = parse_commit($hash);
2644 if (!%co) {
2645 die_error(undef, "Unknown commit object");
2646 }
2647 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2648 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2649
2650 my $parent = $co{'parent'};
2651 if (!defined $parent) {
2652 $parent = "--root";
2653 }
2654 open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
2655 or die_error(undef, "Open git-diff-tree failed");
2656 my @difftree = map { chomp; $_ } <$fd>;
2657 close $fd or die_error(undef, "Reading git-diff-tree failed");
2658
2659 # non-textual hash id's can be cached
2660 my $expires;
2661 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2662 $expires = "+1d";
2663 }
2664 my $refs = git_get_references();
2665 my $ref = format_ref_marker($refs, $co{'id'});
2666
2667 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2668 my $have_snapshot = (defined $ctype && defined $suffix);
2669
2670 my $formats_nav = '';
2671 if (defined $file_name && defined $co{'parent'}) {
2672 my $parent = $co{'parent'};
2673 $formats_nav .=
2674 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2675 "blame");
2676 }
2677 git_header_html(undef, $expires);
2678 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2679 $hash, $co{'tree'}, $hash,
2680 $formats_nav);
2681
2682 if (defined $co{'parent'}) {
2683 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2684 } else {
2685 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2686 }
2687 print "<div class=\"title_text\">\n" .
2688 "<table cellspacing=\"0\">\n";
2689 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2690 "<tr>" .
2691 "<td></td><td> $ad{'rfc2822'}";
2692 if ($ad{'hour_local'} < 6) {
2693 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2694 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2695 } else {
2696 printf(" (%02d:%02d %s)",
2697 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2698 }
2699 print "</td>" .
2700 "</tr>\n";
2701 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2702 print "<tr><td></td><td> $cd{'rfc2822'}" .
2703 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2704 "</td></tr>\n";
2705 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2706 print "<tr>" .
2707 "<td>tree</td>" .
2708 "<td class=\"sha1\">" .
2709 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2710 class => "list"}, $co{'tree'}) .
2711 "</td>" .
2712 "<td class=\"link\">" .
2713 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2714 "tree");
2715 if ($have_snapshot) {
2716 print " | " .
2717 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2718 }
2719 print "</td>" .
2720 "</tr>\n";
2721 my $parents = $co{'parents'};
2722 foreach my $par (@$parents) {
2723 print "<tr>" .
2724 "<td>parent</td>" .
2725 "<td class=\"sha1\">" .
2726 $cgi->a({-href => href(action=>"commit", hash=>$par),
2727 class => "list"}, $par) .
2728 "</td>" .
2729 "<td class=\"link\">" .
2730 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2731 " | " .
2732 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "commitdiff") .
2733 "</td>" .
2734 "</tr>\n";
2735 }
2736 print "</table>".
2737 "</div>\n";
2738
2739 print "<div class=\"page_body\">\n";
2740 git_print_log($co{'comment'});
2741 print "</div>\n";
2742
2743 git_difftree_body(\@difftree, $hash, $parent);
2744
2745 git_footer_html();
2746}
2747
2748sub git_blobdiff {
2749 mkdir($git_temp, 0700);
2750 git_header_html();
2751 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2752 my $formats_nav =
2753 $cgi->a({-href => href(action=>"blobdiff_plain",
2754 hash=>$hash, hash_parent=>$hash_parent)},
2755 "plain");
2756 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2757 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2758 } else {
2759 print <<HTML;
2760<div class="page_nav"><br/><br/></div>
2761<div class="title">$hash vs $hash_parent</div>
2762HTML
2763 }
2764 git_print_page_path($file_name, "blob", $hash_base);
2765 print "<div class=\"page_body\">\n" .
2766 "<div class=\"diff_info\">blob:" .
2767 $cgi->a({-href => href(action=>"blob", hash=>$hash_parent,
2768 hash_base=>$hash_base, file_name=>($file_parent || $file_name))},
2769 $hash_parent) .
2770 " -> blob:" .
2771 $cgi->a({-href => href(action=>"blob", hash=>$hash,
2772 hash_base=>$hash_base, file_name=>$file_name)},
2773 $hash) .
2774 "</div>\n";
2775 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2776 print "</div>"; # page_body
2777 git_footer_html();
2778}
2779
2780sub git_blobdiff_plain {
2781 mkdir($git_temp, 0700);
2782 print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2783 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2784}
2785
2786sub git_commitdiff {
2787 my $format = shift || 'html';
2788 my %co = parse_commit($hash);
2789 if (!%co) {
2790 die_error(undef, "Unknown commit object");
2791 }
2792 if (!defined $hash_parent) {
2793 $hash_parent = $co{'parent'} || '--root';
2794 }
2795
2796 # read commitdiff
2797 my $fd;
2798 my @difftree;
2799 if ($format eq 'html') {
2800 open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C',
2801 "--patch-with-raw", "--full-index", $hash_parent, $hash
2802 or die_error(undef, "Open git-diff-tree failed");
2803
2804 while (chomp(my $line = <$fd>)) {
2805 # empty line ends raw part of diff-tree output
2806 last unless $line;
2807 push @difftree, $line;
2808 }
2809
2810 } elsif ($format eq 'plain') {
2811 open $fd, "-|", $GIT, "diff-tree", '-r', '-p', '-B', $hash_parent, $hash
2812 or die_error(undef, "Open git-diff-tree failed");
2813
2814 } else {
2815 die_error(undef, "Unknown commitdiff format");
2816 }
2817
2818 # non-textual hash id's can be cached
2819 my $expires;
2820 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2821 $expires = "+1d";
2822 }
2823
2824 # write commit message
2825 if ($format eq 'html') {
2826 my $refs = git_get_references();
2827 my $ref = format_ref_marker($refs, $co{'id'});
2828 my $formats_nav =
2829 $cgi->a({-href => href(action=>"commitdiff_plain",
2830 hash=>$hash, hash_parent=>$hash_parent)},
2831 "plain");
2832
2833 git_header_html(undef, $expires);
2834 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2835 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2836 print "<div class=\"page_body\">\n";
2837 print "<div class=\"log\">\n";
2838 git_print_simplified_log($co{'comment'}, 1); # skip title
2839 print "</div>\n"; # class="log"
2840
2841 } elsif ($format eq 'plain') {
2842 my $refs = git_get_references("tags");
2843 my $tagname = git_get_rev_name_tags($hash);
2844 my $filename = basename($project) . "-$hash.patch";
2845
2846 print $cgi->header(
2847 -type => 'text/plain',
2848 -charset => 'utf-8',
2849 -expires => $expires,
2850 -content_disposition => qq(inline; filename="$filename"));
2851 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2852 print <<TEXT;
2853From: $co{'author'}
2854Date: $ad{'rfc2822'} ($ad{'tz_local'})
2855Subject: $co{'title'}
2856TEXT
2857 print "X-Git-Tag: $tagname\n" if $tagname;
2858 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2859
2860 foreach my $line (@{$co{'comment'}}) {
2861 print "$line\n";
2862 }
2863 print "---\n\n";
2864 }
2865
2866 # write patch
2867 if ($format eq 'html') {
2868 #git_difftree_body(\@difftree, $hash, $hash_parent);
2869 #print "<br/>\n";
2870
2871 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
2872 close $fd;
2873 print "</div>\n"; # class="page_body"
2874 git_footer_html();
2875
2876 } elsif ($format eq 'plain') {
2877 local $/ = undef;
2878 print <$fd>;
2879 close $fd
2880 or print "Reading git-diff-tree failed\n";
2881 }
2882}
2883
2884sub git_commitdiff_plain {
2885 git_commitdiff('plain');
2886}
2887
2888sub git_history {
2889 if (!defined $hash_base) {
2890 $hash_base = git_get_head_hash($project);
2891 }
2892 my $ftype;
2893 my %co = parse_commit($hash_base);
2894 if (!%co) {
2895 die_error(undef, "Unknown commit object");
2896 }
2897 my $refs = git_get_references();
2898 git_header_html();
2899 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base);
2900 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2901 if (!defined $hash && defined $file_name) {
2902 $hash = git_get_hash_by_path($hash_base, $file_name);
2903 }
2904 if (defined $hash) {
2905 $ftype = git_get_type($hash);
2906 }
2907 git_print_page_path($file_name, $ftype, $hash_base);
2908
2909 open my $fd, "-|",
2910 $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
2911 git_history_body($fd, $refs, $hash_base, $ftype);
2912
2913 close $fd;
2914 git_footer_html();
2915}
2916
2917sub git_search {
2918 if (!defined $searchtext) {
2919 die_error(undef, "Text field empty");
2920 }
2921 if (!defined $hash) {
2922 $hash = git_get_head_hash($project);
2923 }
2924 my %co = parse_commit($hash);
2925 if (!%co) {
2926 die_error(undef, "Unknown commit object");
2927 }
2928 # pickaxe may take all resources of your box and run for several minutes
2929 # with every query - so decide by yourself how public you make this feature :)
2930 my $commit_search = 1;
2931 my $author_search = 0;
2932 my $committer_search = 0;
2933 my $pickaxe_search = 0;
2934 if ($searchtext =~ s/^author\\://i) {
2935 $author_search = 1;
2936 } elsif ($searchtext =~ s/^committer\\://i) {
2937 $committer_search = 1;
2938 } elsif ($searchtext =~ s/^pickaxe\\://i) {
2939 $commit_search = 0;
2940 $pickaxe_search = 1;
2941 }
2942 git_header_html();
2943 git_print_page_nav('','', $hash,$co{'tree'},$hash);
2944 git_print_header_div('commit', esc_html($co{'title'}), $hash);
2945
2946 print "<table cellspacing=\"0\">\n";
2947 my $alternate = 0;
2948 if ($commit_search) {
2949 $/ = "\0";
2950 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
2951 while (my $commit_text = <$fd>) {
2952 if (!grep m/$searchtext/i, $commit_text) {
2953 next;
2954 }
2955 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2956 next;
2957 }
2958 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2959 next;
2960 }
2961 my @commit_lines = split "\n", $commit_text;
2962 my %co = parse_commit(undef, \@commit_lines);
2963 if (!%co) {
2964 next;
2965 }
2966 if ($alternate) {
2967 print "<tr class=\"dark\">\n";
2968 } else {
2969 print "<tr class=\"light\">\n";
2970 }
2971 $alternate ^= 1;
2972 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2973 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2974 "<td>" .
2975 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
2976 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2977 my $comment = $co{'comment'};
2978 foreach my $line (@$comment) {
2979 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2980 my $lead = esc_html($1) || "";
2981 $lead = chop_str($lead, 30, 10);
2982 my $match = esc_html($2) || "";
2983 my $trail = esc_html($3) || "";
2984 $trail = chop_str($trail, 30, 10);
2985 my $text = "$lead<span class=\"match\">$match</span>$trail";
2986 print chop_str($text, 80, 5) . "<br/>\n";
2987 }
2988 }
2989 print "</td>\n" .
2990 "<td class=\"link\">" .
2991 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2992 " | " .
2993 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2994 print "</td>\n" .
2995 "</tr>\n";
2996 }
2997 close $fd;
2998 }
2999
3000 if ($pickaxe_search) {
3001 $/ = "\n";
3002 open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
3003 undef %co;
3004 my @files;
3005 while (my $line = <$fd>) {
3006 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3007 my %set;
3008 $set{'file'} = $6;
3009 $set{'from_id'} = $3;
3010 $set{'to_id'} = $4;
3011 $set{'id'} = $set{'to_id'};
3012 if ($set{'id'} =~ m/0{40}/) {
3013 $set{'id'} = $set{'from_id'};
3014 }
3015 if ($set{'id'} =~ m/0{40}/) {
3016 next;
3017 }
3018 push @files, \%set;
3019 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3020 if (%co) {
3021 if ($alternate) {
3022 print "<tr class=\"dark\">\n";
3023 } else {
3024 print "<tr class=\"light\">\n";
3025 }
3026 $alternate ^= 1;
3027 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3028 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3029 "<td>" .
3030 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3031 -class => "list subject"},
3032 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3033 while (my $setref = shift @files) {
3034 my %set = %$setref;
3035 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3036 hash=>$set{'id'}, file_name=>$set{'file'}),
3037 -class => "list"},
3038 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3039 "<br/>\n";
3040 }
3041 print "</td>\n" .
3042 "<td class=\"link\">" .
3043 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3044 " | " .
3045 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3046 print "</td>\n" .
3047 "</tr>\n";
3048 }
3049 %co = parse_commit($1);
3050 }
3051 }
3052 close $fd;
3053 }
3054 print "</table>\n";
3055 git_footer_html();
3056}
3057
3058sub git_shortlog {
3059 my $head = git_get_head_hash($project);
3060 if (!defined $hash) {
3061 $hash = $head;
3062 }
3063 if (!defined $page) {
3064 $page = 0;
3065 }
3066 my $refs = git_get_references();
3067
3068 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3069 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
3070 or die_error(undef, "Open git-rev-list failed");
3071 my @revlist = map { chomp; $_ } <$fd>;
3072 close $fd;
3073
3074 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3075 my $next_link = '';
3076 if ($#revlist >= (100 * ($page+1)-1)) {
3077 $next_link =
3078 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3079 -title => "Alt-n"}, "next");
3080 }
3081
3082
3083 git_header_html();
3084 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3085 git_print_header_div('summary', $project);
3086
3087 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3088
3089 git_footer_html();
3090}
3091
3092## ......................................................................
3093## feeds (RSS, OPML)
3094
3095sub git_rss {
3096 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3097 open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_get_head_hash($project)
3098 or die_error(undef, "Open git-rev-list failed");
3099 my @revlist = map { chomp; $_ } <$fd>;
3100 close $fd or die_error(undef, "Reading git-rev-list failed");
3101 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3102 print <<XML;
3103<?xml version="1.0" encoding="utf-8"?>
3104<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3105<channel>
3106<title>$project $my_uri $my_url</title>
3107<link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3108<description>$project log</description>
3109<language>en</language>
3110XML
3111
3112 for (my $i = 0; $i <= $#revlist; $i++) {
3113 my $commit = $revlist[$i];
3114 my %co = parse_commit($commit);
3115 # we read 150, we always show 30 and the ones more recent than 48 hours
3116 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3117 last;
3118 }
3119 my %cd = parse_date($co{'committer_epoch'});
3120 open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
3121 my @difftree = map { chomp; $_ } <$fd>;
3122 close $fd or next;
3123 print "<item>\n" .
3124 "<title>" .
3125 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3126 "</title>\n" .
3127 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3128 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3129 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3130 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3131 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3132 "<content:encoded>" .
3133 "<![CDATA[\n";
3134 my $comment = $co{'comment'};
3135 foreach my $line (@$comment) {
3136 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3137 print "$line<br/>\n";
3138 }
3139 print "<br/>\n";
3140 foreach my $line (@difftree) {
3141 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3142 next;
3143 }
3144 my $file = validate_input(unquote($7));
3145 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3146 print "$file<br/>\n";
3147 }
3148 print "]]>\n" .
3149 "</content:encoded>\n" .
3150 "</item>\n";
3151 }
3152 print "</channel></rss>";
3153}
3154
3155sub git_opml {
3156 my @list = git_get_projects_list();
3157
3158 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3159 print <<XML;
3160<?xml version="1.0" encoding="utf-8"?>
3161<opml version="1.0">
3162<head>
3163 <title>$site_name Git OPML Export</title>
3164</head>
3165<body>
3166<outline text="git RSS feeds">
3167XML
3168
3169 foreach my $pr (@list) {
3170 my %proj = %$pr;
3171 my $head = git_get_head_hash($proj{'path'});
3172 if (!defined $head) {
3173 next;
3174 }
3175 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
3176 my %co = parse_commit($head);
3177 if (!%co) {
3178 next;
3179 }
3180
3181 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3182 my $rss = "$my_url?p=$proj{'path'};a=rss";
3183 my $html = "$my_url?p=$proj{'path'};a=summary";
3184 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3185 }
3186 print <<XML;
3187</outline>
3188</body>
3189</opml>
3190XML
3191}