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