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