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