]> Pileus Git - ~andy/git/commitdiff
git svn: add gc command
authorRobert Allan Zeh <robert.a.zeh@gmail.com>
Sun, 19 Jul 2009 23:00:52 +0000 (18:00 -0500)
committerEric Wong <normalperson@yhbt.net>
Sat, 25 Jul 2009 11:09:43 +0000 (04:09 -0700)
Add a git svn gc command that gzips all unhandled.log files, and
removes all index files under .git/svn.

Signed-off-by: Robert Allan Zeh <robert.a.zeh@gmail.com>
Signed-off-by: Eric Wong <normalperson@yhbt.net>
Documentation/git-svn.txt
git-svn.perl
t/t9143-git-svn-gc.sh [new file with mode: 0755]

index 10af599b44a38981126af62652815b5d184e6774..068aa58a5b55dfa0fb7c33d16e2d5fa89cf645ba 100644 (file)
@@ -338,6 +338,10 @@ Any other arguments are passed directly to 'git log'
        Shows the Subversion externals.  Use -r/--revision to specify a
        specific revision.
 
+'gc'::
+       Compress $GIT_DIR/svn/<refname>/unhandled.log files in .git/svn
+       and remove $GIT_DIR/svn/<refname>index files in .git/svn.
+
 'reset'::
        Undoes the effects of 'fetch' back to the specified revision.
        This allows you to re-'fetch' an SVN revision.  Normally the
index 9369acc4dce3681012aa2c16f6d292a3d76d9baf..70159204cef7368a9c198ba668498e8712c51d92 100755 (executable)
@@ -31,6 +31,7 @@ require SVN::Delta;
 if ($SVN::Core::VERSION lt '1.1.0') {
        fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
 }
+my $can_compress = eval { require Compress::Zlib; 1};
 push @Git::SVN::Ra::ISA, 'SVN::Ra';
 push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
 push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
@@ -40,6 +41,7 @@ use IO::File qw//;
 use File::Basename qw/dirname basename/;
 use File::Path qw/mkpath/;
 use File::Spec;
+use File::Find;
 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
 use IPC::Open3;
 use Git;
@@ -217,6 +219,10 @@ my %cmd = (
                     "Undo fetches back to the specified SVN revision",
                     { 'revision|r=s' => \$_revision,
                       'parent|p' => \$_fetch_parent } ],
+       'gc' => [ \&cmd_gc,
+                 "Compress unhandled.log files in .git/svn and remove " .
+                 "index files in .git/svn",
+               {} ],
 );
 
 my $cmd;
@@ -1107,6 +1113,14 @@ sub cmd_reset {
        print "r$r = $c ($gs->{ref_id})\n";
 }
 
+sub cmd_gc {
+       if (!$can_compress) {
+               warn "Compress::Zlib could not be found; unhandled.log " .
+                    "files will not be compressed.\n";
+       }
+       find({ wanted => \&gc_directory, no_chdir => 1}, "$ENV{GIT_DIR}/svn");
+}
+
 ########################### utility functions #########################
 
 sub rebase_cmd {
@@ -1527,6 +1541,25 @@ sub md5sum {
        return $md5->hexdigest();
 }
 
+sub gc_directory {
+       if ($can_compress && -f $_ && basename($_) eq "unhandled.log") {
+               my $out_filename = $_ . ".gz";
+               open my $in_fh, "<", $_ or die "Unable to open $_: $!\n";
+               binmode $in_fh;
+               my $gz = Compress::Zlib::gzopen($out_filename, "ab") or
+                               die "Unable to open $out_filename: $!\n";
+
+               my $res;
+               while ($res = sysread($in_fh, my $str, 1024)) {
+                       $gz->gzwrite($str) or
+                               die "Unable to write: ".$gz->gzerror()."!\n";
+               }
+               unlink $_ or die "unlink $File::Find::name: $!\n";
+       } elsif (-f $_ && basename($_) eq "index") {
+               unlink $_ or die "unlink $_: $!\n";
+       }
+}
+
 package Git::SVN;
 use strict;
 use warnings;
diff --git a/t/t9143-git-svn-gc.sh b/t/t9143-git-svn-gc.sh
new file mode 100755 (executable)
index 0000000..aaa3af0
--- /dev/null
@@ -0,0 +1,44 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Robert Allan Zeh
+
+test_description='git svn gc basic tests'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'setup directories and test repo' '
+       mkdir import &&
+       mkdir tmp &&
+       echo "Sample text for Subversion repository." > import/test.txt &&
+       svn_cmd import -m "import for git svn" import "$svnrepo" > /dev/null
+       '
+
+test_expect_success 'checkout working copy from svn' \
+       'svn_cmd co "$svnrepo" test_wc'
+
+test_expect_success 'set some properties to create an unhandled.log file' '
+       (
+               cd test_wc &&
+               svn_cmd propset foo bar test.txt &&
+               svn_cmd commit -m "property set"
+       )'
+
+test_expect_success 'Setup repo' 'git svn init "$svnrepo"'
+
+test_expect_success 'Fetch repo' 'git svn fetch'
+
+test_expect_success 'make backup copy of unhandled.log' '
+        cp .git/svn/git-svn/unhandled.log tmp
+       '
+
+test_expect_success 'git svn gc runs' 'git svn gc'
+
+test_expect_success 'git svn gc produces a valid gzip file' '
+        gunzip .git/svn/git-svn/unhandled.log.gz
+       '
+
+test_expect_success 'git svn gc does not change unhandled.log files' '
+        test_cmp .git/svn/git-svn/unhandled.log tmp/unhandled.log
+       '
+
+test_done