@matheusbsbs / matheus-bagliano / commits / 2da9ee0888
Fork 0

git svn: add gc command

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>

Robert Allan Zeh committed Jul 19, 2009 at 18:00 UTC 2da9ee0888a8570f9a85ef11f208556c5316e3d4
3 files changed +81
Documentation/git-svn.txt
+4
@@ -338,6 +338,10 @@ Any other arguments are passed directly to 'git log'
338 Shows the Subversion externals. Use -r/--revision to specify a
339 specific revision.
340
341 +'gc'::
342 + Compress $GIT_DIR/svn/<refname>/unhandled.log files in .git/svn
343 + and remove $GIT_DIR/svn/<refname>index files in .git/svn.
344 +
345 'reset'::
346 Undoes the effects of 'fetch' back to the specified revision.
347 This allows you to re-'fetch' an SVN revision. Normally the
git-svn.perl
+33
@@ -31,6 +31,7 @@ require SVN::Delta;
31 if ($SVN::Core::VERSION lt '1.1.0') {
32 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
33 }
34 +my $can_compress = eval { require Compress::Zlib; 1};
35 push @Git::SVN::Ra::ISA, 'SVN::Ra';
36 push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
37 push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
@@ -40,6 +41,7 @@ use IO::File qw//;
41 use File::Basename qw/dirname basename/;
42 use File::Path qw/mkpath/;
43 use File::Spec;
44 +use File::Find;
45 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
46 use IPC::Open3;
47 use Git;
@@ -217,6 +219,10 @@ my %cmd = (
219 "Undo fetches back to the specified SVN revision",
220 { 'revision|r=s' => \$_revision,
221 'parent|p' => \$_fetch_parent } ],
222 + 'gc' => [ \&cmd_gc,
223 + "Compress unhandled.log files in .git/svn and remove " .
224 + "index files in .git/svn",
225 + {} ],
226 );
227
228 my $cmd;
@@ -1107,6 +1113,14 @@ sub cmd_reset {
1113 print "r$r = $c ($gs->{ref_id})\n";
1114 }
1115
1116 +sub cmd_gc {
1117 + if (!$can_compress) {
1118 + warn "Compress::Zlib could not be found; unhandled.log " .
1119 + "files will not be compressed.\n";
1120 + }
1121 + find({ wanted => \&gc_directory, no_chdir => 1}, "$ENV{GIT_DIR}/svn");
1122 +}
1123 +
1124 ########################### utility functions #########################
1125
1126 sub rebase_cmd {
@@ -1527,6 +1541,25 @@ sub md5sum {
1541 return $md5->hexdigest();
1542 }
1543
1544 +sub gc_directory {
1545 + if ($can_compress && -f $_ && basename($_) eq "unhandled.log") {
1546 + my $out_filename = $_ . ".gz";
1547 + open my $in_fh, "<", $_ or die "Unable to open $_: $!\n";
1548 + binmode $in_fh;
1549 + my $gz = Compress::Zlib::gzopen($out_filename, "ab") or
1550 + die "Unable to open $out_filename: $!\n";
1551 +
1552 + my $res;
1553 + while ($res = sysread($in_fh, my $str, 1024)) {
1554 + $gz->gzwrite($str) or
1555 + die "Unable to write: ".$gz->gzerror()."!\n";
1556 + }
1557 + unlink $_ or die "unlink $File::Find::name: $!\n";
1558 + } elsif (-f $_ && basename($_) eq "index") {
1559 + unlink $_ or die "unlink $_: $!\n";
1560 + }
1561 +}
1562 +
1563 package Git::SVN;
1564 use strict;
1565 use warnings;
t/t9143-git-svn-gc.sh new
+44
@@ -0,0 +1,44 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2009 Robert Allan Zeh
4 +
5 +test_description='git svn gc basic tests'
6 +
7 +. ./lib-git-svn.sh
8 +
9 +test_expect_success 'setup directories and test repo' '
10 + mkdir import &&
11 + mkdir tmp &&
12 + echo "Sample text for Subversion repository." > import/test.txt &&
13 + svn_cmd import -m "import for git svn" import "$svnrepo" > /dev/null
14 + '
15 +
16 +test_expect_success 'checkout working copy from svn' \
17 + 'svn_cmd co "$svnrepo" test_wc'
18 +
19 +test_expect_success 'set some properties to create an unhandled.log file' '
20 + (
21 + cd test_wc &&
22 + svn_cmd propset foo bar test.txt &&
23 + svn_cmd commit -m "property set"
24 + )'
25 +
26 +test_expect_success 'Setup repo' 'git svn init "$svnrepo"'
27 +
28 +test_expect_success 'Fetch repo' 'git svn fetch'
29 +
30 +test_expect_success 'make backup copy of unhandled.log' '
31 + cp .git/svn/git-svn/unhandled.log tmp
32 + '
33 +
34 +test_expect_success 'git svn gc runs' 'git svn gc'
35 +
36 +test_expect_success 'git svn gc produces a valid gzip file' '
37 + gunzip .git/svn/git-svn/unhandled.log.gz
38 + '
39 +
40 +test_expect_success 'git svn gc does not change unhandled.log files' '
41 + test_cmp .git/svn/git-svn/unhandled.log tmp/unhandled.log
42 + '
43 +
44 +test_done