blob: 7609a837419012d94109cf8b4de1b9bbdb969c83 [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,
Eric Wongba24e742008-08-07 02:06:16 -070069 $_git_format, $_commit_url);
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 } ],
Eric Wong1ce255d2007-01-14 23:21:16 -0800134 'set-tree' => [ \&cmd_set_tree,
135 "Set an SVN repository to a git tree-ish",
136 { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200137 'create-ignore' => [ \&cmd_create_ignore,
138 'Create a .gitignore per svn:ignore',
139 { 'revision|r=i' => \$_revision
140 } ],
Benoit Sigoure15153452007-10-16 16:36:50 +0200141 'propget' => [ \&cmd_propget,
142 'Print the value of a property on a file or directory',
143 { 'revision|r=i' => \$_revision } ],
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200144 'proplist' => [ \&cmd_proplist,
145 'List all properties of a file or directory',
146 { 'revision|r=i' => \$_revision } ],
Eric Wong5969cbe2007-01-11 17:58:39 -0800147 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200148 { 'revision|r=i' => \$_revision
Lars Hjemli05b4df32007-09-05 11:35:29 +0200149 } ],
Vineet Kumar2d879792007-11-19 14:56:15 -0800150 'show-externals' => [ \&cmd_show_externals, "Show svn:externals listings",
151 { 'revision|r=i' => \$_revision
152 } ],
Eric Wong1c8443b2007-01-14 02:17:00 -0800153 'multi-fetch' => [ \&cmd_multi_fetch,
Eric Wonge98671e2007-02-14 02:21:19 -0800154 "Deprecated alias for $0 fetch --all",
155 { 'revision|r=s' => \$_revision, %fc_opts } ],
Eric Wong706587f2007-01-18 17:50:01 -0800156 'migrate' => [ sub { },
157 # no-op, we automatically run this anyways,
Eric Wong706587f2007-01-18 17:50:01 -0800158 'Migrate configuration/metadata/layout from
159 previous versions of git-svn',
Eric Wonga836a0e2007-02-14 19:34:56 -0800160 { 'minimize' => \$Git::SVN::Migration::_minimize,
161 %remote_opts } ],
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800162 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
163 { 'limit=i' => \$Git::SVN::Log::limit,
Eric Wong79bb8d82006-06-01 02:35:44 -0700164 'revision|r=s' => \$_revision,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800165 'verbose|v' => \$Git::SVN::Log::verbose,
166 'incremental' => \$Git::SVN::Log::incremental,
167 'oneline' => \$Git::SVN::Log::oneline,
168 'show-commit' => \$Git::SVN::Log::show_commit,
169 'non-recursive' => \$Git::SVN::Log::non_recursive,
Eric Wong79bb8d82006-06-01 02:35:44 -0700170 'authors-file|A=s' => \$_authors,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800171 'color' => \$Git::SVN::Log::color,
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200172 'pager=s' => \$Git::SVN::Log::pager
Eric Wong79bb8d82006-06-01 02:35:44 -0700173 } ],
Eric Wong222566e2008-08-08 01:41:58 -0700174 'find-rev' => [ \&cmd_find_rev,
175 "Translate between SVN revision numbers and tree-ish",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200176 {} ],
Eric Wong905f8b72007-02-16 03:22:40 -0800177 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
178 { 'merge|m|M' => \$_merge,
179 'verbose|v' => \$_verbose,
180 'strategy|s=s' => \$_strategy,
Eric Wongdee41f32007-03-13 11:40:36 -0700181 'local|l' => \$_local,
Eric Wong905f8b72007-02-16 03:22:40 -0800182 'fetch-all|all' => \$_fetch_all,
Seth Falcon7d45e142008-05-19 20:29:17 -0700183 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800184 %fc_opts } ],
Eric Wong44320b92007-01-13 22:35:53 -0800185 'commit-diff' => [ \&cmd_commit_diff,
186 'Commit a diff between two trees',
Eric Wong27e9fb82006-06-27 19:39:12 -0700187 { 'message|m=s' => \$_message,
188 'file|F=s' => \$_file,
Eric Wong45bf4732006-11-09 01:19:37 -0800189 'revision|r=s' => \$_revision,
Eric Wong27e9fb82006-06-27 19:39:12 -0700190 %cmt_opts } ],
David D. Kilzere6fefa92007-11-21 11:57:18 -0800191 'info' => [ \&cmd_info,
192 "Show info about the latest SVN revision
193 on the current branch",
David D. Kilzer8b014d72007-11-21 11:57:19 -0800194 { 'url' => \$_url, } ],
Tim Stoakes6fb53752008-02-10 15:21:08 +1030195 'blame' => [ \&Git::SVN::Log::cmd_blame,
196 "Show what revision and author last modified each line of a file",
Steven Grimm4be40382008-05-10 22:11:18 -0700197 { 'git-format' => \$_git_format } ],
Eric Wong3397f9d2006-02-16 01:24:16 -0800198);
Eric Wong9d55b412006-06-12 15:53:13 -0700199
Eric Wong3397f9d2006-02-16 01:24:16 -0800200my $cmd;
201for (my $i = 0; $i < @ARGV; $i++) {
202 if (defined $cmd{$ARGV[$i]}) {
203 $cmd = $ARGV[$i];
204 splice @ARGV, $i, 1;
205 last;
206 }
207};
208
Eric Wong540424b2007-12-19 00:31:43 -0800209# make sure we're always running at the top-level working directory
210unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
Eric Wong5253dc32007-02-20 01:36:30 -0800211 unless (-d $ENV{GIT_DIR}) {
212 if ($git_dir_user_set) {
213 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
214 "but it is not a directory\n";
215 }
216 my $git_dir = delete $ENV{GIT_DIR};
217 chomp(my $cdup = command_oneline(qw/rev-parse --show-cdup/));
218 unless (length $cdup) {
219 die "Already at toplevel, but $git_dir ",
220 "not found '$cdup'\n";
221 }
222 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
223 unless (-d $git_dir) {
224 die "$git_dir still not found after going to ",
225 "'$cdup'\n";
226 }
227 $ENV{GIT_DIR} = $git_dir;
228 }
Adam Robenffe256f2008-05-23 16:19:41 +0200229 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wong5253dc32007-02-20 01:36:30 -0800230}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100231
232my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
233
234read_repo_config(\%opts);
Eric Wong222566e2008-08-08 01:41:58 -0700235if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
236 Getopt::Long::Configure('pass_through');
237}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100238my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
239 'minimize-connections' => \$Git::SVN::Migration::_minimize,
240 'id|i=s' => \$Git::SVN::default_ref_id,
241 'svn-remote|remote|R=s' => sub {
242 $Git::SVN::no_reuse_existing = 1;
243 $Git::SVN::default_repo_id = $_[1] });
244exit 1 if (!$rv && $cmd && $cmd ne 'log');
245
246usage(0) if $_help;
247version() if $_version;
248usage(1) unless defined $cmd;
249load_authors() if $_authors;
250
Eric Wong0425ea92007-02-16 18:45:01 -0800251unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
Eric Wong706587f2007-01-18 17:50:01 -0800252 Git::SVN::Migration::migration_check();
253}
Eric Wongecc712d2007-01-31 12:28:10 -0800254Git::SVN::init_vars();
Eric Wongb805b442007-01-22 13:52:04 -0800255eval {
256 Git::SVN::verify_remotes_sanity();
257 $cmd{$cmd}->[0]->(@ARGV);
258};
259fatal $@ if $@;
Eric Wong1e889ef2007-02-16 01:45:13 -0800260post_fetch_checkout();
Eric Wong3397f9d2006-02-16 01:24:16 -0800261exit 0;
262
263####################### primary functions ######################
264sub usage {
265 my $exit = shift || 0;
266 my $fd = $exit ? \*STDERR : \*STDOUT;
267 print $fd <<"";
268git-svn - bidirectional operations between a single Subversion tree and git
Stephan Beyer1b1dd232008-07-13 15:36:15 +0200269Usage: git svn <command> [options] [arguments]\n
Eric Wong448c81b2006-03-03 01:20:09 -0800270
271 print $fd "Available commands:\n" unless $cmd;
Eric Wong3397f9d2006-02-16 01:24:16 -0800272
273 foreach (sort keys %cmd) {
Eric Wong448c81b2006-03-03 01:20:09 -0800274 next if $cmd && $cmd ne $_;
Eric Wonga836a0e2007-02-14 19:34:56 -0800275 next if /^multi-/; # don't show deprecated commands
Eric Wongb203b762006-10-11 14:53:36 -0700276 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
Benoit Sigoureaa807bc2007-11-03 19:53:34 +0100277 foreach (sort keys %{$cmd{$_}->[2]}) {
Eric Wong512b6202007-04-03 01:57:08 -0700278 # mixed-case options are for .git/config only
279 next if /[A-Z]/ && /^[a-z]+$/i;
Eric Wong448c81b2006-03-03 01:20:09 -0800280 # prints out arguments as they should be passed:
Eric Wongb8c92ca2006-05-24 01:40:37 -0700281 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
Eric Wongb203b762006-10-11 14:53:36 -0700282 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
Eric Wong448c81b2006-03-03 01:20:09 -0800283 "--$_" : "-$_" }
284 split /\|/,$_)," $x\n";
285 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800286 }
287 print $fd <<"";
Eric Wong448c81b2006-03-03 01:20:09 -0800288\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
289arbitrary identifier if you're tracking multiple SVN branches/repositories in
290one git repository and want to keep them separate. See git-svn(1) for more
291information.
Eric Wong3397f9d2006-02-16 01:24:16 -0800292
293 exit $exit;
294}
295
Eric Wong551ce282006-02-20 10:57:29 -0800296sub version {
Eric Wong7d60ab22006-12-28 01:16:20 -0800297 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
Eric Wong551ce282006-02-20 10:57:29 -0800298 exit 0;
299}
300
Eric Wong8164b652007-01-11 15:35:55 -0800301sub do_git_init_db {
302 unless (-d $ENV{GIT_DIR}) {
303 my @init_db = ('init');
304 push @init_db, "--template=$_template" if defined $_template;
Eric Wongdadc6d22007-02-14 12:27:41 -0800305 if (defined $_shared) {
306 if ($_shared =~ /[a-z]/) {
307 push @init_db, "--shared=$_shared";
308 } else {
309 push @init_db, "--shared";
310 }
311 }
Eric Wong8164b652007-01-11 15:35:55 -0800312 command_noisy(@init_db);
Adam Robenffe256f2008-05-23 16:19:41 +0200313 $_repository = Git->repository(Repository => ".git");
Eric Wong8164b652007-01-11 15:35:55 -0800314 }
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800315 my $set;
316 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
317 foreach my $i (keys %icv) {
318 die "'$set' and '$i' cannot both be set\n" if $set;
319 next unless defined $icv{$i};
320 command_noisy('config', "$pfx.$i", $icv{$i});
321 $set = $i;
322 }
Eric Wong8164b652007-01-11 15:35:55 -0800323}
324
Eric Wongdadc6d22007-02-14 12:27:41 -0800325sub init_subdir {
326 my $repo_path = shift or return;
327 mkpath([$repo_path]) unless -d $repo_path;
328 chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
Eric Wongf30603f2007-02-23 01:26:26 -0800329 $ENV{GIT_DIR} = '.git';
Adam Robenffe256f2008-05-23 16:19:41 +0200330 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wongdadc6d22007-02-14 12:27:41 -0800331}
332
Eric Wong0425ea92007-02-16 18:45:01 -0800333sub cmd_clone {
334 my ($url, $path) = @_;
335 if (!defined $path &&
martin f. krafft8f728fb2007-07-14 11:25:28 +0200336 (defined $_trunk || defined $_branches || defined $_tags ||
337 defined $_stdlayout) &&
Eric Wong0425ea92007-02-16 18:45:01 -0800338 $url !~ m#^[a-z\+]+://#) {
339 $path = $url;
340 }
Eric Wong0425ea92007-02-16 18:45:01 -0800341 $path = basename($url) if !defined $path || !length $path;
Eric Wongf30603f2007-02-23 01:26:26 -0800342 cmd_init($url, $path);
Eric Wong0425ea92007-02-16 18:45:01 -0800343 Git::SVN::fetch_all($Git::SVN::default_repo_id);
344}
345
Eric Wongd2866f92007-01-11 12:26:16 -0800346sub cmd_init {
martin f. krafft8f728fb2007-07-14 11:25:28 +0200347 if (defined $_stdlayout) {
348 $_trunk = 'trunk' if (!defined $_trunk);
349 $_tags = 'tags' if (!defined $_tags);
350 $_branches = 'branches' if (!defined $_branches);
351 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800352 if (defined $_trunk || defined $_branches || defined $_tags) {
353 return cmd_multi_init(@_);
Eric Wong03e0ea82006-06-30 21:42:53 -0700354 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800355 my $url = shift or die "SVN repository location required ",
356 "as a command-line argument\n";
357 init_subdir(@_);
Eric Wong8164b652007-01-11 15:35:55 -0800358 do_git_init_db();
Eric Wong03e0ea82006-06-30 21:42:53 -0700359
Eric Wong706587f2007-01-18 17:50:01 -0800360 Git::SVN->init($url);
Eric Wong3397f9d2006-02-16 01:24:16 -0800361}
362
Eric Wong2a3240b2007-01-04 18:09:56 -0800363sub cmd_fetch {
Eric Wonge98671e2007-02-14 02:21:19 -0800364 if (grep /^\d+=./, @_) {
365 die "'<rev>=<commit>' fetch arguments are ",
366 "no longer supported.\n";
Eric Wong07a1c952007-01-22 15:47:41 -0800367 }
Eric Wonge98671e2007-02-14 02:21:19 -0800368 my ($remote) = @_;
369 if (@_ > 1) {
Eric Wong905f8b72007-02-16 03:22:40 -0800370 die "Usage: $0 fetch [--all] [svn-remote]\n";
Eric Wonge98671e2007-02-14 02:21:19 -0800371 }
372 $remote ||= $Git::SVN::default_repo_id;
373 if ($_fetch_all) {
374 cmd_multi_fetch();
375 } else {
376 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
Eric Wong1c8443b2007-01-14 02:17:00 -0800377 }
Eric Wong2a3240b2007-01-04 18:09:56 -0800378}
379
Eric Wong1ce255d2007-01-14 23:21:16 -0800380sub cmd_set_tree {
Eric Wong3397f9d2006-02-16 01:24:16 -0800381 my (@commits) = @_;
382 if ($_stdin || !@commits) {
383 print "Reading from stdin...\n";
384 @commits = ();
385 while (<STDIN>) {
Eric Wong1ca72ae2006-03-03 01:20:09 -0800386 if (/\b($sha1_short)\b/o) {
Eric Wong3397f9d2006-02-16 01:24:16 -0800387 unshift @commits, $1;
388 }
389 }
390 }
391 my @revs;
Eric Wong8de010a2006-02-20 10:57:26 -0800392 foreach my $c (@commits) {
Eric Wongaef4e922006-12-15 10:59:54 -0800393 my @tmp = command('rev-parse',$c);
Eric Wong8de010a2006-02-20 10:57:26 -0800394 if (scalar @tmp == 1) {
395 push @revs, $tmp[0];
396 } elsif (scalar @tmp > 1) {
Eric Wongaef4e922006-12-15 10:59:54 -0800397 push @revs, reverse(command('rev-list',@tmp));
Eric Wong8de010a2006-02-20 10:57:26 -0800398 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200399 fatal "Failed to rev-parse $c";
Eric Wong8de010a2006-02-20 10:57:26 -0800400 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800401 }
Eric Wong1ce255d2007-01-14 23:21:16 -0800402 my $gs = Git::SVN->new;
403 my ($r_last, $cmt_last) = $gs->last_rev_commit;
404 $gs->fetch;
Eric Wong97f69872007-01-25 11:53:13 -0800405 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
Eric Wong1ce255d2007-01-14 23:21:16 -0800406 fatal "There are new revisions that were fetched ",
407 "and need to be merged (or acknowledged) ",
408 "before committing.\nlast rev: $r_last\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200409 " current: $gs->{last_rev}";
Eric Wong1ce255d2007-01-14 23:21:16 -0800410 }
411 $gs->set_tree($_) foreach @revs;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700412 print "Done committing ",scalar @revs," revisions to SVN\n";
Eric Wong3157dd92007-12-13 08:27:34 -0800413 unlink $gs->{index};
Eric Wonga5e0ced2006-06-12 15:23:48 -0700414}
415
Eric Wongd7ad3be2007-01-14 03:14:28 -0800416sub cmd_dcommit {
417 my $head = shift;
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100418 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
David Reiss826a9332007-11-13 13:47:26 -0800419 'Cannot dcommit with a dirty index. Commit your changes first, '
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100420 . "or stash them with `git stash'.\n";
Eric Wongd7ad3be2007-01-14 03:14:28 -0800421 $head ||= 'HEAD';
Eric Wonga8ae2622007-02-13 14:22:11 -0800422 my @refs;
Eric Wong13c823f2007-04-08 00:59:19 -0700423 my ($url, $rev, $uuid, $gs) = working_head_info($head, \@refs);
Thomas Rast2cb61102008-08-31 15:50:59 +0200424 unless ($gs) {
425 die "Unable to determine upstream SVN information from ",
426 "$head history.\nPerhaps the repository is empty.";
427 }
Eric Wong711521e2008-08-20 00:30:06 -0700428 $url = defined $_commit_url ? $_commit_url : $gs->full_url;
Eric Wongba24e742008-08-07 02:06:16 -0700429 my $last_rev = $_revision if defined $_revision;
Matthieu Moy59b0c242008-04-24 20:06:36 +0200430 if ($url) {
431 print "Committing to $url ...\n";
432 }
Eric Wong733a65a2007-06-13 02:23:28 -0700433 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
Eric Wong751eb392007-08-31 18:16:12 -0700434 if ($_no_rebase && scalar(@$linear_refs) > 1) {
435 warn "Attempting to commit more than one change while ",
436 "--no-rebase is enabled.\n",
437 "If these changes depend on each other, re-running ",
Eric Wong7dfa16b2008-01-02 10:09:49 -0800438 "without --no-rebase may be required."
Eric Wong751eb392007-08-31 18:16:12 -0700439 }
Eric Wong711521e2008-08-20 00:30:06 -0700440 my $expect_url = $url;
441 Git::SVN::remove_username($expect_url);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800442 while (1) {
443 my $d = shift @$linear_refs or last;
Eric Wong45bf4732006-11-09 01:19:37 -0800444 unless (defined $last_rev) {
445 (undef, $last_rev, undef) = cmt_metadata("$d~1");
446 unless (defined $last_rev) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800447 fatal "Unable to extract revision information ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200448 "from commit $d~1";
Eric Wong45bf4732006-11-09 01:19:37 -0800449 }
450 }
Eric Wongb22d4492006-08-26 00:01:23 -0700451 if ($_dry_run) {
452 print "diff-tree $d~1 $d\n";
453 } else {
Eric Wong751eb392007-08-31 18:16:12 -0700454 my $cmt_rev;
Eric Wongd7ad3be2007-01-14 03:14:28 -0800455 my %ed_opts = ( r => $last_rev,
Eric Wong61395352007-01-27 14:33:08 -0800456 log => get_commit_entry($d)->{log},
Eric Wongba24e742008-08-07 02:06:16 -0700457 ra => Git::SVN::Ra->new($url),
Konstantin V. Arkhipov3caf3202007-11-14 03:52:02 +0300458 config => SVN::Core::config_get_config(
459 $Git::SVN::Ra::config_dir
460 ),
Eric Wong61395352007-01-27 14:33:08 -0800461 tree_a => "$d~1",
462 tree_b => $d,
463 editor_cb => sub {
464 print "Committed r$_[0]\n";
Eric Wong751eb392007-08-31 18:16:12 -0700465 $cmt_rev = $_[0];
466 },
Eric Wonga8ae2622007-02-13 14:22:11 -0800467 svn_path => '');
Eric Wong61395352007-01-27 14:33:08 -0800468 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800469 print "No changes\n$d~1 == $d\n";
Eric Wong733a65a2007-06-13 02:23:28 -0700470 } elsif ($parents->{$d} && @{$parents->{$d}}) {
Eric Wong751eb392007-08-31 18:16:12 -0700471 $gs->{inject_parents_dcommit}->{$cmt_rev} =
Eric Wong733a65a2007-06-13 02:23:28 -0700472 $parents->{$d};
Eric Wongd7ad3be2007-01-14 03:14:28 -0800473 }
Eric Wong751eb392007-08-31 18:16:12 -0700474 $_fetch_all ? $gs->fetch_all : $gs->fetch;
Eric Wong7dfa16b2008-01-02 10:09:49 -0800475 $last_rev = $cmt_rev;
Eric Wong751eb392007-08-31 18:16:12 -0700476 next if $_no_rebase;
477
478 # we always want to rebase against the current HEAD,
479 # not any head that was passed to us
Eric Wongc74d9ac2007-11-05 03:21:47 -0800480 my @diff = command('diff-tree', $d,
Eric Wong751eb392007-08-31 18:16:12 -0700481 $gs->refname, '--');
482 my @finish;
483 if (@diff) {
484 @finish = rebase_cmd();
Eric Wongc74d9ac2007-11-05 03:21:47 -0800485 print STDERR "W: $d and ", $gs->refname,
Eric Wong751eb392007-08-31 18:16:12 -0700486 " differ, using @finish:\n",
Eric Wongc74d9ac2007-11-05 03:21:47 -0800487 join("\n", @diff), "\n";
Eric Wong751eb392007-08-31 18:16:12 -0700488 } else {
489 print "No changes between current HEAD and ",
490 $gs->refname,
491 "\nResetting to the latest ",
492 $gs->refname, "\n";
493 @finish = qw/reset --mixed/;
494 }
495 command_noisy(@finish, $gs->refname);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800496 if (@diff) {
497 @refs = ();
498 my ($url_, $rev_, $uuid_, $gs_) =
499 working_head_info($head, \@refs);
500 my ($linear_refs_, $parents_) =
501 linearize_history($gs_, \@refs);
502 if (scalar(@$linear_refs) !=
503 scalar(@$linear_refs_)) {
504 fatal "# of revisions changed ",
505 "\nbefore:\n",
506 join("\n", @$linear_refs),
507 "\n\nafter:\n",
508 join("\n", @$linear_refs_), "\n",
509 'If you are attempting to commit ',
510 "merges, try running:\n\t",
511 'git rebase --interactive',
512 '--preserve-merges ',
513 $gs->refname,
514 "\nBefore dcommitting";
515 }
Eric Wong711521e2008-08-20 00:30:06 -0700516 if ($url_ ne $expect_url) {
Eric Wongc74d9ac2007-11-05 03:21:47 -0800517 fatal "URL mismatch after rebase: ",
Eric Wong711521e2008-08-20 00:30:06 -0700518 "$url_ != $expect_url";
Eric Wongc74d9ac2007-11-05 03:21:47 -0800519 }
520 if ($uuid_ ne $uuid) {
521 fatal "uuid mismatch after rebase: ",
522 "$uuid_ != $uuid";
523 }
524 # remap parents
525 my (%p, @l, $i);
526 for ($i = 0; $i < scalar @$linear_refs; $i++) {
527 my $new = $linear_refs_->[$i] or next;
528 $p{$new} =
529 $parents->{$linear_refs->[$i]};
530 push @l, $new;
531 }
532 $parents = \%p;
533 $linear_refs = \@l;
534 }
Eric Wongb22d4492006-08-26 00:01:23 -0700535 }
536 }
Eric Wong3157dd92007-12-13 08:27:34 -0800537 unlink $gs->{index};
Eric Wongb22d4492006-08-26 00:01:23 -0700538}
539
Adam Roben26e60162007-04-27 11:57:53 -0700540sub cmd_find_rev {
Marc-Andre Lureauea14e6c2008-03-11 10:00:45 +0200541 my $revision_or_hash = shift or die "SVN or git revision required ",
542 "as a command-line argument\n";
Adam Roben26e60162007-04-27 11:57:53 -0700543 my $result;
544 if ($revision_or_hash =~ /^r\d+$/) {
Adam Robenb3cb7e42007-04-29 01:35:27 -0700545 my $head = shift;
546 $head ||= 'HEAD';
547 my @refs;
João Abecasis63c56022008-07-14 16:28:04 +0100548 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
Adam Robenb3cb7e42007-04-29 01:35:27 -0700549 unless ($gs) {
550 die "Unable to determine upstream SVN information from ",
551 "$head history\n";
Adam Roben26e60162007-04-27 11:57:53 -0700552 }
Adam Robenb3cb7e42007-04-29 01:35:27 -0700553 my $desired_revision = substr($revision_or_hash, 1);
João Abecasis63c56022008-07-14 16:28:04 +0100554 $result = $gs->rev_map_get($desired_revision, $uuid);
Adam Roben26e60162007-04-27 11:57:53 -0700555 } else {
556 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
557 $result = $rev;
558 }
559 print "$result\n" if $result;
560}
561
Eric Wong905f8b72007-02-16 03:22:40 -0800562sub cmd_rebase {
563 command_noisy(qw/update-index --refresh/);
Eric Wong13c823f2007-04-08 00:59:19 -0700564 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
565 unless ($gs) {
Eric Wong905f8b72007-02-16 03:22:40 -0800566 die "Unable to determine upstream SVN information from ",
567 "working tree history\n";
568 }
Seth Falcon7d45e142008-05-19 20:29:17 -0700569 if ($_dry_run) {
570 print "Remote Branch: " . $gs->refname . "\n";
571 print "SVN URL: " . $url . "\n";
572 return;
573 }
Eric Wong905f8b72007-02-16 03:22:40 -0800574 if (command(qw/diff-index HEAD --/)) {
575 print STDERR "Cannot rebase with uncommited changes:\n";
576 command_noisy('status');
577 exit 1;
578 }
Eric Wongdee41f32007-03-13 11:40:36 -0700579 unless ($_local) {
Steven Grimmcec0d5a2007-11-29 11:54:39 -0800580 # rebase will checkout for us, so no need to do it explicitly
581 $_no_checkout = 'true';
Eric Wongdee41f32007-03-13 11:40:36 -0700582 $_fetch_all ? $gs->fetch_all : $gs->fetch;
583 }
Eric Wong905f8b72007-02-16 03:22:40 -0800584 command_noisy(rebase_cmd(), $gs->refname);
585}
586
Eric Wong5969cbe2007-01-11 17:58:39 -0800587sub cmd_show_ignore {
Eric Wong13c823f2007-04-08 00:59:19 -0700588 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
589 $gs ||= Git::SVN->new;
Eric Wong5969cbe2007-01-11 17:58:39 -0800590 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200591 $gs->prop_walk($gs->{path}, $r, sub {
592 my ($gs, $path, $props) = @_;
593 print STDOUT "\n# $path\n";
594 my $s = $props->{'svn:ignore'} or return;
595 $s =~ s/[\r\n]+/\n/g;
596 chomp $s;
597 $s =~ s#^#$path#gm;
598 print STDOUT "$s\n";
599 });
Eric Wonga5e0ced2006-06-12 15:23:48 -0700600}
601
Vineet Kumar2d879792007-11-19 14:56:15 -0800602sub cmd_show_externals {
603 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
604 $gs ||= Git::SVN->new;
605 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
606 $gs->prop_walk($gs->{path}, $r, sub {
607 my ($gs, $path, $props) = @_;
608 print STDOUT "\n# $path\n";
609 my $s = $props->{'svn:externals'} or return;
610 $s =~ s/[\r\n]+/\n/g;
611 chomp $s;
612 $s =~ s#^#$path#gm;
613 print STDOUT "$s\n";
614 });
615}
616
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200617sub cmd_create_ignore {
618 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
619 $gs ||= Git::SVN->new;
620 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
621 $gs->prop_walk($gs->{path}, $r, sub {
622 my ($gs, $path, $props) = @_;
623 # $path is of the form /path/to/dir/
624 my $ignore = '.' . $path . '.gitignore';
625 my $s = $props->{'svn:ignore'} or return;
626 open(GITIGNORE, '>', $ignore)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200627 or fatal("Failed to open `$ignore' for writing: $!");
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200628 $s =~ s/[\r\n]+/\n/g;
629 chomp $s;
630 # Prefix all patterns so that the ignore doesn't apply
631 # to sub-directories.
632 $s =~ s#^#/#gm;
633 print GITIGNORE "$s\n";
634 close(GITIGNORE)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200635 or fatal("Failed to close `$ignore': $!");
Gustaf Hendebyc4c66b22008-05-05 00:33:09 +0200636 command_noisy('add', '-f', $ignore);
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200637 });
638}
639
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800640sub canonicalize_path {
641 my ($path) = @_;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800642 my $dot_slash_added = 0;
643 if (substr($path, 0, 1) ne "/") {
644 $path = "./" . $path;
645 $dot_slash_added = 1;
646 }
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800647 # File::Spec->canonpath doesn't collapse x/../y into y (for a
648 # good reason), so let's do this manually.
649 $path =~ s#/+#/#g;
650 $path =~ s#/\.(?:/|$)#/#g;
651 $path =~ s#/[^/]+/\.\.##g;
652 $path =~ s#/$##g;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800653 $path =~ s#^\./## if $dot_slash_added;
Gerrit Pape2fe403e2008-07-06 19:28:50 +0000654 $path =~ s#^/##;
655 $path =~ s#^\.$##;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800656 return $path;
657}
658
Benoit Sigoure15153452007-10-16 16:36:50 +0200659# get_svnprops(PATH)
660# ------------------
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200661# Helper for cmd_propget and cmd_proplist below.
Benoit Sigoure15153452007-10-16 16:36:50 +0200662sub get_svnprops {
663 my $path = shift;
664 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
665 $gs ||= Git::SVN->new;
666
667 # prefix THE PATH by the sub-directory from which the user
668 # invoked us.
669 $path = $cmd_dir_prefix . $path;
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200670 fatal("No such file or directory: $path") unless -e $path;
Benoit Sigoure15153452007-10-16 16:36:50 +0200671 my $is_dir = -d $path ? 1 : 0;
672 $path = $gs->{path} . '/' . $path;
673
674 # canonicalize the path (otherwise libsvn will abort or fail to
675 # find the file)
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800676 $path = canonicalize_path($path);
Benoit Sigoure15153452007-10-16 16:36:50 +0200677
678 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
679 my $props;
680 if ($is_dir) {
681 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
682 }
683 else {
684 (undef, $props) = $gs->ra->get_file($path, $r, undef);
685 }
686 return $props;
687}
688
689# cmd_propget (PROP, PATH)
690# ------------------------
691# Print the SVN property PROP for PATH.
692sub cmd_propget {
693 my ($prop, $path) = @_;
694 $path = '.' if not defined $path;
695 usage(1) if not defined $prop;
696 my $props = get_svnprops($path);
697 if (not defined $props->{$prop}) {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200698 fatal("`$path' does not have a `$prop' SVN property.");
Benoit Sigoure15153452007-10-16 16:36:50 +0200699 }
700 print $props->{$prop} . "\n";
701}
702
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200703# cmd_proplist (PATH)
704# -------------------
705# Print the list of SVN properties for PATH.
706sub cmd_proplist {
707 my $path = shift;
708 $path = '.' if not defined $path;
709 my $props = get_svnprops($path);
710 print "Properties on '$path':\n";
711 foreach (sort keys %{$props}) {
712 print " $_\n";
713 }
714}
715
Eric Wong8164b652007-01-11 15:35:55 -0800716sub cmd_multi_init {
Eric Wong9d55b412006-06-12 15:53:13 -0700717 my $url = shift;
Eric Wong98327e52007-01-04 18:02:00 -0800718 unless (defined $_trunk || defined $_branches || defined $_tags) {
719 usage(1);
720 }
Eric Wongdc431662007-05-19 03:59:02 -0700721
722 # there are currently some bugs that prevent multi-init/multi-fetch
723 # setups from working well without this.
724 $Git::SVN::_minimize_url = 1;
725
Eric Wong8164b652007-01-11 15:35:55 -0800726 $_prefix = '' unless defined $_prefix;
Eric Wongdadc6d22007-02-14 12:27:41 -0800727 if (defined $url) {
728 $url =~ s#/+$##;
729 init_subdir(@_);
730 }
Eric Wongf30603f2007-02-23 01:26:26 -0800731 do_git_init_db();
Eric Wong98327e52007-01-04 18:02:00 -0800732 if (defined $_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800733 my $trunk_ref = $_prefix . 'trunk';
734 # try both old-style and new-style lookups:
735 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
Eric Wong8164b652007-01-11 15:35:55 -0800736 unless ($gs_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800737 my ($trunk_url, $trunk_path) =
738 complete_svn_url($url, $_trunk);
739 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
740 undef, $trunk_ref);
Eric Wong98327e52007-01-04 18:02:00 -0800741 }
Eric Wongc35b96e2006-10-11 11:53:21 -0700742 }
Eric Wong706587f2007-01-18 17:50:01 -0800743 return unless defined $_branches || defined $_tags;
Eric Wonge7db67e2007-01-11 17:09:26 -0800744 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
745 complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix);
746 complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
Eric Wong9d55b412006-06-12 15:53:13 -0700747}
748
Eric Wong1c8443b2007-01-14 02:17:00 -0800749sub cmd_multi_fetch {
Eric Wong0af9c9f2007-01-27 22:28:56 -0800750 my $remotes = Git::SVN::read_all_remotes();
751 foreach my $repo_id (sort keys %$remotes) {
Eric Wongdb03cd22007-02-13 00:38:02 -0800752 if ($remotes->{$repo_id}->{url}) {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800753 Git::SVN::fetch_all($repo_id, $remotes);
754 }
Eric Wong706587f2007-01-18 17:50:01 -0800755 }
Eric Wong9d55b412006-06-12 15:53:13 -0700756}
757
Eric Wong44320b92007-01-13 22:35:53 -0800758# this command is special because it requires no metadata
759sub cmd_commit_diff {
760 my ($ta, $tb, $url) = @_;
761 my $usage = "Usage: $0 commit-diff -r<revision> ".
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200762 "<tree-ish> <tree-ish> [<URL>]";
Eric Wong44320b92007-01-13 22:35:53 -0800763 fatal($usage) if (!defined $ta || !defined $tb);
Karl Hasselströmd72ab8c2008-05-17 17:07:09 +0200764 my $svn_path = '';
Eric Wong44320b92007-01-13 22:35:53 -0800765 if (!defined $url) {
766 my $gs = eval { Git::SVN->new };
767 if (!$gs) {
768 fatal("Needed URL or usable git-svn --id in ",
769 "the command-line\n", $usage);
770 }
771 $url = $gs->{url};
Eric Wongd3a840d2007-01-26 01:32:45 -0800772 $svn_path = $gs->{path};
Eric Wong44320b92007-01-13 22:35:53 -0800773 }
774 unless (defined $_revision) {
775 fatal("-r|--revision is a required argument\n", $usage);
776 }
777 if (defined $_message && defined $_file) {
778 fatal("Both --message/-m and --file/-F specified ",
779 "for the commit message.\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200780 "I have no idea what you mean");
Eric Wong44320b92007-01-13 22:35:53 -0800781 }
782 if (defined $_file) {
783 $_message = file_to_s($_file);
784 } else {
785 $_message ||= get_commit_entry($tb)->{log};
786 }
787 my $ra ||= Git::SVN::Ra->new($url);
788 my $r = $_revision;
789 if ($r eq 'HEAD') {
790 $r = $ra->get_latest_revnum;
791 } elsif ($r !~ /^\d+$/) {
792 die "revision argument: $r not understood by git-svn\n";
793 }
Eric Wong61395352007-01-27 14:33:08 -0800794 my %ed_opts = ( r => $r,
795 log => $_message,
796 ra => $ra,
797 tree_a => $ta,
798 tree_b => $tb,
799 editor_cb => sub { print "Committed r$_[0]\n" },
800 svn_path => $svn_path );
801 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong44320b92007-01-13 22:35:53 -0800802 print "No changes\n$ta == $tb\n";
803 }
Eric Wong44320b92007-01-13 22:35:53 -0800804}
805
Thomas Rast05427b92008-08-26 21:32:37 +0200806sub escape_uri_only {
807 my ($uri) = @_;
808 my @tmp;
809 foreach (split m{/}, $uri) {
810 s/([^\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
811 push @tmp, $_;
812 }
813 join('/', @tmp);
814}
815
816sub escape_url {
817 my ($url) = @_;
818 if ($url =~ m#^([^:]+)://([^/]*)(.*)$#) {
819 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
820 $url = "$scheme://$domain$uri";
821 }
822 $url;
823}
824
David D. Kilzere6fefa92007-11-21 11:57:18 -0800825sub cmd_info {
Eric Wongbd2d4f92008-08-05 00:35:16 -0700826 my $path = canonicalize_path(defined($_[0]) ? $_[0] : ".");
Thomas Rastedde9112008-08-26 21:32:36 +0200827 my $fullpath = canonicalize_path($cmd_dir_prefix . $path);
Eric Wongbd2d4f92008-08-05 00:35:16 -0700828 if (exists $_[1]) {
David D. Kilzere6fefa92007-11-21 11:57:18 -0800829 die "Too many arguments specified\n";
830 }
831
832 my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
833
834 if (!$file_type && !$diff_status) {
Thomas Rast2cf3e3a2008-08-29 15:42:48 +0200835 print STDERR "svn: '$path' is not under version control\n";
836 exit 1;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800837 }
838
839 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
840 unless ($gs) {
841 die "Unable to determine upstream SVN information from ",
842 "working tree history\n";
843 }
Eric Wongbd2d4f92008-08-05 00:35:16 -0700844
845 # canonicalize_path() will return "" to make libsvn 1.5.x happy,
846 $path = "." if $path eq "";
847
Thomas Rastedde9112008-08-26 21:32:36 +0200848 my $full_url = $url . ($fullpath eq "" ? "" : "/$fullpath");
David D. Kilzere6fefa92007-11-21 11:57:18 -0800849
David D. Kilzer8b014d72007-11-21 11:57:19 -0800850 if ($_url) {
Thomas Rast05427b92008-08-26 21:32:37 +0200851 print escape_url($full_url), "\n";
David D. Kilzer8b014d72007-11-21 11:57:19 -0800852 return;
853 }
854
David D. Kilzere6fefa92007-11-21 11:57:18 -0800855 my $result = "Path: $path\n";
856 $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
Thomas Rast05427b92008-08-26 21:32:37 +0200857 $result .= "URL: " . escape_url($full_url) . "\n";
David D. Kilzere6fefa92007-11-21 11:57:18 -0800858
Eric Wonga5460eb2007-11-21 18:20:57 -0800859 eval {
860 my $repos_root = $gs->repos_root;
861 Git::SVN::remove_username($repos_root);
Thomas Rast05427b92008-08-26 21:32:37 +0200862 $result .= "Repository Root: " . escape_url($repos_root) . "\n";
Eric Wonga5460eb2007-11-21 18:20:57 -0800863 };
864 if ($@) {
865 $result .= "Repository Root: (offline)\n";
866 }
David D. Kilzere6fefa92007-11-21 11:57:18 -0800867 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A";
868 $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
869
870 $result .= "Node Kind: " .
871 ($file_type eq "dir" ? "directory" : "file") . "\n";
872
873 my $schedule = $diff_status eq "A"
874 ? "add"
875 : ($diff_status eq "D" ? "delete" : "normal");
876 $result .= "Schedule: $schedule\n";
877
878 if ($diff_status eq "A") {
879 print $result, "\n";
880 return;
881 }
882
883 my ($lc_author, $lc_rev, $lc_date_utc);
Thomas Rastedde9112008-08-26 21:32:36 +0200884 my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath);
David D. Kilzere6fefa92007-11-21 11:57:18 -0800885 my $log = command_output_pipe(@args);
886 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
887 while (<$log>) {
888 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
889 $lc_author = $1;
890 $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
891 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
892 (undef, $lc_rev, undef) = ::extract_metadata($1);
893 }
894 }
895 close $log;
896
897 Git::SVN::Log::set_local_timezone();
898
899 $result .= "Last Changed Author: $lc_author\n";
900 $result .= "Last Changed Rev: $lc_rev\n";
901 $result .= "Last Changed Date: " .
902 Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
903
904 if ($file_type ne "dir") {
905 my $text_last_updated_date =
906 ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
907 $result .=
908 "Text Last Updated: " .
909 Git::SVN::Log::format_svn_date($text_last_updated_date) .
910 "\n";
911 my $checksum;
912 if ($diff_status eq "D") {
913 my ($fh, $ctx) =
914 command_output_pipe(qw(cat-file blob), "HEAD:$path");
915 if ($file_type eq "link") {
916 my $file_name = <$fh>;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -0800917 $checksum = md5sum("link $file_name");
David D. Kilzere6fefa92007-11-21 11:57:18 -0800918 } else {
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -0800919 $checksum = md5sum($fh);
David D. Kilzere6fefa92007-11-21 11:57:18 -0800920 }
921 command_close_pipe($fh, $ctx);
922 } elsif ($file_type eq "link") {
923 my $file_name =
924 command(qw(cat-file blob), "HEAD:$path");
925 $checksum =
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -0800926 md5sum("link " . $file_name);
David D. Kilzere6fefa92007-11-21 11:57:18 -0800927 } else {
928 open FILE, "<", $path or die $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -0800929 $checksum = md5sum(\*FILE);
David D. Kilzere6fefa92007-11-21 11:57:18 -0800930 close FILE or die $!;
931 }
932 $result .= "Checksum: " . $checksum . "\n";
933 }
934
935 print $result, "\n";
936}
937
Eric Wong3397f9d2006-02-16 01:24:16 -0800938########################### utility functions #########################
939
Eric Wong905f8b72007-02-16 03:22:40 -0800940sub rebase_cmd {
941 my @cmd = qw/rebase/;
942 push @cmd, '-v' if $_verbose;
943 push @cmd, qw/--merge/ if $_merge;
944 push @cmd, "--strategy=$_strategy" if $_strategy;
945 @cmd;
946}
947
Eric Wong1e889ef2007-02-16 01:45:13 -0800948sub post_fetch_checkout {
949 return if $_no_checkout;
950 my $gs = $Git::SVN::_head or return;
951 return if verify_ref('refs/heads/master^0');
952
953 my $valid_head = verify_ref('HEAD^0');
954 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
955 return if ($valid_head || !verify_ref('HEAD^0'));
956
957 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
958 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
959 return if -f $index;
960
Matthias Lederhofer7ae3df82007-06-03 16:48:16 +0200961 return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
Eric Wong1e889ef2007-02-16 01:45:13 -0800962 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
963 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
964 print STDERR "Checked out HEAD:\n ",
965 $gs->full_url, " r", $gs->last_rev, "\n";
966}
967
Eric Wong98327e52007-01-04 18:02:00 -0800968sub complete_svn_url {
969 my ($url, $path) = @_;
970 $path =~ s#/+$##;
Eric Wong98327e52007-01-04 18:02:00 -0800971 if ($path !~ m#^[a-z\+]+://#) {
Eric Wong98327e52007-01-04 18:02:00 -0800972 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
973 fatal("E: '$path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200974 "and a separate URL is not specified");
Eric Wong98327e52007-01-04 18:02:00 -0800975 }
Eric Wong706587f2007-01-18 17:50:01 -0800976 return ($url, $path);
Eric Wong98327e52007-01-04 18:02:00 -0800977 }
Eric Wong706587f2007-01-18 17:50:01 -0800978 return ($path, '');
Eric Wong98327e52007-01-04 18:02:00 -0800979}
980
Eric Wong9d55b412006-06-12 15:53:13 -0700981sub complete_url_ls_init {
Eric Wong706587f2007-01-18 17:50:01 -0800982 my ($ra, $repo_path, $switch, $pfx) = @_;
983 unless ($repo_path) {
Eric Wong9d55b412006-06-12 15:53:13 -0700984 print STDERR "W: $switch not specified\n";
985 return;
986 }
Eric Wong706587f2007-01-18 17:50:01 -0800987 $repo_path =~ s#/+$##;
988 if ($repo_path =~ m#^[a-z\+]+://#) {
989 $ra = Git::SVN::Ra->new($repo_path);
990 $repo_path = '';
Eric Wonge7db67e2007-01-11 17:09:26 -0800991 } else {
Eric Wong706587f2007-01-18 17:50:01 -0800992 $repo_path =~ s#^/+##;
Eric Wonge7db67e2007-01-11 17:09:26 -0800993 unless ($ra) {
Eric Wong706587f2007-01-18 17:50:01 -0800994 fatal("E: '$repo_path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200995 "and a separate URL is not specified");
Eric Wong9d55b412006-06-12 15:53:13 -0700996 }
Eric Wonge7db67e2007-01-11 17:09:26 -0800997 }
Eric Wong706587f2007-01-18 17:50:01 -0800998 my $url = $ra->{url};
Eric Wongb4d57e52007-02-14 15:10:44 -0800999 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
1000 my $k = "svn-remote.$gs->{repo_id}.url";
1001 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
1002 if ($orig_url && ($orig_url ne $gs->{url})) {
1003 die "$k already set: $orig_url\n",
1004 "wanted to set to: $gs->{url}\n";
Eric Wong88cf4102007-02-01 03:59:07 -08001005 }
Eric Wongb4d57e52007-02-14 15:10:44 -08001006 command_oneline('config', $k, $gs->{url}) unless $orig_url;
Eric Wonged0b9d42008-03-14 11:01:23 -07001007 my $remote_path = "$ra->{svn_path}/$repo_path";
Eric Wongb4d57e52007-02-14 15:10:44 -08001008 $remote_path =~ s#/+#/#g;
1009 $remote_path =~ s#^/##g;
Eric Wonged0b9d42008-03-14 11:01:23 -07001010 $remote_path .= "/*" if $remote_path !~ /\*/;
Eric Wongb4d57e52007-02-14 15:10:44 -08001011 my ($n) = ($switch =~ /^--(\w+)/);
1012 if (length $pfx && $pfx !~ m#/$#) {
1013 die "--prefix='$pfx' must have a trailing slash '/'\n";
Eric Wong9d55b412006-06-12 15:53:13 -07001014 }
Marcus Griep570d35c2008-08-08 01:41:57 -07001015 command_noisy('config',
1016 "svn-remote.$gs->{repo_id}.$n",
1017 "$remote_path:refs/remotes/$pfx*" .
1018 ('/*' x (($remote_path =~ tr/*/*/) - 1)) );
Eric Wong9d55b412006-06-12 15:53:13 -07001019}
1020
Eric Wongaef4e922006-12-15 10:59:54 -08001021sub verify_ref {
1022 my ($ref) = @_;
Eric Wong2c5c1d52006-12-28 01:16:21 -08001023 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
1024 { STDERR => 0 }); };
Eric Wongaef4e922006-12-15 10:59:54 -08001025}
1026
Eric Wonga5e0ced2006-06-12 15:23:48 -07001027sub get_tree_from_treeish {
Eric Wongcf52b8f2006-02-20 10:57:28 -08001028 my ($treeish) = @_;
Eric Wong44320b92007-01-13 22:35:53 -08001029 # $treeish can be a symbolic ref, too:
Eric Wongaef4e922006-12-15 10:59:54 -08001030 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001031 my $expected;
1032 while ($type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001033 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001034 }
1035 if ($type eq 'commit') {
Eric Wongaef4e922006-12-15 10:59:54 -08001036 $expected = (grep /^tree /, command(qw/cat-file commit/,
1037 $treeish))[0];
Eric Wong44320b92007-01-13 22:35:53 -08001038 ($expected) = ($expected =~ /^tree ($sha1)$/o);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001039 die "Unable to get tree from $treeish\n" unless $expected;
1040 } elsif ($type eq 'tree') {
1041 $expected = $treeish;
1042 } else {
1043 die "$treeish is a $type, expected tree, tag or commit\n";
1044 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07001045 return $expected;
1046}
Eric Wongcf52b8f2006-02-20 10:57:28 -08001047
Eric Wong44320b92007-01-13 22:35:53 -08001048sub get_commit_entry {
1049 my ($treeish) = shift;
1050 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
1051 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1052 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1053 open my $log_fh, '>', $commit_editmsg or croak $!;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001054
Eric Wong44320b92007-01-13 22:35:53 -08001055 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wong4ad45152006-07-09 20:20:48 -07001056 if ($type eq 'commit' || $type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001057 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
Eric Wong44320b92007-01-13 22:35:53 -08001058 $type, $treeish);
Eric Wong3397f9d2006-02-16 01:24:16 -08001059 my $in_msg = 0;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001060 my $author;
1061 my $saw_from = 0;
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001062 my $msgbuf = "";
Eric Wong3397f9d2006-02-16 01:24:16 -08001063 while (<$msg_fh>) {
1064 if (!$in_msg) {
1065 $in_msg = 1 if (/^\s*$/);
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001066 $author = $1 if (/^author (.*>)/);
Eric Wongdf746c52006-03-03 01:20:08 -08001067 } elsif (/^git-svn-id: /) {
Eric Wong44320b92007-01-13 22:35:53 -08001068 # skip this for now, we regenerate the
1069 # correct one on re-fetch anyways
1070 # TODO: set *:merge properties or like...
Eric Wong3397f9d2006-02-16 01:24:16 -08001071 } else {
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001072 if (/^From:/ || /^Signed-off-by:/) {
1073 $saw_from = 1;
1074 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001075 $msgbuf .= $_;
Eric Wong3397f9d2006-02-16 01:24:16 -08001076 }
1077 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001078 $msgbuf =~ s/\s+$//s;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001079 if ($Git::SVN::_add_author_from && defined($author)
1080 && !$saw_from) {
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001081 $msgbuf .= "\n\nFrom: $author";
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001082 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001083 print $log_fh $msgbuf or croak $!;
Eric Wongaef4e922006-12-15 10:59:54 -08001084 command_close_pipe($msg_fh, $ctx);
Eric Wong3397f9d2006-02-16 01:24:16 -08001085 }
Eric Wong44320b92007-01-13 22:35:53 -08001086 close $log_fh or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -08001087
1088 if ($_edit || ($type eq 'tree')) {
1089 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
Eric Wong44320b92007-01-13 22:35:53 -08001090 # TODO: strip out spaces, comments, like git-commit.sh
1091 system($editor, $commit_editmsg);
Eric Wong3397f9d2006-02-16 01:24:16 -08001092 }
Eric Wong44320b92007-01-13 22:35:53 -08001093 rename $commit_editmsg, $commit_msg or croak $!;
1094 open $log_fh, '<', $commit_msg or croak $!;
1095 { local $/; chomp($log_entry{log} = <$log_fh>); }
1096 close $log_fh or croak $!;
1097 unlink $commit_msg;
1098 \%log_entry;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001099}
1100
Eric Wong3397f9d2006-02-16 01:24:16 -08001101sub s_to_file {
1102 my ($str, $file, $mode) = @_;
1103 open my $fd,'>',$file or croak $!;
1104 print $fd $str,"\n" or croak $!;
1105 close $fd or croak $!;
1106 chmod ($mode &~ umask, $file) if (defined $mode);
1107}
1108
1109sub file_to_s {
1110 my $file = shift;
1111 open my $fd,'<',$file or croak "$!: file: $file\n";
1112 local $/;
1113 my $ret = <$fd>;
1114 close $fd or croak $!;
1115 $ret =~ s/\s*$//s;
1116 return $ret;
1117}
1118
Eric Wongeeb0abe2006-03-03 01:20:08 -08001119# '<svn username> = real-name <email address>' mapping based on git-svnimport:
1120sub load_authors {
1121 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001122 my $log = $cmd eq 'log';
Eric Wongeeb0abe2006-03-03 01:20:08 -08001123 while (<$authors>) {
1124 chomp;
Richard MUSIL575d0252007-07-17 19:02:57 +02001125 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
Eric Wongeeb0abe2006-03-03 01:20:08 -08001126 my ($user, $name, $email) = ($1, $2, $3);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001127 if ($log) {
1128 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
1129 } else {
1130 $users{$user} = [$name, $email];
1131 }
Eric Wong79bb8d82006-06-01 02:35:44 -07001132 }
1133 close $authors or croak $!;
1134}
1135
Tom Princee0d10e12007-01-28 16:16:53 -08001136# convert GetOpt::Long specs for use by git-config
Eric Wongb8c92ca2006-05-24 01:40:37 -07001137sub read_repo_config {
Eric Wong706587f2007-01-18 17:50:01 -08001138 return unless -d $ENV{GIT_DIR};
Eric Wongb8c92ca2006-05-24 01:40:37 -07001139 my $opts = shift;
Eric Wong97ae0912007-02-11 15:21:24 -08001140 my @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001141 foreach my $o (keys %$opts) {
Eric Wong97ae0912007-02-11 15:21:24 -08001142 # if we have mixedCase and a long option-only, then
1143 # it's a config-only variable that we don't need for
1144 # the command-line.
1145 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001146 my $v = $opts->{$o};
Eric Wong97ae0912007-02-11 15:21:24 -08001147 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001148 $key =~ s/-//g;
Tom Princee0d10e12007-01-28 16:16:53 -08001149 my $arg = 'git-config';
Eric Wongb8c92ca2006-05-24 01:40:37 -07001150 $arg .= ' --int' if ($o =~ /[:=]i$/);
1151 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1152 if (ref $v eq 'ARRAY') {
1153 chomp(my @tmp = `$arg --get-all svn.$key`);
1154 @$v = @tmp if @tmp;
1155 } else {
1156 chomp(my $tmp = `$arg --get svn.$key`);
Eric Wong77742842007-02-10 21:07:12 -08001157 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
Eric Wongb8c92ca2006-05-24 01:40:37 -07001158 $$v = $tmp;
1159 }
1160 }
1161 }
Eric Wong97ae0912007-02-11 15:21:24 -08001162 delete @$opts{@config_only} if @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001163}
1164
Eric Wong79bb8d82006-06-01 02:35:44 -07001165sub extract_metadata {
Eric Wongc1927a82006-06-27 19:39:11 -07001166 my $id = shift or return (undef, undef, undef);
Sam Vilain3dfab992007-06-30 20:56:13 +12001167 my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
Eric Wong79bb8d82006-06-01 02:35:44 -07001168 \s([a-f\d\-]+)$/x);
Eric Wonge70dc782006-11-23 14:54:04 -08001169 if (!defined $rev || !$uuid || !$url) {
Eric Wong79bb8d82006-06-01 02:35:44 -07001170 # some of the original repositories I made had
Pavel Roskin82e5a822006-07-10 01:50:18 -04001171 # identifiers like this:
Sam Vilain3dfab992007-06-30 20:56:13 +12001172 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
Eric Wong79bb8d82006-06-01 02:35:44 -07001173 }
1174 return ($url, $rev, $uuid);
1175}
1176
Eric Wongc1927a82006-06-27 19:39:11 -07001177sub cmt_metadata {
1178 return extract_metadata((grep(/^git-svn-id: /,
Eric Wongaef4e922006-12-15 10:59:54 -08001179 command(qw/cat-file commit/, shift)))[-1]);
Eric Wongc1927a82006-06-27 19:39:11 -07001180}
1181
Eric Wong905f8b72007-02-16 03:22:40 -08001182sub working_head_info {
1183 my ($head, $refs) = @_;
Pedro Melob6309ac2008-04-10 17:05:21 +01001184 my @args = ('log', '--no-color', '--first-parent', '--pretty=medium');
Lars Hjemli05b4df32007-09-05 11:35:29 +02001185 my ($fh, $ctx) = command_output_pipe(@args, $head);
Sam Vilain3dfab992007-06-30 20:56:13 +12001186 my $hash;
Sam Vilain40cb8f82007-06-30 20:56:14 +12001187 my %max;
Sam Vilain3dfab992007-06-30 20:56:13 +12001188 while (<$fh>) {
1189 if ( m{^commit ($::sha1)$} ) {
1190 unshift @$refs, $hash if $hash and $refs;
1191 $hash = $1;
1192 next;
1193 }
1194 next unless s{^\s*(git-svn-id:)}{$1};
1195 my ($url, $rev, $uuid) = extract_metadata($_);
Eric Wong13c823f2007-04-08 00:59:19 -07001196 if (defined $url && defined $rev) {
Sam Vilain40cb8f82007-06-30 20:56:14 +12001197 next if $max{$url} and $max{$url} < $rev;
Eric Wong13c823f2007-04-08 00:59:19 -07001198 if (my $gs = Git::SVN->find_by_url($url)) {
João Abecasis63c56022008-07-14 16:28:04 +01001199 my $c = $gs->rev_map_get($rev, $uuid);
Adam Robenb03c7a62007-04-25 11:50:32 -07001200 if ($c && $c eq $hash) {
Eric Wong13c823f2007-04-08 00:59:19 -07001201 close $fh; # break the pipe
1202 return ($url, $rev, $uuid, $gs);
Sam Vilain40cb8f82007-06-30 20:56:14 +12001203 } else {
Eric Wong060610c2007-12-08 23:27:41 -08001204 $max{$url} ||= $gs->rev_map_max;
Eric Wong13c823f2007-04-08 00:59:19 -07001205 }
1206 }
1207 }
Eric Wong905f8b72007-02-16 03:22:40 -08001208 }
Eric Wong13c823f2007-04-08 00:59:19 -07001209 command_close_pipe($fh, $ctx);
1210 (undef, undef, undef, undef);
Eric Wong905f8b72007-02-16 03:22:40 -08001211}
1212
Eric Wong733a65a2007-06-13 02:23:28 -07001213sub read_commit_parents {
1214 my ($parents, $c) = @_;
Eric Wong7b02b852007-09-08 16:33:08 -07001215 chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
1216 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1217 @{$parents->{$c}} = split(/ /, $p);
Eric Wong733a65a2007-06-13 02:23:28 -07001218}
1219
1220sub linearize_history {
1221 my ($gs, $refs) = @_;
1222 my %parents;
1223 foreach my $c (@$refs) {
1224 read_commit_parents(\%parents, $c);
1225 }
1226
1227 my @linear_refs;
1228 my %skip = ();
1229 my $last_svn_commit = $gs->last_commit;
1230 foreach my $c (reverse @$refs) {
1231 next if $c eq $last_svn_commit;
1232 last if $skip{$c};
1233
1234 unshift @linear_refs, $c;
1235 $skip{$c} = 1;
1236
1237 # we only want the first parent to diff against for linear
1238 # history, we save the rest to inject when we finalize the
1239 # svn commit
1240 my $fp_a = verify_ref("$c~1");
1241 my $fp_b = shift @{$parents{$c}} if $parents{$c};
1242 if (!$fp_a || !$fp_b) {
1243 die "Commit $c\n",
1244 "has no parent commit, and therefore ",
1245 "nothing to diff against.\n",
1246 "You should be working from a repository ",
1247 "originally created by git-svn\n";
1248 }
1249 if ($fp_a ne $fp_b) {
1250 die "$c~1 = $fp_a, however parsing commit $c ",
1251 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1252 }
1253
1254 foreach my $p (@{$parents{$c}}) {
1255 $skip{$p} = 1;
1256 }
1257 }
1258 (\@linear_refs, \%parents);
1259}
1260
David D. Kilzere6fefa92007-11-21 11:57:18 -08001261sub find_file_type_and_diff_status {
1262 my ($path) = @_;
Dmitry Potapov107cee52008-07-21 00:14:07 +04001263 return ('dir', '') if $path eq '';
David D. Kilzere6fefa92007-11-21 11:57:18 -08001264
1265 my $diff_output =
1266 command_oneline(qw(diff --cached --name-status --), $path) || "";
1267 my $diff_status = (split(' ', $diff_output))[0] || "";
1268
1269 my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || "";
1270
1271 return (undef, undef) if !$diff_status && !$ls_tree;
1272
1273 if ($diff_status eq "A") {
1274 return ("link", $diff_status) if -l $path;
1275 return ("dir", $diff_status) if -d $path;
1276 return ("file", $diff_status);
1277 }
1278
1279 my $mode = (split(' ', $ls_tree))[0] || "";
1280
1281 return ("link", $diff_status) if $mode eq "120000";
1282 return ("dir", $diff_status) if $mode eq "040000";
1283 return ("file", $diff_status);
1284}
1285
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001286sub md5sum {
1287 my $arg = shift;
1288 my $ref = ref $arg;
1289 my $md5 = Digest::MD5->new();
Marcus Griep0b191382008-08-12 12:00:53 -04001290 if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001291 $md5->addfile($arg) or croak $!;
1292 } elsif ($ref eq 'SCALAR') {
1293 $md5->add($$arg) or croak $!;
1294 } elsif (!$ref) {
1295 $md5->add($arg) or croak $!;
1296 } else {
1297 ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1298 }
1299 return $md5->hexdigest();
1300}
1301
Eric Wong9b981fc2007-01-11 12:14:21 -08001302package Git::SVN;
1303use strict;
1304use warnings;
Eric Wong060610c2007-12-08 23:27:41 -08001305use Fcntl qw/:DEFAULT :seek/;
1306use constant rev_map_fmt => 'NH40';
Eric Wongecc712d2007-01-31 12:28:10 -08001307use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
Eric Wong62e349d2007-02-16 19:57:29 -08001308 $_repack $_repack_flags $_use_svm_props $_head
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00001309 $_use_svnsync_props $no_reuse_existing $_minimize_url
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001310 $_use_log_author $_add_author_from/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001311use Carp qw/croak/;
1312use File::Path qw/mkpath/;
Eric Wong373274f2007-01-31 13:54:23 -08001313use File::Copy qw/copy/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001314use IPC::Open3;
1315
Karl Hasselström94bc9142008-02-03 17:56:18 +01001316my ($_gc_nr, $_gc_period);
1317
Eric Wong9b981fc2007-01-11 12:14:21 -08001318# properties that we do not log:
1319my %SKIP_PROP;
1320BEGIN {
1321 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
1322 svn:special svn:executable
1323 svn:entry:committed-rev
1324 svn:entry:last-author
1325 svn:entry:uuid
1326 svn:entry:committed-date/;
Eric Wong91b03282007-02-11 00:51:33 -08001327
1328 # some options are read globally, but can be overridden locally
1329 # per [svn-remote "..."] section. Command-line options will *NOT*
1330 # override options set in an [svn-remote "..."] section
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001331 no strict 'refs';
1332 for my $option (qw/follow_parent no_metadata use_svm_props
1333 use_svnsync_props/) {
1334 my $key = $option;
Eric Wong91b03282007-02-11 00:51:33 -08001335 $key =~ tr/_//d;
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001336 my $prop = "-$option";
1337 *$option = sub {
1338 my ($self) = @_;
1339 return $self->{$prop} if exists $self->{$prop};
1340 my $k = "svn-remote.$self->{repo_id}.$key";
1341 eval { command_oneline(qw/config --get/, $k) };
1342 if ($@) {
1343 $self->{$prop} = ${"Git::SVN::_$option"};
Eric Wong91b03282007-02-11 00:51:33 -08001344 } else {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001345 my $v = command_oneline(qw/config --bool/,$k);
1346 $self->{$prop} = $v eq 'false' ? 0 : 1;
Eric Wong91b03282007-02-11 00:51:33 -08001347 }
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001348 return $self->{$prop};
1349 }
Eric Wong91b03282007-02-11 00:51:33 -08001350 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001351}
1352
Marcus Griep0b191382008-08-12 12:00:53 -04001353
Eric Wong321b1842008-01-02 10:10:03 -08001354my (%LOCKFILES, %INDEX_FILES);
1355END {
1356 unlink keys %LOCKFILES if %LOCKFILES;
1357 unlink keys %INDEX_FILES if %INDEX_FILES;
1358}
Eric Wong373274f2007-01-31 13:54:23 -08001359
Eric Wong4bb9ed02007-02-03 13:29:17 -08001360sub resolve_local_globs {
1361 my ($url, $fetch, $glob_spec) = @_;
1362 return unless defined $glob_spec;
1363 my $ref = $glob_spec->{ref};
1364 my $path = $glob_spec->{path};
1365 foreach (command(qw#for-each-ref --format=%(refname) refs/remotes#)) {
1366 next unless m#^refs/remotes/$ref->{regex}$#;
1367 my $p = $1;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001368 my $pathname = desanitize_refname($path->full_path($p));
1369 my $refname = desanitize_refname($ref->full_path($p));
Eric Wong4bb9ed02007-02-03 13:29:17 -08001370 if (my $existing = $fetch->{$pathname}) {
1371 if ($existing ne $refname) {
1372 die "Refspec conflict:\n",
1373 "existing: refs/remotes/$existing\n",
1374 " globbed: refs/remotes/$refname\n";
1375 }
1376 my $u = (::cmt_metadata("refs/remotes/$refname"))[0];
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001377 $u =~ s!^\Q$url\E(/|$)!! or die
Eric Wong4bb9ed02007-02-03 13:29:17 -08001378 "refs/remotes/$refname: '$url' not found in '$u'\n";
1379 if ($pathname ne $u) {
1380 warn "W: Refspec glob conflict ",
1381 "(ref: refs/remotes/$refname):\n",
1382 "expected path: $pathname\n",
1383 " real path: $u\n",
1384 "Continuing ahead with $u\n";
1385 next;
1386 }
1387 } else {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001388 $fetch->{$pathname} = $refname;
1389 }
1390 }
1391}
1392
Eric Wonge98671e2007-02-14 02:21:19 -08001393sub parse_revision_argument {
1394 my ($base, $head) = @_;
1395 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
1396 return ($base, $head);
1397 }
1398 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
1399 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
1400 return ($head, $head) if ($::_revision eq 'HEAD');
1401 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
1402 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
1403 die "revision argument: $::_revision not understood by git-svn\n";
1404}
1405
Eric Wong0af9c9f2007-01-27 22:28:56 -08001406sub fetch_all {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001407 my ($repo_id, $remotes) = @_;
Eric Wong905f8b72007-02-16 03:22:40 -08001408 if (ref $repo_id) {
1409 my $gs = $repo_id;
1410 $repo_id = undef;
1411 $repo_id = $gs->{repo_id};
1412 }
1413 $remotes ||= read_all_remotes();
Eric Wong7447b4b2007-02-14 18:38:46 -08001414 my $remote = $remotes->{$repo_id} or
1415 die "[svn-remote \"$repo_id\"] unknown\n";
Eric Wonge5181922007-02-08 12:53:57 -08001416 my $fetch = $remote->{fetch};
Eric Wong7447b4b2007-02-14 18:38:46 -08001417 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
Eric Wonge5181922007-02-08 12:53:57 -08001418 my (@gs, @globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001419 my $ra = Git::SVN::Ra->new($url);
Eric Wong26a62d52007-02-12 13:25:25 -08001420 my $uuid = $ra->get_uuid;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001421 my $head = $ra->get_latest_revnum;
Eric Wong28710f72007-02-14 13:32:21 -08001422 my $base = defined $fetch ? $head : 0;
Eric Wonge5181922007-02-08 12:53:57 -08001423
1424 # read the max revs for wildcard expansion (branches/*, tags/*)
1425 foreach my $t (qw/branches tags/) {
1426 defined $remote->{$t} or next;
1427 push @globs, $remote->{$t};
Eric Wong93f26892007-02-11 01:20:26 -08001428 my $max_rev = eval { tmp_config(qw/--int --get/,
1429 "svn-remote.$repo_id.${t}-maxRev") };
1430 if (defined $max_rev && ($max_rev < $base)) {
1431 $base = $max_rev;
Eric Wongd6d33462007-02-16 04:05:33 -08001432 } elsif (!defined $max_rev) {
1433 $base = 0;
Eric Wonge5181922007-02-08 12:53:57 -08001434 }
1435 }
1436
Eric Wongdb03cd22007-02-13 00:38:02 -08001437 if ($fetch) {
1438 foreach my $p (sort keys %$fetch) {
1439 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
Eric Wong060610c2007-12-08 23:27:41 -08001440 my $lr = $gs->rev_map_max;
Eric Wongdb03cd22007-02-13 00:38:02 -08001441 if (defined $lr) {
1442 $base = $lr if ($lr < $base);
1443 }
1444 push @gs, $gs;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001445 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08001446 }
Eric Wonge98671e2007-02-14 02:21:19 -08001447
1448 ($base, $head) = parse_revision_argument($base, $head);
Eric Wonge5181922007-02-08 12:53:57 -08001449 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001450}
1451
Eric Wong47e39c52007-01-21 04:27:09 -08001452sub read_all_remotes {
1453 my $r = {};
João Abecasis63c56022008-07-14 16:28:04 +01001454 my $use_svm_props = eval { command_oneline(qw/config --bool
1455 svn.useSvmProps/) };
1456 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
Eric Wong8b8fc062007-01-22 11:44:57 -08001457 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
Avery Pennarun61192162008-07-30 16:53:55 -04001458 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*(.+)\s*$!) {
1459 my ($remote, $local_ref, $_remote_ref) = ($1, $2, $3);
1460 die("svn-remote.$remote: remote ref '$_remote_ref' "
1461 . "must start with 'refs/remotes/'\n")
1462 unless $_remote_ref =~ m{^refs/remotes/(.+)};
1463 my $remote_ref = $1;
Eric Wong46cf98b2007-07-14 12:40:32 -07001464 $local_ref =~ s{^/}{};
1465 $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
João Abecasis63c56022008-07-14 16:28:04 +01001466 $r->{$remote}->{svm} = {} if $use_svm_props;
1467 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
1468 $r->{$1}->{svm} = {};
Eric Wong47e39c52007-01-21 04:27:09 -08001469 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1470 $r->{$1}->{url} = $2;
Eric Wong4bb9ed02007-02-03 13:29:17 -08001471 } elsif (m!^(.+)\.(branches|tags)=
1472 (.*):refs/remotes/(.+)\s*$/!x) {
1473 my ($p, $g) = ($3, $4);
1474 my $rs = $r->{$1}->{$2} = {
Eric Wonge5181922007-02-08 12:53:57 -08001475 t => $2,
Eric Wong93f26892007-02-11 01:20:26 -08001476 remote => $1,
Eric Wong4bb9ed02007-02-03 13:29:17 -08001477 path => Git::SVN::GlobSpec->new($p),
1478 ref => Git::SVN::GlobSpec->new($g) };
1479 if (length($rs->{ref}->{right}) != 0) {
1480 die "The '*' glob character must be the last ",
1481 "character of '$g'\n";
1482 }
Eric Wong47e39c52007-01-21 04:27:09 -08001483 }
1484 }
João Abecasis63c56022008-07-14 16:28:04 +01001485
1486 map {
1487 if (defined $r->{$_}->{svm}) {
1488 my $svm;
1489 eval {
1490 my $section = "svn-remote.$_";
1491 $svm = {
1492 source => tmp_config('--get',
1493 "$section.svm-source"),
1494 replace => tmp_config('--get',
1495 "$section.svm-replace"),
1496 }
1497 };
1498 $r->{$_}->{svm} = $svm;
1499 }
1500 } keys %$r;
1501
Eric Wong47e39c52007-01-21 04:27:09 -08001502 $r;
1503}
1504
Eric Wongecc712d2007-01-31 12:28:10 -08001505sub init_vars {
Karl Hasselström94bc9142008-02-03 17:56:18 +01001506 $_gc_nr = $_gc_period = 1000;
Karl Hasselströmaf788a62008-02-03 17:56:12 +01001507 if (defined $_repack || defined $_repack_flags) {
1508 warn "Repack options are obsolete; they have no effect.\n";
1509 }
Eric Wongecc712d2007-01-31 12:28:10 -08001510}
1511
Eric Wongb805b442007-01-22 13:52:04 -08001512sub verify_remotes_sanity {
Eric Wong536c4b02007-01-23 11:35:53 -08001513 return unless -d $ENV{GIT_DIR};
Eric Wongb805b442007-01-22 13:52:04 -08001514 my %seen;
1515 foreach (command(qw/config -l/)) {
1516 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1517 if ($seen{$1}) {
1518 die "Remote ref refs/remote/$1 is tracked by",
1519 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1520 "Please resolve this ambiguity in ",
1521 "your git configuration file before ",
1522 "continuing\n";
1523 }
1524 $seen{$1} = $_;
1525 }
1526 }
1527}
1528
Eric Wonge6434f82007-01-23 16:29:23 -08001529sub find_existing_remote {
1530 my ($url, $remotes) = @_;
Eric Wongbefc9ad2007-02-17 02:53:07 -08001531 return undef if $no_reuse_existing;
Eric Wonge6434f82007-01-23 16:29:23 -08001532 my $existing;
1533 foreach my $repo_id (keys %$remotes) {
1534 my $u = $remotes->{$repo_id}->{url} or next;
1535 next if $u ne $url;
1536 $existing = $repo_id;
1537 last;
1538 }
1539 $existing;
1540}
1541
1542sub init_remote_config {
Eric Wongd8115c52007-02-01 03:30:31 -08001543 my ($self, $url, $no_write) = @_;
Eric Wonge6434f82007-01-23 16:29:23 -08001544 $url =~ s!/+$!!; # strip trailing slash
1545 my $r = read_all_remotes();
1546 my $existing = find_existing_remote($url, $r);
1547 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001548 unless ($no_write) {
1549 print STDERR "Using existing ",
1550 "[svn-remote \"$existing\"]\n";
1551 }
Eric Wonge6434f82007-01-23 16:29:23 -08001552 $self->{repo_id} = $existing;
Eric Wong4a1bb4c2007-05-13 09:58:14 -07001553 } elsif ($_minimize_url) {
Eric Wonge6434f82007-01-23 16:29:23 -08001554 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
1555 $existing = find_existing_remote($min_url, $r);
1556 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001557 unless ($no_write) {
1558 print STDERR "Using existing ",
1559 "[svn-remote \"$existing\"]\n";
1560 }
Eric Wonge6434f82007-01-23 16:29:23 -08001561 $self->{repo_id} = $existing;
1562 }
1563 if ($min_url ne $url) {
Eric Wonge5181922007-02-08 12:53:57 -08001564 unless ($no_write) {
1565 print STDERR "Using higher level of URL: ",
1566 "$url => $min_url\n";
1567 }
Eric Wonge6434f82007-01-23 16:29:23 -08001568 my $old_path = $self->{path};
1569 $self->{path} = $url;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001570 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
Eric Wonge6434f82007-01-23 16:29:23 -08001571 if (length $old_path) {
1572 $self->{path} .= "/$old_path";
1573 }
1574 $url = $min_url;
1575 }
1576 }
1577 my $orig_url;
1578 if (!$existing) {
1579 # verify that we aren't overwriting anything:
1580 $orig_url = eval {
1581 command_oneline('config', '--get',
1582 "svn-remote.$self->{repo_id}.url")
1583 };
1584 if ($orig_url && ($orig_url ne $url)) {
1585 die "svn-remote.$self->{repo_id}.url already set: ",
1586 "$orig_url\nwanted to set to: $url\n";
1587 }
1588 }
1589 my ($xrepo_id, $xpath) = find_ref($self->refname);
1590 if (defined $xpath) {
1591 die "svn-remote.$xrepo_id.fetch already set to track ",
1592 "$xpath:refs/remotes/", $self->refname, "\n";
1593 }
Eric Wongd8115c52007-02-01 03:30:31 -08001594 unless ($no_write) {
1595 command_noisy('config',
1596 "svn-remote.$self->{repo_id}.url", $url);
Eric Wong46cf98b2007-07-14 12:40:32 -07001597 $self->{path} =~ s{^/}{};
Eric Wongd8115c52007-02-01 03:30:31 -08001598 command_noisy('config', '--add',
1599 "svn-remote.$self->{repo_id}.fetch",
1600 "$self->{path}:".$self->refname);
1601 }
Eric Wonge6434f82007-01-23 16:29:23 -08001602 $self->{url} = $url;
1603}
1604
Eric Wonga8ae2622007-02-13 14:22:11 -08001605sub find_by_url { # repos_root and, path are optional
1606 my ($class, $full_url, $repos_root, $path) = @_;
Adam Roben56973d22007-04-25 12:42:58 -07001607
Eric Wong1a97a502007-02-20 00:43:19 -08001608 return undef unless defined $full_url;
Adam Roben56973d22007-04-25 12:42:58 -07001609 remove_username($full_url);
1610 remove_username($repos_root) if defined $repos_root;
Eric Wonga8ae2622007-02-13 14:22:11 -08001611 my $remotes = read_all_remotes();
1612 if (defined $full_url && defined $repos_root && !defined $path) {
1613 $path = $full_url;
1614 $path =~ s#^\Q$repos_root\E(?:/|$)##;
1615 }
1616 foreach my $repo_id (keys %$remotes) {
1617 my $u = $remotes->{$repo_id}->{url} or next;
Adam Roben56973d22007-04-25 12:42:58 -07001618 remove_username($u);
Eric Wonga8ae2622007-02-13 14:22:11 -08001619 next if defined $repos_root && $repos_root ne $u;
1620
1621 my $fetch = $remotes->{$repo_id}->{fetch} || {};
1622 foreach (qw/branches tags/) {
1623 resolve_local_globs($u, $fetch,
1624 $remotes->{$repo_id}->{$_});
1625 }
1626 my $p = $path;
John Goerzen0bb91d92008-03-08 16:04:05 -06001627 my $rwr = rewrite_root({repo_id => $repo_id});
João Abecasis63c56022008-07-14 16:28:04 +01001628 my $svm = $remotes->{$repo_id}->{svm}
1629 if defined $remotes->{$repo_id}->{svm};
Eric Wonga8ae2622007-02-13 14:22:11 -08001630 unless (defined $p) {
1631 $p = $full_url;
John Goerzen0bb91d92008-03-08 16:04:05 -06001632 my $z = $u;
João Abecasis63c56022008-07-14 16:28:04 +01001633 my $prefix = '';
John Goerzen0bb91d92008-03-08 16:04:05 -06001634 if ($rwr) {
1635 $z = $rwr;
João Abecasis63c56022008-07-14 16:28:04 +01001636 } elsif (defined $svm) {
1637 $z = $svm->{source};
1638 $prefix = $svm->{replace};
1639 $prefix =~ s#^\Q$u\E(?:/|$)##;
1640 $prefix =~ s#/$##;
John Goerzen0bb91d92008-03-08 16:04:05 -06001641 }
João Abecasis63c56022008-07-14 16:28:04 +01001642 $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next;
Eric Wonga8ae2622007-02-13 14:22:11 -08001643 }
1644 foreach my $f (keys %$fetch) {
1645 next if $f ne $p;
1646 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
1647 }
1648 }
1649 undef;
1650}
1651
Eric Wong9b981fc2007-01-11 12:14:21 -08001652sub init {
Eric Wongd8115c52007-02-01 03:30:31 -08001653 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08001654 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong9b981fc2007-01-11 12:14:21 -08001655 if (defined $url) {
Eric Wongd8115c52007-02-01 03:30:31 -08001656 $self->init_remote_config($url, $no_write);
Eric Wong9b981fc2007-01-11 12:14:21 -08001657 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001658 $self;
1659}
1660
Eric Wong706587f2007-01-18 17:50:01 -08001661sub find_ref {
1662 my ($ref_id) = @_;
1663 foreach (command(qw/config -l/)) {
1664 next unless m!^svn-remote\.(.+)\.fetch=
1665 \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x;
1666 my ($repo_id, $path, $ref) = ($1, $2, $3);
1667 if ($ref eq $ref_id) {
1668 $path = '' if ($path =~ m#^\./?#);
1669 return ($repo_id, $path);
1670 }
1671 }
1672 (undef, undef, undef);
1673}
1674
Eric Wong9b981fc2007-01-11 12:14:21 -08001675sub new {
Eric Wong706587f2007-01-18 17:50:01 -08001676 my ($class, $ref_id, $repo_id, $path) = @_;
1677 if (defined $ref_id && !defined $repo_id && !defined $path) {
1678 ($repo_id, $path) = find_ref($ref_id);
1679 if (!defined $repo_id) {
1680 die "Could not find a \"svn-remote.*.fetch\" key ",
1681 "in the repository configuration matching: ",
1682 "refs/remotes/$ref_id\n";
1683 }
1684 }
1685 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong8b8fc062007-01-22 11:44:57 -08001686 if (!defined $self->{path} || !length $self->{path}) {
1687 my $fetch = command_oneline('config', '--get',
1688 "svn-remote.$repo_id.fetch",
1689 ":refs/remotes/$ref_id\$") or
1690 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1691 "\":refs/remotes/$ref_id\$\" in config\n";
1692 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
1693 }
Eric Wong706587f2007-01-18 17:50:01 -08001694 $self->{url} = command_oneline('config', '--get',
1695 "svn-remote.$repo_id.url") or
1696 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
Eric Wongd6d33462007-02-16 04:05:33 -08001697 $self->rebuild;
Eric Wong9b981fc2007-01-11 12:14:21 -08001698 $self;
1699}
1700
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001701sub refname {
1702 my ($refname) = "refs/remotes/$_[0]->{ref_id}" ;
1703
1704 # It cannot end with a slash /, we'll throw up on this because
1705 # SVN can't have directories with a slash in their name, either:
1706 if ($refname =~ m{/$}) {
1707 die "ref: '$refname' ends with a trailing slash, this is ",
1708 "not permitted by git nor Subversion\n";
1709 }
1710
1711 # It cannot have ASCII control character space, tilde ~, caret ^,
1712 # colon :, question-mark ?, asterisk *, space, or open bracket [
1713 # anywhere.
1714 #
1715 # Additionally, % must be escaped because it is used for escaping
1716 # and we want our escaped refname to be reversible
1717 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
1718
1719 # no slash-separated component can begin with a dot .
1720 # /.* becomes /%2E*
1721 $refname =~ s{/\.}{/%2E}g;
1722
1723 # It cannot have two consecutive dots .. anywhere
1724 # .. becomes %2E%2E
1725 $refname =~ s{\.\.}{%2E%2E}g;
1726
1727 return $refname;
1728}
1729
1730sub desanitize_refname {
1731 my ($refname) = @_;
1732 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
1733 return $refname;
1734}
Eric Wong9b981fc2007-01-11 12:14:21 -08001735
Eric Wong26a62d52007-02-12 13:25:25 -08001736sub svm_uuid {
1737 my ($self) = @_;
1738 return $self->{svm}->{uuid} if $self->svm;
1739 $self->ra;
1740 unless ($self->{svm}) {
1741 die "SVM UUID not cached, and reading remotely failed\n";
1742 }
1743 $self->{svm}->{uuid};
1744}
Eric Wong8a49ee92007-02-10 20:46:50 -08001745
Eric Wong26a62d52007-02-12 13:25:25 -08001746sub svm {
1747 my ($self) = @_;
1748 return $self->{svm} if $self->{svm};
1749 my $svm;
Eric Wong8a49ee92007-02-10 20:46:50 -08001750 # see if we have it in our config, first:
1751 eval {
Eric Wong26a62d52007-02-12 13:25:25 -08001752 my $section = "svn-remote.$self->{repo_id}";
1753 $svm = {
Eric Wong93f26892007-02-11 01:20:26 -08001754 source => tmp_config('--get', "$section.svm-source"),
1755 uuid => tmp_config('--get', "$section.svm-uuid"),
Eric Wongbefc9ad2007-02-17 02:53:07 -08001756 replace => tmp_config('--get', "$section.svm-replace"),
Eric Wong8a49ee92007-02-10 20:46:50 -08001757 }
1758 };
Eric Wongbefc9ad2007-02-17 02:53:07 -08001759 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
1760 $self->{svm} = $svm;
1761 }
Eric Wong26a62d52007-02-12 13:25:25 -08001762 $self->{svm};
1763}
1764
1765sub _set_svm_vars {
1766 my ($self, $ra) = @_;
Eric Wongdb03cd22007-02-13 00:38:02 -08001767 return $ra if $self->svm;
Eric Wong26a62d52007-02-12 13:25:25 -08001768
Eric Wongdb03cd22007-02-13 00:38:02 -08001769 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08001770 "(svm:source, svm:uuid) ",
Eric Wongdb03cd22007-02-13 00:38:02 -08001771 "from the following URLs:\n" );
1772 sub read_svm_props {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001773 my ($self, $ra, $path, $r) = @_;
1774 my $props = ($ra->get_dir($path, $r))[2];
Eric Wongdb03cd22007-02-13 00:38:02 -08001775 my $src = $props->{'svm:source'};
Eric Wong8a49ee92007-02-10 20:46:50 -08001776 my $uuid = $props->{'svm:uuid'};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001777 return undef if (!$src || !$uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08001778
Eric Wongbefc9ad2007-02-17 02:53:07 -08001779 chomp($src, $uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08001780
Eric Wong8a49ee92007-02-10 20:46:50 -08001781 $uuid =~ m{^[0-9a-f\-]{30,}$}
1782 or die "doesn't look right - svm:uuid is '$uuid'\n";
Eric Wongbefc9ad2007-02-17 02:53:07 -08001783
1784 # the '!' is used to mark the repos_root!/relative/path
1785 $src =~ s{/?!/?}{/};
Eric Wongdb03cd22007-02-13 00:38:02 -08001786 $src =~ s{/+$}{}; # no trailing slashes please
Eric Wongbefc9ad2007-02-17 02:53:07 -08001787 # username is of no interest
Eric Wongdb03cd22007-02-13 00:38:02 -08001788 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
Eric Wong8a49ee92007-02-10 20:46:50 -08001789
Eric Wongbefc9ad2007-02-17 02:53:07 -08001790 my $replace = $ra->{url};
1791 $replace .= "/$path" if length $path;
1792
Eric Wongdb03cd22007-02-13 00:38:02 -08001793 my $section = "svn-remote.$self->{repo_id}";
Eric Wongbefc9ad2007-02-17 02:53:07 -08001794 tmp_config("$section.svm-source", $src);
1795 tmp_config("$section.svm-replace", $replace);
1796 tmp_config("$section.svm-uuid", $uuid);
1797 $self->{svm} = {
1798 source => $src,
1799 uuid => $uuid,
1800 replace => $replace
1801 };
Eric Wong8a49ee92007-02-10 20:46:50 -08001802 }
Eric Wongdb03cd22007-02-13 00:38:02 -08001803
1804 my $r = $ra->get_latest_revnum;
1805 my $path = $self->{path};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001806 my %tried;
Eric Wongdb03cd22007-02-13 00:38:02 -08001807 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001808 unless ($tried{"$self->{url}/$path"}) {
1809 return $ra if $self->read_svm_props($ra, $path, $r);
1810 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001811 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001812 $path =~ s#/?[^/]+$##;
Eric Wong8a49ee92007-02-10 20:46:50 -08001813 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001814 die "Path: '$path' should be ''\n" if $path ne '';
1815 return $ra if $self->read_svm_props($ra, $path, $r);
1816 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001817
1818 if ($ra->{repos_root} eq $self->{url}) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001819 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001820 }
1821
1822 # nope, make sure we're connected to the repository root:
1823 my $ok;
1824 my @tried_b;
1825 $path = $ra->{svn_path};
Eric Wongdb03cd22007-02-13 00:38:02 -08001826 $ra = Git::SVN::Ra->new($ra->{repos_root});
1827 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001828 unless ($tried{"$ra->{url}/$path"}) {
1829 $ok = $self->read_svm_props($ra, $path, $r);
1830 last if $ok;
1831 $tried{"$ra->{url}/$path"} = 1;
1832 }
1833 $path =~ s#/?[^/]+$##;
Eric Wongdb03cd22007-02-13 00:38:02 -08001834 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001835 die "Path: '$path' should be ''\n" if $path ne '';
1836 $ok ||= $self->read_svm_props($ra, $path, $r);
1837 $tried{"$ra->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001838 if (!$ok) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001839 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001840 }
1841 Git::SVN::Ra->new($self->{url});
Eric Wong8a49ee92007-02-10 20:46:50 -08001842}
1843
Eric Wong62e349d2007-02-16 19:57:29 -08001844sub svnsync {
1845 my ($self) = @_;
1846 return $self->{svnsync} if $self->{svnsync};
1847
1848 if ($self->no_metadata) {
1849 die "Can't have both 'noMetadata' and ",
1850 "'useSvnsyncProps' options set!\n";
1851 }
1852 if ($self->rewrite_root) {
1853 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1854 "options set!\n";
1855 }
1856
1857 my $svnsync;
1858 # see if we have it in our config, first:
1859 eval {
1860 my $section = "svn-remote.$self->{repo_id}";
Eric Wong98fa5b62008-01-11 23:13:55 -08001861
1862 my $url = tmp_config('--get', "$section.svnsync-url");
1863 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
1864 die "doesn't look right - svn:sync-from-url is '$url'\n";
1865
1866 my $uuid = tmp_config('--get', "$section.svnsync-uuid");
1867 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
1868 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1869
1870 $svnsync = { url => $url, uuid => $uuid }
Eric Wong62e349d2007-02-16 19:57:29 -08001871 };
1872 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
1873 return $self->{svnsync} = $svnsync;
1874 }
1875
1876 my $err = "useSvnsyncProps set, but failed to read " .
1877 "svnsync property: svn:sync-from-";
1878 my $rp = $self->ra->rev_proplist(0);
1879
1880 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
Eric Wong98fa5b62008-01-11 23:13:55 -08001881 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
Eric Wong62e349d2007-02-16 19:57:29 -08001882 die "doesn't look right - svn:sync-from-url is '$url'\n";
1883
1884 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
Eric Wong98fa5b62008-01-11 23:13:55 -08001885 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
Eric Wong62e349d2007-02-16 19:57:29 -08001886 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1887
1888 my $section = "svn-remote.$self->{repo_id}";
1889 tmp_config('--add', "$section.svnsync-uuid", $uuid);
1890 tmp_config('--add', "$section.svnsync-url", $url);
1891 return $self->{svnsync} = { url => $url, uuid => $uuid };
1892}
1893
Eric Wong26a62d52007-02-12 13:25:25 -08001894# this allows us to memoize our SVN::Ra UUID locally and avoid a
1895# remote lookup (useful for 'git svn log').
1896sub ra_uuid {
1897 my ($self) = @_;
1898 unless ($self->{ra_uuid}) {
1899 my $key = "svn-remote.$self->{repo_id}.uuid";
1900 my $uuid = eval { tmp_config('--get', $key) };
1901 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
1902 $self->{ra_uuid} = $uuid;
1903 } else {
1904 die "ra_uuid called without URL\n" unless $self->{url};
1905 $self->{ra_uuid} = $self->ra->get_uuid;
1906 tmp_config('--add', $key, $self->{ra_uuid});
1907 }
1908 }
1909 $self->{ra_uuid};
1910}
1911
Eric Wonga5460eb2007-11-21 18:20:57 -08001912sub _set_repos_root {
1913 my ($self, $repos_root) = @_;
1914 my $k = "svn-remote.$self->{repo_id}.reposRoot";
1915 $repos_root ||= $self->ra->{repos_root};
1916 tmp_config($k, $repos_root);
1917 $repos_root;
1918}
1919
1920sub repos_root {
1921 my ($self) = @_;
1922 my $k = "svn-remote.$self->{repo_id}.reposRoot";
1923 eval { tmp_config('--get', $k) } || $self->_set_repos_root;
1924}
1925
Eric Wong9b981fc2007-01-11 12:14:21 -08001926sub ra {
1927 my ($self) = shift;
Eric Wong8a49ee92007-02-10 20:46:50 -08001928 my $ra = Git::SVN::Ra->new($self->{url});
Eric Wonga5460eb2007-11-21 18:20:57 -08001929 $self->_set_repos_root($ra->{repos_root});
Eric Wong91b03282007-02-11 00:51:33 -08001930 if ($self->use_svm_props && !$self->{svm}) {
1931 if ($self->no_metadata) {
Eric Wong97ae0912007-02-11 15:21:24 -08001932 die "Can't have both 'noMetadata' and ",
1933 "'useSvmProps' options set!\n";
Eric Wong62e349d2007-02-16 19:57:29 -08001934 } elsif ($self->use_svnsync_props) {
1935 die "Can't have both 'useSvnsyncProps' and ",
1936 "'useSvmProps' options set!\n";
Eric Wong91b03282007-02-11 00:51:33 -08001937 }
Eric Wong26a62d52007-02-12 13:25:25 -08001938 $ra = $self->_set_svm_vars($ra);
Eric Wong8a49ee92007-02-10 20:46:50 -08001939 $self->{-want_revprops} = 1;
1940 }
1941 $ra;
Eric Wong9b981fc2007-01-11 12:14:21 -08001942}
1943
Eric Wong15710b62007-01-22 02:20:33 -08001944sub rel_path {
1945 my ($self) = @_;
1946 my $repos_root = $self->ra->{repos_root};
1947 return $self->{path} if ($self->{url} eq $repos_root);
Eric Wong0b594512007-03-25 16:35:31 -07001948 my $url = $self->{url} .
1949 (length $self->{path} ? "/$self->{path}" : $self->{path});
1950 $url =~ s!^\Q$repos_root\E(?:/+|$)!!g;
1951 $url;
Eric Wong15710b62007-01-22 02:20:33 -08001952}
1953
Benoit Sigoure01bdab82007-10-16 16:36:48 +02001954# prop_walk(PATH, REV, SUB)
1955# -------------------------
1956# Recursively traverse PATH at revision REV and invoke SUB for each
1957# directory that contains a SVN property. SUB will be invoked as
1958# follows: &SUB(gs, path, props); where `gs' is this instance of
1959# Git::SVN, `path' the path to the directory where the properties
1960# `props' were found. The `path' will be relative to point of checkout,
1961# that is, if url://repo/trunk is the current Git branch, and that
1962# directory contains a sub-directory `d', SUB will be invoked with `/d/'
1963# as `path' (note the trailing `/').
1964sub prop_walk {
1965 my ($self, $path, $rev, $sub) = @_;
1966
Kevin Ballard35cda062008-01-09 01:37:20 -05001967 $path =~ s#^/##;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02001968 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
1969 $path =~ s#^/*#/#g;
Eric Wong9b981fc2007-01-11 12:14:21 -08001970 my $p = $path;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02001971 # Strip the irrelevant part of the path.
1972 $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
1973 # Ensure the path is terminated by a `/'.
1974 $p =~ s#/*$#/#;
1975
1976 # The properties contain all the internal SVN stuff nobody
1977 # (usually) cares about.
1978 my $interesting_props = 0;
1979 foreach (keys %{$props}) {
1980 # If it doesn't start with `svn:', it must be a
1981 # user-defined property.
1982 ++$interesting_props and next if $_ !~ /^svn:/;
1983 # FIXME: Fragile, if SVN adds new public properties,
1984 # this needs to be updated.
1985 ++$interesting_props if /^svn:(?:ignore|keywords|executable
1986 |eol-style|mime-type
1987 |externals|needs-lock)$/x;
Eric Wong9b981fc2007-01-11 12:14:21 -08001988 }
Benoit Sigoure01bdab82007-10-16 16:36:48 +02001989 &$sub($self, $p, $props) if $interesting_props;
1990
Eric Wong9b981fc2007-01-11 12:14:21 -08001991 foreach (sort keys %$dirent) {
Eric Wong0dc03d62007-05-13 01:04:43 -07001992 next if $dirent->{$_}->{kind} != $SVN::Node::dir;
Christian Engwerb7166cc2008-05-27 08:46:55 +00001993 $self->prop_walk($self->{path} . $p . $_, $rev, $sub);
Eric Wong9b981fc2007-01-11 12:14:21 -08001994 }
1995}
1996
Eric Wong3ebe8df2007-01-25 17:35:40 -08001997sub last_rev { ($_[0]->last_rev_commit)[0] }
1998sub last_commit { ($_[0]->last_rev_commit)[1] }
1999
Eric Wong9b981fc2007-01-11 12:14:21 -08002000# returns the newest SVN revision number and newest commit SHA1
2001sub last_rev_commit {
2002 my ($self) = @_;
2003 if (defined $self->{last_rev} && defined $self->{last_commit}) {
2004 return ($self->{last_rev}, $self->{last_commit});
2005 }
Eric Wongd2866f92007-01-11 12:26:16 -08002006 my $c = ::verify_ref($self->refname.'^0');
Eric Wong91b03282007-02-11 00:51:33 -08002007 if ($c && !$self->use_svm_props && !$self->no_metadata) {
Eric Wongd2866f92007-01-11 12:26:16 -08002008 my $rev = (::cmt_metadata($c))[1];
Eric Wong9b981fc2007-01-11 12:14:21 -08002009 if (defined $rev) {
2010 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2011 return ($rev, $c);
2012 }
2013 }
Eric Wong060610c2007-12-08 23:27:41 -08002014 my $map_path = $self->map_path;
2015 unless (-e $map_path) {
Eric Wong26a62d52007-02-12 13:25:25 -08002016 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
2017 return (undef, undef);
2018 }
Eric Wong66ab84b2007-12-08 23:27:42 -08002019 my ($rev, $commit) = $self->rev_map_max(1);
Eric Wong060610c2007-12-08 23:27:41 -08002020 ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
2021 return ($rev, $commit);
Eric Wong9b981fc2007-01-11 12:14:21 -08002022}
2023
Eric Wong3ebe8df2007-01-25 17:35:40 -08002024sub get_fetch_range {
2025 my ($self, $min, $max) = @_;
2026 $max ||= $self->ra->get_latest_revnum;
Eric Wong060610c2007-12-08 23:27:41 -08002027 $min ||= $self->rev_map_max;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002028 (++$min, $max);
Eric Wong9b981fc2007-01-11 12:14:21 -08002029}
2030
Eric Wong8a49ee92007-02-10 20:46:50 -08002031sub tmp_config {
Eric Wong93f26892007-02-11 01:20:26 -08002032 my (@args) = @_;
Eric Wongb7e53482007-02-16 04:09:28 -08002033 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
2034 my $config = "$ENV{GIT_DIR}/svn/.metadata";
Eric Wong38570a42007-06-13 02:37:05 -07002035 if (! -f $config && -f $old_def_config) {
Eric Wongb7e53482007-02-16 04:09:28 -08002036 rename $old_def_config, $config or
2037 die "Failed rename $old_def_config => $config: $!\n";
2038 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002039 my $old_config = $ENV{GIT_CONFIG};
Eric Wong93f26892007-02-11 01:20:26 -08002040 $ENV{GIT_CONFIG} = $config;
Eric Wong8a49ee92007-02-10 20:46:50 -08002041 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002042 my @ret = eval {
2043 unless (-f $config) {
2044 mkfile($config);
2045 open my $fh, '>', $config or
2046 die "Can't open $config: $!\n";
2047 print $fh "; This file is used internally by ",
2048 "git-svn\n" or die
2049 "Couldn't write to $config: $!\n";
2050 print $fh "; You should not have to edit it\n" or
2051 die "Couldn't write to $config: $!\n";
2052 close $fh or die "Couldn't close $config: $!\n";
2053 }
2054 command('config', @args);
2055 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002056 my $err = $@;
2057 if (defined $old_config) {
2058 $ENV{GIT_CONFIG} = $old_config;
2059 } else {
2060 delete $ENV{GIT_CONFIG};
2061 }
2062 die $err if $err;
2063 wantarray ? @ret : $ret[0];
2064}
2065
Eric Wong9b981fc2007-01-11 12:14:21 -08002066sub tmp_index_do {
2067 my ($self, $sub) = @_;
2068 my $old_index = $ENV{GIT_INDEX_FILE};
2069 $ENV{GIT_INDEX_FILE} = $self->{index};
Eric Wong8a49ee92007-02-10 20:46:50 -08002070 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002071 my @ret = eval {
2072 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
2073 mkpath([$dir]) unless -d $dir;
2074 &$sub;
2075 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002076 my $err = $@;
2077 if (defined $old_index) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002078 $ENV{GIT_INDEX_FILE} = $old_index;
2079 } else {
2080 delete $ENV{GIT_INDEX_FILE};
2081 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002082 die $err if $err;
Eric Wong9b981fc2007-01-11 12:14:21 -08002083 wantarray ? @ret : $ret[0];
2084}
2085
2086sub assert_index_clean {
2087 my ($self, $treeish) = @_;
2088
2089 $self->tmp_index_do(sub {
2090 command_noisy('read-tree', $treeish) unless -e $self->{index};
2091 my $x = command_oneline('write-tree');
2092 my ($y) = (command(qw/cat-file commit/, $treeish) =~
2093 /^tree ($::sha1)/mo);
Eric Wonge8d120b2007-02-14 16:29:52 -08002094 return if $y eq $x;
2095
2096 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2097 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2098 command_noisy('read-tree', $treeish);
Eric Wong9b981fc2007-01-11 12:14:21 -08002099 $x = command_oneline('write-tree');
2100 if ($y ne $x) {
2101 ::fatal "trees ($treeish) $y != $x\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02002102 "Something is seriously wrong...";
Eric Wong9b981fc2007-01-11 12:14:21 -08002103 }
2104 });
2105}
2106
2107sub get_commit_parents {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002108 my ($self, $log_entry) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002109 my (%seen, @ret, @tmp);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002110 # legacy support for 'set-tree'; this is only used by set_tree_cb:
2111 if (my $ip = $self->{inject_parents}) {
2112 if (my $commit = delete $ip->{$log_entry->{revision}}) {
2113 push @tmp, $commit;
Eric Wong9b981fc2007-01-11 12:14:21 -08002114 }
2115 }
Eric Wongd2866f92007-01-11 12:26:16 -08002116 if (my $cur = ::verify_ref($self->refname.'^0')) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002117 push @tmp, $cur;
2118 }
Eric Wong733a65a2007-06-13 02:23:28 -07002119 if (my $ipd = $self->{inject_parents_dcommit}) {
2120 if (my $commit = delete $ipd->{$log_entry->{revision}}) {
2121 push @tmp, @$commit;
2122 }
2123 }
Eric Wong44320b92007-01-13 22:35:53 -08002124 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
Eric Wong9b981fc2007-01-11 12:14:21 -08002125 while (my $p = shift @tmp) {
2126 next if $seen{$p};
2127 $seen{$p} = 1;
2128 push @ret, $p;
2129 # MAXPARENT is defined to 16 in commit-tree.c:
2130 last if @ret >= 16;
2131 }
2132 if (@tmp) {
Eric Wong44320b92007-01-13 22:35:53 -08002133 die "r$log_entry->{revision}: No room for parents:\n\t",
Eric Wong9b981fc2007-01-11 12:14:21 -08002134 join("\n\t", @tmp), "\n";
2135 }
2136 @ret;
2137}
2138
Eric Wongaea736c2007-02-16 19:15:21 -08002139sub rewrite_root {
2140 my ($self) = @_;
2141 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
2142 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2143 my $rwr = eval { command_oneline(qw/config --get/, $k) };
2144 if ($rwr) {
2145 $rwr =~ s#/+$##;
2146 if ($rwr !~ m#^[a-z\+]+://#) {
2147 die "$rwr is not a valid URL (key: $k)\n";
2148 }
2149 }
2150 $self->{-rewrite_root} = $rwr;
2151}
2152
2153sub metadata_url {
2154 my ($self) = @_;
2155 ($self->rewrite_root || $self->{url}) .
2156 (length $self->{path} ? '/' . $self->{path} : '');
2157}
2158
Eric Wong706587f2007-01-18 17:50:01 -08002159sub full_url {
Eric Wong9b981fc2007-01-11 12:14:21 -08002160 my ($self) = @_;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002161 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
Eric Wong9b981fc2007-01-11 12:14:21 -08002162}
2163
Eric Wongad948022007-12-15 19:08:22 -08002164
2165sub set_commit_header_env {
2166 my ($log_entry) = @_;
2167 my %env;
2168 foreach my $ned (qw/NAME EMAIL DATE/) {
2169 foreach my $ac (qw/AUTHOR COMMITTER/) {
2170 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2171 }
2172 }
2173
2174 $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
2175 $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
2176 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
2177
2178 $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
2179 ? $log_entry->{commit_name}
2180 : $log_entry->{name};
2181 $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
2182 ? $log_entry->{commit_email}
2183 : $log_entry->{email};
2184 \%env;
2185}
2186
2187sub restore_commit_header_env {
2188 my ($env) = @_;
2189 foreach my $ned (qw/NAME EMAIL DATE/) {
2190 foreach my $ac (qw/AUTHOR COMMITTER/) {
2191 my $k = "GIT_${ac}_${ned}";
2192 if (defined $env->{$k}) {
2193 $ENV{$k} = $env->{$k};
2194 } else {
2195 delete $ENV{$k};
2196 }
2197 }
2198 }
2199}
2200
Karl Hasselström94bc9142008-02-03 17:56:18 +01002201sub gc {
2202 command_noisy('gc', '--auto');
2203};
2204
Eric Wong9b981fc2007-01-11 12:14:21 -08002205sub do_git_commit {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002206 my ($self, $log_entry) = @_;
Eric Wong8a603772007-01-31 02:45:50 -08002207 my $lr = $self->last_rev;
2208 if (defined $lr && $lr >= $log_entry->{revision}) {
2209 die "Last fetched revision of ", $self->refname,
2210 " was r$lr, but we are about to fetch: ",
2211 "r$log_entry->{revision}!\n";
2212 }
Eric Wong060610c2007-12-08 23:27:41 -08002213 if (my $c = $self->rev_map_get($log_entry->{revision})) {
Eric Wong44320b92007-01-13 22:35:53 -08002214 croak "$log_entry->{revision} = $c already exists! ",
Eric Wong9b981fc2007-01-11 12:14:21 -08002215 "Why are we refetching it?\n";
2216 }
Eric Wongad948022007-12-15 19:08:22 -08002217 my $old_env = set_commit_header_env($log_entry);
Eric Wong44320b92007-01-13 22:35:53 -08002218 my $tree = $log_entry->{tree};
Eric Wong9b981fc2007-01-11 12:14:21 -08002219 if (!defined $tree) {
2220 $tree = $self->tmp_index_do(sub {
2221 command_oneline('write-tree') });
2222 }
2223 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2224
2225 my @exec = ('git-commit-tree', $tree);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002226 foreach ($self->get_commit_parents($log_entry)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002227 push @exec, '-p', $_;
2228 }
2229 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2230 or croak $!;
Eric Wong44320b92007-01-13 22:35:53 -08002231 print $msg_fh $log_entry->{log} or croak $!;
Eric Wongad948022007-12-15 19:08:22 -08002232 restore_commit_header_env($old_env);
Eric Wong91b03282007-02-11 00:51:33 -08002233 unless ($self->no_metadata) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002234 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2235 or croak $!;
Eric Wong9760adc2007-01-31 03:06:56 -08002236 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002237 $msg_fh->flush == 0 or croak $!;
2238 close $msg_fh or croak $!;
2239 chomp(my $commit = do { local $/; <$out_fh> });
2240 close $out_fh or croak $!;
2241 waitpid $pid, 0;
2242 croak $? if $?;
2243 if ($commit !~ /^$::sha1$/o) {
2244 die "Failed to commit, invalid sha1: $commit\n";
2245 }
2246
Eric Wong060610c2007-12-08 23:27:41 -08002247 $self->rev_map_set($log_entry->{revision}, $commit, 1);
Eric Wong9b981fc2007-01-11 12:14:21 -08002248
Eric Wong44320b92007-01-13 22:35:53 -08002249 $self->{last_rev} = $log_entry->{revision};
Eric Wong9b981fc2007-01-11 12:14:21 -08002250 $self->{last_commit} = $commit;
Eric Wong8a49ee92007-02-10 20:46:50 -08002251 print "r$log_entry->{revision}";
2252 if (defined $log_entry->{svm_revision}) {
2253 print " (\@$log_entry->{svm_revision})";
Eric Wong060610c2007-12-08 23:27:41 -08002254 $self->rev_map_set($log_entry->{svm_revision}, $commit,
Eric Wong26a62d52007-02-12 13:25:25 -08002255 0, $self->svm_uuid);
Eric Wong8a49ee92007-02-10 20:46:50 -08002256 }
2257 print " = $commit ($self->{ref_id})\n";
Karl Hasselström94bc9142008-02-03 17:56:18 +01002258 if (--$_gc_nr == 0) {
2259 $_gc_nr = $_gc_period;
2260 gc();
2261 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002262 return $commit;
2263}
2264
Eric Wongfbcc1732007-02-06 18:35:30 -08002265sub match_paths {
2266 my ($self, $paths, $r) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002267 return 1 if $self->{path} eq '';
Eric Wongd542aed2007-02-09 02:19:41 -08002268 if (my $path = $paths->{"/$self->{path}"}) {
2269 return ($path->{action} eq 'D') ? 0 : 1;
2270 }
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002271 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
Eric Wongfbcc1732007-02-06 18:35:30 -08002272 if (grep /$self->{path_regex}/, keys %$paths) {
2273 return 1;
2274 }
2275 my $c = '';
2276 foreach (split m#/#, $self->{path}) {
2277 $c .= "/$_";
Eric Wong74a81222007-02-10 13:28:50 -08002278 next unless ($paths->{$c} &&
2279 ($paths->{$c}->{action} =~ /^[AR]$/));
Eric Wonge5181922007-02-08 12:53:57 -08002280 if ($self->ra->check_path($self->{path}, $r) ==
2281 $SVN::Node::dir) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002282 return 1;
2283 }
2284 }
2285 return 0;
2286}
2287
Eric Wong15710b62007-01-22 02:20:33 -08002288sub find_parent_branch {
2289 my ($self, $paths, $rev) = @_;
Eric Wong91b03282007-02-11 00:51:33 -08002290 return undef unless $self->follow_parent;
Eric Wonge5a0b242007-01-25 15:44:54 -08002291 unless (defined $paths) {
Eric Wongc7eba712007-01-31 03:45:28 -08002292 my $err_handler = $SVN::Error::handler;
2293 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
Eric Wongd4eff2b2007-01-30 14:04:22 -08002294 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, sub {
2295 $paths =
2296 Git::SVN::Ra::dup_changed_paths($_[0]) });
Eric Wongc7eba712007-01-31 03:45:28 -08002297 $SVN::Error::handler = $err_handler;
Eric Wonge5a0b242007-01-25 15:44:54 -08002298 }
2299 return undef unless defined $paths;
Eric Wong15710b62007-01-22 02:20:33 -08002300
2301 # look for a parent from another branch:
Eric Wong7f578c52007-01-24 02:16:25 -08002302 my @b_path_components = split m#/#, $self->rel_path;
2303 my @a_path_components;
2304 my $i;
2305 while (@b_path_components) {
2306 $i = $paths->{'/'.join('/', @b_path_components)};
Eric Wong74a81222007-02-10 13:28:50 -08002307 last if $i && defined $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002308 unshift(@a_path_components, pop(@b_path_components));
2309 }
Eric Wong74a81222007-02-10 13:28:50 -08002310 return undef unless defined $i && defined $i->{copyfrom_path};
2311 my $branch_from = $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002312 if (@a_path_components) {
2313 print STDERR "branch_from: $branch_from => ";
2314 $branch_from .= '/'.join('/', @a_path_components);
2315 print STDERR $branch_from, "\n";
2316 }
Eric Wong3ebe8df2007-01-25 17:35:40 -08002317 my $r = $i->{copyfrom_rev};
Eric Wong15710b62007-01-22 02:20:33 -08002318 my $repos_root = $self->ra->{repos_root};
2319 my $url = $self->ra->{url};
2320 my $new_url = $repos_root . $branch_from;
2321 print STDERR "Found possible branch point: ",
2322 "$new_url => ", $self->full_url, ", $r\n";
2323 $branch_from =~ s#^/##;
Eric Wonga8ae2622007-02-13 14:22:11 -08002324 my $gs = Git::SVN->find_by_url($new_url, $repos_root, $branch_from);
Eric Wong15710b62007-01-22 02:20:33 -08002325 unless ($gs) {
Eric Wongce2a0f22007-01-28 21:34:23 -08002326 my $ref_id = $self->{ref_id};
2327 $ref_id =~ s/\@\d+$//;
2328 $ref_id .= "\@$r";
Eric Wong15710b62007-01-22 02:20:33 -08002329 # just grow a tail if we're not unique enough :x
2330 $ref_id .= '-' while find_ref($ref_id);
Eric Wongce2a0f22007-01-28 21:34:23 -08002331 print STDERR "Initializing parent: $ref_id\n";
Eric Wongf61cc482008-04-07 00:08:03 -07002332 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
Eric Wongc5868792008-01-29 01:18:58 -08002333 if ($u =~ s#^\Q$url\E(/|$)##) {
2334 $p = $u;
2335 $u = $url;
Eric Wongf61cc482008-04-07 00:08:03 -07002336 $repo_id = $self->{repo_id};
Eric Wongc5868792008-01-29 01:18:58 -08002337 }
Eric Wongf61cc482008-04-07 00:08:03 -07002338 $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1);
Eric Wong15710b62007-01-22 02:20:33 -08002339 }
2340 my ($r0, $parent) = $gs->find_rev_before($r, 1);
Eric Wong91b03282007-02-11 00:51:33 -08002341 if (!defined $r0 || !defined $parent) {
Eric Wongd627de62007-04-15 03:01:29 -07002342 my ($base, $head) = parse_revision_argument(0, $r);
2343 if ($base <= $r) {
2344 $gs->fetch($base, $r);
2345 }
Eric Wong15710b62007-01-22 02:20:33 -08002346 ($r0, $parent) = $gs->last_rev_commit;
2347 }
Eric Wongef70de92007-02-01 04:12:41 -08002348 if (defined $r0 && defined $parent) {
Eric Wong15710b62007-01-22 02:20:33 -08002349 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n";
Eric Wong15710b62007-01-22 02:20:33 -08002350 my $ed;
2351 if ($self->ra->can_do_switch) {
Eric Wong2e5e2482007-02-23 02:21:59 -08002352 $self->assert_index_clean($parent);
Eric Wong8b8fc062007-01-22 11:44:57 -08002353 print STDERR "Following parent with do_switch\n";
Eric Wong15710b62007-01-22 02:20:33 -08002354 # do_switch works with svn/trunk >= r22312, but that
Eric Wong2b27f6c2007-01-28 04:59:05 -08002355 # is not included with SVN 1.4.3 (the latest version
Eric Wong15710b62007-01-22 02:20:33 -08002356 # at the moment), so we can't rely on it
2357 $self->{last_commit} = $parent;
2358 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08002359 $gs->ra->gs_do_switch($r0, $rev, $gs,
Eric Wong15710b62007-01-22 02:20:33 -08002360 $self->full_url, $ed)
2361 or die "SVN connection failed somewhere...\n";
Steven Walter9ff74e92007-09-28 13:24:19 -04002362 } elsif ($self->ra->trees_match($new_url, $r0,
2363 $self->full_url, $rev)) {
2364 print STDERR "Trees match:\n",
2365 " $new_url\@$r0\n",
2366 " ${\$self->full_url}\@$rev\n",
2367 "Following parent with no changes\n";
2368 $self->tmp_index_do(sub {
2369 command_noisy('read-tree', $parent);
2370 });
2371 $self->{last_commit} = $parent;
Eric Wong15710b62007-01-22 02:20:33 -08002372 } else {
Eric Wong8b8fc062007-01-22 11:44:57 -08002373 print STDERR "Following parent with do_update\n";
Eric Wong15710b62007-01-22 02:20:33 -08002374 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08002375 $self->ra->gs_do_update($rev, $rev, $self, $ed)
Eric Wong15710b62007-01-22 02:20:33 -08002376 or die "SVN connection failed somewhere...\n";
2377 }
Eric Wongf7c3fc42007-01-29 18:34:55 -08002378 print STDERR "Successfully followed parent\n";
Eric Wong15710b62007-01-22 02:20:33 -08002379 return $self->make_log_entry($rev, [$parent], $ed);
2380 }
Eric Wong15710b62007-01-22 02:20:33 -08002381 return undef;
2382}
2383
Eric Wong9b981fc2007-01-11 12:14:21 -08002384sub do_fetch {
Eric Wong706587f2007-01-18 17:50:01 -08002385 my ($self, $paths, $rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08002386 my $ed;
Eric Wong9b981fc2007-01-11 12:14:21 -08002387 my ($last_rev, @parents);
Eric Wongb9dffd82007-02-09 01:28:30 -08002388 if (my $lc = $self->last_commit) {
2389 # we can have a branch that was deleted, then re-added
2390 # under the same name but copied from another path, in
2391 # which case we'll have multiple parents (we don't
2392 # want to break the original ref, nor lose copypath info):
2393 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2394 push @{$log_entry->{parents}}, $lc;
2395 return $log_entry;
2396 }
Eric Wong15710b62007-01-22 02:20:33 -08002397 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002398 $last_rev = $self->{last_rev};
Eric Wongb9dffd82007-02-09 01:28:30 -08002399 $ed->{c} = $lc;
2400 @parents = ($lc);
Eric Wong9b981fc2007-01-11 12:14:21 -08002401 } else {
2402 $last_rev = $rev;
Eric Wong15710b62007-01-22 02:20:33 -08002403 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2404 return $log_entry;
2405 }
2406 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002407 }
Eric Wong8a603772007-01-31 02:45:50 -08002408 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002409 die "SVN connection failed somewhere...\n";
2410 }
2411 $self->make_log_entry($rev, \@parents, $ed);
2412}
2413
Eric Wong97f69872007-01-25 11:53:13 -08002414sub get_untracked {
2415 my ($self, $ed) = @_;
2416 my @out;
2417 my $h = $ed->{empty};
Eric Wong9b981fc2007-01-11 12:14:21 -08002418 foreach (sort keys %$h) {
2419 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
Eric Wong97f69872007-01-25 11:53:13 -08002420 push @out, " $act: " . uri_encode($_);
Eric Wong9b981fc2007-01-11 12:14:21 -08002421 warn "W: $act: $_\n";
2422 }
2423 foreach my $t (qw/dir_prop file_prop/) {
Eric Wong97f69872007-01-25 11:53:13 -08002424 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002425 foreach my $path (sort keys %$h) {
2426 my $ppath = $path eq '' ? '.' : $path;
2427 foreach my $prop (sort keys %{$h->{$path}}) {
Eric Wong1ce255d2007-01-14 23:21:16 -08002428 next if $SKIP_PROP{$prop};
Eric Wong9b981fc2007-01-11 12:14:21 -08002429 my $v = $h->{$path}->{$prop};
Eric Wong97f69872007-01-25 11:53:13 -08002430 my $t_ppath_prop = "$t: " .
2431 uri_encode($ppath) . ' ' .
2432 uri_encode($prop);
Eric Wong9b981fc2007-01-11 12:14:21 -08002433 if (defined $v) {
Eric Wong97f69872007-01-25 11:53:13 -08002434 push @out, " +$t_ppath_prop " .
2435 uri_encode($v);
Eric Wong9b981fc2007-01-11 12:14:21 -08002436 } else {
Eric Wong97f69872007-01-25 11:53:13 -08002437 push @out, " -$t_ppath_prop";
Eric Wong9b981fc2007-01-11 12:14:21 -08002438 }
2439 }
2440 }
2441 }
2442 foreach my $t (qw/absent_file absent_directory/) {
Eric Wong97f69872007-01-25 11:53:13 -08002443 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002444 foreach my $parent (sort keys %$h) {
2445 foreach my $path (sort @{$h->{$parent}}) {
Eric Wong97f69872007-01-25 11:53:13 -08002446 push @out, " $t: " .
2447 uri_encode("$parent/$path");
Eric Wong9b981fc2007-01-11 12:14:21 -08002448 warn "W: $t: $parent/$path ",
2449 "Insufficient permissions?\n";
2450 }
2451 }
2452 }
Eric Wong97f69872007-01-25 11:53:13 -08002453 \@out;
Eric Wong9b981fc2007-01-11 12:14:21 -08002454}
2455
Eric Wong1c8443b2007-01-14 02:17:00 -08002456sub parse_svn_date {
2457 my $date = shift || return '+0000 1970-01-01 00:00:00';
2458 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
2459 (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x) or
2460 croak "Unable to parse date: $date\n";
2461 "+0000 $Y-$m-$d $H:$M:$S";
2462}
2463
2464sub check_author {
2465 my ($author) = @_;
2466 if (!defined $author || length $author == 0) {
2467 $author = '(no author)';
Thomas Guyot-Sionnest9231f502008-04-22 06:07:47 -04002468 } elsif (defined $::_authors && ! defined $::users{$author}) {
Eric Wong1c8443b2007-01-14 02:17:00 -08002469 die "Author: $author not defined in $::_authors file\n";
2470 }
2471 $author;
2472}
2473
Eric Wong9b981fc2007-01-11 12:14:21 -08002474sub make_log_entry {
Eric Wong97f69872007-01-25 11:53:13 -08002475 my ($self, $rev, $parents, $ed) = @_;
2476 my $untracked = $self->get_untracked($ed);
2477
Eric Wong9b981fc2007-01-11 12:14:21 -08002478 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08002479 print $un "r$rev\n" or croak $!;
2480 print $un $_, "\n" foreach @$untracked;
2481 my %log_entry = ( parents => $parents || [], revision => $rev,
2482 log => '');
Eric Wongfbcc1732007-02-06 18:35:30 -08002483
Eric Wong8a49ee92007-02-10 20:46:50 -08002484 my $headrev;
Eric Wongfbcc1732007-02-06 18:35:30 -08002485 my $logged = delete $self->{logged_rev_props};
Eric Wong8a49ee92007-02-10 20:46:50 -08002486 if (!$logged || $self->{-want_revprops}) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002487 my $rp = $self->ra->rev_proplist($rev);
2488 foreach (sort keys %$rp) {
2489 my $v = $rp->{$_};
2490 if (/^svn:(author|date|log)$/) {
2491 $log_entry{$1} = $v;
Eric Wong8a49ee92007-02-10 20:46:50 -08002492 } elsif ($_ eq 'svm:headrev') {
2493 $headrev = $v;
Eric Wongfbcc1732007-02-06 18:35:30 -08002494 } else {
2495 print $un " rev_prop: ", uri_encode($_), ' ',
2496 uri_encode($v), "\n";
2497 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002498 }
Eric Wongfbcc1732007-02-06 18:35:30 -08002499 } else {
2500 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
Eric Wong9b981fc2007-01-11 12:14:21 -08002501 }
2502 close $un or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08002503
Eric Wong9b981fc2007-01-11 12:14:21 -08002504 $log_entry{date} = parse_svn_date($log_entry{date});
Eric Wong9b981fc2007-01-11 12:14:21 -08002505 $log_entry{log} .= "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002506 my $author = $log_entry{author} = check_author($log_entry{author});
2507 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002508 : ($author, undef);
2509
2510 my ($commit_name, $commit_email) = ($name, $email);
2511 if ($_use_log_author) {
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002512 my $name_field;
2513 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
2514 $name_field = $1;
2515 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
2516 $name_field = $1;
2517 }
2518 if (!defined $name_field) {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02002519 if (!defined $email) {
2520 $email = $name;
2521 }
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002522 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002523 ($name, $email) = ($1, $2);
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002524 } elsif ($name_field =~ /(.*)@/) {
2525 ($name, $email) = ($1, $name_field);
2526 } else {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02002527 ($name, $email) = ($name_field, $name_field);
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002528 }
2529 }
Eric Wong91b03282007-02-11 00:51:33 -08002530 if (defined $headrev && $self->use_svm_props) {
Eric Wongaea736c2007-02-16 19:15:21 -08002531 if ($self->rewrite_root) {
2532 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
2533 "options set!\n";
2534 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002535 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002536 # we don't want "SVM: initializing mirror for junk" ...
2537 return undef if $r == 0;
2538 my $svm = $self->svm;
2539 if ($uuid ne $svm->{uuid}) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002540 die "UUID mismatch on SVM path:\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08002541 "expected: $svm->{uuid}\n",
Eric Wong8a49ee92007-02-10 20:46:50 -08002542 " got: $uuid\n";
2543 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002544 my $full_url = $self->full_url;
2545 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
2546 die "Failed to replace '$svm->{replace}' with ",
2547 "'$svm->{source}' in $full_url\n";
Sam Vilain18ea92b2007-02-23 12:32:29 +13002548 # throw away username for storing in records
2549 remove_username($full_url);
Eric Wong8a49ee92007-02-10 20:46:50 -08002550 $log_entry{metadata} = "$full_url\@$r $uuid";
2551 $log_entry{svm_revision} = $r;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002552 $email ||= "$author\@$uuid";
2553 $commit_email ||= "$author\@$uuid";
Eric Wong62e349d2007-02-16 19:57:29 -08002554 } elsif ($self->use_svnsync_props) {
2555 my $full_url = $self->svnsync->{url};
2556 $full_url .= "/$self->{path}" if length $self->{path};
Adam Robence118732007-04-24 18:02:07 -07002557 remove_username($full_url);
Eric Wong62e349d2007-02-16 19:57:29 -08002558 my $uuid = $self->svnsync->{uuid};
2559 $log_entry{metadata} = "$full_url\@$rev $uuid";
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002560 $email ||= "$author\@$uuid";
2561 $commit_email ||= "$author\@$uuid";
Eric Wong8a49ee92007-02-10 20:46:50 -08002562 } else {
Adam Robence118732007-04-24 18:02:07 -07002563 my $url = $self->metadata_url;
2564 remove_username($url);
2565 $log_entry{metadata} = "$url\@$rev " .
Eric Wong26a62d52007-02-12 13:25:25 -08002566 $self->ra->get_uuid;
Eric Wongdb03cd22007-02-13 00:38:02 -08002567 $email ||= "$author\@" . $self->ra->get_uuid;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002568 $commit_email ||= "$author\@" . $self->ra->get_uuid;
Eric Wong8a49ee92007-02-10 20:46:50 -08002569 }
Eric Wongdb03cd22007-02-13 00:38:02 -08002570 $log_entry{name} = $name;
2571 $log_entry{email} = $email;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002572 $log_entry{commit_name} = $commit_name;
2573 $log_entry{commit_email} = $commit_email;
Eric Wong9b981fc2007-01-11 12:14:21 -08002574 \%log_entry;
2575}
2576
2577sub fetch {
Eric Wong3ebe8df2007-01-25 17:35:40 -08002578 my ($self, $min_rev, $max_rev, @parents) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002579 my ($last_rev, $last_commit) = $self->last_rev_commit;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002580 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
Eric Wonge5181922007-02-08 12:53:57 -08002581 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
Eric Wong9b981fc2007-01-11 12:14:21 -08002582}
2583
2584sub set_tree_cb {
2585 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
Eric Wong490f49e2007-02-10 13:58:33 -08002586 $self->{inject_parents} = { $rev => $tree };
2587 $self->fetch(undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08002588}
2589
2590sub set_tree {
2591 my ($self, $tree) = (shift, shift);
Eric Wong1ce255d2007-01-14 23:21:16 -08002592 my $log_entry = ::get_commit_entry($tree);
Eric Wong9b981fc2007-01-11 12:14:21 -08002593 unless ($self->{last_rev}) {
Luc Heinrich0a1a1c82008-09-29 15:58:18 +02002594 ::fatal("Must have an existing revision to commit");
Eric Wong9b981fc2007-01-11 12:14:21 -08002595 }
Eric Wong61395352007-01-27 14:33:08 -08002596 my %ed_opts = ( r => $self->{last_rev},
2597 log => $log_entry->{log},
2598 ra => $self->ra,
2599 tree_a => $self->{last_commit},
2600 tree_b => $tree,
2601 editor_cb => sub {
2602 $self->set_tree_cb($log_entry, $tree, @_) },
2603 svn_path => $self->{path} );
2604 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002605 print "No changes\nr$self->{last_rev} = $tree\n";
2606 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002607}
2608
Eric Wong060610c2007-12-08 23:27:41 -08002609sub rebuild_from_rev_db {
2610 my ($self, $path) = @_;
2611 my $r = -1;
2612 open my $fh, '<', $path or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02002613 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002614 while (<$fh>) {
2615 length($_) == 41 or croak "inconsistent size in ($_) != 41";
2616 chomp($_);
2617 ++$r;
2618 next if $_ eq ('0' x 40);
2619 $self->rev_map_set($r, $_);
2620 print "r$r = $_\n";
2621 }
2622 close $fh or croak "close: $!";
2623 unlink $path or croak "unlink: $!";
2624}
2625
Eric Wongf0ecca12007-01-30 13:11:14 -08002626sub rebuild {
2627 my ($self) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08002628 my $map_path = $self->map_path;
Deskin Miller2beec892008-09-15 21:12:58 -04002629 my $partial = (-e $map_path && ! -z $map_path);
Eric Wongd6d33462007-02-16 04:05:33 -08002630 return unless ::verify_ref($self->refname.'^0');
Deskin Miller2beec892008-09-15 21:12:58 -04002631 if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
Eric Wong060610c2007-12-08 23:27:41 -08002632 my $rev_db = $self->rev_db_path;
2633 $self->rebuild_from_rev_db($rev_db);
2634 if ($self->use_svm_props) {
2635 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
2636 $self->rebuild_from_rev_db($svm_rev_db);
2637 }
2638 $self->unlink_rev_db_symlink;
Eric Wong26a62d52007-02-12 13:25:25 -08002639 return;
2640 }
Deskin Miller2beec892008-09-15 21:12:58 -04002641 print "Rebuilding $map_path ...\n" if (!$partial);
2642 my ($base_rev, $head) = ($partial ? $self->rev_map_max_norebuild(1) :
2643 (undef, undef));
Eric Wong060610c2007-12-08 23:27:41 -08002644 my ($log, $ctx) =
2645 command_output_pipe(qw/rev-list --pretty=raw --no-color --reverse/,
Deskin Miller2beec892008-09-15 21:12:58 -04002646 ($head ? "$head.." : "") . $self->refname,
2647 '--');
Jan Krüger74b1e122008-06-24 02:17:36 +02002648 my $metadata_url = $self->metadata_url;
2649 remove_username($metadata_url);
Eric Wong060610c2007-12-08 23:27:41 -08002650 my $svn_uuid = $self->ra_uuid;
Sam Vilain3dfab992007-06-30 20:56:13 +12002651 my $c;
2652 while (<$log>) {
2653 if ( m{^commit ($::sha1)$} ) {
2654 $c = $1;
2655 next;
2656 }
2657 next unless s{^\s*(git-svn-id:)}{$1};
2658 my ($url, $rev, $uuid) = ::extract_metadata($_);
Sam Vilain18ea92b2007-02-23 12:32:29 +13002659 remove_username($url);
Eric Wongf0ecca12007-01-30 13:11:14 -08002660
2661 # ignore merges (from set-tree)
2662 next if (!defined $rev || !$uuid);
2663
2664 # if we merged or otherwise started elsewhere, this is
2665 # how we break out of it
Eric Wong060610c2007-12-08 23:27:41 -08002666 if (($uuid ne $svn_uuid) ||
Jan Krüger74b1e122008-06-24 02:17:36 +02002667 ($metadata_url && $url && ($url ne $metadata_url))) {
Eric Wongf0ecca12007-01-30 13:11:14 -08002668 next;
2669 }
Deskin Miller2beec892008-09-15 21:12:58 -04002670 if ($partial && $head) {
2671 print "Partial-rebuilding $map_path ...\n";
2672 print "Currently at $base_rev = $head\n";
2673 $head = undef;
2674 }
Eric Wongf0ecca12007-01-30 13:11:14 -08002675
Eric Wong060610c2007-12-08 23:27:41 -08002676 $self->rev_map_set($rev, $c);
Eric Wongf0ecca12007-01-30 13:11:14 -08002677 print "r$rev = $c\n";
2678 }
Sam Vilain3dfab992007-06-30 20:56:13 +12002679 command_close_pipe($log, $ctx);
Deskin Miller2beec892008-09-15 21:12:58 -04002680 print "Done rebuilding $map_path\n" if (!$partial || !$head);
Eric Wong060610c2007-12-08 23:27:41 -08002681 my $rev_db_path = $self->rev_db_path;
2682 if (-f $self->rev_db_path) {
2683 unlink $self->rev_db_path or croak "unlink: $!";
2684 }
2685 $self->unlink_rev_db_symlink;
Eric Wongf0ecca12007-01-30 13:11:14 -08002686}
2687
Eric Wong060610c2007-12-08 23:27:41 -08002688# rev_map:
Eric Wong9b981fc2007-01-11 12:14:21 -08002689# Tie::File seems to be prone to offset errors if revisions get sparse,
2690# it's not that fast, either. Tie::File is also not in Perl 5.6. So
2691# one of my favorite modules is out :< Next up would be one of the DBM
Eric Wong060610c2007-12-08 23:27:41 -08002692# modules, but I'm not sure which is most portable...
2693#
2694# This is the replacement for the rev_db format, which was too big
2695# and inefficient for large repositories with a lot of sparse history
2696# (mainly tags)
2697#
2698# The format is this:
2699# - 24 bytes for every record,
2700# * 4 bytes for the integer representing an SVN revision number
2701# * 20 bytes representing the sha1 of a git commit
2702# - No empty padding records like the old format
Eric Wong66ab84b2007-12-08 23:27:42 -08002703# (except the last record, which can be overwritten)
Eric Wong060610c2007-12-08 23:27:41 -08002704# - new records are written append-only since SVN revision numbers
2705# increase monotonically
2706# - lookups on SVN revision number are done via a binary search
Eric Wong66ab84b2007-12-08 23:27:42 -08002707# - Piping the file to xxd -c24 is a good way of dumping it for
2708# viewing or editing (piped back through xxd -r), should the need
2709# ever arise.
2710# - The last record can be padding revision with an all-zero sha1
2711# This is used to optimize fetch performance when using multiple
2712# "fetch" directives in .git/config
Eric Wong060610c2007-12-08 23:27:41 -08002713#
Eric Wong97ae0912007-02-11 15:21:24 -08002714# These files are disposable unless noMetadata or useSvmProps is set
Eric Wong9b981fc2007-01-11 12:14:21 -08002715
Eric Wong060610c2007-12-08 23:27:41 -08002716sub _rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08002717 my ($fh, $rev, $commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08002718
Michael Weber4f7ec792008-04-18 15:12:04 +02002719 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002720 my $size = (stat($fh))[7];
2721 ($size % 24) == 0 or croak "inconsistent size: $size";
2722
Eric Wong66ab84b2007-12-08 23:27:42 -08002723 my $wr_offset = 0;
Eric Wong060610c2007-12-08 23:27:41 -08002724 if ($size > 0) {
2725 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
2726 my $read = sysread($fh, my $buf, 24) or croak "read: $!";
2727 $read == 24 or croak "read only $read bytes (!= 24)";
2728 my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08002729 if ($last_commit eq ('0' x40)) {
2730 if ($size >= 48) {
2731 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
2732 $read = sysread($fh, $buf, 24) or
2733 croak "read: $!";
2734 $read == 24 or
2735 croak "read only $read bytes (!= 24)";
2736 ($last_rev, $last_commit) =
2737 unpack(rev_map_fmt, $buf);
2738 if ($last_commit eq ('0' x40)) {
2739 croak "inconsistent .rev_map\n";
2740 }
2741 }
2742 if ($last_rev >= $rev) {
2743 croak "last_rev is higher!: $last_rev >= $rev";
2744 }
2745 $wr_offset = -24;
Eric Wong47a0b752007-01-31 05:13:30 -08002746 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002747 }
Eric Wong66ab84b2007-12-08 23:27:42 -08002748 sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002749 syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or
2750 croak "write: $!";
Eric Wong26a62d52007-02-12 13:25:25 -08002751}
2752
2753sub mkfile {
2754 my ($path) = @_;
2755 unless (-e $path) {
2756 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
2757 mkpath([$dir]) unless -d $dir;
2758 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
2759 close $fh or die "Couldn't close (create) $path: $!\n";
2760 }
2761}
2762
Eric Wong060610c2007-12-08 23:27:41 -08002763sub rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08002764 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
2765 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
Eric Wong060610c2007-12-08 23:27:41 -08002766 my $db = $self->map_path($uuid);
Eric Wong26a62d52007-02-12 13:25:25 -08002767 my $db_lock = "$db.lock";
2768 my $sig;
2769 if ($update_ref) {
2770 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
2771 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
2772 }
2773 mkfile($db);
2774
2775 $LOCKFILES{$db_lock} = 1;
2776 my $sync;
2777 # both of these options make our .rev_db file very, very important
2778 # and we can't afford to lose it because rebuild() won't work
2779 if ($self->use_svm_props || $self->no_metadata) {
2780 $sync = 1;
Eric Wong060610c2007-12-08 23:27:41 -08002781 copy($db, $db_lock) or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08002782 "Failed to copy: ",
2783 "$db => $db_lock ($!)\n";
2784 } else {
Eric Wong060610c2007-12-08 23:27:41 -08002785 rename $db, $db_lock or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08002786 "Failed to rename: ",
2787 "$db => $db_lock ($!)\n";
2788 }
Eric Wong060610c2007-12-08 23:27:41 -08002789
Eric Wong66ab84b2007-12-08 23:27:42 -08002790 sysopen(my $fh, $db_lock, O_RDWR | O_CREAT)
Eric Wong060610c2007-12-08 23:27:41 -08002791 or croak "Couldn't open $db_lock: $!\n";
2792 _rev_map_set($fh, $rev, $commit);
Eric Wong97ae0912007-02-11 15:21:24 -08002793 if ($sync) {
2794 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2795 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2796 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002797 close $fh or croak $!;
Eric Wong373274f2007-01-31 13:54:23 -08002798 if ($update_ref) {
Eric Wong1e889ef2007-02-16 01:45:13 -08002799 $_head = $self;
Eric Wong373274f2007-01-31 13:54:23 -08002800 command_noisy('update-ref', '-m', "r$rev",
2801 $self->refname, $commit);
2802 }
Eric Wong060610c2007-12-08 23:27:41 -08002803 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
Eric Wong373274f2007-01-31 13:54:23 -08002804 "$db_lock => $db ($!)\n";
2805 delete $LOCKFILES{$db_lock};
2806 if ($update_ref) {
2807 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
2808 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
2809 kill $sig, $$ if defined $sig;
2810 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002811}
2812
Eric Wong66ab84b2007-12-08 23:27:42 -08002813# If want_commit, this will return an array of (rev, commit) where
2814# commit _must_ be a valid commit in the archive.
2815# Otherwise, it'll return the max revision (whether or not the
2816# commit is valid or just a 0x40 placeholder).
Eric Wong060610c2007-12-08 23:27:41 -08002817sub rev_map_max {
Eric Wong66ab84b2007-12-08 23:27:42 -08002818 my ($self, $want_commit) = @_;
Eric Wongd6d33462007-02-16 04:05:33 -08002819 $self->rebuild;
Deskin Miller2beec892008-09-15 21:12:58 -04002820 my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
2821 $want_commit ? ($r, $c) : $r;
2822}
2823
2824sub rev_map_max_norebuild {
2825 my ($self, $want_commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08002826 my $map_path = $self->map_path;
Eric Wong66ab84b2007-12-08 23:27:42 -08002827 stat $map_path or return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08002828 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02002829 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002830 my $size = (stat($fh))[7];
2831 ($size % 24) == 0 or croak "inconsistent size: $size";
2832
2833 if ($size == 0) {
2834 close $fh or croak "close: $!";
Eric Wong66ab84b2007-12-08 23:27:42 -08002835 return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08002836 }
2837
Eric Wong66ab84b2007-12-08 23:27:42 -08002838 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002839 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002840 my ($r, $c) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08002841 if ($want_commit && $c eq ('0' x40)) {
2842 if ($size < 48) {
2843 return $want_commit ? (0, undef) : 0;
2844 }
2845 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
2846 sysread($fh, $buf, 24) == 24 or croak "read: $!";
2847 ($r, $c) = unpack(rev_map_fmt, $buf);
2848 if ($c eq ('0'x40)) {
2849 croak "Penultimate record is all-zeroes in $map_path";
2850 }
2851 }
2852 close $fh or croak "close: $!";
2853 $want_commit ? ($r, $c) : $r;
Eric Wong9c93fee2007-01-31 17:22:31 -08002854}
2855
Eric Wong060610c2007-12-08 23:27:41 -08002856sub rev_map_get {
Eric Wong26a62d52007-02-12 13:25:25 -08002857 my ($self, $rev, $uuid) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08002858 my $map_path = $self->map_path($uuid);
2859 return undef unless -e $map_path;
2860
2861 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02002862 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002863 my $size = (stat($fh))[7];
2864 ($size % 24) == 0 or croak "inconsistent size: $size";
2865
2866 if ($size == 0) {
2867 close $fh or croak "close: $fh";
2868 return undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08002869 }
Eric Wong060610c2007-12-08 23:27:41 -08002870
2871 my ($l, $u) = (0, $size - 24);
2872 my ($r, $c, $buf);
2873
2874 while ($l <= $u) {
2875 my $i = int(($l/24 + $u/24) / 2) * 24;
2876 sysseek($fh, $i, SEEK_SET) or croak "seek: $!";
2877 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
2878 my ($r, $c) = unpack('NH40', $buf);
2879
2880 if ($r < $rev) {
2881 $l = $i + 24;
2882 } elsif ($r > $rev) {
2883 $u = $i - 24;
2884 } else { # $r == $rev
2885 close($fh) or croak "close: $!";
Eric Wong66ab84b2007-12-08 23:27:42 -08002886 return $c eq ('0' x 40) ? undef : $c;
Eric Wong060610c2007-12-08 23:27:41 -08002887 }
2888 }
2889 close($fh) or croak "close: $!";
2890 undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08002891}
2892
David D Kilzer111947e2007-11-11 22:56:52 -08002893# Finds the first svn revision that exists on (if $eq_ok is true) or
2894# before $rev for the current branch. It will not search any lower
2895# than $min_rev. Returns the git commit hash and svn revision number
2896# if found, else (undef, undef).
Eric Wong15710b62007-01-22 02:20:33 -08002897sub find_rev_before {
David D Kilzer111947e2007-11-11 22:56:52 -08002898 my ($self, $rev, $eq_ok, $min_rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08002899 --$rev unless $eq_ok;
David D Kilzer111947e2007-11-11 22:56:52 -08002900 $min_rev ||= 1;
2901 while ($rev >= $min_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08002902 if (my $c = $self->rev_map_get($rev)) {
Eric Wong15710b62007-01-22 02:20:33 -08002903 return ($rev, $c);
2904 }
2905 --$rev;
2906 }
2907 return (undef, undef);
2908}
2909
David D Kilzer111947e2007-11-11 22:56:52 -08002910# Finds the first svn revision that exists on (if $eq_ok is true) or
2911# after $rev for the current branch. It will not search any higher
2912# than $max_rev. Returns the git commit hash and svn revision number
2913# if found, else (undef, undef).
2914sub find_rev_after {
2915 my ($self, $rev, $eq_ok, $max_rev) = @_;
2916 ++$rev unless $eq_ok;
Eric Wong060610c2007-12-08 23:27:41 -08002917 $max_rev ||= $self->rev_map_max;
David D Kilzer111947e2007-11-11 22:56:52 -08002918 while ($rev <= $max_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08002919 if (my $c = $self->rev_map_get($rev)) {
David D Kilzer111947e2007-11-11 22:56:52 -08002920 return ($rev, $c);
2921 }
2922 ++$rev;
2923 }
2924 return (undef, undef);
2925}
2926
Eric Wong9b981fc2007-01-11 12:14:21 -08002927sub _new {
Eric Wong706587f2007-01-18 17:50:01 -08002928 my ($class, $repo_id, $ref_id, $path) = @_;
2929 unless (defined $repo_id && length $repo_id) {
2930 $repo_id = $Git::SVN::default_repo_id;
2931 }
2932 unless (defined $ref_id && length $ref_id) {
Eric Wong8b8fc062007-01-22 11:44:57 -08002933 $_[2] = $ref_id = $Git::SVN::default_ref_id;
Eric Wong706587f2007-01-18 17:50:01 -08002934 }
Eric Wong7829f202008-06-28 20:40:32 -07002935 $_[1] = $repo_id;
Eric Wong706587f2007-01-18 17:50:01 -08002936 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
2937 $_[3] = $path = '' unless (defined $path);
Eric Wongb4d57e52007-02-14 15:10:44 -08002938 mkpath(["$ENV{GIT_DIR}/svn"]);
Eric Wong26a62d52007-02-12 13:25:25 -08002939 bless {
2940 ref_id => $ref_id, dir => $dir, index => "$dir/index",
Eric Wong8a49ee92007-02-10 20:46:50 -08002941 path => $path, config => "$ENV{GIT_DIR}/svn/config",
Eric Wong060610c2007-12-08 23:27:41 -08002942 map_root => "$dir/.rev_map", repo_id => $repo_id }, $class;
Eric Wong26a62d52007-02-12 13:25:25 -08002943}
2944
Eric Wong060610c2007-12-08 23:27:41 -08002945# for read-only access of old .rev_db formats
2946sub unlink_rev_db_symlink {
2947 my ($self) = @_;
2948 my $link = $self->rev_db_path;
2949 $link =~ s/\.[\w-]+$// or croak "missing UUID at the end of $link";
2950 if (-l $link) {
2951 unlink $link or croak "unlink: $link failed!";
2952 }
2953}
2954
2955sub rev_db_path {
2956 my ($self, $uuid) = @_;
2957 my $db_path = $self->map_path($uuid);
2958 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
2959 or croak "map_path: $db_path does not contain '/.rev_map.' !";
2960 $db_path;
2961}
2962
2963# the new replacement for .rev_db
2964sub map_path {
Eric Wong26a62d52007-02-12 13:25:25 -08002965 my ($self, $uuid) = @_;
2966 $uuid ||= $self->ra_uuid;
Eric Wong060610c2007-12-08 23:27:41 -08002967 "$self->{map_root}.$uuid";
Eric Wong9b981fc2007-01-11 12:14:21 -08002968}
2969
Eric Wong1c8443b2007-01-14 02:17:00 -08002970sub uri_encode {
2971 my ($f) = @_;
2972 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2973 $f
2974}
Eric Wong9b981fc2007-01-11 12:14:21 -08002975
Sam Vilain18ea92b2007-02-23 12:32:29 +13002976sub remove_username {
2977 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
2978}
2979
Eric Wongd976acf2007-01-04 00:45:03 -08002980package Git::SVN::Prompt;
2981use strict;
2982use warnings;
2983require SVN::Core;
2984use vars qw/$_no_auth_cache $_username/;
2985
2986sub simple {
Eric Wong30d055a2006-11-24 01:38:04 -08002987 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
2988 $may_save = undef if $_no_auth_cache;
2989 $default_username = $_username if defined $_username;
2990 if (defined $default_username && length $default_username) {
2991 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08002992 print STDERR "Authentication realm: $realm\n";
2993 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002994 }
2995 $cred->username($default_username);
2996 } else {
Eric Wongd976acf2007-01-04 00:45:03 -08002997 username($cred, $realm, $may_save, $pool);
Eric Wong30d055a2006-11-24 01:38:04 -08002998 }
2999 $cred->password(_read_password("Password for '" .
3000 $cred->username . "': ", $realm));
3001 $cred->may_save($may_save);
3002 $SVN::_Core::SVN_NO_ERROR;
3003}
3004
Eric Wongd976acf2007-01-04 00:45:03 -08003005sub ssl_server_trust {
Eric Wong30d055a2006-11-24 01:38:04 -08003006 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
3007 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003008 print STDERR "Error validating server certificate for '$realm':\n";
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003009 {
3010 no warnings 'once';
3011 # All variables SVN::Auth::SSL::* are used only once,
3012 # so we're shutting up Perl warnings about this.
3013 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
3014 print STDERR " - The certificate is not issued ",
3015 "by a trusted authority. Use the\n",
3016 " fingerprint to validate ",
3017 "the certificate manually!\n";
3018 }
3019 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
3020 print STDERR " - The certificate hostname ",
3021 "does not match.\n";
3022 }
3023 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
3024 print STDERR " - The certificate is not yet valid.\n";
3025 }
3026 if ($failures & $SVN::Auth::SSL::EXPIRED) {
3027 print STDERR " - The certificate has expired.\n";
3028 }
3029 if ($failures & $SVN::Auth::SSL::OTHER) {
3030 print STDERR " - The certificate has ",
3031 "an unknown error.\n";
3032 }
3033 } # no warnings 'once'
Eric Wong6f729592007-01-15 20:15:55 -08003034 printf STDERR
3035 "Certificate information:\n".
Eric Wong30d055a2006-11-24 01:38:04 -08003036 " - Hostname: %s\n".
3037 " - Valid: from %s until %s\n".
3038 " - Issuer: %s\n".
3039 " - Fingerprint: %s\n",
3040 map $cert_info->$_, qw(hostname valid_from valid_until
Eric Wong6f729592007-01-15 20:15:55 -08003041 issuer_dname fingerprint);
Eric Wong30d055a2006-11-24 01:38:04 -08003042 my $choice;
3043prompt:
Eric Wong6f729592007-01-15 20:15:55 -08003044 print STDERR $may_save ?
Eric Wong30d055a2006-11-24 01:38:04 -08003045 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
3046 "(R)eject or accept (t)emporarily? ";
Eric Wong6f729592007-01-15 20:15:55 -08003047 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003048 $choice = lc(substr(<STDIN> || 'R', 0, 1));
3049 if ($choice =~ /^t$/i) {
3050 $cred->may_save(undef);
3051 } elsif ($choice =~ /^r$/i) {
3052 return -1;
3053 } elsif ($may_save && $choice =~ /^p$/i) {
3054 $cred->may_save($may_save);
3055 } else {
3056 goto prompt;
3057 }
3058 $cred->accepted_failures($failures);
3059 $SVN::_Core::SVN_NO_ERROR;
3060}
3061
Eric Wongd976acf2007-01-04 00:45:03 -08003062sub ssl_client_cert {
Eric Wong30d055a2006-11-24 01:38:04 -08003063 my ($cred, $realm, $may_save, $pool) = @_;
3064 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003065 print STDERR "Client certificate filename: ";
3066 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003067 chomp(my $filename = <STDIN>);
3068 $cred->cert_file($filename);
3069 $cred->may_save($may_save);
3070 $SVN::_Core::SVN_NO_ERROR;
3071}
3072
Eric Wongd976acf2007-01-04 00:45:03 -08003073sub ssl_client_cert_pw {
Eric Wong30d055a2006-11-24 01:38:04 -08003074 my ($cred, $realm, $may_save, $pool) = @_;
3075 $may_save = undef if $_no_auth_cache;
3076 $cred->password(_read_password("Password: ", $realm));
3077 $cred->may_save($may_save);
3078 $SVN::_Core::SVN_NO_ERROR;
3079}
3080
Eric Wongd976acf2007-01-04 00:45:03 -08003081sub username {
Eric Wong30d055a2006-11-24 01:38:04 -08003082 my ($cred, $realm, $may_save, $pool) = @_;
3083 $may_save = undef if $_no_auth_cache;
3084 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003085 print STDERR "Authentication realm: $realm\n";
Eric Wong30d055a2006-11-24 01:38:04 -08003086 }
3087 my $username;
3088 if (defined $_username) {
3089 $username = $_username;
3090 } else {
Eric Wong6f729592007-01-15 20:15:55 -08003091 print STDERR "Username: ";
3092 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003093 chomp($username = <STDIN>);
3094 }
3095 $cred->username($username);
3096 $cred->may_save($may_save);
3097 $SVN::_Core::SVN_NO_ERROR;
3098}
3099
3100sub _read_password {
3101 my ($prompt, $realm) = @_;
Eric Wong6f729592007-01-15 20:15:55 -08003102 print STDERR $prompt;
3103 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003104 require Term::ReadKey;
3105 Term::ReadKey::ReadMode('noecho');
3106 my $password = '';
3107 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
3108 last if $key =~ /[\012\015]/; # \n\r
3109 $password .= $key;
3110 }
3111 Term::ReadKey::ReadMode('restore');
Eric Wong6f729592007-01-15 20:15:55 -08003112 print STDERR "\n";
3113 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003114 $password;
3115}
3116
Eric Wong27a1a802006-11-27 21:44:48 -08003117package SVN::Git::Fetcher;
3118use vars qw/@ISA/;
3119use strict;
3120use warnings;
3121use Carp qw/croak/;
Adam Robenffe256f2008-05-23 16:19:41 +02003122use File::Temp qw/tempfile/;
Eric Wong27a1a802006-11-27 21:44:48 -08003123use IO::File qw//;
3124
3125# file baton members: path, mode_a, mode_b, pool, fh, blob, base
3126sub new {
3127 my ($class, $git_svn) = @_;
3128 my $self = SVN::Delta::Editor->new;
3129 bless $self, $class;
Eric Wong1c8443b2007-01-14 02:17:00 -08003130 $self->{c} = $git_svn->{last_commit} if exists $git_svn->{last_commit};
Eric Wongd2a9a872006-12-12 14:47:00 -08003131 $self->{empty} = {};
3132 $self->{dir_prop} = {};
3133 $self->{file_prop} = {};
3134 $self->{absent_dir} = {};
3135 $self->{absent_file} = {};
Eric Wongef3cfaa2007-01-24 03:30:57 -08003136 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
Eric Wong27a1a802006-11-27 21:44:48 -08003137 $self;
3138}
3139
Eric Wong8b8fc062007-01-22 11:44:57 -08003140sub set_path_strip {
3141 my ($self, $path) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003142 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
Eric Wong8b8fc062007-01-22 11:44:57 -08003143}
3144
Eric Wongd2a9a872006-12-12 14:47:00 -08003145sub open_root {
3146 { path => '' };
3147}
3148
3149sub open_directory {
3150 my ($self, $path, $pb, $rev) = @_;
3151 { path => $path };
3152}
3153
Eric Wong706587f2007-01-18 17:50:01 -08003154sub git_path {
3155 my ($self, $path) = @_;
Eric Wong2b27f6c2007-01-28 04:59:05 -08003156 if ($self->{path_strip}) {
3157 $path =~ s!$self->{path_strip}!! or
3158 die "Failed to strip path '$path' ($self->{path_strip})\n";
3159 }
Eric Wong706587f2007-01-18 17:50:01 -08003160 $path;
3161}
3162
Eric Wong27a1a802006-11-27 21:44:48 -08003163sub delete_entry {
3164 my ($self, $path, $rev, $pb) = @_;
Eric Wong4a87db02007-01-04 01:38:18 -08003165
Eric Wong706587f2007-01-18 17:50:01 -08003166 my $gpath = $self->git_path($path);
Eric Wong8a603772007-01-31 02:45:50 -08003167 return undef if ($gpath eq '');
3168
Eric Wong4a87db02007-01-04 01:38:18 -08003169 # remove entire directories.
Eric Wong706587f2007-01-18 17:50:01 -08003170 if (command('ls-tree', $self->{c}, '--', $gpath) =~ /^040000 tree/) {
Eric Wong4a87db02007-01-04 01:38:18 -08003171 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3172 -r --name-only -z/,
Eric Wong706587f2007-01-18 17:50:01 -08003173 $self->{c}, '--', $gpath);
Eric Wong4a87db02007-01-04 01:38:18 -08003174 local $/ = "\0";
3175 while (<$ls>) {
Eric Wongef3cfaa2007-01-24 03:30:57 -08003176 chomp;
3177 $self->{gii}->remove($_);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003178 print "\tD\t$_\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003179 }
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003180 print "\tD\t$gpath/\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003181 command_close_pipe($ls, $ctx);
3182 $self->{empty}->{$path} = 0
3183 } else {
Eric Wongef3cfaa2007-01-24 03:30:57 -08003184 $self->{gii}->remove($gpath);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003185 print "\tD\t$gpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003186 }
Eric Wong27a1a802006-11-27 21:44:48 -08003187 undef;
3188}
3189
3190sub open_file {
3191 my ($self, $path, $pb, $rev) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08003192 my $gpath = $self->git_path($path);
3193 my ($mode, $blob) = (command('ls-tree', $self->{c}, '--', $gpath)
Eric Wong27a1a802006-11-27 21:44:48 -08003194 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
Eric Wong006ede52006-12-08 01:55:19 -08003195 unless (defined $mode && defined $blob) {
3196 die "$path was not found in commit $self->{c} (r$rev)\n";
3197 }
Eric Wong27a1a802006-11-27 21:44:48 -08003198 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
Eric Wong0864e3b2006-11-28 02:50:17 -08003199 pool => SVN::Pool->new, action => 'M' };
Eric Wong27a1a802006-11-27 21:44:48 -08003200}
3201
3202sub add_file {
3203 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
Eric Wongd2a9a872006-12-12 14:47:00 -08003204 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3205 delete $self->{empty}->{$dir};
Eric Wong27a1a802006-11-27 21:44:48 -08003206 { path => $path, mode_a => 100644, mode_b => 100644,
Eric Wong0864e3b2006-11-28 02:50:17 -08003207 pool => SVN::Pool->new, action => 'A' };
Eric Wong27a1a802006-11-27 21:44:48 -08003208}
3209
Eric Wongd2a9a872006-12-12 14:47:00 -08003210sub add_directory {
3211 my ($self, $path, $cp_path, $cp_rev) = @_;
Eric Wong12a6d752007-12-14 08:39:09 -08003212 my $gpath = $self->git_path($path);
3213 if ($gpath eq '') {
3214 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3215 -r --name-only -z/,
3216 $self->{c});
3217 local $/ = "\0";
3218 while (<$ls>) {
3219 chomp;
3220 $self->{gii}->remove($_);
3221 print "\tD\t$_\n" unless $::_q;
3222 }
3223 command_close_pipe($ls, $ctx);
3224 $self->{empty}->{$path} = 0;
3225 }
Eric Wongd2a9a872006-12-12 14:47:00 -08003226 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3227 delete $self->{empty}->{$dir};
3228 $self->{empty}->{$path} = 1;
3229 { path => $path };
3230}
3231
3232sub change_dir_prop {
3233 my ($self, $db, $prop, $value) = @_;
3234 $self->{dir_prop}->{$db->{path}} ||= {};
3235 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
3236 undef;
3237}
3238
3239sub absent_directory {
3240 my ($self, $path, $pb) = @_;
3241 $self->{absent_dir}->{$pb->{path}} ||= [];
3242 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
3243 undef;
3244}
3245
3246sub absent_file {
3247 my ($self, $path, $pb) = @_;
3248 $self->{absent_file}->{$pb->{path}} ||= [];
3249 push @{$self->{absent_file}->{$pb->{path}}}, $path;
3250 undef;
3251}
3252
Eric Wong27a1a802006-11-27 21:44:48 -08003253sub change_file_prop {
3254 my ($self, $fb, $prop, $value) = @_;
3255 if ($prop eq 'svn:executable') {
3256 if ($fb->{mode_b} != 120000) {
3257 $fb->{mode_b} = defined $value ? 100755 : 100644;
3258 }
3259 } elsif ($prop eq 'svn:special') {
3260 $fb->{mode_b} = defined $value ? 120000 : 100644;
Eric Wongd2a9a872006-12-12 14:47:00 -08003261 } else {
3262 $self->{file_prop}->{$fb->{path}} ||= {};
3263 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
Eric Wong27a1a802006-11-27 21:44:48 -08003264 }
3265 undef;
3266}
3267
3268sub apply_textdelta {
3269 my ($self, $fb, $exp) = @_;
Marcus Griep0b191382008-08-12 12:00:53 -04003270 my $fh = Git::temp_acquire('svn_delta');
Eric Wong27a1a802006-11-27 21:44:48 -08003271 # $fh gets auto-closed() by SVN::TxDelta::apply(),
3272 # (but $base does not,) so dup() it for reading in close_file
3273 open my $dup, '<&', $fh or croak $!;
Marcus Griep0b191382008-08-12 12:00:53 -04003274 my $base = Git::temp_acquire('git_blob');
Eric Wong27a1a802006-11-27 21:44:48 -08003275 if ($fb->{blob}) {
Adam Robenffe256f2008-05-23 16:19:41 +02003276 print $base 'link ' if ($fb->{mode_a} == 120000);
3277 my $size = $::_repository->cat_blob($fb->{blob}, $base);
Junio C Hamanod683a0e2008-05-27 23:33:22 -07003278 die "Failed to read object $fb->{blob}" if ($size < 0);
Eric Wong27a1a802006-11-27 21:44:48 -08003279
3280 if (defined $exp) {
3281 seek $base, 0, 0 or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08003282 my $got = ::md5sum($base);
Eric Wong27a1a802006-11-27 21:44:48 -08003283 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
3284 "expected: $exp\n",
3285 " got: $got\n" if ($got ne $exp);
3286 }
3287 }
3288 seek $base, 0, 0 or croak $!;
Marcus Griep0b191382008-08-12 12:00:53 -04003289 $fb->{fh} = $fh;
Eric Wong27a1a802006-11-27 21:44:48 -08003290 $fb->{base} = $base;
Marcus Griep0b191382008-08-12 12:00:53 -04003291 [ SVN::TxDelta::apply($base, $dup, undef, $fb->{path}, $fb->{pool}) ];
Eric Wong27a1a802006-11-27 21:44:48 -08003292}
3293
3294sub close_file {
3295 my ($self, $fb, $exp) = @_;
3296 my $hash;
Eric Wong706587f2007-01-18 17:50:01 -08003297 my $path = $self->git_path($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08003298 if (my $fh = $fb->{fh}) {
Eric Wong7faf0682007-05-27 15:59:01 -07003299 if (defined $exp) {
3300 seek($fh, 0, 0) or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08003301 my $got = ::md5sum($fh);
Eric Wong7faf0682007-05-27 15:59:01 -07003302 if ($got ne $exp) {
3303 die "Checksum mismatch: $path\n",
3304 "expected: $exp\n got: $got\n";
3305 }
3306 }
Eric Wong27a1a802006-11-27 21:44:48 -08003307 if ($fb->{mode_b} == 120000) {
Marcus Griep510b0942008-08-12 12:45:39 -04003308 sysseek($fh, 0, 0) or croak $!;
3309 sysread($fh, my $buf, 5) == 5 or croak $!;
3310
3311 unless ($buf eq 'link ') {
3312 warn "$path has mode 120000",
3313 " but is not a link\n";
3314 } else {
3315 my $tmp_fh = Git::temp_acquire('svn_hash');
3316 my $res;
3317 while ($res = sysread($fh, my $str, 1024)) {
3318 my $out = syswrite($tmp_fh, $str, $res);
3319 defined($out) && $out == $res
3320 or croak("write ",
Marcus Griep836ff952008-09-08 12:53:01 -04003321 Git::temp_path($tmp_fh),
Marcus Griep510b0942008-08-12 12:45:39 -04003322 ": $!\n");
3323 }
3324 defined $res or croak $!;
3325
3326 ($fh, $tmp_fh) = ($tmp_fh, $fh);
3327 Git::temp_release($tmp_fh, 1);
Eric Wong7fc35e02007-12-19 00:06:45 -08003328 }
Eric Wong27a1a802006-11-27 21:44:48 -08003329 }
Adam Robenffe256f2008-05-23 16:19:41 +02003330
Marcus Griep0b191382008-08-12 12:00:53 -04003331 $hash = $::_repository->hash_and_insert_object(
Marcus Griep836ff952008-09-08 12:53:01 -04003332 Git::temp_path($fh));
Eric Wong27a1a802006-11-27 21:44:48 -08003333 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
Marcus Griep0b191382008-08-12 12:00:53 -04003334
3335 Git::temp_release($fb->{base}, 1);
Marcus Griep510b0942008-08-12 12:45:39 -04003336 Git::temp_release($fh, 1);
Eric Wong27a1a802006-11-27 21:44:48 -08003337 } else {
3338 $hash = $fb->{blob} or die "no blob information\n";
3339 }
3340 $fb->{pool}->clear;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003341 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003342 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
Eric Wong27a1a802006-11-27 21:44:48 -08003343 undef;
3344}
3345
3346sub abort_edit {
3347 my $self = shift;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003348 $self->{nr} = $self->{gii}->{nr};
3349 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08003350 $self->SUPER::abort_edit(@_);
3351}
3352
3353sub close_edit {
3354 my $self = shift;
Eric Wongdad73c02006-11-28 14:06:05 -08003355 $self->{git_commit_ok} = 1;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003356 $self->{nr} = $self->{gii}->{nr};
3357 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08003358 $self->SUPER::close_edit(@_);
3359}
Eric Wong1a82e792006-06-16 02:55:13 -07003360
Eric Wonga5e0ced2006-06-12 15:23:48 -07003361package SVN::Git::Editor;
Eric Wong24e22aa2007-01-29 00:07:49 -08003362use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003363use strict;
3364use warnings;
3365use Carp qw/croak/;
3366use IO::File;
3367
3368sub new {
Eric Wong61395352007-01-27 14:33:08 -08003369 my ($class, $opts) = @_;
3370 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
3371 die "$_ required!\n" unless (defined $opts->{$_});
Eric Wonga5e0ced2006-06-12 15:23:48 -07003372 }
Eric Wong61395352007-01-27 14:33:08 -08003373
3374 my $pool = SVN::Pool->new;
3375 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
3376 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
3377 $opts->{r}, $mods);
3378
3379 # $opts->{ra} functions should not be used after this:
3380 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
3381 $opts->{editor_cb}, $pool);
3382 my $self = SVN::Delta::Editor->new(@ce, $pool);
3383 bless $self, $class;
3384 foreach (qw/svn_path r tree_a tree_b/) {
3385 $self->{$_} = $opts->{$_};
3386 }
3387 $self->{url} = $opts->{ra}->{url};
3388 $self->{mods} = $mods;
3389 $self->{types} = $types;
3390 $self->{pool} = $pool;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003391 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
3392 $self->{rm} = { };
Eric Wongd3a840d2007-01-26 01:32:45 -08003393 $self->{path_prefix} = length $self->{svn_path} ?
3394 "$self->{svn_path}/" : '';
Brad King128de652008-07-25 11:32:37 -04003395 $self->{config} = $opts->{config};
Eric Wonga5e0ced2006-06-12 15:23:48 -07003396 return $self;
3397}
3398
Eric Wong61395352007-01-27 14:33:08 -08003399sub generate_diff {
3400 my ($tree_a, $tree_b) = @_;
3401 my @diff_tree = qw(diff-tree -z -r);
Eric Wong24e22aa2007-01-29 00:07:49 -08003402 if ($_cp_similarity) {
3403 push @diff_tree, "-C$_cp_similarity";
Eric Wong61395352007-01-27 14:33:08 -08003404 } else {
3405 push @diff_tree, '-C';
3406 }
Eric Wong24e22aa2007-01-29 00:07:49 -08003407 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
3408 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
Eric Wong61395352007-01-27 14:33:08 -08003409 push @diff_tree, $tree_a, $tree_b;
3410 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
3411 local $/ = "\0";
3412 my $state = 'meta';
3413 my @mods;
3414 while (<$diff_fh>) {
3415 chomp $_; # this gets rid of the trailing "\0"
3416 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02003417 ($::sha1)\s($::sha1)\s
Eric Wong61395352007-01-27 14:33:08 -08003418 ([MTCRAD])\d*$/xo) {
3419 push @mods, { mode_a => $1, mode_b => $2,
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02003420 sha1_a => $3, sha1_b => $4,
3421 chg => $5 };
3422 if ($5 =~ /^(?:C|R)$/) {
Eric Wong61395352007-01-27 14:33:08 -08003423 $state = 'file_a';
3424 } else {
3425 $state = 'file_b';
3426 }
3427 } elsif ($state eq 'file_a') {
3428 my $x = $mods[$#mods] or croak "Empty array\n";
3429 if ($x->{chg} !~ /^(?:C|R)$/) {
3430 croak "Error parsing $_, $x->{chg}\n";
3431 }
3432 $x->{file_a} = $_;
3433 $state = 'file_b';
3434 } elsif ($state eq 'file_b') {
3435 my $x = $mods[$#mods] or croak "Empty array\n";
3436 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
3437 croak "Error parsing $_, $x->{chg}\n";
3438 }
3439 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
3440 croak "Error parsing $_, $x->{chg}\n";
3441 }
3442 $x->{file_b} = $_;
3443 $state = 'meta';
3444 } else {
3445 croak "Error parsing $_\n";
3446 }
3447 }
3448 command_close_pipe($diff_fh, $ctx);
3449 \@mods;
3450}
3451
3452sub check_diff_paths {
3453 my ($ra, $pfx, $rev, $mods) = @_;
3454 my %types;
3455 $pfx .= '/' if length $pfx;
3456
3457 sub type_diff_paths {
3458 my ($ra, $types, $path, $rev) = @_;
3459 my @p = split m#/+#, $path;
3460 my $c = shift @p;
3461 unless (defined $types->{$c}) {
3462 $types->{$c} = $ra->check_path($c, $rev);
3463 }
3464 while (@p) {
3465 $c .= '/' . shift @p;
3466 next if defined $types->{$c};
3467 $types->{$c} = $ra->check_path($c, $rev);
3468 }
3469 }
3470
3471 foreach my $m (@$mods) {
3472 foreach my $f (qw/file_a file_b/) {
3473 next unless defined $m->{$f};
3474 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
3475 if (length $pfx.$dir && ! defined $types{$dir}) {
3476 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
3477 }
3478 }
3479 }
3480 \%types;
3481}
3482
Eric Wonga5e0ced2006-06-12 15:23:48 -07003483sub split_path {
3484 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
3485}
3486
3487sub repo_path {
Eric Wongd3a840d2007-01-26 01:32:45 -08003488 my ($self, $path) = @_;
3489 $self->{path_prefix}.(defined $path ? $path : '');
Eric Wonga5e0ced2006-06-12 15:23:48 -07003490}
3491
3492sub url_path {
3493 my ($self, $path) = @_;
Eric Wong29633bb2007-07-15 21:53:50 -07003494 if ($self->{url} =~ m#^https?://#) {
3495 $path =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
3496 }
Eric Wong6e8548c2007-01-27 01:32:00 -08003497 $self->{url} . '/' . $self->repo_path($path);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003498}
3499
3500sub rmdirs {
Eric Wong61395352007-01-27 14:33:08 -08003501 my ($self) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003502 my $rm = $self->{rm};
3503 delete $rm->{''}; # we never delete the url we're tracking
3504 return unless %$rm;
3505
3506 foreach (keys %$rm) {
3507 my @d = split m#/#, $_;
3508 my $c = shift @d;
3509 $rm->{$c} = 1;
3510 while (@d) {
3511 $c .= '/' . shift @d;
3512 $rm->{$c} = 1;
3513 }
3514 }
3515 delete $rm->{$self->{svn_path}};
3516 delete $rm->{''}; # we never delete the url we're tracking
3517 return unless %$rm;
3518
Eric Wong61395352007-01-27 14:33:08 -08003519 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
3520 $self->{tree_b});
Eric Wonga5e0ced2006-06-12 15:23:48 -07003521 local $/ = "\0";
3522 while (<$fh>) {
3523 chomp;
Eric Wong747fa122006-11-24 22:38:17 -08003524 my @dn = split m#/#, $_;
Eric Wongc07eee12006-06-19 17:59:35 -07003525 while (pop @dn) {
3526 delete $rm->{join '/', @dn};
3527 }
3528 unless (%$rm) {
Eric Wong22600a22007-02-01 13:12:26 -08003529 close $fh;
Eric Wongc07eee12006-06-19 17:59:35 -07003530 return;
3531 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07003532 }
Eric Wongaef4e922006-12-15 10:59:54 -08003533 command_close_pipe($fh, $ctx);
Eric Wongc07eee12006-06-19 17:59:35 -07003534
Eric Wonga5e0ced2006-06-12 15:23:48 -07003535 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
3536 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3537 $self->close_directory($bat->{$d}, $p);
3538 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
Eric Wong44320b92007-01-13 22:35:53 -08003539 print "\tD+\t$d/\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003540 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
3541 delete $bat->{$d};
3542 }
3543}
3544
3545sub open_or_add_dir {
3546 my ($self, $full_path, $baton) = @_;
Eric Wong6e8548c2007-01-27 01:32:00 -08003547 my $t = $self->{types}->{$full_path};
3548 if (!defined $t) {
3549 die "$full_path not known in r$self->{r} or we have a bug!\n";
3550 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003551 {
3552 no warnings 'once';
3553 # SVN::Node::none and SVN::Node::file are used only once,
3554 # so we're shutting up Perl's warnings about them.
3555 if ($t == $SVN::Node::none) {
3556 return $self->add_directory($full_path, $baton,
3557 undef, -1, $self->{pool});
3558 } elsif ($t == $SVN::Node::dir) {
3559 return $self->open_directory($full_path, $baton,
3560 $self->{r}, $self->{pool});
3561 } # no warnings 'once'
3562 print STDERR "$full_path already exists in repository at ",
3563 "r$self->{r} and it is not a directory (",
3564 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
3565 } # no warnings 'once'
Eric Wonga5e0ced2006-06-12 15:23:48 -07003566 exit 1;
3567}
3568
3569sub ensure_path {
3570 my ($self, $path) = @_;
3571 my $bat = $self->{bat};
Eric Wong6e8548c2007-01-27 01:32:00 -08003572 my $repo_path = $self->repo_path($path);
3573 return $bat->{''} unless (length $repo_path);
3574 my @p = split m#/+#, $repo_path;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003575 my $c = shift @p;
3576 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3577 while (@p) {
3578 my $c0 = $c;
3579 $c .= '/' . shift @p;
3580 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3581 }
3582 return $bat->{$c};
3583}
3584
Brad King128de652008-07-25 11:32:37 -04003585# Subroutine to convert a globbing pattern to a regular expression.
3586# From perl cookbook.
3587sub glob2pat {
3588 my $globstr = shift;
3589 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
3590 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
3591 return '^' . $globstr . '$';
3592}
3593
3594sub check_autoprop {
3595 my ($self, $pattern, $properties, $file, $fbat) = @_;
3596 # Convert the globbing pattern to a regular expression.
3597 my $regex = glob2pat($pattern);
3598 # Check if the pattern matches the file name.
3599 if($file =~ m/($regex)/) {
3600 # Parse the list of properties to set.
3601 my @props = split(/;/, $properties);
3602 foreach my $prop (@props) {
3603 # Parse 'name=value' syntax and set the property.
3604 if ($prop =~ /([^=]+)=(.*)/) {
3605 my ($n,$v) = ($1,$2);
3606 for ($n, $v) {
3607 s/^\s+//; s/\s+$//;
3608 }
3609 $self->change_file_prop($fbat, $n, $v);
3610 }
3611 }
3612 }
3613}
3614
3615sub apply_autoprops {
3616 my ($self, $file, $fbat) = @_;
3617 my $conf_t = ${$self->{config}}{'config'};
3618 no warnings 'once';
3619 # Check [miscellany]/enable-auto-props in svn configuration.
3620 if (SVN::_Core::svn_config_get_bool(
3621 $conf_t,
3622 $SVN::_Core::SVN_CONFIG_SECTION_MISCELLANY,
3623 $SVN::_Core::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS,
3624 0)) {
3625 # Auto-props are enabled. Enumerate them to look for matches.
3626 my $callback = sub {
3627 $self->check_autoprop($_[0], $_[1], $file, $fbat);
3628 };
3629 SVN::_Core::svn_config_enumerate(
3630 $conf_t,
3631 $SVN::_Core::SVN_CONFIG_SECTION_AUTO_PROPS,
3632 $callback);
3633 }
3634}
3635
Eric Wonga5e0ced2006-06-12 15:23:48 -07003636sub A {
Eric Wong44320b92007-01-13 22:35:53 -08003637 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003638 my ($dir, $file) = split_path($m->{file_b});
3639 my $pbat = $self->ensure_path($dir);
3640 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3641 undef, -1);
Eric Wong44320b92007-01-13 22:35:53 -08003642 print "\tA\t$m->{file_b}\n" unless $::_q;
Brad King128de652008-07-25 11:32:37 -04003643 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003644 $self->chg_file($fbat, $m);
3645 $self->close_file($fbat,undef,$self->{pool});
3646}
3647
3648sub C {
Eric Wong44320b92007-01-13 22:35:53 -08003649 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003650 my ($dir, $file) = split_path($m->{file_b});
3651 my $pbat = $self->ensure_path($dir);
3652 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3653 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08003654 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003655 $self->chg_file($fbat, $m);
3656 $self->close_file($fbat,undef,$self->{pool});
3657}
3658
3659sub delete_entry {
3660 my ($self, $path, $pbat) = @_;
3661 my $rpath = $self->repo_path($path);
3662 my ($dir, $file) = split_path($rpath);
3663 $self->{rm}->{$dir} = 1;
3664 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
3665}
3666
3667sub R {
Eric Wong44320b92007-01-13 22:35:53 -08003668 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003669 my ($dir, $file) = split_path($m->{file_b});
3670 my $pbat = $self->ensure_path($dir);
3671 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3672 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08003673 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Paul Talacko7c4d0212008-09-06 18:50:38 -07003674 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003675 $self->chg_file($fbat, $m);
3676 $self->close_file($fbat,undef,$self->{pool});
3677
3678 ($dir, $file) = split_path($m->{file_a});
3679 $pbat = $self->ensure_path($dir);
3680 $self->delete_entry($m->{file_a}, $pbat);
3681}
3682
3683sub M {
Eric Wong44320b92007-01-13 22:35:53 -08003684 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003685 my ($dir, $file) = split_path($m->{file_b});
3686 my $pbat = $self->ensure_path($dir);
3687 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
3688 $pbat,$self->{r},$self->{pool});
Eric Wong44320b92007-01-13 22:35:53 -08003689 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003690 $self->chg_file($fbat, $m);
3691 $self->close_file($fbat,undef,$self->{pool});
3692}
3693
3694sub T { shift->M(@_) }
3695
3696sub change_file_prop {
3697 my ($self, $fbat, $pname, $pval) = @_;
3698 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
3699}
3700
Florian Weimer214a34d2008-08-31 17:45:04 +02003701sub _chg_file_get_blob ($$$$) {
3702 my ($self, $fbat, $m, $which) = @_;
3703 my $fh = Git::temp_acquire("git_blob_$which");
3704 if ($m->{"mode_$which"} =~ /^120/) {
3705 print $fh 'link ' or croak $!;
3706 $self->change_file_prop($fbat,'svn:special','*');
3707 } elsif ($m->{mode_a} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
3708 $self->change_file_prop($fbat,'svn:special',undef);
3709 }
3710 my $blob = $m->{"sha1_$which"};
3711 return ($fh,) if ($blob =~ /^0{40}$/);
3712 my $size = $::_repository->cat_blob($blob, $fh);
3713 croak "Failed to read object $blob" if ($size < 0);
3714 $fh->flush == 0 or croak $!;
3715 seek $fh, 0, 0 or croak $!;
3716
3717 my $exp = ::md5sum($fh);
3718 seek $fh, 0, 0 or croak $!;
3719 return ($fh, $exp);
3720}
3721
Eric Wonga5e0ced2006-06-12 15:23:48 -07003722sub chg_file {
3723 my ($self, $fbat, $m) = @_;
3724 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
3725 $self->change_file_prop($fbat,'svn:executable','*');
3726 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
3727 $self->change_file_prop($fbat,'svn:executable',undef);
3728 }
Florian Weimer8598db932008-08-31 17:47:09 +02003729 my ($fh_a, $exp_a) = _chg_file_get_blob $self, $fbat, $m, 'a';
3730 my ($fh_b, $exp_b) = _chg_file_get_blob $self, $fbat, $m, 'b';
Eric Wongf7197df2006-10-14 15:48:35 -07003731 my $pool = SVN::Pool->new;
Florian Weimer8598db932008-08-31 17:47:09 +02003732 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
3733 if (-s $fh_a) {
3734 my $txstream = SVN::TxDelta::new ($fh_a, $fh_b, $pool);
Eric Wong991255c2008-08-31 19:45:07 -07003735 my $res = SVN::TxDelta::send_txstream($txstream, @$atd, $pool);
3736 if (defined $res) {
3737 die "Unexpected result from send_txstream: $res\n",
3738 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
3739 }
Florian Weimer8598db932008-08-31 17:47:09 +02003740 } else {
3741 my $got = SVN::TxDelta::send_stream($fh_b, @$atd, $pool);
3742 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
3743 if ($got ne $exp_b);
3744 }
3745 Git::temp_release($fh_b, 1);
3746 Git::temp_release($fh_a, 1);
Eric Wongf7197df2006-10-14 15:48:35 -07003747 $pool->clear;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003748}
3749
3750sub D {
Eric Wong44320b92007-01-13 22:35:53 -08003751 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003752 my ($dir, $file) = split_path($m->{file_b});
3753 my $pbat = $self->ensure_path($dir);
Eric Wong44320b92007-01-13 22:35:53 -08003754 print "\tD\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003755 $self->delete_entry($m->{file_b}, $pbat);
3756}
3757
3758sub close_edit {
3759 my ($self) = @_;
3760 my ($p,$bat) = ($self->{pool}, $self->{bat});
3761 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
Eric Wong64427542007-05-19 02:58:37 -07003762 next if $_ eq '';
Eric Wonga5e0ced2006-06-12 15:23:48 -07003763 $self->close_directory($bat->{$_}, $p);
3764 }
Eric Wong64427542007-05-19 02:58:37 -07003765 $self->close_directory($bat->{''}, $p);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003766 $self->SUPER::close_edit($p);
3767 $p->clear;
3768}
3769
3770sub abort_edit {
3771 my ($self) = @_;
3772 $self->SUPER::abort_edit($self->{pool});
Eric Wong61395352007-01-27 14:33:08 -08003773}
3774
3775sub DESTROY {
3776 my $self = shift;
3777 $self->SUPER::DESTROY(@_);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003778 $self->{pool}->clear;
3779}
3780
Eric Wong44320b92007-01-13 22:35:53 -08003781# this drives the editor
3782sub apply_diff {
Eric Wong61395352007-01-27 14:33:08 -08003783 my ($self) = @_;
3784 my $mods = $self->{mods};
Eric Wong44320b92007-01-13 22:35:53 -08003785 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
Eric Wong6e8548c2007-01-27 01:32:00 -08003786 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
Eric Wong44320b92007-01-13 22:35:53 -08003787 my $f = $m->{chg};
3788 if (defined $o{$f}) {
3789 $self->$f($m);
3790 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +02003791 fatal("Invalid change type: $f");
Eric Wong44320b92007-01-13 22:35:53 -08003792 }
3793 }
Eric Wong24e22aa2007-01-29 00:07:49 -08003794 $self->rmdirs if $_rmdir;
Eric Wong6e8548c2007-01-27 01:32:00 -08003795 if (@$mods == 0) {
Eric Wong44320b92007-01-13 22:35:53 -08003796 $self->abort_edit;
3797 } else {
3798 $self->close_edit;
3799 }
Eric Wong6e8548c2007-01-27 01:32:00 -08003800 return scalar @$mods;
Eric Wong44320b92007-01-13 22:35:53 -08003801}
3802
Eric Wongd81bf822007-01-10 01:22:38 -08003803package Git::SVN::Ra;
Eric Wong6af1db42007-02-14 16:04:10 -08003804use vars qw/@ISA $config_dir $_log_window_size/;
Eric Wongd81bf822007-01-10 01:22:38 -08003805use strict;
3806use warnings;
Eric Wonga51cdb02007-09-07 04:00:40 -07003807my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
Eric Wongd81bf822007-01-10 01:22:38 -08003808
3809BEGIN {
3810 # enforce temporary pool usage for some simple functions
Sam Vilainc5f71ad2007-06-15 15:43:59 +12003811 no strict 'refs';
3812 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root/) {
3813 my $SUPER = "SUPER::$f";
3814 *$f = sub {
3815 my $self = shift;
3816 my $pool = SVN::Pool->new;
3817 my @ret = $self->$SUPER(@_,$pool);
3818 $pool->clear;
3819 wantarray ? @ret : $ret[0];
3820 };
Eric Wongd81bf822007-01-10 01:22:38 -08003821 }
Eric Wongd81bf822007-01-10 01:22:38 -08003822}
3823
Steven Walter9ff74e92007-09-28 13:24:19 -04003824sub _auth_providers () {
3825 [
3826 SVN::Client::get_simple_provider(),
3827 SVN::Client::get_ssl_server_trust_file_provider(),
3828 SVN::Client::get_simple_prompt_provider(
3829 \&Git::SVN::Prompt::simple, 2),
3830 SVN::Client::get_ssl_client_cert_file_provider(),
3831 SVN::Client::get_ssl_client_cert_prompt_provider(
3832 \&Git::SVN::Prompt::ssl_client_cert, 2),
Sebastian Noack77266e92008-02-25 15:56:28 +01003833 SVN::Client::get_ssl_client_cert_pw_file_provider(),
Steven Walter9ff74e92007-09-28 13:24:19 -04003834 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
3835 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
3836 SVN::Client::get_username_provider(),
3837 SVN::Client::get_ssl_server_trust_prompt_provider(
3838 \&Git::SVN::Prompt::ssl_server_trust),
3839 SVN::Client::get_username_prompt_provider(
3840 \&Git::SVN::Prompt::username, 2)
3841 ]
3842}
3843
Eric Wongcfbe7ab2007-11-11 23:37:42 -08003844sub escape_uri_only {
3845 my ($uri) = @_;
3846 my @tmp;
3847 foreach (split m{/}, $uri) {
Eric Wongd1a8d0e2008-05-10 17:14:49 -07003848 s/([^\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08003849 push @tmp, $_;
3850 }
3851 join('/', @tmp);
3852}
3853
3854sub escape_url {
3855 my ($url) = @_;
3856 if ($url =~ m#^(https?)://([^/]+)(.*)$#) {
3857 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
3858 $url = "$scheme://$domain$uri";
3859 }
3860 $url;
3861}
3862
Eric Wongd81bf822007-01-10 01:22:38 -08003863sub new {
3864 my ($class, $url) = @_;
Eric Wongf6f09872007-01-18 18:22:18 -08003865 $url =~ s!/+$!!;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08003866 return $RA if ($RA && $RA->{url} eq $url);
Eric Wongf6f09872007-01-18 18:22:18 -08003867
Eric Wongd81bf822007-01-10 01:22:38 -08003868 SVN::_Core::svn_config_ensure($config_dir, undef);
Steven Walter9ff74e92007-09-28 13:24:19 -04003869 my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
Eric Wongd81bf822007-01-10 01:22:38 -08003870 my $config = SVN::Core::config_get_config($config_dir);
Eric Wong7730fbe2007-07-04 14:07:42 -07003871 $RA = undef;
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04003872 my $dont_store_passwords = 1;
3873 my $conf_t = ${$config}{'config'};
3874 {
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003875 no warnings 'once';
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04003876 # The usage of $SVN::_Core::SVN_CONFIG_* variables
3877 # produces warnings that variables are used only once.
3878 # I had not found the better way to shut them up, so
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003879 # the warnings of type 'once' are disabled in this block.
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04003880 if (SVN::_Core::svn_config_get_bool($conf_t,
3881 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
3882 $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
3883 1) == 0) {
3884 SVN::_Core::svn_auth_set_parameter($baton,
3885 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
3886 bless (\$dont_store_passwords, "_p_void"));
3887 }
3888 if (SVN::_Core::svn_config_get_bool($conf_t,
3889 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
3890 $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
3891 1) == 0) {
3892 $Git::SVN::Prompt::_no_auth_cache = 1;
3893 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003894 } # no warnings 'once'
Eric Wongcfbe7ab2007-11-11 23:37:42 -08003895 my $self = SVN::Ra->new(url => escape_url($url), auth => $baton,
Eric Wongd81bf822007-01-10 01:22:38 -08003896 config => $config,
3897 pool => SVN::Pool->new,
3898 auth_provider_callbacks => $callbacks);
Eric Wongcfbe7ab2007-11-11 23:37:42 -08003899 $self->{url} = $url;
Eric Wongd81bf822007-01-10 01:22:38 -08003900 $self->{svn_path} = $url;
3901 $self->{repos_root} = $self->get_repos_root;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003902 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
Eric Wong0dc03d62007-05-13 01:04:43 -07003903 $self->{cache} = { check_path => { r => 0, data => {} },
3904 get_dir => { r => 0, data => {} } };
Eric Wong5d3b7cd2007-01-29 19:16:01 -08003905 $RA = bless $self, $class;
Eric Wongd81bf822007-01-10 01:22:38 -08003906}
3907
Eric Wong0dc03d62007-05-13 01:04:43 -07003908sub check_path {
3909 my ($self, $path, $r) = @_;
3910 my $cache = $self->{cache}->{check_path};
3911 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
3912 return $cache->{data}->{$path};
3913 }
3914 my $pool = SVN::Pool->new;
3915 my $t = $self->SUPER::check_path($path, $r, $pool);
3916 $pool->clear;
3917 if ($r != $cache->{r}) {
3918 %{$cache->{data}} = ();
3919 $cache->{r} = $r;
3920 }
3921 $cache->{data}->{$path} = $t;
3922}
3923
3924sub get_dir {
3925 my ($self, $dir, $r) = @_;
3926 my $cache = $self->{cache}->{get_dir};
3927 if ($r == $cache->{r}) {
3928 if (my $x = $cache->{data}->{$dir}) {
3929 return wantarray ? @$x : $x->[0];
3930 }
3931 }
3932 my $pool = SVN::Pool->new;
3933 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
3934 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
3935 $pool->clear;
3936 if ($r != $cache->{r}) {
3937 %{$cache->{data}} = ();
3938 $cache->{r} = $r;
3939 }
3940 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
3941 wantarray ? (\%dirents, $r, $props) : \%dirents;
3942}
3943
Eric Wongd81bf822007-01-10 01:22:38 -08003944sub DESTROY {
Eric Wong5d3b7cd2007-01-29 19:16:01 -08003945 # do not call the real DESTROY since we store ourselves in $RA
Eric Wongd81bf822007-01-10 01:22:38 -08003946}
3947
Eric Wongd81bf822007-01-10 01:22:38 -08003948sub get_log {
3949 my ($self, @args) = @_;
3950 my $pool = SVN::Pool->new;
Eric Wongd81bf822007-01-10 01:22:38 -08003951 splice(@args, 3, 1) if ($SVN::Core::VERSION le '1.2.0');
3952 my $ret = $self->SUPER::get_log(@args, $pool);
3953 $pool->clear;
3954 $ret;
3955}
3956
Steven Walter9ff74e92007-09-28 13:24:19 -04003957sub trees_match {
3958 my ($self, $url1, $rev1, $url2, $rev2) = @_;
3959 my $ctx = SVN::Client->new(auth => _auth_providers);
3960 my $out = IO::File->new_tmpfile;
3961
3962 # older SVN (1.1.x) doesn't take $pool as the last parameter for
3963 # $ctx->diff(), so we'll create a default one
3964 my $pool = SVN::Pool->new_default_sub;
3965
3966 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
3967 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
3968 $out->flush;
3969 my $ret = (($out->stat)[7] == 0);
3970 close $out or croak $!;
3971
3972 $ret;
3973}
3974
Eric Wongd81bf822007-01-10 01:22:38 -08003975sub get_commit_editor {
Eric Wong44320b92007-01-13 22:35:53 -08003976 my ($self, $log, $cb, $pool) = @_;
Eric Wongd81bf822007-01-10 01:22:38 -08003977 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
Eric Wong44320b92007-01-13 22:35:53 -08003978 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08003979}
3980
Eric Wongd81bf822007-01-10 01:22:38 -08003981sub gs_do_update {
Eric Wong8a603772007-01-31 02:45:50 -08003982 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
3983 my $new = ($rev_a == $rev_b);
3984 my $path = $gs->{path};
3985
Eric Wong2e5e2482007-02-23 02:21:59 -08003986 if ($new && -e $gs->{index}) {
3987 unlink $gs->{index} or die
3988 "Couldn't unlink index: $gs->{index}: $!\n";
3989 }
Eric Wongd81bf822007-01-10 01:22:38 -08003990 my $pool = SVN::Pool->new;
Eric Wong8b8fc062007-01-22 11:44:57 -08003991 $editor->set_path_strip($path);
Eric Wong2b27f6c2007-01-28 04:59:05 -08003992 my (@pc) = split m#/#, $path;
3993 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
Eric Wong8a603772007-01-31 02:45:50 -08003994 1, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08003995 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong2b27f6c2007-01-28 04:59:05 -08003996
3997 # Since we can't rely on svn_ra_reparent being available, we'll
3998 # just have to do some magic with set_path to make it so
3999 # we only want a partial path.
4000 my $sp = '';
4001 my $final = join('/', @pc);
4002 while (@pc) {
4003 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
4004 $sp .= '/' if length $sp;
4005 $sp .= shift @pc;
4006 }
4007 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
4008
Eric Wong2b27f6c2007-01-28 04:59:05 -08004009 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
4010
Eric Wongd81bf822007-01-10 01:22:38 -08004011 $reporter->finish_report($pool);
4012 $pool->clear;
4013 $editor->{git_commit_ok};
4014}
4015
Eric Wong2b27f6c2007-01-28 04:59:05 -08004016# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
4017# svn_ra_reparent didn't work before 1.4)
Eric Wongd81bf822007-01-10 01:22:38 -08004018sub gs_do_switch {
Eric Wong8a603772007-01-31 02:45:50 -08004019 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
4020 my $path = $gs->{path};
Eric Wongd81bf822007-01-10 01:22:38 -08004021 my $pool = SVN::Pool->new;
Eric Wong2b27f6c2007-01-28 04:59:05 -08004022
4023 my $full_url = $self->{url};
4024 my $old_url = $full_url;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004025 $full_url .= '/' . escape_uri_only($path) if length $path;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004026 my ($ra, $reparented);
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004027
4028 if ($old_url =~ m#^svn(\+ssh)?://#) {
4029 $_[0] = undef;
4030 $self = undef;
4031 $RA = undef;
4032 $ra = Git::SVN::Ra->new($full_url);
4033 $ra_invalid = 1;
4034 } elsif ($old_url ne $full_url) {
4035 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, $pool);
4036 $self->{url} = $full_url;
4037 $reparented = 1;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004038 }
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004039
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004040 $ra ||= $self;
Eric Wongf4392df2008-09-06 20:18:18 -07004041 $url_b = escape_url($url_b);
Eric Wong8a603772007-01-31 02:45:50 -08004042 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004043 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong8b8fc062007-01-22 11:44:57 -08004044 $reporter->set_path('', $rev_a, 0, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004045 $reporter->finish_report($pool);
Eric Wong2b27f6c2007-01-28 04:59:05 -08004046
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004047 if ($reparented) {
4048 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
4049 $self->{url} = $old_url;
4050 }
Eric Wong2b27f6c2007-01-28 04:59:05 -08004051
Eric Wongd81bf822007-01-10 01:22:38 -08004052 $pool->clear;
4053 $editor->{git_commit_ok};
4054}
4055
Eric Wongb54a9012007-06-13 02:37:03 -07004056sub longest_common_path {
4057 my ($gsv, $globs) = @_;
Eric Wongd2ae1432007-02-07 11:50:16 -08004058 my %common;
Eric Wonge5181922007-02-08 12:53:57 -08004059 my $common_max = scalar @$gsv;
4060
4061 foreach my $gs (@$gsv) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004062 my @tmp = split m#/#, $gs->{path};
4063 my $p = '';
4064 foreach (@tmp) {
4065 $p .= length($p) ? "/$_" : $_;
4066 $common{$p} ||= 0;
4067 $common{$p}++;
4068 }
4069 }
Eric Wonge5181922007-02-08 12:53:57 -08004070 $globs ||= [];
4071 $common_max += scalar @$globs;
4072 foreach my $glob (@$globs) {
4073 my @tmp = split m#/#, $glob->{path}->{left};
4074 my $p = '';
4075 foreach (@tmp) {
4076 $p .= length($p) ? "/$_" : $_;
4077 $common{$p} ||= 0;
4078 $common{$p}++;
4079 }
4080 }
4081
Eric Wongd2ae1432007-02-07 11:50:16 -08004082 my $longest_path = '';
4083 foreach (sort {length $b <=> length $a} keys %common) {
Eric Wonge5181922007-02-08 12:53:57 -08004084 if ($common{$_} == $common_max) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004085 $longest_path = $_;
4086 last;
4087 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004088 }
Eric Wongb54a9012007-06-13 02:37:03 -07004089 $longest_path;
4090}
4091
4092sub gs_fetch_loop_common {
4093 my ($self, $base, $head, $gsv, $globs) = @_;
4094 return if ($base > $head);
4095 my $inc = $_log_window_size;
4096 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
4097 my $longest_path = longest_common_path($gsv, $globs);
Eric Wonga51cdb02007-09-07 04:00:40 -07004098 my $ra_url = $self->{url};
Eric Wong0af9c9f2007-01-27 22:28:56 -08004099 while (1) {
Eric Wongd4eff2b2007-01-30 14:04:22 -08004100 my %revs;
Eric Wongd2ae1432007-02-07 11:50:16 -08004101 my $err;
Eric Wongf7c3fc42007-01-29 18:34:55 -08004102 my $err_handler = $SVN::Error::handler;
Eric Wongd2ae1432007-02-07 11:50:16 -08004103 $SVN::Error::handler = sub {
4104 ($err) = @_;
4105 skip_unknown_revs($err);
4106 };
4107 sub _cb {
4108 my ($paths, $r, $author, $date, $log) = @_;
4109 [ dup_changed_paths($paths),
4110 { author => $author, date => $date, log => $log } ];
4111 }
4112 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
4113 sub { $revs{$_[1]} = _cb(@_) });
4114 if ($err && $max >= $head) {
4115 print STDERR "Path '$longest_path' ",
4116 "was probably deleted:\n",
4117 $err->expanded_message,
4118 "\nWill attempt to follow ",
4119 "revisions r$min .. r$max ",
4120 "committed before the deletion\n";
4121 my $hi = $max;
4122 while (--$hi >= $min) {
4123 my $ok;
4124 $self->get_log([$longest_path], $min, $hi,
4125 0, 1, 1, sub {
4126 $ok ||= $_[1];
4127 $revs{$_[1]} = _cb(@_) });
4128 if ($ok) {
4129 print STDERR "r$min .. r$ok OK\n";
4130 last;
4131 }
4132 }
4133 }
Eric Wongd4eff2b2007-01-30 14:04:22 -08004134 $SVN::Error::handler = $err_handler;
Eric Wongfbcc1732007-02-06 18:35:30 -08004135
Eric Wonge5181922007-02-08 12:53:57 -08004136 my %exists = map { $_->{path} => $_ } @$gsv;
Eric Wongd4eff2b2007-01-30 14:04:22 -08004137 foreach my $r (sort {$a <=> $b} keys %revs) {
Eric Wongfbcc1732007-02-06 18:35:30 -08004138 my ($paths, $logged) = @{$revs{$r}};
Eric Wonge5181922007-02-08 12:53:57 -08004139
4140 foreach my $gs ($self->match_globs(\%exists, $paths,
4141 $globs, $r)) {
Eric Wong060610c2007-12-08 23:27:41 -08004142 if ($gs->rev_map_max >= $r) {
Eric Wongfbcc1732007-02-06 18:35:30 -08004143 next;
4144 }
4145 next unless $gs->match_paths($paths, $r);
4146 $gs->{logged_rev_props} = $logged;
Eric Wonge8d120b2007-02-14 16:29:52 -08004147 if (my $last_commit = $gs->last_commit) {
4148 $gs->assert_index_clean($last_commit);
4149 }
Eric Wongfbcc1732007-02-06 18:35:30 -08004150 my $log_entry = $gs->do_fetch($paths, $r);
4151 if ($log_entry) {
Eric Wong0af9c9f2007-01-27 22:28:56 -08004152 $gs->do_git_commit($log_entry);
4153 }
Eric Wong321b1842008-01-02 10:10:03 -08004154 $INDEX_FILES{$gs->{index}} = 1;
Eric Wong0af9c9f2007-01-27 22:28:56 -08004155 }
Eric Wonge5181922007-02-08 12:53:57 -08004156 foreach my $g (@$globs) {
Eric Wong93f26892007-02-11 01:20:26 -08004157 my $k = "svn-remote.$g->{remote}." .
4158 "$g->{t}-maxRev";
4159 Git::SVN::tmp_config($k, $r);
Eric Wonge5181922007-02-08 12:53:57 -08004160 }
Eric Wonga51cdb02007-09-07 04:00:40 -07004161 if ($ra_invalid) {
4162 $_[0] = undef;
4163 $self = undef;
4164 $RA = undef;
4165 $self = Git::SVN::Ra->new($ra_url);
4166 $ra_invalid = undef;
4167 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004168 }
Eric Wong9c93fee2007-01-31 17:22:31 -08004169 # pre-fill the .rev_db since it'll eventually get filled in
4170 # with '0' x40 if something new gets committed
Eric Wonge5181922007-02-08 12:53:57 -08004171 foreach my $gs (@$gsv) {
Eric Wong66ab84b2007-12-08 23:27:42 -08004172 next if $gs->rev_map_max >= $max;
4173 next if defined $gs->rev_map_get($max);
4174 $gs->rev_map_set($max, 0 x40);
Eric Wong9c93fee2007-01-31 17:22:31 -08004175 }
Eric Wongc3560e52007-02-12 16:03:32 -08004176 foreach my $g (@$globs) {
4177 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
4178 Git::SVN::tmp_config($k, $max);
4179 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004180 last if $max >= $head;
4181 $min = $max + 1;
4182 $max += $inc;
4183 $max = $head if ($max > $head);
4184 }
Karl Hasselström94bc9142008-02-03 17:56:18 +01004185 Git::SVN::gc();
Eric Wong0af9c9f2007-01-27 22:28:56 -08004186}
4187
Marcus Griep570d35c2008-08-08 01:41:57 -07004188sub get_dir_globbed {
4189 my ($self, $left, $depth, $r) = @_;
4190
4191 my @x = eval { $self->get_dir($left, $r) };
4192 return unless scalar @x == 3;
4193 my $dirents = $x[0];
4194 my @finalents;
4195 foreach my $de (keys %$dirents) {
4196 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
4197 if ($depth > 1) {
4198 my @args = ("$left/$de", $depth - 1, $r);
4199 foreach my $dir ($self->get_dir_globbed(@args)) {
4200 push @finalents, "$de/$dir";
4201 }
4202 } else {
4203 push @finalents, $de;
4204 }
4205 }
4206 @finalents;
4207}
4208
Eric Wonge5181922007-02-08 12:53:57 -08004209sub match_globs {
4210 my ($self, $exists, $paths, $globs, $r) = @_;
Eric Wong74a81222007-02-10 13:28:50 -08004211
4212 sub get_dir_check {
4213 my ($self, $exists, $g, $r) = @_;
Marcus Griep570d35c2008-08-08 01:41:57 -07004214
4215 my @dirs = $self->get_dir_globbed($g->{path}->{left},
4216 $g->{path}->{depth},
4217 $r);
4218
4219 foreach my $de (@dirs) {
Eric Wong74a81222007-02-10 13:28:50 -08004220 my $p = $g->{path}->full_path($de);
4221 next if $exists->{$p};
4222 next if (length $g->{path}->{right} &&
4223 ($self->check_path($p, $r) !=
4224 $SVN::Node::dir));
4225 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
4226 $g->{ref}->full_path($de), 1);
4227 }
4228 }
Eric Wonge5181922007-02-08 12:53:57 -08004229 foreach my $g (@$globs) {
Eric Wong74a81222007-02-10 13:28:50 -08004230 if (my $path = $paths->{"/$g->{path}->{left}"}) {
4231 if ($path->{action} =~ /^[AR]$/) {
4232 get_dir_check($self, $exists, $g, $r);
4233 }
4234 }
Eric Wonge5181922007-02-08 12:53:57 -08004235 foreach (keys %$paths) {
Eric Wong28710f72007-02-14 13:32:21 -08004236 if (/$g->{path}->{left_regex}/ &&
4237 !/$g->{path}->{regex}/) {
Eric Wong74a81222007-02-10 13:28:50 -08004238 next if $paths->{$_}->{action} !~ /^[AR]$/;
4239 get_dir_check($self, $exists, $g, $r);
4240 }
Eric Wonge5181922007-02-08 12:53:57 -08004241 next unless /$g->{path}->{regex}/;
4242 my $p = $1;
4243 my $pathname = $g->{path}->full_path($p);
4244 next if $exists->{$pathname};
Eric Wong0c1ec5a2007-04-18 00:17:33 -07004245 next if ($self->check_path($pathname, $r) !=
4246 $SVN::Node::dir);
Eric Wonge5181922007-02-08 12:53:57 -08004247 $exists->{$pathname} = Git::SVN->init(
4248 $self->{url}, $pathname, undef,
4249 $g->{ref}->full_path($p), 1);
4250 }
4251 my $c = '';
4252 foreach (split m#/#, $g->{path}->{left}) {
4253 $c .= "/$_";
4254 next unless ($paths->{$c} &&
Eric Wong74a81222007-02-10 13:28:50 -08004255 ($paths->{$c}->{action} =~ /^[AR]$/));
4256 get_dir_check($self, $exists, $g, $r);
Eric Wonge5181922007-02-08 12:53:57 -08004257 }
4258 }
4259 values %$exists;
4260}
4261
Eric Wonge6434f82007-01-23 16:29:23 -08004262sub minimize_url {
4263 my ($self) = @_;
4264 return $self->{url} if ($self->{url} eq $self->{repos_root});
4265 my $url = $self->{repos_root};
4266 my @components = split(m!/!, $self->{svn_path});
4267 my $c = '';
4268 do {
4269 $url .= "/$c" if length $c;
4270 eval { (ref $self)->new($url)->get_latest_revnum };
4271 } while ($@ && ($c = shift @components));
4272 $url;
4273}
4274
Eric Wongd81bf822007-01-10 01:22:38 -08004275sub can_do_switch {
4276 my $self = shift;
4277 unless (defined $can_do_switch) {
4278 my $pool = SVN::Pool->new;
4279 my $rep = eval {
4280 $self->do_switch(1, '', 0, $self->{url},
4281 SVN::Delta::Editor->new, $pool);
4282 };
4283 if ($@) {
4284 $can_do_switch = 0;
4285 } else {
4286 $rep->abort_report($pool);
4287 $can_do_switch = 1;
4288 }
4289 $pool->clear;
4290 }
4291 $can_do_switch;
4292}
4293
Eric Wong0af9c9f2007-01-27 22:28:56 -08004294sub skip_unknown_revs {
4295 my ($err) = @_;
4296 my $errno = $err->apr_err();
4297 # Maybe the branch we're tracking didn't
4298 # exist when the repo started, so it's
4299 # not an error if it doesn't, just continue
4300 #
4301 # Wonderfully consistent library, eh?
4302 # 160013 - svn:// and file://
4303 # 175002 - http(s)://
4304 # 175007 - http(s):// (this repo required authorization, too...)
4305 # More codes may be discovered later...
4306 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
Eric Wonga6a15a92007-03-30 17:54:48 -07004307 my $err_key = $err->expanded_message;
4308 # revision numbers change every time, filter them out
4309 $err_key =~ s/\d+/\0/g;
4310 $err_key = "$errno\0$err_key";
4311 unless ($ignored_err{$err_key}) {
4312 warn "W: Ignoring error from SVN, path probably ",
4313 "does not exist: ($errno): ",
4314 $err->expanded_message,"\n";
Eric Wongeee8a172008-01-07 02:40:40 -08004315 warn "W: Do not be alarmed at the above message ",
4316 "git-svn is just searching aggressively for ",
4317 "old history.\n",
4318 "This may take a while on large repositories\n";
Eric Wonga6a15a92007-03-30 17:54:48 -07004319 $ignored_err{$err_key} = 1;
4320 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004321 return;
4322 }
4323 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
4324}
4325
4326# svn_log_changed_path_t objects passed to get_log are likely to be
4327# overwritten even if only the refs are copied to an external variable,
4328# so we should dup the structures in their entirety. Using an externally
4329# passed pool (instead of our temporary and quickly cleared pool in
4330# Git::SVN::Ra) does not help matters at all...
4331sub dup_changed_paths {
4332 my ($paths) = @_;
4333 return undef unless $paths;
4334 my %ret;
4335 foreach my $p (keys %$paths) {
4336 my $i = $paths->{$p};
4337 my %s = map { $_ => $i->$_ }
4338 qw/copyfrom_path copyfrom_rev action/;
4339 $ret{$p} = \%s;
4340 }
4341 \%ret;
4342}
4343
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004344package Git::SVN::Log;
4345use strict;
4346use warnings;
4347use POSIX qw/strftime/;
David D Kilzer111947e2007-11-11 22:56:52 -08004348use constant commit_log_separator => ('-' x 72) . "\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004349use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
4350 %rusers $show_commit $incremental/;
4351my $l_fmt;
4352
4353sub cmt_showable {
4354 my ($c) = @_;
4355 return 1 if defined $c->{r};
Eric Wongc16d0872007-04-08 00:59:22 -07004356
4357 # big commit message got truncated by the 16k pretty buffer in rev-list
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004358 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
4359 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
Eric Wongc16d0872007-04-08 00:59:22 -07004360 @{$c->{l}} = ();
Eric Wong44320b92007-01-13 22:35:53 -08004361 my @log = command(qw/cat-file commit/, $c->{c});
Eric Wongc16d0872007-04-08 00:59:22 -07004362
4363 # shift off the headers
4364 shift @log while ($log[0] ne '');
Eric Wong44320b92007-01-13 22:35:53 -08004365 shift @log;
Eric Wongc16d0872007-04-08 00:59:22 -07004366
4367 # TODO: make $c->{l} not have a trailing newline in the future
4368 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004369
4370 (undef, $c->{r}, undef) = ::extract_metadata(
Eric Wong44320b92007-01-13 22:35:53 -08004371 (grep(/^git-svn-id: /, @log))[-1]);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004372 }
4373 return defined $c->{r};
4374}
4375
4376sub log_use_color {
Jeff Kingcd459e32007-12-11 01:28:42 -05004377 return $color || Git->repository->get_colorbool('color.diff');
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004378}
4379
4380sub git_svn_log_cmd {
Eric Wong3bc718b2007-02-13 17:09:40 -08004381 my ($r_min, $r_max, @args) = @_;
4382 my $head = 'HEAD';
Eric Wong6ed77262007-08-15 09:55:18 -07004383 my (@files, @log_opts);
Eric Wong3bc718b2007-02-13 17:09:40 -08004384 foreach my $x (@args) {
Eric Wong6ed77262007-08-15 09:55:18 -07004385 if ($x eq '--' || @files) {
4386 push @files, $x;
4387 } else {
4388 if (::verify_ref("$x^0")) {
4389 $head = $x;
4390 } else {
4391 push @log_opts, $x;
4392 }
4393 }
Eric Wong3bc718b2007-02-13 17:09:40 -08004394 }
4395
Eric Wong13c823f2007-04-08 00:59:19 -07004396 my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
4397 $gs ||= Git::SVN->_new;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004398 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
4399 $gs->refname);
4400 push @cmd, '-r' unless $non_recursive;
4401 push @cmd, qw/--raw --name-status/ if $verbose;
4402 push @cmd, '--color' if log_use_color();
Eric Wong6ed77262007-08-15 09:55:18 -07004403 push @cmd, @log_opts;
4404 if (defined $r_max && $r_max == $r_min) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004405 push @cmd, '--max-count=1';
Eric Wong060610c2007-12-08 23:27:41 -08004406 if (my $c = $gs->rev_map_get($r_max)) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004407 push @cmd, $c;
4408 }
Eric Wong6ed77262007-08-15 09:55:18 -07004409 } elsif (defined $r_max) {
David D Kilzer111947e2007-11-11 22:56:52 -08004410 if ($r_max < $r_min) {
4411 ($r_min, $r_max) = ($r_max, $r_min);
4412 }
4413 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
4414 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
4415 # If there are no commits in the range, both $c_max and $c_min
4416 # will be undefined. If there is at least 1 commit in the
4417 # range, both will be defined.
4418 return () if !defined $c_min || !defined $c_max;
4419 if ($c_min eq $c_max) {
4420 push @cmd, '--max-count=1', $c_min;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004421 } else {
David D Kilzer111947e2007-11-11 22:56:52 -08004422 push @cmd, '--boundary', "$c_min..$c_max";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004423 }
4424 }
Eric Wong6ed77262007-08-15 09:55:18 -07004425 return (@cmd, @files);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004426}
4427
4428# adapted from pager.c
4429sub config_pager {
4430 $pager ||= $ENV{GIT_PAGER} || $ENV{PAGER};
4431 if (!defined $pager) {
4432 $pager = 'less';
4433 } elsif (length $pager == 0 || $pager eq 'cat') {
4434 $pager = undef;
4435 }
Jeff Kingcd459e32007-12-11 01:28:42 -05004436 $ENV{GIT_PAGER_IN_USE} = defined($pager);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004437}
4438
4439sub run_pager {
Eric Wongb0193042007-09-21 18:48:45 -07004440 return unless -t *STDOUT && defined $pager;
Marcus Griep971e6282008-09-10 11:09:46 -04004441 pipe my ($rfd, $wfd) or return;
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004442 defined(my $pid = fork) or ::fatal "Can't fork: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004443 if (!$pid) {
4444 open STDOUT, '>&', $wfd or
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004445 ::fatal "Can't redirect to stdout: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004446 return;
4447 }
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004448 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004449 $ENV{LESS} ||= 'FRSX';
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004450 exec $pager or ::fatal "Can't run pager: $! ($pager)";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004451}
4452
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004453sub format_svn_date {
4454 return strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)", localtime(shift));
4455}
4456
4457sub parse_git_date {
4458 my ($t, $tz) = @_;
4459 # Date::Parse isn't in the standard Perl distro :(
4460 if ($tz =~ s/^\+//) {
4461 $t += tz_to_s_offset($tz);
4462 } elsif ($tz =~ s/^\-//) {
4463 $t -= tz_to_s_offset($tz);
4464 }
4465 return $t;
4466}
4467
4468sub set_local_timezone {
4469 if (defined $TZ) {
4470 $ENV{TZ} = $TZ;
4471 } else {
4472 delete $ENV{TZ};
4473 }
4474}
4475
Eric Wong21819a32007-01-27 14:38:10 -08004476sub tz_to_s_offset {
4477 my ($tz) = @_;
4478 $tz =~ s/(\d\d)$//;
4479 return ($1 * 60) + ($tz * 3600);
4480}
4481
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004482sub get_author_info {
4483 my ($dest, $author, $t, $tz) = @_;
4484 $author =~ s/(?:^\s*|\s*$)//g;
4485 $dest->{a_raw} = $author;
4486 my $au;
Eric Wong1c8443b2007-01-14 02:17:00 -08004487 if ($::_authors) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004488 $au = $rusers{$author} || undef;
4489 }
4490 if (!$au) {
4491 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
4492 }
4493 $dest->{t} = $t;
4494 $dest->{tz} = $tz;
4495 $dest->{a} = $au;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004496 $dest->{t_utc} = parse_git_date($t, $tz);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004497}
4498
4499sub process_commit {
4500 my ($c, $r_min, $r_max, $defer) = @_;
4501 if (defined $r_min && defined $r_max) {
4502 if ($r_min == $c->{r} && $r_min == $r_max) {
4503 show_commit($c);
4504 return 0;
4505 }
4506 return 1 if $r_min == $r_max;
4507 if ($r_min < $r_max) {
4508 # we need to reverse the print order
4509 return 0 if (defined $limit && --$limit < 0);
4510 push @$defer, $c;
4511 return 1;
4512 }
4513 if ($r_min != $r_max) {
4514 return 1 if ($r_min < $c->{r});
4515 return 1 if ($r_max > $c->{r});
4516 }
4517 }
4518 return 0 if (defined $limit && --$limit < 0);
4519 show_commit($c);
4520 return 1;
4521}
4522
4523sub show_commit {
4524 my $c = shift;
4525 if ($oneline) {
4526 my $x = "\n";
4527 if (my $l = $c->{l}) {
4528 while ($l->[0] =~ /^\s*$/) { shift @$l }
4529 $x = $l->[0];
4530 }
4531 $l_fmt ||= 'A' . length($c->{r});
4532 print 'r',pack($l_fmt, $c->{r}),' | ';
4533 print "$c->{c} | " if $show_commit;
4534 print $x;
4535 } else {
4536 show_commit_normal($c);
4537 }
4538}
4539
4540sub show_commit_changed_paths {
4541 my ($c) = @_;
4542 return unless $c->{changed};
4543 print "Changed paths:\n", @{$c->{changed}};
4544}
4545
4546sub show_commit_normal {
4547 my ($c) = @_;
David D Kilzer111947e2007-11-11 22:56:52 -08004548 print commit_log_separator, "r$c->{r} | ";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004549 print "$c->{c} | " if $show_commit;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004550 print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004551 my $nr_line = 0;
4552
4553 if (my $l = $c->{l}) {
4554 while ($l->[$#$l] eq "\n" && $#$l > 0
4555 && $l->[($#$l - 1)] eq "\n") {
4556 pop @$l;
4557 }
4558 $nr_line = scalar @$l;
4559 if (!$nr_line) {
4560 print "1 line\n\n\n";
4561 } else {
4562 if ($nr_line == 1) {
4563 $nr_line = '1 line';
4564 } else {
4565 $nr_line .= ' lines';
4566 }
4567 print $nr_line, "\n";
4568 show_commit_changed_paths($c);
4569 print "\n";
4570 print $_ foreach @$l;
4571 }
4572 } else {
4573 print "1 line\n";
4574 show_commit_changed_paths($c);
4575 print "\n";
4576
4577 }
Eric Wong488a63e2007-02-15 00:40:42 -08004578 foreach my $x (qw/raw stat diff/) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004579 if ($c->{$x}) {
4580 print "\n";
4581 print $_ foreach @{$c->{$x}}
4582 }
4583 }
4584}
4585
4586sub cmd_show_log {
4587 my (@args) = @_;
4588 my ($r_min, $r_max);
4589 my $r_last = -1; # prevent dupes
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004590 set_local_timezone();
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004591 if (defined $::_revision) {
4592 if ($::_revision =~ /^(\d+):(\d+)$/) {
4593 ($r_min, $r_max) = ($1, $2);
4594 } elsif ($::_revision =~ /^\d+$/) {
4595 $r_min = $r_max = $::_revision;
4596 } else {
4597 ::fatal "-r$::_revision is not supported, use ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004598 "standard 'git log' arguments instead";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004599 }
4600 }
4601
4602 config_pager();
Eric Wong6ed77262007-08-15 09:55:18 -07004603 @args = git_svn_log_cmd($r_min, $r_max, @args);
David D Kilzer111947e2007-11-11 22:56:52 -08004604 if (!@args) {
4605 print commit_log_separator unless $incremental || $oneline;
4606 return;
4607 }
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004608 my $log = command_output_pipe(@args);
4609 run_pager();
Eric Wong488a63e2007-02-15 00:40:42 -08004610 my (@k, $c, $d, $stat);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004611 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
4612 while (<$log>) {
David D Kilzer60f3ff12007-11-10 22:10:34 -08004613 if (/^${esc_color}commit -?($::sha1_short)/o) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004614 my $cmt = $1;
4615 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
4616 $r_last = $c->{r};
4617 process_commit($c, $r_min, $r_max, \@k) or
4618 goto out;
4619 }
4620 $d = undef;
4621 $c = { c => $cmt };
4622 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
4623 get_author_info($c, $1, $2, $3);
4624 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
4625 # ignore
4626 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
4627 push @{$c->{raw}}, $_;
4628 } elsif (/^${esc_color}[ACRMDT]\t/) {
4629 # we could add $SVN->{svn_path} here, but that requires
4630 # remote access at the moment (repo_path_split)...
4631 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
4632 push @{$c->{changed}}, $_;
4633 } elsif (/^${esc_color}diff /o) {
4634 $d = 1;
4635 push @{$c->{diff}}, $_;
4636 } elsif ($d) {
4637 push @{$c->{diff}}, $_;
Eric Wong488a63e2007-02-15 00:40:42 -08004638 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
4639 $esc_color*[\+\-]*$esc_color$/x) {
4640 $stat = 1;
4641 push @{$c->{stat}}, $_;
4642 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
4643 push @{$c->{stat}}, $_;
4644 $stat = undef;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004645 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
4646 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
4647 } elsif (s/^${esc_color} //o) {
4648 push @{$c->{l}}, $_;
4649 }
4650 }
4651 if ($c && defined $c->{r} && $c->{r} != $r_last) {
4652 $r_last = $c->{r};
4653 process_commit($c, $r_min, $r_max, \@k);
4654 }
4655 if (@k) {
David D Kilzer111947e2007-11-11 22:56:52 -08004656 ($r_min, $r_max) = ($r_max, $r_min);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004657 process_commit($_, $r_min, $r_max) foreach reverse @k;
4658 }
4659out:
Eric Wongc843c462007-01-12 03:07:31 -08004660 close $log;
David D Kilzer111947e2007-11-11 22:56:52 -08004661 print commit_log_separator unless $incremental || $oneline;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004662}
4663
Tim Stoakes6fb53752008-02-10 15:21:08 +10304664sub cmd_blame {
Steven Grimm4be40382008-05-10 22:11:18 -07004665 my $path = pop;
Tim Stoakes6fb53752008-02-10 15:21:08 +10304666
4667 config_pager();
4668 run_pager();
4669
Steven Grimm4be40382008-05-10 22:11:18 -07004670 my ($fh, $ctx, $rev);
4671
4672 if ($_git_format) {
4673 ($fh, $ctx) = command_output_pipe('blame', @_, $path);
4674 while (my $line = <$fh>) {
4675 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
4676 # Uncommitted edits show up as a rev ID of
4677 # all zeros, which we can't look up with
4678 # cmt_metadata
4679 if ($1 !~ /^0+$/) {
4680 (undef, $rev, undef) =
4681 ::cmt_metadata($1);
4682 $rev = '0' if (!$rev);
4683 } else {
4684 $rev = '0';
4685 }
4686 $rev = sprintf('%-10s', $rev);
4687 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
4688 }
4689 print $line;
Tim Stoakes6fb53752008-02-10 15:21:08 +10304690 }
Steven Grimm4be40382008-05-10 22:11:18 -07004691 } else {
4692 ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD',
4693 '--', $path);
4694 my ($sha1);
4695 my %authors;
4696 while (my $line = <$fh>) {
4697 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
4698 $sha1 = $1;
4699 (undef, $rev, undef) = ::cmt_metadata($1);
4700 $rev = '0' if (!$rev);
4701 }
4702 elsif ($line =~ /^author (.*)/) {
4703 $authors{$rev} = $1;
4704 $authors{$rev} =~ s/\s/_/g;
4705 }
4706 elsif ($line =~ /^\t(.*)$/) {
4707 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
4708 }
4709 }
Tim Stoakes6fb53752008-02-10 15:21:08 +10304710 }
4711 command_close_pipe($fh, $ctx);
4712}
4713
Eric Wong706587f2007-01-18 17:50:01 -08004714package Git::SVN::Migration;
4715# these version numbers do NOT correspond to actual version numbers
4716# of git nor git-svn. They are just relative.
4717#
4718# v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
4719#
4720# v1 layout: .git/$id/info/url, refs/remotes/$id
4721#
4722# v2 layout: .git/svn/$id/info/url, refs/remotes/$id
4723#
4724# v3 layout: .git/svn/$id, refs/remotes/$id
4725# - info/url may remain for backwards compatibility
4726# - this is what we migrate up to this layout automatically,
4727# - this will be used by git svn init on single branches
Eric Wong26a62d52007-02-12 13:25:25 -08004728# v3.1 layout (auto migrated):
4729# - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
4730# for backwards compatibility
Eric Wong706587f2007-01-18 17:50:01 -08004731#
4732# v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
4733# - this is only created for newly multi-init-ed
4734# repositories. Similar in spirit to the
4735# --use-separate-remotes option in git-clone (now default)
4736# - we do not automatically migrate to this (following
4737# the example set by core git)
Eric Wong060610c2007-12-08 23:27:41 -08004738#
4739# v5 layout: .rev_db.$UUID => .rev_map.$UUID
4740# - newer, more-efficient format that uses 24-bytes per record
4741# with no filler space.
4742# - use xxd -c24 < .rev_map.$UUID to view and debug
4743# - This is a one-way migration, repositories updated to the
4744# new format will not be able to use old git-svn without
4745# rebuilding the .rev_db. Rebuilding the rev_db is not
4746# possible if noMetadata or useSvmProps are set; but should
4747# be no problem for users that use the (sensible) defaults.
Eric Wong706587f2007-01-18 17:50:01 -08004748use strict;
4749use warnings;
4750use Carp qw/croak/;
4751use File::Path qw/mkpath/;
Eric Wong47e39c52007-01-21 04:27:09 -08004752use File::Basename qw/dirname basename/;
4753use vars qw/$_minimize/;
Eric Wong706587f2007-01-18 17:50:01 -08004754
4755sub migrate_from_v0 {
4756 my $git_dir = $ENV{GIT_DIR};
4757 return undef unless -d $git_dir;
4758 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
4759 my $migrated = 0;
4760 while (<$fh>) {
4761 chomp;
4762 my ($id, $orig_ref) = ($_, $_);
4763 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
4764 next unless -f "$git_dir/$id/info/url";
4765 my $new_ref = "refs/remotes/$id";
4766 if (::verify_ref("$new_ref^0")) {
4767 print STDERR "W: $orig_ref is probably an old ",
4768 "branch used by an ancient version of ",
4769 "git-svn.\n",
4770 "However, $new_ref also exists.\n",
4771 "We will not be able ",
4772 "to use this branch until this ",
4773 "ambiguity is resolved.\n";
4774 next;
4775 }
4776 print STDERR "Migrating from v0 layout...\n" if !$migrated;
4777 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
4778 command_noisy('update-ref', $new_ref, $orig_ref);
4779 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
4780 $migrated++;
4781 }
4782 command_close_pipe($fh, $ctx);
4783 print STDERR "Done migrating from v0 layout...\n" if $migrated;
4784 $migrated;
4785}
4786
4787sub migrate_from_v1 {
4788 my $git_dir = $ENV{GIT_DIR};
4789 my $migrated = 0;
4790 return $migrated unless -d $git_dir;
4791 my $svn_dir = "$git_dir/svn";
4792
4793 # just in case somebody used 'svn' as their $id at some point...
4794 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
4795
4796 print STDERR "Migrating from a git-svn v1 layout...\n";
4797 mkpath([$svn_dir]);
4798 print STDERR "Data from a previous version of git-svn exists, but\n\t",
4799 "$svn_dir\n\t(required for this version ",
Frederik Schwarzer8f510be2008-07-14 18:30:24 +02004800 "($::VERSION) of git-svn) does not exist.\n";
Eric Wong706587f2007-01-18 17:50:01 -08004801 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
4802 while (<$fh>) {
4803 my $x = $_;
4804 next unless $x =~ s#^refs/remotes/##;
4805 chomp $x;
4806 next unless -f "$git_dir/$x/info/url";
4807 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
4808 next unless $u;
4809 my $dn = dirname("$git_dir/svn/$x");
4810 mkpath([$dn]) unless -d $dn;
4811 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
4812 mkpath(["$git_dir/svn/svn"]);
4813 print STDERR " - $git_dir/$x/info => ",
4814 "$git_dir/svn/$x/info\n";
4815 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
4816 croak "$!: $x";
4817 # don't worry too much about these, they probably
4818 # don't exist with repos this old (save for index,
4819 # and we can easily regenerate that)
4820 foreach my $f (qw/unhandled.log index .rev_db/) {
4821 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
4822 }
4823 } else {
4824 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
4825 rename "$git_dir/$x", "$git_dir/svn/$x" or
4826 croak "$!: $x";
4827 }
4828 $migrated++;
4829 }
4830 command_close_pipe($fh, $ctx);
4831 print STDERR "Done migrating from a git-svn v1 layout\n";
4832 $migrated;
4833}
4834
4835sub read_old_urls {
4836 my ($l_map, $pfx, $path) = @_;
4837 my @dir;
4838 foreach (<$path/*>) {
4839 if (-r "$_/info/url") {
4840 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
4841 my $ref_id = $pfx . basename $_;
4842 my $url = ::file_to_s("$_/info/url");
4843 $l_map->{$ref_id} = $url;
4844 } elsif (-d $_) {
4845 push @dir, $_;
4846 }
4847 }
4848 foreach (@dir) {
4849 my $x = $_;
4850 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
4851 read_old_urls($l_map, $x, $_);
4852 }
4853}
4854
4855sub migrate_from_v2 {
4856 my @cfg = command(qw/config -l/);
4857 return if grep /^svn-remote\..+\.url=/, @cfg;
4858 my %l_map;
4859 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
4860 my $migrated = 0;
4861
4862 foreach my $ref_id (sort keys %l_map) {
Eric Wong471bc002007-02-01 04:06:27 -08004863 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
4864 if ($@) {
4865 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
4866 }
Eric Wong706587f2007-01-18 17:50:01 -08004867 $migrated++;
4868 }
4869 $migrated;
4870}
4871
Eric Wong47e39c52007-01-21 04:27:09 -08004872sub minimize_connections {
4873 my $r = Git::SVN::read_all_remotes();
4874 my $new_urls = {};
4875 my $root_repos = {};
4876 foreach my $repo_id (keys %$r) {
4877 my $url = $r->{$repo_id}->{url} or next;
4878 my $fetch = $r->{$repo_id}->{fetch} or next;
4879 my $ra = Git::SVN::Ra->new($url);
4880
4881 # skip existing cases where we already connect to the root
4882 if (($ra->{url} eq $ra->{repos_root}) ||
Eric Wong7829f202008-06-28 20:40:32 -07004883 ($ra->{repos_root} eq $repo_id)) {
Eric Wong47e39c52007-01-21 04:27:09 -08004884 $root_repos->{$ra->{url}} = $repo_id;
4885 next;
4886 }
4887
4888 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
4889 my $root_path = $ra->{url};
Eric Wong4e9f6cc2007-02-09 12:17:57 -08004890 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
Eric Wong47e39c52007-01-21 04:27:09 -08004891 foreach my $path (keys %$fetch) {
4892 my $ref_id = $fetch->{$path};
4893 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
4894
4895 # make sure we can read when connecting to
4896 # a higher level of a repository
4897 my ($last_rev, undef) = $gs->last_rev_commit;
4898 if (!defined $last_rev) {
4899 $last_rev = eval {
4900 $root_ra->get_latest_revnum;
4901 };
4902 next if $@;
4903 }
4904 my $new = $root_path;
4905 $new .= length $path ? "/$path" : '';
4906 eval {
4907 $root_ra->get_log([$new], $last_rev, $last_rev,
4908 0, 0, 1, sub { });
4909 };
4910 next if $@;
4911 $new_urls->{$ra->{repos_root}}->{$new} =
4912 { ref_id => $ref_id,
4913 old_repo_id => $repo_id,
4914 old_path => $path };
4915 }
4916 }
4917
4918 my @emptied;
4919 foreach my $url (keys %$new_urls) {
4920 # see if we can re-use an existing [svn-remote "repo_id"]
4921 # instead of creating a(n ugly) new section:
Eric Wong7829f202008-06-28 20:40:32 -07004922 my $repo_id = $root_repos->{$url} || $url;
Eric Wong47e39c52007-01-21 04:27:09 -08004923
4924 my $fetch = $new_urls->{$url};
4925 foreach my $path (keys %$fetch) {
4926 my $x = $fetch->{$path};
4927 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
4928 my $pfx = "svn-remote.$x->{old_repo_id}";
4929
4930 my $old_fetch = quotemeta("$x->{old_path}:".
4931 "refs/remotes/$x->{ref_id}");
Eric Wong8b8fc062007-01-22 11:44:57 -08004932 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08004933 "$pfx.fetch", '^'. $old_fetch . '$');
4934 delete $r->{$x->{old_repo_id}}->
4935 {fetch}->{$x->{old_path}};
4936 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
Eric Wong8b8fc062007-01-22 11:44:57 -08004937 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08004938 "$pfx.url");
4939 push @emptied, $x->{old_repo_id}
4940 }
4941 }
4942 }
4943 if (@emptied) {
4944 my $file = $ENV{GIT_CONFIG} || $ENV{GIT_CONFIG_LOCAL} ||
4945 "$ENV{GIT_DIR}/config";
4946 print STDERR <<EOF;
4947The following [svn-remote] sections in your config file ($file) are empty
4948and can be safely removed:
4949EOF
4950 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
4951 }
4952}
4953
Eric Wong706587f2007-01-18 17:50:01 -08004954sub migration_check {
4955 migrate_from_v0();
4956 migrate_from_v1();
4957 migrate_from_v2();
Eric Wong47e39c52007-01-21 04:27:09 -08004958 minimize_connections() if $_minimize;
Eric Wong706587f2007-01-18 17:50:01 -08004959}
4960
Eric Wongef3cfaa2007-01-24 03:30:57 -08004961package Git::IndexInfo;
4962use strict;
4963use warnings;
4964use Git qw/command_input_pipe command_close_pipe/;
4965
4966sub new {
4967 my ($class) = @_;
4968 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
4969 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
4970}
4971
4972sub remove {
4973 my ($self, $path) = @_;
4974 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
4975 return ++$self->{nr};
4976 }
4977 undef;
4978}
4979
4980sub update {
4981 my ($self, $mode, $hash, $path) = @_;
4982 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
4983 return ++$self->{nr};
4984 }
4985 undef;
4986}
4987
4988sub DESTROY {
4989 my ($self) = @_;
4990 command_close_pipe($self->{gui}, $self->{ctx});
4991}
4992
Eric Wong4bb9ed02007-02-03 13:29:17 -08004993package Git::SVN::GlobSpec;
4994use strict;
4995use warnings;
4996
4997sub new {
4998 my ($class, $glob) = @_;
Eric Wong4bb9ed02007-02-03 13:29:17 -08004999 my $re = $glob;
5000 $re =~ s!/+$!!g; # no need for trailing slashes
Marcus Griep570d35c2008-08-08 01:41:57 -07005001 $re =~ m!^([^*]*)(\*(?:/\*)*)([^*]*)$!;
5002 my $temp = $re;
5003 my ($left, $right) = ($1, $3);
5004 $re = $2;
5005 my $depth = $re =~ tr/*/*/;
5006 if ($depth != $temp =~ tr/*/*/) {
5007 die "Only one set of wildcard directories " .
5008 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
5009 }
5010 if ($depth == 0) {
Eric Wonge5181922007-02-08 12:53:57 -08005011 die "One '*' is needed for glob: '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08005012 }
Marcus Griep570d35c2008-08-08 01:41:57 -07005013 $re =~ s!\*!\[^/\]*!g;
5014 $re = quotemeta($left) . "($re)" . quotemeta($right);
Eric Wong4e9f6cc2007-02-09 12:17:57 -08005015 if (length $left && !($left =~ s!/+$!!g)) {
5016 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
5017 }
5018 if (length $right && !($right =~ s!^/+!!g)) {
5019 die "Missing leading '/' on right side of: '$glob' ($right)\n";
5020 }
Eric Wong74a81222007-02-10 13:28:50 -08005021 my $left_re = qr/^\/\Q$left\E(\/|$)/;
5022 bless { left => $left, right => $right, left_regex => $left_re,
Marcus Griep570d35c2008-08-08 01:41:57 -07005023 regex => qr/$re/, glob => $glob, depth => $depth }, $class;
Eric Wong4bb9ed02007-02-03 13:29:17 -08005024}
5025
5026sub full_path {
5027 my ($self, $path) = @_;
5028 return (length $self->{left} ? "$self->{left}/" : '') .
5029 $path . (length $self->{right} ? "/$self->{right}" : '');
5030}
5031
Eric Wong3397f9d2006-02-16 01:24:16 -08005032__END__
5033
5034Data structures:
5035
Eric Wong4bb9ed02007-02-03 13:29:17 -08005036
5037$remotes = { # returned by read_all_remotes()
5038 'svn' => {
5039 # svn-remote.svn.url=https://svn.musicpd.org
5040 url => 'https://svn.musicpd.org',
5041 # svn-remote.svn.fetch=mpd/trunk:trunk
5042 fetch => {
5043 'mpd/trunk' => 'trunk',
5044 },
5045 # svn-remote.svn.tags=mpd/tags/*:tags/*
5046 tags => {
5047 path => {
5048 left => 'mpd/tags',
5049 right => '',
5050 regex => qr!mpd/tags/([^/]+)$!,
5051 glob => 'tags/*',
5052 },
5053 ref => {
5054 left => 'tags',
5055 right => '',
5056 regex => qr!tags/([^/]+)$!,
5057 glob => 'tags/*',
5058 },
5059 }
5060 }
5061};
5062
Eric Wong44320b92007-01-13 22:35:53 -08005063$log_entry hashref as returned by libsvn_log_entry()
Eric Wong3397f9d2006-02-16 01:24:16 -08005064{
Eric Wong44320b92007-01-13 22:35:53 -08005065 log => 'whitespace-formatted log entry
Eric Wong3397f9d2006-02-16 01:24:16 -08005066', # trailing newline is preserved
5067 revision => '8', # integer
5068 date => '2004-02-24T17:01:44.108345Z', # commit date
5069 author => 'committer name'
5070};
5071
Eric Wong6e8548c2007-01-27 01:32:00 -08005072
5073# this is generated by generate_diff();
Eric Wong3397f9d2006-02-16 01:24:16 -08005074@mods = array of diff-index line hashes, each element represents one line
5075 of diff-index output
5076
5077diff-index line ($m hash)
5078{
5079 mode_a => first column of diff-index output, no leading ':',
5080 mode_b => second column of diff-index output,
5081 sha1_b => sha1sum of the final blob,
Eric Wongac8e0b92006-03-03 01:20:07 -08005082 chg => change type [MCRADT],
Eric Wong3397f9d2006-02-16 01:24:16 -08005083 file_a => original file name of a file (iff chg is 'C' or 'R')
5084 file_b => new/current file name of a file (any chg)
5085}
5086;
Eric Wonga5e0ced2006-06-12 15:23:48 -07005087
Eric Wonga00439a2006-06-27 19:39:13 -07005088# retval of read_url_paths{,_all}();
5089$l_map = {
5090 # repository root url
5091 'https://svn.musicpd.org' => {
5092 # repository path # GIT_SVN_ID
5093 'mpd/trunk' => 'trunk',
5094 'mpd/tags/0.11.5' => 'tags/0.11.5',
5095 },
5096}
5097
Eric Wonga5e0ced2006-06-12 15:23:48 -07005098Notes:
5099 I don't trust the each() function on unless I created %hash myself
5100 because the internal iterator may not have started at base.