PageRenderTime 65ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/.metadata/.plugins/com.aptana.portablegit.win32/libexec/git-core/git-svn

https://github.com/amariebk/SuiviActivPPE
Perl | 6237 lines | 5539 code | 414 blank | 284 comment | 786 complexity | 4c7ba5ca7758cc8bb096fdd155b2206d MD5 | raw file

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

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

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