Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
| 2 | |
| 3 | # This tool is copyright (c) 2005, Matthias Urlichs. |
| 4 | # It is released under the Gnu Public License, version 2. |
| 5 | # |
| 6 | # The basic idea is to pull and analyze SVN changes. |
| 7 | # |
Matthias Urlichs | 4a91b79 | 2005-10-11 17:02:45 +0200 | [diff] [blame] | 8 | # Checking out the files is done by a single long-running SVN connection. |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 9 | # |
| 10 | # The head revision is on branch "origin" by default. |
| 11 | # You can change that with the '-o' option. |
| 12 | |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 13 | use strict; |
| 14 | use warnings; |
| 15 | use Getopt::Std; |
Karl Hasselström | d3cac2c | 2006-02-28 00:08:19 +0100 | [diff] [blame] | 16 | use File::Copy; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 17 | use File::Spec; |
| 18 | use File::Temp qw(tempfile); |
| 19 | use File::Path qw(mkpath); |
| 20 | use File::Basename qw(basename dirname); |
| 21 | use Time::Local; |
| 22 | use IO::Pipe; |
| 23 | use POSIX qw(strftime dup2); |
| 24 | use IPC::Open2; |
| 25 | use SVN::Core; |
| 26 | use SVN::Ra; |
| 27 | |
Junio C Hamano | f3ad062 | 2005-11-04 23:30:12 -0800 | [diff] [blame] | 28 | die "Need SVN:Core 1.2.1 or better" if $SVN::Core::VERSION lt "1.2.1"; |
Matthias Urlichs | 37dcf6d | 2005-10-10 13:42:48 +0200 | [diff] [blame] | 29 | |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 30 | $SIG{'PIPE'}="IGNORE"; |
| 31 | $ENV{'TZ'}="UTC"; |
| 32 | |
Karl Hasselström | c55f3ff | 2006-02-26 06:11:29 +0100 | [diff] [blame] | 33 | our($opt_h,$opt_o,$opt_v,$opt_u,$opt_C,$opt_i,$opt_m,$opt_M,$opt_t,$opt_T, |
Karl Hasselström | 36610b2 | 2006-02-26 06:11:31 +0100 | [diff] [blame] | 34 | $opt_b,$opt_r,$opt_I,$opt_A,$opt_s,$opt_l,$opt_d,$opt_D); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 35 | |
| 36 | sub usage() { |
| 37 | print STDERR <<END; |
Junio C Hamano | f3ad062 | 2005-11-04 23:30:12 -0800 | [diff] [blame] | 38 | Usage: ${\basename $0} # fetch/update GIT from SVN |
Matthias Urlichs | 0349080 | 2005-11-29 08:13:04 +0100 | [diff] [blame] | 39 | [-o branch-for-HEAD] [-h] [-v] [-l max_rev] |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 40 | [-C GIT_repository] [-t tagname] [-T trunkname] [-b branchname] |
Karl Hasselström | c55f3ff | 2006-02-26 06:11:29 +0100 | [diff] [blame] | 41 | [-d|-D] [-i] [-u] [-r] [-I ignorefilename] [-s start_chg] |
Karl Hasselström | 36610b2 | 2006-02-26 06:11:31 +0100 | [diff] [blame] | 42 | [-m] [-M regex] [-A author_file] [SVN_URL] |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 43 | END |
| 44 | exit(1); |
| 45 | } |
| 46 | |
Karl Hasselström | 36610b2 | 2006-02-26 06:11:31 +0100 | [diff] [blame] | 47 | getopts("A:b:C:dDhiI:l:mM:o:rs:t:T:uv") or usage(); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 48 | usage if $opt_h; |
| 49 | |
| 50 | my $tag_name = $opt_t || "tags"; |
| 51 | my $trunk_name = $opt_T || "trunk"; |
| 52 | my $branch_name = $opt_b || "branches"; |
| 53 | |
Matthias Urlichs | 25f6f32 | 2005-10-11 18:13:30 +0200 | [diff] [blame] | 54 | @ARGV == 1 or @ARGV == 2 or usage(); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 55 | |
| 56 | $opt_o ||= "origin"; |
Matthias Urlichs | 2fa9204 | 2005-10-11 16:22:03 +0200 | [diff] [blame] | 57 | $opt_s ||= 1; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 58 | my $git_tree = $opt_C; |
| 59 | $git_tree ||= "."; |
| 60 | |
Matthias Urlichs | 4a91b79 | 2005-10-11 17:02:45 +0200 | [diff] [blame] | 61 | my $svn_url = $ARGV[0]; |
Matthias Urlichs | 25f6f32 | 2005-10-11 18:13:30 +0200 | [diff] [blame] | 62 | my $svn_dir = $ARGV[1]; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 63 | |
| 64 | our @mergerx = (); |
| 65 | if ($opt_m) { |
Florian Forster | 65160b8 | 2006-05-31 12:32:08 +0200 | [diff] [blame] | 66 | my $branch_esc = quotemeta ($branch_name); |
| 67 | my $trunk_esc = quotemeta ($trunk_name); |
| 68 | @mergerx = |
| 69 | ( |
| 70 | qr!\b(?:merg(?:ed?|ing))\b.*?\b((?:(?<=$branch_esc/)[\w\.\-]+)|(?:$trunk_esc))\b!i, |
| 71 | qr!\b(?:from|of)\W+((?:(?<=$branch_esc/)[\w\.\-]+)|(?:$trunk_esc))\b!i, |
| 72 | qr!\b(?:from|of)\W+(?:the )?([\w\.\-]+)[-\s]branch\b!i |
| 73 | ); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 74 | } |
| 75 | if ($opt_M) { |
Florian Forster | 65160b8 | 2006-05-31 12:32:08 +0200 | [diff] [blame] | 76 | unshift (@mergerx, qr/$opt_M/); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Karl Hasselström | d3cac2c | 2006-02-28 00:08:19 +0100 | [diff] [blame] | 79 | # Absolutize filename now, since we will have chdir'ed by the time we |
| 80 | # get around to opening it. |
| 81 | $opt_A = File::Spec->rel2abs($opt_A) if $opt_A; |
| 82 | |
Karl Hasselström | 36610b2 | 2006-02-26 06:11:31 +0100 | [diff] [blame] | 83 | our %users = (); |
Karl Hasselström | d3cac2c | 2006-02-28 00:08:19 +0100 | [diff] [blame] | 84 | our $users_file = undef; |
| 85 | sub read_users($) { |
| 86 | $users_file = File::Spec->rel2abs(@_); |
| 87 | die "Cannot open $users_file\n" unless -f $users_file; |
| 88 | open(my $authors,$users_file); |
Karl Hasselström | 36610b2 | 2006-02-26 06:11:31 +0100 | [diff] [blame] | 89 | while(<$authors>) { |
| 90 | chomp; |
Karl Hasselström | 80804d0 | 2006-02-28 00:08:15 +0100 | [diff] [blame] | 91 | next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/; |
Karl Hasselström | 36610b2 | 2006-02-26 06:11:31 +0100 | [diff] [blame] | 92 | (my $user,my $name,my $email) = ($1,$2,$3); |
| 93 | $users{$user} = [$name,$email]; |
| 94 | } |
| 95 | close($authors); |
| 96 | } |
| 97 | |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 98 | select(STDERR); $|=1; select(STDOUT); |
| 99 | |
| 100 | |
| 101 | package SVNconn; |
| 102 | # Basic SVN connection. |
| 103 | # We're only interested in connecting and downloading, so ... |
| 104 | |
| 105 | use File::Spec; |
| 106 | use File::Temp qw(tempfile); |
| 107 | use POSIX qw(strftime dup2); |
Herbert Valerio Riedel | 08ddd4f | 2006-04-17 06:58:39 -0400 | [diff] [blame] | 108 | use Fcntl qw(SEEK_SET); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 109 | |
| 110 | sub new { |
| 111 | my($what,$repo) = @_; |
| 112 | $what=ref($what) if ref($what); |
| 113 | |
| 114 | my $self = {}; |
| 115 | $self->{'buffer'} = ""; |
| 116 | bless($self,$what); |
| 117 | |
| 118 | $repo =~ s#/+$##; |
| 119 | $self->{'fullrep'} = $repo; |
| 120 | $self->conn(); |
| 121 | |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 122 | return $self; |
| 123 | } |
| 124 | |
| 125 | sub conn { |
| 126 | my $self = shift; |
| 127 | my $repo = $self->{'fullrep'}; |
Eric Wong | 2961e0e | 2006-01-01 13:25:47 -0800 | [diff] [blame] | 128 | my $auth = SVN::Core::auth_open ([SVN::Client::get_simple_provider, |
| 129 | SVN::Client::get_ssl_server_trust_file_provider, |
| 130 | SVN::Client::get_username_provider]); |
| 131 | my $s = SVN::Ra->new(url => $repo, auth => $auth); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 132 | die "SVN connection to $repo: $!\n" unless defined $s; |
| 133 | $self->{'svn'} = $s; |
| 134 | $self->{'repo'} = $repo; |
| 135 | $self->{'maxrev'} = $s->get_latest_revnum(); |
| 136 | } |
| 137 | |
| 138 | sub file { |
| 139 | my($self,$path,$rev) = @_; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 140 | |
Junio C Hamano | 2950411 | 2005-10-16 11:55:35 -0700 | [diff] [blame] | 141 | my ($fh, $name) = tempfile('gitsvn.XXXXXX', |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 142 | DIR => File::Spec->tmpdir(), UNLINK => 1); |
| 143 | |
Matthias Urlichs | 2b5e63d | 2005-10-10 12:33:22 +0200 | [diff] [blame] | 144 | print "... $rev $path ...\n" if $opt_v; |
Karl Hasselström | 4802426 | 2006-02-26 06:11:27 +0100 | [diff] [blame] | 145 | my (undef, $properties); |
Santi_Béjar | d598075 | 2006-03-27 13:26:01 +0200 | [diff] [blame] | 146 | my $pool = SVN::Pool->new(); |
Karl Hasselström | 4802426 | 2006-02-26 06:11:27 +0100 | [diff] [blame] | 147 | eval { (undef, $properties) |
Santi_Béjar | d598075 | 2006-03-27 13:26:01 +0200 | [diff] [blame] | 148 | = $self->{'svn'}->get_file($path,$rev,$fh,$pool); }; |
| 149 | $pool->clear; |
Matthias Urlichs | 25f6f32 | 2005-10-11 18:13:30 +0200 | [diff] [blame] | 150 | if($@) { |
| 151 | return undef if $@ =~ /Attempted to get checksum/; |
| 152 | die $@; |
| 153 | } |
Karl Hasselström | 4802426 | 2006-02-26 06:11:27 +0100 | [diff] [blame] | 154 | my $mode; |
| 155 | if (exists $properties->{'svn:executable'}) { |
Herbert Valerio Riedel | 08ddd4f | 2006-04-17 06:58:39 -0400 | [diff] [blame] | 156 | $mode = '100755'; |
| 157 | } elsif (exists $properties->{'svn:special'}) { |
| 158 | my ($special_content, $filesize); |
| 159 | $filesize = tell $fh; |
| 160 | seek $fh, 0, SEEK_SET; |
| 161 | read $fh, $special_content, $filesize; |
| 162 | if ($special_content =~ s/^link //) { |
| 163 | $mode = '120000'; |
| 164 | seek $fh, 0, SEEK_SET; |
| 165 | truncate $fh, 0; |
| 166 | print $fh $special_content; |
| 167 | } else { |
| 168 | die "unexpected svn:special file encountered"; |
| 169 | } |
Karl Hasselström | 4802426 | 2006-02-26 06:11:27 +0100 | [diff] [blame] | 170 | } else { |
Herbert Valerio Riedel | 08ddd4f | 2006-04-17 06:58:39 -0400 | [diff] [blame] | 171 | $mode = '100644'; |
Karl Hasselström | 4802426 | 2006-02-26 06:11:27 +0100 | [diff] [blame] | 172 | } |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 173 | close ($fh); |
| 174 | |
Karl Hasselström | 4802426 | 2006-02-26 06:11:27 +0100 | [diff] [blame] | 175 | return ($name, $mode); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Karl Hasselström | c55f3ff | 2006-02-26 06:11:29 +0100 | [diff] [blame] | 178 | sub ignore { |
| 179 | my($self,$path,$rev) = @_; |
| 180 | |
| 181 | print "... $rev $path ...\n" if $opt_v; |
| 182 | my (undef,undef,$properties) |
| 183 | = $self->{'svn'}->get_dir($path,$rev,undef); |
| 184 | if (exists $properties->{'svn:ignore'}) { |
| 185 | my ($fh, $name) = tempfile('gitsvn.XXXXXX', |
| 186 | DIR => File::Spec->tmpdir(), |
| 187 | UNLINK => 1); |
| 188 | print $fh $properties->{'svn:ignore'}; |
| 189 | close($fh); |
| 190 | return $name; |
| 191 | } else { |
| 192 | return undef; |
| 193 | } |
| 194 | } |
| 195 | |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 196 | package main; |
Matthias Urlichs | 25f6f32 | 2005-10-11 18:13:30 +0200 | [diff] [blame] | 197 | use URI; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 198 | |
Matthias Urlichs | 0349080 | 2005-11-29 08:13:04 +0100 | [diff] [blame] | 199 | our $svn = $svn_url; |
Matthias Urlichs | 25f6f32 | 2005-10-11 18:13:30 +0200 | [diff] [blame] | 200 | $svn .= "/$svn_dir" if defined $svn_dir; |
Matthias Urlichs | 0349080 | 2005-11-29 08:13:04 +0100 | [diff] [blame] | 201 | my $svn2 = SVNconn->new($svn); |
Matthias Urlichs | 25f6f32 | 2005-10-11 18:13:30 +0200 | [diff] [blame] | 202 | $svn = SVNconn->new($svn); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 203 | |
Matthias Urlichs | 25f6f32 | 2005-10-11 18:13:30 +0200 | [diff] [blame] | 204 | my $lwp_ua; |
| 205 | if($opt_d or $opt_D) { |
| 206 | $svn_url = URI->new($svn_url)->canonical; |
| 207 | if($opt_D) { |
| 208 | $svn_dir =~ s#/*$#/#; |
| 209 | } else { |
| 210 | $svn_dir = ""; |
| 211 | } |
| 212 | if ($svn_url->scheme eq "http") { |
| 213 | use LWP::UserAgent; |
| 214 | $lwp_ua = LWP::UserAgent->new(keep_alive => 1, requests_redirectable => []); |
| 215 | } else { |
| 216 | print STDERR "Warning: not HTTP; turning off direct file access\n"; |
| 217 | $opt_d=0; |
| 218 | } |
| 219 | } |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 220 | |
| 221 | sub pdate($) { |
| 222 | my($d) = @_; |
| 223 | $d =~ m#(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)# |
| 224 | or die "Unparseable date: $d\n"; |
| 225 | my $y=$1; $y-=1900 if $y>1900; |
| 226 | return timegm($6||0,$5,$4,$3,$2-1,$y); |
| 227 | } |
| 228 | |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 229 | sub getwd() { |
| 230 | my $pwd = `pwd`; |
| 231 | chomp $pwd; |
| 232 | return $pwd; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | sub get_headref($$) { |
| 237 | my $name = shift; |
Junio C Hamano | 2950411 | 2005-10-16 11:55:35 -0700 | [diff] [blame] | 238 | my $git_dir = shift; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 239 | my $sha; |
Junio C Hamano | 2950411 | 2005-10-16 11:55:35 -0700 | [diff] [blame] | 240 | |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 241 | if (open(C,"$git_dir/refs/heads/$name")) { |
| 242 | chomp($sha = <C>); |
| 243 | close(C); |
| 244 | length($sha) == 40 |
| 245 | or die "Cannot get head id for $name ($sha): $!\n"; |
| 246 | } |
| 247 | return $sha; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | -d $git_tree |
| 252 | or mkdir($git_tree,0777) |
| 253 | or die "Could not create $git_tree: $!"; |
| 254 | chdir($git_tree); |
| 255 | |
| 256 | my $orig_branch = ""; |
| 257 | my $forward_master = 0; |
| 258 | my %branches; |
| 259 | |
| 260 | my $git_dir = $ENV{"GIT_DIR"} || ".git"; |
| 261 | $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#; |
| 262 | $ENV{"GIT_DIR"} = $git_dir; |
| 263 | my $orig_git_index; |
| 264 | $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE}; |
| 265 | my ($git_ih, $git_index) = tempfile('gitXXXXXX', SUFFIX => '.idx', |
| 266 | DIR => File::Spec->tmpdir()); |
| 267 | close ($git_ih); |
| 268 | $ENV{GIT_INDEX_FILE} = $git_index; |
| 269 | my $maxnum = 0; |
| 270 | my $last_rev = ""; |
| 271 | my $last_branch; |
Matthias Urlichs | 0349080 | 2005-11-29 08:13:04 +0100 | [diff] [blame] | 272 | my $current_rev = $opt_s || 1; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 273 | unless(-d $git_dir) { |
| 274 | system("git-init-db"); |
| 275 | die "Cannot init the GIT db at $git_tree: $?\n" if $?; |
| 276 | system("git-read-tree"); |
| 277 | die "Cannot init an empty tree: $?\n" if $?; |
| 278 | |
| 279 | $last_branch = $opt_o; |
| 280 | $orig_branch = ""; |
| 281 | } else { |
| 282 | -f "$git_dir/refs/heads/$opt_o" |
| 283 | or die "Branch '$opt_o' does not exist.\n". |
| 284 | "Either use the correct '-o branch' option,\n". |
| 285 | "or import to a new repository.\n"; |
| 286 | |
| 287 | -f "$git_dir/svn2git" |
| 288 | or die "'$git_dir/svn2git' does not exist.\n". |
| 289 | "You need that file for incremental imports.\n"; |
Pavel Roskin | 8366a10 | 2005-11-16 13:27:28 -0500 | [diff] [blame] | 290 | open(F, "git-symbolic-ref HEAD |") or |
| 291 | die "Cannot run git-symbolic-ref: $!\n"; |
| 292 | chomp ($last_branch = <F>); |
| 293 | $last_branch = basename($last_branch); |
| 294 | close(F); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 295 | unless($last_branch) { |
| 296 | warn "Cannot read the last branch name: $! -- assuming 'master'\n"; |
| 297 | $last_branch = "master"; |
| 298 | } |
| 299 | $orig_branch = $last_branch; |
| 300 | $last_rev = get_headref($orig_branch, $git_dir); |
| 301 | if (-f "$git_dir/SVN2GIT_HEAD") { |
| 302 | die <<EOM; |
| 303 | SVN2GIT_HEAD exists. |
| 304 | Make sure your working directory corresponds to HEAD and remove SVN2GIT_HEAD. |
| 305 | You may need to run |
| 306 | |
| 307 | git-read-tree -m -u SVN2GIT_HEAD HEAD |
| 308 | EOM |
| 309 | } |
| 310 | system('cp', "$git_dir/HEAD", "$git_dir/SVN2GIT_HEAD"); |
| 311 | |
| 312 | $forward_master = |
| 313 | $opt_o ne 'master' && -f "$git_dir/refs/heads/master" && |
Junio C Hamano | 2950411 | 2005-10-16 11:55:35 -0700 | [diff] [blame] | 314 | system('cmp', '-s', "$git_dir/refs/heads/master", |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 315 | "$git_dir/refs/heads/$opt_o") == 0; |
| 316 | |
| 317 | # populate index |
| 318 | system('git-read-tree', $last_rev); |
| 319 | die "read-tree failed: $?\n" if $?; |
| 320 | |
| 321 | # Get the last import timestamps |
| 322 | open my $B,"<", "$git_dir/svn2git"; |
| 323 | while(<$B>) { |
| 324 | chomp; |
| 325 | my($num,$branch,$ref) = split; |
| 326 | $branches{$branch}{$num} = $ref; |
| 327 | $branches{$branch}{"LAST"} = $ref; |
Matthias Urlichs | 0349080 | 2005-11-29 08:13:04 +0100 | [diff] [blame] | 328 | $current_rev = $num+1 if $current_rev <= $num; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 329 | } |
| 330 | close($B); |
| 331 | } |
| 332 | -d $git_dir |
| 333 | or die "Could not create git subdir ($git_dir).\n"; |
| 334 | |
Karl Hasselström | d3cac2c | 2006-02-28 00:08:19 +0100 | [diff] [blame] | 335 | my $default_authors = "$git_dir/svn-authors"; |
| 336 | if ($opt_A) { |
| 337 | read_users($opt_A); |
| 338 | copy($opt_A,$default_authors) or die "Copy failed: $!"; |
| 339 | } else { |
| 340 | read_users($default_authors) if -f $default_authors; |
| 341 | } |
| 342 | |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 343 | open BRANCHES,">>", "$git_dir/svn2git"; |
| 344 | |
Yaacov Akiba Slama | cbce5d8 | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 345 | sub node_kind($$$) { |
| 346 | my ($branch, $path, $revision) = @_; |
| 347 | my $pool=SVN::Pool->new; |
| 348 | my $kind = $svn->{'svn'}->check_path(revert_split_path($branch,$path),$revision,$pool); |
| 349 | $pool->clear; |
| 350 | return $kind; |
| 351 | } |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 352 | |
Yaacov Akiba Slama | cbce5d8 | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 353 | sub revert_split_path($$) { |
| 354 | my($branch,$path) = @_; |
| 355 | |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 356 | my $svnpath; |
| 357 | $path = "" if $path eq "/"; # this should not happen, but ... |
| 358 | if($branch eq "/") { |
Matthias Urlichs | 0090a61 | 2005-10-11 19:42:27 +0200 | [diff] [blame] | 359 | $svnpath = "$trunk_name/$path"; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 360 | } elsif($branch =~ m#^/#) { |
Matthias Urlichs | 0090a61 | 2005-10-11 19:42:27 +0200 | [diff] [blame] | 361 | $svnpath = "$tag_name$branch/$path"; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 362 | } else { |
Matthias Urlichs | 0090a61 | 2005-10-11 19:42:27 +0200 | [diff] [blame] | 363 | $svnpath = "$branch_name/$branch/$path"; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Matthias Urlichs | 4b1ca25 | 2005-11-14 08:31:00 +0100 | [diff] [blame] | 366 | $svnpath =~ s#/+$##; |
| 367 | return $svnpath; |
Yaacov Akiba Slama | cbce5d8 | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | sub get_file($$$) { |
| 371 | my($rev,$branch,$path) = @_; |
| 372 | |
| 373 | my $svnpath = revert_split_path($branch,$path); |
| 374 | |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 375 | # now get it |
Karl Hasselström | 4802426 | 2006-02-26 06:11:27 +0100 | [diff] [blame] | 376 | my ($name,$mode); |
Matthias Urlichs | 25f6f32 | 2005-10-11 18:13:30 +0200 | [diff] [blame] | 377 | if($opt_d) { |
| 378 | my($req,$res); |
| 379 | |
| 380 | # /svn/!svn/bc/2/django/trunk/django-docs/build.py |
| 381 | my $url=$svn_url->clone(); |
| 382 | $url->path($url->path."/!svn/bc/$rev/$svn_dir$svnpath"); |
Matthias Urlichs | 0090a61 | 2005-10-11 19:42:27 +0200 | [diff] [blame] | 383 | print "... $path...\n" if $opt_v; |
Matthias Urlichs | 25f6f32 | 2005-10-11 18:13:30 +0200 | [diff] [blame] | 384 | $req = HTTP::Request->new(GET => $url); |
| 385 | $res = $lwp_ua->request($req); |
| 386 | if ($res->is_success) { |
| 387 | my $fh; |
Junio C Hamano | 2950411 | 2005-10-16 11:55:35 -0700 | [diff] [blame] | 388 | ($fh, $name) = tempfile('gitsvn.XXXXXX', |
| 389 | DIR => File::Spec->tmpdir(), UNLINK => 1); |
Matthias Urlichs | 25f6f32 | 2005-10-11 18:13:30 +0200 | [diff] [blame] | 390 | print $fh $res->content; |
| 391 | close($fh) or die "Could not write $name: $!\n"; |
| 392 | } else { |
| 393 | return undef if $res->code == 301; # directory? |
| 394 | die $res->status_line." at $url\n"; |
| 395 | } |
Karl Hasselström | 4802426 | 2006-02-26 06:11:27 +0100 | [diff] [blame] | 396 | $mode = '0644'; # can't obtain mode via direct http request? |
Matthias Urlichs | 25f6f32 | 2005-10-11 18:13:30 +0200 | [diff] [blame] | 397 | } else { |
Karl Hasselström | 4802426 | 2006-02-26 06:11:27 +0100 | [diff] [blame] | 398 | ($name,$mode) = $svn->file("$svnpath",$rev); |
Matthias Urlichs | 25f6f32 | 2005-10-11 18:13:30 +0200 | [diff] [blame] | 399 | return undef unless defined $name; |
| 400 | } |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 401 | |
Junio C Hamano | 7ae0dc0 | 2006-02-20 14:14:15 -0800 | [diff] [blame] | 402 | my $pid = open(my $F, '-|'); |
| 403 | die $! unless defined $pid; |
| 404 | if (!$pid) { |
| 405 | exec("git-hash-object", "-w", $name) |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 406 | or die "Cannot create object: $!\n"; |
Junio C Hamano | 7ae0dc0 | 2006-02-20 14:14:15 -0800 | [diff] [blame] | 407 | } |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 408 | my $sha = <$F>; |
| 409 | chomp $sha; |
| 410 | close $F; |
Matthias Urlichs | 22dcbb7 | 2005-10-10 18:54:53 +0200 | [diff] [blame] | 411 | unlink $name; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 412 | return [$mode, $sha, $path]; |
| 413 | } |
| 414 | |
Karl Hasselström | c55f3ff | 2006-02-26 06:11:29 +0100 | [diff] [blame] | 415 | sub get_ignore($$$$$) { |
| 416 | my($new,$old,$rev,$branch,$path) = @_; |
| 417 | |
| 418 | return unless $opt_I; |
| 419 | my $svnpath = revert_split_path($branch,$path); |
| 420 | my $name = $svn->ignore("$svnpath",$rev); |
| 421 | if ($path eq '/') { |
| 422 | $path = $opt_I; |
| 423 | } else { |
| 424 | $path = File::Spec->catfile($path,$opt_I); |
| 425 | } |
| 426 | if (defined $name) { |
| 427 | my $pid = open(my $F, '-|'); |
| 428 | die $! unless defined $pid; |
| 429 | if (!$pid) { |
| 430 | exec("git-hash-object", "-w", $name) |
| 431 | or die "Cannot create object: $!\n"; |
| 432 | } |
| 433 | my $sha = <$F>; |
| 434 | chomp $sha; |
| 435 | close $F; |
| 436 | unlink $name; |
| 437 | push(@$new,['0644',$sha,$path]); |
| 438 | } else { |
| 439 | push(@$old,$path); |
| 440 | } |
| 441 | } |
| 442 | |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 443 | sub split_path($$) { |
| 444 | my($rev,$path) = @_; |
| 445 | my $branch; |
| 446 | |
| 447 | if($path =~ s#^/\Q$tag_name\E/([^/]+)/?##) { |
| 448 | $branch = "/$1"; |
| 449 | } elsif($path =~ s#^/\Q$trunk_name\E/?##) { |
| 450 | $branch = "/"; |
| 451 | } elsif($path =~ s#^/\Q$branch_name\E/([^/]+)/?##) { |
| 452 | $branch = $1; |
| 453 | } else { |
Yaacov Akiba Slama | c2c07a5 | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 454 | my %no_error = ( |
| 455 | "/" => 1, |
| 456 | "/$tag_name" => 1, |
| 457 | "/$branch_name" => 1 |
| 458 | ); |
| 459 | print STDERR "$rev: Unrecognized path: $path\n" unless (defined $no_error{$path}); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 460 | return () |
| 461 | } |
| 462 | $path = "/" if $path eq ""; |
| 463 | return ($branch,$path); |
| 464 | } |
| 465 | |
Yaacov Akiba Slama | 8168373 | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 466 | sub branch_rev($$) { |
| 467 | |
| 468 | my ($srcbranch,$uptorev) = @_; |
| 469 | |
| 470 | my $bbranches = $branches{$srcbranch}; |
| 471 | my @revs = reverse sort { ($a eq 'LAST' ? 0 : $a) <=> ($b eq 'LAST' ? 0 : $b) } keys %$bbranches; |
| 472 | my $therev; |
| 473 | foreach my $arev(@revs) { |
| 474 | next if ($arev eq 'LAST'); |
| 475 | if ($arev <= $uptorev) { |
| 476 | $therev = $arev; |
| 477 | last; |
| 478 | } |
| 479 | } |
| 480 | return $therev; |
| 481 | } |
| 482 | |
Yaacov Akiba Slama | 109fc2b | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 483 | sub copy_path($$$$$$$$) { |
Matthias Urlichs | 0090a61 | 2005-10-11 19:42:27 +0200 | [diff] [blame] | 484 | # Somebody copied a whole subdirectory. |
| 485 | # We need to find the index entries from the old version which the |
| 486 | # SVN log entry points to, and add them to the new place. |
| 487 | |
Yaacov Akiba Slama | 109fc2b | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 488 | my($newrev,$newbranch,$path,$oldpath,$rev,$node_kind,$new,$parents) = @_; |
Matthias Urlichs | 0090a61 | 2005-10-11 19:42:27 +0200 | [diff] [blame] | 489 | |
Yaacov Akiba Slama | 8168373 | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 490 | my($srcbranch,$srcpath) = split_path($rev,$oldpath); |
Matthias Urlichs | 4b1ca25 | 2005-11-14 08:31:00 +0100 | [diff] [blame] | 491 | unless(defined $srcbranch) { |
| 492 | print "Path not found when copying from $oldpath @ $rev\n"; |
| 493 | return; |
| 494 | } |
Yaacov Akiba Slama | 8168373 | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 495 | my $therev = branch_rev($srcbranch, $rev); |
| 496 | my $gitrev = $branches{$srcbranch}{$therev}; |
Matthias Urlichs | 0090a61 | 2005-10-11 19:42:27 +0200 | [diff] [blame] | 497 | unless($gitrev) { |
| 498 | print STDERR "$newrev:$newbranch: could not find $oldpath \@ $rev\n"; |
| 499 | return; |
| 500 | } |
Yaacov Akiba Slama | 109fc2b | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 501 | if ($srcbranch ne $newbranch) { |
| 502 | push(@$parents, $branches{$srcbranch}{'LAST'}); |
| 503 | } |
Yaacov Akiba Slama | 8168373 | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 504 | print "$newrev:$newbranch:$path: copying from $srcbranch:$srcpath @ $rev\n" if $opt_v; |
| 505 | if ($node_kind eq $SVN::Node::dir) { |
| 506 | $srcpath =~ s#/*$#/#; |
| 507 | } |
| 508 | |
Junio C Hamano | 7ae0dc0 | 2006-02-20 14:14:15 -0800 | [diff] [blame] | 509 | my $pid = open my $f,'-|'; |
| 510 | die $! unless defined $pid; |
| 511 | if (!$pid) { |
| 512 | exec("git-ls-tree","-r","-z",$gitrev,$srcpath) |
| 513 | or die $!; |
| 514 | } |
Matthias Urlichs | 0090a61 | 2005-10-11 19:42:27 +0200 | [diff] [blame] | 515 | local $/ = "\0"; |
| 516 | while(<$f>) { |
| 517 | chomp; |
| 518 | my($m,$p) = split(/\t/,$_,2); |
| 519 | my($mode,$type,$sha1) = split(/ /,$m); |
| 520 | next if $type ne "blob"; |
Yaacov Akiba Slama | 8168373 | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 521 | if ($node_kind eq $SVN::Node::dir) { |
| 522 | $p = $path . substr($p,length($srcpath)-1); |
| 523 | } else { |
| 524 | $p = $path; |
| 525 | } |
| 526 | push(@$new,[$mode,$sha1,$p]); |
Matthias Urlichs | 0090a61 | 2005-10-11 19:42:27 +0200 | [diff] [blame] | 527 | } |
Junio C Hamano | 2950411 | 2005-10-16 11:55:35 -0700 | [diff] [blame] | 528 | close($f) or |
Matthias Urlichs | 0090a61 | 2005-10-11 19:42:27 +0200 | [diff] [blame] | 529 | print STDERR "$newrev:$newbranch: could not list files in $oldpath \@ $rev\n"; |
| 530 | } |
| 531 | |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 532 | sub commit { |
| 533 | my($branch, $changed_paths, $revision, $author, $date, $message) = @_; |
| 534 | my($author_name,$author_email,$dest); |
Yaacov Akiba Slama | 109fc2b | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 535 | my(@old,@new,@parents); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 536 | |
Robin Rosenberg | 35c636e | 2006-07-03 00:21:00 +0200 | [diff] [blame] | 537 | if (not defined $author or $author eq "") { |
Matthias Urlichs | 2b5e63d | 2005-10-10 12:33:22 +0200 | [diff] [blame] | 538 | $author_name = $author_email = "unknown"; |
Karl Hasselström | d3cac2c | 2006-02-28 00:08:19 +0100 | [diff] [blame] | 539 | } elsif (defined $users_file) { |
| 540 | die "User $author is not listed in $users_file\n" |
Karl Hasselström | 36610b2 | 2006-02-26 06:11:31 +0100 | [diff] [blame] | 541 | unless exists $users{$author}; |
| 542 | ($author_name,$author_email) = @{$users{$author}}; |
Matthias Urlichs | 2b5e63d | 2005-10-10 12:33:22 +0200 | [diff] [blame] | 543 | } elsif ($author =~ /^(.*?)\s+<(.*)>$/) { |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 544 | ($author_name, $author_email) = ($1, $2); |
| 545 | } else { |
| 546 | $author =~ s/^<(.*)>$/$1/; |
| 547 | $author_name = $author_email = $author; |
| 548 | } |
| 549 | $date = pdate($date); |
| 550 | |
| 551 | my $tag; |
| 552 | my $parent; |
| 553 | if($branch eq "/") { # trunk |
| 554 | $parent = $opt_o; |
| 555 | } elsif($branch =~ m#^/(.+)#) { # tag |
| 556 | $tag = 1; |
| 557 | $parent = $1; |
| 558 | } else { # "normal" branch |
| 559 | # nothing to do |
| 560 | $parent = $branch; |
| 561 | } |
| 562 | $dest = $parent; |
| 563 | |
| 564 | my $prev = $changed_paths->{"/"}; |
Matthias Urlichs | f0daa62 | 2005-10-10 12:41:15 +0200 | [diff] [blame] | 565 | if($prev and $prev->[0] eq "A") { |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 566 | delete $changed_paths->{"/"}; |
Matthias Urlichs | f0daa62 | 2005-10-10 12:41:15 +0200 | [diff] [blame] | 567 | my $oldpath = $prev->[1]; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 568 | my $rev; |
| 569 | if(defined $oldpath) { |
| 570 | my $p; |
| 571 | ($parent,$p) = split_path($revision,$oldpath); |
| 572 | if($parent eq "/") { |
| 573 | $parent = $opt_o; |
| 574 | } else { |
| 575 | $parent =~ s#^/##; # if it's a tag |
| 576 | } |
| 577 | } else { |
| 578 | $parent = undef; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | my $rev; |
Matthias Urlichs | 7ee74a9 | 2005-10-10 15:14:21 +0200 | [diff] [blame] | 583 | if($revision > $opt_s and defined $parent) { |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 584 | open(H,"git-rev-parse --verify $parent |"); |
| 585 | $rev = <H>; |
| 586 | close(H) or do { |
| 587 | print STDERR "$revision: cannot find commit '$parent'!\n"; |
| 588 | return; |
| 589 | }; |
| 590 | chop $rev; |
| 591 | if(length($rev) != 40) { |
| 592 | print STDERR "$revision: cannot find commit '$parent'!\n"; |
| 593 | return; |
| 594 | } |
| 595 | $rev = $branches{($parent eq $opt_o) ? "/" : $parent}{"LAST"}; |
Matthias Urlichs | 7ee74a9 | 2005-10-10 15:14:21 +0200 | [diff] [blame] | 596 | if($revision != $opt_s and not $rev) { |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 597 | print STDERR "$revision: do not know ancestor for '$parent'!\n"; |
| 598 | return; |
| 599 | } |
| 600 | } else { |
| 601 | $rev = undef; |
| 602 | } |
| 603 | |
Matthias Urlichs | f0daa62 | 2005-10-10 12:41:15 +0200 | [diff] [blame] | 604 | # if($prev and $prev->[0] eq "A") { |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 605 | # if(not $tag) { |
| 606 | # unless(open(H,"> $git_dir/refs/heads/$branch")) { |
| 607 | # print STDERR "$revision: Could not create branch $branch: $!\n"; |
| 608 | # $state=11; |
| 609 | # next; |
| 610 | # } |
| 611 | # print H "$rev\n" |
| 612 | # or die "Could not write branch $branch: $!"; |
| 613 | # close(H) |
| 614 | # or die "Could not write branch $branch: $!"; |
| 615 | # } |
| 616 | # } |
| 617 | if(not defined $rev) { |
| 618 | unlink($git_index); |
| 619 | } elsif ($rev ne $last_rev) { |
| 620 | print "Switching from $last_rev to $rev ($branch)\n" if $opt_v; |
| 621 | system("git-read-tree", $rev); |
| 622 | die "read-tree failed for $rev: $?\n" if $?; |
| 623 | $last_rev = $rev; |
| 624 | } |
| 625 | |
Yaacov Akiba Slama | 109fc2b | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 626 | push (@parents, $rev) if defined $rev; |
| 627 | |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 628 | my $cid; |
| 629 | if($tag and not %$changed_paths) { |
| 630 | $cid = $rev; |
| 631 | } else { |
Matthias Urlichs | 0090a61 | 2005-10-11 19:42:27 +0200 | [diff] [blame] | 632 | my @paths = sort keys %$changed_paths; |
| 633 | foreach my $path(@paths) { |
| 634 | my $action = $changed_paths->{$path}; |
| 635 | |
Yaacov Akiba Slama | 8168373 | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 636 | if ($action->[0] eq "R") { |
| 637 | # refer to a file/tree in an earlier commit |
| 638 | push(@old,$path); # remove any old stuff |
| 639 | } |
| 640 | if(($action->[0] eq "A") || ($action->[0] eq "R")) { |
| 641 | my $node_kind = node_kind($branch,$path,$revision); |
Karl Hasselström | e67c662 | 2006-04-07 08:06:09 +0200 | [diff] [blame] | 642 | if ($node_kind eq $SVN::Node::file) { |
Yaacov Akiba Slama | 8168373 | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 643 | my $f = get_file($revision,$branch,$path); |
| 644 | if ($f) { |
| 645 | push(@new,$f) if $f; |
| 646 | } else { |
| 647 | my $opath = $action->[3]; |
| 648 | print STDERR "$revision: $branch: could not fetch '$opath'\n"; |
| 649 | } |
Karl Hasselström | c55f3ff | 2006-02-26 06:11:29 +0100 | [diff] [blame] | 650 | } elsif ($node_kind eq $SVN::Node::dir) { |
Karl Hasselström | e67c662 | 2006-04-07 08:06:09 +0200 | [diff] [blame] | 651 | if($action->[1]) { |
| 652 | copy_path($revision, $branch, |
| 653 | $path, $action->[1], |
| 654 | $action->[2], $node_kind, |
| 655 | \@new, \@parents); |
| 656 | } else { |
| 657 | get_ignore(\@new, \@old, $revision, |
| 658 | $branch, $path); |
| 659 | } |
Matthias Urlichs | 0090a61 | 2005-10-11 19:42:27 +0200 | [diff] [blame] | 660 | } |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 661 | } elsif ($action->[0] eq "D") { |
| 662 | push(@old,$path); |
| 663 | } elsif ($action->[0] eq "M") { |
Yaacov Akiba Slama | 8168373 | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 664 | my $node_kind = node_kind($branch,$path,$revision); |
| 665 | if ($node_kind eq $SVN::Node::file) { |
| 666 | my $f = get_file($revision,$branch,$path); |
| 667 | push(@new,$f) if $f; |
Karl Hasselström | c55f3ff | 2006-02-26 06:11:29 +0100 | [diff] [blame] | 668 | } elsif ($node_kind eq $SVN::Node::dir) { |
| 669 | get_ignore(\@new, \@old, $revision, |
| 670 | $branch,$path); |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 671 | } |
| 672 | } else { |
| 673 | die "$revision: unknown action '".$action->[0]."' for $path\n"; |
| 674 | } |
| 675 | } |
| 676 | |
Sasha Khapyorsky | d9e2e12 | 2006-02-01 17:53:31 +0200 | [diff] [blame] | 677 | while(@old) { |
| 678 | my @o1; |
| 679 | if(@old > 55) { |
| 680 | @o1 = splice(@old,0,50); |
| 681 | } else { |
| 682 | @o1 = @old; |
| 683 | @old = (); |
| 684 | } |
Junio C Hamano | 7ae0dc0 | 2006-02-20 14:14:15 -0800 | [diff] [blame] | 685 | my $pid = open my $F, "-|"; |
| 686 | die "$!" unless defined $pid; |
| 687 | if (!$pid) { |
| 688 | exec("git-ls-files", "-z", @o1) or die $!; |
| 689 | } |
Sasha Khapyorsky | d9e2e12 | 2006-02-01 17:53:31 +0200 | [diff] [blame] | 690 | @o1 = (); |
Matthias Urlichs | 0090a61 | 2005-10-11 19:42:27 +0200 | [diff] [blame] | 691 | local $/ = "\0"; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 692 | while(<$F>) { |
| 693 | chomp; |
Sasha Khapyorsky | d9e2e12 | 2006-02-01 17:53:31 +0200 | [diff] [blame] | 694 | push(@o1,$_); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 695 | } |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 696 | close($F); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 697 | |
Sasha Khapyorsky | d9e2e12 | 2006-02-01 17:53:31 +0200 | [diff] [blame] | 698 | while(@o1) { |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 699 | my @o2; |
Sasha Khapyorsky | d9e2e12 | 2006-02-01 17:53:31 +0200 | [diff] [blame] | 700 | if(@o1 > 55) { |
| 701 | @o2 = splice(@o1,0,50); |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 702 | } else { |
Sasha Khapyorsky | d9e2e12 | 2006-02-01 17:53:31 +0200 | [diff] [blame] | 703 | @o2 = @o1; |
| 704 | @o1 = (); |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 705 | } |
| 706 | system("git-update-index","--force-remove","--",@o2); |
| 707 | die "Cannot remove files: $?\n" if $?; |
| 708 | } |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 709 | } |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 710 | while(@new) { |
| 711 | my @n2; |
| 712 | if(@new > 12) { |
| 713 | @n2 = splice(@new,0,10); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 714 | } else { |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 715 | @n2 = @new; |
| 716 | @new = (); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 717 | } |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 718 | system("git-update-index","--add", |
| 719 | (map { ('--cacheinfo', @$_) } @n2)); |
| 720 | die "Cannot add files: $?\n" if $?; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 721 | } |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 722 | |
| 723 | my $pid = open(C,"-|"); |
| 724 | die "Cannot fork: $!" unless defined $pid; |
| 725 | unless($pid) { |
| 726 | exec("git-write-tree"); |
| 727 | die "Cannot exec git-write-tree: $!\n"; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 728 | } |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 729 | chomp(my $tree = <C>); |
| 730 | length($tree) == 40 |
| 731 | or die "Cannot get tree id ($tree): $!\n"; |
| 732 | close(C) |
| 733 | or die "Error running git-write-tree: $?\n"; |
| 734 | print "Tree ID $tree\n" if $opt_v; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 735 | |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 736 | my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n"; |
| 737 | my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n"; |
| 738 | $pid = fork(); |
| 739 | die "Fork: $!\n" unless defined $pid; |
| 740 | unless($pid) { |
| 741 | $pr->writer(); |
| 742 | $pw->reader(); |
| 743 | open(OUT,">&STDOUT"); |
| 744 | dup2($pw->fileno(),0); |
| 745 | dup2($pr->fileno(),1); |
| 746 | $pr->close(); |
| 747 | $pw->close(); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 748 | |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 749 | my @par = (); |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 750 | |
| 751 | # loose detection of merges |
| 752 | # based on the commit msg |
| 753 | foreach my $rx (@mergerx) { |
| 754 | if ($message =~ $rx) { |
| 755 | my $mparent = $1; |
| 756 | if ($mparent eq 'HEAD') { $mparent = $opt_o }; |
| 757 | if ( -e "$git_dir/refs/heads/$mparent") { |
| 758 | $mparent = get_headref($mparent, $git_dir); |
Yaacov Akiba Slama | 109fc2b | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 759 | push (@parents, $mparent); |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 760 | print OUT "Merge parent branch: $mparent\n" if $opt_v; |
| 761 | } |
Junio C Hamano | 2950411 | 2005-10-16 11:55:35 -0700 | [diff] [blame] | 762 | } |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 763 | } |
Yaacov Akiba Slama | 109fc2b | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 764 | my %seen_parents = (); |
| 765 | my @unique_parents = grep { ! $seen_parents{$_} ++ } @parents; |
| 766 | foreach my $bparent (@unique_parents) { |
| 767 | push @par, '-p', $bparent; |
| 768 | print OUT "Merge parent branch: $bparent\n" if $opt_v; |
| 769 | } |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 770 | |
| 771 | exec("env", |
| 772 | "GIT_AUTHOR_NAME=$author_name", |
| 773 | "GIT_AUTHOR_EMAIL=$author_email", |
| 774 | "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)), |
| 775 | "GIT_COMMITTER_NAME=$author_name", |
| 776 | "GIT_COMMITTER_EMAIL=$author_email", |
| 777 | "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)), |
| 778 | "git-commit-tree", $tree,@par); |
| 779 | die "Cannot exec git-commit-tree: $!\n"; |
| 780 | } |
| 781 | $pw->writer(); |
| 782 | $pr->reader(); |
| 783 | |
| 784 | $message =~ s/[\s\n]+\z//; |
Karl Hasselström | 0a48a34 | 2006-02-14 03:43:34 +0100 | [diff] [blame] | 785 | $message = "r$revision: $message" if $opt_r; |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 786 | |
| 787 | print $pw "$message\n" |
| 788 | or die "Error writing to git-commit-tree: $!\n"; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 789 | $pw->close(); |
| 790 | |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 791 | print "Committed change $revision:$branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v; |
| 792 | chomp($cid = <$pr>); |
| 793 | length($cid) == 40 |
| 794 | or die "Cannot get commit id ($cid): $!\n"; |
| 795 | print "Commit ID $cid\n" if $opt_v; |
| 796 | $pr->close(); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 797 | |
Matthias Urlichs | bf267d9 | 2005-10-10 14:51:13 +0200 | [diff] [blame] | 798 | waitpid($pid,0); |
| 799 | die "Error running git-commit-tree: $?\n" if $?; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 800 | } |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 801 | |
Yaacov Akiba Slama | a16db4f | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 802 | if (not defined $cid) { |
| 803 | $cid = $branches{"/"}{"LAST"}; |
| 804 | } |
| 805 | |
Matthias Urlichs | f02b3eb | 2005-10-10 14:42:59 +0200 | [diff] [blame] | 806 | if(not defined $dest) { |
| 807 | print "... no known parent\n" if $opt_v; |
| 808 | } elsif(not $tag) { |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 809 | print "Writing to refs/heads/$dest\n" if $opt_v; |
Junio C Hamano | 2950411 | 2005-10-16 11:55:35 -0700 | [diff] [blame] | 810 | open(C,">$git_dir/refs/heads/$dest") and |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 811 | print C ("$cid\n") and |
| 812 | close(C) |
| 813 | or die "Cannot write branch $dest for update: $!\n"; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 814 | } |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 815 | |
| 816 | if($tag) { |
| 817 | my($in, $out) = ('',''); |
| 818 | $last_rev = "-" if %$changed_paths; |
| 819 | # the tag was 'complex', i.e. did not refer to a "real" revision |
Junio C Hamano | 2950411 | 2005-10-16 11:55:35 -0700 | [diff] [blame] | 820 | |
Matthias Urlichs | f02b3eb | 2005-10-10 14:42:59 +0200 | [diff] [blame] | 821 | $dest =~ tr/_/\./ if $opt_u; |
Yaacov Akiba Slama | a16db4f | 2005-11-02 23:51:57 +0200 | [diff] [blame] | 822 | $branch = $dest; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 823 | |
| 824 | my $pid = open2($in, $out, 'git-mktag'); |
| 825 | print $out ("object $cid\n". |
| 826 | "type commit\n". |
Matthias Urlichs | f02b3eb | 2005-10-10 14:42:59 +0200 | [diff] [blame] | 827 | "tag $dest\n". |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 828 | "tagger $author_name <$author_email>\n") and |
| 829 | close($out) |
Matthias Urlichs | f02b3eb | 2005-10-10 14:42:59 +0200 | [diff] [blame] | 830 | or die "Cannot create tag object $dest: $!\n"; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 831 | |
| 832 | my $tagobj = <$in>; |
| 833 | chomp $tagobj; |
| 834 | |
| 835 | if ( !close($in) or waitpid($pid, 0) != $pid or |
| 836 | $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) { |
Matthias Urlichs | f02b3eb | 2005-10-10 14:42:59 +0200 | [diff] [blame] | 837 | die "Cannot create tag object $dest: $!\n"; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 838 | } |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 839 | |
Matthias Urlichs | f02b3eb | 2005-10-10 14:42:59 +0200 | [diff] [blame] | 840 | open(C,">$git_dir/refs/tags/$dest") and |
| 841 | print C ("$tagobj\n") and |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 842 | close(C) |
Matthias Urlichs | f02b3eb | 2005-10-10 14:42:59 +0200 | [diff] [blame] | 843 | or die "Cannot create tag $branch: $!\n"; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 844 | |
Matthias Urlichs | f02b3eb | 2005-10-10 14:42:59 +0200 | [diff] [blame] | 845 | print "Created tag '$dest' on '$branch'\n" if $opt_v; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 846 | } |
Matthias Urlichs | e7e477d | 2005-10-10 15:28:00 +0200 | [diff] [blame] | 847 | $branches{$branch}{"LAST"} = $cid; |
| 848 | $branches{$branch}{$revision} = $cid; |
| 849 | $last_rev = $cid; |
| 850 | print BRANCHES "$revision $branch $cid\n"; |
| 851 | print "DONE: $revision $dest $cid\n" if $opt_v; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 852 | } |
| 853 | |
Matthias Urlichs | 0349080 | 2005-11-29 08:13:04 +0100 | [diff] [blame] | 854 | sub commit_all { |
| 855 | # Recursive use of the SVN connection does not work |
| 856 | local $svn = $svn2; |
| 857 | |
| 858 | my ($changed_paths, $revision, $author, $date, $message, $pool) = @_; |
Matthias Urlichs | f0daa62 | 2005-10-10 12:41:15 +0200 | [diff] [blame] | 859 | my %p; |
| 860 | while(my($path,$action) = each %$changed_paths) { |
Matthias Urlichs | 0090a61 | 2005-10-11 19:42:27 +0200 | [diff] [blame] | 861 | $p{$path} = [ $action->action,$action->copyfrom_path, $action->copyfrom_rev, $path ]; |
Matthias Urlichs | f0daa62 | 2005-10-10 12:41:15 +0200 | [diff] [blame] | 862 | } |
| 863 | $changed_paths = \%p; |
Matthias Urlichs | f0daa62 | 2005-10-10 12:41:15 +0200 | [diff] [blame] | 864 | |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 865 | my %done; |
| 866 | my @col; |
| 867 | my $pref; |
| 868 | my $branch; |
| 869 | |
| 870 | while(my($path,$action) = each %$changed_paths) { |
| 871 | ($branch,$path) = split_path($revision,$path); |
| 872 | next if not defined $branch; |
| 873 | $done{$branch}{$path} = $action; |
| 874 | } |
| 875 | while(($branch,$changed_paths) = each %done) { |
| 876 | commit($branch, $changed_paths, $revision, $author, $date, $message); |
| 877 | } |
| 878 | } |
| 879 | |
Matthias Urlichs | 0349080 | 2005-11-29 08:13:04 +0100 | [diff] [blame] | 880 | $opt_l = $svn->{'maxrev'} if not defined $opt_l or $opt_l > $svn->{'maxrev'}; |
Martin Langhoff | 988eece | 2005-12-15 19:26:46 +1300 | [diff] [blame] | 881 | |
Anand Kumria | a7cfb4a | 2006-03-26 09:43:46 +1100 | [diff] [blame] | 882 | if ($opt_l < $current_rev) { |
Martin Langhoff | 988eece | 2005-12-15 19:26:46 +1300 | [diff] [blame] | 883 | print "Up to date: no new revisions to fetch!\n" if $opt_v; |
| 884 | unlink("$git_dir/SVN2GIT_HEAD"); |
| 885 | exit; |
| 886 | } |
| 887 | |
Matthias Urlichs | 0349080 | 2005-11-29 08:13:04 +0100 | [diff] [blame] | 888 | print "Fetching from $current_rev to $opt_l ...\n" if $opt_v; |
| 889 | |
| 890 | my $pool=SVN::Pool->new; |
| 891 | $svn->{'svn'}->get_log("/",$current_rev,$opt_l,0,1,1,\&commit_all,$pool); |
| 892 | $pool->clear; |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 893 | |
| 894 | |
| 895 | unlink($git_index); |
| 896 | |
| 897 | if (defined $orig_git_index) { |
| 898 | $ENV{GIT_INDEX_FILE} = $orig_git_index; |
| 899 | } else { |
| 900 | delete $ENV{GIT_INDEX_FILE}; |
| 901 | } |
| 902 | |
| 903 | # Now switch back to the branch we were in before all of this happened |
| 904 | if($orig_branch) { |
Matthias Urlichs | 3ef378a | 2005-10-10 18:45:00 +0200 | [diff] [blame] | 905 | print "DONE\n" if $opt_v and (not defined $opt_l or $opt_l > 0); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 906 | system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master") |
| 907 | if $forward_master; |
| 908 | unless ($opt_i) { |
| 909 | system('git-read-tree', '-m', '-u', 'SVN2GIT_HEAD', 'HEAD'); |
| 910 | die "read-tree failed: $?\n" if $?; |
| 911 | } |
| 912 | } else { |
| 913 | $orig_branch = "master"; |
Matthias Urlichs | 3ef378a | 2005-10-10 18:45:00 +0200 | [diff] [blame] | 914 | print "DONE; creating $orig_branch branch\n" if $opt_v and (not defined $opt_l or $opt_l > 0); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 915 | system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master") |
| 916 | unless -f "$git_dir/refs/heads/master"; |
Pavel Roskin | 8366a10 | 2005-11-16 13:27:28 -0500 | [diff] [blame] | 917 | system('git-update-ref', 'HEAD', "$orig_branch"); |
Matthias Urlichs | eaf718f | 2005-10-10 11:40:43 +0200 | [diff] [blame] | 918 | unless ($opt_i) { |
| 919 | system('git checkout'); |
| 920 | die "checkout failed: $?\n" if $?; |
| 921 | } |
| 922 | } |
| 923 | unlink("$git_dir/SVN2GIT_HEAD"); |
| 924 | close(BRANCHES); |