50083e3011d145a25c7c56d85d75dd04ef15adf3
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
527## ----------------------------------------------------------------------
528## git utility subroutines, invoking git commands
529
530# get HEAD ref of given project as hash
531sub git_get_head_hash {
532 my $project = shift;
533 my $oENV = $ENV{'GIT_DIR'};
534 my $retval = undef;
535 $ENV{'GIT_DIR'} = "$projectroot/$project";
536 if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
537 my $head = <$fd>;
538 close $fd;
539 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
540 $retval = $1;
541 }
542 }
543 if (defined $oENV) {
544 $ENV{'GIT_DIR'} = $oENV;
545 }
546 return $retval;
547}
548
549# get type of given object
550sub git_get_type {
551 my $hash = shift;
552
553 open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
554 my $type = <$fd>;
555 close $fd or return;
556 chomp $type;
557 return $type;
558}
559
560sub git_get_project_config {
561 my ($key, $type) = @_;
562
563 return unless ($key);
564 $key =~ s/^gitweb\.//;
565 return if ($key =~ m/\W/);
566
567 my @x = ($GIT, 'repo-config');
568 if (defined $type) { push @x, $type; }
569 push @x, "--get";
570 push @x, "gitweb.$key";
571 my $val = qx(@x);
572 chomp $val;
573 return ($val);
574}
575
576# get hash of given path at given ref
577sub git_get_hash_by_path {
578 my $base = shift;
579 my $path = shift || return undef;
580
581 my $tree = $base;
582
583 open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
584 or die_error(undef, "Open git-ls-tree failed");
585 my $line = <$fd>;
586 close $fd or return undef;
587
588 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
589 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
590 return $3;
591}
592
593## ......................................................................
594## git utility functions, directly accessing git repository
595
596# assumes that PATH is not symref
597sub git_get_hash_by_ref {
598 my $path = shift;
599
600 open my $fd, "$projectroot/$path" or return undef;
601 my $head = <$fd>;
602 close $fd;
603 chomp $head;
604 if ($head =~ m/^[0-9a-fA-F]{40}$/) {
605 return $head;
606 }
607}
608
609sub git_get_project_description {
610 my $path = shift;
611
612 open my $fd, "$projectroot/$path/description" or return undef;
613 my $descr = <$fd>;
614 close $fd;
615 chomp $descr;
616 return $descr;
617}
618
619sub git_get_project_url_list {
620 my $path = shift;
621
622 open my $fd, "$projectroot/$path/cloneurl" or return undef;
623 my @git_project_url_list = map { chomp; $_ } <$fd>;
624 close $fd;
625
626 return wantarray ? @git_project_url_list : \@git_project_url_list;
627}
628
629sub git_get_projects_list {
630 my @list;
631
632 if (-d $projects_list) {
633 # search in directory
634 my $dir = $projects_list;
635 opendir my ($dh), $dir or return undef;
636 while (my $dir = readdir($dh)) {
637 if (-e "$projectroot/$dir/HEAD") {
638 my $pr = {
639 path => $dir,
640 };
641 push @list, $pr
642 }
643 }
644 closedir($dh);
645 } elsif (-f $projects_list) {
646 # read from file(url-encoded):
647 # 'git%2Fgit.git Linus+Torvalds'
648 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
649 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
650 open my ($fd), $projects_list or return undef;
651 while (my $line = <$fd>) {
652 chomp $line;
653 my ($path, $owner) = split ' ', $line;
654 $path = unescape($path);
655 $owner = unescape($owner);
656 if (!defined $path) {
657 next;
658 }
659 if (-e "$projectroot/$path/HEAD") {
660 my $pr = {
661 path => $path,
662 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
663 };
664 push @list, $pr
665 }
666 }
667 close $fd;
668 }
669 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
670 return @list;
671}
672
673sub git_get_project_owner {
674 my $project = shift;
675 my $owner;
676
677 return undef unless $project;
678
679 # read from file (url-encoded):
680 # 'git%2Fgit.git Linus+Torvalds'
681 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
682 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
683 if (-f $projects_list) {
684 open (my $fd , $projects_list);
685 while (my $line = <$fd>) {
686 chomp $line;
687 my ($pr, $ow) = split ' ', $line;
688 $pr = unescape($pr);
689 $ow = unescape($ow);
690 if ($pr eq $project) {
691 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
692 last;
693 }
694 }
695 close $fd;
696 }
697 if (!defined $owner) {
698 $owner = get_file_owner("$projectroot/$project");
699 }
700
701 return $owner;
702}
703
704sub git_get_references {
705 my $type = shift || "";
706 my %refs;
707 my $fd;
708 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
709 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
710 if (-f "$projectroot/$project/info/refs") {
711 open $fd, "$projectroot/$project/info/refs"
712 or return;
713 } else {
714 open $fd, "-|", $GIT, "ls-remote", "."
715 or return;
716 }
717
718 while (my $line = <$fd>) {
719 chomp $line;
720 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
721 if (defined $refs{$1}) {
722 push @{$refs{$1}}, $2;
723 } else {
724 $refs{$1} = [ $2 ];
725 }
726 }
727 }
728 close $fd or return;
729 return \%refs;
730}
731
732## ----------------------------------------------------------------------
733## parse to hash functions
734
735sub parse_date {
736 my $epoch = shift;
737 my $tz = shift || "-0000";
738
739 my %date;
740 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
741 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
742 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
743 $date{'hour'} = $hour;
744 $date{'minute'} = $min;
745 $date{'mday'} = $mday;
746 $date{'day'} = $days[$wday];
747 $date{'month'} = $months[$mon];
748 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
749 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
750 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
751 $mday, $months[$mon], $hour ,$min;
752
753 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
754 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
755 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
756 $date{'hour_local'} = $hour;
757 $date{'minute_local'} = $min;
758 $date{'tz_local'} = $tz;
759 return %date;
760}
761
762sub parse_tag {
763 my $tag_id = shift;
764 my %tag;
765 my @comment;
766
767 open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
768 $tag{'id'} = $tag_id;
769 while (my $line = <$fd>) {
770 chomp $line;
771 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
772 $tag{'object'} = $1;
773 } elsif ($line =~ m/^type (.+)$/) {
774 $tag{'type'} = $1;
775 } elsif ($line =~ m/^tag (.+)$/) {
776 $tag{'name'} = $1;
777 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
778 $tag{'author'} = $1;
779 $tag{'epoch'} = $2;
780 $tag{'tz'} = $3;
781 } elsif ($line =~ m/--BEGIN/) {
782 push @comment, $line;
783 last;
784 } elsif ($line eq "") {
785 last;
786 }
787 }
788 push @comment, <$fd>;
789 $tag{'comment'} = \@comment;
790 close $fd or return;
791 if (!defined $tag{'name'}) {
792 return
793 };
794 return %tag
795}
796
797sub parse_commit {
798 my $commit_id = shift;
799 my $commit_text = shift;
800
801 my @commit_lines;
802 my %co;
803
804 if (defined $commit_text) {
805 @commit_lines = @$commit_text;
806 } else {
807 $/ = "\0";
808 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id
809 or return;
810 @commit_lines = split '\n', <$fd>;
811 close $fd or return;
812 $/ = "\n";
813 pop @commit_lines;
814 }
815 my $header = shift @commit_lines;
816 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
817 return;
818 }
819 ($co{'id'}, my @parents) = split ' ', $header;
820 $co{'parents'} = \@parents;
821 $co{'parent'} = $parents[0];
822 while (my $line = shift @commit_lines) {
823 last if $line eq "\n";
824 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
825 $co{'tree'} = $1;
826 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
827 $co{'author'} = $1;
828 $co{'author_epoch'} = $2;
829 $co{'author_tz'} = $3;
830 if ($co{'author'} =~ m/^([^<]+) </) {
831 $co{'author_name'} = $1;
832 } else {
833 $co{'author_name'} = $co{'author'};
834 }
835 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
836 $co{'committer'} = $1;
837 $co{'committer_epoch'} = $2;
838 $co{'committer_tz'} = $3;
839 $co{'committer_name'} = $co{'committer'};
840 $co{'committer_name'} =~ s/ <.*//;
841 }
842 }
843 if (!defined $co{'tree'}) {
844 return;
845 };
846
847 foreach my $title (@commit_lines) {
848 $title =~ s/^ //;
849 if ($title ne "") {
850 $co{'title'} = chop_str($title, 80, 5);
851 # remove leading stuff of merges to make the interesting part visible
852 if (length($title) > 50) {
853 $title =~ s/^Automatic //;
854 $title =~ s/^merge (of|with) /Merge ... /i;
855 if (length($title) > 50) {
856 $title =~ s/(http|rsync):\/\///;
857 }
858 if (length($title) > 50) {
859 $title =~ s/(master|www|rsync)\.//;
860 }
861 if (length($title) > 50) {
862 $title =~ s/kernel.org:?//;
863 }
864 if (length($title) > 50) {
865 $title =~ s/\/pub\/scm//;
866 }
867 }
868 $co{'title_short'} = chop_str($title, 50, 5);
869 last;
870 }
871 }
872 # remove added spaces
873 foreach my $line (@commit_lines) {
874 $line =~ s/^ //;
875 }
876 $co{'comment'} = \@commit_lines;
877
878 my $age = time - $co{'committer_epoch'};
879 $co{'age'} = $age;
880 $co{'age_string'} = age_string($age);
881 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
882 if ($age > 60*60*24*7*2) {
883 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
884 $co{'age_string_age'} = $co{'age_string'};
885 } else {
886 $co{'age_string_date'} = $co{'age_string'};
887 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
888 }
889 return %co;
890}
891
892# parse ref from ref_file, given by ref_id, with given type
893sub parse_ref {
894 my $ref_file = shift;
895 my $ref_id = shift;
896 my $type = shift || git_get_type($ref_id);
897 my %ref_item;
898
899 $ref_item{'type'} = $type;
900 $ref_item{'id'} = $ref_id;
901 $ref_item{'epoch'} = 0;
902 $ref_item{'age'} = "unknown";
903 if ($type eq "tag") {
904 my %tag = parse_tag($ref_id);
905 $ref_item{'comment'} = $tag{'comment'};
906 if ($tag{'type'} eq "commit") {
907 my %co = parse_commit($tag{'object'});
908 $ref_item{'epoch'} = $co{'committer_epoch'};
909 $ref_item{'age'} = $co{'age_string'};
910 } elsif (defined($tag{'epoch'})) {
911 my $age = time - $tag{'epoch'};
912 $ref_item{'epoch'} = $tag{'epoch'};
913 $ref_item{'age'} = age_string($age);
914 }
915 $ref_item{'reftype'} = $tag{'type'};
916 $ref_item{'name'} = $tag{'name'};
917 $ref_item{'refid'} = $tag{'object'};
918 } elsif ($type eq "commit"){
919 my %co = parse_commit($ref_id);
920 $ref_item{'reftype'} = "commit";
921 $ref_item{'name'} = $ref_file;
922 $ref_item{'title'} = $co{'title'};
923 $ref_item{'refid'} = $ref_id;
924 $ref_item{'epoch'} = $co{'committer_epoch'};
925 $ref_item{'age'} = $co{'age_string'};
926 } else {
927 $ref_item{'reftype'} = $type;
928 $ref_item{'name'} = $ref_file;
929 $ref_item{'refid'} = $ref_id;
930 }
931
932 return %ref_item;
933}
934
935# parse line of git-diff-tree "raw" output
936sub parse_difftree_raw_line {
937 my $line = shift;
938 my %res;
939
940 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
941 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
942 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
943 $res{'from_mode'} = $1;
944 $res{'to_mode'} = $2;
945 $res{'from_id'} = $3;
946 $res{'to_id'} = $4;
947 $res{'status'} = $5;
948 $res{'similarity'} = $6;
949 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
950 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
951 } else {
952 $res{'file'} = unquote($7);
953 }
954 }
955 # 'c512b523472485aef4fff9e57b229d9d243c967f'
956 #elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
957 # $res{'commit'} = $1;
958 #}
959
960 return wantarray ? %res : \%res;
961}
962
963## ......................................................................
964## parse to array of hashes functions
965
966sub git_get_refs_list {
967 my $ref_dir = shift;
968 my @reflist;
969
970 my @refs;
971 my $pfxlen = length("$projectroot/$project/$ref_dir");
972 File::Find::find(sub {
973 return if (/^\./);
974 if (-f $_) {
975 push @refs, substr($File::Find::name, $pfxlen + 1);
976 }
977 }, "$projectroot/$project/$ref_dir");
978
979 foreach my $ref_file (@refs) {
980 my $ref_id = git_get_hash_by_ref("$project/$ref_dir/$ref_file");
981 my $type = git_get_type($ref_id) || next;
982 my %ref_item = parse_ref($ref_file, $ref_id, $type);
983
984 push @reflist, \%ref_item;
985 }
986 # sort refs by age
987 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
988 return \@reflist;
989}
990
991## ----------------------------------------------------------------------
992## filesystem-related functions
993
994sub get_file_owner {
995 my $path = shift;
996
997 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
998 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
999 if (!defined $gcos) {
1000 return undef;
1001 }
1002 my $owner = $gcos;
1003 $owner =~ s/[,;].*$//;
1004 return decode("utf8", $owner, Encode::FB_DEFAULT);
1005}
1006
1007## ......................................................................
1008## mimetype related functions
1009
1010sub mimetype_guess_file {
1011 my $filename = shift;
1012 my $mimemap = shift;
1013 -r $mimemap or return undef;
1014
1015 my %mimemap;
1016 open(MIME, $mimemap) or return undef;
1017 while (<MIME>) {
1018 next if m/^#/; # skip comments
1019 my ($mime, $exts) = split(/\t+/);
1020 if (defined $exts) {
1021 my @exts = split(/\s+/, $exts);
1022 foreach my $ext (@exts) {
1023 $mimemap{$ext} = $mime;
1024 }
1025 }
1026 }
1027 close(MIME);
1028
1029 $filename =~ /\.(.*?)$/;
1030 return $mimemap{$1};
1031}
1032
1033sub mimetype_guess {
1034 my $filename = shift;
1035 my $mime;
1036 $filename =~ /\./ or return undef;
1037
1038 if ($mimetypes_file) {
1039 my $file = $mimetypes_file;
1040 if ($file !~ m!^/!) { # if it is relative path
1041 # it is relative to project
1042 $file = "$projectroot/$project/$file";
1043 }
1044 $mime = mimetype_guess_file($filename, $file);
1045 }
1046 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1047 return $mime;
1048}
1049
1050sub blob_mimetype {
1051 my $fd = shift;
1052 my $filename = shift;
1053
1054 if ($filename) {
1055 my $mime = mimetype_guess($filename);
1056 $mime and return $mime;
1057 }
1058
1059 # just in case
1060 return $default_blob_plain_mimetype unless $fd;
1061
1062 if (-T $fd) {
1063 return 'text/plain' .
1064 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1065 } elsif (! $filename) {
1066 return 'application/octet-stream';
1067 } elsif ($filename =~ m/\.png$/i) {
1068 return 'image/png';
1069 } elsif ($filename =~ m/\.gif$/i) {
1070 return 'image/gif';
1071 } elsif ($filename =~ m/\.jpe?g$/i) {
1072 return 'image/jpeg';
1073 } else {
1074 return 'application/octet-stream';
1075 }
1076}
1077
1078## ======================================================================
1079## functions printing HTML: header, footer, error page
1080
1081sub git_header_html {
1082 my $status = shift || "200 OK";
1083 my $expires = shift;
1084
1085 my $title = "$site_name git";
1086 if (defined $project) {
1087 $title .= " - $project";
1088 if (defined $action) {
1089 $title .= "/$action";
1090 if (defined $file_name) {
1091 $title .= " - $file_name";
1092 if ($action eq "tree" && $file_name !~ m|/$|) {
1093 $title .= "/";
1094 }
1095 }
1096 }
1097 }
1098 my $content_type;
1099 # require explicit support from the UA if we are to send the page as
1100 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1101 # we have to do this because MSIE sometimes globs '*/*', pretending to
1102 # support xhtml+xml but choking when it gets what it asked for.
1103 if (defined $cgi->http('HTTP_ACCEPT') &&
1104 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1105 $cgi->Accept('application/xhtml+xml') != 0) {
1106 $content_type = 'application/xhtml+xml';
1107 } else {
1108 $content_type = 'text/html';
1109 }
1110 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1111 -status=> $status, -expires => $expires);
1112 print <<EOF;
1113<?xml version="1.0" encoding="utf-8"?>
1114<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1115<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1116<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1117<!-- git core binaries version $git_version -->
1118<head>
1119<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1120<meta name="generator" content="gitweb/$version git/$git_version"/>
1121<meta name="robots" content="index, nofollow"/>
1122<title>$title</title>
1123<link rel="stylesheet" type="text/css" href="$stylesheet"/>
1124EOF
1125 if (defined $project) {
1126 printf('<link rel="alternate" title="%s log" '.
1127 'href="%s" type="application/rss+xml"/>'."\n",
1128 esc_param($project), href(action=>"rss"));
1129 }
1130
1131 print "</head>\n" .
1132 "<body>\n" .
1133 "<div class=\"page_header\">\n" .
1134 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1135 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1136 "</a>\n";
1137 print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
1138 if (defined $project) {
1139 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1140 if (defined $action) {
1141 print " / $action";
1142 }
1143 print "\n";
1144 if (!defined $searchtext) {
1145 $searchtext = "";
1146 }
1147 my $search_hash;
1148 if (defined $hash_base) {
1149 $search_hash = $hash_base;
1150 } elsif (defined $hash) {
1151 $search_hash = $hash;
1152 } else {
1153 $search_hash = "HEAD";
1154 }
1155 $cgi->param("a", "search");
1156 $cgi->param("h", $search_hash);
1157 print $cgi->startform(-method => "get", -action => $my_uri) .
1158 "<div class=\"search\">\n" .
1159 $cgi->hidden(-name => "p") . "\n" .
1160 $cgi->hidden(-name => "a") . "\n" .
1161 $cgi->hidden(-name => "h") . "\n" .
1162 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1163 "</div>" .
1164 $cgi->end_form() . "\n";
1165 }
1166 print "</div>\n";
1167}
1168
1169sub git_footer_html {
1170 print "<div class=\"page_footer\">\n";
1171 if (defined $project) {
1172 my $descr = git_get_project_description($project);
1173 if (defined $descr) {
1174 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1175 }
1176 print $cgi->a({-href => href(action=>"rss"), -class => "rss_logo"}, "RSS") . "\n";
1177 } else {
1178 print $cgi->a({-href => href(action=>"opml"), -class => "rss_logo"}, "OPML") . "\n";
1179 }
1180 print "</div>\n" .
1181 "</body>\n" .
1182 "</html>";
1183}
1184
1185sub die_error {
1186 my $status = shift || "403 Forbidden";
1187 my $error = shift || "Malformed query, file missing or permission denied";
1188
1189 git_header_html($status);
1190 print <<EOF;
1191<div class="page_body">
1192<br /><br />
1193$status - $error
1194<br />
1195</div>
1196EOF
1197 git_footer_html();
1198 exit;
1199}
1200
1201## ----------------------------------------------------------------------
1202## functions printing or outputting HTML: navigation
1203
1204sub git_print_page_nav {
1205 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1206 $extra = '' if !defined $extra; # pager or formats
1207
1208 my @navs = qw(summary shortlog log commit commitdiff tree);
1209 if ($suppress) {
1210 @navs = grep { $_ ne $suppress } @navs;
1211 }
1212
1213 my %arg = map { $_ => {action=>$_} } @navs;
1214 if (defined $head) {
1215 for (qw(commit commitdiff)) {
1216 $arg{$_}{hash} = $head;
1217 }
1218 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1219 for (qw(shortlog log)) {
1220 $arg{$_}{hash} = $head;
1221 }
1222 }
1223 }
1224 $arg{tree}{hash} = $treehead if defined $treehead;
1225 $arg{tree}{hash_base} = $treebase if defined $treebase;
1226
1227 print "<div class=\"page_nav\">\n" .
1228 (join " | ",
1229 map { $_ eq $current ?
1230 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1231 } @navs);
1232 print "<br/>\n$extra<br/>\n" .
1233 "</div>\n";
1234}
1235
1236sub format_paging_nav {
1237 my ($action, $hash, $head, $page, $nrevs) = @_;
1238 my $paging_nav;
1239
1240
1241 if ($hash ne $head || $page) {
1242 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1243 } else {
1244 $paging_nav .= "HEAD";
1245 }
1246
1247 if ($page > 0) {
1248 $paging_nav .= " ⋅ " .
1249 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1250 -accesskey => "p", -title => "Alt-p"}, "prev");
1251 } else {
1252 $paging_nav .= " ⋅ prev";
1253 }
1254
1255 if ($nrevs >= (100 * ($page+1)-1)) {
1256 $paging_nav .= " ⋅ " .
1257 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1258 -accesskey => "n", -title => "Alt-n"}, "next");
1259 } else {
1260 $paging_nav .= " ⋅ next";
1261 }
1262
1263 return $paging_nav;
1264}
1265
1266## ......................................................................
1267## functions printing or outputting HTML: div
1268
1269sub git_print_header_div {
1270 my ($action, $title, $hash, $hash_base) = @_;
1271 my %args = ();
1272
1273 $args{action} = $action;
1274 $args{hash} = $hash if $hash;
1275 $args{hash_base} = $hash_base if $hash_base;
1276
1277 print "<div class=\"header\">\n" .
1278 $cgi->a({-href => href(%args), -class => "title"},
1279 $title ? $title : $action) .
1280 "\n</div>\n";
1281}
1282
1283sub git_print_page_path {
1284 my $name = shift;
1285 my $type = shift;
1286 my $hb = shift;
1287
1288 if (!defined $name) {
1289 print "<div class=\"page_path\">/</div>\n";
1290 } elsif (defined $type && $type eq 'blob') {
1291 print "<div class=\"page_path\">";
1292 if (defined $hb) {
1293 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1294 hash_base=>$hb)},
1295 esc_html($name));
1296 } else {
1297 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name)},
1298 esc_html($name));
1299 }
1300 print "<br/></div>\n";
1301 } else {
1302 print "<div class=\"page_path\">" . esc_html($name) . "<br/></div>\n";
1303 }
1304}
1305
1306sub git_print_log {
1307 my $log = shift;
1308
1309 # remove leading empty lines
1310 while (defined $log->[0] && $log->[0] eq "") {
1311 shift @$log;
1312 }
1313
1314 # print log
1315 my $signoff = 0;
1316 my $empty = 0;
1317 foreach my $line (@$log) {
1318 # print only one empty line
1319 # do not print empty line after signoff
1320 if ($line eq "") {
1321 next if ($empty || $signoff);
1322 $empty = 1;
1323 } else {
1324 $empty = 0;
1325 }
1326 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1327 $signoff = 1;
1328 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1329 } else {
1330 $signoff = 0;
1331 print format_log_line_html($line) . "<br/>\n";
1332 }
1333 }
1334}
1335
1336sub git_print_simplified_log {
1337 my $log = shift;
1338 my $remove_title = shift;
1339
1340 shift @$log if $remove_title;
1341 # remove leading empty lines
1342 while (defined $log->[0] && $log->[0] eq "") {
1343 shift @$log;
1344 }
1345
1346 # simplify and print log
1347 my $empty = 0;
1348 foreach my $line (@$log) {
1349 # remove signoff lines
1350 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1351 next;
1352 }
1353 # print only one empty line
1354 if ($line eq "") {
1355 next if $empty;
1356 $empty = 1;
1357 } else {
1358 $empty = 0;
1359 }
1360 print format_log_line_html($line) . "<br/>\n";
1361 }
1362 # end with single empty line
1363 print "<br/>\n" unless $empty;
1364}
1365
1366## ......................................................................
1367## functions printing large fragments of HTML
1368
1369sub git_difftree_body {
1370 my ($difftree, $parent) = @_;
1371
1372 print "<div class=\"list_head\">\n";
1373 if ($#{$difftree} > 10) {
1374 print(($#{$difftree} + 1) . " files changed:\n");
1375 }
1376 print "</div>\n";
1377
1378 print "<table class=\"diff_tree\">\n";
1379 my $alternate = 0;
1380 foreach my $line (@{$difftree}) {
1381 my %diff = parse_difftree_raw_line($line);
1382
1383 if ($alternate) {
1384 print "<tr class=\"dark\">\n";
1385 } else {
1386 print "<tr class=\"light\">\n";
1387 }
1388 $alternate ^= 1;
1389
1390 my ($to_mode_oct, $to_mode_str, $to_file_type);
1391 my ($from_mode_oct, $from_mode_str, $from_file_type);
1392 if ($diff{'to_mode'} ne ('0' x 6)) {
1393 $to_mode_oct = oct $diff{'to_mode'};
1394 if (S_ISREG($to_mode_oct)) { # only for regular file
1395 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1396 }
1397 $to_file_type = file_type($diff{'to_mode'});
1398 }
1399 if ($diff{'from_mode'} ne ('0' x 6)) {
1400 $from_mode_oct = oct $diff{'from_mode'};
1401 if (S_ISREG($to_mode_oct)) { # only for regular file
1402 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1403 }
1404 $from_file_type = file_type($diff{'from_mode'});
1405 }
1406
1407 if ($diff{'status'} eq "A") { # created
1408 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1409 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1410 $mode_chng .= "]</span>";
1411 print "<td>" .
1412 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1413 hash_base=>$hash, file_name=>$diff{'file'}),
1414 -class => "list"}, esc_html($diff{'file'})) .
1415 "</td>\n" .
1416 "<td>$mode_chng</td>\n" .
1417 "<td class=\"link\">" .
1418 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1419 hash_base=>$hash, file_name=>$diff{'file'})},
1420 "blob") .
1421 "</td>\n";
1422
1423 } elsif ($diff{'status'} eq "D") { # deleted
1424 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1425 print "<td>" .
1426 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1427 hash_base=>$parent, file_name=>$diff{'file'}),
1428 -class => "list"}, esc_html($diff{'file'})) .
1429 "</td>\n" .
1430 "<td>$mode_chng</td>\n" .
1431 "<td class=\"link\">" .
1432 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1433 hash_base=>$parent, file_name=>$diff{'file'})},
1434 "blob") .
1435 " | " .
1436 $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1437 file_name=>$diff{'file'})},\
1438 "history") .
1439 "</td>\n";
1440
1441 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1442 my $mode_chnge = "";
1443 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1444 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1445 if ($from_file_type != $to_file_type) {
1446 $mode_chnge .= " from $from_file_type to $to_file_type";
1447 }
1448 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1449 if ($from_mode_str && $to_mode_str) {
1450 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1451 } elsif ($to_mode_str) {
1452 $mode_chnge .= " mode: $to_mode_str";
1453 }
1454 }
1455 $mode_chnge .= "]</span>\n";
1456 }
1457 print "<td>";
1458 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1459 print $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1460 hash_base=>$hash, file_name=>$diff{'file'}),
1461 -class => "list"}, esc_html($diff{'file'}));
1462 } else { # only mode changed
1463 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1464 hash_base=>$hash, file_name=>$diff{'file'}),
1465 -class => "list"}, esc_html($diff{'file'}));
1466 }
1467 print "</td>\n" .
1468 "<td>$mode_chnge</td>\n" .
1469 "<td class=\"link\">" .
1470 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1471 hash_base=>$hash, file_name=>$diff{'file'})},
1472 "blob");
1473 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1474 print " | " .
1475 $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1476 hash_base=>$hash, file_name=>$diff{'file'})},
1477 "diff");
1478 }
1479 print " | " .
1480 $cgi->a({-href => href(action=>"history",
1481 hash_base=>$hash, file_name=>$diff{'file'})},
1482 "history");
1483 print "</td>\n";
1484
1485 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1486 my %status_name = ('R' => 'moved', 'C' => 'copied');
1487 my $nstatus = $status_name{$diff{'status'}};
1488 my $mode_chng = "";
1489 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1490 # mode also for directories, so we cannot use $to_mode_str
1491 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1492 }
1493 print "<td>" .
1494 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1495 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1496 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1497 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1498 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1499 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1500 -class => "list"}, esc_html($diff{'from_file'})) .
1501 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1502 "<td class=\"link\">" .
1503 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1504 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1505 "blob");
1506 if ($diff{'to_id'} ne $diff{'from_id'}) {
1507 print " | " .
1508 $cgi->a({-href => href(action=>"blobdiff", hash_base=>$hash,
1509 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1510 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1511 "diff");
1512 }
1513 print "</td>\n";
1514
1515 } # we should not encounter Unmerged (U) or Unknown (X) status
1516 print "</tr>\n";
1517 }
1518 print "</table>\n";
1519}
1520
1521sub git_shortlog_body {
1522 # uses global variable $project
1523 my ($revlist, $from, $to, $refs, $extra) = @_;
1524
1525 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1526 my $have_snapshot = (defined $ctype && defined $suffix);
1527
1528 $from = 0 unless defined $from;
1529 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1530
1531 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1532 my $alternate = 0;
1533 for (my $i = $from; $i <= $to; $i++) {
1534 my $commit = $revlist->[$i];
1535 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1536 my $ref = format_ref_marker($refs, $commit);
1537 my %co = parse_commit($commit);
1538 if ($alternate) {
1539 print "<tr class=\"dark\">\n";
1540 } else {
1541 print "<tr class=\"light\">\n";
1542 }
1543 $alternate ^= 1;
1544 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1545 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1546 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1547 "<td>";
1548 print format_subject_html($co{'title'}, $co{'title_short'},
1549 href(action=>"commit", hash=>$commit), $ref);
1550 print "</td>\n" .
1551 "<td class=\"link\">" .
1552 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1553 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1554 if ($have_snapshot) {
1555 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1556 }
1557 print "</td>\n" .
1558 "</tr>\n";
1559 }
1560 if (defined $extra) {
1561 print "<tr>\n" .
1562 "<td colspan=\"4\">$extra</td>\n" .
1563 "</tr>\n";
1564 }
1565 print "</table>\n";
1566}
1567
1568sub git_history_body {
1569 # Warning: assumes constant type (blob or tree) during history
1570 my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
1571
1572 print "<table class=\"history\" cellspacing=\"0\">\n";
1573 my $alternate = 0;
1574 while (my $line = <$fd>) {
1575 if ($line !~ m/^([0-9a-fA-F]{40})/) {
1576 next;
1577 }
1578
1579 my $commit = $1;
1580 my %co = parse_commit($commit);
1581 if (!%co) {
1582 next;
1583 }
1584
1585 my $ref = format_ref_marker($refs, $commit);
1586
1587 if ($alternate) {
1588 print "<tr class=\"dark\">\n";
1589 } else {
1590 print "<tr class=\"light\">\n";
1591 }
1592 $alternate ^= 1;
1593 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1594 # shortlog uses chop_str($co{'author_name'}, 10)
1595 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1596 "<td>";
1597 # originally git_history used chop_str($co{'title'}, 50)
1598 print format_subject_html($co{'title'}, $co{'title_short'},
1599 href(action=>"commit", hash=>$commit), $ref);
1600 print "</td>\n" .
1601 "<td class=\"link\">" .
1602 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1603 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1604 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
1605
1606 if ($ftype eq 'blob') {
1607 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1608 my $blob_parent = git_get_hash_by_path($commit, $file_name);
1609 if (defined $blob_current && defined $blob_parent &&
1610 $blob_current ne $blob_parent) {
1611 print " | " .
1612 $cgi->a({-href => href(action=>"blobdiff", hash=>$blob_current, hash_parent=>$blob_parent,
1613 hash_base=>$commit, file_name=>$file_name)},
1614 "diff to current");
1615 }
1616 }
1617 print "</td>\n" .
1618 "</tr>\n";
1619 }
1620 if (defined $extra) {
1621 print "<tr>\n" .
1622 "<td colspan=\"4\">$extra</td>\n" .
1623 "</tr>\n";
1624 }
1625 print "</table>\n";
1626}
1627
1628sub git_tags_body {
1629 # uses global variable $project
1630 my ($taglist, $from, $to, $extra) = @_;
1631 $from = 0 unless defined $from;
1632 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1633
1634 print "<table class=\"tags\" cellspacing=\"0\">\n";
1635 my $alternate = 0;
1636 for (my $i = $from; $i <= $to; $i++) {
1637 my $entry = $taglist->[$i];
1638 my %tag = %$entry;
1639 my $comment_lines = $tag{'comment'};
1640 my $comment = shift @$comment_lines;
1641 my $comment_short;
1642 if (defined $comment) {
1643 $comment_short = chop_str($comment, 30, 5);
1644 }
1645 if ($alternate) {
1646 print "<tr class=\"dark\">\n";
1647 } else {
1648 print "<tr class=\"light\">\n";
1649 }
1650 $alternate ^= 1;
1651 print "<td><i>$tag{'age'}</i></td>\n" .
1652 "<td>" .
1653 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
1654 -class => "list name"}, esc_html($tag{'name'})) .
1655 "</td>\n" .
1656 "<td>";
1657 if (defined $comment) {
1658 print format_subject_html($comment, $comment_short,
1659 href(action=>"tag", hash=>$tag{'id'}));
1660 }
1661 print "</td>\n" .
1662 "<td class=\"selflink\">";
1663 if ($tag{'type'} eq "tag") {
1664 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
1665 } else {
1666 print " ";
1667 }
1668 print "</td>\n" .
1669 "<td class=\"link\">" . " | " .
1670 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
1671 if ($tag{'reftype'} eq "commit") {
1672 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
1673 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
1674 } elsif ($tag{'reftype'} eq "blob") {
1675 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
1676 }
1677 print "</td>\n" .
1678 "</tr>";
1679 }
1680 if (defined $extra) {
1681 print "<tr>\n" .
1682 "<td colspan=\"5\">$extra</td>\n" .
1683 "</tr>\n";
1684 }
1685 print "</table>\n";
1686}
1687
1688sub git_heads_body {
1689 # uses global variable $project
1690 my ($taglist, $head, $from, $to, $extra) = @_;
1691 $from = 0 unless defined $from;
1692 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1693
1694 print "<table class=\"heads\" cellspacing=\"0\">\n";
1695 my $alternate = 0;
1696 for (my $i = $from; $i <= $to; $i++) {
1697 my $entry = $taglist->[$i];
1698 my %tag = %$entry;
1699 my $curr = $tag{'id'} eq $head;
1700 if ($alternate) {
1701 print "<tr class=\"dark\">\n";
1702 } else {
1703 print "<tr class=\"light\">\n";
1704 }
1705 $alternate ^= 1;
1706 print "<td><i>$tag{'age'}</i></td>\n" .
1707 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1708 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
1709 -class => "list name"},esc_html($tag{'name'})) .
1710 "</td>\n" .
1711 "<td class=\"link\">" .
1712 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
1713 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
1714 "</td>\n" .
1715 "</tr>";
1716 }
1717 if (defined $extra) {
1718 print "<tr>\n" .
1719 "<td colspan=\"3\">$extra</td>\n" .
1720 "</tr>\n";
1721 }
1722 print "</table>\n";
1723}
1724
1725## ----------------------------------------------------------------------
1726## functions printing large fragments, format as one of arguments
1727
1728sub git_diff_print {
1729 my $from = shift;
1730 my $from_name = shift;
1731 my $to = shift;
1732 my $to_name = shift;
1733 my $format = shift || "html";
1734
1735 my $from_tmp = "/dev/null";
1736 my $to_tmp = "/dev/null";
1737 my $pid = $$;
1738
1739 # create tmp from-file
1740 if (defined $from) {
1741 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
1742 open my $fd2, "> $from_tmp";
1743 open my $fd, "-|", $GIT, "cat-file", "blob", $from;
1744 my @file = <$fd>;
1745 print $fd2 @file;
1746 close $fd2;
1747 close $fd;
1748 }
1749
1750 # create tmp to-file
1751 if (defined $to) {
1752 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
1753 open my $fd2, "> $to_tmp";
1754 open my $fd, "-|", $GIT, "cat-file", "blob", $to;
1755 my @file = <$fd>;
1756 print $fd2 @file;
1757 close $fd2;
1758 close $fd;
1759 }
1760
1761 open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
1762 if ($format eq "plain") {
1763 undef $/;
1764 print <$fd>;
1765 $/ = "\n";
1766 } else {
1767 while (my $line = <$fd>) {
1768 chomp $line;
1769 my $char = substr($line, 0, 1);
1770 my $diff_class = "";
1771 if ($char eq '+') {
1772 $diff_class = " add";
1773 } elsif ($char eq "-") {
1774 $diff_class = " rem";
1775 } elsif ($char eq "@") {
1776 $diff_class = " chunk_header";
1777 } elsif ($char eq "\\") {
1778 # skip errors
1779 next;
1780 }
1781 $line = untabify($line);
1782 print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
1783 }
1784 }
1785 close $fd;
1786
1787 if (defined $from) {
1788 unlink($from_tmp);
1789 }
1790 if (defined $to) {
1791 unlink($to_tmp);
1792 }
1793}
1794
1795
1796## ======================================================================
1797## ======================================================================
1798## actions
1799
1800sub git_project_list {
1801 my $order = $cgi->param('o');
1802 if (defined $order && $order !~ m/project|descr|owner|age/) {
1803 die_error(undef, "Unknown order parameter");
1804 }
1805
1806 my @list = git_get_projects_list();
1807 my @projects;
1808 if (!@list) {
1809 die_error(undef, "No projects found");
1810 }
1811 foreach my $pr (@list) {
1812 my $head = git_get_head_hash($pr->{'path'});
1813 if (!defined $head) {
1814 next;
1815 }
1816 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
1817 my %co = parse_commit($head);
1818 if (!%co) {
1819 next;
1820 }
1821 $pr->{'commit'} = \%co;
1822 if (!defined $pr->{'descr'}) {
1823 my $descr = git_get_project_description($pr->{'path'}) || "";
1824 $pr->{'descr'} = chop_str($descr, 25, 5);
1825 }
1826 if (!defined $pr->{'owner'}) {
1827 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
1828 }
1829 push @projects, $pr;
1830 }
1831
1832 git_header_html();
1833 if (-f $home_text) {
1834 print "<div class=\"index_include\">\n";
1835 open (my $fd, $home_text);
1836 print <$fd>;
1837 close $fd;
1838 print "</div>\n";
1839 }
1840 print "<table class=\"project_list\">\n" .
1841 "<tr>\n";
1842 $order ||= "project";
1843 if ($order eq "project") {
1844 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
1845 print "<th>Project</th>\n";
1846 } else {
1847 print "<th>" .
1848 $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
1849 -class => "header"}, "Project") .
1850 "</th>\n";
1851 }
1852 if ($order eq "descr") {
1853 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
1854 print "<th>Description</th>\n";
1855 } else {
1856 print "<th>" .
1857 $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
1858 -class => "header"}, "Description") .
1859 "</th>\n";
1860 }
1861 if ($order eq "owner") {
1862 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
1863 print "<th>Owner</th>\n";
1864 } else {
1865 print "<th>" .
1866 $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
1867 -class => "header"}, "Owner") .
1868 "</th>\n";
1869 }
1870 if ($order eq "age") {
1871 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
1872 print "<th>Last Change</th>\n";
1873 } else {
1874 print "<th>" .
1875 $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
1876 -class => "header"}, "Last Change") .
1877 "</th>\n";
1878 }
1879 print "<th></th>\n" .
1880 "</tr>\n";
1881 my $alternate = 0;
1882 foreach my $pr (@projects) {
1883 if ($alternate) {
1884 print "<tr class=\"dark\">\n";
1885 } else {
1886 print "<tr class=\"light\">\n";
1887 }
1888 $alternate ^= 1;
1889 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
1890 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
1891 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
1892 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
1893 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
1894 $pr->{'commit'}{'age_string'} . "</td>\n" .
1895 "<td class=\"link\">" .
1896 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
1897 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
1898 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
1899 "</td>\n" .
1900 "</tr>\n";
1901 }
1902 print "</table>\n";
1903 git_footer_html();
1904}
1905
1906sub git_summary {
1907 my $descr = git_get_project_description($project) || "none";
1908 my $head = git_get_head_hash($project);
1909 my %co = parse_commit($head);
1910 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
1911
1912 my $owner = git_get_project_owner($project);
1913
1914 my $refs = git_get_references();
1915 git_header_html();
1916 git_print_page_nav('summary','', $head);
1917
1918 print "<div class=\"title\"> </div>\n";
1919 print "<table cellspacing=\"0\">\n" .
1920 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
1921 "<tr><td>owner</td><td>$owner</td></tr>\n" .
1922 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
1923 # use per project git URL list in $projectroot/$project/cloneurl
1924 # or make project git URL from git base URL and project name
1925 my $url_tag = "URL";
1926 my @url_list = git_get_project_url_list($project);
1927 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
1928 foreach my $git_url (@url_list) {
1929 next unless $git_url;
1930 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
1931 $url_tag = "";
1932 }
1933 print "</table>\n";
1934
1935 open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_get_head_hash($project)
1936 or die_error(undef, "Open git-rev-list failed");
1937 my @revlist = map { chomp; $_ } <$fd>;
1938 close $fd;
1939 git_print_header_div('shortlog');
1940 git_shortlog_body(\@revlist, 0, 15, $refs,
1941 $cgi->a({-href => href(action=>"shortlog")}, "..."));
1942
1943 my $taglist = git_get_refs_list("refs/tags");
1944 if (defined @$taglist) {
1945 git_print_header_div('tags');
1946 git_tags_body($taglist, 0, 15,
1947 $cgi->a({-href => href(action=>"tags")}, "..."));
1948 }
1949
1950 my $headlist = git_get_refs_list("refs/heads");
1951 if (defined @$headlist) {
1952 git_print_header_div('heads');
1953 git_heads_body($headlist, $head, 0, 15,
1954 $cgi->a({-href => href(action=>"heads")}, "..."));
1955 }
1956
1957 git_footer_html();
1958}
1959
1960sub git_tag {
1961 my $head = git_get_head_hash($project);
1962 git_header_html();
1963 git_print_page_nav('','', $head,undef,$head);
1964 my %tag = parse_tag($hash);
1965 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
1966 print "<div class=\"title_text\">\n" .
1967 "<table cellspacing=\"0\">\n" .
1968 "<tr>\n" .
1969 "<td>object</td>\n" .
1970 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
1971 $tag{'object'}) . "</td>\n" .
1972 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
1973 $tag{'type'}) . "</td>\n" .
1974 "</tr>\n";
1975 if (defined($tag{'author'})) {
1976 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
1977 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
1978 print "<tr><td></td><td>" . $ad{'rfc2822'} .
1979 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
1980 "</td></tr>\n";
1981 }
1982 print "</table>\n\n" .
1983 "</div>\n";
1984 print "<div class=\"page_body\">";
1985 my $comment = $tag{'comment'};
1986 foreach my $line (@$comment) {
1987 print esc_html($line) . "<br/>\n";
1988 }
1989 print "</div>\n";
1990 git_footer_html();
1991}
1992
1993sub git_blame2 {
1994 my $fd;
1995 my $ftype;
1996
1997 if (!gitweb_check_feature('blame')) {
1998 die_error('403 Permission denied', "Permission denied");
1999 }
2000 die_error('404 Not Found', "File name not defined") if (!$file_name);
2001 $hash_base ||= git_get_head_hash($project);
2002 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2003 my %co = parse_commit($hash_base)
2004 or die_error(undef, "Reading commit failed");
2005 if (!defined $hash) {
2006 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2007 or die_error(undef, "Error looking up file");
2008 }
2009 $ftype = git_get_type($hash);
2010 if ($ftype !~ "blob") {
2011 die_error("400 Bad Request", "Object is not a blob");
2012 }
2013 open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
2014 or die_error(undef, "Open git-blame failed");
2015 git_header_html();
2016 my $formats_nav =
2017 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2018 "blob") .
2019 " | " .
2020 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2021 "head");
2022 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2023 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2024 git_print_page_path($file_name, $ftype, $hash_base);
2025 my @rev_color = (qw(light2 dark2));
2026 my $num_colors = scalar(@rev_color);
2027 my $current_color = 0;
2028 my $last_rev;
2029 print <<HTML;
2030<div class="page_body">
2031<table class="blame">
2032<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2033HTML
2034 while (<$fd>) {
2035 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2036 my $full_rev = $1;
2037 my $rev = substr($full_rev, 0, 8);
2038 my $lineno = $2;
2039 my $data = $3;
2040
2041 if (!defined $last_rev) {
2042 $last_rev = $full_rev;
2043 } elsif ($last_rev ne $full_rev) {
2044 $last_rev = $full_rev;
2045 $current_color = ++$current_color % $num_colors;
2046 }
2047 print "<tr class=\"$rev_color[$current_color]\">\n";
2048 print "<td class=\"sha1\">" .
2049 $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2050 esc_html($rev)) . "</td>\n";
2051 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2052 esc_html($lineno) . "</a></td>\n";
2053 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2054 print "</tr>\n";
2055 }
2056 print "</table>\n";
2057 print "</div>";
2058 close $fd
2059 or print "Reading blob failed\n";
2060 git_footer_html();
2061}
2062
2063sub git_blame {
2064 my $fd;
2065
2066 if (!gitweb_check_feature('blame')) {
2067 die_error('403 Permission denied', "Permission denied");
2068 }
2069 die_error('404 Not Found', "File name not defined") if (!$file_name);
2070 $hash_base ||= git_get_head_hash($project);
2071 die_error(undef, "Couldn't find base commit") unless ($hash_base);
2072 my %co = parse_commit($hash_base)
2073 or die_error(undef, "Reading commit failed");
2074 if (!defined $hash) {
2075 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2076 or die_error(undef, "Error lookup file");
2077 }
2078 open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2079 or die_error(undef, "Open git-annotate failed");
2080 git_header_html();
2081 my $formats_nav =
2082 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2083 "blob") .
2084 " | " .
2085 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2086 "head");
2087 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2088 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2089 git_print_page_path($file_name, 'blob', $hash_base);
2090 print "<div class=\"page_body\">\n";
2091 print <<HTML;
2092<table class="blame">
2093 <tr>
2094 <th>Commit</th>
2095 <th>Age</th>
2096 <th>Author</th>
2097 <th>Line</th>
2098 <th>Data</th>
2099 </tr>
2100HTML
2101 my @line_class = (qw(light dark));
2102 my $line_class_len = scalar (@line_class);
2103 my $line_class_num = $#line_class;
2104 while (my $line = <$fd>) {
2105 my $long_rev;
2106 my $short_rev;
2107 my $author;
2108 my $time;
2109 my $lineno;
2110 my $data;
2111 my $age;
2112 my $age_str;
2113 my $age_class;
2114
2115 chomp $line;
2116 $line_class_num = ($line_class_num + 1) % $line_class_len;
2117
2118 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
2119 $long_rev = $1;
2120 $author = $2;
2121 $time = $3;
2122 $lineno = $4;
2123 $data = $5;
2124 } else {
2125 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2126 next;
2127 }
2128 $short_rev = substr ($long_rev, 0, 8);
2129 $age = time () - $time;
2130 $age_str = age_string ($age);
2131 $age_str =~ s/ / /g;
2132 $age_class = age_class($age);
2133 $author = esc_html ($author);
2134 $author =~ s/ / /g;
2135
2136 $data = untabify($data);
2137 $data = esc_html ($data);
2138
2139 print <<HTML;
2140 <tr class="$line_class[$line_class_num]">
2141 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2142 <td class="$age_class">$age_str</td>
2143 <td>$author</td>
2144 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2145 <td class="pre">$data</td>
2146 </tr>
2147HTML
2148 } # while (my $line = <$fd>)
2149 print "</table>\n\n";
2150 close $fd
2151 or print "Reading blob failed.\n";
2152 print "</div>";
2153 git_footer_html();
2154}
2155
2156sub git_tags {
2157 my $head = git_get_head_hash($project);
2158 git_header_html();
2159 git_print_page_nav('','', $head,undef,$head);
2160 git_print_header_div('summary', $project);
2161
2162 my $taglist = git_get_refs_list("refs/tags");
2163 if (defined @$taglist) {
2164 git_tags_body($taglist);
2165 }
2166 git_footer_html();
2167}
2168
2169sub git_heads {
2170 my $head = git_get_head_hash($project);
2171 git_header_html();
2172 git_print_page_nav('','', $head,undef,$head);
2173 git_print_header_div('summary', $project);
2174
2175 my $taglist = git_get_refs_list("refs/heads");
2176 if (defined @$taglist) {
2177 git_heads_body($taglist, $head);
2178 }
2179 git_footer_html();
2180}
2181
2182sub git_blob_plain {
2183 if (!defined $hash) {
2184 if (defined $file_name) {
2185 my $base = $hash_base || git_get_head_hash($project);
2186 $hash = git_get_hash_by_path($base, $file_name, "blob")
2187 or die_error(undef, "Error lookup file");
2188 } else {
2189 die_error(undef, "No file name defined");
2190 }
2191 }
2192 my $type = shift;
2193 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2194 or die_error(undef, "Couldn't cat $file_name, $hash");
2195
2196 $type ||= blob_mimetype($fd, $file_name);
2197
2198 # save as filename, even when no $file_name is given
2199 my $save_as = "$hash";
2200 if (defined $file_name) {
2201 $save_as = $file_name;
2202 } elsif ($type =~ m/^text\//) {
2203 $save_as .= '.txt';
2204 }
2205
2206 print $cgi->header(-type => "$type",
2207 -content_disposition => "inline; filename=\"$save_as\"");
2208 undef $/;
2209 binmode STDOUT, ':raw';
2210 print <$fd>;
2211 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2212 $/ = "\n";
2213 close $fd;
2214}
2215
2216sub git_blob {
2217 if (!defined $hash) {
2218 if (defined $file_name) {
2219 my $base = $hash_base || git_get_head_hash($project);
2220 $hash = git_get_hash_by_path($base, $file_name, "blob")
2221 or die_error(undef, "Error lookup file");
2222 } else {
2223 die_error(undef, "No file name defined");
2224 }
2225 }
2226 my $have_blame = gitweb_check_feature('blame');
2227 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2228 or die_error(undef, "Couldn't cat $file_name, $hash");
2229 my $mimetype = blob_mimetype($fd, $file_name);
2230 if ($mimetype !~ m/^text\//) {
2231 close $fd;
2232 return git_blob_plain($mimetype);
2233 }
2234 git_header_html();
2235 my $formats_nav = '';
2236 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2237 if (defined $file_name) {
2238 if ($have_blame) {
2239 $formats_nav .=
2240 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2241 hash=>$hash, file_name=>$file_name)},
2242 "blame") .
2243 " | ";
2244 }
2245 $formats_nav .=
2246 $cgi->a({-href => href(action=>"blob_plain",
2247 hash=>$hash, file_name=>$file_name)},
2248 "plain") .
2249 " | " .
2250 $cgi->a({-href => href(action=>"blob",
2251 hash_base=>"HEAD", file_name=>$file_name)},
2252 "head");
2253 } else {
2254 $formats_nav .=
2255 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
2256 }
2257 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2258 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2259 } else {
2260 print "<div class=\"page_nav\">\n" .
2261 "<br/><br/></div>\n" .
2262 "<div class=\"title\">$hash</div>\n";
2263 }
2264 git_print_page_path($file_name, "blob", $hash_base);
2265 print "<div class=\"page_body\">\n";
2266 my $nr;
2267 while (my $line = <$fd>) {
2268 chomp $line;
2269 $nr++;
2270 $line = untabify($line);
2271 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2272 $nr, $nr, $nr, esc_html($line);
2273 }
2274 close $fd
2275 or print "Reading blob failed.\n";
2276 print "</div>";
2277 git_footer_html();
2278}
2279
2280sub git_tree {
2281 if (!defined $hash) {
2282 $hash = git_get_head_hash($project);
2283 if (defined $file_name) {
2284 my $base = $hash_base || $hash;
2285 $hash = git_get_hash_by_path($base, $file_name, "tree");
2286 }
2287 if (!defined $hash_base) {
2288 $hash_base = $hash;
2289 }
2290 }
2291 $/ = "\0";
2292 open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
2293 or die_error(undef, "Open git-ls-tree failed");
2294 my @entries = map { chomp; $_ } <$fd>;
2295 close $fd or die_error(undef, "Reading tree failed");
2296 $/ = "\n";
2297
2298 my $refs = git_get_references();
2299 my $ref = format_ref_marker($refs, $hash_base);
2300 git_header_html();
2301 my %base_key = ();
2302 my $base = "";
2303 my $have_blame = gitweb_check_feature('blame');
2304 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2305 $base_key{hash_base} = $hash_base;
2306 git_print_page_nav('tree','', $hash_base);
2307 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2308 } else {
2309 print "<div class=\"page_nav\">\n";
2310 print "<br/><br/></div>\n";
2311 print "<div class=\"title\">$hash</div>\n";
2312 }
2313 if (defined $file_name) {
2314 $base = esc_html("$file_name/");
2315 }
2316 git_print_page_path($file_name, 'tree', $hash_base);
2317 print "<div class=\"page_body\">\n";
2318 print "<table cellspacing=\"0\">\n";
2319 my $alternate = 0;
2320 foreach my $line (@entries) {
2321 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2322 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
2323 my $t_mode = $1;
2324 my $t_type = $2;
2325 my $t_hash = $3;
2326 my $t_name = validate_input($4);
2327 if ($alternate) {
2328 print "<tr class=\"dark\">\n";
2329 } else {
2330 print "<tr class=\"light\">\n";
2331 }
2332 $alternate ^= 1;
2333 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
2334 if ($t_type eq "blob") {
2335 print "<td class=\"list\">" .
2336 $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key),
2337 -class => "list"}, esc_html($t_name)) .
2338 "</td>\n" .
2339 "<td class=\"link\">" .
2340 $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2341 "blob");
2342 if ($have_blame) {
2343 print " | " .
2344 $cgi->a({-href => href(action=>"blame", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2345 "blame");
2346 }
2347 print " | " .
2348 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2349 hash=>$t_hash, file_name=>"$base$t_name")},
2350 "history") .
2351 " | " .
2352 $cgi->a({-href => href(action=>"blob_plain",
2353 hash=>$t_hash, file_name=>"$base$t_name")},
2354 "raw") .
2355 "</td>\n";
2356 } elsif ($t_type eq "tree") {
2357 print "<td class=\"list\">" .
2358 $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2359 esc_html($t_name)) .
2360 "</td>\n" .
2361 "<td class=\"link\">" .
2362 $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2363 "tree") .
2364 " | " .
2365 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, file_name=>"$base$t_name")},
2366 "history") .
2367 "</td>\n";
2368 }
2369 print "</tr>\n";
2370 }
2371 print "</table>\n" .
2372 "</div>";
2373 git_footer_html();
2374}
2375
2376sub git_snapshot {
2377
2378 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2379 my $have_snapshot = (defined $ctype && defined $suffix);
2380 if (!$have_snapshot) {
2381 die_error('403 Permission denied', "Permission denied");
2382 }
2383
2384 if (!defined $hash) {
2385 $hash = git_get_head_hash($project);
2386 }
2387
2388 my $filename = basename($project) . "-$hash.tar.$suffix";
2389
2390 print $cgi->header(-type => 'application/x-tar',
2391 -content_encoding => $ctype,
2392 -content_disposition => "inline; filename=\"$filename\"",
2393 -status => '200 OK');
2394
2395 open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | $command" or
2396 die_error(undef, "Execute git-tar-tree failed.");
2397 binmode STDOUT, ':raw';
2398 print <$fd>;
2399 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2400 close $fd;
2401
2402}
2403
2404sub git_log {
2405 my $head = git_get_head_hash($project);
2406 if (!defined $hash) {
2407 $hash = $head;
2408 }
2409 if (!defined $page) {
2410 $page = 0;
2411 }
2412 my $refs = git_get_references();
2413
2414 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2415 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2416 or die_error(undef, "Open git-rev-list failed");
2417 my @revlist = map { chomp; $_ } <$fd>;
2418 close $fd;
2419
2420 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2421
2422 git_header_html();
2423 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2424
2425 if (!@revlist) {
2426 my %co = parse_commit($hash);
2427
2428 git_print_header_div('summary', $project);
2429 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2430 }
2431 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2432 my $commit = $revlist[$i];
2433 my $ref = format_ref_marker($refs, $commit);
2434 my %co = parse_commit($commit);
2435 next if !%co;
2436 my %ad = parse_date($co{'author_epoch'});
2437 git_print_header_div('commit',
2438 "<span class=\"age\">$co{'age_string'}</span>" .
2439 esc_html($co{'title'}) . $ref,
2440 $commit);
2441 print "<div class=\"title_text\">\n" .
2442 "<div class=\"log_link\">\n" .
2443 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2444 " | " .
2445 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2446 "<br/>\n" .
2447 "</div>\n" .
2448 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
2449 "</div>\n";
2450
2451 print "<div class=\"log_body\">\n";
2452 git_print_simplified_log($co{'comment'});
2453 print "</div>\n";
2454 }
2455 git_footer_html();
2456}
2457
2458sub git_commit {
2459 my %co = parse_commit($hash);
2460 if (!%co) {
2461 die_error(undef, "Unknown commit object");
2462 }
2463 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2464 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2465
2466 my $parent = $co{'parent'};
2467 if (!defined $parent) {
2468 $parent = "--root";
2469 }
2470 open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
2471 or die_error(undef, "Open git-diff-tree failed");
2472 my @difftree = map { chomp; $_ } <$fd>;
2473 close $fd or die_error(undef, "Reading git-diff-tree failed");
2474
2475 # non-textual hash id's can be cached
2476 my $expires;
2477 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2478 $expires = "+1d";
2479 }
2480 my $refs = git_get_references();
2481 my $ref = format_ref_marker($refs, $co{'id'});
2482
2483 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2484 my $have_snapshot = (defined $ctype && defined $suffix);
2485
2486 my $formats_nav = '';
2487 if (defined $file_name && defined $co{'parent'}) {
2488 my $parent = $co{'parent'};
2489 $formats_nav .=
2490 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2491 "blame");
2492 }
2493 git_header_html(undef, $expires);
2494 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2495 $hash, $co{'tree'}, $hash,
2496 $formats_nav);
2497
2498 if (defined $co{'parent'}) {
2499 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2500 } else {
2501 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2502 }
2503 print "<div class=\"title_text\">\n" .
2504 "<table cellspacing=\"0\">\n";
2505 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2506 "<tr>" .
2507 "<td></td><td> $ad{'rfc2822'}";
2508 if ($ad{'hour_local'} < 6) {
2509 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2510 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2511 } else {
2512 printf(" (%02d:%02d %s)",
2513 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2514 }
2515 print "</td>" .
2516 "</tr>\n";
2517 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2518 print "<tr><td></td><td> $cd{'rfc2822'}" .
2519 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2520 "</td></tr>\n";
2521 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2522 print "<tr>" .
2523 "<td>tree</td>" .
2524 "<td class=\"sha1\">" .
2525 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2526 class => "list"}, $co{'tree'}) .
2527 "</td>" .
2528 "<td class=\"link\">" .
2529 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2530 "tree");
2531 if ($have_snapshot) {
2532 print " | " .
2533 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2534 }
2535 print "</td>" .
2536 "</tr>\n";
2537 my $parents = $co{'parents'};
2538 foreach my $par (@$parents) {
2539 print "<tr>" .
2540 "<td>parent</td>" .
2541 "<td class=\"sha1\">" .
2542 $cgi->a({-href => href(action=>"commit", hash=>$par),
2543 class => "list"}, $par) .
2544 "</td>" .
2545 "<td class=\"link\">" .
2546 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2547 " | " .
2548 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "commitdiff") .
2549 "</td>" .
2550 "</tr>\n";
2551 }
2552 print "</table>".
2553 "</div>\n";
2554
2555 print "<div class=\"page_body\">\n";
2556 git_print_log($co{'comment'});
2557 print "</div>\n";
2558
2559 git_difftree_body(\@difftree, $parent);
2560
2561 git_footer_html();
2562}
2563
2564sub git_blobdiff {
2565 mkdir($git_temp, 0700);
2566 git_header_html();
2567 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2568 my $formats_nav =
2569 $cgi->a({-href => href(action=>"blobdiff_plain",
2570 hash=>$hash, hash_parent=>$hash_parent)},
2571 "plain");
2572 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2573 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2574 } else {
2575 print <<HTML;
2576<div class="page_nav"><br/><br/></div>
2577<div class="title">$hash vs $hash_parent</div>
2578HTML
2579 }
2580 git_print_page_path($file_name, "blob", $hash_base);
2581 print "<div class=\"page_body\">\n" .
2582 "<div class=\"diff_info\">blob:" .
2583 $cgi->a({-href => href(action=>"blob", hash=>$hash_parent,
2584 hash_base=>$hash_base, file_name=>($file_parent || $file_name))},
2585 $hash_parent) .
2586 " -> blob:" .
2587 $cgi->a({-href => href(action=>"blob", hash=>$hash,
2588 hash_base=>$hash_base, file_name=>$file_name)},
2589 $hash) .
2590 "</div>\n";
2591 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2592 print "</div>"; # page_body
2593 git_footer_html();
2594}
2595
2596sub git_blobdiff_plain {
2597 mkdir($git_temp, 0700);
2598 print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2599 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2600}
2601
2602sub git_commitdiff {
2603 mkdir($git_temp, 0700);
2604 my %co = parse_commit($hash);
2605 if (!%co) {
2606 die_error(undef, "Unknown commit object");
2607 }
2608 if (!defined $hash_parent) {
2609 $hash_parent = $co{'parent'} || '--root';
2610 }
2611 open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2612 or die_error(undef, "Open git-diff-tree failed");
2613 my @difftree = map { chomp; $_ } <$fd>;
2614 close $fd or die_error(undef, "Reading git-diff-tree failed");
2615
2616 # non-textual hash id's can be cached
2617 my $expires;
2618 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2619 $expires = "+1d";
2620 }
2621 my $refs = git_get_references();
2622 my $ref = format_ref_marker($refs, $co{'id'});
2623 my $formats_nav =
2624 $cgi->a({-href => href(action=>"commitdiff_plain", hash=>$hash, hash_parent=>$hash_parent)},
2625 "plain");
2626 git_header_html(undef, $expires);
2627 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2628 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2629 print "<div class=\"page_body\">\n";
2630 git_print_simplified_log($co{'comment'}, 1); # skip title
2631 print "<br/>\n";
2632 foreach my $line (@difftree) {
2633 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2634 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2635 if ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2636 next;
2637 }
2638 my $from_mode = $1;
2639 my $to_mode = $2;
2640 my $from_id = $3;
2641 my $to_id = $4;
2642 my $status = $5;
2643 my $file = validate_input(unquote($6));
2644 if ($status eq "A") {
2645 print "<div class=\"diff_info\">" . file_type($to_mode) . ":" .
2646 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2647 hash=>$to_id, file_name=>$file)},
2648 $to_id) . "(new)" .
2649 "</div>\n";
2650 git_diff_print(undef, "/dev/null", $to_id, "b/$file");
2651 } elsif ($status eq "D") {
2652 print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
2653 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2654 hash=>$from_id, file_name=>$file)},
2655 $from_id) . "(deleted)" .
2656 "</div>\n";
2657 git_diff_print($from_id, "a/$file", undef, "/dev/null");
2658 } elsif ($status eq "M") {
2659 if ($from_id ne $to_id) {
2660 print "<div class=\"diff_info\">" .
2661 file_type($from_mode) . ":" .
2662 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2663 hash=>$from_id, file_name=>$file)},
2664 $from_id) .
2665 " -> " .
2666 file_type($to_mode) . ":" .
2667 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2668 hash=>$to_id, file_name=>$file)},
2669 $to_id);
2670 print "</div>\n";
2671 git_diff_print($from_id, "a/$file", $to_id, "b/$file");
2672 }
2673 }
2674 }
2675 print "<br/>\n" .
2676 "</div>";
2677 git_footer_html();
2678}
2679
2680sub git_commitdiff_plain {
2681 mkdir($git_temp, 0700);
2682 my %co = parse_commit($hash);
2683 if (!%co) {
2684 die_error(undef, "Unknown commit object");
2685 }
2686 if (!defined $hash_parent) {
2687 $hash_parent = $co{'parent'} || '--root';
2688 }
2689 open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2690 or die_error(undef, "Open git-diff-tree failed");
2691 my @difftree = map { chomp; $_ } <$fd>;
2692 close $fd or die_error(undef, "Reading diff-tree failed");
2693
2694 # try to figure out the next tag after this commit
2695 my $tagname;
2696 my $refs = git_get_references("tags");
2697 open $fd, "-|", $GIT, "rev-list", "HEAD";
2698 my @commits = map { chomp; $_ } <$fd>;
2699 close $fd;
2700 foreach my $commit (@commits) {
2701 if (defined $refs->{$commit}) {
2702 $tagname = $refs->{$commit}
2703 }
2704 if ($commit eq $hash) {
2705 last;
2706 }
2707 }
2708
2709 print $cgi->header(-type => "text/plain",
2710 -charset => 'utf-8',
2711 -content_disposition => "inline; filename=\"git-$hash.patch\"");
2712 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2713 my $comment = $co{'comment'};
2714 print <<TEXT;
2715From: $co{'author'}
2716Date: $ad{'rfc2822'} ($ad{'tz_local'})
2717Subject: $co{'title'}
2718TEXT
2719 if (defined $tagname) {
2720 print "X-Git-Tag: $tagname\n";
2721 }
2722 print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
2723 "\n";
2724
2725 foreach my $line (@$comment) {;
2726 print "$line\n";
2727 }
2728 print "---\n\n";
2729
2730 foreach my $line (@difftree) {
2731 if ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2732 next;
2733 }
2734 my $from_id = $3;
2735 my $to_id = $4;
2736 my $status = $5;
2737 my $file = $6;
2738 if ($status eq "A") {
2739 git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
2740 } elsif ($status eq "D") {
2741 git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
2742 } elsif ($status eq "M") {
2743 git_diff_print($from_id, "a/$file", $to_id, "b/$file", "plain");
2744 }
2745 }
2746}
2747
2748sub git_history {
2749 if (!defined $hash_base) {
2750 $hash_base = git_get_head_hash($project);
2751 }
2752 my $ftype;
2753 my %co = parse_commit($hash_base);
2754 if (!%co) {
2755 die_error(undef, "Unknown commit object");
2756 }
2757 my $refs = git_get_references();
2758 git_header_html();
2759 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base);
2760 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2761 if (!defined $hash && defined $file_name) {
2762 $hash = git_get_hash_by_path($hash_base, $file_name);
2763 }
2764 if (defined $hash) {
2765 $ftype = git_get_type($hash);
2766 }
2767 git_print_page_path($file_name, $ftype, $hash_base);
2768
2769 open my $fd, "-|",
2770 $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
2771 git_history_body($fd, $refs, $hash_base, $ftype);
2772
2773 close $fd;
2774 git_footer_html();
2775}
2776
2777sub git_search {
2778 if (!defined $searchtext) {
2779 die_error(undef, "Text field empty");
2780 }
2781 if (!defined $hash) {
2782 $hash = git_get_head_hash($project);
2783 }
2784 my %co = parse_commit($hash);
2785 if (!%co) {
2786 die_error(undef, "Unknown commit object");
2787 }
2788 # pickaxe may take all resources of your box and run for several minutes
2789 # with every query - so decide by yourself how public you make this feature :)
2790 my $commit_search = 1;
2791 my $author_search = 0;
2792 my $committer_search = 0;
2793 my $pickaxe_search = 0;
2794 if ($searchtext =~ s/^author\\://i) {
2795 $author_search = 1;
2796 } elsif ($searchtext =~ s/^committer\\://i) {
2797 $committer_search = 1;
2798 } elsif ($searchtext =~ s/^pickaxe\\://i) {
2799 $commit_search = 0;
2800 $pickaxe_search = 1;
2801 }
2802 git_header_html();
2803 git_print_page_nav('','', $hash,$co{'tree'},$hash);
2804 git_print_header_div('commit', esc_html($co{'title'}), $hash);
2805
2806 print "<table cellspacing=\"0\">\n";
2807 my $alternate = 0;
2808 if ($commit_search) {
2809 $/ = "\0";
2810 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
2811 while (my $commit_text = <$fd>) {
2812 if (!grep m/$searchtext/i, $commit_text) {
2813 next;
2814 }
2815 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2816 next;
2817 }
2818 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2819 next;
2820 }
2821 my @commit_lines = split "\n", $commit_text;
2822 my %co = parse_commit(undef, \@commit_lines);
2823 if (!%co) {
2824 next;
2825 }
2826 if ($alternate) {
2827 print "<tr class=\"dark\">\n";
2828 } else {
2829 print "<tr class=\"light\">\n";
2830 }
2831 $alternate ^= 1;
2832 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2833 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2834 "<td>" .
2835 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
2836 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2837 my $comment = $co{'comment'};
2838 foreach my $line (@$comment) {
2839 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2840 my $lead = esc_html($1) || "";
2841 $lead = chop_str($lead, 30, 10);
2842 my $match = esc_html($2) || "";
2843 my $trail = esc_html($3) || "";
2844 $trail = chop_str($trail, 30, 10);
2845 my $text = "$lead<span class=\"match\">$match</span>$trail";
2846 print chop_str($text, 80, 5) . "<br/>\n";
2847 }
2848 }
2849 print "</td>\n" .
2850 "<td class=\"link\">" .
2851 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2852 " | " .
2853 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2854 print "</td>\n" .
2855 "</tr>\n";
2856 }
2857 close $fd;
2858 }
2859
2860 if ($pickaxe_search) {
2861 $/ = "\n";
2862 open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
2863 undef %co;
2864 my @files;
2865 while (my $line = <$fd>) {
2866 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2867 my %set;
2868 $set{'file'} = $6;
2869 $set{'from_id'} = $3;
2870 $set{'to_id'} = $4;
2871 $set{'id'} = $set{'to_id'};
2872 if ($set{'id'} =~ m/0{40}/) {
2873 $set{'id'} = $set{'from_id'};
2874 }
2875 if ($set{'id'} =~ m/0{40}/) {
2876 next;
2877 }
2878 push @files, \%set;
2879 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2880 if (%co) {
2881 if ($alternate) {
2882 print "<tr class=\"dark\">\n";
2883 } else {
2884 print "<tr class=\"light\">\n";
2885 }
2886 $alternate ^= 1;
2887 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2888 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2889 "<td>" .
2890 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
2891 -class => "list subject"},
2892 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2893 while (my $setref = shift @files) {
2894 my %set = %$setref;
2895 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
2896 hash=>$set{'id'}, file_name=>$set{'file'}),
2897 -class => "list"},
2898 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
2899 "<br/>\n";
2900 }
2901 print "</td>\n" .
2902 "<td class=\"link\">" .
2903 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2904 " | " .
2905 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2906 print "</td>\n" .
2907 "</tr>\n";
2908 }
2909 %co = parse_commit($1);
2910 }
2911 }
2912 close $fd;
2913 }
2914 print "</table>\n";
2915 git_footer_html();
2916}
2917
2918sub git_shortlog {
2919 my $head = git_get_head_hash($project);
2920 if (!defined $hash) {
2921 $hash = $head;
2922 }
2923 if (!defined $page) {
2924 $page = 0;
2925 }
2926 my $refs = git_get_references();
2927
2928 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2929 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2930 or die_error(undef, "Open git-rev-list failed");
2931 my @revlist = map { chomp; $_ } <$fd>;
2932 close $fd;
2933
2934 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
2935 my $next_link = '';
2936 if ($#revlist >= (100 * ($page+1)-1)) {
2937 $next_link =
2938 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
2939 -title => "Alt-n"}, "next");
2940 }
2941
2942
2943 git_header_html();
2944 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
2945 git_print_header_div('summary', $project);
2946
2947 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
2948
2949 git_footer_html();
2950}
2951
2952## ......................................................................
2953## feeds (RSS, OPML)
2954
2955sub git_rss {
2956 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
2957 open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_get_head_hash($project)
2958 or die_error(undef, "Open git-rev-list failed");
2959 my @revlist = map { chomp; $_ } <$fd>;
2960 close $fd or die_error(undef, "Reading git-rev-list failed");
2961 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
2962 print <<XML;
2963<?xml version="1.0" encoding="utf-8"?>
2964<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
2965<channel>
2966<title>$project $my_uri $my_url</title>
2967<link>${\esc_html("$my_url?p=$project;a=summary")}</link>
2968<description>$project log</description>
2969<language>en</language>
2970XML
2971
2972 for (my $i = 0; $i <= $#revlist; $i++) {
2973 my $commit = $revlist[$i];
2974 my %co = parse_commit($commit);
2975 # we read 150, we always show 30 and the ones more recent than 48 hours
2976 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
2977 last;
2978 }
2979 my %cd = parse_date($co{'committer_epoch'});
2980 open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
2981 my @difftree = map { chomp; $_ } <$fd>;
2982 close $fd or next;
2983 print "<item>\n" .
2984 "<title>" .
2985 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
2986 "</title>\n" .
2987 "<author>" . esc_html($co{'author'}) . "</author>\n" .
2988 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
2989 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
2990 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
2991 "<description>" . esc_html($co{'title'}) . "</description>\n" .
2992 "<content:encoded>" .
2993 "<![CDATA[\n";
2994 my $comment = $co{'comment'};
2995 foreach my $line (@$comment) {
2996 $line = decode("utf8", $line, Encode::FB_DEFAULT);
2997 print "$line<br/>\n";
2998 }
2999 print "<br/>\n";
3000 foreach my $line (@difftree) {
3001 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3002 next;
3003 }
3004 my $file = validate_input(unquote($7));
3005 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3006 print "$file<br/>\n";
3007 }
3008 print "]]>\n" .
3009 "</content:encoded>\n" .
3010 "</item>\n";
3011 }
3012 print "</channel></rss>";
3013}
3014
3015sub git_opml {
3016 my @list = git_get_projects_list();
3017
3018 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3019 print <<XML;
3020<?xml version="1.0" encoding="utf-8"?>
3021<opml version="1.0">
3022<head>
3023 <title>$site_name Git OPML Export</title>
3024</head>
3025<body>
3026<outline text="git RSS feeds">
3027XML
3028
3029 foreach my $pr (@list) {
3030 my %proj = %$pr;
3031 my $head = git_get_head_hash($proj{'path'});
3032 if (!defined $head) {
3033 next;
3034 }
3035 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
3036 my %co = parse_commit($head);
3037 if (!%co) {
3038 next;
3039 }
3040
3041 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3042 my $rss = "$my_url?p=$proj{'path'};a=rss";
3043 my $html = "$my_url?p=$proj{'path'};a=summary";
3044 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3045 }
3046 print <<XML;
3047</outline>
3048</body>
3049</opml>
3050XML
3051}