PageRenderTime 80ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/perl/Git/SVN.pm

https://github.com/rrimando/git
Perl | 2386 lines | 2091 code | 169 blank | 126 comment | 288 complexity | cca6086c98619a346a6df59b11d031db MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-2-Clause, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. package Git::SVN;
  2. use strict;
  3. use warnings;
  4. use Fcntl qw/:DEFAULT :seek/;
  5. use constant rev_map_fmt => 'NH40';
  6. use vars qw/$_no_metadata
  7. $_repack $_repack_flags $_use_svm_props $_head
  8. $_use_svnsync_props $no_reuse_existing
  9. $_use_log_author $_add_author_from $_localtime/;
  10. use Carp qw/croak/;
  11. use File::Path qw/mkpath/;
  12. use File::Copy qw/copy/;
  13. use IPC::Open3;
  14. use Memoize; # core since 5.8.0, Jul 2002
  15. use Memoize::Storable;
  16. use POSIX qw(:signal_h);
  17. use Git qw(
  18. command
  19. command_oneline
  20. command_noisy
  21. command_output_pipe
  22. command_close_pipe
  23. get_tz_offset
  24. );
  25. use Git::SVN::Utils qw(
  26. fatal
  27. can_compress
  28. join_paths
  29. canonicalize_path
  30. canonicalize_url
  31. add_path_to_url
  32. );
  33. my $can_use_yaml;
  34. BEGIN {
  35. $can_use_yaml = eval { require Git::SVN::Memoize::YAML; 1};
  36. }
  37. our $_follow_parent = 1;
  38. our $_minimize_url = 'unset';
  39. our $default_repo_id = 'svn';
  40. our $default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
  41. my ($_gc_nr, $_gc_period);
  42. # properties that we do not log:
  43. my %SKIP_PROP;
  44. BEGIN {
  45. %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
  46. svn:special svn:executable
  47. svn:entry:committed-rev
  48. svn:entry:last-author
  49. svn:entry:uuid
  50. svn:entry:committed-date/;
  51. # some options are read globally, but can be overridden locally
  52. # per [svn-remote "..."] section. Command-line options will *NOT*
  53. # override options set in an [svn-remote "..."] section
  54. no strict 'refs';
  55. for my $option (qw/follow_parent no_metadata use_svm_props
  56. use_svnsync_props/) {
  57. my $key = $option;
  58. $key =~ tr/_//d;
  59. my $prop = "-$option";
  60. *$option = sub {
  61. my ($self) = @_;
  62. return $self->{$prop} if exists $self->{$prop};
  63. my $k = "svn-remote.$self->{repo_id}.$key";
  64. eval { command_oneline(qw/config --get/, $k) };
  65. if ($@) {
  66. $self->{$prop} = ${"Git::SVN::_$option"};
  67. } else {
  68. my $v = command_oneline(qw/config --bool/,$k);
  69. $self->{$prop} = $v eq 'false' ? 0 : 1;
  70. }
  71. return $self->{$prop};
  72. }
  73. }
  74. }
  75. my (%LOCKFILES, %INDEX_FILES);
  76. END {
  77. unlink keys %LOCKFILES if %LOCKFILES;
  78. unlink keys %INDEX_FILES if %INDEX_FILES;
  79. }
  80. sub resolve_local_globs {
  81. my ($url, $fetch, $glob_spec) = @_;
  82. return unless defined $glob_spec;
  83. my $ref = $glob_spec->{ref};
  84. my $path = $glob_spec->{path};
  85. foreach (command(qw#for-each-ref --format=%(refname) refs/#)) {
  86. next unless m#^$ref->{regex}$#;
  87. my $p = $1;
  88. my $pathname = desanitize_refname($path->full_path($p));
  89. my $refname = desanitize_refname($ref->full_path($p));
  90. if (my $existing = $fetch->{$pathname}) {
  91. if ($existing ne $refname) {
  92. die "Refspec conflict:\n",
  93. "existing: $existing\n",
  94. " globbed: $refname\n";
  95. }
  96. my $u = (::cmt_metadata("$refname"))[0];
  97. $u =~ s!^\Q$url\E(/|$)!! or die
  98. "$refname: '$url' not found in '$u'\n";
  99. if ($pathname ne $u) {
  100. warn "W: Refspec glob conflict ",
  101. "(ref: $refname):\n",
  102. "expected path: $pathname\n",
  103. " real path: $u\n",
  104. "Continuing ahead with $u\n";
  105. next;
  106. }
  107. } else {
  108. $fetch->{$pathname} = $refname;
  109. }
  110. }
  111. }
  112. sub parse_revision_argument {
  113. my ($base, $head) = @_;
  114. if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
  115. return ($base, $head);
  116. }
  117. return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
  118. return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
  119. return ($head, $head) if ($::_revision eq 'HEAD');
  120. return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
  121. return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
  122. die "revision argument: $::_revision not understood by git-svn\n";
  123. }
  124. sub fetch_all {
  125. my ($repo_id, $remotes) = @_;
  126. if (ref $repo_id) {
  127. my $gs = $repo_id;
  128. $repo_id = undef;
  129. $repo_id = $gs->{repo_id};
  130. }
  131. $remotes ||= read_all_remotes();
  132. my $remote = $remotes->{$repo_id} or
  133. die "[svn-remote \"$repo_id\"] unknown\n";
  134. my $fetch = $remote->{fetch};
  135. my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
  136. my (@gs, @globs);
  137. my $ra = Git::SVN::Ra->new($url);
  138. my $uuid = $ra->get_uuid;
  139. my $head = $ra->get_latest_revnum;
  140. # ignore errors, $head revision may not even exist anymore
  141. eval { $ra->get_log("", $head, 0, 1, 0, 1, sub { $head = $_[1] }) };
  142. warn "W: $@\n" if $@;
  143. my $base = defined $fetch ? $head : 0;
  144. # read the max revs for wildcard expansion (branches/*, tags/*)
  145. foreach my $t (qw/branches tags/) {
  146. defined $remote->{$t} or next;
  147. push @globs, @{$remote->{$t}};
  148. my $max_rev = eval { tmp_config(qw/--int --get/,
  149. "svn-remote.$repo_id.${t}-maxRev") };
  150. if (defined $max_rev && ($max_rev < $base)) {
  151. $base = $max_rev;
  152. } elsif (!defined $max_rev) {
  153. $base = 0;
  154. }
  155. }
  156. if ($fetch) {
  157. foreach my $p (sort keys %$fetch) {
  158. my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
  159. my $lr = $gs->rev_map_max;
  160. if (defined $lr) {
  161. $base = $lr if ($lr < $base);
  162. }
  163. push @gs, $gs;
  164. }
  165. }
  166. ($base, $head) = parse_revision_argument($base, $head);
  167. $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
  168. }
  169. sub read_all_remotes {
  170. my $r = {};
  171. my $use_svm_props = eval { command_oneline(qw/config --bool
  172. svn.useSvmProps/) };
  173. $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
  174. my $svn_refspec = qr{\s*(.*?)\s*:\s*(.+?)\s*};
  175. foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
  176. if (m!^(.+)\.fetch=$svn_refspec$!) {
  177. my ($remote, $local_ref, $remote_ref) = ($1, $2, $3);
  178. die("svn-remote.$remote: remote ref '$remote_ref' "
  179. . "must start with 'refs/'\n")
  180. unless $remote_ref =~ m{^refs/};
  181. $local_ref = uri_decode($local_ref);
  182. $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
  183. $r->{$remote}->{svm} = {} if $use_svm_props;
  184. } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
  185. $r->{$1}->{svm} = {};
  186. } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
  187. $r->{$1}->{url} = canonicalize_url($2);
  188. } elsif (m!^(.+)\.pushurl=\s*(.*)\s*$!) {
  189. $r->{$1}->{pushurl} = canonicalize_url($2);
  190. } elsif (m!^(.+)\.ignore-refs=\s*(.*)\s*$!) {
  191. $r->{$1}->{ignore_refs_regex} = $2;
  192. } elsif (m!^(.+)\.(branches|tags)=$svn_refspec$!) {
  193. my ($remote, $t, $local_ref, $remote_ref) =
  194. ($1, $2, $3, $4);
  195. die("svn-remote.$remote: remote ref '$remote_ref' ($t) "
  196. . "must start with 'refs/'\n")
  197. unless $remote_ref =~ m{^refs/};
  198. $local_ref = uri_decode($local_ref);
  199. require Git::SVN::GlobSpec;
  200. my $rs = {
  201. t => $t,
  202. remote => $remote,
  203. path => Git::SVN::GlobSpec->new($local_ref, 1),
  204. ref => Git::SVN::GlobSpec->new($remote_ref, 0) };
  205. if (length($rs->{ref}->{right}) != 0) {
  206. die "The '*' glob character must be the last ",
  207. "character of '$remote_ref'\n";
  208. }
  209. push @{ $r->{$remote}->{$t} }, $rs;
  210. }
  211. }
  212. map {
  213. if (defined $r->{$_}->{svm}) {
  214. my $svm;
  215. eval {
  216. my $section = "svn-remote.$_";
  217. $svm = {
  218. source => tmp_config('--get',
  219. "$section.svm-source"),
  220. replace => tmp_config('--get',
  221. "$section.svm-replace"),
  222. }
  223. };
  224. $r->{$_}->{svm} = $svm;
  225. }
  226. } keys %$r;
  227. foreach my $remote (keys %$r) {
  228. foreach ( grep { defined $_ }
  229. map { $r->{$remote}->{$_} } qw(branches tags) ) {
  230. foreach my $rs ( @$_ ) {
  231. $rs->{ignore_refs_regex} =
  232. $r->{$remote}->{ignore_refs_regex};
  233. }
  234. }
  235. }
  236. $r;
  237. }
  238. sub init_vars {
  239. $_gc_nr = $_gc_period = 1000;
  240. if (defined $_repack || defined $_repack_flags) {
  241. warn "Repack options are obsolete; they have no effect.\n";
  242. }
  243. }
  244. sub verify_remotes_sanity {
  245. return unless -d $ENV{GIT_DIR};
  246. my %seen;
  247. foreach (command(qw/config -l/)) {
  248. if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
  249. if ($seen{$1}) {
  250. die "Remote ref refs/remote/$1 is tracked by",
  251. "\n \"$_\"\nand\n \"$seen{$1}\"\n",
  252. "Please resolve this ambiguity in ",
  253. "your git configuration file before ",
  254. "continuing\n";
  255. }
  256. $seen{$1} = $_;
  257. }
  258. }
  259. }
  260. sub find_existing_remote {
  261. my ($url, $remotes) = @_;
  262. return undef if $no_reuse_existing;
  263. my $existing;
  264. foreach my $repo_id (keys %$remotes) {
  265. my $u = $remotes->{$repo_id}->{url} or next;
  266. next if $u ne $url;
  267. $existing = $repo_id;
  268. last;
  269. }
  270. $existing;
  271. }
  272. sub init_remote_config {
  273. my ($self, $url, $no_write) = @_;
  274. $url = canonicalize_url($url);
  275. my $r = read_all_remotes();
  276. my $existing = find_existing_remote($url, $r);
  277. if ($existing) {
  278. unless ($no_write) {
  279. print STDERR "Using existing ",
  280. "[svn-remote \"$existing\"]\n";
  281. }
  282. $self->{repo_id} = $existing;
  283. } elsif ($_minimize_url) {
  284. my $min_url = Git::SVN::Ra->new($url)->minimize_url;
  285. $existing = find_existing_remote($min_url, $r);
  286. if ($existing) {
  287. unless ($no_write) {
  288. print STDERR "Using existing ",
  289. "[svn-remote \"$existing\"]\n";
  290. }
  291. $self->{repo_id} = $existing;
  292. }
  293. if ($min_url ne $url) {
  294. unless ($no_write) {
  295. print STDERR "Using higher level of URL: ",
  296. "$url => $min_url\n";
  297. }
  298. my $old_path = $self->path;
  299. $url =~ s!^\Q$min_url\E(/|$)!!;
  300. $url = join_paths($url, $old_path);
  301. $self->path($url);
  302. $url = $min_url;
  303. }
  304. }
  305. my $orig_url;
  306. if (!$existing) {
  307. # verify that we aren't overwriting anything:
  308. $orig_url = eval {
  309. command_oneline('config', '--get',
  310. "svn-remote.$self->{repo_id}.url")
  311. };
  312. if ($orig_url && ($orig_url ne $url)) {
  313. die "svn-remote.$self->{repo_id}.url already set: ",
  314. "$orig_url\nwanted to set to: $url\n";
  315. }
  316. }
  317. my ($xrepo_id, $xpath) = find_ref($self->refname);
  318. if (!$no_write && defined $xpath) {
  319. die "svn-remote.$xrepo_id.fetch already set to track ",
  320. "$xpath:", $self->refname, "\n";
  321. }
  322. unless ($no_write) {
  323. command_noisy('config',
  324. "svn-remote.$self->{repo_id}.url", $url);
  325. my $path = $self->path;
  326. $path =~ s{^/}{};
  327. $path =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
  328. $self->path($path);
  329. command_noisy('config', '--add',
  330. "svn-remote.$self->{repo_id}.fetch",
  331. $self->path.":".$self->refname);
  332. }
  333. $self->url($url);
  334. }
  335. sub find_by_url { # repos_root and, path are optional
  336. my ($class, $full_url, $repos_root, $path) = @_;
  337. $full_url = canonicalize_url($full_url);
  338. return undef unless defined $full_url;
  339. remove_username($full_url);
  340. remove_username($repos_root) if defined $repos_root;
  341. my $remotes = read_all_remotes();
  342. if (defined $full_url && defined $repos_root && !defined $path) {
  343. $path = $full_url;
  344. $path =~ s#^\Q$repos_root\E(?:/|$)##;
  345. }
  346. foreach my $repo_id (keys %$remotes) {
  347. my $u = $remotes->{$repo_id}->{url} or next;
  348. remove_username($u);
  349. next if defined $repos_root && $repos_root ne $u;
  350. my $fetch = $remotes->{$repo_id}->{fetch} || {};
  351. foreach my $t (qw/branches tags/) {
  352. foreach my $globspec (@{$remotes->{$repo_id}->{$t}}) {
  353. resolve_local_globs($u, $fetch, $globspec);
  354. }
  355. }
  356. my $p = $path;
  357. my $rwr = rewrite_root({repo_id => $repo_id});
  358. my $svm = $remotes->{$repo_id}->{svm}
  359. if defined $remotes->{$repo_id}->{svm};
  360. unless (defined $p) {
  361. $p = $full_url;
  362. my $z = $u;
  363. my $prefix = '';
  364. if ($rwr) {
  365. $z = $rwr;
  366. remove_username($z);
  367. } elsif (defined $svm) {
  368. $z = $svm->{source};
  369. $prefix = $svm->{replace};
  370. $prefix =~ s#^\Q$u\E(?:/|$)##;
  371. $prefix =~ s#/$##;
  372. }
  373. $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next;
  374. }
  375. # remote fetch paths are not URI escaped. Decode ours
  376. # so they match
  377. $p = uri_decode($p);
  378. foreach my $f (keys %$fetch) {
  379. next if $f ne $p;
  380. return Git::SVN->new($fetch->{$f}, $repo_id, $f);
  381. }
  382. }
  383. undef;
  384. }
  385. sub init {
  386. my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
  387. my $self = _new($class, $repo_id, $ref_id, $path);
  388. if (defined $url) {
  389. $self->init_remote_config($url, $no_write);
  390. }
  391. $self;
  392. }
  393. sub find_ref {
  394. my ($ref_id) = @_;
  395. foreach (command(qw/config -l/)) {
  396. next unless m!^svn-remote\.(.+)\.fetch=
  397. \s*(.*?)\s*:\s*(.+?)\s*$!x;
  398. my ($repo_id, $path, $ref) = ($1, $2, $3);
  399. if ($ref eq $ref_id) {
  400. $path = '' if ($path =~ m#^\./?#);
  401. return ($repo_id, $path);
  402. }
  403. }
  404. (undef, undef, undef);
  405. }
  406. sub new {
  407. my ($class, $ref_id, $repo_id, $path) = @_;
  408. if (defined $ref_id && !defined $repo_id && !defined $path) {
  409. ($repo_id, $path) = find_ref($ref_id);
  410. if (!defined $repo_id) {
  411. die "Could not find a \"svn-remote.*.fetch\" key ",
  412. "in the repository configuration matching: ",
  413. "$ref_id\n";
  414. }
  415. }
  416. my $self = _new($class, $repo_id, $ref_id, $path);
  417. if (!defined $self->path || !length $self->path) {
  418. my $fetch = command_oneline('config', '--get',
  419. "svn-remote.$repo_id.fetch",
  420. ":$ref_id\$") or
  421. die "Failed to read \"svn-remote.$repo_id.fetch\" ",
  422. "\":$ref_id\$\" in config\n";
  423. my($path) = split(/\s*:\s*/, $fetch);
  424. $self->path($path);
  425. }
  426. {
  427. my $path = $self->path;
  428. $path =~ s{\A/}{};
  429. $path =~ s{/\z}{};
  430. $self->path($path);
  431. }
  432. my $url = command_oneline('config', '--get',
  433. "svn-remote.$repo_id.url") or
  434. die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
  435. $self->url($url);
  436. $self->{pushurl} = eval { command_oneline('config', '--get',
  437. "svn-remote.$repo_id.pushurl") };
  438. $self->rebuild;
  439. $self;
  440. }
  441. sub refname {
  442. my ($refname) = $_[0]->{ref_id} ;
  443. # It cannot end with a slash /, we'll throw up on this because
  444. # SVN can't have directories with a slash in their name, either:
  445. if ($refname =~ m{/$}) {
  446. die "ref: '$refname' ends with a trailing slash, this is ",
  447. "not permitted by git nor Subversion\n";
  448. }
  449. # It cannot have ASCII control character space, tilde ~, caret ^,
  450. # colon :, question-mark ?, asterisk *, space, or open bracket [
  451. # anywhere.
  452. #
  453. # Additionally, % must be escaped because it is used for escaping
  454. # and we want our escaped refname to be reversible
  455. $refname =~ s{([ \%~\^:\?\*\[\t])}{sprintf('%%%02X',ord($1))}eg;
  456. # no slash-separated component can begin with a dot .
  457. # /.* becomes /%2E*
  458. $refname =~ s{/\.}{/%2E}g;
  459. # It cannot have two consecutive dots .. anywhere
  460. # .. becomes %2E%2E
  461. $refname =~ s{\.\.}{%2E%2E}g;
  462. # trailing dots and .lock are not allowed
  463. # .$ becomes %2E and .lock becomes %2Elock
  464. $refname =~ s{\.(?=$|lock$)}{%2E};
  465. # the sequence @{ is used to access the reflog
  466. # @{ becomes %40{
  467. $refname =~ s{\@\{}{%40\{}g;
  468. return $refname;
  469. }
  470. sub desanitize_refname {
  471. my ($refname) = @_;
  472. $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
  473. return $refname;
  474. }
  475. sub svm_uuid {
  476. my ($self) = @_;
  477. return $self->{svm}->{uuid} if $self->svm;
  478. $self->ra;
  479. unless ($self->{svm}) {
  480. die "SVM UUID not cached, and reading remotely failed\n";
  481. }
  482. $self->{svm}->{uuid};
  483. }
  484. sub svm {
  485. my ($self) = @_;
  486. return $self->{svm} if $self->{svm};
  487. my $svm;
  488. # see if we have it in our config, first:
  489. eval {
  490. my $section = "svn-remote.$self->{repo_id}";
  491. $svm = {
  492. source => tmp_config('--get', "$section.svm-source"),
  493. uuid => tmp_config('--get', "$section.svm-uuid"),
  494. replace => tmp_config('--get', "$section.svm-replace"),
  495. }
  496. };
  497. if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
  498. $self->{svm} = $svm;
  499. }
  500. $self->{svm};
  501. }
  502. sub _set_svm_vars {
  503. my ($self, $ra) = @_;
  504. return $ra if $self->svm;
  505. my @err = ( "useSvmProps set, but failed to read SVM properties\n",
  506. "(svm:source, svm:uuid) ",
  507. "from the following URLs:\n" );
  508. sub read_svm_props {
  509. my ($self, $ra, $path, $r) = @_;
  510. my $props = ($ra->get_dir($path, $r))[2];
  511. my $src = $props->{'svm:source'};
  512. my $uuid = $props->{'svm:uuid'};
  513. return undef if (!$src || !$uuid);
  514. chomp($src, $uuid);
  515. $uuid =~ m{^[0-9a-f\-]{30,}$}i
  516. or die "doesn't look right - svm:uuid is '$uuid'\n";
  517. # the '!' is used to mark the repos_root!/relative/path
  518. $src =~ s{/?!/?}{/};
  519. $src =~ s{/+$}{}; # no trailing slashes please
  520. # username is of no interest
  521. $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
  522. my $replace = add_path_to_url($ra->url, $path);
  523. my $section = "svn-remote.$self->{repo_id}";
  524. tmp_config("$section.svm-source", $src);
  525. tmp_config("$section.svm-replace", $replace);
  526. tmp_config("$section.svm-uuid", $uuid);
  527. $self->{svm} = {
  528. source => $src,
  529. uuid => $uuid,
  530. replace => $replace
  531. };
  532. }
  533. my $r = $ra->get_latest_revnum;
  534. my $path = $self->path;
  535. my %tried;
  536. while (length $path) {
  537. my $try = add_path_to_url($self->url, $path);
  538. unless ($tried{$try}) {
  539. return $ra if $self->read_svm_props($ra, $path, $r);
  540. $tried{$try} = 1;
  541. }
  542. $path =~ s#/?[^/]+$##;
  543. }
  544. die "Path: '$path' should be ''\n" if $path ne '';
  545. return $ra if $self->read_svm_props($ra, $path, $r);
  546. $tried{ add_path_to_url($self->url, $path) } = 1;
  547. if ($ra->{repos_root} eq $self->url) {
  548. die @err, (map { " $_\n" } keys %tried), "\n";
  549. }
  550. # nope, make sure we're connected to the repository root:
  551. my $ok;
  552. my @tried_b;
  553. $path = $ra->{svn_path};
  554. $ra = Git::SVN::Ra->new($ra->{repos_root});
  555. while (length $path) {
  556. my $try = add_path_to_url($ra->url, $path);
  557. unless ($tried{$try}) {
  558. $ok = $self->read_svm_props($ra, $path, $r);
  559. last if $ok;
  560. $tried{$try} = 1;
  561. }
  562. $path =~ s#/?[^/]+$##;
  563. }
  564. die "Path: '$path' should be ''\n" if $path ne '';
  565. $ok ||= $self->read_svm_props($ra, $path, $r);
  566. $tried{ add_path_to_url($ra->url, $path) } = 1;
  567. if (!$ok) {
  568. die @err, (map { " $_\n" } keys %tried), "\n";
  569. }
  570. Git::SVN::Ra->new($self->url);
  571. }
  572. sub svnsync {
  573. my ($self) = @_;
  574. return $self->{svnsync} if $self->{svnsync};
  575. if ($self->no_metadata) {
  576. die "Can't have both 'noMetadata' and ",
  577. "'useSvnsyncProps' options set!\n";
  578. }
  579. if ($self->rewrite_root) {
  580. die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
  581. "options set!\n";
  582. }
  583. if ($self->rewrite_uuid) {
  584. die "Can't have both 'useSvnsyncProps' and 'rewriteUUID' ",
  585. "options set!\n";
  586. }
  587. my $svnsync;
  588. # see if we have it in our config, first:
  589. eval {
  590. my $section = "svn-remote.$self->{repo_id}";
  591. my $url = tmp_config('--get', "$section.svnsync-url");
  592. ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
  593. die "doesn't look right - svn:sync-from-url is '$url'\n";
  594. my $uuid = tmp_config('--get', "$section.svnsync-uuid");
  595. ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
  596. die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
  597. $svnsync = { url => $url, uuid => $uuid }
  598. };
  599. if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
  600. return $self->{svnsync} = $svnsync;
  601. }
  602. my $err = "useSvnsyncProps set, but failed to read " .
  603. "svnsync property: svn:sync-from-";
  604. my $rp = $self->ra->rev_proplist(0);
  605. my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
  606. ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
  607. die "doesn't look right - svn:sync-from-url is '$url'\n";
  608. my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
  609. ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
  610. die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
  611. my $section = "svn-remote.$self->{repo_id}";
  612. tmp_config('--add', "$section.svnsync-uuid", $uuid);
  613. tmp_config('--add', "$section.svnsync-url", $url);
  614. return $self->{svnsync} = { url => $url, uuid => $uuid };
  615. }
  616. # this allows us to memoize our SVN::Ra UUID locally and avoid a
  617. # remote lookup (useful for 'git svn log').
  618. sub ra_uuid {
  619. my ($self) = @_;
  620. unless ($self->{ra_uuid}) {
  621. my $key = "svn-remote.$self->{repo_id}.uuid";
  622. my $uuid = eval { tmp_config('--get', $key) };
  623. if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/i) {
  624. $self->{ra_uuid} = $uuid;
  625. } else {
  626. die "ra_uuid called without URL\n" unless $self->url;
  627. $self->{ra_uuid} = $self->ra->get_uuid;
  628. tmp_config('--add', $key, $self->{ra_uuid});
  629. }
  630. }
  631. $self->{ra_uuid};
  632. }
  633. sub _set_repos_root {
  634. my ($self, $repos_root) = @_;
  635. my $k = "svn-remote.$self->{repo_id}.reposRoot";
  636. $repos_root ||= $self->ra->{repos_root};
  637. tmp_config($k, $repos_root);
  638. $repos_root;
  639. }
  640. sub repos_root {
  641. my ($self) = @_;
  642. my $k = "svn-remote.$self->{repo_id}.reposRoot";
  643. eval { tmp_config('--get', $k) } || $self->_set_repos_root;
  644. }
  645. sub ra {
  646. my ($self) = shift;
  647. my $ra = Git::SVN::Ra->new($self->url);
  648. $self->_set_repos_root($ra->{repos_root});
  649. if ($self->use_svm_props && !$self->{svm}) {
  650. if ($self->no_metadata) {
  651. die "Can't have both 'noMetadata' and ",
  652. "'useSvmProps' options set!\n";
  653. } elsif ($self->use_svnsync_props) {
  654. die "Can't have both 'useSvnsyncProps' and ",
  655. "'useSvmProps' options set!\n";
  656. }
  657. $ra = $self->_set_svm_vars($ra);
  658. $self->{-want_revprops} = 1;
  659. }
  660. $ra;
  661. }
  662. # prop_walk(PATH, REV, SUB)
  663. # -------------------------
  664. # Recursively traverse PATH at revision REV and invoke SUB for each
  665. # directory that contains a SVN property. SUB will be invoked as
  666. # follows: &SUB(gs, path, props); where `gs' is this instance of
  667. # Git::SVN, `path' the path to the directory where the properties
  668. # `props' were found. The `path' will be relative to point of checkout,
  669. # that is, if url://repo/trunk is the current Git branch, and that
  670. # directory contains a sub-directory `d', SUB will be invoked with `/d/'
  671. # as `path' (note the trailing `/').
  672. sub prop_walk {
  673. my ($self, $path, $rev, $sub) = @_;
  674. $path =~ s#^/##;
  675. my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
  676. $path =~ s#^/*#/#g;
  677. my $p = $path;
  678. # Strip the irrelevant part of the path.
  679. $p =~ s#^/+\Q@{[$self->path]}\E(/|$)#/#;
  680. # Ensure the path is terminated by a `/'.
  681. $p =~ s#/*$#/#;
  682. # The properties contain all the internal SVN stuff nobody
  683. # (usually) cares about.
  684. my $interesting_props = 0;
  685. foreach (keys %{$props}) {
  686. # If it doesn't start with `svn:', it must be a
  687. # user-defined property.
  688. ++$interesting_props and next if $_ !~ /^svn:/;
  689. # FIXME: Fragile, if SVN adds new public properties,
  690. # this needs to be updated.
  691. ++$interesting_props if /^svn:(?:ignore|keywords|executable
  692. |eol-style|mime-type
  693. |externals|needs-lock)$/x;
  694. }
  695. &$sub($self, $p, $props) if $interesting_props;
  696. foreach (sort keys %$dirent) {
  697. next if $dirent->{$_}->{kind} != $SVN::Node::dir;
  698. $self->prop_walk($self->path . $p . $_, $rev, $sub);
  699. }
  700. }
  701. sub last_rev { ($_[0]->last_rev_commit)[0] }
  702. sub last_commit { ($_[0]->last_rev_commit)[1] }
  703. # returns the newest SVN revision number and newest commit SHA1
  704. sub last_rev_commit {
  705. my ($self) = @_;
  706. if (defined $self->{last_rev} && defined $self->{last_commit}) {
  707. return ($self->{last_rev}, $self->{last_commit});
  708. }
  709. my $c = ::verify_ref($self->refname.'^0');
  710. if ($c && !$self->use_svm_props && !$self->no_metadata) {
  711. my $rev = (::cmt_metadata($c))[1];
  712. if (defined $rev) {
  713. ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
  714. return ($rev, $c);
  715. }
  716. }
  717. my $map_path = $self->map_path;
  718. unless (-e $map_path) {
  719. ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
  720. return (undef, undef);
  721. }
  722. my ($rev, $commit) = $self->rev_map_max(1);
  723. ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
  724. return ($rev, $commit);
  725. }
  726. sub get_fetch_range {
  727. my ($self, $min, $max) = @_;
  728. $max ||= $self->ra->get_latest_revnum;
  729. $min ||= $self->rev_map_max;
  730. (++$min, $max);
  731. }
  732. sub tmp_config {
  733. my (@args) = @_;
  734. my $old_def_config = "$ENV{GIT_DIR}/svn/config";
  735. my $config = "$ENV{GIT_DIR}/svn/.metadata";
  736. if (! -f $config && -f $old_def_config) {
  737. rename $old_def_config, $config or
  738. die "Failed rename $old_def_config => $config: $!\n";
  739. }
  740. my $old_config = $ENV{GIT_CONFIG};
  741. $ENV{GIT_CONFIG} = $config;
  742. $@ = undef;
  743. my @ret = eval {
  744. unless (-f $config) {
  745. mkfile($config);
  746. open my $fh, '>', $config or
  747. die "Can't open $config: $!\n";
  748. print $fh "; This file is used internally by ",
  749. "git-svn\n" or die
  750. "Couldn't write to $config: $!\n";
  751. print $fh "; You should not have to edit it\n" or
  752. die "Couldn't write to $config: $!\n";
  753. close $fh or die "Couldn't close $config: $!\n";
  754. }
  755. command('config', @args);
  756. };
  757. my $err = $@;
  758. if (defined $old_config) {
  759. $ENV{GIT_CONFIG} = $old_config;
  760. } else {
  761. delete $ENV{GIT_CONFIG};
  762. }
  763. die $err if $err;
  764. wantarray ? @ret : $ret[0];
  765. }
  766. sub tmp_index_do {
  767. my ($self, $sub) = @_;
  768. my $old_index = $ENV{GIT_INDEX_FILE};
  769. $ENV{GIT_INDEX_FILE} = $self->{index};
  770. $@ = undef;
  771. my @ret = eval {
  772. my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
  773. mkpath([$dir]) unless -d $dir;
  774. &$sub;
  775. };
  776. my $err = $@;
  777. if (defined $old_index) {
  778. $ENV{GIT_INDEX_FILE} = $old_index;
  779. } else {
  780. delete $ENV{GIT_INDEX_FILE};
  781. }
  782. die $err if $err;
  783. wantarray ? @ret : $ret[0];
  784. }
  785. sub assert_index_clean {
  786. my ($self, $treeish) = @_;
  787. $self->tmp_index_do(sub {
  788. command_noisy('read-tree', $treeish) unless -e $self->{index};
  789. my $x = command_oneline('write-tree');
  790. my ($y) = (command(qw/cat-file commit/, $treeish) =~
  791. /^tree ($::sha1)/mo);
  792. return if $y eq $x;
  793. warn "Index mismatch: $y != $x\nrereading $treeish\n";
  794. unlink $self->{index} or die "unlink $self->{index}: $!\n";
  795. command_noisy('read-tree', $treeish);
  796. $x = command_oneline('write-tree');
  797. if ($y ne $x) {
  798. fatal "trees ($treeish) $y != $x\n",
  799. "Something is seriously wrong...";
  800. }
  801. });
  802. }
  803. sub get_commit_parents {
  804. my ($self, $log_entry) = @_;
  805. my (%seen, @ret, @tmp);
  806. # legacy support for 'set-tree'; this is only used by set_tree_cb:
  807. if (my $ip = $self->{inject_parents}) {
  808. if (my $commit = delete $ip->{$log_entry->{revision}}) {
  809. push @tmp, $commit;
  810. }
  811. }
  812. if (my $cur = ::verify_ref($self->refname.'^0')) {
  813. push @tmp, $cur;
  814. }
  815. if (my $ipd = $self->{inject_parents_dcommit}) {
  816. if (my $commit = delete $ipd->{$log_entry->{revision}}) {
  817. push @tmp, @$commit;
  818. }
  819. }
  820. push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
  821. while (my $p = shift @tmp) {
  822. next if $seen{$p};
  823. $seen{$p} = 1;
  824. push @ret, $p;
  825. }
  826. @ret;
  827. }
  828. sub rewrite_root {
  829. my ($self) = @_;
  830. return $self->{-rewrite_root} if exists $self->{-rewrite_root};
  831. my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
  832. my $rwr = eval { command_oneline(qw/config --get/, $k) };
  833. if ($rwr) {
  834. $rwr =~ s#/+$##;
  835. if ($rwr !~ m#^[a-z\+]+://#) {
  836. die "$rwr is not a valid URL (key: $k)\n";
  837. }
  838. }
  839. $self->{-rewrite_root} = $rwr;
  840. }
  841. sub rewrite_uuid {
  842. my ($self) = @_;
  843. return $self->{-rewrite_uuid} if exists $self->{-rewrite_uuid};
  844. my $k = "svn-remote.$self->{repo_id}.rewriteUUID";
  845. my $rwid = eval { command_oneline(qw/config --get/, $k) };
  846. if ($rwid) {
  847. $rwid =~ s#/+$##;
  848. if ($rwid !~ m#^[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}$#) {
  849. die "$rwid is not a valid UUID (key: $k)\n";
  850. }
  851. }
  852. $self->{-rewrite_uuid} = $rwid;
  853. }
  854. sub metadata_url {
  855. my ($self) = @_;
  856. my $url = $self->rewrite_root || $self->url;
  857. return canonicalize_url( add_path_to_url( $url, $self->path ) );
  858. }
  859. sub full_url {
  860. my ($self) = @_;
  861. return canonicalize_url( add_path_to_url( $self->url, $self->path ) );
  862. }
  863. sub full_pushurl {
  864. my ($self) = @_;
  865. if ($self->{pushurl}) {
  866. return canonicalize_url( add_path_to_url( $self->{pushurl}, $self->path ) );
  867. } else {
  868. return $self->full_url;
  869. }
  870. }
  871. sub set_commit_header_env {
  872. my ($log_entry) = @_;
  873. my %env;
  874. foreach my $ned (qw/NAME EMAIL DATE/) {
  875. foreach my $ac (qw/AUTHOR COMMITTER/) {
  876. $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
  877. }
  878. }
  879. $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
  880. $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
  881. $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
  882. $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
  883. ? $log_entry->{commit_name}
  884. : $log_entry->{name};
  885. $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
  886. ? $log_entry->{commit_email}
  887. : $log_entry->{email};
  888. \%env;
  889. }
  890. sub restore_commit_header_env {
  891. my ($env) = @_;
  892. foreach my $ned (qw/NAME EMAIL DATE/) {
  893. foreach my $ac (qw/AUTHOR COMMITTER/) {
  894. my $k = "GIT_${ac}_${ned}";
  895. if (defined $env->{$k}) {
  896. $ENV{$k} = $env->{$k};
  897. } else {
  898. delete $ENV{$k};
  899. }
  900. }
  901. }
  902. }
  903. sub gc {
  904. command_noisy('gc', '--auto');
  905. };
  906. sub do_git_commit {
  907. my ($self, $log_entry) = @_;
  908. my $lr = $self->last_rev;
  909. if (defined $lr && $lr >= $log_entry->{revision}) {
  910. die "Last fetched revision of ", $self->refname,
  911. " was r$lr, but we are about to fetch: ",
  912. "r$log_entry->{revision}!\n";
  913. }
  914. if (my $c = $self->rev_map_get($log_entry->{revision})) {
  915. croak "$log_entry->{revision} = $c already exists! ",
  916. "Why are we refetching it?\n";
  917. }
  918. my $old_env = set_commit_header_env($log_entry);
  919. my $tree = $log_entry->{tree};
  920. if (!defined $tree) {
  921. $tree = $self->tmp_index_do(sub {
  922. command_oneline('write-tree') });
  923. }
  924. die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
  925. my @exec = ('git', 'commit-tree', $tree);
  926. foreach ($self->get_commit_parents($log_entry)) {
  927. push @exec, '-p', $_;
  928. }
  929. defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
  930. or croak $!;
  931. binmode $msg_fh;
  932. # we always get UTF-8 from SVN, but we may want our commits in
  933. # a different encoding.
  934. if (my $enc = Git::config('i18n.commitencoding')) {
  935. require Encode;
  936. Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
  937. }
  938. print $msg_fh $log_entry->{log} or croak $!;
  939. restore_commit_header_env($old_env);
  940. unless ($self->no_metadata) {
  941. print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
  942. or croak $!;
  943. }
  944. $msg_fh->flush == 0 or croak $!;
  945. close $msg_fh or croak $!;
  946. chomp(my $commit = do { local $/; <$out_fh> });
  947. close $out_fh or croak $!;
  948. waitpid $pid, 0;
  949. croak $? if $?;
  950. if ($commit !~ /^$::sha1$/o) {
  951. die "Failed to commit, invalid sha1: $commit\n";
  952. }
  953. $self->rev_map_set($log_entry->{revision}, $commit, 1);
  954. $self->{last_rev} = $log_entry->{revision};
  955. $self->{last_commit} = $commit;
  956. print "r$log_entry->{revision}" unless $::_q > 1;
  957. if (defined $log_entry->{svm_revision}) {
  958. print " (\@$log_entry->{svm_revision})" unless $::_q > 1;
  959. $self->rev_map_set($log_entry->{svm_revision}, $commit,
  960. 0, $self->svm_uuid);
  961. }
  962. print " = $commit ($self->{ref_id})\n" unless $::_q > 1;
  963. if (--$_gc_nr == 0) {
  964. $_gc_nr = $_gc_period;
  965. gc();
  966. }
  967. return $commit;
  968. }
  969. sub match_paths {
  970. my ($self, $paths, $r) = @_;
  971. return 1 if $self->path eq '';
  972. if (my $path = $paths->{"/".$self->path}) {
  973. return ($path->{action} eq 'D') ? 0 : 1;
  974. }
  975. $self->{path_regex} ||= qr{^/\Q@{[$self->path]}\E/};
  976. if (grep /$self->{path_regex}/, keys %$paths) {
  977. return 1;
  978. }
  979. my $c = '';
  980. foreach (split m#/#, $self->path) {
  981. $c .= "/$_";
  982. next unless ($paths->{$c} &&
  983. ($paths->{$c}->{action} =~ /^[AR]$/));
  984. if ($self->ra->check_path($self->path, $r) ==
  985. $SVN::Node::dir) {
  986. return 1;
  987. }
  988. }
  989. return 0;
  990. }
  991. sub find_parent_branch {
  992. my ($self, $paths, $rev) = @_;
  993. return undef unless $self->follow_parent;
  994. unless (defined $paths) {
  995. my $err_handler = $SVN::Error::handler;
  996. $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
  997. $self->ra->get_log([$self->path], $rev, $rev, 0, 1, 1,
  998. sub { $paths = $_[0] });
  999. $SVN::Error::handler = $err_handler;
  1000. }
  1001. return undef unless defined $paths;
  1002. # look for a parent from another branch:
  1003. my @b_path_components = split m#/#, $self->path;
  1004. my @a_path_components;
  1005. my $i;
  1006. while (@b_path_components) {
  1007. $i = $paths->{'/'.join('/', @b_path_components)};
  1008. last if $i && defined $i->{copyfrom_path};
  1009. unshift(@a_path_components, pop(@b_path_components));
  1010. }
  1011. return undef unless defined $i && defined $i->{copyfrom_path};
  1012. my $branch_from = $i->{copyfrom_path};
  1013. if (@a_path_components) {
  1014. print STDERR "branch_from: $branch_from => ";
  1015. $branch_from .= '/'.join('/', @a_path_components);
  1016. print STDERR $branch_from, "\n";
  1017. }
  1018. my $r = $i->{copyfrom_rev};
  1019. my $repos_root = $self->ra->{repos_root};
  1020. my $url = $self->ra->url;
  1021. my $new_url = canonicalize_url( add_path_to_url( $url, $branch_from ) );
  1022. print STDERR "Found possible branch point: ",
  1023. "$new_url => ", $self->full_url, ", $r\n"
  1024. unless $::_q > 1;
  1025. $branch_from =~ s#^/##;
  1026. my $gs = $self->other_gs($new_url, $url,
  1027. $branch_from, $r, $self->{ref_id});
  1028. my ($r0, $parent) = $gs->find_rev_before($r, 1);
  1029. {
  1030. my ($base, $head);
  1031. if (!defined $r0 || !defined $parent) {
  1032. ($base, $head) = parse_revision_argument(0, $r);
  1033. } else {
  1034. if ($r0 < $r) {
  1035. $gs->ra->get_log([$gs->path], $r0 + 1, $r, 1,
  1036. 0, 1, sub { $base = $_[1] - 1 });
  1037. }
  1038. }
  1039. if (defined $base && $base <= $r) {
  1040. $gs->fetch($base, $r);
  1041. }
  1042. ($r0, $parent) = $gs->find_rev_before($r, 1);
  1043. }
  1044. if (defined $r0 && defined $parent) {
  1045. print STDERR "Found branch parent: ($self->{ref_id}) $parent\n"
  1046. unless $::_q > 1;
  1047. my $ed;
  1048. if ($self->ra->can_do_switch) {
  1049. $self->assert_index_clean($parent);
  1050. print STDERR "Following parent with do_switch\n"
  1051. unless $::_q > 1;
  1052. # do_switch works with svn/trunk >= r22312, but that
  1053. # is not included with SVN 1.4.3 (the latest version
  1054. # at the moment), so we can't rely on it
  1055. $self->{last_rev} = $r0;
  1056. $self->{last_commit} = $parent;
  1057. $ed = Git::SVN::Fetcher->new($self, $gs->path);
  1058. $gs->ra->gs_do_switch($r0, $rev, $gs,
  1059. $self->full_url, $ed)
  1060. or die "SVN connection failed somewhere...\n";
  1061. } elsif ($self->ra->trees_match($new_url, $r0,
  1062. $self->full_url, $rev)) {
  1063. print STDERR "Trees match:\n",
  1064. " $new_url\@$r0\n",
  1065. " ${\$self->full_url}\@$rev\n",
  1066. "Following parent with no changes\n"
  1067. unless $::_q > 1;
  1068. $self->tmp_index_do(sub {
  1069. command_noisy('read-tree', $parent);
  1070. });
  1071. $self->{last_commit} = $parent;
  1072. } else {
  1073. print STDERR "Following parent with do_update\n"
  1074. unless $::_q > 1;
  1075. $ed = Git::SVN::Fetcher->new($self);
  1076. $self->ra->gs_do_update($rev, $rev, $self, $ed)
  1077. or die "SVN connection failed somewhere...\n";
  1078. }
  1079. print STDERR "Successfully followed parent\n" unless $::_q > 1;
  1080. return $self->make_log_entry($rev, [$parent], $ed);
  1081. }
  1082. return undef;
  1083. }
  1084. sub do_fetch {
  1085. my ($self, $paths, $rev) = @_;
  1086. my $ed;
  1087. my ($last_rev, @parents);
  1088. if (my $lc = $self->last_commit) {
  1089. # we can have a branch that was deleted, then re-added
  1090. # under the same name but copied from another path, in
  1091. # which case we'll have multiple parents (we don't
  1092. # want to break the original ref, nor lose copypath info):
  1093. if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
  1094. push @{$log_entry->{parents}}, $lc;
  1095. return $log_entry;
  1096. }
  1097. $ed = Git::SVN::Fetcher->new($self);
  1098. $last_rev = $self->{last_rev};
  1099. $ed->{c} = $lc;
  1100. @parents = ($lc);
  1101. } else {
  1102. $last_rev = $rev;
  1103. if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
  1104. return $log_entry;
  1105. }
  1106. $ed = Git::SVN::Fetcher->new($self);
  1107. }
  1108. unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
  1109. die "SVN connection failed somewhere...\n";
  1110. }
  1111. $self->make_log_entry($rev, \@parents, $ed);
  1112. }
  1113. sub mkemptydirs {
  1114. my ($self, $r) = @_;
  1115. sub scan {
  1116. my ($r, $empty_dirs, $line) = @_;
  1117. if (defined $r && $line =~ /^r(\d+)$/) {
  1118. return 0 if $1 > $r;
  1119. } elsif ($line =~ /^ \+empty_dir: (.+)$/) {
  1120. $empty_dirs->{$1} = 1;
  1121. } elsif ($line =~ /^ \-empty_dir: (.+)$/) {
  1122. my @d = grep {m[^\Q$1\E(/|$)]} (keys %$empty_dirs);
  1123. delete @$empty_dirs{@d};
  1124. }
  1125. 1; # continue
  1126. };
  1127. my %empty_dirs = ();
  1128. my $gz_file = "$self->{dir}/unhandled.log.gz";
  1129. if (-f $gz_file) {
  1130. if (!can_compress()) {
  1131. warn "Compress::Zlib could not be found; ",
  1132. "empty directories in $gz_file will not be read\n";
  1133. } else {
  1134. my $gz = Compress::Zlib::gzopen($gz_file, "rb") or
  1135. die "Unable to open $gz_file: $!\n";
  1136. my $line;
  1137. while ($gz->gzreadline($line) > 0) {
  1138. scan($r, \%empty_dirs, $line) or last;
  1139. }
  1140. $gz->gzclose;
  1141. }
  1142. }
  1143. if (open my $fh, '<', "$self->{dir}/unhandled.log") {
  1144. binmode $fh or croak "binmode: $!";
  1145. while (<$fh>) {
  1146. scan($r, \%empty_dirs, $_) or last;
  1147. }
  1148. close $fh;
  1149. }
  1150. my $strip = qr/\A\Q@{[$self->path]}\E(?:\/|$)/;
  1151. foreach my $d (sort keys %empty_dirs) {
  1152. $d = uri_decode($d);
  1153. $d =~ s/$strip//;
  1154. next unless length($d);
  1155. next if -d $d;
  1156. if (-e $d) {
  1157. warn "$d exists but is not a directory\n";
  1158. } else {
  1159. print "creating empty directory: $d\n";
  1160. mkpath([$d]);
  1161. }
  1162. }
  1163. }
  1164. sub get_untracked {
  1165. my ($self, $ed) = @_;
  1166. my @out;
  1167. my $h = $ed->{empty};
  1168. foreach (sort keys %$h) {
  1169. my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
  1170. push @out, " $act: " . uri_encode($_);
  1171. warn "W: $act: $_\n";
  1172. }
  1173. foreach my $t (qw/dir_prop file_prop/) {
  1174. $h = $ed->{$t} or next;
  1175. foreach my $path (sort keys %$h) {
  1176. my $ppath = $path eq '' ? '.' : $path;
  1177. foreach my $prop (sort keys %{$h->{$path}}) {
  1178. next if $SKIP_PROP{$prop};
  1179. my $v = $h->{$path}->{$prop};
  1180. my $t_ppath_prop = "$t: " .
  1181. uri_encode($ppath) . ' ' .
  1182. uri_encode($prop);
  1183. if (defined $v) {
  1184. push @out, " +$t_ppath_prop " .
  1185. uri_encode($v);
  1186. } else {
  1187. push @out, " -$t_ppath_prop";
  1188. }
  1189. }
  1190. }
  1191. }
  1192. foreach my $t (qw/absent_file absent_directory/) {
  1193. $h = $ed->{$t} or next;
  1194. foreach my $parent (sort keys %$h) {
  1195. foreach my $path (sort @{$h->{$parent}}) {
  1196. push @out, " $t: " .
  1197. uri_encode("$parent/$path");
  1198. warn "W: $t: $parent/$path ",
  1199. "Insufficient permissions?\n";
  1200. }
  1201. }
  1202. }
  1203. \@out;
  1204. }
  1205. # parse_svn_date(DATE)
  1206. # --------------------
  1207. # Given a date (in UTC) from Subversion, return a string in the format
  1208. # "<TZ Offset> <local date/time>" that Git will use.
  1209. #
  1210. # By default the parsed date will be in UTC; if $Git::SVN::_localtime
  1211. # is true we'll convert it to the local timezone instead.
  1212. sub parse_svn_date {
  1213. my $date = shift || return '+0000 1970-01-01 00:00:00';
  1214. my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
  1215. (\d\d)\:(\d\d)\:(\d\d)\.\d*Z$/x) or
  1216. croak "Unable to parse date: $date\n";
  1217. my $parsed_date; # Set next.
  1218. if ($Git::SVN::_localtime) {
  1219. # Translate the Subversion datetime to an epoch time.
  1220. # Begin by switching ourselves to $date's timezone, UTC.
  1221. my $old_env_TZ = $ENV{TZ};
  1222. $ENV{TZ} = 'UTC';
  1223. my $epoch_in_UTC =
  1224. POSIX::strftime('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
  1225. # Determine our local timezone (including DST) at the
  1226. # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the
  1227. # value of TZ, if any, at the time we were run.
  1228. if (defined $Git::SVN::Log::TZ) {
  1229. $ENV{TZ} = $Git::SVN::Log::TZ;
  1230. } else {
  1231. delete $ENV{TZ};
  1232. }
  1233. my $our_TZ = get_tz_offset();
  1234. # This converts $epoch_in_UTC into our local timezone.
  1235. my ($sec, $min, $hour, $mday, $mon, $year,
  1236. $wday, $yday, $isdst) = localtime($epoch_in_UTC);
  1237. $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
  1238. $our_TZ, $year + 1900, $mon + 1,
  1239. $mday, $hour, $min, $sec);
  1240. # Reset us to the timezone in effect when we entered
  1241. # this routine.
  1242. if (defined $old_env_TZ) {
  1243. $ENV{TZ} = $old_env_TZ;
  1244. } else {
  1245. delete $ENV{TZ};
  1246. }
  1247. } else {
  1248. $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
  1249. }
  1250. return $parsed_date;
  1251. }
  1252. sub other_gs {
  1253. my ($self, $new_url, $url,
  1254. $branch_from, $r, $old_ref_id) = @_;
  1255. my $gs = Git::SVN->find_by_url($new_url, $url, $branch_from);
  1256. unless ($gs) {
  1257. my $ref_id = $old_ref_id;
  1258. $ref_id =~ s/\@\d+-*$//;
  1259. $ref_id .= "\@$r";
  1260. # just grow a tail if we're not unique enough :x
  1261. $ref_id .= '-' while find_ref($ref_id);
  1262. my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
  1263. if ($u =~ s#^\Q$url\E(/|$)##) {
  1264. $p = $u;
  1265. $u = $url;
  1266. $repo_id = $self->{repo_id};
  1267. }
  1268. while (1) {
  1269. # It is possible to tag two different subdirectories at
  1270. # the same revision. If the url for an existing ref
  1271. # does not match, we must either find a ref with a
  1272. # matching url or create a new ref by growing a tail.
  1273. $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1);
  1274. my (undef, $max_commit) = $gs->rev_map_max(1);
  1275. last if (!$max_commit);
  1276. my ($url) = ::cmt_metadata($max_commit);
  1277. last if ($url eq $gs->metadata_url);
  1278. $ref_id .= '-';
  1279. }
  1280. print STDERR "Initializing parent: $ref_id\n" unless $::_q > 1;
  1281. }
  1282. $gs
  1283. }
  1284. sub call_authors_prog {
  1285. my ($orig_author) = @_;
  1286. $orig_author = command_oneline('rev-parse', '--sq-quote', $orig_author);
  1287. my $author = `$::_authors_prog $orig_author`;
  1288. if ($? != 0) {
  1289. die "$::_authors_prog failed with exit code $?\n"
  1290. }
  1291. if ($author =~ /^\s*(.+?)\s*<(.*)>\s*$/) {
  1292. my ($name, $email) = ($1, $2);
  1293. $email = undef if length $2 == 0;
  1294. return [$name, $email];
  1295. } else {
  1296. die "Author: $orig_author: $::_authors_prog returned "
  1297. . "invalid author format: $author\n";
  1298. }
  1299. }
  1300. sub check_author {
  1301. my ($author) = @_;
  1302. if (!defined $author || length $author == 0) {
  1303. $author = '(no author)';
  1304. }
  1305. if (!defined $::users{$author}) {
  1306. if (defined $::_authors_prog) {
  1307. $::users{$author} = call_authors_prog($author);
  1308. } elsif (defined $::_authors) {
  1309. die "Author: $author not defined in $::_authors file\n";
  1310. }
  1311. }
  1312. $author;
  1313. }
  1314. sub find_extra_svk_parents {
  1315. my ($self, $ed, $tickets, $parents) = @_;
  1316. # aha! svk:merge property changed...
  1317. my @tickets = split "\n", $tickets;
  1318. my @known_parents;
  1319. for my $ticket ( @tickets ) {
  1320. my ($uuid, $path, $rev) = split /:/, $ticket;
  1321. if ( $uuid eq $self->ra_uuid ) {
  1322. my $repos_root = $self->url;
  1323. my $branch_from = $path;
  1324. $branch_from =~ s{^/}{};
  1325. my $gs = $self->other_gs(add_path_to_url( $repos_root, $branch_from ),
  1326. $repos_root,
  1327. $branch_from,
  1328. $rev,
  1329. $self->{ref_id});
  1330. if ( my $commit = $gs->rev_map_get($rev, $uuid) ) {
  1331. # wahey! we found it, but it might be
  1332. # an old one (!)
  1333. push @known_parents, [ $rev, $commit ];
  1334. }
  1335. }
  1336. }
  1337. # Ordering matters; highest-numbered commit merge tickets
  1338. # first, as they may account for later merge ticket additions
  1339. # or changes.
  1340. @known_parents = map {$_->[1]} sort {$b->[0] <=> $a->[0]} @known_parents;
  1341. for my $parent ( @known_parents ) {
  1342. my @cmd = ('rev-list', $parent, map { "^$_" } @$parents );
  1343. my ($msg_fh, $ctx) = command_output_pipe(@cmd);
  1344. my $new;
  1345. while ( <$msg_fh> ) {
  1346. $new=1;last;
  1347. }
  1348. command_close_pipe($msg_fh, $ctx);
  1349. if ( $new ) {
  1350. print STDERR
  1351. "Found merge parent (svk:merge ticket): $parent\n";
  1352. push @$parents, $parent;
  1353. }
  1354. }
  1355. }
  1356. sub lookup_svn_merge {
  1357. my $uuid = shift;
  1358. my $url = shift;
  1359. my $merge = shift;
  1360. my ($source, $revs) = split ":", $merge;
  1361. my $path = $source;
  1362. $path =~ s{^/}{};
  1363. my $gs = Git::SVN->find_by_url($url.$source, $url, $path);
  1364. if ( !$gs ) {
  1365. warn "Couldn't find revmap for $url$source\n";
  1366. return;
  1367. }
  1368. my @ranges = split ",", $revs;
  1369. my ($tip, $tip_commit);
  1370. my @merged_commit_ranges;
  1371. # find the tip
  1372. for my $range ( @ranges ) {
  1373. my ($bottom, $top) = split "-", $range;
  1374. $top ||= $bottom;
  1375. my $bottom_commit = $gs->find_rev_after( $bottom, 1, $top );
  1376. my $top_commit = $gs->find_rev_before( $top, 1, $bottom );
  1377. unless ($top_commit and $bottom_commit) {
  1378. warn "W:unknown path/rev in svn:mergeinfo "
  1379. ."dirprop: $source:$range\n";
  1380. next;
  1381. }
  1382. if (scalar(command('rev-parse', "$bottom_commit^@"))) {
  1383. push @merged_commit_ranges,
  1384. "$bottom_commit^..$top_commit";
  1385. } else {
  1386. push @merged_commit_ranges, "$top_commit";
  1387. }
  1388. if ( !defined $tip or $top > $tip ) {
  1389. $tip = $top;
  1390. $tip_commit = $top_commit;
  1391. }
  1392. }
  1393. return ($tip_commit, @merged_commit_ranges);
  1394. }
  1395. sub _rev_list {
  1396. my ($msg_fh, $ctx) = command_output_pipe(
  1397. "rev-list", @_,
  1398. );
  1399. my @rv;
  1400. while ( <$msg_fh> ) {
  1401. chomp;
  1402. push @rv, $_;
  1403. }
  1404. command_close_pipe($msg_fh, $ctx);
  1405. @rv;
  1406. }
  1407. sub check_cherry_pick {
  1408. my $base = shift;
  1409. my $tip = shift;
  1410. my $parents = shift;
  1411. my @ranges = @_;
  1412. my %commits = map { $_ => 1 }
  1413. _rev_list("--no-merges", $tip, "--not", $base, @$parents, "--");
  1414. for my $range ( @ranges ) {
  1415. delete @commits{_rev_list($range, "--")};
  1416. }
  1417. for my $commit (keys %commits) {
  1418. if (has_no_changes($commit)) {
  1419. delete $commits{$commit};
  1420. }
  1421. }
  1422. return (keys %commits);
  1423. }
  1424. sub has_no_changes {
  1425. my $commit = shift;
  1426. my @revs = split / /, command_oneline(
  1427. qw(rev-list --parents -1 -m), $commit);
  1428. # Commits with no parents, e.g. the start of a partial branch,
  1429. # have changes by definition.
  1430. return 1 if (@revs < 2);
  1431. # Commits with multiple parents, e.g a merge, have no changes
  1432. # by definition.
  1433. return 0 if (@revs > 2);
  1434. return (command_oneline("rev-parse", "$commit^{tree}") eq
  1435. command_oneline("rev-parse", "$commit~1^{tree}"));
  1436. }
  1437. sub tie_for_persistent_memoization {
  1438. my $hash = shift;
  1439. my $path = shift;
  1440. if ($can_use_yaml) {
  1441. tie %$hash => 'Git::SVN::Memoize::YAML', "$path.yaml";
  1442. } else {
  1443. tie %$hash => 'Memoize::Storable', "$path.db", 'nstore';
  1444. }
  1445. }
  1446. # The GIT_DIR environment variable is not always set until after the command
  1447. # line arguments are processed, so we can't memoize in a BEGIN block.
  1448. {
  1449. my $memoized = 0;
  1450. sub memoize_svn_mergeinfo_functions {
  1451. return if $memoized;
  1452. $memoized = 1;
  1453. my $cache_path = "$ENV{GIT_DIR}/svn/.caches/";
  1454. mkpath([$cache_path]) unless -d $cache_path;
  1455. my %lookup_svn_merge_cache;
  1456. my %check_cherry_pick_cache;
  1457. my %has_no_changes_cache;
  1458. tie_for_persistent_memoization(\%lookup_svn_merge_cache,
  1459. "$cache_path/lookup_svn_merge");
  1460. memoize 'lookup_svn_merge',
  1461. SCALAR_CACHE => 'FAULT',
  1462. LIST_CACHE => ['HASH' => \%lookup_svn_merge_cache],
  1463. ;
  1464. tie_for_persistent_memoization(\%check_cherry_pick_cache,
  1465. "$cache_path/check_cherry_pick");
  1466. memoize 'check_cherry_pick',
  1467. SCALAR_CACHE => 'FAULT',
  1468. LIST_CACHE => ['HASH' => \%check_cherry_pick_cache],
  1469. ;
  1470. tie_for_persistent_memoization(\%has_no_changes_cache,
  1471. "$cache_path/has_no_changes");
  1472. memoize 'has_no_changes',
  1473. SCALAR_CACHE => ['HASH' => \%has_no_changes_cache],
  1474. LIST_CACHE => 'FAULT',
  1475. ;
  1476. }
  1477. sub unmemoize_svn_mergeinfo_functions {
  1478. return if not $memoized;
  1479. $memoized = 0;
  1480. Memoize::unmemoize 'lookup_svn_merge';
  1481. Memoize::unmemoize 'check_cherry_pick';
  1482. Memoize::unmemoize 'has_no_changes';
  1483. }
  1484. sub clear_memoized_mergeinfo_caches {
  1485. die "Only call this method in non-memoized context" if ($memoized);
  1486. my $cache_path = "$ENV{GIT_DIR}/svn/.caches/";
  1487. return unless -d $cache_path;
  1488. for my $cache_file (("$cache_path/lookup_svn_merge",
  1489. "$cache_path/check_cherry_pick",
  1490. "$cache_path/has_no_changes")) {
  1491. for my $suffix (qw(yaml db)) {
  1492. my $file = "$cache_file.$suffix";
  1493. next unless -e $file;
  1494. unlink($file) or die "unlink($file) failed: $!\n";
  1495. }
  1496. }
  1497. }
  1498. Memoize::memoize 'Git::SVN::repos_root';
  1499. }
  1500. END {
  1501. # Force cache writeout explicitly instead of waiting for
  1502. # global destruction to avoid segfault in Storable:
  1503. # http://rt.cpan.org/Public/Bug/Display.html?id=36087
  1504. unmemoize_svn_mergeinfo_functions();
  1505. }
  1506. sub parents_exclude {
  1507. my $parents = shift;
  1508. my @commits = @_;
  1509. return unless @commits;
  1510. my @excluded;
  1511. my $excluded;
  1512. do {
  1513. my @cmd = ('rev-list', "-1", @commits, "--not", @$parents );
  1514. $excluded = command_oneline(@cmd);
  1515. if ( $excluded ) {
  1516. my @new;
  1517. my $found;
  1518. for my $commit ( @commits ) {
  1519. if ( $commit eq $excluded ) {
  1520. push @excluded, $commit;
  1521. $found++;
  1522. }
  1523. else {
  1524. push @new, $commit;
  1525. }
  1526. }
  1527. die "saw commit '$excluded' in rev-list output, "
  1528. ."but we didn't ask for that commit (wanted: @commits --not @$parents)"
  1529. unless $found;
  1530. @commits = @new;
  1531. }
  1532. }
  1533. while ($excluded and @commits);
  1534. return @excluded;
  1535. }
  1536. # note: this function should only be called if the various dirprops
  1537. # have actually changed
  1538. sub find_extra_svn_parents {
  1539. my ($self, $ed, $mergeinfo, $parents) = @_;
  1540. # aha! svk:merge property changed...
  1541. memoize_svn_mergeinfo_functions();
  1542. # We first search for merged tips which are not in our
  1543. # history. Then, we figure out which git revisions are in
  1544. # that tip, but not this revision. If all of those revisions
  1545. # are now marked as merge, we can add the tip as a parent.
  1546. my @merges = split "\n", $mergeinfo;
  1547. my @merge_tips;
  1548. my $url = $self->url;
  1549. my $uuid = $self->ra_uuid;
  1550. my @all_ranges;
  1551. for my $merge ( @merges ) {
  1552. my ($tip_commit, @ranges) =
  1553. lookup_svn_merge( $uuid, $url, $merge );
  1554. unless (!$tip_commit or
  1555. grep { $_ eq $tip_commit } @$parents ) {
  1556. push @merge_tips, $tip_commit;
  1557. push @all_ranges, @ranges;
  1558. } else {
  1559. push @merge_tips, undef;
  1560. }
  1561. }
  1562. my %excluded = map { $_ => 1 }
  1563. parents_exclude($parents, grep { defined } @merge_tips);
  1564. # check merge tips for new parents
  1565. my @new_parents;
  1566. for my $merge_tip ( @merge_tips ) {
  1567. my $spec = shift @merges;
  1568. next unless $merge_tip and $excluded{$merge_tip};
  1569. # check out 'new' tips
  1570. my $merge_base;
  1571. eval {
  1572. $merge_base = command_oneline(
  1573. "merge-base",
  1574. @$parents, $merge_tip,
  1575. );
  1576. };
  1577. if ($@) {
  1578. die "An error occurred during merge-base"
  1579. unless $@->isa("Git::Error::Command");
  1580. warn "W: Cannot find common ancestor between ".
  1581. "@$parents and $merge_tip. Ignoring merge info.\n";
  1582. next;
  1583. }
  1584. # double check that there are no missing non-merge commits
  1585. my (@incomplete) = check_cherry_pick(
  1586. $merge_base, $merge_tip,
  1587. $parents,
  1588. @all_ranges,
  1589. );
  1590. if ( @incomplete ) {
  1591. warn "W:svn cherry-pick ignored ($spec) - missing "
  1592. .@incomplete." commit(s) (eg $incomplete[0])\n";
  1593. } else {
  1594. warn
  1595. "Found merge parent (svn:mergeinfo prop): ",
  1596. $merge_tip, "\n";
  1597. push @new_parents, $merge_tip;
  1598. }
  1599. }
  1600. # cater for merges which merge commits from multiple branches
  1601. if ( @new_parents > 1 ) {
  1602. for ( my $i = 0; $i <= $#new_parents; $i++ ) {
  1603. for ( my $j = 0; $j <= $#new_parents; $j++ ) {
  1604. next if $i == $j;
  1605. next unless $new_parents[$i];
  1606. next unless $new_parents[$j];
  1607. my $revs = command_oneline(
  1608. "rev-list", "-1",
  1609. "$new_parents[$i]..$new_parents[$j]",
  1610. );
  1611. if ( !$revs ) {
  1612. undef($new_parents[$j]);
  1613. }
  1614. }
  1615. }
  1616. }
  1617. push @$parents, grep { defined } @new_parents;
  1618. }
  1619. sub make_log_entry {
  1620. my (…

Large files files are truncated, but you can click here to view the full file