Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
Tommy M. McGuire | 9718a00 | 2005-06-10 01:38:32 -0500 | [diff] [blame] | 2 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 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 aggregate CVS check-ins into related changes. |
| 7 | # Fortunately, "cvsps" does that for us; all we have to do is to parse |
| 8 | # its output. |
| 9 | # |
| 10 | # Checking out the files is done by a single long-running CVS connection |
| 11 | # / server process. |
| 12 | # |
| 13 | # The head revision is on branch "origin" by default. |
| 14 | # You can change that with the '-o' option. |
| 15 | |
| 16 | use strict; |
| 17 | use warnings; |
| 18 | use Getopt::Std; |
| 19 | use File::Path qw(mkpath); |
| 20 | use File::Basename qw(basename dirname); |
| 21 | use Time::Local; |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 22 | use IO::Socket; |
| 23 | use IO::Pipe; |
| 24 | use POSIX qw(strftime dup2); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 25 | |
| 26 | $SIG{'PIPE'}="IGNORE"; |
| 27 | $ENV{'TZ'}="UTC"; |
| 28 | |
Sven Verdoolaege | f9714a4 | 2005-07-03 11:34:59 +0200 | [diff] [blame] | 29 | our($opt_h,$opt_o,$opt_v,$opt_d,$opt_p,$opt_C); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 30 | |
| 31 | sub usage() { |
| 32 | print STDERR <<END; |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 33 | Usage: ${\basename $0} # fetch/update GIT from CVS |
| 34 | [ -o branch-for-HEAD ] [ -h ] [ -v ] [ -d CVSROOT ] |
Sven Verdoolaege | f9714a4 | 2005-07-03 11:34:59 +0200 | [diff] [blame] | 35 | [ -p opts-for-cvsps ] [ -C GIT_repository ] |
| 36 | [ CVS_module ] |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 37 | END |
| 38 | exit(1); |
Tommy M. McGuire | 9718a00 | 2005-06-10 01:38:32 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Sven Verdoolaege | f9714a4 | 2005-07-03 11:34:59 +0200 | [diff] [blame] | 41 | getopts("hqvo:d:p:C:") or usage(); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 42 | usage if $opt_h; |
Linus Torvalds | d4f8b39 | 2005-06-07 15:11:28 -0700 | [diff] [blame] | 43 | |
Sven Verdoolaege | f9714a4 | 2005-07-03 11:34:59 +0200 | [diff] [blame] | 44 | @ARGV <= 1 or usage(); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 45 | |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 46 | if($opt_d) { |
| 47 | $ENV{"CVSROOT"} = $opt_d; |
Sven Verdoolaege | f9714a4 | 2005-07-03 11:34:59 +0200 | [diff] [blame] | 48 | } elsif(-f 'CVS/Root') { |
| 49 | open my $f, '<', 'CVS/Root' or die 'Failed to open CVS/Root'; |
| 50 | $opt_d = <$f>; |
| 51 | chomp $opt_d; |
| 52 | close $f; |
| 53 | $ENV{"CVSROOT"} = $opt_d; |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 54 | } elsif($ENV{"CVSROOT"}) { |
| 55 | $opt_d = $ENV{"CVSROOT"}; |
| 56 | } else { |
| 57 | die "CVSROOT needs to be set"; |
| 58 | } |
| 59 | $opt_o ||= "origin"; |
Sven Verdoolaege | f9714a4 | 2005-07-03 11:34:59 +0200 | [diff] [blame] | 60 | my $git_tree = $opt_C; |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 61 | $git_tree ||= "."; |
| 62 | |
Sven Verdoolaege | f9714a4 | 2005-07-03 11:34:59 +0200 | [diff] [blame] | 63 | my $cvs_tree; |
| 64 | if ($#ARGV == 0) { |
| 65 | $cvs_tree = $ARGV[0]; |
| 66 | } elsif (-f 'CVS/Repository') { |
| 67 | open my $f, '<', 'CVS/Repository' or |
| 68 | die 'Failed to open CVS/Repository'; |
| 69 | $cvs_tree = <$f>; |
| 70 | chomp $cvs_tree; |
| 71 | close $f |
| 72 | } else { |
| 73 | usage(); |
| 74 | } |
| 75 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 76 | select(STDERR); $|=1; select(STDOUT); |
| 77 | |
| 78 | |
| 79 | package CVSconn; |
| 80 | # Basic CVS dialog. |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 81 | # We're only interested in connecting and downloading, so ... |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 82 | |
Matthias Urlichs | f65ae60 | 2005-06-28 19:58:40 +0200 | [diff] [blame] | 83 | use POSIX qw(strftime dup2); |
| 84 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 85 | sub new { |
| 86 | my($what,$repo,$subdir) = @_; |
| 87 | $what=ref($what) if ref($what); |
| 88 | |
| 89 | my $self = {}; |
| 90 | $self->{'buffer'} = ""; |
| 91 | bless($self,$what); |
| 92 | |
| 93 | $repo =~ s#/+$##; |
| 94 | $self->{'fullrep'} = $repo; |
| 95 | $self->conn(); |
| 96 | |
| 97 | $self->{'subdir'} = $subdir; |
| 98 | $self->{'lines'} = undef; |
| 99 | |
| 100 | return $self; |
Linus Torvalds | d4f8b39 | 2005-06-07 15:11:28 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 103 | sub conn { |
| 104 | my $self = shift; |
| 105 | my $repo = $self->{'fullrep'}; |
| 106 | if($repo =~ s/^:pserver:(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) { |
| 107 | my($user,$pass,$serv,$port) = ($1,$2,$3,$4); |
| 108 | $user="anonymous" unless defined $user; |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 109 | my $rr2 = "-"; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 110 | unless($port) { |
| 111 | $rr2 = ":pserver:$user\@$serv:$repo"; |
| 112 | $port=2401; |
| 113 | } |
| 114 | my $rr = ":pserver:$user\@$serv:$port$repo"; |
Linus Torvalds | d4f8b39 | 2005-06-07 15:11:28 -0700 | [diff] [blame] | 115 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 116 | unless($pass) { |
| 117 | open(H,$ENV{'HOME'}."/.cvspass") and do { |
| 118 | # :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z |
| 119 | while(<H>) { |
| 120 | chomp; |
| 121 | s/^\/\d+\s+//; |
| 122 | my ($w,$p) = split(/\s/,$_,2); |
| 123 | if($w eq $rr or $w eq $rr2) { |
| 124 | $pass = $p; |
| 125 | last; |
| 126 | } |
| 127 | } |
| 128 | }; |
| 129 | } |
| 130 | $pass="A" unless $pass; |
Linus Torvalds | d4f8b39 | 2005-06-07 15:11:28 -0700 | [diff] [blame] | 131 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 132 | my $s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port); |
| 133 | die "Socket to $serv: $!\n" unless defined $s; |
| 134 | $s->write("BEGIN AUTH REQUEST\n$repo\n$user\n$pass\nEND AUTH REQUEST\n") |
| 135 | or die "Write to $serv: $!\n"; |
| 136 | $s->flush(); |
| 137 | |
| 138 | my $rep = <$s>; |
| 139 | |
| 140 | if($rep ne "I LOVE YOU\n") { |
| 141 | $rep="<unknown>" unless $rep; |
| 142 | die "AuthReply: $rep\n"; |
| 143 | } |
| 144 | $self->{'socketo'} = $s; |
| 145 | $self->{'socketi'} = $s; |
| 146 | } else { # local: Fork off our own cvs server. |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 147 | my $pr = IO::Pipe->new(); |
| 148 | my $pw = IO::Pipe->new(); |
| 149 | my $pid = fork(); |
| 150 | die "Fork: $!\n" unless defined $pid; |
| 151 | unless($pid) { |
| 152 | $pr->writer(); |
| 153 | $pw->reader(); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 154 | dup2($pw->fileno(),0); |
| 155 | dup2($pr->fileno(),1); |
| 156 | $pr->close(); |
| 157 | $pw->close(); |
| 158 | exec("cvs","server"); |
| 159 | } |
| 160 | $pw->writer(); |
| 161 | $pr->reader(); |
| 162 | $self->{'socketo'} = $pw; |
| 163 | $self->{'socketi'} = $pr; |
| 164 | } |
| 165 | $self->{'socketo'}->write("Root $repo\n"); |
| 166 | |
| 167 | # Trial and error says that this probably is the minimum set |
| 168 | $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E F Checked-in Created Updated Merged Removed\n"); |
| 169 | |
| 170 | $self->{'socketo'}->write("valid-requests\n"); |
| 171 | $self->{'socketo'}->flush(); |
| 172 | |
| 173 | chomp(my $rep=$self->readline()); |
| 174 | if($rep !~ s/^Valid-requests\s*//) { |
| 175 | $rep="<unknown>" unless $rep; |
| 176 | die "Expected Valid-requests from server, but got: $rep\n"; |
| 177 | } |
| 178 | chomp(my $res=$self->readline()); |
| 179 | die "validReply: $res\n" if $res ne "ok"; |
| 180 | |
| 181 | $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/; |
| 182 | $self->{'repo'} = $repo; |
| 183 | } |
| 184 | |
| 185 | sub readline { |
| 186 | my($self) = @_; |
| 187 | return $self->{'socketi'}->getline(); |
| 188 | } |
| 189 | |
| 190 | sub _file { |
| 191 | # Request a file with a given revision. |
| 192 | # Trial and error says this is a good way to do it. :-/ |
| 193 | my($self,$fn,$rev) = @_; |
| 194 | $self->{'socketo'}->write("Argument -N\n") or return undef; |
| 195 | $self->{'socketo'}->write("Argument -P\n") or return undef; |
| 196 | # $self->{'socketo'}->write("Argument -ko\n") or return undef; |
| 197 | # -ko: Linus' version doesn't use it |
| 198 | $self->{'socketo'}->write("Argument -r\n") or return undef; |
| 199 | $self->{'socketo'}->write("Argument $rev\n") or return undef; |
| 200 | $self->{'socketo'}->write("Argument --\n") or return undef; |
| 201 | $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef; |
| 202 | $self->{'socketo'}->write("Directory .\n") or return undef; |
| 203 | $self->{'socketo'}->write("$self->{'repo'}\n") or return undef; |
Matthias Urlichs | 4f7c0ca | 2005-06-30 12:19:48 +0200 | [diff] [blame] | 204 | # $self->{'socketo'}->write("Sticky T1.0\n") or return undef; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 205 | $self->{'socketo'}->write("co\n") or return undef; |
| 206 | $self->{'socketo'}->flush() or return undef; |
| 207 | $self->{'lines'} = 0; |
| 208 | return 1; |
| 209 | } |
| 210 | sub _line { |
| 211 | # Read a line from the server. |
| 212 | # ... except that 'line' may be an entire file. ;-) |
| 213 | my($self) = @_; |
| 214 | die "Not in lines" unless defined $self->{'lines'}; |
| 215 | |
| 216 | my $line; |
| 217 | my $res=""; |
| 218 | while(defined($line = $self->readline())) { |
| 219 | # M U gnupg-cvs-rep/AUTHORS |
| 220 | # Updated gnupg-cvs-rep/ |
| 221 | # /daten/src/rsync/gnupg-cvs-rep/AUTHORS |
| 222 | # /AUTHORS/1.1///T1.1 |
| 223 | # u=rw,g=rw,o=rw |
| 224 | # 0 |
| 225 | # ok |
| 226 | |
| 227 | if($line =~ s/^(?:Created|Updated) //) { |
| 228 | $line = $self->readline(); # path |
| 229 | $line = $self->readline(); # Entries line |
| 230 | my $mode = $self->readline(); chomp $mode; |
| 231 | $self->{'mode'} = $mode; |
| 232 | defined (my $cnt = $self->readline()) |
| 233 | or die "EOF from server after 'Changed'\n"; |
| 234 | chomp $cnt; |
| 235 | die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/; |
| 236 | $line=""; |
| 237 | $res=""; |
| 238 | while($cnt) { |
| 239 | my $buf; |
| 240 | my $num = $self->{'socketi'}->read($buf,$cnt); |
| 241 | die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0; |
| 242 | $res .= $buf; |
| 243 | $cnt -= $num; |
| 244 | } |
| 245 | } elsif($line =~ s/^ //) { |
| 246 | $res .= $line; |
| 247 | } elsif($line =~ /^M\b/) { |
| 248 | # output, do nothing |
| 249 | } elsif($line =~ /^Mbinary\b/) { |
| 250 | my $cnt; |
| 251 | die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline()); |
| 252 | chomp $cnt; |
| 253 | die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1; |
| 254 | $line=""; |
| 255 | while($cnt) { |
| 256 | my $buf; |
| 257 | my $num = $self->{'socketi'}->read($buf,$cnt); |
| 258 | die "S: Mbinary $cnt: $num: $!\n" if not defined $num or $num<=0; |
| 259 | $res .= $buf; |
| 260 | $cnt -= $num; |
| 261 | } |
| 262 | } else { |
| 263 | chomp $line; |
| 264 | if($line eq "ok") { |
| 265 | # print STDERR "S: ok (".length($res).")\n"; |
| 266 | return $res; |
| 267 | } elsif($line =~ s/^E //) { |
| 268 | # print STDERR "S: $line\n"; |
| 269 | } else { |
| 270 | die "Unknown: $line\n"; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | sub file { |
| 276 | my($self,$fn,$rev) = @_; |
| 277 | my $res; |
| 278 | |
| 279 | if ($self->_file($fn,$rev)) { |
| 280 | $res = $self->_line(); |
| 281 | return $res if defined $res; |
| 282 | } |
| 283 | |
| 284 | # retry |
| 285 | $self->conn(); |
| 286 | $self->_file($fn,$rev) |
| 287 | or die "No file command send\n"; |
| 288 | $res = $self->_line(); |
| 289 | die "No input: $fn $rev\n" unless defined $res; |
| 290 | return $res; |
| 291 | } |
| 292 | |
| 293 | |
| 294 | package main; |
| 295 | |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 296 | my $cvs = CVSconn->new($opt_d, $cvs_tree); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 297 | |
| 298 | |
| 299 | sub pdate($) { |
| 300 | my($d) = @_; |
| 301 | m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?# |
| 302 | or die "Unparseable date: $d\n"; |
| 303 | my $y=$1; $y-=1900 if $y>1900; |
| 304 | return timegm($6||0,$5,$4,$3,$2-1,$y); |
| 305 | } |
| 306 | |
| 307 | sub pmode($) { |
| 308 | my($mode) = @_; |
| 309 | my $m = 0; |
| 310 | my $mm = 0; |
| 311 | my $um = 0; |
| 312 | for my $x(split(//,$mode)) { |
| 313 | if($x eq ",") { |
| 314 | $m |= $mm&$um; |
| 315 | $mm = 0; |
| 316 | $um = 0; |
| 317 | } elsif($x eq "u") { $um |= 0700; |
| 318 | } elsif($x eq "g") { $um |= 0070; |
| 319 | } elsif($x eq "o") { $um |= 0007; |
| 320 | } elsif($x eq "r") { $mm |= 0444; |
| 321 | } elsif($x eq "w") { $mm |= 0222; |
| 322 | } elsif($x eq "x") { $mm |= 0111; |
| 323 | } elsif($x eq "=") { # do nothing |
| 324 | } else { die "Unknown mode: $mode\n"; |
| 325 | } |
| 326 | } |
| 327 | $m |= $mm&$um; |
| 328 | return $m; |
| 329 | } |
| 330 | |
| 331 | my $tmpcv = "/var/cache/cvs"; |
| 332 | |
| 333 | sub getwd() { |
| 334 | my $pwd = `pwd`; |
| 335 | chomp $pwd; |
| 336 | return $pwd; |
| 337 | } |
| 338 | |
| 339 | -d $git_tree |
| 340 | or mkdir($git_tree,0777) |
| 341 | or die "Could not create $git_tree: $!"; |
| 342 | chdir($git_tree); |
| 343 | |
| 344 | my $last_branch = ""; |
Matthias Urlichs | 4654166 | 2005-06-28 21:08:15 +0200 | [diff] [blame] | 345 | my $orig_branch = ""; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 346 | my %branch_date; |
| 347 | |
| 348 | my $git_dir = $ENV{"GIT_DIR"} || ".git"; |
| 349 | $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#; |
| 350 | $ENV{"GIT_DIR"} = $git_dir; |
| 351 | unless(-d $git_dir) { |
| 352 | system("git-init-db"); |
| 353 | die "Cannot init the GIT db at $git_tree: $?\n" if $?; |
| 354 | system("git-read-tree"); |
| 355 | die "Cannot init an empty tree: $?\n" if $?; |
| 356 | |
| 357 | $last_branch = $opt_o; |
Matthias Urlichs | 4654166 | 2005-06-28 21:08:15 +0200 | [diff] [blame] | 358 | $orig_branch = ""; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 359 | } else { |
Matthias Urlichs | 4c24e08 | 2005-06-30 22:10:32 +0200 | [diff] [blame] | 360 | -f "$git_dir/refs/head/$opt_o" |
| 361 | or die "Branch '$opt_o' does not exist.\n". |
| 362 | "Either use the correct '-o branch' option,\n". |
| 363 | "or import to a new repository.\n"; |
| 364 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 365 | $last_branch = basename(readlink("$git_dir/HEAD")); |
Matthias Urlichs | 4654166 | 2005-06-28 21:08:15 +0200 | [diff] [blame] | 366 | unless($last_branch) { |
| 367 | warn "Cannot read the last branch name: $! -- assuming 'master'\n"; |
| 368 | $last_branch = "master"; |
| 369 | } |
| 370 | $orig_branch = $last_branch; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 371 | |
| 372 | # Get the last import timestamps |
| 373 | opendir(D,"$git_dir/refs/heads"); |
| 374 | while(defined(my $head = readdir(D))) { |
| 375 | next if $head =~ /^\./; |
| 376 | open(F,"$git_dir/refs/heads/$head") |
| 377 | or die "Bad head branch: $head: $!\n"; |
| 378 | chomp(my $ftag = <F>); |
| 379 | close(F); |
| 380 | open(F,"git-cat-file commit $ftag |"); |
| 381 | while(<F>) { |
| 382 | next unless /^author\s.*\s(\d+)\s[-+]\d{4}$/; |
| 383 | $branch_date{$head} = $1; |
| 384 | last; |
| 385 | } |
| 386 | close(F); |
| 387 | } |
| 388 | closedir(D); |
| 389 | } |
| 390 | |
| 391 | -d $git_dir |
| 392 | or die "Could not create git subdir ($git_dir).\n"; |
| 393 | |
| 394 | my $pid = open(CVS,"-|"); |
| 395 | die "Cannot fork: $!\n" unless defined $pid; |
| 396 | unless($pid) { |
Matthias Urlichs | 2be4fcc | 2005-06-30 22:54:01 +0200 | [diff] [blame] | 397 | my @opt; |
| 398 | @opt = split(/,/,$opt_p) if defined $opt_p; |
Sven Verdoolaege | f9714a4 | 2005-07-03 11:34:59 +0200 | [diff] [blame] | 399 | exec("cvsps",@opt,"-x","-A","--cvs-direct",'--root',$opt_d,$cvs_tree); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 400 | die "Could not start cvsps: $!\n"; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | ## cvsps output: |
| 405 | #--------------------- |
| 406 | #PatchSet 314 |
| 407 | #Date: 1999/09/18 13:03:59 |
| 408 | #Author: wkoch |
| 409 | #Branch: STABLE-BRANCH-1-0 |
| 410 | #Ancestor branch: HEAD |
| 411 | #Tag: (none) |
| 412 | #Log: |
| 413 | # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch |
| 414 | #Members: |
| 415 | # README:1.57->1.57.2.1 |
| 416 | # VERSION:1.96->1.96.2.1 |
| 417 | # |
| 418 | #--------------------- |
| 419 | |
| 420 | my $state = 0; |
| 421 | |
| 422 | my($patchset,$date,$author,$branch,$ancestor,$tag,$logmsg); |
| 423 | my(@old,@new); |
| 424 | my $commit = sub { |
| 425 | my $pid; |
Matthias Urlichs | 4abdecb | 2005-06-30 11:55:57 +0200 | [diff] [blame] | 426 | while(@old) { |
| 427 | my @o2; |
| 428 | if(@old > 55) { |
| 429 | @o2 = splice(@old,0,50); |
| 430 | } else { |
| 431 | @o2 = @old; |
| 432 | @old = (); |
| 433 | } |
| 434 | system("git-update-cache","--force-remove","--",@o2); |
| 435 | die "Cannot remove files: $?\n" if $?; |
| 436 | } |
| 437 | while(@new) { |
| 438 | my @n2; |
| 439 | if(@new > 55) { |
| 440 | @n2 = splice(@new,0,50); |
| 441 | } else { |
| 442 | @n2 = @new; |
| 443 | @new = (); |
| 444 | } |
| 445 | system("git-update-cache","--add","--",@n2); |
| 446 | die "Cannot add files: $?\n" if $?; |
| 447 | } |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 448 | |
| 449 | $pid = open(C,"-|"); |
| 450 | die "Cannot fork: $!" unless defined $pid; |
| 451 | unless($pid) { |
| 452 | exec("git-write-tree"); |
| 453 | die "Cannot exec git-write-tree: $!\n"; |
| 454 | } |
| 455 | chomp(my $tree = <C>); |
| 456 | length($tree) == 40 |
| 457 | or die "Cannot get tree id ($tree): $!\n"; |
| 458 | close(C) |
| 459 | or die "Error running git-write-tree: $?\n"; |
| 460 | print "Tree ID $tree\n" if $opt_v; |
| 461 | |
| 462 | my $parent = ""; |
| 463 | if(open(C,"$git_dir/refs/heads/$last_branch")) { |
| 464 | chomp($parent = <C>); |
| 465 | close(C); |
| 466 | length($parent) == 40 |
| 467 | or die "Cannot get parent id ($parent): $!\n"; |
| 468 | print "Parent ID $parent\n" if $opt_v; |
| 469 | } |
| 470 | |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 471 | my $pr = IO::Pipe->new(); |
| 472 | my $pw = IO::Pipe->new(); |
| 473 | $pid = fork(); |
| 474 | die "Fork: $!\n" unless defined $pid; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 475 | unless($pid) { |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 476 | $pr->writer(); |
| 477 | $pw->reader(); |
| 478 | dup2($pw->fileno(),0); |
| 479 | dup2($pr->fileno(),1); |
| 480 | $pr->close(); |
| 481 | $pw->close(); |
| 482 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 483 | my @par = (); |
| 484 | @par = ("-p",$parent) if $parent; |
| 485 | exec("env", |
| 486 | "GIT_AUTHOR_NAME=$author", |
| 487 | "GIT_AUTHOR_EMAIL=$author", |
| 488 | "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)), |
| 489 | "GIT_COMMITTER_NAME=$author", |
| 490 | "GIT_COMMITTER_EMAIL=$author", |
| 491 | "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)), |
| 492 | "git-commit-tree", $tree,@par); |
| 493 | die "Cannot exec git-commit-tree: $!\n"; |
| 494 | } |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 495 | $pw->writer(); |
| 496 | $pr->reader(); |
Matthias Urlichs | e371046 | 2005-06-30 22:09:42 +0200 | [diff] [blame] | 497 | |
| 498 | # compatibility with git2cvs |
| 499 | substr($logmsg,32767) = "" if length($logmsg) > 32767; |
| 500 | $logmsg =~ s/[\s\n]+\z//; |
| 501 | |
| 502 | print $pw "$logmsg\n" |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 503 | or die "Error writing to git-commit-tree: $!\n"; |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 504 | $pw->close(); |
| 505 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 506 | print "Committed patch $patchset ($branch)\n" if $opt_v; |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 507 | chomp(my $cid = <$pr>); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 508 | length($cid) == 40 |
| 509 | or die "Cannot get commit id ($cid): $!\n"; |
| 510 | print "Commit ID $cid\n" if $opt_v; |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 511 | $pr->close(); |
| 512 | |
| 513 | waitpid($pid,0); |
| 514 | die "Error running git-commit-tree: $?\n" if $?; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 515 | |
| 516 | open(C,">$git_dir/refs/heads/$branch") |
| 517 | or die "Cannot open branch $branch for update: $!\n"; |
| 518 | print C "$cid\n" |
| 519 | or die "Cannot write branch $branch for update: $!\n"; |
| 520 | close(C) |
| 521 | or die "Cannot write branch $branch for update: $!\n"; |
| 522 | |
| 523 | if($tag) { |
| 524 | open(C,">$git_dir/refs/tags/$tag") |
| 525 | or die "Cannot create tag $tag: $!\n"; |
| 526 | print C "$cid\n" |
| 527 | or die "Cannot write tag $branch: $!\n"; |
| 528 | close(C) |
| 529 | or die "Cannot write tag $branch: $!\n"; |
| 530 | print "Created tag '$tag' on '$branch'\n" if $opt_v; |
| 531 | } |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 532 | }; |
| 533 | |
| 534 | while(<CVS>) { |
| 535 | chomp; |
| 536 | if($state == 0 and /^-+$/) { |
| 537 | $state = 1; |
| 538 | } elsif($state == 0) { |
| 539 | $state = 1; |
| 540 | redo; |
| 541 | } elsif(($state==0 or $state==1) and s/^PatchSet\s+//) { |
| 542 | $patchset = 0+$_; |
| 543 | $state=2; |
| 544 | } elsif($state == 2 and s/^Date:\s+//) { |
| 545 | $date = pdate($_); |
| 546 | unless($date) { |
| 547 | print STDERR "Could not parse date: $_\n"; |
| 548 | $state=0; |
| 549 | next; |
| 550 | } |
| 551 | $state=3; |
| 552 | } elsif($state == 3 and s/^Author:\s+//) { |
| 553 | s/\s+$//; |
| 554 | $author = $_; |
| 555 | $state = 4; |
| 556 | } elsif($state == 4 and s/^Branch:\s+//) { |
| 557 | s/\s+$//; |
| 558 | $branch = $_; |
| 559 | $state = 5; |
| 560 | } elsif($state == 5 and s/^Ancestor branch:\s+//) { |
| 561 | s/\s+$//; |
| 562 | $ancestor = $_; |
Sven Verdoolaege | 0fa2824 | 2005-06-30 17:23:22 +0200 | [diff] [blame] | 563 | $ancestor = $opt_o if $ancestor eq "HEAD"; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 564 | $state = 6; |
| 565 | } elsif($state == 5) { |
| 566 | $ancestor = undef; |
| 567 | $state = 6; |
| 568 | redo; |
| 569 | } elsif($state == 6 and s/^Tag:\s+//) { |
| 570 | s/\s+$//; |
| 571 | if($_ eq "(none)") { |
| 572 | $tag = undef; |
| 573 | } else { |
| 574 | $tag = $_; |
| 575 | } |
| 576 | $state = 7; |
| 577 | } elsif($state == 7 and /^Log:/) { |
| 578 | $logmsg = ""; |
| 579 | $state = 8; |
| 580 | } elsif($state == 8 and /^Members:/) { |
| 581 | $branch = $opt_o if $branch eq "HEAD"; |
| 582 | if(defined $branch_date{$branch} and $branch_date{$branch} >= $date) { |
| 583 | # skip |
| 584 | print "skip patchset $patchset: $date before $branch_date{$branch}\n"; |
| 585 | $state = 11; |
| 586 | next; |
| 587 | } |
| 588 | if($ancestor) { |
| 589 | if(-f "$git_dir/refs/heads/$branch") { |
| 590 | print STDERR "Branch $branch already exists!\n"; |
| 591 | $state=11; |
| 592 | next; |
| 593 | } |
| 594 | unless(open(H,"$git_dir/refs/heads/$ancestor")) { |
| 595 | print STDERR "Branch $ancestor does not exist!\n"; |
| 596 | $state=11; |
| 597 | next; |
| 598 | } |
| 599 | chomp(my $id = <H>); |
| 600 | close(H); |
| 601 | unless(open(H,"> $git_dir/refs/heads/$branch")) { |
| 602 | print STDERR "Could not create branch $branch: $!\n"; |
| 603 | $state=11; |
| 604 | next; |
| 605 | } |
| 606 | print H "$id\n" |
| 607 | or die "Could not write branch $branch: $!"; |
| 608 | close(H) |
| 609 | or die "Could not write branch $branch: $!"; |
| 610 | } |
| 611 | if(($ancestor || $branch) ne $last_branch) { |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 612 | print "Switching from $last_branch to $branch\n" if $opt_v; |
Matthias Urlichs | 4654166 | 2005-06-28 21:08:15 +0200 | [diff] [blame] | 613 | system("git-read-tree","-m","-u","$last_branch","$branch"); |
| 614 | die "read-tree failed: $?\n" if $?; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 615 | } |
| 616 | if($branch ne $last_branch) { |
| 617 | unlink("$git_dir/HEAD"); |
| 618 | symlink("refs/heads/$branch","$git_dir/HEAD"); |
| 619 | $last_branch = $branch; |
| 620 | } |
| 621 | $state = 9; |
| 622 | } elsif($state == 8) { |
| 623 | $logmsg .= "$_\n"; |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 624 | } elsif($state == 9 and /^\s+(\S+):(INITIAL|\d(?:\.\d+)+)->(\d(?:\.\d+)+)\s*$/) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 625 | # VERSION:1.96->1.96.2.1 |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 626 | my $init = ($2 eq "INITIAL"); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 627 | my $fn = $1; |
Matthias Urlichs | f65ae60 | 2005-06-28 19:58:40 +0200 | [diff] [blame] | 628 | my $rev = $3; |
| 629 | $fn =~ s#^/+##; |
| 630 | my $data = $cvs->file($fn,$rev); |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 631 | print "".($init ? "New" : "Update")." $fn: ".length($data)." bytes.\n"; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 632 | mkpath(dirname($fn),$opt_v); |
| 633 | open(F,"> ./$fn") |
| 634 | or die "Cannot create '$fn': $!\n"; |
| 635 | print F $data |
| 636 | or die "Cannot write to '$fn': $!\n"; |
| 637 | close(F) |
| 638 | or die "Cannot write to '$fn': $!\n"; |
| 639 | chmod(pmode($cvs->{'mode'}), $fn); |
| 640 | push(@new,$fn); # may be resurrected! |
| 641 | } elsif($state == 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(DEAD\)\s*$/) { |
Matthias Urlichs | f65ae60 | 2005-06-28 19:58:40 +0200 | [diff] [blame] | 642 | my $fn = $1; |
| 643 | $fn =~ s#^/+##; |
| 644 | push(@old,$fn); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 645 | } elsif($state == 9 and /^\s*$/) { |
| 646 | $state = 10; |
| 647 | } elsif(($state == 9 or $state == 10) and /^-+$/) { |
| 648 | &$commit(); |
| 649 | $state = 1; |
| 650 | } elsif($state == 11 and /^-+$/) { |
| 651 | $state = 1; |
| 652 | } elsif(/^-+$/) { # end of unknown-line processing |
| 653 | $state = 1; |
| 654 | } elsif($state != 11) { # ignore stuff when skipping |
| 655 | print "* UNKNOWN LINE * $_\n"; |
| 656 | } |
| 657 | } |
| 658 | &$commit() if $branch and $state != 11; |
| 659 | |
Matthias Urlichs | 4654166 | 2005-06-28 21:08:15 +0200 | [diff] [blame] | 660 | # Now switch back to the branch we were in before all of this happened |
| 661 | if($orig_branch) { |
| 662 | print "DONE; switching back to $orig_branch\n" if $opt_v; |
| 663 | } else { |
| 664 | $orig_branch = "master"; |
| 665 | print "DONE; creating $orig_branch branch\n" if $opt_v; |
| 666 | system("cp","$git_dir/refs/heads/$opt_o","$git_dir/refs/heads/master") |
| 667 | unless -f "$git_dir/refs/heads/master"; |
| 668 | } |
| 669 | |
| 670 | system("git-read-tree","-m","-u","$last_branch","$orig_branch"); |
| 671 | die "read-tree failed: $?\n" if $?; |
| 672 | |
| 673 | unlink("$git_dir/HEAD"); |
| 674 | symlink("refs/heads/$orig_branch","$git_dir/HEAD"); |
| 675 | |