blob: 650c9e5f02ead07351629d6572e82c3a9ac7ef92 [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
Eric Wong150d38c2009-12-22 22:40:18 -0800666 my (undef, $rev, undef, $gs) = working_head_info($head);
667 my $src = $gs->full_url;
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700668
Deskin Millera0fbc872008-12-01 21:43:00 -0500669 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
Marc Branchaud62244062009-06-23 13:02:08 -0400670 my $allglobs = $remote->{ $_tag ? 'tags' : 'branches' };
671 my $glob;
672 if ($#{$allglobs} == 0) {
673 $glob = $allglobs->[0];
674 } else {
675 unless(defined $_branch_dest) {
676 die "Multiple ",
677 $_tag ? "tag" : "branch",
678 " paths defined for Subversion repository.\n",
679 "You must specify where you want to create the ",
680 $_tag ? "tag" : "branch",
681 " with the --destination argument.\n";
682 }
683 foreach my $g (@{$allglobs}) {
Eric Wongf7050592009-06-25 02:28:15 -0700684 # SVN::Git::Editor could probably be moved to Git.pm..
685 my $re = SVN::Git::Editor::glob2pat($g->{path}->{left});
686 if ($_branch_dest =~ /$re/) {
Marc Branchaud62244062009-06-23 13:02:08 -0400687 $glob = $g;
688 last;
689 }
690 }
691 unless (defined $glob) {
Eric Wongeaa14ff2009-07-25 01:36:06 -0700692 my $dest_re = qr/\b\Q$_branch_dest\E\b/;
693 foreach my $g (@{$allglobs}) {
694 $g->{path}->{left} =~ /$dest_re/ or next;
695 if (defined $glob) {
696 die "Ambiguous destination: ",
697 $_branch_dest, "\nmatches both '",
698 $glob->{path}->{left}, "' and '",
699 $g->{path}->{left}, "'\n";
700 }
701 $glob = $g;
702 }
703 unless (defined $glob) {
704 die "Unknown ",
705 $_tag ? "tag" : "branch",
706 " destination $_branch_dest\n";
707 }
Marc Branchaud62244062009-06-23 13:02:08 -0400708 }
709 }
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700710 my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
711 my $dst = join '/', $remote->{url}, $lft, $branch_name, ($rgt || ());
712
713 my $ctx = SVN::Client->new(
714 auth => Git::SVN::Ra::_auth_providers(),
715 log_msg => sub {
716 ${ $_[0] } = defined $_message
717 ? $_message
718 : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
719 . $branch_name;
720 },
721 );
722
723 eval {
724 $ctx->ls($dst, 'HEAD', 0);
725 } and die "branch ${branch_name} already exists\n";
726
727 print "Copying ${src} at r${rev} to ${dst}...\n";
728 $ctx->copy($src, $rev, $dst)
729 unless $_dry_run;
730
731 $gs->fetch_all;
732}
733
Adam Roben26e60162007-04-27 11:57:53 -0700734sub cmd_find_rev {
Marc-Andre Lureauea14e6c2008-03-11 10:00:45 +0200735 my $revision_or_hash = shift or die "SVN or git revision required ",
736 "as a command-line argument\n";
Adam Roben26e60162007-04-27 11:57:53 -0700737 my $result;
738 if ($revision_or_hash =~ /^r\d+$/) {
Adam Robenb3cb7e42007-04-29 01:35:27 -0700739 my $head = shift;
740 $head ||= 'HEAD';
741 my @refs;
João Abecasis63c56022008-07-14 16:28:04 +0100742 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
Adam Robenb3cb7e42007-04-29 01:35:27 -0700743 unless ($gs) {
744 die "Unable to determine upstream SVN information from ",
745 "$head history\n";
Adam Roben26e60162007-04-27 11:57:53 -0700746 }
Adam Robenb3cb7e42007-04-29 01:35:27 -0700747 my $desired_revision = substr($revision_or_hash, 1);
João Abecasis63c56022008-07-14 16:28:04 +0100748 $result = $gs->rev_map_get($desired_revision, $uuid);
Adam Roben26e60162007-04-27 11:57:53 -0700749 } else {
750 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
751 $result = $rev;
752 }
753 print "$result\n" if $result;
754}
755
Eric Wong905f8b72007-02-16 03:22:40 -0800756sub cmd_rebase {
757 command_noisy(qw/update-index --refresh/);
Eric Wong13c823f2007-04-08 00:59:19 -0700758 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
759 unless ($gs) {
Eric Wong905f8b72007-02-16 03:22:40 -0800760 die "Unable to determine upstream SVN information from ",
761 "working tree history\n";
762 }
Seth Falcon7d45e142008-05-19 20:29:17 -0700763 if ($_dry_run) {
764 print "Remote Branch: " . $gs->refname . "\n";
765 print "SVN URL: " . $url . "\n";
766 return;
767 }
Eric Wong905f8b72007-02-16 03:22:40 -0800768 if (command(qw/diff-index HEAD --/)) {
769 print STDERR "Cannot rebase with uncommited changes:\n";
770 command_noisy('status');
771 exit 1;
772 }
Eric Wongdee41f32007-03-13 11:40:36 -0700773 unless ($_local) {
Steven Grimmcec0d5a2007-11-29 11:54:39 -0800774 # rebase will checkout for us, so no need to do it explicitly
775 $_no_checkout = 'true';
Eric Wongdee41f32007-03-13 11:40:36 -0700776 $_fetch_all ? $gs->fetch_all : $gs->fetch;
777 }
Eric Wong905f8b72007-02-16 03:22:40 -0800778 command_noisy(rebase_cmd(), $gs->refname);
Eric Wong6111b932009-11-15 18:57:16 -0800779 $gs->mkemptydirs;
Eric Wong905f8b72007-02-16 03:22:40 -0800780}
781
Eric Wong5969cbe2007-01-11 17:58:39 -0800782sub cmd_show_ignore {
Eric Wong13c823f2007-04-08 00:59:19 -0700783 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
784 $gs ||= Git::SVN->new;
Eric Wong5969cbe2007-01-11 17:58:39 -0800785 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200786 $gs->prop_walk($gs->{path}, $r, sub {
787 my ($gs, $path, $props) = @_;
788 print STDOUT "\n# $path\n";
789 my $s = $props->{'svn:ignore'} or return;
790 $s =~ s/[\r\n]+/\n/g;
Michael Haggertya7d72542009-08-07 21:21:21 +0200791 $s =~ s/^\n+//;
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200792 chomp $s;
793 $s =~ s#^#$path#gm;
794 print STDOUT "$s\n";
795 });
Eric Wonga5e0ced2006-06-12 15:23:48 -0700796}
797
Vineet Kumar2d879792007-11-19 14:56:15 -0800798sub cmd_show_externals {
799 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
800 $gs ||= Git::SVN->new;
801 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
802 $gs->prop_walk($gs->{path}, $r, sub {
803 my ($gs, $path, $props) = @_;
804 print STDOUT "\n# $path\n";
805 my $s = $props->{'svn:externals'} or return;
806 $s =~ s/[\r\n]+/\n/g;
807 chomp $s;
808 $s =~ s#^#$path#gm;
809 print STDOUT "$s\n";
810 });
811}
812
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200813sub cmd_create_ignore {
814 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
815 $gs ||= Git::SVN->new;
816 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
817 $gs->prop_walk($gs->{path}, $r, sub {
818 my ($gs, $path, $props) = @_;
819 # $path is of the form /path/to/dir/
Brian Gernhardt7d9fd452009-02-19 13:08:04 -0500820 $path = '.' . $path;
821 # SVN can have attributes on empty directories,
822 # which git won't track
823 mkpath([$path]) unless -d $path;
824 my $ignore = $path . '.gitignore';
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200825 my $s = $props->{'svn:ignore'} or return;
826 open(GITIGNORE, '>', $ignore)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200827 or fatal("Failed to open `$ignore' for writing: $!");
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200828 $s =~ s/[\r\n]+/\n/g;
Michael Haggertya7d72542009-08-07 21:21:21 +0200829 $s =~ s/^\n+//;
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200830 chomp $s;
831 # Prefix all patterns so that the ignore doesn't apply
832 # to sub-directories.
833 $s =~ s#^#/#gm;
834 print GITIGNORE "$s\n";
835 close(GITIGNORE)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200836 or fatal("Failed to close `$ignore': $!");
Gustaf Hendebyc4c66b22008-05-05 00:33:09 +0200837 command_noisy('add', '-f', $ignore);
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200838 });
839}
840
Eric Wong6111b932009-11-15 18:57:16 -0800841sub cmd_mkdirs {
842 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
843 $gs ||= Git::SVN->new;
844 $gs->mkemptydirs($_revision);
845}
846
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800847sub canonicalize_path {
848 my ($path) = @_;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800849 my $dot_slash_added = 0;
850 if (substr($path, 0, 1) ne "/") {
851 $path = "./" . $path;
852 $dot_slash_added = 1;
853 }
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800854 # File::Spec->canonpath doesn't collapse x/../y into y (for a
855 # good reason), so let's do this manually.
856 $path =~ s#/+#/#g;
857 $path =~ s#/\.(?:/|$)#/#g;
858 $path =~ s#/[^/]+/\.\.##g;
859 $path =~ s#/$##g;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800860 $path =~ s#^\./## if $dot_slash_added;
Gerrit Pape2fe403e2008-07-06 19:28:50 +0000861 $path =~ s#^/##;
862 $path =~ s#^\.$##;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800863 return $path;
864}
865
Ulrich Dangel50ff2362009-06-26 16:52:09 +0200866sub canonicalize_url {
867 my ($url) = @_;
868 $url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
869 return $url;
870}
871
Benoit Sigoure15153452007-10-16 16:36:50 +0200872# get_svnprops(PATH)
873# ------------------
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200874# Helper for cmd_propget and cmd_proplist below.
Benoit Sigoure15153452007-10-16 16:36:50 +0200875sub get_svnprops {
876 my $path = shift;
877 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
878 $gs ||= Git::SVN->new;
879
880 # prefix THE PATH by the sub-directory from which the user
881 # invoked us.
882 $path = $cmd_dir_prefix . $path;
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200883 fatal("No such file or directory: $path") unless -e $path;
Benoit Sigoure15153452007-10-16 16:36:50 +0200884 my $is_dir = -d $path ? 1 : 0;
885 $path = $gs->{path} . '/' . $path;
886
887 # canonicalize the path (otherwise libsvn will abort or fail to
888 # find the file)
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800889 $path = canonicalize_path($path);
Benoit Sigoure15153452007-10-16 16:36:50 +0200890
891 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
892 my $props;
893 if ($is_dir) {
894 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
895 }
896 else {
897 (undef, $props) = $gs->ra->get_file($path, $r, undef);
898 }
899 return $props;
900}
901
902# cmd_propget (PROP, PATH)
903# ------------------------
904# Print the SVN property PROP for PATH.
905sub cmd_propget {
906 my ($prop, $path) = @_;
907 $path = '.' if not defined $path;
908 usage(1) if not defined $prop;
909 my $props = get_svnprops($path);
910 if (not defined $props->{$prop}) {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200911 fatal("`$path' does not have a `$prop' SVN property.");
Benoit Sigoure15153452007-10-16 16:36:50 +0200912 }
913 print $props->{$prop} . "\n";
914}
915
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200916# cmd_proplist (PATH)
917# -------------------
918# Print the list of SVN properties for PATH.
919sub cmd_proplist {
920 my $path = shift;
921 $path = '.' if not defined $path;
922 my $props = get_svnprops($path);
923 print "Properties on '$path':\n";
924 foreach (sort keys %{$props}) {
925 print " $_\n";
926 }
927}
928
Eric Wong8164b652007-01-11 15:35:55 -0800929sub cmd_multi_init {
Eric Wong9d55b412006-06-12 15:53:13 -0700930 my $url = shift;
Marc Branchaud62244062009-06-23 13:02:08 -0400931 unless (defined $_trunk || @_branches || @_tags) {
Eric Wong98327e52007-01-04 18:02:00 -0800932 usage(1);
933 }
Eric Wongdc431662007-05-19 03:59:02 -0700934
Eric Wong8164b652007-01-11 15:35:55 -0800935 $_prefix = '' unless defined $_prefix;
Eric Wongdadc6d22007-02-14 12:27:41 -0800936 if (defined $url) {
Ulrich Dangel50ff2362009-06-26 16:52:09 +0200937 $url = canonicalize_url($url);
Eric Wongdadc6d22007-02-14 12:27:41 -0800938 init_subdir(@_);
939 }
Eric Wongf30603f2007-02-23 01:26:26 -0800940 do_git_init_db();
Eric Wong98327e52007-01-04 18:02:00 -0800941 if (defined $_trunk) {
Adam Brewster6f5748e2009-08-11 23:14:27 -0400942 my $trunk_ref = 'refs/remotes/' . $_prefix . 'trunk';
Eric Wong706587f2007-01-18 17:50:01 -0800943 # try both old-style and new-style lookups:
944 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
Eric Wong8164b652007-01-11 15:35:55 -0800945 unless ($gs_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800946 my ($trunk_url, $trunk_path) =
947 complete_svn_url($url, $_trunk);
948 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
949 undef, $trunk_ref);
Eric Wong98327e52007-01-04 18:02:00 -0800950 }
Eric Wongc35b96e2006-10-11 11:53:21 -0700951 }
Marc Branchaud62244062009-06-23 13:02:08 -0400952 return unless @_branches || @_tags;
Eric Wonge7db67e2007-01-11 17:09:26 -0800953 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
Marc Branchaud62244062009-06-23 13:02:08 -0400954 foreach my $path (@_branches) {
955 complete_url_ls_init($ra, $path, '--branches/-b', $_prefix);
956 }
957 foreach my $path (@_tags) {
958 complete_url_ls_init($ra, $path, '--tags/-t', $_prefix.'tags/');
959 }
Eric Wong9d55b412006-06-12 15:53:13 -0700960}
961
Eric Wong1c8443b2007-01-14 02:17:00 -0800962sub cmd_multi_fetch {
Eric Wong4d0157d2009-11-22 12:37:06 -0800963 $Git::SVN::no_reuse_existing = undef;
Eric Wong0af9c9f2007-01-27 22:28:56 -0800964 my $remotes = Git::SVN::read_all_remotes();
965 foreach my $repo_id (sort keys %$remotes) {
Eric Wongdb03cd22007-02-13 00:38:02 -0800966 if ($remotes->{$repo_id}->{url}) {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800967 Git::SVN::fetch_all($repo_id, $remotes);
968 }
Eric Wong706587f2007-01-18 17:50:01 -0800969 }
Eric Wong9d55b412006-06-12 15:53:13 -0700970}
971
Eric Wong44320b92007-01-13 22:35:53 -0800972# this command is special because it requires no metadata
973sub cmd_commit_diff {
974 my ($ta, $tb, $url) = @_;
975 my $usage = "Usage: $0 commit-diff -r<revision> ".
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200976 "<tree-ish> <tree-ish> [<URL>]";
Eric Wong44320b92007-01-13 22:35:53 -0800977 fatal($usage) if (!defined $ta || !defined $tb);
Karl Hasselströmd72ab8c2008-05-17 17:07:09 +0200978 my $svn_path = '';
Eric Wong44320b92007-01-13 22:35:53 -0800979 if (!defined $url) {
980 my $gs = eval { Git::SVN->new };
981 if (!$gs) {
982 fatal("Needed URL or usable git-svn --id in ",
983 "the command-line\n", $usage);
984 }
985 $url = $gs->{url};
Eric Wongd3a840d2007-01-26 01:32:45 -0800986 $svn_path = $gs->{path};
Eric Wong44320b92007-01-13 22:35:53 -0800987 }
988 unless (defined $_revision) {
989 fatal("-r|--revision is a required argument\n", $usage);
990 }
991 if (defined $_message && defined $_file) {
992 fatal("Both --message/-m and --file/-F specified ",
993 "for the commit message.\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200994 "I have no idea what you mean");
Eric Wong44320b92007-01-13 22:35:53 -0800995 }
996 if (defined $_file) {
997 $_message = file_to_s($_file);
998 } else {
999 $_message ||= get_commit_entry($tb)->{log};
1000 }
1001 my $ra ||= Git::SVN::Ra->new($url);
1002 my $r = $_revision;
1003 if ($r eq 'HEAD') {
1004 $r = $ra->get_latest_revnum;
1005 } elsif ($r !~ /^\d+$/) {
1006 die "revision argument: $r not understood by git-svn\n";
1007 }
Eric Wong61395352007-01-27 14:33:08 -08001008 my %ed_opts = ( r => $r,
1009 log => $_message,
1010 ra => $ra,
1011 tree_a => $ta,
1012 tree_b => $tb,
1013 editor_cb => sub { print "Committed r$_[0]\n" },
1014 svn_path => $svn_path );
1015 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong44320b92007-01-13 22:35:53 -08001016 print "No changes\n$ta == $tb\n";
1017 }
Eric Wong44320b92007-01-13 22:35:53 -08001018}
1019
Thomas Rast05427b92008-08-26 21:32:37 +02001020sub escape_uri_only {
1021 my ($uri) = @_;
1022 my @tmp;
1023 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -07001024 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Thomas Rast05427b92008-08-26 21:32:37 +02001025 push @tmp, $_;
1026 }
1027 join('/', @tmp);
1028}
1029
1030sub escape_url {
1031 my ($url) = @_;
1032 if ($url =~ m#^([^:]+)://([^/]*)(.*)$#) {
1033 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
1034 $url = "$scheme://$domain$uri";
1035 }
1036 $url;
1037}
1038
David D. Kilzere6fefa92007-11-21 11:57:18 -08001039sub cmd_info {
Eric Wongbd2d4f92008-08-05 00:35:16 -07001040 my $path = canonicalize_path(defined($_[0]) ? $_[0] : ".");
Thomas Rastedde9112008-08-26 21:32:36 +02001041 my $fullpath = canonicalize_path($cmd_dir_prefix . $path);
Eric Wongbd2d4f92008-08-05 00:35:16 -07001042 if (exists $_[1]) {
David D. Kilzere6fefa92007-11-21 11:57:18 -08001043 die "Too many arguments specified\n";
1044 }
1045
1046 my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
1047
1048 if (!$file_type && !$diff_status) {
Thomas Rast2cf3e3a2008-08-29 15:42:48 +02001049 print STDERR "svn: '$path' is not under version control\n";
1050 exit 1;
David D. Kilzere6fefa92007-11-21 11:57:18 -08001051 }
1052
1053 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1054 unless ($gs) {
1055 die "Unable to determine upstream SVN information from ",
1056 "working tree history\n";
1057 }
Eric Wongbd2d4f92008-08-05 00:35:16 -07001058
1059 # canonicalize_path() will return "" to make libsvn 1.5.x happy,
1060 $path = "." if $path eq "";
1061
Thomas Rastedde9112008-08-26 21:32:36 +02001062 my $full_url = $url . ($fullpath eq "" ? "" : "/$fullpath");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001063
David D. Kilzer8b014d72007-11-21 11:57:19 -08001064 if ($_url) {
Thomas Rast05427b92008-08-26 21:32:37 +02001065 print escape_url($full_url), "\n";
David D. Kilzer8b014d72007-11-21 11:57:19 -08001066 return;
1067 }
1068
David D. Kilzere6fefa92007-11-21 11:57:18 -08001069 my $result = "Path: $path\n";
1070 $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
Thomas Rast05427b92008-08-26 21:32:37 +02001071 $result .= "URL: " . escape_url($full_url) . "\n";
David D. Kilzere6fefa92007-11-21 11:57:18 -08001072
Eric Wonga5460eb2007-11-21 18:20:57 -08001073 eval {
1074 my $repos_root = $gs->repos_root;
1075 Git::SVN::remove_username($repos_root);
Thomas Rast05427b92008-08-26 21:32:37 +02001076 $result .= "Repository Root: " . escape_url($repos_root) . "\n";
Eric Wonga5460eb2007-11-21 18:20:57 -08001077 };
1078 if ($@) {
1079 $result .= "Repository Root: (offline)\n";
1080 }
Marcel Koeppen22ba47f2009-01-19 03:02:01 +01001081 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" &&
1082 ($SVN::Core::VERSION le '1.5.4' || $file_type ne "dir");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001083 $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
1084
1085 $result .= "Node Kind: " .
1086 ($file_type eq "dir" ? "directory" : "file") . "\n";
1087
1088 my $schedule = $diff_status eq "A"
1089 ? "add"
1090 : ($diff_status eq "D" ? "delete" : "normal");
1091 $result .= "Schedule: $schedule\n";
1092
1093 if ($diff_status eq "A") {
1094 print $result, "\n";
1095 return;
1096 }
1097
1098 my ($lc_author, $lc_rev, $lc_date_utc);
Thomas Rastedde9112008-08-26 21:32:36 +02001099 my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001100 my $log = command_output_pipe(@args);
1101 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
1102 while (<$log>) {
1103 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
1104 $lc_author = $1;
1105 $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
1106 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
1107 (undef, $lc_rev, undef) = ::extract_metadata($1);
1108 }
1109 }
1110 close $log;
1111
1112 Git::SVN::Log::set_local_timezone();
1113
1114 $result .= "Last Changed Author: $lc_author\n";
1115 $result .= "Last Changed Rev: $lc_rev\n";
1116 $result .= "Last Changed Date: " .
1117 Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
1118
1119 if ($file_type ne "dir") {
1120 my $text_last_updated_date =
1121 ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
1122 $result .=
1123 "Text Last Updated: " .
1124 Git::SVN::Log::format_svn_date($text_last_updated_date) .
1125 "\n";
1126 my $checksum;
1127 if ($diff_status eq "D") {
1128 my ($fh, $ctx) =
1129 command_output_pipe(qw(cat-file blob), "HEAD:$path");
1130 if ($file_type eq "link") {
1131 my $file_name = <$fh>;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001132 $checksum = md5sum("link $file_name");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001133 } else {
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001134 $checksum = md5sum($fh);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001135 }
1136 command_close_pipe($fh, $ctx);
1137 } elsif ($file_type eq "link") {
1138 my $file_name =
1139 command(qw(cat-file blob), "HEAD:$path");
1140 $checksum =
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001141 md5sum("link " . $file_name);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001142 } else {
1143 open FILE, "<", $path or die $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001144 $checksum = md5sum(\*FILE);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001145 close FILE or die $!;
1146 }
1147 $result .= "Checksum: " . $checksum . "\n";
1148 }
1149
1150 print $result, "\n";
1151}
1152
Ben Jackson195643f2009-06-03 20:45:52 -07001153sub cmd_reset {
1154 my $target = shift || $_revision or die "SVN revision required\n";
1155 $target = $1 if $target =~ /^r(\d+)$/;
1156 $target =~ /^\d+$/ or die "Numeric SVN revision expected\n";
1157 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1158 unless ($gs) {
1159 die "Unable to determine upstream SVN information from ".
1160 "history\n";
1161 }
1162 my ($r, $c) = $gs->find_rev_before($target, not $_fetch_parent);
1163 $gs->rev_map_set($r, $c, 'reset', $uuid);
1164 print "r$r = $c ($gs->{ref_id})\n";
1165}
1166
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -05001167sub cmd_gc {
1168 if (!$can_compress) {
1169 warn "Compress::Zlib could not be found; unhandled.log " .
1170 "files will not be compressed.\n";
1171 }
1172 find({ wanted => \&gc_directory, no_chdir => 1}, "$ENV{GIT_DIR}/svn");
1173}
1174
Eric Wong3397f9d2006-02-16 01:24:16 -08001175########################### utility functions #########################
1176
Eric Wong905f8b72007-02-16 03:22:40 -08001177sub rebase_cmd {
1178 my @cmd = qw/rebase/;
1179 push @cmd, '-v' if $_verbose;
1180 push @cmd, qw/--merge/ if $_merge;
1181 push @cmd, "--strategy=$_strategy" if $_strategy;
1182 @cmd;
1183}
1184
Eric Wong1e889ef2007-02-16 01:45:13 -08001185sub post_fetch_checkout {
1186 return if $_no_checkout;
1187 my $gs = $Git::SVN::_head or return;
1188 return if verify_ref('refs/heads/master^0');
1189
Eric Wongb186a262009-08-12 16:01:59 -07001190 # look for "trunk" ref if it exists
1191 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
1192 my $fetch = $remote->{fetch};
1193 if ($fetch) {
1194 foreach my $p (keys %$fetch) {
1195 basename($fetch->{$p}) eq 'trunk' or next;
1196 $gs = Git::SVN->new($fetch->{$p}, $gs->{repo_id}, $p);
1197 last;
1198 }
1199 }
1200
Eric Wong1e889ef2007-02-16 01:45:13 -08001201 my $valid_head = verify_ref('HEAD^0');
1202 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
1203 return if ($valid_head || !verify_ref('HEAD^0'));
1204
1205 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
1206 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
1207 return if -f $index;
1208
Matthias Lederhofer7ae3df82007-06-03 16:48:16 +02001209 return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
Eric Wong1e889ef2007-02-16 01:45:13 -08001210 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
1211 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
1212 print STDERR "Checked out HEAD:\n ",
1213 $gs->full_url, " r", $gs->last_rev, "\n";
Eric Wong6111b932009-11-15 18:57:16 -08001214 $gs->mkemptydirs($gs->last_rev);
Eric Wong1e889ef2007-02-16 01:45:13 -08001215}
1216
Eric Wong98327e52007-01-04 18:02:00 -08001217sub complete_svn_url {
1218 my ($url, $path) = @_;
1219 $path =~ s#/+$##;
Eric Wong98327e52007-01-04 18:02:00 -08001220 if ($path !~ m#^[a-z\+]+://#) {
Eric Wong98327e52007-01-04 18:02:00 -08001221 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
1222 fatal("E: '$path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001223 "and a separate URL is not specified");
Eric Wong98327e52007-01-04 18:02:00 -08001224 }
Eric Wong706587f2007-01-18 17:50:01 -08001225 return ($url, $path);
Eric Wong98327e52007-01-04 18:02:00 -08001226 }
Eric Wong706587f2007-01-18 17:50:01 -08001227 return ($path, '');
Eric Wong98327e52007-01-04 18:02:00 -08001228}
1229
Eric Wong9d55b412006-06-12 15:53:13 -07001230sub complete_url_ls_init {
Eric Wong706587f2007-01-18 17:50:01 -08001231 my ($ra, $repo_path, $switch, $pfx) = @_;
1232 unless ($repo_path) {
Eric Wong9d55b412006-06-12 15:53:13 -07001233 print STDERR "W: $switch not specified\n";
1234 return;
1235 }
Eric Wong706587f2007-01-18 17:50:01 -08001236 $repo_path =~ s#/+$##;
1237 if ($repo_path =~ m#^[a-z\+]+://#) {
1238 $ra = Git::SVN::Ra->new($repo_path);
1239 $repo_path = '';
Eric Wonge7db67e2007-01-11 17:09:26 -08001240 } else {
Eric Wong706587f2007-01-18 17:50:01 -08001241 $repo_path =~ s#^/+##;
Eric Wonge7db67e2007-01-11 17:09:26 -08001242 unless ($ra) {
Eric Wong706587f2007-01-18 17:50:01 -08001243 fatal("E: '$repo_path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001244 "and a separate URL is not specified");
Eric Wong9d55b412006-06-12 15:53:13 -07001245 }
Eric Wonge7db67e2007-01-11 17:09:26 -08001246 }
Eric Wong706587f2007-01-18 17:50:01 -08001247 my $url = $ra->{url};
Eric Wongb4d57e52007-02-14 15:10:44 -08001248 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
1249 my $k = "svn-remote.$gs->{repo_id}.url";
1250 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
1251 if ($orig_url && ($orig_url ne $gs->{url})) {
1252 die "$k already set: $orig_url\n",
1253 "wanted to set to: $gs->{url}\n";
Eric Wong88cf4102007-02-01 03:59:07 -08001254 }
Eric Wongb4d57e52007-02-14 15:10:44 -08001255 command_oneline('config', $k, $gs->{url}) unless $orig_url;
Mattias Nissler0b2af452009-07-07 01:40:02 +02001256 my $remote_path = "$gs->{path}/$repo_path";
Eric Wong5268f9e2009-08-16 14:22:12 -07001257 $remote_path =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
Eric Wongb4d57e52007-02-14 15:10:44 -08001258 $remote_path =~ s#/+#/#g;
1259 $remote_path =~ s#^/##g;
Eric Wonged0b9d42008-03-14 11:01:23 -07001260 $remote_path .= "/*" if $remote_path !~ /\*/;
Eric Wongb4d57e52007-02-14 15:10:44 -08001261 my ($n) = ($switch =~ /^--(\w+)/);
1262 if (length $pfx && $pfx !~ m#/$#) {
1263 die "--prefix='$pfx' must have a trailing slash '/'\n";
Eric Wong9d55b412006-06-12 15:53:13 -07001264 }
Marcus Griep570d35c2008-08-08 01:41:57 -07001265 command_noisy('config',
Marc Branchaud62244062009-06-23 13:02:08 -04001266 '--add',
Marcus Griep570d35c2008-08-08 01:41:57 -07001267 "svn-remote.$gs->{repo_id}.$n",
1268 "$remote_path:refs/remotes/$pfx*" .
1269 ('/*' x (($remote_path =~ tr/*/*/) - 1)) );
Eric Wong9d55b412006-06-12 15:53:13 -07001270}
1271
Eric Wongaef4e922006-12-15 10:59:54 -08001272sub verify_ref {
1273 my ($ref) = @_;
Eric Wong2c5c1d52006-12-28 01:16:21 -08001274 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
1275 { STDERR => 0 }); };
Eric Wongaef4e922006-12-15 10:59:54 -08001276}
1277
Eric Wonga5e0ced2006-06-12 15:23:48 -07001278sub get_tree_from_treeish {
Eric Wongcf52b8f2006-02-20 10:57:28 -08001279 my ($treeish) = @_;
Eric Wong44320b92007-01-13 22:35:53 -08001280 # $treeish can be a symbolic ref, too:
Eric Wongaef4e922006-12-15 10:59:54 -08001281 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001282 my $expected;
1283 while ($type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001284 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001285 }
1286 if ($type eq 'commit') {
Eric Wongaef4e922006-12-15 10:59:54 -08001287 $expected = (grep /^tree /, command(qw/cat-file commit/,
1288 $treeish))[0];
Eric Wong44320b92007-01-13 22:35:53 -08001289 ($expected) = ($expected =~ /^tree ($sha1)$/o);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001290 die "Unable to get tree from $treeish\n" unless $expected;
1291 } elsif ($type eq 'tree') {
1292 $expected = $treeish;
1293 } else {
1294 die "$treeish is a $type, expected tree, tag or commit\n";
1295 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07001296 return $expected;
1297}
Eric Wongcf52b8f2006-02-20 10:57:28 -08001298
Eric Wong44320b92007-01-13 22:35:53 -08001299sub get_commit_entry {
1300 my ($treeish) = shift;
1301 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
1302 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1303 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1304 open my $log_fh, '>', $commit_editmsg or croak $!;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001305
Eric Wong44320b92007-01-13 22:35:53 -08001306 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wong4ad45152006-07-09 20:20:48 -07001307 if ($type eq 'commit' || $type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001308 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
Eric Wong44320b92007-01-13 22:35:53 -08001309 $type, $treeish);
Eric Wong3397f9d2006-02-16 01:24:16 -08001310 my $in_msg = 0;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001311 my $author;
1312 my $saw_from = 0;
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001313 my $msgbuf = "";
Eric Wong3397f9d2006-02-16 01:24:16 -08001314 while (<$msg_fh>) {
1315 if (!$in_msg) {
1316 $in_msg = 1 if (/^\s*$/);
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001317 $author = $1 if (/^author (.*>)/);
Eric Wongdf746c52006-03-03 01:20:08 -08001318 } elsif (/^git-svn-id: /) {
Eric Wong44320b92007-01-13 22:35:53 -08001319 # skip this for now, we regenerate the
1320 # correct one on re-fetch anyways
1321 # TODO: set *:merge properties or like...
Eric Wong3397f9d2006-02-16 01:24:16 -08001322 } else {
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001323 if (/^From:/ || /^Signed-off-by:/) {
1324 $saw_from = 1;
1325 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001326 $msgbuf .= $_;
Eric Wong3397f9d2006-02-16 01:24:16 -08001327 }
1328 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001329 $msgbuf =~ s/\s+$//s;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001330 if ($Git::SVN::_add_author_from && defined($author)
1331 && !$saw_from) {
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001332 $msgbuf .= "\n\nFrom: $author";
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001333 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001334 print $log_fh $msgbuf or croak $!;
Eric Wongaef4e922006-12-15 10:59:54 -08001335 command_close_pipe($msg_fh, $ctx);
Eric Wong3397f9d2006-02-16 01:24:16 -08001336 }
Eric Wong44320b92007-01-13 22:35:53 -08001337 close $log_fh or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -08001338
1339 if ($_edit || ($type eq 'tree')) {
Jonathan Niederb4479f02009-10-30 20:42:34 -05001340 chomp(my $editor = command_oneline(qw(var GIT_EDITOR)));
1341 system('sh', '-c', $editor.' "$@"', $editor, $commit_editmsg);
Eric Wong3397f9d2006-02-16 01:24:16 -08001342 }
Eric Wong44320b92007-01-13 22:35:53 -08001343 rename $commit_editmsg, $commit_msg or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07001344 {
Eric Wongb510df82009-05-28 00:56:23 -07001345 require Encode;
Eric Wong16fc08e2008-10-29 23:49:26 -07001346 # SVN requires messages to be UTF-8 when entering the repo
1347 local $/;
1348 open $log_fh, '<', $commit_msg or croak $!;
1349 binmode $log_fh;
1350 chomp($log_entry{log} = <$log_fh>);
1351
Eric Wongb510df82009-05-28 00:56:23 -07001352 my $enc = Git::config('i18n.commitencoding') || 'UTF-8';
1353 my $msg = $log_entry{log};
1354
1355 eval { $msg = Encode::decode($enc, $msg, 1) };
1356 if ($@) {
1357 die "Could not decode as $enc:\n", $msg,
1358 "\nPerhaps you need to set i18n.commitencoding\n";
Eric Wong16fc08e2008-10-29 23:49:26 -07001359 }
Eric Wongb510df82009-05-28 00:56:23 -07001360
1361 eval { $msg = Encode::encode('UTF-8', $msg, 1) };
1362 die "Could not encode as UTF-8:\n$msg\n" if $@;
1363
1364 $log_entry{log} = $msg;
1365
Eric Wong16fc08e2008-10-29 23:49:26 -07001366 close $log_fh or croak $!;
1367 }
Eric Wong44320b92007-01-13 22:35:53 -08001368 unlink $commit_msg;
1369 \%log_entry;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001370}
1371
Eric Wong3397f9d2006-02-16 01:24:16 -08001372sub s_to_file {
1373 my ($str, $file, $mode) = @_;
1374 open my $fd,'>',$file or croak $!;
1375 print $fd $str,"\n" or croak $!;
1376 close $fd or croak $!;
1377 chmod ($mode &~ umask, $file) if (defined $mode);
1378}
1379
1380sub file_to_s {
1381 my $file = shift;
1382 open my $fd,'<',$file or croak "$!: file: $file\n";
1383 local $/;
1384 my $ret = <$fd>;
1385 close $fd or croak $!;
1386 $ret =~ s/\s*$//s;
1387 return $ret;
1388}
1389
Eric Wongeeb0abe2006-03-03 01:20:08 -08001390# '<svn username> = real-name <email address>' mapping based on git-svnimport:
1391sub load_authors {
1392 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001393 my $log = $cmd eq 'log';
Eric Wongeeb0abe2006-03-03 01:20:08 -08001394 while (<$authors>) {
1395 chomp;
Richard MUSIL575d0252007-07-17 19:02:57 +02001396 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
Eric Wongeeb0abe2006-03-03 01:20:08 -08001397 my ($user, $name, $email) = ($1, $2, $3);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001398 if ($log) {
1399 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
1400 } else {
1401 $users{$user} = [$name, $email];
1402 }
Eric Wong79bb8d82006-06-01 02:35:44 -07001403 }
1404 close $authors or croak $!;
1405}
1406
Tom Princee0d10e12007-01-28 16:16:53 -08001407# convert GetOpt::Long specs for use by git-config
Eric Wong1a305822009-11-14 14:25:11 -08001408sub read_git_config {
Eric Wongb8c92ca2006-05-24 01:40:37 -07001409 my $opts = shift;
Eric Wong97ae0912007-02-11 15:21:24 -08001410 my @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001411 foreach my $o (keys %$opts) {
Eric Wong97ae0912007-02-11 15:21:24 -08001412 # if we have mixedCase and a long option-only, then
1413 # it's a config-only variable that we don't need for
1414 # the command-line.
1415 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001416 my $v = $opts->{$o};
Eric Wong97ae0912007-02-11 15:21:24 -08001417 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001418 $key =~ s/-//g;
Deskin Miller225f1d02008-10-23 15:21:34 -04001419 my $arg = 'git config';
Eric Wongb8c92ca2006-05-24 01:40:37 -07001420 $arg .= ' --int' if ($o =~ /[:=]i$/);
1421 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1422 if (ref $v eq 'ARRAY') {
1423 chomp(my @tmp = `$arg --get-all svn.$key`);
1424 @$v = @tmp if @tmp;
1425 } else {
1426 chomp(my $tmp = `$arg --get svn.$key`);
Eric Wong77742842007-02-10 21:07:12 -08001427 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
Eric Wongb8c92ca2006-05-24 01:40:37 -07001428 $$v = $tmp;
1429 }
1430 }
1431 }
Eric Wong97ae0912007-02-11 15:21:24 -08001432 delete @$opts{@config_only} if @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001433}
1434
Eric Wong79bb8d82006-06-01 02:35:44 -07001435sub extract_metadata {
Eric Wongc1927a82006-06-27 19:39:11 -07001436 my $id = shift or return (undef, undef, undef);
Sam Vilain3dfab992007-06-30 20:56:13 +12001437 my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
Eric Wongb3e95932009-07-11 14:13:12 -07001438 \s([a-f\d\-]+)$/ix);
Eric Wonge70dc782006-11-23 14:54:04 -08001439 if (!defined $rev || !$uuid || !$url) {
Eric Wong79bb8d82006-06-01 02:35:44 -07001440 # some of the original repositories I made had
Pavel Roskin82e5a822006-07-10 01:50:18 -04001441 # identifiers like this:
Eric Wongb3e95932009-07-11 14:13:12 -07001442 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/i);
Eric Wong79bb8d82006-06-01 02:35:44 -07001443 }
1444 return ($url, $rev, $uuid);
1445}
1446
Eric Wongc1927a82006-06-27 19:39:11 -07001447sub cmt_metadata {
1448 return extract_metadata((grep(/^git-svn-id: /,
Eric Wongaef4e922006-12-15 10:59:54 -08001449 command(qw/cat-file commit/, shift)))[-1]);
Eric Wongc1927a82006-06-27 19:39:11 -07001450}
1451
Boris Byk6ea42032009-04-11 00:32:41 +04001452sub cmt_sha2rev_batch {
1453 my %s2r;
1454 my ($pid, $in, $out, $ctx) = command_bidi_pipe(qw/cat-file --batch/);
1455 my $list = shift;
1456
1457 foreach my $sha (@{$list}) {
1458 my $first = 1;
1459 my $size = 0;
1460 print $out $sha, "\n";
1461
1462 while (my $line = <$in>) {
1463 if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) {
1464 last;
1465 } elsif ($first &&
1466 $line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) {
1467 $first = 0;
1468 $size = $1;
1469 next;
1470 } elsif ($line =~ /^(git-svn-id: )/) {
1471 my (undef, $rev, undef) =
1472 extract_metadata($line);
1473 $s2r{$sha} = $rev;
1474 }
1475
1476 $size -= length($line);
1477 last if ($size == 0);
1478 }
1479 }
1480
1481 command_close_bidi_pipe($pid, $in, $out, $ctx);
1482
1483 return \%s2r;
1484}
1485
Eric Wong905f8b72007-02-16 03:22:40 -08001486sub working_head_info {
1487 my ($head, $refs) = @_;
Pedro Melob6309ac2008-04-10 17:05:21 +01001488 my @args = ('log', '--no-color', '--first-parent', '--pretty=medium');
Lars Hjemli05b4df32007-09-05 11:35:29 +02001489 my ($fh, $ctx) = command_output_pipe(@args, $head);
Sam Vilain3dfab992007-06-30 20:56:13 +12001490 my $hash;
Sam Vilain40cb8f82007-06-30 20:56:14 +12001491 my %max;
Sam Vilain3dfab992007-06-30 20:56:13 +12001492 while (<$fh>) {
1493 if ( m{^commit ($::sha1)$} ) {
1494 unshift @$refs, $hash if $hash and $refs;
1495 $hash = $1;
1496 next;
1497 }
1498 next unless s{^\s*(git-svn-id:)}{$1};
1499 my ($url, $rev, $uuid) = extract_metadata($_);
Eric Wong13c823f2007-04-08 00:59:19 -07001500 if (defined $url && defined $rev) {
Sam Vilain40cb8f82007-06-30 20:56:14 +12001501 next if $max{$url} and $max{$url} < $rev;
Eric Wong13c823f2007-04-08 00:59:19 -07001502 if (my $gs = Git::SVN->find_by_url($url)) {
João Abecasis63c56022008-07-14 16:28:04 +01001503 my $c = $gs->rev_map_get($rev, $uuid);
Adam Robenb03c7a62007-04-25 11:50:32 -07001504 if ($c && $c eq $hash) {
Eric Wong13c823f2007-04-08 00:59:19 -07001505 close $fh; # break the pipe
1506 return ($url, $rev, $uuid, $gs);
Sam Vilain40cb8f82007-06-30 20:56:14 +12001507 } else {
Eric Wong060610c2007-12-08 23:27:41 -08001508 $max{$url} ||= $gs->rev_map_max;
Eric Wong13c823f2007-04-08 00:59:19 -07001509 }
1510 }
1511 }
Eric Wong905f8b72007-02-16 03:22:40 -08001512 }
Eric Wong13c823f2007-04-08 00:59:19 -07001513 command_close_pipe($fh, $ctx);
1514 (undef, undef, undef, undef);
Eric Wong905f8b72007-02-16 03:22:40 -08001515}
1516
Eric Wong733a65a2007-06-13 02:23:28 -07001517sub read_commit_parents {
1518 my ($parents, $c) = @_;
Eric Wong7b02b852007-09-08 16:33:08 -07001519 chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
1520 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1521 @{$parents->{$c}} = split(/ /, $p);
Eric Wong733a65a2007-06-13 02:23:28 -07001522}
1523
1524sub linearize_history {
1525 my ($gs, $refs) = @_;
1526 my %parents;
1527 foreach my $c (@$refs) {
1528 read_commit_parents(\%parents, $c);
1529 }
1530
1531 my @linear_refs;
1532 my %skip = ();
1533 my $last_svn_commit = $gs->last_commit;
1534 foreach my $c (reverse @$refs) {
1535 next if $c eq $last_svn_commit;
1536 last if $skip{$c};
1537
1538 unshift @linear_refs, $c;
1539 $skip{$c} = 1;
1540
1541 # we only want the first parent to diff against for linear
1542 # history, we save the rest to inject when we finalize the
1543 # svn commit
1544 my $fp_a = verify_ref("$c~1");
1545 my $fp_b = shift @{$parents{$c}} if $parents{$c};
1546 if (!$fp_a || !$fp_b) {
1547 die "Commit $c\n",
1548 "has no parent commit, and therefore ",
1549 "nothing to diff against.\n",
1550 "You should be working from a repository ",
1551 "originally created by git-svn\n";
1552 }
1553 if ($fp_a ne $fp_b) {
1554 die "$c~1 = $fp_a, however parsing commit $c ",
1555 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1556 }
1557
1558 foreach my $p (@{$parents{$c}}) {
1559 $skip{$p} = 1;
1560 }
1561 }
1562 (\@linear_refs, \%parents);
1563}
1564
David D. Kilzere6fefa92007-11-21 11:57:18 -08001565sub find_file_type_and_diff_status {
1566 my ($path) = @_;
Dmitry Potapov107cee52008-07-21 00:14:07 +04001567 return ('dir', '') if $path eq '';
David D. Kilzere6fefa92007-11-21 11:57:18 -08001568
1569 my $diff_output =
1570 command_oneline(qw(diff --cached --name-status --), $path) || "";
1571 my $diff_status = (split(' ', $diff_output))[0] || "";
1572
1573 my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || "";
1574
1575 return (undef, undef) if !$diff_status && !$ls_tree;
1576
1577 if ($diff_status eq "A") {
1578 return ("link", $diff_status) if -l $path;
1579 return ("dir", $diff_status) if -d $path;
1580 return ("file", $diff_status);
1581 }
1582
1583 my $mode = (split(' ', $ls_tree))[0] || "";
1584
1585 return ("link", $diff_status) if $mode eq "120000";
1586 return ("dir", $diff_status) if $mode eq "040000";
1587 return ("file", $diff_status);
1588}
1589
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001590sub md5sum {
1591 my $arg = shift;
1592 my $ref = ref $arg;
1593 my $md5 = Digest::MD5->new();
Marcus Griep0b191382008-08-12 12:00:53 -04001594 if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001595 $md5->addfile($arg) or croak $!;
1596 } elsif ($ref eq 'SCALAR') {
1597 $md5->add($$arg) or croak $!;
1598 } elsif (!$ref) {
1599 $md5->add($arg) or croak $!;
1600 } else {
1601 ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1602 }
1603 return $md5->hexdigest();
1604}
1605
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -05001606sub gc_directory {
1607 if ($can_compress && -f $_ && basename($_) eq "unhandled.log") {
1608 my $out_filename = $_ . ".gz";
1609 open my $in_fh, "<", $_ or die "Unable to open $_: $!\n";
1610 binmode $in_fh;
1611 my $gz = Compress::Zlib::gzopen($out_filename, "ab") or
1612 die "Unable to open $out_filename: $!\n";
1613
1614 my $res;
1615 while ($res = sysread($in_fh, my $str, 1024)) {
1616 $gz->gzwrite($str) or
1617 die "Unable to write: ".$gz->gzerror()."!\n";
1618 }
1619 unlink $_ or die "unlink $File::Find::name: $!\n";
1620 } elsif (-f $_ && basename($_) eq "index") {
1621 unlink $_ or die "unlink $_: $!\n";
1622 }
1623}
1624
Eric Wong9b981fc2007-01-11 12:14:21 -08001625package Git::SVN;
1626use strict;
1627use warnings;
Eric Wong060610c2007-12-08 23:27:41 -08001628use Fcntl qw/:DEFAULT :seek/;
1629use constant rev_map_fmt => 'NH40';
Eric Wongecc712d2007-01-31 12:28:10 -08001630use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
Eric Wong62e349d2007-02-16 19:57:29 -08001631 $_repack $_repack_flags $_use_svm_props $_head
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00001632 $_use_svnsync_props $no_reuse_existing $_minimize_url
Pete Harlane82f0d72009-01-17 20:10:14 -08001633 $_use_log_author $_add_author_from $_localtime/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001634use Carp qw/croak/;
1635use File::Path qw/mkpath/;
Eric Wong373274f2007-01-31 13:54:23 -08001636use File::Copy qw/copy/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001637use IPC::Open3;
Sam Vilain7d944c32009-12-20 00:55:13 +13001638use Memoize; # core since 5.8.0, Jul 2002
Eric Wong9b981fc2007-01-11 12:14:21 -08001639
Karl Hasselström94bc9142008-02-03 17:56:18 +01001640my ($_gc_nr, $_gc_period);
1641
Eric Wong9b981fc2007-01-11 12:14:21 -08001642# properties that we do not log:
1643my %SKIP_PROP;
1644BEGIN {
1645 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
1646 svn:special svn:executable
1647 svn:entry:committed-rev
1648 svn:entry:last-author
1649 svn:entry:uuid
1650 svn:entry:committed-date/;
Eric Wong91b03282007-02-11 00:51:33 -08001651
1652 # some options are read globally, but can be overridden locally
1653 # per [svn-remote "..."] section. Command-line options will *NOT*
1654 # override options set in an [svn-remote "..."] section
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001655 no strict 'refs';
1656 for my $option (qw/follow_parent no_metadata use_svm_props
1657 use_svnsync_props/) {
1658 my $key = $option;
Eric Wong91b03282007-02-11 00:51:33 -08001659 $key =~ tr/_//d;
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001660 my $prop = "-$option";
1661 *$option = sub {
1662 my ($self) = @_;
1663 return $self->{$prop} if exists $self->{$prop};
1664 my $k = "svn-remote.$self->{repo_id}.$key";
1665 eval { command_oneline(qw/config --get/, $k) };
1666 if ($@) {
1667 $self->{$prop} = ${"Git::SVN::_$option"};
Eric Wong91b03282007-02-11 00:51:33 -08001668 } else {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001669 my $v = command_oneline(qw/config --bool/,$k);
1670 $self->{$prop} = $v eq 'false' ? 0 : 1;
Eric Wong91b03282007-02-11 00:51:33 -08001671 }
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001672 return $self->{$prop};
1673 }
Eric Wong91b03282007-02-11 00:51:33 -08001674 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001675}
1676
Marcus Griep0b191382008-08-12 12:00:53 -04001677
Eric Wong321b1842008-01-02 10:10:03 -08001678my (%LOCKFILES, %INDEX_FILES);
1679END {
1680 unlink keys %LOCKFILES if %LOCKFILES;
1681 unlink keys %INDEX_FILES if %INDEX_FILES;
1682}
Eric Wong373274f2007-01-31 13:54:23 -08001683
Eric Wong4bb9ed02007-02-03 13:29:17 -08001684sub resolve_local_globs {
1685 my ($url, $fetch, $glob_spec) = @_;
1686 return unless defined $glob_spec;
1687 my $ref = $glob_spec->{ref};
1688 my $path = $glob_spec->{path};
Adam Brewster6f5748e2009-08-11 23:14:27 -04001689 foreach (command(qw#for-each-ref --format=%(refname) refs/#)) {
1690 next unless m#^$ref->{regex}$#;
Eric Wong4bb9ed02007-02-03 13:29:17 -08001691 my $p = $1;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001692 my $pathname = desanitize_refname($path->full_path($p));
1693 my $refname = desanitize_refname($ref->full_path($p));
Eric Wong4bb9ed02007-02-03 13:29:17 -08001694 if (my $existing = $fetch->{$pathname}) {
1695 if ($existing ne $refname) {
1696 die "Refspec conflict:\n",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001697 "existing: $existing\n",
1698 " globbed: $refname\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08001699 }
Adam Brewster6f5748e2009-08-11 23:14:27 -04001700 my $u = (::cmt_metadata("$refname"))[0];
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001701 $u =~ s!^\Q$url\E(/|$)!! or die
Adam Brewster6f5748e2009-08-11 23:14:27 -04001702 "$refname: '$url' not found in '$u'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08001703 if ($pathname ne $u) {
1704 warn "W: Refspec glob conflict ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001705 "(ref: $refname):\n",
Eric Wong4bb9ed02007-02-03 13:29:17 -08001706 "expected path: $pathname\n",
1707 " real path: $u\n",
1708 "Continuing ahead with $u\n";
1709 next;
1710 }
1711 } else {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001712 $fetch->{$pathname} = $refname;
1713 }
1714 }
1715}
1716
Eric Wonge98671e2007-02-14 02:21:19 -08001717sub parse_revision_argument {
1718 my ($base, $head) = @_;
1719 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
1720 return ($base, $head);
1721 }
1722 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
1723 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
1724 return ($head, $head) if ($::_revision eq 'HEAD');
1725 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
1726 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
1727 die "revision argument: $::_revision not understood by git-svn\n";
1728}
1729
Eric Wong0af9c9f2007-01-27 22:28:56 -08001730sub fetch_all {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001731 my ($repo_id, $remotes) = @_;
Eric Wong905f8b72007-02-16 03:22:40 -08001732 if (ref $repo_id) {
1733 my $gs = $repo_id;
1734 $repo_id = undef;
1735 $repo_id = $gs->{repo_id};
1736 }
1737 $remotes ||= read_all_remotes();
Eric Wong7447b4b2007-02-14 18:38:46 -08001738 my $remote = $remotes->{$repo_id} or
1739 die "[svn-remote \"$repo_id\"] unknown\n";
Eric Wonge5181922007-02-08 12:53:57 -08001740 my $fetch = $remote->{fetch};
Eric Wong7447b4b2007-02-14 18:38:46 -08001741 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
Eric Wonge5181922007-02-08 12:53:57 -08001742 my (@gs, @globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001743 my $ra = Git::SVN::Ra->new($url);
Eric Wong26a62d52007-02-12 13:25:25 -08001744 my $uuid = $ra->get_uuid;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001745 my $head = $ra->get_latest_revnum;
Eric Wong577e9fc2009-12-21 02:06:04 -08001746
1747 # ignore errors, $head revision may not even exist anymore
1748 eval { $ra->get_log("", $head, 0, 1, 0, 1, sub { $head = $_[1] }) };
1749 warn "W: $@\n" if $@;
1750
Eric Wong28710f72007-02-14 13:32:21 -08001751 my $base = defined $fetch ? $head : 0;
Eric Wonge5181922007-02-08 12:53:57 -08001752
1753 # read the max revs for wildcard expansion (branches/*, tags/*)
1754 foreach my $t (qw/branches tags/) {
1755 defined $remote->{$t} or next;
Marc Branchaud62244062009-06-23 13:02:08 -04001756 push @globs, @{$remote->{$t}};
1757
Eric Wong93f26892007-02-11 01:20:26 -08001758 my $max_rev = eval { tmp_config(qw/--int --get/,
1759 "svn-remote.$repo_id.${t}-maxRev") };
1760 if (defined $max_rev && ($max_rev < $base)) {
1761 $base = $max_rev;
Eric Wongd6d33462007-02-16 04:05:33 -08001762 } elsif (!defined $max_rev) {
1763 $base = 0;
Eric Wonge5181922007-02-08 12:53:57 -08001764 }
1765 }
1766
Eric Wongdb03cd22007-02-13 00:38:02 -08001767 if ($fetch) {
1768 foreach my $p (sort keys %$fetch) {
1769 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
Eric Wong060610c2007-12-08 23:27:41 -08001770 my $lr = $gs->rev_map_max;
Eric Wongdb03cd22007-02-13 00:38:02 -08001771 if (defined $lr) {
1772 $base = $lr if ($lr < $base);
1773 }
1774 push @gs, $gs;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001775 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08001776 }
Eric Wonge98671e2007-02-14 02:21:19 -08001777
1778 ($base, $head) = parse_revision_argument($base, $head);
Eric Wonge5181922007-02-08 12:53:57 -08001779 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001780}
1781
Eric Wong47e39c52007-01-21 04:27:09 -08001782sub read_all_remotes {
1783 my $r = {};
João Abecasis63c56022008-07-14 16:28:04 +01001784 my $use_svm_props = eval { command_oneline(qw/config --bool
1785 svn.useSvmProps/) };
1786 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
Eric Wongffd5c8e2009-10-22 23:39:04 -07001787 my $svn_refspec = qr{\s*(.*?)\s*:\s*(.+?)\s*};
Eric Wong8b8fc062007-01-22 11:44:57 -08001788 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
Adam Brewster6f5748e2009-08-11 23:14:27 -04001789 if (m!^(.+)\.fetch=$svn_refspec$!) {
1790 my ($remote, $local_ref, $remote_ref) = ($1, $2, $3);
1791 die("svn-remote.$remote: remote ref '$remote_ref' "
1792 . "must start with 'refs/'\n")
1793 unless $remote_ref =~ m{^refs/};
Eric Wong46cf98b2007-07-14 12:40:32 -07001794 $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
João Abecasis63c56022008-07-14 16:28:04 +01001795 $r->{$remote}->{svm} = {} if $use_svm_props;
1796 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
1797 $r->{$1}->{svm} = {};
Eric Wong47e39c52007-01-21 04:27:09 -08001798 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1799 $r->{$1}->{url} = $2;
Adam Brewster6f5748e2009-08-11 23:14:27 -04001800 } elsif (m!^(.+)\.(branches|tags)=$svn_refspec$!) {
1801 my ($remote, $t, $local_ref, $remote_ref) =
1802 ($1, $2, $3, $4);
1803 die("svn-remote.$remote: remote ref '$remote_ref' ($t) "
1804 . "must start with 'refs/'\n")
1805 unless $remote_ref =~ m{^refs/};
Marc Branchaud62244062009-06-23 13:02:08 -04001806 my $rs = {
Adam Brewster6f5748e2009-08-11 23:14:27 -04001807 t => $t,
1808 remote => $remote,
1809 path => Git::SVN::GlobSpec->new($local_ref),
1810 ref => Git::SVN::GlobSpec->new($remote_ref) };
Eric Wong4bb9ed02007-02-03 13:29:17 -08001811 if (length($rs->{ref}->{right}) != 0) {
1812 die "The '*' glob character must be the last ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001813 "character of '$remote_ref'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08001814 }
Adam Brewster6f5748e2009-08-11 23:14:27 -04001815 push @{ $r->{$remote}->{$t} }, $rs;
Eric Wong47e39c52007-01-21 04:27:09 -08001816 }
1817 }
João Abecasis63c56022008-07-14 16:28:04 +01001818
1819 map {
1820 if (defined $r->{$_}->{svm}) {
1821 my $svm;
1822 eval {
1823 my $section = "svn-remote.$_";
1824 $svm = {
1825 source => tmp_config('--get',
1826 "$section.svm-source"),
1827 replace => tmp_config('--get',
1828 "$section.svm-replace"),
1829 }
1830 };
1831 $r->{$_}->{svm} = $svm;
1832 }
1833 } keys %$r;
1834
Eric Wong47e39c52007-01-21 04:27:09 -08001835 $r;
1836}
1837
Eric Wongecc712d2007-01-31 12:28:10 -08001838sub init_vars {
Karl Hasselström94bc9142008-02-03 17:56:18 +01001839 $_gc_nr = $_gc_period = 1000;
Karl Hasselströmaf788a62008-02-03 17:56:12 +01001840 if (defined $_repack || defined $_repack_flags) {
1841 warn "Repack options are obsolete; they have no effect.\n";
1842 }
Eric Wongecc712d2007-01-31 12:28:10 -08001843}
1844
Eric Wongb805b442007-01-22 13:52:04 -08001845sub verify_remotes_sanity {
Eric Wong536c4b02007-01-23 11:35:53 -08001846 return unless -d $ENV{GIT_DIR};
Eric Wongb805b442007-01-22 13:52:04 -08001847 my %seen;
1848 foreach (command(qw/config -l/)) {
1849 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1850 if ($seen{$1}) {
1851 die "Remote ref refs/remote/$1 is tracked by",
1852 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1853 "Please resolve this ambiguity in ",
1854 "your git configuration file before ",
1855 "continuing\n";
1856 }
1857 $seen{$1} = $_;
1858 }
1859 }
1860}
1861
Eric Wonge6434f82007-01-23 16:29:23 -08001862sub find_existing_remote {
1863 my ($url, $remotes) = @_;
Eric Wongbefc9ad2007-02-17 02:53:07 -08001864 return undef if $no_reuse_existing;
Eric Wonge6434f82007-01-23 16:29:23 -08001865 my $existing;
1866 foreach my $repo_id (keys %$remotes) {
1867 my $u = $remotes->{$repo_id}->{url} or next;
1868 next if $u ne $url;
1869 $existing = $repo_id;
1870 last;
1871 }
1872 $existing;
1873}
1874
1875sub init_remote_config {
Eric Wongd8115c52007-02-01 03:30:31 -08001876 my ($self, $url, $no_write) = @_;
Eric Wonge6434f82007-01-23 16:29:23 -08001877 $url =~ s!/+$!!; # strip trailing slash
1878 my $r = read_all_remotes();
1879 my $existing = find_existing_remote($url, $r);
1880 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001881 unless ($no_write) {
1882 print STDERR "Using existing ",
1883 "[svn-remote \"$existing\"]\n";
1884 }
Eric Wonge6434f82007-01-23 16:29:23 -08001885 $self->{repo_id} = $existing;
Eric Wong4a1bb4c2007-05-13 09:58:14 -07001886 } elsif ($_minimize_url) {
Eric Wonge6434f82007-01-23 16:29:23 -08001887 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
1888 $existing = find_existing_remote($min_url, $r);
1889 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001890 unless ($no_write) {
1891 print STDERR "Using existing ",
1892 "[svn-remote \"$existing\"]\n";
1893 }
Eric Wonge6434f82007-01-23 16:29:23 -08001894 $self->{repo_id} = $existing;
1895 }
1896 if ($min_url ne $url) {
Eric Wonge5181922007-02-08 12:53:57 -08001897 unless ($no_write) {
1898 print STDERR "Using higher level of URL: ",
1899 "$url => $min_url\n";
1900 }
Eric Wonge6434f82007-01-23 16:29:23 -08001901 my $old_path = $self->{path};
1902 $self->{path} = $url;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001903 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
Eric Wonge6434f82007-01-23 16:29:23 -08001904 if (length $old_path) {
1905 $self->{path} .= "/$old_path";
1906 }
1907 $url = $min_url;
1908 }
1909 }
1910 my $orig_url;
1911 if (!$existing) {
1912 # verify that we aren't overwriting anything:
1913 $orig_url = eval {
1914 command_oneline('config', '--get',
1915 "svn-remote.$self->{repo_id}.url")
1916 };
1917 if ($orig_url && ($orig_url ne $url)) {
1918 die "svn-remote.$self->{repo_id}.url already set: ",
1919 "$orig_url\nwanted to set to: $url\n";
1920 }
1921 }
1922 my ($xrepo_id, $xpath) = find_ref($self->refname);
Adam Brewster6f5748e2009-08-11 23:14:27 -04001923 if (!$no_write && defined $xpath) {
Eric Wonge6434f82007-01-23 16:29:23 -08001924 die "svn-remote.$xrepo_id.fetch already set to track ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001925 "$xpath:", $self->refname, "\n";
Eric Wonge6434f82007-01-23 16:29:23 -08001926 }
Eric Wongd8115c52007-02-01 03:30:31 -08001927 unless ($no_write) {
1928 command_noisy('config',
1929 "svn-remote.$self->{repo_id}.url", $url);
Eric Wong46cf98b2007-07-14 12:40:32 -07001930 $self->{path} =~ s{^/}{};
Eric Wong5268f9e2009-08-16 14:22:12 -07001931 $self->{path} =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
Eric Wongd8115c52007-02-01 03:30:31 -08001932 command_noisy('config', '--add',
1933 "svn-remote.$self->{repo_id}.fetch",
1934 "$self->{path}:".$self->refname);
1935 }
Eric Wonge6434f82007-01-23 16:29:23 -08001936 $self->{url} = $url;
1937}
1938
Eric Wonga8ae2622007-02-13 14:22:11 -08001939sub find_by_url { # repos_root and, path are optional
1940 my ($class, $full_url, $repos_root, $path) = @_;
Adam Roben56973d22007-04-25 12:42:58 -07001941
Eric Wong1a97a502007-02-20 00:43:19 -08001942 return undef unless defined $full_url;
Adam Roben56973d22007-04-25 12:42:58 -07001943 remove_username($full_url);
1944 remove_username($repos_root) if defined $repos_root;
Eric Wonga8ae2622007-02-13 14:22:11 -08001945 my $remotes = read_all_remotes();
1946 if (defined $full_url && defined $repos_root && !defined $path) {
1947 $path = $full_url;
1948 $path =~ s#^\Q$repos_root\E(?:/|$)##;
1949 }
1950 foreach my $repo_id (keys %$remotes) {
1951 my $u = $remotes->{$repo_id}->{url} or next;
Adam Roben56973d22007-04-25 12:42:58 -07001952 remove_username($u);
Eric Wonga8ae2622007-02-13 14:22:11 -08001953 next if defined $repos_root && $repos_root ne $u;
1954
1955 my $fetch = $remotes->{$repo_id}->{fetch} || {};
Marc Branchaud62244062009-06-23 13:02:08 -04001956 foreach my $t (qw/branches tags/) {
1957 foreach my $globspec (@{$remotes->{$repo_id}->{$t}}) {
1958 resolve_local_globs($u, $fetch, $globspec);
1959 }
Eric Wonga8ae2622007-02-13 14:22:11 -08001960 }
1961 my $p = $path;
John Goerzen0bb91d92008-03-08 16:04:05 -06001962 my $rwr = rewrite_root({repo_id => $repo_id});
João Abecasis63c56022008-07-14 16:28:04 +01001963 my $svm = $remotes->{$repo_id}->{svm}
1964 if defined $remotes->{$repo_id}->{svm};
Eric Wonga8ae2622007-02-13 14:22:11 -08001965 unless (defined $p) {
1966 $p = $full_url;
John Goerzen0bb91d92008-03-08 16:04:05 -06001967 my $z = $u;
João Abecasis63c56022008-07-14 16:28:04 +01001968 my $prefix = '';
John Goerzen0bb91d92008-03-08 16:04:05 -06001969 if ($rwr) {
1970 $z = $rwr;
Dévai Tamás1b7e5432009-02-12 00:14:02 +01001971 remove_username($z);
João Abecasis63c56022008-07-14 16:28:04 +01001972 } elsif (defined $svm) {
1973 $z = $svm->{source};
1974 $prefix = $svm->{replace};
1975 $prefix =~ s#^\Q$u\E(?:/|$)##;
1976 $prefix =~ s#/$##;
John Goerzen0bb91d92008-03-08 16:04:05 -06001977 }
João Abecasis63c56022008-07-14 16:28:04 +01001978 $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next;
Eric Wonga8ae2622007-02-13 14:22:11 -08001979 }
1980 foreach my $f (keys %$fetch) {
1981 next if $f ne $p;
1982 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
1983 }
1984 }
1985 undef;
1986}
1987
Eric Wong9b981fc2007-01-11 12:14:21 -08001988sub init {
Eric Wongd8115c52007-02-01 03:30:31 -08001989 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08001990 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong9b981fc2007-01-11 12:14:21 -08001991 if (defined $url) {
Eric Wongd8115c52007-02-01 03:30:31 -08001992 $self->init_remote_config($url, $no_write);
Eric Wong9b981fc2007-01-11 12:14:21 -08001993 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001994 $self;
1995}
1996
Eric Wong706587f2007-01-18 17:50:01 -08001997sub find_ref {
1998 my ($ref_id) = @_;
1999 foreach (command(qw/config -l/)) {
2000 next unless m!^svn-remote\.(.+)\.fetch=
Eric Wongffd5c8e2009-10-22 23:39:04 -07002001 \s*(.*?)\s*:\s*(.+?)\s*$!x;
Eric Wong706587f2007-01-18 17:50:01 -08002002 my ($repo_id, $path, $ref) = ($1, $2, $3);
2003 if ($ref eq $ref_id) {
2004 $path = '' if ($path =~ m#^\./?#);
2005 return ($repo_id, $path);
2006 }
2007 }
2008 (undef, undef, undef);
2009}
2010
Eric Wong9b981fc2007-01-11 12:14:21 -08002011sub new {
Eric Wong706587f2007-01-18 17:50:01 -08002012 my ($class, $ref_id, $repo_id, $path) = @_;
2013 if (defined $ref_id && !defined $repo_id && !defined $path) {
2014 ($repo_id, $path) = find_ref($ref_id);
2015 if (!defined $repo_id) {
2016 die "Could not find a \"svn-remote.*.fetch\" key ",
2017 "in the repository configuration matching: ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04002018 "$ref_id\n";
Eric Wong706587f2007-01-18 17:50:01 -08002019 }
2020 }
2021 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong8b8fc062007-01-22 11:44:57 -08002022 if (!defined $self->{path} || !length $self->{path}) {
2023 my $fetch = command_oneline('config', '--get',
2024 "svn-remote.$repo_id.fetch",
Adam Brewster6f5748e2009-08-11 23:14:27 -04002025 ":$ref_id\$") or
Eric Wong8b8fc062007-01-22 11:44:57 -08002026 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04002027 "\":$ref_id\$\" in config\n";
Eric Wong8b8fc062007-01-22 11:44:57 -08002028 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
2029 }
Eric Wong706587f2007-01-18 17:50:01 -08002030 $self->{url} = command_oneline('config', '--get',
2031 "svn-remote.$repo_id.url") or
2032 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
Eric Wongd6d33462007-02-16 04:05:33 -08002033 $self->rebuild;
Eric Wong9b981fc2007-01-11 12:14:21 -08002034 $self;
2035}
2036
Robert Ewaldbf655fd2007-07-30 11:08:21 +02002037sub refname {
Adam Brewster6f5748e2009-08-11 23:14:27 -04002038 my ($refname) = $_[0]->{ref_id} ;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02002039
2040 # It cannot end with a slash /, we'll throw up on this because
2041 # SVN can't have directories with a slash in their name, either:
2042 if ($refname =~ m{/$}) {
2043 die "ref: '$refname' ends with a trailing slash, this is ",
2044 "not permitted by git nor Subversion\n";
2045 }
2046
2047 # It cannot have ASCII control character space, tilde ~, caret ^,
2048 # colon :, question-mark ?, asterisk *, space, or open bracket [
2049 # anywhere.
2050 #
2051 # Additionally, % must be escaped because it is used for escaping
2052 # and we want our escaped refname to be reversible
2053 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
2054
2055 # no slash-separated component can begin with a dot .
2056 # /.* becomes /%2E*
2057 $refname =~ s{/\.}{/%2E}g;
2058
2059 # It cannot have two consecutive dots .. anywhere
2060 # .. becomes %2E%2E
2061 $refname =~ s{\.\.}{%2E%2E}g;
2062
2063 return $refname;
2064}
2065
2066sub desanitize_refname {
2067 my ($refname) = @_;
2068 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
2069 return $refname;
2070}
Eric Wong9b981fc2007-01-11 12:14:21 -08002071
Eric Wong26a62d52007-02-12 13:25:25 -08002072sub svm_uuid {
2073 my ($self) = @_;
2074 return $self->{svm}->{uuid} if $self->svm;
2075 $self->ra;
2076 unless ($self->{svm}) {
2077 die "SVM UUID not cached, and reading remotely failed\n";
2078 }
2079 $self->{svm}->{uuid};
2080}
Eric Wong8a49ee92007-02-10 20:46:50 -08002081
Eric Wong26a62d52007-02-12 13:25:25 -08002082sub svm {
2083 my ($self) = @_;
2084 return $self->{svm} if $self->{svm};
2085 my $svm;
Eric Wong8a49ee92007-02-10 20:46:50 -08002086 # see if we have it in our config, first:
2087 eval {
Eric Wong26a62d52007-02-12 13:25:25 -08002088 my $section = "svn-remote.$self->{repo_id}";
2089 $svm = {
Eric Wong93f26892007-02-11 01:20:26 -08002090 source => tmp_config('--get', "$section.svm-source"),
2091 uuid => tmp_config('--get', "$section.svm-uuid"),
Eric Wongbefc9ad2007-02-17 02:53:07 -08002092 replace => tmp_config('--get', "$section.svm-replace"),
Eric Wong8a49ee92007-02-10 20:46:50 -08002093 }
2094 };
Eric Wongbefc9ad2007-02-17 02:53:07 -08002095 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
2096 $self->{svm} = $svm;
2097 }
Eric Wong26a62d52007-02-12 13:25:25 -08002098 $self->{svm};
2099}
2100
2101sub _set_svm_vars {
2102 my ($self, $ra) = @_;
Eric Wongdb03cd22007-02-13 00:38:02 -08002103 return $ra if $self->svm;
Eric Wong26a62d52007-02-12 13:25:25 -08002104
Eric Wongdb03cd22007-02-13 00:38:02 -08002105 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08002106 "(svm:source, svm:uuid) ",
Eric Wongdb03cd22007-02-13 00:38:02 -08002107 "from the following URLs:\n" );
2108 sub read_svm_props {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002109 my ($self, $ra, $path, $r) = @_;
2110 my $props = ($ra->get_dir($path, $r))[2];
Eric Wongdb03cd22007-02-13 00:38:02 -08002111 my $src = $props->{'svm:source'};
Eric Wong8a49ee92007-02-10 20:46:50 -08002112 my $uuid = $props->{'svm:uuid'};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002113 return undef if (!$src || !$uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08002114
Eric Wongbefc9ad2007-02-17 02:53:07 -08002115 chomp($src, $uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08002116
Eric Wongb3e95932009-07-11 14:13:12 -07002117 $uuid =~ m{^[0-9a-f\-]{30,}$}i
Eric Wong8a49ee92007-02-10 20:46:50 -08002118 or die "doesn't look right - svm:uuid is '$uuid'\n";
Eric Wongbefc9ad2007-02-17 02:53:07 -08002119
2120 # the '!' is used to mark the repos_root!/relative/path
2121 $src =~ s{/?!/?}{/};
Eric Wongdb03cd22007-02-13 00:38:02 -08002122 $src =~ s{/+$}{}; # no trailing slashes please
Eric Wongbefc9ad2007-02-17 02:53:07 -08002123 # username is of no interest
Eric Wongdb03cd22007-02-13 00:38:02 -08002124 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
Eric Wong8a49ee92007-02-10 20:46:50 -08002125
Eric Wongbefc9ad2007-02-17 02:53:07 -08002126 my $replace = $ra->{url};
2127 $replace .= "/$path" if length $path;
2128
Eric Wongdb03cd22007-02-13 00:38:02 -08002129 my $section = "svn-remote.$self->{repo_id}";
Eric Wongbefc9ad2007-02-17 02:53:07 -08002130 tmp_config("$section.svm-source", $src);
2131 tmp_config("$section.svm-replace", $replace);
2132 tmp_config("$section.svm-uuid", $uuid);
2133 $self->{svm} = {
2134 source => $src,
2135 uuid => $uuid,
2136 replace => $replace
2137 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002138 }
Eric Wongdb03cd22007-02-13 00:38:02 -08002139
2140 my $r = $ra->get_latest_revnum;
2141 my $path = $self->{path};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002142 my %tried;
Eric Wongdb03cd22007-02-13 00:38:02 -08002143 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002144 unless ($tried{"$self->{url}/$path"}) {
2145 return $ra if $self->read_svm_props($ra, $path, $r);
2146 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002147 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002148 $path =~ s#/?[^/]+$##;
Eric Wong8a49ee92007-02-10 20:46:50 -08002149 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002150 die "Path: '$path' should be ''\n" if $path ne '';
2151 return $ra if $self->read_svm_props($ra, $path, $r);
2152 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002153
2154 if ($ra->{repos_root} eq $self->{url}) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002155 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002156 }
2157
2158 # nope, make sure we're connected to the repository root:
2159 my $ok;
2160 my @tried_b;
2161 $path = $ra->{svn_path};
Eric Wongdb03cd22007-02-13 00:38:02 -08002162 $ra = Git::SVN::Ra->new($ra->{repos_root});
2163 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002164 unless ($tried{"$ra->{url}/$path"}) {
2165 $ok = $self->read_svm_props($ra, $path, $r);
2166 last if $ok;
2167 $tried{"$ra->{url}/$path"} = 1;
2168 }
2169 $path =~ s#/?[^/]+$##;
Eric Wongdb03cd22007-02-13 00:38:02 -08002170 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002171 die "Path: '$path' should be ''\n" if $path ne '';
2172 $ok ||= $self->read_svm_props($ra, $path, $r);
2173 $tried{"$ra->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002174 if (!$ok) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002175 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002176 }
2177 Git::SVN::Ra->new($self->{url});
Eric Wong8a49ee92007-02-10 20:46:50 -08002178}
2179
Eric Wong62e349d2007-02-16 19:57:29 -08002180sub svnsync {
2181 my ($self) = @_;
2182 return $self->{svnsync} if $self->{svnsync};
2183
2184 if ($self->no_metadata) {
2185 die "Can't have both 'noMetadata' and ",
2186 "'useSvnsyncProps' options set!\n";
2187 }
2188 if ($self->rewrite_root) {
2189 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
2190 "options set!\n";
2191 }
2192
2193 my $svnsync;
2194 # see if we have it in our config, first:
2195 eval {
2196 my $section = "svn-remote.$self->{repo_id}";
Eric Wong98fa5b62008-01-11 23:13:55 -08002197
2198 my $url = tmp_config('--get', "$section.svnsync-url");
2199 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
2200 die "doesn't look right - svn:sync-from-url is '$url'\n";
2201
2202 my $uuid = tmp_config('--get', "$section.svnsync-uuid");
Eric Wongb3e95932009-07-11 14:13:12 -07002203 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
Eric Wong98fa5b62008-01-11 23:13:55 -08002204 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2205
2206 $svnsync = { url => $url, uuid => $uuid }
Eric Wong62e349d2007-02-16 19:57:29 -08002207 };
2208 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
2209 return $self->{svnsync} = $svnsync;
2210 }
2211
2212 my $err = "useSvnsyncProps set, but failed to read " .
2213 "svnsync property: svn:sync-from-";
2214 my $rp = $self->ra->rev_proplist(0);
2215
2216 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
Eric Wong98fa5b62008-01-11 23:13:55 -08002217 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
Eric Wong62e349d2007-02-16 19:57:29 -08002218 die "doesn't look right - svn:sync-from-url is '$url'\n";
2219
2220 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
Eric Wongb3e95932009-07-11 14:13:12 -07002221 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
Eric Wong62e349d2007-02-16 19:57:29 -08002222 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2223
2224 my $section = "svn-remote.$self->{repo_id}";
2225 tmp_config('--add', "$section.svnsync-uuid", $uuid);
2226 tmp_config('--add', "$section.svnsync-url", $url);
2227 return $self->{svnsync} = { url => $url, uuid => $uuid };
2228}
2229
Eric Wong26a62d52007-02-12 13:25:25 -08002230# this allows us to memoize our SVN::Ra UUID locally and avoid a
2231# remote lookup (useful for 'git svn log').
2232sub ra_uuid {
2233 my ($self) = @_;
2234 unless ($self->{ra_uuid}) {
2235 my $key = "svn-remote.$self->{repo_id}.uuid";
2236 my $uuid = eval { tmp_config('--get', $key) };
Eric Wongb3e95932009-07-11 14:13:12 -07002237 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/i) {
Eric Wong26a62d52007-02-12 13:25:25 -08002238 $self->{ra_uuid} = $uuid;
2239 } else {
2240 die "ra_uuid called without URL\n" unless $self->{url};
2241 $self->{ra_uuid} = $self->ra->get_uuid;
2242 tmp_config('--add', $key, $self->{ra_uuid});
2243 }
2244 }
2245 $self->{ra_uuid};
2246}
2247
Eric Wonga5460eb2007-11-21 18:20:57 -08002248sub _set_repos_root {
2249 my ($self, $repos_root) = @_;
2250 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2251 $repos_root ||= $self->ra->{repos_root};
2252 tmp_config($k, $repos_root);
2253 $repos_root;
2254}
2255
2256sub repos_root {
2257 my ($self) = @_;
2258 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2259 eval { tmp_config('--get', $k) } || $self->_set_repos_root;
2260}
2261
Eric Wong9b981fc2007-01-11 12:14:21 -08002262sub ra {
2263 my ($self) = shift;
Eric Wong8a49ee92007-02-10 20:46:50 -08002264 my $ra = Git::SVN::Ra->new($self->{url});
Eric Wonga5460eb2007-11-21 18:20:57 -08002265 $self->_set_repos_root($ra->{repos_root});
Eric Wong91b03282007-02-11 00:51:33 -08002266 if ($self->use_svm_props && !$self->{svm}) {
2267 if ($self->no_metadata) {
Eric Wong97ae0912007-02-11 15:21:24 -08002268 die "Can't have both 'noMetadata' and ",
2269 "'useSvmProps' options set!\n";
Eric Wong62e349d2007-02-16 19:57:29 -08002270 } elsif ($self->use_svnsync_props) {
2271 die "Can't have both 'useSvnsyncProps' and ",
2272 "'useSvmProps' options set!\n";
Eric Wong91b03282007-02-11 00:51:33 -08002273 }
Eric Wong26a62d52007-02-12 13:25:25 -08002274 $ra = $self->_set_svm_vars($ra);
Eric Wong8a49ee92007-02-10 20:46:50 -08002275 $self->{-want_revprops} = 1;
2276 }
2277 $ra;
Eric Wong9b981fc2007-01-11 12:14:21 -08002278}
2279
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002280# prop_walk(PATH, REV, SUB)
2281# -------------------------
2282# Recursively traverse PATH at revision REV and invoke SUB for each
2283# directory that contains a SVN property. SUB will be invoked as
2284# follows: &SUB(gs, path, props); where `gs' is this instance of
2285# Git::SVN, `path' the path to the directory where the properties
2286# `props' were found. The `path' will be relative to point of checkout,
2287# that is, if url://repo/trunk is the current Git branch, and that
2288# directory contains a sub-directory `d', SUB will be invoked with `/d/'
2289# as `path' (note the trailing `/').
2290sub prop_walk {
2291 my ($self, $path, $rev, $sub) = @_;
2292
Kevin Ballard35cda062008-01-09 01:37:20 -05002293 $path =~ s#^/##;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002294 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
2295 $path =~ s#^/*#/#g;
Eric Wong9b981fc2007-01-11 12:14:21 -08002296 my $p = $path;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002297 # Strip the irrelevant part of the path.
2298 $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
2299 # Ensure the path is terminated by a `/'.
2300 $p =~ s#/*$#/#;
2301
2302 # The properties contain all the internal SVN stuff nobody
2303 # (usually) cares about.
2304 my $interesting_props = 0;
2305 foreach (keys %{$props}) {
2306 # If it doesn't start with `svn:', it must be a
2307 # user-defined property.
2308 ++$interesting_props and next if $_ !~ /^svn:/;
2309 # FIXME: Fragile, if SVN adds new public properties,
2310 # this needs to be updated.
2311 ++$interesting_props if /^svn:(?:ignore|keywords|executable
2312 |eol-style|mime-type
2313 |externals|needs-lock)$/x;
Eric Wong9b981fc2007-01-11 12:14:21 -08002314 }
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002315 &$sub($self, $p, $props) if $interesting_props;
2316
Eric Wong9b981fc2007-01-11 12:14:21 -08002317 foreach (sort keys %$dirent) {
Eric Wong0dc03d62007-05-13 01:04:43 -07002318 next if $dirent->{$_}->{kind} != $SVN::Node::dir;
Christian Engwerb7166cc2008-05-27 08:46:55 +00002319 $self->prop_walk($self->{path} . $p . $_, $rev, $sub);
Eric Wong9b981fc2007-01-11 12:14:21 -08002320 }
2321}
2322
Eric Wong3ebe8df2007-01-25 17:35:40 -08002323sub last_rev { ($_[0]->last_rev_commit)[0] }
2324sub last_commit { ($_[0]->last_rev_commit)[1] }
2325
Eric Wong9b981fc2007-01-11 12:14:21 -08002326# returns the newest SVN revision number and newest commit SHA1
2327sub last_rev_commit {
2328 my ($self) = @_;
2329 if (defined $self->{last_rev} && defined $self->{last_commit}) {
2330 return ($self->{last_rev}, $self->{last_commit});
2331 }
Eric Wongd2866f92007-01-11 12:26:16 -08002332 my $c = ::verify_ref($self->refname.'^0');
Eric Wong91b03282007-02-11 00:51:33 -08002333 if ($c && !$self->use_svm_props && !$self->no_metadata) {
Eric Wongd2866f92007-01-11 12:26:16 -08002334 my $rev = (::cmt_metadata($c))[1];
Eric Wong9b981fc2007-01-11 12:14:21 -08002335 if (defined $rev) {
2336 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2337 return ($rev, $c);
2338 }
2339 }
Eric Wong060610c2007-12-08 23:27:41 -08002340 my $map_path = $self->map_path;
2341 unless (-e $map_path) {
Eric Wong26a62d52007-02-12 13:25:25 -08002342 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
2343 return (undef, undef);
2344 }
Eric Wong66ab84b2007-12-08 23:27:42 -08002345 my ($rev, $commit) = $self->rev_map_max(1);
Eric Wong060610c2007-12-08 23:27:41 -08002346 ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
2347 return ($rev, $commit);
Eric Wong9b981fc2007-01-11 12:14:21 -08002348}
2349
Eric Wong3ebe8df2007-01-25 17:35:40 -08002350sub get_fetch_range {
2351 my ($self, $min, $max) = @_;
2352 $max ||= $self->ra->get_latest_revnum;
Eric Wong060610c2007-12-08 23:27:41 -08002353 $min ||= $self->rev_map_max;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002354 (++$min, $max);
Eric Wong9b981fc2007-01-11 12:14:21 -08002355}
2356
Eric Wong8a49ee92007-02-10 20:46:50 -08002357sub tmp_config {
Eric Wong93f26892007-02-11 01:20:26 -08002358 my (@args) = @_;
Eric Wongb7e53482007-02-16 04:09:28 -08002359 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
2360 my $config = "$ENV{GIT_DIR}/svn/.metadata";
Eric Wong38570a42007-06-13 02:37:05 -07002361 if (! -f $config && -f $old_def_config) {
Eric Wongb7e53482007-02-16 04:09:28 -08002362 rename $old_def_config, $config or
2363 die "Failed rename $old_def_config => $config: $!\n";
2364 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002365 my $old_config = $ENV{GIT_CONFIG};
Eric Wong93f26892007-02-11 01:20:26 -08002366 $ENV{GIT_CONFIG} = $config;
Eric Wong8a49ee92007-02-10 20:46:50 -08002367 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002368 my @ret = eval {
2369 unless (-f $config) {
2370 mkfile($config);
2371 open my $fh, '>', $config or
2372 die "Can't open $config: $!\n";
2373 print $fh "; This file is used internally by ",
2374 "git-svn\n" or die
2375 "Couldn't write to $config: $!\n";
2376 print $fh "; You should not have to edit it\n" or
2377 die "Couldn't write to $config: $!\n";
2378 close $fh or die "Couldn't close $config: $!\n";
2379 }
2380 command('config', @args);
2381 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002382 my $err = $@;
2383 if (defined $old_config) {
2384 $ENV{GIT_CONFIG} = $old_config;
2385 } else {
2386 delete $ENV{GIT_CONFIG};
2387 }
2388 die $err if $err;
2389 wantarray ? @ret : $ret[0];
2390}
2391
Eric Wong9b981fc2007-01-11 12:14:21 -08002392sub tmp_index_do {
2393 my ($self, $sub) = @_;
2394 my $old_index = $ENV{GIT_INDEX_FILE};
2395 $ENV{GIT_INDEX_FILE} = $self->{index};
Eric Wong8a49ee92007-02-10 20:46:50 -08002396 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002397 my @ret = eval {
2398 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
2399 mkpath([$dir]) unless -d $dir;
2400 &$sub;
2401 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002402 my $err = $@;
2403 if (defined $old_index) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002404 $ENV{GIT_INDEX_FILE} = $old_index;
2405 } else {
2406 delete $ENV{GIT_INDEX_FILE};
2407 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002408 die $err if $err;
Eric Wong9b981fc2007-01-11 12:14:21 -08002409 wantarray ? @ret : $ret[0];
2410}
2411
2412sub assert_index_clean {
2413 my ($self, $treeish) = @_;
2414
2415 $self->tmp_index_do(sub {
2416 command_noisy('read-tree', $treeish) unless -e $self->{index};
2417 my $x = command_oneline('write-tree');
2418 my ($y) = (command(qw/cat-file commit/, $treeish) =~
2419 /^tree ($::sha1)/mo);
Eric Wonge8d120b2007-02-14 16:29:52 -08002420 return if $y eq $x;
2421
2422 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2423 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2424 command_noisy('read-tree', $treeish);
Eric Wong9b981fc2007-01-11 12:14:21 -08002425 $x = command_oneline('write-tree');
2426 if ($y ne $x) {
2427 ::fatal "trees ($treeish) $y != $x\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02002428 "Something is seriously wrong...";
Eric Wong9b981fc2007-01-11 12:14:21 -08002429 }
2430 });
2431}
2432
2433sub get_commit_parents {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002434 my ($self, $log_entry) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002435 my (%seen, @ret, @tmp);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002436 # legacy support for 'set-tree'; this is only used by set_tree_cb:
2437 if (my $ip = $self->{inject_parents}) {
2438 if (my $commit = delete $ip->{$log_entry->{revision}}) {
2439 push @tmp, $commit;
Eric Wong9b981fc2007-01-11 12:14:21 -08002440 }
2441 }
Eric Wongd2866f92007-01-11 12:26:16 -08002442 if (my $cur = ::verify_ref($self->refname.'^0')) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002443 push @tmp, $cur;
2444 }
Eric Wong733a65a2007-06-13 02:23:28 -07002445 if (my $ipd = $self->{inject_parents_dcommit}) {
2446 if (my $commit = delete $ipd->{$log_entry->{revision}}) {
2447 push @tmp, @$commit;
2448 }
2449 }
Eric Wong44320b92007-01-13 22:35:53 -08002450 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
Eric Wong9b981fc2007-01-11 12:14:21 -08002451 while (my $p = shift @tmp) {
2452 next if $seen{$p};
2453 $seen{$p} = 1;
2454 push @ret, $p;
Eric Wong9b981fc2007-01-11 12:14:21 -08002455 }
2456 @ret;
2457}
2458
Eric Wongaea736c2007-02-16 19:15:21 -08002459sub rewrite_root {
2460 my ($self) = @_;
2461 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
2462 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2463 my $rwr = eval { command_oneline(qw/config --get/, $k) };
2464 if ($rwr) {
2465 $rwr =~ s#/+$##;
2466 if ($rwr !~ m#^[a-z\+]+://#) {
2467 die "$rwr is not a valid URL (key: $k)\n";
2468 }
2469 }
2470 $self->{-rewrite_root} = $rwr;
2471}
2472
2473sub metadata_url {
2474 my ($self) = @_;
2475 ($self->rewrite_root || $self->{url}) .
2476 (length $self->{path} ? '/' . $self->{path} : '');
2477}
2478
Eric Wong706587f2007-01-18 17:50:01 -08002479sub full_url {
Eric Wong9b981fc2007-01-11 12:14:21 -08002480 my ($self) = @_;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002481 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
Eric Wong9b981fc2007-01-11 12:14:21 -08002482}
2483
Eric Wongad948022007-12-15 19:08:22 -08002484
2485sub set_commit_header_env {
2486 my ($log_entry) = @_;
2487 my %env;
2488 foreach my $ned (qw/NAME EMAIL DATE/) {
2489 foreach my $ac (qw/AUTHOR COMMITTER/) {
2490 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2491 }
2492 }
2493
2494 $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
2495 $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
2496 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
2497
2498 $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
2499 ? $log_entry->{commit_name}
2500 : $log_entry->{name};
2501 $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
2502 ? $log_entry->{commit_email}
2503 : $log_entry->{email};
2504 \%env;
2505}
2506
2507sub restore_commit_header_env {
2508 my ($env) = @_;
2509 foreach my $ned (qw/NAME EMAIL DATE/) {
2510 foreach my $ac (qw/AUTHOR COMMITTER/) {
2511 my $k = "GIT_${ac}_${ned}";
2512 if (defined $env->{$k}) {
2513 $ENV{$k} = $env->{$k};
2514 } else {
2515 delete $ENV{$k};
2516 }
2517 }
2518 }
2519}
2520
Karl Hasselström94bc9142008-02-03 17:56:18 +01002521sub gc {
2522 command_noisy('gc', '--auto');
2523};
2524
Eric Wong9b981fc2007-01-11 12:14:21 -08002525sub do_git_commit {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002526 my ($self, $log_entry) = @_;
Eric Wong8a603772007-01-31 02:45:50 -08002527 my $lr = $self->last_rev;
2528 if (defined $lr && $lr >= $log_entry->{revision}) {
2529 die "Last fetched revision of ", $self->refname,
2530 " was r$lr, but we are about to fetch: ",
2531 "r$log_entry->{revision}!\n";
2532 }
Eric Wong060610c2007-12-08 23:27:41 -08002533 if (my $c = $self->rev_map_get($log_entry->{revision})) {
Eric Wong44320b92007-01-13 22:35:53 -08002534 croak "$log_entry->{revision} = $c already exists! ",
Eric Wong9b981fc2007-01-11 12:14:21 -08002535 "Why are we refetching it?\n";
2536 }
Eric Wongad948022007-12-15 19:08:22 -08002537 my $old_env = set_commit_header_env($log_entry);
Eric Wong44320b92007-01-13 22:35:53 -08002538 my $tree = $log_entry->{tree};
Eric Wong9b981fc2007-01-11 12:14:21 -08002539 if (!defined $tree) {
2540 $tree = $self->tmp_index_do(sub {
2541 command_oneline('write-tree') });
2542 }
2543 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2544
Deskin Millere855bfc2008-10-31 00:10:25 -04002545 my @exec = ('git', 'commit-tree', $tree);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002546 foreach ($self->get_commit_parents($log_entry)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002547 push @exec, '-p', $_;
2548 }
2549 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2550 or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07002551 binmode $msg_fh;
2552
2553 # we always get UTF-8 from SVN, but we may want our commits in
2554 # a different encoding.
2555 if (my $enc = Git::config('i18n.commitencoding')) {
2556 require Encode;
2557 Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
2558 }
Eric Wong44320b92007-01-13 22:35:53 -08002559 print $msg_fh $log_entry->{log} or croak $!;
Eric Wongad948022007-12-15 19:08:22 -08002560 restore_commit_header_env($old_env);
Eric Wong91b03282007-02-11 00:51:33 -08002561 unless ($self->no_metadata) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002562 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2563 or croak $!;
Eric Wong9760adc2007-01-31 03:06:56 -08002564 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002565 $msg_fh->flush == 0 or croak $!;
2566 close $msg_fh or croak $!;
2567 chomp(my $commit = do { local $/; <$out_fh> });
2568 close $out_fh or croak $!;
2569 waitpid $pid, 0;
2570 croak $? if $?;
2571 if ($commit !~ /^$::sha1$/o) {
2572 die "Failed to commit, invalid sha1: $commit\n";
2573 }
2574
Eric Wong060610c2007-12-08 23:27:41 -08002575 $self->rev_map_set($log_entry->{revision}, $commit, 1);
Eric Wong9b981fc2007-01-11 12:14:21 -08002576
Eric Wong44320b92007-01-13 22:35:53 -08002577 $self->{last_rev} = $log_entry->{revision};
Eric Wong9b981fc2007-01-11 12:14:21 -08002578 $self->{last_commit} = $commit;
Simon Arlott49750f32009-03-30 19:31:41 +01002579 print "r$log_entry->{revision}" unless $::_q > 1;
Eric Wong8a49ee92007-02-10 20:46:50 -08002580 if (defined $log_entry->{svm_revision}) {
Simon Arlott49750f32009-03-30 19:31:41 +01002581 print " (\@$log_entry->{svm_revision})" unless $::_q > 1;
Eric Wong060610c2007-12-08 23:27:41 -08002582 $self->rev_map_set($log_entry->{svm_revision}, $commit,
Eric Wong26a62d52007-02-12 13:25:25 -08002583 0, $self->svm_uuid);
Eric Wong8a49ee92007-02-10 20:46:50 -08002584 }
Simon Arlott49750f32009-03-30 19:31:41 +01002585 print " = $commit ($self->{ref_id})\n" unless $::_q > 1;
Karl Hasselström94bc9142008-02-03 17:56:18 +01002586 if (--$_gc_nr == 0) {
2587 $_gc_nr = $_gc_period;
2588 gc();
2589 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002590 return $commit;
2591}
2592
Eric Wongfbcc1732007-02-06 18:35:30 -08002593sub match_paths {
2594 my ($self, $paths, $r) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002595 return 1 if $self->{path} eq '';
Eric Wongd542aed2007-02-09 02:19:41 -08002596 if (my $path = $paths->{"/$self->{path}"}) {
2597 return ($path->{action} eq 'D') ? 0 : 1;
2598 }
Mattias Nissler0b2af452009-07-07 01:40:02 +02002599 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
Eric Wongfbcc1732007-02-06 18:35:30 -08002600 if (grep /$self->{path_regex}/, keys %$paths) {
2601 return 1;
2602 }
2603 my $c = '';
2604 foreach (split m#/#, $self->{path}) {
2605 $c .= "/$_";
Eric Wong74a81222007-02-10 13:28:50 -08002606 next unless ($paths->{$c} &&
2607 ($paths->{$c}->{action} =~ /^[AR]$/));
Eric Wonge5181922007-02-08 12:53:57 -08002608 if ($self->ra->check_path($self->{path}, $r) ==
2609 $SVN::Node::dir) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002610 return 1;
2611 }
2612 }
2613 return 0;
2614}
2615
Eric Wong15710b62007-01-22 02:20:33 -08002616sub find_parent_branch {
2617 my ($self, $paths, $rev) = @_;
Eric Wong91b03282007-02-11 00:51:33 -08002618 return undef unless $self->follow_parent;
Eric Wonge5a0b242007-01-25 15:44:54 -08002619 unless (defined $paths) {
Eric Wongc7eba712007-01-31 03:45:28 -08002620 my $err_handler = $SVN::Error::handler;
2621 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
Mattias Nissler3c49a032009-07-07 01:39:52 +02002622 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1,
2623 sub { $paths = $_[0] });
Eric Wongc7eba712007-01-31 03:45:28 -08002624 $SVN::Error::handler = $err_handler;
Eric Wonge5a0b242007-01-25 15:44:54 -08002625 }
2626 return undef unless defined $paths;
Eric Wong15710b62007-01-22 02:20:33 -08002627
2628 # look for a parent from another branch:
Mattias Nissler0b2af452009-07-07 01:40:02 +02002629 my @b_path_components = split m#/#, $self->{path};
Eric Wong7f578c52007-01-24 02:16:25 -08002630 my @a_path_components;
2631 my $i;
2632 while (@b_path_components) {
2633 $i = $paths->{'/'.join('/', @b_path_components)};
Eric Wong74a81222007-02-10 13:28:50 -08002634 last if $i && defined $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002635 unshift(@a_path_components, pop(@b_path_components));
2636 }
Eric Wong74a81222007-02-10 13:28:50 -08002637 return undef unless defined $i && defined $i->{copyfrom_path};
2638 my $branch_from = $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002639 if (@a_path_components) {
2640 print STDERR "branch_from: $branch_from => ";
2641 $branch_from .= '/'.join('/', @a_path_components);
2642 print STDERR $branch_from, "\n";
2643 }
Eric Wong3ebe8df2007-01-25 17:35:40 -08002644 my $r = $i->{copyfrom_rev};
Eric Wong15710b62007-01-22 02:20:33 -08002645 my $repos_root = $self->ra->{repos_root};
2646 my $url = $self->ra->{url};
Mattias Nissler0b2af452009-07-07 01:40:02 +02002647 my $new_url = $url . $branch_from;
Eric Wong15710b62007-01-22 02:20:33 -08002648 print STDERR "Found possible branch point: ",
Simon Arlott85886162009-10-09 13:21:13 +01002649 "$new_url => ", $self->full_url, ", $r\n"
2650 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002651 $branch_from =~ s#^/##;
Mattias Nissler0b2af452009-07-07 01:40:02 +02002652 my $gs = $self->other_gs($new_url, $url,
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002653 $branch_from, $r, $self->{ref_id});
Eric Wong15710b62007-01-22 02:20:33 -08002654 my ($r0, $parent) = $gs->find_rev_before($r, 1);
Deskin Miller553589f2008-12-08 08:31:31 -05002655 {
2656 my ($base, $head);
2657 if (!defined $r0 || !defined $parent) {
2658 ($base, $head) = parse_revision_argument(0, $r);
2659 } else {
2660 if ($r0 < $r) {
2661 $gs->ra->get_log([$gs->{path}], $r0 + 1, $r, 1,
2662 0, 1, sub { $base = $_[1] - 1 });
2663 }
2664 }
2665 if (defined $base && $base <= $r) {
Eric Wongd627de62007-04-15 03:01:29 -07002666 $gs->fetch($base, $r);
2667 }
Deskin Miller553589f2008-12-08 08:31:31 -05002668 ($r0, $parent) = $gs->find_rev_before($r, 1);
Eric Wong15710b62007-01-22 02:20:33 -08002669 }
Eric Wongef70de92007-02-01 04:12:41 -08002670 if (defined $r0 && defined $parent) {
Simon Arlott85886162009-10-09 13:21:13 +01002671 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n"
2672 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002673 my $ed;
2674 if ($self->ra->can_do_switch) {
Eric Wong2e5e2482007-02-23 02:21:59 -08002675 $self->assert_index_clean($parent);
Simon Arlott85886162009-10-09 13:21:13 +01002676 print STDERR "Following parent with do_switch\n"
2677 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002678 # do_switch works with svn/trunk >= r22312, but that
Eric Wong2b27f6c2007-01-28 04:59:05 -08002679 # is not included with SVN 1.4.3 (the latest version
Eric Wong15710b62007-01-22 02:20:33 -08002680 # at the moment), so we can't rely on it
Eric Wong83c2fcf2009-02-22 20:25:00 -08002681 $self->{last_rev} = $r0;
Eric Wong15710b62007-01-22 02:20:33 -08002682 $self->{last_commit} = $parent;
Eric Wong8841b372009-02-11 01:56:58 -08002683 $ed = SVN::Git::Fetcher->new($self, $gs->{path});
Eric Wong8a603772007-01-31 02:45:50 -08002684 $gs->ra->gs_do_switch($r0, $rev, $gs,
Eric Wong15710b62007-01-22 02:20:33 -08002685 $self->full_url, $ed)
2686 or die "SVN connection failed somewhere...\n";
Steven Walter9ff74e92007-09-28 13:24:19 -04002687 } elsif ($self->ra->trees_match($new_url, $r0,
2688 $self->full_url, $rev)) {
2689 print STDERR "Trees match:\n",
2690 " $new_url\@$r0\n",
2691 " ${\$self->full_url}\@$rev\n",
Simon Arlott85886162009-10-09 13:21:13 +01002692 "Following parent with no changes\n"
2693 unless $::_q > 1;
Steven Walter9ff74e92007-09-28 13:24:19 -04002694 $self->tmp_index_do(sub {
2695 command_noisy('read-tree', $parent);
2696 });
2697 $self->{last_commit} = $parent;
Eric Wong15710b62007-01-22 02:20:33 -08002698 } else {
Simon Arlott85886162009-10-09 13:21:13 +01002699 print STDERR "Following parent with do_update\n"
2700 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002701 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08002702 $self->ra->gs_do_update($rev, $rev, $self, $ed)
Eric Wong15710b62007-01-22 02:20:33 -08002703 or die "SVN connection failed somewhere...\n";
2704 }
Simon Arlott85886162009-10-09 13:21:13 +01002705 print STDERR "Successfully followed parent\n" unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002706 return $self->make_log_entry($rev, [$parent], $ed);
2707 }
Eric Wong15710b62007-01-22 02:20:33 -08002708 return undef;
2709}
2710
Eric Wong9b981fc2007-01-11 12:14:21 -08002711sub do_fetch {
Eric Wong706587f2007-01-18 17:50:01 -08002712 my ($self, $paths, $rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08002713 my $ed;
Eric Wong9b981fc2007-01-11 12:14:21 -08002714 my ($last_rev, @parents);
Eric Wongb9dffd82007-02-09 01:28:30 -08002715 if (my $lc = $self->last_commit) {
2716 # we can have a branch that was deleted, then re-added
2717 # under the same name but copied from another path, in
2718 # which case we'll have multiple parents (we don't
2719 # want to break the original ref, nor lose copypath info):
2720 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2721 push @{$log_entry->{parents}}, $lc;
2722 return $log_entry;
2723 }
Eric Wong15710b62007-01-22 02:20:33 -08002724 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002725 $last_rev = $self->{last_rev};
Eric Wongb9dffd82007-02-09 01:28:30 -08002726 $ed->{c} = $lc;
2727 @parents = ($lc);
Eric Wong9b981fc2007-01-11 12:14:21 -08002728 } else {
2729 $last_rev = $rev;
Eric Wong15710b62007-01-22 02:20:33 -08002730 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2731 return $log_entry;
2732 }
2733 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002734 }
Eric Wong8a603772007-01-31 02:45:50 -08002735 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002736 die "SVN connection failed somewhere...\n";
2737 }
2738 $self->make_log_entry($rev, \@parents, $ed);
2739}
2740
Eric Wong6111b932009-11-15 18:57:16 -08002741sub mkemptydirs {
2742 my ($self, $r) = @_;
Eric Wong6111b932009-11-15 18:57:16 -08002743
Eric Wonga5b80d92009-12-19 13:49:00 -08002744 sub scan {
2745 my ($r, $empty_dirs, $line) = @_;
2746 if (defined $r && $line =~ /^r(\d+)$/) {
2747 return 0 if $1 > $r;
2748 } elsif ($line =~ /^ \+empty_dir: (.+)$/) {
2749 $empty_dirs->{$1} = 1;
2750 } elsif ($line =~ /^ \-empty_dir: (.+)$/) {
2751 my @d = grep {m[^\Q$1\E(/|$)]} (keys %$empty_dirs);
2752 delete @$empty_dirs{@d};
2753 }
2754 1; # continue
2755 };
2756
2757 my %empty_dirs = ();
2758 my $gz_file = "$self->{dir}/unhandled.log.gz";
2759 if (-f $gz_file) {
2760 if (!$can_compress) {
2761 warn "Compress::Zlib could not be found; ",
2762 "empty directories in $gz_file will not be read\n";
2763 } else {
2764 my $gz = Compress::Zlib::gzopen($gz_file, "rb") or
2765 die "Unable to open $gz_file: $!\n";
2766 my $line;
2767 while ($gz->gzreadline($line) > 0) {
2768 scan($r, \%empty_dirs, $line) or last;
2769 }
2770 $gz->gzclose;
Eric Wong6111b932009-11-15 18:57:16 -08002771 }
2772 }
Eric Wonga5b80d92009-12-19 13:49:00 -08002773
2774 if (open my $fh, '<', "$self->{dir}/unhandled.log") {
2775 binmode $fh or croak "binmode: $!";
2776 while (<$fh>) {
2777 scan($r, \%empty_dirs, $_) or last;
2778 }
2779 close $fh;
2780 }
Eric Wong9be30ee2009-11-22 18:11:32 -08002781
2782 my $strip = qr/\A\Q$self->{path}\E(?:\/|$)/;
Eric Wong6111b932009-11-15 18:57:16 -08002783 foreach my $d (sort keys %empty_dirs) {
2784 $d = uri_decode($d);
Eric Wong9be30ee2009-11-22 18:11:32 -08002785 $d =~ s/$strip//;
Eric Wong6111b932009-11-15 18:57:16 -08002786 next if -d $d;
2787 if (-e _) {
2788 warn "$d exists but is not a directory\n";
2789 } else {
2790 print "creating empty directory: $d\n";
2791 mkpath([$d]);
2792 }
2793 }
2794}
2795
Eric Wong97f69872007-01-25 11:53:13 -08002796sub get_untracked {
2797 my ($self, $ed) = @_;
2798 my @out;
2799 my $h = $ed->{empty};
Eric Wong9b981fc2007-01-11 12:14:21 -08002800 foreach (sort keys %$h) {
2801 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
Eric Wong97f69872007-01-25 11:53:13 -08002802 push @out, " $act: " . uri_encode($_);
Eric Wong9b981fc2007-01-11 12:14:21 -08002803 warn "W: $act: $_\n";
2804 }
2805 foreach my $t (qw/dir_prop file_prop/) {
Eric Wong97f69872007-01-25 11:53:13 -08002806 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002807 foreach my $path (sort keys %$h) {
2808 my $ppath = $path eq '' ? '.' : $path;
2809 foreach my $prop (sort keys %{$h->{$path}}) {
Eric Wong1ce255d2007-01-14 23:21:16 -08002810 next if $SKIP_PROP{$prop};
Eric Wong9b981fc2007-01-11 12:14:21 -08002811 my $v = $h->{$path}->{$prop};
Eric Wong97f69872007-01-25 11:53:13 -08002812 my $t_ppath_prop = "$t: " .
2813 uri_encode($ppath) . ' ' .
2814 uri_encode($prop);
Eric Wong9b981fc2007-01-11 12:14:21 -08002815 if (defined $v) {
Eric Wong97f69872007-01-25 11:53:13 -08002816 push @out, " +$t_ppath_prop " .
2817 uri_encode($v);
Eric Wong9b981fc2007-01-11 12:14:21 -08002818 } else {
Eric Wong97f69872007-01-25 11:53:13 -08002819 push @out, " -$t_ppath_prop";
Eric Wong9b981fc2007-01-11 12:14:21 -08002820 }
2821 }
2822 }
2823 }
2824 foreach my $t (qw/absent_file absent_directory/) {
Eric Wong97f69872007-01-25 11:53:13 -08002825 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002826 foreach my $parent (sort keys %$h) {
2827 foreach my $path (sort @{$h->{$parent}}) {
Eric Wong97f69872007-01-25 11:53:13 -08002828 push @out, " $t: " .
2829 uri_encode("$parent/$path");
Eric Wong9b981fc2007-01-11 12:14:21 -08002830 warn "W: $t: $parent/$path ",
2831 "Insufficient permissions?\n";
2832 }
2833 }
2834 }
Eric Wong97f69872007-01-25 11:53:13 -08002835 \@out;
Eric Wong9b981fc2007-01-11 12:14:21 -08002836}
2837
Pete Harlane82f0d72009-01-17 20:10:14 -08002838# parse_svn_date(DATE)
2839# --------------------
2840# Given a date (in UTC) from Subversion, return a string in the format
2841# "<TZ Offset> <local date/time>" that Git will use.
2842#
2843# By default the parsed date will be in UTC; if $Git::SVN::_localtime
2844# is true we'll convert it to the local timezone instead.
Eric Wong1c8443b2007-01-14 02:17:00 -08002845sub parse_svn_date {
2846 my $date = shift || return '+0000 1970-01-01 00:00:00';
2847 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
Junio C Hamanob94ead72009-02-18 10:48:01 -08002848 (\d\d)\:(\d\d)\:(\d\d)\.\d*Z$/x) or
Eric Wong1c8443b2007-01-14 02:17:00 -08002849 croak "Unable to parse date: $date\n";
Pete Harlane82f0d72009-01-17 20:10:14 -08002850 my $parsed_date; # Set next.
2851
2852 if ($Git::SVN::_localtime) {
2853 # Translate the Subversion datetime to an epoch time.
2854 # Begin by switching ourselves to $date's timezone, UTC.
2855 my $old_env_TZ = $ENV{TZ};
2856 $ENV{TZ} = 'UTC';
2857
2858 my $epoch_in_UTC =
2859 POSIX::strftime('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
2860
2861 # Determine our local timezone (including DST) at the
2862 # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the
2863 # value of TZ, if any, at the time we were run.
2864 if (defined $Git::SVN::Log::TZ) {
2865 $ENV{TZ} = $Git::SVN::Log::TZ;
2866 } else {
2867 delete $ENV{TZ};
2868 }
2869
2870 my $our_TZ =
2871 POSIX::strftime('%Z', $S, $M, $H, $d, $m - 1, $Y - 1900);
2872
2873 # This converts $epoch_in_UTC into our local timezone.
2874 my ($sec, $min, $hour, $mday, $mon, $year,
2875 $wday, $yday, $isdst) = localtime($epoch_in_UTC);
2876
2877 $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
2878 $our_TZ, $year + 1900, $mon + 1,
2879 $mday, $hour, $min, $sec);
2880
2881 # Reset us to the timezone in effect when we entered
2882 # this routine.
2883 if (defined $old_env_TZ) {
2884 $ENV{TZ} = $old_env_TZ;
2885 } else {
2886 delete $ENV{TZ};
2887 }
2888 } else {
2889 $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
2890 }
2891
2892 return $parsed_date;
Eric Wong1c8443b2007-01-14 02:17:00 -08002893}
2894
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002895sub other_gs {
Mattias Nissler0b2af452009-07-07 01:40:02 +02002896 my ($self, $new_url, $url,
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002897 $branch_from, $r, $old_ref_id) = @_;
Mattias Nissler0b2af452009-07-07 01:40:02 +02002898 my $gs = Git::SVN->find_by_url($new_url, $url, $branch_from);
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002899 unless ($gs) {
2900 my $ref_id = $old_ref_id;
2901 $ref_id =~ s/\@\d+$//;
2902 $ref_id .= "\@$r";
2903 # just grow a tail if we're not unique enough :x
2904 $ref_id .= '-' while find_ref($ref_id);
Simon Arlott85886162009-10-09 13:21:13 +01002905 print STDERR "Initializing parent: $ref_id\n" unless $::_q > 1;
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002906 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
2907 if ($u =~ s#^\Q$url\E(/|$)##) {
2908 $p = $u;
2909 $u = $url;
2910 $repo_id = $self->{repo_id};
2911 }
2912 $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1);
2913 }
2914 $gs
2915}
2916
Mark Lodato36db1ed2009-05-14 21:27:15 -04002917sub call_authors_prog {
2918 my ($orig_author) = @_;
Mark Lodatod3d7d472009-09-12 20:33:23 -04002919 $orig_author = command_oneline('rev-parse', '--sq-quote', $orig_author);
Mark Lodato36db1ed2009-05-14 21:27:15 -04002920 my $author = `$::_authors_prog $orig_author`;
2921 if ($? != 0) {
2922 die "$::_authors_prog failed with exit code $?\n"
2923 }
2924 if ($author =~ /^\s*(.+?)\s*<(.*)>\s*$/) {
2925 my ($name, $email) = ($1, $2);
2926 $email = undef if length $2 == 0;
2927 return [$name, $email];
2928 } else {
2929 die "Author: $orig_author: $::_authors_prog returned "
2930 . "invalid author format: $author\n";
2931 }
2932}
2933
Eric Wong1c8443b2007-01-14 02:17:00 -08002934sub check_author {
2935 my ($author) = @_;
2936 if (!defined $author || length $author == 0) {
2937 $author = '(no author)';
Mark Lodato36db1ed2009-05-14 21:27:15 -04002938 }
2939 if (!defined $::users{$author}) {
2940 if (defined $::_authors_prog) {
2941 $::users{$author} = call_authors_prog($author);
2942 } elsif (defined $::_authors) {
2943 die "Author: $author not defined in $::_authors file\n";
2944 }
Eric Wong1c8443b2007-01-14 02:17:00 -08002945 }
2946 $author;
2947}
2948
Sam Vilainf1264bd2009-10-20 15:42:01 +13002949sub find_extra_svk_parents {
2950 my ($self, $ed, $tickets, $parents) = @_;
2951 # aha! svk:merge property changed...
2952 my @tickets = split "\n", $tickets;
2953 my @known_parents;
2954 for my $ticket ( @tickets ) {
2955 my ($uuid, $path, $rev) = split /:/, $ticket;
2956 if ( $uuid eq $self->ra_uuid ) {
2957 my $url = $self->rewrite_root || $self->{url};
2958 my $repos_root = $url;
2959 my $branch_from = $path;
2960 $branch_from =~ s{^/}{};
2961 my $gs = $self->other_gs($repos_root."/".$branch_from,
2962 $url,
2963 $branch_from,
2964 $rev,
2965 $self->{ref_id});
2966 if ( my $commit = $gs->rev_map_get($rev, $uuid) ) {
2967 # wahey! we found it, but it might be
2968 # an old one (!)
Alex Vandivere9e4c8b2009-11-29 02:20:21 -05002969 push @known_parents, [ $rev, $commit ];
Sam Vilainf1264bd2009-10-20 15:42:01 +13002970 }
2971 }
2972 }
Alex Vandivere9e4c8b2009-11-29 02:20:21 -05002973 # Ordering matters; highest-numbered commit merge tickets
2974 # first, as they may account for later merge ticket additions
2975 # or changes.
2976 @known_parents = map {$_->[1]} sort {$b->[0] <=> $a->[0]} @known_parents;
Sam Vilainf1264bd2009-10-20 15:42:01 +13002977 for my $parent ( @known_parents ) {
2978 my @cmd = ('rev-list', $parent, map { "^$_" } @$parents );
2979 my ($msg_fh, $ctx) = command_output_pipe(@cmd);
2980 my $new;
2981 while ( <$msg_fh> ) {
2982 $new=1;last;
2983 }
2984 command_close_pipe($msg_fh, $ctx);
2985 if ( $new ) {
2986 print STDERR
2987 "Found merge parent (svk:merge ticket): $parent\n";
2988 push @$parents, $parent;
2989 }
2990 }
2991}
2992
Sam Vilain7d944c32009-12-20 00:55:13 +13002993sub lookup_svn_merge {
2994 my $uuid = shift;
2995 my $url = shift;
2996 my $merge = shift;
2997
2998 my ($source, $revs) = split ":", $merge;
2999 my $path = $source;
3000 $path =~ s{^/}{};
3001 my $gs = Git::SVN->find_by_url($url.$source, $url, $path);
3002 if ( !$gs ) {
3003 warn "Couldn't find revmap for $url$source\n";
3004 return;
3005 }
3006 my @ranges = split ",", $revs;
3007 my ($tip, $tip_commit);
3008 my @merged_commit_ranges;
3009 # find the tip
3010 for my $range ( @ranges ) {
3011 my ($bottom, $top) = split "-", $range;
3012 $top ||= $bottom;
Sam Vilain33973a52009-12-20 05:22:42 +13003013 my $bottom_commit = $gs->find_rev_after( $bottom, 1, $top );
3014 my $top_commit = $gs->find_rev_before( $top, 1, $bottom );
Sam Vilain7d944c32009-12-20 00:55:13 +13003015
3016 unless ($top_commit and $bottom_commit) {
3017 warn "W:unknown path/rev in svn:mergeinfo "
3018 ."dirprop: $source:$range\n";
3019 next;
3020 }
3021
3022 push @merged_commit_ranges,
Sam Vilain33973a52009-12-20 05:22:42 +13003023 "$bottom_commit^..$top_commit";
Sam Vilain7d944c32009-12-20 00:55:13 +13003024
3025 if ( !defined $tip or $top > $tip ) {
3026 $tip = $top;
3027 $tip_commit = $top_commit;
3028 }
3029 }
3030 return ($tip_commit, @merged_commit_ranges);
3031}
Sam Vilain7a955a52009-12-20 05:26:26 +13003032
3033sub _rev_list {
3034 my ($msg_fh, $ctx) = command_output_pipe(
3035 "rev-list", @_,
3036 );
3037 my @rv;
3038 while ( <$msg_fh> ) {
3039 chomp;
3040 push @rv, $_;
3041 }
3042 command_close_pipe($msg_fh, $ctx);
3043 @rv;
3044}
3045
3046sub check_cherry_pick {
3047 my $base = shift;
3048 my $tip = shift;
3049 my @ranges = @_;
3050 my %commits = map { $_ => 1 }
3051 _rev_list("--no-merges", $tip, "--not", $base);
3052 for my $range ( @ranges ) {
3053 delete @commits{_rev_list($range)};
3054 }
3055 return (keys %commits);
3056}
3057
Sam Vilain7d944c32009-12-20 00:55:13 +13003058BEGIN {
3059 memoize 'lookup_svn_merge';
Sam Vilain7a955a52009-12-20 05:26:26 +13003060 memoize 'check_cherry_pick';
Sam Vilain7d944c32009-12-20 00:55:13 +13003061}
3062
Sam Vilainea020cb2009-12-20 05:25:31 +13003063sub parents_exclude {
3064 my $parents = shift;
3065 my @commits = @_;
3066 return unless @commits;
3067
3068 my @excluded;
3069 my $excluded;
3070 do {
3071 my @cmd = ('rev-list', "-1", @commits, "--not", @$parents );
3072 $excluded = command_oneline(@cmd);
3073 if ( $excluded ) {
3074 my @new;
3075 my $found;
3076 for my $commit ( @commits ) {
3077 if ( $commit eq $excluded ) {
3078 push @excluded, $commit;
3079 $found++;
3080 last;
3081 }
3082 else {
3083 push @new, $commit;
3084 }
3085 }
3086 die "saw commit '$excluded' in rev-list output, "
3087 ."but we didn't ask for that commit (wanted: @commits --not @$parents)"
3088 unless $found;
3089 @commits = @new;
3090 }
3091 }
3092 while ($excluded and @commits);
3093
3094 return @excluded;
3095}
3096
3097
Sam Vilaindff589e2009-10-20 15:42:03 +13003098# note: this function should only be called if the various dirprops
3099# have actually changed
3100sub find_extra_svn_parents {
3101 my ($self, $ed, $mergeinfo, $parents) = @_;
3102 # aha! svk:merge property changed...
3103
3104 # We first search for merged tips which are not in our
3105 # history. Then, we figure out which git revisions are in
3106 # that tip, but not this revision. If all of those revisions
3107 # are now marked as merge, we can add the tip as a parent.
3108 my @merges = split "\n", $mergeinfo;
3109 my @merge_tips;
Sam Vilaindff589e2009-10-20 15:42:03 +13003110 my $url = $self->rewrite_root || $self->{url};
Sam Vilain7d944c32009-12-20 00:55:13 +13003111 my $uuid = $self->ra_uuid;
Sam Vilainea020cb2009-12-20 05:25:31 +13003112 my %ranges;
Sam Vilaindff589e2009-10-20 15:42:03 +13003113 for my $merge ( @merges ) {
Sam Vilain7d944c32009-12-20 00:55:13 +13003114 my ($tip_commit, @ranges) =
3115 lookup_svn_merge( $uuid, $url, $merge );
Sam Vilaindff589e2009-10-20 15:42:03 +13003116 unless (!$tip_commit or
3117 grep { $_ eq $tip_commit } @$parents ) {
3118 push @merge_tips, $tip_commit;
Sam Vilainea020cb2009-12-20 05:25:31 +13003119 $ranges{$tip_commit} = \@ranges;
Sam Vilaindff589e2009-10-20 15:42:03 +13003120 } else {
3121 push @merge_tips, undef;
3122 }
3123 }
Sam Vilainea020cb2009-12-20 05:25:31 +13003124
3125 my %excluded = map { $_ => 1 }
3126 parents_exclude($parents, grep { defined } @merge_tips);
3127
3128 # check merge tips for new parents
3129 my @new_parents;
Sam Vilaindff589e2009-10-20 15:42:03 +13003130 for my $merge_tip ( @merge_tips ) {
3131 my $spec = shift @merges;
Sam Vilainea020cb2009-12-20 05:25:31 +13003132 next unless $merge_tip and $excluded{$merge_tip};
3133
3134 my $ranges = $ranges{$merge_tip};
3135
Sam Vilain7a955a52009-12-20 05:26:26 +13003136 # check out 'new' tips
3137 my $merge_base = command_oneline(
3138 "merge-base",
3139 @$parents, $merge_tip,
3140 );
3141
3142 # double check that there are no missing non-merge commits
3143 my (@incomplete) = check_cherry_pick(
3144 $merge_base, $merge_tip,
3145 @$ranges,
3146 );
3147
3148 if ( @incomplete ) {
3149 warn "W:svn cherry-pick ignored ($spec) - missing "
3150 .@incomplete." commit(s) (eg $incomplete[0])\n";
3151 } else {
3152 warn
3153 "Found merge parent (svn:mergeinfo prop): ",
3154 $merge_tip, "\n";
3155 push @new_parents, $merge_tip;
Sam Vilaindff589e2009-10-20 15:42:03 +13003156 }
Sam Vilain7a955a52009-12-20 05:26:26 +13003157 }
3158
3159 # cater for merges which merge commits from multiple branches
3160 if ( @new_parents > 1 ) {
3161 for ( my $i = 0; $i <= $#new_parents; $i++ ) {
3162 for ( my $j = 0; $j <= $#new_parents; $j++ ) {
3163 next if $i == $j;
3164 next unless $new_parents[$i];
3165 next unless $new_parents[$j];
3166 my $revs = command_oneline(
Eric Wong0fe19752009-12-22 12:15:40 -08003167 "rev-list", "-1",
3168 "$new_parents[$i]..$new_parents[$j]",
Sam Vilain7a955a52009-12-20 05:26:26 +13003169 );
3170 if ( !$revs ) {
3171 undef($new_parents[$i]);
3172 }
Sam Vilaindff589e2009-10-20 15:42:03 +13003173 }
3174 }
3175 }
Sam Vilain7a955a52009-12-20 05:26:26 +13003176 push @$parents, grep { defined } @new_parents;
Sam Vilaindff589e2009-10-20 15:42:03 +13003177}
3178
Eric Wong9b981fc2007-01-11 12:14:21 -08003179sub make_log_entry {
Eric Wong97f69872007-01-25 11:53:13 -08003180 my ($self, $rev, $parents, $ed) = @_;
3181 my $untracked = $self->get_untracked($ed);
3182
Sam Vilainf1264bd2009-10-20 15:42:01 +13003183 my @parents = @$parents;
3184 my $ps = $ed->{path_strip} || "";
3185 for my $path ( grep { m/$ps/ } %{$ed->{dir_prop}} ) {
3186 my $props = $ed->{dir_prop}{$path};
3187 if ( $props->{"svk:merge"} ) {
3188 $self->find_extra_svk_parents
3189 ($ed, $props->{"svk:merge"}, \@parents);
3190 }
Sam Vilaindff589e2009-10-20 15:42:03 +13003191 if ( $props->{"svn:mergeinfo"} ) {
3192 $self->find_extra_svn_parents
3193 ($ed,
3194 $props->{"svn:mergeinfo"},
3195 \@parents);
3196 }
Sam Vilainf1264bd2009-10-20 15:42:01 +13003197 }
3198
Eric Wong9b981fc2007-01-11 12:14:21 -08003199 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08003200 print $un "r$rev\n" or croak $!;
3201 print $un $_, "\n" foreach @$untracked;
Sam Vilainf1264bd2009-10-20 15:42:01 +13003202 my %log_entry = ( parents => \@parents, revision => $rev,
Eric Wong97f69872007-01-25 11:53:13 -08003203 log => '');
Eric Wongfbcc1732007-02-06 18:35:30 -08003204
Eric Wong8a49ee92007-02-10 20:46:50 -08003205 my $headrev;
Eric Wongfbcc1732007-02-06 18:35:30 -08003206 my $logged = delete $self->{logged_rev_props};
Eric Wong8a49ee92007-02-10 20:46:50 -08003207 if (!$logged || $self->{-want_revprops}) {
Eric Wongfbcc1732007-02-06 18:35:30 -08003208 my $rp = $self->ra->rev_proplist($rev);
3209 foreach (sort keys %$rp) {
3210 my $v = $rp->{$_};
3211 if (/^svn:(author|date|log)$/) {
3212 $log_entry{$1} = $v;
Eric Wong8a49ee92007-02-10 20:46:50 -08003213 } elsif ($_ eq 'svm:headrev') {
3214 $headrev = $v;
Eric Wongfbcc1732007-02-06 18:35:30 -08003215 } else {
3216 print $un " rev_prop: ", uri_encode($_), ' ',
3217 uri_encode($v), "\n";
3218 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003219 }
Eric Wongfbcc1732007-02-06 18:35:30 -08003220 } else {
3221 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
Eric Wong9b981fc2007-01-11 12:14:21 -08003222 }
3223 close $un or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08003224
Eric Wong9b981fc2007-01-11 12:14:21 -08003225 $log_entry{date} = parse_svn_date($log_entry{date});
Eric Wong9b981fc2007-01-11 12:14:21 -08003226 $log_entry{log} .= "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08003227 my $author = $log_entry{author} = check_author($log_entry{author});
3228 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003229 : ($author, undef);
3230
3231 my ($commit_name, $commit_email) = ($name, $email);
3232 if ($_use_log_author) {
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00003233 my $name_field;
3234 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
3235 $name_field = $1;
3236 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
3237 $name_field = $1;
3238 }
3239 if (!defined $name_field) {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02003240 if (!defined $email) {
3241 $email = $name;
3242 }
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00003243 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003244 ($name, $email) = ($1, $2);
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00003245 } elsif ($name_field =~ /(.*)@/) {
3246 ($name, $email) = ($1, $name_field);
3247 } else {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02003248 ($name, $email) = ($name_field, $name_field);
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003249 }
3250 }
Eric Wong91b03282007-02-11 00:51:33 -08003251 if (defined $headrev && $self->use_svm_props) {
Eric Wongaea736c2007-02-16 19:15:21 -08003252 if ($self->rewrite_root) {
3253 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
3254 "options set!\n";
3255 }
Eric Wongb3e95932009-07-11 14:13:12 -07003256 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$}i;
Eric Wongbefc9ad2007-02-17 02:53:07 -08003257 # we don't want "SVM: initializing mirror for junk" ...
3258 return undef if $r == 0;
3259 my $svm = $self->svm;
3260 if ($uuid ne $svm->{uuid}) {
Eric Wong8a49ee92007-02-10 20:46:50 -08003261 die "UUID mismatch on SVM path:\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08003262 "expected: $svm->{uuid}\n",
Eric Wong8a49ee92007-02-10 20:46:50 -08003263 " got: $uuid\n";
3264 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08003265 my $full_url = $self->full_url;
3266 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
3267 die "Failed to replace '$svm->{replace}' with ",
3268 "'$svm->{source}' in $full_url\n";
Sam Vilain18ea92b2007-02-23 12:32:29 +13003269 # throw away username for storing in records
3270 remove_username($full_url);
Eric Wong8a49ee92007-02-10 20:46:50 -08003271 $log_entry{metadata} = "$full_url\@$r $uuid";
3272 $log_entry{svm_revision} = $r;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003273 $email ||= "$author\@$uuid";
3274 $commit_email ||= "$author\@$uuid";
Eric Wong62e349d2007-02-16 19:57:29 -08003275 } elsif ($self->use_svnsync_props) {
3276 my $full_url = $self->svnsync->{url};
3277 $full_url .= "/$self->{path}" if length $self->{path};
Adam Robence118732007-04-24 18:02:07 -07003278 remove_username($full_url);
Eric Wong62e349d2007-02-16 19:57:29 -08003279 my $uuid = $self->svnsync->{uuid};
3280 $log_entry{metadata} = "$full_url\@$rev $uuid";
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003281 $email ||= "$author\@$uuid";
3282 $commit_email ||= "$author\@$uuid";
Eric Wong8a49ee92007-02-10 20:46:50 -08003283 } else {
Adam Robence118732007-04-24 18:02:07 -07003284 my $url = $self->metadata_url;
3285 remove_username($url);
3286 $log_entry{metadata} = "$url\@$rev " .
Eric Wong26a62d52007-02-12 13:25:25 -08003287 $self->ra->get_uuid;
Eric Wongdb03cd22007-02-13 00:38:02 -08003288 $email ||= "$author\@" . $self->ra->get_uuid;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003289 $commit_email ||= "$author\@" . $self->ra->get_uuid;
Eric Wong8a49ee92007-02-10 20:46:50 -08003290 }
Eric Wongdb03cd22007-02-13 00:38:02 -08003291 $log_entry{name} = $name;
3292 $log_entry{email} = $email;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003293 $log_entry{commit_name} = $commit_name;
3294 $log_entry{commit_email} = $commit_email;
Eric Wong9b981fc2007-01-11 12:14:21 -08003295 \%log_entry;
3296}
3297
3298sub fetch {
Eric Wong3ebe8df2007-01-25 17:35:40 -08003299 my ($self, $min_rev, $max_rev, @parents) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08003300 my ($last_rev, $last_commit) = $self->last_rev_commit;
Eric Wong3ebe8df2007-01-25 17:35:40 -08003301 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
Eric Wonge5181922007-02-08 12:53:57 -08003302 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
Eric Wong9b981fc2007-01-11 12:14:21 -08003303}
3304
3305sub set_tree_cb {
3306 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
Eric Wong490f49e2007-02-10 13:58:33 -08003307 $self->{inject_parents} = { $rev => $tree };
3308 $self->fetch(undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08003309}
3310
3311sub set_tree {
3312 my ($self, $tree) = (shift, shift);
Eric Wong1ce255d2007-01-14 23:21:16 -08003313 my $log_entry = ::get_commit_entry($tree);
Eric Wong9b981fc2007-01-11 12:14:21 -08003314 unless ($self->{last_rev}) {
Luc Heinrich0a1a1c82008-09-29 15:58:18 +02003315 ::fatal("Must have an existing revision to commit");
Eric Wong9b981fc2007-01-11 12:14:21 -08003316 }
Eric Wong61395352007-01-27 14:33:08 -08003317 my %ed_opts = ( r => $self->{last_rev},
3318 log => $log_entry->{log},
3319 ra => $self->ra,
3320 tree_a => $self->{last_commit},
3321 tree_b => $tree,
3322 editor_cb => sub {
3323 $self->set_tree_cb($log_entry, $tree, @_) },
3324 svn_path => $self->{path} );
3325 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong9b981fc2007-01-11 12:14:21 -08003326 print "No changes\nr$self->{last_rev} = $tree\n";
3327 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003328}
3329
Eric Wong060610c2007-12-08 23:27:41 -08003330sub rebuild_from_rev_db {
3331 my ($self, $path) = @_;
3332 my $r = -1;
3333 open my $fh, '<', $path or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02003334 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003335 while (<$fh>) {
3336 length($_) == 41 or croak "inconsistent size in ($_) != 41";
3337 chomp($_);
3338 ++$r;
3339 next if $_ eq ('0' x 40);
3340 $self->rev_map_set($r, $_);
3341 print "r$r = $_\n";
3342 }
3343 close $fh or croak "close: $!";
3344 unlink $path or croak "unlink: $!";
3345}
3346
Eric Wongf0ecca12007-01-30 13:11:14 -08003347sub rebuild {
3348 my ($self) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003349 my $map_path = $self->map_path;
Deskin Miller2beec892008-09-15 21:12:58 -04003350 my $partial = (-e $map_path && ! -z $map_path);
Eric Wongd6d33462007-02-16 04:05:33 -08003351 return unless ::verify_ref($self->refname.'^0');
Deskin Miller2beec892008-09-15 21:12:58 -04003352 if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
Eric Wong060610c2007-12-08 23:27:41 -08003353 my $rev_db = $self->rev_db_path;
3354 $self->rebuild_from_rev_db($rev_db);
3355 if ($self->use_svm_props) {
3356 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
3357 $self->rebuild_from_rev_db($svm_rev_db);
3358 }
3359 $self->unlink_rev_db_symlink;
Eric Wong26a62d52007-02-12 13:25:25 -08003360 return;
3361 }
Deskin Miller2beec892008-09-15 21:12:58 -04003362 print "Rebuilding $map_path ...\n" if (!$partial);
3363 my ($base_rev, $head) = ($partial ? $self->rev_map_max_norebuild(1) :
3364 (undef, undef));
Eric Wong060610c2007-12-08 23:27:41 -08003365 my ($log, $ctx) =
3366 command_output_pipe(qw/rev-list --pretty=raw --no-color --reverse/,
Deskin Miller2beec892008-09-15 21:12:58 -04003367 ($head ? "$head.." : "") . $self->refname,
3368 '--');
Jan Krüger74b1e122008-06-24 02:17:36 +02003369 my $metadata_url = $self->metadata_url;
3370 remove_username($metadata_url);
Eric Wong060610c2007-12-08 23:27:41 -08003371 my $svn_uuid = $self->ra_uuid;
Sam Vilain3dfab992007-06-30 20:56:13 +12003372 my $c;
3373 while (<$log>) {
3374 if ( m{^commit ($::sha1)$} ) {
3375 $c = $1;
3376 next;
3377 }
3378 next unless s{^\s*(git-svn-id:)}{$1};
3379 my ($url, $rev, $uuid) = ::extract_metadata($_);
Sam Vilain18ea92b2007-02-23 12:32:29 +13003380 remove_username($url);
Eric Wongf0ecca12007-01-30 13:11:14 -08003381
3382 # ignore merges (from set-tree)
3383 next if (!defined $rev || !$uuid);
3384
3385 # if we merged or otherwise started elsewhere, this is
3386 # how we break out of it
Eric Wong060610c2007-12-08 23:27:41 -08003387 if (($uuid ne $svn_uuid) ||
Jan Krüger74b1e122008-06-24 02:17:36 +02003388 ($metadata_url && $url && ($url ne $metadata_url))) {
Eric Wongf0ecca12007-01-30 13:11:14 -08003389 next;
3390 }
Deskin Miller2beec892008-09-15 21:12:58 -04003391 if ($partial && $head) {
3392 print "Partial-rebuilding $map_path ...\n";
3393 print "Currently at $base_rev = $head\n";
3394 $head = undef;
3395 }
Eric Wongf0ecca12007-01-30 13:11:14 -08003396
Eric Wong060610c2007-12-08 23:27:41 -08003397 $self->rev_map_set($rev, $c);
Eric Wongf0ecca12007-01-30 13:11:14 -08003398 print "r$rev = $c\n";
3399 }
Sam Vilain3dfab992007-06-30 20:56:13 +12003400 command_close_pipe($log, $ctx);
Deskin Miller2beec892008-09-15 21:12:58 -04003401 print "Done rebuilding $map_path\n" if (!$partial || !$head);
Eric Wong060610c2007-12-08 23:27:41 -08003402 my $rev_db_path = $self->rev_db_path;
3403 if (-f $self->rev_db_path) {
3404 unlink $self->rev_db_path or croak "unlink: $!";
3405 }
3406 $self->unlink_rev_db_symlink;
Eric Wongf0ecca12007-01-30 13:11:14 -08003407}
3408
Eric Wong060610c2007-12-08 23:27:41 -08003409# rev_map:
Eric Wong9b981fc2007-01-11 12:14:21 -08003410# Tie::File seems to be prone to offset errors if revisions get sparse,
3411# it's not that fast, either. Tie::File is also not in Perl 5.6. So
3412# one of my favorite modules is out :< Next up would be one of the DBM
Eric Wong060610c2007-12-08 23:27:41 -08003413# modules, but I'm not sure which is most portable...
3414#
3415# This is the replacement for the rev_db format, which was too big
3416# and inefficient for large repositories with a lot of sparse history
3417# (mainly tags)
3418#
3419# The format is this:
3420# - 24 bytes for every record,
3421# * 4 bytes for the integer representing an SVN revision number
3422# * 20 bytes representing the sha1 of a git commit
3423# - No empty padding records like the old format
Eric Wong66ab84b2007-12-08 23:27:42 -08003424# (except the last record, which can be overwritten)
Eric Wong060610c2007-12-08 23:27:41 -08003425# - new records are written append-only since SVN revision numbers
3426# increase monotonically
3427# - lookups on SVN revision number are done via a binary search
Eric Wong66ab84b2007-12-08 23:27:42 -08003428# - Piping the file to xxd -c24 is a good way of dumping it for
3429# viewing or editing (piped back through xxd -r), should the need
3430# ever arise.
3431# - The last record can be padding revision with an all-zero sha1
3432# This is used to optimize fetch performance when using multiple
3433# "fetch" directives in .git/config
Eric Wong060610c2007-12-08 23:27:41 -08003434#
Eric Wong97ae0912007-02-11 15:21:24 -08003435# These files are disposable unless noMetadata or useSvmProps is set
Eric Wong9b981fc2007-01-11 12:14:21 -08003436
Eric Wong060610c2007-12-08 23:27:41 -08003437sub _rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08003438 my ($fh, $rev, $commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003439
Michael Weber4f7ec792008-04-18 15:12:04 +02003440 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003441 my $size = (stat($fh))[7];
3442 ($size % 24) == 0 or croak "inconsistent size: $size";
3443
Eric Wong66ab84b2007-12-08 23:27:42 -08003444 my $wr_offset = 0;
Eric Wong060610c2007-12-08 23:27:41 -08003445 if ($size > 0) {
3446 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
3447 my $read = sysread($fh, my $buf, 24) or croak "read: $!";
3448 $read == 24 or croak "read only $read bytes (!= 24)";
3449 my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08003450 if ($last_commit eq ('0' x40)) {
3451 if ($size >= 48) {
3452 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
3453 $read = sysread($fh, $buf, 24) or
3454 croak "read: $!";
3455 $read == 24 or
3456 croak "read only $read bytes (!= 24)";
3457 ($last_rev, $last_commit) =
3458 unpack(rev_map_fmt, $buf);
3459 if ($last_commit eq ('0' x40)) {
3460 croak "inconsistent .rev_map\n";
3461 }
3462 }
3463 if ($last_rev >= $rev) {
3464 croak "last_rev is higher!: $last_rev >= $rev";
3465 }
3466 $wr_offset = -24;
Eric Wong47a0b752007-01-31 05:13:30 -08003467 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003468 }
Eric Wong66ab84b2007-12-08 23:27:42 -08003469 sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003470 syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or
3471 croak "write: $!";
Eric Wong26a62d52007-02-12 13:25:25 -08003472}
3473
Ben Jackson195643f2009-06-03 20:45:52 -07003474sub _rev_map_reset {
3475 my ($fh, $rev, $commit) = @_;
3476 my $c = _rev_map_get($fh, $rev);
3477 $c eq $commit or die "_rev_map_reset(@_) commit $c does not match!\n";
3478 my $offset = sysseek($fh, 0, SEEK_CUR) or croak "seek: $!";
3479 truncate $fh, $offset or croak "truncate: $!";
3480}
3481
Eric Wong26a62d52007-02-12 13:25:25 -08003482sub mkfile {
3483 my ($path) = @_;
3484 unless (-e $path) {
3485 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
3486 mkpath([$dir]) unless -d $dir;
3487 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
3488 close $fh or die "Couldn't close (create) $path: $!\n";
3489 }
3490}
3491
Eric Wong060610c2007-12-08 23:27:41 -08003492sub rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08003493 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
3494 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
Eric Wong060610c2007-12-08 23:27:41 -08003495 my $db = $self->map_path($uuid);
Eric Wong26a62d52007-02-12 13:25:25 -08003496 my $db_lock = "$db.lock";
3497 my $sig;
Ben Jackson195643f2009-06-03 20:45:52 -07003498 $update_ref ||= 0;
Eric Wong26a62d52007-02-12 13:25:25 -08003499 if ($update_ref) {
3500 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
3501 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
3502 }
3503 mkfile($db);
3504
3505 $LOCKFILES{$db_lock} = 1;
3506 my $sync;
3507 # both of these options make our .rev_db file very, very important
3508 # and we can't afford to lose it because rebuild() won't work
3509 if ($self->use_svm_props || $self->no_metadata) {
3510 $sync = 1;
Eric Wong060610c2007-12-08 23:27:41 -08003511 copy($db, $db_lock) or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08003512 "Failed to copy: ",
3513 "$db => $db_lock ($!)\n";
3514 } else {
Eric Wong060610c2007-12-08 23:27:41 -08003515 rename $db, $db_lock or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08003516 "Failed to rename: ",
3517 "$db => $db_lock ($!)\n";
3518 }
Eric Wong060610c2007-12-08 23:27:41 -08003519
Eric Wong66ab84b2007-12-08 23:27:42 -08003520 sysopen(my $fh, $db_lock, O_RDWR | O_CREAT)
Eric Wong060610c2007-12-08 23:27:41 -08003521 or croak "Couldn't open $db_lock: $!\n";
Ben Jackson195643f2009-06-03 20:45:52 -07003522 $update_ref eq 'reset' ? _rev_map_reset($fh, $rev, $commit) :
3523 _rev_map_set($fh, $rev, $commit);
Eric Wong97ae0912007-02-11 15:21:24 -08003524 if ($sync) {
3525 $fh->flush or die "Couldn't flush $db_lock: $!\n";
3526 $fh->sync or die "Couldn't sync $db_lock: $!\n";
3527 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003528 close $fh or croak $!;
Eric Wong373274f2007-01-31 13:54:23 -08003529 if ($update_ref) {
Eric Wong1e889ef2007-02-16 01:45:13 -08003530 $_head = $self;
Ben Jackson195643f2009-06-03 20:45:52 -07003531 my $note = "";
3532 $note = " ($update_ref)" if ($update_ref !~ /^\d*$/);
3533 command_noisy('update-ref', '-m', "r$rev$note",
Eric Wong373274f2007-01-31 13:54:23 -08003534 $self->refname, $commit);
3535 }
Eric Wong060610c2007-12-08 23:27:41 -08003536 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
Eric Wong373274f2007-01-31 13:54:23 -08003537 "$db_lock => $db ($!)\n";
3538 delete $LOCKFILES{$db_lock};
3539 if ($update_ref) {
3540 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
3541 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
3542 kill $sig, $$ if defined $sig;
3543 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003544}
3545
Eric Wong66ab84b2007-12-08 23:27:42 -08003546# If want_commit, this will return an array of (rev, commit) where
3547# commit _must_ be a valid commit in the archive.
3548# Otherwise, it'll return the max revision (whether or not the
3549# commit is valid or just a 0x40 placeholder).
Eric Wong060610c2007-12-08 23:27:41 -08003550sub rev_map_max {
Eric Wong66ab84b2007-12-08 23:27:42 -08003551 my ($self, $want_commit) = @_;
Eric Wongd6d33462007-02-16 04:05:33 -08003552 $self->rebuild;
Deskin Miller2beec892008-09-15 21:12:58 -04003553 my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
3554 $want_commit ? ($r, $c) : $r;
3555}
3556
3557sub rev_map_max_norebuild {
3558 my ($self, $want_commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003559 my $map_path = $self->map_path;
Eric Wong66ab84b2007-12-08 23:27:42 -08003560 stat $map_path or return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08003561 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02003562 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003563 my $size = (stat($fh))[7];
3564 ($size % 24) == 0 or croak "inconsistent size: $size";
3565
3566 if ($size == 0) {
3567 close $fh or croak "close: $!";
Eric Wong66ab84b2007-12-08 23:27:42 -08003568 return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08003569 }
3570
Eric Wong66ab84b2007-12-08 23:27:42 -08003571 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003572 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003573 my ($r, $c) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08003574 if ($want_commit && $c eq ('0' x40)) {
3575 if ($size < 48) {
3576 return $want_commit ? (0, undef) : 0;
3577 }
3578 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
3579 sysread($fh, $buf, 24) == 24 or croak "read: $!";
3580 ($r, $c) = unpack(rev_map_fmt, $buf);
3581 if ($c eq ('0'x40)) {
3582 croak "Penultimate record is all-zeroes in $map_path";
3583 }
3584 }
3585 close $fh or croak "close: $!";
3586 $want_commit ? ($r, $c) : $r;
Eric Wong9c93fee2007-01-31 17:22:31 -08003587}
3588
Eric Wong060610c2007-12-08 23:27:41 -08003589sub rev_map_get {
Eric Wong26a62d52007-02-12 13:25:25 -08003590 my ($self, $rev, $uuid) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003591 my $map_path = $self->map_path($uuid);
3592 return undef unless -e $map_path;
3593
3594 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Ben Jackson195643f2009-06-03 20:45:52 -07003595 my $c = _rev_map_get($fh, $rev);
3596 close($fh) or croak "close: $!";
3597 $c
3598}
3599
3600sub _rev_map_get {
3601 my ($fh, $rev) = @_;
3602
Michael Weber4f7ec792008-04-18 15:12:04 +02003603 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003604 my $size = (stat($fh))[7];
3605 ($size % 24) == 0 or croak "inconsistent size: $size";
3606
3607 if ($size == 0) {
Eric Wong060610c2007-12-08 23:27:41 -08003608 return undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003609 }
Eric Wong060610c2007-12-08 23:27:41 -08003610
3611 my ($l, $u) = (0, $size - 24);
3612 my ($r, $c, $buf);
3613
3614 while ($l <= $u) {
3615 my $i = int(($l/24 + $u/24) / 2) * 24;
3616 sysseek($fh, $i, SEEK_SET) or croak "seek: $!";
3617 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
Eric Wongc83f4e62009-08-12 22:20:02 -07003618 my ($r, $c) = unpack(rev_map_fmt, $buf);
Eric Wong060610c2007-12-08 23:27:41 -08003619
3620 if ($r < $rev) {
3621 $l = $i + 24;
3622 } elsif ($r > $rev) {
3623 $u = $i - 24;
3624 } else { # $r == $rev
Eric Wong66ab84b2007-12-08 23:27:42 -08003625 return $c eq ('0' x 40) ? undef : $c;
Eric Wong060610c2007-12-08 23:27:41 -08003626 }
3627 }
Eric Wong060610c2007-12-08 23:27:41 -08003628 undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003629}
3630
David D Kilzer111947e2007-11-11 22:56:52 -08003631# Finds the first svn revision that exists on (if $eq_ok is true) or
3632# before $rev for the current branch. It will not search any lower
3633# than $min_rev. Returns the git commit hash and svn revision number
3634# if found, else (undef, undef).
Eric Wong15710b62007-01-22 02:20:33 -08003635sub find_rev_before {
David D Kilzer111947e2007-11-11 22:56:52 -08003636 my ($self, $rev, $eq_ok, $min_rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08003637 --$rev unless $eq_ok;
David D Kilzer111947e2007-11-11 22:56:52 -08003638 $min_rev ||= 1;
Ben Jacksonca5e8802009-06-03 20:45:51 -07003639 my $max_rev = $self->rev_map_max;
3640 $rev = $max_rev if ($rev > $max_rev);
David D Kilzer111947e2007-11-11 22:56:52 -08003641 while ($rev >= $min_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003642 if (my $c = $self->rev_map_get($rev)) {
Eric Wong15710b62007-01-22 02:20:33 -08003643 return ($rev, $c);
3644 }
3645 --$rev;
3646 }
3647 return (undef, undef);
3648}
3649
David D Kilzer111947e2007-11-11 22:56:52 -08003650# Finds the first svn revision that exists on (if $eq_ok is true) or
3651# after $rev for the current branch. It will not search any higher
3652# than $max_rev. Returns the git commit hash and svn revision number
3653# if found, else (undef, undef).
3654sub find_rev_after {
3655 my ($self, $rev, $eq_ok, $max_rev) = @_;
3656 ++$rev unless $eq_ok;
Eric Wong060610c2007-12-08 23:27:41 -08003657 $max_rev ||= $self->rev_map_max;
David D Kilzer111947e2007-11-11 22:56:52 -08003658 while ($rev <= $max_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003659 if (my $c = $self->rev_map_get($rev)) {
David D Kilzer111947e2007-11-11 22:56:52 -08003660 return ($rev, $c);
3661 }
3662 ++$rev;
3663 }
3664 return (undef, undef);
3665}
3666
Eric Wong9b981fc2007-01-11 12:14:21 -08003667sub _new {
Eric Wong706587f2007-01-18 17:50:01 -08003668 my ($class, $repo_id, $ref_id, $path) = @_;
3669 unless (defined $repo_id && length $repo_id) {
3670 $repo_id = $Git::SVN::default_repo_id;
3671 }
3672 unless (defined $ref_id && length $ref_id) {
Adam Brewster63de84a2009-08-03 21:40:38 -04003673 $_prefix = '' unless defined($_prefix);
Adam Brewster6f5748e2009-08-11 23:14:27 -04003674 $_[2] = $ref_id =
3675 "refs/remotes/$_prefix$Git::SVN::default_ref_id";
Eric Wong706587f2007-01-18 17:50:01 -08003676 }
Eric Wong7829f202008-06-28 20:40:32 -07003677 $_[1] = $repo_id;
Eric Wong706587f2007-01-18 17:50:01 -08003678 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
Adam Brewster6f5748e2009-08-11 23:14:27 -04003679
3680 # Older repos imported by us used $GIT_DIR/svn/foo instead of
3681 # $GIT_DIR/svn/refs/remotes/foo when tracking refs/remotes/foo
3682 if ($ref_id =~ m{^refs/remotes/(.*)}) {
3683 my $old_dir = "$ENV{GIT_DIR}/svn/$1";
3684 if (-d $old_dir && ! -d $dir) {
3685 $dir = $old_dir;
3686 }
3687 }
3688
Eric Wong706587f2007-01-18 17:50:01 -08003689 $_[3] = $path = '' unless (defined $path);
Adam Brewster6f5748e2009-08-11 23:14:27 -04003690 mkpath([$dir]);
Eric Wong26a62d52007-02-12 13:25:25 -08003691 bless {
3692 ref_id => $ref_id, dir => $dir, index => "$dir/index",
Eric Wong8a49ee92007-02-10 20:46:50 -08003693 path => $path, config => "$ENV{GIT_DIR}/svn/config",
Eric Wong060610c2007-12-08 23:27:41 -08003694 map_root => "$dir/.rev_map", repo_id => $repo_id }, $class;
Eric Wong26a62d52007-02-12 13:25:25 -08003695}
3696
Eric Wong060610c2007-12-08 23:27:41 -08003697# for read-only access of old .rev_db formats
3698sub unlink_rev_db_symlink {
3699 my ($self) = @_;
3700 my $link = $self->rev_db_path;
3701 $link =~ s/\.[\w-]+$// or croak "missing UUID at the end of $link";
3702 if (-l $link) {
3703 unlink $link or croak "unlink: $link failed!";
3704 }
3705}
3706
3707sub rev_db_path {
3708 my ($self, $uuid) = @_;
3709 my $db_path = $self->map_path($uuid);
3710 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
3711 or croak "map_path: $db_path does not contain '/.rev_map.' !";
3712 $db_path;
3713}
3714
3715# the new replacement for .rev_db
3716sub map_path {
Eric Wong26a62d52007-02-12 13:25:25 -08003717 my ($self, $uuid) = @_;
3718 $uuid ||= $self->ra_uuid;
Eric Wong060610c2007-12-08 23:27:41 -08003719 "$self->{map_root}.$uuid";
Eric Wong9b981fc2007-01-11 12:14:21 -08003720}
3721
Eric Wong1c8443b2007-01-14 02:17:00 -08003722sub uri_encode {
3723 my ($f) = @_;
3724 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
3725 $f
3726}
Eric Wong9b981fc2007-01-11 12:14:21 -08003727
Eric Wong6111b932009-11-15 18:57:16 -08003728sub uri_decode {
3729 my ($f) = @_;
3730 $f =~ s#%([0-9a-fA-F]{2})#chr(hex($1))#eg;
3731 $f
3732}
3733
Sam Vilain18ea92b2007-02-23 12:32:29 +13003734sub remove_username {
3735 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
3736}
3737
Eric Wongd976acf2007-01-04 00:45:03 -08003738package Git::SVN::Prompt;
3739use strict;
3740use warnings;
3741require SVN::Core;
3742use vars qw/$_no_auth_cache $_username/;
3743
3744sub simple {
Eric Wong30d055a2006-11-24 01:38:04 -08003745 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
3746 $may_save = undef if $_no_auth_cache;
3747 $default_username = $_username if defined $_username;
3748 if (defined $default_username && length $default_username) {
3749 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003750 print STDERR "Authentication realm: $realm\n";
3751 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003752 }
3753 $cred->username($default_username);
3754 } else {
Eric Wongd976acf2007-01-04 00:45:03 -08003755 username($cred, $realm, $may_save, $pool);
Eric Wong30d055a2006-11-24 01:38:04 -08003756 }
3757 $cred->password(_read_password("Password for '" .
3758 $cred->username . "': ", $realm));
3759 $cred->may_save($may_save);
3760 $SVN::_Core::SVN_NO_ERROR;
3761}
3762
Eric Wongd976acf2007-01-04 00:45:03 -08003763sub ssl_server_trust {
Eric Wong30d055a2006-11-24 01:38:04 -08003764 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
3765 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003766 print STDERR "Error validating server certificate for '$realm':\n";
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003767 {
3768 no warnings 'once';
3769 # All variables SVN::Auth::SSL::* are used only once,
3770 # so we're shutting up Perl warnings about this.
3771 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
3772 print STDERR " - The certificate is not issued ",
3773 "by a trusted authority. Use the\n",
3774 " fingerprint to validate ",
3775 "the certificate manually!\n";
3776 }
3777 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
3778 print STDERR " - The certificate hostname ",
3779 "does not match.\n";
3780 }
3781 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
3782 print STDERR " - The certificate is not yet valid.\n";
3783 }
3784 if ($failures & $SVN::Auth::SSL::EXPIRED) {
3785 print STDERR " - The certificate has expired.\n";
3786 }
3787 if ($failures & $SVN::Auth::SSL::OTHER) {
3788 print STDERR " - The certificate has ",
3789 "an unknown error.\n";
3790 }
3791 } # no warnings 'once'
Eric Wong6f729592007-01-15 20:15:55 -08003792 printf STDERR
3793 "Certificate information:\n".
Eric Wong30d055a2006-11-24 01:38:04 -08003794 " - Hostname: %s\n".
3795 " - Valid: from %s until %s\n".
3796 " - Issuer: %s\n".
3797 " - Fingerprint: %s\n",
3798 map $cert_info->$_, qw(hostname valid_from valid_until
Eric Wong6f729592007-01-15 20:15:55 -08003799 issuer_dname fingerprint);
Eric Wong30d055a2006-11-24 01:38:04 -08003800 my $choice;
3801prompt:
Eric Wong6f729592007-01-15 20:15:55 -08003802 print STDERR $may_save ?
Eric Wong30d055a2006-11-24 01:38:04 -08003803 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
3804 "(R)eject or accept (t)emporarily? ";
Eric Wong6f729592007-01-15 20:15:55 -08003805 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003806 $choice = lc(substr(<STDIN> || 'R', 0, 1));
3807 if ($choice =~ /^t$/i) {
3808 $cred->may_save(undef);
3809 } elsif ($choice =~ /^r$/i) {
3810 return -1;
3811 } elsif ($may_save && $choice =~ /^p$/i) {
3812 $cred->may_save($may_save);
3813 } else {
3814 goto prompt;
3815 }
3816 $cred->accepted_failures($failures);
3817 $SVN::_Core::SVN_NO_ERROR;
3818}
3819
Eric Wongd976acf2007-01-04 00:45:03 -08003820sub ssl_client_cert {
Eric Wong30d055a2006-11-24 01:38:04 -08003821 my ($cred, $realm, $may_save, $pool) = @_;
3822 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003823 print STDERR "Client certificate filename: ";
3824 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003825 chomp(my $filename = <STDIN>);
3826 $cred->cert_file($filename);
3827 $cred->may_save($may_save);
3828 $SVN::_Core::SVN_NO_ERROR;
3829}
3830
Eric Wongd976acf2007-01-04 00:45:03 -08003831sub ssl_client_cert_pw {
Eric Wong30d055a2006-11-24 01:38:04 -08003832 my ($cred, $realm, $may_save, $pool) = @_;
3833 $may_save = undef if $_no_auth_cache;
3834 $cred->password(_read_password("Password: ", $realm));
3835 $cred->may_save($may_save);
3836 $SVN::_Core::SVN_NO_ERROR;
3837}
3838
Eric Wongd976acf2007-01-04 00:45:03 -08003839sub username {
Eric Wong30d055a2006-11-24 01:38:04 -08003840 my ($cred, $realm, $may_save, $pool) = @_;
3841 $may_save = undef if $_no_auth_cache;
3842 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003843 print STDERR "Authentication realm: $realm\n";
Eric Wong30d055a2006-11-24 01:38:04 -08003844 }
3845 my $username;
3846 if (defined $_username) {
3847 $username = $_username;
3848 } else {
Eric Wong6f729592007-01-15 20:15:55 -08003849 print STDERR "Username: ";
3850 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003851 chomp($username = <STDIN>);
3852 }
3853 $cred->username($username);
3854 $cred->may_save($may_save);
3855 $SVN::_Core::SVN_NO_ERROR;
3856}
3857
3858sub _read_password {
3859 my ($prompt, $realm) = @_;
Eric Wong6f729592007-01-15 20:15:55 -08003860 print STDERR $prompt;
3861 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003862 require Term::ReadKey;
3863 Term::ReadKey::ReadMode('noecho');
3864 my $password = '';
3865 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
3866 last if $key =~ /[\012\015]/; # \n\r
3867 $password .= $key;
3868 }
3869 Term::ReadKey::ReadMode('restore');
Eric Wong6f729592007-01-15 20:15:55 -08003870 print STDERR "\n";
3871 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003872 $password;
3873}
3874
Eric Wong27a1a802006-11-27 21:44:48 -08003875package SVN::Git::Fetcher;
3876use vars qw/@ISA/;
3877use strict;
3878use warnings;
3879use Carp qw/croak/;
Adam Robenffe256f2008-05-23 16:19:41 +02003880use File::Temp qw/tempfile/;
Eric Wong27a1a802006-11-27 21:44:48 -08003881use IO::File qw//;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003882use vars qw/$_ignore_regex/;
Eric Wong27a1a802006-11-27 21:44:48 -08003883
3884# file baton members: path, mode_a, mode_b, pool, fh, blob, base
3885sub new {
Eric Wong8841b372009-02-11 01:56:58 -08003886 my ($class, $git_svn, $switch_path) = @_;
Eric Wong27a1a802006-11-27 21:44:48 -08003887 my $self = SVN::Delta::Editor->new;
3888 bless $self, $class;
Eric Wongdbc6c742009-01-11 16:51:10 -08003889 if (exists $git_svn->{last_commit}) {
3890 $self->{c} = $git_svn->{last_commit};
Eric Wong8841b372009-02-11 01:56:58 -08003891 $self->{empty_symlinks} =
3892 _mark_empty_symlinks($git_svn, $switch_path);
Eric Wongdbc6c742009-01-11 16:51:10 -08003893 }
Ben Jackson0d8bee72009-04-11 10:46:17 -07003894 $self->{ignore_regex} = eval { command_oneline('config', '--get',
3895 "svn-remote.$git_svn->{repo_id}.ignore-paths") };
Eric Wongd2a9a872006-12-12 14:47:00 -08003896 $self->{empty} = {};
3897 $self->{dir_prop} = {};
3898 $self->{file_prop} = {};
3899 $self->{absent_dir} = {};
3900 $self->{absent_file} = {};
Eric Wongef3cfaa2007-01-24 03:30:57 -08003901 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
Eric Wong27a1a802006-11-27 21:44:48 -08003902 $self;
3903}
3904
Eric Wongdbc6c742009-01-11 16:51:10 -08003905# this uses the Ra object, so it must be called before do_{switch,update},
3906# not inside them (when the Git::SVN::Fetcher object is passed) to
3907# do_{switch,update}
3908sub _mark_empty_symlinks {
Eric Wong8841b372009-02-11 01:56:58 -08003909 my ($git_svn, $switch_path) = @_;
Eric Wong4c58a712009-01-31 17:31:12 -08003910 my $bool = Git::config_bool('svn.brokenSymlinkWorkaround');
Eric Wong48679e52009-02-27 19:40:16 -08003911 return {} if (!defined($bool)) || (defined($bool) && ! $bool);
Eric Wong4c58a712009-01-31 17:31:12 -08003912
Eric Wongdbc6c742009-01-11 16:51:10 -08003913 my %ret;
3914 my ($rev, $cmt) = $git_svn->last_rev_commit;
3915 return {} unless ($rev && $cmt);
3916
Eric Wong4c58a712009-01-31 17:31:12 -08003917 # allow the warning to be printed for each revision we fetch to
3918 # ensure the user sees it. The user can also disable the workaround
3919 # on the repository even while git svn is running and the next
3920 # revision fetched will skip this expensive function.
3921 my $printed_warning;
Eric Wongdbc6c742009-01-11 16:51:10 -08003922 chomp(my $empty_blob = `git hash-object -t blob --stdin < /dev/null`);
3923 my ($ls, $ctx) = command_output_pipe(qw/ls-tree -r -z/, $cmt);
3924 local $/ = "\0";
Eric Wong8841b372009-02-11 01:56:58 -08003925 my $pfx = defined($switch_path) ? $switch_path : $git_svn->{path};
Eric Wongdbc6c742009-01-11 16:51:10 -08003926 $pfx .= '/' if length($pfx);
3927 while (<$ls>) {
3928 chomp;
3929 s/\A100644 blob $empty_blob\t//o or next;
Eric Wong4c58a712009-01-31 17:31:12 -08003930 unless ($printed_warning) {
3931 print STDERR "Scanning for empty symlinks, ",
3932 "this may take a while if you have ",
3933 "many empty files\n",
3934 "You may disable this with `",
3935 "git config svn.brokenSymlinkWorkaround ",
3936 "false'.\n",
3937 "This may be done in a different ",
3938 "terminal without restarting ",
3939 "git svn\n";
3940 $printed_warning = 1;
3941 }
Eric Wongdbc6c742009-01-11 16:51:10 -08003942 my $path = $_;
3943 my (undef, $props) =
3944 $git_svn->ra->get_file($pfx.$path, $rev, undef);
3945 if ($props->{'svn:special'}) {
3946 $ret{$path} = 1;
3947 }
3948 }
3949 command_close_pipe($ls, $ctx);
3950 \%ret;
3951}
3952
Eric Wongb03a71a2009-01-11 18:23:38 -08003953# returns true if a given path is inside a ".git" directory
3954sub in_dot_git {
3955 $_[0] =~ m{(?:^|/)\.git(?:/|$)};
3956}
3957
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003958# return value: 0 -- don't ignore, 1 -- ignore
3959sub is_path_ignored {
Ben Jackson0d8bee72009-04-11 10:46:17 -07003960 my ($self, $path) = @_;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003961 return 1 if in_dot_git($path);
Ben Jackson0d8bee72009-04-11 10:46:17 -07003962 return 1 if defined($self->{ignore_regex}) &&
3963 $path =~ m!$self->{ignore_regex}!;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003964 return 0 unless defined($_ignore_regex);
3965 return 1 if $path =~ m!$_ignore_regex!o;
3966 return 0;
3967}
3968
Eric Wong8b8fc062007-01-22 11:44:57 -08003969sub set_path_strip {
3970 my ($self, $path) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003971 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
Eric Wong8b8fc062007-01-22 11:44:57 -08003972}
3973
Eric Wongd2a9a872006-12-12 14:47:00 -08003974sub open_root {
3975 { path => '' };
3976}
3977
3978sub open_directory {
3979 my ($self, $path, $pb, $rev) = @_;
3980 { path => $path };
3981}
3982
Eric Wong706587f2007-01-18 17:50:01 -08003983sub git_path {
3984 my ($self, $path) = @_;
Eric Wong2b27f6c2007-01-28 04:59:05 -08003985 if ($self->{path_strip}) {
3986 $path =~ s!$self->{path_strip}!! or
3987 die "Failed to strip path '$path' ($self->{path_strip})\n";
3988 }
Eric Wong706587f2007-01-18 17:50:01 -08003989 $path;
3990}
3991
Eric Wong27a1a802006-11-27 21:44:48 -08003992sub delete_entry {
3993 my ($self, $path, $rev, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003994 return undef if $self->is_path_ignored($path);
Eric Wong4a87db02007-01-04 01:38:18 -08003995
Eric Wong706587f2007-01-18 17:50:01 -08003996 my $gpath = $self->git_path($path);
Eric Wong8a603772007-01-31 02:45:50 -08003997 return undef if ($gpath eq '');
3998
Eric Wong4a87db02007-01-04 01:38:18 -08003999 # remove entire directories.
Eric Wong4f821012009-03-28 22:10:08 -07004000 my ($tree) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
4001 =~ /\A040000 tree ([a-f\d]{40})\t\Q$gpath\E\0/);
4002 if ($tree) {
Eric Wong4a87db02007-01-04 01:38:18 -08004003 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
4004 -r --name-only -z/,
Eric Wong4f821012009-03-28 22:10:08 -07004005 $tree);
Eric Wong4a87db02007-01-04 01:38:18 -08004006 local $/ = "\0";
4007 while (<$ls>) {
Eric Wongef3cfaa2007-01-24 03:30:57 -08004008 chomp;
Eric Wong4f821012009-03-28 22:10:08 -07004009 my $rmpath = "$gpath/$_";
4010 $self->{gii}->remove($rmpath);
4011 print "\tD\t$rmpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08004012 }
Eric Wong9e3cdbd2007-02-09 12:23:47 -08004013 print "\tD\t$gpath/\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08004014 command_close_pipe($ls, $ctx);
Eric Wong4a87db02007-01-04 01:38:18 -08004015 } else {
Eric Wongef3cfaa2007-01-24 03:30:57 -08004016 $self->{gii}->remove($gpath);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08004017 print "\tD\t$gpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08004018 }
Eric Wongf9ad77a2009-12-07 20:49:38 -08004019 $self->{empty}->{$path} = 0;
Eric Wong27a1a802006-11-27 21:44:48 -08004020 undef;
4021}
4022
4023sub open_file {
4024 my ($self, $path, $pb, $rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08004025 my ($mode, $blob);
4026
Ben Jackson0d8bee72009-04-11 10:46:17 -07004027 goto out if $self->is_path_ignored($path);
Eric Wongb03a71a2009-01-11 18:23:38 -08004028
Eric Wong706587f2007-01-18 17:50:01 -08004029 my $gpath = $self->git_path($path);
Eric Wong4f821012009-03-28 22:10:08 -07004030 ($mode, $blob) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
4031 =~ /\A(\d{6}) blob ([a-f\d]{40})\t\Q$gpath\E\0/);
Eric Wong006ede52006-12-08 01:55:19 -08004032 unless (defined $mode && defined $blob) {
4033 die "$path was not found in commit $self->{c} (r$rev)\n";
4034 }
Eric Wongdbc6c742009-01-11 16:51:10 -08004035 if ($mode eq '100644' && $self->{empty_symlinks}->{$path}) {
4036 $mode = '120000';
4037 }
Eric Wongb03a71a2009-01-11 18:23:38 -08004038out:
Eric Wong27a1a802006-11-27 21:44:48 -08004039 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
Eric Wong0864e3b2006-11-28 02:50:17 -08004040 pool => SVN::Pool->new, action => 'M' };
Eric Wong27a1a802006-11-27 21:44:48 -08004041}
4042
4043sub add_file {
4044 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08004045 my $mode;
4046
Ben Jackson0d8bee72009-04-11 10:46:17 -07004047 if (!$self->is_path_ignored($path)) {
Eric Wongb03a71a2009-01-11 18:23:38 -08004048 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
4049 delete $self->{empty}->{$dir};
4050 $mode = '100644';
4051 }
4052 { path => $path, mode_a => $mode, mode_b => $mode,
Eric Wong0864e3b2006-11-28 02:50:17 -08004053 pool => SVN::Pool->new, action => 'A' };
Eric Wong27a1a802006-11-27 21:44:48 -08004054}
4055
Eric Wongd2a9a872006-12-12 14:47:00 -08004056sub add_directory {
4057 my ($self, $path, $cp_path, $cp_rev) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004058 goto out if $self->is_path_ignored($path);
Eric Wong12a6d752007-12-14 08:39:09 -08004059 my $gpath = $self->git_path($path);
4060 if ($gpath eq '') {
4061 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
4062 -r --name-only -z/,
4063 $self->{c});
4064 local $/ = "\0";
4065 while (<$ls>) {
4066 chomp;
4067 $self->{gii}->remove($_);
4068 print "\tD\t$_\n" unless $::_q;
4069 }
4070 command_close_pipe($ls, $ctx);
4071 $self->{empty}->{$path} = 0;
4072 }
Eric Wongd2a9a872006-12-12 14:47:00 -08004073 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
4074 delete $self->{empty}->{$dir};
4075 $self->{empty}->{$path} = 1;
Eric Wongb03a71a2009-01-11 18:23:38 -08004076out:
Eric Wongd2a9a872006-12-12 14:47:00 -08004077 { path => $path };
4078}
4079
4080sub change_dir_prop {
4081 my ($self, $db, $prop, $value) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004082 return undef if $self->is_path_ignored($db->{path});
Eric Wongd2a9a872006-12-12 14:47:00 -08004083 $self->{dir_prop}->{$db->{path}} ||= {};
4084 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
4085 undef;
4086}
4087
4088sub absent_directory {
4089 my ($self, $path, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004090 return undef if $self->is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08004091 $self->{absent_dir}->{$pb->{path}} ||= [];
4092 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
4093 undef;
4094}
4095
4096sub absent_file {
4097 my ($self, $path, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004098 return undef if $self->is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08004099 $self->{absent_file}->{$pb->{path}} ||= [];
4100 push @{$self->{absent_file}->{$pb->{path}}}, $path;
4101 undef;
4102}
4103
Eric Wong27a1a802006-11-27 21:44:48 -08004104sub change_file_prop {
4105 my ($self, $fb, $prop, $value) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004106 return undef if $self->is_path_ignored($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08004107 if ($prop eq 'svn:executable') {
4108 if ($fb->{mode_b} != 120000) {
4109 $fb->{mode_b} = defined $value ? 100755 : 100644;
4110 }
4111 } elsif ($prop eq 'svn:special') {
4112 $fb->{mode_b} = defined $value ? 120000 : 100644;
Eric Wongd2a9a872006-12-12 14:47:00 -08004113 } else {
4114 $self->{file_prop}->{$fb->{path}} ||= {};
4115 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
Eric Wong27a1a802006-11-27 21:44:48 -08004116 }
4117 undef;
4118}
4119
4120sub apply_textdelta {
4121 my ($self, $fb, $exp) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004122 return undef if $self->is_path_ignored($fb->{path});
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004123 my $fh = $::_repository->temp_acquire('svn_delta');
Eric Wong27a1a802006-11-27 21:44:48 -08004124 # $fh gets auto-closed() by SVN::TxDelta::apply(),
4125 # (but $base does not,) so dup() it for reading in close_file
4126 open my $dup, '<&', $fh or croak $!;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004127 my $base = $::_repository->temp_acquire('git_blob');
Eric Wongb03a71a2009-01-11 18:23:38 -08004128
Eric Wong27a1a802006-11-27 21:44:48 -08004129 if ($fb->{blob}) {
Eric Wongbaf5fa82009-01-11 16:51:11 -08004130 my ($base_is_link, $size);
4131
Eric Wongdbc6c742009-01-11 16:51:10 -08004132 if ($fb->{mode_a} eq '120000' &&
4133 ! $self->{empty_symlinks}->{$fb->{path}}) {
4134 print $base 'link ' or die "print $!\n";
Eric Wongbaf5fa82009-01-11 16:51:11 -08004135 $base_is_link = 1;
Eric Wongdbc6c742009-01-11 16:51:10 -08004136 }
Eric Wongbaf5fa82009-01-11 16:51:11 -08004137 retry:
4138 $size = $::_repository->cat_blob($fb->{blob}, $base);
Junio C Hamanod683a0e2008-05-27 23:33:22 -07004139 die "Failed to read object $fb->{blob}" if ($size < 0);
Eric Wong27a1a802006-11-27 21:44:48 -08004140
4141 if (defined $exp) {
4142 seek $base, 0, 0 or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08004143 my $got = ::md5sum($base);
Eric Wongbaf5fa82009-01-11 16:51:11 -08004144 if ($got ne $exp) {
4145 my $err = "Checksum mismatch: ".
4146 "$fb->{path} $fb->{blob}\n" .
4147 "expected: $exp\n" .
4148 " got: $got\n";
4149 if ($base_is_link) {
4150 warn $err,
4151 "Retrying... (possibly ",
4152 "a bad symlink from SVN)\n";
4153 $::_repository->temp_reset($base);
4154 $base_is_link = 0;
4155 goto retry;
4156 }
4157 die $err;
4158 }
Eric Wong27a1a802006-11-27 21:44:48 -08004159 }
4160 }
4161 seek $base, 0, 0 or croak $!;
Marcus Griep0b191382008-08-12 12:00:53 -04004162 $fb->{fh} = $fh;
Eric Wong27a1a802006-11-27 21:44:48 -08004163 $fb->{base} = $base;
Marcus Griep0b191382008-08-12 12:00:53 -04004164 [ SVN::TxDelta::apply($base, $dup, undef, $fb->{path}, $fb->{pool}) ];
Eric Wong27a1a802006-11-27 21:44:48 -08004165}
4166
4167sub close_file {
4168 my ($self, $fb, $exp) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004169 return undef if $self->is_path_ignored($fb->{path});
Eric Wongb03a71a2009-01-11 18:23:38 -08004170
Eric Wong27a1a802006-11-27 21:44:48 -08004171 my $hash;
Eric Wong706587f2007-01-18 17:50:01 -08004172 my $path = $self->git_path($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08004173 if (my $fh = $fb->{fh}) {
Eric Wong7faf0682007-05-27 15:59:01 -07004174 if (defined $exp) {
4175 seek($fh, 0, 0) or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08004176 my $got = ::md5sum($fh);
Eric Wong7faf0682007-05-27 15:59:01 -07004177 if ($got ne $exp) {
4178 die "Checksum mismatch: $path\n",
4179 "expected: $exp\n got: $got\n";
4180 }
4181 }
Eric Wong27a1a802006-11-27 21:44:48 -08004182 if ($fb->{mode_b} == 120000) {
Marcus Griep510b0942008-08-12 12:45:39 -04004183 sysseek($fh, 0, 0) or croak $!;
Eric Wongdbc6c742009-01-11 16:51:10 -08004184 my $rd = sysread($fh, my $buf, 5);
Marcus Griep510b0942008-08-12 12:45:39 -04004185
Eric Wongdbc6c742009-01-11 16:51:10 -08004186 if (!defined $rd) {
4187 croak "sysread: $!\n";
4188 } elsif ($rd == 0) {
Marcus Griep510b0942008-08-12 12:45:39 -04004189 warn "$path has mode 120000",
Eric Wongdbc6c742009-01-11 16:51:10 -08004190 " but it points to nothing\n",
4191 "converting to an empty file with mode",
4192 " 100644\n";
4193 $fb->{mode_b} = '100644';
4194 } elsif ($buf ne 'link ') {
4195 warn "$path has mode 120000",
4196 " but is not a link\n";
Marcus Griep510b0942008-08-12 12:45:39 -04004197 } else {
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004198 my $tmp_fh = $::_repository->temp_acquire(
4199 'svn_hash');
Marcus Griep510b0942008-08-12 12:45:39 -04004200 my $res;
4201 while ($res = sysread($fh, my $str, 1024)) {
4202 my $out = syswrite($tmp_fh, $str, $res);
4203 defined($out) && $out == $res
4204 or croak("write ",
Marcus Griep836ff952008-09-08 12:53:01 -04004205 Git::temp_path($tmp_fh),
Marcus Griep510b0942008-08-12 12:45:39 -04004206 ": $!\n");
4207 }
4208 defined $res or croak $!;
4209
4210 ($fh, $tmp_fh) = ($tmp_fh, $fh);
4211 Git::temp_release($tmp_fh, 1);
Eric Wong7fc35e02007-12-19 00:06:45 -08004212 }
Eric Wong27a1a802006-11-27 21:44:48 -08004213 }
Adam Robenffe256f2008-05-23 16:19:41 +02004214
Marcus Griep0b191382008-08-12 12:00:53 -04004215 $hash = $::_repository->hash_and_insert_object(
Marcus Griep836ff952008-09-08 12:53:01 -04004216 Git::temp_path($fh));
Eric Wong27a1a802006-11-27 21:44:48 -08004217 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
Marcus Griep0b191382008-08-12 12:00:53 -04004218
4219 Git::temp_release($fb->{base}, 1);
Marcus Griep510b0942008-08-12 12:45:39 -04004220 Git::temp_release($fh, 1);
Eric Wong27a1a802006-11-27 21:44:48 -08004221 } else {
4222 $hash = $fb->{blob} or die "no blob information\n";
4223 }
4224 $fb->{pool}->clear;
Eric Wongef3cfaa2007-01-24 03:30:57 -08004225 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
Eric Wong9e3cdbd2007-02-09 12:23:47 -08004226 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
Eric Wong27a1a802006-11-27 21:44:48 -08004227 undef;
4228}
4229
4230sub abort_edit {
4231 my $self = shift;
Eric Wongef3cfaa2007-01-24 03:30:57 -08004232 $self->{nr} = $self->{gii}->{nr};
4233 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08004234 $self->SUPER::abort_edit(@_);
4235}
4236
4237sub close_edit {
4238 my $self = shift;
Eric Wongdad73c02006-11-28 14:06:05 -08004239 $self->{git_commit_ok} = 1;
Eric Wongef3cfaa2007-01-24 03:30:57 -08004240 $self->{nr} = $self->{gii}->{nr};
4241 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08004242 $self->SUPER::close_edit(@_);
4243}
Eric Wong1a82e792006-06-16 02:55:13 -07004244
Eric Wonga5e0ced2006-06-12 15:23:48 -07004245package SVN::Git::Editor;
Eric Wong24e22aa2007-01-29 00:07:49 -08004246use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004247use strict;
4248use warnings;
4249use Carp qw/croak/;
4250use IO::File;
4251
4252sub new {
Eric Wong61395352007-01-27 14:33:08 -08004253 my ($class, $opts) = @_;
4254 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
4255 die "$_ required!\n" unless (defined $opts->{$_});
Eric Wonga5e0ced2006-06-12 15:23:48 -07004256 }
Eric Wong61395352007-01-27 14:33:08 -08004257
4258 my $pool = SVN::Pool->new;
4259 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
4260 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
4261 $opts->{r}, $mods);
4262
4263 # $opts->{ra} functions should not be used after this:
4264 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
4265 $opts->{editor_cb}, $pool);
4266 my $self = SVN::Delta::Editor->new(@ce, $pool);
4267 bless $self, $class;
4268 foreach (qw/svn_path r tree_a tree_b/) {
4269 $self->{$_} = $opts->{$_};
4270 }
4271 $self->{url} = $opts->{ra}->{url};
4272 $self->{mods} = $mods;
4273 $self->{types} = $types;
4274 $self->{pool} = $pool;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004275 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
4276 $self->{rm} = { };
Eric Wongd3a840d2007-01-26 01:32:45 -08004277 $self->{path_prefix} = length $self->{svn_path} ?
4278 "$self->{svn_path}/" : '';
Brad King128de652008-07-25 11:32:37 -04004279 $self->{config} = $opts->{config};
Eric Wonga5e0ced2006-06-12 15:23:48 -07004280 return $self;
4281}
4282
Eric Wong61395352007-01-27 14:33:08 -08004283sub generate_diff {
4284 my ($tree_a, $tree_b) = @_;
4285 my @diff_tree = qw(diff-tree -z -r);
Eric Wong24e22aa2007-01-29 00:07:49 -08004286 if ($_cp_similarity) {
4287 push @diff_tree, "-C$_cp_similarity";
Eric Wong61395352007-01-27 14:33:08 -08004288 } else {
4289 push @diff_tree, '-C';
4290 }
Eric Wong24e22aa2007-01-29 00:07:49 -08004291 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
4292 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
Eric Wong61395352007-01-27 14:33:08 -08004293 push @diff_tree, $tree_a, $tree_b;
4294 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
4295 local $/ = "\0";
4296 my $state = 'meta';
4297 my @mods;
4298 while (<$diff_fh>) {
4299 chomp $_; # this gets rid of the trailing "\0"
4300 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02004301 ($::sha1)\s($::sha1)\s
Eric Wong61395352007-01-27 14:33:08 -08004302 ([MTCRAD])\d*$/xo) {
4303 push @mods, { mode_a => $1, mode_b => $2,
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02004304 sha1_a => $3, sha1_b => $4,
4305 chg => $5 };
4306 if ($5 =~ /^(?:C|R)$/) {
Eric Wong61395352007-01-27 14:33:08 -08004307 $state = 'file_a';
4308 } else {
4309 $state = 'file_b';
4310 }
4311 } elsif ($state eq 'file_a') {
4312 my $x = $mods[$#mods] or croak "Empty array\n";
4313 if ($x->{chg} !~ /^(?:C|R)$/) {
4314 croak "Error parsing $_, $x->{chg}\n";
4315 }
4316 $x->{file_a} = $_;
4317 $state = 'file_b';
4318 } elsif ($state eq 'file_b') {
4319 my $x = $mods[$#mods] or croak "Empty array\n";
4320 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
4321 croak "Error parsing $_, $x->{chg}\n";
4322 }
4323 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
4324 croak "Error parsing $_, $x->{chg}\n";
4325 }
4326 $x->{file_b} = $_;
4327 $state = 'meta';
4328 } else {
4329 croak "Error parsing $_\n";
4330 }
4331 }
4332 command_close_pipe($diff_fh, $ctx);
4333 \@mods;
4334}
4335
4336sub check_diff_paths {
4337 my ($ra, $pfx, $rev, $mods) = @_;
4338 my %types;
4339 $pfx .= '/' if length $pfx;
4340
4341 sub type_diff_paths {
4342 my ($ra, $types, $path, $rev) = @_;
4343 my @p = split m#/+#, $path;
4344 my $c = shift @p;
4345 unless (defined $types->{$c}) {
4346 $types->{$c} = $ra->check_path($c, $rev);
4347 }
4348 while (@p) {
4349 $c .= '/' . shift @p;
4350 next if defined $types->{$c};
4351 $types->{$c} = $ra->check_path($c, $rev);
4352 }
4353 }
4354
4355 foreach my $m (@$mods) {
4356 foreach my $f (qw/file_a file_b/) {
4357 next unless defined $m->{$f};
4358 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
4359 if (length $pfx.$dir && ! defined $types{$dir}) {
4360 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
4361 }
4362 }
4363 }
4364 \%types;
4365}
4366
Eric Wonga5e0ced2006-06-12 15:23:48 -07004367sub split_path {
4368 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
4369}
4370
4371sub repo_path {
Eric Wongd3a840d2007-01-26 01:32:45 -08004372 my ($self, $path) = @_;
4373 $self->{path_prefix}.(defined $path ? $path : '');
Eric Wonga5e0ced2006-06-12 15:23:48 -07004374}
4375
4376sub url_path {
4377 my ($self, $path) = @_;
Eric Wong29633bb2007-07-15 21:53:50 -07004378 if ($self->{url} =~ m#^https?://#) {
Eric Wong884cce52009-07-25 02:29:28 -07004379 $path =~ s!([^~a-zA-Z0-9_./-])!uc sprintf("%%%02x",ord($1))!eg;
Eric Wong29633bb2007-07-15 21:53:50 -07004380 }
Eric Wong6e8548c2007-01-27 01:32:00 -08004381 $self->{url} . '/' . $self->repo_path($path);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004382}
4383
4384sub rmdirs {
Eric Wong61395352007-01-27 14:33:08 -08004385 my ($self) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004386 my $rm = $self->{rm};
4387 delete $rm->{''}; # we never delete the url we're tracking
4388 return unless %$rm;
4389
4390 foreach (keys %$rm) {
4391 my @d = split m#/#, $_;
4392 my $c = shift @d;
4393 $rm->{$c} = 1;
4394 while (@d) {
4395 $c .= '/' . shift @d;
4396 $rm->{$c} = 1;
4397 }
4398 }
4399 delete $rm->{$self->{svn_path}};
4400 delete $rm->{''}; # we never delete the url we're tracking
4401 return unless %$rm;
4402
Eric Wong61395352007-01-27 14:33:08 -08004403 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
4404 $self->{tree_b});
Eric Wonga5e0ced2006-06-12 15:23:48 -07004405 local $/ = "\0";
4406 while (<$fh>) {
4407 chomp;
Eric Wong747fa122006-11-24 22:38:17 -08004408 my @dn = split m#/#, $_;
Eric Wongc07eee12006-06-19 17:59:35 -07004409 while (pop @dn) {
4410 delete $rm->{join '/', @dn};
4411 }
4412 unless (%$rm) {
Eric Wong22600a22007-02-01 13:12:26 -08004413 close $fh;
Eric Wongc07eee12006-06-19 17:59:35 -07004414 return;
4415 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07004416 }
Eric Wongaef4e922006-12-15 10:59:54 -08004417 command_close_pipe($fh, $ctx);
Eric Wongc07eee12006-06-19 17:59:35 -07004418
Eric Wonga5e0ced2006-06-12 15:23:48 -07004419 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
4420 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
4421 $self->close_directory($bat->{$d}, $p);
4422 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
Eric Wong44320b92007-01-13 22:35:53 -08004423 print "\tD+\t$d/\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004424 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
4425 delete $bat->{$d};
4426 }
4427}
4428
4429sub open_or_add_dir {
4430 my ($self, $full_path, $baton) = @_;
Eric Wong6e8548c2007-01-27 01:32:00 -08004431 my $t = $self->{types}->{$full_path};
4432 if (!defined $t) {
4433 die "$full_path not known in r$self->{r} or we have a bug!\n";
4434 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004435 {
4436 no warnings 'once';
4437 # SVN::Node::none and SVN::Node::file are used only once,
4438 # so we're shutting up Perl's warnings about them.
4439 if ($t == $SVN::Node::none) {
4440 return $self->add_directory($full_path, $baton,
4441 undef, -1, $self->{pool});
4442 } elsif ($t == $SVN::Node::dir) {
4443 return $self->open_directory($full_path, $baton,
4444 $self->{r}, $self->{pool});
4445 } # no warnings 'once'
4446 print STDERR "$full_path already exists in repository at ",
4447 "r$self->{r} and it is not a directory (",
4448 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
4449 } # no warnings 'once'
Eric Wonga5e0ced2006-06-12 15:23:48 -07004450 exit 1;
4451}
4452
4453sub ensure_path {
4454 my ($self, $path) = @_;
4455 my $bat = $self->{bat};
Eric Wong6e8548c2007-01-27 01:32:00 -08004456 my $repo_path = $self->repo_path($path);
4457 return $bat->{''} unless (length $repo_path);
4458 my @p = split m#/+#, $repo_path;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004459 my $c = shift @p;
4460 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
4461 while (@p) {
4462 my $c0 = $c;
4463 $c .= '/' . shift @p;
4464 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
4465 }
4466 return $bat->{$c};
4467}
4468
Brad King128de652008-07-25 11:32:37 -04004469# Subroutine to convert a globbing pattern to a regular expression.
4470# From perl cookbook.
4471sub glob2pat {
4472 my $globstr = shift;
4473 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
4474 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
4475 return '^' . $globstr . '$';
4476}
4477
4478sub check_autoprop {
4479 my ($self, $pattern, $properties, $file, $fbat) = @_;
4480 # Convert the globbing pattern to a regular expression.
4481 my $regex = glob2pat($pattern);
4482 # Check if the pattern matches the file name.
4483 if($file =~ m/($regex)/) {
4484 # Parse the list of properties to set.
4485 my @props = split(/;/, $properties);
4486 foreach my $prop (@props) {
4487 # Parse 'name=value' syntax and set the property.
4488 if ($prop =~ /([^=]+)=(.*)/) {
4489 my ($n,$v) = ($1,$2);
4490 for ($n, $v) {
4491 s/^\s+//; s/\s+$//;
4492 }
4493 $self->change_file_prop($fbat, $n, $v);
4494 }
4495 }
4496 }
4497}
4498
4499sub apply_autoprops {
4500 my ($self, $file, $fbat) = @_;
4501 my $conf_t = ${$self->{config}}{'config'};
4502 no warnings 'once';
4503 # Check [miscellany]/enable-auto-props in svn configuration.
4504 if (SVN::_Core::svn_config_get_bool(
4505 $conf_t,
4506 $SVN::_Core::SVN_CONFIG_SECTION_MISCELLANY,
4507 $SVN::_Core::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS,
4508 0)) {
4509 # Auto-props are enabled. Enumerate them to look for matches.
4510 my $callback = sub {
4511 $self->check_autoprop($_[0], $_[1], $file, $fbat);
4512 };
4513 SVN::_Core::svn_config_enumerate(
4514 $conf_t,
4515 $SVN::_Core::SVN_CONFIG_SECTION_AUTO_PROPS,
4516 $callback);
4517 }
4518}
4519
Eric Wonga5e0ced2006-06-12 15:23:48 -07004520sub A {
Eric Wong44320b92007-01-13 22:35:53 -08004521 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004522 my ($dir, $file) = split_path($m->{file_b});
4523 my $pbat = $self->ensure_path($dir);
4524 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4525 undef, -1);
Eric Wong44320b92007-01-13 22:35:53 -08004526 print "\tA\t$m->{file_b}\n" unless $::_q;
Brad King128de652008-07-25 11:32:37 -04004527 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004528 $self->chg_file($fbat, $m);
4529 $self->close_file($fbat,undef,$self->{pool});
4530}
4531
4532sub C {
Eric Wong44320b92007-01-13 22:35:53 -08004533 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004534 my ($dir, $file) = split_path($m->{file_b});
4535 my $pbat = $self->ensure_path($dir);
4536 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4537 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08004538 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004539 $self->chg_file($fbat, $m);
4540 $self->close_file($fbat,undef,$self->{pool});
4541}
4542
4543sub delete_entry {
4544 my ($self, $path, $pbat) = @_;
4545 my $rpath = $self->repo_path($path);
4546 my ($dir, $file) = split_path($rpath);
4547 $self->{rm}->{$dir} = 1;
4548 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
4549}
4550
4551sub R {
Eric Wong44320b92007-01-13 22:35:53 -08004552 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004553 my ($dir, $file) = split_path($m->{file_b});
4554 my $pbat = $self->ensure_path($dir);
4555 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4556 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08004557 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Paul Talacko7c4d0212008-09-06 18:50:38 -07004558 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004559 $self->chg_file($fbat, $m);
4560 $self->close_file($fbat,undef,$self->{pool});
4561
4562 ($dir, $file) = split_path($m->{file_a});
4563 $pbat = $self->ensure_path($dir);
4564 $self->delete_entry($m->{file_a}, $pbat);
4565}
4566
4567sub M {
Eric Wong44320b92007-01-13 22:35:53 -08004568 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004569 my ($dir, $file) = split_path($m->{file_b});
4570 my $pbat = $self->ensure_path($dir);
4571 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
4572 $pbat,$self->{r},$self->{pool});
Eric Wong44320b92007-01-13 22:35:53 -08004573 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004574 $self->chg_file($fbat, $m);
4575 $self->close_file($fbat,undef,$self->{pool});
4576}
4577
4578sub T { shift->M(@_) }
4579
4580sub change_file_prop {
4581 my ($self, $fbat, $pname, $pval) = @_;
4582 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
4583}
4584
Florian Weimer214a34d2008-08-31 17:45:04 +02004585sub _chg_file_get_blob ($$$$) {
4586 my ($self, $fbat, $m, $which) = @_;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004587 my $fh = $::_repository->temp_acquire("git_blob_$which");
Florian Weimer214a34d2008-08-31 17:45:04 +02004588 if ($m->{"mode_$which"} =~ /^120/) {
4589 print $fh 'link ' or croak $!;
4590 $self->change_file_prop($fbat,'svn:special','*');
4591 } elsif ($m->{mode_a} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
4592 $self->change_file_prop($fbat,'svn:special',undef);
4593 }
4594 my $blob = $m->{"sha1_$which"};
4595 return ($fh,) if ($blob =~ /^0{40}$/);
4596 my $size = $::_repository->cat_blob($blob, $fh);
4597 croak "Failed to read object $blob" if ($size < 0);
4598 $fh->flush == 0 or croak $!;
4599 seek $fh, 0, 0 or croak $!;
4600
4601 my $exp = ::md5sum($fh);
4602 seek $fh, 0, 0 or croak $!;
4603 return ($fh, $exp);
4604}
4605
Eric Wonga5e0ced2006-06-12 15:23:48 -07004606sub chg_file {
4607 my ($self, $fbat, $m) = @_;
4608 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
4609 $self->change_file_prop($fbat,'svn:executable','*');
4610 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
4611 $self->change_file_prop($fbat,'svn:executable',undef);
4612 }
Florian Weimer8598db932008-08-31 17:47:09 +02004613 my ($fh_a, $exp_a) = _chg_file_get_blob $self, $fbat, $m, 'a';
4614 my ($fh_b, $exp_b) = _chg_file_get_blob $self, $fbat, $m, 'b';
Eric Wongf7197df2006-10-14 15:48:35 -07004615 my $pool = SVN::Pool->new;
Florian Weimer8598db932008-08-31 17:47:09 +02004616 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
4617 if (-s $fh_a) {
4618 my $txstream = SVN::TxDelta::new ($fh_a, $fh_b, $pool);
Eric Wong991255c2008-08-31 19:45:07 -07004619 my $res = SVN::TxDelta::send_txstream($txstream, @$atd, $pool);
4620 if (defined $res) {
4621 die "Unexpected result from send_txstream: $res\n",
4622 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
4623 }
Florian Weimer8598db932008-08-31 17:47:09 +02004624 } else {
4625 my $got = SVN::TxDelta::send_stream($fh_b, @$atd, $pool);
4626 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
4627 if ($got ne $exp_b);
4628 }
4629 Git::temp_release($fh_b, 1);
4630 Git::temp_release($fh_a, 1);
Eric Wongf7197df2006-10-14 15:48:35 -07004631 $pool->clear;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004632}
4633
4634sub D {
Eric Wong44320b92007-01-13 22:35:53 -08004635 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004636 my ($dir, $file) = split_path($m->{file_b});
4637 my $pbat = $self->ensure_path($dir);
Eric Wong44320b92007-01-13 22:35:53 -08004638 print "\tD\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004639 $self->delete_entry($m->{file_b}, $pbat);
4640}
4641
4642sub close_edit {
4643 my ($self) = @_;
4644 my ($p,$bat) = ($self->{pool}, $self->{bat});
4645 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
Eric Wong64427542007-05-19 02:58:37 -07004646 next if $_ eq '';
Eric Wonga5e0ced2006-06-12 15:23:48 -07004647 $self->close_directory($bat->{$_}, $p);
4648 }
Eric Wong64427542007-05-19 02:58:37 -07004649 $self->close_directory($bat->{''}, $p);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004650 $self->SUPER::close_edit($p);
4651 $p->clear;
4652}
4653
4654sub abort_edit {
4655 my ($self) = @_;
4656 $self->SUPER::abort_edit($self->{pool});
Eric Wong61395352007-01-27 14:33:08 -08004657}
4658
4659sub DESTROY {
4660 my $self = shift;
4661 $self->SUPER::DESTROY(@_);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004662 $self->{pool}->clear;
4663}
4664
Eric Wong44320b92007-01-13 22:35:53 -08004665# this drives the editor
4666sub apply_diff {
Eric Wong61395352007-01-27 14:33:08 -08004667 my ($self) = @_;
4668 my $mods = $self->{mods};
Eric Wong44320b92007-01-13 22:35:53 -08004669 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
Eric Wong6e8548c2007-01-27 01:32:00 -08004670 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
Eric Wong44320b92007-01-13 22:35:53 -08004671 my $f = $m->{chg};
4672 if (defined $o{$f}) {
4673 $self->$f($m);
4674 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004675 fatal("Invalid change type: $f");
Eric Wong44320b92007-01-13 22:35:53 -08004676 }
4677 }
Eric Wong24e22aa2007-01-29 00:07:49 -08004678 $self->rmdirs if $_rmdir;
Eric Wong6e8548c2007-01-27 01:32:00 -08004679 if (@$mods == 0) {
Eric Wong44320b92007-01-13 22:35:53 -08004680 $self->abort_edit;
4681 } else {
4682 $self->close_edit;
4683 }
Eric Wong6e8548c2007-01-27 01:32:00 -08004684 return scalar @$mods;
Eric Wong44320b92007-01-13 22:35:53 -08004685}
4686
Eric Wongd81bf822007-01-10 01:22:38 -08004687package Git::SVN::Ra;
Eric Wong6af1db42007-02-14 16:04:10 -08004688use vars qw/@ISA $config_dir $_log_window_size/;
Eric Wongd81bf822007-01-10 01:22:38 -08004689use strict;
4690use warnings;
Eric Wonga51cdb02007-09-07 04:00:40 -07004691my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
Eric Wongd81bf822007-01-10 01:22:38 -08004692
4693BEGIN {
4694 # enforce temporary pool usage for some simple functions
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004695 no strict 'refs';
Eric Wongbf8a40b2009-01-25 15:35:52 -08004696 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root
4697 get_file/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004698 my $SUPER = "SUPER::$f";
4699 *$f = sub {
4700 my $self = shift;
4701 my $pool = SVN::Pool->new;
4702 my @ret = $self->$SUPER(@_,$pool);
4703 $pool->clear;
4704 wantarray ? @ret : $ret[0];
4705 };
Eric Wongd81bf822007-01-10 01:22:38 -08004706 }
Eric Wongd81bf822007-01-10 01:22:38 -08004707}
4708
Steven Walter9ff74e92007-09-28 13:24:19 -04004709sub _auth_providers () {
4710 [
4711 SVN::Client::get_simple_provider(),
4712 SVN::Client::get_ssl_server_trust_file_provider(),
4713 SVN::Client::get_simple_prompt_provider(
4714 \&Git::SVN::Prompt::simple, 2),
4715 SVN::Client::get_ssl_client_cert_file_provider(),
4716 SVN::Client::get_ssl_client_cert_prompt_provider(
4717 \&Git::SVN::Prompt::ssl_client_cert, 2),
Sebastian Noack77266e92008-02-25 15:56:28 +01004718 SVN::Client::get_ssl_client_cert_pw_file_provider(),
Steven Walter9ff74e92007-09-28 13:24:19 -04004719 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
4720 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
4721 SVN::Client::get_username_provider(),
4722 SVN::Client::get_ssl_server_trust_prompt_provider(
4723 \&Git::SVN::Prompt::ssl_server_trust),
4724 SVN::Client::get_username_prompt_provider(
4725 \&Git::SVN::Prompt::username, 2)
4726 ]
4727}
4728
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004729sub escape_uri_only {
4730 my ($uri) = @_;
4731 my @tmp;
4732 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -07004733 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004734 push @tmp, $_;
4735 }
4736 join('/', @tmp);
4737}
4738
4739sub escape_url {
4740 my ($url) = @_;
4741 if ($url =~ m#^(https?)://([^/]+)(.*)$#) {
4742 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
4743 $url = "$scheme://$domain$uri";
4744 }
4745 $url;
4746}
4747
Eric Wongd81bf822007-01-10 01:22:38 -08004748sub new {
4749 my ($class, $url) = @_;
Eric Wongf6f09872007-01-18 18:22:18 -08004750 $url =~ s!/+$!!;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004751 return $RA if ($RA && $RA->{url} eq $url);
Eric Wongf6f09872007-01-18 18:22:18 -08004752
Eric Wongd81bf822007-01-10 01:22:38 -08004753 SVN::_Core::svn_config_ensure($config_dir, undef);
Steven Walter9ff74e92007-09-28 13:24:19 -04004754 my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
Eric Wongd81bf822007-01-10 01:22:38 -08004755 my $config = SVN::Core::config_get_config($config_dir);
Eric Wong7730fbe2007-07-04 14:07:42 -07004756 $RA = undef;
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004757 my $dont_store_passwords = 1;
4758 my $conf_t = ${$config}{'config'};
4759 {
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004760 no warnings 'once';
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004761 # The usage of $SVN::_Core::SVN_CONFIG_* variables
4762 # produces warnings that variables are used only once.
4763 # I had not found the better way to shut them up, so
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004764 # the warnings of type 'once' are disabled in this block.
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004765 if (SVN::_Core::svn_config_get_bool($conf_t,
4766 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4767 $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
4768 1) == 0) {
4769 SVN::_Core::svn_auth_set_parameter($baton,
4770 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
4771 bless (\$dont_store_passwords, "_p_void"));
4772 }
4773 if (SVN::_Core::svn_config_get_bool($conf_t,
4774 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4775 $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
4776 1) == 0) {
4777 $Git::SVN::Prompt::_no_auth_cache = 1;
4778 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004779 } # no warnings 'once'
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004780 my $self = SVN::Ra->new(url => escape_url($url), auth => $baton,
Eric Wongd81bf822007-01-10 01:22:38 -08004781 config => $config,
4782 pool => SVN::Pool->new,
4783 auth_provider_callbacks => $callbacks);
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004784 $self->{url} = $url;
Eric Wongd81bf822007-01-10 01:22:38 -08004785 $self->{svn_path} = $url;
4786 $self->{repos_root} = $self->get_repos_root;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08004787 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
Eric Wong0dc03d62007-05-13 01:04:43 -07004788 $self->{cache} = { check_path => { r => 0, data => {} },
4789 get_dir => { r => 0, data => {} } };
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004790 $RA = bless $self, $class;
Eric Wongd81bf822007-01-10 01:22:38 -08004791}
4792
Eric Wong0dc03d62007-05-13 01:04:43 -07004793sub check_path {
4794 my ($self, $path, $r) = @_;
4795 my $cache = $self->{cache}->{check_path};
4796 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
4797 return $cache->{data}->{$path};
4798 }
4799 my $pool = SVN::Pool->new;
4800 my $t = $self->SUPER::check_path($path, $r, $pool);
4801 $pool->clear;
4802 if ($r != $cache->{r}) {
4803 %{$cache->{data}} = ();
4804 $cache->{r} = $r;
4805 }
4806 $cache->{data}->{$path} = $t;
4807}
4808
4809sub get_dir {
4810 my ($self, $dir, $r) = @_;
4811 my $cache = $self->{cache}->{get_dir};
4812 if ($r == $cache->{r}) {
4813 if (my $x = $cache->{data}->{$dir}) {
4814 return wantarray ? @$x : $x->[0];
4815 }
4816 }
4817 my $pool = SVN::Pool->new;
4818 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
4819 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
4820 $pool->clear;
4821 if ($r != $cache->{r}) {
4822 %{$cache->{data}} = ();
4823 $cache->{r} = $r;
4824 }
4825 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
4826 wantarray ? (\%dirents, $r, $props) : \%dirents;
4827}
4828
Eric Wongd81bf822007-01-10 01:22:38 -08004829sub DESTROY {
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004830 # do not call the real DESTROY since we store ourselves in $RA
Eric Wongd81bf822007-01-10 01:22:38 -08004831}
4832
Eric Wong1ef626b2009-01-17 22:11:44 -08004833# get_log(paths, start, end, limit,
4834# discover_changed_paths, strict_node_history, receiver)
Eric Wongd81bf822007-01-10 01:22:38 -08004835sub get_log {
4836 my ($self, @args) = @_;
4837 my $pool = SVN::Pool->new;
Eric Wong1ef626b2009-01-17 22:11:44 -08004838
Mattias Nissler3c49a032009-07-07 01:39:52 +02004839 # svn_log_changed_path_t objects passed to get_log are likely to be
4840 # overwritten even if only the refs are copied to an external variable,
4841 # so we should dup the structures in their entirety. Using an
4842 # externally passed pool (instead of our temporary and quickly cleared
4843 # pool in Git::SVN::Ra) does not help matters at all...
4844 my $receiver = pop @args;
Mattias Nissler0b2af452009-07-07 01:40:02 +02004845 my $prefix = "/".$self->{svn_path};
4846 $prefix =~ s#/+($)##;
4847 my $prefix_regex = qr#^\Q$prefix\E#;
Mattias Nissler3c49a032009-07-07 01:39:52 +02004848 push(@args, sub {
4849 my ($paths) = $_[0];
4850 return &$receiver(@_) unless $paths;
4851 $_[0] = ();
4852 foreach my $p (keys %$paths) {
4853 my $i = $paths->{$p};
Mattias Nissler0b2af452009-07-07 01:40:02 +02004854 # Make path relative to our url, not repos_root
4855 $p =~ s/$prefix_regex//;
4856 my %s = map { $_ => $i->$_; }
4857 qw/copyfrom_path copyfrom_rev action/;
4858 if ($s{'copyfrom_path'}) {
4859 $s{'copyfrom_path'} =~ s/$prefix_regex//;
4860 }
Mattias Nissler3c49a032009-07-07 01:39:52 +02004861 $_[0]{$p} = \%s;
4862 }
4863 &$receiver(@_);
4864 });
4865
4866
Eric Wong1ef626b2009-01-17 22:11:44 -08004867 # the limit parameter was not supported in SVN 1.1.x, so we
4868 # drop it. Therefore, the receiver callback passed to it
4869 # is made aware of this limitation by being wrapped if
4870 # the limit passed to is being wrapped.
4871 if ($SVN::Core::VERSION le '1.2.0') {
4872 my $limit = splice(@args, 3, 1);
4873 if ($limit > 0) {
4874 my $receiver = pop @args;
4875 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
4876 }
4877 }
Eric Wongd81bf822007-01-10 01:22:38 -08004878 my $ret = $self->SUPER::get_log(@args, $pool);
4879 $pool->clear;
4880 $ret;
4881}
4882
Steven Walter9ff74e92007-09-28 13:24:19 -04004883sub trees_match {
4884 my ($self, $url1, $rev1, $url2, $rev2) = @_;
4885 my $ctx = SVN::Client->new(auth => _auth_providers);
4886 my $out = IO::File->new_tmpfile;
4887
4888 # older SVN (1.1.x) doesn't take $pool as the last parameter for
4889 # $ctx->diff(), so we'll create a default one
4890 my $pool = SVN::Pool->new_default_sub;
4891
4892 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
4893 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
4894 $out->flush;
4895 my $ret = (($out->stat)[7] == 0);
4896 close $out or croak $!;
4897
4898 $ret;
4899}
4900
Eric Wongd81bf822007-01-10 01:22:38 -08004901sub get_commit_editor {
Eric Wong44320b92007-01-13 22:35:53 -08004902 my ($self, $log, $cb, $pool) = @_;
Eric Wongd81bf822007-01-10 01:22:38 -08004903 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
Eric Wong44320b92007-01-13 22:35:53 -08004904 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004905}
4906
Eric Wongd81bf822007-01-10 01:22:38 -08004907sub gs_do_update {
Eric Wong8a603772007-01-31 02:45:50 -08004908 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
4909 my $new = ($rev_a == $rev_b);
4910 my $path = $gs->{path};
4911
Eric Wong2e5e2482007-02-23 02:21:59 -08004912 if ($new && -e $gs->{index}) {
4913 unlink $gs->{index} or die
4914 "Couldn't unlink index: $gs->{index}: $!\n";
4915 }
Eric Wongd81bf822007-01-10 01:22:38 -08004916 my $pool = SVN::Pool->new;
Eric Wong8b8fc062007-01-22 11:44:57 -08004917 $editor->set_path_strip($path);
Eric Wong2b27f6c2007-01-28 04:59:05 -08004918 my (@pc) = split m#/#, $path;
4919 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
Eric Wong8a603772007-01-31 02:45:50 -08004920 1, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004921 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong2b27f6c2007-01-28 04:59:05 -08004922
4923 # Since we can't rely on svn_ra_reparent being available, we'll
4924 # just have to do some magic with set_path to make it so
4925 # we only want a partial path.
4926 my $sp = '';
4927 my $final = join('/', @pc);
4928 while (@pc) {
4929 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
4930 $sp .= '/' if length $sp;
4931 $sp .= shift @pc;
4932 }
4933 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
4934
Eric Wong2b27f6c2007-01-28 04:59:05 -08004935 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
4936
Eric Wongd81bf822007-01-10 01:22:38 -08004937 $reporter->finish_report($pool);
4938 $pool->clear;
4939 $editor->{git_commit_ok};
4940}
4941
Eric Wong2b27f6c2007-01-28 04:59:05 -08004942# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
4943# svn_ra_reparent didn't work before 1.4)
Eric Wongd81bf822007-01-10 01:22:38 -08004944sub gs_do_switch {
Eric Wong8a603772007-01-31 02:45:50 -08004945 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
4946 my $path = $gs->{path};
Eric Wongd81bf822007-01-10 01:22:38 -08004947 my $pool = SVN::Pool->new;
Eric Wong2b27f6c2007-01-28 04:59:05 -08004948
4949 my $full_url = $self->{url};
4950 my $old_url = $full_url;
Eric Wong2a679c72009-07-19 22:08:45 -07004951 $full_url .= '/' . $path if length $path;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004952 my ($ra, $reparented);
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004953
Eric Wong2a679c72009-07-19 22:08:45 -07004954 if ($old_url =~ m#^svn(\+ssh)?://# ||
4955 ($full_url =~ m#^https?://# &&
4956 escape_url($full_url) ne $full_url)) {
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004957 $_[0] = undef;
4958 $self = undef;
4959 $RA = undef;
4960 $ra = Git::SVN::Ra->new($full_url);
4961 $ra_invalid = 1;
4962 } elsif ($old_url ne $full_url) {
4963 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, $pool);
4964 $self->{url} = $full_url;
4965 $reparented = 1;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004966 }
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004967
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004968 $ra ||= $self;
Eric Wongf4392df2008-09-06 20:18:18 -07004969 $url_b = escape_url($url_b);
Eric Wong8a603772007-01-31 02:45:50 -08004970 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004971 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong8b8fc062007-01-22 11:44:57 -08004972 $reporter->set_path('', $rev_a, 0, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004973 $reporter->finish_report($pool);
Eric Wong2b27f6c2007-01-28 04:59:05 -08004974
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004975 if ($reparented) {
4976 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
4977 $self->{url} = $old_url;
4978 }
Eric Wong2b27f6c2007-01-28 04:59:05 -08004979
Eric Wongd81bf822007-01-10 01:22:38 -08004980 $pool->clear;
4981 $editor->{git_commit_ok};
4982}
4983
Eric Wongb54a9012007-06-13 02:37:03 -07004984sub longest_common_path {
4985 my ($gsv, $globs) = @_;
Eric Wongd2ae1432007-02-07 11:50:16 -08004986 my %common;
Eric Wonge5181922007-02-08 12:53:57 -08004987 my $common_max = scalar @$gsv;
4988
4989 foreach my $gs (@$gsv) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004990 my @tmp = split m#/#, $gs->{path};
4991 my $p = '';
4992 foreach (@tmp) {
4993 $p .= length($p) ? "/$_" : $_;
4994 $common{$p} ||= 0;
4995 $common{$p}++;
4996 }
4997 }
Eric Wonge5181922007-02-08 12:53:57 -08004998 $globs ||= [];
4999 $common_max += scalar @$globs;
5000 foreach my $glob (@$globs) {
5001 my @tmp = split m#/#, $glob->{path}->{left};
5002 my $p = '';
5003 foreach (@tmp) {
5004 $p .= length($p) ? "/$_" : $_;
5005 $common{$p} ||= 0;
5006 $common{$p}++;
5007 }
5008 }
5009
Eric Wongd2ae1432007-02-07 11:50:16 -08005010 my $longest_path = '';
5011 foreach (sort {length $b <=> length $a} keys %common) {
Eric Wonge5181922007-02-08 12:53:57 -08005012 if ($common{$_} == $common_max) {
Eric Wongd2ae1432007-02-07 11:50:16 -08005013 $longest_path = $_;
5014 last;
5015 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08005016 }
Eric Wongb54a9012007-06-13 02:37:03 -07005017 $longest_path;
5018}
5019
5020sub gs_fetch_loop_common {
5021 my ($self, $base, $head, $gsv, $globs) = @_;
5022 return if ($base > $head);
5023 my $inc = $_log_window_size;
5024 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
5025 my $longest_path = longest_common_path($gsv, $globs);
Eric Wonga51cdb02007-09-07 04:00:40 -07005026 my $ra_url = $self->{url};
Alex Vandiverc69700f2009-05-06 16:18:52 -04005027 my $find_trailing_edge;
Eric Wong0af9c9f2007-01-27 22:28:56 -08005028 while (1) {
Eric Wongd4eff2b2007-01-30 14:04:22 -08005029 my %revs;
Eric Wongd2ae1432007-02-07 11:50:16 -08005030 my $err;
Eric Wongf7c3fc42007-01-29 18:34:55 -08005031 my $err_handler = $SVN::Error::handler;
Eric Wongd2ae1432007-02-07 11:50:16 -08005032 $SVN::Error::handler = sub {
5033 ($err) = @_;
5034 skip_unknown_revs($err);
5035 };
5036 sub _cb {
5037 my ($paths, $r, $author, $date, $log) = @_;
Mattias Nissler3c49a032009-07-07 01:39:52 +02005038 [ $paths,
Eric Wongd2ae1432007-02-07 11:50:16 -08005039 { author => $author, date => $date, log => $log } ];
5040 }
5041 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
5042 sub { $revs{$_[1]} = _cb(@_) });
Deskin Miller99366562009-02-08 19:33:18 -05005043 if ($err) {
5044 print "Checked through r$max\r";
Alex Vandiverc69700f2009-05-06 16:18:52 -04005045 } else {
5046 $find_trailing_edge = 1;
Deskin Miller99366562009-02-08 19:33:18 -05005047 }
Alex Vandiverc69700f2009-05-06 16:18:52 -04005048 if ($err and $find_trailing_edge) {
Eric Wongd2ae1432007-02-07 11:50:16 -08005049 print STDERR "Path '$longest_path' ",
5050 "was probably deleted:\n",
5051 $err->expanded_message,
5052 "\nWill attempt to follow ",
5053 "revisions r$min .. r$max ",
5054 "committed before the deletion\n";
5055 my $hi = $max;
5056 while (--$hi >= $min) {
5057 my $ok;
5058 $self->get_log([$longest_path], $min, $hi,
5059 0, 1, 1, sub {
Alex Vandiverb6c61772009-05-06 16:18:53 -04005060 $ok = $_[1];
Eric Wongd2ae1432007-02-07 11:50:16 -08005061 $revs{$_[1]} = _cb(@_) });
5062 if ($ok) {
5063 print STDERR "r$min .. r$ok OK\n";
5064 last;
5065 }
5066 }
Alex Vandiverc69700f2009-05-06 16:18:52 -04005067 $find_trailing_edge = 0;
Eric Wongd2ae1432007-02-07 11:50:16 -08005068 }
Eric Wongd4eff2b2007-01-30 14:04:22 -08005069 $SVN::Error::handler = $err_handler;
Eric Wongfbcc1732007-02-06 18:35:30 -08005070
Eric Wonge5181922007-02-08 12:53:57 -08005071 my %exists = map { $_->{path} => $_ } @$gsv;
Eric Wongd4eff2b2007-01-30 14:04:22 -08005072 foreach my $r (sort {$a <=> $b} keys %revs) {
Eric Wongfbcc1732007-02-06 18:35:30 -08005073 my ($paths, $logged) = @{$revs{$r}};
Eric Wonge5181922007-02-08 12:53:57 -08005074
5075 foreach my $gs ($self->match_globs(\%exists, $paths,
5076 $globs, $r)) {
Eric Wong060610c2007-12-08 23:27:41 -08005077 if ($gs->rev_map_max >= $r) {
Eric Wongfbcc1732007-02-06 18:35:30 -08005078 next;
5079 }
5080 next unless $gs->match_paths($paths, $r);
5081 $gs->{logged_rev_props} = $logged;
Eric Wonge8d120b2007-02-14 16:29:52 -08005082 if (my $last_commit = $gs->last_commit) {
5083 $gs->assert_index_clean($last_commit);
5084 }
Eric Wongfbcc1732007-02-06 18:35:30 -08005085 my $log_entry = $gs->do_fetch($paths, $r);
5086 if ($log_entry) {
Eric Wong0af9c9f2007-01-27 22:28:56 -08005087 $gs->do_git_commit($log_entry);
5088 }
Eric Wong321b1842008-01-02 10:10:03 -08005089 $INDEX_FILES{$gs->{index}} = 1;
Eric Wong0af9c9f2007-01-27 22:28:56 -08005090 }
Eric Wonge5181922007-02-08 12:53:57 -08005091 foreach my $g (@$globs) {
Eric Wong93f26892007-02-11 01:20:26 -08005092 my $k = "svn-remote.$g->{remote}." .
5093 "$g->{t}-maxRev";
5094 Git::SVN::tmp_config($k, $r);
Eric Wonge5181922007-02-08 12:53:57 -08005095 }
Eric Wonga51cdb02007-09-07 04:00:40 -07005096 if ($ra_invalid) {
5097 $_[0] = undef;
5098 $self = undef;
5099 $RA = undef;
5100 $self = Git::SVN::Ra->new($ra_url);
5101 $ra_invalid = undef;
5102 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08005103 }
Eric Wong9c93fee2007-01-31 17:22:31 -08005104 # pre-fill the .rev_db since it'll eventually get filled in
5105 # with '0' x40 if something new gets committed
Eric Wonge5181922007-02-08 12:53:57 -08005106 foreach my $gs (@$gsv) {
Eric Wong66ab84b2007-12-08 23:27:42 -08005107 next if $gs->rev_map_max >= $max;
5108 next if defined $gs->rev_map_get($max);
5109 $gs->rev_map_set($max, 0 x40);
Eric Wong9c93fee2007-01-31 17:22:31 -08005110 }
Eric Wongc3560e52007-02-12 16:03:32 -08005111 foreach my $g (@$globs) {
5112 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
5113 Git::SVN::tmp_config($k, $max);
5114 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08005115 last if $max >= $head;
5116 $min = $max + 1;
5117 $max += $inc;
5118 $max = $head if ($max > $head);
5119 }
Karl Hasselström94bc9142008-02-03 17:56:18 +01005120 Git::SVN::gc();
Eric Wong0af9c9f2007-01-27 22:28:56 -08005121}
5122
Marcus Griep570d35c2008-08-08 01:41:57 -07005123sub get_dir_globbed {
5124 my ($self, $left, $depth, $r) = @_;
5125
5126 my @x = eval { $self->get_dir($left, $r) };
5127 return unless scalar @x == 3;
5128 my $dirents = $x[0];
5129 my @finalents;
5130 foreach my $de (keys %$dirents) {
5131 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
5132 if ($depth > 1) {
5133 my @args = ("$left/$de", $depth - 1, $r);
5134 foreach my $dir ($self->get_dir_globbed(@args)) {
5135 push @finalents, "$de/$dir";
5136 }
5137 } else {
5138 push @finalents, $de;
5139 }
5140 }
5141 @finalents;
5142}
5143
Eric Wonge5181922007-02-08 12:53:57 -08005144sub match_globs {
5145 my ($self, $exists, $paths, $globs, $r) = @_;
Eric Wong74a81222007-02-10 13:28:50 -08005146
5147 sub get_dir_check {
5148 my ($self, $exists, $g, $r) = @_;
Marcus Griep570d35c2008-08-08 01:41:57 -07005149
5150 my @dirs = $self->get_dir_globbed($g->{path}->{left},
5151 $g->{path}->{depth},
5152 $r);
5153
5154 foreach my $de (@dirs) {
Eric Wong74a81222007-02-10 13:28:50 -08005155 my $p = $g->{path}->full_path($de);
5156 next if $exists->{$p};
5157 next if (length $g->{path}->{right} &&
5158 ($self->check_path($p, $r) !=
5159 $SVN::Node::dir));
5160 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
5161 $g->{ref}->full_path($de), 1);
5162 }
5163 }
Eric Wonge5181922007-02-08 12:53:57 -08005164 foreach my $g (@$globs) {
Eric Wong74a81222007-02-10 13:28:50 -08005165 if (my $path = $paths->{"/$g->{path}->{left}"}) {
5166 if ($path->{action} =~ /^[AR]$/) {
5167 get_dir_check($self, $exists, $g, $r);
5168 }
5169 }
Eric Wonge5181922007-02-08 12:53:57 -08005170 foreach (keys %$paths) {
Eric Wong28710f72007-02-14 13:32:21 -08005171 if (/$g->{path}->{left_regex}/ &&
5172 !/$g->{path}->{regex}/) {
Eric Wong74a81222007-02-10 13:28:50 -08005173 next if $paths->{$_}->{action} !~ /^[AR]$/;
5174 get_dir_check($self, $exists, $g, $r);
5175 }
Eric Wonge5181922007-02-08 12:53:57 -08005176 next unless /$g->{path}->{regex}/;
5177 my $p = $1;
5178 my $pathname = $g->{path}->full_path($p);
5179 next if $exists->{$pathname};
Eric Wong0c1ec5a2007-04-18 00:17:33 -07005180 next if ($self->check_path($pathname, $r) !=
5181 $SVN::Node::dir);
Eric Wonge5181922007-02-08 12:53:57 -08005182 $exists->{$pathname} = Git::SVN->init(
5183 $self->{url}, $pathname, undef,
5184 $g->{ref}->full_path($p), 1);
5185 }
5186 my $c = '';
5187 foreach (split m#/#, $g->{path}->{left}) {
5188 $c .= "/$_";
5189 next unless ($paths->{$c} &&
Eric Wong74a81222007-02-10 13:28:50 -08005190 ($paths->{$c}->{action} =~ /^[AR]$/));
5191 get_dir_check($self, $exists, $g, $r);
Eric Wonge5181922007-02-08 12:53:57 -08005192 }
5193 }
5194 values %$exists;
5195}
5196
Eric Wonge6434f82007-01-23 16:29:23 -08005197sub minimize_url {
5198 my ($self) = @_;
5199 return $self->{url} if ($self->{url} eq $self->{repos_root});
5200 my $url = $self->{repos_root};
5201 my @components = split(m!/!, $self->{svn_path});
5202 my $c = '';
5203 do {
5204 $url .= "/$c" if length $c;
Eric Wong5f8b2cb2009-07-25 13:14:16 -07005205 eval {
5206 my $ra = (ref $self)->new($url);
5207 my $latest = $ra->get_latest_revnum;
5208 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
5209 };
Eric Wonge6434f82007-01-23 16:29:23 -08005210 } while ($@ && ($c = shift @components));
5211 $url;
5212}
5213
Eric Wongd81bf822007-01-10 01:22:38 -08005214sub can_do_switch {
5215 my $self = shift;
5216 unless (defined $can_do_switch) {
5217 my $pool = SVN::Pool->new;
5218 my $rep = eval {
5219 $self->do_switch(1, '', 0, $self->{url},
5220 SVN::Delta::Editor->new, $pool);
5221 };
5222 if ($@) {
5223 $can_do_switch = 0;
5224 } else {
5225 $rep->abort_report($pool);
5226 $can_do_switch = 1;
5227 }
5228 $pool->clear;
5229 }
5230 $can_do_switch;
5231}
5232
Eric Wong0af9c9f2007-01-27 22:28:56 -08005233sub skip_unknown_revs {
5234 my ($err) = @_;
5235 my $errno = $err->apr_err();
5236 # Maybe the branch we're tracking didn't
5237 # exist when the repo started, so it's
5238 # not an error if it doesn't, just continue
5239 #
5240 # Wonderfully consistent library, eh?
5241 # 160013 - svn:// and file://
5242 # 175002 - http(s)://
5243 # 175007 - http(s):// (this repo required authorization, too...)
5244 # More codes may be discovered later...
5245 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
Eric Wonga6a15a92007-03-30 17:54:48 -07005246 my $err_key = $err->expanded_message;
5247 # revision numbers change every time, filter them out
5248 $err_key =~ s/\d+/\0/g;
5249 $err_key = "$errno\0$err_key";
5250 unless ($ignored_err{$err_key}) {
5251 warn "W: Ignoring error from SVN, path probably ",
5252 "does not exist: ($errno): ",
5253 $err->expanded_message,"\n";
Eric Wongeee8a172008-01-07 02:40:40 -08005254 warn "W: Do not be alarmed at the above message ",
5255 "git-svn is just searching aggressively for ",
5256 "old history.\n",
5257 "This may take a while on large repositories\n";
Eric Wonga6a15a92007-03-30 17:54:48 -07005258 $ignored_err{$err_key} = 1;
5259 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08005260 return;
5261 }
5262 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
5263}
5264
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005265package Git::SVN::Log;
5266use strict;
5267use warnings;
5268use POSIX qw/strftime/;
Ben Waltone8717842009-02-24 14:44:49 -05005269use Time::Local;
David D Kilzer111947e2007-11-11 22:56:52 -08005270use constant commit_log_separator => ('-' x 72) . "\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005271use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
5272 %rusers $show_commit $incremental/;
5273my $l_fmt;
5274
5275sub cmt_showable {
5276 my ($c) = @_;
5277 return 1 if defined $c->{r};
Eric Wongc16d0872007-04-08 00:59:22 -07005278
5279 # big commit message got truncated by the 16k pretty buffer in rev-list
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005280 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
5281 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
Eric Wongc16d0872007-04-08 00:59:22 -07005282 @{$c->{l}} = ();
Eric Wong44320b92007-01-13 22:35:53 -08005283 my @log = command(qw/cat-file commit/, $c->{c});
Eric Wongc16d0872007-04-08 00:59:22 -07005284
5285 # shift off the headers
5286 shift @log while ($log[0] ne '');
Eric Wong44320b92007-01-13 22:35:53 -08005287 shift @log;
Eric Wongc16d0872007-04-08 00:59:22 -07005288
5289 # TODO: make $c->{l} not have a trailing newline in the future
5290 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005291
5292 (undef, $c->{r}, undef) = ::extract_metadata(
Eric Wong44320b92007-01-13 22:35:53 -08005293 (grep(/^git-svn-id: /, @log))[-1]);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005294 }
5295 return defined $c->{r};
5296}
5297
5298sub log_use_color {
Jeff Kingcd459e32007-12-11 01:28:42 -05005299 return $color || Git->repository->get_colorbool('color.diff');
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005300}
5301
5302sub git_svn_log_cmd {
Eric Wong3bc718b2007-02-13 17:09:40 -08005303 my ($r_min, $r_max, @args) = @_;
5304 my $head = 'HEAD';
Eric Wong6ed77262007-08-15 09:55:18 -07005305 my (@files, @log_opts);
Eric Wong3bc718b2007-02-13 17:09:40 -08005306 foreach my $x (@args) {
Eric Wong6ed77262007-08-15 09:55:18 -07005307 if ($x eq '--' || @files) {
5308 push @files, $x;
5309 } else {
5310 if (::verify_ref("$x^0")) {
5311 $head = $x;
5312 } else {
5313 push @log_opts, $x;
5314 }
5315 }
Eric Wong3bc718b2007-02-13 17:09:40 -08005316 }
5317
Eric Wong13c823f2007-04-08 00:59:19 -07005318 my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
5319 $gs ||= Git::SVN->_new;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005320 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
5321 $gs->refname);
5322 push @cmd, '-r' unless $non_recursive;
5323 push @cmd, qw/--raw --name-status/ if $verbose;
5324 push @cmd, '--color' if log_use_color();
Eric Wong6ed77262007-08-15 09:55:18 -07005325 push @cmd, @log_opts;
5326 if (defined $r_max && $r_max == $r_min) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005327 push @cmd, '--max-count=1';
Eric Wong060610c2007-12-08 23:27:41 -08005328 if (my $c = $gs->rev_map_get($r_max)) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005329 push @cmd, $c;
5330 }
Eric Wong6ed77262007-08-15 09:55:18 -07005331 } elsif (defined $r_max) {
David D Kilzer111947e2007-11-11 22:56:52 -08005332 if ($r_max < $r_min) {
5333 ($r_min, $r_max) = ($r_max, $r_min);
5334 }
5335 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
5336 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
5337 # If there are no commits in the range, both $c_max and $c_min
5338 # will be undefined. If there is at least 1 commit in the
5339 # range, both will be defined.
5340 return () if !defined $c_min || !defined $c_max;
5341 if ($c_min eq $c_max) {
5342 push @cmd, '--max-count=1', $c_min;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005343 } else {
David D Kilzer111947e2007-11-11 22:56:52 -08005344 push @cmd, '--boundary', "$c_min..$c_max";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005345 }
5346 }
Eric Wong6ed77262007-08-15 09:55:18 -07005347 return (@cmd, @files);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005348}
5349
5350# adapted from pager.c
5351sub config_pager {
Jonathan Niederdec543e2009-10-30 20:43:19 -05005352 chomp(my $pager = command_oneline(qw(var GIT_PAGER)));
5353 if ($pager eq 'cat') {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005354 $pager = undef;
5355 }
Jeff Kingcd459e32007-12-11 01:28:42 -05005356 $ENV{GIT_PAGER_IN_USE} = defined($pager);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005357}
5358
5359sub run_pager {
Eric Wongb0193042007-09-21 18:48:45 -07005360 return unless -t *STDOUT && defined $pager;
Marcus Griep971e6282008-09-10 11:09:46 -04005361 pipe my ($rfd, $wfd) or return;
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005362 defined(my $pid = fork) or ::fatal "Can't fork: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005363 if (!$pid) {
5364 open STDOUT, '>&', $wfd or
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005365 ::fatal "Can't redirect to stdout: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005366 return;
5367 }
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005368 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005369 $ENV{LESS} ||= 'FRSX';
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005370 exec $pager or ::fatal "Can't run pager: $! ($pager)";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005371}
5372
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005373sub format_svn_date {
Ben Waltone8717842009-02-24 14:44:49 -05005374 # some systmes don't handle or mishandle %z, so be creative.
Ben Walton736e6192009-02-27 22:11:45 -05005375 my $t = shift || time;
Ben Waltone8717842009-02-24 14:44:49 -05005376 my $gm = timelocal(gmtime($t));
5377 my $sign = qw( + + - )[ $t <=> $gm ];
5378 my $gmoff = sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
5379 return strftime("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005380}
5381
5382sub parse_git_date {
5383 my ($t, $tz) = @_;
5384 # Date::Parse isn't in the standard Perl distro :(
5385 if ($tz =~ s/^\+//) {
5386 $t += tz_to_s_offset($tz);
5387 } elsif ($tz =~ s/^\-//) {
5388 $t -= tz_to_s_offset($tz);
5389 }
5390 return $t;
5391}
5392
5393sub set_local_timezone {
5394 if (defined $TZ) {
5395 $ENV{TZ} = $TZ;
5396 } else {
5397 delete $ENV{TZ};
5398 }
5399}
5400
Eric Wong21819a32007-01-27 14:38:10 -08005401sub tz_to_s_offset {
5402 my ($tz) = @_;
5403 $tz =~ s/(\d\d)$//;
5404 return ($1 * 60) + ($tz * 3600);
5405}
5406
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005407sub get_author_info {
5408 my ($dest, $author, $t, $tz) = @_;
5409 $author =~ s/(?:^\s*|\s*$)//g;
5410 $dest->{a_raw} = $author;
5411 my $au;
Eric Wong1c8443b2007-01-14 02:17:00 -08005412 if ($::_authors) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005413 $au = $rusers{$author} || undef;
5414 }
5415 if (!$au) {
5416 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
5417 }
5418 $dest->{t} = $t;
5419 $dest->{tz} = $tz;
5420 $dest->{a} = $au;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005421 $dest->{t_utc} = parse_git_date($t, $tz);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005422}
5423
5424sub process_commit {
5425 my ($c, $r_min, $r_max, $defer) = @_;
5426 if (defined $r_min && defined $r_max) {
5427 if ($r_min == $c->{r} && $r_min == $r_max) {
5428 show_commit($c);
5429 return 0;
5430 }
5431 return 1 if $r_min == $r_max;
5432 if ($r_min < $r_max) {
5433 # we need to reverse the print order
5434 return 0 if (defined $limit && --$limit < 0);
5435 push @$defer, $c;
5436 return 1;
5437 }
5438 if ($r_min != $r_max) {
5439 return 1 if ($r_min < $c->{r});
5440 return 1 if ($r_max > $c->{r});
5441 }
5442 }
5443 return 0 if (defined $limit && --$limit < 0);
5444 show_commit($c);
5445 return 1;
5446}
5447
5448sub show_commit {
5449 my $c = shift;
5450 if ($oneline) {
5451 my $x = "\n";
5452 if (my $l = $c->{l}) {
5453 while ($l->[0] =~ /^\s*$/) { shift @$l }
5454 $x = $l->[0];
5455 }
5456 $l_fmt ||= 'A' . length($c->{r});
5457 print 'r',pack($l_fmt, $c->{r}),' | ';
5458 print "$c->{c} | " if $show_commit;
5459 print $x;
5460 } else {
5461 show_commit_normal($c);
5462 }
5463}
5464
5465sub show_commit_changed_paths {
5466 my ($c) = @_;
5467 return unless $c->{changed};
5468 print "Changed paths:\n", @{$c->{changed}};
5469}
5470
5471sub show_commit_normal {
5472 my ($c) = @_;
David D Kilzer111947e2007-11-11 22:56:52 -08005473 print commit_log_separator, "r$c->{r} | ";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005474 print "$c->{c} | " if $show_commit;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005475 print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005476 my $nr_line = 0;
5477
5478 if (my $l = $c->{l}) {
5479 while ($l->[$#$l] eq "\n" && $#$l > 0
5480 && $l->[($#$l - 1)] eq "\n") {
5481 pop @$l;
5482 }
5483 $nr_line = scalar @$l;
5484 if (!$nr_line) {
5485 print "1 line\n\n\n";
5486 } else {
5487 if ($nr_line == 1) {
5488 $nr_line = '1 line';
5489 } else {
5490 $nr_line .= ' lines';
5491 }
5492 print $nr_line, "\n";
5493 show_commit_changed_paths($c);
5494 print "\n";
5495 print $_ foreach @$l;
5496 }
5497 } else {
5498 print "1 line\n";
5499 show_commit_changed_paths($c);
5500 print "\n";
5501
5502 }
Eric Wong488a63e2007-02-15 00:40:42 -08005503 foreach my $x (qw/raw stat diff/) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005504 if ($c->{$x}) {
5505 print "\n";
5506 print $_ foreach @{$c->{$x}}
5507 }
5508 }
5509}
5510
5511sub cmd_show_log {
5512 my (@args) = @_;
5513 my ($r_min, $r_max);
5514 my $r_last = -1; # prevent dupes
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005515 set_local_timezone();
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005516 if (defined $::_revision) {
5517 if ($::_revision =~ /^(\d+):(\d+)$/) {
5518 ($r_min, $r_max) = ($1, $2);
5519 } elsif ($::_revision =~ /^\d+$/) {
5520 $r_min = $r_max = $::_revision;
5521 } else {
5522 ::fatal "-r$::_revision is not supported, use ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005523 "standard 'git log' arguments instead";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005524 }
5525 }
5526
5527 config_pager();
Eric Wong6ed77262007-08-15 09:55:18 -07005528 @args = git_svn_log_cmd($r_min, $r_max, @args);
David D Kilzer111947e2007-11-11 22:56:52 -08005529 if (!@args) {
5530 print commit_log_separator unless $incremental || $oneline;
5531 return;
5532 }
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005533 my $log = command_output_pipe(@args);
5534 run_pager();
Eric Wong488a63e2007-02-15 00:40:42 -08005535 my (@k, $c, $d, $stat);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005536 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
5537 while (<$log>) {
David D Kilzer60f3ff12007-11-10 22:10:34 -08005538 if (/^${esc_color}commit -?($::sha1_short)/o) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005539 my $cmt = $1;
5540 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
5541 $r_last = $c->{r};
5542 process_commit($c, $r_min, $r_max, \@k) or
5543 goto out;
5544 }
5545 $d = undef;
5546 $c = { c => $cmt };
5547 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
5548 get_author_info($c, $1, $2, $3);
5549 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
5550 # ignore
5551 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
5552 push @{$c->{raw}}, $_;
5553 } elsif (/^${esc_color}[ACRMDT]\t/) {
5554 # we could add $SVN->{svn_path} here, but that requires
5555 # remote access at the moment (repo_path_split)...
5556 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
5557 push @{$c->{changed}}, $_;
5558 } elsif (/^${esc_color}diff /o) {
5559 $d = 1;
5560 push @{$c->{diff}}, $_;
5561 } elsif ($d) {
5562 push @{$c->{diff}}, $_;
Eric Wong488a63e2007-02-15 00:40:42 -08005563 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
5564 $esc_color*[\+\-]*$esc_color$/x) {
5565 $stat = 1;
5566 push @{$c->{stat}}, $_;
5567 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
5568 push @{$c->{stat}}, $_;
5569 $stat = undef;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005570 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
5571 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
5572 } elsif (s/^${esc_color} //o) {
5573 push @{$c->{l}}, $_;
5574 }
5575 }
5576 if ($c && defined $c->{r} && $c->{r} != $r_last) {
5577 $r_last = $c->{r};
5578 process_commit($c, $r_min, $r_max, \@k);
5579 }
5580 if (@k) {
David D Kilzer111947e2007-11-11 22:56:52 -08005581 ($r_min, $r_max) = ($r_max, $r_min);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005582 process_commit($_, $r_min, $r_max) foreach reverse @k;
5583 }
5584out:
Eric Wongc843c462007-01-12 03:07:31 -08005585 close $log;
David D Kilzer111947e2007-11-11 22:56:52 -08005586 print commit_log_separator unless $incremental || $oneline;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005587}
5588
Tim Stoakes6fb53752008-02-10 15:21:08 +10305589sub cmd_blame {
Steven Grimm4be40382008-05-10 22:11:18 -07005590 my $path = pop;
Tim Stoakes6fb53752008-02-10 15:21:08 +10305591
5592 config_pager();
5593 run_pager();
5594
Steven Grimm4be40382008-05-10 22:11:18 -07005595 my ($fh, $ctx, $rev);
5596
5597 if ($_git_format) {
5598 ($fh, $ctx) = command_output_pipe('blame', @_, $path);
5599 while (my $line = <$fh>) {
5600 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
5601 # Uncommitted edits show up as a rev ID of
5602 # all zeros, which we can't look up with
5603 # cmt_metadata
5604 if ($1 !~ /^0+$/) {
5605 (undef, $rev, undef) =
5606 ::cmt_metadata($1);
5607 $rev = '0' if (!$rev);
5608 } else {
5609 $rev = '0';
5610 }
5611 $rev = sprintf('%-10s', $rev);
5612 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
5613 }
5614 print $line;
Tim Stoakes6fb53752008-02-10 15:21:08 +10305615 }
Steven Grimm4be40382008-05-10 22:11:18 -07005616 } else {
5617 ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD',
5618 '--', $path);
5619 my ($sha1);
5620 my %authors;
Boris Byk6ea42032009-04-11 00:32:41 +04005621 my @buffer;
5622 my %dsha; #distinct sha keys
5623
Steven Grimm4be40382008-05-10 22:11:18 -07005624 while (my $line = <$fh>) {
Boris Byk6ea42032009-04-11 00:32:41 +04005625 push @buffer, $line;
Steven Grimm4be40382008-05-10 22:11:18 -07005626 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
Boris Byk6ea42032009-04-11 00:32:41 +04005627 $dsha{$1} = 1;
5628 }
5629 }
5630
5631 my $s2r = ::cmt_sha2rev_batch([keys %dsha]);
5632
5633 foreach my $line (@buffer) {
5634 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
5635 $rev = $s2r->{$1};
5636 $rev = '0' if (!$rev)
Steven Grimm4be40382008-05-10 22:11:18 -07005637 }
5638 elsif ($line =~ /^author (.*)/) {
5639 $authors{$rev} = $1;
5640 $authors{$rev} =~ s/\s/_/g;
5641 }
5642 elsif ($line =~ /^\t(.*)$/) {
5643 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
5644 }
5645 }
Tim Stoakes6fb53752008-02-10 15:21:08 +10305646 }
5647 command_close_pipe($fh, $ctx);
5648}
5649
Eric Wong706587f2007-01-18 17:50:01 -08005650package Git::SVN::Migration;
5651# these version numbers do NOT correspond to actual version numbers
5652# of git nor git-svn. They are just relative.
5653#
5654# v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
5655#
5656# v1 layout: .git/$id/info/url, refs/remotes/$id
5657#
5658# v2 layout: .git/svn/$id/info/url, refs/remotes/$id
5659#
5660# v3 layout: .git/svn/$id, refs/remotes/$id
5661# - info/url may remain for backwards compatibility
5662# - this is what we migrate up to this layout automatically,
5663# - this will be used by git svn init on single branches
Eric Wong26a62d52007-02-12 13:25:25 -08005664# v3.1 layout (auto migrated):
5665# - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
5666# for backwards compatibility
Eric Wong706587f2007-01-18 17:50:01 -08005667#
5668# v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
5669# - this is only created for newly multi-init-ed
5670# repositories. Similar in spirit to the
5671# --use-separate-remotes option in git-clone (now default)
5672# - we do not automatically migrate to this (following
5673# the example set by core git)
Eric Wong060610c2007-12-08 23:27:41 -08005674#
5675# v5 layout: .rev_db.$UUID => .rev_map.$UUID
5676# - newer, more-efficient format that uses 24-bytes per record
5677# with no filler space.
5678# - use xxd -c24 < .rev_map.$UUID to view and debug
5679# - This is a one-way migration, repositories updated to the
5680# new format will not be able to use old git-svn without
5681# rebuilding the .rev_db. Rebuilding the rev_db is not
5682# possible if noMetadata or useSvmProps are set; but should
5683# be no problem for users that use the (sensible) defaults.
Eric Wong706587f2007-01-18 17:50:01 -08005684use strict;
5685use warnings;
5686use Carp qw/croak/;
5687use File::Path qw/mkpath/;
Eric Wong47e39c52007-01-21 04:27:09 -08005688use File::Basename qw/dirname basename/;
5689use vars qw/$_minimize/;
Eric Wong706587f2007-01-18 17:50:01 -08005690
5691sub migrate_from_v0 {
5692 my $git_dir = $ENV{GIT_DIR};
5693 return undef unless -d $git_dir;
5694 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5695 my $migrated = 0;
5696 while (<$fh>) {
5697 chomp;
5698 my ($id, $orig_ref) = ($_, $_);
5699 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
5700 next unless -f "$git_dir/$id/info/url";
5701 my $new_ref = "refs/remotes/$id";
5702 if (::verify_ref("$new_ref^0")) {
5703 print STDERR "W: $orig_ref is probably an old ",
5704 "branch used by an ancient version of ",
5705 "git-svn.\n",
5706 "However, $new_ref also exists.\n",
5707 "We will not be able ",
5708 "to use this branch until this ",
5709 "ambiguity is resolved.\n";
5710 next;
5711 }
5712 print STDERR "Migrating from v0 layout...\n" if !$migrated;
5713 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
5714 command_noisy('update-ref', $new_ref, $orig_ref);
5715 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
5716 $migrated++;
5717 }
5718 command_close_pipe($fh, $ctx);
5719 print STDERR "Done migrating from v0 layout...\n" if $migrated;
5720 $migrated;
5721}
5722
5723sub migrate_from_v1 {
5724 my $git_dir = $ENV{GIT_DIR};
5725 my $migrated = 0;
5726 return $migrated unless -d $git_dir;
5727 my $svn_dir = "$git_dir/svn";
5728
5729 # just in case somebody used 'svn' as their $id at some point...
5730 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
5731
5732 print STDERR "Migrating from a git-svn v1 layout...\n";
5733 mkpath([$svn_dir]);
5734 print STDERR "Data from a previous version of git-svn exists, but\n\t",
5735 "$svn_dir\n\t(required for this version ",
Frederik Schwarzer8f510be2008-07-14 18:30:24 +02005736 "($::VERSION) of git-svn) does not exist.\n";
Eric Wong706587f2007-01-18 17:50:01 -08005737 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5738 while (<$fh>) {
5739 my $x = $_;
5740 next unless $x =~ s#^refs/remotes/##;
5741 chomp $x;
5742 next unless -f "$git_dir/$x/info/url";
5743 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
5744 next unless $u;
5745 my $dn = dirname("$git_dir/svn/$x");
5746 mkpath([$dn]) unless -d $dn;
5747 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
5748 mkpath(["$git_dir/svn/svn"]);
5749 print STDERR " - $git_dir/$x/info => ",
5750 "$git_dir/svn/$x/info\n";
5751 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
5752 croak "$!: $x";
5753 # don't worry too much about these, they probably
5754 # don't exist with repos this old (save for index,
5755 # and we can easily regenerate that)
5756 foreach my $f (qw/unhandled.log index .rev_db/) {
5757 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
5758 }
5759 } else {
5760 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
5761 rename "$git_dir/$x", "$git_dir/svn/$x" or
5762 croak "$!: $x";
5763 }
5764 $migrated++;
5765 }
5766 command_close_pipe($fh, $ctx);
5767 print STDERR "Done migrating from a git-svn v1 layout\n";
5768 $migrated;
5769}
5770
5771sub read_old_urls {
5772 my ($l_map, $pfx, $path) = @_;
5773 my @dir;
5774 foreach (<$path/*>) {
5775 if (-r "$_/info/url") {
5776 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
5777 my $ref_id = $pfx . basename $_;
5778 my $url = ::file_to_s("$_/info/url");
5779 $l_map->{$ref_id} = $url;
5780 } elsif (-d $_) {
5781 push @dir, $_;
5782 }
5783 }
5784 foreach (@dir) {
5785 my $x = $_;
5786 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
5787 read_old_urls($l_map, $x, $_);
5788 }
5789}
5790
5791sub migrate_from_v2 {
5792 my @cfg = command(qw/config -l/);
5793 return if grep /^svn-remote\..+\.url=/, @cfg;
5794 my %l_map;
5795 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
5796 my $migrated = 0;
5797
5798 foreach my $ref_id (sort keys %l_map) {
Eric Wong471bc002007-02-01 04:06:27 -08005799 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
5800 if ($@) {
5801 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
5802 }
Eric Wong706587f2007-01-18 17:50:01 -08005803 $migrated++;
5804 }
5805 $migrated;
5806}
5807
Eric Wong47e39c52007-01-21 04:27:09 -08005808sub minimize_connections {
5809 my $r = Git::SVN::read_all_remotes();
5810 my $new_urls = {};
5811 my $root_repos = {};
5812 foreach my $repo_id (keys %$r) {
5813 my $url = $r->{$repo_id}->{url} or next;
5814 my $fetch = $r->{$repo_id}->{fetch} or next;
5815 my $ra = Git::SVN::Ra->new($url);
5816
5817 # skip existing cases where we already connect to the root
5818 if (($ra->{url} eq $ra->{repos_root}) ||
Eric Wong7829f202008-06-28 20:40:32 -07005819 ($ra->{repos_root} eq $repo_id)) {
Eric Wong47e39c52007-01-21 04:27:09 -08005820 $root_repos->{$ra->{url}} = $repo_id;
5821 next;
5822 }
5823
5824 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
5825 my $root_path = $ra->{url};
Eric Wong4e9f6cc2007-02-09 12:17:57 -08005826 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
Eric Wong47e39c52007-01-21 04:27:09 -08005827 foreach my $path (keys %$fetch) {
5828 my $ref_id = $fetch->{$path};
5829 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
5830
5831 # make sure we can read when connecting to
5832 # a higher level of a repository
5833 my ($last_rev, undef) = $gs->last_rev_commit;
5834 if (!defined $last_rev) {
5835 $last_rev = eval {
5836 $root_ra->get_latest_revnum;
5837 };
5838 next if $@;
5839 }
5840 my $new = $root_path;
5841 $new .= length $path ? "/$path" : '';
5842 eval {
5843 $root_ra->get_log([$new], $last_rev, $last_rev,
5844 0, 0, 1, sub { });
5845 };
5846 next if $@;
5847 $new_urls->{$ra->{repos_root}}->{$new} =
5848 { ref_id => $ref_id,
5849 old_repo_id => $repo_id,
5850 old_path => $path };
5851 }
5852 }
5853
5854 my @emptied;
5855 foreach my $url (keys %$new_urls) {
5856 # see if we can re-use an existing [svn-remote "repo_id"]
5857 # instead of creating a(n ugly) new section:
Eric Wong7829f202008-06-28 20:40:32 -07005858 my $repo_id = $root_repos->{$url} || $url;
Eric Wong47e39c52007-01-21 04:27:09 -08005859
5860 my $fetch = $new_urls->{$url};
5861 foreach my $path (keys %$fetch) {
5862 my $x = $fetch->{$path};
5863 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
5864 my $pfx = "svn-remote.$x->{old_repo_id}";
5865
5866 my $old_fetch = quotemeta("$x->{old_path}:".
Adam Brewster6f5748e2009-08-11 23:14:27 -04005867 "$x->{ref_id}");
Eric Wong8b8fc062007-01-22 11:44:57 -08005868 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08005869 "$pfx.fetch", '^'. $old_fetch . '$');
5870 delete $r->{$x->{old_repo_id}}->
5871 {fetch}->{$x->{old_path}};
5872 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
Eric Wong8b8fc062007-01-22 11:44:57 -08005873 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08005874 "$pfx.url");
5875 push @emptied, $x->{old_repo_id}
5876 }
5877 }
5878 }
5879 if (@emptied) {
Johannes Schindelin8befc502008-12-14 23:10:52 +01005880 my $file = $ENV{GIT_CONFIG} || "$ENV{GIT_DIR}/config";
Eric Wong47e39c52007-01-21 04:27:09 -08005881 print STDERR <<EOF;
5882The following [svn-remote] sections in your config file ($file) are empty
5883and can be safely removed:
5884EOF
5885 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
5886 }
5887}
5888
Eric Wong706587f2007-01-18 17:50:01 -08005889sub migration_check {
5890 migrate_from_v0();
5891 migrate_from_v1();
5892 migrate_from_v2();
Eric Wong47e39c52007-01-21 04:27:09 -08005893 minimize_connections() if $_minimize;
Eric Wong706587f2007-01-18 17:50:01 -08005894}
5895
Eric Wongef3cfaa2007-01-24 03:30:57 -08005896package Git::IndexInfo;
5897use strict;
5898use warnings;
5899use Git qw/command_input_pipe command_close_pipe/;
5900
5901sub new {
5902 my ($class) = @_;
5903 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
5904 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
5905}
5906
5907sub remove {
5908 my ($self, $path) = @_;
5909 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
5910 return ++$self->{nr};
5911 }
5912 undef;
5913}
5914
5915sub update {
5916 my ($self, $mode, $hash, $path) = @_;
5917 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
5918 return ++$self->{nr};
5919 }
5920 undef;
5921}
5922
5923sub DESTROY {
5924 my ($self) = @_;
5925 command_close_pipe($self->{gui}, $self->{ctx});
5926}
5927
Eric Wong4bb9ed02007-02-03 13:29:17 -08005928package Git::SVN::GlobSpec;
5929use strict;
5930use warnings;
5931
5932sub new {
5933 my ($class, $glob) = @_;
Eric Wong4bb9ed02007-02-03 13:29:17 -08005934 my $re = $glob;
5935 $re =~ s!/+$!!g; # no need for trailing slashes
Adam Brewster6f5748e2009-08-11 23:14:27 -04005936 $re =~ m!^([^*]*)(\*(?:/\*)*)(.*)$!;
Marcus Griep570d35c2008-08-08 01:41:57 -07005937 my $temp = $re;
5938 my ($left, $right) = ($1, $3);
5939 $re = $2;
5940 my $depth = $re =~ tr/*/*/;
5941 if ($depth != $temp =~ tr/*/*/) {
5942 die "Only one set of wildcard directories " .
5943 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
5944 }
5945 if ($depth == 0) {
Eric Wonge5181922007-02-08 12:53:57 -08005946 die "One '*' is needed for glob: '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08005947 }
Marcus Griep570d35c2008-08-08 01:41:57 -07005948 $re =~ s!\*!\[^/\]*!g;
5949 $re = quotemeta($left) . "($re)" . quotemeta($right);
Eric Wong4e9f6cc2007-02-09 12:17:57 -08005950 if (length $left && !($left =~ s!/+$!!g)) {
5951 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
5952 }
5953 if (length $right && !($right =~ s!^/+!!g)) {
5954 die "Missing leading '/' on right side of: '$glob' ($right)\n";
5955 }
Eric Wong74a81222007-02-10 13:28:50 -08005956 my $left_re = qr/^\/\Q$left\E(\/|$)/;
5957 bless { left => $left, right => $right, left_regex => $left_re,
Marcus Griep570d35c2008-08-08 01:41:57 -07005958 regex => qr/$re/, glob => $glob, depth => $depth }, $class;
Eric Wong4bb9ed02007-02-03 13:29:17 -08005959}
5960
5961sub full_path {
5962 my ($self, $path) = @_;
5963 return (length $self->{left} ? "$self->{left}/" : '') .
5964 $path . (length $self->{right} ? "/$self->{right}" : '');
5965}
5966
Eric Wong3397f9d2006-02-16 01:24:16 -08005967__END__
5968
5969Data structures:
5970
Eric Wong4bb9ed02007-02-03 13:29:17 -08005971
5972$remotes = { # returned by read_all_remotes()
5973 'svn' => {
5974 # svn-remote.svn.url=https://svn.musicpd.org
5975 url => 'https://svn.musicpd.org',
5976 # svn-remote.svn.fetch=mpd/trunk:trunk
5977 fetch => {
5978 'mpd/trunk' => 'trunk',
5979 },
5980 # svn-remote.svn.tags=mpd/tags/*:tags/*
5981 tags => {
5982 path => {
5983 left => 'mpd/tags',
5984 right => '',
5985 regex => qr!mpd/tags/([^/]+)$!,
5986 glob => 'tags/*',
5987 },
5988 ref => {
5989 left => 'tags',
5990 right => '',
5991 regex => qr!tags/([^/]+)$!,
5992 glob => 'tags/*',
5993 },
5994 }
5995 }
5996};
5997
Eric Wong44320b92007-01-13 22:35:53 -08005998$log_entry hashref as returned by libsvn_log_entry()
Eric Wong3397f9d2006-02-16 01:24:16 -08005999{
Eric Wong44320b92007-01-13 22:35:53 -08006000 log => 'whitespace-formatted log entry
Eric Wong3397f9d2006-02-16 01:24:16 -08006001', # trailing newline is preserved
6002 revision => '8', # integer
6003 date => '2004-02-24T17:01:44.108345Z', # commit date
6004 author => 'committer name'
6005};
6006
Eric Wong6e8548c2007-01-27 01:32:00 -08006007
6008# this is generated by generate_diff();
Eric Wong3397f9d2006-02-16 01:24:16 -08006009@mods = array of diff-index line hashes, each element represents one line
6010 of diff-index output
6011
6012diff-index line ($m hash)
6013{
6014 mode_a => first column of diff-index output, no leading ':',
6015 mode_b => second column of diff-index output,
6016 sha1_b => sha1sum of the final blob,
Eric Wongac8e0b92006-03-03 01:20:07 -08006017 chg => change type [MCRADT],
Eric Wong3397f9d2006-02-16 01:24:16 -08006018 file_a => original file name of a file (iff chg is 'C' or 'R')
6019 file_b => new/current file name of a file (any chg)
6020}
6021;
Eric Wonga5e0ced2006-06-12 15:23:48 -07006022
Eric Wonga00439a2006-06-27 19:39:13 -07006023# retval of read_url_paths{,_all}();
6024$l_map = {
6025 # repository root url
6026 'https://svn.musicpd.org' => {
6027 # repository path # GIT_SVN_ID
6028 'mpd/trunk' => 'trunk',
6029 'mpd/tags/0.11.5' => 'tags/0.11.5',
6030 },
6031}
6032
Eric Wonga5e0ced2006-06-12 15:23:48 -07006033Notes:
6034 I don't trust the each() function on unless I created %hash myself
6035 because the internal iterator may not have started at base.