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