@matheusbsbs / matheus-bagliano / commits / 9b981fc659
Fork 0

git-svn: add Git::SVN module (to avoid global variables)

This should make it easier to improve multi-fetch and --follow-parent by avoiding global variables. Signed-off-by: Eric Wong <normalperson@yhbt.net>

Eric Wong committed Jan 11, 2007 at 12:14 UTC 9b981fc6596e369e82bc744e12afd5e54f2514f0
1 file changed +485
git-svn.perl
+485
@@ -1907,6 +1907,491 @@ sub show_commit_normal {
1907 }
1908 }
1909
1910 +package Git::SVN;
1911 +use strict;
1912 +use warnings;
1913 +use vars qw/$default/;
1914 +use Carp qw/croak/;
1915 +use File::Path qw/mkpath/;
1916 +use IPC::Open3;
1917 +
1918 +# properties that we do not log:
1919 +my %SKIP_PROP;
1920 +BEGIN {
1921 + %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
1922 + svn:special svn:executable
1923 + svn:entry:committed-rev
1924 + svn:entry:last-author
1925 + svn:entry:uuid
1926 + svn:entry:committed-date/;
1927 +}
1928 +
1929 +sub init {
1930 + my ($class, $id, $url) = @_;
1931 + my $self = _new($class, $id);
1932 + mkpath(["$self->{dir}/info"]);
1933 + if (defined $url) {
1934 + $url =~ s!/+$!!; # strip trailing slash
1935 + s_to_file($url, "$self->{dir}/info/url");
1936 + }
1937 + $self->{url} = $url;
1938 + open my $fh, '>>', $self->{db_path} or croak $!;
1939 + close $fh or croak $!;
1940 + $self;
1941 +}
1942 +
1943 +sub new {
1944 + my ($class, $id) = @_;
1945 + my $self = _new($class, $id);
1946 + $self->{url} = file_to_s("$self->{dir}/info/url");
1947 + $self;
1948 +}
1949 +
1950 +sub refname { "refs/remotes/$_[0]->{id}" }
1951 +
1952 +sub ra {
1953 + my ($self) = shift;
1954 + $self->{ra} ||= Git::SVN::Ra->new($self->{url});
1955 +}
1956 +
1957 +sub copy_remote_ref {
1958 + my ($self) = @_;
1959 + my $origin = $::_cp_remote ? $::_cp_remote : 'origin';
1960 + my $ref = $self->refname;
1961 + if (command('ls-remote', $origin, $ref)) {
1962 + command_noisy('fetch', $origin, "$ref:$ref");
1963 + } elsif ($::_cp_remote && !$::_upgrade) {
1964 + die "Unable to find remote reference: $ref on $origin\n";
1965 + }
1966 +}
1967 +
1968 +sub traverse_ignore {
1969 + my ($self, $fh, $path, $r) = @_;
1970 + $path =~ s#^/+##g;
1971 + my ($dirent, undef, $props) = $self->ra->get_dir($path, $r);
1972 + my $p = $path;
1973 + $p =~ s#^\Q$self->{ra}->{svn_path}\E/##;
1974 + print $fh length $p ? "\n# $p\n" : "\n# /\n";
1975 + if (my $s = $props->{'svn:ignore'}) {
1976 + $s =~ s/[\r\n]+/\n/g;
1977 + chomp $s;
1978 + if (length $p == 0) {
1979 + $s =~ s#\n#\n/$p#g;
1980 + print $fh "/$s\n";
1981 + } else {
1982 + $s =~ s#\n#\n/$p/#g;
1983 + print $fh "/$p/$s\n";
1984 + }
1985 + }
1986 + foreach (sort keys %$dirent) {
1987 + next if $dirent->{$_}->kind != $SVN::Node::dir;
1988 + $self->traverse_ignore($fh, "$path/$_", $r);
1989 + }
1990 +}
1991 +
1992 +# returns the newest SVN revision number and newest commit SHA1
1993 +sub last_rev_commit {
1994 + my ($self) = @_;
1995 + if (defined $self->{last_rev} && defined $self->{last_commit}) {
1996 + return ($self->{last_rev}, $self->{last_commit});
1997 + }
1998 + my $c = verify_ref($self->refname.'^0');
1999 + if (defined $c && length $c) {
2000 + my $rev = (cmt_metadata($c))[1];
2001 + if (defined $rev) {
2002 + ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2003 + return ($rev, $c);
2004 + }
2005 + }
2006 + my $offset = -41; # from tail
2007 + my $rl;
2008 + open my $fh, '<', $self->{db_path} or
2009 + croak "$self->{db_path} not readable: $!\n";
2010 + seek $fh, $offset, 2;
2011 + $rl = readline $fh;
2012 + defined $rl or return (undef, undef);
2013 + chomp $rl;
2014 + while ($c ne $rl && tell $fh != 0) {
2015 + $offset -= 41;
2016 + seek $fh, $offset, 2;
2017 + $rl = readline $fh;
2018 + defined $rl or return (undef, undef);
2019 + chomp $rl;
2020 + }
2021 + my $rev = tell $fh;
2022 + croak $! if ($rev < 0);
2023 + $rev = ($rev - 41) / 41;
2024 + close $fh or croak $!;
2025 + ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2026 + return ($rev, $c);
2027 +}
2028 +
2029 +sub parse_revision {
2030 + my ($self, $base) = @_;
2031 + my $head = $self->ra->get_latest_revnum;
2032 + if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
2033 + return ($base + 1, $head) if (defined $base);
2034 + return (0, $head);
2035 + }
2036 + return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
2037 + return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
2038 + if ($::_revision =~ /^BASE:(\d+)$/) {
2039 + return ($base + 1, $1) if (defined $base);
2040 + return (0, $head);
2041 + }
2042 + return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
2043 + die "revision argument: $::_revision not understood by git-svn\n",
2044 + "Try using the command-line svn client instead\n";
2045 +}
2046 +
2047 +sub tmp_index_do {
2048 + my ($self, $sub) = @_;
2049 + my $old_index = $ENV{GIT_INDEX_FILE};
2050 + $ENV{GIT_INDEX_FILE} = $self->{index};
2051 + my @ret = &$sub;
2052 + if ($old_index) {
2053 + $ENV{GIT_INDEX_FILE} = $old_index;
2054 + } else {
2055 + delete $ENV{GIT_INDEX_FILE};
2056 + }
2057 + wantarray ? @ret : $ret[0];
2058 +}
2059 +
2060 +sub assert_index_clean {
2061 + my ($self, $treeish) = @_;
2062 +
2063 + $self->tmp_index_do(sub {
2064 + command_noisy('read-tree', $treeish) unless -e $self->{index};
2065 + my $x = command_oneline('write-tree');
2066 + my ($y) = (command(qw/cat-file commit/, $treeish) =~
2067 + /^tree ($::sha1)/mo);
2068 + if ($y ne $x) {
2069 + unlink $self->{index} or croak $!;
2070 + command_noisy('read-tree', $treeish);
2071 + }
2072 + $x = command_oneline('write-tree');
2073 + if ($y ne $x) {
2074 + ::fatal "trees ($treeish) $y != $x\n",
2075 + "Something is seriously wrong...\n";
2076 + }
2077 + });
2078 +}
2079 +
2080 +sub get_commit_parents {
2081 + my ($self, $log_msg, @parents) = @_;
2082 + my (%seen, @ret, @tmp);
2083 + # commit parents can be conditionally bound to a particular
2084 + # svn revision via: "svn_revno=commit_sha1", filter them out here:
2085 + foreach my $p (@parents) {
2086 + next unless defined $p;
2087 + if ($p =~ /^(\d+)=($::sha1_short)$/o) {
2088 + push @tmp, $2 if $1 == $log_msg->{revision};
2089 + } else {
2090 + push @tmp, $p if $p =~ /^$::sha1_short$/o;
2091 + }
2092 + }
2093 + if (my $cur = verify_ref($self->refname.'^0')) {
2094 + push @tmp, $cur;
2095 + }
2096 + push @tmp, $_ foreach (@{$log_msg->{parents}}, @tmp);
2097 + while (my $p = shift @tmp) {
2098 + next if $seen{$p};
2099 + $seen{$p} = 1;
2100 + push @ret, $p;
2101 + # MAXPARENT is defined to 16 in commit-tree.c:
2102 + last if @ret >= 16;
2103 + }
2104 + if (@tmp) {
2105 + die "r$log_msg->{revision}: No room for parents:\n\t",
2106 + join("\n\t", @tmp), "\n";
2107 + }
2108 + @ret;
2109 +}
2110 +
2111 +sub check_upgrade_needed {
2112 + my ($self) = @_;
2113 + if (!-r $self->{db_path}) {
2114 + -d $self->{dir} or mkpath([$self->{dir}]);
2115 + open my $fh, '>>', $self->{db_path} or croak $!;
2116 + close $fh;
2117 + }
2118 + return unless verify_ref($self->{id}.'-HEAD^0');
2119 + my $head = verify_ref($self->refname.'^0');
2120 + if ($@ || !$head) {
2121 + fatal("Please run: $0 rebuild --upgrade\n");
2122 + }
2123 +}
2124 +
2125 +sub do_git_commit {
2126 + my ($self, $log_msg, @parents) = @_;
2127 + if (my $c = $self->rev_db_get($log_msg->{revision})) {
2128 + croak "$log_msg->{revision} = $c already exists! ",
2129 + "Why are we refetching it?\n";
2130 + }
2131 + my ($name, $email) = author_name_email($log_msg->{author}, $self->ra);
2132 + $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
2133 + $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
2134 + $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_msg->{date};
2135 +
2136 + my $tree = $log_msg->{tree};
2137 + if (!defined $tree) {
2138 + $tree = $self->tmp_index_do(sub {
2139 + command_oneline('write-tree') });
2140 + }
2141 + die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2142 +
2143 + my @exec = ('git-commit-tree', $tree);
2144 + foreach ($self->get_commit_parents($log_msg, @parents)) {
2145 + push @exec, '-p', $_;
2146 + }
2147 + defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2148 + or croak $!;
2149 + print $msg_fh $log_msg->{log} or croak $!;
2150 + print $msg_fh "\ngit-svn-id: $self->{ra}->{url}\@$log_msg->{revision}",
2151 + " ", $self->ra->uuid,"\n" or croak $!;
2152 + $msg_fh->flush == 0 or croak $!;
2153 + close $msg_fh or croak $!;
2154 + chomp(my $commit = do { local $/; <$out_fh> });
2155 + close $out_fh or croak $!;
2156 + waitpid $pid, 0;
2157 + croak $? if $?;
2158 + if ($commit !~ /^$::sha1$/o) {
2159 + die "Failed to commit, invalid sha1: $commit\n";
2160 + }
2161 +
2162 + command_noisy('update-ref',$self->refname, $commit);
2163 + $self->rev_db_set($log_msg->{revision}, $commit);
2164 +
2165 + $self->{last_rev} = $log_msg->{revision};
2166 + $self->{last_commit} = $commit;
2167 + print "r$log_msg->{revision} = $commit\n";
2168 + return $commit;
2169 +}
2170 +
2171 +sub do_fetch {
2172 + my ($self, $paths, $rev) = @_; #, $author, $date, $msg) = @_;
2173 + my $ed = SVN::Git::Fetcher->new($self);
2174 + my ($last_rev, @parents);
2175 + if ($self->{last_commit}) {
2176 + $last_rev = $self->{last_rev};
2177 + $ed->{c} = $self->{last_commit};
2178 + @parents = ($self->{last_commit});
2179 + } else {
2180 + $last_rev = $rev;
2181 + }
2182 + unless ($self->ra->do_update($last_rev, $rev, '', 1, $ed)) {
2183 + die "SVN connection failed somewhere...\n";
2184 + }
2185 + $self->make_log_entry($rev, \@parents, $ed);
2186 +}
2187 +
2188 +sub write_untracked {
2189 + my ($self, $rev, $fh, $untracked) = @_;
2190 + my $h;
2191 + print $fh "r$rev\n" or croak $!;
2192 + $h = $untracked->{empty};
2193 + foreach (sort keys %$h) {
2194 + my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
2195 + print $fh " $act: ", uri_encode($_), "\n" or croak $!;
2196 + warn "W: $act: $_\n";
2197 + }
2198 + foreach my $t (qw/dir_prop file_prop/) {
2199 + $h = $untracked->{$t} or next;
2200 + foreach my $path (sort keys %$h) {
2201 + my $ppath = $path eq '' ? '.' : $path;
2202 + foreach my $prop (sort keys %{$h->{$path}}) {
2203 + next if $SKIP{$prop};
2204 + my $v = $h->{$path}->{$prop};
2205 + if (defined $v) {
2206 + print $fh " +$t: ",
2207 + uri_encode($ppath), ' ',
2208 + uri_encode($prop), ' ',
2209 + uri_encode($v), "\n"
2210 + or croak $!;
2211 + } else {
2212 + print $fh " -$t: ",
2213 + uri_encode($ppath), ' ',
2214 + uri_encode($prop), "\n"
2215 + or croak $!;
2216 + }
2217 + }
2218 + }
2219 + }
2220 + foreach my $t (qw/absent_file absent_directory/) {
2221 + $h = $untracked->{$t} or next;
2222 + foreach my $parent (sort keys %$h) {
2223 + foreach my $path (sort @{$h->{$parent}}) {
2224 + print $fh " $t: ",
2225 + uri_encode("$parent/$path"), "\n"
2226 + or croak $!;
2227 + warn "W: $t: $parent/$path ",
2228 + "Insufficient permissions?\n";
2229 + }
2230 + }
2231 + }
2232 +}
2233 +
2234 +sub make_log_entry {
2235 + my ($self, $rev, $parents, $untracked) = @_;
2236 + my $rp = $self->ra->rev_proplist($rev);
2237 + my %log_entry = ( parents => $parents || [], revision => $rev,
2238 + revprops => $rp, log => '');
2239 + open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
2240 + $self->write_untracked($rev, $un, $untracked);
2241 + foreach (sort keys %$rp) {
2242 + my $v = $rp->{$_};
2243 + if (/^svn:(author|date|log)$/) {
2244 + $log_entry{$1} = $v;
2245 + } else {
2246 + print $un " rev_prop: ", uri_encode($_), ' ',
2247 + uri_encode($v), "\n";
2248 + }
2249 + }
2250 + close $un or croak $!;
2251 + $log_entry{date} = parse_svn_date($log_entry{date});
2252 + $log_entry{author} = check_author($log_entry{author});
2253 + $log_entry{log} .= "\n";
2254 + \%log_entry;
2255 +}
2256 +
2257 +sub fetch {
2258 + my ($self, @parents) = @_;
2259 + my ($last_rev, $last_commit) = $self->last_rev_commit;
2260 + my ($base, $head) = $self->parse_revision($last_rev);
2261 + return if ($base > $head);
2262 + if (defined $last_commit) {
2263 + $self->assert_index_clean($last_commit);
2264 + }
2265 + my $inc = 1000;
2266 + my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
2267 + my $err_handler = $SVN::Error::handler;
2268 + $SVN::Error::handler = \&skip_unknown_revs;
2269 + while (1) {
2270 + my @revs;
2271 + $self->ra->get_log([''], $min, $max, 0, 1, 1, sub {
2272 + my ($paths, $rev, $author, $date, $msg) = @_;
2273 + push @revs, $rev });
2274 + foreach (@revs) {
2275 + my $log_entry = $self->do_fetch(undef, $_);
2276 + $self->do_git_commit($log_entry, @parents);
2277 + }
2278 + last if $max >= $head;
2279 + $min = $max + 1;
2280 + $max += $inc;
2281 + $max = $head if ($max > $head);
2282 + }
2283 + $SVN::Error::handler = $err_handler;
2284 +}
2285 +
2286 +sub set_tree_cb {
2287 + my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
2288 + # TODO: enable and test optimized commits:
2289 + if (0 && $rev == ($self->{last_rev} + 1)) {
2290 + $log_entry->{revision} = $rev;
2291 + $log_entry->{author} = $author;
2292 + $self->do_git_commit($log_entry, "$rev=$tree");
2293 + } else {
2294 + $self->fetch("$rev=$tree");
2295 + }
2296 +}
2297 +
2298 +sub set_tree {
2299 + my ($self, $tree) = (shift, shift);
2300 + my $log_entry = get_commit_entry($tree);
2301 + unless ($self->{last_rev}) {
2302 + fatal("Must have an existing revision to commit\n");
2303 + }
2304 + my $pool = SVN::Pool->new;
2305 + my $ed = SVN::Git::Editor->new({ r => $self->{last_rev},
2306 + ra => $self->ra->dup,
2307 + c => $tree,
2308 + svn_path => $self->ra->{svn_path}
2309 + },
2310 + $self->ra->get_commit_editor(
2311 + $log_entry->{log}, sub {
2312 + $self->set_tree_cb($log_entry,
2313 + $tree, @_);
2314 + }),
2315 + $pool);
2316 + my $mods = $ed->apply_diff($self->{last_commit}, $tree);
2317 + if (@$mods == 0) {
2318 + print "No changes\nr$self->{last_rev} = $tree\n";
2319 + }
2320 + $pool->clear;
2321 +}
2322 +
2323 +sub skip_unknown_revs {
2324 + my ($err) = @_;
2325 + my $errno = $err->apr_err();
2326 + # Maybe the branch we're tracking didn't
2327 + # exist when the repo started, so it's
2328 + # not an error if it doesn't, just continue
2329 + #
2330 + # Wonderfully consistent library, eh?
2331 + # 160013 - svn:// and file://
2332 + # 175002 - http(s)://
2333 + # 175007 - http(s):// (this repo required authorization, too...)
2334 + # More codes may be discovered later...
2335 + if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
2336 + return;
2337 + }
2338 + croak "Error from SVN, ($errno): ", $err->expanded_message,"\n";
2339 +}
2340 +
2341 +# rev_db:
2342 +# Tie::File seems to be prone to offset errors if revisions get sparse,
2343 +# it's not that fast, either. Tie::File is also not in Perl 5.6. So
2344 +# one of my favorite modules is out :< Next up would be one of the DBM
2345 +# modules, but I'm not sure which is most portable... So I'll just
2346 +# go with something that's plain-text, but still capable of
2347 +# being randomly accessed. So here's my ultra-simple fixed-width
2348 +# database. All records are 40 characters + "\n", so it's easy to seek
2349 +# to a revision: (41 * rev) is the byte offset.
2350 +# A record of 40 0s denotes an empty revision.
2351 +# And yes, it's still pretty fast (faster than Tie::File).
2352 +
2353 +sub rev_db_set {
2354 + my ($self, $rev, $commit) = @_;
2355 + length $commit == 40 or croak "arg3 must be a full SHA1 hexsum\n";
2356 + open my $fh, '+<', $self->{db_path} or croak $!;
2357 + my $offset = $rev * 41;
2358 + # assume that append is the common case:
2359 + seek $fh, 0, 2 or croak $!;
2360 + my $pos = tell $fh;
2361 + if ($pos < $offset) {
2362 + print $fh (('0' x 40),"\n") x (($offset - $pos) / 41)
2363 + or croak $!;
2364 + }
2365 + seek $fh, $offset, 0 or croak $!;
2366 + print $fh $commit,"\n" or croak $!;
2367 + close $fh or croak $!;
2368 +}
2369 +
2370 +sub rev_db_get {
2371 + my ($self, $rev) = @_;
2372 + my $ret;
2373 + my $offset = $rev * 41;
2374 + open my $fh, '<', $self->{db_path} or croak $!;
2375 + if (seek $fh, $offset, 0) {
2376 + $ret = readline $fh;
2377 + if (defined $ret) {
2378 + chomp $ret;
2379 + $ret = undef if ($ret =~ /^0{40}$/);
2380 + }
2381 + }
2382 + close $fh or croak $!;
2383 + $ret;
2384 +}
2385 +
2386 +sub _new {
2387 + my ($class, $id) = @_;
2388 + $id ||= $Git::SVN::default;
2389 + my $dir = "$ENV{GIT_DIR}/svn/$id";
2390 + bless { id => $id, dir => $dir, index => "$dir/index",
2391 + db_path => "$dir/.rev_db" }, $class;
2392 +}
2393 +
2394 +
2395 package Git::SVN::Prompt;
2396 use strict;
2397 use warnings;