gitweb: Reordering code and dividing it into categories
Reorder gitweb code around, divide it into sections (categories) and add some comments. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Jakub Narebski committed
Jul 31, 2006 at 21:22 UTC
717b831178a6c0e86ed5e7cd61002bd9ec0c2a95
1 file changed
+967
-903
gitweb/gitweb.cgi
+967
-903
@@ -160,21 +160,7 @@ if (defined $searchtext) {
160
$searchtext = quotemeta $searchtext;
161
}
162
163
-sub validate_input {
164
- my $input = shift;
165
-
166
- if ($input =~ m/^[0-9a-fA-F]{40}$/) {
167
- return $input;
168
- }
169
- if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
170
- return undef;
171
- }
172
- if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
173
- return undef;
174
- }
175
- return $input;
176
-}
177
-
163
+# dispatch
164
if (!defined $action || $action eq "summary") {
165
git_summary();
166
exit;
@@ -235,6 +221,24 @@ if (!defined $action || $action eq "summary") {
221
exit;
222
}
223
224
+## ======================================================================
225
+## validation, quoting/unquoting and escaping
226
+
227
+sub validate_input {
228
+ my $input = shift;
229
+
230
+ if ($input =~ m/^[0-9a-fA-F]{40}$/) {
231
+ return $input;
232
+ }
233
+ if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
234
+ return undef;
235
+ }
236
+ if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
237
+ return undef;
238
+ }
239
+ return $input;
240
+}
241
+
242
# quote unsafe chars, but keep the slash, even when it's not
243
# correct, but quoted slashes look too horrible in bookmarks
244
sub esc_param {
@@ -264,6 +268,29 @@ sub unquote {
268
return $str;
269
}
270
271
+## ----------------------------------------------------------------------
272
+## HTML aware string manipulation
273
+
274
+sub chop_str {
275
+ my $str = shift;
276
+ my $len = shift;
277
+ my $add_len = shift || 10;
278
+
279
+ # allow only $len chars, but don't cut a word if it would fit in $add_len
280
+ # if it doesn't fit, cut it if it's still longer than the dots we would add
281
+ $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
282
+ my $body = $1;
283
+ my $tail = $2;
284
+ if (length($tail) > 4) {
285
+ $tail = " ...";
286
+ $body =~ s/&[^;]*$//; # remove chopped character entities
287
+ }
288
+ return "$body$tail";
289
+}
290
+
291
+## ----------------------------------------------------------------------
292
+## functions returning short strings
293
+
294
# CSS class for given age value (in seconds)
295
sub age_class {
296
my $age = shift;
@@ -277,202 +304,108 @@ sub age_class {
304
}
305
}
306
280
-sub git_header_html {
281
- my $status = shift || "200 OK";
282
- my $expires = shift;
307
+# convert age in seconds to "nn units ago" string
308
+sub age_string {
309
+ my $age = shift;
310
+ my $age_str;
311
284
- my $title = "$site_name git";
285
- if (defined $project) {
286
- $title .= " - $project";
287
- if (defined $action) {
288
- $title .= "/$action";
289
- if (defined $file_name) {
290
- $title .= " - $file_name";
291
- if ($action eq "tree" && $file_name !~ m|/$|) {
292
- $title .= "/";
293
- }
294
- }
295
- }
296
- }
297
- my $content_type;
298
- # require explicit support from the UA if we are to send the page as
299
- # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
300
- # we have to do this because MSIE sometimes globs '*/*', pretending to
301
- # support xhtml+xml but choking when it gets what it asked for.
302
- if ($cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ && $cgi->Accept('application/xhtml+xml') != 0) {
303
- $content_type = 'application/xhtml+xml';
312
+ if ($age > 60*60*24*365*2) {
313
+ $age_str = (int $age/60/60/24/365);
314
+ $age_str .= " years ago";
315
+ } elsif ($age > 60*60*24*(365/12)*2) {
316
+ $age_str = int $age/60/60/24/(365/12);
317
+ $age_str .= " months ago";
318
+ } elsif ($age > 60*60*24*7*2) {
319
+ $age_str = int $age/60/60/24/7;
320
+ $age_str .= " weeks ago";
321
+ } elsif ($age > 60*60*24*2) {
322
+ $age_str = int $age/60/60/24;
323
+ $age_str .= " days ago";
324
+ } elsif ($age > 60*60*2) {
325
+ $age_str = int $age/60/60;
326
+ $age_str .= " hours ago";
327
+ } elsif ($age > 60*2) {
328
+ $age_str = int $age/60;
329
+ $age_str .= " min ago";
330
+ } elsif ($age > 2) {
331
+ $age_str = int $age;
332
+ $age_str .= " sec ago";
333
} else {
305
- $content_type = 'text/html';
306
- }
307
- print $cgi->header(-type=>$content_type, -charset => 'utf-8', -status=> $status, -expires => $expires);
308
- print <<EOF;
309
-<?xml version="1.0" encoding="utf-8"?>
310
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
311
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
312
-<!-- git web interface v$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
313
-<!-- git core binaries version $git_version -->
314
-<head>
315
-<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
316
-<meta name="robots" content="index, nofollow"/>
317
-<title>$title</title>
318
-<link rel="stylesheet" type="text/css" href="$stylesheet"/>
319
-$rss_link
320
-</head>
321
-<body>
322
-EOF
323
- print "<div class=\"page_header\">\n" .
324
- "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
325
- "<img src=\"$my_uri?" . esc_param("a=git-logo.png") . "\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
326
- "</a>\n";
327
- print $cgi->a({-href => esc_param($home_link)}, "projects") . " / ";
328
- if (defined $project) {
329
- print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, esc_html($project));
330
- if (defined $action) {
331
- print " / $action";
332
- }
333
- print "\n";
334
- if (!defined $searchtext) {
335
- $searchtext = "";
336
- }
337
- my $search_hash;
338
- if (defined $hash_base) {
339
- $search_hash = $hash_base;
340
- } elsif (defined $hash) {
341
- $search_hash = $hash;
342
- } else {
343
- $search_hash = "HEAD";
344
- }
345
- $cgi->param("a", "search");
346
- $cgi->param("h", $search_hash);
347
- print $cgi->startform(-method => "get", -action => $my_uri) .
348
- "<div class=\"search\">\n" .
349
- $cgi->hidden(-name => "p") . "\n" .
350
- $cgi->hidden(-name => "a") . "\n" .
351
- $cgi->hidden(-name => "h") . "\n" .
352
- $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
353
- "</div>" .
354
- $cgi->end_form() . "\n";
334
+ $age_str .= " right now";
335
}
356
- print "</div>\n";
336
+ return $age_str;
337
}
338
359
-sub git_footer_html {
360
- print "<div class=\"page_footer\">\n";
361
- if (defined $project) {
362
- my $descr = git_read_description($project);
363
- if (defined $descr) {
364
- print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
365
- }
366
- print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=rss"), -class => "rss_logo"}, "RSS") . "\n";
339
+# convert file mode in octal to symbolic file mode string
340
+sub mode_str {
341
+ my $mode = oct shift;
342
+
343
+ if (S_ISDIR($mode & S_IFMT)) {
344
+ return 'drwxr-xr-x';
345
+ } elsif (S_ISLNK($mode)) {
346
+ return 'lrwxrwxrwx';
347
+ } elsif (S_ISREG($mode)) {
348
+ # git cares only about the executable bit
349
+ if ($mode & S_IXUSR) {
350
+ return '-rwxr-xr-x';
351
+ } else {
352
+ return '-rw-r--r--';
353
+ };
354
} else {
368
- print $cgi->a({-href => "$my_uri?" . esc_param("a=opml"), -class => "rss_logo"}, "OPML") . "\n";
355
+ return '----------';
356
}
370
- print "</div>\n" .
371
- "</body>\n" .
372
- "</html>";
357
}
358
375
-sub die_error {
376
- my $status = shift || "403 Forbidden";
377
- my $error = shift || "Malformed query, file missing or permission denied";
359
+# convert file mode in octal to file type string
360
+sub file_type {
361
+ my $mode = oct shift;
362
379
- git_header_html($status);
380
- print "<div class=\"page_body\">\n" .
381
- "<br/><br/>\n" .
382
- "$status - $error\n" .
383
- "<br/>\n" .
384
- "</div>\n";
385
- git_footer_html();
386
- exit;
363
+ if (S_ISDIR($mode & S_IFMT)) {
364
+ return "directory";
365
+ } elsif (S_ISLNK($mode)) {
366
+ return "symlink";
367
+ } elsif (S_ISREG($mode)) {
368
+ return "file";
369
+ } else {
370
+ return "unknown";
371
+ }
372
}
373
389
-sub git_page_nav {
390
- my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
391
- $extra = '' if !defined $extra; # pager or formats
374
+## ----------------------------------------------------------------------
375
+## functions returning short HTML fragments, or transforming HTML fragments
376
+## which don't beling to other sections
377
393
- my @navs = qw(summary shortlog log commit commitdiff tree);
394
- if ($suppress) {
395
- @navs = grep { $_ ne $suppress } @navs;
396
- }
378
+# format line of commit message or tag comment
379
+sub format_log_line_html {
380
+ my $line = shift;
381
398
- my %arg = map { $_, ''} @navs;
399
- if (defined $head) {
400
- for (qw(commit commitdiff)) {
401
- $arg{$_} = ";h=$head";
402
- }
403
- if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
404
- for (qw(shortlog log)) {
405
- $arg{$_} = ";h=$head";
406
- }
382
+ $line = esc_html($line);
383
+ $line =~ s/ / /g;
384
+ if ($line =~ m/([0-9a-fA-F]{40})/) {
385
+ my $hash_text = $1;
386
+ if (git_get_type($hash_text) eq "commit") {
387
+ my $link = $cgi->a({-class => "text", -href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_text")}, $hash_text);
388
+ $line =~ s/$hash_text/$link/;
389
}
390
}
409
- $arg{tree} .= ";h=$treehead" if defined $treehead;
410
- $arg{tree} .= ";hb=$treebase" if defined $treebase;
411
-
412
- print "<div class=\"page_nav\">\n" .
413
- (join " | ",
414
- map { $_ eq $current
415
- ? $_
416
- : $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$_$arg{$_}")}, "$_")
417
- }
418
- @navs);
419
- print "<br/>\n$extra<br/>\n" .
420
- "</div>\n";
391
+ return $line;
392
}
393
423
-sub git_header_div {
424
- my ($action, $title, $hash, $hash_base) = @_;
425
- my $rest = '';
426
-
427
- $rest .= ";h=$hash" if $hash;
428
- $rest .= ";hb=$hash_base" if $hash_base;
394
+# format marker of refs pointing to given object
395
+sub git_get_referencing {
396
+ my ($refs, $id) = @_;
397
430
- print "<div class=\"header\">\n" .
431
- $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action$rest"),
432
- -class => "title"}, $title ? $title : $action) . "\n" .
433
- "</div>\n";
398
+ if (defined $refs->{$id}) {
399
+ return ' <span class="tag">' . esc_html($refs->{$id}) . '</span>';
400
+ } else {
401
+ return "";
402
+ }
403
}
404
436
-sub git_get_paging_nav {
437
- my ($action, $hash, $head, $page, $nrevs) = @_;
438
- my $paging_nav;
439
-
440
-
441
- if ($hash ne $head || $page) {
442
- $paging_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action")}, "HEAD");
443
- } else {
444
- $paging_nav .= "HEAD";
445
- }
446
-
447
- if ($page > 0) {
448
- $paging_nav .= " ⋅ " .
449
- $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action;h=$hash;pg=" . ($page-1)),
450
- -accesskey => "p", -title => "Alt-p"}, "prev");
451
- } else {
452
- $paging_nav .= " ⋅ prev";
453
- }
454
-
455
- if ($nrevs >= (100 * ($page+1)-1)) {
456
- $paging_nav .= " ⋅ " .
457
- $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action;h=$hash;pg=" . ($page+1)),
458
- -accesskey => "n", -title => "Alt-n"}, "next");
459
- } else {
460
- $paging_nav .= " ⋅ next";
461
- }
462
-
463
- return $paging_nav;
464
-}
465
-
466
-sub git_get_type {
467
- my $hash = shift;
468
-
469
- open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
470
- my $type = <$fd>;
471
- close $fd or return;
472
- chomp $type;
473
- return $type;
474
-}
405
+## ----------------------------------------------------------------------
406
+## git utility subroutines, invoking git commands
407
408
+# get HEAD ref of given project as hash
409
sub git_read_head {
410
my $project = shift;
411
my $oENV = $ENV{'GIT_DIR'};
@@ -491,6 +424,57 @@ sub git_read_head {
424
return $retval;
425
}
426
427
+# get type of given object
428
+sub git_get_type {
429
+ my $hash = shift;
430
+
431
+ open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
432
+ my $type = <$fd>;
433
+ close $fd or return;
434
+ chomp $type;
435
+ return $type;
436
+}
437
+
438
+sub git_get_project_config {
439
+ my $key = shift;
440
+
441
+ return unless ($key);
442
+ $key =~ s/^gitweb\.//;
443
+ return if ($key =~ m/\W/);
444
+
445
+ my $val = qx($GIT repo-config --get gitweb.$key);
446
+ return ($val);
447
+}
448
+
449
+sub git_get_project_config_bool {
450
+ my $val = git_get_project_config (@_);
451
+ if ($val and $val =~ m/true|yes|on/) {
452
+ return (1);
453
+ }
454
+ return; # implicit false
455
+}
456
+
457
+# get hash of given path at given ref
458
+sub git_get_hash_by_path {
459
+ my $base = shift;
460
+ my $path = shift || return undef;
461
+
462
+ my $tree = $base;
463
+
464
+ open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
465
+ or die_error(undef, "Open git-ls-tree failed.");
466
+ my $line = <$fd>;
467
+ close $fd or return undef;
468
+
469
+ #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
470
+ $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
471
+ return $3;
472
+}
473
+
474
+## ......................................................................
475
+## git utility functions, directly accessing git repository
476
+
477
+# assumes that PATH is not symref
478
sub git_read_hash {
479
my $path = shift;
480
@@ -513,6 +497,100 @@ sub git_read_description {
497
return $descr;
498
}
499
500
+sub git_read_projects {
501
+ my @list;
502
+
503
+ if (-d $projects_list) {
504
+ # search in directory
505
+ my $dir = $projects_list;
506
+ opendir my ($dh), $dir or return undef;
507
+ while (my $dir = readdir($dh)) {
508
+ if (-e "$projectroot/$dir/HEAD") {
509
+ my $pr = {
510
+ path => $dir,
511
+ };
512
+ push @list, $pr
513
+ }
514
+ }
515
+ closedir($dh);
516
+ } elsif (-f $projects_list) {
517
+ # read from file(url-encoded):
518
+ # 'git%2Fgit.git Linus+Torvalds'
519
+ # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
520
+ # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
521
+ open my ($fd), $projects_list or return undef;
522
+ while (my $line = <$fd>) {
523
+ chomp $line;
524
+ my ($path, $owner) = split ' ', $line;
525
+ $path = unescape($path);
526
+ $owner = unescape($owner);
527
+ if (!defined $path) {
528
+ next;
529
+ }
530
+ if (-e "$projectroot/$path/HEAD") {
531
+ my $pr = {
532
+ path => $path,
533
+ owner => decode("utf8", $owner, Encode::FB_DEFAULT),
534
+ };
535
+ push @list, $pr
536
+ }
537
+ }
538
+ close $fd;
539
+ }
540
+ @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
541
+ return @list;
542
+}
543
+
544
+sub read_info_ref {
545
+ my $type = shift || "";
546
+ my %refs;
547
+ # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
548
+ # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
549
+ open my $fd, "$projectroot/$project/info/refs" or return;
550
+ while (my $line = <$fd>) {
551
+ chomp $line;
552
+ # attention: for $type == "" it saves only last path part of ref name
553
+ # e.g. from 'refs/heads/jn/gitweb' it would leave only 'gitweb'
554
+ if ($line =~ m/^([0-9a-fA-F]{40})\t.*$type\/([^\^]+)/) {
555
+ if (defined $refs{$1}) {
556
+ $refs{$1} .= " / $2";
557
+ } else {
558
+ $refs{$1} = $2;
559
+ }
560
+ }
561
+ }
562
+ close $fd or return;
563
+ return \%refs;
564
+}
565
+
566
+## ----------------------------------------------------------------------
567
+## parse to hash functions
568
+
569
+sub date_str {
570
+ my $epoch = shift;
571
+ my $tz = shift || "-0000";
572
+
573
+ my %date;
574
+ my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
575
+ my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
576
+ my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
577
+ $date{'hour'} = $hour;
578
+ $date{'minute'} = $min;
579
+ $date{'mday'} = $mday;
580
+ $date{'day'} = $days[$wday];
581
+ $date{'month'} = $months[$mon];
582
+ $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
583
+ $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
584
+
585
+ $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
586
+ my $local = $epoch + ((int $1 + ($2/60)) * 3600);
587
+ ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
588
+ $date{'hour_local'} = $hour;
589
+ $date{'minute_local'} = $min;
590
+ $date{'tz_local'} = $tz;
591
+ return %date;
592
+}
593
+
594
sub git_read_tag {
595
my $tag_id = shift;
596
my %tag;
@@ -548,37 +626,6 @@ sub git_read_tag {
626
return %tag
627
}
628
551
-sub age_string {
552
- my $age = shift;
553
- my $age_str;
554
-
555
- if ($age > 60*60*24*365*2) {
556
- $age_str = (int $age/60/60/24/365);
557
- $age_str .= " years ago";
558
- } elsif ($age > 60*60*24*(365/12)*2) {
559
- $age_str = int $age/60/60/24/(365/12);
560
- $age_str .= " months ago";
561
- } elsif ($age > 60*60*24*7*2) {
562
- $age_str = int $age/60/60/24/7;
563
- $age_str .= " weeks ago";
564
- } elsif ($age > 60*60*24*2) {
565
- $age_str = int $age/60/60/24;
566
- $age_str .= " days ago";
567
- } elsif ($age > 60*60*2) {
568
- $age_str = int $age/60/60;
569
- $age_str .= " hours ago";
570
- } elsif ($age > 60*2) {
571
- $age_str = int $age/60;
572
- $age_str .= " min ago";
573
- } elsif ($age > 2) {
574
- $age_str = int $age;
575
- $age_str .= " sec ago";
576
- } else {
577
- $age_str .= " right now";
578
- }
579
- return $age_str;
580
-}
581
-
629
sub git_read_commit {
630
my $commit_id = shift;
631
my $commit_text = shift;
@@ -673,187 +720,78 @@ sub git_read_commit {
720
return %co;
721
}
722
676
-sub git_diff_print {
677
- my $from = shift;
678
- my $from_name = shift;
679
- my $to = shift;
680
- my $to_name = shift;
681
- my $format = shift || "html";
682
-
683
- my $from_tmp = "/dev/null";
684
- my $to_tmp = "/dev/null";
685
- my $pid = $$;
723
+## ......................................................................
724
+## parse to array of hashes functions
725
687
- # create tmp from-file
688
- if (defined $from) {
689
- $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
690
- open my $fd2, "> $from_tmp";
691
- open my $fd, "-|", $GIT, "cat-file", "blob", $from;
692
- my @file = <$fd>;
693
- print $fd2 @file;
694
- close $fd2;
695
- close $fd;
696
- }
726
+sub git_read_refs {
727
+ my $ref_dir = shift;
728
+ my @reflist;
729
698
- # create tmp to-file
699
- if (defined $to) {
700
- $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
701
- open my $fd2, "> $to_tmp";
702
- open my $fd, "-|", $GIT, "cat-file", "blob", $to;
703
- my @file = <$fd>;
704
- print $fd2 @file;
705
- close $fd2;
706
- close $fd;
730
+ my @refs;
731
+ opendir my $dh, "$projectroot/$project/$ref_dir";
732
+ while (my $dir = readdir($dh)) {
733
+ if ($dir =~ m/^\./) {
734
+ next;
735
+ }
736
+ if (-d "$projectroot/$project/$ref_dir/$dir") {
737
+ opendir my $dh2, "$projectroot/$project/$ref_dir/$dir";
738
+ my @subdirs = grep !m/^\./, readdir $dh2;
739
+ closedir($dh2);
740
+ foreach my $subdir (@subdirs) {
741
+ push @refs, "$dir/$subdir"
742
+ }
743
+ next;
744
+ }
745
+ push @refs, $dir;
746
}
708
-
709
- open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
710
- if ($format eq "plain") {
711
- undef $/;
712
- print <$fd>;
713
- $/ = "\n";
714
- } else {
715
- while (my $line = <$fd>) {
716
- chomp $line;
717
- my $char = substr($line, 0, 1);
718
- my $diff_class = "";
719
- if ($char eq '+') {
720
- $diff_class = " add";
721
- } elsif ($char eq "-") {
722
- $diff_class = " rem";
723
- } elsif ($char eq "@") {
724
- $diff_class = " chunk_header";
725
- } elsif ($char eq "\\") {
726
- # skip errors
727
- next;
728
- }
729
- while ((my $pos = index($line, "\t")) != -1) {
730
- if (my $count = (8 - (($pos-1) % 8))) {
731
- my $spaces = ' ' x $count;
732
- $line =~ s/\t/$spaces/;
733
- }
747
+ closedir($dh);
748
+ foreach my $ref_file (@refs) {
749
+ my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
750
+ my $type = git_get_type($ref_id) || next;
751
+ my %ref_item;
752
+ my %co;
753
+ $ref_item{'type'} = $type;
754
+ $ref_item{'id'} = $ref_id;
755
+ $ref_item{'epoch'} = 0;
756
+ $ref_item{'age'} = "unknown";
757
+ if ($type eq "tag") {
758
+ my %tag = git_read_tag($ref_id);
759
+ $ref_item{'comment'} = $tag{'comment'};
760
+ if ($tag{'type'} eq "commit") {
761
+ %co = git_read_commit($tag{'object'});
762
+ $ref_item{'epoch'} = $co{'committer_epoch'};
763
+ $ref_item{'age'} = $co{'age_string'};
764
+ } elsif (defined($tag{'epoch'})) {
765
+ my $age = time - $tag{'epoch'};
766
+ $ref_item{'epoch'} = $tag{'epoch'};
767
+ $ref_item{'age'} = age_string($age);
768
}
735
- print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
736
- }
737
- }
738
- close $fd;
739
-
740
- if (defined $from) {
741
- unlink($from_tmp);
742
- }
743
- if (defined $to) {
744
- unlink($to_tmp);
745
- }
746
-}
747
-
748
-sub mode_str {
749
- my $mode = oct shift;
750
-
751
- if (S_ISDIR($mode & S_IFMT)) {
752
- return 'drwxr-xr-x';
753
- } elsif (S_ISLNK($mode)) {
754
- return 'lrwxrwxrwx';
755
- } elsif (S_ISREG($mode)) {
756
- # git cares only about the executable bit
757
- if ($mode & S_IXUSR) {
758
- return '-rwxr-xr-x';
769
+ $ref_item{'reftype'} = $tag{'type'};
770
+ $ref_item{'name'} = $tag{'name'};
771
+ $ref_item{'refid'} = $tag{'object'};
772
+ } elsif ($type eq "commit"){
773
+ %co = git_read_commit($ref_id);
774
+ $ref_item{'reftype'} = "commit";
775
+ $ref_item{'name'} = $ref_file;
776
+ $ref_item{'title'} = $co{'title'};
777
+ $ref_item{'refid'} = $ref_id;
778
+ $ref_item{'epoch'} = $co{'committer_epoch'};
779
+ $ref_item{'age'} = $co{'age_string'};
780
} else {
760
- return '-rw-r--r--';
761
- };
762
- } else {
763
- return '----------';
764
- }
765
-}
766
-
767
-sub chop_str {
768
- my $str = shift;
769
- my $len = shift;
770
- my $add_len = shift || 10;
771
-
772
- # allow only $len chars, but don't cut a word if it would fit in $add_len
773
- # if it doesn't fit, cut it if it's still longer than the dots we would add
774
- $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
775
- my $body = $1;
776
- my $tail = $2;
777
- if (length($tail) > 4) {
778
- $tail = " ...";
779
- $body =~ s/&[^;]*$//; # remove chopped character entities
780
- }
781
- return "$body$tail";
782
-}
783
-
784
-sub file_type {
785
- my $mode = oct shift;
786
-
787
- if (S_ISDIR($mode & S_IFMT)) {
788
- return "directory";
789
- } elsif (S_ISLNK($mode)) {
790
- return "symlink";
791
- } elsif (S_ISREG($mode)) {
792
- return "file";
793
- } else {
794
- return "unknown";
795
- }
796
-}
797
-
798
-sub format_log_line_html {
799
- my $line = shift;
800
-
801
- $line = esc_html($line);
802
- $line =~ s/ / /g;
803
- if ($line =~ m/([0-9a-fA-F]{40})/) {
804
- my $hash_text = $1;
805
- if (git_get_type($hash_text) eq "commit") {
806
- my $link = $cgi->a({-class => "text", -href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_text")}, $hash_text);
807
- $line =~ s/$hash_text/$link/;
781
+ $ref_item{'reftype'} = $type;
782
+ $ref_item{'name'} = $ref_file;
783
+ $ref_item{'refid'} = $ref_id;
784
}
809
- }
810
- return $line;
811
-}
812
-
813
-sub date_str {
814
- my $epoch = shift;
815
- my $tz = shift || "-0000";
816
-
817
- my %date;
818
- my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
819
- my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
820
- my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
821
- $date{'hour'} = $hour;
822
- $date{'minute'} = $min;
823
- $date{'mday'} = $mday;
824
- $date{'day'} = $days[$wday];
825
- $date{'month'} = $months[$mon];
826
- $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
827
- $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
785
829
- $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
830
- my $local = $epoch + ((int $1 + ($2/60)) * 3600);
831
- ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
832
- $date{'hour_local'} = $hour;
833
- $date{'minute_local'} = $min;
834
- $date{'tz_local'} = $tz;
835
- return %date;
786
+ push @reflist, \%ref_item;
787
+ }
788
+ # sort tags by age
789
+ @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
790
+ return \@reflist;
791
}
792
838
-# git-logo (cached in browser for one day)
839
-sub git_logo {
840
- binmode STDOUT, ':raw';
841
- print $cgi->header(-type => 'image/png', -expires => '+1d');
842
- # cat git-logo.png | hexdump -e '16/1 " %02x" "\n"' | sed 's/ /\\x/g'
843
- print "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
844
- "\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
845
- "\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
846
- "\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
847
- "\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
848
- "\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
849
- "\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
850
- "\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
851
- "\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
852
- "\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
853
- "\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
854
- "\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
855
- "\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
856
-}
793
+## ----------------------------------------------------------------------
794
+## filesystem-related functions
795
796
sub get_file_owner {
797
my $path = shift;
@@ -868,253 +806,283 @@ sub get_file_owner {
806
return decode("utf8", $owner, Encode::FB_DEFAULT);
807
}
808
871
-sub git_read_projects {
872
- my @list;
809
+## ......................................................................
810
+## mimetype related functions
811
874
- if (-d $projects_list) {
875
- # search in directory
876
- my $dir = $projects_list;
877
- opendir my ($dh), $dir or return undef;
878
- while (my $dir = readdir($dh)) {
879
- if (-e "$projectroot/$dir/HEAD") {
880
- my $pr = {
881
- path => $dir,
882
- };
883
- push @list, $pr
884
- }
885
- }
886
- closedir($dh);
887
- } elsif (-f $projects_list) {
888
- # read from file(url-encoded):
889
- # 'git%2Fgit.git Linus+Torvalds'
890
- # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
891
- # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
892
- open my ($fd), $projects_list or return undef;
893
- while (my $line = <$fd>) {
894
- chomp $line;
895
- my ($path, $owner) = split ' ', $line;
896
- $path = unescape($path);
897
- $owner = unescape($owner);
898
- if (!defined $path) {
899
- next;
900
- }
901
- if (-e "$projectroot/$path/HEAD") {
902
- my $pr = {
903
- path => $path,
904
- owner => decode("utf8", $owner, Encode::FB_DEFAULT),
905
- };
906
- push @list, $pr
907
- }
812
+sub mimetype_guess_file {
813
+ my $filename = shift;
814
+ my $mimemap = shift;
815
+ -r $mimemap or return undef;
816
+
817
+ my %mimemap;
818
+ open(MIME, $mimemap) or return undef;
819
+ while (<MIME>) {
820
+ my ($mime, $exts) = split(/\t+/);
821
+ my @exts = split(/\s+/, $exts);
822
+ foreach my $ext (@exts) {
823
+ $mimemap{$ext} = $mime;
824
}
909
- close $fd;
825
}
911
- @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
912
- return @list;
913
-}
826
+ close(MIME);
827
915
-sub git_get_project_config {
916
- my $key = shift;
828
+ $filename =~ /\.(.*?)$/;
829
+ return $mimemap{$1};
830
+}
831
918
- return unless ($key);
919
- $key =~ s/^gitweb\.//;
920
- return if ($key =~ m/\W/);
832
+sub mimetype_guess {
833
+ my $filename = shift;
834
+ my $mime;
835
+ $filename =~ /\./ or return undef;
836
922
- my $val = qx($GIT repo-config --get gitweb.$key);
923
- return ($val);
837
+ if ($mimetypes_file) {
838
+ my $file = $mimetypes_file;
839
+ #$file =~ m#^/# or $file = "$projectroot/$path/$file";
840
+ $mime = mimetype_guess_file($filename, $file);
841
+ }
842
+ $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
843
+ return $mime;
844
}
845
926
-sub git_get_project_config_bool {
927
- my $val = git_get_project_config (@_);
928
- if ($val and $val =~ m/true|yes|on/) {
929
- return (1);
930
- }
931
- return; # implicit false
932
-}
846
+sub git_blob_plain_mimetype {
847
+ my $fd = shift;
848
+ my $filename = shift;
849
934
-sub git_project_list {
935
- my @list = git_read_projects();
936
- my @projects;
937
- if (!@list) {
938
- die_error(undef, "No project found.");
939
- }
940
- foreach my $pr (@list) {
941
- my $head = git_read_head($pr->{'path'});
942
- if (!defined $head) {
943
- next;
944
- }
945
- $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
946
- my %co = git_read_commit($head);
947
- if (!%co) {
948
- next;
949
- }
950
- $pr->{'commit'} = \%co;
951
- if (!defined $pr->{'descr'}) {
952
- my $descr = git_read_description($pr->{'path'}) || "";
953
- $pr->{'descr'} = chop_str($descr, 25, 5);
954
- }
955
- if (!defined $pr->{'owner'}) {
956
- $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
957
- }
958
- push @projects, $pr;
959
- }
960
- git_header_html();
961
- if (-f $home_text) {
962
- print "<div class=\"index_include\">\n";
963
- open (my $fd, $home_text);
964
- print <$fd>;
965
- close $fd;
966
- print "</div>\n";
967
- }
968
- print "<table class=\"project_list\">\n" .
969
- "<tr>\n";
970
- if (!defined($order) || (defined($order) && ($order eq "project"))) {
971
- @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
972
- print "<th>Project</th>\n";
973
- } else {
974
- print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=project")}, "Project") . "</th>\n";
850
+ if ($filename) {
851
+ my $mime = mimetype_guess($filename);
852
+ $mime and return $mime;
853
}
976
- if (defined($order) && ($order eq "descr")) {
977
- @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
978
- print "<th>Description</th>\n";
854
+
855
+ # just in case
856
+ return $default_blob_plain_mimetype unless $fd;
857
+
858
+ if (-T $fd) {
859
+ return 'text/plain' .
860
+ ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
861
+ } elsif (! $filename) {
862
+ return 'application/octet-stream';
863
+ } elsif ($filename =~ m/\.png$/i) {
864
+ return 'image/png';
865
+ } elsif ($filename =~ m/\.gif$/i) {
866
+ return 'image/gif';
867
+ } elsif ($filename =~ m/\.jpe?g$/i) {
868
+ return 'image/jpeg';
869
} else {
980
- print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=descr")}, "Description") . "</th>\n";
870
+ return 'application/octet-stream';
871
}
982
- if (defined($order) && ($order eq "owner")) {
983
- @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
984
- print "<th>Owner</th>\n";
985
- } else {
986
- print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=owner")}, "Owner") . "</th>\n";
872
+}
873
+
874
+## ======================================================================
875
+## functions printing HTML: header, footer, error page
876
+
877
+sub git_header_html {
878
+ my $status = shift || "200 OK";
879
+ my $expires = shift;
880
+
881
+ my $title = "$site_name git";
882
+ if (defined $project) {
883
+ $title .= " - $project";
884
+ if (defined $action) {
885
+ $title .= "/$action";
886
+ if (defined $file_name) {
887
+ $title .= " - $file_name";
888
+ if ($action eq "tree" && $file_name !~ m|/$|) {
889
+ $title .= "/";
890
+ }
891
+ }
892
+ }
893
}
988
- if (defined($order) && ($order eq "age")) {
989
- @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
990
- print "<th>Last Change</th>\n";
894
+ my $content_type;
895
+ # require explicit support from the UA if we are to send the page as
896
+ # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
897
+ # we have to do this because MSIE sometimes globs '*/*', pretending to
898
+ # support xhtml+xml but choking when it gets what it asked for.
899
+ if ($cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ && $cgi->Accept('application/xhtml+xml') != 0) {
900
+ $content_type = 'application/xhtml+xml';
901
} else {
992
- print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=age")}, "Last Change") . "</th>\n";
902
+ $content_type = 'text/html';
903
}
994
- print "<th></th>\n" .
995
- "</tr>\n";
996
- my $alternate = 0;
997
- foreach my $pr (@projects) {
998
- if ($alternate) {
999
- print "<tr class=\"dark\">\n";
904
+ print $cgi->header(-type=>$content_type, -charset => 'utf-8', -status=> $status, -expires => $expires);
905
+ print <<EOF;
906
+<?xml version="1.0" encoding="utf-8"?>
907
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
908
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
909
+<!-- git web interface v$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
910
+<!-- git core binaries version $git_version -->
911
+<head>
912
+<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
913
+<meta name="robots" content="index, nofollow"/>
914
+<title>$title</title>
915
+<link rel="stylesheet" type="text/css" href="$stylesheet"/>
916
+$rss_link
917
+</head>
918
+<body>
919
+EOF
920
+ print "<div class=\"page_header\">\n" .
921
+ "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
922
+ "<img src=\"$my_uri?" . esc_param("a=git-logo.png") . "\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
923
+ "</a>\n";
924
+ print $cgi->a({-href => esc_param($home_link)}, "projects") . " / ";
925
+ if (defined $project) {
926
+ print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, esc_html($project));
927
+ if (defined $action) {
928
+ print " / $action";
929
+ }
930
+ print "\n";
931
+ if (!defined $searchtext) {
932
+ $searchtext = "";
933
+ }
934
+ my $search_hash;
935
+ if (defined $hash_base) {
936
+ $search_hash = $hash_base;
937
+ } elsif (defined $hash) {
938
+ $search_hash = $hash;
939
} else {
1001
- print "<tr class=\"light\">\n";
940
+ $search_hash = "HEAD";
941
}
1003
- $alternate ^= 1;
1004
- print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary"), -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
1005
- "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
1006
- "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
1007
- print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" . $pr->{'commit'}{'age_string'} . "</td>\n" .
1008
- "<td class=\"link\">" .
1009
- $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary")}, "summary") .
1010
- " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=shortlog")}, "shortlog") .
1011
- " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=log")}, "log") .
1012
- "</td>\n" .
1013
- "</tr>\n";
942
+ $cgi->param("a", "search");
943
+ $cgi->param("h", $search_hash);
944
+ print $cgi->startform(-method => "get", -action => $my_uri) .
945
+ "<div class=\"search\">\n" .
946
+ $cgi->hidden(-name => "p") . "\n" .
947
+ $cgi->hidden(-name => "a") . "\n" .
948
+ $cgi->hidden(-name => "h") . "\n" .
949
+ $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
950
+ "</div>" .
951
+ $cgi->end_form() . "\n";
952
}
1015
- print "</table>\n";
953
+ print "</div>\n";
954
+}
955
+
956
+sub git_footer_html {
957
+ print "<div class=\"page_footer\">\n";
958
+ if (defined $project) {
959
+ my $descr = git_read_description($project);
960
+ if (defined $descr) {
961
+ print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
962
+ }
963
+ print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=rss"), -class => "rss_logo"}, "RSS") . "\n";
964
+ } else {
965
+ print $cgi->a({-href => "$my_uri?" . esc_param("a=opml"), -class => "rss_logo"}, "OPML") . "\n";
966
+ }
967
+ print "</div>\n" .
968
+ "</body>\n" .
969
+ "</html>";
970
+}
971
+
972
+sub die_error {
973
+ my $status = shift || "403 Forbidden";
974
+ my $error = shift || "Malformed query, file missing or permission denied";
975
+
976
+ git_header_html($status);
977
+ print "<div class=\"page_body\">\n" .
978
+ "<br/><br/>\n" .
979
+ "$status - $error\n" .
980
+ "<br/>\n" .
981
+ "</div>\n";
982
git_footer_html();
983
+ exit;
984
}
985
1019
-sub read_info_ref {
1020
- my $type = shift || "";
1021
- my %refs;
1022
- # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1023
- # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1024
- open my $fd, "$projectroot/$project/info/refs" or return;
1025
- while (my $line = <$fd>) {
1026
- chomp $line;
1027
- # attention: for $type == "" it saves only last path part of ref name
1028
- # e.g. from 'refs/heads/jn/gitweb' it would leave only 'gitweb'
1029
- if ($line =~ m/^([0-9a-fA-F]{40})\t.*$type\/([^\^]+)/) {
1030
- if (defined $refs{$1}) {
1031
- $refs{$1} .= " / $2";
1032
- } else {
1033
- $refs{$1} = $2;
986
+## ----------------------------------------------------------------------
987
+## functions printing or outputting HTML: navigation
988
+
989
+sub git_page_nav {
990
+ my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
991
+ $extra = '' if !defined $extra; # pager or formats
992
+
993
+ my @navs = qw(summary shortlog log commit commitdiff tree);
994
+ if ($suppress) {
995
+ @navs = grep { $_ ne $suppress } @navs;
996
+ }
997
+
998
+ my %arg = map { $_, ''} @navs;
999
+ if (defined $head) {
1000
+ for (qw(commit commitdiff)) {
1001
+ $arg{$_} = ";h=$head";
1002
+ }
1003
+ if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1004
+ for (qw(shortlog log)) {
1005
+ $arg{$_} = ";h=$head";
1006
}
1007
}
1008
}
1037
- close $fd or return;
1038
- return \%refs;
1009
+ $arg{tree} .= ";h=$treehead" if defined $treehead;
1010
+ $arg{tree} .= ";hb=$treebase" if defined $treebase;
1011
+
1012
+ print "<div class=\"page_nav\">\n" .
1013
+ (join " | ",
1014
+ map { $_ eq $current
1015
+ ? $_
1016
+ : $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$_$arg{$_}")}, "$_")
1017
+ }
1018
+ @navs);
1019
+ print "<br/>\n$extra<br/>\n" .
1020
+ "</div>\n";
1021
}
1022
1041
-sub git_get_referencing {
1042
- my ($refs, $id) = @_;
1023
+sub git_get_paging_nav {
1024
+ my ($action, $hash, $head, $page, $nrevs) = @_;
1025
+ my $paging_nav;
1026
1044
- if (defined $refs->{$id}) {
1045
- return ' <span class="tag">' . esc_html($refs->{$id}) . '</span>';
1027
+
1028
+ if ($hash ne $head || $page) {
1029
+ $paging_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action")}, "HEAD");
1030
} else {
1047
- return "";
1031
+ $paging_nav .= "HEAD";
1032
+ }
1033
+
1034
+ if ($page > 0) {
1035
+ $paging_nav .= " ⋅ " .
1036
+ $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action;h=$hash;pg=" . ($page-1)),
1037
+ -accesskey => "p", -title => "Alt-p"}, "prev");
1038
+ } else {
1039
+ $paging_nav .= " ⋅ prev";
1040
+ }
1041
+
1042
+ if ($nrevs >= (100 * ($page+1)-1)) {
1043
+ $paging_nav .= " ⋅ " .
1044
+ $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action;h=$hash;pg=" . ($page+1)),
1045
+ -accesskey => "n", -title => "Alt-n"}, "next");
1046
+ } else {
1047
+ $paging_nav .= " ⋅ next";
1048
}
1049
+
1050
+ return $paging_nav;
1051
}
1052
1051
-sub git_read_refs {
1052
- my $ref_dir = shift;
1053
- my @reflist;
1053
+## ......................................................................
1054
+## functions printing or outputting HTML: div
1055
+
1056
+sub git_header_div {
1057
+ my ($action, $title, $hash, $hash_base) = @_;
1058
+ my $rest = '';
1059
+
1060
+ $rest .= ";h=$hash" if $hash;
1061
+ $rest .= ";hb=$hash_base" if $hash_base;
1062
+
1063
+ print "<div class=\"header\">\n" .
1064
+ $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action$rest"),
1065
+ -class => "title"}, $title ? $title : $action) . "\n" .
1066
+ "</div>\n";
1067
+}
1068
1055
- my @refs;
1056
- opendir my $dh, "$projectroot/$project/$ref_dir";
1057
- while (my $dir = readdir($dh)) {
1058
- if ($dir =~ m/^\./) {
1059
- next;
1060
- }
1061
- if (-d "$projectroot/$project/$ref_dir/$dir") {
1062
- opendir my $dh2, "$projectroot/$project/$ref_dir/$dir";
1063
- my @subdirs = grep !m/^\./, readdir $dh2;
1064
- closedir($dh2);
1065
- foreach my $subdir (@subdirs) {
1066
- push @refs, "$dir/$subdir"
1067
- }
1068
- next;
1069
- }
1070
- push @refs, $dir;
1071
- }
1072
- closedir($dh);
1073
- foreach my $ref_file (@refs) {
1074
- my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
1075
- my $type = git_get_type($ref_id) || next;
1076
- my %ref_item;
1077
- my %co;
1078
- $ref_item{'type'} = $type;
1079
- $ref_item{'id'} = $ref_id;
1080
- $ref_item{'epoch'} = 0;
1081
- $ref_item{'age'} = "unknown";
1082
- if ($type eq "tag") {
1083
- my %tag = git_read_tag($ref_id);
1084
- $ref_item{'comment'} = $tag{'comment'};
1085
- if ($tag{'type'} eq "commit") {
1086
- %co = git_read_commit($tag{'object'});
1087
- $ref_item{'epoch'} = $co{'committer_epoch'};
1088
- $ref_item{'age'} = $co{'age_string'};
1089
- } elsif (defined($tag{'epoch'})) {
1090
- my $age = time - $tag{'epoch'};
1091
- $ref_item{'epoch'} = $tag{'epoch'};
1092
- $ref_item{'age'} = age_string($age);
1093
- }
1094
- $ref_item{'reftype'} = $tag{'type'};
1095
- $ref_item{'name'} = $tag{'name'};
1096
- $ref_item{'refid'} = $tag{'object'};
1097
- } elsif ($type eq "commit"){
1098
- %co = git_read_commit($ref_id);
1099
- $ref_item{'reftype'} = "commit";
1100
- $ref_item{'name'} = $ref_file;
1101
- $ref_item{'title'} = $co{'title'};
1102
- $ref_item{'refid'} = $ref_id;
1103
- $ref_item{'epoch'} = $co{'committer_epoch'};
1104
- $ref_item{'age'} = $co{'age_string'};
1105
- } else {
1106
- $ref_item{'reftype'} = $type;
1107
- $ref_item{'name'} = $ref_file;
1108
- $ref_item{'refid'} = $ref_id;
1109
- }
1069
+sub git_print_page_path {
1070
+ my $name = shift;
1071
+ my $type = shift;
1072
1111
- push @reflist, \%ref_item;
1073
+ if (!defined $name) {
1074
+ print "<div class=\"page_path\"><b>/</b></div>\n";
1075
+ } elsif ($type =~ "blob") {
1076
+ print "<div class=\"page_path\"><b>" .
1077
+ $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;f=$file_name")}, esc_html($name)) . "</b><br/></div>\n";
1078
+ } else {
1079
+ print "<div class=\"page_path\"><b>" . esc_html($name) . "</b><br/></div>\n";
1080
}
1113
- # sort tags by age
1114
- @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1115
- return \@reflist;
1081
}
1082
1083
+## ......................................................................
1084
+## functions printing large fragments of HTML
1085
+
1086
sub git_shortlog_body {
1087
# uses global variable $project
1088
my ($revlist, $from, $to, $refs, $extra) = @_;
@@ -1163,106 +1131,291 @@ sub git_shortlog_body {
1131
print "</table>\n";
1132
}
1133
1166
-sub git_tags_body {
1167
- # uses global variable $project
1168
- my ($taglist, $from, $to, $extra) = @_;
1169
- $from = 0 unless defined $from;
1170
- $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1134
+sub git_tags_body {
1135
+ # uses global variable $project
1136
+ my ($taglist, $from, $to, $extra) = @_;
1137
+ $from = 0 unless defined $from;
1138
+ $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1139
+
1140
+ print "<table class=\"tags\" cellspacing=\"0\">\n";
1141
+ my $alternate = 0;
1142
+ for (my $i = $from; $i <= $to; $i++) {
1143
+ my $entry = $taglist->[$i];
1144
+ my %tag = %$entry;
1145
+ my $comment_lines = $tag{'comment'};
1146
+ my $comment = shift @$comment_lines;
1147
+ my $comment_short;
1148
+ if (defined $comment) {
1149
+ $comment_short = chop_str($comment, 30, 5);
1150
+ }
1151
+ if ($alternate) {
1152
+ print "<tr class=\"dark\">\n";
1153
+ } else {
1154
+ print "<tr class=\"light\">\n";
1155
+ }
1156
+ $alternate ^= 1;
1157
+ print "<td><i>$tag{'age'}</i></td>\n" .
1158
+ "<td>" .
1159
+ $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"),
1160
+ -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1161
+ "</td>\n" .
1162
+ "<td>";
1163
+ if (defined $comment) {
1164
+ if (length($comment_short) < length($comment)) {
1165
+ print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}"),
1166
+ -class => "list", -title => $comment}, $comment_short);
1167
+ } else {
1168
+ print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}"),
1169
+ -class => "list"}, $comment);
1170
+ }
1171
+ }
1172
+ print "</td>\n" .
1173
+ "<td class=\"selflink\">";
1174
+ if ($tag{'type'} eq "tag") {
1175
+ print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag");
1176
+ } else {
1177
+ print " ";
1178
+ }
1179
+ print "</td>\n" .
1180
+ "<td class=\"link\">" . " | " .
1181
+ $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
1182
+ if ($tag{'reftype'} eq "commit") {
1183
+ print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1184
+ " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
1185
+ } elsif ($tag{'reftype'} eq "blob") {
1186
+ print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$tag{'refid'}")}, "raw");
1187
+ }
1188
+ print "</td>\n" .
1189
+ "</tr>";
1190
+ }
1191
+ if (defined $extra) {
1192
+ print "<tr>\n" .
1193
+ "<td colspan=\"5\">$extra</td>\n" .
1194
+ "</tr>\n";
1195
+ }
1196
+ print "</table>\n";
1197
+}
1198
+
1199
+sub git_heads_body {
1200
+ # uses global variable $project
1201
+ my ($taglist, $head, $from, $to, $extra) = @_;
1202
+ $from = 0 unless defined $from;
1203
+ $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1204
+
1205
+ print "<table class=\"heads\" cellspacing=\"0\">\n";
1206
+ my $alternate = 0;
1207
+ for (my $i = $from; $i <= $to; $i++) {
1208
+ my $entry = $taglist->[$i];
1209
+ my %tag = %$entry;
1210
+ my $curr = $tag{'id'} eq $head;
1211
+ if ($alternate) {
1212
+ print "<tr class=\"dark\">\n";
1213
+ } else {
1214
+ print "<tr class=\"light\">\n";
1215
+ }
1216
+ $alternate ^= 1;
1217
+ print "<td><i>$tag{'age'}</i></td>\n" .
1218
+ ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1219
+ $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"),
1220
+ -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1221
+ "</td>\n" .
1222
+ "<td class=\"link\">" .
1223
+ $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") . " | " .
1224
+ $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1225
+ "</td>\n" .
1226
+ "</tr>";
1227
+ }
1228
+ if (defined $extra) {
1229
+ print "<tr>\n" .
1230
+ "<td colspan=\"3\">$extra</td>\n" .
1231
+ "</tr>\n";
1232
+ }
1233
+ print "</table>\n";
1234
+}
1235
+
1236
+## ----------------------------------------------------------------------
1237
+## functions printing large fragments, format as one of arguments
1238
+
1239
+sub git_diff_print {
1240
+ my $from = shift;
1241
+ my $from_name = shift;
1242
+ my $to = shift;
1243
+ my $to_name = shift;
1244
+ my $format = shift || "html";
1245
+
1246
+ my $from_tmp = "/dev/null";
1247
+ my $to_tmp = "/dev/null";
1248
+ my $pid = $$;
1249
+
1250
+ # create tmp from-file
1251
+ if (defined $from) {
1252
+ $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
1253
+ open my $fd2, "> $from_tmp";
1254
+ open my $fd, "-|", $GIT, "cat-file", "blob", $from;
1255
+ my @file = <$fd>;
1256
+ print $fd2 @file;
1257
+ close $fd2;
1258
+ close $fd;
1259
+ }
1260
+
1261
+ # create tmp to-file
1262
+ if (defined $to) {
1263
+ $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
1264
+ open my $fd2, "> $to_tmp";
1265
+ open my $fd, "-|", $GIT, "cat-file", "blob", $to;
1266
+ my @file = <$fd>;
1267
+ print $fd2 @file;
1268
+ close $fd2;
1269
+ close $fd;
1270
+ }
1271
+
1272
+ open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
1273
+ if ($format eq "plain") {
1274
+ undef $/;
1275
+ print <$fd>;
1276
+ $/ = "\n";
1277
+ } else {
1278
+ while (my $line = <$fd>) {
1279
+ chomp $line;
1280
+ my $char = substr($line, 0, 1);
1281
+ my $diff_class = "";
1282
+ if ($char eq '+') {
1283
+ $diff_class = " add";
1284
+ } elsif ($char eq "-") {
1285
+ $diff_class = " rem";
1286
+ } elsif ($char eq "@") {
1287
+ $diff_class = " chunk_header";
1288
+ } elsif ($char eq "\\") {
1289
+ # skip errors
1290
+ next;
1291
+ }
1292
+ while ((my $pos = index($line, "\t")) != -1) {
1293
+ if (my $count = (8 - (($pos-1) % 8))) {
1294
+ my $spaces = ' ' x $count;
1295
+ $line =~ s/\t/$spaces/;
1296
+ }
1297
+ }
1298
+ print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
1299
+ }
1300
+ }
1301
+ close $fd;
1302
+
1303
+ if (defined $from) {
1304
+ unlink($from_tmp);
1305
+ }
1306
+ if (defined $to) {
1307
+ unlink($to_tmp);
1308
+ }
1309
+}
1310
+
1311
+
1312
+## ======================================================================
1313
+## ======================================================================
1314
+## actions
1315
+
1316
+# git-logo (cached in browser for one day)
1317
+sub git_logo {
1318
+ binmode STDOUT, ':raw';
1319
+ print $cgi->header(-type => 'image/png', -expires => '+1d');
1320
+ # cat git-logo.png | hexdump -e '16/1 " %02x" "\n"' | sed 's/ /\\x/g'
1321
+ print "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
1322
+ "\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
1323
+ "\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
1324
+ "\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
1325
+ "\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
1326
+ "\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
1327
+ "\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
1328
+ "\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
1329
+ "\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
1330
+ "\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
1331
+ "\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
1332
+ "\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
1333
+ "\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
1334
+}
1335
1172
- print "<table class=\"tags\" cellspacing=\"0\">\n";
1173
- my $alternate = 0;
1174
- for (my $i = $from; $i <= $to; $i++) {
1175
- my $entry = $taglist->[$i];
1176
- my %tag = %$entry;
1177
- my $comment_lines = $tag{'comment'};
1178
- my $comment = shift @$comment_lines;
1179
- my $comment_short;
1180
- if (defined $comment) {
1181
- $comment_short = chop_str($comment, 30, 5);
1182
- }
1183
- if ($alternate) {
1184
- print "<tr class=\"dark\">\n";
1185
- } else {
1186
- print "<tr class=\"light\">\n";
1336
+sub git_project_list {
1337
+ my @list = git_read_projects();
1338
+ my @projects;
1339
+ if (!@list) {
1340
+ die_error(undef, "No project found.");
1341
+ }
1342
+ foreach my $pr (@list) {
1343
+ my $head = git_read_head($pr->{'path'});
1344
+ if (!defined $head) {
1345
+ next;
1346
}
1188
- $alternate ^= 1;
1189
- print "<td><i>$tag{'age'}</i></td>\n" .
1190
- "<td>" .
1191
- $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"),
1192
- -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1193
- "</td>\n" .
1194
- "<td>";
1195
- if (defined $comment) {
1196
- if (length($comment_short) < length($comment)) {
1197
- print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}"),
1198
- -class => "list", -title => $comment}, $comment_short);
1199
- } else {
1200
- print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}"),
1201
- -class => "list"}, $comment);
1202
- }
1347
+ $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
1348
+ my %co = git_read_commit($head);
1349
+ if (!%co) {
1350
+ next;
1351
}
1204
- print "</td>\n" .
1205
- "<td class=\"selflink\">";
1206
- if ($tag{'type'} eq "tag") {
1207
- print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag");
1208
- } else {
1209
- print " ";
1352
+ $pr->{'commit'} = \%co;
1353
+ if (!defined $pr->{'descr'}) {
1354
+ my $descr = git_read_description($pr->{'path'}) || "";
1355
+ $pr->{'descr'} = chop_str($descr, 25, 5);
1356
}
1211
- print "</td>\n" .
1212
- "<td class=\"link\">" . " | " .
1213
- $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
1214
- if ($tag{'reftype'} eq "commit") {
1215
- print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1216
- " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
1217
- } elsif ($tag{'reftype'} eq "blob") {
1218
- print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$tag{'refid'}")}, "raw");
1357
+ if (!defined $pr->{'owner'}) {
1358
+ $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
1359
}
1220
- print "</td>\n" .
1221
- "</tr>";
1360
+ push @projects, $pr;
1361
}
1223
- if (defined $extra) {
1224
- print "<tr>\n" .
1225
- "<td colspan=\"5\">$extra</td>\n" .
1226
- "</tr>\n";
1362
+ git_header_html();
1363
+ if (-f $home_text) {
1364
+ print "<div class=\"index_include\">\n";
1365
+ open (my $fd, $home_text);
1366
+ print <$fd>;
1367
+ close $fd;
1368
+ print "</div>\n";
1369
}
1228
- print "</table>\n";
1229
-}
1230
-
1231
-sub git_heads_body {
1232
- # uses global variable $project
1233
- my ($taglist, $head, $from, $to, $extra) = @_;
1234
- $from = 0 unless defined $from;
1235
- $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1236
-
1237
- print "<table class=\"heads\" cellspacing=\"0\">\n";
1370
+ print "<table class=\"project_list\">\n" .
1371
+ "<tr>\n";
1372
+ if (!defined($order) || (defined($order) && ($order eq "project"))) {
1373
+ @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
1374
+ print "<th>Project</th>\n";
1375
+ } else {
1376
+ print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=project")}, "Project") . "</th>\n";
1377
+ }
1378
+ if (defined($order) && ($order eq "descr")) {
1379
+ @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
1380
+ print "<th>Description</th>\n";
1381
+ } else {
1382
+ print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=descr")}, "Description") . "</th>\n";
1383
+ }
1384
+ if (defined($order) && ($order eq "owner")) {
1385
+ @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
1386
+ print "<th>Owner</th>\n";
1387
+ } else {
1388
+ print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=owner")}, "Owner") . "</th>\n";
1389
+ }
1390
+ if (defined($order) && ($order eq "age")) {
1391
+ @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
1392
+ print "<th>Last Change</th>\n";
1393
+ } else {
1394
+ print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=age")}, "Last Change") . "</th>\n";
1395
+ }
1396
+ print "<th></th>\n" .
1397
+ "</tr>\n";
1398
my $alternate = 0;
1239
- for (my $i = $from; $i <= $to; $i++) {
1240
- my $entry = $taglist->[$i];
1241
- my %tag = %$entry;
1242
- my $curr = $tag{'id'} eq $head;
1399
+ foreach my $pr (@projects) {
1400
if ($alternate) {
1401
print "<tr class=\"dark\">\n";
1402
} else {
1403
print "<tr class=\"light\">\n";
1404
}
1405
$alternate ^= 1;
1249
- print "<td><i>$tag{'age'}</i></td>\n" .
1250
- ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1251
- $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"),
1252
- -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1253
- "</td>\n" .
1406
+ print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary"), -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
1407
+ "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
1408
+ "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
1409
+ print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" . $pr->{'commit'}{'age_string'} . "</td>\n" .
1410
"<td class=\"link\">" .
1255
- $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") . " | " .
1256
- $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1411
+ $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary")}, "summary") .
1412
+ " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=shortlog")}, "shortlog") .
1413
+ " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=log")}, "log") .
1414
"</td>\n" .
1258
- "</tr>";
1259
- }
1260
- if (defined $extra) {
1261
- print "<tr>\n" .
1262
- "<td colspan=\"3\">$extra</td>\n" .
1415
"</tr>\n";
1416
}
1417
print "</table>\n";
1418
+ git_footer_html();
1419
}
1420
1421
sub git_summary {
@@ -1326,20 +1479,6 @@ sub git_summary {
1479
git_footer_html();
1480
}
1481
1329
-sub git_print_page_path {
1330
- my $name = shift;
1331
- my $type = shift;
1332
-
1333
- if (!defined $name) {
1334
- print "<div class=\"page_path\"><b>/</b></div>\n";
1335
- } elsif ($type =~ "blob") {
1336
- print "<div class=\"page_path\"><b>" .
1337
- $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;f=$file_name")}, esc_html($name)) . "</b><br/></div>\n";
1338
- } else {
1339
- print "<div class=\"page_path\"><b>" . esc_html($name) . "</b><br/></div>\n";
1340
- }
1341
-}
1342
-
1482
sub git_tag {
1483
my $head = git_read_head($project);
1484
git_header_html();
@@ -1500,128 +1639,50 @@ HTML
1639
my $spaces = ' ' x $count;
1640
$data =~ s/\t/$spaces/;
1641
}
1503
- }
1504
- $data = esc_html ($data);
1505
-
1506
- print <<HTML;
1507
- <tr class="$line_class[$line_class_num]">
1508
- <td class="sha1"><a href="$my_uri?${\esc_param ("p=$project;a=commit;h=$long_rev")}" class="text">$short_rev..</a></td>
1509
- <td class="$age_class">$age_str</td>
1510
- <td>$author</td>
1511
- <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
1512
- <td class="pre">$data</td>
1513
- </tr>
1514
-HTML
1515
- } # while (my $line = <$fd>)
1516
- print "</table>\n\n";
1517
- close $fd or print "Reading blob failed.\n";
1518
- print "</div>";
1519
- git_footer_html();
1520
-}
1521
-
1522
-sub git_tags {
1523
- my $head = git_read_head($project);
1524
- git_header_html();
1525
- git_page_nav('','', $head,undef,$head);
1526
- git_header_div('summary', $project);
1527
-
1528
- my $taglist = git_read_refs("refs/tags");
1529
- if (defined @$taglist) {
1530
- git_tags_body($taglist);
1531
- }
1532
- git_footer_html();
1533
-}
1534
-
1535
-sub git_heads {
1536
- my $head = git_read_head($project);
1537
- git_header_html();
1538
- git_page_nav('','', $head,undef,$head);
1539
- git_header_div('summary', $project);
1540
-
1541
- my $taglist = git_read_refs("refs/heads");
1542
- my $alternate = 0;
1543
- if (defined @$taglist) {
1544
- git_heads_body($taglist, $head);
1545
- }
1546
- git_footer_html();
1547
-}
1548
-
1549
-sub git_get_hash_by_path {
1550
- my $base = shift;
1551
- my $path = shift || return undef;
1552
-
1553
- my $tree = $base;
1554
-
1555
- open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
1556
- or die_error(undef, "Open git-ls-tree failed.");
1557
- my $line = <$fd>;
1558
- close $fd or return undef;
1559
-
1560
- #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1561
- $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1562
- return $3;
1563
-}
1564
-
1565
-sub mimetype_guess_file {
1566
- my $filename = shift;
1567
- my $mimemap = shift;
1568
- -r $mimemap or return undef;
1569
-
1570
- my %mimemap;
1571
- open(MIME, $mimemap) or return undef;
1572
- while (<MIME>) {
1573
- my ($mime, $exts) = split(/\t+/);
1574
- my @exts = split(/\s+/, $exts);
1575
- foreach my $ext (@exts) {
1576
- $mimemap{$ext} = $mime;
1577
- }
1578
- }
1579
- close(MIME);
1642
+ }
1643
+ $data = esc_html ($data);
1644
1581
- $filename =~ /\.(.*?)$/;
1582
- return $mimemap{$1};
1645
+ print <<HTML;
1646
+ <tr class="$line_class[$line_class_num]">
1647
+ <td class="sha1"><a href="$my_uri?${\esc_param ("p=$project;a=commit;h=$long_rev")}" class="text">$short_rev..</a></td>
1648
+ <td class="$age_class">$age_str</td>
1649
+ <td>$author</td>
1650
+ <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
1651
+ <td class="pre">$data</td>
1652
+ </tr>
1653
+HTML
1654
+ } # while (my $line = <$fd>)
1655
+ print "</table>\n\n";
1656
+ close $fd or print "Reading blob failed.\n";
1657
+ print "</div>";
1658
+ git_footer_html();
1659
}
1660
1585
-sub mimetype_guess {
1586
- my $filename = shift;
1587
- my $mime;
1588
- $filename =~ /\./ or return undef;
1661
+sub git_tags {
1662
+ my $head = git_read_head($project);
1663
+ git_header_html();
1664
+ git_page_nav('','', $head,undef,$head);
1665
+ git_header_div('summary', $project);
1666
1590
- if ($mimetypes_file) {
1591
- my $file = $mimetypes_file;
1592
- #$file =~ m#^/# or $file = "$projectroot/$path/$file";
1593
- $mime = mimetype_guess_file($filename, $file);
1667
+ my $taglist = git_read_refs("refs/tags");
1668
+ if (defined @$taglist) {
1669
+ git_tags_body($taglist);
1670
}
1595
- $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1596
- return $mime;
1671
+ git_footer_html();
1672
}
1673
1599
-sub git_blob_plain_mimetype {
1600
- my $fd = shift;
1601
- my $filename = shift;
1602
-
1603
- if ($filename) {
1604
- my $mime = mimetype_guess($filename);
1605
- $mime and return $mime;
1606
- }
1607
-
1608
- # just in case
1609
- return $default_blob_plain_mimetype unless $fd;
1674
+sub git_heads {
1675
+ my $head = git_read_head($project);
1676
+ git_header_html();
1677
+ git_page_nav('','', $head,undef,$head);
1678
+ git_header_div('summary', $project);
1679
1611
- if (-T $fd) {
1612
- return 'text/plain' .
1613
- ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1614
- } elsif (! $filename) {
1615
- return 'application/octet-stream';
1616
- } elsif ($filename =~ m/\.png$/i) {
1617
- return 'image/png';
1618
- } elsif ($filename =~ m/\.gif$/i) {
1619
- return 'image/gif';
1620
- } elsif ($filename =~ m/\.jpe?g$/i) {
1621
- return 'image/jpeg';
1622
- } else {
1623
- return 'application/octet-stream';
1680
+ my $taglist = git_read_refs("refs/heads");
1681
+ my $alternate = 0;
1682
+ if (defined @$taglist) {
1683
+ git_heads_body($taglist, $head);
1684
}
1685
+ git_footer_html();
1686
}
1687
1688
sub git_blob_plain {
@@ -1793,98 +1854,6 @@ sub git_tree {
1854
git_footer_html();
1855
}
1856
1796
-sub git_rss {
1797
- # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
1798
- open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_read_head($project)
1799
- or die_error(undef, "Open git-rev-list failed.");
1800
- my @revlist = map { chomp; $_ } <$fd>;
1801
- close $fd or die_error(undef, "Reading rev-list failed.");
1802
- print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1803
- print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1804
- "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
1805
- print "<channel>\n";
1806
- print "<title>$project</title>\n".
1807
- "<link>" . esc_html("$my_url?p=$project;a=summary") . "</link>\n".
1808
- "<description>$project log</description>\n".
1809
- "<language>en</language>\n";
1810
-
1811
- for (my $i = 0; $i <= $#revlist; $i++) {
1812
- my $commit = $revlist[$i];
1813
- my %co = git_read_commit($commit);
1814
- # we read 150, we always show 30 and the ones more recent than 48 hours
1815
- if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
1816
- last;
1817
- }
1818
- my %cd = date_str($co{'committer_epoch'});
1819
- open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
1820
- my @difftree = map { chomp; $_ } <$fd>;
1821
- close $fd or next;
1822
- print "<item>\n" .
1823
- "<title>" .
1824
- sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
1825
- "</title>\n" .
1826
- "<author>" . esc_html($co{'author'}) . "</author>\n" .
1827
- "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
1828
- "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
1829
- "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
1830
- "<description>" . esc_html($co{'title'}) . "</description>\n" .
1831
- "<content:encoded>" .
1832
- "<![CDATA[\n";
1833
- my $comment = $co{'comment'};
1834
- foreach my $line (@$comment) {
1835
- $line = decode("utf8", $line, Encode::FB_DEFAULT);
1836
- print "$line<br/>\n";
1837
- }
1838
- print "<br/>\n";
1839
- foreach my $line (@difftree) {
1840
- if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1841
- next;
1842
- }
1843
- my $file = validate_input(unquote($7));
1844
- $file = decode("utf8", $file, Encode::FB_DEFAULT);
1845
- print "$file<br/>\n";
1846
- }
1847
- print "]]>\n" .
1848
- "</content:encoded>\n" .
1849
- "</item>\n";
1850
- }
1851
- print "</channel></rss>";
1852
-}
1853
-
1854
-sub git_opml {
1855
- my @list = git_read_projects();
1856
-
1857
- print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1858
- print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1859
- "<opml version=\"1.0\">\n".
1860
- "<head>".
1861
- " <title>$site_name Git OPML Export</title>\n".
1862
- "</head>\n".
1863
- "<body>\n".
1864
- "<outline text=\"git RSS feeds\">\n";
1865
-
1866
- foreach my $pr (@list) {
1867
- my %proj = %$pr;
1868
- my $head = git_read_head($proj{'path'});
1869
- if (!defined $head) {
1870
- next;
1871
- }
1872
- $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
1873
- my %co = git_read_commit($head);
1874
- if (!%co) {
1875
- next;
1876
- }
1877
-
1878
- my $path = esc_html(chop_str($proj{'path'}, 25, 5));
1879
- my $rss = "$my_url?p=$proj{'path'};a=rss";
1880
- my $html = "$my_url?p=$proj{'path'};a=summary";
1881
- print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
1882
- }
1883
- print "</outline>\n".
1884
- "</body>\n".
1885
- "</opml>\n";
1886
-}
1887
-
1857
sub git_log {
1858
my $head = git_read_head($project);
1859
if (!defined $hash) {
@@ -2556,3 +2525,98 @@ sub git_shortlog {
2525
2526
git_footer_html();
2527
}
2528
+
2529
+## ......................................................................
2530
+## feeds (RSS, OPML)
2531
+
2532
+sub git_rss {
2533
+ # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
2534
+ open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_read_head($project)
2535
+ or die_error(undef, "Open git-rev-list failed.");
2536
+ my @revlist = map { chomp; $_ } <$fd>;
2537
+ close $fd or die_error(undef, "Reading rev-list failed.");
2538
+ print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
2539
+ print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
2540
+ "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
2541
+ print "<channel>\n";
2542
+ print "<title>$project</title>\n".
2543
+ "<link>" . esc_html("$my_url?p=$project;a=summary") . "</link>\n".
2544
+ "<description>$project log</description>\n".
2545
+ "<language>en</language>\n";
2546
+
2547
+ for (my $i = 0; $i <= $#revlist; $i++) {
2548
+ my $commit = $revlist[$i];
2549
+ my %co = git_read_commit($commit);
2550
+ # we read 150, we always show 30 and the ones more recent than 48 hours
2551
+ if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
2552
+ last;
2553
+ }
2554
+ my %cd = date_str($co{'committer_epoch'});
2555
+ open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
2556
+ my @difftree = map { chomp; $_ } <$fd>;
2557
+ close $fd or next;
2558
+ print "<item>\n" .
2559
+ "<title>" .
2560
+ sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
2561
+ "</title>\n" .
2562
+ "<author>" . esc_html($co{'author'}) . "</author>\n" .
2563
+ "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
2564
+ "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
2565
+ "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
2566
+ "<description>" . esc_html($co{'title'}) . "</description>\n" .
2567
+ "<content:encoded>" .
2568
+ "<![CDATA[\n";
2569
+ my $comment = $co{'comment'};
2570
+ foreach my $line (@$comment) {
2571
+ $line = decode("utf8", $line, Encode::FB_DEFAULT);
2572
+ print "$line<br/>\n";
2573
+ }
2574
+ print "<br/>\n";
2575
+ foreach my $line (@difftree) {
2576
+ if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
2577
+ next;
2578
+ }
2579
+ my $file = validate_input(unquote($7));
2580
+ $file = decode("utf8", $file, Encode::FB_DEFAULT);
2581
+ print "$file<br/>\n";
2582
+ }
2583
+ print "]]>\n" .
2584
+ "</content:encoded>\n" .
2585
+ "</item>\n";
2586
+ }
2587
+ print "</channel></rss>";
2588
+}
2589
+
2590
+sub git_opml {
2591
+ my @list = git_read_projects();
2592
+
2593
+ print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
2594
+ print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
2595
+ "<opml version=\"1.0\">\n".
2596
+ "<head>".
2597
+ " <title>$site_name Git OPML Export</title>\n".
2598
+ "</head>\n".
2599
+ "<body>\n".
2600
+ "<outline text=\"git RSS feeds\">\n";
2601
+
2602
+ foreach my $pr (@list) {
2603
+ my %proj = %$pr;
2604
+ my $head = git_read_head($proj{'path'});
2605
+ if (!defined $head) {
2606
+ next;
2607
+ }
2608
+ $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
2609
+ my %co = git_read_commit($head);
2610
+ if (!%co) {
2611
+ next;
2612
+ }
2613
+
2614
+ my $path = esc_html(chop_str($proj{'path'}, 25, 5));
2615
+ my $rss = "$my_url?p=$proj{'path'};a=rss";
2616
+ my $html = "$my_url?p=$proj{'path'};a=summary";
2617
+ print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
2618
+ }
2619
+ print "</outline>\n".
2620
+ "</body>\n".
2621
+ "</opml>\n";
2622
+}