gen_page.pl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my $top_level_dir = "/var/www/html/git/";
  5. my $projects_dir = $top_level_dir . "projects/";
  6. my $index = $top_level_dir . "index.html";
  7. my @ignore_dirs = ( "finance-perl.git/","misc-scripts.git/","git-site-gen.git/" );
  8. my @git_project_dirs;
  9. foreach my $dir ( split("\n",`ls -d */`) ) {
  10. chomp $dir;
  11. if ( grep( /^$dir$/, @ignore_dirs ) ) {
  12. print "Skipping $dir\n";
  13. next;
  14. }
  15. if ( $dir =~ /.git\/$/ ) {
  16. push(@git_project_dirs,$dir);
  17. }
  18. }
  19. my $write = sub {
  20. my $content = shift;
  21. my $path = shift;
  22. open(my $fh, ">", $path) or die "Couldnt open $path\n";
  23. print $fh "$content";
  24. close $fh;
  25. };
  26. my $append = sub {
  27. my $content = shift;
  28. my $path = shift;
  29. open(my $fh, ">>", $path) or die "Couldnt open $path\n";
  30. print $fh "$content";
  31. close $fh;
  32. };
  33. $write->("",$index);
  34. foreach my $dir ( split("\n", `cd $projects_dir ; ls -d */; cd /home/git`) ) {
  35. if ( ! grep( /^$dir$/, @git_project_dirs ) ) {
  36. print "Found $dir in webroot but not in git root, removing...\n";
  37. my $rmdir = $projects_dir . $dir;
  38. print "Removing $rmdir\n";
  39. system("rm -rf $rmdir") == 0 or die "Couldnt rm $dir\n";
  40. }
  41. }
  42. # Write index
  43. $append->("<html><body><b>Git Projects</b><br><head><META NAME=\"ROBOTS\" CONTENT=\"NOINDEX, NOFOLLOW\"></head>\n",$index);
  44. $append->("<small><i>Statically generated web root for browsing my git projects</i></small><br>",$index);
  45. $append->("<small><i>This is a read-only site and does not provide a merge/clone interface</i></small><hr/>",$index);
  46. foreach my $project ( @git_project_dirs ) {
  47. $append->("<table><div id=\"cotent\"><table id=\"index\"><tbody>",$index);
  48. $append->("<tr><td><a href=\"projects/$project/index.html\">$project</a></td>",$index);
  49. #$append->("<a href=\"projects/$project/index.html\">$project</a><br>",$index);
  50. system("mkdir -p $projects_dir$project") == 0 or die "Couldnt create $projects_dir/$project";
  51. }
  52. $append->("</tr></tbody></table></div></body></html>",$index);
  53. # Write files
  54. foreach my $project ( @git_project_dirs ) {
  55. my $spec_project_dir = $projects_dir . $project . "/";
  56. my $project_index = $spec_project_dir . "/index.html";
  57. $write->("",$project_index);
  58. $append->("<html><a href=\"../../../index.html\">Return to index</a></b><hr/>",$project_index);
  59. $append->("<b>Files for $project</b><br>",$project_index);
  60. $append->("<hr/>",$project_index);
  61. my %fileTree;
  62. foreach my $file ( split("\n", `cd $project ; git ls-tree --full-tree -r HEAD; cd ..`) ) {
  63. chomp $file;
  64. $file =~ /([a-z0-9]{40})\t(.*)$/;
  65. # Name - commit hash
  66. $fileTree{$2} = $1;
  67. }
  68. my %fileContent;
  69. foreach my $filename ( keys %fileTree ) {
  70. my $content = `cd $project ; git show $fileTree{$filename} ; cd ..`;
  71. chomp $content;
  72. $fileContent{$filename} = $content;
  73. }
  74. $append->("<table><div id=\"cotent\"><table id=\"index\"><thead><tr><td><b>File</b></td><td><b>Commit</b></td></tr></thead><tbody>",$project_index);
  75. foreach my $filename ( sort keys %fileContent ) {
  76. my $browserVersion = $filename . ".txt";
  77. if ( $filename =~ m/\// ) {
  78. my $copy = $filename;
  79. $copy =~ s/\//_/g;
  80. $browserVersion = $copy . ".txt";
  81. }
  82. #$append->("$fileTree{$filename} - <a href=\"$browserVersion\">$filename</a><br>",$project_index);
  83. $append->("<tr><td><a href=\"$browserVersion\">$filename</a></td><td>$fileTree{$filename}</td>",$project_index);
  84. $write->("$fileContent{$filename}",$spec_project_dir . $browserVersion);
  85. }
  86. $append->("</tr></tbody></table></div></body>",$project_index);
  87. $append->("<br>", $project_index);
  88. }
  89. # Get logs hash and write out logs page
  90. foreach my $project ( @git_project_dirs ) {
  91. my $spec_project_dir = $projects_dir . $project . "/";
  92. my $project_index = $spec_project_dir . "/index.html";
  93. my @commit_ids;
  94. foreach my $log_line ( split("\n",`cd $project; git log ; cd ..`) ) {
  95. if ( $log_line =~ m/commit\ ([a-z0-9]{40})/ ) {
  96. push(@commit_ids,$1);
  97. }
  98. }
  99. # Key: commit SHA, value commit info (git show)
  100. my %commits;
  101. foreach my $commit_id ( @commit_ids ) {
  102. my $commit_info = `cd $project; git show $commit_id; cd ..`;
  103. chomp $commit_info;
  104. $commits{$commit_id} = $commit_info;
  105. }
  106. $append->("<html><b>Logs for $project</b><br>",$project_index);
  107. $append->("<table><div id=\"cotent\"><table id=\"index\"><tbody>",$project_index);
  108. $append->("<hr/>",$project_index);
  109. # iterate over array to keep ordering
  110. foreach my $commit_id ( @commit_ids ) {
  111. my $filename = $commit_id . ".txt";
  112. #$append->("<a href=\"$filename\">$commit_id</a><br>",$project_index);
  113. $append->("<tr><td><a href=\"$filename\">$filename</a></td>",$project_index);
  114. $write->($commits{$commit_id},$spec_project_dir . $filename);
  115. }
  116. $append->("</tr></tbody></table></div></body>",$project_index);
  117. $append->("</html>",$project_index);
  118. }
  119. print "Done\n";