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; |
Sven Verdoolaege | 79ee456 | 2005-07-04 13:36:59 +0200 | [diff] [blame] | 19 | use File::Spec; |
Martin Langhoff | 7ccd900 | 2006-06-24 23:13:08 +1200 | [diff] [blame] | 20 | use File::Temp qw(tempfile tmpnam); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 21 | use File::Path qw(mkpath); |
| 22 | use File::Basename qw(basename dirname); |
| 23 | use Time::Local; |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 24 | use IO::Socket; |
| 25 | use IO::Pipe; |
Jeff King | e49289d | 2006-05-24 09:58:28 -0400 | [diff] [blame] | 26 | use POSIX qw(strftime dup2 ENOENT); |
H. Peter Anvin | 0d821d4 | 2005-09-06 10:36:01 -0700 | [diff] [blame] | 27 | use IPC::Open2; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 28 | |
| 29 | $SIG{'PIPE'}="IGNORE"; |
| 30 | $ENV{'TZ'}="UTC"; |
| 31 | |
Martin Langhoff | ded9f40 | 2007-01-08 19:43:39 +1300 | [diff] [blame] | 32 | our ($opt_h,$opt_o,$opt_v,$opt_k,$opt_u,$opt_d,$opt_p,$opt_C,$opt_z,$opt_i,$opt_P, $opt_s,$opt_m,$opt_M,$opt_A,$opt_S,$opt_L, $opt_a); |
Andreas Ericsson | ffd97f3 | 2006-01-13 00:38:59 +0100 | [diff] [blame] | 33 | my (%conv_author_name, %conv_author_email); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 34 | |
| 35 | sub usage() { |
| 36 | print STDERR <<END; |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 37 | Usage: ${\basename $0} # fetch/update GIT from CVS |
Andreas Ericsson | ffd97f3 | 2006-01-13 00:38:59 +0100 | [diff] [blame] | 38 | [-o branch-for-HEAD] [-h] [-v] [-d CVSROOT] [-A author-conv-file] |
| 39 | [-p opts-for-cvsps] [-C GIT_repository] [-z fuzz] [-i] [-k] [-u] |
Martin Langhoff | ded9f40 | 2007-01-08 19:43:39 +1300 | [diff] [blame] | 40 | [-s subst] [-a] [-m] [-M regex] [-S regex] [CVS_module] |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 41 | END |
| 42 | exit(1); |
Tommy M. McGuire | 9718a00 | 2005-06-10 01:38:32 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Andreas Ericsson | ffd97f3 | 2006-01-13 00:38:59 +0100 | [diff] [blame] | 45 | sub read_author_info($) { |
| 46 | my ($file) = @_; |
| 47 | my $user; |
| 48 | open my $f, '<', "$file" or die("Failed to open $file: $!\n"); |
| 49 | |
| 50 | while (<$f>) { |
Junio C Hamano | 8cd1621 | 2006-01-15 03:30:30 -0800 | [diff] [blame] | 51 | # Expected format is this: |
Andreas Ericsson | ffd97f3 | 2006-01-13 00:38:59 +0100 | [diff] [blame] | 52 | # exon=Andreas Ericsson <ae@op5.se> |
Junio C Hamano | 8cd1621 | 2006-01-15 03:30:30 -0800 | [diff] [blame] | 53 | if (m/^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/) { |
Andreas Ericsson | ffd97f3 | 2006-01-13 00:38:59 +0100 | [diff] [blame] | 54 | $user = $1; |
Junio C Hamano | 8cd1621 | 2006-01-15 03:30:30 -0800 | [diff] [blame] | 55 | $conv_author_name{$user} = $2; |
| 56 | $conv_author_email{$user} = $3; |
Andreas Ericsson | ffd97f3 | 2006-01-13 00:38:59 +0100 | [diff] [blame] | 57 | } |
Junio C Hamano | 8cd1621 | 2006-01-15 03:30:30 -0800 | [diff] [blame] | 58 | # However, we also read from CVSROOT/users format |
| 59 | # to ease migration. |
| 60 | elsif (/^(\w+):(['"]?)(.+?)\2\s*$/) { |
| 61 | my $mapped; |
| 62 | ($user, $mapped) = ($1, $3); |
| 63 | if ($mapped =~ /^\s*(.*?)\s*<(.*)>\s*$/) { |
| 64 | $conv_author_name{$user} = $1; |
| 65 | $conv_author_email{$user} = $2; |
| 66 | } |
| 67 | elsif ($mapped =~ /^<?(.*)>?$/) { |
| 68 | $conv_author_name{$user} = $user; |
| 69 | $conv_author_email{$user} = $1; |
| 70 | } |
| 71 | } |
| 72 | # NEEDSWORK: Maybe warn on unrecognized lines? |
Andreas Ericsson | ffd97f3 | 2006-01-13 00:38:59 +0100 | [diff] [blame] | 73 | } |
| 74 | close ($f); |
| 75 | } |
| 76 | |
| 77 | sub write_author_info($) { |
| 78 | my ($file) = @_; |
| 79 | open my $f, '>', $file or |
| 80 | die("Failed to open $file for writing: $!"); |
| 81 | |
| 82 | foreach (keys %conv_author_name) { |
Junio C Hamano | 8cd1621 | 2006-01-15 03:30:30 -0800 | [diff] [blame] | 83 | print $f "$_=$conv_author_name{$_} <$conv_author_email{$_}>\n"; |
Andreas Ericsson | ffd97f3 | 2006-01-13 00:38:59 +0100 | [diff] [blame] | 84 | } |
| 85 | close ($f); |
| 86 | } |
| 87 | |
James Bowes | ed35dec | 2007-02-07 17:57:43 -0500 | [diff] [blame] | 88 | # convert getopts specs for use by git-repo-config |
| 89 | sub read_repo_config { |
| 90 | # Split the string between characters, unless there is a ':' |
| 91 | # So "abc:de" becomes ["a", "b", "c:", "d", "e"] |
| 92 | my @opts = split(/ *(?!:)/, shift); |
| 93 | foreach my $o (@opts) { |
| 94 | my $key = $o; |
| 95 | $key =~ s/://g; |
| 96 | my $arg = 'git-repo-config'; |
| 97 | $arg .= ' --bool' if ($o !~ /:$/); |
| 98 | |
| 99 | chomp(my $tmp = `$arg --get cvsimport.$key`); |
| 100 | if ($tmp && !($arg =~ /--bool/ && $tmp eq 'false')) { |
| 101 | no strict 'refs'; |
| 102 | my $opt_name = "opt_" . $key; |
| 103 | if (!$$opt_name) { |
| 104 | $$opt_name = $tmp; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | if (@ARGV == 0) { |
| 109 | chomp(my $module = `git-repo-config --get cvsimport.module`); |
| 110 | push(@ARGV, $module); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | my $opts = "haivmkuo:d:p:C:z:s:M:P:A:S:L:"; |
| 115 | read_repo_config($opts); |
| 116 | getopts($opts) or usage(); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 117 | usage if $opt_h; |
Linus Torvalds | d4f8b39 | 2005-06-07 15:11:28 -0700 | [diff] [blame] | 118 | |
Sven Verdoolaege | f9714a4 | 2005-07-03 11:34:59 +0200 | [diff] [blame] | 119 | @ARGV <= 1 or usage(); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 120 | |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 121 | if ($opt_d) { |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 122 | $ENV{"CVSROOT"} = $opt_d; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 123 | } elsif (-f 'CVS/Root') { |
Sven Verdoolaege | f9714a4 | 2005-07-03 11:34:59 +0200 | [diff] [blame] | 124 | open my $f, '<', 'CVS/Root' or die 'Failed to open CVS/Root'; |
| 125 | $opt_d = <$f>; |
| 126 | chomp $opt_d; |
| 127 | close $f; |
| 128 | $ENV{"CVSROOT"} = $opt_d; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 129 | } elsif ($ENV{"CVSROOT"}) { |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 130 | $opt_d = $ENV{"CVSROOT"}; |
| 131 | } else { |
| 132 | die "CVSROOT needs to be set"; |
| 133 | } |
| 134 | $opt_o ||= "origin"; |
Johannes Schindelin | fbfd60d | 2005-08-17 11:19:20 +0200 | [diff] [blame] | 135 | $opt_s ||= "-"; |
Martin Langhoff | ded9f40 | 2007-01-08 19:43:39 +1300 | [diff] [blame] | 136 | $opt_a ||= 0; |
| 137 | |
Sven Verdoolaege | f9714a4 | 2005-07-03 11:34:59 +0200 | [diff] [blame] | 138 | my $git_tree = $opt_C; |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 139 | $git_tree ||= "."; |
| 140 | |
Sven Verdoolaege | f9714a4 | 2005-07-03 11:34:59 +0200 | [diff] [blame] | 141 | my $cvs_tree; |
| 142 | if ($#ARGV == 0) { |
| 143 | $cvs_tree = $ARGV[0]; |
| 144 | } elsif (-f 'CVS/Repository') { |
| 145 | open my $f, '<', 'CVS/Repository' or |
| 146 | die 'Failed to open CVS/Repository'; |
| 147 | $cvs_tree = <$f>; |
| 148 | chomp $cvs_tree; |
Martin Langhoff | db4b658 | 2005-08-16 22:35:27 +1200 | [diff] [blame] | 149 | close $f; |
Sven Verdoolaege | f9714a4 | 2005-07-03 11:34:59 +0200 | [diff] [blame] | 150 | } else { |
| 151 | usage(); |
| 152 | } |
| 153 | |
Martin Langhoff | db4b658 | 2005-08-16 22:35:27 +1200 | [diff] [blame] | 154 | our @mergerx = (); |
| 155 | if ($opt_m) { |
| 156 | @mergerx = ( qr/\W(?:from|of|merge|merging|merged) (\w+)/i ); |
| 157 | } |
| 158 | if ($opt_M) { |
| 159 | push (@mergerx, qr/$opt_M/); |
| 160 | } |
| 161 | |
Martin Langhoff | 6211988 | 2007-01-08 14:11:23 +1300 | [diff] [blame] | 162 | # Remember UTC of our starting time |
| 163 | # we'll want to avoid importing commits |
| 164 | # that are too recent |
| 165 | our $starttime = time(); |
| 166 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 167 | select(STDERR); $|=1; select(STDOUT); |
| 168 | |
| 169 | |
| 170 | package CVSconn; |
| 171 | # Basic CVS dialog. |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 172 | # We're only interested in connecting and downloading, so ... |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 173 | |
Sven Verdoolaege | 2eb6d82 | 2005-07-04 00:43:26 +0200 | [diff] [blame] | 174 | use File::Spec; |
| 175 | use File::Temp qw(tempfile); |
Matthias Urlichs | f65ae60 | 2005-06-28 19:58:40 +0200 | [diff] [blame] | 176 | use POSIX qw(strftime dup2); |
| 177 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 178 | sub new { |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 179 | my ($what,$repo,$subdir) = @_; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 180 | $what=ref($what) if ref($what); |
| 181 | |
| 182 | my $self = {}; |
| 183 | $self->{'buffer'} = ""; |
| 184 | bless($self,$what); |
| 185 | |
| 186 | $repo =~ s#/+$##; |
| 187 | $self->{'fullrep'} = $repo; |
| 188 | $self->conn(); |
| 189 | |
| 190 | $self->{'subdir'} = $subdir; |
| 191 | $self->{'lines'} = undef; |
| 192 | |
| 193 | return $self; |
Linus Torvalds | d4f8b39 | 2005-06-07 15:11:28 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 196 | sub conn { |
| 197 | my $self = shift; |
| 198 | my $repo = $self->{'fullrep'}; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 199 | if ($repo =~ s/^:pserver(?:([^:]*)):(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?//) { |
| 200 | my ($param,$user,$pass,$serv,$port) = ($1,$2,$3,$4,$5); |
Iñaki Arenaza | 73bcf53 | 2006-11-22 23:26:57 +0100 | [diff] [blame] | 201 | |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 202 | my ($proxyhost,$proxyport); |
| 203 | if ($param && ($param =~ m/proxy=([^;]+)/)) { |
Iñaki Arenaza | 73bcf53 | 2006-11-22 23:26:57 +0100 | [diff] [blame] | 204 | $proxyhost = $1; |
| 205 | # Default proxyport, if not specified, is 8080. |
| 206 | $proxyport = 8080; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 207 | if ($ENV{"CVS_PROXY_PORT"}) { |
Iñaki Arenaza | 73bcf53 | 2006-11-22 23:26:57 +0100 | [diff] [blame] | 208 | $proxyport = $ENV{"CVS_PROXY_PORT"}; |
| 209 | } |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 210 | if ($param =~ m/proxyport=([^;]+)/) { |
Iñaki Arenaza | 73bcf53 | 2006-11-22 23:26:57 +0100 | [diff] [blame] | 211 | $proxyport = $1; |
| 212 | } |
| 213 | } |
| 214 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 215 | $user="anonymous" unless defined $user; |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 216 | my $rr2 = "-"; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 217 | unless ($port) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 218 | $rr2 = ":pserver:$user\@$serv:$repo"; |
| 219 | $port=2401; |
| 220 | } |
| 221 | my $rr = ":pserver:$user\@$serv:$port$repo"; |
Linus Torvalds | d4f8b39 | 2005-06-07 15:11:28 -0700 | [diff] [blame] | 222 | |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 223 | unless ($pass) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 224 | open(H,$ENV{'HOME'}."/.cvspass") and do { |
| 225 | # :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 226 | while (<H>) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 227 | chomp; |
| 228 | s/^\/\d+\s+//; |
| 229 | my ($w,$p) = split(/\s/,$_,2); |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 230 | if ($w eq $rr or $w eq $rr2) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 231 | $pass = $p; |
| 232 | last; |
| 233 | } |
| 234 | } |
| 235 | }; |
| 236 | } |
| 237 | $pass="A" unless $pass; |
Linus Torvalds | d4f8b39 | 2005-06-07 15:11:28 -0700 | [diff] [blame] | 238 | |
Iñaki Arenaza | 73bcf53 | 2006-11-22 23:26:57 +0100 | [diff] [blame] | 239 | my ($s, $rep); |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 240 | if ($proxyhost) { |
Iñaki Arenaza | 73bcf53 | 2006-11-22 23:26:57 +0100 | [diff] [blame] | 241 | |
| 242 | # Use a HTTP Proxy. Only works for HTTP proxies that |
| 243 | # don't require user authentication |
| 244 | # |
| 245 | # See: http://www.ietf.org/rfc/rfc2817.txt |
| 246 | |
| 247 | $s = IO::Socket::INET->new(PeerHost => $proxyhost, PeerPort => $proxyport); |
| 248 | die "Socket to $proxyhost: $!\n" unless defined $s; |
| 249 | $s->write("CONNECT $serv:$port HTTP/1.1\r\nHost: $serv:$port\r\n\r\n") |
| 250 | or die "Write to $proxyhost: $!\n"; |
| 251 | $s->flush(); |
| 252 | |
| 253 | $rep = <$s>; |
| 254 | |
| 255 | # The answer should look like 'HTTP/1.x 2yy ....' |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 256 | if (!($rep =~ m#^HTTP/1\.. 2[0-9][0-9]#)) { |
Iñaki Arenaza | 73bcf53 | 2006-11-22 23:26:57 +0100 | [diff] [blame] | 257 | die "Proxy connect: $rep\n"; |
| 258 | } |
| 259 | # Skip up to the empty line of the proxy server output |
| 260 | # including the response headers. |
| 261 | while ($rep = <$s>) { |
| 262 | last if (!defined $rep || |
| 263 | $rep eq "\n" || |
| 264 | $rep eq "\r\n"); |
| 265 | } |
| 266 | } else { |
| 267 | $s = IO::Socket::INET->new(PeerHost => $serv, PeerPort => $port); |
| 268 | die "Socket to $serv: $!\n" unless defined $s; |
| 269 | } |
| 270 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 271 | $s->write("BEGIN AUTH REQUEST\n$repo\n$user\n$pass\nEND AUTH REQUEST\n") |
| 272 | or die "Write to $serv: $!\n"; |
| 273 | $s->flush(); |
| 274 | |
Iñaki Arenaza | 73bcf53 | 2006-11-22 23:26:57 +0100 | [diff] [blame] | 275 | $rep = <$s>; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 276 | |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 277 | if ($rep ne "I LOVE YOU\n") { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 278 | $rep="<unknown>" unless $rep; |
| 279 | die "AuthReply: $rep\n"; |
| 280 | } |
| 281 | $self->{'socketo'} = $s; |
| 282 | $self->{'socketi'} = $s; |
Sven Verdoolaege | 3415539 | 2005-07-03 13:02:06 +0200 | [diff] [blame] | 283 | } else { # local or ext: Fork off our own cvs server. |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 284 | my $pr = IO::Pipe->new(); |
| 285 | my $pw = IO::Pipe->new(); |
| 286 | my $pid = fork(); |
| 287 | die "Fork: $!\n" unless defined $pid; |
Sven Verdoolaege | 8d0ea31 | 2005-07-03 12:26:51 +0200 | [diff] [blame] | 288 | my $cvs = 'cvs'; |
| 289 | $cvs = $ENV{CVS_SERVER} if exists $ENV{CVS_SERVER}; |
Sven Verdoolaege | 3415539 | 2005-07-03 13:02:06 +0200 | [diff] [blame] | 290 | my $rsh = 'rsh'; |
| 291 | $rsh = $ENV{CVS_RSH} if exists $ENV{CVS_RSH}; |
| 292 | |
| 293 | my @cvs = ($cvs, 'server'); |
| 294 | my ($local, $user, $host); |
| 295 | $local = $repo =~ s/:local://; |
| 296 | if (!$local) { |
| 297 | $repo =~ s/:ext://; |
| 298 | $local = !($repo =~ s/^(?:([^\@:]+)\@)?([^:]+)://); |
| 299 | ($user, $host) = ($1, $2); |
| 300 | } |
| 301 | if (!$local) { |
| 302 | if ($user) { |
| 303 | unshift @cvs, $rsh, '-l', $user, $host; |
| 304 | } else { |
| 305 | unshift @cvs, $rsh, $host; |
| 306 | } |
| 307 | } |
| 308 | |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 309 | unless ($pid) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 310 | $pr->writer(); |
| 311 | $pw->reader(); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 312 | dup2($pw->fileno(),0); |
| 313 | dup2($pr->fileno(),1); |
| 314 | $pr->close(); |
| 315 | $pw->close(); |
Sven Verdoolaege | 3415539 | 2005-07-03 13:02:06 +0200 | [diff] [blame] | 316 | exec(@cvs); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 317 | } |
| 318 | $pw->writer(); |
| 319 | $pr->reader(); |
| 320 | $self->{'socketo'} = $pw; |
| 321 | $self->{'socketi'} = $pr; |
| 322 | } |
| 323 | $self->{'socketo'}->write("Root $repo\n"); |
| 324 | |
| 325 | # Trial and error says that this probably is the minimum set |
iso-8859-1?Q?David_K=E5gedal | b092133 | 2005-08-15 20:18:25 +0200 | [diff] [blame] | 326 | $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E Checked-in Created Updated Merged Removed\n"); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 327 | |
| 328 | $self->{'socketo'}->write("valid-requests\n"); |
| 329 | $self->{'socketo'}->flush(); |
| 330 | |
| 331 | chomp(my $rep=$self->readline()); |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 332 | if ($rep !~ s/^Valid-requests\s*//) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 333 | $rep="<unknown>" unless $rep; |
| 334 | die "Expected Valid-requests from server, but got: $rep\n"; |
| 335 | } |
| 336 | chomp(my $res=$self->readline()); |
| 337 | die "validReply: $res\n" if $res ne "ok"; |
| 338 | |
| 339 | $self->{'socketo'}->write("UseUnchanged\n") if $rep =~ /\bUseUnchanged\b/; |
| 340 | $self->{'repo'} = $repo; |
| 341 | } |
| 342 | |
| 343 | sub readline { |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 344 | my ($self) = @_; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 345 | return $self->{'socketi'}->getline(); |
| 346 | } |
| 347 | |
| 348 | sub _file { |
| 349 | # Request a file with a given revision. |
| 350 | # Trial and error says this is a good way to do it. :-/ |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 351 | my ($self,$fn,$rev) = @_; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 352 | $self->{'socketo'}->write("Argument -N\n") or return undef; |
| 353 | $self->{'socketo'}->write("Argument -P\n") or return undef; |
Martin Langhoff | abe0582 | 2005-08-16 17:39:29 +1200 | [diff] [blame] | 354 | # -kk: Linus' version doesn't use it - defaults to off |
| 355 | if ($opt_k) { |
| 356 | $self->{'socketo'}->write("Argument -kk\n") or return undef; |
| 357 | } |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 358 | $self->{'socketo'}->write("Argument -r\n") or return undef; |
| 359 | $self->{'socketo'}->write("Argument $rev\n") or return undef; |
| 360 | $self->{'socketo'}->write("Argument --\n") or return undef; |
| 361 | $self->{'socketo'}->write("Argument $self->{'subdir'}/$fn\n") or return undef; |
| 362 | $self->{'socketo'}->write("Directory .\n") or return undef; |
| 363 | $self->{'socketo'}->write("$self->{'repo'}\n") or return undef; |
Matthias Urlichs | 4f7c0ca | 2005-06-30 12:19:48 +0200 | [diff] [blame] | 364 | # $self->{'socketo'}->write("Sticky T1.0\n") or return undef; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 365 | $self->{'socketo'}->write("co\n") or return undef; |
| 366 | $self->{'socketo'}->flush() or return undef; |
| 367 | $self->{'lines'} = 0; |
| 368 | return 1; |
| 369 | } |
| 370 | sub _line { |
| 371 | # Read a line from the server. |
| 372 | # ... except that 'line' may be an entire file. ;-) |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 373 | my ($self, $fh) = @_; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 374 | die "Not in lines" unless defined $self->{'lines'}; |
| 375 | |
| 376 | my $line; |
Sven Verdoolaege | 2eb6d82 | 2005-07-04 00:43:26 +0200 | [diff] [blame] | 377 | my $res=0; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 378 | while (defined($line = $self->readline())) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 379 | # M U gnupg-cvs-rep/AUTHORS |
| 380 | # Updated gnupg-cvs-rep/ |
| 381 | # /daten/src/rsync/gnupg-cvs-rep/AUTHORS |
| 382 | # /AUTHORS/1.1///T1.1 |
| 383 | # u=rw,g=rw,o=rw |
| 384 | # 0 |
| 385 | # ok |
| 386 | |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 387 | if ($line =~ s/^(?:Created|Updated) //) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 388 | $line = $self->readline(); # path |
| 389 | $line = $self->readline(); # Entries line |
| 390 | my $mode = $self->readline(); chomp $mode; |
| 391 | $self->{'mode'} = $mode; |
| 392 | defined (my $cnt = $self->readline()) |
| 393 | or die "EOF from server after 'Changed'\n"; |
| 394 | chomp $cnt; |
| 395 | die "Duh: Filesize $cnt" if $cnt !~ /^\d+$/; |
| 396 | $line=""; |
Martin Langhoff | 55cad84 | 2006-05-23 20:08:58 +1200 | [diff] [blame] | 397 | $res = $self->_fetchfile($fh, $cnt); |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 398 | } elsif ($line =~ s/^ //) { |
Sven Verdoolaege | 2eb6d82 | 2005-07-04 00:43:26 +0200 | [diff] [blame] | 399 | print $fh $line; |
| 400 | $res += length($line); |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 401 | } elsif ($line =~ /^M\b/) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 402 | # output, do nothing |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 403 | } elsif ($line =~ /^Mbinary\b/) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 404 | my $cnt; |
| 405 | die "EOF from server after 'Mbinary'" unless defined ($cnt = $self->readline()); |
| 406 | chomp $cnt; |
| 407 | die "Duh: Mbinary $cnt" if $cnt !~ /^\d+$/ or $cnt<1; |
| 408 | $line=""; |
Martin Langhoff | 55cad84 | 2006-05-23 20:08:58 +1200 | [diff] [blame] | 409 | $res += $self->_fetchfile($fh, $cnt); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 410 | } else { |
| 411 | chomp $line; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 412 | if ($line eq "ok") { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 413 | # print STDERR "S: ok (".length($res).")\n"; |
| 414 | return $res; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 415 | } elsif ($line =~ s/^E //) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 416 | # print STDERR "S: $line\n"; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 417 | } elsif ($line =~ /^(Remove-entry|Removed) /i) { |
Matthias Urlichs | 8b8840e | 2005-08-15 11:28:19 +0200 | [diff] [blame] | 418 | $line = $self->readline(); # filename |
| 419 | $line = $self->readline(); # OK |
| 420 | chomp $line; |
| 421 | die "Unknown: $line" if $line ne "ok"; |
| 422 | return -1; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 423 | } else { |
| 424 | die "Unknown: $line\n"; |
| 425 | } |
| 426 | } |
| 427 | } |
Martin Mares | 39ba7d5 | 2006-02-18 21:44:20 +0100 | [diff] [blame] | 428 | return undef; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 429 | } |
| 430 | sub file { |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 431 | my ($self,$fn,$rev) = @_; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 432 | my $res; |
| 433 | |
Sven Verdoolaege | 2eb6d82 | 2005-07-04 00:43:26 +0200 | [diff] [blame] | 434 | my ($fh, $name) = tempfile('gitcvs.XXXXXX', |
| 435 | DIR => File::Spec->tmpdir(), UNLINK => 1); |
| 436 | |
| 437 | $self->_file($fn,$rev) and $res = $self->_line($fh); |
| 438 | |
| 439 | if (!defined $res) { |
Martin Mares | 39ba7d5 | 2006-02-18 21:44:20 +0100 | [diff] [blame] | 440 | print STDERR "Server has gone away while fetching $fn $rev, retrying...\n"; |
| 441 | truncate $fh, 0; |
Sven Verdoolaege | 2eb6d82 | 2005-07-04 00:43:26 +0200 | [diff] [blame] | 442 | $self->conn(); |
Martin Mares | 39ba7d5 | 2006-02-18 21:44:20 +0100 | [diff] [blame] | 443 | $self->_file($fn,$rev) or die "No file command send"; |
Sven Verdoolaege | 2eb6d82 | 2005-07-04 00:43:26 +0200 | [diff] [blame] | 444 | $res = $self->_line($fh); |
Martin Mares | 39ba7d5 | 2006-02-18 21:44:20 +0100 | [diff] [blame] | 445 | die "Retry failed" unless defined $res; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 446 | } |
Sven Verdoolaege | c619ad5 | 2005-07-06 08:37:12 +0200 | [diff] [blame] | 447 | close ($fh); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 448 | |
Sven Verdoolaege | 2eb6d82 | 2005-07-04 00:43:26 +0200 | [diff] [blame] | 449 | return ($name, $res); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 450 | } |
Martin Langhoff | 55cad84 | 2006-05-23 20:08:58 +1200 | [diff] [blame] | 451 | sub _fetchfile { |
| 452 | my ($self, $fh, $cnt) = @_; |
Junio C Hamano | 61efa5e | 2006-05-23 16:30:39 -0700 | [diff] [blame] | 453 | my $res = 0; |
Martin Langhoff | 55cad84 | 2006-05-23 20:08:58 +1200 | [diff] [blame] | 454 | my $bufsize = 1024 * 1024; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 455 | while ($cnt) { |
Martin Langhoff | 55cad84 | 2006-05-23 20:08:58 +1200 | [diff] [blame] | 456 | if ($bufsize > $cnt) { |
| 457 | $bufsize = $cnt; |
| 458 | } |
| 459 | my $buf; |
| 460 | my $num = $self->{'socketi'}->read($buf,$bufsize); |
| 461 | die "Server: Filesize $cnt: $num: $!\n" if not defined $num or $num<=0; |
| 462 | print $fh $buf; |
| 463 | $res += $num; |
| 464 | $cnt -= $num; |
| 465 | } |
| 466 | return $res; |
| 467 | } |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 468 | |
| 469 | |
| 470 | package main; |
| 471 | |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 472 | my $cvs = CVSconn->new($opt_d, $cvs_tree); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 473 | |
| 474 | |
| 475 | sub pdate($) { |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 476 | my ($d) = @_; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 477 | m#(\d{2,4})/(\d\d)/(\d\d)\s(\d\d):(\d\d)(?::(\d\d))?# |
| 478 | or die "Unparseable date: $d\n"; |
| 479 | my $y=$1; $y-=1900 if $y>1900; |
| 480 | return timegm($6||0,$5,$4,$3,$2-1,$y); |
| 481 | } |
| 482 | |
| 483 | sub pmode($) { |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 484 | my ($mode) = @_; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 485 | my $m = 0; |
| 486 | my $mm = 0; |
| 487 | my $um = 0; |
| 488 | for my $x(split(//,$mode)) { |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 489 | if ($x eq ",") { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 490 | $m |= $mm&$um; |
| 491 | $mm = 0; |
| 492 | $um = 0; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 493 | } elsif ($x eq "u") { $um |= 0700; |
| 494 | } elsif ($x eq "g") { $um |= 0070; |
| 495 | } elsif ($x eq "o") { $um |= 0007; |
| 496 | } elsif ($x eq "r") { $mm |= 0444; |
| 497 | } elsif ($x eq "w") { $mm |= 0222; |
| 498 | } elsif ($x eq "x") { $mm |= 0111; |
| 499 | } elsif ($x eq "=") { # do nothing |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 500 | } else { die "Unknown mode: $mode\n"; |
| 501 | } |
| 502 | } |
| 503 | $m |= $mm&$um; |
| 504 | return $m; |
| 505 | } |
| 506 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 507 | sub getwd() { |
| 508 | my $pwd = `pwd`; |
| 509 | chomp $pwd; |
| 510 | return $pwd; |
| 511 | } |
| 512 | |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 513 | sub is_sha1 { |
| 514 | my $s = shift; |
| 515 | return $s =~ /^[a-f0-9]{40}$/; |
Martin Langhoff | db4b658 | 2005-08-16 22:35:27 +1200 | [diff] [blame] | 516 | } |
| 517 | |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 518 | sub get_headref ($$) { |
| 519 | my $name = shift; |
| 520 | my $git_dir = shift; |
| 521 | |
| 522 | my $f = "$git_dir/refs/heads/$name"; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 523 | if (open(my $fh, $f)) { |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 524 | chomp(my $r = <$fh>); |
| 525 | is_sha1($r) or die "Cannot get head id for $name ($r): $!"; |
| 526 | return $r; |
| 527 | } |
| 528 | die "unable to open $f: $!" unless $! == POSIX::ENOENT; |
| 529 | return undef; |
| 530 | } |
Martin Langhoff | db4b658 | 2005-08-16 22:35:27 +1200 | [diff] [blame] | 531 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 532 | -d $git_tree |
| 533 | or mkdir($git_tree,0777) |
| 534 | or die "Could not create $git_tree: $!"; |
| 535 | chdir($git_tree); |
| 536 | |
| 537 | my $last_branch = ""; |
Matthias Urlichs | 4654166 | 2005-06-28 21:08:15 +0200 | [diff] [blame] | 538 | my $orig_branch = ""; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 539 | my %branch_date; |
Junio C Hamano | 8a5f2ea | 2006-03-17 14:08:39 -0800 | [diff] [blame] | 540 | my $tip_at_start = undef; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 541 | |
| 542 | my $git_dir = $ENV{"GIT_DIR"} || ".git"; |
| 543 | $git_dir = getwd()."/".$git_dir unless $git_dir =~ m#^/#; |
| 544 | $ENV{"GIT_DIR"} = $git_dir; |
Sven Verdoolaege | 79ee456 | 2005-07-04 13:36:59 +0200 | [diff] [blame] | 545 | my $orig_git_index; |
| 546 | $orig_git_index = $ENV{GIT_INDEX_FILE} if exists $ENV{GIT_INDEX_FILE}; |
Martin Langhoff | 8f73264 | 2006-06-12 23:50:49 +1200 | [diff] [blame] | 547 | |
| 548 | my %index; # holds filenames of one index per branch |
Johannes Schindelin | 061303f | 2006-06-24 21:42:20 +0200 | [diff] [blame] | 549 | |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 550 | unless (-d $git_dir) { |
Nicolas Pitre | 5c94f87 | 2007-01-12 16:01:46 -0500 | [diff] [blame] | 551 | system("git-init"); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 552 | die "Cannot init the GIT db at $git_tree: $?\n" if $?; |
| 553 | system("git-read-tree"); |
| 554 | die "Cannot init an empty tree: $?\n" if $?; |
| 555 | |
| 556 | $last_branch = $opt_o; |
Matthias Urlichs | 4654166 | 2005-06-28 21:08:15 +0200 | [diff] [blame] | 557 | $orig_branch = ""; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 558 | } else { |
Sven Verdoolaege | 866d131 | 2005-07-03 23:40:48 +0200 | [diff] [blame] | 559 | -f "$git_dir/refs/heads/$opt_o" |
Matthias Urlichs | 4c24e08 | 2005-06-30 22:10:32 +0200 | [diff] [blame] | 560 | or die "Branch '$opt_o' does not exist.\n". |
| 561 | "Either use the correct '-o branch' option,\n". |
| 562 | "or import to a new repository.\n"; |
| 563 | |
Pavel Roskin | 8366a10 | 2005-11-16 13:27:28 -0500 | [diff] [blame] | 564 | open(F, "git-symbolic-ref HEAD |") or |
| 565 | die "Cannot run git-symbolic-ref: $!\n"; |
| 566 | chomp ($last_branch = <F>); |
| 567 | $last_branch = basename($last_branch); |
| 568 | close(F); |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 569 | unless ($last_branch) { |
Matthias Urlichs | 4654166 | 2005-06-28 21:08:15 +0200 | [diff] [blame] | 570 | warn "Cannot read the last branch name: $! -- assuming 'master'\n"; |
| 571 | $last_branch = "master"; |
| 572 | } |
| 573 | $orig_branch = $last_branch; |
Junio C Hamano | 8a5f2ea | 2006-03-17 14:08:39 -0800 | [diff] [blame] | 574 | $tip_at_start = `git-rev-parse --verify HEAD`; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 575 | |
| 576 | # Get the last import timestamps |
Andy Whitcroft | 1f24c58 | 2006-09-20 17:37:04 +0100 | [diff] [blame] | 577 | my $fmt = '($ref, $author) = (%(refname), %(author));'; |
| 578 | open(H, "git-for-each-ref --perl --format='$fmt' refs/heads |") or |
| 579 | die "Cannot run git-for-each-ref: $!\n"; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 580 | while (defined(my $entry = <H>)) { |
Andy Whitcroft | 1f24c58 | 2006-09-20 17:37:04 +0100 | [diff] [blame] | 581 | my ($ref, $author); |
| 582 | eval($entry) || die "cannot eval refs list: $@"; |
| 583 | my ($head) = ($ref =~ m|^refs/heads/(.*)|); |
| 584 | $author =~ /^.*\s(\d+)\s[-+]\d{4}$/; |
| 585 | $branch_date{$head} = $1; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 586 | } |
Andy Whitcroft | 1f24c58 | 2006-09-20 17:37:04 +0100 | [diff] [blame] | 587 | close(H); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | -d $git_dir |
| 591 | or die "Could not create git subdir ($git_dir).\n"; |
| 592 | |
Andreas Ericsson | ffd97f3 | 2006-01-13 00:38:59 +0100 | [diff] [blame] | 593 | # now we read (and possibly save) author-info as well |
| 594 | -f "$git_dir/cvs-authors" and |
| 595 | read_author_info("$git_dir/cvs-authors"); |
| 596 | if ($opt_A) { |
| 597 | read_author_info($opt_A); |
| 598 | write_author_info("$git_dir/cvs-authors"); |
| 599 | } |
| 600 | |
Martin Langhoff | 2f57c69 | 2006-06-11 20:12:20 +1200 | [diff] [blame] | 601 | |
| 602 | # |
| 603 | # run cvsps into a file unless we are getting |
| 604 | # it passed as a file via $opt_P |
| 605 | # |
Martin Langhoff | 4083c2f | 2007-01-08 21:08:46 +1300 | [diff] [blame] | 606 | my $cvspsfile; |
Martin Langhoff | 2f57c69 | 2006-06-11 20:12:20 +1200 | [diff] [blame] | 607 | unless ($opt_P) { |
| 608 | print "Running cvsps...\n" if $opt_v; |
| 609 | my $pid = open(CVSPS,"-|"); |
Martin Langhoff | 4083c2f | 2007-01-08 21:08:46 +1300 | [diff] [blame] | 610 | my $cvspsfh; |
Martin Langhoff | 2f57c69 | 2006-06-11 20:12:20 +1200 | [diff] [blame] | 611 | die "Cannot fork: $!\n" unless defined $pid; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 612 | unless ($pid) { |
Martin Langhoff | 2f57c69 | 2006-06-11 20:12:20 +1200 | [diff] [blame] | 613 | my @opt; |
| 614 | @opt = split(/,/,$opt_p) if defined $opt_p; |
| 615 | unshift @opt, '-z', $opt_z if defined $opt_z; |
| 616 | unshift @opt, '-q' unless defined $opt_v; |
| 617 | unless (defined($opt_p) && $opt_p =~ m/--no-cvs-direct/) { |
| 618 | push @opt, '--cvs-direct'; |
| 619 | } |
| 620 | exec("cvsps","--norc",@opt,"-u","-A",'--root',$opt_d,$cvs_tree); |
| 621 | die "Could not start cvsps: $!\n"; |
Martin Langhoff | df73e9c | 2005-10-11 21:57:04 -0700 | [diff] [blame] | 622 | } |
Martin Langhoff | 4083c2f | 2007-01-08 21:08:46 +1300 | [diff] [blame] | 623 | ($cvspsfh, $cvspsfile) = tempfile('gitXXXXXX', SUFFIX => '.cvsps', |
| 624 | DIR => File::Spec->tmpdir()); |
Martin Langhoff | 2f57c69 | 2006-06-11 20:12:20 +1200 | [diff] [blame] | 625 | while (<CVSPS>) { |
| 626 | print $cvspsfh $_; |
Martin Langhoff | 211dcac | 2005-11-02 13:48:47 +1300 | [diff] [blame] | 627 | } |
Martin Langhoff | 2f57c69 | 2006-06-11 20:12:20 +1200 | [diff] [blame] | 628 | close CVSPS; |
| 629 | close $cvspsfh; |
Martin Langhoff | 4083c2f | 2007-01-08 21:08:46 +1300 | [diff] [blame] | 630 | } else { |
| 631 | $cvspsfile = $opt_P; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 632 | } |
| 633 | |
Martin Langhoff | 4083c2f | 2007-01-08 21:08:46 +1300 | [diff] [blame] | 634 | open(CVS, "<$cvspsfile") or die $!; |
Martin Langhoff | 2f57c69 | 2006-06-11 20:12:20 +1200 | [diff] [blame] | 635 | |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 636 | ## cvsps output: |
| 637 | #--------------------- |
| 638 | #PatchSet 314 |
| 639 | #Date: 1999/09/18 13:03:59 |
| 640 | #Author: wkoch |
| 641 | #Branch: STABLE-BRANCH-1-0 |
| 642 | #Ancestor branch: HEAD |
| 643 | #Tag: (none) |
| 644 | #Log: |
| 645 | # See ChangeLog: Sat Sep 18 13:03:28 CEST 1999 Werner Koch |
| 646 | #Members: |
| 647 | # README:1.57->1.57.2.1 |
| 648 | # VERSION:1.96->1.96.2.1 |
| 649 | # |
| 650 | #--------------------- |
| 651 | |
| 652 | my $state = 0; |
| 653 | |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 654 | sub update_index (\@\@) { |
| 655 | my $old = shift; |
| 656 | my $new = shift; |
Jeff King | 6a1871e | 2006-05-23 03:27:45 -0400 | [diff] [blame] | 657 | open(my $fh, '|-', qw(git-update-index -z --index-info)) |
| 658 | or die "unable to open git-update-index: $!"; |
| 659 | print $fh |
| 660 | (map { "0 0000000000000000000000000000000000000000\t$_\0" } |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 661 | @$old), |
Jeff King | 6a1871e | 2006-05-23 03:27:45 -0400 | [diff] [blame] | 662 | (map { '100' . sprintf('%o', $_->[0]) . " $_->[1]\t$_->[2]\0" } |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 663 | @$new) |
Jeff King | 6a1871e | 2006-05-23 03:27:45 -0400 | [diff] [blame] | 664 | or die "unable to write to git-update-index: $!"; |
| 665 | close $fh |
| 666 | or die "unable to write to git-update-index: $!"; |
| 667 | $? and die "git-update-index reported error: $?"; |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 668 | } |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 669 | |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 670 | sub write_tree () { |
| 671 | open(my $fh, '-|', qw(git-write-tree)) |
| 672 | or die "unable to open git-write-tree: $!"; |
| 673 | chomp(my $tree = <$fh>); |
| 674 | is_sha1($tree) |
| 675 | or die "Cannot get tree id ($tree): $!"; |
| 676 | close($fh) |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 677 | or die "Error running git-write-tree: $?\n"; |
| 678 | print "Tree ID $tree\n" if $opt_v; |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 679 | return $tree; |
| 680 | } |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 681 | |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 682 | my ($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg); |
| 683 | my (@old,@new,@skipped,%ignorebranch); |
Martin Langhoff | 71b0814 | 2006-06-11 20:12:09 +1200 | [diff] [blame] | 684 | |
| 685 | # commits that cvsps cannot place anywhere... |
| 686 | $ignorebranch{'#CVSPS_NO_BRANCH'} = 1; |
| 687 | |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 688 | sub commit { |
Martin Langhoff | c5f448b | 2006-06-28 22:13:23 +1200 | [diff] [blame] | 689 | if ($branch eq $opt_o && !$index{branch} && !get_headref($branch, $git_dir)) { |
| 690 | # looks like an initial commit |
Nicolas Pitre | 5c94f87 | 2007-01-12 16:01:46 -0500 | [diff] [blame] | 691 | # use the index primed by git-init |
Martin Langhoff | c5f448b | 2006-06-28 22:13:23 +1200 | [diff] [blame] | 692 | $ENV{GIT_INDEX_FILE} = '.git/index'; |
| 693 | $index{$branch} = '.git/index'; |
| 694 | } else { |
| 695 | # use an index per branch to speed up |
| 696 | # imports of projects with many branches |
| 697 | unless ($index{$branch}) { |
| 698 | $index{$branch} = tmpnam(); |
| 699 | $ENV{GIT_INDEX_FILE} = $index{$branch}; |
| 700 | if ($ancestor) { |
| 701 | system("git-read-tree", $ancestor); |
| 702 | } else { |
| 703 | system("git-read-tree", $branch); |
| 704 | } |
| 705 | die "read-tree failed: $?\n" if $?; |
| 706 | } |
| 707 | } |
| 708 | $ENV{GIT_INDEX_FILE} = $index{$branch}; |
| 709 | |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 710 | update_index(@old, @new); |
| 711 | @old = @new = (); |
| 712 | my $tree = write_tree(); |
| 713 | my $parent = get_headref($last_branch, $git_dir); |
| 714 | print "Parent ID " . ($parent ? $parent : "(empty)") . "\n" if $opt_v; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 715 | |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 716 | my @commit_args; |
| 717 | push @commit_args, ("-p", $parent) if $parent; |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 718 | |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 719 | # loose detection of merges |
| 720 | # based on the commit msg |
| 721 | foreach my $rx (@mergerx) { |
| 722 | next unless $logmsg =~ $rx && $1; |
| 723 | my $mparent = $1 eq 'HEAD' ? $opt_o : $1; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 724 | if (my $sha1 = get_headref($mparent, $git_dir)) { |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 725 | push @commit_args, '-p', $mparent; |
| 726 | print "Merge parent branch: $mparent\n" if $opt_v; |
Martin Langhoff | db4b658 | 2005-08-16 22:35:27 +1200 | [diff] [blame] | 727 | } |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 728 | } |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 729 | |
| 730 | my $commit_date = strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)); |
Jeff King | 62bf0d9 | 2006-05-23 16:59:44 -0400 | [diff] [blame] | 731 | $ENV{GIT_AUTHOR_NAME} = $author_name; |
| 732 | $ENV{GIT_AUTHOR_EMAIL} = $author_email; |
| 733 | $ENV{GIT_AUTHOR_DATE} = $commit_date; |
| 734 | $ENV{GIT_COMMITTER_NAME} = $author_name; |
| 735 | $ENV{GIT_COMMITTER_EMAIL} = $author_email; |
| 736 | $ENV{GIT_COMMITTER_DATE} = $commit_date; |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 737 | my $pid = open2(my $commit_read, my $commit_write, |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 738 | 'git-commit-tree', $tree, @commit_args); |
Matthias Urlichs | e371046 | 2005-06-30 22:09:42 +0200 | [diff] [blame] | 739 | |
| 740 | # compatibility with git2cvs |
| 741 | substr($logmsg,32767) = "" if length($logmsg) > 32767; |
| 742 | $logmsg =~ s/[\s\n]+\z//; |
| 743 | |
Martin Langhoff | 5179c8a | 2006-01-30 19:12:41 +1300 | [diff] [blame] | 744 | if (@skipped) { |
| 745 | $logmsg .= "\n\n\nSKIPPED:\n\t"; |
| 746 | $logmsg .= join("\n\t", @skipped) . "\n"; |
Martin Langhoff | f396f01 | 2006-05-23 00:45:47 +1200 | [diff] [blame] | 747 | @skipped = (); |
Martin Langhoff | 5179c8a | 2006-01-30 19:12:41 +1300 | [diff] [blame] | 748 | } |
| 749 | |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 750 | print($commit_write "$logmsg\n") && close($commit_write) |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 751 | or die "Error writing to git-commit-tree: $!\n"; |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 752 | |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 753 | print "Committed patch $patchset ($branch $commit_date)\n" if $opt_v; |
| 754 | chomp(my $cid = <$commit_read>); |
| 755 | is_sha1($cid) or die "Cannot get commit id ($cid): $!\n"; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 756 | print "Commit ID $cid\n" if $opt_v; |
Jeff King | e73aefe | 2006-05-23 03:27:46 -0400 | [diff] [blame] | 757 | close($commit_read); |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 758 | |
| 759 | waitpid($pid,0); |
| 760 | die "Error running git-commit-tree: $?\n" if $?; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 761 | |
Johannes Schindelin | 42277bc | 2006-03-30 14:06:15 +0200 | [diff] [blame] | 762 | system("git-update-ref refs/heads/$branch $cid") == 0 |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 763 | or die "Cannot write branch $branch for update: $!\n"; |
| 764 | |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 765 | if ($tag) { |
| 766 | my ($in, $out) = ('',''); |
| 767 | my ($xtag) = $tag; |
H. Peter Anvin | 0d821d4 | 2005-09-06 10:36:01 -0700 | [diff] [blame] | 768 | $xtag =~ s/\s+\*\*.*$//; # Remove stuff like ** INVALID ** and ** FUNKY ** |
| 769 | $xtag =~ tr/_/\./ if ( $opt_u ); |
Joe English | 34c99da | 2006-01-06 12:52:27 -0800 | [diff] [blame] | 770 | $xtag =~ s/[\/]/$opt_s/g; |
H. Peter Anvin | 0d821d4 | 2005-09-06 10:36:01 -0700 | [diff] [blame] | 771 | |
| 772 | my $pid = open2($in, $out, 'git-mktag'); |
| 773 | print $out "object $cid\n". |
| 774 | "type commit\n". |
| 775 | "tag $xtag\n". |
Junio C Hamano | 94c2334 | 2005-09-30 01:48:57 -0700 | [diff] [blame] | 776 | "tagger $author_name <$author_email>\n" |
H. Peter Anvin | 0d821d4 | 2005-09-06 10:36:01 -0700 | [diff] [blame] | 777 | or die "Cannot create tag object $xtag: $!\n"; |
| 778 | close($out) |
| 779 | or die "Cannot create tag object $xtag: $!\n"; |
| 780 | |
| 781 | my $tagobj = <$in>; |
| 782 | chomp $tagobj; |
| 783 | |
| 784 | if ( !close($in) or waitpid($pid, 0) != $pid or |
| 785 | $? != 0 or $tagobj !~ /^[0123456789abcdef]{40}$/ ) { |
| 786 | die "Cannot create tag object $xtag: $!\n"; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | open(C,">$git_dir/refs/tags/$xtag") |
| 791 | or die "Cannot create tag $xtag: $!\n"; |
| 792 | print C "$tagobj\n" |
| 793 | or die "Cannot write tag $xtag: $!\n"; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 794 | close(C) |
H. Peter Anvin | 0d821d4 | 2005-09-06 10:36:01 -0700 | [diff] [blame] | 795 | or die "Cannot write tag $xtag: $!\n"; |
| 796 | |
| 797 | print "Created tag '$xtag' on '$branch'\n" if $opt_v; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 798 | } |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 799 | }; |
| 800 | |
Martin Langhoff | 0691834 | 2006-05-22 23:38:08 +1200 | [diff] [blame] | 801 | my $commitcount = 1; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 802 | while (<CVS>) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 803 | chomp; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 804 | if ($state == 0 and /^-+$/) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 805 | $state = 1; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 806 | } elsif ($state == 0) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 807 | $state = 1; |
| 808 | redo; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 809 | } elsif (($state==0 or $state==1) and s/^PatchSet\s+//) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 810 | $patchset = 0+$_; |
| 811 | $state=2; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 812 | } elsif ($state == 2 and s/^Date:\s+//) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 813 | $date = pdate($_); |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 814 | unless ($date) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 815 | print STDERR "Could not parse date: $_\n"; |
| 816 | $state=0; |
| 817 | next; |
| 818 | } |
| 819 | $state=3; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 820 | } elsif ($state == 3 and s/^Author:\s+//) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 821 | s/\s+$//; |
Junio C Hamano | 94c2334 | 2005-09-30 01:48:57 -0700 | [diff] [blame] | 822 | if (/^(.*?)\s+<(.*)>/) { |
| 823 | ($author_name, $author_email) = ($1, $2); |
Andreas Ericsson | ffd97f3 | 2006-01-13 00:38:59 +0100 | [diff] [blame] | 824 | } elsif ($conv_author_name{$_}) { |
| 825 | $author_name = $conv_author_name{$_}; |
| 826 | $author_email = $conv_author_email{$_}; |
Junio C Hamano | 94c2334 | 2005-09-30 01:48:57 -0700 | [diff] [blame] | 827 | } else { |
| 828 | $author_name = $author_email = $_; |
| 829 | } |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 830 | $state = 4; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 831 | } elsif ($state == 4 and s/^Branch:\s+//) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 832 | s/\s+$//; |
Johannes Schindelin | fbfd60d | 2005-08-17 11:19:20 +0200 | [diff] [blame] | 833 | s/[\/]/$opt_s/g; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 834 | $branch = $_; |
| 835 | $state = 5; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 836 | } elsif ($state == 5 and s/^Ancestor branch:\s+//) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 837 | s/\s+$//; |
| 838 | $ancestor = $_; |
Sven Verdoolaege | 0fa2824 | 2005-06-30 17:23:22 +0200 | [diff] [blame] | 839 | $ancestor = $opt_o if $ancestor eq "HEAD"; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 840 | $state = 6; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 841 | } elsif ($state == 5) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 842 | $ancestor = undef; |
| 843 | $state = 6; |
| 844 | redo; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 845 | } elsif ($state == 6 and s/^Tag:\s+//) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 846 | s/\s+$//; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 847 | if ($_ eq "(none)") { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 848 | $tag = undef; |
| 849 | } else { |
| 850 | $tag = $_; |
| 851 | } |
| 852 | $state = 7; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 853 | } elsif ($state == 7 and /^Log:/) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 854 | $logmsg = ""; |
| 855 | $state = 8; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 856 | } elsif ($state == 8 and /^Members:/) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 857 | $branch = $opt_o if $branch eq "HEAD"; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 858 | if (defined $branch_date{$branch} and $branch_date{$branch} >= $date) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 859 | # skip |
Matthias Urlichs | 9da07f3 | 2005-07-03 19:03:30 +0200 | [diff] [blame] | 860 | print "skip patchset $patchset: $date before $branch_date{$branch}\n" if $opt_v; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 861 | $state = 11; |
| 862 | next; |
| 863 | } |
Martin Langhoff | ded9f40 | 2007-01-08 19:43:39 +1300 | [diff] [blame] | 864 | if (!$opt_a && $starttime - 300 - (defined $opt_z ? $opt_z : 300) <= $date) { |
Martin Langhoff | 6211988 | 2007-01-08 14:11:23 +1300 | [diff] [blame] | 865 | # skip if the commit is too recent |
| 866 | # that the cvsps default fuzz is 300s, we give ourselves another |
| 867 | # 300s just in case -- this also prevents skipping commits |
| 868 | # due to server clock drift |
| 869 | print "skip patchset $patchset: $date too recent\n" if $opt_v; |
| 870 | $state = 11; |
| 871 | next; |
| 872 | } |
Martin Langhoff | 71b0814 | 2006-06-11 20:12:09 +1200 | [diff] [blame] | 873 | if (exists $ignorebranch{$branch}) { |
| 874 | print STDERR "Skipping $branch\n"; |
| 875 | $state = 11; |
| 876 | next; |
| 877 | } |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 878 | if ($ancestor) { |
| 879 | if ($ancestor eq $branch) { |
Martin Langhoff | 71b0814 | 2006-06-11 20:12:09 +1200 | [diff] [blame] | 880 | print STDERR "Branch $branch erroneously stems from itself -- changed ancestor to $opt_o\n"; |
| 881 | $ancestor = $opt_o; |
| 882 | } |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 883 | if (-f "$git_dir/refs/heads/$branch") { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 884 | print STDERR "Branch $branch already exists!\n"; |
| 885 | $state=11; |
| 886 | next; |
| 887 | } |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 888 | unless (open(H,"$git_dir/refs/heads/$ancestor")) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 889 | print STDERR "Branch $ancestor does not exist!\n"; |
Martin Langhoff | 71b0814 | 2006-06-11 20:12:09 +1200 | [diff] [blame] | 890 | $ignorebranch{$branch} = 1; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 891 | $state=11; |
| 892 | next; |
| 893 | } |
| 894 | chomp(my $id = <H>); |
| 895 | close(H); |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 896 | unless (open(H,"> $git_dir/refs/heads/$branch")) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 897 | print STDERR "Could not create branch $branch: $!\n"; |
Martin Langhoff | 71b0814 | 2006-06-11 20:12:09 +1200 | [diff] [blame] | 898 | $ignorebranch{$branch} = 1; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 899 | $state=11; |
| 900 | next; |
| 901 | } |
| 902 | print H "$id\n" |
| 903 | or die "Could not write branch $branch: $!"; |
| 904 | close(H) |
| 905 | or die "Could not write branch $branch: $!"; |
| 906 | } |
Sven Verdoolaege | 46e63ef | 2005-07-04 15:28:36 +0200 | [diff] [blame] | 907 | $last_branch = $branch if $branch ne $last_branch; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 908 | $state = 9; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 909 | } elsif ($state == 8) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 910 | $logmsg .= "$_\n"; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 911 | } elsif ($state == 9 and /^\s+(.+?):(INITIAL|\d+(?:\.\d+)+)->(\d+(?:\.\d+)+)\s*$/) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 912 | # VERSION:1.96->1.96.2.1 |
Matthias Urlichs | 2a3e1a8 | 2005-06-28 19:49:19 +0200 | [diff] [blame] | 913 | my $init = ($2 eq "INITIAL"); |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 914 | my $fn = $1; |
Matthias Urlichs | f65ae60 | 2005-06-28 19:58:40 +0200 | [diff] [blame] | 915 | my $rev = $3; |
| 916 | $fn =~ s#^/+##; |
Martin Langhoff | 5179c8a | 2006-01-30 19:12:41 +1300 | [diff] [blame] | 917 | if ($opt_S && $fn =~ m/$opt_S/) { |
| 918 | print "SKIPPING $fn v $rev\n"; |
| 919 | push(@skipped, $fn); |
| 920 | next; |
| 921 | } |
| 922 | print "Fetching $fn v $rev\n" if $opt_v; |
Sven Verdoolaege | 2eb6d82 | 2005-07-04 00:43:26 +0200 | [diff] [blame] | 923 | my ($tmpname, $size) = $cvs->file($fn,$rev); |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 924 | if ($size == -1) { |
Matthias Urlichs | 8b8840e | 2005-08-15 11:28:19 +0200 | [diff] [blame] | 925 | push(@old,$fn); |
| 926 | print "Drop $fn\n" if $opt_v; |
| 927 | } else { |
| 928 | print "".($init ? "New" : "Update")." $fn: $size bytes\n" if $opt_v; |
Junio C Hamano | dd27478 | 2006-02-20 14:17:28 -0800 | [diff] [blame] | 929 | my $pid = open(my $F, '-|'); |
| 930 | die $! unless defined $pid; |
| 931 | if (!$pid) { |
| 932 | exec("git-hash-object", "-w", $tmpname) |
Matthias Urlichs | 8b8840e | 2005-08-15 11:28:19 +0200 | [diff] [blame] | 933 | or die "Cannot create object: $!\n"; |
Junio C Hamano | dd27478 | 2006-02-20 14:17:28 -0800 | [diff] [blame] | 934 | } |
Matthias Urlichs | 8b8840e | 2005-08-15 11:28:19 +0200 | [diff] [blame] | 935 | my $sha = <$F>; |
| 936 | chomp $sha; |
| 937 | close $F; |
| 938 | my $mode = pmode($cvs->{'mode'}); |
| 939 | push(@new,[$mode, $sha, $fn]); # may be resurrected! |
| 940 | } |
Sven Verdoolaege | 2eb6d82 | 2005-07-04 00:43:26 +0200 | [diff] [blame] | 941 | unlink($tmpname); |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 942 | } elsif ($state == 9 and /^\s+(.+?):\d+(?:\.\d+)+->(\d+(?:\.\d+)+)\(DEAD\)\s*$/) { |
Matthias Urlichs | f65ae60 | 2005-06-28 19:58:40 +0200 | [diff] [blame] | 943 | my $fn = $1; |
| 944 | $fn =~ s#^/+##; |
| 945 | push(@old,$fn); |
Matthias Urlichs | 8b8840e | 2005-08-15 11:28:19 +0200 | [diff] [blame] | 946 | print "Delete $fn\n" if $opt_v; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 947 | } elsif ($state == 9 and /^\s*$/) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 948 | $state = 10; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 949 | } elsif (($state == 9 or $state == 10) and /^-+$/) { |
Linus Torvalds | 4adcea9 | 2006-05-22 19:28:37 -0700 | [diff] [blame] | 950 | $commitcount++; |
| 951 | if ($opt_L && $commitcount > $opt_L) { |
Martin Langhoff | 0691834 | 2006-05-22 23:38:08 +1200 | [diff] [blame] | 952 | last; |
| 953 | } |
Martin Langhoff | c4b16f8 | 2006-05-23 00:45:39 +1200 | [diff] [blame] | 954 | commit(); |
Linus Torvalds | 4adcea9 | 2006-05-22 19:28:37 -0700 | [diff] [blame] | 955 | if (($commitcount & 1023) == 0) { |
| 956 | system("git repack -a -d"); |
| 957 | } |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 958 | $state = 1; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 959 | } elsif ($state == 11 and /^-+$/) { |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 960 | $state = 1; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 961 | } elsif (/^-+$/) { # end of unknown-line processing |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 962 | $state = 1; |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 963 | } elsif ($state != 11) { # ignore stuff when skipping |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 964 | print "* UNKNOWN LINE * $_\n"; |
| 965 | } |
| 966 | } |
Martin Langhoff | c4b16f8 | 2006-05-23 00:45:39 +1200 | [diff] [blame] | 967 | commit() if $branch and $state != 11; |
Matthias Urlichs | a57a949 | 2005-06-28 16:48:40 +0200 | [diff] [blame] | 968 | |
Martin Langhoff | 4083c2f | 2007-01-08 21:08:46 +1300 | [diff] [blame] | 969 | unless ($opt_P) { |
| 970 | unlink($cvspsfile); |
| 971 | } |
| 972 | |
Jim Meyering | efe4abd | 2006-11-15 21:15:44 +0100 | [diff] [blame] | 973 | # The heuristic of repacking every 1024 commits can leave a |
| 974 | # lot of unpacked data. If there is more than 1MB worth of |
| 975 | # not-packed objects, repack once more. |
| 976 | my $line = `git-count-objects`; |
| 977 | if ($line =~ /^(\d+) objects, (\d+) kilobytes$/) { |
| 978 | my ($n_objects, $kb) = ($1, $2); |
| 979 | 1024 < $kb |
| 980 | and system("git repack -a -d"); |
| 981 | } |
| 982 | |
Martin Langhoff | 8f73264 | 2006-06-12 23:50:49 +1200 | [diff] [blame] | 983 | foreach my $git_index (values %index) { |
Martin Langhoff | c5f448b | 2006-06-28 22:13:23 +1200 | [diff] [blame] | 984 | if ($git_index ne '.git/index') { |
| 985 | unlink($git_index); |
| 986 | } |
Martin Langhoff | 8f73264 | 2006-06-12 23:50:49 +1200 | [diff] [blame] | 987 | } |
Sven Verdoolaege | 79ee456 | 2005-07-04 13:36:59 +0200 | [diff] [blame] | 988 | |
Sven Verdoolaege | 210569f | 2005-07-05 13:19:59 +0200 | [diff] [blame] | 989 | if (defined $orig_git_index) { |
| 990 | $ENV{GIT_INDEX_FILE} = $orig_git_index; |
| 991 | } else { |
| 992 | delete $ENV{GIT_INDEX_FILE}; |
| 993 | } |
| 994 | |
Matthias Urlichs | 4654166 | 2005-06-28 21:08:15 +0200 | [diff] [blame] | 995 | # Now switch back to the branch we were in before all of this happened |
Junio C Hamano | 86d11cf | 2006-11-27 14:21:30 -0800 | [diff] [blame] | 996 | if ($orig_branch) { |
Junio C Hamano | 8a5f2ea | 2006-03-17 14:08:39 -0800 | [diff] [blame] | 997 | print "DONE.\n" if $opt_v; |
| 998 | if ($opt_i) { |
| 999 | exit 0; |
| 1000 | } |
| 1001 | my $tip_at_end = `git-rev-parse --verify HEAD`; |
| 1002 | if ($tip_at_start ne $tip_at_end) { |
Junio C Hamano | cb9594e | 2006-03-18 02:05:02 -0800 | [diff] [blame] | 1003 | for ($tip_at_start, $tip_at_end) { chomp; } |
Junio C Hamano | 8a5f2ea | 2006-03-17 14:08:39 -0800 | [diff] [blame] | 1004 | print "Fetched into the current branch.\n" if $opt_v; |
| 1005 | system(qw(git-read-tree -u -m), |
| 1006 | $tip_at_start, $tip_at_end); |
| 1007 | die "Fast-forward update failed: $?\n" if $?; |
| 1008 | } |
| 1009 | else { |
| 1010 | system(qw(git-merge cvsimport HEAD), "refs/heads/$opt_o"); |
| 1011 | die "Could not merge $opt_o into the current branch.\n" if $?; |
| 1012 | } |
Matthias Urlichs | 4654166 | 2005-06-28 21:08:15 +0200 | [diff] [blame] | 1013 | } else { |
| 1014 | $orig_branch = "master"; |
| 1015 | print "DONE; creating $orig_branch branch\n" if $opt_v; |
Matthias Urlichs | a541211 | 2006-03-07 10:08:34 +0100 | [diff] [blame] | 1016 | system("git-update-ref", "refs/heads/master", "refs/heads/$opt_o") |
Matthias Urlichs | 4654166 | 2005-06-28 21:08:15 +0200 | [diff] [blame] | 1017 | unless -f "$git_dir/refs/heads/master"; |
Pavel Roskin | 8366a10 | 2005-11-16 13:27:28 -0500 | [diff] [blame] | 1018 | system('git-update-ref', 'HEAD', "$orig_branch"); |
Sven Verdoolaege | c1c774e | 2005-07-11 16:57:49 +0200 | [diff] [blame] | 1019 | unless ($opt_i) { |
| 1020 | system('git checkout'); |
| 1021 | die "checkout failed: $?\n" if $?; |
| 1022 | } |
Matthias Urlichs | 4654166 | 2005-06-28 21:08:15 +0200 | [diff] [blame] | 1023 | } |