blob: 914c707a90e5755879574b2717c064fd9364d054 [file] [log] [blame]
Eric Wong3397f9d2006-02-16 01:24:16 -08001#!/usr/bin/env perl
Eric Wong551ce282006-02-20 10:57:29 -08002# Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3# License: GPL v2 or later
Eric Wong3397f9d2006-02-16 01:24:16 -08004use warnings;
5use strict;
6use vars qw/ $AUTHOR $VERSION
Adam Robenffe256f2008-05-23 16:19:41 +02007 $sha1 $sha1_short $_revision $_repository
Eric Wong9760adc2007-01-31 03:06:56 -08008 $_q $_authors %users/;
Eric Wong3397f9d2006-02-16 01:24:16 -08009$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
Eric Wong60d02cc2006-07-06 00:14:16 -070010$VERSION = '@@GIT_VERSION@@';
Eric Wong13ccd6d2006-03-29 22:37:18 -080011
Benoit Sigoure15153452007-10-16 16:36:50 +020012# From which subdir have we been invoked?
13my $cmd_dir_prefix = eval {
14 command_oneline([qw/rev-parse --show-prefix/], STDERR => 0)
15} || '';
16
Eric Wong5253dc32007-02-20 01:36:30 -080017my $git_dir_user_set = 1 if defined $ENV{GIT_DIR};
Eric Wong706587f2007-01-18 17:50:01 -080018$ENV{GIT_DIR} ||= '.git';
Eric Wong9fa00b62007-02-03 12:49:48 -080019$Git::SVN::default_repo_id = 'svn';
Eric Wong8b8fc062007-01-22 11:44:57 -080020$Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
Eric Wong6af1db42007-02-14 16:04:10 -080021$Git::SVN::Ra::_log_window_size = 100;
Eric Wong13ccd6d2006-03-29 22:37:18 -080022
Eric Wongf8c9d1d2007-01-12 02:35:20 -080023$Git::SVN::Log::TZ = $ENV{TZ};
Eric Wong3397f9d2006-02-16 01:24:16 -080024$ENV{TZ} = 'UTC';
Eric Wonga00439a2006-06-27 19:39:13 -070025$| = 1; # unbuffer STDOUT
Eric Wong3397f9d2006-02-16 01:24:16 -080026
Benoit Sigoure207f1a72007-10-16 16:36:52 +020027sub fatal (@) { print STDERR "@_\n"; exit 1 }
Eric Wongb9c85182006-12-15 23:58:07 -080028require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
29require SVN::Ra;
30require SVN::Delta;
31if ($SVN::Core::VERSION lt '1.1.0') {
Benoit Sigoure207f1a72007-10-16 16:36:52 +020032 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
Eric Wongb9c85182006-12-15 23:58:07 -080033}
Eric Wongd81bf822007-01-10 01:22:38 -080034push @Git::SVN::Ra::ISA, 'SVN::Ra';
Eric Wongb9c85182006-12-15 23:58:07 -080035push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
36push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
Eric Wong3397f9d2006-02-16 01:24:16 -080037use Carp qw/croak/;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -080038use Digest::MD5;
Eric Wong3397f9d2006-02-16 01:24:16 -080039use IO::File qw//;
40use File::Basename qw/dirname basename/;
41use File::Path qw/mkpath/;
Eric Wong512b6202007-04-03 01:57:08 -070042use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
Eric Wong968bdf12006-06-15 13:36:12 -070043use IPC::Open3;
Eric Wong336f1712007-01-11 02:14:43 -080044use Git;
Eric Wonga5e0ced2006-06-12 15:23:48 -070045
Eric Wong336f1712007-01-11 02:14:43 -080046BEGIN {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120047 # import functions from Git into our packages, en masse
48 no strict 'refs';
Eric Wong336f1712007-01-11 02:14:43 -080049 foreach (qw/command command_oneline command_noisy command_output_pipe
50 command_input_pipe command_close_pipe/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120051 for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -080052 Git::SVN::Migration Git::SVN::Log Git::SVN),
Sam Vilainc5f71ad2007-06-15 15:43:59 +120053 __PACKAGE__) {
54 *{"${package}::$_"} = \&{"Git::$_"};
55 }
Eric Wong336f1712007-01-11 02:14:43 -080056 }
Eric Wong336f1712007-01-11 02:14:43 -080057}
58
Eric Wongb9c85182006-12-15 23:58:07 -080059my ($SVN);
Eric Wong83e99402006-10-11 18:19:55 -070060
Eric Wongf8c9d1d2007-01-12 02:35:20 -080061$sha1 = qr/[a-f\d]{40}/;
62$sha1_short = qr/[a-f\d]{4,40}/;
Eric Wong44320b92007-01-13 22:35:53 -080063my ($_stdin, $_help, $_edit,
Eric Wong9760adc2007-01-31 03:06:56 -080064 $_message, $_file,
Eric Wongd05d72e2007-01-15 22:59:26 -080065 $_template, $_shared,
Karl Hasselström171af112007-05-03 07:51:35 +020066 $_version, $_fetch_all, $_no_rebase,
Eric Wongdee41f32007-03-13 11:40:36 -070067 $_merge, $_strategy, $_dry_run, $_local,
Steven Grimm4be40382008-05-10 22:11:18 -070068 $_prefix, $_no_checkout, $_url, $_verbose,
Florian Ragwitz5de70ef2008-10-04 19:35:17 -070069 $_git_format, $_commit_url, $_tag);
Eric Wong0bed5ea2007-02-09 02:45:03 -080070$Git::SVN::_follow_parent = 1;
Eric Wong706587f2007-01-18 17:50:01 -080071my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
72 'config-dir=s' => \$Git::SVN::Ra::config_dir,
73 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache );
Eric Wong0bed5ea2007-02-09 02:45:03 -080074my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
Eric Wongdc5869c2006-05-24 02:07:32 -070075 'authors-file|A=s' => \$_authors,
Eric Wongecc712d2007-01-31 12:28:10 -080076 'repack:i' => \$Git::SVN::_repack,
Eric Wong97ae0912007-02-11 15:21:24 -080077 'noMetadata' => \$Git::SVN::_no_metadata,
78 'useSvmProps' => \$Git::SVN::_use_svm_props,
Eric Wong62e349d2007-02-16 19:57:29 -080079 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props,
Eric Wong6af1db42007-02-14 16:04:10 -080080 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size,
Eric Wong1e889ef2007-02-16 01:45:13 -080081 'no-checkout' => \$_no_checkout,
Eric Wong80f50742006-06-27 19:39:14 -070082 'quiet|q' => \$_q,
Eric Wongecc712d2007-01-31 12:28:10 -080083 'repack-flags|repack-args|repack-opts=s' =>
84 \$Git::SVN::_repack_flags,
Andy Whitcroft70ae04e2007-11-22 13:44:42 +000085 'use-log-author' => \$Git::SVN::_use_log_author,
Avery Pennarun6aa9ba12008-04-15 21:04:17 -040086 'add-author-from' => \$Git::SVN::_add_author_from,
Eric Wong706587f2007-01-18 17:50:01 -080087 %remote_opts );
Eric Wong36f5b1f2006-05-23 19:23:41 -070088
martin f. krafft8f728fb2007-07-14 11:25:28 +020089my ($_trunk, $_tags, $_branches, $_stdlayout);
Eric Wong0dfaf0a2007-02-18 02:34:09 -080090my %icv;
Eric Wongdadc6d22007-02-14 12:27:41 -080091my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared,
92 'trunk|T=s' => \$_trunk, 'tags|t=s' => \$_tags,
93 'branches|b=s' => \$_branches, 'prefix=s' => \$_prefix,
martin f. krafft8f728fb2007-07-14 11:25:28 +020094 'stdlayout|s' => \$_stdlayout,
Eric Wong4a1bb4c2007-05-13 09:58:14 -070095 'minimize-url|m' => \$Git::SVN::_minimize_url,
Eric Wong0dfaf0a2007-02-18 02:34:09 -080096 'no-metadata' => sub { $icv{noMetadata} = 1 },
97 'use-svm-props' => sub { $icv{useSvmProps} = 1 },
98 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 },
99 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] },
Eric Wongdadc6d22007-02-14 12:27:41 -0800100 %remote_opts );
Eric Wong27e9fb82006-06-27 19:39:12 -0700101my %cmt_opts = ( 'edit|e' => \$_edit,
Eric Wong24e22aa2007-01-29 00:07:49 -0800102 'rmdir' => \$SVN::Git::Editor::_rmdir,
103 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder,
104 'l=i' => \$SVN::Git::Editor::_rename_limit,
105 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity
Eric Wong27e9fb82006-06-27 19:39:12 -0700106);
Eric Wong9d55b412006-06-12 15:53:13 -0700107
Eric Wong3397f9d2006-02-16 01:24:16 -0800108my %cmd = (
Eric Wong2a3240b2007-01-04 18:09:56 -0800109 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
Eric Wonge98671e2007-02-14 02:21:19 -0800110 { 'revision|r=s' => \$_revision,
Eric Wong905f8b72007-02-16 03:22:40 -0800111 'fetch-all|all' => \$_fetch_all,
Eric Wonge98671e2007-02-14 02:21:19 -0800112 %fc_opts } ],
Eric Wong0425ea92007-02-16 18:45:01 -0800113 clone => [ \&cmd_clone, "Initialize and fetch revisions",
114 { 'revision|r=s' => \$_revision,
115 %fc_opts, %init_opts } ],
Eric Wongd2866f92007-01-11 12:26:16 -0800116 init => [ \&cmd_init, "Initialize a repo for tracking" .
Eric Wongf8ab6b72006-05-31 15:49:56 -0700117 " (requires URL argument)",
Eric Wong9d55b412006-06-12 15:53:13 -0700118 \%init_opts ],
Eric Wongdadc6d22007-02-14 12:27:41 -0800119 'multi-init' => [ \&cmd_multi_init,
120 "Deprecated alias for ".
121 "'$0 init -T<trunk> -b<branches> -t<tags>'",
122 \%init_opts ],
Eric Wongd7ad3be2007-01-14 03:14:28 -0800123 dcommit => [ \&cmd_dcommit,
124 'Commit several diffs to merge with upstream',
Eric Wong3289e862006-12-15 23:58:08 -0800125 { 'merge|m|M' => \$_merge,
126 'strategy|s=s' => \$_strategy,
Eric Wong905f8b72007-02-16 03:22:40 -0800127 'verbose|v' => \$_verbose,
Eric Wong3289e862006-12-15 23:58:08 -0800128 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800129 'fetch-all|all' => \$_fetch_all,
Eric Wongba24e742008-08-07 02:06:16 -0700130 'commit-url=s' => \$_commit_url,
131 'revision|r=i' => \$_revision,
Karl Hasselström171af112007-05-03 07:51:35 +0200132 'no-rebase' => \$_no_rebase,
Eric Wong4b155222006-12-22 21:59:24 -0800133 %cmt_opts, %fc_opts } ],
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700134 branch => [ \&cmd_branch,
135 'Create a branch in the SVN repository',
136 { 'message|m=s' => \$_message,
137 'dry-run|n' => \$_dry_run,
138 'tag|t' => \$_tag } ],
139 tag => [ sub { $_tag = 1; cmd_branch(@_) },
140 'Create a tag in the SVN repository',
141 { 'message|m=s' => \$_message,
142 'dry-run|n' => \$_dry_run } ],
Eric Wong1ce255d2007-01-14 23:21:16 -0800143 'set-tree' => [ \&cmd_set_tree,
144 "Set an SVN repository to a git tree-ish",
145 { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200146 'create-ignore' => [ \&cmd_create_ignore,
147 'Create a .gitignore per svn:ignore',
148 { 'revision|r=i' => \$_revision
149 } ],
Benoit Sigoure15153452007-10-16 16:36:50 +0200150 'propget' => [ \&cmd_propget,
151 'Print the value of a property on a file or directory',
152 { 'revision|r=i' => \$_revision } ],
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200153 'proplist' => [ \&cmd_proplist,
154 'List all properties of a file or directory',
155 { 'revision|r=i' => \$_revision } ],
Eric Wong5969cbe2007-01-11 17:58:39 -0800156 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200157 { 'revision|r=i' => \$_revision
Lars Hjemli05b4df32007-09-05 11:35:29 +0200158 } ],
Vineet Kumar2d879792007-11-19 14:56:15 -0800159 'show-externals' => [ \&cmd_show_externals, "Show svn:externals listings",
160 { 'revision|r=i' => \$_revision
161 } ],
Eric Wong1c8443b2007-01-14 02:17:00 -0800162 'multi-fetch' => [ \&cmd_multi_fetch,
Eric Wonge98671e2007-02-14 02:21:19 -0800163 "Deprecated alias for $0 fetch --all",
164 { 'revision|r=s' => \$_revision, %fc_opts } ],
Eric Wong706587f2007-01-18 17:50:01 -0800165 'migrate' => [ sub { },
166 # no-op, we automatically run this anyways,
Eric Wong706587f2007-01-18 17:50:01 -0800167 'Migrate configuration/metadata/layout from
168 previous versions of git-svn',
Eric Wonga836a0e2007-02-14 19:34:56 -0800169 { 'minimize' => \$Git::SVN::Migration::_minimize,
170 %remote_opts } ],
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800171 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
172 { 'limit=i' => \$Git::SVN::Log::limit,
Eric Wong79bb8d82006-06-01 02:35:44 -0700173 'revision|r=s' => \$_revision,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800174 'verbose|v' => \$Git::SVN::Log::verbose,
175 'incremental' => \$Git::SVN::Log::incremental,
176 'oneline' => \$Git::SVN::Log::oneline,
177 'show-commit' => \$Git::SVN::Log::show_commit,
178 'non-recursive' => \$Git::SVN::Log::non_recursive,
Eric Wong79bb8d82006-06-01 02:35:44 -0700179 'authors-file|A=s' => \$_authors,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800180 'color' => \$Git::SVN::Log::color,
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200181 'pager=s' => \$Git::SVN::Log::pager
Eric Wong79bb8d82006-06-01 02:35:44 -0700182 } ],
Eric Wong222566e2008-08-08 01:41:58 -0700183 'find-rev' => [ \&cmd_find_rev,
184 "Translate between SVN revision numbers and tree-ish",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200185 {} ],
Eric Wong905f8b72007-02-16 03:22:40 -0800186 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
187 { 'merge|m|M' => \$_merge,
188 'verbose|v' => \$_verbose,
189 'strategy|s=s' => \$_strategy,
Eric Wongdee41f32007-03-13 11:40:36 -0700190 'local|l' => \$_local,
Eric Wong905f8b72007-02-16 03:22:40 -0800191 'fetch-all|all' => \$_fetch_all,
Seth Falcon7d45e142008-05-19 20:29:17 -0700192 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800193 %fc_opts } ],
Eric Wong44320b92007-01-13 22:35:53 -0800194 'commit-diff' => [ \&cmd_commit_diff,
195 'Commit a diff between two trees',
Eric Wong27e9fb82006-06-27 19:39:12 -0700196 { 'message|m=s' => \$_message,
197 'file|F=s' => \$_file,
Eric Wong45bf4732006-11-09 01:19:37 -0800198 'revision|r=s' => \$_revision,
Eric Wong27e9fb82006-06-27 19:39:12 -0700199 %cmt_opts } ],
David D. Kilzere6fefa92007-11-21 11:57:18 -0800200 'info' => [ \&cmd_info,
201 "Show info about the latest SVN revision
202 on the current branch",
David D. Kilzer8b014d72007-11-21 11:57:19 -0800203 { 'url' => \$_url, } ],
Tim Stoakes6fb53752008-02-10 15:21:08 +1030204 'blame' => [ \&Git::SVN::Log::cmd_blame,
205 "Show what revision and author last modified each line of a file",
Steven Grimm4be40382008-05-10 22:11:18 -0700206 { 'git-format' => \$_git_format } ],
Eric Wong3397f9d2006-02-16 01:24:16 -0800207);
Eric Wong9d55b412006-06-12 15:53:13 -0700208
Eric Wong3397f9d2006-02-16 01:24:16 -0800209my $cmd;
210for (my $i = 0; $i < @ARGV; $i++) {
211 if (defined $cmd{$ARGV[$i]}) {
212 $cmd = $ARGV[$i];
213 splice @ARGV, $i, 1;
214 last;
215 }
216};
217
Eric Wong540424b2007-12-19 00:31:43 -0800218# make sure we're always running at the top-level working directory
219unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
Eric Wong5253dc32007-02-20 01:36:30 -0800220 unless (-d $ENV{GIT_DIR}) {
221 if ($git_dir_user_set) {
222 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
223 "but it is not a directory\n";
224 }
225 my $git_dir = delete $ENV{GIT_DIR};
Deskin Millerfe4003f2008-11-06 00:07:39 -0500226 my $cdup = undef;
227 git_cmd_try {
228 $cdup = command_oneline(qw/rev-parse --show-cdup/);
229 $git_dir = '.' unless ($cdup);
230 chomp $cdup if ($cdup);
231 $cdup = "." unless ($cdup && length $cdup);
232 } "Already at toplevel, but $git_dir not found\n";
Eric Wong5253dc32007-02-20 01:36:30 -0800233 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
234 unless (-d $git_dir) {
235 die "$git_dir still not found after going to ",
236 "'$cdup'\n";
237 }
238 $ENV{GIT_DIR} = $git_dir;
239 }
Adam Robenffe256f2008-05-23 16:19:41 +0200240 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wong5253dc32007-02-20 01:36:30 -0800241}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100242
243my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
244
245read_repo_config(\%opts);
Eric Wong222566e2008-08-08 01:41:58 -0700246if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
247 Getopt::Long::Configure('pass_through');
248}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100249my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
250 'minimize-connections' => \$Git::SVN::Migration::_minimize,
251 'id|i=s' => \$Git::SVN::default_ref_id,
252 'svn-remote|remote|R=s' => sub {
253 $Git::SVN::no_reuse_existing = 1;
254 $Git::SVN::default_repo_id = $_[1] });
255exit 1 if (!$rv && $cmd && $cmd ne 'log');
256
257usage(0) if $_help;
258version() if $_version;
259usage(1) unless defined $cmd;
260load_authors() if $_authors;
261
Eric Wong0425ea92007-02-16 18:45:01 -0800262unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
Eric Wong706587f2007-01-18 17:50:01 -0800263 Git::SVN::Migration::migration_check();
264}
Eric Wongecc712d2007-01-31 12:28:10 -0800265Git::SVN::init_vars();
Eric Wongb805b442007-01-22 13:52:04 -0800266eval {
267 Git::SVN::verify_remotes_sanity();
268 $cmd{$cmd}->[0]->(@ARGV);
269};
270fatal $@ if $@;
Eric Wong1e889ef2007-02-16 01:45:13 -0800271post_fetch_checkout();
Eric Wong3397f9d2006-02-16 01:24:16 -0800272exit 0;
273
274####################### primary functions ######################
275sub usage {
276 my $exit = shift || 0;
277 my $fd = $exit ? \*STDERR : \*STDOUT;
278 print $fd <<"";
279git-svn - bidirectional operations between a single Subversion tree and git
Stephan Beyer1b1dd232008-07-13 15:36:15 +0200280Usage: git svn <command> [options] [arguments]\n
Eric Wong448c81b2006-03-03 01:20:09 -0800281
282 print $fd "Available commands:\n" unless $cmd;
Eric Wong3397f9d2006-02-16 01:24:16 -0800283
284 foreach (sort keys %cmd) {
Eric Wong448c81b2006-03-03 01:20:09 -0800285 next if $cmd && $cmd ne $_;
Eric Wonga836a0e2007-02-14 19:34:56 -0800286 next if /^multi-/; # don't show deprecated commands
Eric Wongb203b762006-10-11 14:53:36 -0700287 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
Benoit Sigoureaa807bc2007-11-03 19:53:34 +0100288 foreach (sort keys %{$cmd{$_}->[2]}) {
Eric Wong512b6202007-04-03 01:57:08 -0700289 # mixed-case options are for .git/config only
290 next if /[A-Z]/ && /^[a-z]+$/i;
Eric Wong448c81b2006-03-03 01:20:09 -0800291 # prints out arguments as they should be passed:
Eric Wongb8c92ca2006-05-24 01:40:37 -0700292 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
Eric Wongb203b762006-10-11 14:53:36 -0700293 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
Eric Wong448c81b2006-03-03 01:20:09 -0800294 "--$_" : "-$_" }
295 split /\|/,$_)," $x\n";
296 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800297 }
298 print $fd <<"";
Eric Wong448c81b2006-03-03 01:20:09 -0800299\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
300arbitrary identifier if you're tracking multiple SVN branches/repositories in
301one git repository and want to keep them separate. See git-svn(1) for more
302information.
Eric Wong3397f9d2006-02-16 01:24:16 -0800303
304 exit $exit;
305}
306
Eric Wong551ce282006-02-20 10:57:29 -0800307sub version {
Eric Wong7d60ab22006-12-28 01:16:20 -0800308 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
Eric Wong551ce282006-02-20 10:57:29 -0800309 exit 0;
310}
311
Eric Wong8164b652007-01-11 15:35:55 -0800312sub do_git_init_db {
313 unless (-d $ENV{GIT_DIR}) {
314 my @init_db = ('init');
315 push @init_db, "--template=$_template" if defined $_template;
Eric Wongdadc6d22007-02-14 12:27:41 -0800316 if (defined $_shared) {
317 if ($_shared =~ /[a-z]/) {
318 push @init_db, "--shared=$_shared";
319 } else {
320 push @init_db, "--shared";
321 }
322 }
Eric Wong8164b652007-01-11 15:35:55 -0800323 command_noisy(@init_db);
Adam Robenffe256f2008-05-23 16:19:41 +0200324 $_repository = Git->repository(Repository => ".git");
Eric Wong8164b652007-01-11 15:35:55 -0800325 }
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800326 my $set;
327 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
328 foreach my $i (keys %icv) {
329 die "'$set' and '$i' cannot both be set\n" if $set;
330 next unless defined $icv{$i};
331 command_noisy('config', "$pfx.$i", $icv{$i});
332 $set = $i;
333 }
Eric Wong8164b652007-01-11 15:35:55 -0800334}
335
Eric Wongdadc6d22007-02-14 12:27:41 -0800336sub init_subdir {
337 my $repo_path = shift or return;
338 mkpath([$repo_path]) unless -d $repo_path;
339 chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
Eric Wongf30603f2007-02-23 01:26:26 -0800340 $ENV{GIT_DIR} = '.git';
Adam Robenffe256f2008-05-23 16:19:41 +0200341 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wongdadc6d22007-02-14 12:27:41 -0800342}
343
Eric Wong0425ea92007-02-16 18:45:01 -0800344sub cmd_clone {
345 my ($url, $path) = @_;
346 if (!defined $path &&
martin f. krafft8f728fb2007-07-14 11:25:28 +0200347 (defined $_trunk || defined $_branches || defined $_tags ||
348 defined $_stdlayout) &&
Eric Wong0425ea92007-02-16 18:45:01 -0800349 $url !~ m#^[a-z\+]+://#) {
350 $path = $url;
351 }
Eric Wong0425ea92007-02-16 18:45:01 -0800352 $path = basename($url) if !defined $path || !length $path;
Eric Wongf30603f2007-02-23 01:26:26 -0800353 cmd_init($url, $path);
Eric Wong0425ea92007-02-16 18:45:01 -0800354 Git::SVN::fetch_all($Git::SVN::default_repo_id);
355}
356
Eric Wongd2866f92007-01-11 12:26:16 -0800357sub cmd_init {
martin f. krafft8f728fb2007-07-14 11:25:28 +0200358 if (defined $_stdlayout) {
359 $_trunk = 'trunk' if (!defined $_trunk);
360 $_tags = 'tags' if (!defined $_tags);
361 $_branches = 'branches' if (!defined $_branches);
362 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800363 if (defined $_trunk || defined $_branches || defined $_tags) {
364 return cmd_multi_init(@_);
Eric Wong03e0ea82006-06-30 21:42:53 -0700365 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800366 my $url = shift or die "SVN repository location required ",
367 "as a command-line argument\n";
368 init_subdir(@_);
Eric Wong8164b652007-01-11 15:35:55 -0800369 do_git_init_db();
Eric Wong03e0ea82006-06-30 21:42:53 -0700370
Eric Wong706587f2007-01-18 17:50:01 -0800371 Git::SVN->init($url);
Eric Wong3397f9d2006-02-16 01:24:16 -0800372}
373
Eric Wong2a3240b2007-01-04 18:09:56 -0800374sub cmd_fetch {
Eric Wonge98671e2007-02-14 02:21:19 -0800375 if (grep /^\d+=./, @_) {
376 die "'<rev>=<commit>' fetch arguments are ",
377 "no longer supported.\n";
Eric Wong07a1c952007-01-22 15:47:41 -0800378 }
Eric Wonge98671e2007-02-14 02:21:19 -0800379 my ($remote) = @_;
380 if (@_ > 1) {
Eric Wong905f8b72007-02-16 03:22:40 -0800381 die "Usage: $0 fetch [--all] [svn-remote]\n";
Eric Wonge98671e2007-02-14 02:21:19 -0800382 }
383 $remote ||= $Git::SVN::default_repo_id;
384 if ($_fetch_all) {
385 cmd_multi_fetch();
386 } else {
387 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
Eric Wong1c8443b2007-01-14 02:17:00 -0800388 }
Eric Wong2a3240b2007-01-04 18:09:56 -0800389}
390
Eric Wong1ce255d2007-01-14 23:21:16 -0800391sub cmd_set_tree {
Eric Wong3397f9d2006-02-16 01:24:16 -0800392 my (@commits) = @_;
393 if ($_stdin || !@commits) {
394 print "Reading from stdin...\n";
395 @commits = ();
396 while (<STDIN>) {
Eric Wong1ca72ae2006-03-03 01:20:09 -0800397 if (/\b($sha1_short)\b/o) {
Eric Wong3397f9d2006-02-16 01:24:16 -0800398 unshift @commits, $1;
399 }
400 }
401 }
402 my @revs;
Eric Wong8de010a2006-02-20 10:57:26 -0800403 foreach my $c (@commits) {
Eric Wongaef4e922006-12-15 10:59:54 -0800404 my @tmp = command('rev-parse',$c);
Eric Wong8de010a2006-02-20 10:57:26 -0800405 if (scalar @tmp == 1) {
406 push @revs, $tmp[0];
407 } elsif (scalar @tmp > 1) {
Eric Wongaef4e922006-12-15 10:59:54 -0800408 push @revs, reverse(command('rev-list',@tmp));
Eric Wong8de010a2006-02-20 10:57:26 -0800409 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200410 fatal "Failed to rev-parse $c";
Eric Wong8de010a2006-02-20 10:57:26 -0800411 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800412 }
Eric Wong1ce255d2007-01-14 23:21:16 -0800413 my $gs = Git::SVN->new;
414 my ($r_last, $cmt_last) = $gs->last_rev_commit;
415 $gs->fetch;
Eric Wong97f69872007-01-25 11:53:13 -0800416 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
Eric Wong1ce255d2007-01-14 23:21:16 -0800417 fatal "There are new revisions that were fetched ",
418 "and need to be merged (or acknowledged) ",
419 "before committing.\nlast rev: $r_last\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200420 " current: $gs->{last_rev}";
Eric Wong1ce255d2007-01-14 23:21:16 -0800421 }
422 $gs->set_tree($_) foreach @revs;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700423 print "Done committing ",scalar @revs," revisions to SVN\n";
Eric Wong3157dd92007-12-13 08:27:34 -0800424 unlink $gs->{index};
Eric Wonga5e0ced2006-06-12 15:23:48 -0700425}
426
Eric Wongd7ad3be2007-01-14 03:14:28 -0800427sub cmd_dcommit {
428 my $head = shift;
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100429 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
David Reiss826a9332007-11-13 13:47:26 -0800430 'Cannot dcommit with a dirty index. Commit your changes first, '
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100431 . "or stash them with `git stash'.\n";
Eric Wongd7ad3be2007-01-14 03:14:28 -0800432 $head ||= 'HEAD';
Eric Wonga8ae2622007-02-13 14:22:11 -0800433 my @refs;
Eric Wong13c823f2007-04-08 00:59:19 -0700434 my ($url, $rev, $uuid, $gs) = working_head_info($head, \@refs);
Thomas Rast2cb61102008-08-31 15:50:59 +0200435 unless ($gs) {
436 die "Unable to determine upstream SVN information from ",
437 "$head history.\nPerhaps the repository is empty.";
438 }
Eric Wong711521e2008-08-20 00:30:06 -0700439 $url = defined $_commit_url ? $_commit_url : $gs->full_url;
Eric Wongba24e742008-08-07 02:06:16 -0700440 my $last_rev = $_revision if defined $_revision;
Matthieu Moy59b0c242008-04-24 20:06:36 +0200441 if ($url) {
442 print "Committing to $url ...\n";
443 }
Eric Wong733a65a2007-06-13 02:23:28 -0700444 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
Eric Wong751eb392007-08-31 18:16:12 -0700445 if ($_no_rebase && scalar(@$linear_refs) > 1) {
446 warn "Attempting to commit more than one change while ",
447 "--no-rebase is enabled.\n",
448 "If these changes depend on each other, re-running ",
Eric Wong7dfa16b2008-01-02 10:09:49 -0800449 "without --no-rebase may be required."
Eric Wong751eb392007-08-31 18:16:12 -0700450 }
Eric Wong711521e2008-08-20 00:30:06 -0700451 my $expect_url = $url;
452 Git::SVN::remove_username($expect_url);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800453 while (1) {
454 my $d = shift @$linear_refs or last;
Eric Wong45bf4732006-11-09 01:19:37 -0800455 unless (defined $last_rev) {
456 (undef, $last_rev, undef) = cmt_metadata("$d~1");
457 unless (defined $last_rev) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800458 fatal "Unable to extract revision information ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200459 "from commit $d~1";
Eric Wong45bf4732006-11-09 01:19:37 -0800460 }
461 }
Eric Wongb22d4492006-08-26 00:01:23 -0700462 if ($_dry_run) {
463 print "diff-tree $d~1 $d\n";
464 } else {
Eric Wong751eb392007-08-31 18:16:12 -0700465 my $cmt_rev;
Eric Wongd7ad3be2007-01-14 03:14:28 -0800466 my %ed_opts = ( r => $last_rev,
Eric Wong61395352007-01-27 14:33:08 -0800467 log => get_commit_entry($d)->{log},
Eric Wongba24e742008-08-07 02:06:16 -0700468 ra => Git::SVN::Ra->new($url),
Konstantin V. Arkhipov3caf3202007-11-14 03:52:02 +0300469 config => SVN::Core::config_get_config(
470 $Git::SVN::Ra::config_dir
471 ),
Eric Wong61395352007-01-27 14:33:08 -0800472 tree_a => "$d~1",
473 tree_b => $d,
474 editor_cb => sub {
475 print "Committed r$_[0]\n";
Eric Wong751eb392007-08-31 18:16:12 -0700476 $cmt_rev = $_[0];
477 },
Eric Wonga8ae2622007-02-13 14:22:11 -0800478 svn_path => '');
Eric Wong61395352007-01-27 14:33:08 -0800479 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800480 print "No changes\n$d~1 == $d\n";
Eric Wong733a65a2007-06-13 02:23:28 -0700481 } elsif ($parents->{$d} && @{$parents->{$d}}) {
Eric Wong751eb392007-08-31 18:16:12 -0700482 $gs->{inject_parents_dcommit}->{$cmt_rev} =
Eric Wong733a65a2007-06-13 02:23:28 -0700483 $parents->{$d};
Eric Wongd7ad3be2007-01-14 03:14:28 -0800484 }
Eric Wong751eb392007-08-31 18:16:12 -0700485 $_fetch_all ? $gs->fetch_all : $gs->fetch;
Eric Wong7dfa16b2008-01-02 10:09:49 -0800486 $last_rev = $cmt_rev;
Eric Wong751eb392007-08-31 18:16:12 -0700487 next if $_no_rebase;
488
489 # we always want to rebase against the current HEAD,
490 # not any head that was passed to us
Eric Wongc74d9ac2007-11-05 03:21:47 -0800491 my @diff = command('diff-tree', $d,
Eric Wong751eb392007-08-31 18:16:12 -0700492 $gs->refname, '--');
493 my @finish;
494 if (@diff) {
495 @finish = rebase_cmd();
Eric Wongc74d9ac2007-11-05 03:21:47 -0800496 print STDERR "W: $d and ", $gs->refname,
Eric Wong751eb392007-08-31 18:16:12 -0700497 " differ, using @finish:\n",
Eric Wongc74d9ac2007-11-05 03:21:47 -0800498 join("\n", @diff), "\n";
Eric Wong751eb392007-08-31 18:16:12 -0700499 } else {
500 print "No changes between current HEAD and ",
501 $gs->refname,
502 "\nResetting to the latest ",
503 $gs->refname, "\n";
504 @finish = qw/reset --mixed/;
505 }
506 command_noisy(@finish, $gs->refname);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800507 if (@diff) {
508 @refs = ();
509 my ($url_, $rev_, $uuid_, $gs_) =
510 working_head_info($head, \@refs);
511 my ($linear_refs_, $parents_) =
512 linearize_history($gs_, \@refs);
513 if (scalar(@$linear_refs) !=
514 scalar(@$linear_refs_)) {
515 fatal "# of revisions changed ",
516 "\nbefore:\n",
517 join("\n", @$linear_refs),
518 "\n\nafter:\n",
519 join("\n", @$linear_refs_), "\n",
520 'If you are attempting to commit ',
521 "merges, try running:\n\t",
522 'git rebase --interactive',
523 '--preserve-merges ',
524 $gs->refname,
525 "\nBefore dcommitting";
526 }
Eric Wong711521e2008-08-20 00:30:06 -0700527 if ($url_ ne $expect_url) {
Eric Wongc74d9ac2007-11-05 03:21:47 -0800528 fatal "URL mismatch after rebase: ",
Eric Wong711521e2008-08-20 00:30:06 -0700529 "$url_ != $expect_url";
Eric Wongc74d9ac2007-11-05 03:21:47 -0800530 }
531 if ($uuid_ ne $uuid) {
532 fatal "uuid mismatch after rebase: ",
533 "$uuid_ != $uuid";
534 }
535 # remap parents
536 my (%p, @l, $i);
537 for ($i = 0; $i < scalar @$linear_refs; $i++) {
538 my $new = $linear_refs_->[$i] or next;
539 $p{$new} =
540 $parents->{$linear_refs->[$i]};
541 push @l, $new;
542 }
543 $parents = \%p;
544 $linear_refs = \@l;
545 }
Eric Wongb22d4492006-08-26 00:01:23 -0700546 }
547 }
Eric Wong3157dd92007-12-13 08:27:34 -0800548 unlink $gs->{index};
Eric Wongb22d4492006-08-26 00:01:23 -0700549}
550
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700551sub cmd_branch {
552 my ($branch_name, $head) = @_;
553
554 unless (defined $branch_name && length $branch_name) {
555 die(($_tag ? "tag" : "branch") . " name required\n");
556 }
557 $head ||= 'HEAD';
558
559 my ($src, $rev, undef, $gs) = working_head_info($head);
560
561 my $remote = Git::SVN::read_all_remotes()->{svn};
562 my $glob = $remote->{ $_tag ? 'tags' : 'branches' };
563 my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
564 my $dst = join '/', $remote->{url}, $lft, $branch_name, ($rgt || ());
565
566 my $ctx = SVN::Client->new(
567 auth => Git::SVN::Ra::_auth_providers(),
568 log_msg => sub {
569 ${ $_[0] } = defined $_message
570 ? $_message
571 : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
572 . $branch_name;
573 },
574 );
575
576 eval {
577 $ctx->ls($dst, 'HEAD', 0);
578 } and die "branch ${branch_name} already exists\n";
579
580 print "Copying ${src} at r${rev} to ${dst}...\n";
581 $ctx->copy($src, $rev, $dst)
582 unless $_dry_run;
583
584 $gs->fetch_all;
585}
586
Adam Roben26e60162007-04-27 11:57:53 -0700587sub cmd_find_rev {
Marc-Andre Lureauea14e6c2008-03-11 10:00:45 +0200588 my $revision_or_hash = shift or die "SVN or git revision required ",
589 "as a command-line argument\n";
Adam Roben26e60162007-04-27 11:57:53 -0700590 my $result;
591 if ($revision_or_hash =~ /^r\d+$/) {
Adam Robenb3cb7e42007-04-29 01:35:27 -0700592 my $head = shift;
593 $head ||= 'HEAD';
594 my @refs;
João Abecasis63c56022008-07-14 16:28:04 +0100595 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
Adam Robenb3cb7e42007-04-29 01:35:27 -0700596 unless ($gs) {
597 die "Unable to determine upstream SVN information from ",
598 "$head history\n";
Adam Roben26e60162007-04-27 11:57:53 -0700599 }
Adam Robenb3cb7e42007-04-29 01:35:27 -0700600 my $desired_revision = substr($revision_or_hash, 1);
João Abecasis63c56022008-07-14 16:28:04 +0100601 $result = $gs->rev_map_get($desired_revision, $uuid);
Adam Roben26e60162007-04-27 11:57:53 -0700602 } else {
603 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
604 $result = $rev;
605 }
606 print "$result\n" if $result;
607}
608
Eric Wong905f8b72007-02-16 03:22:40 -0800609sub cmd_rebase {
610 command_noisy(qw/update-index --refresh/);
Eric Wong13c823f2007-04-08 00:59:19 -0700611 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
612 unless ($gs) {
Eric Wong905f8b72007-02-16 03:22:40 -0800613 die "Unable to determine upstream SVN information from ",
614 "working tree history\n";
615 }
Seth Falcon7d45e142008-05-19 20:29:17 -0700616 if ($_dry_run) {
617 print "Remote Branch: " . $gs->refname . "\n";
618 print "SVN URL: " . $url . "\n";
619 return;
620 }
Eric Wong905f8b72007-02-16 03:22:40 -0800621 if (command(qw/diff-index HEAD --/)) {
622 print STDERR "Cannot rebase with uncommited changes:\n";
623 command_noisy('status');
624 exit 1;
625 }
Eric Wongdee41f32007-03-13 11:40:36 -0700626 unless ($_local) {
Steven Grimmcec0d5a2007-11-29 11:54:39 -0800627 # rebase will checkout for us, so no need to do it explicitly
628 $_no_checkout = 'true';
Eric Wongdee41f32007-03-13 11:40:36 -0700629 $_fetch_all ? $gs->fetch_all : $gs->fetch;
630 }
Eric Wong905f8b72007-02-16 03:22:40 -0800631 command_noisy(rebase_cmd(), $gs->refname);
632}
633
Eric Wong5969cbe2007-01-11 17:58:39 -0800634sub cmd_show_ignore {
Eric Wong13c823f2007-04-08 00:59:19 -0700635 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
636 $gs ||= Git::SVN->new;
Eric Wong5969cbe2007-01-11 17:58:39 -0800637 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200638 $gs->prop_walk($gs->{path}, $r, sub {
639 my ($gs, $path, $props) = @_;
640 print STDOUT "\n# $path\n";
641 my $s = $props->{'svn:ignore'} or return;
642 $s =~ s/[\r\n]+/\n/g;
643 chomp $s;
644 $s =~ s#^#$path#gm;
645 print STDOUT "$s\n";
646 });
Eric Wonga5e0ced2006-06-12 15:23:48 -0700647}
648
Vineet Kumar2d879792007-11-19 14:56:15 -0800649sub cmd_show_externals {
650 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
651 $gs ||= Git::SVN->new;
652 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
653 $gs->prop_walk($gs->{path}, $r, sub {
654 my ($gs, $path, $props) = @_;
655 print STDOUT "\n# $path\n";
656 my $s = $props->{'svn:externals'} or return;
657 $s =~ s/[\r\n]+/\n/g;
658 chomp $s;
659 $s =~ s#^#$path#gm;
660 print STDOUT "$s\n";
661 });
662}
663
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200664sub cmd_create_ignore {
665 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
666 $gs ||= Git::SVN->new;
667 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
668 $gs->prop_walk($gs->{path}, $r, sub {
669 my ($gs, $path, $props) = @_;
670 # $path is of the form /path/to/dir/
671 my $ignore = '.' . $path . '.gitignore';
672 my $s = $props->{'svn:ignore'} or return;
673 open(GITIGNORE, '>', $ignore)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200674 or fatal("Failed to open `$ignore' for writing: $!");
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200675 $s =~ s/[\r\n]+/\n/g;
676 chomp $s;
677 # Prefix all patterns so that the ignore doesn't apply
678 # to sub-directories.
679 $s =~ s#^#/#gm;
680 print GITIGNORE "$s\n";
681 close(GITIGNORE)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200682 or fatal("Failed to close `$ignore': $!");
Gustaf Hendebyc4c66b22008-05-05 00:33:09 +0200683 command_noisy('add', '-f', $ignore);
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200684 });
685}
686
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800687sub canonicalize_path {
688 my ($path) = @_;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800689 my $dot_slash_added = 0;
690 if (substr($path, 0, 1) ne "/") {
691 $path = "./" . $path;
692 $dot_slash_added = 1;
693 }
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800694 # File::Spec->canonpath doesn't collapse x/../y into y (for a
695 # good reason), so let's do this manually.
696 $path =~ s#/+#/#g;
697 $path =~ s#/\.(?:/|$)#/#g;
698 $path =~ s#/[^/]+/\.\.##g;
699 $path =~ s#/$##g;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800700 $path =~ s#^\./## if $dot_slash_added;
Gerrit Pape2fe403e2008-07-06 19:28:50 +0000701 $path =~ s#^/##;
702 $path =~ s#^\.$##;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800703 return $path;
704}
705
Benoit Sigoure15153452007-10-16 16:36:50 +0200706# get_svnprops(PATH)
707# ------------------
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200708# Helper for cmd_propget and cmd_proplist below.
Benoit Sigoure15153452007-10-16 16:36:50 +0200709sub get_svnprops {
710 my $path = shift;
711 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
712 $gs ||= Git::SVN->new;
713
714 # prefix THE PATH by the sub-directory from which the user
715 # invoked us.
716 $path = $cmd_dir_prefix . $path;
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200717 fatal("No such file or directory: $path") unless -e $path;
Benoit Sigoure15153452007-10-16 16:36:50 +0200718 my $is_dir = -d $path ? 1 : 0;
719 $path = $gs->{path} . '/' . $path;
720
721 # canonicalize the path (otherwise libsvn will abort or fail to
722 # find the file)
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800723 $path = canonicalize_path($path);
Benoit Sigoure15153452007-10-16 16:36:50 +0200724
725 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
726 my $props;
727 if ($is_dir) {
728 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
729 }
730 else {
731 (undef, $props) = $gs->ra->get_file($path, $r, undef);
732 }
733 return $props;
734}
735
736# cmd_propget (PROP, PATH)
737# ------------------------
738# Print the SVN property PROP for PATH.
739sub cmd_propget {
740 my ($prop, $path) = @_;
741 $path = '.' if not defined $path;
742 usage(1) if not defined $prop;
743 my $props = get_svnprops($path);
744 if (not defined $props->{$prop}) {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200745 fatal("`$path' does not have a `$prop' SVN property.");
Benoit Sigoure15153452007-10-16 16:36:50 +0200746 }
747 print $props->{$prop} . "\n";
748}
749
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200750# cmd_proplist (PATH)
751# -------------------
752# Print the list of SVN properties for PATH.
753sub cmd_proplist {
754 my $path = shift;
755 $path = '.' if not defined $path;
756 my $props = get_svnprops($path);
757 print "Properties on '$path':\n";
758 foreach (sort keys %{$props}) {
759 print " $_\n";
760 }
761}
762
Eric Wong8164b652007-01-11 15:35:55 -0800763sub cmd_multi_init {
Eric Wong9d55b412006-06-12 15:53:13 -0700764 my $url = shift;
Eric Wong98327e52007-01-04 18:02:00 -0800765 unless (defined $_trunk || defined $_branches || defined $_tags) {
766 usage(1);
767 }
Eric Wongdc431662007-05-19 03:59:02 -0700768
769 # there are currently some bugs that prevent multi-init/multi-fetch
770 # setups from working well without this.
771 $Git::SVN::_minimize_url = 1;
772
Eric Wong8164b652007-01-11 15:35:55 -0800773 $_prefix = '' unless defined $_prefix;
Eric Wongdadc6d22007-02-14 12:27:41 -0800774 if (defined $url) {
775 $url =~ s#/+$##;
776 init_subdir(@_);
777 }
Eric Wongf30603f2007-02-23 01:26:26 -0800778 do_git_init_db();
Eric Wong98327e52007-01-04 18:02:00 -0800779 if (defined $_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800780 my $trunk_ref = $_prefix . 'trunk';
781 # try both old-style and new-style lookups:
782 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
Eric Wong8164b652007-01-11 15:35:55 -0800783 unless ($gs_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800784 my ($trunk_url, $trunk_path) =
785 complete_svn_url($url, $_trunk);
786 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
787 undef, $trunk_ref);
Eric Wong98327e52007-01-04 18:02:00 -0800788 }
Eric Wongc35b96e2006-10-11 11:53:21 -0700789 }
Eric Wong706587f2007-01-18 17:50:01 -0800790 return unless defined $_branches || defined $_tags;
Eric Wonge7db67e2007-01-11 17:09:26 -0800791 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
792 complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix);
793 complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
Eric Wong9d55b412006-06-12 15:53:13 -0700794}
795
Eric Wong1c8443b2007-01-14 02:17:00 -0800796sub cmd_multi_fetch {
Eric Wong0af9c9f2007-01-27 22:28:56 -0800797 my $remotes = Git::SVN::read_all_remotes();
798 foreach my $repo_id (sort keys %$remotes) {
Eric Wongdb03cd22007-02-13 00:38:02 -0800799 if ($remotes->{$repo_id}->{url}) {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800800 Git::SVN::fetch_all($repo_id, $remotes);
801 }
Eric Wong706587f2007-01-18 17:50:01 -0800802 }
Eric Wong9d55b412006-06-12 15:53:13 -0700803}
804
Eric Wong44320b92007-01-13 22:35:53 -0800805# this command is special because it requires no metadata
806sub cmd_commit_diff {
807 my ($ta, $tb, $url) = @_;
808 my $usage = "Usage: $0 commit-diff -r<revision> ".
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200809 "<tree-ish> <tree-ish> [<URL>]";
Eric Wong44320b92007-01-13 22:35:53 -0800810 fatal($usage) if (!defined $ta || !defined $tb);
Karl Hasselströmd72ab8c2008-05-17 17:07:09 +0200811 my $svn_path = '';
Eric Wong44320b92007-01-13 22:35:53 -0800812 if (!defined $url) {
813 my $gs = eval { Git::SVN->new };
814 if (!$gs) {
815 fatal("Needed URL or usable git-svn --id in ",
816 "the command-line\n", $usage);
817 }
818 $url = $gs->{url};
Eric Wongd3a840d2007-01-26 01:32:45 -0800819 $svn_path = $gs->{path};
Eric Wong44320b92007-01-13 22:35:53 -0800820 }
821 unless (defined $_revision) {
822 fatal("-r|--revision is a required argument\n", $usage);
823 }
824 if (defined $_message && defined $_file) {
825 fatal("Both --message/-m and --file/-F specified ",
826 "for the commit message.\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200827 "I have no idea what you mean");
Eric Wong44320b92007-01-13 22:35:53 -0800828 }
829 if (defined $_file) {
830 $_message = file_to_s($_file);
831 } else {
832 $_message ||= get_commit_entry($tb)->{log};
833 }
834 my $ra ||= Git::SVN::Ra->new($url);
835 my $r = $_revision;
836 if ($r eq 'HEAD') {
837 $r = $ra->get_latest_revnum;
838 } elsif ($r !~ /^\d+$/) {
839 die "revision argument: $r not understood by git-svn\n";
840 }
Eric Wong61395352007-01-27 14:33:08 -0800841 my %ed_opts = ( r => $r,
842 log => $_message,
843 ra => $ra,
844 tree_a => $ta,
845 tree_b => $tb,
846 editor_cb => sub { print "Committed r$_[0]\n" },
847 svn_path => $svn_path );
848 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong44320b92007-01-13 22:35:53 -0800849 print "No changes\n$ta == $tb\n";
850 }
Eric Wong44320b92007-01-13 22:35:53 -0800851}
852
Thomas Rast05427b92008-08-26 21:32:37 +0200853sub escape_uri_only {
854 my ($uri) = @_;
855 my @tmp;
856 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -0700857 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Thomas Rast05427b92008-08-26 21:32:37 +0200858 push @tmp, $_;
859 }
860 join('/', @tmp);
861}
862
863sub escape_url {
864 my ($url) = @_;
865 if ($url =~ m#^([^:]+)://([^/]*)(.*)$#) {
866 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
867 $url = "$scheme://$domain$uri";
868 }
869 $url;
870}
871
David D. Kilzere6fefa92007-11-21 11:57:18 -0800872sub cmd_info {
Eric Wongbd2d4f92008-08-05 00:35:16 -0700873 my $path = canonicalize_path(defined($_[0]) ? $_[0] : ".");
Thomas Rastedde9112008-08-26 21:32:36 +0200874 my $fullpath = canonicalize_path($cmd_dir_prefix . $path);
Eric Wongbd2d4f92008-08-05 00:35:16 -0700875 if (exists $_[1]) {
David D. Kilzere6fefa92007-11-21 11:57:18 -0800876 die "Too many arguments specified\n";
877 }
878
879 my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
880
881 if (!$file_type && !$diff_status) {
Thomas Rast2cf3e3a2008-08-29 15:42:48 +0200882 print STDERR "svn: '$path' is not under version control\n";
883 exit 1;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800884 }
885
886 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
887 unless ($gs) {
888 die "Unable to determine upstream SVN information from ",
889 "working tree history\n";
890 }
Eric Wongbd2d4f92008-08-05 00:35:16 -0700891
892 # canonicalize_path() will return "" to make libsvn 1.5.x happy,
893 $path = "." if $path eq "";
894
Thomas Rastedde9112008-08-26 21:32:36 +0200895 my $full_url = $url . ($fullpath eq "" ? "" : "/$fullpath");
David D. Kilzere6fefa92007-11-21 11:57:18 -0800896
David D. Kilzer8b014d72007-11-21 11:57:19 -0800897 if ($_url) {
Thomas Rast05427b92008-08-26 21:32:37 +0200898 print escape_url($full_url), "\n";
David D. Kilzer8b014d72007-11-21 11:57:19 -0800899 return;
900 }
901
David D. Kilzere6fefa92007-11-21 11:57:18 -0800902 my $result = "Path: $path\n";
903 $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
Thomas Rast05427b92008-08-26 21:32:37 +0200904 $result .= "URL: " . escape_url($full_url) . "\n";
David D. Kilzere6fefa92007-11-21 11:57:18 -0800905
Eric Wonga5460eb2007-11-21 18:20:57 -0800906 eval {
907 my $repos_root = $gs->repos_root;
908 Git::SVN::remove_username($repos_root);
Thomas Rast05427b92008-08-26 21:32:37 +0200909 $result .= "Repository Root: " . escape_url($repos_root) . "\n";
Eric Wonga5460eb2007-11-21 18:20:57 -0800910 };
911 if ($@) {
912 $result .= "Repository Root: (offline)\n";
913 }
David D. Kilzere6fefa92007-11-21 11:57:18 -0800914 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A";
915 $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
916
917 $result .= "Node Kind: " .
918 ($file_type eq "dir" ? "directory" : "file") . "\n";
919
920 my $schedule = $diff_status eq "A"
921 ? "add"
922 : ($diff_status eq "D" ? "delete" : "normal");
923 $result .= "Schedule: $schedule\n";
924
925 if ($diff_status eq "A") {
926 print $result, "\n";
927 return;
928 }
929
930 my ($lc_author, $lc_rev, $lc_date_utc);
Thomas Rastedde9112008-08-26 21:32:36 +0200931 my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath);
David D. Kilzere6fefa92007-11-21 11:57:18 -0800932 my $log = command_output_pipe(@args);
933 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
934 while (<$log>) {
935 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
936 $lc_author = $1;
937 $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
938 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
939 (undef, $lc_rev, undef) = ::extract_metadata($1);
940 }
941 }
942 close $log;
943
944 Git::SVN::Log::set_local_timezone();
945
946 $result .= "Last Changed Author: $lc_author\n";
947 $result .= "Last Changed Rev: $lc_rev\n";
948 $result .= "Last Changed Date: " .
949 Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
950
951 if ($file_type ne "dir") {
952 my $text_last_updated_date =
953 ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
954 $result .=
955 "Text Last Updated: " .
956 Git::SVN::Log::format_svn_date($text_last_updated_date) .
957 "\n";
958 my $checksum;
959 if ($diff_status eq "D") {
960 my ($fh, $ctx) =
961 command_output_pipe(qw(cat-file blob), "HEAD:$path");
962 if ($file_type eq "link") {
963 my $file_name = <$fh>;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -0800964 $checksum = md5sum("link $file_name");
David D. Kilzere6fefa92007-11-21 11:57:18 -0800965 } else {
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -0800966 $checksum = md5sum($fh);
David D. Kilzere6fefa92007-11-21 11:57:18 -0800967 }
968 command_close_pipe($fh, $ctx);
969 } elsif ($file_type eq "link") {
970 my $file_name =
971 command(qw(cat-file blob), "HEAD:$path");
972 $checksum =
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -0800973 md5sum("link " . $file_name);
David D. Kilzere6fefa92007-11-21 11:57:18 -0800974 } else {
975 open FILE, "<", $path or die $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -0800976 $checksum = md5sum(\*FILE);
David D. Kilzere6fefa92007-11-21 11:57:18 -0800977 close FILE or die $!;
978 }
979 $result .= "Checksum: " . $checksum . "\n";
980 }
981
982 print $result, "\n";
983}
984
Eric Wong3397f9d2006-02-16 01:24:16 -0800985########################### utility functions #########################
986
Eric Wong905f8b72007-02-16 03:22:40 -0800987sub rebase_cmd {
988 my @cmd = qw/rebase/;
989 push @cmd, '-v' if $_verbose;
990 push @cmd, qw/--merge/ if $_merge;
991 push @cmd, "--strategy=$_strategy" if $_strategy;
992 @cmd;
993}
994
Eric Wong1e889ef2007-02-16 01:45:13 -0800995sub post_fetch_checkout {
996 return if $_no_checkout;
997 my $gs = $Git::SVN::_head or return;
998 return if verify_ref('refs/heads/master^0');
999
1000 my $valid_head = verify_ref('HEAD^0');
1001 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
1002 return if ($valid_head || !verify_ref('HEAD^0'));
1003
1004 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
1005 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
1006 return if -f $index;
1007
Matthias Lederhofer7ae3df82007-06-03 16:48:16 +02001008 return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
Eric Wong1e889ef2007-02-16 01:45:13 -08001009 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
1010 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
1011 print STDERR "Checked out HEAD:\n ",
1012 $gs->full_url, " r", $gs->last_rev, "\n";
1013}
1014
Eric Wong98327e52007-01-04 18:02:00 -08001015sub complete_svn_url {
1016 my ($url, $path) = @_;
1017 $path =~ s#/+$##;
Eric Wong98327e52007-01-04 18:02:00 -08001018 if ($path !~ m#^[a-z\+]+://#) {
Eric Wong98327e52007-01-04 18:02:00 -08001019 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
1020 fatal("E: '$path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001021 "and a separate URL is not specified");
Eric Wong98327e52007-01-04 18:02:00 -08001022 }
Eric Wong706587f2007-01-18 17:50:01 -08001023 return ($url, $path);
Eric Wong98327e52007-01-04 18:02:00 -08001024 }
Eric Wong706587f2007-01-18 17:50:01 -08001025 return ($path, '');
Eric Wong98327e52007-01-04 18:02:00 -08001026}
1027
Eric Wong9d55b412006-06-12 15:53:13 -07001028sub complete_url_ls_init {
Eric Wong706587f2007-01-18 17:50:01 -08001029 my ($ra, $repo_path, $switch, $pfx) = @_;
1030 unless ($repo_path) {
Eric Wong9d55b412006-06-12 15:53:13 -07001031 print STDERR "W: $switch not specified\n";
1032 return;
1033 }
Eric Wong706587f2007-01-18 17:50:01 -08001034 $repo_path =~ s#/+$##;
1035 if ($repo_path =~ m#^[a-z\+]+://#) {
1036 $ra = Git::SVN::Ra->new($repo_path);
1037 $repo_path = '';
Eric Wonge7db67e2007-01-11 17:09:26 -08001038 } else {
Eric Wong706587f2007-01-18 17:50:01 -08001039 $repo_path =~ s#^/+##;
Eric Wonge7db67e2007-01-11 17:09:26 -08001040 unless ($ra) {
Eric Wong706587f2007-01-18 17:50:01 -08001041 fatal("E: '$repo_path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001042 "and a separate URL is not specified");
Eric Wong9d55b412006-06-12 15:53:13 -07001043 }
Eric Wonge7db67e2007-01-11 17:09:26 -08001044 }
Eric Wong706587f2007-01-18 17:50:01 -08001045 my $url = $ra->{url};
Eric Wongb4d57e52007-02-14 15:10:44 -08001046 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
1047 my $k = "svn-remote.$gs->{repo_id}.url";
1048 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
1049 if ($orig_url && ($orig_url ne $gs->{url})) {
1050 die "$k already set: $orig_url\n",
1051 "wanted to set to: $gs->{url}\n";
Eric Wong88cf4102007-02-01 03:59:07 -08001052 }
Eric Wongb4d57e52007-02-14 15:10:44 -08001053 command_oneline('config', $k, $gs->{url}) unless $orig_url;
Eric Wonged0b9d42008-03-14 11:01:23 -07001054 my $remote_path = "$ra->{svn_path}/$repo_path";
Eric Wongb4d57e52007-02-14 15:10:44 -08001055 $remote_path =~ s#/+#/#g;
1056 $remote_path =~ s#^/##g;
Eric Wonged0b9d42008-03-14 11:01:23 -07001057 $remote_path .= "/*" if $remote_path !~ /\*/;
Eric Wongb4d57e52007-02-14 15:10:44 -08001058 my ($n) = ($switch =~ /^--(\w+)/);
1059 if (length $pfx && $pfx !~ m#/$#) {
1060 die "--prefix='$pfx' must have a trailing slash '/'\n";
Eric Wong9d55b412006-06-12 15:53:13 -07001061 }
Marcus Griep570d35c2008-08-08 01:41:57 -07001062 command_noisy('config',
1063 "svn-remote.$gs->{repo_id}.$n",
1064 "$remote_path:refs/remotes/$pfx*" .
1065 ('/*' x (($remote_path =~ tr/*/*/) - 1)) );
Eric Wong9d55b412006-06-12 15:53:13 -07001066}
1067
Eric Wongaef4e922006-12-15 10:59:54 -08001068sub verify_ref {
1069 my ($ref) = @_;
Eric Wong2c5c1d52006-12-28 01:16:21 -08001070 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
1071 { STDERR => 0 }); };
Eric Wongaef4e922006-12-15 10:59:54 -08001072}
1073
Eric Wonga5e0ced2006-06-12 15:23:48 -07001074sub get_tree_from_treeish {
Eric Wongcf52b8f2006-02-20 10:57:28 -08001075 my ($treeish) = @_;
Eric Wong44320b92007-01-13 22:35:53 -08001076 # $treeish can be a symbolic ref, too:
Eric Wongaef4e922006-12-15 10:59:54 -08001077 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001078 my $expected;
1079 while ($type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001080 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001081 }
1082 if ($type eq 'commit') {
Eric Wongaef4e922006-12-15 10:59:54 -08001083 $expected = (grep /^tree /, command(qw/cat-file commit/,
1084 $treeish))[0];
Eric Wong44320b92007-01-13 22:35:53 -08001085 ($expected) = ($expected =~ /^tree ($sha1)$/o);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001086 die "Unable to get tree from $treeish\n" unless $expected;
1087 } elsif ($type eq 'tree') {
1088 $expected = $treeish;
1089 } else {
1090 die "$treeish is a $type, expected tree, tag or commit\n";
1091 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07001092 return $expected;
1093}
Eric Wongcf52b8f2006-02-20 10:57:28 -08001094
Eric Wong44320b92007-01-13 22:35:53 -08001095sub get_commit_entry {
1096 my ($treeish) = shift;
1097 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
1098 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1099 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1100 open my $log_fh, '>', $commit_editmsg or croak $!;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001101
Eric Wong44320b92007-01-13 22:35:53 -08001102 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wong4ad45152006-07-09 20:20:48 -07001103 if ($type eq 'commit' || $type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001104 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
Eric Wong44320b92007-01-13 22:35:53 -08001105 $type, $treeish);
Eric Wong3397f9d2006-02-16 01:24:16 -08001106 my $in_msg = 0;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001107 my $author;
1108 my $saw_from = 0;
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001109 my $msgbuf = "";
Eric Wong3397f9d2006-02-16 01:24:16 -08001110 while (<$msg_fh>) {
1111 if (!$in_msg) {
1112 $in_msg = 1 if (/^\s*$/);
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001113 $author = $1 if (/^author (.*>)/);
Eric Wongdf746c52006-03-03 01:20:08 -08001114 } elsif (/^git-svn-id: /) {
Eric Wong44320b92007-01-13 22:35:53 -08001115 # skip this for now, we regenerate the
1116 # correct one on re-fetch anyways
1117 # TODO: set *:merge properties or like...
Eric Wong3397f9d2006-02-16 01:24:16 -08001118 } else {
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001119 if (/^From:/ || /^Signed-off-by:/) {
1120 $saw_from = 1;
1121 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001122 $msgbuf .= $_;
Eric Wong3397f9d2006-02-16 01:24:16 -08001123 }
1124 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001125 $msgbuf =~ s/\s+$//s;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001126 if ($Git::SVN::_add_author_from && defined($author)
1127 && !$saw_from) {
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001128 $msgbuf .= "\n\nFrom: $author";
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001129 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001130 print $log_fh $msgbuf or croak $!;
Eric Wongaef4e922006-12-15 10:59:54 -08001131 command_close_pipe($msg_fh, $ctx);
Eric Wong3397f9d2006-02-16 01:24:16 -08001132 }
Eric Wong44320b92007-01-13 22:35:53 -08001133 close $log_fh or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -08001134
1135 if ($_edit || ($type eq 'tree')) {
1136 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
Eric Wong44320b92007-01-13 22:35:53 -08001137 # TODO: strip out spaces, comments, like git-commit.sh
1138 system($editor, $commit_editmsg);
Eric Wong3397f9d2006-02-16 01:24:16 -08001139 }
Eric Wong44320b92007-01-13 22:35:53 -08001140 rename $commit_editmsg, $commit_msg or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07001141 {
1142 # SVN requires messages to be UTF-8 when entering the repo
1143 local $/;
1144 open $log_fh, '<', $commit_msg or croak $!;
1145 binmode $log_fh;
1146 chomp($log_entry{log} = <$log_fh>);
1147
1148 if (my $enc = Git::config('i18n.commitencoding')) {
1149 require Encode;
1150 Encode::from_to($log_entry{log}, $enc, 'UTF-8');
1151 }
1152 close $log_fh or croak $!;
1153 }
Eric Wong44320b92007-01-13 22:35:53 -08001154 unlink $commit_msg;
1155 \%log_entry;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001156}
1157
Eric Wong3397f9d2006-02-16 01:24:16 -08001158sub s_to_file {
1159 my ($str, $file, $mode) = @_;
1160 open my $fd,'>',$file or croak $!;
1161 print $fd $str,"\n" or croak $!;
1162 close $fd or croak $!;
1163 chmod ($mode &~ umask, $file) if (defined $mode);
1164}
1165
1166sub file_to_s {
1167 my $file = shift;
1168 open my $fd,'<',$file or croak "$!: file: $file\n";
1169 local $/;
1170 my $ret = <$fd>;
1171 close $fd or croak $!;
1172 $ret =~ s/\s*$//s;
1173 return $ret;
1174}
1175
Eric Wongeeb0abe2006-03-03 01:20:08 -08001176# '<svn username> = real-name <email address>' mapping based on git-svnimport:
1177sub load_authors {
1178 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001179 my $log = $cmd eq 'log';
Eric Wongeeb0abe2006-03-03 01:20:08 -08001180 while (<$authors>) {
1181 chomp;
Richard MUSIL575d0252007-07-17 19:02:57 +02001182 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
Eric Wongeeb0abe2006-03-03 01:20:08 -08001183 my ($user, $name, $email) = ($1, $2, $3);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001184 if ($log) {
1185 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
1186 } else {
1187 $users{$user} = [$name, $email];
1188 }
Eric Wong79bb8d82006-06-01 02:35:44 -07001189 }
1190 close $authors or croak $!;
1191}
1192
Tom Princee0d10e12007-01-28 16:16:53 -08001193# convert GetOpt::Long specs for use by git-config
Eric Wongb8c92ca2006-05-24 01:40:37 -07001194sub read_repo_config {
Eric Wong706587f2007-01-18 17:50:01 -08001195 return unless -d $ENV{GIT_DIR};
Eric Wongb8c92ca2006-05-24 01:40:37 -07001196 my $opts = shift;
Eric Wong97ae0912007-02-11 15:21:24 -08001197 my @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001198 foreach my $o (keys %$opts) {
Eric Wong97ae0912007-02-11 15:21:24 -08001199 # if we have mixedCase and a long option-only, then
1200 # it's a config-only variable that we don't need for
1201 # the command-line.
1202 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001203 my $v = $opts->{$o};
Eric Wong97ae0912007-02-11 15:21:24 -08001204 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001205 $key =~ s/-//g;
Deskin Miller225f1d02008-10-23 15:21:34 -04001206 my $arg = 'git config';
Eric Wongb8c92ca2006-05-24 01:40:37 -07001207 $arg .= ' --int' if ($o =~ /[:=]i$/);
1208 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1209 if (ref $v eq 'ARRAY') {
1210 chomp(my @tmp = `$arg --get-all svn.$key`);
1211 @$v = @tmp if @tmp;
1212 } else {
1213 chomp(my $tmp = `$arg --get svn.$key`);
Eric Wong77742842007-02-10 21:07:12 -08001214 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
Eric Wongb8c92ca2006-05-24 01:40:37 -07001215 $$v = $tmp;
1216 }
1217 }
1218 }
Eric Wong97ae0912007-02-11 15:21:24 -08001219 delete @$opts{@config_only} if @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001220}
1221
Eric Wong79bb8d82006-06-01 02:35:44 -07001222sub extract_metadata {
Eric Wongc1927a82006-06-27 19:39:11 -07001223 my $id = shift or return (undef, undef, undef);
Sam Vilain3dfab992007-06-30 20:56:13 +12001224 my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
Eric Wong79bb8d82006-06-01 02:35:44 -07001225 \s([a-f\d\-]+)$/x);
Eric Wonge70dc782006-11-23 14:54:04 -08001226 if (!defined $rev || !$uuid || !$url) {
Eric Wong79bb8d82006-06-01 02:35:44 -07001227 # some of the original repositories I made had
Pavel Roskin82e5a822006-07-10 01:50:18 -04001228 # identifiers like this:
Sam Vilain3dfab992007-06-30 20:56:13 +12001229 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
Eric Wong79bb8d82006-06-01 02:35:44 -07001230 }
1231 return ($url, $rev, $uuid);
1232}
1233
Eric Wongc1927a82006-06-27 19:39:11 -07001234sub cmt_metadata {
1235 return extract_metadata((grep(/^git-svn-id: /,
Eric Wongaef4e922006-12-15 10:59:54 -08001236 command(qw/cat-file commit/, shift)))[-1]);
Eric Wongc1927a82006-06-27 19:39:11 -07001237}
1238
Eric Wong905f8b72007-02-16 03:22:40 -08001239sub working_head_info {
1240 my ($head, $refs) = @_;
Pedro Melob6309ac2008-04-10 17:05:21 +01001241 my @args = ('log', '--no-color', '--first-parent', '--pretty=medium');
Lars Hjemli05b4df32007-09-05 11:35:29 +02001242 my ($fh, $ctx) = command_output_pipe(@args, $head);
Sam Vilain3dfab992007-06-30 20:56:13 +12001243 my $hash;
Sam Vilain40cb8f82007-06-30 20:56:14 +12001244 my %max;
Sam Vilain3dfab992007-06-30 20:56:13 +12001245 while (<$fh>) {
1246 if ( m{^commit ($::sha1)$} ) {
1247 unshift @$refs, $hash if $hash and $refs;
1248 $hash = $1;
1249 next;
1250 }
1251 next unless s{^\s*(git-svn-id:)}{$1};
1252 my ($url, $rev, $uuid) = extract_metadata($_);
Eric Wong13c823f2007-04-08 00:59:19 -07001253 if (defined $url && defined $rev) {
Sam Vilain40cb8f82007-06-30 20:56:14 +12001254 next if $max{$url} and $max{$url} < $rev;
Eric Wong13c823f2007-04-08 00:59:19 -07001255 if (my $gs = Git::SVN->find_by_url($url)) {
João Abecasis63c56022008-07-14 16:28:04 +01001256 my $c = $gs->rev_map_get($rev, $uuid);
Adam Robenb03c7a62007-04-25 11:50:32 -07001257 if ($c && $c eq $hash) {
Eric Wong13c823f2007-04-08 00:59:19 -07001258 close $fh; # break the pipe
1259 return ($url, $rev, $uuid, $gs);
Sam Vilain40cb8f82007-06-30 20:56:14 +12001260 } else {
Eric Wong060610c2007-12-08 23:27:41 -08001261 $max{$url} ||= $gs->rev_map_max;
Eric Wong13c823f2007-04-08 00:59:19 -07001262 }
1263 }
1264 }
Eric Wong905f8b72007-02-16 03:22:40 -08001265 }
Eric Wong13c823f2007-04-08 00:59:19 -07001266 command_close_pipe($fh, $ctx);
1267 (undef, undef, undef, undef);
Eric Wong905f8b72007-02-16 03:22:40 -08001268}
1269
Eric Wong733a65a2007-06-13 02:23:28 -07001270sub read_commit_parents {
1271 my ($parents, $c) = @_;
Eric Wong7b02b852007-09-08 16:33:08 -07001272 chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
1273 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1274 @{$parents->{$c}} = split(/ /, $p);
Eric Wong733a65a2007-06-13 02:23:28 -07001275}
1276
1277sub linearize_history {
1278 my ($gs, $refs) = @_;
1279 my %parents;
1280 foreach my $c (@$refs) {
1281 read_commit_parents(\%parents, $c);
1282 }
1283
1284 my @linear_refs;
1285 my %skip = ();
1286 my $last_svn_commit = $gs->last_commit;
1287 foreach my $c (reverse @$refs) {
1288 next if $c eq $last_svn_commit;
1289 last if $skip{$c};
1290
1291 unshift @linear_refs, $c;
1292 $skip{$c} = 1;
1293
1294 # we only want the first parent to diff against for linear
1295 # history, we save the rest to inject when we finalize the
1296 # svn commit
1297 my $fp_a = verify_ref("$c~1");
1298 my $fp_b = shift @{$parents{$c}} if $parents{$c};
1299 if (!$fp_a || !$fp_b) {
1300 die "Commit $c\n",
1301 "has no parent commit, and therefore ",
1302 "nothing to diff against.\n",
1303 "You should be working from a repository ",
1304 "originally created by git-svn\n";
1305 }
1306 if ($fp_a ne $fp_b) {
1307 die "$c~1 = $fp_a, however parsing commit $c ",
1308 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1309 }
1310
1311 foreach my $p (@{$parents{$c}}) {
1312 $skip{$p} = 1;
1313 }
1314 }
1315 (\@linear_refs, \%parents);
1316}
1317
David D. Kilzere6fefa92007-11-21 11:57:18 -08001318sub find_file_type_and_diff_status {
1319 my ($path) = @_;
Dmitry Potapov107cee52008-07-21 00:14:07 +04001320 return ('dir', '') if $path eq '';
David D. Kilzere6fefa92007-11-21 11:57:18 -08001321
1322 my $diff_output =
1323 command_oneline(qw(diff --cached --name-status --), $path) || "";
1324 my $diff_status = (split(' ', $diff_output))[0] || "";
1325
1326 my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || "";
1327
1328 return (undef, undef) if !$diff_status && !$ls_tree;
1329
1330 if ($diff_status eq "A") {
1331 return ("link", $diff_status) if -l $path;
1332 return ("dir", $diff_status) if -d $path;
1333 return ("file", $diff_status);
1334 }
1335
1336 my $mode = (split(' ', $ls_tree))[0] || "";
1337
1338 return ("link", $diff_status) if $mode eq "120000";
1339 return ("dir", $diff_status) if $mode eq "040000";
1340 return ("file", $diff_status);
1341}
1342
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001343sub md5sum {
1344 my $arg = shift;
1345 my $ref = ref $arg;
1346 my $md5 = Digest::MD5->new();
Marcus Griep0b191382008-08-12 12:00:53 -04001347 if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001348 $md5->addfile($arg) or croak $!;
1349 } elsif ($ref eq 'SCALAR') {
1350 $md5->add($$arg) or croak $!;
1351 } elsif (!$ref) {
1352 $md5->add($arg) or croak $!;
1353 } else {
1354 ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1355 }
1356 return $md5->hexdigest();
1357}
1358
Eric Wong9b981fc2007-01-11 12:14:21 -08001359package Git::SVN;
1360use strict;
1361use warnings;
Eric Wong060610c2007-12-08 23:27:41 -08001362use Fcntl qw/:DEFAULT :seek/;
1363use constant rev_map_fmt => 'NH40';
Eric Wongecc712d2007-01-31 12:28:10 -08001364use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
Eric Wong62e349d2007-02-16 19:57:29 -08001365 $_repack $_repack_flags $_use_svm_props $_head
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00001366 $_use_svnsync_props $no_reuse_existing $_minimize_url
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001367 $_use_log_author $_add_author_from/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001368use Carp qw/croak/;
1369use File::Path qw/mkpath/;
Eric Wong373274f2007-01-31 13:54:23 -08001370use File::Copy qw/copy/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001371use IPC::Open3;
1372
Karl Hasselström94bc9142008-02-03 17:56:18 +01001373my ($_gc_nr, $_gc_period);
1374
Eric Wong9b981fc2007-01-11 12:14:21 -08001375# properties that we do not log:
1376my %SKIP_PROP;
1377BEGIN {
1378 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
1379 svn:special svn:executable
1380 svn:entry:committed-rev
1381 svn:entry:last-author
1382 svn:entry:uuid
1383 svn:entry:committed-date/;
Eric Wong91b03282007-02-11 00:51:33 -08001384
1385 # some options are read globally, but can be overridden locally
1386 # per [svn-remote "..."] section. Command-line options will *NOT*
1387 # override options set in an [svn-remote "..."] section
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001388 no strict 'refs';
1389 for my $option (qw/follow_parent no_metadata use_svm_props
1390 use_svnsync_props/) {
1391 my $key = $option;
Eric Wong91b03282007-02-11 00:51:33 -08001392 $key =~ tr/_//d;
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001393 my $prop = "-$option";
1394 *$option = sub {
1395 my ($self) = @_;
1396 return $self->{$prop} if exists $self->{$prop};
1397 my $k = "svn-remote.$self->{repo_id}.$key";
1398 eval { command_oneline(qw/config --get/, $k) };
1399 if ($@) {
1400 $self->{$prop} = ${"Git::SVN::_$option"};
Eric Wong91b03282007-02-11 00:51:33 -08001401 } else {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001402 my $v = command_oneline(qw/config --bool/,$k);
1403 $self->{$prop} = $v eq 'false' ? 0 : 1;
Eric Wong91b03282007-02-11 00:51:33 -08001404 }
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001405 return $self->{$prop};
1406 }
Eric Wong91b03282007-02-11 00:51:33 -08001407 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001408}
1409
Marcus Griep0b191382008-08-12 12:00:53 -04001410
Eric Wong321b1842008-01-02 10:10:03 -08001411my (%LOCKFILES, %INDEX_FILES);
1412END {
1413 unlink keys %LOCKFILES if %LOCKFILES;
1414 unlink keys %INDEX_FILES if %INDEX_FILES;
1415}
Eric Wong373274f2007-01-31 13:54:23 -08001416
Eric Wong4bb9ed02007-02-03 13:29:17 -08001417sub resolve_local_globs {
1418 my ($url, $fetch, $glob_spec) = @_;
1419 return unless defined $glob_spec;
1420 my $ref = $glob_spec->{ref};
1421 my $path = $glob_spec->{path};
1422 foreach (command(qw#for-each-ref --format=%(refname) refs/remotes#)) {
1423 next unless m#^refs/remotes/$ref->{regex}$#;
1424 my $p = $1;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001425 my $pathname = desanitize_refname($path->full_path($p));
1426 my $refname = desanitize_refname($ref->full_path($p));
Eric Wong4bb9ed02007-02-03 13:29:17 -08001427 if (my $existing = $fetch->{$pathname}) {
1428 if ($existing ne $refname) {
1429 die "Refspec conflict:\n",
1430 "existing: refs/remotes/$existing\n",
1431 " globbed: refs/remotes/$refname\n";
1432 }
1433 my $u = (::cmt_metadata("refs/remotes/$refname"))[0];
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001434 $u =~ s!^\Q$url\E(/|$)!! or die
Eric Wong4bb9ed02007-02-03 13:29:17 -08001435 "refs/remotes/$refname: '$url' not found in '$u'\n";
1436 if ($pathname ne $u) {
1437 warn "W: Refspec glob conflict ",
1438 "(ref: refs/remotes/$refname):\n",
1439 "expected path: $pathname\n",
1440 " real path: $u\n",
1441 "Continuing ahead with $u\n";
1442 next;
1443 }
1444 } else {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001445 $fetch->{$pathname} = $refname;
1446 }
1447 }
1448}
1449
Eric Wonge98671e2007-02-14 02:21:19 -08001450sub parse_revision_argument {
1451 my ($base, $head) = @_;
1452 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
1453 return ($base, $head);
1454 }
1455 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
1456 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
1457 return ($head, $head) if ($::_revision eq 'HEAD');
1458 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
1459 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
1460 die "revision argument: $::_revision not understood by git-svn\n";
1461}
1462
Eric Wong0af9c9f2007-01-27 22:28:56 -08001463sub fetch_all {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001464 my ($repo_id, $remotes) = @_;
Eric Wong905f8b72007-02-16 03:22:40 -08001465 if (ref $repo_id) {
1466 my $gs = $repo_id;
1467 $repo_id = undef;
1468 $repo_id = $gs->{repo_id};
1469 }
1470 $remotes ||= read_all_remotes();
Eric Wong7447b4b2007-02-14 18:38:46 -08001471 my $remote = $remotes->{$repo_id} or
1472 die "[svn-remote \"$repo_id\"] unknown\n";
Eric Wonge5181922007-02-08 12:53:57 -08001473 my $fetch = $remote->{fetch};
Eric Wong7447b4b2007-02-14 18:38:46 -08001474 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
Eric Wonge5181922007-02-08 12:53:57 -08001475 my (@gs, @globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001476 my $ra = Git::SVN::Ra->new($url);
Eric Wong26a62d52007-02-12 13:25:25 -08001477 my $uuid = $ra->get_uuid;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001478 my $head = $ra->get_latest_revnum;
Eric Wong28710f72007-02-14 13:32:21 -08001479 my $base = defined $fetch ? $head : 0;
Eric Wonge5181922007-02-08 12:53:57 -08001480
1481 # read the max revs for wildcard expansion (branches/*, tags/*)
1482 foreach my $t (qw/branches tags/) {
1483 defined $remote->{$t} or next;
1484 push @globs, $remote->{$t};
Eric Wong93f26892007-02-11 01:20:26 -08001485 my $max_rev = eval { tmp_config(qw/--int --get/,
1486 "svn-remote.$repo_id.${t}-maxRev") };
1487 if (defined $max_rev && ($max_rev < $base)) {
1488 $base = $max_rev;
Eric Wongd6d33462007-02-16 04:05:33 -08001489 } elsif (!defined $max_rev) {
1490 $base = 0;
Eric Wonge5181922007-02-08 12:53:57 -08001491 }
1492 }
1493
Eric Wongdb03cd22007-02-13 00:38:02 -08001494 if ($fetch) {
1495 foreach my $p (sort keys %$fetch) {
1496 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
Eric Wong060610c2007-12-08 23:27:41 -08001497 my $lr = $gs->rev_map_max;
Eric Wongdb03cd22007-02-13 00:38:02 -08001498 if (defined $lr) {
1499 $base = $lr if ($lr < $base);
1500 }
1501 push @gs, $gs;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001502 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08001503 }
Eric Wonge98671e2007-02-14 02:21:19 -08001504
1505 ($base, $head) = parse_revision_argument($base, $head);
Eric Wonge5181922007-02-08 12:53:57 -08001506 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001507}
1508
Eric Wong47e39c52007-01-21 04:27:09 -08001509sub read_all_remotes {
1510 my $r = {};
João Abecasis63c56022008-07-14 16:28:04 +01001511 my $use_svm_props = eval { command_oneline(qw/config --bool
1512 svn.useSvmProps/) };
1513 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
Eric Wong8b8fc062007-01-22 11:44:57 -08001514 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
Avery Pennarun61192162008-07-30 16:53:55 -04001515 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*(.+)\s*$!) {
1516 my ($remote, $local_ref, $_remote_ref) = ($1, $2, $3);
1517 die("svn-remote.$remote: remote ref '$_remote_ref' "
1518 . "must start with 'refs/remotes/'\n")
1519 unless $_remote_ref =~ m{^refs/remotes/(.+)};
1520 my $remote_ref = $1;
Eric Wong46cf98b2007-07-14 12:40:32 -07001521 $local_ref =~ s{^/}{};
1522 $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
João Abecasis63c56022008-07-14 16:28:04 +01001523 $r->{$remote}->{svm} = {} if $use_svm_props;
1524 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
1525 $r->{$1}->{svm} = {};
Eric Wong47e39c52007-01-21 04:27:09 -08001526 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1527 $r->{$1}->{url} = $2;
Eric Wong4bb9ed02007-02-03 13:29:17 -08001528 } elsif (m!^(.+)\.(branches|tags)=
1529 (.*):refs/remotes/(.+)\s*$/!x) {
1530 my ($p, $g) = ($3, $4);
1531 my $rs = $r->{$1}->{$2} = {
Eric Wonge5181922007-02-08 12:53:57 -08001532 t => $2,
Eric Wong93f26892007-02-11 01:20:26 -08001533 remote => $1,
Eric Wong4bb9ed02007-02-03 13:29:17 -08001534 path => Git::SVN::GlobSpec->new($p),
1535 ref => Git::SVN::GlobSpec->new($g) };
1536 if (length($rs->{ref}->{right}) != 0) {
1537 die "The '*' glob character must be the last ",
1538 "character of '$g'\n";
1539 }
Eric Wong47e39c52007-01-21 04:27:09 -08001540 }
1541 }
João Abecasis63c56022008-07-14 16:28:04 +01001542
1543 map {
1544 if (defined $r->{$_}->{svm}) {
1545 my $svm;
1546 eval {
1547 my $section = "svn-remote.$_";
1548 $svm = {
1549 source => tmp_config('--get',
1550 "$section.svm-source"),
1551 replace => tmp_config('--get',
1552 "$section.svm-replace"),
1553 }
1554 };
1555 $r->{$_}->{svm} = $svm;
1556 }
1557 } keys %$r;
1558
Eric Wong47e39c52007-01-21 04:27:09 -08001559 $r;
1560}
1561
Eric Wongecc712d2007-01-31 12:28:10 -08001562sub init_vars {
Karl Hasselström94bc9142008-02-03 17:56:18 +01001563 $_gc_nr = $_gc_period = 1000;
Karl Hasselströmaf788a62008-02-03 17:56:12 +01001564 if (defined $_repack || defined $_repack_flags) {
1565 warn "Repack options are obsolete; they have no effect.\n";
1566 }
Eric Wongecc712d2007-01-31 12:28:10 -08001567}
1568
Eric Wongb805b442007-01-22 13:52:04 -08001569sub verify_remotes_sanity {
Eric Wong536c4b02007-01-23 11:35:53 -08001570 return unless -d $ENV{GIT_DIR};
Eric Wongb805b442007-01-22 13:52:04 -08001571 my %seen;
1572 foreach (command(qw/config -l/)) {
1573 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1574 if ($seen{$1}) {
1575 die "Remote ref refs/remote/$1 is tracked by",
1576 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1577 "Please resolve this ambiguity in ",
1578 "your git configuration file before ",
1579 "continuing\n";
1580 }
1581 $seen{$1} = $_;
1582 }
1583 }
1584}
1585
Eric Wonge6434f82007-01-23 16:29:23 -08001586sub find_existing_remote {
1587 my ($url, $remotes) = @_;
Eric Wongbefc9ad2007-02-17 02:53:07 -08001588 return undef if $no_reuse_existing;
Eric Wonge6434f82007-01-23 16:29:23 -08001589 my $existing;
1590 foreach my $repo_id (keys %$remotes) {
1591 my $u = $remotes->{$repo_id}->{url} or next;
1592 next if $u ne $url;
1593 $existing = $repo_id;
1594 last;
1595 }
1596 $existing;
1597}
1598
1599sub init_remote_config {
Eric Wongd8115c52007-02-01 03:30:31 -08001600 my ($self, $url, $no_write) = @_;
Eric Wonge6434f82007-01-23 16:29:23 -08001601 $url =~ s!/+$!!; # strip trailing slash
1602 my $r = read_all_remotes();
1603 my $existing = find_existing_remote($url, $r);
1604 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001605 unless ($no_write) {
1606 print STDERR "Using existing ",
1607 "[svn-remote \"$existing\"]\n";
1608 }
Eric Wonge6434f82007-01-23 16:29:23 -08001609 $self->{repo_id} = $existing;
Eric Wong4a1bb4c2007-05-13 09:58:14 -07001610 } elsif ($_minimize_url) {
Eric Wonge6434f82007-01-23 16:29:23 -08001611 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
1612 $existing = find_existing_remote($min_url, $r);
1613 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001614 unless ($no_write) {
1615 print STDERR "Using existing ",
1616 "[svn-remote \"$existing\"]\n";
1617 }
Eric Wonge6434f82007-01-23 16:29:23 -08001618 $self->{repo_id} = $existing;
1619 }
1620 if ($min_url ne $url) {
Eric Wonge5181922007-02-08 12:53:57 -08001621 unless ($no_write) {
1622 print STDERR "Using higher level of URL: ",
1623 "$url => $min_url\n";
1624 }
Eric Wonge6434f82007-01-23 16:29:23 -08001625 my $old_path = $self->{path};
1626 $self->{path} = $url;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001627 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
Eric Wonge6434f82007-01-23 16:29:23 -08001628 if (length $old_path) {
1629 $self->{path} .= "/$old_path";
1630 }
1631 $url = $min_url;
1632 }
1633 }
1634 my $orig_url;
1635 if (!$existing) {
1636 # verify that we aren't overwriting anything:
1637 $orig_url = eval {
1638 command_oneline('config', '--get',
1639 "svn-remote.$self->{repo_id}.url")
1640 };
1641 if ($orig_url && ($orig_url ne $url)) {
1642 die "svn-remote.$self->{repo_id}.url already set: ",
1643 "$orig_url\nwanted to set to: $url\n";
1644 }
1645 }
1646 my ($xrepo_id, $xpath) = find_ref($self->refname);
1647 if (defined $xpath) {
1648 die "svn-remote.$xrepo_id.fetch already set to track ",
1649 "$xpath:refs/remotes/", $self->refname, "\n";
1650 }
Eric Wongd8115c52007-02-01 03:30:31 -08001651 unless ($no_write) {
1652 command_noisy('config',
1653 "svn-remote.$self->{repo_id}.url", $url);
Eric Wong46cf98b2007-07-14 12:40:32 -07001654 $self->{path} =~ s{^/}{};
Eric Wongd8115c52007-02-01 03:30:31 -08001655 command_noisy('config', '--add',
1656 "svn-remote.$self->{repo_id}.fetch",
1657 "$self->{path}:".$self->refname);
1658 }
Eric Wonge6434f82007-01-23 16:29:23 -08001659 $self->{url} = $url;
1660}
1661
Eric Wonga8ae2622007-02-13 14:22:11 -08001662sub find_by_url { # repos_root and, path are optional
1663 my ($class, $full_url, $repos_root, $path) = @_;
Adam Roben56973d22007-04-25 12:42:58 -07001664
Eric Wong1a97a502007-02-20 00:43:19 -08001665 return undef unless defined $full_url;
Adam Roben56973d22007-04-25 12:42:58 -07001666 remove_username($full_url);
1667 remove_username($repos_root) if defined $repos_root;
Eric Wonga8ae2622007-02-13 14:22:11 -08001668 my $remotes = read_all_remotes();
1669 if (defined $full_url && defined $repos_root && !defined $path) {
1670 $path = $full_url;
1671 $path =~ s#^\Q$repos_root\E(?:/|$)##;
1672 }
1673 foreach my $repo_id (keys %$remotes) {
1674 my $u = $remotes->{$repo_id}->{url} or next;
Adam Roben56973d22007-04-25 12:42:58 -07001675 remove_username($u);
Eric Wonga8ae2622007-02-13 14:22:11 -08001676 next if defined $repos_root && $repos_root ne $u;
1677
1678 my $fetch = $remotes->{$repo_id}->{fetch} || {};
1679 foreach (qw/branches tags/) {
1680 resolve_local_globs($u, $fetch,
1681 $remotes->{$repo_id}->{$_});
1682 }
1683 my $p = $path;
John Goerzen0bb91d92008-03-08 16:04:05 -06001684 my $rwr = rewrite_root({repo_id => $repo_id});
João Abecasis63c56022008-07-14 16:28:04 +01001685 my $svm = $remotes->{$repo_id}->{svm}
1686 if defined $remotes->{$repo_id}->{svm};
Eric Wonga8ae2622007-02-13 14:22:11 -08001687 unless (defined $p) {
1688 $p = $full_url;
John Goerzen0bb91d92008-03-08 16:04:05 -06001689 my $z = $u;
João Abecasis63c56022008-07-14 16:28:04 +01001690 my $prefix = '';
John Goerzen0bb91d92008-03-08 16:04:05 -06001691 if ($rwr) {
1692 $z = $rwr;
João Abecasis63c56022008-07-14 16:28:04 +01001693 } elsif (defined $svm) {
1694 $z = $svm->{source};
1695 $prefix = $svm->{replace};
1696 $prefix =~ s#^\Q$u\E(?:/|$)##;
1697 $prefix =~ s#/$##;
John Goerzen0bb91d92008-03-08 16:04:05 -06001698 }
João Abecasis63c56022008-07-14 16:28:04 +01001699 $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next;
Eric Wonga8ae2622007-02-13 14:22:11 -08001700 }
1701 foreach my $f (keys %$fetch) {
1702 next if $f ne $p;
1703 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
1704 }
1705 }
1706 undef;
1707}
1708
Eric Wong9b981fc2007-01-11 12:14:21 -08001709sub init {
Eric Wongd8115c52007-02-01 03:30:31 -08001710 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08001711 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong9b981fc2007-01-11 12:14:21 -08001712 if (defined $url) {
Eric Wongd8115c52007-02-01 03:30:31 -08001713 $self->init_remote_config($url, $no_write);
Eric Wong9b981fc2007-01-11 12:14:21 -08001714 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001715 $self;
1716}
1717
Eric Wong706587f2007-01-18 17:50:01 -08001718sub find_ref {
1719 my ($ref_id) = @_;
1720 foreach (command(qw/config -l/)) {
1721 next unless m!^svn-remote\.(.+)\.fetch=
1722 \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x;
1723 my ($repo_id, $path, $ref) = ($1, $2, $3);
1724 if ($ref eq $ref_id) {
1725 $path = '' if ($path =~ m#^\./?#);
1726 return ($repo_id, $path);
1727 }
1728 }
1729 (undef, undef, undef);
1730}
1731
Eric Wong9b981fc2007-01-11 12:14:21 -08001732sub new {
Eric Wong706587f2007-01-18 17:50:01 -08001733 my ($class, $ref_id, $repo_id, $path) = @_;
1734 if (defined $ref_id && !defined $repo_id && !defined $path) {
1735 ($repo_id, $path) = find_ref($ref_id);
1736 if (!defined $repo_id) {
1737 die "Could not find a \"svn-remote.*.fetch\" key ",
1738 "in the repository configuration matching: ",
1739 "refs/remotes/$ref_id\n";
1740 }
1741 }
1742 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong8b8fc062007-01-22 11:44:57 -08001743 if (!defined $self->{path} || !length $self->{path}) {
1744 my $fetch = command_oneline('config', '--get',
1745 "svn-remote.$repo_id.fetch",
1746 ":refs/remotes/$ref_id\$") or
1747 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1748 "\":refs/remotes/$ref_id\$\" in config\n";
1749 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
1750 }
Eric Wong706587f2007-01-18 17:50:01 -08001751 $self->{url} = command_oneline('config', '--get',
1752 "svn-remote.$repo_id.url") or
1753 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
Eric Wongd6d33462007-02-16 04:05:33 -08001754 $self->rebuild;
Eric Wong9b981fc2007-01-11 12:14:21 -08001755 $self;
1756}
1757
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001758sub refname {
1759 my ($refname) = "refs/remotes/$_[0]->{ref_id}" ;
1760
1761 # It cannot end with a slash /, we'll throw up on this because
1762 # SVN can't have directories with a slash in their name, either:
1763 if ($refname =~ m{/$}) {
1764 die "ref: '$refname' ends with a trailing slash, this is ",
1765 "not permitted by git nor Subversion\n";
1766 }
1767
1768 # It cannot have ASCII control character space, tilde ~, caret ^,
1769 # colon :, question-mark ?, asterisk *, space, or open bracket [
1770 # anywhere.
1771 #
1772 # Additionally, % must be escaped because it is used for escaping
1773 # and we want our escaped refname to be reversible
1774 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
1775
1776 # no slash-separated component can begin with a dot .
1777 # /.* becomes /%2E*
1778 $refname =~ s{/\.}{/%2E}g;
1779
1780 # It cannot have two consecutive dots .. anywhere
1781 # .. becomes %2E%2E
1782 $refname =~ s{\.\.}{%2E%2E}g;
1783
1784 return $refname;
1785}
1786
1787sub desanitize_refname {
1788 my ($refname) = @_;
1789 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
1790 return $refname;
1791}
Eric Wong9b981fc2007-01-11 12:14:21 -08001792
Eric Wong26a62d52007-02-12 13:25:25 -08001793sub svm_uuid {
1794 my ($self) = @_;
1795 return $self->{svm}->{uuid} if $self->svm;
1796 $self->ra;
1797 unless ($self->{svm}) {
1798 die "SVM UUID not cached, and reading remotely failed\n";
1799 }
1800 $self->{svm}->{uuid};
1801}
Eric Wong8a49ee92007-02-10 20:46:50 -08001802
Eric Wong26a62d52007-02-12 13:25:25 -08001803sub svm {
1804 my ($self) = @_;
1805 return $self->{svm} if $self->{svm};
1806 my $svm;
Eric Wong8a49ee92007-02-10 20:46:50 -08001807 # see if we have it in our config, first:
1808 eval {
Eric Wong26a62d52007-02-12 13:25:25 -08001809 my $section = "svn-remote.$self->{repo_id}";
1810 $svm = {
Eric Wong93f26892007-02-11 01:20:26 -08001811 source => tmp_config('--get', "$section.svm-source"),
1812 uuid => tmp_config('--get', "$section.svm-uuid"),
Eric Wongbefc9ad2007-02-17 02:53:07 -08001813 replace => tmp_config('--get', "$section.svm-replace"),
Eric Wong8a49ee92007-02-10 20:46:50 -08001814 }
1815 };
Eric Wongbefc9ad2007-02-17 02:53:07 -08001816 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
1817 $self->{svm} = $svm;
1818 }
Eric Wong26a62d52007-02-12 13:25:25 -08001819 $self->{svm};
1820}
1821
1822sub _set_svm_vars {
1823 my ($self, $ra) = @_;
Eric Wongdb03cd22007-02-13 00:38:02 -08001824 return $ra if $self->svm;
Eric Wong26a62d52007-02-12 13:25:25 -08001825
Eric Wongdb03cd22007-02-13 00:38:02 -08001826 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08001827 "(svm:source, svm:uuid) ",
Eric Wongdb03cd22007-02-13 00:38:02 -08001828 "from the following URLs:\n" );
1829 sub read_svm_props {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001830 my ($self, $ra, $path, $r) = @_;
1831 my $props = ($ra->get_dir($path, $r))[2];
Eric Wongdb03cd22007-02-13 00:38:02 -08001832 my $src = $props->{'svm:source'};
Eric Wong8a49ee92007-02-10 20:46:50 -08001833 my $uuid = $props->{'svm:uuid'};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001834 return undef if (!$src || !$uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08001835
Eric Wongbefc9ad2007-02-17 02:53:07 -08001836 chomp($src, $uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08001837
Eric Wong8a49ee92007-02-10 20:46:50 -08001838 $uuid =~ m{^[0-9a-f\-]{30,}$}
1839 or die "doesn't look right - svm:uuid is '$uuid'\n";
Eric Wongbefc9ad2007-02-17 02:53:07 -08001840
1841 # the '!' is used to mark the repos_root!/relative/path
1842 $src =~ s{/?!/?}{/};
Eric Wongdb03cd22007-02-13 00:38:02 -08001843 $src =~ s{/+$}{}; # no trailing slashes please
Eric Wongbefc9ad2007-02-17 02:53:07 -08001844 # username is of no interest
Eric Wongdb03cd22007-02-13 00:38:02 -08001845 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
Eric Wong8a49ee92007-02-10 20:46:50 -08001846
Eric Wongbefc9ad2007-02-17 02:53:07 -08001847 my $replace = $ra->{url};
1848 $replace .= "/$path" if length $path;
1849
Eric Wongdb03cd22007-02-13 00:38:02 -08001850 my $section = "svn-remote.$self->{repo_id}";
Eric Wongbefc9ad2007-02-17 02:53:07 -08001851 tmp_config("$section.svm-source", $src);
1852 tmp_config("$section.svm-replace", $replace);
1853 tmp_config("$section.svm-uuid", $uuid);
1854 $self->{svm} = {
1855 source => $src,
1856 uuid => $uuid,
1857 replace => $replace
1858 };
Eric Wong8a49ee92007-02-10 20:46:50 -08001859 }
Eric Wongdb03cd22007-02-13 00:38:02 -08001860
1861 my $r = $ra->get_latest_revnum;
1862 my $path = $self->{path};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001863 my %tried;
Eric Wongdb03cd22007-02-13 00:38:02 -08001864 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001865 unless ($tried{"$self->{url}/$path"}) {
1866 return $ra if $self->read_svm_props($ra, $path, $r);
1867 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001868 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001869 $path =~ s#/?[^/]+$##;
Eric Wong8a49ee92007-02-10 20:46:50 -08001870 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001871 die "Path: '$path' should be ''\n" if $path ne '';
1872 return $ra if $self->read_svm_props($ra, $path, $r);
1873 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001874
1875 if ($ra->{repos_root} eq $self->{url}) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001876 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001877 }
1878
1879 # nope, make sure we're connected to the repository root:
1880 my $ok;
1881 my @tried_b;
1882 $path = $ra->{svn_path};
Eric Wongdb03cd22007-02-13 00:38:02 -08001883 $ra = Git::SVN::Ra->new($ra->{repos_root});
1884 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001885 unless ($tried{"$ra->{url}/$path"}) {
1886 $ok = $self->read_svm_props($ra, $path, $r);
1887 last if $ok;
1888 $tried{"$ra->{url}/$path"} = 1;
1889 }
1890 $path =~ s#/?[^/]+$##;
Eric Wongdb03cd22007-02-13 00:38:02 -08001891 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001892 die "Path: '$path' should be ''\n" if $path ne '';
1893 $ok ||= $self->read_svm_props($ra, $path, $r);
1894 $tried{"$ra->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001895 if (!$ok) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001896 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001897 }
1898 Git::SVN::Ra->new($self->{url});
Eric Wong8a49ee92007-02-10 20:46:50 -08001899}
1900
Eric Wong62e349d2007-02-16 19:57:29 -08001901sub svnsync {
1902 my ($self) = @_;
1903 return $self->{svnsync} if $self->{svnsync};
1904
1905 if ($self->no_metadata) {
1906 die "Can't have both 'noMetadata' and ",
1907 "'useSvnsyncProps' options set!\n";
1908 }
1909 if ($self->rewrite_root) {
1910 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1911 "options set!\n";
1912 }
1913
1914 my $svnsync;
1915 # see if we have it in our config, first:
1916 eval {
1917 my $section = "svn-remote.$self->{repo_id}";
Eric Wong98fa5b62008-01-11 23:13:55 -08001918
1919 my $url = tmp_config('--get', "$section.svnsync-url");
1920 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
1921 die "doesn't look right - svn:sync-from-url is '$url'\n";
1922
1923 my $uuid = tmp_config('--get', "$section.svnsync-uuid");
1924 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
1925 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1926
1927 $svnsync = { url => $url, uuid => $uuid }
Eric Wong62e349d2007-02-16 19:57:29 -08001928 };
1929 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
1930 return $self->{svnsync} = $svnsync;
1931 }
1932
1933 my $err = "useSvnsyncProps set, but failed to read " .
1934 "svnsync property: svn:sync-from-";
1935 my $rp = $self->ra->rev_proplist(0);
1936
1937 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
Eric Wong98fa5b62008-01-11 23:13:55 -08001938 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
Eric Wong62e349d2007-02-16 19:57:29 -08001939 die "doesn't look right - svn:sync-from-url is '$url'\n";
1940
1941 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
Eric Wong98fa5b62008-01-11 23:13:55 -08001942 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
Eric Wong62e349d2007-02-16 19:57:29 -08001943 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1944
1945 my $section = "svn-remote.$self->{repo_id}";
1946 tmp_config('--add', "$section.svnsync-uuid", $uuid);
1947 tmp_config('--add', "$section.svnsync-url", $url);
1948 return $self->{svnsync} = { url => $url, uuid => $uuid };
1949}
1950
Eric Wong26a62d52007-02-12 13:25:25 -08001951# this allows us to memoize our SVN::Ra UUID locally and avoid a
1952# remote lookup (useful for 'git svn log').
1953sub ra_uuid {
1954 my ($self) = @_;
1955 unless ($self->{ra_uuid}) {
1956 my $key = "svn-remote.$self->{repo_id}.uuid";
1957 my $uuid = eval { tmp_config('--get', $key) };
1958 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
1959 $self->{ra_uuid} = $uuid;
1960 } else {
1961 die "ra_uuid called without URL\n" unless $self->{url};
1962 $self->{ra_uuid} = $self->ra->get_uuid;
1963 tmp_config('--add', $key, $self->{ra_uuid});
1964 }
1965 }
1966 $self->{ra_uuid};
1967}
1968
Eric Wonga5460eb2007-11-21 18:20:57 -08001969sub _set_repos_root {
1970 my ($self, $repos_root) = @_;
1971 my $k = "svn-remote.$self->{repo_id}.reposRoot";
1972 $repos_root ||= $self->ra->{repos_root};
1973 tmp_config($k, $repos_root);
1974 $repos_root;
1975}
1976
1977sub repos_root {
1978 my ($self) = @_;
1979 my $k = "svn-remote.$self->{repo_id}.reposRoot";
1980 eval { tmp_config('--get', $k) } || $self->_set_repos_root;
1981}
1982
Eric Wong9b981fc2007-01-11 12:14:21 -08001983sub ra {
1984 my ($self) = shift;
Eric Wong8a49ee92007-02-10 20:46:50 -08001985 my $ra = Git::SVN::Ra->new($self->{url});
Eric Wonga5460eb2007-11-21 18:20:57 -08001986 $self->_set_repos_root($ra->{repos_root});
Eric Wong91b03282007-02-11 00:51:33 -08001987 if ($self->use_svm_props && !$self->{svm}) {
1988 if ($self->no_metadata) {
Eric Wong97ae0912007-02-11 15:21:24 -08001989 die "Can't have both 'noMetadata' and ",
1990 "'useSvmProps' options set!\n";
Eric Wong62e349d2007-02-16 19:57:29 -08001991 } elsif ($self->use_svnsync_props) {
1992 die "Can't have both 'useSvnsyncProps' and ",
1993 "'useSvmProps' options set!\n";
Eric Wong91b03282007-02-11 00:51:33 -08001994 }
Eric Wong26a62d52007-02-12 13:25:25 -08001995 $ra = $self->_set_svm_vars($ra);
Eric Wong8a49ee92007-02-10 20:46:50 -08001996 $self->{-want_revprops} = 1;
1997 }
1998 $ra;
Eric Wong9b981fc2007-01-11 12:14:21 -08001999}
2000
Eric Wong15710b62007-01-22 02:20:33 -08002001sub rel_path {
2002 my ($self) = @_;
2003 my $repos_root = $self->ra->{repos_root};
2004 return $self->{path} if ($self->{url} eq $repos_root);
Eric Wong0b594512007-03-25 16:35:31 -07002005 my $url = $self->{url} .
2006 (length $self->{path} ? "/$self->{path}" : $self->{path});
2007 $url =~ s!^\Q$repos_root\E(?:/+|$)!!g;
2008 $url;
Eric Wong15710b62007-01-22 02:20:33 -08002009}
2010
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002011# prop_walk(PATH, REV, SUB)
2012# -------------------------
2013# Recursively traverse PATH at revision REV and invoke SUB for each
2014# directory that contains a SVN property. SUB will be invoked as
2015# follows: &SUB(gs, path, props); where `gs' is this instance of
2016# Git::SVN, `path' the path to the directory where the properties
2017# `props' were found. The `path' will be relative to point of checkout,
2018# that is, if url://repo/trunk is the current Git branch, and that
2019# directory contains a sub-directory `d', SUB will be invoked with `/d/'
2020# as `path' (note the trailing `/').
2021sub prop_walk {
2022 my ($self, $path, $rev, $sub) = @_;
2023
Kevin Ballard35cda062008-01-09 01:37:20 -05002024 $path =~ s#^/##;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002025 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
2026 $path =~ s#^/*#/#g;
Eric Wong9b981fc2007-01-11 12:14:21 -08002027 my $p = $path;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002028 # Strip the irrelevant part of the path.
2029 $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
2030 # Ensure the path is terminated by a `/'.
2031 $p =~ s#/*$#/#;
2032
2033 # The properties contain all the internal SVN stuff nobody
2034 # (usually) cares about.
2035 my $interesting_props = 0;
2036 foreach (keys %{$props}) {
2037 # If it doesn't start with `svn:', it must be a
2038 # user-defined property.
2039 ++$interesting_props and next if $_ !~ /^svn:/;
2040 # FIXME: Fragile, if SVN adds new public properties,
2041 # this needs to be updated.
2042 ++$interesting_props if /^svn:(?:ignore|keywords|executable
2043 |eol-style|mime-type
2044 |externals|needs-lock)$/x;
Eric Wong9b981fc2007-01-11 12:14:21 -08002045 }
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002046 &$sub($self, $p, $props) if $interesting_props;
2047
Eric Wong9b981fc2007-01-11 12:14:21 -08002048 foreach (sort keys %$dirent) {
Eric Wong0dc03d62007-05-13 01:04:43 -07002049 next if $dirent->{$_}->{kind} != $SVN::Node::dir;
Christian Engwerb7166cc2008-05-27 08:46:55 +00002050 $self->prop_walk($self->{path} . $p . $_, $rev, $sub);
Eric Wong9b981fc2007-01-11 12:14:21 -08002051 }
2052}
2053
Eric Wong3ebe8df2007-01-25 17:35:40 -08002054sub last_rev { ($_[0]->last_rev_commit)[0] }
2055sub last_commit { ($_[0]->last_rev_commit)[1] }
2056
Eric Wong9b981fc2007-01-11 12:14:21 -08002057# returns the newest SVN revision number and newest commit SHA1
2058sub last_rev_commit {
2059 my ($self) = @_;
2060 if (defined $self->{last_rev} && defined $self->{last_commit}) {
2061 return ($self->{last_rev}, $self->{last_commit});
2062 }
Eric Wongd2866f92007-01-11 12:26:16 -08002063 my $c = ::verify_ref($self->refname.'^0');
Eric Wong91b03282007-02-11 00:51:33 -08002064 if ($c && !$self->use_svm_props && !$self->no_metadata) {
Eric Wongd2866f92007-01-11 12:26:16 -08002065 my $rev = (::cmt_metadata($c))[1];
Eric Wong9b981fc2007-01-11 12:14:21 -08002066 if (defined $rev) {
2067 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2068 return ($rev, $c);
2069 }
2070 }
Eric Wong060610c2007-12-08 23:27:41 -08002071 my $map_path = $self->map_path;
2072 unless (-e $map_path) {
Eric Wong26a62d52007-02-12 13:25:25 -08002073 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
2074 return (undef, undef);
2075 }
Eric Wong66ab84b2007-12-08 23:27:42 -08002076 my ($rev, $commit) = $self->rev_map_max(1);
Eric Wong060610c2007-12-08 23:27:41 -08002077 ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
2078 return ($rev, $commit);
Eric Wong9b981fc2007-01-11 12:14:21 -08002079}
2080
Eric Wong3ebe8df2007-01-25 17:35:40 -08002081sub get_fetch_range {
2082 my ($self, $min, $max) = @_;
2083 $max ||= $self->ra->get_latest_revnum;
Eric Wong060610c2007-12-08 23:27:41 -08002084 $min ||= $self->rev_map_max;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002085 (++$min, $max);
Eric Wong9b981fc2007-01-11 12:14:21 -08002086}
2087
Eric Wong8a49ee92007-02-10 20:46:50 -08002088sub tmp_config {
Eric Wong93f26892007-02-11 01:20:26 -08002089 my (@args) = @_;
Eric Wongb7e53482007-02-16 04:09:28 -08002090 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
2091 my $config = "$ENV{GIT_DIR}/svn/.metadata";
Eric Wong38570a42007-06-13 02:37:05 -07002092 if (! -f $config && -f $old_def_config) {
Eric Wongb7e53482007-02-16 04:09:28 -08002093 rename $old_def_config, $config or
2094 die "Failed rename $old_def_config => $config: $!\n";
2095 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002096 my $old_config = $ENV{GIT_CONFIG};
Eric Wong93f26892007-02-11 01:20:26 -08002097 $ENV{GIT_CONFIG} = $config;
Eric Wong8a49ee92007-02-10 20:46:50 -08002098 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002099 my @ret = eval {
2100 unless (-f $config) {
2101 mkfile($config);
2102 open my $fh, '>', $config or
2103 die "Can't open $config: $!\n";
2104 print $fh "; This file is used internally by ",
2105 "git-svn\n" or die
2106 "Couldn't write to $config: $!\n";
2107 print $fh "; You should not have to edit it\n" or
2108 die "Couldn't write to $config: $!\n";
2109 close $fh or die "Couldn't close $config: $!\n";
2110 }
2111 command('config', @args);
2112 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002113 my $err = $@;
2114 if (defined $old_config) {
2115 $ENV{GIT_CONFIG} = $old_config;
2116 } else {
2117 delete $ENV{GIT_CONFIG};
2118 }
2119 die $err if $err;
2120 wantarray ? @ret : $ret[0];
2121}
2122
Eric Wong9b981fc2007-01-11 12:14:21 -08002123sub tmp_index_do {
2124 my ($self, $sub) = @_;
2125 my $old_index = $ENV{GIT_INDEX_FILE};
2126 $ENV{GIT_INDEX_FILE} = $self->{index};
Eric Wong8a49ee92007-02-10 20:46:50 -08002127 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002128 my @ret = eval {
2129 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
2130 mkpath([$dir]) unless -d $dir;
2131 &$sub;
2132 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002133 my $err = $@;
2134 if (defined $old_index) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002135 $ENV{GIT_INDEX_FILE} = $old_index;
2136 } else {
2137 delete $ENV{GIT_INDEX_FILE};
2138 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002139 die $err if $err;
Eric Wong9b981fc2007-01-11 12:14:21 -08002140 wantarray ? @ret : $ret[0];
2141}
2142
2143sub assert_index_clean {
2144 my ($self, $treeish) = @_;
2145
2146 $self->tmp_index_do(sub {
2147 command_noisy('read-tree', $treeish) unless -e $self->{index};
2148 my $x = command_oneline('write-tree');
2149 my ($y) = (command(qw/cat-file commit/, $treeish) =~
2150 /^tree ($::sha1)/mo);
Eric Wonge8d120b2007-02-14 16:29:52 -08002151 return if $y eq $x;
2152
2153 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2154 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2155 command_noisy('read-tree', $treeish);
Eric Wong9b981fc2007-01-11 12:14:21 -08002156 $x = command_oneline('write-tree');
2157 if ($y ne $x) {
2158 ::fatal "trees ($treeish) $y != $x\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02002159 "Something is seriously wrong...";
Eric Wong9b981fc2007-01-11 12:14:21 -08002160 }
2161 });
2162}
2163
2164sub get_commit_parents {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002165 my ($self, $log_entry) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002166 my (%seen, @ret, @tmp);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002167 # legacy support for 'set-tree'; this is only used by set_tree_cb:
2168 if (my $ip = $self->{inject_parents}) {
2169 if (my $commit = delete $ip->{$log_entry->{revision}}) {
2170 push @tmp, $commit;
Eric Wong9b981fc2007-01-11 12:14:21 -08002171 }
2172 }
Eric Wongd2866f92007-01-11 12:26:16 -08002173 if (my $cur = ::verify_ref($self->refname.'^0')) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002174 push @tmp, $cur;
2175 }
Eric Wong733a65a2007-06-13 02:23:28 -07002176 if (my $ipd = $self->{inject_parents_dcommit}) {
2177 if (my $commit = delete $ipd->{$log_entry->{revision}}) {
2178 push @tmp, @$commit;
2179 }
2180 }
Eric Wong44320b92007-01-13 22:35:53 -08002181 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
Eric Wong9b981fc2007-01-11 12:14:21 -08002182 while (my $p = shift @tmp) {
2183 next if $seen{$p};
2184 $seen{$p} = 1;
2185 push @ret, $p;
2186 # MAXPARENT is defined to 16 in commit-tree.c:
2187 last if @ret >= 16;
2188 }
2189 if (@tmp) {
Eric Wong44320b92007-01-13 22:35:53 -08002190 die "r$log_entry->{revision}: No room for parents:\n\t",
Eric Wong9b981fc2007-01-11 12:14:21 -08002191 join("\n\t", @tmp), "\n";
2192 }
2193 @ret;
2194}
2195
Eric Wongaea736c2007-02-16 19:15:21 -08002196sub rewrite_root {
2197 my ($self) = @_;
2198 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
2199 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2200 my $rwr = eval { command_oneline(qw/config --get/, $k) };
2201 if ($rwr) {
2202 $rwr =~ s#/+$##;
2203 if ($rwr !~ m#^[a-z\+]+://#) {
2204 die "$rwr is not a valid URL (key: $k)\n";
2205 }
2206 }
2207 $self->{-rewrite_root} = $rwr;
2208}
2209
2210sub metadata_url {
2211 my ($self) = @_;
2212 ($self->rewrite_root || $self->{url}) .
2213 (length $self->{path} ? '/' . $self->{path} : '');
2214}
2215
Eric Wong706587f2007-01-18 17:50:01 -08002216sub full_url {
Eric Wong9b981fc2007-01-11 12:14:21 -08002217 my ($self) = @_;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002218 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
Eric Wong9b981fc2007-01-11 12:14:21 -08002219}
2220
Eric Wongad948022007-12-15 19:08:22 -08002221
2222sub set_commit_header_env {
2223 my ($log_entry) = @_;
2224 my %env;
2225 foreach my $ned (qw/NAME EMAIL DATE/) {
2226 foreach my $ac (qw/AUTHOR COMMITTER/) {
2227 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2228 }
2229 }
2230
2231 $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
2232 $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
2233 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
2234
2235 $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
2236 ? $log_entry->{commit_name}
2237 : $log_entry->{name};
2238 $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
2239 ? $log_entry->{commit_email}
2240 : $log_entry->{email};
2241 \%env;
2242}
2243
2244sub restore_commit_header_env {
2245 my ($env) = @_;
2246 foreach my $ned (qw/NAME EMAIL DATE/) {
2247 foreach my $ac (qw/AUTHOR COMMITTER/) {
2248 my $k = "GIT_${ac}_${ned}";
2249 if (defined $env->{$k}) {
2250 $ENV{$k} = $env->{$k};
2251 } else {
2252 delete $ENV{$k};
2253 }
2254 }
2255 }
2256}
2257
Karl Hasselström94bc9142008-02-03 17:56:18 +01002258sub gc {
2259 command_noisy('gc', '--auto');
2260};
2261
Eric Wong9b981fc2007-01-11 12:14:21 -08002262sub do_git_commit {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002263 my ($self, $log_entry) = @_;
Eric Wong8a603772007-01-31 02:45:50 -08002264 my $lr = $self->last_rev;
2265 if (defined $lr && $lr >= $log_entry->{revision}) {
2266 die "Last fetched revision of ", $self->refname,
2267 " was r$lr, but we are about to fetch: ",
2268 "r$log_entry->{revision}!\n";
2269 }
Eric Wong060610c2007-12-08 23:27:41 -08002270 if (my $c = $self->rev_map_get($log_entry->{revision})) {
Eric Wong44320b92007-01-13 22:35:53 -08002271 croak "$log_entry->{revision} = $c already exists! ",
Eric Wong9b981fc2007-01-11 12:14:21 -08002272 "Why are we refetching it?\n";
2273 }
Eric Wongad948022007-12-15 19:08:22 -08002274 my $old_env = set_commit_header_env($log_entry);
Eric Wong44320b92007-01-13 22:35:53 -08002275 my $tree = $log_entry->{tree};
Eric Wong9b981fc2007-01-11 12:14:21 -08002276 if (!defined $tree) {
2277 $tree = $self->tmp_index_do(sub {
2278 command_oneline('write-tree') });
2279 }
2280 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2281
Deskin Millere855bfc2008-10-31 00:10:25 -04002282 my @exec = ('git', 'commit-tree', $tree);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002283 foreach ($self->get_commit_parents($log_entry)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002284 push @exec, '-p', $_;
2285 }
2286 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2287 or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07002288 binmode $msg_fh;
2289
2290 # we always get UTF-8 from SVN, but we may want our commits in
2291 # a different encoding.
2292 if (my $enc = Git::config('i18n.commitencoding')) {
2293 require Encode;
2294 Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
2295 }
Eric Wong44320b92007-01-13 22:35:53 -08002296 print $msg_fh $log_entry->{log} or croak $!;
Eric Wongad948022007-12-15 19:08:22 -08002297 restore_commit_header_env($old_env);
Eric Wong91b03282007-02-11 00:51:33 -08002298 unless ($self->no_metadata) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002299 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2300 or croak $!;
Eric Wong9760adc2007-01-31 03:06:56 -08002301 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002302 $msg_fh->flush == 0 or croak $!;
2303 close $msg_fh or croak $!;
2304 chomp(my $commit = do { local $/; <$out_fh> });
2305 close $out_fh or croak $!;
2306 waitpid $pid, 0;
2307 croak $? if $?;
2308 if ($commit !~ /^$::sha1$/o) {
2309 die "Failed to commit, invalid sha1: $commit\n";
2310 }
2311
Eric Wong060610c2007-12-08 23:27:41 -08002312 $self->rev_map_set($log_entry->{revision}, $commit, 1);
Eric Wong9b981fc2007-01-11 12:14:21 -08002313
Eric Wong44320b92007-01-13 22:35:53 -08002314 $self->{last_rev} = $log_entry->{revision};
Eric Wong9b981fc2007-01-11 12:14:21 -08002315 $self->{last_commit} = $commit;
Eric Wong8a49ee92007-02-10 20:46:50 -08002316 print "r$log_entry->{revision}";
2317 if (defined $log_entry->{svm_revision}) {
2318 print " (\@$log_entry->{svm_revision})";
Eric Wong060610c2007-12-08 23:27:41 -08002319 $self->rev_map_set($log_entry->{svm_revision}, $commit,
Eric Wong26a62d52007-02-12 13:25:25 -08002320 0, $self->svm_uuid);
Eric Wong8a49ee92007-02-10 20:46:50 -08002321 }
2322 print " = $commit ($self->{ref_id})\n";
Karl Hasselström94bc9142008-02-03 17:56:18 +01002323 if (--$_gc_nr == 0) {
2324 $_gc_nr = $_gc_period;
2325 gc();
2326 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002327 return $commit;
2328}
2329
Eric Wongfbcc1732007-02-06 18:35:30 -08002330sub match_paths {
2331 my ($self, $paths, $r) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002332 return 1 if $self->{path} eq '';
Eric Wongd542aed2007-02-09 02:19:41 -08002333 if (my $path = $paths->{"/$self->{path}"}) {
2334 return ($path->{action} eq 'D') ? 0 : 1;
2335 }
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002336 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
Eric Wongfbcc1732007-02-06 18:35:30 -08002337 if (grep /$self->{path_regex}/, keys %$paths) {
2338 return 1;
2339 }
2340 my $c = '';
2341 foreach (split m#/#, $self->{path}) {
2342 $c .= "/$_";
Eric Wong74a81222007-02-10 13:28:50 -08002343 next unless ($paths->{$c} &&
2344 ($paths->{$c}->{action} =~ /^[AR]$/));
Eric Wonge5181922007-02-08 12:53:57 -08002345 if ($self->ra->check_path($self->{path}, $r) ==
2346 $SVN::Node::dir) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002347 return 1;
2348 }
2349 }
2350 return 0;
2351}
2352
Eric Wong15710b62007-01-22 02:20:33 -08002353sub find_parent_branch {
2354 my ($self, $paths, $rev) = @_;
Eric Wong91b03282007-02-11 00:51:33 -08002355 return undef unless $self->follow_parent;
Eric Wonge5a0b242007-01-25 15:44:54 -08002356 unless (defined $paths) {
Eric Wongc7eba712007-01-31 03:45:28 -08002357 my $err_handler = $SVN::Error::handler;
2358 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
Eric Wongd4eff2b2007-01-30 14:04:22 -08002359 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, sub {
2360 $paths =
2361 Git::SVN::Ra::dup_changed_paths($_[0]) });
Eric Wongc7eba712007-01-31 03:45:28 -08002362 $SVN::Error::handler = $err_handler;
Eric Wonge5a0b242007-01-25 15:44:54 -08002363 }
2364 return undef unless defined $paths;
Eric Wong15710b62007-01-22 02:20:33 -08002365
2366 # look for a parent from another branch:
Eric Wong7f578c52007-01-24 02:16:25 -08002367 my @b_path_components = split m#/#, $self->rel_path;
2368 my @a_path_components;
2369 my $i;
2370 while (@b_path_components) {
2371 $i = $paths->{'/'.join('/', @b_path_components)};
Eric Wong74a81222007-02-10 13:28:50 -08002372 last if $i && defined $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002373 unshift(@a_path_components, pop(@b_path_components));
2374 }
Eric Wong74a81222007-02-10 13:28:50 -08002375 return undef unless defined $i && defined $i->{copyfrom_path};
2376 my $branch_from = $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002377 if (@a_path_components) {
2378 print STDERR "branch_from: $branch_from => ";
2379 $branch_from .= '/'.join('/', @a_path_components);
2380 print STDERR $branch_from, "\n";
2381 }
Eric Wong3ebe8df2007-01-25 17:35:40 -08002382 my $r = $i->{copyfrom_rev};
Eric Wong15710b62007-01-22 02:20:33 -08002383 my $repos_root = $self->ra->{repos_root};
2384 my $url = $self->ra->{url};
2385 my $new_url = $repos_root . $branch_from;
2386 print STDERR "Found possible branch point: ",
2387 "$new_url => ", $self->full_url, ", $r\n";
2388 $branch_from =~ s#^/##;
Eric Wonga8ae2622007-02-13 14:22:11 -08002389 my $gs = Git::SVN->find_by_url($new_url, $repos_root, $branch_from);
Eric Wong15710b62007-01-22 02:20:33 -08002390 unless ($gs) {
Eric Wongce2a0f22007-01-28 21:34:23 -08002391 my $ref_id = $self->{ref_id};
2392 $ref_id =~ s/\@\d+$//;
2393 $ref_id .= "\@$r";
Eric Wong15710b62007-01-22 02:20:33 -08002394 # just grow a tail if we're not unique enough :x
2395 $ref_id .= '-' while find_ref($ref_id);
Eric Wongce2a0f22007-01-28 21:34:23 -08002396 print STDERR "Initializing parent: $ref_id\n";
Eric Wongf61cc482008-04-07 00:08:03 -07002397 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
Eric Wongc5868792008-01-29 01:18:58 -08002398 if ($u =~ s#^\Q$url\E(/|$)##) {
2399 $p = $u;
2400 $u = $url;
Eric Wongf61cc482008-04-07 00:08:03 -07002401 $repo_id = $self->{repo_id};
Eric Wongc5868792008-01-29 01:18:58 -08002402 }
Eric Wongf61cc482008-04-07 00:08:03 -07002403 $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1);
Eric Wong15710b62007-01-22 02:20:33 -08002404 }
2405 my ($r0, $parent) = $gs->find_rev_before($r, 1);
Eric Wong91b03282007-02-11 00:51:33 -08002406 if (!defined $r0 || !defined $parent) {
Eric Wongd627de62007-04-15 03:01:29 -07002407 my ($base, $head) = parse_revision_argument(0, $r);
2408 if ($base <= $r) {
2409 $gs->fetch($base, $r);
2410 }
Eric Wong15710b62007-01-22 02:20:33 -08002411 ($r0, $parent) = $gs->last_rev_commit;
2412 }
Eric Wongef70de92007-02-01 04:12:41 -08002413 if (defined $r0 && defined $parent) {
Eric Wong15710b62007-01-22 02:20:33 -08002414 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n";
Eric Wong15710b62007-01-22 02:20:33 -08002415 my $ed;
2416 if ($self->ra->can_do_switch) {
Eric Wong2e5e2482007-02-23 02:21:59 -08002417 $self->assert_index_clean($parent);
Eric Wong8b8fc062007-01-22 11:44:57 -08002418 print STDERR "Following parent with do_switch\n";
Eric Wong15710b62007-01-22 02:20:33 -08002419 # do_switch works with svn/trunk >= r22312, but that
Eric Wong2b27f6c2007-01-28 04:59:05 -08002420 # is not included with SVN 1.4.3 (the latest version
Eric Wong15710b62007-01-22 02:20:33 -08002421 # at the moment), so we can't rely on it
2422 $self->{last_commit} = $parent;
2423 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08002424 $gs->ra->gs_do_switch($r0, $rev, $gs,
Eric Wong15710b62007-01-22 02:20:33 -08002425 $self->full_url, $ed)
2426 or die "SVN connection failed somewhere...\n";
Steven Walter9ff74e92007-09-28 13:24:19 -04002427 } elsif ($self->ra->trees_match($new_url, $r0,
2428 $self->full_url, $rev)) {
2429 print STDERR "Trees match:\n",
2430 " $new_url\@$r0\n",
2431 " ${\$self->full_url}\@$rev\n",
2432 "Following parent with no changes\n";
2433 $self->tmp_index_do(sub {
2434 command_noisy('read-tree', $parent);
2435 });
2436 $self->{last_commit} = $parent;
Eric Wong15710b62007-01-22 02:20:33 -08002437 } else {
Eric Wong8b8fc062007-01-22 11:44:57 -08002438 print STDERR "Following parent with do_update\n";
Eric Wong15710b62007-01-22 02:20:33 -08002439 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08002440 $self->ra->gs_do_update($rev, $rev, $self, $ed)
Eric Wong15710b62007-01-22 02:20:33 -08002441 or die "SVN connection failed somewhere...\n";
2442 }
Eric Wongf7c3fc42007-01-29 18:34:55 -08002443 print STDERR "Successfully followed parent\n";
Eric Wong15710b62007-01-22 02:20:33 -08002444 return $self->make_log_entry($rev, [$parent], $ed);
2445 }
Eric Wong15710b62007-01-22 02:20:33 -08002446 return undef;
2447}
2448
Eric Wong9b981fc2007-01-11 12:14:21 -08002449sub do_fetch {
Eric Wong706587f2007-01-18 17:50:01 -08002450 my ($self, $paths, $rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08002451 my $ed;
Eric Wong9b981fc2007-01-11 12:14:21 -08002452 my ($last_rev, @parents);
Eric Wongb9dffd82007-02-09 01:28:30 -08002453 if (my $lc = $self->last_commit) {
2454 # we can have a branch that was deleted, then re-added
2455 # under the same name but copied from another path, in
2456 # which case we'll have multiple parents (we don't
2457 # want to break the original ref, nor lose copypath info):
2458 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2459 push @{$log_entry->{parents}}, $lc;
2460 return $log_entry;
2461 }
Eric Wong15710b62007-01-22 02:20:33 -08002462 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002463 $last_rev = $self->{last_rev};
Eric Wongb9dffd82007-02-09 01:28:30 -08002464 $ed->{c} = $lc;
2465 @parents = ($lc);
Eric Wong9b981fc2007-01-11 12:14:21 -08002466 } else {
2467 $last_rev = $rev;
Eric Wong15710b62007-01-22 02:20:33 -08002468 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2469 return $log_entry;
2470 }
2471 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002472 }
Eric Wong8a603772007-01-31 02:45:50 -08002473 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002474 die "SVN connection failed somewhere...\n";
2475 }
2476 $self->make_log_entry($rev, \@parents, $ed);
2477}
2478
Eric Wong97f69872007-01-25 11:53:13 -08002479sub get_untracked {
2480 my ($self, $ed) = @_;
2481 my @out;
2482 my $h = $ed->{empty};
Eric Wong9b981fc2007-01-11 12:14:21 -08002483 foreach (sort keys %$h) {
2484 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
Eric Wong97f69872007-01-25 11:53:13 -08002485 push @out, " $act: " . uri_encode($_);
Eric Wong9b981fc2007-01-11 12:14:21 -08002486 warn "W: $act: $_\n";
2487 }
2488 foreach my $t (qw/dir_prop file_prop/) {
Eric Wong97f69872007-01-25 11:53:13 -08002489 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002490 foreach my $path (sort keys %$h) {
2491 my $ppath = $path eq '' ? '.' : $path;
2492 foreach my $prop (sort keys %{$h->{$path}}) {
Eric Wong1ce255d2007-01-14 23:21:16 -08002493 next if $SKIP_PROP{$prop};
Eric Wong9b981fc2007-01-11 12:14:21 -08002494 my $v = $h->{$path}->{$prop};
Eric Wong97f69872007-01-25 11:53:13 -08002495 my $t_ppath_prop = "$t: " .
2496 uri_encode($ppath) . ' ' .
2497 uri_encode($prop);
Eric Wong9b981fc2007-01-11 12:14:21 -08002498 if (defined $v) {
Eric Wong97f69872007-01-25 11:53:13 -08002499 push @out, " +$t_ppath_prop " .
2500 uri_encode($v);
Eric Wong9b981fc2007-01-11 12:14:21 -08002501 } else {
Eric Wong97f69872007-01-25 11:53:13 -08002502 push @out, " -$t_ppath_prop";
Eric Wong9b981fc2007-01-11 12:14:21 -08002503 }
2504 }
2505 }
2506 }
2507 foreach my $t (qw/absent_file absent_directory/) {
Eric Wong97f69872007-01-25 11:53:13 -08002508 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002509 foreach my $parent (sort keys %$h) {
2510 foreach my $path (sort @{$h->{$parent}}) {
Eric Wong97f69872007-01-25 11:53:13 -08002511 push @out, " $t: " .
2512 uri_encode("$parent/$path");
Eric Wong9b981fc2007-01-11 12:14:21 -08002513 warn "W: $t: $parent/$path ",
2514 "Insufficient permissions?\n";
2515 }
2516 }
2517 }
Eric Wong97f69872007-01-25 11:53:13 -08002518 \@out;
Eric Wong9b981fc2007-01-11 12:14:21 -08002519}
2520
Eric Wong1c8443b2007-01-14 02:17:00 -08002521sub parse_svn_date {
2522 my $date = shift || return '+0000 1970-01-01 00:00:00';
2523 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
2524 (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x) or
2525 croak "Unable to parse date: $date\n";
2526 "+0000 $Y-$m-$d $H:$M:$S";
2527}
2528
2529sub check_author {
2530 my ($author) = @_;
2531 if (!defined $author || length $author == 0) {
2532 $author = '(no author)';
Thomas Guyot-Sionnest9231f502008-04-22 06:07:47 -04002533 } elsif (defined $::_authors && ! defined $::users{$author}) {
Eric Wong1c8443b2007-01-14 02:17:00 -08002534 die "Author: $author not defined in $::_authors file\n";
2535 }
2536 $author;
2537}
2538
Eric Wong9b981fc2007-01-11 12:14:21 -08002539sub make_log_entry {
Eric Wong97f69872007-01-25 11:53:13 -08002540 my ($self, $rev, $parents, $ed) = @_;
2541 my $untracked = $self->get_untracked($ed);
2542
Eric Wong9b981fc2007-01-11 12:14:21 -08002543 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08002544 print $un "r$rev\n" or croak $!;
2545 print $un $_, "\n" foreach @$untracked;
2546 my %log_entry = ( parents => $parents || [], revision => $rev,
2547 log => '');
Eric Wongfbcc1732007-02-06 18:35:30 -08002548
Eric Wong8a49ee92007-02-10 20:46:50 -08002549 my $headrev;
Eric Wongfbcc1732007-02-06 18:35:30 -08002550 my $logged = delete $self->{logged_rev_props};
Eric Wong8a49ee92007-02-10 20:46:50 -08002551 if (!$logged || $self->{-want_revprops}) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002552 my $rp = $self->ra->rev_proplist($rev);
2553 foreach (sort keys %$rp) {
2554 my $v = $rp->{$_};
2555 if (/^svn:(author|date|log)$/) {
2556 $log_entry{$1} = $v;
Eric Wong8a49ee92007-02-10 20:46:50 -08002557 } elsif ($_ eq 'svm:headrev') {
2558 $headrev = $v;
Eric Wongfbcc1732007-02-06 18:35:30 -08002559 } else {
2560 print $un " rev_prop: ", uri_encode($_), ' ',
2561 uri_encode($v), "\n";
2562 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002563 }
Eric Wongfbcc1732007-02-06 18:35:30 -08002564 } else {
2565 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
Eric Wong9b981fc2007-01-11 12:14:21 -08002566 }
2567 close $un or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08002568
Eric Wong9b981fc2007-01-11 12:14:21 -08002569 $log_entry{date} = parse_svn_date($log_entry{date});
Eric Wong9b981fc2007-01-11 12:14:21 -08002570 $log_entry{log} .= "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002571 my $author = $log_entry{author} = check_author($log_entry{author});
2572 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002573 : ($author, undef);
2574
2575 my ($commit_name, $commit_email) = ($name, $email);
2576 if ($_use_log_author) {
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002577 my $name_field;
2578 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
2579 $name_field = $1;
2580 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
2581 $name_field = $1;
2582 }
2583 if (!defined $name_field) {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02002584 if (!defined $email) {
2585 $email = $name;
2586 }
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002587 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002588 ($name, $email) = ($1, $2);
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002589 } elsif ($name_field =~ /(.*)@/) {
2590 ($name, $email) = ($1, $name_field);
2591 } else {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02002592 ($name, $email) = ($name_field, $name_field);
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002593 }
2594 }
Eric Wong91b03282007-02-11 00:51:33 -08002595 if (defined $headrev && $self->use_svm_props) {
Eric Wongaea736c2007-02-16 19:15:21 -08002596 if ($self->rewrite_root) {
2597 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
2598 "options set!\n";
2599 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002600 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002601 # we don't want "SVM: initializing mirror for junk" ...
2602 return undef if $r == 0;
2603 my $svm = $self->svm;
2604 if ($uuid ne $svm->{uuid}) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002605 die "UUID mismatch on SVM path:\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08002606 "expected: $svm->{uuid}\n",
Eric Wong8a49ee92007-02-10 20:46:50 -08002607 " got: $uuid\n";
2608 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002609 my $full_url = $self->full_url;
2610 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
2611 die "Failed to replace '$svm->{replace}' with ",
2612 "'$svm->{source}' in $full_url\n";
Sam Vilain18ea92b2007-02-23 12:32:29 +13002613 # throw away username for storing in records
2614 remove_username($full_url);
Eric Wong8a49ee92007-02-10 20:46:50 -08002615 $log_entry{metadata} = "$full_url\@$r $uuid";
2616 $log_entry{svm_revision} = $r;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002617 $email ||= "$author\@$uuid";
2618 $commit_email ||= "$author\@$uuid";
Eric Wong62e349d2007-02-16 19:57:29 -08002619 } elsif ($self->use_svnsync_props) {
2620 my $full_url = $self->svnsync->{url};
2621 $full_url .= "/$self->{path}" if length $self->{path};
Adam Robence118732007-04-24 18:02:07 -07002622 remove_username($full_url);
Eric Wong62e349d2007-02-16 19:57:29 -08002623 my $uuid = $self->svnsync->{uuid};
2624 $log_entry{metadata} = "$full_url\@$rev $uuid";
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002625 $email ||= "$author\@$uuid";
2626 $commit_email ||= "$author\@$uuid";
Eric Wong8a49ee92007-02-10 20:46:50 -08002627 } else {
Adam Robence118732007-04-24 18:02:07 -07002628 my $url = $self->metadata_url;
2629 remove_username($url);
2630 $log_entry{metadata} = "$url\@$rev " .
Eric Wong26a62d52007-02-12 13:25:25 -08002631 $self->ra->get_uuid;
Eric Wongdb03cd22007-02-13 00:38:02 -08002632 $email ||= "$author\@" . $self->ra->get_uuid;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002633 $commit_email ||= "$author\@" . $self->ra->get_uuid;
Eric Wong8a49ee92007-02-10 20:46:50 -08002634 }
Eric Wongdb03cd22007-02-13 00:38:02 -08002635 $log_entry{name} = $name;
2636 $log_entry{email} = $email;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002637 $log_entry{commit_name} = $commit_name;
2638 $log_entry{commit_email} = $commit_email;
Eric Wong9b981fc2007-01-11 12:14:21 -08002639 \%log_entry;
2640}
2641
2642sub fetch {
Eric Wong3ebe8df2007-01-25 17:35:40 -08002643 my ($self, $min_rev, $max_rev, @parents) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002644 my ($last_rev, $last_commit) = $self->last_rev_commit;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002645 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
Eric Wonge5181922007-02-08 12:53:57 -08002646 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
Eric Wong9b981fc2007-01-11 12:14:21 -08002647}
2648
2649sub set_tree_cb {
2650 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
Eric Wong490f49e2007-02-10 13:58:33 -08002651 $self->{inject_parents} = { $rev => $tree };
2652 $self->fetch(undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08002653}
2654
2655sub set_tree {
2656 my ($self, $tree) = (shift, shift);
Eric Wong1ce255d2007-01-14 23:21:16 -08002657 my $log_entry = ::get_commit_entry($tree);
Eric Wong9b981fc2007-01-11 12:14:21 -08002658 unless ($self->{last_rev}) {
Luc Heinrich0a1a1c82008-09-29 15:58:18 +02002659 ::fatal("Must have an existing revision to commit");
Eric Wong9b981fc2007-01-11 12:14:21 -08002660 }
Eric Wong61395352007-01-27 14:33:08 -08002661 my %ed_opts = ( r => $self->{last_rev},
2662 log => $log_entry->{log},
2663 ra => $self->ra,
2664 tree_a => $self->{last_commit},
2665 tree_b => $tree,
2666 editor_cb => sub {
2667 $self->set_tree_cb($log_entry, $tree, @_) },
2668 svn_path => $self->{path} );
2669 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002670 print "No changes\nr$self->{last_rev} = $tree\n";
2671 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002672}
2673
Eric Wong060610c2007-12-08 23:27:41 -08002674sub rebuild_from_rev_db {
2675 my ($self, $path) = @_;
2676 my $r = -1;
2677 open my $fh, '<', $path or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02002678 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002679 while (<$fh>) {
2680 length($_) == 41 or croak "inconsistent size in ($_) != 41";
2681 chomp($_);
2682 ++$r;
2683 next if $_ eq ('0' x 40);
2684 $self->rev_map_set($r, $_);
2685 print "r$r = $_\n";
2686 }
2687 close $fh or croak "close: $!";
2688 unlink $path or croak "unlink: $!";
2689}
2690
Eric Wongf0ecca12007-01-30 13:11:14 -08002691sub rebuild {
2692 my ($self) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08002693 my $map_path = $self->map_path;
Deskin Miller2beec892008-09-15 21:12:58 -04002694 my $partial = (-e $map_path && ! -z $map_path);
Eric Wongd6d33462007-02-16 04:05:33 -08002695 return unless ::verify_ref($self->refname.'^0');
Deskin Miller2beec892008-09-15 21:12:58 -04002696 if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
Eric Wong060610c2007-12-08 23:27:41 -08002697 my $rev_db = $self->rev_db_path;
2698 $self->rebuild_from_rev_db($rev_db);
2699 if ($self->use_svm_props) {
2700 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
2701 $self->rebuild_from_rev_db($svm_rev_db);
2702 }
2703 $self->unlink_rev_db_symlink;
Eric Wong26a62d52007-02-12 13:25:25 -08002704 return;
2705 }
Deskin Miller2beec892008-09-15 21:12:58 -04002706 print "Rebuilding $map_path ...\n" if (!$partial);
2707 my ($base_rev, $head) = ($partial ? $self->rev_map_max_norebuild(1) :
2708 (undef, undef));
Eric Wong060610c2007-12-08 23:27:41 -08002709 my ($log, $ctx) =
2710 command_output_pipe(qw/rev-list --pretty=raw --no-color --reverse/,
Deskin Miller2beec892008-09-15 21:12:58 -04002711 ($head ? "$head.." : "") . $self->refname,
2712 '--');
Jan Krüger74b1e122008-06-24 02:17:36 +02002713 my $metadata_url = $self->metadata_url;
2714 remove_username($metadata_url);
Eric Wong060610c2007-12-08 23:27:41 -08002715 my $svn_uuid = $self->ra_uuid;
Sam Vilain3dfab992007-06-30 20:56:13 +12002716 my $c;
2717 while (<$log>) {
2718 if ( m{^commit ($::sha1)$} ) {
2719 $c = $1;
2720 next;
2721 }
2722 next unless s{^\s*(git-svn-id:)}{$1};
2723 my ($url, $rev, $uuid) = ::extract_metadata($_);
Sam Vilain18ea92b2007-02-23 12:32:29 +13002724 remove_username($url);
Eric Wongf0ecca12007-01-30 13:11:14 -08002725
2726 # ignore merges (from set-tree)
2727 next if (!defined $rev || !$uuid);
2728
2729 # if we merged or otherwise started elsewhere, this is
2730 # how we break out of it
Eric Wong060610c2007-12-08 23:27:41 -08002731 if (($uuid ne $svn_uuid) ||
Jan Krüger74b1e122008-06-24 02:17:36 +02002732 ($metadata_url && $url && ($url ne $metadata_url))) {
Eric Wongf0ecca12007-01-30 13:11:14 -08002733 next;
2734 }
Deskin Miller2beec892008-09-15 21:12:58 -04002735 if ($partial && $head) {
2736 print "Partial-rebuilding $map_path ...\n";
2737 print "Currently at $base_rev = $head\n";
2738 $head = undef;
2739 }
Eric Wongf0ecca12007-01-30 13:11:14 -08002740
Eric Wong060610c2007-12-08 23:27:41 -08002741 $self->rev_map_set($rev, $c);
Eric Wongf0ecca12007-01-30 13:11:14 -08002742 print "r$rev = $c\n";
2743 }
Sam Vilain3dfab992007-06-30 20:56:13 +12002744 command_close_pipe($log, $ctx);
Deskin Miller2beec892008-09-15 21:12:58 -04002745 print "Done rebuilding $map_path\n" if (!$partial || !$head);
Eric Wong060610c2007-12-08 23:27:41 -08002746 my $rev_db_path = $self->rev_db_path;
2747 if (-f $self->rev_db_path) {
2748 unlink $self->rev_db_path or croak "unlink: $!";
2749 }
2750 $self->unlink_rev_db_symlink;
Eric Wongf0ecca12007-01-30 13:11:14 -08002751}
2752
Eric Wong060610c2007-12-08 23:27:41 -08002753# rev_map:
Eric Wong9b981fc2007-01-11 12:14:21 -08002754# Tie::File seems to be prone to offset errors if revisions get sparse,
2755# it's not that fast, either. Tie::File is also not in Perl 5.6. So
2756# one of my favorite modules is out :< Next up would be one of the DBM
Eric Wong060610c2007-12-08 23:27:41 -08002757# modules, but I'm not sure which is most portable...
2758#
2759# This is the replacement for the rev_db format, which was too big
2760# and inefficient for large repositories with a lot of sparse history
2761# (mainly tags)
2762#
2763# The format is this:
2764# - 24 bytes for every record,
2765# * 4 bytes for the integer representing an SVN revision number
2766# * 20 bytes representing the sha1 of a git commit
2767# - No empty padding records like the old format
Eric Wong66ab84b2007-12-08 23:27:42 -08002768# (except the last record, which can be overwritten)
Eric Wong060610c2007-12-08 23:27:41 -08002769# - new records are written append-only since SVN revision numbers
2770# increase monotonically
2771# - lookups on SVN revision number are done via a binary search
Eric Wong66ab84b2007-12-08 23:27:42 -08002772# - Piping the file to xxd -c24 is a good way of dumping it for
2773# viewing or editing (piped back through xxd -r), should the need
2774# ever arise.
2775# - The last record can be padding revision with an all-zero sha1
2776# This is used to optimize fetch performance when using multiple
2777# "fetch" directives in .git/config
Eric Wong060610c2007-12-08 23:27:41 -08002778#
Eric Wong97ae0912007-02-11 15:21:24 -08002779# These files are disposable unless noMetadata or useSvmProps is set
Eric Wong9b981fc2007-01-11 12:14:21 -08002780
Eric Wong060610c2007-12-08 23:27:41 -08002781sub _rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08002782 my ($fh, $rev, $commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08002783
Michael Weber4f7ec792008-04-18 15:12:04 +02002784 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002785 my $size = (stat($fh))[7];
2786 ($size % 24) == 0 or croak "inconsistent size: $size";
2787
Eric Wong66ab84b2007-12-08 23:27:42 -08002788 my $wr_offset = 0;
Eric Wong060610c2007-12-08 23:27:41 -08002789 if ($size > 0) {
2790 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
2791 my $read = sysread($fh, my $buf, 24) or croak "read: $!";
2792 $read == 24 or croak "read only $read bytes (!= 24)";
2793 my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08002794 if ($last_commit eq ('0' x40)) {
2795 if ($size >= 48) {
2796 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
2797 $read = sysread($fh, $buf, 24) or
2798 croak "read: $!";
2799 $read == 24 or
2800 croak "read only $read bytes (!= 24)";
2801 ($last_rev, $last_commit) =
2802 unpack(rev_map_fmt, $buf);
2803 if ($last_commit eq ('0' x40)) {
2804 croak "inconsistent .rev_map\n";
2805 }
2806 }
2807 if ($last_rev >= $rev) {
2808 croak "last_rev is higher!: $last_rev >= $rev";
2809 }
2810 $wr_offset = -24;
Eric Wong47a0b752007-01-31 05:13:30 -08002811 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002812 }
Eric Wong66ab84b2007-12-08 23:27:42 -08002813 sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002814 syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or
2815 croak "write: $!";
Eric Wong26a62d52007-02-12 13:25:25 -08002816}
2817
2818sub mkfile {
2819 my ($path) = @_;
2820 unless (-e $path) {
2821 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
2822 mkpath([$dir]) unless -d $dir;
2823 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
2824 close $fh or die "Couldn't close (create) $path: $!\n";
2825 }
2826}
2827
Eric Wong060610c2007-12-08 23:27:41 -08002828sub rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08002829 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
2830 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
Eric Wong060610c2007-12-08 23:27:41 -08002831 my $db = $self->map_path($uuid);
Eric Wong26a62d52007-02-12 13:25:25 -08002832 my $db_lock = "$db.lock";
2833 my $sig;
2834 if ($update_ref) {
2835 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
2836 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
2837 }
2838 mkfile($db);
2839
2840 $LOCKFILES{$db_lock} = 1;
2841 my $sync;
2842 # both of these options make our .rev_db file very, very important
2843 # and we can't afford to lose it because rebuild() won't work
2844 if ($self->use_svm_props || $self->no_metadata) {
2845 $sync = 1;
Eric Wong060610c2007-12-08 23:27:41 -08002846 copy($db, $db_lock) or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08002847 "Failed to copy: ",
2848 "$db => $db_lock ($!)\n";
2849 } else {
Eric Wong060610c2007-12-08 23:27:41 -08002850 rename $db, $db_lock or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08002851 "Failed to rename: ",
2852 "$db => $db_lock ($!)\n";
2853 }
Eric Wong060610c2007-12-08 23:27:41 -08002854
Eric Wong66ab84b2007-12-08 23:27:42 -08002855 sysopen(my $fh, $db_lock, O_RDWR | O_CREAT)
Eric Wong060610c2007-12-08 23:27:41 -08002856 or croak "Couldn't open $db_lock: $!\n";
2857 _rev_map_set($fh, $rev, $commit);
Eric Wong97ae0912007-02-11 15:21:24 -08002858 if ($sync) {
2859 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2860 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2861 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002862 close $fh or croak $!;
Eric Wong373274f2007-01-31 13:54:23 -08002863 if ($update_ref) {
Eric Wong1e889ef2007-02-16 01:45:13 -08002864 $_head = $self;
Eric Wong373274f2007-01-31 13:54:23 -08002865 command_noisy('update-ref', '-m', "r$rev",
2866 $self->refname, $commit);
2867 }
Eric Wong060610c2007-12-08 23:27:41 -08002868 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
Eric Wong373274f2007-01-31 13:54:23 -08002869 "$db_lock => $db ($!)\n";
2870 delete $LOCKFILES{$db_lock};
2871 if ($update_ref) {
2872 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
2873 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
2874 kill $sig, $$ if defined $sig;
2875 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002876}
2877
Eric Wong66ab84b2007-12-08 23:27:42 -08002878# If want_commit, this will return an array of (rev, commit) where
2879# commit _must_ be a valid commit in the archive.
2880# Otherwise, it'll return the max revision (whether or not the
2881# commit is valid or just a 0x40 placeholder).
Eric Wong060610c2007-12-08 23:27:41 -08002882sub rev_map_max {
Eric Wong66ab84b2007-12-08 23:27:42 -08002883 my ($self, $want_commit) = @_;
Eric Wongd6d33462007-02-16 04:05:33 -08002884 $self->rebuild;
Deskin Miller2beec892008-09-15 21:12:58 -04002885 my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
2886 $want_commit ? ($r, $c) : $r;
2887}
2888
2889sub rev_map_max_norebuild {
2890 my ($self, $want_commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08002891 my $map_path = $self->map_path;
Eric Wong66ab84b2007-12-08 23:27:42 -08002892 stat $map_path or return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08002893 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02002894 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002895 my $size = (stat($fh))[7];
2896 ($size % 24) == 0 or croak "inconsistent size: $size";
2897
2898 if ($size == 0) {
2899 close $fh or croak "close: $!";
Eric Wong66ab84b2007-12-08 23:27:42 -08002900 return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08002901 }
2902
Eric Wong66ab84b2007-12-08 23:27:42 -08002903 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002904 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002905 my ($r, $c) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08002906 if ($want_commit && $c eq ('0' x40)) {
2907 if ($size < 48) {
2908 return $want_commit ? (0, undef) : 0;
2909 }
2910 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
2911 sysread($fh, $buf, 24) == 24 or croak "read: $!";
2912 ($r, $c) = unpack(rev_map_fmt, $buf);
2913 if ($c eq ('0'x40)) {
2914 croak "Penultimate record is all-zeroes in $map_path";
2915 }
2916 }
2917 close $fh or croak "close: $!";
2918 $want_commit ? ($r, $c) : $r;
Eric Wong9c93fee2007-01-31 17:22:31 -08002919}
2920
Eric Wong060610c2007-12-08 23:27:41 -08002921sub rev_map_get {
Eric Wong26a62d52007-02-12 13:25:25 -08002922 my ($self, $rev, $uuid) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08002923 my $map_path = $self->map_path($uuid);
2924 return undef unless -e $map_path;
2925
2926 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02002927 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002928 my $size = (stat($fh))[7];
2929 ($size % 24) == 0 or croak "inconsistent size: $size";
2930
2931 if ($size == 0) {
2932 close $fh or croak "close: $fh";
2933 return undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08002934 }
Eric Wong060610c2007-12-08 23:27:41 -08002935
2936 my ($l, $u) = (0, $size - 24);
2937 my ($r, $c, $buf);
2938
2939 while ($l <= $u) {
2940 my $i = int(($l/24 + $u/24) / 2) * 24;
2941 sysseek($fh, $i, SEEK_SET) or croak "seek: $!";
2942 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
2943 my ($r, $c) = unpack('NH40', $buf);
2944
2945 if ($r < $rev) {
2946 $l = $i + 24;
2947 } elsif ($r > $rev) {
2948 $u = $i - 24;
2949 } else { # $r == $rev
2950 close($fh) or croak "close: $!";
Eric Wong66ab84b2007-12-08 23:27:42 -08002951 return $c eq ('0' x 40) ? undef : $c;
Eric Wong060610c2007-12-08 23:27:41 -08002952 }
2953 }
2954 close($fh) or croak "close: $!";
2955 undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08002956}
2957
David D Kilzer111947e2007-11-11 22:56:52 -08002958# Finds the first svn revision that exists on (if $eq_ok is true) or
2959# before $rev for the current branch. It will not search any lower
2960# than $min_rev. Returns the git commit hash and svn revision number
2961# if found, else (undef, undef).
Eric Wong15710b62007-01-22 02:20:33 -08002962sub find_rev_before {
David D Kilzer111947e2007-11-11 22:56:52 -08002963 my ($self, $rev, $eq_ok, $min_rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08002964 --$rev unless $eq_ok;
David D Kilzer111947e2007-11-11 22:56:52 -08002965 $min_rev ||= 1;
2966 while ($rev >= $min_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08002967 if (my $c = $self->rev_map_get($rev)) {
Eric Wong15710b62007-01-22 02:20:33 -08002968 return ($rev, $c);
2969 }
2970 --$rev;
2971 }
2972 return (undef, undef);
2973}
2974
David D Kilzer111947e2007-11-11 22:56:52 -08002975# Finds the first svn revision that exists on (if $eq_ok is true) or
2976# after $rev for the current branch. It will not search any higher
2977# than $max_rev. Returns the git commit hash and svn revision number
2978# if found, else (undef, undef).
2979sub find_rev_after {
2980 my ($self, $rev, $eq_ok, $max_rev) = @_;
2981 ++$rev unless $eq_ok;
Eric Wong060610c2007-12-08 23:27:41 -08002982 $max_rev ||= $self->rev_map_max;
David D Kilzer111947e2007-11-11 22:56:52 -08002983 while ($rev <= $max_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08002984 if (my $c = $self->rev_map_get($rev)) {
David D Kilzer111947e2007-11-11 22:56:52 -08002985 return ($rev, $c);
2986 }
2987 ++$rev;
2988 }
2989 return (undef, undef);
2990}
2991
Eric Wong9b981fc2007-01-11 12:14:21 -08002992sub _new {
Eric Wong706587f2007-01-18 17:50:01 -08002993 my ($class, $repo_id, $ref_id, $path) = @_;
2994 unless (defined $repo_id && length $repo_id) {
2995 $repo_id = $Git::SVN::default_repo_id;
2996 }
2997 unless (defined $ref_id && length $ref_id) {
Eric Wong8b8fc062007-01-22 11:44:57 -08002998 $_[2] = $ref_id = $Git::SVN::default_ref_id;
Eric Wong706587f2007-01-18 17:50:01 -08002999 }
Eric Wong7829f202008-06-28 20:40:32 -07003000 $_[1] = $repo_id;
Eric Wong706587f2007-01-18 17:50:01 -08003001 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
3002 $_[3] = $path = '' unless (defined $path);
Eric Wongb4d57e52007-02-14 15:10:44 -08003003 mkpath(["$ENV{GIT_DIR}/svn"]);
Eric Wong26a62d52007-02-12 13:25:25 -08003004 bless {
3005 ref_id => $ref_id, dir => $dir, index => "$dir/index",
Eric Wong8a49ee92007-02-10 20:46:50 -08003006 path => $path, config => "$ENV{GIT_DIR}/svn/config",
Eric Wong060610c2007-12-08 23:27:41 -08003007 map_root => "$dir/.rev_map", repo_id => $repo_id }, $class;
Eric Wong26a62d52007-02-12 13:25:25 -08003008}
3009
Eric Wong060610c2007-12-08 23:27:41 -08003010# for read-only access of old .rev_db formats
3011sub unlink_rev_db_symlink {
3012 my ($self) = @_;
3013 my $link = $self->rev_db_path;
3014 $link =~ s/\.[\w-]+$// or croak "missing UUID at the end of $link";
3015 if (-l $link) {
3016 unlink $link or croak "unlink: $link failed!";
3017 }
3018}
3019
3020sub rev_db_path {
3021 my ($self, $uuid) = @_;
3022 my $db_path = $self->map_path($uuid);
3023 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
3024 or croak "map_path: $db_path does not contain '/.rev_map.' !";
3025 $db_path;
3026}
3027
3028# the new replacement for .rev_db
3029sub map_path {
Eric Wong26a62d52007-02-12 13:25:25 -08003030 my ($self, $uuid) = @_;
3031 $uuid ||= $self->ra_uuid;
Eric Wong060610c2007-12-08 23:27:41 -08003032 "$self->{map_root}.$uuid";
Eric Wong9b981fc2007-01-11 12:14:21 -08003033}
3034
Eric Wong1c8443b2007-01-14 02:17:00 -08003035sub uri_encode {
3036 my ($f) = @_;
3037 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
3038 $f
3039}
Eric Wong9b981fc2007-01-11 12:14:21 -08003040
Sam Vilain18ea92b2007-02-23 12:32:29 +13003041sub remove_username {
3042 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
3043}
3044
Eric Wongd976acf2007-01-04 00:45:03 -08003045package Git::SVN::Prompt;
3046use strict;
3047use warnings;
3048require SVN::Core;
3049use vars qw/$_no_auth_cache $_username/;
3050
3051sub simple {
Eric Wong30d055a2006-11-24 01:38:04 -08003052 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
3053 $may_save = undef if $_no_auth_cache;
3054 $default_username = $_username if defined $_username;
3055 if (defined $default_username && length $default_username) {
3056 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003057 print STDERR "Authentication realm: $realm\n";
3058 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003059 }
3060 $cred->username($default_username);
3061 } else {
Eric Wongd976acf2007-01-04 00:45:03 -08003062 username($cred, $realm, $may_save, $pool);
Eric Wong30d055a2006-11-24 01:38:04 -08003063 }
3064 $cred->password(_read_password("Password for '" .
3065 $cred->username . "': ", $realm));
3066 $cred->may_save($may_save);
3067 $SVN::_Core::SVN_NO_ERROR;
3068}
3069
Eric Wongd976acf2007-01-04 00:45:03 -08003070sub ssl_server_trust {
Eric Wong30d055a2006-11-24 01:38:04 -08003071 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
3072 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003073 print STDERR "Error validating server certificate for '$realm':\n";
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003074 {
3075 no warnings 'once';
3076 # All variables SVN::Auth::SSL::* are used only once,
3077 # so we're shutting up Perl warnings about this.
3078 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
3079 print STDERR " - The certificate is not issued ",
3080 "by a trusted authority. Use the\n",
3081 " fingerprint to validate ",
3082 "the certificate manually!\n";
3083 }
3084 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
3085 print STDERR " - The certificate hostname ",
3086 "does not match.\n";
3087 }
3088 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
3089 print STDERR " - The certificate is not yet valid.\n";
3090 }
3091 if ($failures & $SVN::Auth::SSL::EXPIRED) {
3092 print STDERR " - The certificate has expired.\n";
3093 }
3094 if ($failures & $SVN::Auth::SSL::OTHER) {
3095 print STDERR " - The certificate has ",
3096 "an unknown error.\n";
3097 }
3098 } # no warnings 'once'
Eric Wong6f729592007-01-15 20:15:55 -08003099 printf STDERR
3100 "Certificate information:\n".
Eric Wong30d055a2006-11-24 01:38:04 -08003101 " - Hostname: %s\n".
3102 " - Valid: from %s until %s\n".
3103 " - Issuer: %s\n".
3104 " - Fingerprint: %s\n",
3105 map $cert_info->$_, qw(hostname valid_from valid_until
Eric Wong6f729592007-01-15 20:15:55 -08003106 issuer_dname fingerprint);
Eric Wong30d055a2006-11-24 01:38:04 -08003107 my $choice;
3108prompt:
Eric Wong6f729592007-01-15 20:15:55 -08003109 print STDERR $may_save ?
Eric Wong30d055a2006-11-24 01:38:04 -08003110 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
3111 "(R)eject or accept (t)emporarily? ";
Eric Wong6f729592007-01-15 20:15:55 -08003112 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003113 $choice = lc(substr(<STDIN> || 'R', 0, 1));
3114 if ($choice =~ /^t$/i) {
3115 $cred->may_save(undef);
3116 } elsif ($choice =~ /^r$/i) {
3117 return -1;
3118 } elsif ($may_save && $choice =~ /^p$/i) {
3119 $cred->may_save($may_save);
3120 } else {
3121 goto prompt;
3122 }
3123 $cred->accepted_failures($failures);
3124 $SVN::_Core::SVN_NO_ERROR;
3125}
3126
Eric Wongd976acf2007-01-04 00:45:03 -08003127sub ssl_client_cert {
Eric Wong30d055a2006-11-24 01:38:04 -08003128 my ($cred, $realm, $may_save, $pool) = @_;
3129 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003130 print STDERR "Client certificate filename: ";
3131 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003132 chomp(my $filename = <STDIN>);
3133 $cred->cert_file($filename);
3134 $cred->may_save($may_save);
3135 $SVN::_Core::SVN_NO_ERROR;
3136}
3137
Eric Wongd976acf2007-01-04 00:45:03 -08003138sub ssl_client_cert_pw {
Eric Wong30d055a2006-11-24 01:38:04 -08003139 my ($cred, $realm, $may_save, $pool) = @_;
3140 $may_save = undef if $_no_auth_cache;
3141 $cred->password(_read_password("Password: ", $realm));
3142 $cred->may_save($may_save);
3143 $SVN::_Core::SVN_NO_ERROR;
3144}
3145
Eric Wongd976acf2007-01-04 00:45:03 -08003146sub username {
Eric Wong30d055a2006-11-24 01:38:04 -08003147 my ($cred, $realm, $may_save, $pool) = @_;
3148 $may_save = undef if $_no_auth_cache;
3149 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003150 print STDERR "Authentication realm: $realm\n";
Eric Wong30d055a2006-11-24 01:38:04 -08003151 }
3152 my $username;
3153 if (defined $_username) {
3154 $username = $_username;
3155 } else {
Eric Wong6f729592007-01-15 20:15:55 -08003156 print STDERR "Username: ";
3157 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003158 chomp($username = <STDIN>);
3159 }
3160 $cred->username($username);
3161 $cred->may_save($may_save);
3162 $SVN::_Core::SVN_NO_ERROR;
3163}
3164
3165sub _read_password {
3166 my ($prompt, $realm) = @_;
Eric Wong6f729592007-01-15 20:15:55 -08003167 print STDERR $prompt;
3168 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003169 require Term::ReadKey;
3170 Term::ReadKey::ReadMode('noecho');
3171 my $password = '';
3172 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
3173 last if $key =~ /[\012\015]/; # \n\r
3174 $password .= $key;
3175 }
3176 Term::ReadKey::ReadMode('restore');
Eric Wong6f729592007-01-15 20:15:55 -08003177 print STDERR "\n";
3178 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003179 $password;
3180}
3181
Eric Wong27a1a802006-11-27 21:44:48 -08003182package SVN::Git::Fetcher;
3183use vars qw/@ISA/;
3184use strict;
3185use warnings;
3186use Carp qw/croak/;
Adam Robenffe256f2008-05-23 16:19:41 +02003187use File::Temp qw/tempfile/;
Eric Wong27a1a802006-11-27 21:44:48 -08003188use IO::File qw//;
3189
3190# file baton members: path, mode_a, mode_b, pool, fh, blob, base
3191sub new {
3192 my ($class, $git_svn) = @_;
3193 my $self = SVN::Delta::Editor->new;
3194 bless $self, $class;
Eric Wong1c8443b2007-01-14 02:17:00 -08003195 $self->{c} = $git_svn->{last_commit} if exists $git_svn->{last_commit};
Eric Wongd2a9a872006-12-12 14:47:00 -08003196 $self->{empty} = {};
3197 $self->{dir_prop} = {};
3198 $self->{file_prop} = {};
3199 $self->{absent_dir} = {};
3200 $self->{absent_file} = {};
Eric Wongef3cfaa2007-01-24 03:30:57 -08003201 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
Eric Wong27a1a802006-11-27 21:44:48 -08003202 $self;
3203}
3204
Eric Wong8b8fc062007-01-22 11:44:57 -08003205sub set_path_strip {
3206 my ($self, $path) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003207 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
Eric Wong8b8fc062007-01-22 11:44:57 -08003208}
3209
Eric Wongd2a9a872006-12-12 14:47:00 -08003210sub open_root {
3211 { path => '' };
3212}
3213
3214sub open_directory {
3215 my ($self, $path, $pb, $rev) = @_;
3216 { path => $path };
3217}
3218
Eric Wong706587f2007-01-18 17:50:01 -08003219sub git_path {
3220 my ($self, $path) = @_;
Eric Wong2b27f6c2007-01-28 04:59:05 -08003221 if ($self->{path_strip}) {
3222 $path =~ s!$self->{path_strip}!! or
3223 die "Failed to strip path '$path' ($self->{path_strip})\n";
3224 }
Eric Wong706587f2007-01-18 17:50:01 -08003225 $path;
3226}
3227
Eric Wong27a1a802006-11-27 21:44:48 -08003228sub delete_entry {
3229 my ($self, $path, $rev, $pb) = @_;
Eric Wong4a87db02007-01-04 01:38:18 -08003230
Eric Wong706587f2007-01-18 17:50:01 -08003231 my $gpath = $self->git_path($path);
Eric Wong8a603772007-01-31 02:45:50 -08003232 return undef if ($gpath eq '');
3233
Eric Wong4a87db02007-01-04 01:38:18 -08003234 # remove entire directories.
Eric Wong706587f2007-01-18 17:50:01 -08003235 if (command('ls-tree', $self->{c}, '--', $gpath) =~ /^040000 tree/) {
Eric Wong4a87db02007-01-04 01:38:18 -08003236 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3237 -r --name-only -z/,
Eric Wong706587f2007-01-18 17:50:01 -08003238 $self->{c}, '--', $gpath);
Eric Wong4a87db02007-01-04 01:38:18 -08003239 local $/ = "\0";
3240 while (<$ls>) {
Eric Wongef3cfaa2007-01-24 03:30:57 -08003241 chomp;
3242 $self->{gii}->remove($_);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003243 print "\tD\t$_\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003244 }
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003245 print "\tD\t$gpath/\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003246 command_close_pipe($ls, $ctx);
3247 $self->{empty}->{$path} = 0
3248 } else {
Eric Wongef3cfaa2007-01-24 03:30:57 -08003249 $self->{gii}->remove($gpath);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003250 print "\tD\t$gpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003251 }
Eric Wong27a1a802006-11-27 21:44:48 -08003252 undef;
3253}
3254
3255sub open_file {
3256 my ($self, $path, $pb, $rev) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08003257 my $gpath = $self->git_path($path);
3258 my ($mode, $blob) = (command('ls-tree', $self->{c}, '--', $gpath)
Eric Wong27a1a802006-11-27 21:44:48 -08003259 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
Eric Wong006ede52006-12-08 01:55:19 -08003260 unless (defined $mode && defined $blob) {
3261 die "$path was not found in commit $self->{c} (r$rev)\n";
3262 }
Eric Wong27a1a802006-11-27 21:44:48 -08003263 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
Eric Wong0864e3b2006-11-28 02:50:17 -08003264 pool => SVN::Pool->new, action => 'M' };
Eric Wong27a1a802006-11-27 21:44:48 -08003265}
3266
3267sub add_file {
3268 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
Eric Wongd2a9a872006-12-12 14:47:00 -08003269 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3270 delete $self->{empty}->{$dir};
Eric Wong27a1a802006-11-27 21:44:48 -08003271 { path => $path, mode_a => 100644, mode_b => 100644,
Eric Wong0864e3b2006-11-28 02:50:17 -08003272 pool => SVN::Pool->new, action => 'A' };
Eric Wong27a1a802006-11-27 21:44:48 -08003273}
3274
Eric Wongd2a9a872006-12-12 14:47:00 -08003275sub add_directory {
3276 my ($self, $path, $cp_path, $cp_rev) = @_;
Eric Wong12a6d752007-12-14 08:39:09 -08003277 my $gpath = $self->git_path($path);
3278 if ($gpath eq '') {
3279 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3280 -r --name-only -z/,
3281 $self->{c});
3282 local $/ = "\0";
3283 while (<$ls>) {
3284 chomp;
3285 $self->{gii}->remove($_);
3286 print "\tD\t$_\n" unless $::_q;
3287 }
3288 command_close_pipe($ls, $ctx);
3289 $self->{empty}->{$path} = 0;
3290 }
Eric Wongd2a9a872006-12-12 14:47:00 -08003291 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3292 delete $self->{empty}->{$dir};
3293 $self->{empty}->{$path} = 1;
3294 { path => $path };
3295}
3296
3297sub change_dir_prop {
3298 my ($self, $db, $prop, $value) = @_;
3299 $self->{dir_prop}->{$db->{path}} ||= {};
3300 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
3301 undef;
3302}
3303
3304sub absent_directory {
3305 my ($self, $path, $pb) = @_;
3306 $self->{absent_dir}->{$pb->{path}} ||= [];
3307 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
3308 undef;
3309}
3310
3311sub absent_file {
3312 my ($self, $path, $pb) = @_;
3313 $self->{absent_file}->{$pb->{path}} ||= [];
3314 push @{$self->{absent_file}->{$pb->{path}}}, $path;
3315 undef;
3316}
3317
Eric Wong27a1a802006-11-27 21:44:48 -08003318sub change_file_prop {
3319 my ($self, $fb, $prop, $value) = @_;
3320 if ($prop eq 'svn:executable') {
3321 if ($fb->{mode_b} != 120000) {
3322 $fb->{mode_b} = defined $value ? 100755 : 100644;
3323 }
3324 } elsif ($prop eq 'svn:special') {
3325 $fb->{mode_b} = defined $value ? 120000 : 100644;
Eric Wongd2a9a872006-12-12 14:47:00 -08003326 } else {
3327 $self->{file_prop}->{$fb->{path}} ||= {};
3328 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
Eric Wong27a1a802006-11-27 21:44:48 -08003329 }
3330 undef;
3331}
3332
3333sub apply_textdelta {
3334 my ($self, $fb, $exp) = @_;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003335 my $fh = $::_repository->temp_acquire('svn_delta');
Eric Wong27a1a802006-11-27 21:44:48 -08003336 # $fh gets auto-closed() by SVN::TxDelta::apply(),
3337 # (but $base does not,) so dup() it for reading in close_file
3338 open my $dup, '<&', $fh or croak $!;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003339 my $base = $::_repository->temp_acquire('git_blob');
Eric Wong27a1a802006-11-27 21:44:48 -08003340 if ($fb->{blob}) {
Adam Robenffe256f2008-05-23 16:19:41 +02003341 print $base 'link ' if ($fb->{mode_a} == 120000);
3342 my $size = $::_repository->cat_blob($fb->{blob}, $base);
Junio C Hamanod683a0e2008-05-27 23:33:22 -07003343 die "Failed to read object $fb->{blob}" if ($size < 0);
Eric Wong27a1a802006-11-27 21:44:48 -08003344
3345 if (defined $exp) {
3346 seek $base, 0, 0 or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08003347 my $got = ::md5sum($base);
Eric Wong27a1a802006-11-27 21:44:48 -08003348 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
3349 "expected: $exp\n",
3350 " got: $got\n" if ($got ne $exp);
3351 }
3352 }
3353 seek $base, 0, 0 or croak $!;
Marcus Griep0b191382008-08-12 12:00:53 -04003354 $fb->{fh} = $fh;
Eric Wong27a1a802006-11-27 21:44:48 -08003355 $fb->{base} = $base;
Marcus Griep0b191382008-08-12 12:00:53 -04003356 [ SVN::TxDelta::apply($base, $dup, undef, $fb->{path}, $fb->{pool}) ];
Eric Wong27a1a802006-11-27 21:44:48 -08003357}
3358
3359sub close_file {
3360 my ($self, $fb, $exp) = @_;
3361 my $hash;
Eric Wong706587f2007-01-18 17:50:01 -08003362 my $path = $self->git_path($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08003363 if (my $fh = $fb->{fh}) {
Eric Wong7faf0682007-05-27 15:59:01 -07003364 if (defined $exp) {
3365 seek($fh, 0, 0) or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08003366 my $got = ::md5sum($fh);
Eric Wong7faf0682007-05-27 15:59:01 -07003367 if ($got ne $exp) {
3368 die "Checksum mismatch: $path\n",
3369 "expected: $exp\n got: $got\n";
3370 }
3371 }
Eric Wong27a1a802006-11-27 21:44:48 -08003372 if ($fb->{mode_b} == 120000) {
Marcus Griep510b0942008-08-12 12:45:39 -04003373 sysseek($fh, 0, 0) or croak $!;
3374 sysread($fh, my $buf, 5) == 5 or croak $!;
3375
3376 unless ($buf eq 'link ') {
3377 warn "$path has mode 120000",
3378 " but is not a link\n";
3379 } else {
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003380 my $tmp_fh = $::_repository->temp_acquire(
3381 'svn_hash');
Marcus Griep510b0942008-08-12 12:45:39 -04003382 my $res;
3383 while ($res = sysread($fh, my $str, 1024)) {
3384 my $out = syswrite($tmp_fh, $str, $res);
3385 defined($out) && $out == $res
3386 or croak("write ",
Marcus Griep836ff952008-09-08 12:53:01 -04003387 Git::temp_path($tmp_fh),
Marcus Griep510b0942008-08-12 12:45:39 -04003388 ": $!\n");
3389 }
3390 defined $res or croak $!;
3391
3392 ($fh, $tmp_fh) = ($tmp_fh, $fh);
3393 Git::temp_release($tmp_fh, 1);
Eric Wong7fc35e02007-12-19 00:06:45 -08003394 }
Eric Wong27a1a802006-11-27 21:44:48 -08003395 }
Adam Robenffe256f2008-05-23 16:19:41 +02003396
Marcus Griep0b191382008-08-12 12:00:53 -04003397 $hash = $::_repository->hash_and_insert_object(
Marcus Griep836ff952008-09-08 12:53:01 -04003398 Git::temp_path($fh));
Eric Wong27a1a802006-11-27 21:44:48 -08003399 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
Marcus Griep0b191382008-08-12 12:00:53 -04003400
3401 Git::temp_release($fb->{base}, 1);
Marcus Griep510b0942008-08-12 12:45:39 -04003402 Git::temp_release($fh, 1);
Eric Wong27a1a802006-11-27 21:44:48 -08003403 } else {
3404 $hash = $fb->{blob} or die "no blob information\n";
3405 }
3406 $fb->{pool}->clear;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003407 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003408 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
Eric Wong27a1a802006-11-27 21:44:48 -08003409 undef;
3410}
3411
3412sub abort_edit {
3413 my $self = shift;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003414 $self->{nr} = $self->{gii}->{nr};
3415 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08003416 $self->SUPER::abort_edit(@_);
3417}
3418
3419sub close_edit {
3420 my $self = shift;
Eric Wongdad73c02006-11-28 14:06:05 -08003421 $self->{git_commit_ok} = 1;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003422 $self->{nr} = $self->{gii}->{nr};
3423 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08003424 $self->SUPER::close_edit(@_);
3425}
Eric Wong1a82e792006-06-16 02:55:13 -07003426
Eric Wonga5e0ced2006-06-12 15:23:48 -07003427package SVN::Git::Editor;
Eric Wong24e22aa2007-01-29 00:07:49 -08003428use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003429use strict;
3430use warnings;
3431use Carp qw/croak/;
3432use IO::File;
3433
3434sub new {
Eric Wong61395352007-01-27 14:33:08 -08003435 my ($class, $opts) = @_;
3436 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
3437 die "$_ required!\n" unless (defined $opts->{$_});
Eric Wonga5e0ced2006-06-12 15:23:48 -07003438 }
Eric Wong61395352007-01-27 14:33:08 -08003439
3440 my $pool = SVN::Pool->new;
3441 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
3442 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
3443 $opts->{r}, $mods);
3444
3445 # $opts->{ra} functions should not be used after this:
3446 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
3447 $opts->{editor_cb}, $pool);
3448 my $self = SVN::Delta::Editor->new(@ce, $pool);
3449 bless $self, $class;
3450 foreach (qw/svn_path r tree_a tree_b/) {
3451 $self->{$_} = $opts->{$_};
3452 }
3453 $self->{url} = $opts->{ra}->{url};
3454 $self->{mods} = $mods;
3455 $self->{types} = $types;
3456 $self->{pool} = $pool;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003457 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
3458 $self->{rm} = { };
Eric Wongd3a840d2007-01-26 01:32:45 -08003459 $self->{path_prefix} = length $self->{svn_path} ?
3460 "$self->{svn_path}/" : '';
Brad King128de652008-07-25 11:32:37 -04003461 $self->{config} = $opts->{config};
Eric Wonga5e0ced2006-06-12 15:23:48 -07003462 return $self;
3463}
3464
Eric Wong61395352007-01-27 14:33:08 -08003465sub generate_diff {
3466 my ($tree_a, $tree_b) = @_;
3467 my @diff_tree = qw(diff-tree -z -r);
Eric Wong24e22aa2007-01-29 00:07:49 -08003468 if ($_cp_similarity) {
3469 push @diff_tree, "-C$_cp_similarity";
Eric Wong61395352007-01-27 14:33:08 -08003470 } else {
3471 push @diff_tree, '-C';
3472 }
Eric Wong24e22aa2007-01-29 00:07:49 -08003473 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
3474 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
Eric Wong61395352007-01-27 14:33:08 -08003475 push @diff_tree, $tree_a, $tree_b;
3476 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
3477 local $/ = "\0";
3478 my $state = 'meta';
3479 my @mods;
3480 while (<$diff_fh>) {
3481 chomp $_; # this gets rid of the trailing "\0"
3482 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02003483 ($::sha1)\s($::sha1)\s
Eric Wong61395352007-01-27 14:33:08 -08003484 ([MTCRAD])\d*$/xo) {
3485 push @mods, { mode_a => $1, mode_b => $2,
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02003486 sha1_a => $3, sha1_b => $4,
3487 chg => $5 };
3488 if ($5 =~ /^(?:C|R)$/) {
Eric Wong61395352007-01-27 14:33:08 -08003489 $state = 'file_a';
3490 } else {
3491 $state = 'file_b';
3492 }
3493 } elsif ($state eq 'file_a') {
3494 my $x = $mods[$#mods] or croak "Empty array\n";
3495 if ($x->{chg} !~ /^(?:C|R)$/) {
3496 croak "Error parsing $_, $x->{chg}\n";
3497 }
3498 $x->{file_a} = $_;
3499 $state = 'file_b';
3500 } elsif ($state eq 'file_b') {
3501 my $x = $mods[$#mods] or croak "Empty array\n";
3502 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
3503 croak "Error parsing $_, $x->{chg}\n";
3504 }
3505 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
3506 croak "Error parsing $_, $x->{chg}\n";
3507 }
3508 $x->{file_b} = $_;
3509 $state = 'meta';
3510 } else {
3511 croak "Error parsing $_\n";
3512 }
3513 }
3514 command_close_pipe($diff_fh, $ctx);
3515 \@mods;
3516}
3517
3518sub check_diff_paths {
3519 my ($ra, $pfx, $rev, $mods) = @_;
3520 my %types;
3521 $pfx .= '/' if length $pfx;
3522
3523 sub type_diff_paths {
3524 my ($ra, $types, $path, $rev) = @_;
3525 my @p = split m#/+#, $path;
3526 my $c = shift @p;
3527 unless (defined $types->{$c}) {
3528 $types->{$c} = $ra->check_path($c, $rev);
3529 }
3530 while (@p) {
3531 $c .= '/' . shift @p;
3532 next if defined $types->{$c};
3533 $types->{$c} = $ra->check_path($c, $rev);
3534 }
3535 }
3536
3537 foreach my $m (@$mods) {
3538 foreach my $f (qw/file_a file_b/) {
3539 next unless defined $m->{$f};
3540 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
3541 if (length $pfx.$dir && ! defined $types{$dir}) {
3542 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
3543 }
3544 }
3545 }
3546 \%types;
3547}
3548
Eric Wonga5e0ced2006-06-12 15:23:48 -07003549sub split_path {
3550 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
3551}
3552
3553sub repo_path {
Eric Wongd3a840d2007-01-26 01:32:45 -08003554 my ($self, $path) = @_;
3555 $self->{path_prefix}.(defined $path ? $path : '');
Eric Wonga5e0ced2006-06-12 15:23:48 -07003556}
3557
3558sub url_path {
3559 my ($self, $path) = @_;
Eric Wong29633bb2007-07-15 21:53:50 -07003560 if ($self->{url} =~ m#^https?://#) {
Eric Wong6a004d32008-10-21 14:12:15 -07003561 $path =~ s/([^~a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
Eric Wong29633bb2007-07-15 21:53:50 -07003562 }
Eric Wong6e8548c2007-01-27 01:32:00 -08003563 $self->{url} . '/' . $self->repo_path($path);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003564}
3565
3566sub rmdirs {
Eric Wong61395352007-01-27 14:33:08 -08003567 my ($self) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003568 my $rm = $self->{rm};
3569 delete $rm->{''}; # we never delete the url we're tracking
3570 return unless %$rm;
3571
3572 foreach (keys %$rm) {
3573 my @d = split m#/#, $_;
3574 my $c = shift @d;
3575 $rm->{$c} = 1;
3576 while (@d) {
3577 $c .= '/' . shift @d;
3578 $rm->{$c} = 1;
3579 }
3580 }
3581 delete $rm->{$self->{svn_path}};
3582 delete $rm->{''}; # we never delete the url we're tracking
3583 return unless %$rm;
3584
Eric Wong61395352007-01-27 14:33:08 -08003585 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
3586 $self->{tree_b});
Eric Wonga5e0ced2006-06-12 15:23:48 -07003587 local $/ = "\0";
3588 while (<$fh>) {
3589 chomp;
Eric Wong747fa122006-11-24 22:38:17 -08003590 my @dn = split m#/#, $_;
Eric Wongc07eee12006-06-19 17:59:35 -07003591 while (pop @dn) {
3592 delete $rm->{join '/', @dn};
3593 }
3594 unless (%$rm) {
Eric Wong22600a22007-02-01 13:12:26 -08003595 close $fh;
Eric Wongc07eee12006-06-19 17:59:35 -07003596 return;
3597 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07003598 }
Eric Wongaef4e922006-12-15 10:59:54 -08003599 command_close_pipe($fh, $ctx);
Eric Wongc07eee12006-06-19 17:59:35 -07003600
Eric Wonga5e0ced2006-06-12 15:23:48 -07003601 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
3602 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3603 $self->close_directory($bat->{$d}, $p);
3604 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
Eric Wong44320b92007-01-13 22:35:53 -08003605 print "\tD+\t$d/\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003606 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
3607 delete $bat->{$d};
3608 }
3609}
3610
3611sub open_or_add_dir {
3612 my ($self, $full_path, $baton) = @_;
Eric Wong6e8548c2007-01-27 01:32:00 -08003613 my $t = $self->{types}->{$full_path};
3614 if (!defined $t) {
3615 die "$full_path not known in r$self->{r} or we have a bug!\n";
3616 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003617 {
3618 no warnings 'once';
3619 # SVN::Node::none and SVN::Node::file are used only once,
3620 # so we're shutting up Perl's warnings about them.
3621 if ($t == $SVN::Node::none) {
3622 return $self->add_directory($full_path, $baton,
3623 undef, -1, $self->{pool});
3624 } elsif ($t == $SVN::Node::dir) {
3625 return $self->open_directory($full_path, $baton,
3626 $self->{r}, $self->{pool});
3627 } # no warnings 'once'
3628 print STDERR "$full_path already exists in repository at ",
3629 "r$self->{r} and it is not a directory (",
3630 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
3631 } # no warnings 'once'
Eric Wonga5e0ced2006-06-12 15:23:48 -07003632 exit 1;
3633}
3634
3635sub ensure_path {
3636 my ($self, $path) = @_;
3637 my $bat = $self->{bat};
Eric Wong6e8548c2007-01-27 01:32:00 -08003638 my $repo_path = $self->repo_path($path);
3639 return $bat->{''} unless (length $repo_path);
3640 my @p = split m#/+#, $repo_path;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003641 my $c = shift @p;
3642 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3643 while (@p) {
3644 my $c0 = $c;
3645 $c .= '/' . shift @p;
3646 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3647 }
3648 return $bat->{$c};
3649}
3650
Brad King128de652008-07-25 11:32:37 -04003651# Subroutine to convert a globbing pattern to a regular expression.
3652# From perl cookbook.
3653sub glob2pat {
3654 my $globstr = shift;
3655 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
3656 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
3657 return '^' . $globstr . '$';
3658}
3659
3660sub check_autoprop {
3661 my ($self, $pattern, $properties, $file, $fbat) = @_;
3662 # Convert the globbing pattern to a regular expression.
3663 my $regex = glob2pat($pattern);
3664 # Check if the pattern matches the file name.
3665 if($file =~ m/($regex)/) {
3666 # Parse the list of properties to set.
3667 my @props = split(/;/, $properties);
3668 foreach my $prop (@props) {
3669 # Parse 'name=value' syntax and set the property.
3670 if ($prop =~ /([^=]+)=(.*)/) {
3671 my ($n,$v) = ($1,$2);
3672 for ($n, $v) {
3673 s/^\s+//; s/\s+$//;
3674 }
3675 $self->change_file_prop($fbat, $n, $v);
3676 }
3677 }
3678 }
3679}
3680
3681sub apply_autoprops {
3682 my ($self, $file, $fbat) = @_;
3683 my $conf_t = ${$self->{config}}{'config'};
3684 no warnings 'once';
3685 # Check [miscellany]/enable-auto-props in svn configuration.
3686 if (SVN::_Core::svn_config_get_bool(
3687 $conf_t,
3688 $SVN::_Core::SVN_CONFIG_SECTION_MISCELLANY,
3689 $SVN::_Core::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS,
3690 0)) {
3691 # Auto-props are enabled. Enumerate them to look for matches.
3692 my $callback = sub {
3693 $self->check_autoprop($_[0], $_[1], $file, $fbat);
3694 };
3695 SVN::_Core::svn_config_enumerate(
3696 $conf_t,
3697 $SVN::_Core::SVN_CONFIG_SECTION_AUTO_PROPS,
3698 $callback);
3699 }
3700}
3701
Eric Wonga5e0ced2006-06-12 15:23:48 -07003702sub A {
Eric Wong44320b92007-01-13 22:35:53 -08003703 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003704 my ($dir, $file) = split_path($m->{file_b});
3705 my $pbat = $self->ensure_path($dir);
3706 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3707 undef, -1);
Eric Wong44320b92007-01-13 22:35:53 -08003708 print "\tA\t$m->{file_b}\n" unless $::_q;
Brad King128de652008-07-25 11:32:37 -04003709 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003710 $self->chg_file($fbat, $m);
3711 $self->close_file($fbat,undef,$self->{pool});
3712}
3713
3714sub C {
Eric Wong44320b92007-01-13 22:35:53 -08003715 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003716 my ($dir, $file) = split_path($m->{file_b});
3717 my $pbat = $self->ensure_path($dir);
3718 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3719 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08003720 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003721 $self->chg_file($fbat, $m);
3722 $self->close_file($fbat,undef,$self->{pool});
3723}
3724
3725sub delete_entry {
3726 my ($self, $path, $pbat) = @_;
3727 my $rpath = $self->repo_path($path);
3728 my ($dir, $file) = split_path($rpath);
3729 $self->{rm}->{$dir} = 1;
3730 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
3731}
3732
3733sub R {
Eric Wong44320b92007-01-13 22:35:53 -08003734 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003735 my ($dir, $file) = split_path($m->{file_b});
3736 my $pbat = $self->ensure_path($dir);
3737 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3738 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08003739 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Paul Talacko7c4d0212008-09-06 18:50:38 -07003740 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003741 $self->chg_file($fbat, $m);
3742 $self->close_file($fbat,undef,$self->{pool});
3743
3744 ($dir, $file) = split_path($m->{file_a});
3745 $pbat = $self->ensure_path($dir);
3746 $self->delete_entry($m->{file_a}, $pbat);
3747}
3748
3749sub M {
Eric Wong44320b92007-01-13 22:35:53 -08003750 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003751 my ($dir, $file) = split_path($m->{file_b});
3752 my $pbat = $self->ensure_path($dir);
3753 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
3754 $pbat,$self->{r},$self->{pool});
Eric Wong44320b92007-01-13 22:35:53 -08003755 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003756 $self->chg_file($fbat, $m);
3757 $self->close_file($fbat,undef,$self->{pool});
3758}
3759
3760sub T { shift->M(@_) }
3761
3762sub change_file_prop {
3763 my ($self, $fbat, $pname, $pval) = @_;
3764 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
3765}
3766
Florian Weimer214a34d2008-08-31 17:45:04 +02003767sub _chg_file_get_blob ($$$$) {
3768 my ($self, $fbat, $m, $which) = @_;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003769 my $fh = $::_repository->temp_acquire("git_blob_$which");
Florian Weimer214a34d2008-08-31 17:45:04 +02003770 if ($m->{"mode_$which"} =~ /^120/) {
3771 print $fh 'link ' or croak $!;
3772 $self->change_file_prop($fbat,'svn:special','*');
3773 } elsif ($m->{mode_a} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
3774 $self->change_file_prop($fbat,'svn:special',undef);
3775 }
3776 my $blob = $m->{"sha1_$which"};
3777 return ($fh,) if ($blob =~ /^0{40}$/);
3778 my $size = $::_repository->cat_blob($blob, $fh);
3779 croak "Failed to read object $blob" if ($size < 0);
3780 $fh->flush == 0 or croak $!;
3781 seek $fh, 0, 0 or croak $!;
3782
3783 my $exp = ::md5sum($fh);
3784 seek $fh, 0, 0 or croak $!;
3785 return ($fh, $exp);
3786}
3787
Eric Wonga5e0ced2006-06-12 15:23:48 -07003788sub chg_file {
3789 my ($self, $fbat, $m) = @_;
3790 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
3791 $self->change_file_prop($fbat,'svn:executable','*');
3792 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
3793 $self->change_file_prop($fbat,'svn:executable',undef);
3794 }
Florian Weimer8598db932008-08-31 17:47:09 +02003795 my ($fh_a, $exp_a) = _chg_file_get_blob $self, $fbat, $m, 'a';
3796 my ($fh_b, $exp_b) = _chg_file_get_blob $self, $fbat, $m, 'b';
Eric Wongf7197df2006-10-14 15:48:35 -07003797 my $pool = SVN::Pool->new;
Florian Weimer8598db932008-08-31 17:47:09 +02003798 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
3799 if (-s $fh_a) {
3800 my $txstream = SVN::TxDelta::new ($fh_a, $fh_b, $pool);
Eric Wong991255c2008-08-31 19:45:07 -07003801 my $res = SVN::TxDelta::send_txstream($txstream, @$atd, $pool);
3802 if (defined $res) {
3803 die "Unexpected result from send_txstream: $res\n",
3804 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
3805 }
Florian Weimer8598db932008-08-31 17:47:09 +02003806 } else {
3807 my $got = SVN::TxDelta::send_stream($fh_b, @$atd, $pool);
3808 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
3809 if ($got ne $exp_b);
3810 }
3811 Git::temp_release($fh_b, 1);
3812 Git::temp_release($fh_a, 1);
Eric Wongf7197df2006-10-14 15:48:35 -07003813 $pool->clear;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003814}
3815
3816sub D {
Eric Wong44320b92007-01-13 22:35:53 -08003817 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003818 my ($dir, $file) = split_path($m->{file_b});
3819 my $pbat = $self->ensure_path($dir);
Eric Wong44320b92007-01-13 22:35:53 -08003820 print "\tD\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003821 $self->delete_entry($m->{file_b}, $pbat);
3822}
3823
3824sub close_edit {
3825 my ($self) = @_;
3826 my ($p,$bat) = ($self->{pool}, $self->{bat});
3827 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
Eric Wong64427542007-05-19 02:58:37 -07003828 next if $_ eq '';
Eric Wonga5e0ced2006-06-12 15:23:48 -07003829 $self->close_directory($bat->{$_}, $p);
3830 }
Eric Wong64427542007-05-19 02:58:37 -07003831 $self->close_directory($bat->{''}, $p);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003832 $self->SUPER::close_edit($p);
3833 $p->clear;
3834}
3835
3836sub abort_edit {
3837 my ($self) = @_;
3838 $self->SUPER::abort_edit($self->{pool});
Eric Wong61395352007-01-27 14:33:08 -08003839}
3840
3841sub DESTROY {
3842 my $self = shift;
3843 $self->SUPER::DESTROY(@_);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003844 $self->{pool}->clear;
3845}
3846
Eric Wong44320b92007-01-13 22:35:53 -08003847# this drives the editor
3848sub apply_diff {
Eric Wong61395352007-01-27 14:33:08 -08003849 my ($self) = @_;
3850 my $mods = $self->{mods};
Eric Wong44320b92007-01-13 22:35:53 -08003851 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
Eric Wong6e8548c2007-01-27 01:32:00 -08003852 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
Eric Wong44320b92007-01-13 22:35:53 -08003853 my $f = $m->{chg};
3854 if (defined $o{$f}) {
3855 $self->$f($m);
3856 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +02003857 fatal("Invalid change type: $f");
Eric Wong44320b92007-01-13 22:35:53 -08003858 }
3859 }
Eric Wong24e22aa2007-01-29 00:07:49 -08003860 $self->rmdirs if $_rmdir;
Eric Wong6e8548c2007-01-27 01:32:00 -08003861 if (@$mods == 0) {
Eric Wong44320b92007-01-13 22:35:53 -08003862 $self->abort_edit;
3863 } else {
3864 $self->close_edit;
3865 }
Eric Wong6e8548c2007-01-27 01:32:00 -08003866 return scalar @$mods;
Eric Wong44320b92007-01-13 22:35:53 -08003867}
3868
Eric Wongd81bf822007-01-10 01:22:38 -08003869package Git::SVN::Ra;
Eric Wong6af1db42007-02-14 16:04:10 -08003870use vars qw/@ISA $config_dir $_log_window_size/;
Eric Wongd81bf822007-01-10 01:22:38 -08003871use strict;
3872use warnings;
Eric Wonga51cdb02007-09-07 04:00:40 -07003873my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
Eric Wongd81bf822007-01-10 01:22:38 -08003874
3875BEGIN {
3876 # enforce temporary pool usage for some simple functions
Sam Vilainc5f71ad2007-06-15 15:43:59 +12003877 no strict 'refs';
3878 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root/) {
3879 my $SUPER = "SUPER::$f";
3880 *$f = sub {
3881 my $self = shift;
3882 my $pool = SVN::Pool->new;
3883 my @ret = $self->$SUPER(@_,$pool);
3884 $pool->clear;
3885 wantarray ? @ret : $ret[0];
3886 };
Eric Wongd81bf822007-01-10 01:22:38 -08003887 }
Eric Wongd81bf822007-01-10 01:22:38 -08003888}
3889
Steven Walter9ff74e92007-09-28 13:24:19 -04003890sub _auth_providers () {
3891 [
3892 SVN::Client::get_simple_provider(),
3893 SVN::Client::get_ssl_server_trust_file_provider(),
3894 SVN::Client::get_simple_prompt_provider(
3895 \&Git::SVN::Prompt::simple, 2),
3896 SVN::Client::get_ssl_client_cert_file_provider(),
3897 SVN::Client::get_ssl_client_cert_prompt_provider(
3898 \&Git::SVN::Prompt::ssl_client_cert, 2),
Sebastian Noack77266e92008-02-25 15:56:28 +01003899 SVN::Client::get_ssl_client_cert_pw_file_provider(),
Steven Walter9ff74e92007-09-28 13:24:19 -04003900 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
3901 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
3902 SVN::Client::get_username_provider(),
3903 SVN::Client::get_ssl_server_trust_prompt_provider(
3904 \&Git::SVN::Prompt::ssl_server_trust),
3905 SVN::Client::get_username_prompt_provider(
3906 \&Git::SVN::Prompt::username, 2)
3907 ]
3908}
3909
Eric Wongcfbe7ab2007-11-11 23:37:42 -08003910sub escape_uri_only {
3911 my ($uri) = @_;
3912 my @tmp;
3913 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -07003914 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08003915 push @tmp, $_;
3916 }
3917 join('/', @tmp);
3918}
3919
3920sub escape_url {
3921 my ($url) = @_;
3922 if ($url =~ m#^(https?)://([^/]+)(.*)$#) {
3923 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
3924 $url = "$scheme://$domain$uri";
3925 }
3926 $url;
3927}
3928
Eric Wongd81bf822007-01-10 01:22:38 -08003929sub new {
3930 my ($class, $url) = @_;
Eric Wongf6f09872007-01-18 18:22:18 -08003931 $url =~ s!/+$!!;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08003932 return $RA if ($RA && $RA->{url} eq $url);
Eric Wongf6f09872007-01-18 18:22:18 -08003933
Eric Wongd81bf822007-01-10 01:22:38 -08003934 SVN::_Core::svn_config_ensure($config_dir, undef);
Steven Walter9ff74e92007-09-28 13:24:19 -04003935 my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
Eric Wongd81bf822007-01-10 01:22:38 -08003936 my $config = SVN::Core::config_get_config($config_dir);
Eric Wong7730fbe2007-07-04 14:07:42 -07003937 $RA = undef;
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04003938 my $dont_store_passwords = 1;
3939 my $conf_t = ${$config}{'config'};
3940 {
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003941 no warnings 'once';
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04003942 # The usage of $SVN::_Core::SVN_CONFIG_* variables
3943 # produces warnings that variables are used only once.
3944 # I had not found the better way to shut them up, so
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003945 # the warnings of type 'once' are disabled in this block.
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04003946 if (SVN::_Core::svn_config_get_bool($conf_t,
3947 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
3948 $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
3949 1) == 0) {
3950 SVN::_Core::svn_auth_set_parameter($baton,
3951 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
3952 bless (\$dont_store_passwords, "_p_void"));
3953 }
3954 if (SVN::_Core::svn_config_get_bool($conf_t,
3955 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
3956 $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
3957 1) == 0) {
3958 $Git::SVN::Prompt::_no_auth_cache = 1;
3959 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003960 } # no warnings 'once'
Eric Wongcfbe7ab2007-11-11 23:37:42 -08003961 my $self = SVN::Ra->new(url => escape_url($url), auth => $baton,
Eric Wongd81bf822007-01-10 01:22:38 -08003962 config => $config,
3963 pool => SVN::Pool->new,
3964 auth_provider_callbacks => $callbacks);
Eric Wongcfbe7ab2007-11-11 23:37:42 -08003965 $self->{url} = $url;
Eric Wongd81bf822007-01-10 01:22:38 -08003966 $self->{svn_path} = $url;
3967 $self->{repos_root} = $self->get_repos_root;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003968 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
Eric Wong0dc03d62007-05-13 01:04:43 -07003969 $self->{cache} = { check_path => { r => 0, data => {} },
3970 get_dir => { r => 0, data => {} } };
Eric Wong5d3b7cd2007-01-29 19:16:01 -08003971 $RA = bless $self, $class;
Eric Wongd81bf822007-01-10 01:22:38 -08003972}
3973
Eric Wong0dc03d62007-05-13 01:04:43 -07003974sub check_path {
3975 my ($self, $path, $r) = @_;
3976 my $cache = $self->{cache}->{check_path};
3977 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
3978 return $cache->{data}->{$path};
3979 }
3980 my $pool = SVN::Pool->new;
3981 my $t = $self->SUPER::check_path($path, $r, $pool);
3982 $pool->clear;
3983 if ($r != $cache->{r}) {
3984 %{$cache->{data}} = ();
3985 $cache->{r} = $r;
3986 }
3987 $cache->{data}->{$path} = $t;
3988}
3989
3990sub get_dir {
3991 my ($self, $dir, $r) = @_;
3992 my $cache = $self->{cache}->{get_dir};
3993 if ($r == $cache->{r}) {
3994 if (my $x = $cache->{data}->{$dir}) {
3995 return wantarray ? @$x : $x->[0];
3996 }
3997 }
3998 my $pool = SVN::Pool->new;
3999 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
4000 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
4001 $pool->clear;
4002 if ($r != $cache->{r}) {
4003 %{$cache->{data}} = ();
4004 $cache->{r} = $r;
4005 }
4006 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
4007 wantarray ? (\%dirents, $r, $props) : \%dirents;
4008}
4009
Eric Wongd81bf822007-01-10 01:22:38 -08004010sub DESTROY {
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004011 # do not call the real DESTROY since we store ourselves in $RA
Eric Wongd81bf822007-01-10 01:22:38 -08004012}
4013
Eric Wongd81bf822007-01-10 01:22:38 -08004014sub get_log {
4015 my ($self, @args) = @_;
4016 my $pool = SVN::Pool->new;
Eric Wongd81bf822007-01-10 01:22:38 -08004017 splice(@args, 3, 1) if ($SVN::Core::VERSION le '1.2.0');
4018 my $ret = $self->SUPER::get_log(@args, $pool);
4019 $pool->clear;
4020 $ret;
4021}
4022
Steven Walter9ff74e92007-09-28 13:24:19 -04004023sub trees_match {
4024 my ($self, $url1, $rev1, $url2, $rev2) = @_;
4025 my $ctx = SVN::Client->new(auth => _auth_providers);
4026 my $out = IO::File->new_tmpfile;
4027
4028 # older SVN (1.1.x) doesn't take $pool as the last parameter for
4029 # $ctx->diff(), so we'll create a default one
4030 my $pool = SVN::Pool->new_default_sub;
4031
4032 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
4033 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
4034 $out->flush;
4035 my $ret = (($out->stat)[7] == 0);
4036 close $out or croak $!;
4037
4038 $ret;
4039}
4040
Eric Wongd81bf822007-01-10 01:22:38 -08004041sub get_commit_editor {
Eric Wong44320b92007-01-13 22:35:53 -08004042 my ($self, $log, $cb, $pool) = @_;
Eric Wongd81bf822007-01-10 01:22:38 -08004043 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
Eric Wong44320b92007-01-13 22:35:53 -08004044 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004045}
4046
Eric Wongd81bf822007-01-10 01:22:38 -08004047sub gs_do_update {
Eric Wong8a603772007-01-31 02:45:50 -08004048 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
4049 my $new = ($rev_a == $rev_b);
4050 my $path = $gs->{path};
4051
Eric Wong2e5e2482007-02-23 02:21:59 -08004052 if ($new && -e $gs->{index}) {
4053 unlink $gs->{index} or die
4054 "Couldn't unlink index: $gs->{index}: $!\n";
4055 }
Eric Wongd81bf822007-01-10 01:22:38 -08004056 my $pool = SVN::Pool->new;
Eric Wong8b8fc062007-01-22 11:44:57 -08004057 $editor->set_path_strip($path);
Eric Wong2b27f6c2007-01-28 04:59:05 -08004058 my (@pc) = split m#/#, $path;
4059 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
Eric Wong8a603772007-01-31 02:45:50 -08004060 1, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004061 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong2b27f6c2007-01-28 04:59:05 -08004062
4063 # Since we can't rely on svn_ra_reparent being available, we'll
4064 # just have to do some magic with set_path to make it so
4065 # we only want a partial path.
4066 my $sp = '';
4067 my $final = join('/', @pc);
4068 while (@pc) {
4069 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
4070 $sp .= '/' if length $sp;
4071 $sp .= shift @pc;
4072 }
4073 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
4074
Eric Wong2b27f6c2007-01-28 04:59:05 -08004075 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
4076
Eric Wongd81bf822007-01-10 01:22:38 -08004077 $reporter->finish_report($pool);
4078 $pool->clear;
4079 $editor->{git_commit_ok};
4080}
4081
Eric Wong2b27f6c2007-01-28 04:59:05 -08004082# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
4083# svn_ra_reparent didn't work before 1.4)
Eric Wongd81bf822007-01-10 01:22:38 -08004084sub gs_do_switch {
Eric Wong8a603772007-01-31 02:45:50 -08004085 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
4086 my $path = $gs->{path};
Eric Wongd81bf822007-01-10 01:22:38 -08004087 my $pool = SVN::Pool->new;
Eric Wong2b27f6c2007-01-28 04:59:05 -08004088
4089 my $full_url = $self->{url};
4090 my $old_url = $full_url;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004091 $full_url .= '/' . escape_uri_only($path) if length $path;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004092 my ($ra, $reparented);
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004093
4094 if ($old_url =~ m#^svn(\+ssh)?://#) {
4095 $_[0] = undef;
4096 $self = undef;
4097 $RA = undef;
4098 $ra = Git::SVN::Ra->new($full_url);
4099 $ra_invalid = 1;
4100 } elsif ($old_url ne $full_url) {
4101 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, $pool);
4102 $self->{url} = $full_url;
4103 $reparented = 1;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004104 }
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004105
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004106 $ra ||= $self;
Eric Wongf4392df2008-09-06 20:18:18 -07004107 $url_b = escape_url($url_b);
Eric Wong8a603772007-01-31 02:45:50 -08004108 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004109 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong8b8fc062007-01-22 11:44:57 -08004110 $reporter->set_path('', $rev_a, 0, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004111 $reporter->finish_report($pool);
Eric Wong2b27f6c2007-01-28 04:59:05 -08004112
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004113 if ($reparented) {
4114 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
4115 $self->{url} = $old_url;
4116 }
Eric Wong2b27f6c2007-01-28 04:59:05 -08004117
Eric Wongd81bf822007-01-10 01:22:38 -08004118 $pool->clear;
4119 $editor->{git_commit_ok};
4120}
4121
Eric Wongb54a9012007-06-13 02:37:03 -07004122sub longest_common_path {
4123 my ($gsv, $globs) = @_;
Eric Wongd2ae1432007-02-07 11:50:16 -08004124 my %common;
Eric Wonge5181922007-02-08 12:53:57 -08004125 my $common_max = scalar @$gsv;
4126
4127 foreach my $gs (@$gsv) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004128 my @tmp = split m#/#, $gs->{path};
4129 my $p = '';
4130 foreach (@tmp) {
4131 $p .= length($p) ? "/$_" : $_;
4132 $common{$p} ||= 0;
4133 $common{$p}++;
4134 }
4135 }
Eric Wonge5181922007-02-08 12:53:57 -08004136 $globs ||= [];
4137 $common_max += scalar @$globs;
4138 foreach my $glob (@$globs) {
4139 my @tmp = split m#/#, $glob->{path}->{left};
4140 my $p = '';
4141 foreach (@tmp) {
4142 $p .= length($p) ? "/$_" : $_;
4143 $common{$p} ||= 0;
4144 $common{$p}++;
4145 }
4146 }
4147
Eric Wongd2ae1432007-02-07 11:50:16 -08004148 my $longest_path = '';
4149 foreach (sort {length $b <=> length $a} keys %common) {
Eric Wonge5181922007-02-08 12:53:57 -08004150 if ($common{$_} == $common_max) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004151 $longest_path = $_;
4152 last;
4153 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004154 }
Eric Wongb54a9012007-06-13 02:37:03 -07004155 $longest_path;
4156}
4157
4158sub gs_fetch_loop_common {
4159 my ($self, $base, $head, $gsv, $globs) = @_;
4160 return if ($base > $head);
4161 my $inc = $_log_window_size;
4162 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
4163 my $longest_path = longest_common_path($gsv, $globs);
Eric Wonga51cdb02007-09-07 04:00:40 -07004164 my $ra_url = $self->{url};
Eric Wong0af9c9f2007-01-27 22:28:56 -08004165 while (1) {
Eric Wongd4eff2b2007-01-30 14:04:22 -08004166 my %revs;
Eric Wongd2ae1432007-02-07 11:50:16 -08004167 my $err;
Eric Wongf7c3fc42007-01-29 18:34:55 -08004168 my $err_handler = $SVN::Error::handler;
Eric Wongd2ae1432007-02-07 11:50:16 -08004169 $SVN::Error::handler = sub {
4170 ($err) = @_;
4171 skip_unknown_revs($err);
4172 };
4173 sub _cb {
4174 my ($paths, $r, $author, $date, $log) = @_;
4175 [ dup_changed_paths($paths),
4176 { author => $author, date => $date, log => $log } ];
4177 }
4178 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
4179 sub { $revs{$_[1]} = _cb(@_) });
4180 if ($err && $max >= $head) {
4181 print STDERR "Path '$longest_path' ",
4182 "was probably deleted:\n",
4183 $err->expanded_message,
4184 "\nWill attempt to follow ",
4185 "revisions r$min .. r$max ",
4186 "committed before the deletion\n";
4187 my $hi = $max;
4188 while (--$hi >= $min) {
4189 my $ok;
4190 $self->get_log([$longest_path], $min, $hi,
4191 0, 1, 1, sub {
4192 $ok ||= $_[1];
4193 $revs{$_[1]} = _cb(@_) });
4194 if ($ok) {
4195 print STDERR "r$min .. r$ok OK\n";
4196 last;
4197 }
4198 }
4199 }
Eric Wongd4eff2b2007-01-30 14:04:22 -08004200 $SVN::Error::handler = $err_handler;
Eric Wongfbcc1732007-02-06 18:35:30 -08004201
Eric Wonge5181922007-02-08 12:53:57 -08004202 my %exists = map { $_->{path} => $_ } @$gsv;
Eric Wongd4eff2b2007-01-30 14:04:22 -08004203 foreach my $r (sort {$a <=> $b} keys %revs) {
Eric Wongfbcc1732007-02-06 18:35:30 -08004204 my ($paths, $logged) = @{$revs{$r}};
Eric Wonge5181922007-02-08 12:53:57 -08004205
4206 foreach my $gs ($self->match_globs(\%exists, $paths,
4207 $globs, $r)) {
Eric Wong060610c2007-12-08 23:27:41 -08004208 if ($gs->rev_map_max >= $r) {
Eric Wongfbcc1732007-02-06 18:35:30 -08004209 next;
4210 }
4211 next unless $gs->match_paths($paths, $r);
4212 $gs->{logged_rev_props} = $logged;
Eric Wonge8d120b2007-02-14 16:29:52 -08004213 if (my $last_commit = $gs->last_commit) {
4214 $gs->assert_index_clean($last_commit);
4215 }
Eric Wongfbcc1732007-02-06 18:35:30 -08004216 my $log_entry = $gs->do_fetch($paths, $r);
4217 if ($log_entry) {
Eric Wong0af9c9f2007-01-27 22:28:56 -08004218 $gs->do_git_commit($log_entry);
4219 }
Eric Wong321b1842008-01-02 10:10:03 -08004220 $INDEX_FILES{$gs->{index}} = 1;
Eric Wong0af9c9f2007-01-27 22:28:56 -08004221 }
Eric Wonge5181922007-02-08 12:53:57 -08004222 foreach my $g (@$globs) {
Eric Wong93f26892007-02-11 01:20:26 -08004223 my $k = "svn-remote.$g->{remote}." .
4224 "$g->{t}-maxRev";
4225 Git::SVN::tmp_config($k, $r);
Eric Wonge5181922007-02-08 12:53:57 -08004226 }
Eric Wonga51cdb02007-09-07 04:00:40 -07004227 if ($ra_invalid) {
4228 $_[0] = undef;
4229 $self = undef;
4230 $RA = undef;
4231 $self = Git::SVN::Ra->new($ra_url);
4232 $ra_invalid = undef;
4233 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004234 }
Eric Wong9c93fee2007-01-31 17:22:31 -08004235 # pre-fill the .rev_db since it'll eventually get filled in
4236 # with '0' x40 if something new gets committed
Eric Wonge5181922007-02-08 12:53:57 -08004237 foreach my $gs (@$gsv) {
Eric Wong66ab84b2007-12-08 23:27:42 -08004238 next if $gs->rev_map_max >= $max;
4239 next if defined $gs->rev_map_get($max);
4240 $gs->rev_map_set($max, 0 x40);
Eric Wong9c93fee2007-01-31 17:22:31 -08004241 }
Eric Wongc3560e52007-02-12 16:03:32 -08004242 foreach my $g (@$globs) {
4243 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
4244 Git::SVN::tmp_config($k, $max);
4245 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004246 last if $max >= $head;
4247 $min = $max + 1;
4248 $max += $inc;
4249 $max = $head if ($max > $head);
4250 }
Karl Hasselström94bc9142008-02-03 17:56:18 +01004251 Git::SVN::gc();
Eric Wong0af9c9f2007-01-27 22:28:56 -08004252}
4253
Marcus Griep570d35c2008-08-08 01:41:57 -07004254sub get_dir_globbed {
4255 my ($self, $left, $depth, $r) = @_;
4256
4257 my @x = eval { $self->get_dir($left, $r) };
4258 return unless scalar @x == 3;
4259 my $dirents = $x[0];
4260 my @finalents;
4261 foreach my $de (keys %$dirents) {
4262 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
4263 if ($depth > 1) {
4264 my @args = ("$left/$de", $depth - 1, $r);
4265 foreach my $dir ($self->get_dir_globbed(@args)) {
4266 push @finalents, "$de/$dir";
4267 }
4268 } else {
4269 push @finalents, $de;
4270 }
4271 }
4272 @finalents;
4273}
4274
Eric Wonge5181922007-02-08 12:53:57 -08004275sub match_globs {
4276 my ($self, $exists, $paths, $globs, $r) = @_;
Eric Wong74a81222007-02-10 13:28:50 -08004277
4278 sub get_dir_check {
4279 my ($self, $exists, $g, $r) = @_;
Marcus Griep570d35c2008-08-08 01:41:57 -07004280
4281 my @dirs = $self->get_dir_globbed($g->{path}->{left},
4282 $g->{path}->{depth},
4283 $r);
4284
4285 foreach my $de (@dirs) {
Eric Wong74a81222007-02-10 13:28:50 -08004286 my $p = $g->{path}->full_path($de);
4287 next if $exists->{$p};
4288 next if (length $g->{path}->{right} &&
4289 ($self->check_path($p, $r) !=
4290 $SVN::Node::dir));
4291 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
4292 $g->{ref}->full_path($de), 1);
4293 }
4294 }
Eric Wonge5181922007-02-08 12:53:57 -08004295 foreach my $g (@$globs) {
Eric Wong74a81222007-02-10 13:28:50 -08004296 if (my $path = $paths->{"/$g->{path}->{left}"}) {
4297 if ($path->{action} =~ /^[AR]$/) {
4298 get_dir_check($self, $exists, $g, $r);
4299 }
4300 }
Eric Wonge5181922007-02-08 12:53:57 -08004301 foreach (keys %$paths) {
Eric Wong28710f72007-02-14 13:32:21 -08004302 if (/$g->{path}->{left_regex}/ &&
4303 !/$g->{path}->{regex}/) {
Eric Wong74a81222007-02-10 13:28:50 -08004304 next if $paths->{$_}->{action} !~ /^[AR]$/;
4305 get_dir_check($self, $exists, $g, $r);
4306 }
Eric Wonge5181922007-02-08 12:53:57 -08004307 next unless /$g->{path}->{regex}/;
4308 my $p = $1;
4309 my $pathname = $g->{path}->full_path($p);
4310 next if $exists->{$pathname};
Eric Wong0c1ec5a2007-04-18 00:17:33 -07004311 next if ($self->check_path($pathname, $r) !=
4312 $SVN::Node::dir);
Eric Wonge5181922007-02-08 12:53:57 -08004313 $exists->{$pathname} = Git::SVN->init(
4314 $self->{url}, $pathname, undef,
4315 $g->{ref}->full_path($p), 1);
4316 }
4317 my $c = '';
4318 foreach (split m#/#, $g->{path}->{left}) {
4319 $c .= "/$_";
4320 next unless ($paths->{$c} &&
Eric Wong74a81222007-02-10 13:28:50 -08004321 ($paths->{$c}->{action} =~ /^[AR]$/));
4322 get_dir_check($self, $exists, $g, $r);
Eric Wonge5181922007-02-08 12:53:57 -08004323 }
4324 }
4325 values %$exists;
4326}
4327
Eric Wonge6434f82007-01-23 16:29:23 -08004328sub minimize_url {
4329 my ($self) = @_;
4330 return $self->{url} if ($self->{url} eq $self->{repos_root});
4331 my $url = $self->{repos_root};
4332 my @components = split(m!/!, $self->{svn_path});
4333 my $c = '';
4334 do {
4335 $url .= "/$c" if length $c;
4336 eval { (ref $self)->new($url)->get_latest_revnum };
4337 } while ($@ && ($c = shift @components));
4338 $url;
4339}
4340
Eric Wongd81bf822007-01-10 01:22:38 -08004341sub can_do_switch {
4342 my $self = shift;
4343 unless (defined $can_do_switch) {
4344 my $pool = SVN::Pool->new;
4345 my $rep = eval {
4346 $self->do_switch(1, '', 0, $self->{url},
4347 SVN::Delta::Editor->new, $pool);
4348 };
4349 if ($@) {
4350 $can_do_switch = 0;
4351 } else {
4352 $rep->abort_report($pool);
4353 $can_do_switch = 1;
4354 }
4355 $pool->clear;
4356 }
4357 $can_do_switch;
4358}
4359
Eric Wong0af9c9f2007-01-27 22:28:56 -08004360sub skip_unknown_revs {
4361 my ($err) = @_;
4362 my $errno = $err->apr_err();
4363 # Maybe the branch we're tracking didn't
4364 # exist when the repo started, so it's
4365 # not an error if it doesn't, just continue
4366 #
4367 # Wonderfully consistent library, eh?
4368 # 160013 - svn:// and file://
4369 # 175002 - http(s)://
4370 # 175007 - http(s):// (this repo required authorization, too...)
4371 # More codes may be discovered later...
4372 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
Eric Wonga6a15a92007-03-30 17:54:48 -07004373 my $err_key = $err->expanded_message;
4374 # revision numbers change every time, filter them out
4375 $err_key =~ s/\d+/\0/g;
4376 $err_key = "$errno\0$err_key";
4377 unless ($ignored_err{$err_key}) {
4378 warn "W: Ignoring error from SVN, path probably ",
4379 "does not exist: ($errno): ",
4380 $err->expanded_message,"\n";
Eric Wongeee8a172008-01-07 02:40:40 -08004381 warn "W: Do not be alarmed at the above message ",
4382 "git-svn is just searching aggressively for ",
4383 "old history.\n",
4384 "This may take a while on large repositories\n";
Eric Wonga6a15a92007-03-30 17:54:48 -07004385 $ignored_err{$err_key} = 1;
4386 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004387 return;
4388 }
4389 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
4390}
4391
4392# svn_log_changed_path_t objects passed to get_log are likely to be
4393# overwritten even if only the refs are copied to an external variable,
4394# so we should dup the structures in their entirety. Using an externally
4395# passed pool (instead of our temporary and quickly cleared pool in
4396# Git::SVN::Ra) does not help matters at all...
4397sub dup_changed_paths {
4398 my ($paths) = @_;
4399 return undef unless $paths;
4400 my %ret;
4401 foreach my $p (keys %$paths) {
4402 my $i = $paths->{$p};
4403 my %s = map { $_ => $i->$_ }
4404 qw/copyfrom_path copyfrom_rev action/;
4405 $ret{$p} = \%s;
4406 }
4407 \%ret;
4408}
4409
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004410package Git::SVN::Log;
4411use strict;
4412use warnings;
4413use POSIX qw/strftime/;
David D Kilzer111947e2007-11-11 22:56:52 -08004414use constant commit_log_separator => ('-' x 72) . "\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004415use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
4416 %rusers $show_commit $incremental/;
4417my $l_fmt;
4418
4419sub cmt_showable {
4420 my ($c) = @_;
4421 return 1 if defined $c->{r};
Eric Wongc16d0872007-04-08 00:59:22 -07004422
4423 # big commit message got truncated by the 16k pretty buffer in rev-list
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004424 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
4425 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
Eric Wongc16d0872007-04-08 00:59:22 -07004426 @{$c->{l}} = ();
Eric Wong44320b92007-01-13 22:35:53 -08004427 my @log = command(qw/cat-file commit/, $c->{c});
Eric Wongc16d0872007-04-08 00:59:22 -07004428
4429 # shift off the headers
4430 shift @log while ($log[0] ne '');
Eric Wong44320b92007-01-13 22:35:53 -08004431 shift @log;
Eric Wongc16d0872007-04-08 00:59:22 -07004432
4433 # TODO: make $c->{l} not have a trailing newline in the future
4434 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004435
4436 (undef, $c->{r}, undef) = ::extract_metadata(
Eric Wong44320b92007-01-13 22:35:53 -08004437 (grep(/^git-svn-id: /, @log))[-1]);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004438 }
4439 return defined $c->{r};
4440}
4441
4442sub log_use_color {
Jeff Kingcd459e32007-12-11 01:28:42 -05004443 return $color || Git->repository->get_colorbool('color.diff');
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004444}
4445
4446sub git_svn_log_cmd {
Eric Wong3bc718b2007-02-13 17:09:40 -08004447 my ($r_min, $r_max, @args) = @_;
4448 my $head = 'HEAD';
Eric Wong6ed77262007-08-15 09:55:18 -07004449 my (@files, @log_opts);
Eric Wong3bc718b2007-02-13 17:09:40 -08004450 foreach my $x (@args) {
Eric Wong6ed77262007-08-15 09:55:18 -07004451 if ($x eq '--' || @files) {
4452 push @files, $x;
4453 } else {
4454 if (::verify_ref("$x^0")) {
4455 $head = $x;
4456 } else {
4457 push @log_opts, $x;
4458 }
4459 }
Eric Wong3bc718b2007-02-13 17:09:40 -08004460 }
4461
Eric Wong13c823f2007-04-08 00:59:19 -07004462 my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
4463 $gs ||= Git::SVN->_new;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004464 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
4465 $gs->refname);
4466 push @cmd, '-r' unless $non_recursive;
4467 push @cmd, qw/--raw --name-status/ if $verbose;
4468 push @cmd, '--color' if log_use_color();
Eric Wong6ed77262007-08-15 09:55:18 -07004469 push @cmd, @log_opts;
4470 if (defined $r_max && $r_max == $r_min) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004471 push @cmd, '--max-count=1';
Eric Wong060610c2007-12-08 23:27:41 -08004472 if (my $c = $gs->rev_map_get($r_max)) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004473 push @cmd, $c;
4474 }
Eric Wong6ed77262007-08-15 09:55:18 -07004475 } elsif (defined $r_max) {
David D Kilzer111947e2007-11-11 22:56:52 -08004476 if ($r_max < $r_min) {
4477 ($r_min, $r_max) = ($r_max, $r_min);
4478 }
4479 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
4480 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
4481 # If there are no commits in the range, both $c_max and $c_min
4482 # will be undefined. If there is at least 1 commit in the
4483 # range, both will be defined.
4484 return () if !defined $c_min || !defined $c_max;
4485 if ($c_min eq $c_max) {
4486 push @cmd, '--max-count=1', $c_min;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004487 } else {
David D Kilzer111947e2007-11-11 22:56:52 -08004488 push @cmd, '--boundary', "$c_min..$c_max";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004489 }
4490 }
Eric Wong6ed77262007-08-15 09:55:18 -07004491 return (@cmd, @files);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004492}
4493
4494# adapted from pager.c
4495sub config_pager {
4496 $pager ||= $ENV{GIT_PAGER} || $ENV{PAGER};
4497 if (!defined $pager) {
4498 $pager = 'less';
4499 } elsif (length $pager == 0 || $pager eq 'cat') {
4500 $pager = undef;
4501 }
Jeff Kingcd459e32007-12-11 01:28:42 -05004502 $ENV{GIT_PAGER_IN_USE} = defined($pager);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004503}
4504
4505sub run_pager {
Eric Wongb0193042007-09-21 18:48:45 -07004506 return unless -t *STDOUT && defined $pager;
Marcus Griep971e6282008-09-10 11:09:46 -04004507 pipe my ($rfd, $wfd) or return;
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004508 defined(my $pid = fork) or ::fatal "Can't fork: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004509 if (!$pid) {
4510 open STDOUT, '>&', $wfd or
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004511 ::fatal "Can't redirect to stdout: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004512 return;
4513 }
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004514 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004515 $ENV{LESS} ||= 'FRSX';
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004516 exec $pager or ::fatal "Can't run pager: $! ($pager)";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004517}
4518
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004519sub format_svn_date {
4520 return strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)", localtime(shift));
4521}
4522
4523sub parse_git_date {
4524 my ($t, $tz) = @_;
4525 # Date::Parse isn't in the standard Perl distro :(
4526 if ($tz =~ s/^\+//) {
4527 $t += tz_to_s_offset($tz);
4528 } elsif ($tz =~ s/^\-//) {
4529 $t -= tz_to_s_offset($tz);
4530 }
4531 return $t;
4532}
4533
4534sub set_local_timezone {
4535 if (defined $TZ) {
4536 $ENV{TZ} = $TZ;
4537 } else {
4538 delete $ENV{TZ};
4539 }
4540}
4541
Eric Wong21819a32007-01-27 14:38:10 -08004542sub tz_to_s_offset {
4543 my ($tz) = @_;
4544 $tz =~ s/(\d\d)$//;
4545 return ($1 * 60) + ($tz * 3600);
4546}
4547
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004548sub get_author_info {
4549 my ($dest, $author, $t, $tz) = @_;
4550 $author =~ s/(?:^\s*|\s*$)//g;
4551 $dest->{a_raw} = $author;
4552 my $au;
Eric Wong1c8443b2007-01-14 02:17:00 -08004553 if ($::_authors) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004554 $au = $rusers{$author} || undef;
4555 }
4556 if (!$au) {
4557 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
4558 }
4559 $dest->{t} = $t;
4560 $dest->{tz} = $tz;
4561 $dest->{a} = $au;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004562 $dest->{t_utc} = parse_git_date($t, $tz);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004563}
4564
4565sub process_commit {
4566 my ($c, $r_min, $r_max, $defer) = @_;
4567 if (defined $r_min && defined $r_max) {
4568 if ($r_min == $c->{r} && $r_min == $r_max) {
4569 show_commit($c);
4570 return 0;
4571 }
4572 return 1 if $r_min == $r_max;
4573 if ($r_min < $r_max) {
4574 # we need to reverse the print order
4575 return 0 if (defined $limit && --$limit < 0);
4576 push @$defer, $c;
4577 return 1;
4578 }
4579 if ($r_min != $r_max) {
4580 return 1 if ($r_min < $c->{r});
4581 return 1 if ($r_max > $c->{r});
4582 }
4583 }
4584 return 0 if (defined $limit && --$limit < 0);
4585 show_commit($c);
4586 return 1;
4587}
4588
4589sub show_commit {
4590 my $c = shift;
4591 if ($oneline) {
4592 my $x = "\n";
4593 if (my $l = $c->{l}) {
4594 while ($l->[0] =~ /^\s*$/) { shift @$l }
4595 $x = $l->[0];
4596 }
4597 $l_fmt ||= 'A' . length($c->{r});
4598 print 'r',pack($l_fmt, $c->{r}),' | ';
4599 print "$c->{c} | " if $show_commit;
4600 print $x;
4601 } else {
4602 show_commit_normal($c);
4603 }
4604}
4605
4606sub show_commit_changed_paths {
4607 my ($c) = @_;
4608 return unless $c->{changed};
4609 print "Changed paths:\n", @{$c->{changed}};
4610}
4611
4612sub show_commit_normal {
4613 my ($c) = @_;
David D Kilzer111947e2007-11-11 22:56:52 -08004614 print commit_log_separator, "r$c->{r} | ";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004615 print "$c->{c} | " if $show_commit;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004616 print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004617 my $nr_line = 0;
4618
4619 if (my $l = $c->{l}) {
4620 while ($l->[$#$l] eq "\n" && $#$l > 0
4621 && $l->[($#$l - 1)] eq "\n") {
4622 pop @$l;
4623 }
4624 $nr_line = scalar @$l;
4625 if (!$nr_line) {
4626 print "1 line\n\n\n";
4627 } else {
4628 if ($nr_line == 1) {
4629 $nr_line = '1 line';
4630 } else {
4631 $nr_line .= ' lines';
4632 }
4633 print $nr_line, "\n";
4634 show_commit_changed_paths($c);
4635 print "\n";
4636 print $_ foreach @$l;
4637 }
4638 } else {
4639 print "1 line\n";
4640 show_commit_changed_paths($c);
4641 print "\n";
4642
4643 }
Eric Wong488a63e2007-02-15 00:40:42 -08004644 foreach my $x (qw/raw stat diff/) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004645 if ($c->{$x}) {
4646 print "\n";
4647 print $_ foreach @{$c->{$x}}
4648 }
4649 }
4650}
4651
4652sub cmd_show_log {
4653 my (@args) = @_;
4654 my ($r_min, $r_max);
4655 my $r_last = -1; # prevent dupes
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004656 set_local_timezone();
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004657 if (defined $::_revision) {
4658 if ($::_revision =~ /^(\d+):(\d+)$/) {
4659 ($r_min, $r_max) = ($1, $2);
4660 } elsif ($::_revision =~ /^\d+$/) {
4661 $r_min = $r_max = $::_revision;
4662 } else {
4663 ::fatal "-r$::_revision is not supported, use ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004664 "standard 'git log' arguments instead";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004665 }
4666 }
4667
4668 config_pager();
Eric Wong6ed77262007-08-15 09:55:18 -07004669 @args = git_svn_log_cmd($r_min, $r_max, @args);
David D Kilzer111947e2007-11-11 22:56:52 -08004670 if (!@args) {
4671 print commit_log_separator unless $incremental || $oneline;
4672 return;
4673 }
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004674 my $log = command_output_pipe(@args);
4675 run_pager();
Eric Wong488a63e2007-02-15 00:40:42 -08004676 my (@k, $c, $d, $stat);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004677 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
4678 while (<$log>) {
David D Kilzer60f3ff12007-11-10 22:10:34 -08004679 if (/^${esc_color}commit -?($::sha1_short)/o) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004680 my $cmt = $1;
4681 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
4682 $r_last = $c->{r};
4683 process_commit($c, $r_min, $r_max, \@k) or
4684 goto out;
4685 }
4686 $d = undef;
4687 $c = { c => $cmt };
4688 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
4689 get_author_info($c, $1, $2, $3);
4690 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
4691 # ignore
4692 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
4693 push @{$c->{raw}}, $_;
4694 } elsif (/^${esc_color}[ACRMDT]\t/) {
4695 # we could add $SVN->{svn_path} here, but that requires
4696 # remote access at the moment (repo_path_split)...
4697 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
4698 push @{$c->{changed}}, $_;
4699 } elsif (/^${esc_color}diff /o) {
4700 $d = 1;
4701 push @{$c->{diff}}, $_;
4702 } elsif ($d) {
4703 push @{$c->{diff}}, $_;
Eric Wong488a63e2007-02-15 00:40:42 -08004704 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
4705 $esc_color*[\+\-]*$esc_color$/x) {
4706 $stat = 1;
4707 push @{$c->{stat}}, $_;
4708 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
4709 push @{$c->{stat}}, $_;
4710 $stat = undef;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004711 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
4712 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
4713 } elsif (s/^${esc_color} //o) {
4714 push @{$c->{l}}, $_;
4715 }
4716 }
4717 if ($c && defined $c->{r} && $c->{r} != $r_last) {
4718 $r_last = $c->{r};
4719 process_commit($c, $r_min, $r_max, \@k);
4720 }
4721 if (@k) {
David D Kilzer111947e2007-11-11 22:56:52 -08004722 ($r_min, $r_max) = ($r_max, $r_min);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004723 process_commit($_, $r_min, $r_max) foreach reverse @k;
4724 }
4725out:
Eric Wongc843c462007-01-12 03:07:31 -08004726 close $log;
David D Kilzer111947e2007-11-11 22:56:52 -08004727 print commit_log_separator unless $incremental || $oneline;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004728}
4729
Tim Stoakes6fb53752008-02-10 15:21:08 +10304730sub cmd_blame {
Steven Grimm4be40382008-05-10 22:11:18 -07004731 my $path = pop;
Tim Stoakes6fb53752008-02-10 15:21:08 +10304732
4733 config_pager();
4734 run_pager();
4735
Steven Grimm4be40382008-05-10 22:11:18 -07004736 my ($fh, $ctx, $rev);
4737
4738 if ($_git_format) {
4739 ($fh, $ctx) = command_output_pipe('blame', @_, $path);
4740 while (my $line = <$fh>) {
4741 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
4742 # Uncommitted edits show up as a rev ID of
4743 # all zeros, which we can't look up with
4744 # cmt_metadata
4745 if ($1 !~ /^0+$/) {
4746 (undef, $rev, undef) =
4747 ::cmt_metadata($1);
4748 $rev = '0' if (!$rev);
4749 } else {
4750 $rev = '0';
4751 }
4752 $rev = sprintf('%-10s', $rev);
4753 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
4754 }
4755 print $line;
Tim Stoakes6fb53752008-02-10 15:21:08 +10304756 }
Steven Grimm4be40382008-05-10 22:11:18 -07004757 } else {
4758 ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD',
4759 '--', $path);
4760 my ($sha1);
4761 my %authors;
4762 while (my $line = <$fh>) {
4763 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
4764 $sha1 = $1;
4765 (undef, $rev, undef) = ::cmt_metadata($1);
4766 $rev = '0' if (!$rev);
4767 }
4768 elsif ($line =~ /^author (.*)/) {
4769 $authors{$rev} = $1;
4770 $authors{$rev} =~ s/\s/_/g;
4771 }
4772 elsif ($line =~ /^\t(.*)$/) {
4773 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
4774 }
4775 }
Tim Stoakes6fb53752008-02-10 15:21:08 +10304776 }
4777 command_close_pipe($fh, $ctx);
4778}
4779
Eric Wong706587f2007-01-18 17:50:01 -08004780package Git::SVN::Migration;
4781# these version numbers do NOT correspond to actual version numbers
4782# of git nor git-svn. They are just relative.
4783#
4784# v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
4785#
4786# v1 layout: .git/$id/info/url, refs/remotes/$id
4787#
4788# v2 layout: .git/svn/$id/info/url, refs/remotes/$id
4789#
4790# v3 layout: .git/svn/$id, refs/remotes/$id
4791# - info/url may remain for backwards compatibility
4792# - this is what we migrate up to this layout automatically,
4793# - this will be used by git svn init on single branches
Eric Wong26a62d52007-02-12 13:25:25 -08004794# v3.1 layout (auto migrated):
4795# - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
4796# for backwards compatibility
Eric Wong706587f2007-01-18 17:50:01 -08004797#
4798# v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
4799# - this is only created for newly multi-init-ed
4800# repositories. Similar in spirit to the
4801# --use-separate-remotes option in git-clone (now default)
4802# - we do not automatically migrate to this (following
4803# the example set by core git)
Eric Wong060610c2007-12-08 23:27:41 -08004804#
4805# v5 layout: .rev_db.$UUID => .rev_map.$UUID
4806# - newer, more-efficient format that uses 24-bytes per record
4807# with no filler space.
4808# - use xxd -c24 < .rev_map.$UUID to view and debug
4809# - This is a one-way migration, repositories updated to the
4810# new format will not be able to use old git-svn without
4811# rebuilding the .rev_db. Rebuilding the rev_db is not
4812# possible if noMetadata or useSvmProps are set; but should
4813# be no problem for users that use the (sensible) defaults.
Eric Wong706587f2007-01-18 17:50:01 -08004814use strict;
4815use warnings;
4816use Carp qw/croak/;
4817use File::Path qw/mkpath/;
Eric Wong47e39c52007-01-21 04:27:09 -08004818use File::Basename qw/dirname basename/;
4819use vars qw/$_minimize/;
Eric Wong706587f2007-01-18 17:50:01 -08004820
4821sub migrate_from_v0 {
4822 my $git_dir = $ENV{GIT_DIR};
4823 return undef unless -d $git_dir;
4824 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
4825 my $migrated = 0;
4826 while (<$fh>) {
4827 chomp;
4828 my ($id, $orig_ref) = ($_, $_);
4829 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
4830 next unless -f "$git_dir/$id/info/url";
4831 my $new_ref = "refs/remotes/$id";
4832 if (::verify_ref("$new_ref^0")) {
4833 print STDERR "W: $orig_ref is probably an old ",
4834 "branch used by an ancient version of ",
4835 "git-svn.\n",
4836 "However, $new_ref also exists.\n",
4837 "We will not be able ",
4838 "to use this branch until this ",
4839 "ambiguity is resolved.\n";
4840 next;
4841 }
4842 print STDERR "Migrating from v0 layout...\n" if !$migrated;
4843 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
4844 command_noisy('update-ref', $new_ref, $orig_ref);
4845 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
4846 $migrated++;
4847 }
4848 command_close_pipe($fh, $ctx);
4849 print STDERR "Done migrating from v0 layout...\n" if $migrated;
4850 $migrated;
4851}
4852
4853sub migrate_from_v1 {
4854 my $git_dir = $ENV{GIT_DIR};
4855 my $migrated = 0;
4856 return $migrated unless -d $git_dir;
4857 my $svn_dir = "$git_dir/svn";
4858
4859 # just in case somebody used 'svn' as their $id at some point...
4860 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
4861
4862 print STDERR "Migrating from a git-svn v1 layout...\n";
4863 mkpath([$svn_dir]);
4864 print STDERR "Data from a previous version of git-svn exists, but\n\t",
4865 "$svn_dir\n\t(required for this version ",
Frederik Schwarzer8f510be2008-07-14 18:30:24 +02004866 "($::VERSION) of git-svn) does not exist.\n";
Eric Wong706587f2007-01-18 17:50:01 -08004867 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
4868 while (<$fh>) {
4869 my $x = $_;
4870 next unless $x =~ s#^refs/remotes/##;
4871 chomp $x;
4872 next unless -f "$git_dir/$x/info/url";
4873 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
4874 next unless $u;
4875 my $dn = dirname("$git_dir/svn/$x");
4876 mkpath([$dn]) unless -d $dn;
4877 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
4878 mkpath(["$git_dir/svn/svn"]);
4879 print STDERR " - $git_dir/$x/info => ",
4880 "$git_dir/svn/$x/info\n";
4881 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
4882 croak "$!: $x";
4883 # don't worry too much about these, they probably
4884 # don't exist with repos this old (save for index,
4885 # and we can easily regenerate that)
4886 foreach my $f (qw/unhandled.log index .rev_db/) {
4887 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
4888 }
4889 } else {
4890 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
4891 rename "$git_dir/$x", "$git_dir/svn/$x" or
4892 croak "$!: $x";
4893 }
4894 $migrated++;
4895 }
4896 command_close_pipe($fh, $ctx);
4897 print STDERR "Done migrating from a git-svn v1 layout\n";
4898 $migrated;
4899}
4900
4901sub read_old_urls {
4902 my ($l_map, $pfx, $path) = @_;
4903 my @dir;
4904 foreach (<$path/*>) {
4905 if (-r "$_/info/url") {
4906 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
4907 my $ref_id = $pfx . basename $_;
4908 my $url = ::file_to_s("$_/info/url");
4909 $l_map->{$ref_id} = $url;
4910 } elsif (-d $_) {
4911 push @dir, $_;
4912 }
4913 }
4914 foreach (@dir) {
4915 my $x = $_;
4916 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
4917 read_old_urls($l_map, $x, $_);
4918 }
4919}
4920
4921sub migrate_from_v2 {
4922 my @cfg = command(qw/config -l/);
4923 return if grep /^svn-remote\..+\.url=/, @cfg;
4924 my %l_map;
4925 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
4926 my $migrated = 0;
4927
4928 foreach my $ref_id (sort keys %l_map) {
Eric Wong471bc002007-02-01 04:06:27 -08004929 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
4930 if ($@) {
4931 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
4932 }
Eric Wong706587f2007-01-18 17:50:01 -08004933 $migrated++;
4934 }
4935 $migrated;
4936}
4937
Eric Wong47e39c52007-01-21 04:27:09 -08004938sub minimize_connections {
4939 my $r = Git::SVN::read_all_remotes();
4940 my $new_urls = {};
4941 my $root_repos = {};
4942 foreach my $repo_id (keys %$r) {
4943 my $url = $r->{$repo_id}->{url} or next;
4944 my $fetch = $r->{$repo_id}->{fetch} or next;
4945 my $ra = Git::SVN::Ra->new($url);
4946
4947 # skip existing cases where we already connect to the root
4948 if (($ra->{url} eq $ra->{repos_root}) ||
Eric Wong7829f202008-06-28 20:40:32 -07004949 ($ra->{repos_root} eq $repo_id)) {
Eric Wong47e39c52007-01-21 04:27:09 -08004950 $root_repos->{$ra->{url}} = $repo_id;
4951 next;
4952 }
4953
4954 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
4955 my $root_path = $ra->{url};
Eric Wong4e9f6cc2007-02-09 12:17:57 -08004956 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
Eric Wong47e39c52007-01-21 04:27:09 -08004957 foreach my $path (keys %$fetch) {
4958 my $ref_id = $fetch->{$path};
4959 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
4960
4961 # make sure we can read when connecting to
4962 # a higher level of a repository
4963 my ($last_rev, undef) = $gs->last_rev_commit;
4964 if (!defined $last_rev) {
4965 $last_rev = eval {
4966 $root_ra->get_latest_revnum;
4967 };
4968 next if $@;
4969 }
4970 my $new = $root_path;
4971 $new .= length $path ? "/$path" : '';
4972 eval {
4973 $root_ra->get_log([$new], $last_rev, $last_rev,
4974 0, 0, 1, sub { });
4975 };
4976 next if $@;
4977 $new_urls->{$ra->{repos_root}}->{$new} =
4978 { ref_id => $ref_id,
4979 old_repo_id => $repo_id,
4980 old_path => $path };
4981 }
4982 }
4983
4984 my @emptied;
4985 foreach my $url (keys %$new_urls) {
4986 # see if we can re-use an existing [svn-remote "repo_id"]
4987 # instead of creating a(n ugly) new section:
Eric Wong7829f202008-06-28 20:40:32 -07004988 my $repo_id = $root_repos->{$url} || $url;
Eric Wong47e39c52007-01-21 04:27:09 -08004989
4990 my $fetch = $new_urls->{$url};
4991 foreach my $path (keys %$fetch) {
4992 my $x = $fetch->{$path};
4993 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
4994 my $pfx = "svn-remote.$x->{old_repo_id}";
4995
4996 my $old_fetch = quotemeta("$x->{old_path}:".
4997 "refs/remotes/$x->{ref_id}");
Eric Wong8b8fc062007-01-22 11:44:57 -08004998 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08004999 "$pfx.fetch", '^'. $old_fetch . '$');
5000 delete $r->{$x->{old_repo_id}}->
5001 {fetch}->{$x->{old_path}};
5002 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
Eric Wong8b8fc062007-01-22 11:44:57 -08005003 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08005004 "$pfx.url");
5005 push @emptied, $x->{old_repo_id}
5006 }
5007 }
5008 }
5009 if (@emptied) {
5010 my $file = $ENV{GIT_CONFIG} || $ENV{GIT_CONFIG_LOCAL} ||
5011 "$ENV{GIT_DIR}/config";
5012 print STDERR <<EOF;
5013The following [svn-remote] sections in your config file ($file) are empty
5014and can be safely removed:
5015EOF
5016 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
5017 }
5018}
5019
Eric Wong706587f2007-01-18 17:50:01 -08005020sub migration_check {
5021 migrate_from_v0();
5022 migrate_from_v1();
5023 migrate_from_v2();
Eric Wong47e39c52007-01-21 04:27:09 -08005024 minimize_connections() if $_minimize;
Eric Wong706587f2007-01-18 17:50:01 -08005025}
5026
Eric Wongef3cfaa2007-01-24 03:30:57 -08005027package Git::IndexInfo;
5028use strict;
5029use warnings;
5030use Git qw/command_input_pipe command_close_pipe/;
5031
5032sub new {
5033 my ($class) = @_;
5034 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
5035 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
5036}
5037
5038sub remove {
5039 my ($self, $path) = @_;
5040 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
5041 return ++$self->{nr};
5042 }
5043 undef;
5044}
5045
5046sub update {
5047 my ($self, $mode, $hash, $path) = @_;
5048 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
5049 return ++$self->{nr};
5050 }
5051 undef;
5052}
5053
5054sub DESTROY {
5055 my ($self) = @_;
5056 command_close_pipe($self->{gui}, $self->{ctx});
5057}
5058
Eric Wong4bb9ed02007-02-03 13:29:17 -08005059package Git::SVN::GlobSpec;
5060use strict;
5061use warnings;
5062
5063sub new {
5064 my ($class, $glob) = @_;
Eric Wong4bb9ed02007-02-03 13:29:17 -08005065 my $re = $glob;
5066 $re =~ s!/+$!!g; # no need for trailing slashes
Marcus Griep570d35c2008-08-08 01:41:57 -07005067 $re =~ m!^([^*]*)(\*(?:/\*)*)([^*]*)$!;
5068 my $temp = $re;
5069 my ($left, $right) = ($1, $3);
5070 $re = $2;
5071 my $depth = $re =~ tr/*/*/;
5072 if ($depth != $temp =~ tr/*/*/) {
5073 die "Only one set of wildcard directories " .
5074 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
5075 }
5076 if ($depth == 0) {
Eric Wonge5181922007-02-08 12:53:57 -08005077 die "One '*' is needed for glob: '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08005078 }
Marcus Griep570d35c2008-08-08 01:41:57 -07005079 $re =~ s!\*!\[^/\]*!g;
5080 $re = quotemeta($left) . "($re)" . quotemeta($right);
Eric Wong4e9f6cc2007-02-09 12:17:57 -08005081 if (length $left && !($left =~ s!/+$!!g)) {
5082 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
5083 }
5084 if (length $right && !($right =~ s!^/+!!g)) {
5085 die "Missing leading '/' on right side of: '$glob' ($right)\n";
5086 }
Eric Wong74a81222007-02-10 13:28:50 -08005087 my $left_re = qr/^\/\Q$left\E(\/|$)/;
5088 bless { left => $left, right => $right, left_regex => $left_re,
Marcus Griep570d35c2008-08-08 01:41:57 -07005089 regex => qr/$re/, glob => $glob, depth => $depth }, $class;
Eric Wong4bb9ed02007-02-03 13:29:17 -08005090}
5091
5092sub full_path {
5093 my ($self, $path) = @_;
5094 return (length $self->{left} ? "$self->{left}/" : '') .
5095 $path . (length $self->{right} ? "/$self->{right}" : '');
5096}
5097
Eric Wong3397f9d2006-02-16 01:24:16 -08005098__END__
5099
5100Data structures:
5101
Eric Wong4bb9ed02007-02-03 13:29:17 -08005102
5103$remotes = { # returned by read_all_remotes()
5104 'svn' => {
5105 # svn-remote.svn.url=https://svn.musicpd.org
5106 url => 'https://svn.musicpd.org',
5107 # svn-remote.svn.fetch=mpd/trunk:trunk
5108 fetch => {
5109 'mpd/trunk' => 'trunk',
5110 },
5111 # svn-remote.svn.tags=mpd/tags/*:tags/*
5112 tags => {
5113 path => {
5114 left => 'mpd/tags',
5115 right => '',
5116 regex => qr!mpd/tags/([^/]+)$!,
5117 glob => 'tags/*',
5118 },
5119 ref => {
5120 left => 'tags',
5121 right => '',
5122 regex => qr!tags/([^/]+)$!,
5123 glob => 'tags/*',
5124 },
5125 }
5126 }
5127};
5128
Eric Wong44320b92007-01-13 22:35:53 -08005129$log_entry hashref as returned by libsvn_log_entry()
Eric Wong3397f9d2006-02-16 01:24:16 -08005130{
Eric Wong44320b92007-01-13 22:35:53 -08005131 log => 'whitespace-formatted log entry
Eric Wong3397f9d2006-02-16 01:24:16 -08005132', # trailing newline is preserved
5133 revision => '8', # integer
5134 date => '2004-02-24T17:01:44.108345Z', # commit date
5135 author => 'committer name'
5136};
5137
Eric Wong6e8548c2007-01-27 01:32:00 -08005138
5139# this is generated by generate_diff();
Eric Wong3397f9d2006-02-16 01:24:16 -08005140@mods = array of diff-index line hashes, each element represents one line
5141 of diff-index output
5142
5143diff-index line ($m hash)
5144{
5145 mode_a => first column of diff-index output, no leading ':',
5146 mode_b => second column of diff-index output,
5147 sha1_b => sha1sum of the final blob,
Eric Wongac8e0b92006-03-03 01:20:07 -08005148 chg => change type [MCRADT],
Eric Wong3397f9d2006-02-16 01:24:16 -08005149 file_a => original file name of a file (iff chg is 'C' or 'R')
5150 file_b => new/current file name of a file (any chg)
5151}
5152;
Eric Wonga5e0ced2006-06-12 15:23:48 -07005153
Eric Wonga00439a2006-06-27 19:39:13 -07005154# retval of read_url_paths{,_all}();
5155$l_map = {
5156 # repository root url
5157 'https://svn.musicpd.org' => {
5158 # repository path # GIT_SVN_ID
5159 'mpd/trunk' => 'trunk',
5160 'mpd/tags/0.11.5' => 'tags/0.11.5',
5161 },
5162}
5163
Eric Wonga5e0ced2006-06-12 15:23:48 -07005164Notes:
5165 I don't trust the each() function on unless I created %hash myself
5166 because the internal iterator may not have started at base.