-
Notifications
You must be signed in to change notification settings - Fork 4
/
gitweb.cgi.source.new
2466 lines (2328 loc) · 77 KB
/
gitweb.cgi.source.new
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# gitweb - simple web interface to track changes in git repositories
#
# (C) 2005, Kay Sievers <[email protected]>
# (C) 2005, Christian Gierke <[email protected]>
#
# This program is licensed under the GPLv2
use strict;
use warnings;
use CGI qw(:standard :escapeHTML -nosticky);
use CGI::Util qw(unescape);
use CGI::Carp qw(fatalsToBrowser);
use Encode;
use Fcntl ':mode';
use File::Temp qw(tempfile);
binmode STDOUT, ':utf8';
my $cgi = new CGI;
my $version = "264";
my $my_url = $cgi->url();
my $my_uri = $cgi->url(-absolute => 1);
my $rss_link = "";
# absolute fs-path which will be prepended to the project path
#my $projectroot = "/pub/scm";
my $projectroot = "/home/kay/public_html/pub/scm";
# location of the git-core binaries
my $gitbin = "/usr/bin";
# location for temporary files needed for diffs
my $git_temp = "/tmp/gitweb";
# target of the home link on top of all pages
my $home_link = $my_uri;
# html text to include at home page
my $home_text = "indextext.html";
# source of projects list
#my $projects_list = $projectroot;
my $projects_list = "index/index.aux";
# where to find etags
my $etagsbin = "etags";
my $snapshots_url = "http://git.xmms.se/snapshot.cgi";
my $git_base_url = "git://git.xmms.se/xmms2";
# syntax highlighting
my %highlight = (
"SConstruct" => "py",
"Program" => "py",
"Library" => "py",
"\.py\$" => "py",
"\.c\$" => "c",
"\.h\$" => "c",
"\.cpp\$" => "cpp",
"\.rb\$" => "ruby",
"\.java\$" => "java",
"\.cgi\$" => "pl",
"\.css\$" => "css",
"\.cfg\$" => "ini",
"\.kid\$" => "html",
"Makefile" => "make",
"\.pyx\$" => "pyx",
"\.js\$" => "js",
);
# custom stuff - mantis/commit tags integration
sub committags_real {
my $a = shift;
my $href = shift || undef;
my $class = shift || "";
my $aStart = "";
my $aEnd = "";
if (defined $href) {
$aStart = "</a>";
if ($class ne "") {
$class = "class=\"$class\"";
}
$aEnd = "<a $class href=\"$href\">";
}
#$a =~ s!
# (?<=BUG\()
# ([\d,\s]+)
# (?=\))
# !
# join q{, }, map {
# qq[$aStart<a href="http://bugs.xmms2.xmms.se/view.php?id=$_">$_</a>$aEnd]
# } split /,\s*/, $1 !xge;
#$a =~ s!
# (?<=FEATURE\()
# ([\d,\s]+)
# (?=\))
# !
# join q{, }, map {
# qq[$aStart<a href="http://bugs.xmms2.xmms.se/view.php?id=$_">$_</a>$aEnd]
# } split /,\s*/, $1 !xge;
#$a =~ s!RELEASE: (.*)!RELEASE: $aStart<a href="http://wiki.xmms2.xmms.se/index.php/Release:$1">$1</a>$aEnd!g;
return $a;
}
sub committags($){
my $a = shift;
return committags_real($a);
}
sub committags_shortlog($$){
my $a = shift;
my $href = shift;
return committags_real($a, $href, "list");
}
# custom stuff - refactored navbar
sub git_navbar($$$$) {
my $project = shift;
my $head = shift;
my $curSection = shift;
my $extraLinks = shift;
my $sumLink = $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary");
my $slogLink = $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog");
my $logLink = $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log");
my $comLink = $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit");
my $comdifLink = $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff");
my $treeLink = $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree")}, "tree");
my $snapLink = $cgi->a({-href => "$snapshots_url?tree=$project&commit=HEAD"}, "latest snapshot");
if ($curSection eq "summary") {
$sumLink = "summary";
}
elsif ($curSection eq "shortlog") {
$slogLink = "shortlog";
}
elsif ($curSection eq "log") {
$logLink = "log";
}
elsif ($curSection eq "commit") {
$comLink = "commit";
}
elsif ($curSection eq "commitdiff") {
$comdifLink = "commitdiff";
}
elsif ($curSection eq "tree") {
$treeLink = "tree";
}
my $retVal = "<div class=\"page_nav\">\n" .
$sumLink .
" | " . $slogLink .
" | " . $logLink .
" | " . $comLink .
" | " . $comdifLink .
" | " . $treeLink .
" | " . $snapLink .
"<br/>$extraLinks<br/>\n" .
"</div>\n";
return $retVal;
}
# input validation and dispatch
my $action = $cgi->param('a');
if (defined $action) {
if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
undef $action;
die_error(undef, "Invalid action parameter.");
}
if ($action eq "git-logo.png") {
git_logo();
exit;
} elsif ($action eq "opml") {
git_opml();
exit;
}
}
my $order = $cgi->param('o');
if (defined $order) {
if ($order =~ m/[^0-9a-zA-Z_]/) {
undef $order;
die_error(undef, "Invalid order parameter.");
}
}
my $project = $cgi->param('p');
if (defined $project) {
$project = validate_input($project);
if (!defined($project)) {
die_error(undef, "Invalid project parameter.");
}
if (!(-d "$projectroot/$project")) {
undef $project;
die_error(undef, "No such directory.");
}
if (!(-e "$projectroot/$project/HEAD")) {
undef $project;
die_error(undef, "No such project.");
}
$rss_link = "<link rel=\"alternate\" title=\"" . esc_param($project) . " log\" href=\"" .
"$my_uri?" . esc_param("p=$project;a=rss") . "\" type=\"application/rss+xml\"/>";
$ENV{'GIT_DIR'} = "$projectroot/$project";
} else {
git_project_list();
exit;
}
my $file_name = $cgi->param('f');
if (defined $file_name) {
$file_name = validate_input($file_name);
if (!defined($file_name)) {
die_error(undef, "Invalid file parameter.");
}
}
my $hash = $cgi->param('h');
if (defined $hash) {
$hash = validate_input($hash);
if (!defined($hash)) {
die_error(undef, "Invalid hash parameter.");
}
}
my $hash_parent = $cgi->param('hp');
if (defined $hash_parent) {
$hash_parent = validate_input($hash_parent);
if (!defined($hash_parent)) {
die_error(undef, "Invalid hash parent parameter.");
}
}
my $hash_base = $cgi->param('hb');
if (defined $hash_base) {
$hash_base = validate_input($hash_base);
if (!defined($hash_base)) {
die_error(undef, "Invalid hash base parameter.");
}
}
my $page = $cgi->param('pg');
if (defined $page) {
if ($page =~ m/[^0-9]$/) {
undef $page;
die_error(undef, "Invalid page parameter.");
}
}
my $searchtext = $cgi->param('s');
if (defined $searchtext) {
if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
undef $searchtext;
die_error(undef, "Invalid search parameter.");
}
$searchtext = quotemeta $searchtext;
}
sub validate_input {
my $input = shift;
if ($input =~ m/^[0-9a-fA-F]{40}$/) {
return $input;
}
if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
return undef;
}
if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
return undef;
}
return $input;
}
if (!defined $action || $action eq "summary") {
git_summary();
exit;
} elsif ($action eq "heads") {
git_heads();
exit;
} elsif ($action eq "tags") {
git_tags();
exit;
} elsif ($action eq "blob") {
git_blob();
exit;
} elsif ($action eq "blob_plain") {
git_blob_plain();
exit;
} elsif ($action eq "tree") {
git_tree();
exit;
} elsif ($action eq "rss") {
git_rss();
exit;
} elsif ($action eq "commit") {
git_commit();
exit;
} elsif ($action eq "log") {
git_log();
exit;
} elsif ($action eq "blobdiff") {
git_blobdiff();
exit;
} elsif ($action eq "blobdiff_plain") {
git_blobdiff_plain();
exit;
} elsif ($action eq "commitdiff") {
git_commitdiff();
exit;
} elsif ($action eq "commitdiff_plain") {
git_commitdiff_plain();
exit;
} elsif ($action eq "history") {
git_history();
exit;
} elsif ($action eq "search") {
git_search();
exit;
} elsif ($action eq "shortlog") {
git_shortlog();
exit;
} elsif ($action eq "tag") {
git_tag();
exit;
} else {
undef $action;
die_error(undef, "Unknown action.");
exit;
}
# quote unsafe chars, but keep the slash, even when it's not
# correct, but quoted slashes look too horrible in bookmarks
sub esc_param {
my $str = shift;
$str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
$str =~ s/\+/%2B/g;
$str =~ s/ /\+/g;
return $str;
}
# replace invalid utf8 character with SUBSTITUTION sequence
sub esc_html {
my $str = shift;
$str = decode("utf8", $str, Encode::FB_DEFAULT);
$str = escapeHTML($str);
return $str;
}
# git may return quoted and escaped filenames
sub unquote {
my $str = shift;
if ($str =~ m/^"(.*)"$/) {
$str = $1;
$str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
}
return $str;
}
sub git_header_html {
my $status = shift || "200 OK";
my $expires = shift;
my $title = "git";
if (defined $project) {
$title .= " - $project";
if (defined $action) {
$title .= "/$action";
}
}
print $cgi->header(-type=>'text/html', -charset => 'utf-8', -status=> $status, -expires => $expires);
print <<EOF;
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<!-- git web interface v$version, (C) 2005, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke <ch\@gierke.de> -->
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta name="robots" content="index, nofollow"/>
<title>$title</title>
$rss_link
<link rel="stylesheet" type="text/css" href="gitweb.css"/>
</head>
<body>
EOF
print "<div class=\"page_header\">\n" .
"<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
"<img src=\"$my_uri?" . esc_param("a=git-logo.png") . "\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
"</a>\n";
print $cgi->a({-href => esc_param($home_link)}, "projects") . " / ";
if (defined $project) {
print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, esc_html($project));
if (defined $action) {
print " / $action";
}
print "\n";
if (!defined $searchtext) {
$searchtext = "";
}
my $search_hash;
if (defined $hash) {
$search_hash = $hash;
} else {
$search_hash = "HEAD";
}
$cgi->param("a", "search");
$cgi->param("h", $search_hash);
print $cgi->start_form(-method => "get", -action => $my_uri) .
"<div class=\"search\">\n" .
$cgi->hidden(-name => "p") . "\n" .
$cgi->hidden(-name => "a") . "\n" .
$cgi->hidden(-name => "h") . "\n" .
$cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
"</div>" .
$cgi->end_form() . "\n";
}
print "</div>\n";
}
sub git_footer_html {
print "<div class=\"page_footer\">\n";
if (defined $project) {
my $descr = git_read_description($project);
if (defined $descr) {
print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
}
print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=rss"), -class => "rss_logo"}, "RSS") . "\n";
} else {
print $cgi->a({-href => "$my_uri?" . esc_param("a=opml"), -class => "rss_logo"}, "OPML") . "\n";
}
print "</div>\n" .
"</body>\n" .
"</html>";
}
sub die_error {
my $status = shift || "403 Forbidden";
my $error = shift || "Malformed query, file missing or permission denied";
git_header_html($status);
print "<div class=\"page_body\">\n" .
"<br/><br/>\n" .
"$status - $error\n" .
"<br/>\n" .
"</div>\n";
git_footer_html();
exit;
}
sub git_get_type {
my $hash = shift;
open my $fd, "-|", "$gitbin/git-cat-file -t $hash" or return;
my $type = <$fd>;
close $fd or return;
chomp $type;
return $type;
}
sub git_read_head {
my $project = shift;
my $oENV = $ENV{'GIT_DIR'};
my $retval = undef;
$ENV{'GIT_DIR'} = "$projectroot/$project";
if (open my $fd, "-|", "$gitbin/git-rev-parse", "--verify", "HEAD") {
my $head = <$fd>;
close $fd;
if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
$retval = $1;
}
}
if (defined $oENV) {
$ENV{'GIT_DIR'} = $oENV;
}
return $retval;
}
sub git_read_hash {
my $path = shift;
open my $fd, "$projectroot/$path" or return undef;
my $head = <$fd>;
close $fd;
chomp $head;
if ($head =~ m/^[0-9a-fA-F]{40}$/) {
return $head;
}
}
sub git_read_file {
my $path = shift;
open my $fd, "$path" or return undef;
my $content = <$fd>;
close $fd;
chomp $content;
return $content;
}
sub git_read_description {
my $path = shift;
return git_read_file("$projectroot/$path/description");
}
sub git_read_category {
my $path = shift;
return git_read_file("$projectroot/$path/category");
}
sub git_read_tag {
my $tag_id = shift;
my %tag;
my @comment;
open my $fd, "-|", "$gitbin/git-cat-file tag $tag_id" or return;
$tag{'id'} = $tag_id;
while (my $line = <$fd>) {
chomp $line;
if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
$tag{'object'} = $1;
} elsif ($line =~ m/^type (.+)$/) {
$tag{'type'} = $1;
} elsif ($line =~ m/^tag (.+)$/) {
$tag{'name'} = $1;
} elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
$tag{'author'} = $1;
$tag{'epoch'} = $2;
$tag{'tz'} = $3;
} elsif ($line =~ m/--BEGIN/) {
push @comment, $line;
last;
} elsif ($line eq "") {
last;
}
}
push @comment, <$fd>;
$tag{'comment'} = \@comment;
close $fd or return;
if (!defined $tag{'name'}) {
return
};
return %tag
}
sub age_string {
my $age = shift;
my $age_str;
if ($age > 60*60*24*365*2) {
$age_str = (int $age/60/60/24/365);
$age_str .= " years ago";
} elsif ($age > 60*60*24*(365/12)*2) {
$age_str = int $age/60/60/24/(365/12);
$age_str .= " months ago";
} elsif ($age > 60*60*24*7*2) {
$age_str = int $age/60/60/24/7;
$age_str .= " weeks ago";
} elsif ($age > 60*60*24*2) {
$age_str = int $age/60/60/24;
$age_str .= " days ago";
} elsif ($age > 60*60*2) {
$age_str = int $age/60/60;
$age_str .= " hours ago";
} elsif ($age > 60*2) {
$age_str = int $age/60;
$age_str .= " min ago";
} elsif ($age > 2) {
$age_str = int $age;
$age_str .= " sec ago";
} else {
$age_str .= " right now";
}
return $age_str;
}
sub git_read_commit {
my $commit_id = shift;
my $commit_text = shift;
my @commit_lines;
my %co;
if (defined $commit_text) {
@commit_lines = @$commit_text;
} else {
$/ = "\0";
open my $fd, "-|", "$gitbin/git-rev-list --header --parents --max-count=1 $commit_id" or return;
@commit_lines = split '\n', <$fd>;
close $fd or return;
$/ = "\n";
pop @commit_lines;
}
my $header = shift @commit_lines;
if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
return;
}
($co{'id'}, my @parents) = split ' ', $header;
$co{'parents'} = \@parents;
$co{'parent'} = $parents[0];
while (my $line = shift @commit_lines) {
last if $line eq "\n";
if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
$co{'tree'} = $1;
} elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
$co{'author'} = $1;
$co{'author_epoch'} = $2;
$co{'author_tz'} = $3;
if ($co{'author'} =~ m/^([^<]+) </) {
$co{'author_name'} = $1;
} else {
$co{'author_name'} = $co{'author'};
}
} elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
$co{'committer'} = $1;
$co{'committer_epoch'} = $2;
$co{'committer_tz'} = $3;
$co{'committer_name'} = $co{'committer'};
$co{'committer_name'} =~ s/ <.*//;
}
}
if (!defined $co{'tree'}) {
return;
};
foreach my $title (@commit_lines) {
$title =~ s/^ //;
if ($title ne "") {
$co{'title'} = chop_str($title, 80, 5);
# remove leading stuff of merges to make the interesting part visible
if (length($title) > 50) {
$title =~ s/^Automatic //;
$title =~ s/^merge (of|with) /Merge ... /i;
if (length($title) > 50) {
$title =~ s/(http|rsync):\/\///;
}
if (length($title) > 50) {
$title =~ s/(master|www|rsync)\.//;
}
if (length($title) > 50) {
$title =~ s/kernel.org:?//;
}
if (length($title) > 50) {
$title =~ s/\/pub\/scm//;
}
}
$co{'title_short'} = chop_str($title, 50, 5);
last;
}
}
# remove added spaces
foreach my $line (@commit_lines) {
$line =~ s/^ //;
}
$co{'comment'} = \@commit_lines;
my $age = time - $co{'committer_epoch'};
$co{'age'} = $age;
$co{'age_string'} = age_string($age);
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
if ($age > 60*60*24*7*2) {
$co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
$co{'age_string_age'} = $co{'age_string'};
} else {
$co{'age_string_date'} = $co{'age_string'};
$co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
}
return %co;
}
sub git_diff_print {
my $from = shift;
my $from_name = shift;
my $to = shift;
my $to_name = shift;
my $format = shift || "html";
my $from_tmp = "/dev/null";
my $to_tmp = "/dev/null";
my $pid = $$;
# create tmp from-file
if (defined $from) {
$from_tmp = "$git_temp/gitweb_" . $$ . "_from";
open my $fd2, "> $from_tmp";
open my $fd, "-|", "$gitbin/git-cat-file blob $from";
my @file = <$fd>;
print $fd2 @file;
close $fd2;
close $fd;
}
# create tmp to-file
if (defined $to) {
$to_tmp = "$git_temp/gitweb_" . $$ . "_to";
open my $fd2, "> $to_tmp";
open my $fd, "-|", "$gitbin/git-cat-file blob $to";
my @file = <$fd>;
print $fd2 @file;
close $fd2;
close $fd;
}
open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
if ($format eq "plain") {
undef $/;
print <$fd>;
$/ = "\n";
} else {
while (my $line = <$fd>) {
chomp($line);
my $char = substr($line, 0, 1);
my $color = "";
if ($char eq '+') {
$color = " style=\"color:#008800;\"";
} elsif ($char eq "-") {
$color = " style=\"color:#cc0000;\"";
} elsif ($char eq "@") {
$color = " style=\"color:#990099;\"";
} elsif ($char eq "\\") {
# skip errors
next;
}
while ((my $pos = index($line, "\t")) != -1) {
if (my $count = (8 - (($pos-1) % 8))) {
my $spaces = ' ' x $count;
$line =~ s/\t/$spaces/;
}
}
print "<div class=\"pre\"$color>" . esc_html($line) . "</div>\n";
}
}
close $fd;
if (defined $from) {
unlink($from_tmp);
}
if (defined $to) {
unlink($to_tmp);
}
}
sub mode_str {
my $mode = oct shift;
if (S_ISDIR($mode & S_IFMT)) {
return 'drwxr-xr-x';
} elsif (S_ISLNK($mode)) {
return 'lrwxrwxrwx';
} elsif (S_ISREG($mode)) {
# git cares only about the executable bit
if ($mode & S_IXUSR) {
return '-rwxr-xr-x';
} else {
return '-rw-r--r--';
};
} else {
return '----------';
}
}
sub chop_str {
my $str = shift;
my $len = shift;
my $add_len = shift || 10;
# allow only $len chars, but don't cut a word if it would fit in $add_len
# if it doesn't fit, cut it if it's still longer than the dots we would add
$str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
my $body = $1;
my $tail = $2;
if (length($tail) > 4) {
$tail = " ...";
}
return "$body$tail";
}
sub file_type {
my $mode = oct shift;
if (S_ISDIR($mode & S_IFMT)) {
return "directory";
} elsif (S_ISLNK($mode)) {
return "symlink";
} elsif (S_ISREG($mode)) {
return "file";
} else {
return "unknown";
}
}
sub format_log_line_html {
my $line = shift;
$line = esc_html($line);
$line =~ s/ / /g;
if ($line =~ m/([0-9a-fA-F]{40})/) {
my $hash_text = $1;
if (git_get_type($hash_text) eq "commit") {
my $link = $cgi->a({-class => "text", -href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_text")}, $hash_text);
$line =~ s/$hash_text/$link/;
}
}
return committags($line);
}
sub date_str {
my $epoch = shift;
my $tz = shift || "-0000";
my %date;
my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
$date{'hour'} = $hour;
$date{'minute'} = $min;
$date{'mday'} = $mday;
$date{'day'} = $days[$wday];
$date{'month'} = $months[$mon];
$date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
$date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
$tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
my $local = $epoch + ((int $1 + ($2/60)) * 3600);
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
$date{'hour_local'} = $hour;
$date{'minute_local'} = $min;
$date{'tz_local'} = $tz;
return %date;
}
# git-logo (cached in browser for one day)
sub git_logo {
binmode STDOUT, ':raw';
print $cgi->header(-type => 'image/png', -expires => '+1d');
# cat git-logo.png | hexdump -e '16/1 " %02x" "\n"' | sed 's/ /\\x/g'
print "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
"\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
"\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
"\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
"\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
"\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
"\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
"\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
"\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
"\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
"\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
"\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
"\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
}
sub get_file_owner {
my $path = shift;
my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
if (!defined $gcos) {
return undef;
}
my $owner = $gcos;
$owner =~ s/[,;].*$//;
return $owner;
}
sub git_read_projects {
my @list;
if (-d $projects_list) {
# search in directory
my $dir = $projects_list;
opendir my $dh, $dir or return undef;
while (my $dir = readdir($dh)) {
if (-e "$projectroot/$dir/HEAD") {
my $pr = {
path => $dir,
};
push @list, $pr
}
}
closedir($dh);
} elsif (-f $projects_list) {
# read from file(url-encoded):
# 'git%2Fgit.git Linus+Torvalds'
# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
open my $fd , $projects_list or return undef;
while (my $line = <$fd>) {
chomp $line;
my ($path, $owner) = split ' ', $line;
$path = unescape($path);
$owner = unescape($owner);
if (!defined $path) {
next;
}
if (-e "$projectroot/$path/HEAD") {
my $pr = {
path => $path,
owner => $owner,
};
push @list, $pr
}
}
close $fd;
}
@list = sort {$a->{'path'} cmp $b->{'path'}} @list;
return @list;
}
sub git_project_list {
my @list = git_read_projects();
my %projects;
if (!@list) {
die_error(undef, "No project found.");
}
foreach my $pr (@list) {
my $head = git_read_head($pr->{'path'});
if (!defined $head) {
next;
}
$ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
my %co = git_read_commit($head);
if (!%co) {
next;
}
$pr->{'commit'} = \%co;
if (!defined $pr->{'descr'}) {
my $descr = git_read_description($pr->{'path'}) || "";
$pr->{'descr'} = chop_str($descr, 25, 5);
}
if (!defined $pr->{'category'}) {
my $cat = git_read_category($pr->{'path'}) || "Misc";
$pr->{'category'} = $cat;
}
if (!defined $pr->{'owner'}) {
$pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
}
if (defined $projects{$pr->{'category'}}) {
push @{$projects{$pr->{'category'}}}, $pr;
}
else {
my @catList = ($pr);
$projects{$pr->{'category'}} = \@catList;
}
}
git_header_html();
if (-f $home_text) {
print "<div class=\"index_include\">\n";
open (my $fd, $home_text);
print <$fd>;
close $fd;
print "</div>\n";
}
print "<table cellspacing=\"0\">\n" .
"<tr>\n";
print "<th></th>\n" .
"</tr>\n";
my $alternate = 0;
my $headRow = "";
my $headDone = 0;
foreach my $cat (sort keys %projects) {
my @catList = @{$projects{"$cat"}};
if (!defined($order) || (defined($order) && ($order eq "project"))) {
@catList = sort {$a->{'path'} cmp $b->{'path'}} @catList;
$headRow .= "<th>Project</th>\n";
} else {
$headRow .= "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=project")}, "Project") . "</th>\n";
}
if (defined($order) && ($order eq "descr")) {
@catList = sort {$a->{'descr'} cmp $b->{'descr'}} @catList;
$headRow .= "<th>Description</th>\n";
} else {
$headRow .= "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=descr")}, "Description") . "</th>\n";
}
if (defined($order) && ($order eq "owner")) {
@catList = sort {$a->{'owner'} cmp $b->{'owner'}} @catList;
$headRow .= "<th>Owner</th>\n";
} else {
$headRow .= "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=owner")}, "Owner") . "</th>\n";
}
if (defined($order) && ($order eq "age")) {
@catList = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @catList;
$headRow .= "<th>Last Change</th>\n";
} else {
$headRow .= "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=age")}, "Last Change") . "</th>\n";
}
if (!$headDone) {
print '<tr>'.$headRow.'</tr>';
$headDone = 1;
}
print "<tr><td colspan=\"5\"><b>" . $cat . "</b></td><tr>";
foreach my $pr (@catList) {
if ($alternate) {
print "<tr class=\"dark\">\n";
} else {
print "<tr class=\"light\">\n";
}
$alternate ^= 1;