blob: a4a45ef3986453571f3063dfd6aee75c7c127744 [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
Mark Lodato36db1ed2009-05-14 21:27:15 -04008 $_q $_authors $_authors_prog %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 Wong6b488292009-07-25 00:00:50 -070022$Git::SVN::_minimize_url = 'unset';
Eric Wong13ccd6d2006-03-29 22:37:18 -080023
Karthik Rf3a87d92009-08-18 18:54:40 -050024if (! exists $ENV{SVN_SSH}) {
25 if (exists $ENV{GIT_SSH}) {
26 $ENV{SVN_SSH} = $ENV{GIT_SSH};
27 if ($^O eq 'msys') {
28 $ENV{SVN_SSH} =~ s/\\/\\\\/g;
29 }
30 }
31}
32
Eric Wongf8c9d1d2007-01-12 02:35:20 -080033$Git::SVN::Log::TZ = $ENV{TZ};
Eric Wong3397f9d2006-02-16 01:24:16 -080034$ENV{TZ} = 'UTC';
Eric Wonga00439a2006-06-27 19:39:13 -070035$| = 1; # unbuffer STDOUT
Eric Wong3397f9d2006-02-16 01:24:16 -080036
Benoit Sigoure207f1a72007-10-16 16:36:52 +020037sub fatal (@) { print STDERR "@_\n"; exit 1 }
Eric Wongb9c85182006-12-15 23:58:07 -080038require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
39require SVN::Ra;
40require SVN::Delta;
41if ($SVN::Core::VERSION lt '1.1.0') {
Benoit Sigoure207f1a72007-10-16 16:36:52 +020042 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
Eric Wongb9c85182006-12-15 23:58:07 -080043}
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -050044my $can_compress = eval { require Compress::Zlib; 1};
Eric Wongd81bf822007-01-10 01:22:38 -080045push @Git::SVN::Ra::ISA, 'SVN::Ra';
Eric Wongb9c85182006-12-15 23:58:07 -080046push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
47push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
Eric Wong3397f9d2006-02-16 01:24:16 -080048use Carp qw/croak/;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -080049use Digest::MD5;
Eric Wong3397f9d2006-02-16 01:24:16 -080050use IO::File qw//;
51use File::Basename qw/dirname basename/;
52use File::Path qw/mkpath/;
Mark Lodato36db1ed2009-05-14 21:27:15 -040053use File::Spec;
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -050054use File::Find;
Eric Wong512b6202007-04-03 01:57:08 -070055use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
Eric Wong968bdf12006-06-15 13:36:12 -070056use IPC::Open3;
Eric Wong336f1712007-01-11 02:14:43 -080057use Git;
Eric Wonga5e0ced2006-06-12 15:23:48 -070058
Eric Wong336f1712007-01-11 02:14:43 -080059BEGIN {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120060 # import functions from Git into our packages, en masse
61 no strict 'refs';
Eric Wong336f1712007-01-11 02:14:43 -080062 foreach (qw/command command_oneline command_noisy command_output_pipe
Boris Byk6ea42032009-04-11 00:32:41 +040063 command_input_pipe command_close_pipe
64 command_bidi_pipe command_close_bidi_pipe/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120065 for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -080066 Git::SVN::Migration Git::SVN::Log Git::SVN),
Sam Vilainc5f71ad2007-06-15 15:43:59 +120067 __PACKAGE__) {
68 *{"${package}::$_"} = \&{"Git::$_"};
69 }
Eric Wong336f1712007-01-11 02:14:43 -080070 }
Eric Wong336f1712007-01-11 02:14:43 -080071}
72
Eric Wongb9c85182006-12-15 23:58:07 -080073my ($SVN);
Eric Wong83e99402006-10-11 18:19:55 -070074
Eric Wongf8c9d1d2007-01-12 02:35:20 -080075$sha1 = qr/[a-f\d]{40}/;
76$sha1_short = qr/[a-f\d]{4,40}/;
Eric Wong44320b92007-01-13 22:35:53 -080077my ($_stdin, $_help, $_edit,
Marc Branchaud62244062009-06-23 13:02:08 -040078 $_message, $_file, $_branch_dest,
Eric Wongd05d72e2007-01-15 22:59:26 -080079 $_template, $_shared,
Jason Merrillc2abd832009-04-06 16:37:59 -040080 $_version, $_fetch_all, $_no_rebase, $_fetch_parent,
Eric Wongdee41f32007-03-13 11:40:36 -070081 $_merge, $_strategy, $_dry_run, $_local,
Steven Grimm4be40382008-05-10 22:11:18 -070082 $_prefix, $_no_checkout, $_url, $_verbose,
Florian Ragwitz5de70ef2008-10-04 19:35:17 -070083 $_git_format, $_commit_url, $_tag);
Eric Wong0bed5ea2007-02-09 02:45:03 -080084$Git::SVN::_follow_parent = 1;
Simon Arlott49750f32009-03-30 19:31:41 +010085$_q ||= 0;
Eric Wong706587f2007-01-18 17:50:01 -080086my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
87 'config-dir=s' => \$Git::SVN::Ra::config_dir,
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +020088 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
89 'ignore-paths=s' => \$SVN::Git::Fetcher::_ignore_regex );
Eric Wong0bed5ea2007-02-09 02:45:03 -080090my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
Eric Wongdc5869c2006-05-24 02:07:32 -070091 'authors-file|A=s' => \$_authors,
Mark Lodato36db1ed2009-05-14 21:27:15 -040092 'authors-prog=s' => \$_authors_prog,
Eric Wongecc712d2007-01-31 12:28:10 -080093 'repack:i' => \$Git::SVN::_repack,
Eric Wong97ae0912007-02-11 15:21:24 -080094 'noMetadata' => \$Git::SVN::_no_metadata,
95 'useSvmProps' => \$Git::SVN::_use_svm_props,
Eric Wong62e349d2007-02-16 19:57:29 -080096 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props,
Eric Wong6af1db42007-02-14 16:04:10 -080097 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size,
Eric Wong1e889ef2007-02-16 01:45:13 -080098 'no-checkout' => \$_no_checkout,
Simon Arlott49750f32009-03-30 19:31:41 +010099 'quiet|q+' => \$_q,
Eric Wongecc712d2007-01-31 12:28:10 -0800100 'repack-flags|repack-args|repack-opts=s' =>
101 \$Git::SVN::_repack_flags,
Andy Whitcroft70ae04e2007-11-22 13:44:42 +0000102 'use-log-author' => \$Git::SVN::_use_log_author,
Avery Pennarun6aa9ba12008-04-15 21:04:17 -0400103 'add-author-from' => \$Git::SVN::_add_author_from,
Pete Harlane82f0d72009-01-17 20:10:14 -0800104 'localtime' => \$Git::SVN::_localtime,
Eric Wong706587f2007-01-18 17:50:01 -0800105 %remote_opts );
Eric Wong36f5b1f2006-05-23 19:23:41 -0700106
Marc Branchaud62244062009-06-23 13:02:08 -0400107my ($_trunk, @_tags, @_branches, $_stdlayout);
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800108my %icv;
Eric Wongdadc6d22007-02-14 12:27:41 -0800109my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared,
Marc Branchaud62244062009-06-23 13:02:08 -0400110 'trunk|T=s' => \$_trunk, 'tags|t=s@' => \@_tags,
111 'branches|b=s@' => \@_branches, 'prefix=s' => \$_prefix,
martin f. krafft8f728fb2007-07-14 11:25:28 +0200112 'stdlayout|s' => \$_stdlayout,
Eric Wong6b488292009-07-25 00:00:50 -0700113 'minimize-url|m!' => \$Git::SVN::_minimize_url,
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800114 'no-metadata' => sub { $icv{noMetadata} = 1 },
115 'use-svm-props' => sub { $icv{useSvmProps} = 1 },
116 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 },
117 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] },
Eric Wongdadc6d22007-02-14 12:27:41 -0800118 %remote_opts );
Eric Wong27e9fb82006-06-27 19:39:12 -0700119my %cmt_opts = ( 'edit|e' => \$_edit,
Eric Wong24e22aa2007-01-29 00:07:49 -0800120 'rmdir' => \$SVN::Git::Editor::_rmdir,
121 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder,
122 'l=i' => \$SVN::Git::Editor::_rename_limit,
123 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity
Eric Wong27e9fb82006-06-27 19:39:12 -0700124);
Eric Wong9d55b412006-06-12 15:53:13 -0700125
Eric Wong3397f9d2006-02-16 01:24:16 -0800126my %cmd = (
Eric Wong2a3240b2007-01-04 18:09:56 -0800127 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
Eric Wonge98671e2007-02-14 02:21:19 -0800128 { 'revision|r=s' => \$_revision,
Eric Wong905f8b72007-02-16 03:22:40 -0800129 'fetch-all|all' => \$_fetch_all,
Jason Merrillc2abd832009-04-06 16:37:59 -0400130 'parent|p' => \$_fetch_parent,
Eric Wonge98671e2007-02-14 02:21:19 -0800131 %fc_opts } ],
Eric Wong0425ea92007-02-16 18:45:01 -0800132 clone => [ \&cmd_clone, "Initialize and fetch revisions",
133 { 'revision|r=s' => \$_revision,
134 %fc_opts, %init_opts } ],
Eric Wongd2866f92007-01-11 12:26:16 -0800135 init => [ \&cmd_init, "Initialize a repo for tracking" .
Eric Wongf8ab6b72006-05-31 15:49:56 -0700136 " (requires URL argument)",
Eric Wong9d55b412006-06-12 15:53:13 -0700137 \%init_opts ],
Eric Wongdadc6d22007-02-14 12:27:41 -0800138 'multi-init' => [ \&cmd_multi_init,
139 "Deprecated alias for ".
140 "'$0 init -T<trunk> -b<branches> -t<tags>'",
141 \%init_opts ],
Eric Wongd7ad3be2007-01-14 03:14:28 -0800142 dcommit => [ \&cmd_dcommit,
143 'Commit several diffs to merge with upstream',
Eric Wong3289e862006-12-15 23:58:08 -0800144 { 'merge|m|M' => \$_merge,
145 'strategy|s=s' => \$_strategy,
Eric Wong905f8b72007-02-16 03:22:40 -0800146 'verbose|v' => \$_verbose,
Eric Wong3289e862006-12-15 23:58:08 -0800147 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800148 'fetch-all|all' => \$_fetch_all,
Eric Wongba24e742008-08-07 02:06:16 -0700149 'commit-url=s' => \$_commit_url,
150 'revision|r=i' => \$_revision,
Karl Hasselström171af112007-05-03 07:51:35 +0200151 'no-rebase' => \$_no_rebase,
Eric Wong4b155222006-12-22 21:59:24 -0800152 %cmt_opts, %fc_opts } ],
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700153 branch => [ \&cmd_branch,
154 'Create a branch in the SVN repository',
155 { 'message|m=s' => \$_message,
Marc Branchaud62244062009-06-23 13:02:08 -0400156 'destination|d=s' => \$_branch_dest,
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700157 'dry-run|n' => \$_dry_run,
158 'tag|t' => \$_tag } ],
159 tag => [ sub { $_tag = 1; cmd_branch(@_) },
160 'Create a tag in the SVN repository',
161 { 'message|m=s' => \$_message,
Marc Branchaud62244062009-06-23 13:02:08 -0400162 'destination|d=s' => \$_branch_dest,
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700163 'dry-run|n' => \$_dry_run } ],
Eric Wong1ce255d2007-01-14 23:21:16 -0800164 'set-tree' => [ \&cmd_set_tree,
165 "Set an SVN repository to a git tree-ish",
Robin H. Johnsone84dc6d2009-05-05 11:16:14 -0700166 { 'stdin' => \$_stdin, %cmt_opts, %fc_opts, } ],
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200167 'create-ignore' => [ \&cmd_create_ignore,
168 'Create a .gitignore per svn:ignore',
169 { 'revision|r=i' => \$_revision
170 } ],
Eric Wong6111b932009-11-15 18:57:16 -0800171 'mkdirs' => [ \&cmd_mkdirs ,
172 "recreate empty directories after a checkout",
173 { 'revision|r=i' => \$_revision } ],
Benoit Sigoure15153452007-10-16 16:36:50 +0200174 'propget' => [ \&cmd_propget,
175 'Print the value of a property on a file or directory',
176 { 'revision|r=i' => \$_revision } ],
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200177 'proplist' => [ \&cmd_proplist,
178 'List all properties of a file or directory',
179 { 'revision|r=i' => \$_revision } ],
Eric Wong5969cbe2007-01-11 17:58:39 -0800180 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200181 { 'revision|r=i' => \$_revision
Lars Hjemli05b4df32007-09-05 11:35:29 +0200182 } ],
Vineet Kumar2d879792007-11-19 14:56:15 -0800183 'show-externals' => [ \&cmd_show_externals, "Show svn:externals listings",
184 { 'revision|r=i' => \$_revision
185 } ],
Eric Wong1c8443b2007-01-14 02:17:00 -0800186 'multi-fetch' => [ \&cmd_multi_fetch,
Eric Wonge98671e2007-02-14 02:21:19 -0800187 "Deprecated alias for $0 fetch --all",
188 { 'revision|r=s' => \$_revision, %fc_opts } ],
Eric Wong706587f2007-01-18 17:50:01 -0800189 'migrate' => [ sub { },
190 # no-op, we automatically run this anyways,
Eric Wong706587f2007-01-18 17:50:01 -0800191 'Migrate configuration/metadata/layout from
192 previous versions of git-svn',
Eric Wonga836a0e2007-02-14 19:34:56 -0800193 { 'minimize' => \$Git::SVN::Migration::_minimize,
194 %remote_opts } ],
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800195 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
196 { 'limit=i' => \$Git::SVN::Log::limit,
Eric Wong79bb8d82006-06-01 02:35:44 -0700197 'revision|r=s' => \$_revision,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800198 'verbose|v' => \$Git::SVN::Log::verbose,
199 'incremental' => \$Git::SVN::Log::incremental,
200 'oneline' => \$Git::SVN::Log::oneline,
201 'show-commit' => \$Git::SVN::Log::show_commit,
202 'non-recursive' => \$Git::SVN::Log::non_recursive,
Eric Wong79bb8d82006-06-01 02:35:44 -0700203 'authors-file|A=s' => \$_authors,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800204 'color' => \$Git::SVN::Log::color,
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200205 'pager=s' => \$Git::SVN::Log::pager
Eric Wong79bb8d82006-06-01 02:35:44 -0700206 } ],
Eric Wong222566e2008-08-08 01:41:58 -0700207 'find-rev' => [ \&cmd_find_rev,
208 "Translate between SVN revision numbers and tree-ish",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200209 {} ],
Eric Wong905f8b72007-02-16 03:22:40 -0800210 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
211 { 'merge|m|M' => \$_merge,
212 'verbose|v' => \$_verbose,
213 'strategy|s=s' => \$_strategy,
Eric Wongdee41f32007-03-13 11:40:36 -0700214 'local|l' => \$_local,
Eric Wong905f8b72007-02-16 03:22:40 -0800215 'fetch-all|all' => \$_fetch_all,
Seth Falcon7d45e142008-05-19 20:29:17 -0700216 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800217 %fc_opts } ],
Eric Wong44320b92007-01-13 22:35:53 -0800218 'commit-diff' => [ \&cmd_commit_diff,
219 'Commit a diff between two trees',
Eric Wong27e9fb82006-06-27 19:39:12 -0700220 { 'message|m=s' => \$_message,
221 'file|F=s' => \$_file,
Eric Wong45bf4732006-11-09 01:19:37 -0800222 'revision|r=s' => \$_revision,
Eric Wong27e9fb82006-06-27 19:39:12 -0700223 %cmt_opts } ],
David D. Kilzere6fefa92007-11-21 11:57:18 -0800224 'info' => [ \&cmd_info,
225 "Show info about the latest SVN revision
226 on the current branch",
David D. Kilzer8b014d72007-11-21 11:57:19 -0800227 { 'url' => \$_url, } ],
Tim Stoakes6fb53752008-02-10 15:21:08 +1030228 'blame' => [ \&Git::SVN::Log::cmd_blame,
229 "Show what revision and author last modified each line of a file",
Steven Grimm4be40382008-05-10 22:11:18 -0700230 { 'git-format' => \$_git_format } ],
Ben Jackson195643f2009-06-03 20:45:52 -0700231 'reset' => [ \&cmd_reset,
232 "Undo fetches back to the specified SVN revision",
233 { 'revision|r=s' => \$_revision,
234 'parent|p' => \$_fetch_parent } ],
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -0500235 'gc' => [ \&cmd_gc,
236 "Compress unhandled.log files in .git/svn and remove " .
237 "index files in .git/svn",
238 {} ],
Eric Wong3397f9d2006-02-16 01:24:16 -0800239);
Eric Wong9d55b412006-06-12 15:53:13 -0700240
Eric Wong3397f9d2006-02-16 01:24:16 -0800241my $cmd;
242for (my $i = 0; $i < @ARGV; $i++) {
243 if (defined $cmd{$ARGV[$i]}) {
244 $cmd = $ARGV[$i];
245 splice @ARGV, $i, 1;
246 last;
Ben Jackson9a8c92a2009-05-30 18:17:06 -0700247 } elsif ($ARGV[$i] eq 'help') {
248 $cmd = $ARGV[$i+1];
249 usage(0);
Eric Wong3397f9d2006-02-16 01:24:16 -0800250 }
251};
252
Eric Wong540424b2007-12-19 00:31:43 -0800253# make sure we're always running at the top-level working directory
254unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
Eric Wong5253dc32007-02-20 01:36:30 -0800255 unless (-d $ENV{GIT_DIR}) {
256 if ($git_dir_user_set) {
257 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
258 "but it is not a directory\n";
259 }
260 my $git_dir = delete $ENV{GIT_DIR};
Deskin Millerfe4003f2008-11-06 00:07:39 -0500261 my $cdup = undef;
262 git_cmd_try {
263 $cdup = command_oneline(qw/rev-parse --show-cdup/);
264 $git_dir = '.' unless ($cdup);
265 chomp $cdup if ($cdup);
266 $cdup = "." unless ($cdup && length $cdup);
267 } "Already at toplevel, but $git_dir not found\n";
Eric Wong5253dc32007-02-20 01:36:30 -0800268 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
269 unless (-d $git_dir) {
270 die "$git_dir still not found after going to ",
271 "'$cdup'\n";
272 }
273 $ENV{GIT_DIR} = $git_dir;
274 }
Adam Robenffe256f2008-05-23 16:19:41 +0200275 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wong5253dc32007-02-20 01:36:30 -0800276}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100277
278my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
279
Eric Wong1a305822009-11-14 14:25:11 -0800280read_git_config(\%opts);
Eric Wong222566e2008-08-08 01:41:58 -0700281if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
282 Getopt::Long::Configure('pass_through');
283}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100284my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
285 'minimize-connections' => \$Git::SVN::Migration::_minimize,
286 'id|i=s' => \$Git::SVN::default_ref_id,
287 'svn-remote|remote|R=s' => sub {
288 $Git::SVN::no_reuse_existing = 1;
289 $Git::SVN::default_repo_id = $_[1] });
290exit 1 if (!$rv && $cmd && $cmd ne 'log');
291
292usage(0) if $_help;
293version() if $_version;
294usage(1) unless defined $cmd;
295load_authors() if $_authors;
Mark Lodato36db1ed2009-05-14 21:27:15 -0400296if (defined $_authors_prog) {
297 $_authors_prog = "'" . File::Spec->rel2abs($_authors_prog) . "'";
298}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100299
Eric Wong0425ea92007-02-16 18:45:01 -0800300unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
Eric Wong706587f2007-01-18 17:50:01 -0800301 Git::SVN::Migration::migration_check();
302}
Eric Wongecc712d2007-01-31 12:28:10 -0800303Git::SVN::init_vars();
Eric Wongb805b442007-01-22 13:52:04 -0800304eval {
305 Git::SVN::verify_remotes_sanity();
306 $cmd{$cmd}->[0]->(@ARGV);
307};
308fatal $@ if $@;
Eric Wong1e889ef2007-02-16 01:45:13 -0800309post_fetch_checkout();
Eric Wong3397f9d2006-02-16 01:24:16 -0800310exit 0;
311
312####################### primary functions ######################
313sub usage {
314 my $exit = shift || 0;
315 my $fd = $exit ? \*STDERR : \*STDOUT;
316 print $fd <<"";
317git-svn - bidirectional operations between a single Subversion tree and git
Stephan Beyer1b1dd232008-07-13 15:36:15 +0200318Usage: git svn <command> [options] [arguments]\n
Eric Wong448c81b2006-03-03 01:20:09 -0800319
320 print $fd "Available commands:\n" unless $cmd;
Eric Wong3397f9d2006-02-16 01:24:16 -0800321
322 foreach (sort keys %cmd) {
Eric Wong448c81b2006-03-03 01:20:09 -0800323 next if $cmd && $cmd ne $_;
Eric Wonga836a0e2007-02-14 19:34:56 -0800324 next if /^multi-/; # don't show deprecated commands
Eric Wongb203b762006-10-11 14:53:36 -0700325 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
Benoit Sigoureaa807bc2007-11-03 19:53:34 +0100326 foreach (sort keys %{$cmd{$_}->[2]}) {
Eric Wong512b6202007-04-03 01:57:08 -0700327 # mixed-case options are for .git/config only
328 next if /[A-Z]/ && /^[a-z]+$/i;
Eric Wong448c81b2006-03-03 01:20:09 -0800329 # prints out arguments as they should be passed:
Eric Wongb8c92ca2006-05-24 01:40:37 -0700330 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
Eric Wongb203b762006-10-11 14:53:36 -0700331 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
Eric Wong448c81b2006-03-03 01:20:09 -0800332 "--$_" : "-$_" }
333 split /\|/,$_)," $x\n";
334 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800335 }
336 print $fd <<"";
Eric Wong448c81b2006-03-03 01:20:09 -0800337\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
338arbitrary identifier if you're tracking multiple SVN branches/repositories in
339one git repository and want to keep them separate. See git-svn(1) for more
340information.
Eric Wong3397f9d2006-02-16 01:24:16 -0800341
342 exit $exit;
343}
344
Eric Wong551ce282006-02-20 10:57:29 -0800345sub version {
Eric Wong7d60ab22006-12-28 01:16:20 -0800346 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
Eric Wong551ce282006-02-20 10:57:29 -0800347 exit 0;
348}
349
Eric Wong8164b652007-01-11 15:35:55 -0800350sub do_git_init_db {
351 unless (-d $ENV{GIT_DIR}) {
352 my @init_db = ('init');
353 push @init_db, "--template=$_template" if defined $_template;
Eric Wongdadc6d22007-02-14 12:27:41 -0800354 if (defined $_shared) {
355 if ($_shared =~ /[a-z]/) {
356 push @init_db, "--shared=$_shared";
357 } else {
358 push @init_db, "--shared";
359 }
360 }
Eric Wong8164b652007-01-11 15:35:55 -0800361 command_noisy(@init_db);
Adam Robenffe256f2008-05-23 16:19:41 +0200362 $_repository = Git->repository(Repository => ".git");
Eric Wong8164b652007-01-11 15:35:55 -0800363 }
Johannes Schindelind3c96342009-04-09 13:29:57 +0200364 command_noisy('config', 'core.autocrlf', 'false');
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800365 my $set;
366 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
367 foreach my $i (keys %icv) {
368 die "'$set' and '$i' cannot both be set\n" if $set;
369 next unless defined $icv{$i};
370 command_noisy('config', "$pfx.$i", $icv{$i});
371 $set = $i;
372 }
Ben Jackson88ec2052009-04-11 10:46:18 -0700373 my $ignore_regex = \$SVN::Git::Fetcher::_ignore_regex;
374 command_noisy('config', "$pfx.ignore-paths", $$ignore_regex)
375 if defined $$ignore_regex;
Eric Wong8164b652007-01-11 15:35:55 -0800376}
377
Eric Wongdadc6d22007-02-14 12:27:41 -0800378sub init_subdir {
379 my $repo_path = shift or return;
380 mkpath([$repo_path]) unless -d $repo_path;
381 chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
Eric Wongf30603f2007-02-23 01:26:26 -0800382 $ENV{GIT_DIR} = '.git';
Adam Robenffe256f2008-05-23 16:19:41 +0200383 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wongdadc6d22007-02-14 12:27:41 -0800384}
385
Eric Wong0425ea92007-02-16 18:45:01 -0800386sub cmd_clone {
387 my ($url, $path) = @_;
388 if (!defined $path &&
Marc Branchaud62244062009-06-23 13:02:08 -0400389 (defined $_trunk || @_branches || @_tags ||
martin f. krafft8f728fb2007-07-14 11:25:28 +0200390 defined $_stdlayout) &&
Eric Wong0425ea92007-02-16 18:45:01 -0800391 $url !~ m#^[a-z\+]+://#) {
392 $path = $url;
393 }
Eric Wong0425ea92007-02-16 18:45:01 -0800394 $path = basename($url) if !defined $path || !length $path;
Alex Vandiver2bc35dc2009-12-08 15:54:10 -0500395 my $authors_absolute = $_authors ? File::Spec->rel2abs($_authors) : "";
Eric Wongf30603f2007-02-23 01:26:26 -0800396 cmd_init($url, $path);
Alex Vandiver2bc35dc2009-12-08 15:54:10 -0500397 command_oneline('config', 'svn.authorsfile', $authors_absolute)
398 if $_authors;
Alex Vandiverf5841502009-12-08 15:54:11 -0500399 Git::SVN::fetch_all($Git::SVN::default_repo_id);
Eric Wong0425ea92007-02-16 18:45:01 -0800400}
401
Eric Wongd2866f92007-01-11 12:26:16 -0800402sub cmd_init {
martin f. krafft8f728fb2007-07-14 11:25:28 +0200403 if (defined $_stdlayout) {
404 $_trunk = 'trunk' if (!defined $_trunk);
Marc Branchaud62244062009-06-23 13:02:08 -0400405 @_tags = 'tags' if (! @_tags);
406 @_branches = 'branches' if (! @_branches);
martin f. krafft8f728fb2007-07-14 11:25:28 +0200407 }
Marc Branchaud62244062009-06-23 13:02:08 -0400408 if (defined $_trunk || @_branches || @_tags) {
Eric Wongdadc6d22007-02-14 12:27:41 -0800409 return cmd_multi_init(@_);
Eric Wong03e0ea82006-06-30 21:42:53 -0700410 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800411 my $url = shift or die "SVN repository location required ",
412 "as a command-line argument\n";
Ulrich Dangel50ff2362009-06-26 16:52:09 +0200413 $url = canonicalize_url($url);
Eric Wongdadc6d22007-02-14 12:27:41 -0800414 init_subdir(@_);
Eric Wong8164b652007-01-11 15:35:55 -0800415 do_git_init_db();
Eric Wong03e0ea82006-06-30 21:42:53 -0700416
Eric Wong6b488292009-07-25 00:00:50 -0700417 if ($Git::SVN::_minimize_url eq 'unset') {
418 $Git::SVN::_minimize_url = 0;
419 }
420
Eric Wong706587f2007-01-18 17:50:01 -0800421 Git::SVN->init($url);
Eric Wong3397f9d2006-02-16 01:24:16 -0800422}
423
Eric Wong2a3240b2007-01-04 18:09:56 -0800424sub cmd_fetch {
Eric Wonge98671e2007-02-14 02:21:19 -0800425 if (grep /^\d+=./, @_) {
426 die "'<rev>=<commit>' fetch arguments are ",
427 "no longer supported.\n";
Eric Wong07a1c952007-01-22 15:47:41 -0800428 }
Eric Wonge98671e2007-02-14 02:21:19 -0800429 my ($remote) = @_;
430 if (@_ > 1) {
Jason Merrillc2abd832009-04-06 16:37:59 -0400431 die "Usage: $0 fetch [--all] [--parent] [svn-remote]\n";
Eric Wonge98671e2007-02-14 02:21:19 -0800432 }
Eric Wong4d0157d2009-11-22 12:37:06 -0800433 $Git::SVN::no_reuse_existing = undef;
Jason Merrillc2abd832009-04-06 16:37:59 -0400434 if ($_fetch_parent) {
435 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
436 unless ($gs) {
437 die "Unable to determine upstream SVN information from ",
438 "working tree history\n";
439 }
440 # just fetch, don't checkout.
441 $_no_checkout = 'true';
442 $_fetch_all ? $gs->fetch_all : $gs->fetch;
443 } elsif ($_fetch_all) {
Eric Wonge98671e2007-02-14 02:21:19 -0800444 cmd_multi_fetch();
445 } else {
Jason Merrillc2abd832009-04-06 16:37:59 -0400446 $remote ||= $Git::SVN::default_repo_id;
Eric Wonge98671e2007-02-14 02:21:19 -0800447 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
Eric Wong1c8443b2007-01-14 02:17:00 -0800448 }
Eric Wong2a3240b2007-01-04 18:09:56 -0800449}
450
Eric Wong1ce255d2007-01-14 23:21:16 -0800451sub cmd_set_tree {
Eric Wong3397f9d2006-02-16 01:24:16 -0800452 my (@commits) = @_;
453 if ($_stdin || !@commits) {
454 print "Reading from stdin...\n";
455 @commits = ();
456 while (<STDIN>) {
Eric Wong1ca72ae2006-03-03 01:20:09 -0800457 if (/\b($sha1_short)\b/o) {
Eric Wong3397f9d2006-02-16 01:24:16 -0800458 unshift @commits, $1;
459 }
460 }
461 }
462 my @revs;
Eric Wong8de010a2006-02-20 10:57:26 -0800463 foreach my $c (@commits) {
Eric Wongaef4e922006-12-15 10:59:54 -0800464 my @tmp = command('rev-parse',$c);
Eric Wong8de010a2006-02-20 10:57:26 -0800465 if (scalar @tmp == 1) {
466 push @revs, $tmp[0];
467 } elsif (scalar @tmp > 1) {
Eric Wongaef4e922006-12-15 10:59:54 -0800468 push @revs, reverse(command('rev-list',@tmp));
Eric Wong8de010a2006-02-20 10:57:26 -0800469 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200470 fatal "Failed to rev-parse $c";
Eric Wong8de010a2006-02-20 10:57:26 -0800471 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800472 }
Eric Wong1ce255d2007-01-14 23:21:16 -0800473 my $gs = Git::SVN->new;
474 my ($r_last, $cmt_last) = $gs->last_rev_commit;
475 $gs->fetch;
Eric Wong97f69872007-01-25 11:53:13 -0800476 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
Eric Wong1ce255d2007-01-14 23:21:16 -0800477 fatal "There are new revisions that were fetched ",
478 "and need to be merged (or acknowledged) ",
479 "before committing.\nlast rev: $r_last\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200480 " current: $gs->{last_rev}";
Eric Wong1ce255d2007-01-14 23:21:16 -0800481 }
482 $gs->set_tree($_) foreach @revs;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700483 print "Done committing ",scalar @revs," revisions to SVN\n";
Eric Wong3157dd92007-12-13 08:27:34 -0800484 unlink $gs->{index};
Eric Wonga5e0ced2006-06-12 15:23:48 -0700485}
486
Eric Wongd7ad3be2007-01-14 03:14:28 -0800487sub cmd_dcommit {
488 my $head = shift;
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100489 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
David Reiss826a9332007-11-13 13:47:26 -0800490 'Cannot dcommit with a dirty index. Commit your changes first, '
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100491 . "or stash them with `git stash'.\n";
Eric Wongd7ad3be2007-01-14 03:14:28 -0800492 $head ||= 'HEAD';
Thomas Rast5eec27e2009-05-29 17:09:42 +0200493
494 my $old_head;
495 if ($head ne 'HEAD') {
496 $old_head = eval {
497 command_oneline([qw/symbolic-ref -q HEAD/])
498 };
499 if ($old_head) {
500 $old_head =~ s{^refs/heads/}{};
501 } else {
502 $old_head = eval { command_oneline(qw/rev-parse HEAD/) };
503 }
504 command(['checkout', $head], STDERR => 0);
505 }
506
Eric Wonga8ae2622007-02-13 14:22:11 -0800507 my @refs;
Thomas Rast5eec27e2009-05-29 17:09:42 +0200508 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD', \@refs);
Thomas Rast2cb61102008-08-31 15:50:59 +0200509 unless ($gs) {
510 die "Unable to determine upstream SVN information from ",
511 "$head history.\nPerhaps the repository is empty.";
512 }
Peter Oberndorfer0df84052009-02-23 12:02:53 +0100513
514 if (defined $_commit_url) {
515 $url = $_commit_url;
516 } else {
517 $url = eval { command_oneline('config', '--get',
518 "svn-remote.$gs->{repo_id}.commiturl") };
519 if (!$url) {
520 $url = $gs->full_url
521 }
522 }
523
Eric Wongba24e742008-08-07 02:06:16 -0700524 my $last_rev = $_revision if defined $_revision;
Matthieu Moy59b0c242008-04-24 20:06:36 +0200525 if ($url) {
526 print "Committing to $url ...\n";
527 }
Eric Wong733a65a2007-06-13 02:23:28 -0700528 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
Eric Wong751eb392007-08-31 18:16:12 -0700529 if ($_no_rebase && scalar(@$linear_refs) > 1) {
530 warn "Attempting to commit more than one change while ",
531 "--no-rebase is enabled.\n",
532 "If these changes depend on each other, re-running ",
Eric Wong7dfa16b2008-01-02 10:09:49 -0800533 "without --no-rebase may be required."
Eric Wong751eb392007-08-31 18:16:12 -0700534 }
Eric Wong711521e2008-08-20 00:30:06 -0700535 my $expect_url = $url;
536 Git::SVN::remove_username($expect_url);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800537 while (1) {
538 my $d = shift @$linear_refs or last;
Eric Wong45bf4732006-11-09 01:19:37 -0800539 unless (defined $last_rev) {
540 (undef, $last_rev, undef) = cmt_metadata("$d~1");
541 unless (defined $last_rev) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800542 fatal "Unable to extract revision information ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200543 "from commit $d~1";
Eric Wong45bf4732006-11-09 01:19:37 -0800544 }
545 }
Eric Wongb22d4492006-08-26 00:01:23 -0700546 if ($_dry_run) {
547 print "diff-tree $d~1 $d\n";
548 } else {
Eric Wong751eb392007-08-31 18:16:12 -0700549 my $cmt_rev;
Eric Wongd7ad3be2007-01-14 03:14:28 -0800550 my %ed_opts = ( r => $last_rev,
Eric Wong61395352007-01-27 14:33:08 -0800551 log => get_commit_entry($d)->{log},
Eric Wongba24e742008-08-07 02:06:16 -0700552 ra => Git::SVN::Ra->new($url),
Konstantin V. Arkhipov3caf3202007-11-14 03:52:02 +0300553 config => SVN::Core::config_get_config(
554 $Git::SVN::Ra::config_dir
555 ),
Eric Wong61395352007-01-27 14:33:08 -0800556 tree_a => "$d~1",
557 tree_b => $d,
558 editor_cb => sub {
559 print "Committed r$_[0]\n";
Eric Wong751eb392007-08-31 18:16:12 -0700560 $cmt_rev = $_[0];
561 },
Eric Wonga8ae2622007-02-13 14:22:11 -0800562 svn_path => '');
Eric Wong61395352007-01-27 14:33:08 -0800563 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800564 print "No changes\n$d~1 == $d\n";
Eric Wong733a65a2007-06-13 02:23:28 -0700565 } elsif ($parents->{$d} && @{$parents->{$d}}) {
Eric Wong751eb392007-08-31 18:16:12 -0700566 $gs->{inject_parents_dcommit}->{$cmt_rev} =
Eric Wong733a65a2007-06-13 02:23:28 -0700567 $parents->{$d};
Eric Wongd7ad3be2007-01-14 03:14:28 -0800568 }
Eric Wong751eb392007-08-31 18:16:12 -0700569 $_fetch_all ? $gs->fetch_all : $gs->fetch;
Eric Wong7dfa16b2008-01-02 10:09:49 -0800570 $last_rev = $cmt_rev;
Eric Wong751eb392007-08-31 18:16:12 -0700571 next if $_no_rebase;
572
573 # we always want to rebase against the current HEAD,
574 # not any head that was passed to us
Eric Wongc74d9ac2007-11-05 03:21:47 -0800575 my @diff = command('diff-tree', $d,
Eric Wong751eb392007-08-31 18:16:12 -0700576 $gs->refname, '--');
577 my @finish;
578 if (@diff) {
579 @finish = rebase_cmd();
Eric Wongc74d9ac2007-11-05 03:21:47 -0800580 print STDERR "W: $d and ", $gs->refname,
Eric Wong751eb392007-08-31 18:16:12 -0700581 " differ, using @finish:\n",
Eric Wongc74d9ac2007-11-05 03:21:47 -0800582 join("\n", @diff), "\n";
Eric Wong751eb392007-08-31 18:16:12 -0700583 } else {
584 print "No changes between current HEAD and ",
585 $gs->refname,
586 "\nResetting to the latest ",
587 $gs->refname, "\n";
588 @finish = qw/reset --mixed/;
589 }
590 command_noisy(@finish, $gs->refname);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800591 if (@diff) {
592 @refs = ();
593 my ($url_, $rev_, $uuid_, $gs_) =
Thomas Rast5eec27e2009-05-29 17:09:42 +0200594 working_head_info('HEAD', \@refs);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800595 my ($linear_refs_, $parents_) =
596 linearize_history($gs_, \@refs);
597 if (scalar(@$linear_refs) !=
598 scalar(@$linear_refs_)) {
599 fatal "# of revisions changed ",
600 "\nbefore:\n",
601 join("\n", @$linear_refs),
602 "\n\nafter:\n",
603 join("\n", @$linear_refs_), "\n",
604 'If you are attempting to commit ',
605 "merges, try running:\n\t",
606 'git rebase --interactive',
607 '--preserve-merges ',
608 $gs->refname,
609 "\nBefore dcommitting";
610 }
Eric Wong711521e2008-08-20 00:30:06 -0700611 if ($url_ ne $expect_url) {
Alexander Gavrilovc03c1f72009-10-09 11:01:04 +0400612 if ($url_ eq $gs->metadata_url) {
613 print
614 "Accepting rewritten URL:",
615 " $url_\n";
616 } else {
617 fatal
618 "URL mismatch after rebase:",
619 " $url_ != $expect_url";
620 }
Eric Wongc74d9ac2007-11-05 03:21:47 -0800621 }
622 if ($uuid_ ne $uuid) {
623 fatal "uuid mismatch after rebase: ",
624 "$uuid_ != $uuid";
625 }
626 # remap parents
627 my (%p, @l, $i);
628 for ($i = 0; $i < scalar @$linear_refs; $i++) {
629 my $new = $linear_refs_->[$i] or next;
630 $p{$new} =
631 $parents->{$linear_refs->[$i]};
632 push @l, $new;
633 }
634 $parents = \%p;
635 $linear_refs = \@l;
636 }
Eric Wongb22d4492006-08-26 00:01:23 -0700637 }
638 }
Thomas Rast5eec27e2009-05-29 17:09:42 +0200639
640 if ($old_head) {
641 my $new_head = command_oneline(qw/rev-parse HEAD/);
642 my $new_is_symbolic = eval {
643 command_oneline(qw/symbolic-ref -q HEAD/);
644 };
645 if ($new_is_symbolic) {
646 print "dcommitted the branch ", $head, "\n";
647 } else {
648 print "dcommitted on a detached HEAD because you gave ",
649 "a revision argument.\n",
650 "The rewritten commit is: ", $new_head, "\n";
651 }
652 command(['checkout', $old_head], STDERR => 0);
653 }
654
Eric Wong3157dd92007-12-13 08:27:34 -0800655 unlink $gs->{index};
Eric Wongb22d4492006-08-26 00:01:23 -0700656}
657
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700658sub cmd_branch {
659 my ($branch_name, $head) = @_;
660
661 unless (defined $branch_name && length $branch_name) {
662 die(($_tag ? "tag" : "branch") . " name required\n");
663 }
664 $head ||= 'HEAD';
665
666 my ($src, $rev, undef, $gs) = working_head_info($head);
667
Deskin Millera0fbc872008-12-01 21:43:00 -0500668 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
Marc Branchaud62244062009-06-23 13:02:08 -0400669 my $allglobs = $remote->{ $_tag ? 'tags' : 'branches' };
670 my $glob;
671 if ($#{$allglobs} == 0) {
672 $glob = $allglobs->[0];
673 } else {
674 unless(defined $_branch_dest) {
675 die "Multiple ",
676 $_tag ? "tag" : "branch",
677 " paths defined for Subversion repository.\n",
678 "You must specify where you want to create the ",
679 $_tag ? "tag" : "branch",
680 " with the --destination argument.\n";
681 }
682 foreach my $g (@{$allglobs}) {
Eric Wongf7050592009-06-25 02:28:15 -0700683 # SVN::Git::Editor could probably be moved to Git.pm..
684 my $re = SVN::Git::Editor::glob2pat($g->{path}->{left});
685 if ($_branch_dest =~ /$re/) {
Marc Branchaud62244062009-06-23 13:02:08 -0400686 $glob = $g;
687 last;
688 }
689 }
690 unless (defined $glob) {
Eric Wongeaa14ff2009-07-25 01:36:06 -0700691 my $dest_re = qr/\b\Q$_branch_dest\E\b/;
692 foreach my $g (@{$allglobs}) {
693 $g->{path}->{left} =~ /$dest_re/ or next;
694 if (defined $glob) {
695 die "Ambiguous destination: ",
696 $_branch_dest, "\nmatches both '",
697 $glob->{path}->{left}, "' and '",
698 $g->{path}->{left}, "'\n";
699 }
700 $glob = $g;
701 }
702 unless (defined $glob) {
703 die "Unknown ",
704 $_tag ? "tag" : "branch",
705 " destination $_branch_dest\n";
706 }
Marc Branchaud62244062009-06-23 13:02:08 -0400707 }
708 }
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700709 my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
710 my $dst = join '/', $remote->{url}, $lft, $branch_name, ($rgt || ());
711
712 my $ctx = SVN::Client->new(
713 auth => Git::SVN::Ra::_auth_providers(),
714 log_msg => sub {
715 ${ $_[0] } = defined $_message
716 ? $_message
717 : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
718 . $branch_name;
719 },
720 );
721
722 eval {
723 $ctx->ls($dst, 'HEAD', 0);
724 } and die "branch ${branch_name} already exists\n";
725
726 print "Copying ${src} at r${rev} to ${dst}...\n";
727 $ctx->copy($src, $rev, $dst)
728 unless $_dry_run;
729
730 $gs->fetch_all;
731}
732
Adam Roben26e60162007-04-27 11:57:53 -0700733sub cmd_find_rev {
Marc-Andre Lureauea14e6c2008-03-11 10:00:45 +0200734 my $revision_or_hash = shift or die "SVN or git revision required ",
735 "as a command-line argument\n";
Adam Roben26e60162007-04-27 11:57:53 -0700736 my $result;
737 if ($revision_or_hash =~ /^r\d+$/) {
Adam Robenb3cb7e42007-04-29 01:35:27 -0700738 my $head = shift;
739 $head ||= 'HEAD';
740 my @refs;
João Abecasis63c56022008-07-14 16:28:04 +0100741 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
Adam Robenb3cb7e42007-04-29 01:35:27 -0700742 unless ($gs) {
743 die "Unable to determine upstream SVN information from ",
744 "$head history\n";
Adam Roben26e60162007-04-27 11:57:53 -0700745 }
Adam Robenb3cb7e42007-04-29 01:35:27 -0700746 my $desired_revision = substr($revision_or_hash, 1);
João Abecasis63c56022008-07-14 16:28:04 +0100747 $result = $gs->rev_map_get($desired_revision, $uuid);
Adam Roben26e60162007-04-27 11:57:53 -0700748 } else {
749 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
750 $result = $rev;
751 }
752 print "$result\n" if $result;
753}
754
Eric Wong905f8b72007-02-16 03:22:40 -0800755sub cmd_rebase {
756 command_noisy(qw/update-index --refresh/);
Eric Wong13c823f2007-04-08 00:59:19 -0700757 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
758 unless ($gs) {
Eric Wong905f8b72007-02-16 03:22:40 -0800759 die "Unable to determine upstream SVN information from ",
760 "working tree history\n";
761 }
Seth Falcon7d45e142008-05-19 20:29:17 -0700762 if ($_dry_run) {
763 print "Remote Branch: " . $gs->refname . "\n";
764 print "SVN URL: " . $url . "\n";
765 return;
766 }
Eric Wong905f8b72007-02-16 03:22:40 -0800767 if (command(qw/diff-index HEAD --/)) {
768 print STDERR "Cannot rebase with uncommited changes:\n";
769 command_noisy('status');
770 exit 1;
771 }
Eric Wongdee41f32007-03-13 11:40:36 -0700772 unless ($_local) {
Steven Grimmcec0d5a2007-11-29 11:54:39 -0800773 # rebase will checkout for us, so no need to do it explicitly
774 $_no_checkout = 'true';
Eric Wongdee41f32007-03-13 11:40:36 -0700775 $_fetch_all ? $gs->fetch_all : $gs->fetch;
776 }
Eric Wong905f8b72007-02-16 03:22:40 -0800777 command_noisy(rebase_cmd(), $gs->refname);
Eric Wong6111b932009-11-15 18:57:16 -0800778 $gs->mkemptydirs;
Eric Wong905f8b72007-02-16 03:22:40 -0800779}
780
Eric Wong5969cbe2007-01-11 17:58:39 -0800781sub cmd_show_ignore {
Eric Wong13c823f2007-04-08 00:59:19 -0700782 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
783 $gs ||= Git::SVN->new;
Eric Wong5969cbe2007-01-11 17:58:39 -0800784 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200785 $gs->prop_walk($gs->{path}, $r, sub {
786 my ($gs, $path, $props) = @_;
787 print STDOUT "\n# $path\n";
788 my $s = $props->{'svn:ignore'} or return;
789 $s =~ s/[\r\n]+/\n/g;
Michael Haggertya7d72542009-08-07 21:21:21 +0200790 $s =~ s/^\n+//;
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200791 chomp $s;
792 $s =~ s#^#$path#gm;
793 print STDOUT "$s\n";
794 });
Eric Wonga5e0ced2006-06-12 15:23:48 -0700795}
796
Vineet Kumar2d879792007-11-19 14:56:15 -0800797sub cmd_show_externals {
798 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
799 $gs ||= Git::SVN->new;
800 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
801 $gs->prop_walk($gs->{path}, $r, sub {
802 my ($gs, $path, $props) = @_;
803 print STDOUT "\n# $path\n";
804 my $s = $props->{'svn:externals'} or return;
805 $s =~ s/[\r\n]+/\n/g;
806 chomp $s;
807 $s =~ s#^#$path#gm;
808 print STDOUT "$s\n";
809 });
810}
811
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200812sub cmd_create_ignore {
813 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
814 $gs ||= Git::SVN->new;
815 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
816 $gs->prop_walk($gs->{path}, $r, sub {
817 my ($gs, $path, $props) = @_;
818 # $path is of the form /path/to/dir/
Brian Gernhardt7d9fd452009-02-19 13:08:04 -0500819 $path = '.' . $path;
820 # SVN can have attributes on empty directories,
821 # which git won't track
822 mkpath([$path]) unless -d $path;
823 my $ignore = $path . '.gitignore';
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200824 my $s = $props->{'svn:ignore'} or return;
825 open(GITIGNORE, '>', $ignore)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200826 or fatal("Failed to open `$ignore' for writing: $!");
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200827 $s =~ s/[\r\n]+/\n/g;
Michael Haggertya7d72542009-08-07 21:21:21 +0200828 $s =~ s/^\n+//;
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200829 chomp $s;
830 # Prefix all patterns so that the ignore doesn't apply
831 # to sub-directories.
832 $s =~ s#^#/#gm;
833 print GITIGNORE "$s\n";
834 close(GITIGNORE)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200835 or fatal("Failed to close `$ignore': $!");
Gustaf Hendebyc4c66b22008-05-05 00:33:09 +0200836 command_noisy('add', '-f', $ignore);
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200837 });
838}
839
Eric Wong6111b932009-11-15 18:57:16 -0800840sub cmd_mkdirs {
841 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
842 $gs ||= Git::SVN->new;
843 $gs->mkemptydirs($_revision);
844}
845
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800846sub canonicalize_path {
847 my ($path) = @_;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800848 my $dot_slash_added = 0;
849 if (substr($path, 0, 1) ne "/") {
850 $path = "./" . $path;
851 $dot_slash_added = 1;
852 }
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800853 # File::Spec->canonpath doesn't collapse x/../y into y (for a
854 # good reason), so let's do this manually.
855 $path =~ s#/+#/#g;
856 $path =~ s#/\.(?:/|$)#/#g;
857 $path =~ s#/[^/]+/\.\.##g;
858 $path =~ s#/$##g;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800859 $path =~ s#^\./## if $dot_slash_added;
Gerrit Pape2fe403e2008-07-06 19:28:50 +0000860 $path =~ s#^/##;
861 $path =~ s#^\.$##;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800862 return $path;
863}
864
Ulrich Dangel50ff2362009-06-26 16:52:09 +0200865sub canonicalize_url {
866 my ($url) = @_;
867 $url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
868 return $url;
869}
870
Benoit Sigoure15153452007-10-16 16:36:50 +0200871# get_svnprops(PATH)
872# ------------------
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200873# Helper for cmd_propget and cmd_proplist below.
Benoit Sigoure15153452007-10-16 16:36:50 +0200874sub get_svnprops {
875 my $path = shift;
876 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
877 $gs ||= Git::SVN->new;
878
879 # prefix THE PATH by the sub-directory from which the user
880 # invoked us.
881 $path = $cmd_dir_prefix . $path;
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200882 fatal("No such file or directory: $path") unless -e $path;
Benoit Sigoure15153452007-10-16 16:36:50 +0200883 my $is_dir = -d $path ? 1 : 0;
884 $path = $gs->{path} . '/' . $path;
885
886 # canonicalize the path (otherwise libsvn will abort or fail to
887 # find the file)
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800888 $path = canonicalize_path($path);
Benoit Sigoure15153452007-10-16 16:36:50 +0200889
890 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
891 my $props;
892 if ($is_dir) {
893 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
894 }
895 else {
896 (undef, $props) = $gs->ra->get_file($path, $r, undef);
897 }
898 return $props;
899}
900
901# cmd_propget (PROP, PATH)
902# ------------------------
903# Print the SVN property PROP for PATH.
904sub cmd_propget {
905 my ($prop, $path) = @_;
906 $path = '.' if not defined $path;
907 usage(1) if not defined $prop;
908 my $props = get_svnprops($path);
909 if (not defined $props->{$prop}) {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200910 fatal("`$path' does not have a `$prop' SVN property.");
Benoit Sigoure15153452007-10-16 16:36:50 +0200911 }
912 print $props->{$prop} . "\n";
913}
914
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200915# cmd_proplist (PATH)
916# -------------------
917# Print the list of SVN properties for PATH.
918sub cmd_proplist {
919 my $path = shift;
920 $path = '.' if not defined $path;
921 my $props = get_svnprops($path);
922 print "Properties on '$path':\n";
923 foreach (sort keys %{$props}) {
924 print " $_\n";
925 }
926}
927
Eric Wong8164b652007-01-11 15:35:55 -0800928sub cmd_multi_init {
Eric Wong9d55b412006-06-12 15:53:13 -0700929 my $url = shift;
Marc Branchaud62244062009-06-23 13:02:08 -0400930 unless (defined $_trunk || @_branches || @_tags) {
Eric Wong98327e52007-01-04 18:02:00 -0800931 usage(1);
932 }
Eric Wongdc431662007-05-19 03:59:02 -0700933
Eric Wong8164b652007-01-11 15:35:55 -0800934 $_prefix = '' unless defined $_prefix;
Eric Wongdadc6d22007-02-14 12:27:41 -0800935 if (defined $url) {
Ulrich Dangel50ff2362009-06-26 16:52:09 +0200936 $url = canonicalize_url($url);
Eric Wongdadc6d22007-02-14 12:27:41 -0800937 init_subdir(@_);
938 }
Eric Wongf30603f2007-02-23 01:26:26 -0800939 do_git_init_db();
Eric Wong98327e52007-01-04 18:02:00 -0800940 if (defined $_trunk) {
Adam Brewster6f5748e2009-08-11 23:14:27 -0400941 my $trunk_ref = 'refs/remotes/' . $_prefix . 'trunk';
Eric Wong706587f2007-01-18 17:50:01 -0800942 # try both old-style and new-style lookups:
943 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
Eric Wong8164b652007-01-11 15:35:55 -0800944 unless ($gs_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800945 my ($trunk_url, $trunk_path) =
946 complete_svn_url($url, $_trunk);
947 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
948 undef, $trunk_ref);
Eric Wong98327e52007-01-04 18:02:00 -0800949 }
Eric Wongc35b96e2006-10-11 11:53:21 -0700950 }
Marc Branchaud62244062009-06-23 13:02:08 -0400951 return unless @_branches || @_tags;
Eric Wonge7db67e2007-01-11 17:09:26 -0800952 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
Marc Branchaud62244062009-06-23 13:02:08 -0400953 foreach my $path (@_branches) {
954 complete_url_ls_init($ra, $path, '--branches/-b', $_prefix);
955 }
956 foreach my $path (@_tags) {
957 complete_url_ls_init($ra, $path, '--tags/-t', $_prefix.'tags/');
958 }
Eric Wong9d55b412006-06-12 15:53:13 -0700959}
960
Eric Wong1c8443b2007-01-14 02:17:00 -0800961sub cmd_multi_fetch {
Eric Wong4d0157d2009-11-22 12:37:06 -0800962 $Git::SVN::no_reuse_existing = undef;
Eric Wong0af9c9f2007-01-27 22:28:56 -0800963 my $remotes = Git::SVN::read_all_remotes();
964 foreach my $repo_id (sort keys %$remotes) {
Eric Wongdb03cd22007-02-13 00:38:02 -0800965 if ($remotes->{$repo_id}->{url}) {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800966 Git::SVN::fetch_all($repo_id, $remotes);
967 }
Eric Wong706587f2007-01-18 17:50:01 -0800968 }
Eric Wong9d55b412006-06-12 15:53:13 -0700969}
970
Eric Wong44320b92007-01-13 22:35:53 -0800971# this command is special because it requires no metadata
972sub cmd_commit_diff {
973 my ($ta, $tb, $url) = @_;
974 my $usage = "Usage: $0 commit-diff -r<revision> ".
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200975 "<tree-ish> <tree-ish> [<URL>]";
Eric Wong44320b92007-01-13 22:35:53 -0800976 fatal($usage) if (!defined $ta || !defined $tb);
Karl Hasselströmd72ab8c2008-05-17 17:07:09 +0200977 my $svn_path = '';
Eric Wong44320b92007-01-13 22:35:53 -0800978 if (!defined $url) {
979 my $gs = eval { Git::SVN->new };
980 if (!$gs) {
981 fatal("Needed URL or usable git-svn --id in ",
982 "the command-line\n", $usage);
983 }
984 $url = $gs->{url};
Eric Wongd3a840d2007-01-26 01:32:45 -0800985 $svn_path = $gs->{path};
Eric Wong44320b92007-01-13 22:35:53 -0800986 }
987 unless (defined $_revision) {
988 fatal("-r|--revision is a required argument\n", $usage);
989 }
990 if (defined $_message && defined $_file) {
991 fatal("Both --message/-m and --file/-F specified ",
992 "for the commit message.\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200993 "I have no idea what you mean");
Eric Wong44320b92007-01-13 22:35:53 -0800994 }
995 if (defined $_file) {
996 $_message = file_to_s($_file);
997 } else {
998 $_message ||= get_commit_entry($tb)->{log};
999 }
1000 my $ra ||= Git::SVN::Ra->new($url);
1001 my $r = $_revision;
1002 if ($r eq 'HEAD') {
1003 $r = $ra->get_latest_revnum;
1004 } elsif ($r !~ /^\d+$/) {
1005 die "revision argument: $r not understood by git-svn\n";
1006 }
Eric Wong61395352007-01-27 14:33:08 -08001007 my %ed_opts = ( r => $r,
1008 log => $_message,
1009 ra => $ra,
1010 tree_a => $ta,
1011 tree_b => $tb,
1012 editor_cb => sub { print "Committed r$_[0]\n" },
1013 svn_path => $svn_path );
1014 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong44320b92007-01-13 22:35:53 -08001015 print "No changes\n$ta == $tb\n";
1016 }
Eric Wong44320b92007-01-13 22:35:53 -08001017}
1018
Thomas Rast05427b92008-08-26 21:32:37 +02001019sub escape_uri_only {
1020 my ($uri) = @_;
1021 my @tmp;
1022 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -07001023 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Thomas Rast05427b92008-08-26 21:32:37 +02001024 push @tmp, $_;
1025 }
1026 join('/', @tmp);
1027}
1028
1029sub escape_url {
1030 my ($url) = @_;
1031 if ($url =~ m#^([^:]+)://([^/]*)(.*)$#) {
1032 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
1033 $url = "$scheme://$domain$uri";
1034 }
1035 $url;
1036}
1037
David D. Kilzere6fefa92007-11-21 11:57:18 -08001038sub cmd_info {
Eric Wongbd2d4f92008-08-05 00:35:16 -07001039 my $path = canonicalize_path(defined($_[0]) ? $_[0] : ".");
Thomas Rastedde9112008-08-26 21:32:36 +02001040 my $fullpath = canonicalize_path($cmd_dir_prefix . $path);
Eric Wongbd2d4f92008-08-05 00:35:16 -07001041 if (exists $_[1]) {
David D. Kilzere6fefa92007-11-21 11:57:18 -08001042 die "Too many arguments specified\n";
1043 }
1044
1045 my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
1046
1047 if (!$file_type && !$diff_status) {
Thomas Rast2cf3e3a2008-08-29 15:42:48 +02001048 print STDERR "svn: '$path' is not under version control\n";
1049 exit 1;
David D. Kilzere6fefa92007-11-21 11:57:18 -08001050 }
1051
1052 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1053 unless ($gs) {
1054 die "Unable to determine upstream SVN information from ",
1055 "working tree history\n";
1056 }
Eric Wongbd2d4f92008-08-05 00:35:16 -07001057
1058 # canonicalize_path() will return "" to make libsvn 1.5.x happy,
1059 $path = "." if $path eq "";
1060
Thomas Rastedde9112008-08-26 21:32:36 +02001061 my $full_url = $url . ($fullpath eq "" ? "" : "/$fullpath");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001062
David D. Kilzer8b014d72007-11-21 11:57:19 -08001063 if ($_url) {
Thomas Rast05427b92008-08-26 21:32:37 +02001064 print escape_url($full_url), "\n";
David D. Kilzer8b014d72007-11-21 11:57:19 -08001065 return;
1066 }
1067
David D. Kilzere6fefa92007-11-21 11:57:18 -08001068 my $result = "Path: $path\n";
1069 $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
Thomas Rast05427b92008-08-26 21:32:37 +02001070 $result .= "URL: " . escape_url($full_url) . "\n";
David D. Kilzere6fefa92007-11-21 11:57:18 -08001071
Eric Wonga5460eb2007-11-21 18:20:57 -08001072 eval {
1073 my $repos_root = $gs->repos_root;
1074 Git::SVN::remove_username($repos_root);
Thomas Rast05427b92008-08-26 21:32:37 +02001075 $result .= "Repository Root: " . escape_url($repos_root) . "\n";
Eric Wonga5460eb2007-11-21 18:20:57 -08001076 };
1077 if ($@) {
1078 $result .= "Repository Root: (offline)\n";
1079 }
Marcel Koeppen22ba47f2009-01-19 03:02:01 +01001080 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" &&
1081 ($SVN::Core::VERSION le '1.5.4' || $file_type ne "dir");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001082 $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
1083
1084 $result .= "Node Kind: " .
1085 ($file_type eq "dir" ? "directory" : "file") . "\n";
1086
1087 my $schedule = $diff_status eq "A"
1088 ? "add"
1089 : ($diff_status eq "D" ? "delete" : "normal");
1090 $result .= "Schedule: $schedule\n";
1091
1092 if ($diff_status eq "A") {
1093 print $result, "\n";
1094 return;
1095 }
1096
1097 my ($lc_author, $lc_rev, $lc_date_utc);
Thomas Rastedde9112008-08-26 21:32:36 +02001098 my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001099 my $log = command_output_pipe(@args);
1100 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
1101 while (<$log>) {
1102 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
1103 $lc_author = $1;
1104 $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
1105 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
1106 (undef, $lc_rev, undef) = ::extract_metadata($1);
1107 }
1108 }
1109 close $log;
1110
1111 Git::SVN::Log::set_local_timezone();
1112
1113 $result .= "Last Changed Author: $lc_author\n";
1114 $result .= "Last Changed Rev: $lc_rev\n";
1115 $result .= "Last Changed Date: " .
1116 Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
1117
1118 if ($file_type ne "dir") {
1119 my $text_last_updated_date =
1120 ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
1121 $result .=
1122 "Text Last Updated: " .
1123 Git::SVN::Log::format_svn_date($text_last_updated_date) .
1124 "\n";
1125 my $checksum;
1126 if ($diff_status eq "D") {
1127 my ($fh, $ctx) =
1128 command_output_pipe(qw(cat-file blob), "HEAD:$path");
1129 if ($file_type eq "link") {
1130 my $file_name = <$fh>;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001131 $checksum = md5sum("link $file_name");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001132 } else {
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001133 $checksum = md5sum($fh);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001134 }
1135 command_close_pipe($fh, $ctx);
1136 } elsif ($file_type eq "link") {
1137 my $file_name =
1138 command(qw(cat-file blob), "HEAD:$path");
1139 $checksum =
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001140 md5sum("link " . $file_name);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001141 } else {
1142 open FILE, "<", $path or die $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001143 $checksum = md5sum(\*FILE);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001144 close FILE or die $!;
1145 }
1146 $result .= "Checksum: " . $checksum . "\n";
1147 }
1148
1149 print $result, "\n";
1150}
1151
Ben Jackson195643f2009-06-03 20:45:52 -07001152sub cmd_reset {
1153 my $target = shift || $_revision or die "SVN revision required\n";
1154 $target = $1 if $target =~ /^r(\d+)$/;
1155 $target =~ /^\d+$/ or die "Numeric SVN revision expected\n";
1156 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1157 unless ($gs) {
1158 die "Unable to determine upstream SVN information from ".
1159 "history\n";
1160 }
1161 my ($r, $c) = $gs->find_rev_before($target, not $_fetch_parent);
1162 $gs->rev_map_set($r, $c, 'reset', $uuid);
1163 print "r$r = $c ($gs->{ref_id})\n";
1164}
1165
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -05001166sub cmd_gc {
1167 if (!$can_compress) {
1168 warn "Compress::Zlib could not be found; unhandled.log " .
1169 "files will not be compressed.\n";
1170 }
1171 find({ wanted => \&gc_directory, no_chdir => 1}, "$ENV{GIT_DIR}/svn");
1172}
1173
Eric Wong3397f9d2006-02-16 01:24:16 -08001174########################### utility functions #########################
1175
Eric Wong905f8b72007-02-16 03:22:40 -08001176sub rebase_cmd {
1177 my @cmd = qw/rebase/;
1178 push @cmd, '-v' if $_verbose;
1179 push @cmd, qw/--merge/ if $_merge;
1180 push @cmd, "--strategy=$_strategy" if $_strategy;
1181 @cmd;
1182}
1183
Eric Wong1e889ef2007-02-16 01:45:13 -08001184sub post_fetch_checkout {
1185 return if $_no_checkout;
1186 my $gs = $Git::SVN::_head or return;
1187 return if verify_ref('refs/heads/master^0');
1188
Eric Wongb186a262009-08-12 16:01:59 -07001189 # look for "trunk" ref if it exists
1190 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
1191 my $fetch = $remote->{fetch};
1192 if ($fetch) {
1193 foreach my $p (keys %$fetch) {
1194 basename($fetch->{$p}) eq 'trunk' or next;
1195 $gs = Git::SVN->new($fetch->{$p}, $gs->{repo_id}, $p);
1196 last;
1197 }
1198 }
1199
Eric Wong1e889ef2007-02-16 01:45:13 -08001200 my $valid_head = verify_ref('HEAD^0');
1201 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
1202 return if ($valid_head || !verify_ref('HEAD^0'));
1203
1204 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
1205 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
1206 return if -f $index;
1207
Matthias Lederhofer7ae3df82007-06-03 16:48:16 +02001208 return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
Eric Wong1e889ef2007-02-16 01:45:13 -08001209 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
1210 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
1211 print STDERR "Checked out HEAD:\n ",
1212 $gs->full_url, " r", $gs->last_rev, "\n";
Eric Wong6111b932009-11-15 18:57:16 -08001213 $gs->mkemptydirs($gs->last_rev);
Eric Wong1e889ef2007-02-16 01:45:13 -08001214}
1215
Eric Wong98327e52007-01-04 18:02:00 -08001216sub complete_svn_url {
1217 my ($url, $path) = @_;
1218 $path =~ s#/+$##;
Eric Wong98327e52007-01-04 18:02:00 -08001219 if ($path !~ m#^[a-z\+]+://#) {
Eric Wong98327e52007-01-04 18:02:00 -08001220 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
1221 fatal("E: '$path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001222 "and a separate URL is not specified");
Eric Wong98327e52007-01-04 18:02:00 -08001223 }
Eric Wong706587f2007-01-18 17:50:01 -08001224 return ($url, $path);
Eric Wong98327e52007-01-04 18:02:00 -08001225 }
Eric Wong706587f2007-01-18 17:50:01 -08001226 return ($path, '');
Eric Wong98327e52007-01-04 18:02:00 -08001227}
1228
Eric Wong9d55b412006-06-12 15:53:13 -07001229sub complete_url_ls_init {
Eric Wong706587f2007-01-18 17:50:01 -08001230 my ($ra, $repo_path, $switch, $pfx) = @_;
1231 unless ($repo_path) {
Eric Wong9d55b412006-06-12 15:53:13 -07001232 print STDERR "W: $switch not specified\n";
1233 return;
1234 }
Eric Wong706587f2007-01-18 17:50:01 -08001235 $repo_path =~ s#/+$##;
1236 if ($repo_path =~ m#^[a-z\+]+://#) {
1237 $ra = Git::SVN::Ra->new($repo_path);
1238 $repo_path = '';
Eric Wonge7db67e2007-01-11 17:09:26 -08001239 } else {
Eric Wong706587f2007-01-18 17:50:01 -08001240 $repo_path =~ s#^/+##;
Eric Wonge7db67e2007-01-11 17:09:26 -08001241 unless ($ra) {
Eric Wong706587f2007-01-18 17:50:01 -08001242 fatal("E: '$repo_path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001243 "and a separate URL is not specified");
Eric Wong9d55b412006-06-12 15:53:13 -07001244 }
Eric Wonge7db67e2007-01-11 17:09:26 -08001245 }
Eric Wong706587f2007-01-18 17:50:01 -08001246 my $url = $ra->{url};
Eric Wongb4d57e52007-02-14 15:10:44 -08001247 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
1248 my $k = "svn-remote.$gs->{repo_id}.url";
1249 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
1250 if ($orig_url && ($orig_url ne $gs->{url})) {
1251 die "$k already set: $orig_url\n",
1252 "wanted to set to: $gs->{url}\n";
Eric Wong88cf4102007-02-01 03:59:07 -08001253 }
Eric Wongb4d57e52007-02-14 15:10:44 -08001254 command_oneline('config', $k, $gs->{url}) unless $orig_url;
Mattias Nissler0b2af452009-07-07 01:40:02 +02001255 my $remote_path = "$gs->{path}/$repo_path";
Eric Wong5268f9e2009-08-16 14:22:12 -07001256 $remote_path =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
Eric Wongb4d57e52007-02-14 15:10:44 -08001257 $remote_path =~ s#/+#/#g;
1258 $remote_path =~ s#^/##g;
Eric Wonged0b9d42008-03-14 11:01:23 -07001259 $remote_path .= "/*" if $remote_path !~ /\*/;
Eric Wongb4d57e52007-02-14 15:10:44 -08001260 my ($n) = ($switch =~ /^--(\w+)/);
1261 if (length $pfx && $pfx !~ m#/$#) {
1262 die "--prefix='$pfx' must have a trailing slash '/'\n";
Eric Wong9d55b412006-06-12 15:53:13 -07001263 }
Marcus Griep570d35c2008-08-08 01:41:57 -07001264 command_noisy('config',
Marc Branchaud62244062009-06-23 13:02:08 -04001265 '--add',
Marcus Griep570d35c2008-08-08 01:41:57 -07001266 "svn-remote.$gs->{repo_id}.$n",
1267 "$remote_path:refs/remotes/$pfx*" .
1268 ('/*' x (($remote_path =~ tr/*/*/) - 1)) );
Eric Wong9d55b412006-06-12 15:53:13 -07001269}
1270
Eric Wongaef4e922006-12-15 10:59:54 -08001271sub verify_ref {
1272 my ($ref) = @_;
Eric Wong2c5c1d52006-12-28 01:16:21 -08001273 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
1274 { STDERR => 0 }); };
Eric Wongaef4e922006-12-15 10:59:54 -08001275}
1276
Eric Wonga5e0ced2006-06-12 15:23:48 -07001277sub get_tree_from_treeish {
Eric Wongcf52b8f2006-02-20 10:57:28 -08001278 my ($treeish) = @_;
Eric Wong44320b92007-01-13 22:35:53 -08001279 # $treeish can be a symbolic ref, too:
Eric Wongaef4e922006-12-15 10:59:54 -08001280 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001281 my $expected;
1282 while ($type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001283 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001284 }
1285 if ($type eq 'commit') {
Eric Wongaef4e922006-12-15 10:59:54 -08001286 $expected = (grep /^tree /, command(qw/cat-file commit/,
1287 $treeish))[0];
Eric Wong44320b92007-01-13 22:35:53 -08001288 ($expected) = ($expected =~ /^tree ($sha1)$/o);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001289 die "Unable to get tree from $treeish\n" unless $expected;
1290 } elsif ($type eq 'tree') {
1291 $expected = $treeish;
1292 } else {
1293 die "$treeish is a $type, expected tree, tag or commit\n";
1294 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07001295 return $expected;
1296}
Eric Wongcf52b8f2006-02-20 10:57:28 -08001297
Eric Wong44320b92007-01-13 22:35:53 -08001298sub get_commit_entry {
1299 my ($treeish) = shift;
1300 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
1301 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1302 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1303 open my $log_fh, '>', $commit_editmsg or croak $!;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001304
Eric Wong44320b92007-01-13 22:35:53 -08001305 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wong4ad45152006-07-09 20:20:48 -07001306 if ($type eq 'commit' || $type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001307 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
Eric Wong44320b92007-01-13 22:35:53 -08001308 $type, $treeish);
Eric Wong3397f9d2006-02-16 01:24:16 -08001309 my $in_msg = 0;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001310 my $author;
1311 my $saw_from = 0;
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001312 my $msgbuf = "";
Eric Wong3397f9d2006-02-16 01:24:16 -08001313 while (<$msg_fh>) {
1314 if (!$in_msg) {
1315 $in_msg = 1 if (/^\s*$/);
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001316 $author = $1 if (/^author (.*>)/);
Eric Wongdf746c52006-03-03 01:20:08 -08001317 } elsif (/^git-svn-id: /) {
Eric Wong44320b92007-01-13 22:35:53 -08001318 # skip this for now, we regenerate the
1319 # correct one on re-fetch anyways
1320 # TODO: set *:merge properties or like...
Eric Wong3397f9d2006-02-16 01:24:16 -08001321 } else {
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001322 if (/^From:/ || /^Signed-off-by:/) {
1323 $saw_from = 1;
1324 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001325 $msgbuf .= $_;
Eric Wong3397f9d2006-02-16 01:24:16 -08001326 }
1327 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001328 $msgbuf =~ s/\s+$//s;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001329 if ($Git::SVN::_add_author_from && defined($author)
1330 && !$saw_from) {
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001331 $msgbuf .= "\n\nFrom: $author";
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001332 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001333 print $log_fh $msgbuf or croak $!;
Eric Wongaef4e922006-12-15 10:59:54 -08001334 command_close_pipe($msg_fh, $ctx);
Eric Wong3397f9d2006-02-16 01:24:16 -08001335 }
Eric Wong44320b92007-01-13 22:35:53 -08001336 close $log_fh or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -08001337
1338 if ($_edit || ($type eq 'tree')) {
Jonathan Niederb4479f02009-10-30 20:42:34 -05001339 chomp(my $editor = command_oneline(qw(var GIT_EDITOR)));
1340 system('sh', '-c', $editor.' "$@"', $editor, $commit_editmsg);
Eric Wong3397f9d2006-02-16 01:24:16 -08001341 }
Eric Wong44320b92007-01-13 22:35:53 -08001342 rename $commit_editmsg, $commit_msg or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07001343 {
Eric Wongb510df82009-05-28 00:56:23 -07001344 require Encode;
Eric Wong16fc08e2008-10-29 23:49:26 -07001345 # SVN requires messages to be UTF-8 when entering the repo
1346 local $/;
1347 open $log_fh, '<', $commit_msg or croak $!;
1348 binmode $log_fh;
1349 chomp($log_entry{log} = <$log_fh>);
1350
Eric Wongb510df82009-05-28 00:56:23 -07001351 my $enc = Git::config('i18n.commitencoding') || 'UTF-8';
1352 my $msg = $log_entry{log};
1353
1354 eval { $msg = Encode::decode($enc, $msg, 1) };
1355 if ($@) {
1356 die "Could not decode as $enc:\n", $msg,
1357 "\nPerhaps you need to set i18n.commitencoding\n";
Eric Wong16fc08e2008-10-29 23:49:26 -07001358 }
Eric Wongb510df82009-05-28 00:56:23 -07001359
1360 eval { $msg = Encode::encode('UTF-8', $msg, 1) };
1361 die "Could not encode as UTF-8:\n$msg\n" if $@;
1362
1363 $log_entry{log} = $msg;
1364
Eric Wong16fc08e2008-10-29 23:49:26 -07001365 close $log_fh or croak $!;
1366 }
Eric Wong44320b92007-01-13 22:35:53 -08001367 unlink $commit_msg;
1368 \%log_entry;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001369}
1370
Eric Wong3397f9d2006-02-16 01:24:16 -08001371sub s_to_file {
1372 my ($str, $file, $mode) = @_;
1373 open my $fd,'>',$file or croak $!;
1374 print $fd $str,"\n" or croak $!;
1375 close $fd or croak $!;
1376 chmod ($mode &~ umask, $file) if (defined $mode);
1377}
1378
1379sub file_to_s {
1380 my $file = shift;
1381 open my $fd,'<',$file or croak "$!: file: $file\n";
1382 local $/;
1383 my $ret = <$fd>;
1384 close $fd or croak $!;
1385 $ret =~ s/\s*$//s;
1386 return $ret;
1387}
1388
Eric Wongeeb0abe2006-03-03 01:20:08 -08001389# '<svn username> = real-name <email address>' mapping based on git-svnimport:
1390sub load_authors {
1391 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001392 my $log = $cmd eq 'log';
Eric Wongeeb0abe2006-03-03 01:20:08 -08001393 while (<$authors>) {
1394 chomp;
Richard MUSIL575d0252007-07-17 19:02:57 +02001395 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
Eric Wongeeb0abe2006-03-03 01:20:08 -08001396 my ($user, $name, $email) = ($1, $2, $3);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001397 if ($log) {
1398 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
1399 } else {
1400 $users{$user} = [$name, $email];
1401 }
Eric Wong79bb8d82006-06-01 02:35:44 -07001402 }
1403 close $authors or croak $!;
1404}
1405
Tom Princee0d10e12007-01-28 16:16:53 -08001406# convert GetOpt::Long specs for use by git-config
Eric Wong1a305822009-11-14 14:25:11 -08001407sub read_git_config {
Eric Wongb8c92ca2006-05-24 01:40:37 -07001408 my $opts = shift;
Eric Wong97ae0912007-02-11 15:21:24 -08001409 my @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001410 foreach my $o (keys %$opts) {
Eric Wong97ae0912007-02-11 15:21:24 -08001411 # if we have mixedCase and a long option-only, then
1412 # it's a config-only variable that we don't need for
1413 # the command-line.
1414 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001415 my $v = $opts->{$o};
Eric Wong97ae0912007-02-11 15:21:24 -08001416 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001417 $key =~ s/-//g;
Deskin Miller225f1d02008-10-23 15:21:34 -04001418 my $arg = 'git config';
Eric Wongb8c92ca2006-05-24 01:40:37 -07001419 $arg .= ' --int' if ($o =~ /[:=]i$/);
1420 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1421 if (ref $v eq 'ARRAY') {
1422 chomp(my @tmp = `$arg --get-all svn.$key`);
1423 @$v = @tmp if @tmp;
1424 } else {
1425 chomp(my $tmp = `$arg --get svn.$key`);
Eric Wong77742842007-02-10 21:07:12 -08001426 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
Eric Wongb8c92ca2006-05-24 01:40:37 -07001427 $$v = $tmp;
1428 }
1429 }
1430 }
Eric Wong97ae0912007-02-11 15:21:24 -08001431 delete @$opts{@config_only} if @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001432}
1433
Eric Wong79bb8d82006-06-01 02:35:44 -07001434sub extract_metadata {
Eric Wongc1927a82006-06-27 19:39:11 -07001435 my $id = shift or return (undef, undef, undef);
Sam Vilain3dfab992007-06-30 20:56:13 +12001436 my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
Eric Wongb3e95932009-07-11 14:13:12 -07001437 \s([a-f\d\-]+)$/ix);
Eric Wonge70dc782006-11-23 14:54:04 -08001438 if (!defined $rev || !$uuid || !$url) {
Eric Wong79bb8d82006-06-01 02:35:44 -07001439 # some of the original repositories I made had
Pavel Roskin82e5a822006-07-10 01:50:18 -04001440 # identifiers like this:
Eric Wongb3e95932009-07-11 14:13:12 -07001441 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/i);
Eric Wong79bb8d82006-06-01 02:35:44 -07001442 }
1443 return ($url, $rev, $uuid);
1444}
1445
Eric Wongc1927a82006-06-27 19:39:11 -07001446sub cmt_metadata {
1447 return extract_metadata((grep(/^git-svn-id: /,
Eric Wongaef4e922006-12-15 10:59:54 -08001448 command(qw/cat-file commit/, shift)))[-1]);
Eric Wongc1927a82006-06-27 19:39:11 -07001449}
1450
Boris Byk6ea42032009-04-11 00:32:41 +04001451sub cmt_sha2rev_batch {
1452 my %s2r;
1453 my ($pid, $in, $out, $ctx) = command_bidi_pipe(qw/cat-file --batch/);
1454 my $list = shift;
1455
1456 foreach my $sha (@{$list}) {
1457 my $first = 1;
1458 my $size = 0;
1459 print $out $sha, "\n";
1460
1461 while (my $line = <$in>) {
1462 if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) {
1463 last;
1464 } elsif ($first &&
1465 $line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) {
1466 $first = 0;
1467 $size = $1;
1468 next;
1469 } elsif ($line =~ /^(git-svn-id: )/) {
1470 my (undef, $rev, undef) =
1471 extract_metadata($line);
1472 $s2r{$sha} = $rev;
1473 }
1474
1475 $size -= length($line);
1476 last if ($size == 0);
1477 }
1478 }
1479
1480 command_close_bidi_pipe($pid, $in, $out, $ctx);
1481
1482 return \%s2r;
1483}
1484
Eric Wong905f8b72007-02-16 03:22:40 -08001485sub working_head_info {
1486 my ($head, $refs) = @_;
Pedro Melob6309ac2008-04-10 17:05:21 +01001487 my @args = ('log', '--no-color', '--first-parent', '--pretty=medium');
Lars Hjemli05b4df32007-09-05 11:35:29 +02001488 my ($fh, $ctx) = command_output_pipe(@args, $head);
Sam Vilain3dfab992007-06-30 20:56:13 +12001489 my $hash;
Sam Vilain40cb8f82007-06-30 20:56:14 +12001490 my %max;
Sam Vilain3dfab992007-06-30 20:56:13 +12001491 while (<$fh>) {
1492 if ( m{^commit ($::sha1)$} ) {
1493 unshift @$refs, $hash if $hash and $refs;
1494 $hash = $1;
1495 next;
1496 }
1497 next unless s{^\s*(git-svn-id:)}{$1};
1498 my ($url, $rev, $uuid) = extract_metadata($_);
Eric Wong13c823f2007-04-08 00:59:19 -07001499 if (defined $url && defined $rev) {
Sam Vilain40cb8f82007-06-30 20:56:14 +12001500 next if $max{$url} and $max{$url} < $rev;
Eric Wong13c823f2007-04-08 00:59:19 -07001501 if (my $gs = Git::SVN->find_by_url($url)) {
João Abecasis63c56022008-07-14 16:28:04 +01001502 my $c = $gs->rev_map_get($rev, $uuid);
Adam Robenb03c7a62007-04-25 11:50:32 -07001503 if ($c && $c eq $hash) {
Eric Wong13c823f2007-04-08 00:59:19 -07001504 close $fh; # break the pipe
1505 return ($url, $rev, $uuid, $gs);
Sam Vilain40cb8f82007-06-30 20:56:14 +12001506 } else {
Eric Wong060610c2007-12-08 23:27:41 -08001507 $max{$url} ||= $gs->rev_map_max;
Eric Wong13c823f2007-04-08 00:59:19 -07001508 }
1509 }
1510 }
Eric Wong905f8b72007-02-16 03:22:40 -08001511 }
Eric Wong13c823f2007-04-08 00:59:19 -07001512 command_close_pipe($fh, $ctx);
1513 (undef, undef, undef, undef);
Eric Wong905f8b72007-02-16 03:22:40 -08001514}
1515
Eric Wong733a65a2007-06-13 02:23:28 -07001516sub read_commit_parents {
1517 my ($parents, $c) = @_;
Eric Wong7b02b852007-09-08 16:33:08 -07001518 chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
1519 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1520 @{$parents->{$c}} = split(/ /, $p);
Eric Wong733a65a2007-06-13 02:23:28 -07001521}
1522
1523sub linearize_history {
1524 my ($gs, $refs) = @_;
1525 my %parents;
1526 foreach my $c (@$refs) {
1527 read_commit_parents(\%parents, $c);
1528 }
1529
1530 my @linear_refs;
1531 my %skip = ();
1532 my $last_svn_commit = $gs->last_commit;
1533 foreach my $c (reverse @$refs) {
1534 next if $c eq $last_svn_commit;
1535 last if $skip{$c};
1536
1537 unshift @linear_refs, $c;
1538 $skip{$c} = 1;
1539
1540 # we only want the first parent to diff against for linear
1541 # history, we save the rest to inject when we finalize the
1542 # svn commit
1543 my $fp_a = verify_ref("$c~1");
1544 my $fp_b = shift @{$parents{$c}} if $parents{$c};
1545 if (!$fp_a || !$fp_b) {
1546 die "Commit $c\n",
1547 "has no parent commit, and therefore ",
1548 "nothing to diff against.\n",
1549 "You should be working from a repository ",
1550 "originally created by git-svn\n";
1551 }
1552 if ($fp_a ne $fp_b) {
1553 die "$c~1 = $fp_a, however parsing commit $c ",
1554 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1555 }
1556
1557 foreach my $p (@{$parents{$c}}) {
1558 $skip{$p} = 1;
1559 }
1560 }
1561 (\@linear_refs, \%parents);
1562}
1563
David D. Kilzere6fefa92007-11-21 11:57:18 -08001564sub find_file_type_and_diff_status {
1565 my ($path) = @_;
Dmitry Potapov107cee52008-07-21 00:14:07 +04001566 return ('dir', '') if $path eq '';
David D. Kilzere6fefa92007-11-21 11:57:18 -08001567
1568 my $diff_output =
1569 command_oneline(qw(diff --cached --name-status --), $path) || "";
1570 my $diff_status = (split(' ', $diff_output))[0] || "";
1571
1572 my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || "";
1573
1574 return (undef, undef) if !$diff_status && !$ls_tree;
1575
1576 if ($diff_status eq "A") {
1577 return ("link", $diff_status) if -l $path;
1578 return ("dir", $diff_status) if -d $path;
1579 return ("file", $diff_status);
1580 }
1581
1582 my $mode = (split(' ', $ls_tree))[0] || "";
1583
1584 return ("link", $diff_status) if $mode eq "120000";
1585 return ("dir", $diff_status) if $mode eq "040000";
1586 return ("file", $diff_status);
1587}
1588
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001589sub md5sum {
1590 my $arg = shift;
1591 my $ref = ref $arg;
1592 my $md5 = Digest::MD5->new();
Marcus Griep0b191382008-08-12 12:00:53 -04001593 if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001594 $md5->addfile($arg) or croak $!;
1595 } elsif ($ref eq 'SCALAR') {
1596 $md5->add($$arg) or croak $!;
1597 } elsif (!$ref) {
1598 $md5->add($arg) or croak $!;
1599 } else {
1600 ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1601 }
1602 return $md5->hexdigest();
1603}
1604
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -05001605sub gc_directory {
1606 if ($can_compress && -f $_ && basename($_) eq "unhandled.log") {
1607 my $out_filename = $_ . ".gz";
1608 open my $in_fh, "<", $_ or die "Unable to open $_: $!\n";
1609 binmode $in_fh;
1610 my $gz = Compress::Zlib::gzopen($out_filename, "ab") or
1611 die "Unable to open $out_filename: $!\n";
1612
1613 my $res;
1614 while ($res = sysread($in_fh, my $str, 1024)) {
1615 $gz->gzwrite($str) or
1616 die "Unable to write: ".$gz->gzerror()."!\n";
1617 }
1618 unlink $_ or die "unlink $File::Find::name: $!\n";
1619 } elsif (-f $_ && basename($_) eq "index") {
1620 unlink $_ or die "unlink $_: $!\n";
1621 }
1622}
1623
Eric Wong9b981fc2007-01-11 12:14:21 -08001624package Git::SVN;
1625use strict;
1626use warnings;
Eric Wong060610c2007-12-08 23:27:41 -08001627use Fcntl qw/:DEFAULT :seek/;
1628use constant rev_map_fmt => 'NH40';
Eric Wongecc712d2007-01-31 12:28:10 -08001629use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
Eric Wong62e349d2007-02-16 19:57:29 -08001630 $_repack $_repack_flags $_use_svm_props $_head
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00001631 $_use_svnsync_props $no_reuse_existing $_minimize_url
Pete Harlane82f0d72009-01-17 20:10:14 -08001632 $_use_log_author $_add_author_from $_localtime/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001633use Carp qw/croak/;
1634use File::Path qw/mkpath/;
Eric Wong373274f2007-01-31 13:54:23 -08001635use File::Copy qw/copy/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001636use IPC::Open3;
Sam Vilain7d944c32009-12-20 00:55:13 +13001637use Memoize; # core since 5.8.0, Jul 2002
Eric Wong9b981fc2007-01-11 12:14:21 -08001638
Karl Hasselström94bc9142008-02-03 17:56:18 +01001639my ($_gc_nr, $_gc_period);
1640
Eric Wong9b981fc2007-01-11 12:14:21 -08001641# properties that we do not log:
1642my %SKIP_PROP;
1643BEGIN {
1644 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
1645 svn:special svn:executable
1646 svn:entry:committed-rev
1647 svn:entry:last-author
1648 svn:entry:uuid
1649 svn:entry:committed-date/;
Eric Wong91b03282007-02-11 00:51:33 -08001650
1651 # some options are read globally, but can be overridden locally
1652 # per [svn-remote "..."] section. Command-line options will *NOT*
1653 # override options set in an [svn-remote "..."] section
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001654 no strict 'refs';
1655 for my $option (qw/follow_parent no_metadata use_svm_props
1656 use_svnsync_props/) {
1657 my $key = $option;
Eric Wong91b03282007-02-11 00:51:33 -08001658 $key =~ tr/_//d;
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001659 my $prop = "-$option";
1660 *$option = sub {
1661 my ($self) = @_;
1662 return $self->{$prop} if exists $self->{$prop};
1663 my $k = "svn-remote.$self->{repo_id}.$key";
1664 eval { command_oneline(qw/config --get/, $k) };
1665 if ($@) {
1666 $self->{$prop} = ${"Git::SVN::_$option"};
Eric Wong91b03282007-02-11 00:51:33 -08001667 } else {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001668 my $v = command_oneline(qw/config --bool/,$k);
1669 $self->{$prop} = $v eq 'false' ? 0 : 1;
Eric Wong91b03282007-02-11 00:51:33 -08001670 }
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001671 return $self->{$prop};
1672 }
Eric Wong91b03282007-02-11 00:51:33 -08001673 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001674}
1675
Marcus Griep0b191382008-08-12 12:00:53 -04001676
Eric Wong321b1842008-01-02 10:10:03 -08001677my (%LOCKFILES, %INDEX_FILES);
1678END {
1679 unlink keys %LOCKFILES if %LOCKFILES;
1680 unlink keys %INDEX_FILES if %INDEX_FILES;
1681}
Eric Wong373274f2007-01-31 13:54:23 -08001682
Eric Wong4bb9ed02007-02-03 13:29:17 -08001683sub resolve_local_globs {
1684 my ($url, $fetch, $glob_spec) = @_;
1685 return unless defined $glob_spec;
1686 my $ref = $glob_spec->{ref};
1687 my $path = $glob_spec->{path};
Adam Brewster6f5748e2009-08-11 23:14:27 -04001688 foreach (command(qw#for-each-ref --format=%(refname) refs/#)) {
1689 next unless m#^$ref->{regex}$#;
Eric Wong4bb9ed02007-02-03 13:29:17 -08001690 my $p = $1;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001691 my $pathname = desanitize_refname($path->full_path($p));
1692 my $refname = desanitize_refname($ref->full_path($p));
Eric Wong4bb9ed02007-02-03 13:29:17 -08001693 if (my $existing = $fetch->{$pathname}) {
1694 if ($existing ne $refname) {
1695 die "Refspec conflict:\n",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001696 "existing: $existing\n",
1697 " globbed: $refname\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08001698 }
Adam Brewster6f5748e2009-08-11 23:14:27 -04001699 my $u = (::cmt_metadata("$refname"))[0];
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001700 $u =~ s!^\Q$url\E(/|$)!! or die
Adam Brewster6f5748e2009-08-11 23:14:27 -04001701 "$refname: '$url' not found in '$u'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08001702 if ($pathname ne $u) {
1703 warn "W: Refspec glob conflict ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001704 "(ref: $refname):\n",
Eric Wong4bb9ed02007-02-03 13:29:17 -08001705 "expected path: $pathname\n",
1706 " real path: $u\n",
1707 "Continuing ahead with $u\n";
1708 next;
1709 }
1710 } else {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001711 $fetch->{$pathname} = $refname;
1712 }
1713 }
1714}
1715
Eric Wonge98671e2007-02-14 02:21:19 -08001716sub parse_revision_argument {
1717 my ($base, $head) = @_;
1718 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
1719 return ($base, $head);
1720 }
1721 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
1722 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
1723 return ($head, $head) if ($::_revision eq 'HEAD');
1724 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
1725 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
1726 die "revision argument: $::_revision not understood by git-svn\n";
1727}
1728
Eric Wong0af9c9f2007-01-27 22:28:56 -08001729sub fetch_all {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001730 my ($repo_id, $remotes) = @_;
Eric Wong905f8b72007-02-16 03:22:40 -08001731 if (ref $repo_id) {
1732 my $gs = $repo_id;
1733 $repo_id = undef;
1734 $repo_id = $gs->{repo_id};
1735 }
1736 $remotes ||= read_all_remotes();
Eric Wong7447b4b2007-02-14 18:38:46 -08001737 my $remote = $remotes->{$repo_id} or
1738 die "[svn-remote \"$repo_id\"] unknown\n";
Eric Wonge5181922007-02-08 12:53:57 -08001739 my $fetch = $remote->{fetch};
Eric Wong7447b4b2007-02-14 18:38:46 -08001740 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
Eric Wonge5181922007-02-08 12:53:57 -08001741 my (@gs, @globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001742 my $ra = Git::SVN::Ra->new($url);
Eric Wong26a62d52007-02-12 13:25:25 -08001743 my $uuid = $ra->get_uuid;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001744 my $head = $ra->get_latest_revnum;
Eric Wong577e9fc2009-12-21 02:06:04 -08001745
1746 # ignore errors, $head revision may not even exist anymore
1747 eval { $ra->get_log("", $head, 0, 1, 0, 1, sub { $head = $_[1] }) };
1748 warn "W: $@\n" if $@;
1749
Eric Wong28710f72007-02-14 13:32:21 -08001750 my $base = defined $fetch ? $head : 0;
Eric Wonge5181922007-02-08 12:53:57 -08001751
1752 # read the max revs for wildcard expansion (branches/*, tags/*)
1753 foreach my $t (qw/branches tags/) {
1754 defined $remote->{$t} or next;
Marc Branchaud62244062009-06-23 13:02:08 -04001755 push @globs, @{$remote->{$t}};
1756
Eric Wong93f26892007-02-11 01:20:26 -08001757 my $max_rev = eval { tmp_config(qw/--int --get/,
1758 "svn-remote.$repo_id.${t}-maxRev") };
1759 if (defined $max_rev && ($max_rev < $base)) {
1760 $base = $max_rev;
Eric Wongd6d33462007-02-16 04:05:33 -08001761 } elsif (!defined $max_rev) {
1762 $base = 0;
Eric Wonge5181922007-02-08 12:53:57 -08001763 }
1764 }
1765
Eric Wongdb03cd22007-02-13 00:38:02 -08001766 if ($fetch) {
1767 foreach my $p (sort keys %$fetch) {
1768 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
Eric Wong060610c2007-12-08 23:27:41 -08001769 my $lr = $gs->rev_map_max;
Eric Wongdb03cd22007-02-13 00:38:02 -08001770 if (defined $lr) {
1771 $base = $lr if ($lr < $base);
1772 }
1773 push @gs, $gs;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001774 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08001775 }
Eric Wonge98671e2007-02-14 02:21:19 -08001776
1777 ($base, $head) = parse_revision_argument($base, $head);
Eric Wonge5181922007-02-08 12:53:57 -08001778 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001779}
1780
Eric Wong47e39c52007-01-21 04:27:09 -08001781sub read_all_remotes {
1782 my $r = {};
João Abecasis63c56022008-07-14 16:28:04 +01001783 my $use_svm_props = eval { command_oneline(qw/config --bool
1784 svn.useSvmProps/) };
1785 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
Eric Wongffd5c8e2009-10-22 23:39:04 -07001786 my $svn_refspec = qr{\s*(.*?)\s*:\s*(.+?)\s*};
Eric Wong8b8fc062007-01-22 11:44:57 -08001787 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
Adam Brewster6f5748e2009-08-11 23:14:27 -04001788 if (m!^(.+)\.fetch=$svn_refspec$!) {
1789 my ($remote, $local_ref, $remote_ref) = ($1, $2, $3);
1790 die("svn-remote.$remote: remote ref '$remote_ref' "
1791 . "must start with 'refs/'\n")
1792 unless $remote_ref =~ m{^refs/};
Eric Wong46cf98b2007-07-14 12:40:32 -07001793 $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
João Abecasis63c56022008-07-14 16:28:04 +01001794 $r->{$remote}->{svm} = {} if $use_svm_props;
1795 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
1796 $r->{$1}->{svm} = {};
Eric Wong47e39c52007-01-21 04:27:09 -08001797 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1798 $r->{$1}->{url} = $2;
Adam Brewster6f5748e2009-08-11 23:14:27 -04001799 } elsif (m!^(.+)\.(branches|tags)=$svn_refspec$!) {
1800 my ($remote, $t, $local_ref, $remote_ref) =
1801 ($1, $2, $3, $4);
1802 die("svn-remote.$remote: remote ref '$remote_ref' ($t) "
1803 . "must start with 'refs/'\n")
1804 unless $remote_ref =~ m{^refs/};
Marc Branchaud62244062009-06-23 13:02:08 -04001805 my $rs = {
Adam Brewster6f5748e2009-08-11 23:14:27 -04001806 t => $t,
1807 remote => $remote,
1808 path => Git::SVN::GlobSpec->new($local_ref),
1809 ref => Git::SVN::GlobSpec->new($remote_ref) };
Eric Wong4bb9ed02007-02-03 13:29:17 -08001810 if (length($rs->{ref}->{right}) != 0) {
1811 die "The '*' glob character must be the last ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001812 "character of '$remote_ref'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08001813 }
Adam Brewster6f5748e2009-08-11 23:14:27 -04001814 push @{ $r->{$remote}->{$t} }, $rs;
Eric Wong47e39c52007-01-21 04:27:09 -08001815 }
1816 }
João Abecasis63c56022008-07-14 16:28:04 +01001817
1818 map {
1819 if (defined $r->{$_}->{svm}) {
1820 my $svm;
1821 eval {
1822 my $section = "svn-remote.$_";
1823 $svm = {
1824 source => tmp_config('--get',
1825 "$section.svm-source"),
1826 replace => tmp_config('--get',
1827 "$section.svm-replace"),
1828 }
1829 };
1830 $r->{$_}->{svm} = $svm;
1831 }
1832 } keys %$r;
1833
Eric Wong47e39c52007-01-21 04:27:09 -08001834 $r;
1835}
1836
Eric Wongecc712d2007-01-31 12:28:10 -08001837sub init_vars {
Karl Hasselström94bc9142008-02-03 17:56:18 +01001838 $_gc_nr = $_gc_period = 1000;
Karl Hasselströmaf788a62008-02-03 17:56:12 +01001839 if (defined $_repack || defined $_repack_flags) {
1840 warn "Repack options are obsolete; they have no effect.\n";
1841 }
Eric Wongecc712d2007-01-31 12:28:10 -08001842}
1843
Eric Wongb805b442007-01-22 13:52:04 -08001844sub verify_remotes_sanity {
Eric Wong536c4b02007-01-23 11:35:53 -08001845 return unless -d $ENV{GIT_DIR};
Eric Wongb805b442007-01-22 13:52:04 -08001846 my %seen;
1847 foreach (command(qw/config -l/)) {
1848 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1849 if ($seen{$1}) {
1850 die "Remote ref refs/remote/$1 is tracked by",
1851 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1852 "Please resolve this ambiguity in ",
1853 "your git configuration file before ",
1854 "continuing\n";
1855 }
1856 $seen{$1} = $_;
1857 }
1858 }
1859}
1860
Eric Wonge6434f82007-01-23 16:29:23 -08001861sub find_existing_remote {
1862 my ($url, $remotes) = @_;
Eric Wongbefc9ad2007-02-17 02:53:07 -08001863 return undef if $no_reuse_existing;
Eric Wonge6434f82007-01-23 16:29:23 -08001864 my $existing;
1865 foreach my $repo_id (keys %$remotes) {
1866 my $u = $remotes->{$repo_id}->{url} or next;
1867 next if $u ne $url;
1868 $existing = $repo_id;
1869 last;
1870 }
1871 $existing;
1872}
1873
1874sub init_remote_config {
Eric Wongd8115c52007-02-01 03:30:31 -08001875 my ($self, $url, $no_write) = @_;
Eric Wonge6434f82007-01-23 16:29:23 -08001876 $url =~ s!/+$!!; # strip trailing slash
1877 my $r = read_all_remotes();
1878 my $existing = find_existing_remote($url, $r);
1879 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001880 unless ($no_write) {
1881 print STDERR "Using existing ",
1882 "[svn-remote \"$existing\"]\n";
1883 }
Eric Wonge6434f82007-01-23 16:29:23 -08001884 $self->{repo_id} = $existing;
Eric Wong4a1bb4c2007-05-13 09:58:14 -07001885 } elsif ($_minimize_url) {
Eric Wonge6434f82007-01-23 16:29:23 -08001886 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
1887 $existing = find_existing_remote($min_url, $r);
1888 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001889 unless ($no_write) {
1890 print STDERR "Using existing ",
1891 "[svn-remote \"$existing\"]\n";
1892 }
Eric Wonge6434f82007-01-23 16:29:23 -08001893 $self->{repo_id} = $existing;
1894 }
1895 if ($min_url ne $url) {
Eric Wonge5181922007-02-08 12:53:57 -08001896 unless ($no_write) {
1897 print STDERR "Using higher level of URL: ",
1898 "$url => $min_url\n";
1899 }
Eric Wonge6434f82007-01-23 16:29:23 -08001900 my $old_path = $self->{path};
1901 $self->{path} = $url;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001902 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
Eric Wonge6434f82007-01-23 16:29:23 -08001903 if (length $old_path) {
1904 $self->{path} .= "/$old_path";
1905 }
1906 $url = $min_url;
1907 }
1908 }
1909 my $orig_url;
1910 if (!$existing) {
1911 # verify that we aren't overwriting anything:
1912 $orig_url = eval {
1913 command_oneline('config', '--get',
1914 "svn-remote.$self->{repo_id}.url")
1915 };
1916 if ($orig_url && ($orig_url ne $url)) {
1917 die "svn-remote.$self->{repo_id}.url already set: ",
1918 "$orig_url\nwanted to set to: $url\n";
1919 }
1920 }
1921 my ($xrepo_id, $xpath) = find_ref($self->refname);
Adam Brewster6f5748e2009-08-11 23:14:27 -04001922 if (!$no_write && defined $xpath) {
Eric Wonge6434f82007-01-23 16:29:23 -08001923 die "svn-remote.$xrepo_id.fetch already set to track ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001924 "$xpath:", $self->refname, "\n";
Eric Wonge6434f82007-01-23 16:29:23 -08001925 }
Eric Wongd8115c52007-02-01 03:30:31 -08001926 unless ($no_write) {
1927 command_noisy('config',
1928 "svn-remote.$self->{repo_id}.url", $url);
Eric Wong46cf98b2007-07-14 12:40:32 -07001929 $self->{path} =~ s{^/}{};
Eric Wong5268f9e2009-08-16 14:22:12 -07001930 $self->{path} =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
Eric Wongd8115c52007-02-01 03:30:31 -08001931 command_noisy('config', '--add',
1932 "svn-remote.$self->{repo_id}.fetch",
1933 "$self->{path}:".$self->refname);
1934 }
Eric Wonge6434f82007-01-23 16:29:23 -08001935 $self->{url} = $url;
1936}
1937
Eric Wonga8ae2622007-02-13 14:22:11 -08001938sub find_by_url { # repos_root and, path are optional
1939 my ($class, $full_url, $repos_root, $path) = @_;
Adam Roben56973d22007-04-25 12:42:58 -07001940
Eric Wong1a97a502007-02-20 00:43:19 -08001941 return undef unless defined $full_url;
Adam Roben56973d22007-04-25 12:42:58 -07001942 remove_username($full_url);
1943 remove_username($repos_root) if defined $repos_root;
Eric Wonga8ae2622007-02-13 14:22:11 -08001944 my $remotes = read_all_remotes();
1945 if (defined $full_url && defined $repos_root && !defined $path) {
1946 $path = $full_url;
1947 $path =~ s#^\Q$repos_root\E(?:/|$)##;
1948 }
1949 foreach my $repo_id (keys %$remotes) {
1950 my $u = $remotes->{$repo_id}->{url} or next;
Adam Roben56973d22007-04-25 12:42:58 -07001951 remove_username($u);
Eric Wonga8ae2622007-02-13 14:22:11 -08001952 next if defined $repos_root && $repos_root ne $u;
1953
1954 my $fetch = $remotes->{$repo_id}->{fetch} || {};
Marc Branchaud62244062009-06-23 13:02:08 -04001955 foreach my $t (qw/branches tags/) {
1956 foreach my $globspec (@{$remotes->{$repo_id}->{$t}}) {
1957 resolve_local_globs($u, $fetch, $globspec);
1958 }
Eric Wonga8ae2622007-02-13 14:22:11 -08001959 }
1960 my $p = $path;
John Goerzen0bb91d92008-03-08 16:04:05 -06001961 my $rwr = rewrite_root({repo_id => $repo_id});
João Abecasis63c56022008-07-14 16:28:04 +01001962 my $svm = $remotes->{$repo_id}->{svm}
1963 if defined $remotes->{$repo_id}->{svm};
Eric Wonga8ae2622007-02-13 14:22:11 -08001964 unless (defined $p) {
1965 $p = $full_url;
John Goerzen0bb91d92008-03-08 16:04:05 -06001966 my $z = $u;
João Abecasis63c56022008-07-14 16:28:04 +01001967 my $prefix = '';
John Goerzen0bb91d92008-03-08 16:04:05 -06001968 if ($rwr) {
1969 $z = $rwr;
Dévai Tamás1b7e5432009-02-12 00:14:02 +01001970 remove_username($z);
João Abecasis63c56022008-07-14 16:28:04 +01001971 } elsif (defined $svm) {
1972 $z = $svm->{source};
1973 $prefix = $svm->{replace};
1974 $prefix =~ s#^\Q$u\E(?:/|$)##;
1975 $prefix =~ s#/$##;
John Goerzen0bb91d92008-03-08 16:04:05 -06001976 }
João Abecasis63c56022008-07-14 16:28:04 +01001977 $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next;
Eric Wonga8ae2622007-02-13 14:22:11 -08001978 }
1979 foreach my $f (keys %$fetch) {
1980 next if $f ne $p;
1981 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
1982 }
1983 }
1984 undef;
1985}
1986
Eric Wong9b981fc2007-01-11 12:14:21 -08001987sub init {
Eric Wongd8115c52007-02-01 03:30:31 -08001988 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08001989 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong9b981fc2007-01-11 12:14:21 -08001990 if (defined $url) {
Eric Wongd8115c52007-02-01 03:30:31 -08001991 $self->init_remote_config($url, $no_write);
Eric Wong9b981fc2007-01-11 12:14:21 -08001992 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001993 $self;
1994}
1995
Eric Wong706587f2007-01-18 17:50:01 -08001996sub find_ref {
1997 my ($ref_id) = @_;
1998 foreach (command(qw/config -l/)) {
1999 next unless m!^svn-remote\.(.+)\.fetch=
Eric Wongffd5c8e2009-10-22 23:39:04 -07002000 \s*(.*?)\s*:\s*(.+?)\s*$!x;
Eric Wong706587f2007-01-18 17:50:01 -08002001 my ($repo_id, $path, $ref) = ($1, $2, $3);
2002 if ($ref eq $ref_id) {
2003 $path = '' if ($path =~ m#^\./?#);
2004 return ($repo_id, $path);
2005 }
2006 }
2007 (undef, undef, undef);
2008}
2009
Eric Wong9b981fc2007-01-11 12:14:21 -08002010sub new {
Eric Wong706587f2007-01-18 17:50:01 -08002011 my ($class, $ref_id, $repo_id, $path) = @_;
2012 if (defined $ref_id && !defined $repo_id && !defined $path) {
2013 ($repo_id, $path) = find_ref($ref_id);
2014 if (!defined $repo_id) {
2015 die "Could not find a \"svn-remote.*.fetch\" key ",
2016 "in the repository configuration matching: ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04002017 "$ref_id\n";
Eric Wong706587f2007-01-18 17:50:01 -08002018 }
2019 }
2020 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong8b8fc062007-01-22 11:44:57 -08002021 if (!defined $self->{path} || !length $self->{path}) {
2022 my $fetch = command_oneline('config', '--get',
2023 "svn-remote.$repo_id.fetch",
Adam Brewster6f5748e2009-08-11 23:14:27 -04002024 ":$ref_id\$") or
Eric Wong8b8fc062007-01-22 11:44:57 -08002025 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04002026 "\":$ref_id\$\" in config\n";
Eric Wong8b8fc062007-01-22 11:44:57 -08002027 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
2028 }
Eric Wong706587f2007-01-18 17:50:01 -08002029 $self->{url} = command_oneline('config', '--get',
2030 "svn-remote.$repo_id.url") or
2031 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
Eric Wongd6d33462007-02-16 04:05:33 -08002032 $self->rebuild;
Eric Wong9b981fc2007-01-11 12:14:21 -08002033 $self;
2034}
2035
Robert Ewaldbf655fd2007-07-30 11:08:21 +02002036sub refname {
Adam Brewster6f5748e2009-08-11 23:14:27 -04002037 my ($refname) = $_[0]->{ref_id} ;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02002038
2039 # It cannot end with a slash /, we'll throw up on this because
2040 # SVN can't have directories with a slash in their name, either:
2041 if ($refname =~ m{/$}) {
2042 die "ref: '$refname' ends with a trailing slash, this is ",
2043 "not permitted by git nor Subversion\n";
2044 }
2045
2046 # It cannot have ASCII control character space, tilde ~, caret ^,
2047 # colon :, question-mark ?, asterisk *, space, or open bracket [
2048 # anywhere.
2049 #
2050 # Additionally, % must be escaped because it is used for escaping
2051 # and we want our escaped refname to be reversible
2052 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
2053
2054 # no slash-separated component can begin with a dot .
2055 # /.* becomes /%2E*
2056 $refname =~ s{/\.}{/%2E}g;
2057
2058 # It cannot have two consecutive dots .. anywhere
2059 # .. becomes %2E%2E
2060 $refname =~ s{\.\.}{%2E%2E}g;
2061
2062 return $refname;
2063}
2064
2065sub desanitize_refname {
2066 my ($refname) = @_;
2067 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
2068 return $refname;
2069}
Eric Wong9b981fc2007-01-11 12:14:21 -08002070
Eric Wong26a62d52007-02-12 13:25:25 -08002071sub svm_uuid {
2072 my ($self) = @_;
2073 return $self->{svm}->{uuid} if $self->svm;
2074 $self->ra;
2075 unless ($self->{svm}) {
2076 die "SVM UUID not cached, and reading remotely failed\n";
2077 }
2078 $self->{svm}->{uuid};
2079}
Eric Wong8a49ee92007-02-10 20:46:50 -08002080
Eric Wong26a62d52007-02-12 13:25:25 -08002081sub svm {
2082 my ($self) = @_;
2083 return $self->{svm} if $self->{svm};
2084 my $svm;
Eric Wong8a49ee92007-02-10 20:46:50 -08002085 # see if we have it in our config, first:
2086 eval {
Eric Wong26a62d52007-02-12 13:25:25 -08002087 my $section = "svn-remote.$self->{repo_id}";
2088 $svm = {
Eric Wong93f26892007-02-11 01:20:26 -08002089 source => tmp_config('--get', "$section.svm-source"),
2090 uuid => tmp_config('--get', "$section.svm-uuid"),
Eric Wongbefc9ad2007-02-17 02:53:07 -08002091 replace => tmp_config('--get', "$section.svm-replace"),
Eric Wong8a49ee92007-02-10 20:46:50 -08002092 }
2093 };
Eric Wongbefc9ad2007-02-17 02:53:07 -08002094 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
2095 $self->{svm} = $svm;
2096 }
Eric Wong26a62d52007-02-12 13:25:25 -08002097 $self->{svm};
2098}
2099
2100sub _set_svm_vars {
2101 my ($self, $ra) = @_;
Eric Wongdb03cd22007-02-13 00:38:02 -08002102 return $ra if $self->svm;
Eric Wong26a62d52007-02-12 13:25:25 -08002103
Eric Wongdb03cd22007-02-13 00:38:02 -08002104 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08002105 "(svm:source, svm:uuid) ",
Eric Wongdb03cd22007-02-13 00:38:02 -08002106 "from the following URLs:\n" );
2107 sub read_svm_props {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002108 my ($self, $ra, $path, $r) = @_;
2109 my $props = ($ra->get_dir($path, $r))[2];
Eric Wongdb03cd22007-02-13 00:38:02 -08002110 my $src = $props->{'svm:source'};
Eric Wong8a49ee92007-02-10 20:46:50 -08002111 my $uuid = $props->{'svm:uuid'};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002112 return undef if (!$src || !$uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08002113
Eric Wongbefc9ad2007-02-17 02:53:07 -08002114 chomp($src, $uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08002115
Eric Wongb3e95932009-07-11 14:13:12 -07002116 $uuid =~ m{^[0-9a-f\-]{30,}$}i
Eric Wong8a49ee92007-02-10 20:46:50 -08002117 or die "doesn't look right - svm:uuid is '$uuid'\n";
Eric Wongbefc9ad2007-02-17 02:53:07 -08002118
2119 # the '!' is used to mark the repos_root!/relative/path
2120 $src =~ s{/?!/?}{/};
Eric Wongdb03cd22007-02-13 00:38:02 -08002121 $src =~ s{/+$}{}; # no trailing slashes please
Eric Wongbefc9ad2007-02-17 02:53:07 -08002122 # username is of no interest
Eric Wongdb03cd22007-02-13 00:38:02 -08002123 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
Eric Wong8a49ee92007-02-10 20:46:50 -08002124
Eric Wongbefc9ad2007-02-17 02:53:07 -08002125 my $replace = $ra->{url};
2126 $replace .= "/$path" if length $path;
2127
Eric Wongdb03cd22007-02-13 00:38:02 -08002128 my $section = "svn-remote.$self->{repo_id}";
Eric Wongbefc9ad2007-02-17 02:53:07 -08002129 tmp_config("$section.svm-source", $src);
2130 tmp_config("$section.svm-replace", $replace);
2131 tmp_config("$section.svm-uuid", $uuid);
2132 $self->{svm} = {
2133 source => $src,
2134 uuid => $uuid,
2135 replace => $replace
2136 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002137 }
Eric Wongdb03cd22007-02-13 00:38:02 -08002138
2139 my $r = $ra->get_latest_revnum;
2140 my $path = $self->{path};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002141 my %tried;
Eric Wongdb03cd22007-02-13 00:38:02 -08002142 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002143 unless ($tried{"$self->{url}/$path"}) {
2144 return $ra if $self->read_svm_props($ra, $path, $r);
2145 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002146 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002147 $path =~ s#/?[^/]+$##;
Eric Wong8a49ee92007-02-10 20:46:50 -08002148 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002149 die "Path: '$path' should be ''\n" if $path ne '';
2150 return $ra if $self->read_svm_props($ra, $path, $r);
2151 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002152
2153 if ($ra->{repos_root} eq $self->{url}) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002154 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002155 }
2156
2157 # nope, make sure we're connected to the repository root:
2158 my $ok;
2159 my @tried_b;
2160 $path = $ra->{svn_path};
Eric Wongdb03cd22007-02-13 00:38:02 -08002161 $ra = Git::SVN::Ra->new($ra->{repos_root});
2162 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002163 unless ($tried{"$ra->{url}/$path"}) {
2164 $ok = $self->read_svm_props($ra, $path, $r);
2165 last if $ok;
2166 $tried{"$ra->{url}/$path"} = 1;
2167 }
2168 $path =~ s#/?[^/]+$##;
Eric Wongdb03cd22007-02-13 00:38:02 -08002169 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002170 die "Path: '$path' should be ''\n" if $path ne '';
2171 $ok ||= $self->read_svm_props($ra, $path, $r);
2172 $tried{"$ra->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002173 if (!$ok) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002174 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002175 }
2176 Git::SVN::Ra->new($self->{url});
Eric Wong8a49ee92007-02-10 20:46:50 -08002177}
2178
Eric Wong62e349d2007-02-16 19:57:29 -08002179sub svnsync {
2180 my ($self) = @_;
2181 return $self->{svnsync} if $self->{svnsync};
2182
2183 if ($self->no_metadata) {
2184 die "Can't have both 'noMetadata' and ",
2185 "'useSvnsyncProps' options set!\n";
2186 }
2187 if ($self->rewrite_root) {
2188 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
2189 "options set!\n";
2190 }
2191
2192 my $svnsync;
2193 # see if we have it in our config, first:
2194 eval {
2195 my $section = "svn-remote.$self->{repo_id}";
Eric Wong98fa5b62008-01-11 23:13:55 -08002196
2197 my $url = tmp_config('--get', "$section.svnsync-url");
2198 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
2199 die "doesn't look right - svn:sync-from-url is '$url'\n";
2200
2201 my $uuid = tmp_config('--get', "$section.svnsync-uuid");
Eric Wongb3e95932009-07-11 14:13:12 -07002202 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
Eric Wong98fa5b62008-01-11 23:13:55 -08002203 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2204
2205 $svnsync = { url => $url, uuid => $uuid }
Eric Wong62e349d2007-02-16 19:57:29 -08002206 };
2207 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
2208 return $self->{svnsync} = $svnsync;
2209 }
2210
2211 my $err = "useSvnsyncProps set, but failed to read " .
2212 "svnsync property: svn:sync-from-";
2213 my $rp = $self->ra->rev_proplist(0);
2214
2215 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
Eric Wong98fa5b62008-01-11 23:13:55 -08002216 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
Eric Wong62e349d2007-02-16 19:57:29 -08002217 die "doesn't look right - svn:sync-from-url is '$url'\n";
2218
2219 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
Eric Wongb3e95932009-07-11 14:13:12 -07002220 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
Eric Wong62e349d2007-02-16 19:57:29 -08002221 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2222
2223 my $section = "svn-remote.$self->{repo_id}";
2224 tmp_config('--add', "$section.svnsync-uuid", $uuid);
2225 tmp_config('--add', "$section.svnsync-url", $url);
2226 return $self->{svnsync} = { url => $url, uuid => $uuid };
2227}
2228
Eric Wong26a62d52007-02-12 13:25:25 -08002229# this allows us to memoize our SVN::Ra UUID locally and avoid a
2230# remote lookup (useful for 'git svn log').
2231sub ra_uuid {
2232 my ($self) = @_;
2233 unless ($self->{ra_uuid}) {
2234 my $key = "svn-remote.$self->{repo_id}.uuid";
2235 my $uuid = eval { tmp_config('--get', $key) };
Eric Wongb3e95932009-07-11 14:13:12 -07002236 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/i) {
Eric Wong26a62d52007-02-12 13:25:25 -08002237 $self->{ra_uuid} = $uuid;
2238 } else {
2239 die "ra_uuid called without URL\n" unless $self->{url};
2240 $self->{ra_uuid} = $self->ra->get_uuid;
2241 tmp_config('--add', $key, $self->{ra_uuid});
2242 }
2243 }
2244 $self->{ra_uuid};
2245}
2246
Eric Wonga5460eb2007-11-21 18:20:57 -08002247sub _set_repos_root {
2248 my ($self, $repos_root) = @_;
2249 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2250 $repos_root ||= $self->ra->{repos_root};
2251 tmp_config($k, $repos_root);
2252 $repos_root;
2253}
2254
2255sub repos_root {
2256 my ($self) = @_;
2257 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2258 eval { tmp_config('--get', $k) } || $self->_set_repos_root;
2259}
2260
Eric Wong9b981fc2007-01-11 12:14:21 -08002261sub ra {
2262 my ($self) = shift;
Eric Wong8a49ee92007-02-10 20:46:50 -08002263 my $ra = Git::SVN::Ra->new($self->{url});
Eric Wonga5460eb2007-11-21 18:20:57 -08002264 $self->_set_repos_root($ra->{repos_root});
Eric Wong91b03282007-02-11 00:51:33 -08002265 if ($self->use_svm_props && !$self->{svm}) {
2266 if ($self->no_metadata) {
Eric Wong97ae0912007-02-11 15:21:24 -08002267 die "Can't have both 'noMetadata' and ",
2268 "'useSvmProps' options set!\n";
Eric Wong62e349d2007-02-16 19:57:29 -08002269 } elsif ($self->use_svnsync_props) {
2270 die "Can't have both 'useSvnsyncProps' and ",
2271 "'useSvmProps' options set!\n";
Eric Wong91b03282007-02-11 00:51:33 -08002272 }
Eric Wong26a62d52007-02-12 13:25:25 -08002273 $ra = $self->_set_svm_vars($ra);
Eric Wong8a49ee92007-02-10 20:46:50 -08002274 $self->{-want_revprops} = 1;
2275 }
2276 $ra;
Eric Wong9b981fc2007-01-11 12:14:21 -08002277}
2278
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002279# prop_walk(PATH, REV, SUB)
2280# -------------------------
2281# Recursively traverse PATH at revision REV and invoke SUB for each
2282# directory that contains a SVN property. SUB will be invoked as
2283# follows: &SUB(gs, path, props); where `gs' is this instance of
2284# Git::SVN, `path' the path to the directory where the properties
2285# `props' were found. The `path' will be relative to point of checkout,
2286# that is, if url://repo/trunk is the current Git branch, and that
2287# directory contains a sub-directory `d', SUB will be invoked with `/d/'
2288# as `path' (note the trailing `/').
2289sub prop_walk {
2290 my ($self, $path, $rev, $sub) = @_;
2291
Kevin Ballard35cda062008-01-09 01:37:20 -05002292 $path =~ s#^/##;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002293 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
2294 $path =~ s#^/*#/#g;
Eric Wong9b981fc2007-01-11 12:14:21 -08002295 my $p = $path;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002296 # Strip the irrelevant part of the path.
2297 $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
2298 # Ensure the path is terminated by a `/'.
2299 $p =~ s#/*$#/#;
2300
2301 # The properties contain all the internal SVN stuff nobody
2302 # (usually) cares about.
2303 my $interesting_props = 0;
2304 foreach (keys %{$props}) {
2305 # If it doesn't start with `svn:', it must be a
2306 # user-defined property.
2307 ++$interesting_props and next if $_ !~ /^svn:/;
2308 # FIXME: Fragile, if SVN adds new public properties,
2309 # this needs to be updated.
2310 ++$interesting_props if /^svn:(?:ignore|keywords|executable
2311 |eol-style|mime-type
2312 |externals|needs-lock)$/x;
Eric Wong9b981fc2007-01-11 12:14:21 -08002313 }
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002314 &$sub($self, $p, $props) if $interesting_props;
2315
Eric Wong9b981fc2007-01-11 12:14:21 -08002316 foreach (sort keys %$dirent) {
Eric Wong0dc03d62007-05-13 01:04:43 -07002317 next if $dirent->{$_}->{kind} != $SVN::Node::dir;
Christian Engwerb7166cc2008-05-27 08:46:55 +00002318 $self->prop_walk($self->{path} . $p . $_, $rev, $sub);
Eric Wong9b981fc2007-01-11 12:14:21 -08002319 }
2320}
2321
Eric Wong3ebe8df2007-01-25 17:35:40 -08002322sub last_rev { ($_[0]->last_rev_commit)[0] }
2323sub last_commit { ($_[0]->last_rev_commit)[1] }
2324
Eric Wong9b981fc2007-01-11 12:14:21 -08002325# returns the newest SVN revision number and newest commit SHA1
2326sub last_rev_commit {
2327 my ($self) = @_;
2328 if (defined $self->{last_rev} && defined $self->{last_commit}) {
2329 return ($self->{last_rev}, $self->{last_commit});
2330 }
Eric Wongd2866f92007-01-11 12:26:16 -08002331 my $c = ::verify_ref($self->refname.'^0');
Eric Wong91b03282007-02-11 00:51:33 -08002332 if ($c && !$self->use_svm_props && !$self->no_metadata) {
Eric Wongd2866f92007-01-11 12:26:16 -08002333 my $rev = (::cmt_metadata($c))[1];
Eric Wong9b981fc2007-01-11 12:14:21 -08002334 if (defined $rev) {
2335 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2336 return ($rev, $c);
2337 }
2338 }
Eric Wong060610c2007-12-08 23:27:41 -08002339 my $map_path = $self->map_path;
2340 unless (-e $map_path) {
Eric Wong26a62d52007-02-12 13:25:25 -08002341 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
2342 return (undef, undef);
2343 }
Eric Wong66ab84b2007-12-08 23:27:42 -08002344 my ($rev, $commit) = $self->rev_map_max(1);
Eric Wong060610c2007-12-08 23:27:41 -08002345 ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
2346 return ($rev, $commit);
Eric Wong9b981fc2007-01-11 12:14:21 -08002347}
2348
Eric Wong3ebe8df2007-01-25 17:35:40 -08002349sub get_fetch_range {
2350 my ($self, $min, $max) = @_;
2351 $max ||= $self->ra->get_latest_revnum;
Eric Wong060610c2007-12-08 23:27:41 -08002352 $min ||= $self->rev_map_max;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002353 (++$min, $max);
Eric Wong9b981fc2007-01-11 12:14:21 -08002354}
2355
Eric Wong8a49ee92007-02-10 20:46:50 -08002356sub tmp_config {
Eric Wong93f26892007-02-11 01:20:26 -08002357 my (@args) = @_;
Eric Wongb7e53482007-02-16 04:09:28 -08002358 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
2359 my $config = "$ENV{GIT_DIR}/svn/.metadata";
Eric Wong38570a42007-06-13 02:37:05 -07002360 if (! -f $config && -f $old_def_config) {
Eric Wongb7e53482007-02-16 04:09:28 -08002361 rename $old_def_config, $config or
2362 die "Failed rename $old_def_config => $config: $!\n";
2363 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002364 my $old_config = $ENV{GIT_CONFIG};
Eric Wong93f26892007-02-11 01:20:26 -08002365 $ENV{GIT_CONFIG} = $config;
Eric Wong8a49ee92007-02-10 20:46:50 -08002366 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002367 my @ret = eval {
2368 unless (-f $config) {
2369 mkfile($config);
2370 open my $fh, '>', $config or
2371 die "Can't open $config: $!\n";
2372 print $fh "; This file is used internally by ",
2373 "git-svn\n" or die
2374 "Couldn't write to $config: $!\n";
2375 print $fh "; You should not have to edit it\n" or
2376 die "Couldn't write to $config: $!\n";
2377 close $fh or die "Couldn't close $config: $!\n";
2378 }
2379 command('config', @args);
2380 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002381 my $err = $@;
2382 if (defined $old_config) {
2383 $ENV{GIT_CONFIG} = $old_config;
2384 } else {
2385 delete $ENV{GIT_CONFIG};
2386 }
2387 die $err if $err;
2388 wantarray ? @ret : $ret[0];
2389}
2390
Eric Wong9b981fc2007-01-11 12:14:21 -08002391sub tmp_index_do {
2392 my ($self, $sub) = @_;
2393 my $old_index = $ENV{GIT_INDEX_FILE};
2394 $ENV{GIT_INDEX_FILE} = $self->{index};
Eric Wong8a49ee92007-02-10 20:46:50 -08002395 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002396 my @ret = eval {
2397 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
2398 mkpath([$dir]) unless -d $dir;
2399 &$sub;
2400 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002401 my $err = $@;
2402 if (defined $old_index) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002403 $ENV{GIT_INDEX_FILE} = $old_index;
2404 } else {
2405 delete $ENV{GIT_INDEX_FILE};
2406 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002407 die $err if $err;
Eric Wong9b981fc2007-01-11 12:14:21 -08002408 wantarray ? @ret : $ret[0];
2409}
2410
2411sub assert_index_clean {
2412 my ($self, $treeish) = @_;
2413
2414 $self->tmp_index_do(sub {
2415 command_noisy('read-tree', $treeish) unless -e $self->{index};
2416 my $x = command_oneline('write-tree');
2417 my ($y) = (command(qw/cat-file commit/, $treeish) =~
2418 /^tree ($::sha1)/mo);
Eric Wonge8d120b2007-02-14 16:29:52 -08002419 return if $y eq $x;
2420
2421 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2422 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2423 command_noisy('read-tree', $treeish);
Eric Wong9b981fc2007-01-11 12:14:21 -08002424 $x = command_oneline('write-tree');
2425 if ($y ne $x) {
2426 ::fatal "trees ($treeish) $y != $x\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02002427 "Something is seriously wrong...";
Eric Wong9b981fc2007-01-11 12:14:21 -08002428 }
2429 });
2430}
2431
2432sub get_commit_parents {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002433 my ($self, $log_entry) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002434 my (%seen, @ret, @tmp);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002435 # legacy support for 'set-tree'; this is only used by set_tree_cb:
2436 if (my $ip = $self->{inject_parents}) {
2437 if (my $commit = delete $ip->{$log_entry->{revision}}) {
2438 push @tmp, $commit;
Eric Wong9b981fc2007-01-11 12:14:21 -08002439 }
2440 }
Eric Wongd2866f92007-01-11 12:26:16 -08002441 if (my $cur = ::verify_ref($self->refname.'^0')) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002442 push @tmp, $cur;
2443 }
Eric Wong733a65a2007-06-13 02:23:28 -07002444 if (my $ipd = $self->{inject_parents_dcommit}) {
2445 if (my $commit = delete $ipd->{$log_entry->{revision}}) {
2446 push @tmp, @$commit;
2447 }
2448 }
Eric Wong44320b92007-01-13 22:35:53 -08002449 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
Eric Wong9b981fc2007-01-11 12:14:21 -08002450 while (my $p = shift @tmp) {
2451 next if $seen{$p};
2452 $seen{$p} = 1;
2453 push @ret, $p;
2454 # MAXPARENT is defined to 16 in commit-tree.c:
2455 last if @ret >= 16;
2456 }
2457 if (@tmp) {
Eric Wong44320b92007-01-13 22:35:53 -08002458 die "r$log_entry->{revision}: No room for parents:\n\t",
Eric Wong9b981fc2007-01-11 12:14:21 -08002459 join("\n\t", @tmp), "\n";
2460 }
2461 @ret;
2462}
2463
Eric Wongaea736c2007-02-16 19:15:21 -08002464sub rewrite_root {
2465 my ($self) = @_;
2466 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
2467 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2468 my $rwr = eval { command_oneline(qw/config --get/, $k) };
2469 if ($rwr) {
2470 $rwr =~ s#/+$##;
2471 if ($rwr !~ m#^[a-z\+]+://#) {
2472 die "$rwr is not a valid URL (key: $k)\n";
2473 }
2474 }
2475 $self->{-rewrite_root} = $rwr;
2476}
2477
2478sub metadata_url {
2479 my ($self) = @_;
2480 ($self->rewrite_root || $self->{url}) .
2481 (length $self->{path} ? '/' . $self->{path} : '');
2482}
2483
Eric Wong706587f2007-01-18 17:50:01 -08002484sub full_url {
Eric Wong9b981fc2007-01-11 12:14:21 -08002485 my ($self) = @_;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002486 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
Eric Wong9b981fc2007-01-11 12:14:21 -08002487}
2488
Eric Wongad948022007-12-15 19:08:22 -08002489
2490sub set_commit_header_env {
2491 my ($log_entry) = @_;
2492 my %env;
2493 foreach my $ned (qw/NAME EMAIL DATE/) {
2494 foreach my $ac (qw/AUTHOR COMMITTER/) {
2495 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2496 }
2497 }
2498
2499 $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
2500 $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
2501 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
2502
2503 $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
2504 ? $log_entry->{commit_name}
2505 : $log_entry->{name};
2506 $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
2507 ? $log_entry->{commit_email}
2508 : $log_entry->{email};
2509 \%env;
2510}
2511
2512sub restore_commit_header_env {
2513 my ($env) = @_;
2514 foreach my $ned (qw/NAME EMAIL DATE/) {
2515 foreach my $ac (qw/AUTHOR COMMITTER/) {
2516 my $k = "GIT_${ac}_${ned}";
2517 if (defined $env->{$k}) {
2518 $ENV{$k} = $env->{$k};
2519 } else {
2520 delete $ENV{$k};
2521 }
2522 }
2523 }
2524}
2525
Karl Hasselström94bc9142008-02-03 17:56:18 +01002526sub gc {
2527 command_noisy('gc', '--auto');
2528};
2529
Eric Wong9b981fc2007-01-11 12:14:21 -08002530sub do_git_commit {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002531 my ($self, $log_entry) = @_;
Eric Wong8a603772007-01-31 02:45:50 -08002532 my $lr = $self->last_rev;
2533 if (defined $lr && $lr >= $log_entry->{revision}) {
2534 die "Last fetched revision of ", $self->refname,
2535 " was r$lr, but we are about to fetch: ",
2536 "r$log_entry->{revision}!\n";
2537 }
Eric Wong060610c2007-12-08 23:27:41 -08002538 if (my $c = $self->rev_map_get($log_entry->{revision})) {
Eric Wong44320b92007-01-13 22:35:53 -08002539 croak "$log_entry->{revision} = $c already exists! ",
Eric Wong9b981fc2007-01-11 12:14:21 -08002540 "Why are we refetching it?\n";
2541 }
Eric Wongad948022007-12-15 19:08:22 -08002542 my $old_env = set_commit_header_env($log_entry);
Eric Wong44320b92007-01-13 22:35:53 -08002543 my $tree = $log_entry->{tree};
Eric Wong9b981fc2007-01-11 12:14:21 -08002544 if (!defined $tree) {
2545 $tree = $self->tmp_index_do(sub {
2546 command_oneline('write-tree') });
2547 }
2548 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2549
Deskin Millere855bfc2008-10-31 00:10:25 -04002550 my @exec = ('git', 'commit-tree', $tree);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002551 foreach ($self->get_commit_parents($log_entry)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002552 push @exec, '-p', $_;
2553 }
2554 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2555 or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07002556 binmode $msg_fh;
2557
2558 # we always get UTF-8 from SVN, but we may want our commits in
2559 # a different encoding.
2560 if (my $enc = Git::config('i18n.commitencoding')) {
2561 require Encode;
2562 Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
2563 }
Eric Wong44320b92007-01-13 22:35:53 -08002564 print $msg_fh $log_entry->{log} or croak $!;
Eric Wongad948022007-12-15 19:08:22 -08002565 restore_commit_header_env($old_env);
Eric Wong91b03282007-02-11 00:51:33 -08002566 unless ($self->no_metadata) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002567 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2568 or croak $!;
Eric Wong9760adc2007-01-31 03:06:56 -08002569 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002570 $msg_fh->flush == 0 or croak $!;
2571 close $msg_fh or croak $!;
2572 chomp(my $commit = do { local $/; <$out_fh> });
2573 close $out_fh or croak $!;
2574 waitpid $pid, 0;
2575 croak $? if $?;
2576 if ($commit !~ /^$::sha1$/o) {
2577 die "Failed to commit, invalid sha1: $commit\n";
2578 }
2579
Eric Wong060610c2007-12-08 23:27:41 -08002580 $self->rev_map_set($log_entry->{revision}, $commit, 1);
Eric Wong9b981fc2007-01-11 12:14:21 -08002581
Eric Wong44320b92007-01-13 22:35:53 -08002582 $self->{last_rev} = $log_entry->{revision};
Eric Wong9b981fc2007-01-11 12:14:21 -08002583 $self->{last_commit} = $commit;
Simon Arlott49750f32009-03-30 19:31:41 +01002584 print "r$log_entry->{revision}" unless $::_q > 1;
Eric Wong8a49ee92007-02-10 20:46:50 -08002585 if (defined $log_entry->{svm_revision}) {
Simon Arlott49750f32009-03-30 19:31:41 +01002586 print " (\@$log_entry->{svm_revision})" unless $::_q > 1;
Eric Wong060610c2007-12-08 23:27:41 -08002587 $self->rev_map_set($log_entry->{svm_revision}, $commit,
Eric Wong26a62d52007-02-12 13:25:25 -08002588 0, $self->svm_uuid);
Eric Wong8a49ee92007-02-10 20:46:50 -08002589 }
Simon Arlott49750f32009-03-30 19:31:41 +01002590 print " = $commit ($self->{ref_id})\n" unless $::_q > 1;
Karl Hasselström94bc9142008-02-03 17:56:18 +01002591 if (--$_gc_nr == 0) {
2592 $_gc_nr = $_gc_period;
2593 gc();
2594 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002595 return $commit;
2596}
2597
Eric Wongfbcc1732007-02-06 18:35:30 -08002598sub match_paths {
2599 my ($self, $paths, $r) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002600 return 1 if $self->{path} eq '';
Eric Wongd542aed2007-02-09 02:19:41 -08002601 if (my $path = $paths->{"/$self->{path}"}) {
2602 return ($path->{action} eq 'D') ? 0 : 1;
2603 }
Mattias Nissler0b2af452009-07-07 01:40:02 +02002604 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
Eric Wongfbcc1732007-02-06 18:35:30 -08002605 if (grep /$self->{path_regex}/, keys %$paths) {
2606 return 1;
2607 }
2608 my $c = '';
2609 foreach (split m#/#, $self->{path}) {
2610 $c .= "/$_";
Eric Wong74a81222007-02-10 13:28:50 -08002611 next unless ($paths->{$c} &&
2612 ($paths->{$c}->{action} =~ /^[AR]$/));
Eric Wonge5181922007-02-08 12:53:57 -08002613 if ($self->ra->check_path($self->{path}, $r) ==
2614 $SVN::Node::dir) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002615 return 1;
2616 }
2617 }
2618 return 0;
2619}
2620
Eric Wong15710b62007-01-22 02:20:33 -08002621sub find_parent_branch {
2622 my ($self, $paths, $rev) = @_;
Eric Wong91b03282007-02-11 00:51:33 -08002623 return undef unless $self->follow_parent;
Eric Wonge5a0b242007-01-25 15:44:54 -08002624 unless (defined $paths) {
Eric Wongc7eba712007-01-31 03:45:28 -08002625 my $err_handler = $SVN::Error::handler;
2626 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
Mattias Nissler3c49a032009-07-07 01:39:52 +02002627 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1,
2628 sub { $paths = $_[0] });
Eric Wongc7eba712007-01-31 03:45:28 -08002629 $SVN::Error::handler = $err_handler;
Eric Wonge5a0b242007-01-25 15:44:54 -08002630 }
2631 return undef unless defined $paths;
Eric Wong15710b62007-01-22 02:20:33 -08002632
2633 # look for a parent from another branch:
Mattias Nissler0b2af452009-07-07 01:40:02 +02002634 my @b_path_components = split m#/#, $self->{path};
Eric Wong7f578c52007-01-24 02:16:25 -08002635 my @a_path_components;
2636 my $i;
2637 while (@b_path_components) {
2638 $i = $paths->{'/'.join('/', @b_path_components)};
Eric Wong74a81222007-02-10 13:28:50 -08002639 last if $i && defined $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002640 unshift(@a_path_components, pop(@b_path_components));
2641 }
Eric Wong74a81222007-02-10 13:28:50 -08002642 return undef unless defined $i && defined $i->{copyfrom_path};
2643 my $branch_from = $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002644 if (@a_path_components) {
2645 print STDERR "branch_from: $branch_from => ";
2646 $branch_from .= '/'.join('/', @a_path_components);
2647 print STDERR $branch_from, "\n";
2648 }
Eric Wong3ebe8df2007-01-25 17:35:40 -08002649 my $r = $i->{copyfrom_rev};
Eric Wong15710b62007-01-22 02:20:33 -08002650 my $repos_root = $self->ra->{repos_root};
2651 my $url = $self->ra->{url};
Mattias Nissler0b2af452009-07-07 01:40:02 +02002652 my $new_url = $url . $branch_from;
Eric Wong15710b62007-01-22 02:20:33 -08002653 print STDERR "Found possible branch point: ",
Simon Arlott85886162009-10-09 13:21:13 +01002654 "$new_url => ", $self->full_url, ", $r\n"
2655 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002656 $branch_from =~ s#^/##;
Mattias Nissler0b2af452009-07-07 01:40:02 +02002657 my $gs = $self->other_gs($new_url, $url,
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002658 $branch_from, $r, $self->{ref_id});
Eric Wong15710b62007-01-22 02:20:33 -08002659 my ($r0, $parent) = $gs->find_rev_before($r, 1);
Deskin Miller553589f2008-12-08 08:31:31 -05002660 {
2661 my ($base, $head);
2662 if (!defined $r0 || !defined $parent) {
2663 ($base, $head) = parse_revision_argument(0, $r);
2664 } else {
2665 if ($r0 < $r) {
2666 $gs->ra->get_log([$gs->{path}], $r0 + 1, $r, 1,
2667 0, 1, sub { $base = $_[1] - 1 });
2668 }
2669 }
2670 if (defined $base && $base <= $r) {
Eric Wongd627de62007-04-15 03:01:29 -07002671 $gs->fetch($base, $r);
2672 }
Deskin Miller553589f2008-12-08 08:31:31 -05002673 ($r0, $parent) = $gs->find_rev_before($r, 1);
Eric Wong15710b62007-01-22 02:20:33 -08002674 }
Eric Wongef70de92007-02-01 04:12:41 -08002675 if (defined $r0 && defined $parent) {
Simon Arlott85886162009-10-09 13:21:13 +01002676 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n"
2677 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002678 my $ed;
2679 if ($self->ra->can_do_switch) {
Eric Wong2e5e2482007-02-23 02:21:59 -08002680 $self->assert_index_clean($parent);
Simon Arlott85886162009-10-09 13:21:13 +01002681 print STDERR "Following parent with do_switch\n"
2682 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002683 # do_switch works with svn/trunk >= r22312, but that
Eric Wong2b27f6c2007-01-28 04:59:05 -08002684 # is not included with SVN 1.4.3 (the latest version
Eric Wong15710b62007-01-22 02:20:33 -08002685 # at the moment), so we can't rely on it
Eric Wong83c2fcf2009-02-22 20:25:00 -08002686 $self->{last_rev} = $r0;
Eric Wong15710b62007-01-22 02:20:33 -08002687 $self->{last_commit} = $parent;
Eric Wong8841b372009-02-11 01:56:58 -08002688 $ed = SVN::Git::Fetcher->new($self, $gs->{path});
Eric Wong8a603772007-01-31 02:45:50 -08002689 $gs->ra->gs_do_switch($r0, $rev, $gs,
Eric Wong15710b62007-01-22 02:20:33 -08002690 $self->full_url, $ed)
2691 or die "SVN connection failed somewhere...\n";
Steven Walter9ff74e92007-09-28 13:24:19 -04002692 } elsif ($self->ra->trees_match($new_url, $r0,
2693 $self->full_url, $rev)) {
2694 print STDERR "Trees match:\n",
2695 " $new_url\@$r0\n",
2696 " ${\$self->full_url}\@$rev\n",
Simon Arlott85886162009-10-09 13:21:13 +01002697 "Following parent with no changes\n"
2698 unless $::_q > 1;
Steven Walter9ff74e92007-09-28 13:24:19 -04002699 $self->tmp_index_do(sub {
2700 command_noisy('read-tree', $parent);
2701 });
2702 $self->{last_commit} = $parent;
Eric Wong15710b62007-01-22 02:20:33 -08002703 } else {
Simon Arlott85886162009-10-09 13:21:13 +01002704 print STDERR "Following parent with do_update\n"
2705 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002706 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08002707 $self->ra->gs_do_update($rev, $rev, $self, $ed)
Eric Wong15710b62007-01-22 02:20:33 -08002708 or die "SVN connection failed somewhere...\n";
2709 }
Simon Arlott85886162009-10-09 13:21:13 +01002710 print STDERR "Successfully followed parent\n" unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002711 return $self->make_log_entry($rev, [$parent], $ed);
2712 }
Eric Wong15710b62007-01-22 02:20:33 -08002713 return undef;
2714}
2715
Eric Wong9b981fc2007-01-11 12:14:21 -08002716sub do_fetch {
Eric Wong706587f2007-01-18 17:50:01 -08002717 my ($self, $paths, $rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08002718 my $ed;
Eric Wong9b981fc2007-01-11 12:14:21 -08002719 my ($last_rev, @parents);
Eric Wongb9dffd82007-02-09 01:28:30 -08002720 if (my $lc = $self->last_commit) {
2721 # we can have a branch that was deleted, then re-added
2722 # under the same name but copied from another path, in
2723 # which case we'll have multiple parents (we don't
2724 # want to break the original ref, nor lose copypath info):
2725 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2726 push @{$log_entry->{parents}}, $lc;
2727 return $log_entry;
2728 }
Eric Wong15710b62007-01-22 02:20:33 -08002729 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002730 $last_rev = $self->{last_rev};
Eric Wongb9dffd82007-02-09 01:28:30 -08002731 $ed->{c} = $lc;
2732 @parents = ($lc);
Eric Wong9b981fc2007-01-11 12:14:21 -08002733 } else {
2734 $last_rev = $rev;
Eric Wong15710b62007-01-22 02:20:33 -08002735 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2736 return $log_entry;
2737 }
2738 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002739 }
Eric Wong8a603772007-01-31 02:45:50 -08002740 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002741 die "SVN connection failed somewhere...\n";
2742 }
2743 $self->make_log_entry($rev, \@parents, $ed);
2744}
2745
Eric Wong6111b932009-11-15 18:57:16 -08002746sub mkemptydirs {
2747 my ($self, $r) = @_;
Eric Wong6111b932009-11-15 18:57:16 -08002748
Eric Wonga5b80d92009-12-19 13:49:00 -08002749 sub scan {
2750 my ($r, $empty_dirs, $line) = @_;
2751 if (defined $r && $line =~ /^r(\d+)$/) {
2752 return 0 if $1 > $r;
2753 } elsif ($line =~ /^ \+empty_dir: (.+)$/) {
2754 $empty_dirs->{$1} = 1;
2755 } elsif ($line =~ /^ \-empty_dir: (.+)$/) {
2756 my @d = grep {m[^\Q$1\E(/|$)]} (keys %$empty_dirs);
2757 delete @$empty_dirs{@d};
2758 }
2759 1; # continue
2760 };
2761
2762 my %empty_dirs = ();
2763 my $gz_file = "$self->{dir}/unhandled.log.gz";
2764 if (-f $gz_file) {
2765 if (!$can_compress) {
2766 warn "Compress::Zlib could not be found; ",
2767 "empty directories in $gz_file will not be read\n";
2768 } else {
2769 my $gz = Compress::Zlib::gzopen($gz_file, "rb") or
2770 die "Unable to open $gz_file: $!\n";
2771 my $line;
2772 while ($gz->gzreadline($line) > 0) {
2773 scan($r, \%empty_dirs, $line) or last;
2774 }
2775 $gz->gzclose;
Eric Wong6111b932009-11-15 18:57:16 -08002776 }
2777 }
Eric Wonga5b80d92009-12-19 13:49:00 -08002778
2779 if (open my $fh, '<', "$self->{dir}/unhandled.log") {
2780 binmode $fh or croak "binmode: $!";
2781 while (<$fh>) {
2782 scan($r, \%empty_dirs, $_) or last;
2783 }
2784 close $fh;
2785 }
Eric Wong9be30ee2009-11-22 18:11:32 -08002786
2787 my $strip = qr/\A\Q$self->{path}\E(?:\/|$)/;
Eric Wong6111b932009-11-15 18:57:16 -08002788 foreach my $d (sort keys %empty_dirs) {
2789 $d = uri_decode($d);
Eric Wong9be30ee2009-11-22 18:11:32 -08002790 $d =~ s/$strip//;
Eric Wong6111b932009-11-15 18:57:16 -08002791 next if -d $d;
2792 if (-e _) {
2793 warn "$d exists but is not a directory\n";
2794 } else {
2795 print "creating empty directory: $d\n";
2796 mkpath([$d]);
2797 }
2798 }
2799}
2800
Eric Wong97f69872007-01-25 11:53:13 -08002801sub get_untracked {
2802 my ($self, $ed) = @_;
2803 my @out;
2804 my $h = $ed->{empty};
Eric Wong9b981fc2007-01-11 12:14:21 -08002805 foreach (sort keys %$h) {
2806 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
Eric Wong97f69872007-01-25 11:53:13 -08002807 push @out, " $act: " . uri_encode($_);
Eric Wong9b981fc2007-01-11 12:14:21 -08002808 warn "W: $act: $_\n";
2809 }
2810 foreach my $t (qw/dir_prop file_prop/) {
Eric Wong97f69872007-01-25 11:53:13 -08002811 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002812 foreach my $path (sort keys %$h) {
2813 my $ppath = $path eq '' ? '.' : $path;
2814 foreach my $prop (sort keys %{$h->{$path}}) {
Eric Wong1ce255d2007-01-14 23:21:16 -08002815 next if $SKIP_PROP{$prop};
Eric Wong9b981fc2007-01-11 12:14:21 -08002816 my $v = $h->{$path}->{$prop};
Eric Wong97f69872007-01-25 11:53:13 -08002817 my $t_ppath_prop = "$t: " .
2818 uri_encode($ppath) . ' ' .
2819 uri_encode($prop);
Eric Wong9b981fc2007-01-11 12:14:21 -08002820 if (defined $v) {
Eric Wong97f69872007-01-25 11:53:13 -08002821 push @out, " +$t_ppath_prop " .
2822 uri_encode($v);
Eric Wong9b981fc2007-01-11 12:14:21 -08002823 } else {
Eric Wong97f69872007-01-25 11:53:13 -08002824 push @out, " -$t_ppath_prop";
Eric Wong9b981fc2007-01-11 12:14:21 -08002825 }
2826 }
2827 }
2828 }
2829 foreach my $t (qw/absent_file absent_directory/) {
Eric Wong97f69872007-01-25 11:53:13 -08002830 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002831 foreach my $parent (sort keys %$h) {
2832 foreach my $path (sort @{$h->{$parent}}) {
Eric Wong97f69872007-01-25 11:53:13 -08002833 push @out, " $t: " .
2834 uri_encode("$parent/$path");
Eric Wong9b981fc2007-01-11 12:14:21 -08002835 warn "W: $t: $parent/$path ",
2836 "Insufficient permissions?\n";
2837 }
2838 }
2839 }
Eric Wong97f69872007-01-25 11:53:13 -08002840 \@out;
Eric Wong9b981fc2007-01-11 12:14:21 -08002841}
2842
Pete Harlane82f0d72009-01-17 20:10:14 -08002843# parse_svn_date(DATE)
2844# --------------------
2845# Given a date (in UTC) from Subversion, return a string in the format
2846# "<TZ Offset> <local date/time>" that Git will use.
2847#
2848# By default the parsed date will be in UTC; if $Git::SVN::_localtime
2849# is true we'll convert it to the local timezone instead.
Eric Wong1c8443b2007-01-14 02:17:00 -08002850sub parse_svn_date {
2851 my $date = shift || return '+0000 1970-01-01 00:00:00';
2852 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
Junio C Hamanob94ead72009-02-18 10:48:01 -08002853 (\d\d)\:(\d\d)\:(\d\d)\.\d*Z$/x) or
Eric Wong1c8443b2007-01-14 02:17:00 -08002854 croak "Unable to parse date: $date\n";
Pete Harlane82f0d72009-01-17 20:10:14 -08002855 my $parsed_date; # Set next.
2856
2857 if ($Git::SVN::_localtime) {
2858 # Translate the Subversion datetime to an epoch time.
2859 # Begin by switching ourselves to $date's timezone, UTC.
2860 my $old_env_TZ = $ENV{TZ};
2861 $ENV{TZ} = 'UTC';
2862
2863 my $epoch_in_UTC =
2864 POSIX::strftime('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
2865
2866 # Determine our local timezone (including DST) at the
2867 # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the
2868 # value of TZ, if any, at the time we were run.
2869 if (defined $Git::SVN::Log::TZ) {
2870 $ENV{TZ} = $Git::SVN::Log::TZ;
2871 } else {
2872 delete $ENV{TZ};
2873 }
2874
2875 my $our_TZ =
2876 POSIX::strftime('%Z', $S, $M, $H, $d, $m - 1, $Y - 1900);
2877
2878 # This converts $epoch_in_UTC into our local timezone.
2879 my ($sec, $min, $hour, $mday, $mon, $year,
2880 $wday, $yday, $isdst) = localtime($epoch_in_UTC);
2881
2882 $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
2883 $our_TZ, $year + 1900, $mon + 1,
2884 $mday, $hour, $min, $sec);
2885
2886 # Reset us to the timezone in effect when we entered
2887 # this routine.
2888 if (defined $old_env_TZ) {
2889 $ENV{TZ} = $old_env_TZ;
2890 } else {
2891 delete $ENV{TZ};
2892 }
2893 } else {
2894 $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
2895 }
2896
2897 return $parsed_date;
Eric Wong1c8443b2007-01-14 02:17:00 -08002898}
2899
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002900sub other_gs {
Mattias Nissler0b2af452009-07-07 01:40:02 +02002901 my ($self, $new_url, $url,
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002902 $branch_from, $r, $old_ref_id) = @_;
Mattias Nissler0b2af452009-07-07 01:40:02 +02002903 my $gs = Git::SVN->find_by_url($new_url, $url, $branch_from);
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002904 unless ($gs) {
2905 my $ref_id = $old_ref_id;
2906 $ref_id =~ s/\@\d+$//;
2907 $ref_id .= "\@$r";
2908 # just grow a tail if we're not unique enough :x
2909 $ref_id .= '-' while find_ref($ref_id);
Simon Arlott85886162009-10-09 13:21:13 +01002910 print STDERR "Initializing parent: $ref_id\n" unless $::_q > 1;
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002911 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
2912 if ($u =~ s#^\Q$url\E(/|$)##) {
2913 $p = $u;
2914 $u = $url;
2915 $repo_id = $self->{repo_id};
2916 }
2917 $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1);
2918 }
2919 $gs
2920}
2921
Mark Lodato36db1ed2009-05-14 21:27:15 -04002922sub call_authors_prog {
2923 my ($orig_author) = @_;
Mark Lodatod3d7d472009-09-12 20:33:23 -04002924 $orig_author = command_oneline('rev-parse', '--sq-quote', $orig_author);
Mark Lodato36db1ed2009-05-14 21:27:15 -04002925 my $author = `$::_authors_prog $orig_author`;
2926 if ($? != 0) {
2927 die "$::_authors_prog failed with exit code $?\n"
2928 }
2929 if ($author =~ /^\s*(.+?)\s*<(.*)>\s*$/) {
2930 my ($name, $email) = ($1, $2);
2931 $email = undef if length $2 == 0;
2932 return [$name, $email];
2933 } else {
2934 die "Author: $orig_author: $::_authors_prog returned "
2935 . "invalid author format: $author\n";
2936 }
2937}
2938
Eric Wong1c8443b2007-01-14 02:17:00 -08002939sub check_author {
2940 my ($author) = @_;
2941 if (!defined $author || length $author == 0) {
2942 $author = '(no author)';
Mark Lodato36db1ed2009-05-14 21:27:15 -04002943 }
2944 if (!defined $::users{$author}) {
2945 if (defined $::_authors_prog) {
2946 $::users{$author} = call_authors_prog($author);
2947 } elsif (defined $::_authors) {
2948 die "Author: $author not defined in $::_authors file\n";
2949 }
Eric Wong1c8443b2007-01-14 02:17:00 -08002950 }
2951 $author;
2952}
2953
Sam Vilainf1264bd2009-10-20 15:42:01 +13002954sub find_extra_svk_parents {
2955 my ($self, $ed, $tickets, $parents) = @_;
2956 # aha! svk:merge property changed...
2957 my @tickets = split "\n", $tickets;
2958 my @known_parents;
2959 for my $ticket ( @tickets ) {
2960 my ($uuid, $path, $rev) = split /:/, $ticket;
2961 if ( $uuid eq $self->ra_uuid ) {
2962 my $url = $self->rewrite_root || $self->{url};
2963 my $repos_root = $url;
2964 my $branch_from = $path;
2965 $branch_from =~ s{^/}{};
2966 my $gs = $self->other_gs($repos_root."/".$branch_from,
2967 $url,
2968 $branch_from,
2969 $rev,
2970 $self->{ref_id});
2971 if ( my $commit = $gs->rev_map_get($rev, $uuid) ) {
2972 # wahey! we found it, but it might be
2973 # an old one (!)
Alex Vandivere9e4c8b2009-11-29 02:20:21 -05002974 push @known_parents, [ $rev, $commit ];
Sam Vilainf1264bd2009-10-20 15:42:01 +13002975 }
2976 }
2977 }
Alex Vandivere9e4c8b2009-11-29 02:20:21 -05002978 # Ordering matters; highest-numbered commit merge tickets
2979 # first, as they may account for later merge ticket additions
2980 # or changes.
2981 @known_parents = map {$_->[1]} sort {$b->[0] <=> $a->[0]} @known_parents;
Sam Vilainf1264bd2009-10-20 15:42:01 +13002982 for my $parent ( @known_parents ) {
2983 my @cmd = ('rev-list', $parent, map { "^$_" } @$parents );
2984 my ($msg_fh, $ctx) = command_output_pipe(@cmd);
2985 my $new;
2986 while ( <$msg_fh> ) {
2987 $new=1;last;
2988 }
2989 command_close_pipe($msg_fh, $ctx);
2990 if ( $new ) {
2991 print STDERR
2992 "Found merge parent (svk:merge ticket): $parent\n";
2993 push @$parents, $parent;
2994 }
2995 }
2996}
2997
Sam Vilain7d944c32009-12-20 00:55:13 +13002998sub lookup_svn_merge {
2999 my $uuid = shift;
3000 my $url = shift;
3001 my $merge = shift;
3002
3003 my ($source, $revs) = split ":", $merge;
3004 my $path = $source;
3005 $path =~ s{^/}{};
3006 my $gs = Git::SVN->find_by_url($url.$source, $url, $path);
3007 if ( !$gs ) {
3008 warn "Couldn't find revmap for $url$source\n";
3009 return;
3010 }
3011 my @ranges = split ",", $revs;
3012 my ($tip, $tip_commit);
3013 my @merged_commit_ranges;
3014 # find the tip
3015 for my $range ( @ranges ) {
3016 my ($bottom, $top) = split "-", $range;
3017 $top ||= $bottom;
Sam Vilain33973a52009-12-20 05:22:42 +13003018 my $bottom_commit = $gs->find_rev_after( $bottom, 1, $top );
3019 my $top_commit = $gs->find_rev_before( $top, 1, $bottom );
Sam Vilain7d944c32009-12-20 00:55:13 +13003020
3021 unless ($top_commit and $bottom_commit) {
3022 warn "W:unknown path/rev in svn:mergeinfo "
3023 ."dirprop: $source:$range\n";
3024 next;
3025 }
3026
3027 push @merged_commit_ranges,
Sam Vilain33973a52009-12-20 05:22:42 +13003028 "$bottom_commit^..$top_commit";
Sam Vilain7d944c32009-12-20 00:55:13 +13003029
3030 if ( !defined $tip or $top > $tip ) {
3031 $tip = $top;
3032 $tip_commit = $top_commit;
3033 }
3034 }
3035 return ($tip_commit, @merged_commit_ranges);
3036}
3037BEGIN {
3038 memoize 'lookup_svn_merge';
3039}
3040
Sam Vilaindff589e2009-10-20 15:42:03 +13003041# note: this function should only be called if the various dirprops
3042# have actually changed
3043sub find_extra_svn_parents {
3044 my ($self, $ed, $mergeinfo, $parents) = @_;
3045 # aha! svk:merge property changed...
3046
3047 # We first search for merged tips which are not in our
3048 # history. Then, we figure out which git revisions are in
3049 # that tip, but not this revision. If all of those revisions
3050 # are now marked as merge, we can add the tip as a parent.
3051 my @merges = split "\n", $mergeinfo;
3052 my @merge_tips;
3053 my @merged_commit_ranges;
3054 my $url = $self->rewrite_root || $self->{url};
Sam Vilain7d944c32009-12-20 00:55:13 +13003055 my $uuid = $self->ra_uuid;
Sam Vilaindff589e2009-10-20 15:42:03 +13003056 for my $merge ( @merges ) {
Sam Vilain7d944c32009-12-20 00:55:13 +13003057 my ($tip_commit, @ranges) =
3058 lookup_svn_merge( $uuid, $url, $merge );
3059 push @merged_commit_ranges, @ranges;
Sam Vilaindff589e2009-10-20 15:42:03 +13003060 unless (!$tip_commit or
3061 grep { $_ eq $tip_commit } @$parents ) {
3062 push @merge_tips, $tip_commit;
3063 } else {
3064 push @merge_tips, undef;
3065 }
3066 }
3067 for my $merge_tip ( @merge_tips ) {
3068 my $spec = shift @merges;
3069 next unless $merge_tip;
3070 my @cmd = ('rev-list', "-1", $merge_tip,
3071 "--not", @$parents );
3072 my ($msg_fh, $ctx) = command_output_pipe(@cmd);
3073 my $new;
3074 while ( <$msg_fh> ) {
3075 $new=1;last;
3076 }
3077 command_close_pipe($msg_fh, $ctx);
3078 if ( $new ) {
3079 push @cmd, @merged_commit_ranges;
3080 my ($msg_fh, $ctx) = command_output_pipe(@cmd);
3081 my $unmerged;
3082 while ( <$msg_fh> ) {
3083 $unmerged=1;last;
3084 }
3085 command_close_pipe($msg_fh, $ctx);
3086 if ( $unmerged ) {
3087 warn "W:svn cherry-pick ignored ($spec)\n";
3088 } else {
3089 warn
3090 "Found merge parent (svn:mergeinfo prop): ",
3091 $merge_tip, "\n";
3092 push @$parents, $merge_tip;
3093 }
3094 }
3095 }
3096}
3097
Eric Wong9b981fc2007-01-11 12:14:21 -08003098sub make_log_entry {
Eric Wong97f69872007-01-25 11:53:13 -08003099 my ($self, $rev, $parents, $ed) = @_;
3100 my $untracked = $self->get_untracked($ed);
3101
Sam Vilainf1264bd2009-10-20 15:42:01 +13003102 my @parents = @$parents;
3103 my $ps = $ed->{path_strip} || "";
3104 for my $path ( grep { m/$ps/ } %{$ed->{dir_prop}} ) {
3105 my $props = $ed->{dir_prop}{$path};
3106 if ( $props->{"svk:merge"} ) {
3107 $self->find_extra_svk_parents
3108 ($ed, $props->{"svk:merge"}, \@parents);
3109 }
Sam Vilaindff589e2009-10-20 15:42:03 +13003110 if ( $props->{"svn:mergeinfo"} ) {
3111 $self->find_extra_svn_parents
3112 ($ed,
3113 $props->{"svn:mergeinfo"},
3114 \@parents);
3115 }
Sam Vilainf1264bd2009-10-20 15:42:01 +13003116 }
3117
Eric Wong9b981fc2007-01-11 12:14:21 -08003118 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08003119 print $un "r$rev\n" or croak $!;
3120 print $un $_, "\n" foreach @$untracked;
Sam Vilainf1264bd2009-10-20 15:42:01 +13003121 my %log_entry = ( parents => \@parents, revision => $rev,
Eric Wong97f69872007-01-25 11:53:13 -08003122 log => '');
Eric Wongfbcc1732007-02-06 18:35:30 -08003123
Eric Wong8a49ee92007-02-10 20:46:50 -08003124 my $headrev;
Eric Wongfbcc1732007-02-06 18:35:30 -08003125 my $logged = delete $self->{logged_rev_props};
Eric Wong8a49ee92007-02-10 20:46:50 -08003126 if (!$logged || $self->{-want_revprops}) {
Eric Wongfbcc1732007-02-06 18:35:30 -08003127 my $rp = $self->ra->rev_proplist($rev);
3128 foreach (sort keys %$rp) {
3129 my $v = $rp->{$_};
3130 if (/^svn:(author|date|log)$/) {
3131 $log_entry{$1} = $v;
Eric Wong8a49ee92007-02-10 20:46:50 -08003132 } elsif ($_ eq 'svm:headrev') {
3133 $headrev = $v;
Eric Wongfbcc1732007-02-06 18:35:30 -08003134 } else {
3135 print $un " rev_prop: ", uri_encode($_), ' ',
3136 uri_encode($v), "\n";
3137 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003138 }
Eric Wongfbcc1732007-02-06 18:35:30 -08003139 } else {
3140 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
Eric Wong9b981fc2007-01-11 12:14:21 -08003141 }
3142 close $un or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08003143
Eric Wong9b981fc2007-01-11 12:14:21 -08003144 $log_entry{date} = parse_svn_date($log_entry{date});
Eric Wong9b981fc2007-01-11 12:14:21 -08003145 $log_entry{log} .= "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08003146 my $author = $log_entry{author} = check_author($log_entry{author});
3147 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003148 : ($author, undef);
3149
3150 my ($commit_name, $commit_email) = ($name, $email);
3151 if ($_use_log_author) {
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00003152 my $name_field;
3153 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
3154 $name_field = $1;
3155 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
3156 $name_field = $1;
3157 }
3158 if (!defined $name_field) {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02003159 if (!defined $email) {
3160 $email = $name;
3161 }
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00003162 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003163 ($name, $email) = ($1, $2);
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00003164 } elsif ($name_field =~ /(.*)@/) {
3165 ($name, $email) = ($1, $name_field);
3166 } else {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02003167 ($name, $email) = ($name_field, $name_field);
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003168 }
3169 }
Eric Wong91b03282007-02-11 00:51:33 -08003170 if (defined $headrev && $self->use_svm_props) {
Eric Wongaea736c2007-02-16 19:15:21 -08003171 if ($self->rewrite_root) {
3172 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
3173 "options set!\n";
3174 }
Eric Wongb3e95932009-07-11 14:13:12 -07003175 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$}i;
Eric Wongbefc9ad2007-02-17 02:53:07 -08003176 # we don't want "SVM: initializing mirror for junk" ...
3177 return undef if $r == 0;
3178 my $svm = $self->svm;
3179 if ($uuid ne $svm->{uuid}) {
Eric Wong8a49ee92007-02-10 20:46:50 -08003180 die "UUID mismatch on SVM path:\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08003181 "expected: $svm->{uuid}\n",
Eric Wong8a49ee92007-02-10 20:46:50 -08003182 " got: $uuid\n";
3183 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08003184 my $full_url = $self->full_url;
3185 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
3186 die "Failed to replace '$svm->{replace}' with ",
3187 "'$svm->{source}' in $full_url\n";
Sam Vilain18ea92b2007-02-23 12:32:29 +13003188 # throw away username for storing in records
3189 remove_username($full_url);
Eric Wong8a49ee92007-02-10 20:46:50 -08003190 $log_entry{metadata} = "$full_url\@$r $uuid";
3191 $log_entry{svm_revision} = $r;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003192 $email ||= "$author\@$uuid";
3193 $commit_email ||= "$author\@$uuid";
Eric Wong62e349d2007-02-16 19:57:29 -08003194 } elsif ($self->use_svnsync_props) {
3195 my $full_url = $self->svnsync->{url};
3196 $full_url .= "/$self->{path}" if length $self->{path};
Adam Robence118732007-04-24 18:02:07 -07003197 remove_username($full_url);
Eric Wong62e349d2007-02-16 19:57:29 -08003198 my $uuid = $self->svnsync->{uuid};
3199 $log_entry{metadata} = "$full_url\@$rev $uuid";
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003200 $email ||= "$author\@$uuid";
3201 $commit_email ||= "$author\@$uuid";
Eric Wong8a49ee92007-02-10 20:46:50 -08003202 } else {
Adam Robence118732007-04-24 18:02:07 -07003203 my $url = $self->metadata_url;
3204 remove_username($url);
3205 $log_entry{metadata} = "$url\@$rev " .
Eric Wong26a62d52007-02-12 13:25:25 -08003206 $self->ra->get_uuid;
Eric Wongdb03cd22007-02-13 00:38:02 -08003207 $email ||= "$author\@" . $self->ra->get_uuid;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003208 $commit_email ||= "$author\@" . $self->ra->get_uuid;
Eric Wong8a49ee92007-02-10 20:46:50 -08003209 }
Eric Wongdb03cd22007-02-13 00:38:02 -08003210 $log_entry{name} = $name;
3211 $log_entry{email} = $email;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003212 $log_entry{commit_name} = $commit_name;
3213 $log_entry{commit_email} = $commit_email;
Eric Wong9b981fc2007-01-11 12:14:21 -08003214 \%log_entry;
3215}
3216
3217sub fetch {
Eric Wong3ebe8df2007-01-25 17:35:40 -08003218 my ($self, $min_rev, $max_rev, @parents) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08003219 my ($last_rev, $last_commit) = $self->last_rev_commit;
Eric Wong3ebe8df2007-01-25 17:35:40 -08003220 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
Eric Wonge5181922007-02-08 12:53:57 -08003221 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
Eric Wong9b981fc2007-01-11 12:14:21 -08003222}
3223
3224sub set_tree_cb {
3225 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
Eric Wong490f49e2007-02-10 13:58:33 -08003226 $self->{inject_parents} = { $rev => $tree };
3227 $self->fetch(undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08003228}
3229
3230sub set_tree {
3231 my ($self, $tree) = (shift, shift);
Eric Wong1ce255d2007-01-14 23:21:16 -08003232 my $log_entry = ::get_commit_entry($tree);
Eric Wong9b981fc2007-01-11 12:14:21 -08003233 unless ($self->{last_rev}) {
Luc Heinrich0a1a1c82008-09-29 15:58:18 +02003234 ::fatal("Must have an existing revision to commit");
Eric Wong9b981fc2007-01-11 12:14:21 -08003235 }
Eric Wong61395352007-01-27 14:33:08 -08003236 my %ed_opts = ( r => $self->{last_rev},
3237 log => $log_entry->{log},
3238 ra => $self->ra,
3239 tree_a => $self->{last_commit},
3240 tree_b => $tree,
3241 editor_cb => sub {
3242 $self->set_tree_cb($log_entry, $tree, @_) },
3243 svn_path => $self->{path} );
3244 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong9b981fc2007-01-11 12:14:21 -08003245 print "No changes\nr$self->{last_rev} = $tree\n";
3246 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003247}
3248
Eric Wong060610c2007-12-08 23:27:41 -08003249sub rebuild_from_rev_db {
3250 my ($self, $path) = @_;
3251 my $r = -1;
3252 open my $fh, '<', $path or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02003253 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003254 while (<$fh>) {
3255 length($_) == 41 or croak "inconsistent size in ($_) != 41";
3256 chomp($_);
3257 ++$r;
3258 next if $_ eq ('0' x 40);
3259 $self->rev_map_set($r, $_);
3260 print "r$r = $_\n";
3261 }
3262 close $fh or croak "close: $!";
3263 unlink $path or croak "unlink: $!";
3264}
3265
Eric Wongf0ecca12007-01-30 13:11:14 -08003266sub rebuild {
3267 my ($self) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003268 my $map_path = $self->map_path;
Deskin Miller2beec892008-09-15 21:12:58 -04003269 my $partial = (-e $map_path && ! -z $map_path);
Eric Wongd6d33462007-02-16 04:05:33 -08003270 return unless ::verify_ref($self->refname.'^0');
Deskin Miller2beec892008-09-15 21:12:58 -04003271 if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
Eric Wong060610c2007-12-08 23:27:41 -08003272 my $rev_db = $self->rev_db_path;
3273 $self->rebuild_from_rev_db($rev_db);
3274 if ($self->use_svm_props) {
3275 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
3276 $self->rebuild_from_rev_db($svm_rev_db);
3277 }
3278 $self->unlink_rev_db_symlink;
Eric Wong26a62d52007-02-12 13:25:25 -08003279 return;
3280 }
Deskin Miller2beec892008-09-15 21:12:58 -04003281 print "Rebuilding $map_path ...\n" if (!$partial);
3282 my ($base_rev, $head) = ($partial ? $self->rev_map_max_norebuild(1) :
3283 (undef, undef));
Eric Wong060610c2007-12-08 23:27:41 -08003284 my ($log, $ctx) =
3285 command_output_pipe(qw/rev-list --pretty=raw --no-color --reverse/,
Deskin Miller2beec892008-09-15 21:12:58 -04003286 ($head ? "$head.." : "") . $self->refname,
3287 '--');
Jan Krüger74b1e122008-06-24 02:17:36 +02003288 my $metadata_url = $self->metadata_url;
3289 remove_username($metadata_url);
Eric Wong060610c2007-12-08 23:27:41 -08003290 my $svn_uuid = $self->ra_uuid;
Sam Vilain3dfab992007-06-30 20:56:13 +12003291 my $c;
3292 while (<$log>) {
3293 if ( m{^commit ($::sha1)$} ) {
3294 $c = $1;
3295 next;
3296 }
3297 next unless s{^\s*(git-svn-id:)}{$1};
3298 my ($url, $rev, $uuid) = ::extract_metadata($_);
Sam Vilain18ea92b2007-02-23 12:32:29 +13003299 remove_username($url);
Eric Wongf0ecca12007-01-30 13:11:14 -08003300
3301 # ignore merges (from set-tree)
3302 next if (!defined $rev || !$uuid);
3303
3304 # if we merged or otherwise started elsewhere, this is
3305 # how we break out of it
Eric Wong060610c2007-12-08 23:27:41 -08003306 if (($uuid ne $svn_uuid) ||
Jan Krüger74b1e122008-06-24 02:17:36 +02003307 ($metadata_url && $url && ($url ne $metadata_url))) {
Eric Wongf0ecca12007-01-30 13:11:14 -08003308 next;
3309 }
Deskin Miller2beec892008-09-15 21:12:58 -04003310 if ($partial && $head) {
3311 print "Partial-rebuilding $map_path ...\n";
3312 print "Currently at $base_rev = $head\n";
3313 $head = undef;
3314 }
Eric Wongf0ecca12007-01-30 13:11:14 -08003315
Eric Wong060610c2007-12-08 23:27:41 -08003316 $self->rev_map_set($rev, $c);
Eric Wongf0ecca12007-01-30 13:11:14 -08003317 print "r$rev = $c\n";
3318 }
Sam Vilain3dfab992007-06-30 20:56:13 +12003319 command_close_pipe($log, $ctx);
Deskin Miller2beec892008-09-15 21:12:58 -04003320 print "Done rebuilding $map_path\n" if (!$partial || !$head);
Eric Wong060610c2007-12-08 23:27:41 -08003321 my $rev_db_path = $self->rev_db_path;
3322 if (-f $self->rev_db_path) {
3323 unlink $self->rev_db_path or croak "unlink: $!";
3324 }
3325 $self->unlink_rev_db_symlink;
Eric Wongf0ecca12007-01-30 13:11:14 -08003326}
3327
Eric Wong060610c2007-12-08 23:27:41 -08003328# rev_map:
Eric Wong9b981fc2007-01-11 12:14:21 -08003329# Tie::File seems to be prone to offset errors if revisions get sparse,
3330# it's not that fast, either. Tie::File is also not in Perl 5.6. So
3331# one of my favorite modules is out :< Next up would be one of the DBM
Eric Wong060610c2007-12-08 23:27:41 -08003332# modules, but I'm not sure which is most portable...
3333#
3334# This is the replacement for the rev_db format, which was too big
3335# and inefficient for large repositories with a lot of sparse history
3336# (mainly tags)
3337#
3338# The format is this:
3339# - 24 bytes for every record,
3340# * 4 bytes for the integer representing an SVN revision number
3341# * 20 bytes representing the sha1 of a git commit
3342# - No empty padding records like the old format
Eric Wong66ab84b2007-12-08 23:27:42 -08003343# (except the last record, which can be overwritten)
Eric Wong060610c2007-12-08 23:27:41 -08003344# - new records are written append-only since SVN revision numbers
3345# increase monotonically
3346# - lookups on SVN revision number are done via a binary search
Eric Wong66ab84b2007-12-08 23:27:42 -08003347# - Piping the file to xxd -c24 is a good way of dumping it for
3348# viewing or editing (piped back through xxd -r), should the need
3349# ever arise.
3350# - The last record can be padding revision with an all-zero sha1
3351# This is used to optimize fetch performance when using multiple
3352# "fetch" directives in .git/config
Eric Wong060610c2007-12-08 23:27:41 -08003353#
Eric Wong97ae0912007-02-11 15:21:24 -08003354# These files are disposable unless noMetadata or useSvmProps is set
Eric Wong9b981fc2007-01-11 12:14:21 -08003355
Eric Wong060610c2007-12-08 23:27:41 -08003356sub _rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08003357 my ($fh, $rev, $commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003358
Michael Weber4f7ec792008-04-18 15:12:04 +02003359 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003360 my $size = (stat($fh))[7];
3361 ($size % 24) == 0 or croak "inconsistent size: $size";
3362
Eric Wong66ab84b2007-12-08 23:27:42 -08003363 my $wr_offset = 0;
Eric Wong060610c2007-12-08 23:27:41 -08003364 if ($size > 0) {
3365 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
3366 my $read = sysread($fh, my $buf, 24) or croak "read: $!";
3367 $read == 24 or croak "read only $read bytes (!= 24)";
3368 my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08003369 if ($last_commit eq ('0' x40)) {
3370 if ($size >= 48) {
3371 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
3372 $read = sysread($fh, $buf, 24) or
3373 croak "read: $!";
3374 $read == 24 or
3375 croak "read only $read bytes (!= 24)";
3376 ($last_rev, $last_commit) =
3377 unpack(rev_map_fmt, $buf);
3378 if ($last_commit eq ('0' x40)) {
3379 croak "inconsistent .rev_map\n";
3380 }
3381 }
3382 if ($last_rev >= $rev) {
3383 croak "last_rev is higher!: $last_rev >= $rev";
3384 }
3385 $wr_offset = -24;
Eric Wong47a0b752007-01-31 05:13:30 -08003386 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003387 }
Eric Wong66ab84b2007-12-08 23:27:42 -08003388 sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003389 syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or
3390 croak "write: $!";
Eric Wong26a62d52007-02-12 13:25:25 -08003391}
3392
Ben Jackson195643f2009-06-03 20:45:52 -07003393sub _rev_map_reset {
3394 my ($fh, $rev, $commit) = @_;
3395 my $c = _rev_map_get($fh, $rev);
3396 $c eq $commit or die "_rev_map_reset(@_) commit $c does not match!\n";
3397 my $offset = sysseek($fh, 0, SEEK_CUR) or croak "seek: $!";
3398 truncate $fh, $offset or croak "truncate: $!";
3399}
3400
Eric Wong26a62d52007-02-12 13:25:25 -08003401sub mkfile {
3402 my ($path) = @_;
3403 unless (-e $path) {
3404 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
3405 mkpath([$dir]) unless -d $dir;
3406 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
3407 close $fh or die "Couldn't close (create) $path: $!\n";
3408 }
3409}
3410
Eric Wong060610c2007-12-08 23:27:41 -08003411sub rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08003412 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
3413 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
Eric Wong060610c2007-12-08 23:27:41 -08003414 my $db = $self->map_path($uuid);
Eric Wong26a62d52007-02-12 13:25:25 -08003415 my $db_lock = "$db.lock";
3416 my $sig;
Ben Jackson195643f2009-06-03 20:45:52 -07003417 $update_ref ||= 0;
Eric Wong26a62d52007-02-12 13:25:25 -08003418 if ($update_ref) {
3419 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
3420 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
3421 }
3422 mkfile($db);
3423
3424 $LOCKFILES{$db_lock} = 1;
3425 my $sync;
3426 # both of these options make our .rev_db file very, very important
3427 # and we can't afford to lose it because rebuild() won't work
3428 if ($self->use_svm_props || $self->no_metadata) {
3429 $sync = 1;
Eric Wong060610c2007-12-08 23:27:41 -08003430 copy($db, $db_lock) or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08003431 "Failed to copy: ",
3432 "$db => $db_lock ($!)\n";
3433 } else {
Eric Wong060610c2007-12-08 23:27:41 -08003434 rename $db, $db_lock or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08003435 "Failed to rename: ",
3436 "$db => $db_lock ($!)\n";
3437 }
Eric Wong060610c2007-12-08 23:27:41 -08003438
Eric Wong66ab84b2007-12-08 23:27:42 -08003439 sysopen(my $fh, $db_lock, O_RDWR | O_CREAT)
Eric Wong060610c2007-12-08 23:27:41 -08003440 or croak "Couldn't open $db_lock: $!\n";
Ben Jackson195643f2009-06-03 20:45:52 -07003441 $update_ref eq 'reset' ? _rev_map_reset($fh, $rev, $commit) :
3442 _rev_map_set($fh, $rev, $commit);
Eric Wong97ae0912007-02-11 15:21:24 -08003443 if ($sync) {
3444 $fh->flush or die "Couldn't flush $db_lock: $!\n";
3445 $fh->sync or die "Couldn't sync $db_lock: $!\n";
3446 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003447 close $fh or croak $!;
Eric Wong373274f2007-01-31 13:54:23 -08003448 if ($update_ref) {
Eric Wong1e889ef2007-02-16 01:45:13 -08003449 $_head = $self;
Ben Jackson195643f2009-06-03 20:45:52 -07003450 my $note = "";
3451 $note = " ($update_ref)" if ($update_ref !~ /^\d*$/);
3452 command_noisy('update-ref', '-m', "r$rev$note",
Eric Wong373274f2007-01-31 13:54:23 -08003453 $self->refname, $commit);
3454 }
Eric Wong060610c2007-12-08 23:27:41 -08003455 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
Eric Wong373274f2007-01-31 13:54:23 -08003456 "$db_lock => $db ($!)\n";
3457 delete $LOCKFILES{$db_lock};
3458 if ($update_ref) {
3459 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
3460 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
3461 kill $sig, $$ if defined $sig;
3462 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003463}
3464
Eric Wong66ab84b2007-12-08 23:27:42 -08003465# If want_commit, this will return an array of (rev, commit) where
3466# commit _must_ be a valid commit in the archive.
3467# Otherwise, it'll return the max revision (whether or not the
3468# commit is valid or just a 0x40 placeholder).
Eric Wong060610c2007-12-08 23:27:41 -08003469sub rev_map_max {
Eric Wong66ab84b2007-12-08 23:27:42 -08003470 my ($self, $want_commit) = @_;
Eric Wongd6d33462007-02-16 04:05:33 -08003471 $self->rebuild;
Deskin Miller2beec892008-09-15 21:12:58 -04003472 my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
3473 $want_commit ? ($r, $c) : $r;
3474}
3475
3476sub rev_map_max_norebuild {
3477 my ($self, $want_commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003478 my $map_path = $self->map_path;
Eric Wong66ab84b2007-12-08 23:27:42 -08003479 stat $map_path or return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08003480 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02003481 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003482 my $size = (stat($fh))[7];
3483 ($size % 24) == 0 or croak "inconsistent size: $size";
3484
3485 if ($size == 0) {
3486 close $fh or croak "close: $!";
Eric Wong66ab84b2007-12-08 23:27:42 -08003487 return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08003488 }
3489
Eric Wong66ab84b2007-12-08 23:27:42 -08003490 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003491 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003492 my ($r, $c) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08003493 if ($want_commit && $c eq ('0' x40)) {
3494 if ($size < 48) {
3495 return $want_commit ? (0, undef) : 0;
3496 }
3497 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
3498 sysread($fh, $buf, 24) == 24 or croak "read: $!";
3499 ($r, $c) = unpack(rev_map_fmt, $buf);
3500 if ($c eq ('0'x40)) {
3501 croak "Penultimate record is all-zeroes in $map_path";
3502 }
3503 }
3504 close $fh or croak "close: $!";
3505 $want_commit ? ($r, $c) : $r;
Eric Wong9c93fee2007-01-31 17:22:31 -08003506}
3507
Eric Wong060610c2007-12-08 23:27:41 -08003508sub rev_map_get {
Eric Wong26a62d52007-02-12 13:25:25 -08003509 my ($self, $rev, $uuid) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003510 my $map_path = $self->map_path($uuid);
3511 return undef unless -e $map_path;
3512
3513 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Ben Jackson195643f2009-06-03 20:45:52 -07003514 my $c = _rev_map_get($fh, $rev);
3515 close($fh) or croak "close: $!";
3516 $c
3517}
3518
3519sub _rev_map_get {
3520 my ($fh, $rev) = @_;
3521
Michael Weber4f7ec792008-04-18 15:12:04 +02003522 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003523 my $size = (stat($fh))[7];
3524 ($size % 24) == 0 or croak "inconsistent size: $size";
3525
3526 if ($size == 0) {
Eric Wong060610c2007-12-08 23:27:41 -08003527 return undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003528 }
Eric Wong060610c2007-12-08 23:27:41 -08003529
3530 my ($l, $u) = (0, $size - 24);
3531 my ($r, $c, $buf);
3532
3533 while ($l <= $u) {
3534 my $i = int(($l/24 + $u/24) / 2) * 24;
3535 sysseek($fh, $i, SEEK_SET) or croak "seek: $!";
3536 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
Eric Wongc83f4e62009-08-12 22:20:02 -07003537 my ($r, $c) = unpack(rev_map_fmt, $buf);
Eric Wong060610c2007-12-08 23:27:41 -08003538
3539 if ($r < $rev) {
3540 $l = $i + 24;
3541 } elsif ($r > $rev) {
3542 $u = $i - 24;
3543 } else { # $r == $rev
Eric Wong66ab84b2007-12-08 23:27:42 -08003544 return $c eq ('0' x 40) ? undef : $c;
Eric Wong060610c2007-12-08 23:27:41 -08003545 }
3546 }
Eric Wong060610c2007-12-08 23:27:41 -08003547 undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003548}
3549
David D Kilzer111947e2007-11-11 22:56:52 -08003550# Finds the first svn revision that exists on (if $eq_ok is true) or
3551# before $rev for the current branch. It will not search any lower
3552# than $min_rev. Returns the git commit hash and svn revision number
3553# if found, else (undef, undef).
Eric Wong15710b62007-01-22 02:20:33 -08003554sub find_rev_before {
David D Kilzer111947e2007-11-11 22:56:52 -08003555 my ($self, $rev, $eq_ok, $min_rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08003556 --$rev unless $eq_ok;
David D Kilzer111947e2007-11-11 22:56:52 -08003557 $min_rev ||= 1;
Ben Jacksonca5e8802009-06-03 20:45:51 -07003558 my $max_rev = $self->rev_map_max;
3559 $rev = $max_rev if ($rev > $max_rev);
David D Kilzer111947e2007-11-11 22:56:52 -08003560 while ($rev >= $min_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003561 if (my $c = $self->rev_map_get($rev)) {
Eric Wong15710b62007-01-22 02:20:33 -08003562 return ($rev, $c);
3563 }
3564 --$rev;
3565 }
3566 return (undef, undef);
3567}
3568
David D Kilzer111947e2007-11-11 22:56:52 -08003569# Finds the first svn revision that exists on (if $eq_ok is true) or
3570# after $rev for the current branch. It will not search any higher
3571# than $max_rev. Returns the git commit hash and svn revision number
3572# if found, else (undef, undef).
3573sub find_rev_after {
3574 my ($self, $rev, $eq_ok, $max_rev) = @_;
3575 ++$rev unless $eq_ok;
Eric Wong060610c2007-12-08 23:27:41 -08003576 $max_rev ||= $self->rev_map_max;
David D Kilzer111947e2007-11-11 22:56:52 -08003577 while ($rev <= $max_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003578 if (my $c = $self->rev_map_get($rev)) {
David D Kilzer111947e2007-11-11 22:56:52 -08003579 return ($rev, $c);
3580 }
3581 ++$rev;
3582 }
3583 return (undef, undef);
3584}
3585
Eric Wong9b981fc2007-01-11 12:14:21 -08003586sub _new {
Eric Wong706587f2007-01-18 17:50:01 -08003587 my ($class, $repo_id, $ref_id, $path) = @_;
3588 unless (defined $repo_id && length $repo_id) {
3589 $repo_id = $Git::SVN::default_repo_id;
3590 }
3591 unless (defined $ref_id && length $ref_id) {
Adam Brewster63de84a2009-08-03 21:40:38 -04003592 $_prefix = '' unless defined($_prefix);
Adam Brewster6f5748e2009-08-11 23:14:27 -04003593 $_[2] = $ref_id =
3594 "refs/remotes/$_prefix$Git::SVN::default_ref_id";
Eric Wong706587f2007-01-18 17:50:01 -08003595 }
Eric Wong7829f202008-06-28 20:40:32 -07003596 $_[1] = $repo_id;
Eric Wong706587f2007-01-18 17:50:01 -08003597 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
Adam Brewster6f5748e2009-08-11 23:14:27 -04003598
3599 # Older repos imported by us used $GIT_DIR/svn/foo instead of
3600 # $GIT_DIR/svn/refs/remotes/foo when tracking refs/remotes/foo
3601 if ($ref_id =~ m{^refs/remotes/(.*)}) {
3602 my $old_dir = "$ENV{GIT_DIR}/svn/$1";
3603 if (-d $old_dir && ! -d $dir) {
3604 $dir = $old_dir;
3605 }
3606 }
3607
Eric Wong706587f2007-01-18 17:50:01 -08003608 $_[3] = $path = '' unless (defined $path);
Adam Brewster6f5748e2009-08-11 23:14:27 -04003609 mkpath([$dir]);
Eric Wong26a62d52007-02-12 13:25:25 -08003610 bless {
3611 ref_id => $ref_id, dir => $dir, index => "$dir/index",
Eric Wong8a49ee92007-02-10 20:46:50 -08003612 path => $path, config => "$ENV{GIT_DIR}/svn/config",
Eric Wong060610c2007-12-08 23:27:41 -08003613 map_root => "$dir/.rev_map", repo_id => $repo_id }, $class;
Eric Wong26a62d52007-02-12 13:25:25 -08003614}
3615
Eric Wong060610c2007-12-08 23:27:41 -08003616# for read-only access of old .rev_db formats
3617sub unlink_rev_db_symlink {
3618 my ($self) = @_;
3619 my $link = $self->rev_db_path;
3620 $link =~ s/\.[\w-]+$// or croak "missing UUID at the end of $link";
3621 if (-l $link) {
3622 unlink $link or croak "unlink: $link failed!";
3623 }
3624}
3625
3626sub rev_db_path {
3627 my ($self, $uuid) = @_;
3628 my $db_path = $self->map_path($uuid);
3629 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
3630 or croak "map_path: $db_path does not contain '/.rev_map.' !";
3631 $db_path;
3632}
3633
3634# the new replacement for .rev_db
3635sub map_path {
Eric Wong26a62d52007-02-12 13:25:25 -08003636 my ($self, $uuid) = @_;
3637 $uuid ||= $self->ra_uuid;
Eric Wong060610c2007-12-08 23:27:41 -08003638 "$self->{map_root}.$uuid";
Eric Wong9b981fc2007-01-11 12:14:21 -08003639}
3640
Eric Wong1c8443b2007-01-14 02:17:00 -08003641sub uri_encode {
3642 my ($f) = @_;
3643 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
3644 $f
3645}
Eric Wong9b981fc2007-01-11 12:14:21 -08003646
Eric Wong6111b932009-11-15 18:57:16 -08003647sub uri_decode {
3648 my ($f) = @_;
3649 $f =~ s#%([0-9a-fA-F]{2})#chr(hex($1))#eg;
3650 $f
3651}
3652
Sam Vilain18ea92b2007-02-23 12:32:29 +13003653sub remove_username {
3654 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
3655}
3656
Eric Wongd976acf2007-01-04 00:45:03 -08003657package Git::SVN::Prompt;
3658use strict;
3659use warnings;
3660require SVN::Core;
3661use vars qw/$_no_auth_cache $_username/;
3662
3663sub simple {
Eric Wong30d055a2006-11-24 01:38:04 -08003664 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
3665 $may_save = undef if $_no_auth_cache;
3666 $default_username = $_username if defined $_username;
3667 if (defined $default_username && length $default_username) {
3668 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003669 print STDERR "Authentication realm: $realm\n";
3670 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003671 }
3672 $cred->username($default_username);
3673 } else {
Eric Wongd976acf2007-01-04 00:45:03 -08003674 username($cred, $realm, $may_save, $pool);
Eric Wong30d055a2006-11-24 01:38:04 -08003675 }
3676 $cred->password(_read_password("Password for '" .
3677 $cred->username . "': ", $realm));
3678 $cred->may_save($may_save);
3679 $SVN::_Core::SVN_NO_ERROR;
3680}
3681
Eric Wongd976acf2007-01-04 00:45:03 -08003682sub ssl_server_trust {
Eric Wong30d055a2006-11-24 01:38:04 -08003683 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
3684 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003685 print STDERR "Error validating server certificate for '$realm':\n";
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003686 {
3687 no warnings 'once';
3688 # All variables SVN::Auth::SSL::* are used only once,
3689 # so we're shutting up Perl warnings about this.
3690 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
3691 print STDERR " - The certificate is not issued ",
3692 "by a trusted authority. Use the\n",
3693 " fingerprint to validate ",
3694 "the certificate manually!\n";
3695 }
3696 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
3697 print STDERR " - The certificate hostname ",
3698 "does not match.\n";
3699 }
3700 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
3701 print STDERR " - The certificate is not yet valid.\n";
3702 }
3703 if ($failures & $SVN::Auth::SSL::EXPIRED) {
3704 print STDERR " - The certificate has expired.\n";
3705 }
3706 if ($failures & $SVN::Auth::SSL::OTHER) {
3707 print STDERR " - The certificate has ",
3708 "an unknown error.\n";
3709 }
3710 } # no warnings 'once'
Eric Wong6f729592007-01-15 20:15:55 -08003711 printf STDERR
3712 "Certificate information:\n".
Eric Wong30d055a2006-11-24 01:38:04 -08003713 " - Hostname: %s\n".
3714 " - Valid: from %s until %s\n".
3715 " - Issuer: %s\n".
3716 " - Fingerprint: %s\n",
3717 map $cert_info->$_, qw(hostname valid_from valid_until
Eric Wong6f729592007-01-15 20:15:55 -08003718 issuer_dname fingerprint);
Eric Wong30d055a2006-11-24 01:38:04 -08003719 my $choice;
3720prompt:
Eric Wong6f729592007-01-15 20:15:55 -08003721 print STDERR $may_save ?
Eric Wong30d055a2006-11-24 01:38:04 -08003722 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
3723 "(R)eject or accept (t)emporarily? ";
Eric Wong6f729592007-01-15 20:15:55 -08003724 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003725 $choice = lc(substr(<STDIN> || 'R', 0, 1));
3726 if ($choice =~ /^t$/i) {
3727 $cred->may_save(undef);
3728 } elsif ($choice =~ /^r$/i) {
3729 return -1;
3730 } elsif ($may_save && $choice =~ /^p$/i) {
3731 $cred->may_save($may_save);
3732 } else {
3733 goto prompt;
3734 }
3735 $cred->accepted_failures($failures);
3736 $SVN::_Core::SVN_NO_ERROR;
3737}
3738
Eric Wongd976acf2007-01-04 00:45:03 -08003739sub ssl_client_cert {
Eric Wong30d055a2006-11-24 01:38:04 -08003740 my ($cred, $realm, $may_save, $pool) = @_;
3741 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003742 print STDERR "Client certificate filename: ";
3743 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003744 chomp(my $filename = <STDIN>);
3745 $cred->cert_file($filename);
3746 $cred->may_save($may_save);
3747 $SVN::_Core::SVN_NO_ERROR;
3748}
3749
Eric Wongd976acf2007-01-04 00:45:03 -08003750sub ssl_client_cert_pw {
Eric Wong30d055a2006-11-24 01:38:04 -08003751 my ($cred, $realm, $may_save, $pool) = @_;
3752 $may_save = undef if $_no_auth_cache;
3753 $cred->password(_read_password("Password: ", $realm));
3754 $cred->may_save($may_save);
3755 $SVN::_Core::SVN_NO_ERROR;
3756}
3757
Eric Wongd976acf2007-01-04 00:45:03 -08003758sub username {
Eric Wong30d055a2006-11-24 01:38:04 -08003759 my ($cred, $realm, $may_save, $pool) = @_;
3760 $may_save = undef if $_no_auth_cache;
3761 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003762 print STDERR "Authentication realm: $realm\n";
Eric Wong30d055a2006-11-24 01:38:04 -08003763 }
3764 my $username;
3765 if (defined $_username) {
3766 $username = $_username;
3767 } else {
Eric Wong6f729592007-01-15 20:15:55 -08003768 print STDERR "Username: ";
3769 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003770 chomp($username = <STDIN>);
3771 }
3772 $cred->username($username);
3773 $cred->may_save($may_save);
3774 $SVN::_Core::SVN_NO_ERROR;
3775}
3776
3777sub _read_password {
3778 my ($prompt, $realm) = @_;
Eric Wong6f729592007-01-15 20:15:55 -08003779 print STDERR $prompt;
3780 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003781 require Term::ReadKey;
3782 Term::ReadKey::ReadMode('noecho');
3783 my $password = '';
3784 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
3785 last if $key =~ /[\012\015]/; # \n\r
3786 $password .= $key;
3787 }
3788 Term::ReadKey::ReadMode('restore');
Eric Wong6f729592007-01-15 20:15:55 -08003789 print STDERR "\n";
3790 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003791 $password;
3792}
3793
Eric Wong27a1a802006-11-27 21:44:48 -08003794package SVN::Git::Fetcher;
3795use vars qw/@ISA/;
3796use strict;
3797use warnings;
3798use Carp qw/croak/;
Adam Robenffe256f2008-05-23 16:19:41 +02003799use File::Temp qw/tempfile/;
Eric Wong27a1a802006-11-27 21:44:48 -08003800use IO::File qw//;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003801use vars qw/$_ignore_regex/;
Eric Wong27a1a802006-11-27 21:44:48 -08003802
3803# file baton members: path, mode_a, mode_b, pool, fh, blob, base
3804sub new {
Eric Wong8841b372009-02-11 01:56:58 -08003805 my ($class, $git_svn, $switch_path) = @_;
Eric Wong27a1a802006-11-27 21:44:48 -08003806 my $self = SVN::Delta::Editor->new;
3807 bless $self, $class;
Eric Wongdbc6c742009-01-11 16:51:10 -08003808 if (exists $git_svn->{last_commit}) {
3809 $self->{c} = $git_svn->{last_commit};
Eric Wong8841b372009-02-11 01:56:58 -08003810 $self->{empty_symlinks} =
3811 _mark_empty_symlinks($git_svn, $switch_path);
Eric Wongdbc6c742009-01-11 16:51:10 -08003812 }
Ben Jackson0d8bee72009-04-11 10:46:17 -07003813 $self->{ignore_regex} = eval { command_oneline('config', '--get',
3814 "svn-remote.$git_svn->{repo_id}.ignore-paths") };
Eric Wongd2a9a872006-12-12 14:47:00 -08003815 $self->{empty} = {};
3816 $self->{dir_prop} = {};
3817 $self->{file_prop} = {};
3818 $self->{absent_dir} = {};
3819 $self->{absent_file} = {};
Eric Wongef3cfaa2007-01-24 03:30:57 -08003820 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
Eric Wong27a1a802006-11-27 21:44:48 -08003821 $self;
3822}
3823
Eric Wongdbc6c742009-01-11 16:51:10 -08003824# this uses the Ra object, so it must be called before do_{switch,update},
3825# not inside them (when the Git::SVN::Fetcher object is passed) to
3826# do_{switch,update}
3827sub _mark_empty_symlinks {
Eric Wong8841b372009-02-11 01:56:58 -08003828 my ($git_svn, $switch_path) = @_;
Eric Wong4c58a712009-01-31 17:31:12 -08003829 my $bool = Git::config_bool('svn.brokenSymlinkWorkaround');
Eric Wong48679e52009-02-27 19:40:16 -08003830 return {} if (!defined($bool)) || (defined($bool) && ! $bool);
Eric Wong4c58a712009-01-31 17:31:12 -08003831
Eric Wongdbc6c742009-01-11 16:51:10 -08003832 my %ret;
3833 my ($rev, $cmt) = $git_svn->last_rev_commit;
3834 return {} unless ($rev && $cmt);
3835
Eric Wong4c58a712009-01-31 17:31:12 -08003836 # allow the warning to be printed for each revision we fetch to
3837 # ensure the user sees it. The user can also disable the workaround
3838 # on the repository even while git svn is running and the next
3839 # revision fetched will skip this expensive function.
3840 my $printed_warning;
Eric Wongdbc6c742009-01-11 16:51:10 -08003841 chomp(my $empty_blob = `git hash-object -t blob --stdin < /dev/null`);
3842 my ($ls, $ctx) = command_output_pipe(qw/ls-tree -r -z/, $cmt);
3843 local $/ = "\0";
Eric Wong8841b372009-02-11 01:56:58 -08003844 my $pfx = defined($switch_path) ? $switch_path : $git_svn->{path};
Eric Wongdbc6c742009-01-11 16:51:10 -08003845 $pfx .= '/' if length($pfx);
3846 while (<$ls>) {
3847 chomp;
3848 s/\A100644 blob $empty_blob\t//o or next;
Eric Wong4c58a712009-01-31 17:31:12 -08003849 unless ($printed_warning) {
3850 print STDERR "Scanning for empty symlinks, ",
3851 "this may take a while if you have ",
3852 "many empty files\n",
3853 "You may disable this with `",
3854 "git config svn.brokenSymlinkWorkaround ",
3855 "false'.\n",
3856 "This may be done in a different ",
3857 "terminal without restarting ",
3858 "git svn\n";
3859 $printed_warning = 1;
3860 }
Eric Wongdbc6c742009-01-11 16:51:10 -08003861 my $path = $_;
3862 my (undef, $props) =
3863 $git_svn->ra->get_file($pfx.$path, $rev, undef);
3864 if ($props->{'svn:special'}) {
3865 $ret{$path} = 1;
3866 }
3867 }
3868 command_close_pipe($ls, $ctx);
3869 \%ret;
3870}
3871
Eric Wongb03a71a2009-01-11 18:23:38 -08003872# returns true if a given path is inside a ".git" directory
3873sub in_dot_git {
3874 $_[0] =~ m{(?:^|/)\.git(?:/|$)};
3875}
3876
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003877# return value: 0 -- don't ignore, 1 -- ignore
3878sub is_path_ignored {
Ben Jackson0d8bee72009-04-11 10:46:17 -07003879 my ($self, $path) = @_;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003880 return 1 if in_dot_git($path);
Ben Jackson0d8bee72009-04-11 10:46:17 -07003881 return 1 if defined($self->{ignore_regex}) &&
3882 $path =~ m!$self->{ignore_regex}!;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003883 return 0 unless defined($_ignore_regex);
3884 return 1 if $path =~ m!$_ignore_regex!o;
3885 return 0;
3886}
3887
Eric Wong8b8fc062007-01-22 11:44:57 -08003888sub set_path_strip {
3889 my ($self, $path) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003890 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
Eric Wong8b8fc062007-01-22 11:44:57 -08003891}
3892
Eric Wongd2a9a872006-12-12 14:47:00 -08003893sub open_root {
3894 { path => '' };
3895}
3896
3897sub open_directory {
3898 my ($self, $path, $pb, $rev) = @_;
3899 { path => $path };
3900}
3901
Eric Wong706587f2007-01-18 17:50:01 -08003902sub git_path {
3903 my ($self, $path) = @_;
Eric Wong2b27f6c2007-01-28 04:59:05 -08003904 if ($self->{path_strip}) {
3905 $path =~ s!$self->{path_strip}!! or
3906 die "Failed to strip path '$path' ($self->{path_strip})\n";
3907 }
Eric Wong706587f2007-01-18 17:50:01 -08003908 $path;
3909}
3910
Eric Wong27a1a802006-11-27 21:44:48 -08003911sub delete_entry {
3912 my ($self, $path, $rev, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003913 return undef if $self->is_path_ignored($path);
Eric Wong4a87db02007-01-04 01:38:18 -08003914
Eric Wong706587f2007-01-18 17:50:01 -08003915 my $gpath = $self->git_path($path);
Eric Wong8a603772007-01-31 02:45:50 -08003916 return undef if ($gpath eq '');
3917
Eric Wong4a87db02007-01-04 01:38:18 -08003918 # remove entire directories.
Eric Wong4f821012009-03-28 22:10:08 -07003919 my ($tree) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
3920 =~ /\A040000 tree ([a-f\d]{40})\t\Q$gpath\E\0/);
3921 if ($tree) {
Eric Wong4a87db02007-01-04 01:38:18 -08003922 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3923 -r --name-only -z/,
Eric Wong4f821012009-03-28 22:10:08 -07003924 $tree);
Eric Wong4a87db02007-01-04 01:38:18 -08003925 local $/ = "\0";
3926 while (<$ls>) {
Eric Wongef3cfaa2007-01-24 03:30:57 -08003927 chomp;
Eric Wong4f821012009-03-28 22:10:08 -07003928 my $rmpath = "$gpath/$_";
3929 $self->{gii}->remove($rmpath);
3930 print "\tD\t$rmpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003931 }
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003932 print "\tD\t$gpath/\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003933 command_close_pipe($ls, $ctx);
Eric Wong4a87db02007-01-04 01:38:18 -08003934 } else {
Eric Wongef3cfaa2007-01-24 03:30:57 -08003935 $self->{gii}->remove($gpath);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003936 print "\tD\t$gpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003937 }
Eric Wongf9ad77a2009-12-07 20:49:38 -08003938 $self->{empty}->{$path} = 0;
Eric Wong27a1a802006-11-27 21:44:48 -08003939 undef;
3940}
3941
3942sub open_file {
3943 my ($self, $path, $pb, $rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08003944 my ($mode, $blob);
3945
Ben Jackson0d8bee72009-04-11 10:46:17 -07003946 goto out if $self->is_path_ignored($path);
Eric Wongb03a71a2009-01-11 18:23:38 -08003947
Eric Wong706587f2007-01-18 17:50:01 -08003948 my $gpath = $self->git_path($path);
Eric Wong4f821012009-03-28 22:10:08 -07003949 ($mode, $blob) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
3950 =~ /\A(\d{6}) blob ([a-f\d]{40})\t\Q$gpath\E\0/);
Eric Wong006ede52006-12-08 01:55:19 -08003951 unless (defined $mode && defined $blob) {
3952 die "$path was not found in commit $self->{c} (r$rev)\n";
3953 }
Eric Wongdbc6c742009-01-11 16:51:10 -08003954 if ($mode eq '100644' && $self->{empty_symlinks}->{$path}) {
3955 $mode = '120000';
3956 }
Eric Wongb03a71a2009-01-11 18:23:38 -08003957out:
Eric Wong27a1a802006-11-27 21:44:48 -08003958 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
Eric Wong0864e3b2006-11-28 02:50:17 -08003959 pool => SVN::Pool->new, action => 'M' };
Eric Wong27a1a802006-11-27 21:44:48 -08003960}
3961
3962sub add_file {
3963 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08003964 my $mode;
3965
Ben Jackson0d8bee72009-04-11 10:46:17 -07003966 if (!$self->is_path_ignored($path)) {
Eric Wongb03a71a2009-01-11 18:23:38 -08003967 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3968 delete $self->{empty}->{$dir};
3969 $mode = '100644';
3970 }
3971 { path => $path, mode_a => $mode, mode_b => $mode,
Eric Wong0864e3b2006-11-28 02:50:17 -08003972 pool => SVN::Pool->new, action => 'A' };
Eric Wong27a1a802006-11-27 21:44:48 -08003973}
3974
Eric Wongd2a9a872006-12-12 14:47:00 -08003975sub add_directory {
3976 my ($self, $path, $cp_path, $cp_rev) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003977 goto out if $self->is_path_ignored($path);
Eric Wong12a6d752007-12-14 08:39:09 -08003978 my $gpath = $self->git_path($path);
3979 if ($gpath eq '') {
3980 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3981 -r --name-only -z/,
3982 $self->{c});
3983 local $/ = "\0";
3984 while (<$ls>) {
3985 chomp;
3986 $self->{gii}->remove($_);
3987 print "\tD\t$_\n" unless $::_q;
3988 }
3989 command_close_pipe($ls, $ctx);
3990 $self->{empty}->{$path} = 0;
3991 }
Eric Wongd2a9a872006-12-12 14:47:00 -08003992 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3993 delete $self->{empty}->{$dir};
3994 $self->{empty}->{$path} = 1;
Eric Wongb03a71a2009-01-11 18:23:38 -08003995out:
Eric Wongd2a9a872006-12-12 14:47:00 -08003996 { path => $path };
3997}
3998
3999sub change_dir_prop {
4000 my ($self, $db, $prop, $value) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004001 return undef if $self->is_path_ignored($db->{path});
Eric Wongd2a9a872006-12-12 14:47:00 -08004002 $self->{dir_prop}->{$db->{path}} ||= {};
4003 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
4004 undef;
4005}
4006
4007sub absent_directory {
4008 my ($self, $path, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004009 return undef if $self->is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08004010 $self->{absent_dir}->{$pb->{path}} ||= [];
4011 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
4012 undef;
4013}
4014
4015sub absent_file {
4016 my ($self, $path, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004017 return undef if $self->is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08004018 $self->{absent_file}->{$pb->{path}} ||= [];
4019 push @{$self->{absent_file}->{$pb->{path}}}, $path;
4020 undef;
4021}
4022
Eric Wong27a1a802006-11-27 21:44:48 -08004023sub change_file_prop {
4024 my ($self, $fb, $prop, $value) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004025 return undef if $self->is_path_ignored($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08004026 if ($prop eq 'svn:executable') {
4027 if ($fb->{mode_b} != 120000) {
4028 $fb->{mode_b} = defined $value ? 100755 : 100644;
4029 }
4030 } elsif ($prop eq 'svn:special') {
4031 $fb->{mode_b} = defined $value ? 120000 : 100644;
Eric Wongd2a9a872006-12-12 14:47:00 -08004032 } else {
4033 $self->{file_prop}->{$fb->{path}} ||= {};
4034 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
Eric Wong27a1a802006-11-27 21:44:48 -08004035 }
4036 undef;
4037}
4038
4039sub apply_textdelta {
4040 my ($self, $fb, $exp) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004041 return undef if $self->is_path_ignored($fb->{path});
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004042 my $fh = $::_repository->temp_acquire('svn_delta');
Eric Wong27a1a802006-11-27 21:44:48 -08004043 # $fh gets auto-closed() by SVN::TxDelta::apply(),
4044 # (but $base does not,) so dup() it for reading in close_file
4045 open my $dup, '<&', $fh or croak $!;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004046 my $base = $::_repository->temp_acquire('git_blob');
Eric Wongb03a71a2009-01-11 18:23:38 -08004047
Eric Wong27a1a802006-11-27 21:44:48 -08004048 if ($fb->{blob}) {
Eric Wongbaf5fa82009-01-11 16:51:11 -08004049 my ($base_is_link, $size);
4050
Eric Wongdbc6c742009-01-11 16:51:10 -08004051 if ($fb->{mode_a} eq '120000' &&
4052 ! $self->{empty_symlinks}->{$fb->{path}}) {
4053 print $base 'link ' or die "print $!\n";
Eric Wongbaf5fa82009-01-11 16:51:11 -08004054 $base_is_link = 1;
Eric Wongdbc6c742009-01-11 16:51:10 -08004055 }
Eric Wongbaf5fa82009-01-11 16:51:11 -08004056 retry:
4057 $size = $::_repository->cat_blob($fb->{blob}, $base);
Junio C Hamanod683a0e2008-05-27 23:33:22 -07004058 die "Failed to read object $fb->{blob}" if ($size < 0);
Eric Wong27a1a802006-11-27 21:44:48 -08004059
4060 if (defined $exp) {
4061 seek $base, 0, 0 or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08004062 my $got = ::md5sum($base);
Eric Wongbaf5fa82009-01-11 16:51:11 -08004063 if ($got ne $exp) {
4064 my $err = "Checksum mismatch: ".
4065 "$fb->{path} $fb->{blob}\n" .
4066 "expected: $exp\n" .
4067 " got: $got\n";
4068 if ($base_is_link) {
4069 warn $err,
4070 "Retrying... (possibly ",
4071 "a bad symlink from SVN)\n";
4072 $::_repository->temp_reset($base);
4073 $base_is_link = 0;
4074 goto retry;
4075 }
4076 die $err;
4077 }
Eric Wong27a1a802006-11-27 21:44:48 -08004078 }
4079 }
4080 seek $base, 0, 0 or croak $!;
Marcus Griep0b191382008-08-12 12:00:53 -04004081 $fb->{fh} = $fh;
Eric Wong27a1a802006-11-27 21:44:48 -08004082 $fb->{base} = $base;
Marcus Griep0b191382008-08-12 12:00:53 -04004083 [ SVN::TxDelta::apply($base, $dup, undef, $fb->{path}, $fb->{pool}) ];
Eric Wong27a1a802006-11-27 21:44:48 -08004084}
4085
4086sub close_file {
4087 my ($self, $fb, $exp) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004088 return undef if $self->is_path_ignored($fb->{path});
Eric Wongb03a71a2009-01-11 18:23:38 -08004089
Eric Wong27a1a802006-11-27 21:44:48 -08004090 my $hash;
Eric Wong706587f2007-01-18 17:50:01 -08004091 my $path = $self->git_path($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08004092 if (my $fh = $fb->{fh}) {
Eric Wong7faf0682007-05-27 15:59:01 -07004093 if (defined $exp) {
4094 seek($fh, 0, 0) or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08004095 my $got = ::md5sum($fh);
Eric Wong7faf0682007-05-27 15:59:01 -07004096 if ($got ne $exp) {
4097 die "Checksum mismatch: $path\n",
4098 "expected: $exp\n got: $got\n";
4099 }
4100 }
Eric Wong27a1a802006-11-27 21:44:48 -08004101 if ($fb->{mode_b} == 120000) {
Marcus Griep510b0942008-08-12 12:45:39 -04004102 sysseek($fh, 0, 0) or croak $!;
Eric Wongdbc6c742009-01-11 16:51:10 -08004103 my $rd = sysread($fh, my $buf, 5);
Marcus Griep510b0942008-08-12 12:45:39 -04004104
Eric Wongdbc6c742009-01-11 16:51:10 -08004105 if (!defined $rd) {
4106 croak "sysread: $!\n";
4107 } elsif ($rd == 0) {
Marcus Griep510b0942008-08-12 12:45:39 -04004108 warn "$path has mode 120000",
Eric Wongdbc6c742009-01-11 16:51:10 -08004109 " but it points to nothing\n",
4110 "converting to an empty file with mode",
4111 " 100644\n";
4112 $fb->{mode_b} = '100644';
4113 } elsif ($buf ne 'link ') {
4114 warn "$path has mode 120000",
4115 " but is not a link\n";
Marcus Griep510b0942008-08-12 12:45:39 -04004116 } else {
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004117 my $tmp_fh = $::_repository->temp_acquire(
4118 'svn_hash');
Marcus Griep510b0942008-08-12 12:45:39 -04004119 my $res;
4120 while ($res = sysread($fh, my $str, 1024)) {
4121 my $out = syswrite($tmp_fh, $str, $res);
4122 defined($out) && $out == $res
4123 or croak("write ",
Marcus Griep836ff952008-09-08 12:53:01 -04004124 Git::temp_path($tmp_fh),
Marcus Griep510b0942008-08-12 12:45:39 -04004125 ": $!\n");
4126 }
4127 defined $res or croak $!;
4128
4129 ($fh, $tmp_fh) = ($tmp_fh, $fh);
4130 Git::temp_release($tmp_fh, 1);
Eric Wong7fc35e02007-12-19 00:06:45 -08004131 }
Eric Wong27a1a802006-11-27 21:44:48 -08004132 }
Adam Robenffe256f2008-05-23 16:19:41 +02004133
Marcus Griep0b191382008-08-12 12:00:53 -04004134 $hash = $::_repository->hash_and_insert_object(
Marcus Griep836ff952008-09-08 12:53:01 -04004135 Git::temp_path($fh));
Eric Wong27a1a802006-11-27 21:44:48 -08004136 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
Marcus Griep0b191382008-08-12 12:00:53 -04004137
4138 Git::temp_release($fb->{base}, 1);
Marcus Griep510b0942008-08-12 12:45:39 -04004139 Git::temp_release($fh, 1);
Eric Wong27a1a802006-11-27 21:44:48 -08004140 } else {
4141 $hash = $fb->{blob} or die "no blob information\n";
4142 }
4143 $fb->{pool}->clear;
Eric Wongef3cfaa2007-01-24 03:30:57 -08004144 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
Eric Wong9e3cdbd2007-02-09 12:23:47 -08004145 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
Eric Wong27a1a802006-11-27 21:44:48 -08004146 undef;
4147}
4148
4149sub abort_edit {
4150 my $self = shift;
Eric Wongef3cfaa2007-01-24 03:30:57 -08004151 $self->{nr} = $self->{gii}->{nr};
4152 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08004153 $self->SUPER::abort_edit(@_);
4154}
4155
4156sub close_edit {
4157 my $self = shift;
Eric Wongdad73c02006-11-28 14:06:05 -08004158 $self->{git_commit_ok} = 1;
Eric Wongef3cfaa2007-01-24 03:30:57 -08004159 $self->{nr} = $self->{gii}->{nr};
4160 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08004161 $self->SUPER::close_edit(@_);
4162}
Eric Wong1a82e792006-06-16 02:55:13 -07004163
Eric Wonga5e0ced2006-06-12 15:23:48 -07004164package SVN::Git::Editor;
Eric Wong24e22aa2007-01-29 00:07:49 -08004165use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004166use strict;
4167use warnings;
4168use Carp qw/croak/;
4169use IO::File;
4170
4171sub new {
Eric Wong61395352007-01-27 14:33:08 -08004172 my ($class, $opts) = @_;
4173 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
4174 die "$_ required!\n" unless (defined $opts->{$_});
Eric Wonga5e0ced2006-06-12 15:23:48 -07004175 }
Eric Wong61395352007-01-27 14:33:08 -08004176
4177 my $pool = SVN::Pool->new;
4178 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
4179 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
4180 $opts->{r}, $mods);
4181
4182 # $opts->{ra} functions should not be used after this:
4183 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
4184 $opts->{editor_cb}, $pool);
4185 my $self = SVN::Delta::Editor->new(@ce, $pool);
4186 bless $self, $class;
4187 foreach (qw/svn_path r tree_a tree_b/) {
4188 $self->{$_} = $opts->{$_};
4189 }
4190 $self->{url} = $opts->{ra}->{url};
4191 $self->{mods} = $mods;
4192 $self->{types} = $types;
4193 $self->{pool} = $pool;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004194 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
4195 $self->{rm} = { };
Eric Wongd3a840d2007-01-26 01:32:45 -08004196 $self->{path_prefix} = length $self->{svn_path} ?
4197 "$self->{svn_path}/" : '';
Brad King128de652008-07-25 11:32:37 -04004198 $self->{config} = $opts->{config};
Eric Wonga5e0ced2006-06-12 15:23:48 -07004199 return $self;
4200}
4201
Eric Wong61395352007-01-27 14:33:08 -08004202sub generate_diff {
4203 my ($tree_a, $tree_b) = @_;
4204 my @diff_tree = qw(diff-tree -z -r);
Eric Wong24e22aa2007-01-29 00:07:49 -08004205 if ($_cp_similarity) {
4206 push @diff_tree, "-C$_cp_similarity";
Eric Wong61395352007-01-27 14:33:08 -08004207 } else {
4208 push @diff_tree, '-C';
4209 }
Eric Wong24e22aa2007-01-29 00:07:49 -08004210 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
4211 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
Eric Wong61395352007-01-27 14:33:08 -08004212 push @diff_tree, $tree_a, $tree_b;
4213 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
4214 local $/ = "\0";
4215 my $state = 'meta';
4216 my @mods;
4217 while (<$diff_fh>) {
4218 chomp $_; # this gets rid of the trailing "\0"
4219 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02004220 ($::sha1)\s($::sha1)\s
Eric Wong61395352007-01-27 14:33:08 -08004221 ([MTCRAD])\d*$/xo) {
4222 push @mods, { mode_a => $1, mode_b => $2,
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02004223 sha1_a => $3, sha1_b => $4,
4224 chg => $5 };
4225 if ($5 =~ /^(?:C|R)$/) {
Eric Wong61395352007-01-27 14:33:08 -08004226 $state = 'file_a';
4227 } else {
4228 $state = 'file_b';
4229 }
4230 } elsif ($state eq 'file_a') {
4231 my $x = $mods[$#mods] or croak "Empty array\n";
4232 if ($x->{chg} !~ /^(?:C|R)$/) {
4233 croak "Error parsing $_, $x->{chg}\n";
4234 }
4235 $x->{file_a} = $_;
4236 $state = 'file_b';
4237 } elsif ($state eq 'file_b') {
4238 my $x = $mods[$#mods] or croak "Empty array\n";
4239 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
4240 croak "Error parsing $_, $x->{chg}\n";
4241 }
4242 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
4243 croak "Error parsing $_, $x->{chg}\n";
4244 }
4245 $x->{file_b} = $_;
4246 $state = 'meta';
4247 } else {
4248 croak "Error parsing $_\n";
4249 }
4250 }
4251 command_close_pipe($diff_fh, $ctx);
4252 \@mods;
4253}
4254
4255sub check_diff_paths {
4256 my ($ra, $pfx, $rev, $mods) = @_;
4257 my %types;
4258 $pfx .= '/' if length $pfx;
4259
4260 sub type_diff_paths {
4261 my ($ra, $types, $path, $rev) = @_;
4262 my @p = split m#/+#, $path;
4263 my $c = shift @p;
4264 unless (defined $types->{$c}) {
4265 $types->{$c} = $ra->check_path($c, $rev);
4266 }
4267 while (@p) {
4268 $c .= '/' . shift @p;
4269 next if defined $types->{$c};
4270 $types->{$c} = $ra->check_path($c, $rev);
4271 }
4272 }
4273
4274 foreach my $m (@$mods) {
4275 foreach my $f (qw/file_a file_b/) {
4276 next unless defined $m->{$f};
4277 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
4278 if (length $pfx.$dir && ! defined $types{$dir}) {
4279 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
4280 }
4281 }
4282 }
4283 \%types;
4284}
4285
Eric Wonga5e0ced2006-06-12 15:23:48 -07004286sub split_path {
4287 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
4288}
4289
4290sub repo_path {
Eric Wongd3a840d2007-01-26 01:32:45 -08004291 my ($self, $path) = @_;
4292 $self->{path_prefix}.(defined $path ? $path : '');
Eric Wonga5e0ced2006-06-12 15:23:48 -07004293}
4294
4295sub url_path {
4296 my ($self, $path) = @_;
Eric Wong29633bb2007-07-15 21:53:50 -07004297 if ($self->{url} =~ m#^https?://#) {
Eric Wong884cce52009-07-25 02:29:28 -07004298 $path =~ s!([^~a-zA-Z0-9_./-])!uc sprintf("%%%02x",ord($1))!eg;
Eric Wong29633bb2007-07-15 21:53:50 -07004299 }
Eric Wong6e8548c2007-01-27 01:32:00 -08004300 $self->{url} . '/' . $self->repo_path($path);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004301}
4302
4303sub rmdirs {
Eric Wong61395352007-01-27 14:33:08 -08004304 my ($self) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004305 my $rm = $self->{rm};
4306 delete $rm->{''}; # we never delete the url we're tracking
4307 return unless %$rm;
4308
4309 foreach (keys %$rm) {
4310 my @d = split m#/#, $_;
4311 my $c = shift @d;
4312 $rm->{$c} = 1;
4313 while (@d) {
4314 $c .= '/' . shift @d;
4315 $rm->{$c} = 1;
4316 }
4317 }
4318 delete $rm->{$self->{svn_path}};
4319 delete $rm->{''}; # we never delete the url we're tracking
4320 return unless %$rm;
4321
Eric Wong61395352007-01-27 14:33:08 -08004322 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
4323 $self->{tree_b});
Eric Wonga5e0ced2006-06-12 15:23:48 -07004324 local $/ = "\0";
4325 while (<$fh>) {
4326 chomp;
Eric Wong747fa122006-11-24 22:38:17 -08004327 my @dn = split m#/#, $_;
Eric Wongc07eee12006-06-19 17:59:35 -07004328 while (pop @dn) {
4329 delete $rm->{join '/', @dn};
4330 }
4331 unless (%$rm) {
Eric Wong22600a22007-02-01 13:12:26 -08004332 close $fh;
Eric Wongc07eee12006-06-19 17:59:35 -07004333 return;
4334 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07004335 }
Eric Wongaef4e922006-12-15 10:59:54 -08004336 command_close_pipe($fh, $ctx);
Eric Wongc07eee12006-06-19 17:59:35 -07004337
Eric Wonga5e0ced2006-06-12 15:23:48 -07004338 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
4339 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
4340 $self->close_directory($bat->{$d}, $p);
4341 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
Eric Wong44320b92007-01-13 22:35:53 -08004342 print "\tD+\t$d/\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004343 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
4344 delete $bat->{$d};
4345 }
4346}
4347
4348sub open_or_add_dir {
4349 my ($self, $full_path, $baton) = @_;
Eric Wong6e8548c2007-01-27 01:32:00 -08004350 my $t = $self->{types}->{$full_path};
4351 if (!defined $t) {
4352 die "$full_path not known in r$self->{r} or we have a bug!\n";
4353 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004354 {
4355 no warnings 'once';
4356 # SVN::Node::none and SVN::Node::file are used only once,
4357 # so we're shutting up Perl's warnings about them.
4358 if ($t == $SVN::Node::none) {
4359 return $self->add_directory($full_path, $baton,
4360 undef, -1, $self->{pool});
4361 } elsif ($t == $SVN::Node::dir) {
4362 return $self->open_directory($full_path, $baton,
4363 $self->{r}, $self->{pool});
4364 } # no warnings 'once'
4365 print STDERR "$full_path already exists in repository at ",
4366 "r$self->{r} and it is not a directory (",
4367 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
4368 } # no warnings 'once'
Eric Wonga5e0ced2006-06-12 15:23:48 -07004369 exit 1;
4370}
4371
4372sub ensure_path {
4373 my ($self, $path) = @_;
4374 my $bat = $self->{bat};
Eric Wong6e8548c2007-01-27 01:32:00 -08004375 my $repo_path = $self->repo_path($path);
4376 return $bat->{''} unless (length $repo_path);
4377 my @p = split m#/+#, $repo_path;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004378 my $c = shift @p;
4379 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
4380 while (@p) {
4381 my $c0 = $c;
4382 $c .= '/' . shift @p;
4383 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
4384 }
4385 return $bat->{$c};
4386}
4387
Brad King128de652008-07-25 11:32:37 -04004388# Subroutine to convert a globbing pattern to a regular expression.
4389# From perl cookbook.
4390sub glob2pat {
4391 my $globstr = shift;
4392 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
4393 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
4394 return '^' . $globstr . '$';
4395}
4396
4397sub check_autoprop {
4398 my ($self, $pattern, $properties, $file, $fbat) = @_;
4399 # Convert the globbing pattern to a regular expression.
4400 my $regex = glob2pat($pattern);
4401 # Check if the pattern matches the file name.
4402 if($file =~ m/($regex)/) {
4403 # Parse the list of properties to set.
4404 my @props = split(/;/, $properties);
4405 foreach my $prop (@props) {
4406 # Parse 'name=value' syntax and set the property.
4407 if ($prop =~ /([^=]+)=(.*)/) {
4408 my ($n,$v) = ($1,$2);
4409 for ($n, $v) {
4410 s/^\s+//; s/\s+$//;
4411 }
4412 $self->change_file_prop($fbat, $n, $v);
4413 }
4414 }
4415 }
4416}
4417
4418sub apply_autoprops {
4419 my ($self, $file, $fbat) = @_;
4420 my $conf_t = ${$self->{config}}{'config'};
4421 no warnings 'once';
4422 # Check [miscellany]/enable-auto-props in svn configuration.
4423 if (SVN::_Core::svn_config_get_bool(
4424 $conf_t,
4425 $SVN::_Core::SVN_CONFIG_SECTION_MISCELLANY,
4426 $SVN::_Core::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS,
4427 0)) {
4428 # Auto-props are enabled. Enumerate them to look for matches.
4429 my $callback = sub {
4430 $self->check_autoprop($_[0], $_[1], $file, $fbat);
4431 };
4432 SVN::_Core::svn_config_enumerate(
4433 $conf_t,
4434 $SVN::_Core::SVN_CONFIG_SECTION_AUTO_PROPS,
4435 $callback);
4436 }
4437}
4438
Eric Wonga5e0ced2006-06-12 15:23:48 -07004439sub A {
Eric Wong44320b92007-01-13 22:35:53 -08004440 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004441 my ($dir, $file) = split_path($m->{file_b});
4442 my $pbat = $self->ensure_path($dir);
4443 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4444 undef, -1);
Eric Wong44320b92007-01-13 22:35:53 -08004445 print "\tA\t$m->{file_b}\n" unless $::_q;
Brad King128de652008-07-25 11:32:37 -04004446 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004447 $self->chg_file($fbat, $m);
4448 $self->close_file($fbat,undef,$self->{pool});
4449}
4450
4451sub C {
Eric Wong44320b92007-01-13 22:35:53 -08004452 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004453 my ($dir, $file) = split_path($m->{file_b});
4454 my $pbat = $self->ensure_path($dir);
4455 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4456 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08004457 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004458 $self->chg_file($fbat, $m);
4459 $self->close_file($fbat,undef,$self->{pool});
4460}
4461
4462sub delete_entry {
4463 my ($self, $path, $pbat) = @_;
4464 my $rpath = $self->repo_path($path);
4465 my ($dir, $file) = split_path($rpath);
4466 $self->{rm}->{$dir} = 1;
4467 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
4468}
4469
4470sub R {
Eric Wong44320b92007-01-13 22:35:53 -08004471 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004472 my ($dir, $file) = split_path($m->{file_b});
4473 my $pbat = $self->ensure_path($dir);
4474 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4475 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08004476 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Paul Talacko7c4d0212008-09-06 18:50:38 -07004477 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004478 $self->chg_file($fbat, $m);
4479 $self->close_file($fbat,undef,$self->{pool});
4480
4481 ($dir, $file) = split_path($m->{file_a});
4482 $pbat = $self->ensure_path($dir);
4483 $self->delete_entry($m->{file_a}, $pbat);
4484}
4485
4486sub M {
Eric Wong44320b92007-01-13 22:35:53 -08004487 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004488 my ($dir, $file) = split_path($m->{file_b});
4489 my $pbat = $self->ensure_path($dir);
4490 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
4491 $pbat,$self->{r},$self->{pool});
Eric Wong44320b92007-01-13 22:35:53 -08004492 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004493 $self->chg_file($fbat, $m);
4494 $self->close_file($fbat,undef,$self->{pool});
4495}
4496
4497sub T { shift->M(@_) }
4498
4499sub change_file_prop {
4500 my ($self, $fbat, $pname, $pval) = @_;
4501 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
4502}
4503
Florian Weimer214a34d2008-08-31 17:45:04 +02004504sub _chg_file_get_blob ($$$$) {
4505 my ($self, $fbat, $m, $which) = @_;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004506 my $fh = $::_repository->temp_acquire("git_blob_$which");
Florian Weimer214a34d2008-08-31 17:45:04 +02004507 if ($m->{"mode_$which"} =~ /^120/) {
4508 print $fh 'link ' or croak $!;
4509 $self->change_file_prop($fbat,'svn:special','*');
4510 } elsif ($m->{mode_a} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
4511 $self->change_file_prop($fbat,'svn:special',undef);
4512 }
4513 my $blob = $m->{"sha1_$which"};
4514 return ($fh,) if ($blob =~ /^0{40}$/);
4515 my $size = $::_repository->cat_blob($blob, $fh);
4516 croak "Failed to read object $blob" if ($size < 0);
4517 $fh->flush == 0 or croak $!;
4518 seek $fh, 0, 0 or croak $!;
4519
4520 my $exp = ::md5sum($fh);
4521 seek $fh, 0, 0 or croak $!;
4522 return ($fh, $exp);
4523}
4524
Eric Wonga5e0ced2006-06-12 15:23:48 -07004525sub chg_file {
4526 my ($self, $fbat, $m) = @_;
4527 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
4528 $self->change_file_prop($fbat,'svn:executable','*');
4529 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
4530 $self->change_file_prop($fbat,'svn:executable',undef);
4531 }
Florian Weimer8598db932008-08-31 17:47:09 +02004532 my ($fh_a, $exp_a) = _chg_file_get_blob $self, $fbat, $m, 'a';
4533 my ($fh_b, $exp_b) = _chg_file_get_blob $self, $fbat, $m, 'b';
Eric Wongf7197df2006-10-14 15:48:35 -07004534 my $pool = SVN::Pool->new;
Florian Weimer8598db932008-08-31 17:47:09 +02004535 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
4536 if (-s $fh_a) {
4537 my $txstream = SVN::TxDelta::new ($fh_a, $fh_b, $pool);
Eric Wong991255c2008-08-31 19:45:07 -07004538 my $res = SVN::TxDelta::send_txstream($txstream, @$atd, $pool);
4539 if (defined $res) {
4540 die "Unexpected result from send_txstream: $res\n",
4541 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
4542 }
Florian Weimer8598db932008-08-31 17:47:09 +02004543 } else {
4544 my $got = SVN::TxDelta::send_stream($fh_b, @$atd, $pool);
4545 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
4546 if ($got ne $exp_b);
4547 }
4548 Git::temp_release($fh_b, 1);
4549 Git::temp_release($fh_a, 1);
Eric Wongf7197df2006-10-14 15:48:35 -07004550 $pool->clear;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004551}
4552
4553sub D {
Eric Wong44320b92007-01-13 22:35:53 -08004554 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004555 my ($dir, $file) = split_path($m->{file_b});
4556 my $pbat = $self->ensure_path($dir);
Eric Wong44320b92007-01-13 22:35:53 -08004557 print "\tD\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004558 $self->delete_entry($m->{file_b}, $pbat);
4559}
4560
4561sub close_edit {
4562 my ($self) = @_;
4563 my ($p,$bat) = ($self->{pool}, $self->{bat});
4564 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
Eric Wong64427542007-05-19 02:58:37 -07004565 next if $_ eq '';
Eric Wonga5e0ced2006-06-12 15:23:48 -07004566 $self->close_directory($bat->{$_}, $p);
4567 }
Eric Wong64427542007-05-19 02:58:37 -07004568 $self->close_directory($bat->{''}, $p);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004569 $self->SUPER::close_edit($p);
4570 $p->clear;
4571}
4572
4573sub abort_edit {
4574 my ($self) = @_;
4575 $self->SUPER::abort_edit($self->{pool});
Eric Wong61395352007-01-27 14:33:08 -08004576}
4577
4578sub DESTROY {
4579 my $self = shift;
4580 $self->SUPER::DESTROY(@_);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004581 $self->{pool}->clear;
4582}
4583
Eric Wong44320b92007-01-13 22:35:53 -08004584# this drives the editor
4585sub apply_diff {
Eric Wong61395352007-01-27 14:33:08 -08004586 my ($self) = @_;
4587 my $mods = $self->{mods};
Eric Wong44320b92007-01-13 22:35:53 -08004588 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
Eric Wong6e8548c2007-01-27 01:32:00 -08004589 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
Eric Wong44320b92007-01-13 22:35:53 -08004590 my $f = $m->{chg};
4591 if (defined $o{$f}) {
4592 $self->$f($m);
4593 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004594 fatal("Invalid change type: $f");
Eric Wong44320b92007-01-13 22:35:53 -08004595 }
4596 }
Eric Wong24e22aa2007-01-29 00:07:49 -08004597 $self->rmdirs if $_rmdir;
Eric Wong6e8548c2007-01-27 01:32:00 -08004598 if (@$mods == 0) {
Eric Wong44320b92007-01-13 22:35:53 -08004599 $self->abort_edit;
4600 } else {
4601 $self->close_edit;
4602 }
Eric Wong6e8548c2007-01-27 01:32:00 -08004603 return scalar @$mods;
Eric Wong44320b92007-01-13 22:35:53 -08004604}
4605
Eric Wongd81bf822007-01-10 01:22:38 -08004606package Git::SVN::Ra;
Eric Wong6af1db42007-02-14 16:04:10 -08004607use vars qw/@ISA $config_dir $_log_window_size/;
Eric Wongd81bf822007-01-10 01:22:38 -08004608use strict;
4609use warnings;
Eric Wonga51cdb02007-09-07 04:00:40 -07004610my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
Eric Wongd81bf822007-01-10 01:22:38 -08004611
4612BEGIN {
4613 # enforce temporary pool usage for some simple functions
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004614 no strict 'refs';
Eric Wongbf8a40b2009-01-25 15:35:52 -08004615 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root
4616 get_file/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004617 my $SUPER = "SUPER::$f";
4618 *$f = sub {
4619 my $self = shift;
4620 my $pool = SVN::Pool->new;
4621 my @ret = $self->$SUPER(@_,$pool);
4622 $pool->clear;
4623 wantarray ? @ret : $ret[0];
4624 };
Eric Wongd81bf822007-01-10 01:22:38 -08004625 }
Eric Wongd81bf822007-01-10 01:22:38 -08004626}
4627
Steven Walter9ff74e92007-09-28 13:24:19 -04004628sub _auth_providers () {
4629 [
4630 SVN::Client::get_simple_provider(),
4631 SVN::Client::get_ssl_server_trust_file_provider(),
4632 SVN::Client::get_simple_prompt_provider(
4633 \&Git::SVN::Prompt::simple, 2),
4634 SVN::Client::get_ssl_client_cert_file_provider(),
4635 SVN::Client::get_ssl_client_cert_prompt_provider(
4636 \&Git::SVN::Prompt::ssl_client_cert, 2),
Sebastian Noack77266e92008-02-25 15:56:28 +01004637 SVN::Client::get_ssl_client_cert_pw_file_provider(),
Steven Walter9ff74e92007-09-28 13:24:19 -04004638 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
4639 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
4640 SVN::Client::get_username_provider(),
4641 SVN::Client::get_ssl_server_trust_prompt_provider(
4642 \&Git::SVN::Prompt::ssl_server_trust),
4643 SVN::Client::get_username_prompt_provider(
4644 \&Git::SVN::Prompt::username, 2)
4645 ]
4646}
4647
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004648sub escape_uri_only {
4649 my ($uri) = @_;
4650 my @tmp;
4651 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -07004652 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004653 push @tmp, $_;
4654 }
4655 join('/', @tmp);
4656}
4657
4658sub escape_url {
4659 my ($url) = @_;
4660 if ($url =~ m#^(https?)://([^/]+)(.*)$#) {
4661 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
4662 $url = "$scheme://$domain$uri";
4663 }
4664 $url;
4665}
4666
Eric Wongd81bf822007-01-10 01:22:38 -08004667sub new {
4668 my ($class, $url) = @_;
Eric Wongf6f09872007-01-18 18:22:18 -08004669 $url =~ s!/+$!!;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004670 return $RA if ($RA && $RA->{url} eq $url);
Eric Wongf6f09872007-01-18 18:22:18 -08004671
Eric Wongd81bf822007-01-10 01:22:38 -08004672 SVN::_Core::svn_config_ensure($config_dir, undef);
Steven Walter9ff74e92007-09-28 13:24:19 -04004673 my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
Eric Wongd81bf822007-01-10 01:22:38 -08004674 my $config = SVN::Core::config_get_config($config_dir);
Eric Wong7730fbe2007-07-04 14:07:42 -07004675 $RA = undef;
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004676 my $dont_store_passwords = 1;
4677 my $conf_t = ${$config}{'config'};
4678 {
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004679 no warnings 'once';
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004680 # The usage of $SVN::_Core::SVN_CONFIG_* variables
4681 # produces warnings that variables are used only once.
4682 # I had not found the better way to shut them up, so
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004683 # the warnings of type 'once' are disabled in this block.
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004684 if (SVN::_Core::svn_config_get_bool($conf_t,
4685 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4686 $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
4687 1) == 0) {
4688 SVN::_Core::svn_auth_set_parameter($baton,
4689 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
4690 bless (\$dont_store_passwords, "_p_void"));
4691 }
4692 if (SVN::_Core::svn_config_get_bool($conf_t,
4693 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4694 $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
4695 1) == 0) {
4696 $Git::SVN::Prompt::_no_auth_cache = 1;
4697 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004698 } # no warnings 'once'
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004699 my $self = SVN::Ra->new(url => escape_url($url), auth => $baton,
Eric Wongd81bf822007-01-10 01:22:38 -08004700 config => $config,
4701 pool => SVN::Pool->new,
4702 auth_provider_callbacks => $callbacks);
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004703 $self->{url} = $url;
Eric Wongd81bf822007-01-10 01:22:38 -08004704 $self->{svn_path} = $url;
4705 $self->{repos_root} = $self->get_repos_root;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08004706 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
Eric Wong0dc03d62007-05-13 01:04:43 -07004707 $self->{cache} = { check_path => { r => 0, data => {} },
4708 get_dir => { r => 0, data => {} } };
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004709 $RA = bless $self, $class;
Eric Wongd81bf822007-01-10 01:22:38 -08004710}
4711
Eric Wong0dc03d62007-05-13 01:04:43 -07004712sub check_path {
4713 my ($self, $path, $r) = @_;
4714 my $cache = $self->{cache}->{check_path};
4715 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
4716 return $cache->{data}->{$path};
4717 }
4718 my $pool = SVN::Pool->new;
4719 my $t = $self->SUPER::check_path($path, $r, $pool);
4720 $pool->clear;
4721 if ($r != $cache->{r}) {
4722 %{$cache->{data}} = ();
4723 $cache->{r} = $r;
4724 }
4725 $cache->{data}->{$path} = $t;
4726}
4727
4728sub get_dir {
4729 my ($self, $dir, $r) = @_;
4730 my $cache = $self->{cache}->{get_dir};
4731 if ($r == $cache->{r}) {
4732 if (my $x = $cache->{data}->{$dir}) {
4733 return wantarray ? @$x : $x->[0];
4734 }
4735 }
4736 my $pool = SVN::Pool->new;
4737 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
4738 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
4739 $pool->clear;
4740 if ($r != $cache->{r}) {
4741 %{$cache->{data}} = ();
4742 $cache->{r} = $r;
4743 }
4744 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
4745 wantarray ? (\%dirents, $r, $props) : \%dirents;
4746}
4747
Eric Wongd81bf822007-01-10 01:22:38 -08004748sub DESTROY {
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004749 # do not call the real DESTROY since we store ourselves in $RA
Eric Wongd81bf822007-01-10 01:22:38 -08004750}
4751
Eric Wong1ef626b2009-01-17 22:11:44 -08004752# get_log(paths, start, end, limit,
4753# discover_changed_paths, strict_node_history, receiver)
Eric Wongd81bf822007-01-10 01:22:38 -08004754sub get_log {
4755 my ($self, @args) = @_;
4756 my $pool = SVN::Pool->new;
Eric Wong1ef626b2009-01-17 22:11:44 -08004757
Mattias Nissler3c49a032009-07-07 01:39:52 +02004758 # svn_log_changed_path_t objects passed to get_log are likely to be
4759 # overwritten even if only the refs are copied to an external variable,
4760 # so we should dup the structures in their entirety. Using an
4761 # externally passed pool (instead of our temporary and quickly cleared
4762 # pool in Git::SVN::Ra) does not help matters at all...
4763 my $receiver = pop @args;
Mattias Nissler0b2af452009-07-07 01:40:02 +02004764 my $prefix = "/".$self->{svn_path};
4765 $prefix =~ s#/+($)##;
4766 my $prefix_regex = qr#^\Q$prefix\E#;
Mattias Nissler3c49a032009-07-07 01:39:52 +02004767 push(@args, sub {
4768 my ($paths) = $_[0];
4769 return &$receiver(@_) unless $paths;
4770 $_[0] = ();
4771 foreach my $p (keys %$paths) {
4772 my $i = $paths->{$p};
Mattias Nissler0b2af452009-07-07 01:40:02 +02004773 # Make path relative to our url, not repos_root
4774 $p =~ s/$prefix_regex//;
4775 my %s = map { $_ => $i->$_; }
4776 qw/copyfrom_path copyfrom_rev action/;
4777 if ($s{'copyfrom_path'}) {
4778 $s{'copyfrom_path'} =~ s/$prefix_regex//;
4779 }
Mattias Nissler3c49a032009-07-07 01:39:52 +02004780 $_[0]{$p} = \%s;
4781 }
4782 &$receiver(@_);
4783 });
4784
4785
Eric Wong1ef626b2009-01-17 22:11:44 -08004786 # the limit parameter was not supported in SVN 1.1.x, so we
4787 # drop it. Therefore, the receiver callback passed to it
4788 # is made aware of this limitation by being wrapped if
4789 # the limit passed to is being wrapped.
4790 if ($SVN::Core::VERSION le '1.2.0') {
4791 my $limit = splice(@args, 3, 1);
4792 if ($limit > 0) {
4793 my $receiver = pop @args;
4794 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
4795 }
4796 }
Eric Wongd81bf822007-01-10 01:22:38 -08004797 my $ret = $self->SUPER::get_log(@args, $pool);
4798 $pool->clear;
4799 $ret;
4800}
4801
Steven Walter9ff74e92007-09-28 13:24:19 -04004802sub trees_match {
4803 my ($self, $url1, $rev1, $url2, $rev2) = @_;
4804 my $ctx = SVN::Client->new(auth => _auth_providers);
4805 my $out = IO::File->new_tmpfile;
4806
4807 # older SVN (1.1.x) doesn't take $pool as the last parameter for
4808 # $ctx->diff(), so we'll create a default one
4809 my $pool = SVN::Pool->new_default_sub;
4810
4811 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
4812 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
4813 $out->flush;
4814 my $ret = (($out->stat)[7] == 0);
4815 close $out or croak $!;
4816
4817 $ret;
4818}
4819
Eric Wongd81bf822007-01-10 01:22:38 -08004820sub get_commit_editor {
Eric Wong44320b92007-01-13 22:35:53 -08004821 my ($self, $log, $cb, $pool) = @_;
Eric Wongd81bf822007-01-10 01:22:38 -08004822 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
Eric Wong44320b92007-01-13 22:35:53 -08004823 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004824}
4825
Eric Wongd81bf822007-01-10 01:22:38 -08004826sub gs_do_update {
Eric Wong8a603772007-01-31 02:45:50 -08004827 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
4828 my $new = ($rev_a == $rev_b);
4829 my $path = $gs->{path};
4830
Eric Wong2e5e2482007-02-23 02:21:59 -08004831 if ($new && -e $gs->{index}) {
4832 unlink $gs->{index} or die
4833 "Couldn't unlink index: $gs->{index}: $!\n";
4834 }
Eric Wongd81bf822007-01-10 01:22:38 -08004835 my $pool = SVN::Pool->new;
Eric Wong8b8fc062007-01-22 11:44:57 -08004836 $editor->set_path_strip($path);
Eric Wong2b27f6c2007-01-28 04:59:05 -08004837 my (@pc) = split m#/#, $path;
4838 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
Eric Wong8a603772007-01-31 02:45:50 -08004839 1, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004840 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong2b27f6c2007-01-28 04:59:05 -08004841
4842 # Since we can't rely on svn_ra_reparent being available, we'll
4843 # just have to do some magic with set_path to make it so
4844 # we only want a partial path.
4845 my $sp = '';
4846 my $final = join('/', @pc);
4847 while (@pc) {
4848 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
4849 $sp .= '/' if length $sp;
4850 $sp .= shift @pc;
4851 }
4852 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
4853
Eric Wong2b27f6c2007-01-28 04:59:05 -08004854 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
4855
Eric Wongd81bf822007-01-10 01:22:38 -08004856 $reporter->finish_report($pool);
4857 $pool->clear;
4858 $editor->{git_commit_ok};
4859}
4860
Eric Wong2b27f6c2007-01-28 04:59:05 -08004861# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
4862# svn_ra_reparent didn't work before 1.4)
Eric Wongd81bf822007-01-10 01:22:38 -08004863sub gs_do_switch {
Eric Wong8a603772007-01-31 02:45:50 -08004864 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
4865 my $path = $gs->{path};
Eric Wongd81bf822007-01-10 01:22:38 -08004866 my $pool = SVN::Pool->new;
Eric Wong2b27f6c2007-01-28 04:59:05 -08004867
4868 my $full_url = $self->{url};
4869 my $old_url = $full_url;
Eric Wong2a679c72009-07-19 22:08:45 -07004870 $full_url .= '/' . $path if length $path;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004871 my ($ra, $reparented);
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004872
Eric Wong2a679c72009-07-19 22:08:45 -07004873 if ($old_url =~ m#^svn(\+ssh)?://# ||
4874 ($full_url =~ m#^https?://# &&
4875 escape_url($full_url) ne $full_url)) {
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004876 $_[0] = undef;
4877 $self = undef;
4878 $RA = undef;
4879 $ra = Git::SVN::Ra->new($full_url);
4880 $ra_invalid = 1;
4881 } elsif ($old_url ne $full_url) {
4882 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, $pool);
4883 $self->{url} = $full_url;
4884 $reparented = 1;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004885 }
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004886
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004887 $ra ||= $self;
Eric Wongf4392df2008-09-06 20:18:18 -07004888 $url_b = escape_url($url_b);
Eric Wong8a603772007-01-31 02:45:50 -08004889 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004890 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong8b8fc062007-01-22 11:44:57 -08004891 $reporter->set_path('', $rev_a, 0, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004892 $reporter->finish_report($pool);
Eric Wong2b27f6c2007-01-28 04:59:05 -08004893
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004894 if ($reparented) {
4895 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
4896 $self->{url} = $old_url;
4897 }
Eric Wong2b27f6c2007-01-28 04:59:05 -08004898
Eric Wongd81bf822007-01-10 01:22:38 -08004899 $pool->clear;
4900 $editor->{git_commit_ok};
4901}
4902
Eric Wongb54a9012007-06-13 02:37:03 -07004903sub longest_common_path {
4904 my ($gsv, $globs) = @_;
Eric Wongd2ae1432007-02-07 11:50:16 -08004905 my %common;
Eric Wonge5181922007-02-08 12:53:57 -08004906 my $common_max = scalar @$gsv;
4907
4908 foreach my $gs (@$gsv) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004909 my @tmp = split m#/#, $gs->{path};
4910 my $p = '';
4911 foreach (@tmp) {
4912 $p .= length($p) ? "/$_" : $_;
4913 $common{$p} ||= 0;
4914 $common{$p}++;
4915 }
4916 }
Eric Wonge5181922007-02-08 12:53:57 -08004917 $globs ||= [];
4918 $common_max += scalar @$globs;
4919 foreach my $glob (@$globs) {
4920 my @tmp = split m#/#, $glob->{path}->{left};
4921 my $p = '';
4922 foreach (@tmp) {
4923 $p .= length($p) ? "/$_" : $_;
4924 $common{$p} ||= 0;
4925 $common{$p}++;
4926 }
4927 }
4928
Eric Wongd2ae1432007-02-07 11:50:16 -08004929 my $longest_path = '';
4930 foreach (sort {length $b <=> length $a} keys %common) {
Eric Wonge5181922007-02-08 12:53:57 -08004931 if ($common{$_} == $common_max) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004932 $longest_path = $_;
4933 last;
4934 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004935 }
Eric Wongb54a9012007-06-13 02:37:03 -07004936 $longest_path;
4937}
4938
4939sub gs_fetch_loop_common {
4940 my ($self, $base, $head, $gsv, $globs) = @_;
4941 return if ($base > $head);
4942 my $inc = $_log_window_size;
4943 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
4944 my $longest_path = longest_common_path($gsv, $globs);
Eric Wonga51cdb02007-09-07 04:00:40 -07004945 my $ra_url = $self->{url};
Alex Vandiverc69700f2009-05-06 16:18:52 -04004946 my $find_trailing_edge;
Eric Wong0af9c9f2007-01-27 22:28:56 -08004947 while (1) {
Eric Wongd4eff2b2007-01-30 14:04:22 -08004948 my %revs;
Eric Wongd2ae1432007-02-07 11:50:16 -08004949 my $err;
Eric Wongf7c3fc42007-01-29 18:34:55 -08004950 my $err_handler = $SVN::Error::handler;
Eric Wongd2ae1432007-02-07 11:50:16 -08004951 $SVN::Error::handler = sub {
4952 ($err) = @_;
4953 skip_unknown_revs($err);
4954 };
4955 sub _cb {
4956 my ($paths, $r, $author, $date, $log) = @_;
Mattias Nissler3c49a032009-07-07 01:39:52 +02004957 [ $paths,
Eric Wongd2ae1432007-02-07 11:50:16 -08004958 { author => $author, date => $date, log => $log } ];
4959 }
4960 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
4961 sub { $revs{$_[1]} = _cb(@_) });
Deskin Miller99366562009-02-08 19:33:18 -05004962 if ($err) {
4963 print "Checked through r$max\r";
Alex Vandiverc69700f2009-05-06 16:18:52 -04004964 } else {
4965 $find_trailing_edge = 1;
Deskin Miller99366562009-02-08 19:33:18 -05004966 }
Alex Vandiverc69700f2009-05-06 16:18:52 -04004967 if ($err and $find_trailing_edge) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004968 print STDERR "Path '$longest_path' ",
4969 "was probably deleted:\n",
4970 $err->expanded_message,
4971 "\nWill attempt to follow ",
4972 "revisions r$min .. r$max ",
4973 "committed before the deletion\n";
4974 my $hi = $max;
4975 while (--$hi >= $min) {
4976 my $ok;
4977 $self->get_log([$longest_path], $min, $hi,
4978 0, 1, 1, sub {
Alex Vandiverb6c61772009-05-06 16:18:53 -04004979 $ok = $_[1];
Eric Wongd2ae1432007-02-07 11:50:16 -08004980 $revs{$_[1]} = _cb(@_) });
4981 if ($ok) {
4982 print STDERR "r$min .. r$ok OK\n";
4983 last;
4984 }
4985 }
Alex Vandiverc69700f2009-05-06 16:18:52 -04004986 $find_trailing_edge = 0;
Eric Wongd2ae1432007-02-07 11:50:16 -08004987 }
Eric Wongd4eff2b2007-01-30 14:04:22 -08004988 $SVN::Error::handler = $err_handler;
Eric Wongfbcc1732007-02-06 18:35:30 -08004989
Eric Wonge5181922007-02-08 12:53:57 -08004990 my %exists = map { $_->{path} => $_ } @$gsv;
Eric Wongd4eff2b2007-01-30 14:04:22 -08004991 foreach my $r (sort {$a <=> $b} keys %revs) {
Eric Wongfbcc1732007-02-06 18:35:30 -08004992 my ($paths, $logged) = @{$revs{$r}};
Eric Wonge5181922007-02-08 12:53:57 -08004993
4994 foreach my $gs ($self->match_globs(\%exists, $paths,
4995 $globs, $r)) {
Eric Wong060610c2007-12-08 23:27:41 -08004996 if ($gs->rev_map_max >= $r) {
Eric Wongfbcc1732007-02-06 18:35:30 -08004997 next;
4998 }
4999 next unless $gs->match_paths($paths, $r);
5000 $gs->{logged_rev_props} = $logged;
Eric Wonge8d120b2007-02-14 16:29:52 -08005001 if (my $last_commit = $gs->last_commit) {
5002 $gs->assert_index_clean($last_commit);
5003 }
Eric Wongfbcc1732007-02-06 18:35:30 -08005004 my $log_entry = $gs->do_fetch($paths, $r);
5005 if ($log_entry) {
Eric Wong0af9c9f2007-01-27 22:28:56 -08005006 $gs->do_git_commit($log_entry);
5007 }
Eric Wong321b1842008-01-02 10:10:03 -08005008 $INDEX_FILES{$gs->{index}} = 1;
Eric Wong0af9c9f2007-01-27 22:28:56 -08005009 }
Eric Wonge5181922007-02-08 12:53:57 -08005010 foreach my $g (@$globs) {
Eric Wong93f26892007-02-11 01:20:26 -08005011 my $k = "svn-remote.$g->{remote}." .
5012 "$g->{t}-maxRev";
5013 Git::SVN::tmp_config($k, $r);
Eric Wonge5181922007-02-08 12:53:57 -08005014 }
Eric Wonga51cdb02007-09-07 04:00:40 -07005015 if ($ra_invalid) {
5016 $_[0] = undef;
5017 $self = undef;
5018 $RA = undef;
5019 $self = Git::SVN::Ra->new($ra_url);
5020 $ra_invalid = undef;
5021 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08005022 }
Eric Wong9c93fee2007-01-31 17:22:31 -08005023 # pre-fill the .rev_db since it'll eventually get filled in
5024 # with '0' x40 if something new gets committed
Eric Wonge5181922007-02-08 12:53:57 -08005025 foreach my $gs (@$gsv) {
Eric Wong66ab84b2007-12-08 23:27:42 -08005026 next if $gs->rev_map_max >= $max;
5027 next if defined $gs->rev_map_get($max);
5028 $gs->rev_map_set($max, 0 x40);
Eric Wong9c93fee2007-01-31 17:22:31 -08005029 }
Eric Wongc3560e52007-02-12 16:03:32 -08005030 foreach my $g (@$globs) {
5031 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
5032 Git::SVN::tmp_config($k, $max);
5033 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08005034 last if $max >= $head;
5035 $min = $max + 1;
5036 $max += $inc;
5037 $max = $head if ($max > $head);
5038 }
Karl Hasselström94bc9142008-02-03 17:56:18 +01005039 Git::SVN::gc();
Eric Wong0af9c9f2007-01-27 22:28:56 -08005040}
5041
Marcus Griep570d35c2008-08-08 01:41:57 -07005042sub get_dir_globbed {
5043 my ($self, $left, $depth, $r) = @_;
5044
5045 my @x = eval { $self->get_dir($left, $r) };
5046 return unless scalar @x == 3;
5047 my $dirents = $x[0];
5048 my @finalents;
5049 foreach my $de (keys %$dirents) {
5050 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
5051 if ($depth > 1) {
5052 my @args = ("$left/$de", $depth - 1, $r);
5053 foreach my $dir ($self->get_dir_globbed(@args)) {
5054 push @finalents, "$de/$dir";
5055 }
5056 } else {
5057 push @finalents, $de;
5058 }
5059 }
5060 @finalents;
5061}
5062
Eric Wonge5181922007-02-08 12:53:57 -08005063sub match_globs {
5064 my ($self, $exists, $paths, $globs, $r) = @_;
Eric Wong74a81222007-02-10 13:28:50 -08005065
5066 sub get_dir_check {
5067 my ($self, $exists, $g, $r) = @_;
Marcus Griep570d35c2008-08-08 01:41:57 -07005068
5069 my @dirs = $self->get_dir_globbed($g->{path}->{left},
5070 $g->{path}->{depth},
5071 $r);
5072
5073 foreach my $de (@dirs) {
Eric Wong74a81222007-02-10 13:28:50 -08005074 my $p = $g->{path}->full_path($de);
5075 next if $exists->{$p};
5076 next if (length $g->{path}->{right} &&
5077 ($self->check_path($p, $r) !=
5078 $SVN::Node::dir));
5079 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
5080 $g->{ref}->full_path($de), 1);
5081 }
5082 }
Eric Wonge5181922007-02-08 12:53:57 -08005083 foreach my $g (@$globs) {
Eric Wong74a81222007-02-10 13:28:50 -08005084 if (my $path = $paths->{"/$g->{path}->{left}"}) {
5085 if ($path->{action} =~ /^[AR]$/) {
5086 get_dir_check($self, $exists, $g, $r);
5087 }
5088 }
Eric Wonge5181922007-02-08 12:53:57 -08005089 foreach (keys %$paths) {
Eric Wong28710f72007-02-14 13:32:21 -08005090 if (/$g->{path}->{left_regex}/ &&
5091 !/$g->{path}->{regex}/) {
Eric Wong74a81222007-02-10 13:28:50 -08005092 next if $paths->{$_}->{action} !~ /^[AR]$/;
5093 get_dir_check($self, $exists, $g, $r);
5094 }
Eric Wonge5181922007-02-08 12:53:57 -08005095 next unless /$g->{path}->{regex}/;
5096 my $p = $1;
5097 my $pathname = $g->{path}->full_path($p);
5098 next if $exists->{$pathname};
Eric Wong0c1ec5a2007-04-18 00:17:33 -07005099 next if ($self->check_path($pathname, $r) !=
5100 $SVN::Node::dir);
Eric Wonge5181922007-02-08 12:53:57 -08005101 $exists->{$pathname} = Git::SVN->init(
5102 $self->{url}, $pathname, undef,
5103 $g->{ref}->full_path($p), 1);
5104 }
5105 my $c = '';
5106 foreach (split m#/#, $g->{path}->{left}) {
5107 $c .= "/$_";
5108 next unless ($paths->{$c} &&
Eric Wong74a81222007-02-10 13:28:50 -08005109 ($paths->{$c}->{action} =~ /^[AR]$/));
5110 get_dir_check($self, $exists, $g, $r);
Eric Wonge5181922007-02-08 12:53:57 -08005111 }
5112 }
5113 values %$exists;
5114}
5115
Eric Wonge6434f82007-01-23 16:29:23 -08005116sub minimize_url {
5117 my ($self) = @_;
5118 return $self->{url} if ($self->{url} eq $self->{repos_root});
5119 my $url = $self->{repos_root};
5120 my @components = split(m!/!, $self->{svn_path});
5121 my $c = '';
5122 do {
5123 $url .= "/$c" if length $c;
Eric Wong5f8b2cb2009-07-25 13:14:16 -07005124 eval {
5125 my $ra = (ref $self)->new($url);
5126 my $latest = $ra->get_latest_revnum;
5127 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
5128 };
Eric Wonge6434f82007-01-23 16:29:23 -08005129 } while ($@ && ($c = shift @components));
5130 $url;
5131}
5132
Eric Wongd81bf822007-01-10 01:22:38 -08005133sub can_do_switch {
5134 my $self = shift;
5135 unless (defined $can_do_switch) {
5136 my $pool = SVN::Pool->new;
5137 my $rep = eval {
5138 $self->do_switch(1, '', 0, $self->{url},
5139 SVN::Delta::Editor->new, $pool);
5140 };
5141 if ($@) {
5142 $can_do_switch = 0;
5143 } else {
5144 $rep->abort_report($pool);
5145 $can_do_switch = 1;
5146 }
5147 $pool->clear;
5148 }
5149 $can_do_switch;
5150}
5151
Eric Wong0af9c9f2007-01-27 22:28:56 -08005152sub skip_unknown_revs {
5153 my ($err) = @_;
5154 my $errno = $err->apr_err();
5155 # Maybe the branch we're tracking didn't
5156 # exist when the repo started, so it's
5157 # not an error if it doesn't, just continue
5158 #
5159 # Wonderfully consistent library, eh?
5160 # 160013 - svn:// and file://
5161 # 175002 - http(s)://
5162 # 175007 - http(s):// (this repo required authorization, too...)
5163 # More codes may be discovered later...
5164 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
Eric Wonga6a15a92007-03-30 17:54:48 -07005165 my $err_key = $err->expanded_message;
5166 # revision numbers change every time, filter them out
5167 $err_key =~ s/\d+/\0/g;
5168 $err_key = "$errno\0$err_key";
5169 unless ($ignored_err{$err_key}) {
5170 warn "W: Ignoring error from SVN, path probably ",
5171 "does not exist: ($errno): ",
5172 $err->expanded_message,"\n";
Eric Wongeee8a172008-01-07 02:40:40 -08005173 warn "W: Do not be alarmed at the above message ",
5174 "git-svn is just searching aggressively for ",
5175 "old history.\n",
5176 "This may take a while on large repositories\n";
Eric Wonga6a15a92007-03-30 17:54:48 -07005177 $ignored_err{$err_key} = 1;
5178 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08005179 return;
5180 }
5181 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
5182}
5183
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005184package Git::SVN::Log;
5185use strict;
5186use warnings;
5187use POSIX qw/strftime/;
Ben Waltone8717842009-02-24 14:44:49 -05005188use Time::Local;
David D Kilzer111947e2007-11-11 22:56:52 -08005189use constant commit_log_separator => ('-' x 72) . "\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005190use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
5191 %rusers $show_commit $incremental/;
5192my $l_fmt;
5193
5194sub cmt_showable {
5195 my ($c) = @_;
5196 return 1 if defined $c->{r};
Eric Wongc16d0872007-04-08 00:59:22 -07005197
5198 # big commit message got truncated by the 16k pretty buffer in rev-list
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005199 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
5200 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
Eric Wongc16d0872007-04-08 00:59:22 -07005201 @{$c->{l}} = ();
Eric Wong44320b92007-01-13 22:35:53 -08005202 my @log = command(qw/cat-file commit/, $c->{c});
Eric Wongc16d0872007-04-08 00:59:22 -07005203
5204 # shift off the headers
5205 shift @log while ($log[0] ne '');
Eric Wong44320b92007-01-13 22:35:53 -08005206 shift @log;
Eric Wongc16d0872007-04-08 00:59:22 -07005207
5208 # TODO: make $c->{l} not have a trailing newline in the future
5209 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005210
5211 (undef, $c->{r}, undef) = ::extract_metadata(
Eric Wong44320b92007-01-13 22:35:53 -08005212 (grep(/^git-svn-id: /, @log))[-1]);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005213 }
5214 return defined $c->{r};
5215}
5216
5217sub log_use_color {
Jeff Kingcd459e32007-12-11 01:28:42 -05005218 return $color || Git->repository->get_colorbool('color.diff');
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005219}
5220
5221sub git_svn_log_cmd {
Eric Wong3bc718b2007-02-13 17:09:40 -08005222 my ($r_min, $r_max, @args) = @_;
5223 my $head = 'HEAD';
Eric Wong6ed77262007-08-15 09:55:18 -07005224 my (@files, @log_opts);
Eric Wong3bc718b2007-02-13 17:09:40 -08005225 foreach my $x (@args) {
Eric Wong6ed77262007-08-15 09:55:18 -07005226 if ($x eq '--' || @files) {
5227 push @files, $x;
5228 } else {
5229 if (::verify_ref("$x^0")) {
5230 $head = $x;
5231 } else {
5232 push @log_opts, $x;
5233 }
5234 }
Eric Wong3bc718b2007-02-13 17:09:40 -08005235 }
5236
Eric Wong13c823f2007-04-08 00:59:19 -07005237 my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
5238 $gs ||= Git::SVN->_new;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005239 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
5240 $gs->refname);
5241 push @cmd, '-r' unless $non_recursive;
5242 push @cmd, qw/--raw --name-status/ if $verbose;
5243 push @cmd, '--color' if log_use_color();
Eric Wong6ed77262007-08-15 09:55:18 -07005244 push @cmd, @log_opts;
5245 if (defined $r_max && $r_max == $r_min) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005246 push @cmd, '--max-count=1';
Eric Wong060610c2007-12-08 23:27:41 -08005247 if (my $c = $gs->rev_map_get($r_max)) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005248 push @cmd, $c;
5249 }
Eric Wong6ed77262007-08-15 09:55:18 -07005250 } elsif (defined $r_max) {
David D Kilzer111947e2007-11-11 22:56:52 -08005251 if ($r_max < $r_min) {
5252 ($r_min, $r_max) = ($r_max, $r_min);
5253 }
5254 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
5255 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
5256 # If there are no commits in the range, both $c_max and $c_min
5257 # will be undefined. If there is at least 1 commit in the
5258 # range, both will be defined.
5259 return () if !defined $c_min || !defined $c_max;
5260 if ($c_min eq $c_max) {
5261 push @cmd, '--max-count=1', $c_min;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005262 } else {
David D Kilzer111947e2007-11-11 22:56:52 -08005263 push @cmd, '--boundary', "$c_min..$c_max";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005264 }
5265 }
Eric Wong6ed77262007-08-15 09:55:18 -07005266 return (@cmd, @files);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005267}
5268
5269# adapted from pager.c
5270sub config_pager {
Jonathan Niederdec543e2009-10-30 20:43:19 -05005271 chomp(my $pager = command_oneline(qw(var GIT_PAGER)));
5272 if ($pager eq 'cat') {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005273 $pager = undef;
5274 }
Jeff Kingcd459e32007-12-11 01:28:42 -05005275 $ENV{GIT_PAGER_IN_USE} = defined($pager);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005276}
5277
5278sub run_pager {
Eric Wongb0193042007-09-21 18:48:45 -07005279 return unless -t *STDOUT && defined $pager;
Marcus Griep971e6282008-09-10 11:09:46 -04005280 pipe my ($rfd, $wfd) or return;
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005281 defined(my $pid = fork) or ::fatal "Can't fork: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005282 if (!$pid) {
5283 open STDOUT, '>&', $wfd or
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005284 ::fatal "Can't redirect to stdout: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005285 return;
5286 }
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005287 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005288 $ENV{LESS} ||= 'FRSX';
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005289 exec $pager or ::fatal "Can't run pager: $! ($pager)";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005290}
5291
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005292sub format_svn_date {
Ben Waltone8717842009-02-24 14:44:49 -05005293 # some systmes don't handle or mishandle %z, so be creative.
Ben Walton736e6192009-02-27 22:11:45 -05005294 my $t = shift || time;
Ben Waltone8717842009-02-24 14:44:49 -05005295 my $gm = timelocal(gmtime($t));
5296 my $sign = qw( + + - )[ $t <=> $gm ];
5297 my $gmoff = sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
5298 return strftime("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005299}
5300
5301sub parse_git_date {
5302 my ($t, $tz) = @_;
5303 # Date::Parse isn't in the standard Perl distro :(
5304 if ($tz =~ s/^\+//) {
5305 $t += tz_to_s_offset($tz);
5306 } elsif ($tz =~ s/^\-//) {
5307 $t -= tz_to_s_offset($tz);
5308 }
5309 return $t;
5310}
5311
5312sub set_local_timezone {
5313 if (defined $TZ) {
5314 $ENV{TZ} = $TZ;
5315 } else {
5316 delete $ENV{TZ};
5317 }
5318}
5319
Eric Wong21819a32007-01-27 14:38:10 -08005320sub tz_to_s_offset {
5321 my ($tz) = @_;
5322 $tz =~ s/(\d\d)$//;
5323 return ($1 * 60) + ($tz * 3600);
5324}
5325
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005326sub get_author_info {
5327 my ($dest, $author, $t, $tz) = @_;
5328 $author =~ s/(?:^\s*|\s*$)//g;
5329 $dest->{a_raw} = $author;
5330 my $au;
Eric Wong1c8443b2007-01-14 02:17:00 -08005331 if ($::_authors) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005332 $au = $rusers{$author} || undef;
5333 }
5334 if (!$au) {
5335 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
5336 }
5337 $dest->{t} = $t;
5338 $dest->{tz} = $tz;
5339 $dest->{a} = $au;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005340 $dest->{t_utc} = parse_git_date($t, $tz);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005341}
5342
5343sub process_commit {
5344 my ($c, $r_min, $r_max, $defer) = @_;
5345 if (defined $r_min && defined $r_max) {
5346 if ($r_min == $c->{r} && $r_min == $r_max) {
5347 show_commit($c);
5348 return 0;
5349 }
5350 return 1 if $r_min == $r_max;
5351 if ($r_min < $r_max) {
5352 # we need to reverse the print order
5353 return 0 if (defined $limit && --$limit < 0);
5354 push @$defer, $c;
5355 return 1;
5356 }
5357 if ($r_min != $r_max) {
5358 return 1 if ($r_min < $c->{r});
5359 return 1 if ($r_max > $c->{r});
5360 }
5361 }
5362 return 0 if (defined $limit && --$limit < 0);
5363 show_commit($c);
5364 return 1;
5365}
5366
5367sub show_commit {
5368 my $c = shift;
5369 if ($oneline) {
5370 my $x = "\n";
5371 if (my $l = $c->{l}) {
5372 while ($l->[0] =~ /^\s*$/) { shift @$l }
5373 $x = $l->[0];
5374 }
5375 $l_fmt ||= 'A' . length($c->{r});
5376 print 'r',pack($l_fmt, $c->{r}),' | ';
5377 print "$c->{c} | " if $show_commit;
5378 print $x;
5379 } else {
5380 show_commit_normal($c);
5381 }
5382}
5383
5384sub show_commit_changed_paths {
5385 my ($c) = @_;
5386 return unless $c->{changed};
5387 print "Changed paths:\n", @{$c->{changed}};
5388}
5389
5390sub show_commit_normal {
5391 my ($c) = @_;
David D Kilzer111947e2007-11-11 22:56:52 -08005392 print commit_log_separator, "r$c->{r} | ";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005393 print "$c->{c} | " if $show_commit;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005394 print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005395 my $nr_line = 0;
5396
5397 if (my $l = $c->{l}) {
5398 while ($l->[$#$l] eq "\n" && $#$l > 0
5399 && $l->[($#$l - 1)] eq "\n") {
5400 pop @$l;
5401 }
5402 $nr_line = scalar @$l;
5403 if (!$nr_line) {
5404 print "1 line\n\n\n";
5405 } else {
5406 if ($nr_line == 1) {
5407 $nr_line = '1 line';
5408 } else {
5409 $nr_line .= ' lines';
5410 }
5411 print $nr_line, "\n";
5412 show_commit_changed_paths($c);
5413 print "\n";
5414 print $_ foreach @$l;
5415 }
5416 } else {
5417 print "1 line\n";
5418 show_commit_changed_paths($c);
5419 print "\n";
5420
5421 }
Eric Wong488a63e2007-02-15 00:40:42 -08005422 foreach my $x (qw/raw stat diff/) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005423 if ($c->{$x}) {
5424 print "\n";
5425 print $_ foreach @{$c->{$x}}
5426 }
5427 }
5428}
5429
5430sub cmd_show_log {
5431 my (@args) = @_;
5432 my ($r_min, $r_max);
5433 my $r_last = -1; # prevent dupes
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005434 set_local_timezone();
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005435 if (defined $::_revision) {
5436 if ($::_revision =~ /^(\d+):(\d+)$/) {
5437 ($r_min, $r_max) = ($1, $2);
5438 } elsif ($::_revision =~ /^\d+$/) {
5439 $r_min = $r_max = $::_revision;
5440 } else {
5441 ::fatal "-r$::_revision is not supported, use ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005442 "standard 'git log' arguments instead";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005443 }
5444 }
5445
5446 config_pager();
Eric Wong6ed77262007-08-15 09:55:18 -07005447 @args = git_svn_log_cmd($r_min, $r_max, @args);
David D Kilzer111947e2007-11-11 22:56:52 -08005448 if (!@args) {
5449 print commit_log_separator unless $incremental || $oneline;
5450 return;
5451 }
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005452 my $log = command_output_pipe(@args);
5453 run_pager();
Eric Wong488a63e2007-02-15 00:40:42 -08005454 my (@k, $c, $d, $stat);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005455 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
5456 while (<$log>) {
David D Kilzer60f3ff12007-11-10 22:10:34 -08005457 if (/^${esc_color}commit -?($::sha1_short)/o) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005458 my $cmt = $1;
5459 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
5460 $r_last = $c->{r};
5461 process_commit($c, $r_min, $r_max, \@k) or
5462 goto out;
5463 }
5464 $d = undef;
5465 $c = { c => $cmt };
5466 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
5467 get_author_info($c, $1, $2, $3);
5468 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
5469 # ignore
5470 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
5471 push @{$c->{raw}}, $_;
5472 } elsif (/^${esc_color}[ACRMDT]\t/) {
5473 # we could add $SVN->{svn_path} here, but that requires
5474 # remote access at the moment (repo_path_split)...
5475 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
5476 push @{$c->{changed}}, $_;
5477 } elsif (/^${esc_color}diff /o) {
5478 $d = 1;
5479 push @{$c->{diff}}, $_;
5480 } elsif ($d) {
5481 push @{$c->{diff}}, $_;
Eric Wong488a63e2007-02-15 00:40:42 -08005482 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
5483 $esc_color*[\+\-]*$esc_color$/x) {
5484 $stat = 1;
5485 push @{$c->{stat}}, $_;
5486 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
5487 push @{$c->{stat}}, $_;
5488 $stat = undef;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005489 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
5490 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
5491 } elsif (s/^${esc_color} //o) {
5492 push @{$c->{l}}, $_;
5493 }
5494 }
5495 if ($c && defined $c->{r} && $c->{r} != $r_last) {
5496 $r_last = $c->{r};
5497 process_commit($c, $r_min, $r_max, \@k);
5498 }
5499 if (@k) {
David D Kilzer111947e2007-11-11 22:56:52 -08005500 ($r_min, $r_max) = ($r_max, $r_min);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005501 process_commit($_, $r_min, $r_max) foreach reverse @k;
5502 }
5503out:
Eric Wongc843c462007-01-12 03:07:31 -08005504 close $log;
David D Kilzer111947e2007-11-11 22:56:52 -08005505 print commit_log_separator unless $incremental || $oneline;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005506}
5507
Tim Stoakes6fb53752008-02-10 15:21:08 +10305508sub cmd_blame {
Steven Grimm4be40382008-05-10 22:11:18 -07005509 my $path = pop;
Tim Stoakes6fb53752008-02-10 15:21:08 +10305510
5511 config_pager();
5512 run_pager();
5513
Steven Grimm4be40382008-05-10 22:11:18 -07005514 my ($fh, $ctx, $rev);
5515
5516 if ($_git_format) {
5517 ($fh, $ctx) = command_output_pipe('blame', @_, $path);
5518 while (my $line = <$fh>) {
5519 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
5520 # Uncommitted edits show up as a rev ID of
5521 # all zeros, which we can't look up with
5522 # cmt_metadata
5523 if ($1 !~ /^0+$/) {
5524 (undef, $rev, undef) =
5525 ::cmt_metadata($1);
5526 $rev = '0' if (!$rev);
5527 } else {
5528 $rev = '0';
5529 }
5530 $rev = sprintf('%-10s', $rev);
5531 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
5532 }
5533 print $line;
Tim Stoakes6fb53752008-02-10 15:21:08 +10305534 }
Steven Grimm4be40382008-05-10 22:11:18 -07005535 } else {
5536 ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD',
5537 '--', $path);
5538 my ($sha1);
5539 my %authors;
Boris Byk6ea42032009-04-11 00:32:41 +04005540 my @buffer;
5541 my %dsha; #distinct sha keys
5542
Steven Grimm4be40382008-05-10 22:11:18 -07005543 while (my $line = <$fh>) {
Boris Byk6ea42032009-04-11 00:32:41 +04005544 push @buffer, $line;
Steven Grimm4be40382008-05-10 22:11:18 -07005545 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
Boris Byk6ea42032009-04-11 00:32:41 +04005546 $dsha{$1} = 1;
5547 }
5548 }
5549
5550 my $s2r = ::cmt_sha2rev_batch([keys %dsha]);
5551
5552 foreach my $line (@buffer) {
5553 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
5554 $rev = $s2r->{$1};
5555 $rev = '0' if (!$rev)
Steven Grimm4be40382008-05-10 22:11:18 -07005556 }
5557 elsif ($line =~ /^author (.*)/) {
5558 $authors{$rev} = $1;
5559 $authors{$rev} =~ s/\s/_/g;
5560 }
5561 elsif ($line =~ /^\t(.*)$/) {
5562 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
5563 }
5564 }
Tim Stoakes6fb53752008-02-10 15:21:08 +10305565 }
5566 command_close_pipe($fh, $ctx);
5567}
5568
Eric Wong706587f2007-01-18 17:50:01 -08005569package Git::SVN::Migration;
5570# these version numbers do NOT correspond to actual version numbers
5571# of git nor git-svn. They are just relative.
5572#
5573# v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
5574#
5575# v1 layout: .git/$id/info/url, refs/remotes/$id
5576#
5577# v2 layout: .git/svn/$id/info/url, refs/remotes/$id
5578#
5579# v3 layout: .git/svn/$id, refs/remotes/$id
5580# - info/url may remain for backwards compatibility
5581# - this is what we migrate up to this layout automatically,
5582# - this will be used by git svn init on single branches
Eric Wong26a62d52007-02-12 13:25:25 -08005583# v3.1 layout (auto migrated):
5584# - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
5585# for backwards compatibility
Eric Wong706587f2007-01-18 17:50:01 -08005586#
5587# v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
5588# - this is only created for newly multi-init-ed
5589# repositories. Similar in spirit to the
5590# --use-separate-remotes option in git-clone (now default)
5591# - we do not automatically migrate to this (following
5592# the example set by core git)
Eric Wong060610c2007-12-08 23:27:41 -08005593#
5594# v5 layout: .rev_db.$UUID => .rev_map.$UUID
5595# - newer, more-efficient format that uses 24-bytes per record
5596# with no filler space.
5597# - use xxd -c24 < .rev_map.$UUID to view and debug
5598# - This is a one-way migration, repositories updated to the
5599# new format will not be able to use old git-svn without
5600# rebuilding the .rev_db. Rebuilding the rev_db is not
5601# possible if noMetadata or useSvmProps are set; but should
5602# be no problem for users that use the (sensible) defaults.
Eric Wong706587f2007-01-18 17:50:01 -08005603use strict;
5604use warnings;
5605use Carp qw/croak/;
5606use File::Path qw/mkpath/;
Eric Wong47e39c52007-01-21 04:27:09 -08005607use File::Basename qw/dirname basename/;
5608use vars qw/$_minimize/;
Eric Wong706587f2007-01-18 17:50:01 -08005609
5610sub migrate_from_v0 {
5611 my $git_dir = $ENV{GIT_DIR};
5612 return undef unless -d $git_dir;
5613 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5614 my $migrated = 0;
5615 while (<$fh>) {
5616 chomp;
5617 my ($id, $orig_ref) = ($_, $_);
5618 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
5619 next unless -f "$git_dir/$id/info/url";
5620 my $new_ref = "refs/remotes/$id";
5621 if (::verify_ref("$new_ref^0")) {
5622 print STDERR "W: $orig_ref is probably an old ",
5623 "branch used by an ancient version of ",
5624 "git-svn.\n",
5625 "However, $new_ref also exists.\n",
5626 "We will not be able ",
5627 "to use this branch until this ",
5628 "ambiguity is resolved.\n";
5629 next;
5630 }
5631 print STDERR "Migrating from v0 layout...\n" if !$migrated;
5632 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
5633 command_noisy('update-ref', $new_ref, $orig_ref);
5634 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
5635 $migrated++;
5636 }
5637 command_close_pipe($fh, $ctx);
5638 print STDERR "Done migrating from v0 layout...\n" if $migrated;
5639 $migrated;
5640}
5641
5642sub migrate_from_v1 {
5643 my $git_dir = $ENV{GIT_DIR};
5644 my $migrated = 0;
5645 return $migrated unless -d $git_dir;
5646 my $svn_dir = "$git_dir/svn";
5647
5648 # just in case somebody used 'svn' as their $id at some point...
5649 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
5650
5651 print STDERR "Migrating from a git-svn v1 layout...\n";
5652 mkpath([$svn_dir]);
5653 print STDERR "Data from a previous version of git-svn exists, but\n\t",
5654 "$svn_dir\n\t(required for this version ",
Frederik Schwarzer8f510be2008-07-14 18:30:24 +02005655 "($::VERSION) of git-svn) does not exist.\n";
Eric Wong706587f2007-01-18 17:50:01 -08005656 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5657 while (<$fh>) {
5658 my $x = $_;
5659 next unless $x =~ s#^refs/remotes/##;
5660 chomp $x;
5661 next unless -f "$git_dir/$x/info/url";
5662 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
5663 next unless $u;
5664 my $dn = dirname("$git_dir/svn/$x");
5665 mkpath([$dn]) unless -d $dn;
5666 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
5667 mkpath(["$git_dir/svn/svn"]);
5668 print STDERR " - $git_dir/$x/info => ",
5669 "$git_dir/svn/$x/info\n";
5670 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
5671 croak "$!: $x";
5672 # don't worry too much about these, they probably
5673 # don't exist with repos this old (save for index,
5674 # and we can easily regenerate that)
5675 foreach my $f (qw/unhandled.log index .rev_db/) {
5676 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
5677 }
5678 } else {
5679 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
5680 rename "$git_dir/$x", "$git_dir/svn/$x" or
5681 croak "$!: $x";
5682 }
5683 $migrated++;
5684 }
5685 command_close_pipe($fh, $ctx);
5686 print STDERR "Done migrating from a git-svn v1 layout\n";
5687 $migrated;
5688}
5689
5690sub read_old_urls {
5691 my ($l_map, $pfx, $path) = @_;
5692 my @dir;
5693 foreach (<$path/*>) {
5694 if (-r "$_/info/url") {
5695 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
5696 my $ref_id = $pfx . basename $_;
5697 my $url = ::file_to_s("$_/info/url");
5698 $l_map->{$ref_id} = $url;
5699 } elsif (-d $_) {
5700 push @dir, $_;
5701 }
5702 }
5703 foreach (@dir) {
5704 my $x = $_;
5705 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
5706 read_old_urls($l_map, $x, $_);
5707 }
5708}
5709
5710sub migrate_from_v2 {
5711 my @cfg = command(qw/config -l/);
5712 return if grep /^svn-remote\..+\.url=/, @cfg;
5713 my %l_map;
5714 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
5715 my $migrated = 0;
5716
5717 foreach my $ref_id (sort keys %l_map) {
Eric Wong471bc002007-02-01 04:06:27 -08005718 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
5719 if ($@) {
5720 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
5721 }
Eric Wong706587f2007-01-18 17:50:01 -08005722 $migrated++;
5723 }
5724 $migrated;
5725}
5726
Eric Wong47e39c52007-01-21 04:27:09 -08005727sub minimize_connections {
5728 my $r = Git::SVN::read_all_remotes();
5729 my $new_urls = {};
5730 my $root_repos = {};
5731 foreach my $repo_id (keys %$r) {
5732 my $url = $r->{$repo_id}->{url} or next;
5733 my $fetch = $r->{$repo_id}->{fetch} or next;
5734 my $ra = Git::SVN::Ra->new($url);
5735
5736 # skip existing cases where we already connect to the root
5737 if (($ra->{url} eq $ra->{repos_root}) ||
Eric Wong7829f202008-06-28 20:40:32 -07005738 ($ra->{repos_root} eq $repo_id)) {
Eric Wong47e39c52007-01-21 04:27:09 -08005739 $root_repos->{$ra->{url}} = $repo_id;
5740 next;
5741 }
5742
5743 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
5744 my $root_path = $ra->{url};
Eric Wong4e9f6cc2007-02-09 12:17:57 -08005745 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
Eric Wong47e39c52007-01-21 04:27:09 -08005746 foreach my $path (keys %$fetch) {
5747 my $ref_id = $fetch->{$path};
5748 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
5749
5750 # make sure we can read when connecting to
5751 # a higher level of a repository
5752 my ($last_rev, undef) = $gs->last_rev_commit;
5753 if (!defined $last_rev) {
5754 $last_rev = eval {
5755 $root_ra->get_latest_revnum;
5756 };
5757 next if $@;
5758 }
5759 my $new = $root_path;
5760 $new .= length $path ? "/$path" : '';
5761 eval {
5762 $root_ra->get_log([$new], $last_rev, $last_rev,
5763 0, 0, 1, sub { });
5764 };
5765 next if $@;
5766 $new_urls->{$ra->{repos_root}}->{$new} =
5767 { ref_id => $ref_id,
5768 old_repo_id => $repo_id,
5769 old_path => $path };
5770 }
5771 }
5772
5773 my @emptied;
5774 foreach my $url (keys %$new_urls) {
5775 # see if we can re-use an existing [svn-remote "repo_id"]
5776 # instead of creating a(n ugly) new section:
Eric Wong7829f202008-06-28 20:40:32 -07005777 my $repo_id = $root_repos->{$url} || $url;
Eric Wong47e39c52007-01-21 04:27:09 -08005778
5779 my $fetch = $new_urls->{$url};
5780 foreach my $path (keys %$fetch) {
5781 my $x = $fetch->{$path};
5782 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
5783 my $pfx = "svn-remote.$x->{old_repo_id}";
5784
5785 my $old_fetch = quotemeta("$x->{old_path}:".
Adam Brewster6f5748e2009-08-11 23:14:27 -04005786 "$x->{ref_id}");
Eric Wong8b8fc062007-01-22 11:44:57 -08005787 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08005788 "$pfx.fetch", '^'. $old_fetch . '$');
5789 delete $r->{$x->{old_repo_id}}->
5790 {fetch}->{$x->{old_path}};
5791 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
Eric Wong8b8fc062007-01-22 11:44:57 -08005792 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08005793 "$pfx.url");
5794 push @emptied, $x->{old_repo_id}
5795 }
5796 }
5797 }
5798 if (@emptied) {
Johannes Schindelin8befc502008-12-14 23:10:52 +01005799 my $file = $ENV{GIT_CONFIG} || "$ENV{GIT_DIR}/config";
Eric Wong47e39c52007-01-21 04:27:09 -08005800 print STDERR <<EOF;
5801The following [svn-remote] sections in your config file ($file) are empty
5802and can be safely removed:
5803EOF
5804 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
5805 }
5806}
5807
Eric Wong706587f2007-01-18 17:50:01 -08005808sub migration_check {
5809 migrate_from_v0();
5810 migrate_from_v1();
5811 migrate_from_v2();
Eric Wong47e39c52007-01-21 04:27:09 -08005812 minimize_connections() if $_minimize;
Eric Wong706587f2007-01-18 17:50:01 -08005813}
5814
Eric Wongef3cfaa2007-01-24 03:30:57 -08005815package Git::IndexInfo;
5816use strict;
5817use warnings;
5818use Git qw/command_input_pipe command_close_pipe/;
5819
5820sub new {
5821 my ($class) = @_;
5822 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
5823 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
5824}
5825
5826sub remove {
5827 my ($self, $path) = @_;
5828 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
5829 return ++$self->{nr};
5830 }
5831 undef;
5832}
5833
5834sub update {
5835 my ($self, $mode, $hash, $path) = @_;
5836 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
5837 return ++$self->{nr};
5838 }
5839 undef;
5840}
5841
5842sub DESTROY {
5843 my ($self) = @_;
5844 command_close_pipe($self->{gui}, $self->{ctx});
5845}
5846
Eric Wong4bb9ed02007-02-03 13:29:17 -08005847package Git::SVN::GlobSpec;
5848use strict;
5849use warnings;
5850
5851sub new {
5852 my ($class, $glob) = @_;
Eric Wong4bb9ed02007-02-03 13:29:17 -08005853 my $re = $glob;
5854 $re =~ s!/+$!!g; # no need for trailing slashes
Adam Brewster6f5748e2009-08-11 23:14:27 -04005855 $re =~ m!^([^*]*)(\*(?:/\*)*)(.*)$!;
Marcus Griep570d35c2008-08-08 01:41:57 -07005856 my $temp = $re;
5857 my ($left, $right) = ($1, $3);
5858 $re = $2;
5859 my $depth = $re =~ tr/*/*/;
5860 if ($depth != $temp =~ tr/*/*/) {
5861 die "Only one set of wildcard directories " .
5862 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
5863 }
5864 if ($depth == 0) {
Eric Wonge5181922007-02-08 12:53:57 -08005865 die "One '*' is needed for glob: '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08005866 }
Marcus Griep570d35c2008-08-08 01:41:57 -07005867 $re =~ s!\*!\[^/\]*!g;
5868 $re = quotemeta($left) . "($re)" . quotemeta($right);
Eric Wong4e9f6cc2007-02-09 12:17:57 -08005869 if (length $left && !($left =~ s!/+$!!g)) {
5870 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
5871 }
5872 if (length $right && !($right =~ s!^/+!!g)) {
5873 die "Missing leading '/' on right side of: '$glob' ($right)\n";
5874 }
Eric Wong74a81222007-02-10 13:28:50 -08005875 my $left_re = qr/^\/\Q$left\E(\/|$)/;
5876 bless { left => $left, right => $right, left_regex => $left_re,
Marcus Griep570d35c2008-08-08 01:41:57 -07005877 regex => qr/$re/, glob => $glob, depth => $depth }, $class;
Eric Wong4bb9ed02007-02-03 13:29:17 -08005878}
5879
5880sub full_path {
5881 my ($self, $path) = @_;
5882 return (length $self->{left} ? "$self->{left}/" : '') .
5883 $path . (length $self->{right} ? "/$self->{right}" : '');
5884}
5885
Eric Wong3397f9d2006-02-16 01:24:16 -08005886__END__
5887
5888Data structures:
5889
Eric Wong4bb9ed02007-02-03 13:29:17 -08005890
5891$remotes = { # returned by read_all_remotes()
5892 'svn' => {
5893 # svn-remote.svn.url=https://svn.musicpd.org
5894 url => 'https://svn.musicpd.org',
5895 # svn-remote.svn.fetch=mpd/trunk:trunk
5896 fetch => {
5897 'mpd/trunk' => 'trunk',
5898 },
5899 # svn-remote.svn.tags=mpd/tags/*:tags/*
5900 tags => {
5901 path => {
5902 left => 'mpd/tags',
5903 right => '',
5904 regex => qr!mpd/tags/([^/]+)$!,
5905 glob => 'tags/*',
5906 },
5907 ref => {
5908 left => 'tags',
5909 right => '',
5910 regex => qr!tags/([^/]+)$!,
5911 glob => 'tags/*',
5912 },
5913 }
5914 }
5915};
5916
Eric Wong44320b92007-01-13 22:35:53 -08005917$log_entry hashref as returned by libsvn_log_entry()
Eric Wong3397f9d2006-02-16 01:24:16 -08005918{
Eric Wong44320b92007-01-13 22:35:53 -08005919 log => 'whitespace-formatted log entry
Eric Wong3397f9d2006-02-16 01:24:16 -08005920', # trailing newline is preserved
5921 revision => '8', # integer
5922 date => '2004-02-24T17:01:44.108345Z', # commit date
5923 author => 'committer name'
5924};
5925
Eric Wong6e8548c2007-01-27 01:32:00 -08005926
5927# this is generated by generate_diff();
Eric Wong3397f9d2006-02-16 01:24:16 -08005928@mods = array of diff-index line hashes, each element represents one line
5929 of diff-index output
5930
5931diff-index line ($m hash)
5932{
5933 mode_a => first column of diff-index output, no leading ':',
5934 mode_b => second column of diff-index output,
5935 sha1_b => sha1sum of the final blob,
Eric Wongac8e0b92006-03-03 01:20:07 -08005936 chg => change type [MCRADT],
Eric Wong3397f9d2006-02-16 01:24:16 -08005937 file_a => original file name of a file (iff chg is 'C' or 'R')
5938 file_b => new/current file name of a file (any chg)
5939}
5940;
Eric Wonga5e0ced2006-06-12 15:23:48 -07005941
Eric Wonga00439a2006-06-27 19:39:13 -07005942# retval of read_url_paths{,_all}();
5943$l_map = {
5944 # repository root url
5945 'https://svn.musicpd.org' => {
5946 # repository path # GIT_SVN_ID
5947 'mpd/trunk' => 'trunk',
5948 'mpd/tags/0.11.5' => 'tags/0.11.5',
5949 },
5950}
5951
Eric Wonga5e0ced2006-06-12 15:23:48 -07005952Notes:
5953 I don't trust the each() function on unless I created %hash myself
5954 because the internal iterator may not have started at base.