make destination directory if needed
[bak-git.git] / bak-git-server.pl
index 5b91ddc..c979f6e 100755 (executable)
@@ -26,6 +26,8 @@ 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
@@ -89,6 +91,11 @@ warn "dir: $dir listen: $server_ip:9001\n"
        , $shell_client
 ;
 
+sub rsync {
+       warn "# rsync ",join(' ', @_), "\n";
+       system 'rsync', @_;
+}
+
 sub pull_changes {
        my $hostname = shift;
        system "find $hostname -type f | sed 's,$hostname,,' > /tmp/$hostname.list";
@@ -97,7 +104,7 @@ sub pull_changes {
                print $files "$_\n" foreach @_;
                close($files);
        }
-       system "rsync -avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/"
+       rsync( qw( -avv --files-from /tmp/$hostname.list root\@$hostname:/ $hostname/ ) );
 }
 
 while (my $client = $server->accept()) {
@@ -143,7 +150,7 @@ while (my $client = $server->accept()) {
        } elsif ( $command eq 'add' ) {
                mkpath "$hostname/$dir" unless -e "$hostname/$dir";
                while ( $path ) {
-                       system 'rsync', '-avv', "root\@$hostname:$path", "$hostname/$path";
+                       rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
                        print $client git 'add', "$hostname/$path";
 
                        $args_message =~ s/^(.+)\b// || last;
@@ -160,14 +167,16 @@ 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\@$on_host:$path", "$on_host/$path";
+                       mkpath "$hostname/$dir" unless -e "$hostname/$dir";
+                       rsync( '-avv', "root\@$hostname:$path", "$hostname/$path" );
+                       rsync( '-avv', "root\@$on_host:$path", "$on_host/$path" );
                        open(my $diff, '-|', "diff -Nuw $hostname$path $on_host$path");
                        while(<$diff>) {
                                print $client $_;
                        }
                } 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
@@ -175,11 +184,18 @@ while (my $client = $server->accept()) {
                }
        } elsif ( $command eq 'revert' ) {
                if ( $on_host ) {
-                       system 'rsync', '-avv', "$on_host/$path", "root\@$hostname:$path";
+                       rsync( '-avv', "$on_host/$path", "root\@$hostname:$path" );
                } else {
                        print $client git "checkout -- $hostname/$path";
-                       system 'rsync', '-avv', "$hostname/$path", "root\@$hostname:$path";
+                       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";
        }