Introducing contrib/git-svn.
Eric Wong committed
Feb 16, 2006 at 01:24 UTC
3397f9df53092871de2c33c143f1f4413654c40d
2 files changed
+978
contrib/git-svn/git-svn
new
+770
@@ -0,0 +1,770 @@
1
+#!/usr/bin/env perl
2
+use warnings;
3
+use strict;
4
+use vars qw/ $AUTHOR $VERSION
5
+ $SVN_URL $SVN_INFO $SVN_WC
6
+ $GIT_SVN_INDEX $GIT_SVN
7
+ $GIT_DIR $REV_DIR/;
8
+$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
9
+$VERSION = '0.9.0';
10
+$GIT_DIR = $ENV{GIT_DIR} || "$ENV{PWD}/.git";
11
+$GIT_SVN = $ENV{GIT_SVN_ID} || 'git-svn';
12
+$GIT_SVN_INDEX = "$GIT_DIR/$GIT_SVN/index";
13
+$ENV{GIT_DIR} ||= $GIT_DIR;
14
+$SVN_URL = undef;
15
+$REV_DIR = "$GIT_DIR/$GIT_SVN/revs";
16
+$SVN_WC = "$GIT_DIR/$GIT_SVN/tree";
17
+
18
+# make sure the svn binary gives consistent output between locales and TZs:
19
+$ENV{TZ} = 'UTC';
20
+$ENV{LC_ALL} = 'C';
21
+
22
+# If SVN:: library support is added, please make the dependencies
23
+# optional and preserve the capability to use the command-line client.
24
+# See what I do with XML::Simple to make the dependency optional.
25
+use Carp qw/croak/;
26
+use IO::File qw//;
27
+use File::Basename qw/dirname basename/;
28
+use File::Path qw/mkpath/;
29
+use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
30
+use File::Spec qw//;
31
+my $sha1 = qr/[a-f\d]{40}/;
32
+my $sha1_short = qr/[a-f\d]{6,40}/;
33
+my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit);
34
+
35
+GetOptions( 'revision|r=s' => \$_revision,
36
+ 'no-ignore-externals' => \$_no_ignore_ext,
37
+ 'stdin|' => \$_stdin,
38
+ 'edit|e' => \$_edit,
39
+ 'rmdir' => \$_rmdir,
40
+ 'help|H|h' => \$_help,
41
+ 'no-stop-copy' => \$_no_stop_copy );
42
+my %cmd = (
43
+ fetch => [ \&fetch, "Download new revisions from SVN" ],
44
+ init => [ \&init, "Initialize and fetch (import)"],
45
+ commit => [ \&commit, "Commit git revisions to SVN" ],
46
+ rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)" ],
47
+ help => [ \&usage, "Show help" ],
48
+);
49
+my $cmd;
50
+for (my $i = 0; $i < @ARGV; $i++) {
51
+ if (defined $cmd{$ARGV[$i]}) {
52
+ $cmd = $ARGV[$i];
53
+ splice @ARGV, $i, 1;
54
+ last;
55
+ }
56
+};
57
+
58
+# we may be called as git-svn-(command), or git-svn(command).
59
+foreach (keys %cmd) {
60
+ if (/git\-svn\-?($_)(?:\.\w+)?$/) {
61
+ $cmd = $1;
62
+ last;
63
+ }
64
+}
65
+usage(0) if $_help;
66
+usage(1) unless (defined $cmd);
67
+svn_check_ignore_externals();
68
+$cmd{$cmd}->[0]->(@ARGV);
69
+exit 0;
70
+
71
+####################### primary functions ######################
72
+sub usage {
73
+ my $exit = shift || 0;
74
+ my $fd = $exit ? \*STDERR : \*STDOUT;
75
+ print $fd <<"";
76
+git-svn - bidirectional operations between a single Subversion tree and git
77
+Usage: $0 <command> [options] [arguments]\n
78
+Available commands:
79
+
80
+ foreach (sort keys %cmd) {
81
+ print $fd ' ',pack('A10',$_),$cmd{$_}->[1],"\n";
82
+ }
83
+ print $fd <<"";
84
+\nGIT_SVN_ID may be set in the environment to an arbitrary identifier if
85
+you're tracking multiple SVN branches/repositories in one git repository
86
+and want to keep them separate.
87
+
88
+ exit $exit;
89
+}
90
+
91
+sub rebuild {
92
+ $SVN_URL = shift or undef;
93
+ my $repo_uuid;
94
+ my $newest_rev = 0;
95
+
96
+ my $pid = open(my $rev_list,'-|');
97
+ defined $pid or croak $!;
98
+ if ($pid == 0) {
99
+ exec("git-rev-list","$GIT_SVN-HEAD") or croak $!;
100
+ }
101
+ my $first;
102
+ while (<$rev_list>) {
103
+ chomp;
104
+ my $c = $_;
105
+ croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
106
+ my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
107
+ next if (!@commit); # skip merges
108
+ my $id = $commit[$#commit];
109
+ my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
110
+ \s([a-f\d\-]+)$/x);
111
+ if (!$rev || !$uuid || !$url) {
112
+ # some of the original repositories I made had
113
+ # indentifiers like this:
114
+ ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)
115
+ \@([a-f\d\-]+)/x);
116
+ if (!$rev || !$uuid) {
117
+ croak "Unable to extract revision or UUID from ",
118
+ "$c, $id\n";
119
+ }
120
+ }
121
+ print "r$rev = $c\n";
122
+ unless (defined $first) {
123
+ if (!$SVN_URL && !$url) {
124
+ croak "SVN repository location required: $url\n";
125
+ }
126
+ $SVN_URL ||= $url;
127
+ $repo_uuid = setup_git_svn();
128
+ $first = $rev;
129
+ }
130
+ if ($uuid ne $repo_uuid) {
131
+ croak "Repository UUIDs do not match!\ngot: $uuid\n",
132
+ "expected: $repo_uuid\n";
133
+ }
134
+ assert_revision_eq_or_unknown($rev, $c);
135
+ sys('git-update-ref',"$GIT_SVN/revs/$rev",$c);
136
+ $newest_rev = $rev if ($rev > $newest_rev);
137
+ }
138
+ close $rev_list or croak $?;
139
+ if (!chdir $SVN_WC) {
140
+ my @svn_co = ('svn','co',"-r$first");
141
+ push @svn_co, '--ignore-externals' unless $_no_ignore_ext;
142
+ sys(@svn_co, $SVN_URL, $SVN_WC);
143
+ chdir $SVN_WC or croak $!;
144
+ }
145
+
146
+ $pid = fork;
147
+ defined $pid or croak $!;
148
+ if ($pid == 0) {
149
+ my @svn_up = qw(svn up);
150
+ push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
151
+ sys(@svn_up,"-r$newest_rev");
152
+ $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
153
+ git_addremove();
154
+ exec('git-write-tree');
155
+ }
156
+ waitpid $pid, 0;
157
+}
158
+
159
+sub init {
160
+ $SVN_URL = shift or croak "SVN repository location required\n";
161
+ unless (-d $GIT_DIR) {
162
+ sys('git-init-db');
163
+ }
164
+ setup_git_svn();
165
+}
166
+
167
+sub fetch {
168
+ my (@parents) = @_;
169
+ $SVN_URL ||= file_to_s("$GIT_DIR/$GIT_SVN/info/url");
170
+ my @log_args = -d $SVN_WC ? ($SVN_WC) : ($SVN_URL);
171
+ if (-d $SVN_WC && !$_revision) {
172
+ $_revision = 'BASE:HEAD';
173
+ }
174
+ push @log_args, "-r$_revision" if $_revision;
175
+ push @log_args, '--stop-on-copy' unless $_no_stop_copy;
176
+
177
+ eval { require XML::Simple or croak $! };
178
+ my $svn_log = $@ ? svn_log_raw(@log_args) : svn_log_xml(@log_args);
179
+
180
+ my $base = shift @$svn_log or croak "No base revision!\n";
181
+ my $last_commit = undef;
182
+ unless (-d $SVN_WC) {
183
+ my @svn_co = ('svn','co',"-r$base->{revision}");
184
+ push @svn_co,'--ignore-externals' unless $_no_ignore_ext;
185
+ sys(@svn_co, $SVN_URL, $SVN_WC);
186
+ chdir $SVN_WC or croak $!;
187
+ $last_commit = git_commit($base, @parents);
188
+ unless (-f "$GIT_DIR/refs/heads/master") {
189
+ sys(qw(git-update-ref refs/heads/master),$last_commit);
190
+ }
191
+ assert_svn_wc_clean($base->{revision}, $last_commit);
192
+ } else {
193
+ chdir $SVN_WC or croak $!;
194
+ $last_commit = file_to_s("$REV_DIR/$base->{revision}");
195
+ }
196
+ my @svn_up = qw(svn up);
197
+ push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
198
+ my $last_rev = $base->{revision};
199
+ foreach my $log_msg (@$svn_log) {
200
+ assert_svn_wc_clean($last_rev, $last_commit);
201
+ $last_rev = $log_msg->{revision};
202
+ sys(@svn_up,"-r$last_rev");
203
+ $last_commit = git_commit($log_msg, $last_commit, @parents);
204
+ }
205
+ assert_svn_wc_clean($last_rev, $last_commit);
206
+ return pop @$svn_log;
207
+}
208
+
209
+sub commit {
210
+ my (@commits) = @_;
211
+ if ($_stdin || !@commits) {
212
+ print "Reading from stdin...\n";
213
+ @commits = ();
214
+ while (<STDIN>) {
215
+ if (/^([a-f\d]{6,40})\b/) {
216
+ unshift @commits, $1;
217
+ }
218
+ }
219
+ }
220
+ my @revs;
221
+ foreach (@commits) {
222
+ push @revs, (safe_qx('git-rev-parse',$_));
223
+ }
224
+ chomp @revs;
225
+
226
+ fetch();
227
+ chdir $SVN_WC or croak $!;
228
+ my $svn_current_rev = svn_info('.')->{'Last Changed Rev'};
229
+ foreach my $c (@revs) {
230
+ print "Committing $c\n";
231
+ svn_checkout_tree($svn_current_rev, $c);
232
+ $svn_current_rev = svn_commit_tree($svn_current_rev, $c);
233
+ }
234
+ print "Done committing ",scalar @revs," revisions to SVN\n";
235
+
236
+}
237
+
238
+########################### utility functions #########################
239
+
240
+sub setup_git_svn {
241
+ defined $SVN_URL or croak "SVN repository location required\n";
242
+ unless (-d $GIT_DIR) {
243
+ croak "GIT_DIR=$GIT_DIR does not exist!\n";
244
+ }
245
+ mkpath(["$GIT_DIR/$GIT_SVN"]);
246
+ mkpath(["$GIT_DIR/$GIT_SVN/info"]);
247
+ mkpath([$REV_DIR]);
248
+ s_to_file($SVN_URL,"$GIT_DIR/$GIT_SVN/info/url");
249
+ my $uuid = svn_info($SVN_URL)->{'Repository UUID'} or
250
+ croak "Repository UUID unreadable\n";
251
+ s_to_file($uuid,"$GIT_DIR/$GIT_SVN/info/uuid");
252
+
253
+ open my $fd, '>>', "$GIT_DIR/$GIT_SVN/info/exclude" or croak $!;
254
+ print $fd '.svn',"\n";
255
+ close $fd or croak $!;
256
+ return $uuid;
257
+}
258
+
259
+sub assert_svn_wc_clean {
260
+ my ($svn_rev, $commit) = @_;
261
+ croak "$svn_rev is not an integer!\n" unless ($svn_rev =~ /^\d+$/);
262
+ croak "$commit is not a sha1!\n" unless ($commit =~ /^$sha1$/o);
263
+ my $svn_info = svn_info('.');
264
+ if ($svn_rev != $svn_info->{'Last Changed Rev'}) {
265
+ croak "Expected r$svn_rev, got r",
266
+ $svn_info->{'Last Changed Rev'},"\n";
267
+ }
268
+ my @status = grep(!/^Performing status on external/,(`svn status`));
269
+ @status = grep(!/^\s*$/,@status);
270
+ if (scalar @status) {
271
+ print STDERR "Tree ($SVN_WC) is not clean:\n";
272
+ print STDERR $_ foreach @status;
273
+ croak;
274
+ }
275
+ my ($tree_a) = grep(/^tree $sha1$/o,`git-cat-file commit $commit`);
276
+ $tree_a =~ s/^tree //;
277
+ chomp $tree_a;
278
+ chomp(my $tree_b = `GIT_INDEX_FILE=$GIT_SVN_INDEX git-write-tree`);
279
+ if ($tree_a ne $tree_b) {
280
+ croak "$svn_rev != $commit, $tree_a != $tree_b\n";
281
+ }
282
+}
283
+
284
+sub parse_diff_tree {
285
+ my $diff_fh = shift;
286
+ local $/ = "\0";
287
+ my $state = 'meta';
288
+ my @mods;
289
+ while (<$diff_fh>) {
290
+ chomp $_; # this gets rid of the trailing "\0"
291
+ print $_,"\n";
292
+ if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
293
+ $sha1\s($sha1)\s([MTCRAD])\d*$/xo) {
294
+ push @mods, { mode_a => $1, mode_b => $2,
295
+ sha1_b => $3, chg => $4 };
296
+ if ($4 =~ /^(?:C|R)$/) {
297
+ $state = 'file_a';
298
+ } else {
299
+ $state = 'file_b';
300
+ }
301
+ } elsif ($state eq 'file_a') {
302
+ my $x = $mods[$#mods] or croak __LINE__,": Empty array\n";
303
+ if ($x->{chg} !~ /^(?:C|R)$/) {
304
+ croak __LINE__,": Error parsing $_, $x->{chg}\n";
305
+ }
306
+ $x->{file_a} = $_;
307
+ $state = 'file_b';
308
+ } elsif ($state eq 'file_b') {
309
+ my $x = $mods[$#mods] or croak __LINE__,": Empty array\n";
310
+ if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
311
+ croak __LINE__,": Error parsing $_, $x->{chg}\n";
312
+ }
313
+ if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
314
+ croak __LINE__,": Error parsing $_, $x->{chg}\n";
315
+ }
316
+ $x->{file_b} = $_;
317
+ $state = 'meta';
318
+ } else {
319
+ croak __LINE__,": Error parsing $_\n";
320
+ }
321
+ }
322
+ close $diff_fh or croak $!;
323
+ return \@mods;
324
+}
325
+
326
+sub svn_check_prop_executable {
327
+ my $m = shift;
328
+ if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
329
+ sys(qw(svn propset svn:executable 1), $m->{file_b});
330
+ } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
331
+ sys(qw(svn propdel svn:executable), $m->{file_b});
332
+ }
333
+}
334
+
335
+sub svn_ensure_parent_path {
336
+ my $dir_b = dirname(shift);
337
+ svn_ensure_parent_path($dir_b) if ($dir_b ne File::Spec->curdir);
338
+ mkpath([$dir_b]) unless (-d $dir_b);
339
+ sys(qw(svn add -N), $dir_b) unless (-d "$dir_b/.svn");
340
+}
341
+
342
+sub svn_checkout_tree {
343
+ my ($svn_rev, $commit) = @_;
344
+ my $from = file_to_s("$REV_DIR/$svn_rev");
345
+ assert_svn_wc_clean($svn_rev,$from);
346
+ print "diff-tree '$from' '$commit'\n";
347
+ my $pid = open my $diff_fh, '-|';
348
+ defined $pid or croak $!;
349
+ if ($pid == 0) {
350
+ exec(qw(git-diff-tree -z -r -C), $from, $commit) or croak $!;
351
+ }
352
+ my $mods = parse_diff_tree($diff_fh);
353
+ unless (@$mods) {
354
+ # git can do empty commits, SVN doesn't allow it...
355
+ return $svn_rev;
356
+ }
357
+ my %rm;
358
+ foreach my $m (@$mods) {
359
+ if ($m->{chg} eq 'C') {
360
+ svn_ensure_parent_path( $m->{file_b} );
361
+ sys(qw(svn cp), $m->{file_a}, $m->{file_b});
362
+ blob_to_file( $m->{sha1_b}, $m->{file_b});
363
+ svn_check_prop_executable($m);
364
+ } elsif ($m->{chg} eq 'D') {
365
+ $rm{dirname $m->{file_b}}->{basename $m->{file_b}} = 1;
366
+ sys(qw(svn rm --force), $m->{file_b});
367
+ } elsif ($m->{chg} eq 'R') {
368
+ svn_ensure_parent_path( $m->{file_b} );
369
+ sys(qw(svn mv --force), $m->{file_a}, $m->{file_b});
370
+ blob_to_file( $m->{sha1_b}, $m->{file_b});
371
+ svn_check_prop_executable($m);
372
+ $rm{dirname $m->{file_a}}->{basename $m->{file_a}} = 1;
373
+ } elsif ($m->{chg} eq 'M') {
374
+ if ($m->{mode_b} =~ /^120/ && $m->{mode_a} =~ /^120/) {
375
+ unlink $m->{file_b} or croak $!;
376
+ blob_to_symlink($m->{sha1_b}, $m->{file_b});
377
+ } else {
378
+ blob_to_file($m->{sha1_b}, $m->{file_b});
379
+ }
380
+ svn_check_prop_executable($m);
381
+ } elsif ($m->{chg} eq 'T') {
382
+ sys(qw(svn rm --force),$m->{file_b});
383
+ if ($m->{mode_b} =~ /^120/ && $m->{mode_a} =~ /^100/) {
384
+ blob_to_symlink($m->{sha1_b}, $m->{file_b});
385
+ } else {
386
+ blob_to_file($m->{sha1_b}, $m->{file_b});
387
+ }
388
+ svn_check_prop_executable($m);
389
+ sys(qw(svn add --force), $m->{file_b});
390
+ } elsif ($m->{chg} eq 'A') {
391
+ svn_ensure_parent_path( $m->{file_b} );
392
+ blob_to_file( $m->{sha1_b}, $m->{file_b});
393
+ if ($m->{mode_b} =~ /755$/) {
394
+ chmod 0755, $m->{file_b};
395
+ }
396
+ sys(qw(svn add --force), $m->{file_b});
397
+ } else {
398
+ croak "Invalid chg: $m->{chg}\n";
399
+ }
400
+ }
401
+ if ($_rmdir) {
402
+ my $old_index = $ENV{GIT_INDEX_FILE};
403
+ $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
404
+ foreach my $dir (keys %rm) {
405
+ my $files = $rm{$dir};
406
+ my @files;
407
+ foreach (safe_qx('svn','ls',$dir)) {
408
+ chomp;
409
+ push @files, $_ unless $files->{$_};
410
+ }
411
+ sys(qw(svn rm),$dir) unless @files;
412
+ }
413
+ if ($old_index) {
414
+ $ENV{GIT_INDEX_FILE} = $old_index;
415
+ } else {
416
+ delete $ENV{GIT_INDEX_FILE};
417
+ }
418
+ }
419
+}
420
+
421
+sub svn_commit_tree {
422
+ my ($svn_rev, $commit) = @_;
423
+ my $commit_msg = "$GIT_DIR/$GIT_SVN/.svn-commit.tmp.$$";
424
+ open my $msg, '>', $commit_msg or croak $!;
425
+
426
+ chomp(my $type = `git-cat-file -t $commit`);
427
+ if ($type eq 'commit') {
428
+ my $pid = open my $msg_fh, '-|';
429
+ defined $pid or croak $!;
430
+
431
+ if ($pid == 0) {
432
+ exec(qw(git-cat-file commit), $commit) or croak $!;
433
+ }
434
+ my $in_msg = 0;
435
+ while (<$msg_fh>) {
436
+ if (!$in_msg) {
437
+ $in_msg = 1 if (/^\s*$/);
438
+ } else {
439
+ print $msg $_ or croak $!;
440
+ }
441
+ }
442
+ close $msg_fh or croak $!;
443
+ }
444
+ close $msg or croak $!;
445
+
446
+ if ($_edit || ($type eq 'tree')) {
447
+ my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
448
+ system($editor, $commit_msg);
449
+ }
450
+ my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
451
+ my ($committed) = grep(/^Committed revision \d+\./,@ci_output);
452
+ unlink $commit_msg;
453
+ defined $committed or croak
454
+ "Commit output failed to parse committed revision!\n",
455
+ join("\n",@ci_output),"\n";
456
+ my ($rev_committed) = ($committed =~ /^Committed revision (\d+)\./);
457
+
458
+ # resync immediately
459
+ my @svn_up = (qw(svn up), "-r$svn_rev");
460
+ push @svn_up, '--ignore-externals' unless $_no_ignore_ext;
461
+ sys(@svn_up);
462
+ return fetch("$rev_committed=$commit")->{revision};
463
+}
464
+
465
+sub svn_log_xml {
466
+ my (@log_args) = @_;
467
+ my $log_fh = IO::File->new_tmpfile or croak $!;
468
+
469
+ my $pid = fork;
470
+ defined $pid or croak $!;
471
+
472
+ if ($pid == 0) {
473
+ open STDOUT, '>&', $log_fh or croak $!;
474
+ exec (qw(svn log --xml), @log_args) or croak $!
475
+ }
476
+
477
+ waitpid $pid, 0;
478
+ croak $? if $?;
479
+
480
+ seek $log_fh, 0, 0;
481
+ my @svn_log;
482
+ my $log = XML::Simple::XMLin( $log_fh,
483
+ ForceArray => ['path','revision','logentry'],
484
+ KeepRoot => 0,
485
+ KeyAttr => { logentry => '+revision',
486
+ paths => '+path' },
487
+ )->{logentry};
488
+ foreach my $r (sort {$a <=> $b} keys %$log) {
489
+ my $log_msg = $log->{$r};
490
+ my ($Y,$m,$d,$H,$M,$S) = ($log_msg->{date} =~
491
+ /(\d{4})\-(\d\d)\-(\d\d)T
492
+ (\d\d)\:(\d\d)\:(\d\d)\.\d+Z$/x)
493
+ or croak "Failed to parse date: ",
494
+ $log->{$r}->{date};
495
+ $log_msg->{date} = "+0000 $Y-$m-$d $H:$M:$S";
496
+
497
+ # XML::Simple can't handle <msg></msg> as a string:
498
+ if (ref $log_msg->{msg} eq 'HASH') {
499
+ $log_msg->{msg} = "\n";
500
+ } else {
501
+ $log_msg->{msg} .= "\n";
502
+ }
503
+ push @svn_log, $log->{$r};
504
+ }
505
+ return \@svn_log;
506
+}
507
+
508
+sub svn_log_raw {
509
+ my (@log_args) = @_;
510
+ my $pid = open my $log_fh,'-|';
511
+ defined $pid or croak $!;
512
+
513
+ if ($pid == 0) {
514
+ exec (qw(svn log), @log_args) or croak $!
515
+ }
516
+
517
+ my @svn_log;
518
+ my $state;
519
+ while (<$log_fh>) {
520
+ chomp;
521
+ if (/^\-{72}$/) {
522
+ $state = 'rev';
523
+
524
+ # if we have an empty log message, put something there:
525
+ if (@svn_log) {
526
+ $svn_log[0]->{msg} ||= "\n";
527
+ }
528
+ next;
529
+ }
530
+ if ($state eq 'rev' && s/^r(\d+)\s*\|\s*//) {
531
+ my $rev = $1;
532
+ my ($author, $date) = split(/\s*\|\s*/, $_, 2);
533
+ my ($Y,$m,$d,$H,$M,$S,$tz) = ($date =~
534
+ /(\d{4})\-(\d\d)\-(\d\d)\s
535
+ (\d\d)\:(\d\d)\:(\d\d)\s([\-\+]\d+)/x)
536
+ or croak "Failed to parse date: $date\n";
537
+ my %log_msg = ( revision => $rev,
538
+ date => "$tz $Y-$m-$d $H:$M:$S",
539
+ author => $author,
540
+ msg => '' );
541
+ unshift @svn_log, \%log_msg;
542
+ $state = 'msg_start';
543
+ next;
544
+ }
545
+ # skip the first blank line of the message:
546
+ if ($state eq 'msg_start' && /^$/) {
547
+ $state = 'msg';
548
+ } elsif ($state eq 'msg') {
549
+ $svn_log[0]->{msg} .= $_."\n";
550
+ }
551
+ }
552
+ close $log_fh or croak $?;
553
+ return \@svn_log;
554
+}
555
+
556
+sub svn_info {
557
+ my $url = shift || $SVN_URL;
558
+
559
+ my $pid = open my $info_fh, '-|';
560
+ defined $pid or croak $!;
561
+
562
+ if ($pid == 0) {
563
+ exec(qw(svn info),$url) or croak $!;
564
+ }
565
+
566
+ my $ret = {};
567
+ # only single-lines seem to exist in svn info output
568
+ while (<$info_fh>) {
569
+ chomp $_;
570
+ if (m#^([^:]+)\s*:\s*(\S*)$#) {
571
+ $ret->{$1} = $2;
572
+ push @{$ret->{-order}}, $1;
573
+ }
574
+ }
575
+ close $info_fh or croak $!;
576
+ return $ret;
577
+}
578
+
579
+sub sys { system(@_) == 0 or croak $? }
580
+
581
+sub git_addremove {
582
+ system( "git-ls-files -z --others ".
583
+ "'--exclude-from=$GIT_DIR/$GIT_SVN/info/exclude'".
584
+ "| git-update-index --add -z --stdin; ".
585
+ "git-ls-files -z --deleted ".
586
+ "| git-update-index --remove -z --stdin; ".
587
+ "git-ls-files -z --modified".
588
+ "| git-update-index -z --stdin") == 0 or croak $?
589
+}
590
+
591
+sub s_to_file {
592
+ my ($str, $file, $mode) = @_;
593
+ open my $fd,'>',$file or croak $!;
594
+ print $fd $str,"\n" or croak $!;
595
+ close $fd or croak $!;
596
+ chmod ($mode &~ umask, $file) if (defined $mode);
597
+}
598
+
599
+sub file_to_s {
600
+ my $file = shift;
601
+ open my $fd,'<',$file or croak "$!: file: $file\n";
602
+ local $/;
603
+ my $ret = <$fd>;
604
+ close $fd or croak $!;
605
+ $ret =~ s/\s*$//s;
606
+ return $ret;
607
+}
608
+
609
+sub assert_revision_unknown {
610
+ my $revno = shift;
611
+ if (-f "$REV_DIR/$revno") {
612
+ croak "$REV_DIR/$revno already exists! ",
613
+ "Why are we refetching it?";
614
+ }
615
+}
616
+
617
+sub assert_revision_eq_or_unknown {
618
+ my ($revno, $commit) = @_;
619
+ if (-f "$REV_DIR/$revno") {
620
+ my $current = file_to_s("$REV_DIR/$revno");
621
+ if ($commit ne $current) {
622
+ croak "$REV_DIR/$revno already exists!\n",
623
+ "current: $current\nexpected: $commit\n";
624
+ }
625
+ return;
626
+ }
627
+}
628
+
629
+sub git_commit {
630
+ my ($log_msg, @parents) = @_;
631
+ assert_revision_unknown($log_msg->{revision});
632
+ my $out_fh = IO::File->new_tmpfile or croak $!;
633
+ my $info = svn_info('.');
634
+ my $uuid = $info->{'Repository UUID'};
635
+ defined $uuid or croak "Unable to get Repository UUID\n";
636
+
637
+ # commit parents can be conditionally bound to a particular
638
+ # svn revision via: "svn_revno=commit_sha1", filter them out here:
639
+ my @exec_parents;
640
+ foreach my $p (@parents) {
641
+ next unless defined $p;
642
+ if ($p =~ /^(\d+)=($sha1_short)$/o) {
643
+ if ($1 == $log_msg->{revision}) {
644
+ push @exec_parents, $2;
645
+ }
646
+ } else {
647
+ push @exec_parents, $p if $p =~ /$sha1_short/o;
648
+ }
649
+ }
650
+
651
+ my $pid = fork;
652
+ defined $pid or croak $!;
653
+ if ($pid == 0) {
654
+ $ENV{GIT_INDEX_FILE} = $GIT_SVN_INDEX;
655
+ git_addremove();
656
+ chomp(my $tree = `git-write-tree`);
657
+ croak if $?;
658
+ my $msg_fh = IO::File->new_tmpfile or croak $!;
659
+ print $msg_fh $log_msg->{msg}, "\ngit-svn-id: ",
660
+ "$SVN_URL\@$log_msg->{revision}",
661
+ " $uuid\n" or croak $!;
662
+ $msg_fh->flush == 0 or croak $!;
663
+ seek $msg_fh, 0, 0 or croak $!;
664
+
665
+ $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} =
666
+ $log_msg->{author};
667
+ $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} =
668
+ $log_msg->{author}."\@$uuid";
669
+ $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} =
670
+ $log_msg->{date};
671
+ my @exec = ('git-commit-tree',$tree);
672
+ push @exec, '-p', $_ foreach @exec_parents;
673
+ open STDIN, '<&', $msg_fh or croak $!;
674
+ open STDOUT, '>&', $out_fh or croak $!;
675
+ exec @exec or croak $!;
676
+ }
677
+ waitpid($pid,0);
678
+ croak if $?;
679
+
680
+ $out_fh->flush == 0 or croak $!;
681
+ seek $out_fh, 0, 0 or croak $!;
682
+ chomp(my $commit = do { local $/; <$out_fh> });
683
+ if ($commit !~ /^$sha1$/o) {
684
+ croak "Failed to commit, invalid sha1: $commit\n";
685
+ }
686
+ my @update_ref = ('git-update-ref',"refs/heads/$GIT_SVN-HEAD",$commit);
687
+ if (my $primary_parent = shift @exec_parents) {
688
+ push @update_ref, $primary_parent;
689
+ }
690
+ sys(@update_ref);
691
+ sys('git-update-ref',"$GIT_SVN/revs/$log_msg->{revision}",$commit);
692
+ print "r$log_msg->{revision} = $commit\n";
693
+ return $commit;
694
+}
695
+
696
+sub blob_to_symlink {
697
+ my ($blob, $link) = @_;
698
+ defined $link or croak "\$link not defined!\n";
699
+ croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
700
+ my $dest = `git-cat-file blob $blob`; # no newline, so no chomp
701
+ symlink $dest, $link or croak $!;
702
+}
703
+
704
+sub blob_to_file {
705
+ my ($blob, $file) = @_;
706
+ defined $file or croak "\$file not defined!\n";
707
+ croak "Not a sha1: $blob\n" unless $blob =~ /^$sha1$/o;
708
+ open my $blob_fh, '>', $file or croak "$!: $file\n";
709
+ my $pid = fork;
710
+ defined $pid or croak $!;
711
+
712
+ if ($pid == 0) {
713
+ open STDOUT, '>&', $blob_fh or croak $!;
714
+ exec('git-cat-file','blob',$blob);
715
+ }
716
+ waitpid $pid, 0;
717
+ croak $? if $?;
718
+
719
+ close $blob_fh or croak $!;
720
+}
721
+
722
+sub safe_qx {
723
+ my $pid = open my $child, '-|';
724
+ defined $pid or croak $!;
725
+ if ($pid == 0) {
726
+ exec(@_) or croak $?;
727
+ }
728
+ my @ret = (<$child>);
729
+ close $child or croak $?;
730
+ die $? if $?; # just in case close didn't error out
731
+ return wantarray ? @ret : join('',@ret);
732
+}
733
+
734
+sub svn_check_ignore_externals {
735
+ return if $_no_ignore_ext;
736
+ unless (grep /ignore-externals/,(safe_qx(qw(svn co -h)))) {
737
+ print STDERR "W: Installed svn version does not support ",
738
+ "--ignore-externals\n";
739
+ $_no_ignore_ext = 1;
740
+ }
741
+}
742
+__END__
743
+
744
+Data structures:
745
+
746
+@svn_log = array of log_msg hashes
747
+
748
+$log_msg hash
749
+{
750
+ msg => 'whitespace-formatted log entry
751
+', # trailing newline is preserved
752
+ revision => '8', # integer
753
+ date => '2004-02-24T17:01:44.108345Z', # commit date
754
+ author => 'committer name'
755
+};
756
+
757
+
758
+@mods = array of diff-index line hashes, each element represents one line
759
+ of diff-index output
760
+
761
+diff-index line ($m hash)
762
+{
763
+ mode_a => first column of diff-index output, no leading ':',
764
+ mode_b => second column of diff-index output,
765
+ sha1_b => sha1sum of the final blob,
766
+ chg => change type [MCRAD],
767
+ file_a => original file name of a file (iff chg is 'C' or 'R')
768
+ file_b => new/current file name of a file (any chg)
769
+}
770
+;
contrib/git-svn/git-svn.txt
new
+208
@@ -0,0 +1,208 @@
1
+git-svn(1)
2
+==========
3
+
4
+NAME
5
+----
6
+git-svn - bidirectional operation between a single Subversion branch and git
7
+
8
+SYNOPSIS
9
+--------
10
+'git-svn' <command> [options] [arguments]
11
+
12
+DESCRIPTION
13
+-----------
14
+git-svn is a simple conduit for changesets between a single Subversion
15
+branch and git.
16
+
17
+git-svn is not to be confused with git-svnimport. The were designed
18
+with very different goals in mind.
19
+
20
+git-svn is designed for an individual developer who wants a
21
+bidirectional flow of changesets between a single branch in Subversion
22
+and an arbitrary number of branches in git. git-svnimport is designed
23
+for read-only operation on repositories that match a particular layout
24
+(albeit the recommended one by SVN developers).
25
+
26
+For importing svn, git-svnimport is potentially more powerful when
27
+operating on repositories organized under the recommended
28
+trunk/branch/tags structure, and should be faster, too.
29
+
30
+git-svn completely ignores the very limited view of branching that
31
+Subversion has. This allows git-svn to be much easier to use,
32
+especially on repositories that are not organized in a manner that
33
+git-svnimport is designed for.
34
+
35
+COMMANDS
36
+--------
37
+init::
38
+ Creates an empty git repository with additional metadata
39
+ directories for git-svn. The SVN_URL must be specified
40
+ at this point.
41
+
42
+fetch::
43
+ Fetch unfetched revisions from the SVN_URL we are tracking.
44
+ refs/heads/git-svn-HEAD will be updated to the latest revision.
45
+
46
+commit::
47
+ Commit specified commit or tree objects to SVN. This relies on
48
+ your imported fetch data being up-to-date. This makes
49
+ absolutely no attempts to do patching when committing to SVN, it
50
+ simply overwrites files with those specified in the tree or
51
+ commit. All merging is assumed to have taken place
52
+ independently of git-svn functions.
53
+
54
+rebuild::
55
+ Not a part of daily usage, but this is a useful command if
56
+ you've just cloned a repository (using git-clone) that was
57
+ tracked with git-svn. Unfortunately, git-clone does not clone
58
+ git-svn metadata and the svn working tree that git-svn uses for
59
+ its operations. This rebuilds the metadata so git-svn can
60
+ resume fetch operations. SVN_URL may be optionally specified if
61
+ the directory/repository you're tracking has moved or changed
62
+ protocols.
63
+
64
+OPTIONS
65
+-------
66
+-r <ARG>::
67
+--revision <ARG>::
68
+ Only used with the 'fetch' command.
69
+
70
+ Takes any valid -r<argument> svn would accept and passes it
71
+ directly to svn. -r<ARG1>:<ARG2> ranges and "{" DATE "}" syntax
72
+ is also supported. This is passed directly to svn, see svn
73
+ documentation for more details.
74
+
75
+ This can allow you to make partial mirrors when running fetch.
76
+
77
+-::
78
+--stdin::
79
+ Only used with the 'commit' command.
80
+
81
+ Read a list of commits from stdin and commit them in reverse
82
+ order. Only the leading sha1 is read from each line, so
83
+ git-rev-list --pretty=oneline output can be used.
84
+
85
+--rmdir::
86
+ Only used with the 'commit' command.
87
+
88
+ Remove directories from the SVN tree if there are no files left
89
+ behind. SVN can version empty directories, and they are not
90
+ removed by default if there are no files left in them. git
91
+ cannot version empty directories. Enabling this flag will make
92
+ the commit to SVN act like git.
93
+
94
+-e::
95
+--edit::
96
+ Only used with the 'commit' command.
97
+
98
+ Edit the commit message before committing to SVN. This is off by
99
+ default for objects that are commits, and forced on when committing
100
+ tree objects.
101
+
102
+COMPATIBILITY OPTIONS
103
+---------------------
104
+--no-ignore-externals::
105
+ Only used with the 'fetch' and 'rebuild' command.
106
+
107
+ By default, git-svn passes --ignore-externals to svn to avoid
108
+ fetching svn:external trees into git. Pass this flag to enable
109
+ externals tracking directly via git.
110
+
111
+ Versions of svn that do not support --ignore-externals are
112
+ automatically detected and this flag will be automatically
113
+ enabled for them.
114
+
115
+ Otherwise, do not enable this flag unless you know what you're
116
+ doing.
117
+
118
+--no-stop-on-copy::
119
+ Only used with the 'fetch' command.
120
+
121
+ By default, git-svn passes --stop-on-copy to avoid dealing with
122
+ the copied/renamed branch directory problem entirely. A
123
+ copied/renamed branch is the result of a <SVN_URL> being created
124
+ in the past from a different source. These are problematic to
125
+ deal with even when working purely with svn if you work inside
126
+ subdirectories.
127
+
128
+ Do not use this flag unless you know exactly what you're getting
129
+ yourself into. You have been warned.
130
+
131
+Examples
132
+~~~~~~~~
133
+
134
+Tracking and contributing to an Subversion managed-project:
135
+
136
+# Initialize a tree (like git init-db)::
137
+ git-svn init http://svn.foo.org/project/trunk
138
+# Fetch remote revisions::
139
+ git-svn fetch
140
+# Create your own branch to hack on::
141
+ git checkout -b my-branch git-svn-HEAD
142
+# Commit only the git commits you want to SVN::
143
+ git-svn commit <tree-ish> [<tree-ish_2> ...]
144
+# Commit all the git commits from my-branch that don't exist in SVN::
145
+ git rev-list --pretty=oneline git-svn-HEAD..my-branch | git-svn commit
146
+# Something is committed to SVN, pull the latest into your branch::
147
+ git-svn fetch && git pull . git-svn-HEAD
148
+
149
+DESIGN PHILOSOPHY
150
+-----------------
151
+Merge tracking in Subversion is lacking and doing branched development
152
+with Subversion is cumbersome as a result. git-svn completely forgoes
153
+any automated merge/branch tracking on the Subversion side and leaves it
154
+entirely up to the user on the git side. It's simply not worth it to do
155
+a useful translation when the the original signal is weak.
156
+
157
+TRACKING MULTIPLE REPOSITORIES OR BRANCHES
158
+------------------------------------------
159
+This is for advanced users, most users should ignore this section.
160
+
161
+Because git-svn does not care about relationships between different
162
+branches or directories in a Subversion repository, git-svn has a simple
163
+hack to allow it to track an arbitrary number of related _or_ unrelated
164
+SVN repositories via one git repository. Simply set the GIT_SVN_ID
165
+environment variable to a name other other than "git-svn" (the default)
166
+and git-svn will ignore the contents of the $GIT_DIR/git-svn directory
167
+and instead do all of its work in $GIT_DIR/$GIT_SVN_ID for that
168
+invocation.
169
+
170
+ADDITIONAL FETCH ARGUMENTS
171
+--------------------------
172
+This is for advanced users, most users should ignore this section.
173
+
174
+Unfetched SVN revisions may be imported as children of existing commits
175
+by specifying additional arguments to 'fetch'. Additional parents may
176
+optionally be specified in the form of sha1 hex sums at the
177
+command-line. Unfetched SVN revisions may also be tied to particular
178
+git commits with the following syntax:
179
+
180
+ svn_revision_number=git_commit_sha1
181
+
182
+This allows you to tie unfetched SVN revision 375 to your current HEAD::
183
+
184
+ git-svn fetch 375=$(git-rev-parse HEAD)
185
+
186
+BUGS
187
+----
188
+If somebody commits a conflicting changeset to SVN at a bad moment
189
+(right before you commit) causing a conflict and your commit to fail,
190
+your svn working tree ($GIT_DIR/git-svn/tree) may be dirtied. The
191
+easiest thing to do is probably just to rm -rf $GIT_DIR/git-svn/tree and
192
+run 'rebuild'.
193
+
194
+We ignore all SVN properties except svn:executable. Too difficult to
195
+map them since we rely heavily on git write-tree being _exactly_ the
196
+same on both the SVN and git working trees and I prefer not to clutter
197
+working trees with metadata files.
198
+
199
+svn:keywords can't be ignored in Subversion (at least I don't know of
200
+a way to ignore them).
201
+
202
+Author
203
+------
204
+Written by Eric Wong <normalperson@yhbt.net>.
205
+
206
+Documentation
207
+-------------
208
+Written by Eric Wong <normalperson@yhbt.net>.