PageRenderTime 75ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/git-1.7.11.2/git-svn.perl

#
Perl | 5138 lines | 4569 code | 328 blank | 241 comment | 608 complexity | 6bc0439836ed7722dacda1a501a90ba9 MD5 | raw file
Possible License(s): Apache-2.0, BSD-2-Clause, GPL-2.0, LGPL-2.1

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

  1. #!/usr/bin/perl
  2. # Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
  3. # License: GPL v2 or later
  4. use 5.008;
  5. use warnings;
  6. use strict;
  7. use vars qw/ $AUTHOR $VERSION
  8. $sha1 $sha1_short $_revision $_repository
  9. $_q $_authors $_authors_prog %users/;
  10. $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
  11. $VERSION = '@@GIT_VERSION@@';
  12. # From which subdir have we been invoked?
  13. my $cmd_dir_prefix = eval {
  14. command_oneline([qw/rev-parse --show-prefix/], STDERR => 0)
  15. } || '';
  16. my $git_dir_user_set = 1 if defined $ENV{GIT_DIR};
  17. $ENV{GIT_DIR} ||= '.git';
  18. $Git::SVN::default_repo_id = 'svn';
  19. $Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
  20. $Git::SVN::Ra::_log_window_size = 100;
  21. $Git::SVN::_minimize_url = 'unset';
  22. if (! exists $ENV{SVN_SSH} && exists $ENV{GIT_SSH}) {
  23. $ENV{SVN_SSH} = $ENV{GIT_SSH};
  24. }
  25. if (exists $ENV{SVN_SSH} && $^O eq 'msys') {
  26. $ENV{SVN_SSH} =~ s/\\/\\\\/g;
  27. $ENV{SVN_SSH} =~ s/(.*)/"$1"/;
  28. }
  29. $Git::SVN::Log::TZ = $ENV{TZ};
  30. $ENV{TZ} = 'UTC';
  31. $| = 1; # unbuffer STDOUT
  32. sub fatal (@) { print STDERR "@_\n"; exit 1 }
  33. # All SVN commands do it. Otherwise we may die on SIGPIPE when the remote
  34. # repository decides to close the connection which we expect to be kept alive.
  35. $SIG{PIPE} = 'IGNORE';
  36. # Given a dot separated version number, "subtract" it from
  37. # the SVN::Core::VERSION; non-negaitive return means the SVN::Core
  38. # is at least at the version the caller asked for.
  39. sub compare_svn_version {
  40. my (@ours) = split(/\./, $SVN::Core::VERSION);
  41. my (@theirs) = split(/\./, $_[0]);
  42. my ($i, $diff);
  43. for ($i = 0; $i < @ours && $i < @theirs; $i++) {
  44. $diff = $ours[$i] - $theirs[$i];
  45. return $diff if ($diff);
  46. }
  47. return 1 if ($i < @ours);
  48. return -1 if ($i < @theirs);
  49. return 0;
  50. }
  51. sub _req_svn {
  52. require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
  53. require SVN::Ra;
  54. require SVN::Delta;
  55. if (::compare_svn_version('1.1.0') < 0) {
  56. fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
  57. }
  58. }
  59. my $can_compress = eval { require Compress::Zlib; 1};
  60. use Carp qw/croak/;
  61. use Digest::MD5;
  62. use IO::File qw//;
  63. use File::Basename qw/dirname basename/;
  64. use File::Path qw/mkpath/;
  65. use File::Spec;
  66. use File::Find;
  67. use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
  68. use IPC::Open3;
  69. use Git;
  70. use Git::SVN::Editor qw//;
  71. use Git::SVN::Fetcher qw//;
  72. use Git::SVN::Ra qw//;
  73. use Git::SVN::Prompt qw//;
  74. use Memoize; # core since 5.8.0, Jul 2002
  75. BEGIN {
  76. # import functions from Git into our packages, en masse
  77. no strict 'refs';
  78. foreach (qw/command command_oneline command_noisy command_output_pipe
  79. command_input_pipe command_close_pipe
  80. command_bidi_pipe command_close_bidi_pipe/) {
  81. for my $package ( qw(Git::SVN::Migration Git::SVN::Log Git::SVN),
  82. __PACKAGE__) {
  83. *{"${package}::$_"} = \&{"Git::$_"};
  84. }
  85. }
  86. Memoize::memoize 'Git::config';
  87. Memoize::memoize 'Git::config_bool';
  88. }
  89. my ($SVN);
  90. $sha1 = qr/[a-f\d]{40}/;
  91. $sha1_short = qr/[a-f\d]{4,40}/;
  92. my ($_stdin, $_help, $_edit,
  93. $_message, $_file, $_branch_dest,
  94. $_template, $_shared,
  95. $_version, $_fetch_all, $_no_rebase, $_fetch_parent,
  96. $_merge, $_strategy, $_preserve_merges, $_dry_run, $_local,
  97. $_prefix, $_no_checkout, $_url, $_verbose,
  98. $_git_format, $_commit_url, $_tag, $_merge_info, $_interactive);
  99. $Git::SVN::_follow_parent = 1;
  100. $Git::SVN::Fetcher::_placeholder_filename = ".gitignore";
  101. $_q ||= 0;
  102. my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
  103. 'config-dir=s' => \$Git::SVN::Ra::config_dir,
  104. 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
  105. 'ignore-paths=s' => \$Git::SVN::Fetcher::_ignore_regex,
  106. 'ignore-refs=s' => \$Git::SVN::Ra::_ignore_refs_regex );
  107. my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
  108. 'authors-file|A=s' => \$_authors,
  109. 'authors-prog=s' => \$_authors_prog,
  110. 'repack:i' => \$Git::SVN::_repack,
  111. 'noMetadata' => \$Git::SVN::_no_metadata,
  112. 'useSvmProps' => \$Git::SVN::_use_svm_props,
  113. 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props,
  114. 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size,
  115. 'no-checkout' => \$_no_checkout,
  116. 'quiet|q+' => \$_q,
  117. 'repack-flags|repack-args|repack-opts=s' =>
  118. \$Git::SVN::_repack_flags,
  119. 'use-log-author' => \$Git::SVN::_use_log_author,
  120. 'add-author-from' => \$Git::SVN::_add_author_from,
  121. 'localtime' => \$Git::SVN::_localtime,
  122. %remote_opts );
  123. my ($_trunk, @_tags, @_branches, $_stdlayout);
  124. my %icv;
  125. my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared,
  126. 'trunk|T=s' => \$_trunk, 'tags|t=s@' => \@_tags,
  127. 'branches|b=s@' => \@_branches, 'prefix=s' => \$_prefix,
  128. 'stdlayout|s' => \$_stdlayout,
  129. 'minimize-url|m!' => \$Git::SVN::_minimize_url,
  130. 'no-metadata' => sub { $icv{noMetadata} = 1 },
  131. 'use-svm-props' => sub { $icv{useSvmProps} = 1 },
  132. 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 },
  133. 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] },
  134. 'rewrite-uuid=s' => sub { $icv{rewriteUUID} = $_[1] },
  135. %remote_opts );
  136. my %cmt_opts = ( 'edit|e' => \$_edit,
  137. 'rmdir' => \$Git::SVN::Editor::_rmdir,
  138. 'find-copies-harder' => \$Git::SVN::Editor::_find_copies_harder,
  139. 'l=i' => \$Git::SVN::Editor::_rename_limit,
  140. 'copy-similarity|C=i'=> \$Git::SVN::Editor::_cp_similarity
  141. );
  142. my %cmd = (
  143. fetch => [ \&cmd_fetch, "Download new revisions from SVN",
  144. { 'revision|r=s' => \$_revision,
  145. 'fetch-all|all' => \$_fetch_all,
  146. 'parent|p' => \$_fetch_parent,
  147. %fc_opts } ],
  148. clone => [ \&cmd_clone, "Initialize and fetch revisions",
  149. { 'revision|r=s' => \$_revision,
  150. 'preserve-empty-dirs' =>
  151. \$Git::SVN::Fetcher::_preserve_empty_dirs,
  152. 'placeholder-filename=s' =>
  153. \$Git::SVN::Fetcher::_placeholder_filename,
  154. %fc_opts, %init_opts } ],
  155. init => [ \&cmd_init, "Initialize a repo for tracking" .
  156. " (requires URL argument)",
  157. \%init_opts ],
  158. 'multi-init' => [ \&cmd_multi_init,
  159. "Deprecated alias for ".
  160. "'$0 init -T<trunk> -b<branches> -t<tags>'",
  161. \%init_opts ],
  162. dcommit => [ \&cmd_dcommit,
  163. 'Commit several diffs to merge with upstream',
  164. { 'merge|m|M' => \$_merge,
  165. 'strategy|s=s' => \$_strategy,
  166. 'verbose|v' => \$_verbose,
  167. 'dry-run|n' => \$_dry_run,
  168. 'fetch-all|all' => \$_fetch_all,
  169. 'commit-url=s' => \$_commit_url,
  170. 'revision|r=i' => \$_revision,
  171. 'no-rebase' => \$_no_rebase,
  172. 'mergeinfo=s' => \$_merge_info,
  173. 'interactive|i' => \$_interactive,
  174. %cmt_opts, %fc_opts } ],
  175. branch => [ \&cmd_branch,
  176. 'Create a branch in the SVN repository',
  177. { 'message|m=s' => \$_message,
  178. 'destination|d=s' => \$_branch_dest,
  179. 'dry-run|n' => \$_dry_run,
  180. 'tag|t' => \$_tag,
  181. 'username=s' => \$Git::SVN::Prompt::_username,
  182. 'commit-url=s' => \$_commit_url } ],
  183. tag => [ sub { $_tag = 1; cmd_branch(@_) },
  184. 'Create a tag in the SVN repository',
  185. { 'message|m=s' => \$_message,
  186. 'destination|d=s' => \$_branch_dest,
  187. 'dry-run|n' => \$_dry_run,
  188. 'username=s' => \$Git::SVN::Prompt::_username,
  189. 'commit-url=s' => \$_commit_url } ],
  190. 'set-tree' => [ \&cmd_set_tree,
  191. "Set an SVN repository to a git tree-ish",
  192. { 'stdin' => \$_stdin, %cmt_opts, %fc_opts, } ],
  193. 'create-ignore' => [ \&cmd_create_ignore,
  194. 'Create a .gitignore per svn:ignore',
  195. { 'revision|r=i' => \$_revision
  196. } ],
  197. 'mkdirs' => [ \&cmd_mkdirs ,
  198. "recreate empty directories after a checkout",
  199. { 'revision|r=i' => \$_revision } ],
  200. 'propget' => [ \&cmd_propget,
  201. 'Print the value of a property on a file or directory',
  202. { 'revision|r=i' => \$_revision } ],
  203. 'proplist' => [ \&cmd_proplist,
  204. 'List all properties of a file or directory',
  205. { 'revision|r=i' => \$_revision } ],
  206. 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
  207. { 'revision|r=i' => \$_revision
  208. } ],
  209. 'show-externals' => [ \&cmd_show_externals, "Show svn:externals listings",
  210. { 'revision|r=i' => \$_revision
  211. } ],
  212. 'multi-fetch' => [ \&cmd_multi_fetch,
  213. "Deprecated alias for $0 fetch --all",
  214. { 'revision|r=s' => \$_revision, %fc_opts } ],
  215. 'migrate' => [ sub { },
  216. # no-op, we automatically run this anyways,
  217. 'Migrate configuration/metadata/layout from
  218. previous versions of git-svn',
  219. { 'minimize' => \$Git::SVN::Migration::_minimize,
  220. %remote_opts } ],
  221. 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
  222. { 'limit=i' => \$Git::SVN::Log::limit,
  223. 'revision|r=s' => \$_revision,
  224. 'verbose|v' => \$Git::SVN::Log::verbose,
  225. 'incremental' => \$Git::SVN::Log::incremental,
  226. 'oneline' => \$Git::SVN::Log::oneline,
  227. 'show-commit' => \$Git::SVN::Log::show_commit,
  228. 'non-recursive' => \$Git::SVN::Log::non_recursive,
  229. 'authors-file|A=s' => \$_authors,
  230. 'color' => \$Git::SVN::Log::color,
  231. 'pager=s' => \$Git::SVN::Log::pager
  232. } ],
  233. 'find-rev' => [ \&cmd_find_rev,
  234. "Translate between SVN revision numbers and tree-ish",
  235. {} ],
  236. 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
  237. { 'merge|m|M' => \$_merge,
  238. 'verbose|v' => \$_verbose,
  239. 'strategy|s=s' => \$_strategy,
  240. 'local|l' => \$_local,
  241. 'fetch-all|all' => \$_fetch_all,
  242. 'dry-run|n' => \$_dry_run,
  243. 'preserve-merges|p' => \$_preserve_merges,
  244. %fc_opts } ],
  245. 'commit-diff' => [ \&cmd_commit_diff,
  246. 'Commit a diff between two trees',
  247. { 'message|m=s' => \$_message,
  248. 'file|F=s' => \$_file,
  249. 'revision|r=s' => \$_revision,
  250. %cmt_opts } ],
  251. 'info' => [ \&cmd_info,
  252. "Show info about the latest SVN revision
  253. on the current branch",
  254. { 'url' => \$_url, } ],
  255. 'blame' => [ \&Git::SVN::Log::cmd_blame,
  256. "Show what revision and author last modified each line of a file",
  257. { 'git-format' => \$_git_format } ],
  258. 'reset' => [ \&cmd_reset,
  259. "Undo fetches back to the specified SVN revision",
  260. { 'revision|r=s' => \$_revision,
  261. 'parent|p' => \$_fetch_parent } ],
  262. 'gc' => [ \&cmd_gc,
  263. "Compress unhandled.log files in .git/svn and remove " .
  264. "index files in .git/svn",
  265. {} ],
  266. );
  267. use Term::ReadLine;
  268. package FakeTerm;
  269. sub new {
  270. my ($class, $reason) = @_;
  271. return bless \$reason, shift;
  272. }
  273. sub readline {
  274. my $self = shift;
  275. die "Cannot use readline on FakeTerm: $$self";
  276. }
  277. package main;
  278. my $term = eval {
  279. $ENV{"GIT_SVN_NOTTY"}
  280. ? new Term::ReadLine 'git-svn', \*STDIN, \*STDOUT
  281. : new Term::ReadLine 'git-svn';
  282. };
  283. if ($@) {
  284. $term = new FakeTerm "$@: going non-interactive";
  285. }
  286. my $cmd;
  287. for (my $i = 0; $i < @ARGV; $i++) {
  288. if (defined $cmd{$ARGV[$i]}) {
  289. $cmd = $ARGV[$i];
  290. splice @ARGV, $i, 1;
  291. last;
  292. } elsif ($ARGV[$i] eq 'help') {
  293. $cmd = $ARGV[$i+1];
  294. usage(0);
  295. }
  296. };
  297. # make sure we're always running at the top-level working directory
  298. unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
  299. unless (-d $ENV{GIT_DIR}) {
  300. if ($git_dir_user_set) {
  301. die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
  302. "but it is not a directory\n";
  303. }
  304. my $git_dir = delete $ENV{GIT_DIR};
  305. my $cdup = undef;
  306. git_cmd_try {
  307. $cdup = command_oneline(qw/rev-parse --show-cdup/);
  308. $git_dir = '.' unless ($cdup);
  309. chomp $cdup if ($cdup);
  310. $cdup = "." unless ($cdup && length $cdup);
  311. } "Already at toplevel, but $git_dir not found\n";
  312. chdir $cdup or die "Unable to chdir up to '$cdup'\n";
  313. unless (-d $git_dir) {
  314. die "$git_dir still not found after going to ",
  315. "'$cdup'\n";
  316. }
  317. $ENV{GIT_DIR} = $git_dir;
  318. }
  319. $_repository = Git->repository(Repository => $ENV{GIT_DIR});
  320. }
  321. my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
  322. read_git_config(\%opts);
  323. if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
  324. Getopt::Long::Configure('pass_through');
  325. }
  326. my $rv = GetOptions(%opts, 'h|H' => \$_help, 'version|V' => \$_version,
  327. 'minimize-connections' => \$Git::SVN::Migration::_minimize,
  328. 'id|i=s' => \$Git::SVN::default_ref_id,
  329. 'svn-remote|remote|R=s' => sub {
  330. $Git::SVN::no_reuse_existing = 1;
  331. $Git::SVN::default_repo_id = $_[1] });
  332. exit 1 if (!$rv && $cmd && $cmd ne 'log');
  333. usage(0) if $_help;
  334. version() if $_version;
  335. usage(1) unless defined $cmd;
  336. load_authors() if $_authors;
  337. if (defined $_authors_prog) {
  338. $_authors_prog = "'" . File::Spec->rel2abs($_authors_prog) . "'";
  339. }
  340. unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
  341. Git::SVN::Migration::migration_check();
  342. }
  343. Git::SVN::init_vars();
  344. eval {
  345. Git::SVN::verify_remotes_sanity();
  346. $cmd{$cmd}->[0]->(@ARGV);
  347. };
  348. fatal $@ if $@;
  349. post_fetch_checkout();
  350. exit 0;
  351. ####################### primary functions ######################
  352. sub usage {
  353. my $exit = shift || 0;
  354. my $fd = $exit ? \*STDERR : \*STDOUT;
  355. print $fd <<"";
  356. git-svn - bidirectional operations between a single Subversion tree and git
  357. Usage: git svn <command> [options] [arguments]\n
  358. print $fd "Available commands:\n" unless $cmd;
  359. foreach (sort keys %cmd) {
  360. next if $cmd && $cmd ne $_;
  361. next if /^multi-/; # don't show deprecated commands
  362. print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
  363. foreach (sort keys %{$cmd{$_}->[2]}) {
  364. # mixed-case options are for .git/config only
  365. next if /[A-Z]/ && /^[a-z]+$/i;
  366. # prints out arguments as they should be passed:
  367. my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
  368. print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
  369. "--$_" : "-$_" }
  370. split /\|/,$_)," $x\n";
  371. }
  372. }
  373. print $fd <<"";
  374. \nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
  375. arbitrary identifier if you're tracking multiple SVN branches/repositories in
  376. one git repository and want to keep them separate. See git-svn(1) for more
  377. information.
  378. exit $exit;
  379. }
  380. sub version {
  381. ::_req_svn();
  382. print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
  383. exit 0;
  384. }
  385. sub ask {
  386. my ($prompt, %arg) = @_;
  387. my $valid_re = $arg{valid_re};
  388. my $default = $arg{default};
  389. my $resp;
  390. my $i = 0;
  391. if ( !( defined($term->IN)
  392. && defined( fileno($term->IN) )
  393. && defined( $term->OUT )
  394. && defined( fileno($term->OUT) ) ) ){
  395. return defined($default) ? $default : undef;
  396. }
  397. while ($i++ < 10) {
  398. $resp = $term->readline($prompt);
  399. if (!defined $resp) { # EOF
  400. print "\n";
  401. return defined $default ? $default : undef;
  402. }
  403. if ($resp eq '' and defined $default) {
  404. return $default;
  405. }
  406. if (!defined $valid_re or $resp =~ /$valid_re/) {
  407. return $resp;
  408. }
  409. }
  410. return undef;
  411. }
  412. sub do_git_init_db {
  413. unless (-d $ENV{GIT_DIR}) {
  414. my @init_db = ('init');
  415. push @init_db, "--template=$_template" if defined $_template;
  416. if (defined $_shared) {
  417. if ($_shared =~ /[a-z]/) {
  418. push @init_db, "--shared=$_shared";
  419. } else {
  420. push @init_db, "--shared";
  421. }
  422. }
  423. command_noisy(@init_db);
  424. $_repository = Git->repository(Repository => ".git");
  425. }
  426. my $set;
  427. my $pfx = "svn-remote.$Git::SVN::default_repo_id";
  428. foreach my $i (keys %icv) {
  429. die "'$set' and '$i' cannot both be set\n" if $set;
  430. next unless defined $icv{$i};
  431. command_noisy('config', "$pfx.$i", $icv{$i});
  432. $set = $i;
  433. }
  434. my $ignore_paths_regex = \$Git::SVN::Fetcher::_ignore_regex;
  435. command_noisy('config', "$pfx.ignore-paths", $$ignore_paths_regex)
  436. if defined $$ignore_paths_regex;
  437. my $ignore_refs_regex = \$Git::SVN::Ra::_ignore_refs_regex;
  438. command_noisy('config', "$pfx.ignore-refs", $$ignore_refs_regex)
  439. if defined $$ignore_refs_regex;
  440. if (defined $Git::SVN::Fetcher::_preserve_empty_dirs) {
  441. my $fname = \$Git::SVN::Fetcher::_placeholder_filename;
  442. command_noisy('config', "$pfx.preserve-empty-dirs", 'true');
  443. command_noisy('config', "$pfx.placeholder-filename", $$fname);
  444. }
  445. }
  446. sub init_subdir {
  447. my $repo_path = shift or return;
  448. mkpath([$repo_path]) unless -d $repo_path;
  449. chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
  450. $ENV{GIT_DIR} = '.git';
  451. $_repository = Git->repository(Repository => $ENV{GIT_DIR});
  452. }
  453. sub cmd_clone {
  454. my ($url, $path) = @_;
  455. if (!defined $path &&
  456. (defined $_trunk || @_branches || @_tags ||
  457. defined $_stdlayout) &&
  458. $url !~ m#^[a-z\+]+://#) {
  459. $path = $url;
  460. }
  461. $path = basename($url) if !defined $path || !length $path;
  462. my $authors_absolute = $_authors ? File::Spec->rel2abs($_authors) : "";
  463. cmd_init($url, $path);
  464. command_oneline('config', 'svn.authorsfile', $authors_absolute)
  465. if $_authors;
  466. Git::SVN::fetch_all($Git::SVN::default_repo_id);
  467. }
  468. sub cmd_init {
  469. if (defined $_stdlayout) {
  470. $_trunk = 'trunk' if (!defined $_trunk);
  471. @_tags = 'tags' if (! @_tags);
  472. @_branches = 'branches' if (! @_branches);
  473. }
  474. if (defined $_trunk || @_branches || @_tags) {
  475. return cmd_multi_init(@_);
  476. }
  477. my $url = shift or die "SVN repository location required ",
  478. "as a command-line argument\n";
  479. $url = canonicalize_url($url);
  480. init_subdir(@_);
  481. do_git_init_db();
  482. if ($Git::SVN::_minimize_url eq 'unset') {
  483. $Git::SVN::_minimize_url = 0;
  484. }
  485. Git::SVN->init($url);
  486. }
  487. sub cmd_fetch {
  488. if (grep /^\d+=./, @_) {
  489. die "'<rev>=<commit>' fetch arguments are ",
  490. "no longer supported.\n";
  491. }
  492. my ($remote) = @_;
  493. if (@_ > 1) {
  494. die "Usage: $0 fetch [--all] [--parent] [svn-remote]\n";
  495. }
  496. $Git::SVN::no_reuse_existing = undef;
  497. if ($_fetch_parent) {
  498. my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
  499. unless ($gs) {
  500. die "Unable to determine upstream SVN information from ",
  501. "working tree history\n";
  502. }
  503. # just fetch, don't checkout.
  504. $_no_checkout = 'true';
  505. $_fetch_all ? $gs->fetch_all : $gs->fetch;
  506. } elsif ($_fetch_all) {
  507. cmd_multi_fetch();
  508. } else {
  509. $remote ||= $Git::SVN::default_repo_id;
  510. Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
  511. }
  512. }
  513. sub cmd_set_tree {
  514. my (@commits) = @_;
  515. if ($_stdin || !@commits) {
  516. print "Reading from stdin...\n";
  517. @commits = ();
  518. while (<STDIN>) {
  519. if (/\b($sha1_short)\b/o) {
  520. unshift @commits, $1;
  521. }
  522. }
  523. }
  524. my @revs;
  525. foreach my $c (@commits) {
  526. my @tmp = command('rev-parse',$c);
  527. if (scalar @tmp == 1) {
  528. push @revs, $tmp[0];
  529. } elsif (scalar @tmp > 1) {
  530. push @revs, reverse(command('rev-list',@tmp));
  531. } else {
  532. fatal "Failed to rev-parse $c";
  533. }
  534. }
  535. my $gs = Git::SVN->new;
  536. my ($r_last, $cmt_last) = $gs->last_rev_commit;
  537. $gs->fetch;
  538. if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
  539. fatal "There are new revisions that were fetched ",
  540. "and need to be merged (or acknowledged) ",
  541. "before committing.\nlast rev: $r_last\n",
  542. " current: $gs->{last_rev}";
  543. }
  544. $gs->set_tree($_) foreach @revs;
  545. print "Done committing ",scalar @revs," revisions to SVN\n";
  546. unlink $gs->{index};
  547. }
  548. sub split_merge_info_range {
  549. my ($range) = @_;
  550. if ($range =~ /(\d+)-(\d+)/) {
  551. return (int($1), int($2));
  552. } else {
  553. return (int($range), int($range));
  554. }
  555. }
  556. sub combine_ranges {
  557. my ($in) = @_;
  558. my @fnums = ();
  559. my @arr = split(/,/, $in);
  560. for my $element (@arr) {
  561. my ($start, $end) = split_merge_info_range($element);
  562. push @fnums, $start;
  563. }
  564. my @sorted = @arr [ sort {
  565. $fnums[$a] <=> $fnums[$b]
  566. } 0..$#arr ];
  567. my @return = ();
  568. my $last = -1;
  569. my $first = -1;
  570. for my $element (@sorted) {
  571. my ($start, $end) = split_merge_info_range($element);
  572. if ($last == -1) {
  573. $first = $start;
  574. $last = $end;
  575. next;
  576. }
  577. if ($start <= $last+1) {
  578. if ($end > $last) {
  579. $last = $end;
  580. }
  581. next;
  582. }
  583. if ($first == $last) {
  584. push @return, "$first";
  585. } else {
  586. push @return, "$first-$last";
  587. }
  588. $first = $start;
  589. $last = $end;
  590. }
  591. if ($first != -1) {
  592. if ($first == $last) {
  593. push @return, "$first";
  594. } else {
  595. push @return, "$first-$last";
  596. }
  597. }
  598. return join(',', @return);
  599. }
  600. sub merge_revs_into_hash {
  601. my ($hash, $minfo) = @_;
  602. my @lines = split(' ', $minfo);
  603. for my $line (@lines) {
  604. my ($branchpath, $revs) = split(/:/, $line);
  605. if (exists($hash->{$branchpath})) {
  606. # Merge the two revision sets
  607. my $combined = "$hash->{$branchpath},$revs";
  608. $hash->{$branchpath} = combine_ranges($combined);
  609. } else {
  610. # Just do range combining for consolidation
  611. $hash->{$branchpath} = combine_ranges($revs);
  612. }
  613. }
  614. }
  615. sub merge_merge_info {
  616. my ($mergeinfo_one, $mergeinfo_two) = @_;
  617. my %result_hash = ();
  618. merge_revs_into_hash(\%result_hash, $mergeinfo_one);
  619. merge_revs_into_hash(\%result_hash, $mergeinfo_two);
  620. my $result = '';
  621. # Sort below is for consistency's sake
  622. for my $branchname (sort keys(%result_hash)) {
  623. my $revlist = $result_hash{$branchname};
  624. $result .= "$branchname:$revlist\n"
  625. }
  626. return $result;
  627. }
  628. sub populate_merge_info {
  629. my ($d, $gs, $uuid, $linear_refs, $rewritten_parent) = @_;
  630. my %parentshash;
  631. read_commit_parents(\%parentshash, $d);
  632. my @parents = @{$parentshash{$d}};
  633. if ($#parents > 0) {
  634. # Merge commit
  635. my $all_parents_ok = 1;
  636. my $aggregate_mergeinfo = '';
  637. my $rooturl = $gs->repos_root;
  638. if (defined($rewritten_parent)) {
  639. # Replace first parent with newly-rewritten version
  640. shift @parents;
  641. unshift @parents, $rewritten_parent;
  642. }
  643. foreach my $parent (@parents) {
  644. my ($branchurl, $svnrev, $paruuid) =
  645. cmt_metadata($parent);
  646. unless (defined($svnrev)) {
  647. # Should have been caught be preflight check
  648. fatal "merge commit $d has ancestor $parent, but that change "
  649. ."does not have git-svn metadata!";
  650. }
  651. unless ($branchurl =~ /^\Q$rooturl\E(.*)/) {
  652. fatal "commit $parent git-svn metadata changed mid-run!";
  653. }
  654. my $branchpath = $1;
  655. my $ra = Git::SVN::Ra->new($branchurl);
  656. my (undef, undef, $props) =
  657. $ra->get_dir(canonicalize_path("."), $svnrev);
  658. my $par_mergeinfo = $props->{'svn:mergeinfo'};
  659. unless (defined $par_mergeinfo) {
  660. $par_mergeinfo = '';
  661. }
  662. # Merge previous mergeinfo values
  663. $aggregate_mergeinfo =
  664. merge_merge_info($aggregate_mergeinfo,
  665. $par_mergeinfo, 0);
  666. next if $parent eq $parents[0]; # Skip first parent
  667. # Add new changes being placed in tree by merge
  668. my @cmd = (qw/rev-list --reverse/,
  669. $parent, qw/--not/);
  670. foreach my $par (@parents) {
  671. unless ($par eq $parent) {
  672. push @cmd, $par;
  673. }
  674. }
  675. my @revsin = ();
  676. my ($revlist, $ctx) = command_output_pipe(@cmd);
  677. while (<$revlist>) {
  678. my $irev = $_;
  679. chomp $irev;
  680. my (undef, $csvnrev, undef) =
  681. cmt_metadata($irev);
  682. unless (defined $csvnrev) {
  683. # A child is missing SVN annotations...
  684. # this might be OK, or might not be.
  685. warn "W:child $irev is merged into revision "
  686. ."$d but does not have git-svn metadata. "
  687. ."This means git-svn cannot determine the "
  688. ."svn revision numbers to place into the "
  689. ."svn:mergeinfo property. You must ensure "
  690. ."a branch is entirely committed to "
  691. ."SVN before merging it in order for "
  692. ."svn:mergeinfo population to function "
  693. ."properly";
  694. }
  695. push @revsin, $csvnrev;
  696. }
  697. command_close_pipe($revlist, $ctx);
  698. last unless $all_parents_ok;
  699. # We now have a list of all SVN revnos which are
  700. # merged by this particular parent. Integrate them.
  701. next if $#revsin == -1;
  702. my $newmergeinfo = "$branchpath:" . join(',', @revsin);
  703. $aggregate_mergeinfo =
  704. merge_merge_info($aggregate_mergeinfo,
  705. $newmergeinfo, 1);
  706. }
  707. if ($all_parents_ok and $aggregate_mergeinfo) {
  708. return $aggregate_mergeinfo;
  709. }
  710. }
  711. return undef;
  712. }
  713. sub cmd_dcommit {
  714. my $head = shift;
  715. command_noisy(qw/update-index --refresh/);
  716. git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
  717. 'Cannot dcommit with a dirty index. Commit your changes first, '
  718. . "or stash them with `git stash'.\n";
  719. $head ||= 'HEAD';
  720. my $old_head;
  721. if ($head ne 'HEAD') {
  722. $old_head = eval {
  723. command_oneline([qw/symbolic-ref -q HEAD/])
  724. };
  725. if ($old_head) {
  726. $old_head =~ s{^refs/heads/}{};
  727. } else {
  728. $old_head = eval { command_oneline(qw/rev-parse HEAD/) };
  729. }
  730. command(['checkout', $head], STDERR => 0);
  731. }
  732. my @refs;
  733. my ($url, $rev, $uuid, $gs) = working_head_info('HEAD', \@refs);
  734. unless ($gs) {
  735. die "Unable to determine upstream SVN information from ",
  736. "$head history.\nPerhaps the repository is empty.";
  737. }
  738. if (defined $_commit_url) {
  739. $url = $_commit_url;
  740. } else {
  741. $url = eval { command_oneline('config', '--get',
  742. "svn-remote.$gs->{repo_id}.commiturl") };
  743. if (!$url) {
  744. $url = $gs->full_pushurl
  745. }
  746. }
  747. my $last_rev = $_revision if defined $_revision;
  748. if ($url) {
  749. print "Committing to $url ...\n";
  750. }
  751. my ($linear_refs, $parents) = linearize_history($gs, \@refs);
  752. if ($_no_rebase && scalar(@$linear_refs) > 1) {
  753. warn "Attempting to commit more than one change while ",
  754. "--no-rebase is enabled.\n",
  755. "If these changes depend on each other, re-running ",
  756. "without --no-rebase may be required."
  757. }
  758. if (defined $_interactive){
  759. my $ask_default = "y";
  760. foreach my $d (@$linear_refs){
  761. my ($fh, $ctx) = command_output_pipe(qw(show --summary), "$d");
  762. while (<$fh>){
  763. print $_;
  764. }
  765. command_close_pipe($fh, $ctx);
  766. $_ = ask("Commit this patch to SVN? ([y]es (default)|[n]o|[q]uit|[a]ll): ",
  767. valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i,
  768. default => $ask_default);
  769. die "Commit this patch reply required" unless defined $_;
  770. if (/^[nq]/i) {
  771. exit(0);
  772. } elsif (/^a/i) {
  773. last;
  774. }
  775. }
  776. }
  777. my $expect_url = $url;
  778. my $push_merge_info = eval {
  779. command_oneline(qw/config --get svn.pushmergeinfo/)
  780. };
  781. if (not defined($push_merge_info)
  782. or $push_merge_info eq "false"
  783. or $push_merge_info eq "no"
  784. or $push_merge_info eq "never") {
  785. $push_merge_info = 0;
  786. }
  787. unless (defined($_merge_info) || ! $push_merge_info) {
  788. # Preflight check of changes to ensure no issues with mergeinfo
  789. # This includes check for uncommitted-to-SVN parents
  790. # (other than the first parent, which we will handle),
  791. # information from different SVN repos, and paths
  792. # which are not underneath this repository root.
  793. my $rooturl = $gs->repos_root;
  794. foreach my $d (@$linear_refs) {
  795. my %parentshash;
  796. read_commit_parents(\%parentshash, $d);
  797. my @realparents = @{$parentshash{$d}};
  798. if ($#realparents > 0) {
  799. # Merge commit
  800. shift @realparents; # Remove/ignore first parent
  801. foreach my $parent (@realparents) {
  802. my ($branchurl, $svnrev, $paruuid) = cmt_metadata($parent);
  803. unless (defined $paruuid) {
  804. # A parent is missing SVN annotations...
  805. # abort the whole operation.
  806. fatal "$parent is merged into revision $d, "
  807. ."but does not have git-svn metadata. "
  808. ."Either dcommit the branch or use a "
  809. ."local cherry-pick, FF merge, or rebase "
  810. ."instead of an explicit merge commit.";
  811. }
  812. unless ($paruuid eq $uuid) {
  813. # Parent has SVN metadata from different repository
  814. fatal "merge parent $parent for change $d has "
  815. ."git-svn uuid $paruuid, while current change "
  816. ."has uuid $uuid!";
  817. }
  818. unless ($branchurl =~ /^\Q$rooturl\E(.*)/) {
  819. # This branch is very strange indeed.
  820. fatal "merge parent $parent for $d is on branch "
  821. ."$branchurl, which is not under the "
  822. ."git-svn root $rooturl!";
  823. }
  824. }
  825. }
  826. }
  827. }
  828. my $rewritten_parent;
  829. Git::SVN::remove_username($expect_url);
  830. if (defined($_merge_info)) {
  831. $_merge_info =~ tr{ }{\n};
  832. }
  833. while (1) {
  834. my $d = shift @$linear_refs or last;
  835. unless (defined $last_rev) {
  836. (undef, $last_rev, undef) = cmt_metadata("$d~1");
  837. unless (defined $last_rev) {
  838. fatal "Unable to extract revision information ",
  839. "from commit $d~1";
  840. }
  841. }
  842. if ($_dry_run) {
  843. print "diff-tree $d~1 $d\n";
  844. } else {
  845. my $cmt_rev;
  846. unless (defined($_merge_info) || ! $push_merge_info) {
  847. $_merge_info = populate_merge_info($d, $gs,
  848. $uuid,
  849. $linear_refs,
  850. $rewritten_parent);
  851. }
  852. my %ed_opts = ( r => $last_rev,
  853. log => get_commit_entry($d)->{log},
  854. ra => Git::SVN::Ra->new($url),
  855. config => SVN::Core::config_get_config(
  856. $Git::SVN::Ra::config_dir
  857. ),
  858. tree_a => "$d~1",
  859. tree_b => $d,
  860. editor_cb => sub {
  861. print "Committed r$_[0]\n";
  862. $cmt_rev = $_[0];
  863. },
  864. mergeinfo => $_merge_info,
  865. svn_path => '');
  866. if (!Git::SVN::Editor->new(\%ed_opts)->apply_diff) {
  867. print "No changes\n$d~1 == $d\n";
  868. } elsif ($parents->{$d} && @{$parents->{$d}}) {
  869. $gs->{inject_parents_dcommit}->{$cmt_rev} =
  870. $parents->{$d};
  871. }
  872. $_fetch_all ? $gs->fetch_all : $gs->fetch;
  873. $last_rev = $cmt_rev;
  874. next if $_no_rebase;
  875. # we always want to rebase against the current HEAD,
  876. # not any head that was passed to us
  877. my @diff = command('diff-tree', $d,
  878. $gs->refname, '--');
  879. my @finish;
  880. if (@diff) {
  881. @finish = rebase_cmd();
  882. print STDERR "W: $d and ", $gs->refname,
  883. " differ, using @finish:\n",
  884. join("\n", @diff), "\n";
  885. } else {
  886. print "No changes between current HEAD and ",
  887. $gs->refname,
  888. "\nResetting to the latest ",
  889. $gs->refname, "\n";
  890. @finish = qw/reset --mixed/;
  891. }
  892. command_noisy(@finish, $gs->refname);
  893. $rewritten_parent = command_oneline(qw/rev-parse HEAD/);
  894. if (@diff) {
  895. @refs = ();
  896. my ($url_, $rev_, $uuid_, $gs_) =
  897. working_head_info('HEAD', \@refs);
  898. my ($linear_refs_, $parents_) =
  899. linearize_history($gs_, \@refs);
  900. if (scalar(@$linear_refs) !=
  901. scalar(@$linear_refs_)) {
  902. fatal "# of revisions changed ",
  903. "\nbefore:\n",
  904. join("\n", @$linear_refs),
  905. "\n\nafter:\n",
  906. join("\n", @$linear_refs_), "\n",
  907. 'If you are attempting to commit ',
  908. "merges, try running:\n\t",
  909. 'git rebase --interactive',
  910. '--preserve-merges ',
  911. $gs->refname,
  912. "\nBefore dcommitting";
  913. }
  914. if ($url_ ne $expect_url) {
  915. if ($url_ eq $gs->metadata_url) {
  916. print
  917. "Accepting rewritten URL:",
  918. " $url_\n";
  919. } else {
  920. fatal
  921. "URL mismatch after rebase:",
  922. " $url_ != $expect_url";
  923. }
  924. }
  925. if ($uuid_ ne $uuid) {
  926. fatal "uuid mismatch after rebase: ",
  927. "$uuid_ != $uuid";
  928. }
  929. # remap parents
  930. my (%p, @l, $i);
  931. for ($i = 0; $i < scalar @$linear_refs; $i++) {
  932. my $new = $linear_refs_->[$i] or next;
  933. $p{$new} =
  934. $parents->{$linear_refs->[$i]};
  935. push @l, $new;
  936. }
  937. $parents = \%p;
  938. $linear_refs = \@l;
  939. }
  940. }
  941. }
  942. if ($old_head) {
  943. my $new_head = command_oneline(qw/rev-parse HEAD/);
  944. my $new_is_symbolic = eval {
  945. command_oneline(qw/symbolic-ref -q HEAD/);
  946. };
  947. if ($new_is_symbolic) {
  948. print "dcommitted the branch ", $head, "\n";
  949. } else {
  950. print "dcommitted on a detached HEAD because you gave ",
  951. "a revision argument.\n",
  952. "The rewritten commit is: ", $new_head, "\n";
  953. }
  954. command(['checkout', $old_head], STDERR => 0);
  955. }
  956. unlink $gs->{index};
  957. }
  958. sub cmd_branch {
  959. my ($branch_name, $head) = @_;
  960. unless (defined $branch_name && length $branch_name) {
  961. die(($_tag ? "tag" : "branch") . " name required\n");
  962. }
  963. $head ||= 'HEAD';
  964. my (undef, $rev, undef, $gs) = working_head_info($head);
  965. my $src = $gs->full_pushurl;
  966. my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
  967. my $allglobs = $remote->{ $_tag ? 'tags' : 'branches' };
  968. my $glob;
  969. if ($#{$allglobs} == 0) {
  970. $glob = $allglobs->[0];
  971. } else {
  972. unless(defined $_branch_dest) {
  973. die "Multiple ",
  974. $_tag ? "tag" : "branch",
  975. " paths defined for Subversion repository.\n",
  976. "You must specify where you want to create the ",
  977. $_tag ? "tag" : "branch",
  978. " with the --destination argument.\n";
  979. }
  980. foreach my $g (@{$allglobs}) {
  981. my $re = Git::SVN::Editor::glob2pat($g->{path}->{left});
  982. if ($_branch_dest =~ /$re/) {
  983. $glob = $g;
  984. last;
  985. }
  986. }
  987. unless (defined $glob) {
  988. my $dest_re = qr/\b\Q$_branch_dest\E\b/;
  989. foreach my $g (@{$allglobs}) {
  990. $g->{path}->{left} =~ /$dest_re/ or next;
  991. if (defined $glob) {
  992. die "Ambiguous destination: ",
  993. $_branch_dest, "\nmatches both '",
  994. $glob->{path}->{left}, "' and '",
  995. $g->{path}->{left}, "'\n";
  996. }
  997. $glob = $g;
  998. }
  999. unless (defined $glob) {
  1000. die "Unknown ",
  1001. $_tag ? "tag" : "branch",
  1002. " destination $_branch_dest\n";
  1003. }
  1004. }
  1005. }
  1006. my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
  1007. my $url;
  1008. if (defined $_commit_url) {
  1009. $url = $_commit_url;
  1010. } else {
  1011. $url = eval { command_oneline('config', '--get',
  1012. "svn-remote.$gs->{repo_id}.commiturl") };
  1013. if (!$url) {
  1014. $url = $remote->{pushurl} || $remote->{url};
  1015. }
  1016. }
  1017. my $dst = join '/', $url, $lft, $branch_name, ($rgt || ());
  1018. if ($dst =~ /^https:/ && $src =~ /^http:/) {
  1019. $src=~s/^http:/https:/;
  1020. }
  1021. ::_req_svn();
  1022. my $ctx = SVN::Client->new(
  1023. auth => Git::SVN::Ra::_auth_providers(),
  1024. log_msg => sub {
  1025. ${ $_[0] } = defined $_message
  1026. ? $_message
  1027. : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
  1028. . $branch_name;
  1029. },
  1030. );
  1031. eval {
  1032. $ctx->ls($dst, 'HEAD', 0);
  1033. } and die "branch ${branch_name} already exists\n";
  1034. print "Copying ${src} at r${rev} to ${dst}...\n";
  1035. $ctx->copy($src, $rev, $dst)
  1036. unless $_dry_run;
  1037. $gs->fetch_all;
  1038. }
  1039. sub cmd_find_rev {
  1040. my $revision_or_hash = shift or die "SVN or git revision required ",
  1041. "as a command-line argument\n";
  1042. my $result;
  1043. if ($revision_or_hash =~ /^r\d+$/) {
  1044. my $head = shift;
  1045. $head ||= 'HEAD';
  1046. my @refs;
  1047. my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
  1048. unless ($gs) {
  1049. die "Unable to determine upstream SVN information from ",
  1050. "$head history\n";
  1051. }
  1052. my $desired_revision = substr($revision_or_hash, 1);
  1053. $result = $gs->rev_map_get($desired_revision, $uuid);
  1054. } else {
  1055. my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
  1056. $result = $rev;
  1057. }
  1058. print "$result\n" if $result;
  1059. }
  1060. sub auto_create_empty_directories {
  1061. my ($gs) = @_;
  1062. my $var = eval { command_oneline('config', '--get', '--bool',
  1063. "svn-remote.$gs->{repo_id}.automkdirs") };
  1064. # By default, create empty directories by consulting the unhandled log,
  1065. # but allow setting it to 'false' to skip it.
  1066. return !($var && $var eq 'false');
  1067. }
  1068. sub cmd_rebase {
  1069. command_noisy(qw/update-index --refresh/);
  1070. my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
  1071. unless ($gs) {
  1072. die "Unable to determine upstream SVN information from ",
  1073. "working tree history\n";
  1074. }
  1075. if ($_dry_run) {
  1076. print "Remote Branch: " . $gs->refname . "\n";
  1077. print "SVN URL: " . $url . "\n";
  1078. return;
  1079. }
  1080. if (command(qw/diff-index HEAD --/)) {
  1081. print STDERR "Cannot rebase with uncommited changes:\n";
  1082. command_noisy('status');
  1083. exit 1;
  1084. }
  1085. unless ($_local) {
  1086. # rebase will checkout for us, so no need to do it explicitly
  1087. $_no_checkout = 'true';
  1088. $_fetch_all ? $gs->fetch_all : $gs->fetch;
  1089. }
  1090. command_noisy(rebase_cmd(), $gs->refname);
  1091. if (auto_create_empty_directories($gs)) {
  1092. $gs->mkemptydirs;
  1093. }
  1094. }
  1095. sub cmd_show_ignore {
  1096. my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
  1097. $gs ||= Git::SVN->new;
  1098. my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
  1099. $gs->prop_walk($gs->{path}, $r, sub {
  1100. my ($gs, $path, $props) = @_;
  1101. print STDOUT "\n# $path\n";
  1102. my $s = $props->{'svn:ignore'} or return;
  1103. $s =~ s/[\r\n]+/\n/g;
  1104. $s =~ s/^\n+//;
  1105. chomp $s;
  1106. $s =~ s#^#$path#gm;
  1107. print STDOUT "$s\n";
  1108. });
  1109. }
  1110. sub cmd_show_externals {
  1111. my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
  1112. $gs ||= Git::SVN->new;
  1113. my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
  1114. $gs->prop_walk($gs->{path}, $r, sub {
  1115. my ($gs, $path, $props) = @_;
  1116. print STDOUT "\n# $path\n";
  1117. my $s = $props->{'svn:externals'} or return;
  1118. $s =~ s/[\r\n]+/\n/g;
  1119. chomp $s;
  1120. $s =~ s#^#$path#gm;
  1121. print STDOUT "$s\n";
  1122. });
  1123. }
  1124. sub cmd_create_ignore {
  1125. my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
  1126. $gs ||= Git::SVN->new;
  1127. my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
  1128. $gs->prop_walk($gs->{path}, $r, sub {
  1129. my ($gs, $path, $props) = @_;
  1130. # $path is of the form /path/to/dir/
  1131. $path = '.' . $path;
  1132. # SVN can have attributes on empty directories,
  1133. # which git won't track
  1134. mkpath([$path]) unless -d $path;
  1135. my $ignore = $path . '.gitignore';
  1136. my $s = $props->{'svn:ignore'} or return;
  1137. open(GITIGNORE, '>', $ignore)
  1138. or fatal("Failed to open `$ignore' for writing: $!");
  1139. $s =~ s/[\r\n]+/\n/g;
  1140. $s =~ s/^\n+//;
  1141. chomp $s;
  1142. # Prefix all patterns so that the ignore doesn't apply
  1143. # to sub-directories.
  1144. $s =~ s#^#/#gm;
  1145. print GITIGNORE "$s\n";
  1146. close(GITIGNORE)
  1147. or fatal("Failed to close `$ignore': $!");
  1148. command_noisy('add', '-f', $ignore);
  1149. });
  1150. }
  1151. sub cmd_mkdirs {
  1152. my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
  1153. $gs ||= Git::SVN->new;
  1154. $gs->mkemptydirs($_revision);
  1155. }
  1156. sub canonicalize_path {
  1157. my ($path) = @_;
  1158. my $dot_slash_added = 0;
  1159. if (substr($path, 0, 1) ne "/") {
  1160. $path = "./" . $path;
  1161. $dot_slash_added = 1;
  1162. }
  1163. # File::Spec->canonpath doesn't collapse x/../y into y (for a
  1164. # good reason), so let's do this manually.
  1165. $path =~ s#/+#/#g;
  1166. $path =~ s#/\.(?:/|$)#/#g;
  1167. $path =~ s#/[^/]+/\.\.##g;
  1168. $path =~ s#/$##g;
  1169. $path =~ s#^\./## if $dot_slash_added;
  1170. $path =~ s#^/##;
  1171. $path =~ s#^\.$##;
  1172. return $path;
  1173. }
  1174. sub canonicalize_url {
  1175. my ($url) = @_;
  1176. $url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
  1177. return $url;
  1178. }
  1179. # get_svnprops(PATH)
  1180. # ------------------
  1181. # Helper for cmd_propget and cmd_proplist below.
  1182. sub get_svnprops {
  1183. my $path = shift;
  1184. my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
  1185. $gs ||= Git::SVN->new;
  1186. # prefix THE PATH by the sub-directory from which the user
  1187. # invoked us.
  1188. $path = $cmd_dir_prefix . $path;
  1189. fatal("No such file or directory: $path") unless -e $path;
  1190. my $is_dir = -d $path ? 1 : 0;
  1191. $path = $gs->{path} . '/' . $path;
  1192. # canonicalize the path (otherwise libsvn will abort or fail to
  1193. # find the file)
  1194. $path = canonicalize_path($path);
  1195. my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
  1196. my $props;
  1197. if ($is_dir) {
  1198. (undef, undef, $props) = $gs->ra->get_dir($path, $r);
  1199. }
  1200. else {
  1201. (undef, $props) = $gs->ra->get_file($path, $r, undef);
  1202. }
  1203. return $props;
  1204. }
  1205. # cmd_propget (PROP, PATH)
  1206. # ------------------------
  1207. # Print the SVN property PROP for PATH.
  1208. sub cmd_propget {
  1209. my ($prop, $path) = @_;
  1210. $path = '.' if not defined $path;
  1211. usage(1) if not defined $prop;
  1212. my $props = get_svnprops($path);
  1213. if (not defined $props->{$prop}) {
  1214. fatal("`$path' does not have a `$prop' SVN property.");
  1215. }
  1216. print $props->{$prop} . "\n";
  1217. }
  1218. # cmd_proplist (PATH)
  1219. # -------------------
  1220. # Print the list of SVN properties for PATH.
  1221. sub cmd_proplist {
  1222. my $path = shift;
  1223. $path = '.' if not defined $path;
  1224. my $props = get_svnprops($path);
  1225. print "Properties on '$path':\n";
  1226. foreach (sort keys %{$props}) {
  1227. print " $_\n";
  1228. }
  1229. }
  1230. sub cmd_multi_init {
  1231. my $url = shift;
  1232. unless (defined $_trunk || @_branches || @_tags) {
  1233. usage(1);
  1234. }
  1235. $_prefix = '' unless defined $_prefix;
  1236. if (defined $url) {
  1237. $url = canonicalize_url($url);
  1238. init_subdir(@_);
  1239. }
  1240. do_git_init_db();
  1241. if (defined $_trunk) {
  1242. $_trunk =~ s#^/+##;
  1243. my $trunk_ref = 'refs/remotes/' . $_prefix . 'trunk';
  1244. # try both old-style and new-style lookups:
  1245. my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
  1246. unless ($gs_trunk) {
  1247. my ($trunk_url, $trunk_path) =
  1248. complete_svn_url($url, $_trunk);
  1249. $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
  1250. undef, $trunk_ref);
  1251. }
  1252. }
  1253. return unless @_branches || @_tags;
  1254. my $ra = $url ? Git::SVN::Ra->new($url) : undef;
  1255. foreach my $path (@_branches) {
  1256. complete_url_ls_init($ra, $path, '--branches/-b', $_prefix);
  1257. }
  1258. foreach my $path (@_tags) {
  1259. complete_url_ls_init($ra, $path, '--tags/-t', $_prefix.'tags/');
  1260. }
  1261. }
  1262. sub cmd_multi_fetch {
  1263. $Git::SVN::no_reuse_existing = undef;
  1264. my $remotes = Git::SVN::read_all_remotes();
  1265. foreach my $repo_id (sort keys %$remotes) {
  1266. if ($remotes->{$repo_id}->{url}) {
  1267. Git::SVN::fetch_all($repo_id, $remotes);
  1268. }
  1269. }
  1270. }
  1271. # this command is special because it requires no metadata
  1272. sub cmd_commit_diff {
  1273. my ($ta, $tb, $url) = @_;
  1274. my $usage = "Usage: $0 commit-diff -r<revision> ".
  1275. "<tree-ish> <tree-ish> [<URL>]";
  1276. fatal($usage) if (!defined $ta || !defined $tb);
  1277. my $svn_path = '';
  1278. if (!defined $url) {
  1279. my $gs = eval { Git::SVN->new };
  1280. if (!$gs) {
  1281. fatal("Needed URL or usable git-svn --id in ",
  1282. "the command-line\n", $usage);
  1283. }
  1284. $url = $gs->{url};
  1285. $svn_path = $gs->{path};
  1286. }
  1287. unless (defined $_revision) {
  1288. fatal("-r|--revision is a required argument\n", $usage);
  1289. }
  1290. if (defined $_message && defined $_file) {
  1291. fatal("Both --message/-m and --file/-F specified ",
  1292. "for the commit message.\n",
  1293. "I have no idea what you mean");
  1294. }
  1295. if (defined $_file) {
  1296. $_message = file_to_s($_file);
  1297. } else {
  1298. $_message ||= get_commit_entry($tb)->{log};
  1299. }
  1300. my $ra ||= Git::SVN::Ra->new($url);
  1301. my $r = $_revision;
  1302. if ($r eq 'HEAD') {
  1303. $r = $ra->get_latest_revnum;
  1304. } elsif ($r !~ /^\d+$/) {
  1305. die "revision argument: $r not understood by git-svn\n";
  1306. }
  1307. my %ed_opts = ( r => $r,
  1308. log => $_message,
  1309. ra => $ra,
  1310. tree_a => $ta,
  1311. tree_b => $tb,
  1312. editor_cb => sub { print "Committed r$_[0]\n" },
  1313. svn_path => $svn_path );
  1314. if (!Git::SVN::Editor->new(\%ed_opts)->apply_diff) {
  1315. print "No changes\n$ta == $tb\n";
  1316. }
  1317. }
  1318. sub escape_uri_only {
  1319. my ($uri) = @_;
  1320. my @tmp;
  1321. foreach (split m{/}, $uri) {
  1322. s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
  1323. push @tmp, $_;
  1324. }
  1325. join('/', @tmp);
  1326. }
  1327. sub escape_url {
  1328. my ($url) = @_;
  1329. if ($url =~ m#^([^:]+)://([^/]*)(.*)$#) {
  1330. my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
  1331. $url = "$scheme://$domain$uri";
  1332. }
  1333. $url;
  1334. }
  1335. sub cmd_info {
  1336. my $path = canonicalize_path(defined($_[0]) ? $_[0] : ".");
  1337. my $fullpath = canonicalize_path($cmd_dir_prefix . $path);
  1338. if (exists $_[1]) {
  1339. die "Too many arguments specified\n";
  1340. }
  1341. my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
  1342. if (!$file_type && !$diff_status) {
  1343. print STDERR "svn: '$path' is not under version control\n";
  1344. exit 1;
  1345. }
  1346. my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
  1347. unless ($gs) {
  1348. die "Unable to determine upstream SVN information from ",
  1349. "working tree history\n";
  1350. }
  1351. # canonicalize_path() will return "" to make libsvn 1.5.x happy,
  1352. $path = "." if $path eq "";
  1353. my $full_url = $url . ($fullpath eq "" ? "" : "/$fullpath");
  1354. if ($_url) {
  1355. print escape_url($full_url), "\n";
  1356. return;
  1357. }
  1358. my $result = "Path: $path\n";
  1359. $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
  1360. $result .= "URL: " . escape_url($full_url) . "\n";
  1361. eval {
  1362. my $repos_root = $gs->repos_root;
  1363. Git::SVN::remove_username($repos_root);
  1364. $result .= "Repository Root: " . escape_url($repos_root) . "\n";
  1365. };
  1366. if ($@) {
  1367. $result .= "Repository Root: (offline)\n";
  1368. }
  1369. ::_req_svn();
  1370. $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" &&
  1371. (::compare_svn_version('1.5.4') <= 0 || $file_type ne "dir");
  1372. $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
  1373. $result .= "Node Kind: " .
  1374. ($file_type eq "dir" ? "directory" : "file") . "\n";
  1375. my $schedule = $diff_status eq "A"
  1376. ? "add"
  1377. : ($diff_status eq "D" ? "delete" : "normal");
  1378. $result .= "Schedule: $schedule\n";
  1379. if ($diff_status eq "A") {
  1380. print $result, "\n";
  1381. return;
  1382. }
  1383. my ($lc_author, $lc_rev, $lc_date_utc);
  1384. my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath);
  1385. my $log = command_output_pipe(@args);
  1386. my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
  1387. while (<$log>) {
  1388. if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
  1389. $lc_author = $1;
  1390. $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
  1391. } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
  1392. (undef, $lc_rev, undef) = ::extract_metadata($1);
  1393. }
  1394. }
  1395. close $log;
  1396. Git::SVN::Log::set_local_timezone();
  1397. $result .= "Last Changed Author: $lc_author\n";
  1398. $result .= "Last Changed Rev: $lc_rev\n";
  1399. $result .= "Last Changed Date: " .
  1400. Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
  1401. if ($file_type ne "dir") {
  1402. my $text_last_updated_date =
  1403. ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
  1404. $result .=
  1405. "Text Last Updated: " .
  1406. Git::SVN::Log::format_svn_date($text_last_updated_date) .
  1407. "\n";
  1408. my $checksum;
  1409. if ($diff_status eq "D") {
  1410. my ($fh, $ctx) =
  1411. command_output_pipe(qw(cat-file blob), "HEAD:$path");
  1412. if ($file_type eq "link") {
  1413. my $file_name = <$fh>;
  1414. $checksum = md5sum("link $file_name");
  1415. } else {
  1416. $checksum = md5sum($fh);
  1417. }
  1418. command_close_pipe($fh, $ctx);
  1419. } elsif ($file_type eq "link") {
  1420. my $file_name =
  1421. command(qw(cat-file blob), "HEAD:$path");
  1422. $checksum =
  1423. md5sum("link " . $file_name);
  1424. } else {
  1425. open FILE, "<", $path or die $!;
  1426. $checksum = md5sum(\*FILE);
  1427. close FILE or die $!;
  1428. }
  1429. $result .= "Checksum: " . $checksum . "\n";
  1430. }
  1431. print $result, "\n";
  1432. }
  1433. sub cmd_reset {
  1434. my $target = shift || $_revision or die "SVN revision required\n";
  1435. $target = $1 if $target =~ /^r(\d+)$/;
  1436. $target =~ /^\d+$/ or die "Numeric SVN revision expected\n";
  1437. my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
  1438. unless ($gs) {
  1439. die "Unable to determine upstream SVN information from ".
  1440. "history\n";
  1441. }
  1442. my ($r, $c) = $gs->find_rev_before($target, not $_fetch_parent);
  1443. die "Cannot find SVN revision $target\n" unless defined($c);
  1444. $gs->rev_map_set($r, $c, 'reset', $uuid);
  1445. print "r$r = $c ($gs->{ref_id})\n";
  1446. }
  1447. sub cmd_gc {
  1448. if (!$can_compress) {
  1449. warn "Compress::Zlib could not be found; unhandled.log " .
  1450. "files will not be compressed.\n";
  1451. }
  1452. find({ wanted => \&gc_directory, no_chdir => 1}, "$ENV{GIT_DIR}/svn");
  1453. }
  1454. ########################### utility functions #########################
  1455. sub rebase_cmd {
  1456. my @cmd = qw/rebase/;
  1457. push @cmd, '-v' if $_verbose;
  1458. push @cmd, qw/--merge/ if $_merge;
  1459. push @cmd, "--strategy=$_strategy" if $_strategy;
  1460. push @cmd, "--preserve-merges" if $_preserve_merges;
  1461. @cmd;
  1462. }
  1463. sub post_fetch_checkout {
  1464. return if $_no_checkout;
  1465. my $gs = $Git::SVN::_head or return;
  1466. return if verify_ref('refs/heads/master^0');
  1467. # look for "trunk" ref if it exists
  1468. my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
  1469. my $fetch = $remote->{fetch};
  1470. if ($fetch) {
  1471. foreach my $p (keys %$fetch) {
  1472. basename($fetch->{$p}) eq 'trunk' or next;
  1473. $gs = Git::SVN->new($fetch->{$p}, $gs->{repo_id}, $p);
  1474. last;
  1475. }
  1476. }
  1477. my $valid_head = verify_ref('HEAD^0');
  1478. command_noisy(qw(update-ref refs/heads/master), $gs->refname);
  1479. return if ($valid_head || !verify_ref('HEAD^0'));
  1480. return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
  1481. my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
  1482. return if -f $index;
  1483. return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
  1484. return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
  1485. command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
  1486. print STDERR "Checked out HEAD:\n ",
  1487. $gs->full_url, " r", $gs->last_rev, "\n";
  1488. if (auto_create_empty_directories($gs)) {
  1489. $gs->mkemptydirs($gs->last_rev);
  1490. }
  1491. }
  1492. sub complete_svn_url {
  1493. my ($url, $path) = @_;
  1494. $path =~ s#/+$##;
  1495. if ($path !~ m#^[a-z\+]+://#) {
  1496. if (!defined $url || $url !~ m#^[a-z\+]+://#) {
  1497. fatal("E: '$path' is not a complete URL ",
  1498. "and a separate URL is not specified");
  1499. }
  1500. return ($url, $path);
  1501. }
  1502. return ($path, '');
  1503. }
  1504. sub complete_url_ls_init {
  1505. my ($ra, $repo_path, $switch, $pfx) = @_;
  1506. unless ($repo_path) {
  1507. print STDERR "W: $switch not specified\n";
  1508. return;
  1509. }
  1510. $repo_path =~ s#/+$##;
  1511. if ($repo_path =~ m#^[a-z\+]+://#) {
  1512. $ra = Git::SVN::Ra->new($repo_path);
  1513. $repo_path = '';
  1514. } else {
  1515. $repo_path =~ s#^/+##;
  1516. unless ($ra) {
  1517. fatal("E: '$repo_path' is not a complete URL ",
  1518. "and a separate URL is not specified");
  1519. }
  1520. }
  1521. my $url = $ra->{url};
  1522. my $gs = Git::SVN->init($url, undef, undef, undef, 1);
  1523. my $k = "svn-remote.$gs->{repo_id}.url";
  1524. my $orig_url = eval { command_oneline(qw/config --get/, $k) };
  1525. if ($orig_url && ($orig_url ne $gs->{url})) {
  1526. die "$k already set: $orig_url\n",
  1527. "wanted to set to: $gs->{url}\n";
  1528. }
  1529. command_oneline('config', $k, $gs->{url}) unless $orig_url;
  1530. my $remote_path = "$gs->{path}/$repo_path";
  1531. $remote_path =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
  1532. $remote_path =~ s#/+#/#g;
  1533. $remote_path =~ s#^/##g;
  1534. $remote_path .= "/*" if $remote_path !~ /\*/;
  1535. my ($n) = ($switch =~ /^--(\w+)/);
  1536. if (length $pfx && $pfx !~ m#/$#) {
  1537. die "--prefix='$pfx' must have a trailing slash '/'\n";
  1538. }
  1539. command_noisy('config',
  1540. '--add',
  1541. "svn-remote.$gs->{repo_id}.$n",
  1542. "$remote_path:refs/remotes/$pfx*" .
  1543. ('/*' x (($remote_path =~ tr/*/*/) - 1)) );
  1544. }
  1545. sub verify_ref {
  1546. my ($ref) = @_;
  1547. eval { command_oneline([ 'rev-parse', '--verify', $ref ],
  1548. { STDERR => 0 }); };
  1549. }
  1550. sub get_tree_from_treeish {
  1551. my ($treeish) = @_;
  1552. # $treeish can be a symbolic ref, too:
  1553. my $type = command_oneline(qw/cat-file -t/, $treeish);
  1554. my $expected;
  1555. while ($type eq 'tag') {
  1556. ($treeish, $type) = command(qw/cat-file tag/, $treeish);
  1557. }
  1558. if ($type eq 'commit') {
  1559. $expected = (grep /^tree /, command(qw/cat-file commit/,
  1560. $treeish))[0];
  1561. ($expected)

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