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