#!/usr/bin/perl use strict; use warnings; my $top_level_dir = "/var/www/html/git/"; my $projects_dir = $top_level_dir . "projects/"; my $index = $top_level_dir . "index.html"; my @ignore_dirs = ( "finance-perl.git/","misc-scripts.git/","git-site-gen.git/" ); my @git_project_dirs; foreach my $dir ( split("\n",`ls -d */`) ) { chomp $dir; if ( grep( /^$dir$/, @ignore_dirs ) ) { print "Skipping $dir\n"; next; } if ( $dir =~ /.git\/$/ ) { push(@git_project_dirs,$dir); } } my $write = sub { my $content = shift; my $path = shift; open(my $fh, ">", $path) or die "Couldnt open $path\n"; print $fh "$content"; close $fh; }; my $append = sub { my $content = shift; my $path = shift; open(my $fh, ">>", $path) or die "Couldnt open $path\n"; print $fh "$content"; close $fh; }; $write->("",$index); foreach my $dir ( split("\n", `cd $projects_dir ; ls -d */; cd /home/git`) ) { if ( ! grep( /^$dir$/, @git_project_dirs ) ) { print "Found $dir in webroot but not in git root, removing...\n"; my $rmdir = $projects_dir . $dir; print "Removing $rmdir\n"; system("rm -rf $rmdir") == 0 or die "Couldnt rm $dir\n"; } } # Write index $append->("Git Projects
\n",$index); $append->("Statically generated web root for browsing my git projects
",$index); $append->("This is a read-only site and does not provide a merge/clone interface
",$index); foreach my $project ( @git_project_dirs ) { $append->("
",$index); $append->("",$index); #$append->("$project
",$index); system("mkdir -p $projects_dir$project") == 0 or die "Couldnt create $projects_dir/$project"; } $append->("
$project
",$index); # Write files foreach my $project ( @git_project_dirs ) { my $spec_project_dir = $projects_dir . $project . "/"; my $project_index = $spec_project_dir . "/index.html"; $write->("",$project_index); $append->("Return to index
",$project_index); $append->("Files for $project
",$project_index); $append->("
",$project_index); my %fileTree; foreach my $file ( split("\n", `cd $project ; git ls-tree --full-tree -r HEAD; cd ..`) ) { chomp $file; $file =~ /([a-z0-9]{40})\t(.*)$/; # Name - commit hash $fileTree{$2} = $1; } my %fileContent; foreach my $filename ( keys %fileTree ) { my $content = `cd $project ; git show $fileTree{$filename} ; cd ..`; chomp $content; $fileContent{$filename} = $content; } $append->("
",$project_index); foreach my $filename ( sort keys %fileContent ) { my $browserVersion = $filename . ".txt"; if ( $filename =~ m/\// ) { my $copy = $filename; $copy =~ s/\//_/g; $browserVersion = $copy . ".txt"; } #$append->("$fileTree{$filename} - $filename
",$project_index); $append->("",$project_index); $write->("$fileContent{$filename}",$spec_project_dir . $browserVersion); } $append->("
FileCommit
$filename$fileTree{$filename}
",$project_index); $append->("
", $project_index); } # Get logs hash and write out logs page foreach my $project ( @git_project_dirs ) { my $spec_project_dir = $projects_dir . $project . "/"; my $project_index = $spec_project_dir . "/index.html"; my @commit_ids; foreach my $log_line ( split("\n",`cd $project; git log ; cd ..`) ) { if ( $log_line =~ m/commit\ ([a-z0-9]{40})/ ) { push(@commit_ids,$1); } } # Key: commit SHA, value commit info (git show) my %commits; foreach my $commit_id ( @commit_ids ) { my $commit_info = `cd $project; git show $commit_id; cd ..`; chomp $commit_info; $commits{$commit_id} = $commit_info; } $append->("Logs for $project
",$project_index); $append->("
",$project_index); $append->("
",$project_index); # iterate over array to keep ordering foreach my $commit_id ( @commit_ids ) { my $filename = $commit_id . ".txt"; #$append->("$commit_id
",$project_index); $append->("",$project_index); $write->($commits{$commit_id},$spec_project_dir . $filename); } $append->("
$filename
",$project_index); $append->("",$project_index); } print "Done\n";