gen_page.pl 4.7 KB

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