23278a85f88dd4a7252c3dbe09e247f00979464c
3 # git2svn, converts a git branch to a svn ditto
5 # Copyright (c) 2008 Love Hörnquist Åstrand
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
12 # 1. Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
19 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 use POSIX
qw(strftime);
33 use Getopt
::Long qw
/:config gnu_getopt no_ignore_case auto_abbrev/;
39 my ($help, $verbose, $keeplogs, $no_load);
42 my $svntree = "repro";
43 my $basedir = "trunk";
47 my $branch = "master";
57 my ($IN, $next, $length, $data, $l) = (shift, shift);
58 unless($next =~ m/^data (\d+)/) { die "missing data: $next" ; }
61 $l = read(IN
, $data, $length);
62 unless ($l == $length) { die "failed to read data $l != $length"; }
63 $data = "" if ($length == 0);
69 my ($key, $value) = (shift, shift);
70 "K " . length($key) . "\n$key\nV " . length($value) . "\n$value\n";
76 my ($SVN, $type, $name);
78 open(SVN
, "svn ls -R $url|") or die "failed to open svn ls -R $url";
88 $paths{$name} = $type;
92 open(SVN
, "svn info $url|") or die "failed to open svn info $url";
95 if ($revision eq 1 && /^Revision: (\d+)/) {
104 my ($gittree, $name) = (shift, shift);
108 open FOO
, "cd $gittree ".
109 "&& git rev-parse $name 2>/dev/null|" or
110 die "git rev-parse $name failed";
115 $id = "" if ($id eq $name);
121 my ($gittree, $branch, $shortbranch) = (shift, shift, shift);
123 $syncname = "git2svn-syncpoint-${shortbranch}";
124 print STDERR
"syncname tag: $syncname\n" if ($verbose);
126 $masterrev = find_branch_id
($gittree, $branch);
127 die "No head found for ${branch}" if ($masterrev eq "");
129 my $oldmasterrev = find_branch_id
($gittree, $syncname);
131 if ($oldmasterrev ne "") {
133 die "no $svntree, but incremental (have synctag) ?\n".
134 "(\"cd $gittree && git tag -d $syncname\" to remove)"
135 unless ( -d
$svntree);
137 if (${oldmasterrev
} eq $masterrev) {
138 print STDERR
"nothing to sync, $syncname matches $branch\n"
143 $fexport = "$oldmasterrev..$masterrev";
145 $fexport="$masterrev";
147 system("svnadmin create ./$svntree") unless (-d
$svntree);
157 # pick first dir, create, take next dir, continue until we reached basename
158 while ($path =~ m@
^([^/]+)/(.*)$@
) {
159 my $first = $base . $1;
161 $base = $first . "/";
162 next if ($paths{$first});
166 printf OUT
"Node-path: $first\n";
167 printf OUT
"Node-kind: dir\n";
168 printf OUT
"Node-action: add\n";
169 printf OUT
"Prop-content-length: 0\n";
170 printf OUT
"Content-length: 0\n";
184 # This is to allow setting props for a path, XXX add configuration
185 # file/options for this.
191 ### given path, return prop("prop", "value")
199 $result = GetOptions
("git-branch=s" => \
$branch,
200 "svn-prefix=s" => \
$basedir,
201 "keep-logs" => \
$keeplogs,
202 "no-load" => \
$no_load,
203 "verbose+" => \
$verbose,
204 "help" => \
$help) or pod2usage
(2);
206 pod2usage
(0) if ($help);
208 die "to few arguments" if ($#ARGV < 1);
210 mkdir ".data" unless (-d
".data");
212 die "cant find branch name" unless ($branch =~ m@
/?([^/]+)$@
);
213 my $shortbranch = $1;
215 my $gittree = $ARGV[0];
218 # create an identifier by replacing path separators
219 # (i.e. "/", ":" and "\") with underscores
220 my $svntree_id = $svntree;
221 $svntree_id =~ s/[\/:\\]/_
/g
;
223 my $gitdump = ".data/git.dump-${svntree_id}-${shortbranch}";
224 my $svndump = ".data/svn.dump-${svntree_id}-${shortbranch}";
225 my $log = ".data/log-${svntree_id}-${shortbranch}";
227 parse_git_tree
($gittree, $branch, $shortbranch);
231 parse_svn_tree
("file://" . $cwd ."/". $svntree);
235 print STDERR
"git fast-export $branch ($fexport)\n" if ($verbose);
237 system("(cd $gittree && git fast-export $fexport) > $gitdump 2>$log") == 0 or
238 die "git fast-export: $!";
240 open IN
, "$gitdump" or
241 die "failed to open $gitdump";
243 open OUT
, ">$svndump" or
244 die "failed to open $svndump";
246 print STDERR
"creating svn dump from revision $revision...\n" if ($verbose);
248 print OUT
"SVN-fs-dump-format-version: 3\n";
250 my $next = next_line
();
251 COMMAND
: while (!eof(IN
)) {
253 $next = next_line
($IN);
255 } elsif ($next =~ /^commit (.*)/) {
259 $next = next_line
($IN);
260 if ($next =~ m/mark +(.*)/) {
262 $next = next_line
($IN);
264 if ($next =~ m/author +(.*)/) {
265 $commit{Author
} = $1;
266 $next = next_line
($IN);
268 unless ($next =~ m/committer +(.+) +<([^>]+)> +(\d+) +[+-](\d+)$/) {
269 die "missing comitter: $_";
272 $commit{CommitterName
} = $1;
273 $commit{CommitterEmail
} = $2;
274 $commit{CommitterWhen
} = $3;
275 $commit{CommitterTZ
} = $4;
277 $next = next_line
($IN);
278 my $log = read_data
($IN, $next);
280 $next = next_line
($IN);
281 if ($next =~ m/from (.*)/) {
283 $next = next_line
($IN);
285 if ($next =~ m/merge (.*)/) {
287 $next = next_line
($IN);
291 strftime
("%Y-%m-%dT%H:%M:%S.000000Z",
292 gmtime($commit{CommitterWhen
}));
294 my $author = "(no author)";
295 if ($commit{CommitterEmail
} =~ m/([^@]+)/) {
298 $author = "git2svn-dump" if ($author eq "(no author)");
301 $props .= prop
("svn:author", $author);
302 $props .= prop
("svn:log", $log);
303 $props .= prop
("svn:date", $date);
304 $props .= "PROPS-END";
308 printf OUT
"Revision-number: $revision\n"; $revision++;
309 printf OUT
"Prop-content-length: ". length($props) . "\n";
310 printf OUT
"Content-length: " . length($props) . "\n";
312 print OUT
"$props\n";
315 if ($next =~ m/M (\d+) (\S+) (.*)$/) {
316 my ($mode, $dataref, $path) = (oct $1, $2, $3);
318 if ($dataref eq "inline") {
319 $next = next_line
($IN);
320 $content = read_data
($IN, $next);
322 if (defined $blob{$dataref}) {
323 $content = $blob{$dataref};
325 # Submodules cannot be converted
326 print STDERR
"Ignored line, please check if this is a submodule: $next\n" if ($verbose);
327 $next = next_line
($IN);
330 # here we really want to delete $blob{$dataref},
331 # but it might be referenced in the future. To
332 # avoid keepig everything in memory for larger
333 # repositories this must be written out to disk
334 # and removed when done.
337 $path = "$basedir/$path";
342 my $type = $mode & 0777000;
345 if (($paths{$path} != 2) && ($type != 0120000)) {
346 die "file was a dir";
347 } elsif (($paths{$path} != 2) && ($type == 0120000)) {
348 print STDERR
"Dir is now a symlink, deleting: $path\n" if ($verbose);
349 foreach ( keys( %paths ) ) {
350 delete $paths{$_} if ( /^$path/ );
352 printf OUT
"Node-path: $path\nNode-action: delete\n\n";
362 $kind = "file" if ($type == 0100000);
363 $kind = "symlink" if ($type == 0120000);
364 die "$type unknown" if ($kind eq "");
367 $props .= prop
("svn:executable", "on") if ($mode & 0111);
368 $props .= prop
("svn:special", "*") if ($kind eq "symlink");
369 $props .= auto_props
($path);
370 $props .= "PROPS-END\n" if ($props ne "");
372 $content = "link $content\n" if ($kind eq "symlink");
374 my $plen = length($props);
375 my $clen = length($content);
377 printf OUT
"Node-path: $path\n";
378 printf OUT
"Node-kind: file\n";
379 printf OUT
"Node-action: $action\n";
380 printf OUT
"Text-content-length: $clen\n";
381 printf OUT
"Content-length: " . ($clen + $plen) . "\n";
382 printf OUT
"Prop-content-length: $plen\n" if ($plen);
385 print OUT
"$props" if ($plen);
389 } elsif ($next =~ m/D (.*)/) {
390 my $path = $basedir . "/". $1;
393 foreach ( keys( %paths ) ) {
394 delete $paths{$_} if ( /^$path/ );
397 printf OUT
"Node-path: $path\n";
398 printf OUT
"Node-action: delete\n";
401 print STDERR
"deleting non existing object: $path\n";
404 } elsif ($next =~ m/^C (.*)/) {
406 } elsif ($next =~ m/^R (.*)/) {
408 } elsif ($next =~ m/^filedeleteall$/) {
409 die "file delete all ?";
413 $next = next_line
($IN);
416 } elsif ($next =~ /^tag .*/) {
417 } elsif ($next =~ /^reset .*/) {
418 } elsif ($next =~ /^blob/) {
420 $next = next_line
($IN);
421 if ($next =~ m/mark (.*)/) {
423 $next = next_line
($IN);
425 my $data = read_data
($IN, $next);
426 $blob{$mark} = $data if (defined $mark);
427 } elsif ($next =~ /^checkpoint .*/) {
428 } elsif ($next =~ /^progress (.*)/) {
429 print STDERR
"progress: $1\n" if ($verbose);
431 die "unknown command $next";
433 $next = next_line
($IN);
439 print STDERR
"...dumped to revision $revision\n" if ($verbose);
440 print STDERR
"(re-)setting sync-tag to new master\n" if ($verbose);
443 system("cd $gittree && ".
444 "git tag -m \"sync $(date)\" -a -f ${syncname} ${masterrev}");
447 print STDERR
"loading dump into svn\n" if ($verbose);
450 system("svnadmin load $svntree < $svndump >>$log 2>&1") == 0 or
454 unlink $svndump, $gitdump, $log unless ($keeplogs);
463 B<git2svn> - converts a git branch to a svn ditto
467 B<git2svn> [options] git-repro svn-repro
473 =item B<--git-branch>
475 The git branch to export. The default is branch is master.
477 =item B<--svn-prefix>
479 The svn prefix where the branch is import. The default is trunk to
480 match the default GIT branch (master).
484 Don't load the svn repository or update the syncpoint tagname.
488 Don't delete the logs in $CWD/.data on success.
492 More verbose output, can be give more then once to increase the verbosity.
496 Print a brief help message and exits.
502 B<git2svn> will convert a git branch to a svn ditto, it also
503 support incremantal updates.
505 B<git2svn> takes a git fast-export dump and converts it into a
506 svn dump that is feed into svnadmin load.
508 B<git2svn> assumes its the only process that writes into the svn
509 repository. This is because of the race between getting the to svn
510 Revsion number from the svn, creating the dump with correct Revsions,
511 and do the svnadmin load.
513 B<git2svn> also support incremental updates from a git branch to
514 a svn reprositry. Its does this by setting a git tag
515 (git2svn-syncpoint-<branchname>) where the last update was pulled
518 B<git2svn> was created as a hack over a weekend to support a
519 smoother migration away from svn and allow users access to tools to
520 browse and search code (fisheye) and use anonymouns svn servers.
524 B<git2svn> ~/src/heimdal svn-repro
526 B<git2svn> --git-branch heimdal-1-0-branch \
527 --svn-prefix branches/heimdal-1-0-branch \
528 ~/src/heimdal svn-repro
532 B<git2svn> is avaible from repo.or.cz
534 git clone git://repo.or.cz/git2svn.git
538 Love Hörnquist Åstrand <lha@kth.se>
542 Send bug reports to lha@kth.se