added cat [host:]/path
[bak-git.git] / bak-git-server.pl
index 5042551..1c8efc1 100755 (executable)
@@ -26,8 +26,12 @@ bak command overview:
   bak ch[anges]
   bak revert [host:]/path
 
+  bak cat [host:]/path
+
   bak - push all changed files to server
 
+See L<http://blog.rot13.org/bak-git> for more information
+
 =cut
 
 use warnings;
@@ -158,6 +162,7 @@ while (my $client = $server->accept()) {
                $command = 'log --patch-with-stat' if $command =~ m/^ch/;
                pull_changes( $hostname ) if $command eq 'diff';
                if ( $on_host ) {
+                       system 'rsync', '-avv', "root\@$hostname:$path", "$hostname/$path";
                        system 'rsync', '-avv', "root\@$on_host:$path", "$on_host/$path";
                        open(my $diff, '-|', "diff -Nuw $hostname$path $on_host$path");
                        while(<$diff>) {
@@ -165,7 +170,7 @@ while (my $client = $server->accept()) {
                        }
                } else {
                        # commands without path will show host-wide status/changes
-                       my $backup_path = $rel_path ? $backup_path : "$hostname/";
+                       my $backup_path = $path ? "$hostname/$path" : "$hostname/";
                        # hostname must end with / to prevent error from git:
                        # ambiguous argument 'arh-hw': both revision and filename
                        # to support branches named as hosts
@@ -178,6 +183,13 @@ while (my $client = $server->accept()) {
                        print $client git "checkout -- $hostname/$path";
                        system 'rsync', '-avv', "$hostname/$path", "root\@$hostname:$path";
                }
+       } elsif ( $command eq 'cat' ) {
+               my $file_path = ( $on_host ? $on_host : $hostname ) . "/$path";
+               open(my $file, '<', $file_path) || warn "ERROR $file_path: $!";
+               while(<$file>) {
+                       print $client $_;
+               }
+               close($file);
        } else {
                print $client "Unknown command: $command\n";
        }