blob: 6c42e2afca8f0fdc699fe36b866050f20e8a7f1a [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 Wong13ccd6d2006-03-29 22:37:18 -080022
Eric Wongf8c9d1d2007-01-12 02:35:20 -080023$Git::SVN::Log::TZ = $ENV{TZ};
Eric Wong3397f9d2006-02-16 01:24:16 -080024$ENV{TZ} = 'UTC';
Eric Wonga00439a2006-06-27 19:39:13 -070025$| = 1; # unbuffer STDOUT
Eric Wong3397f9d2006-02-16 01:24:16 -080026
Benoit Sigoure207f1a72007-10-16 16:36:52 +020027sub fatal (@) { print STDERR "@_\n"; exit 1 }
Eric Wongb9c85182006-12-15 23:58:07 -080028require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
29require SVN::Ra;
30require SVN::Delta;
31if ($SVN::Core::VERSION lt '1.1.0') {
Benoit Sigoure207f1a72007-10-16 16:36:52 +020032 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
Eric Wongb9c85182006-12-15 23:58:07 -080033}
Eric Wongd81bf822007-01-10 01:22:38 -080034push @Git::SVN::Ra::ISA, 'SVN::Ra';
Eric Wongb9c85182006-12-15 23:58:07 -080035push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
36push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
Eric Wong3397f9d2006-02-16 01:24:16 -080037use Carp qw/croak/;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -080038use Digest::MD5;
Eric Wong3397f9d2006-02-16 01:24:16 -080039use IO::File qw//;
40use File::Basename qw/dirname basename/;
41use File::Path qw/mkpath/;
Mark Lodato36db1ed2009-05-14 21:27:15 -040042use File::Spec;
Eric Wong512b6202007-04-03 01:57:08 -070043use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
Eric Wong968bdf12006-06-15 13:36:12 -070044use IPC::Open3;
Eric Wong336f1712007-01-11 02:14:43 -080045use Git;
Eric Wonga5e0ced2006-06-12 15:23:48 -070046
Eric Wong336f1712007-01-11 02:14:43 -080047BEGIN {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120048 # import functions from Git into our packages, en masse
49 no strict 'refs';
Eric Wong336f1712007-01-11 02:14:43 -080050 foreach (qw/command command_oneline command_noisy command_output_pipe
Boris Byk6ea42032009-04-11 00:32:41 +040051 command_input_pipe command_close_pipe
52 command_bidi_pipe command_close_bidi_pipe/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120053 for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -080054 Git::SVN::Migration Git::SVN::Log Git::SVN),
Sam Vilainc5f71ad2007-06-15 15:43:59 +120055 __PACKAGE__) {
56 *{"${package}::$_"} = \&{"Git::$_"};
57 }
Eric Wong336f1712007-01-11 02:14:43 -080058 }
Eric Wong336f1712007-01-11 02:14:43 -080059}
60
Eric Wongb9c85182006-12-15 23:58:07 -080061my ($SVN);
Eric Wong83e99402006-10-11 18:19:55 -070062
Eric Wongf8c9d1d2007-01-12 02:35:20 -080063$sha1 = qr/[a-f\d]{40}/;
64$sha1_short = qr/[a-f\d]{4,40}/;
Eric Wong44320b92007-01-13 22:35:53 -080065my ($_stdin, $_help, $_edit,
Marc Branchaud62244062009-06-23 13:02:08 -040066 $_message, $_file, $_branch_dest,
Eric Wongd05d72e2007-01-15 22:59:26 -080067 $_template, $_shared,
Jason Merrillc2abd832009-04-06 16:37:59 -040068 $_version, $_fetch_all, $_no_rebase, $_fetch_parent,
Eric Wongdee41f32007-03-13 11:40:36 -070069 $_merge, $_strategy, $_dry_run, $_local,
Steven Grimm4be40382008-05-10 22:11:18 -070070 $_prefix, $_no_checkout, $_url, $_verbose,
Florian Ragwitz5de70ef2008-10-04 19:35:17 -070071 $_git_format, $_commit_url, $_tag);
Eric Wong0bed5ea2007-02-09 02:45:03 -080072$Git::SVN::_follow_parent = 1;
Simon Arlott49750f32009-03-30 19:31:41 +010073$_q ||= 0;
Eric Wong706587f2007-01-18 17:50:01 -080074my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
75 'config-dir=s' => \$Git::SVN::Ra::config_dir,
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +020076 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
77 'ignore-paths=s' => \$SVN::Git::Fetcher::_ignore_regex );
Eric Wong0bed5ea2007-02-09 02:45:03 -080078my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
Eric Wongdc5869c2006-05-24 02:07:32 -070079 'authors-file|A=s' => \$_authors,
Mark Lodato36db1ed2009-05-14 21:27:15 -040080 'authors-prog=s' => \$_authors_prog,
Eric Wongecc712d2007-01-31 12:28:10 -080081 'repack:i' => \$Git::SVN::_repack,
Eric Wong97ae0912007-02-11 15:21:24 -080082 'noMetadata' => \$Git::SVN::_no_metadata,
83 'useSvmProps' => \$Git::SVN::_use_svm_props,
Eric Wong62e349d2007-02-16 19:57:29 -080084 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props,
Eric Wong6af1db42007-02-14 16:04:10 -080085 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size,
Eric Wong1e889ef2007-02-16 01:45:13 -080086 'no-checkout' => \$_no_checkout,
Simon Arlott49750f32009-03-30 19:31:41 +010087 'quiet|q+' => \$_q,
Eric Wongecc712d2007-01-31 12:28:10 -080088 'repack-flags|repack-args|repack-opts=s' =>
89 \$Git::SVN::_repack_flags,
Andy Whitcroft70ae04e2007-11-22 13:44:42 +000090 'use-log-author' => \$Git::SVN::_use_log_author,
Avery Pennarun6aa9ba12008-04-15 21:04:17 -040091 'add-author-from' => \$Git::SVN::_add_author_from,
Pete Harlane82f0d72009-01-17 20:10:14 -080092 'localtime' => \$Git::SVN::_localtime,
Eric Wong706587f2007-01-18 17:50:01 -080093 %remote_opts );
Eric Wong36f5b1f2006-05-23 19:23:41 -070094
Marc Branchaud62244062009-06-23 13:02:08 -040095my ($_trunk, @_tags, @_branches, $_stdlayout);
Eric Wong0dfaf0a2007-02-18 02:34:09 -080096my %icv;
Eric Wongdadc6d22007-02-14 12:27:41 -080097my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared,
Marc Branchaud62244062009-06-23 13:02:08 -040098 'trunk|T=s' => \$_trunk, 'tags|t=s@' => \@_tags,
99 'branches|b=s@' => \@_branches, 'prefix=s' => \$_prefix,
martin f. krafft8f728fb2007-07-14 11:25:28 +0200100 'stdlayout|s' => \$_stdlayout,
Eric Wong4a1bb4c2007-05-13 09:58:14 -0700101 'minimize-url|m' => \$Git::SVN::_minimize_url,
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800102 'no-metadata' => sub { $icv{noMetadata} = 1 },
103 'use-svm-props' => sub { $icv{useSvmProps} = 1 },
104 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 },
105 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] },
Eric Wongdadc6d22007-02-14 12:27:41 -0800106 %remote_opts );
Eric Wong27e9fb82006-06-27 19:39:12 -0700107my %cmt_opts = ( 'edit|e' => \$_edit,
Eric Wong24e22aa2007-01-29 00:07:49 -0800108 'rmdir' => \$SVN::Git::Editor::_rmdir,
109 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder,
110 'l=i' => \$SVN::Git::Editor::_rename_limit,
111 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity
Eric Wong27e9fb82006-06-27 19:39:12 -0700112);
Eric Wong9d55b412006-06-12 15:53:13 -0700113
Eric Wong3397f9d2006-02-16 01:24:16 -0800114my %cmd = (
Eric Wong2a3240b2007-01-04 18:09:56 -0800115 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
Eric Wonge98671e2007-02-14 02:21:19 -0800116 { 'revision|r=s' => \$_revision,
Eric Wong905f8b72007-02-16 03:22:40 -0800117 'fetch-all|all' => \$_fetch_all,
Jason Merrillc2abd832009-04-06 16:37:59 -0400118 'parent|p' => \$_fetch_parent,
Eric Wonge98671e2007-02-14 02:21:19 -0800119 %fc_opts } ],
Eric Wong0425ea92007-02-16 18:45:01 -0800120 clone => [ \&cmd_clone, "Initialize and fetch revisions",
121 { 'revision|r=s' => \$_revision,
122 %fc_opts, %init_opts } ],
Eric Wongd2866f92007-01-11 12:26:16 -0800123 init => [ \&cmd_init, "Initialize a repo for tracking" .
Eric Wongf8ab6b72006-05-31 15:49:56 -0700124 " (requires URL argument)",
Eric Wong9d55b412006-06-12 15:53:13 -0700125 \%init_opts ],
Eric Wongdadc6d22007-02-14 12:27:41 -0800126 'multi-init' => [ \&cmd_multi_init,
127 "Deprecated alias for ".
128 "'$0 init -T<trunk> -b<branches> -t<tags>'",
129 \%init_opts ],
Eric Wongd7ad3be2007-01-14 03:14:28 -0800130 dcommit => [ \&cmd_dcommit,
131 'Commit several diffs to merge with upstream',
Eric Wong3289e862006-12-15 23:58:08 -0800132 { 'merge|m|M' => \$_merge,
133 'strategy|s=s' => \$_strategy,
Eric Wong905f8b72007-02-16 03:22:40 -0800134 'verbose|v' => \$_verbose,
Eric Wong3289e862006-12-15 23:58:08 -0800135 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800136 'fetch-all|all' => \$_fetch_all,
Eric Wongba24e742008-08-07 02:06:16 -0700137 'commit-url=s' => \$_commit_url,
138 'revision|r=i' => \$_revision,
Karl Hasselström171af112007-05-03 07:51:35 +0200139 'no-rebase' => \$_no_rebase,
Eric Wong4b155222006-12-22 21:59:24 -0800140 %cmt_opts, %fc_opts } ],
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700141 branch => [ \&cmd_branch,
142 'Create a branch in the SVN repository',
143 { 'message|m=s' => \$_message,
Marc Branchaud62244062009-06-23 13:02:08 -0400144 'destination|d=s' => \$_branch_dest,
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700145 'dry-run|n' => \$_dry_run,
146 'tag|t' => \$_tag } ],
147 tag => [ sub { $_tag = 1; cmd_branch(@_) },
148 'Create a tag in the SVN repository',
149 { 'message|m=s' => \$_message,
Marc Branchaud62244062009-06-23 13:02:08 -0400150 'destination|d=s' => \$_branch_dest,
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700151 'dry-run|n' => \$_dry_run } ],
Eric Wong1ce255d2007-01-14 23:21:16 -0800152 'set-tree' => [ \&cmd_set_tree,
153 "Set an SVN repository to a git tree-ish",
Robin H. Johnsone84dc6d2009-05-05 11:16:14 -0700154 { 'stdin' => \$_stdin, %cmt_opts, %fc_opts, } ],
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200155 'create-ignore' => [ \&cmd_create_ignore,
156 'Create a .gitignore per svn:ignore',
157 { 'revision|r=i' => \$_revision
158 } ],
Benoit Sigoure15153452007-10-16 16:36:50 +0200159 'propget' => [ \&cmd_propget,
160 'Print the value of a property on a file or directory',
161 { 'revision|r=i' => \$_revision } ],
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200162 'proplist' => [ \&cmd_proplist,
163 'List all properties of a file or directory',
164 { 'revision|r=i' => \$_revision } ],
Eric Wong5969cbe2007-01-11 17:58:39 -0800165 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200166 { 'revision|r=i' => \$_revision
Lars Hjemli05b4df32007-09-05 11:35:29 +0200167 } ],
Vineet Kumar2d879792007-11-19 14:56:15 -0800168 'show-externals' => [ \&cmd_show_externals, "Show svn:externals listings",
169 { 'revision|r=i' => \$_revision
170 } ],
Eric Wong1c8443b2007-01-14 02:17:00 -0800171 'multi-fetch' => [ \&cmd_multi_fetch,
Eric Wonge98671e2007-02-14 02:21:19 -0800172 "Deprecated alias for $0 fetch --all",
173 { 'revision|r=s' => \$_revision, %fc_opts } ],
Eric Wong706587f2007-01-18 17:50:01 -0800174 'migrate' => [ sub { },
175 # no-op, we automatically run this anyways,
Eric Wong706587f2007-01-18 17:50:01 -0800176 'Migrate configuration/metadata/layout from
177 previous versions of git-svn',
Eric Wonga836a0e2007-02-14 19:34:56 -0800178 { 'minimize' => \$Git::SVN::Migration::_minimize,
179 %remote_opts } ],
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800180 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
181 { 'limit=i' => \$Git::SVN::Log::limit,
Eric Wong79bb8d82006-06-01 02:35:44 -0700182 'revision|r=s' => \$_revision,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800183 'verbose|v' => \$Git::SVN::Log::verbose,
184 'incremental' => \$Git::SVN::Log::incremental,
185 'oneline' => \$Git::SVN::Log::oneline,
186 'show-commit' => \$Git::SVN::Log::show_commit,
187 'non-recursive' => \$Git::SVN::Log::non_recursive,
Eric Wong79bb8d82006-06-01 02:35:44 -0700188 'authors-file|A=s' => \$_authors,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800189 'color' => \$Git::SVN::Log::color,
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200190 'pager=s' => \$Git::SVN::Log::pager
Eric Wong79bb8d82006-06-01 02:35:44 -0700191 } ],
Eric Wong222566e2008-08-08 01:41:58 -0700192 'find-rev' => [ \&cmd_find_rev,
193 "Translate between SVN revision numbers and tree-ish",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200194 {} ],
Eric Wong905f8b72007-02-16 03:22:40 -0800195 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
196 { 'merge|m|M' => \$_merge,
197 'verbose|v' => \$_verbose,
198 'strategy|s=s' => \$_strategy,
Eric Wongdee41f32007-03-13 11:40:36 -0700199 'local|l' => \$_local,
Eric Wong905f8b72007-02-16 03:22:40 -0800200 'fetch-all|all' => \$_fetch_all,
Seth Falcon7d45e142008-05-19 20:29:17 -0700201 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800202 %fc_opts } ],
Eric Wong44320b92007-01-13 22:35:53 -0800203 'commit-diff' => [ \&cmd_commit_diff,
204 'Commit a diff between two trees',
Eric Wong27e9fb82006-06-27 19:39:12 -0700205 { 'message|m=s' => \$_message,
206 'file|F=s' => \$_file,
Eric Wong45bf4732006-11-09 01:19:37 -0800207 'revision|r=s' => \$_revision,
Eric Wong27e9fb82006-06-27 19:39:12 -0700208 %cmt_opts } ],
David D. Kilzere6fefa92007-11-21 11:57:18 -0800209 'info' => [ \&cmd_info,
210 "Show info about the latest SVN revision
211 on the current branch",
David D. Kilzer8b014d72007-11-21 11:57:19 -0800212 { 'url' => \$_url, } ],
Tim Stoakes6fb53752008-02-10 15:21:08 +1030213 'blame' => [ \&Git::SVN::Log::cmd_blame,
214 "Show what revision and author last modified each line of a file",
Steven Grimm4be40382008-05-10 22:11:18 -0700215 { 'git-format' => \$_git_format } ],
Ben Jackson195643f2009-06-03 20:45:52 -0700216 'reset' => [ \&cmd_reset,
217 "Undo fetches back to the specified SVN revision",
218 { 'revision|r=s' => \$_revision,
219 'parent|p' => \$_fetch_parent } ],
Eric Wong3397f9d2006-02-16 01:24:16 -0800220);
Eric Wong9d55b412006-06-12 15:53:13 -0700221
Eric Wong3397f9d2006-02-16 01:24:16 -0800222my $cmd;
223for (my $i = 0; $i < @ARGV; $i++) {
224 if (defined $cmd{$ARGV[$i]}) {
225 $cmd = $ARGV[$i];
226 splice @ARGV, $i, 1;
227 last;
Ben Jackson9a8c92a2009-05-30 18:17:06 -0700228 } elsif ($ARGV[$i] eq 'help') {
229 $cmd = $ARGV[$i+1];
230 usage(0);
Eric Wong3397f9d2006-02-16 01:24:16 -0800231 }
232};
233
Eric Wong540424b2007-12-19 00:31:43 -0800234# make sure we're always running at the top-level working directory
235unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
Eric Wong5253dc32007-02-20 01:36:30 -0800236 unless (-d $ENV{GIT_DIR}) {
237 if ($git_dir_user_set) {
238 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
239 "but it is not a directory\n";
240 }
241 my $git_dir = delete $ENV{GIT_DIR};
Deskin Millerfe4003f2008-11-06 00:07:39 -0500242 my $cdup = undef;
243 git_cmd_try {
244 $cdup = command_oneline(qw/rev-parse --show-cdup/);
245 $git_dir = '.' unless ($cdup);
246 chomp $cdup if ($cdup);
247 $cdup = "." unless ($cdup && length $cdup);
248 } "Already at toplevel, but $git_dir not found\n";
Eric Wong5253dc32007-02-20 01:36:30 -0800249 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
250 unless (-d $git_dir) {
251 die "$git_dir still not found after going to ",
252 "'$cdup'\n";
253 }
254 $ENV{GIT_DIR} = $git_dir;
255 }
Adam Robenffe256f2008-05-23 16:19:41 +0200256 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wong5253dc32007-02-20 01:36:30 -0800257}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100258
259my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
260
261read_repo_config(\%opts);
Eric Wong222566e2008-08-08 01:41:58 -0700262if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
263 Getopt::Long::Configure('pass_through');
264}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100265my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
266 'minimize-connections' => \$Git::SVN::Migration::_minimize,
267 'id|i=s' => \$Git::SVN::default_ref_id,
268 'svn-remote|remote|R=s' => sub {
269 $Git::SVN::no_reuse_existing = 1;
270 $Git::SVN::default_repo_id = $_[1] });
271exit 1 if (!$rv && $cmd && $cmd ne 'log');
272
273usage(0) if $_help;
274version() if $_version;
275usage(1) unless defined $cmd;
276load_authors() if $_authors;
Mark Lodato36db1ed2009-05-14 21:27:15 -0400277if (defined $_authors_prog) {
278 $_authors_prog = "'" . File::Spec->rel2abs($_authors_prog) . "'";
279}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100280
Eric Wong0425ea92007-02-16 18:45:01 -0800281unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
Eric Wong706587f2007-01-18 17:50:01 -0800282 Git::SVN::Migration::migration_check();
283}
Eric Wongecc712d2007-01-31 12:28:10 -0800284Git::SVN::init_vars();
Eric Wongb805b442007-01-22 13:52:04 -0800285eval {
286 Git::SVN::verify_remotes_sanity();
287 $cmd{$cmd}->[0]->(@ARGV);
288};
289fatal $@ if $@;
Eric Wong1e889ef2007-02-16 01:45:13 -0800290post_fetch_checkout();
Eric Wong3397f9d2006-02-16 01:24:16 -0800291exit 0;
292
293####################### primary functions ######################
294sub usage {
295 my $exit = shift || 0;
296 my $fd = $exit ? \*STDERR : \*STDOUT;
297 print $fd <<"";
298git-svn - bidirectional operations between a single Subversion tree and git
Stephan Beyer1b1dd232008-07-13 15:36:15 +0200299Usage: git svn <command> [options] [arguments]\n
Eric Wong448c81b2006-03-03 01:20:09 -0800300
301 print $fd "Available commands:\n" unless $cmd;
Eric Wong3397f9d2006-02-16 01:24:16 -0800302
303 foreach (sort keys %cmd) {
Eric Wong448c81b2006-03-03 01:20:09 -0800304 next if $cmd && $cmd ne $_;
Eric Wonga836a0e2007-02-14 19:34:56 -0800305 next if /^multi-/; # don't show deprecated commands
Eric Wongb203b762006-10-11 14:53:36 -0700306 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
Benoit Sigoureaa807bc2007-11-03 19:53:34 +0100307 foreach (sort keys %{$cmd{$_}->[2]}) {
Eric Wong512b6202007-04-03 01:57:08 -0700308 # mixed-case options are for .git/config only
309 next if /[A-Z]/ && /^[a-z]+$/i;
Eric Wong448c81b2006-03-03 01:20:09 -0800310 # prints out arguments as they should be passed:
Eric Wongb8c92ca2006-05-24 01:40:37 -0700311 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
Eric Wongb203b762006-10-11 14:53:36 -0700312 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
Eric Wong448c81b2006-03-03 01:20:09 -0800313 "--$_" : "-$_" }
314 split /\|/,$_)," $x\n";
315 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800316 }
317 print $fd <<"";
Eric Wong448c81b2006-03-03 01:20:09 -0800318\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
319arbitrary identifier if you're tracking multiple SVN branches/repositories in
320one git repository and want to keep them separate. See git-svn(1) for more
321information.
Eric Wong3397f9d2006-02-16 01:24:16 -0800322
323 exit $exit;
324}
325
Eric Wong551ce282006-02-20 10:57:29 -0800326sub version {
Eric Wong7d60ab22006-12-28 01:16:20 -0800327 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
Eric Wong551ce282006-02-20 10:57:29 -0800328 exit 0;
329}
330
Eric Wong8164b652007-01-11 15:35:55 -0800331sub do_git_init_db {
332 unless (-d $ENV{GIT_DIR}) {
333 my @init_db = ('init');
334 push @init_db, "--template=$_template" if defined $_template;
Eric Wongdadc6d22007-02-14 12:27:41 -0800335 if (defined $_shared) {
336 if ($_shared =~ /[a-z]/) {
337 push @init_db, "--shared=$_shared";
338 } else {
339 push @init_db, "--shared";
340 }
341 }
Eric Wong8164b652007-01-11 15:35:55 -0800342 command_noisy(@init_db);
Adam Robenffe256f2008-05-23 16:19:41 +0200343 $_repository = Git->repository(Repository => ".git");
Eric Wong8164b652007-01-11 15:35:55 -0800344 }
Johannes Schindelind3c96342009-04-09 13:29:57 +0200345 command_noisy('config', 'core.autocrlf', 'false');
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800346 my $set;
347 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
348 foreach my $i (keys %icv) {
349 die "'$set' and '$i' cannot both be set\n" if $set;
350 next unless defined $icv{$i};
351 command_noisy('config', "$pfx.$i", $icv{$i});
352 $set = $i;
353 }
Ben Jackson88ec2052009-04-11 10:46:18 -0700354 my $ignore_regex = \$SVN::Git::Fetcher::_ignore_regex;
355 command_noisy('config', "$pfx.ignore-paths", $$ignore_regex)
356 if defined $$ignore_regex;
Eric Wong8164b652007-01-11 15:35:55 -0800357}
358
Eric Wongdadc6d22007-02-14 12:27:41 -0800359sub init_subdir {
360 my $repo_path = shift or return;
361 mkpath([$repo_path]) unless -d $repo_path;
362 chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
Eric Wongf30603f2007-02-23 01:26:26 -0800363 $ENV{GIT_DIR} = '.git';
Adam Robenffe256f2008-05-23 16:19:41 +0200364 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wongdadc6d22007-02-14 12:27:41 -0800365}
366
Eric Wong0425ea92007-02-16 18:45:01 -0800367sub cmd_clone {
368 my ($url, $path) = @_;
369 if (!defined $path &&
Marc Branchaud62244062009-06-23 13:02:08 -0400370 (defined $_trunk || @_branches || @_tags ||
martin f. krafft8f728fb2007-07-14 11:25:28 +0200371 defined $_stdlayout) &&
Eric Wong0425ea92007-02-16 18:45:01 -0800372 $url !~ m#^[a-z\+]+://#) {
373 $path = $url;
374 }
Eric Wong0425ea92007-02-16 18:45:01 -0800375 $path = basename($url) if !defined $path || !length $path;
Eric Wongf30603f2007-02-23 01:26:26 -0800376 cmd_init($url, $path);
Eric Wong0425ea92007-02-16 18:45:01 -0800377 Git::SVN::fetch_all($Git::SVN::default_repo_id);
Alex Vandiver42a5da12009-05-06 16:19:45 -0400378 command_oneline('config', 'svn.authorsfile', $_authors) if $_authors;
Eric Wong0425ea92007-02-16 18:45:01 -0800379}
380
Eric Wongd2866f92007-01-11 12:26:16 -0800381sub cmd_init {
martin f. krafft8f728fb2007-07-14 11:25:28 +0200382 if (defined $_stdlayout) {
383 $_trunk = 'trunk' if (!defined $_trunk);
Marc Branchaud62244062009-06-23 13:02:08 -0400384 @_tags = 'tags' if (! @_tags);
385 @_branches = 'branches' if (! @_branches);
martin f. krafft8f728fb2007-07-14 11:25:28 +0200386 }
Marc Branchaud62244062009-06-23 13:02:08 -0400387 if (defined $_trunk || @_branches || @_tags) {
Eric Wongdadc6d22007-02-14 12:27:41 -0800388 return cmd_multi_init(@_);
Eric Wong03e0ea82006-06-30 21:42:53 -0700389 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800390 my $url = shift or die "SVN repository location required ",
391 "as a command-line argument\n";
392 init_subdir(@_);
Eric Wong8164b652007-01-11 15:35:55 -0800393 do_git_init_db();
Eric Wong03e0ea82006-06-30 21:42:53 -0700394
Eric Wong706587f2007-01-18 17:50:01 -0800395 Git::SVN->init($url);
Eric Wong3397f9d2006-02-16 01:24:16 -0800396}
397
Eric Wong2a3240b2007-01-04 18:09:56 -0800398sub cmd_fetch {
Eric Wonge98671e2007-02-14 02:21:19 -0800399 if (grep /^\d+=./, @_) {
400 die "'<rev>=<commit>' fetch arguments are ",
401 "no longer supported.\n";
Eric Wong07a1c952007-01-22 15:47:41 -0800402 }
Eric Wonge98671e2007-02-14 02:21:19 -0800403 my ($remote) = @_;
404 if (@_ > 1) {
Jason Merrillc2abd832009-04-06 16:37:59 -0400405 die "Usage: $0 fetch [--all] [--parent] [svn-remote]\n";
Eric Wonge98671e2007-02-14 02:21:19 -0800406 }
Jason Merrillc2abd832009-04-06 16:37:59 -0400407 if ($_fetch_parent) {
408 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
409 unless ($gs) {
410 die "Unable to determine upstream SVN information from ",
411 "working tree history\n";
412 }
413 # just fetch, don't checkout.
414 $_no_checkout = 'true';
415 $_fetch_all ? $gs->fetch_all : $gs->fetch;
416 } elsif ($_fetch_all) {
Eric Wonge98671e2007-02-14 02:21:19 -0800417 cmd_multi_fetch();
418 } else {
Jason Merrillc2abd832009-04-06 16:37:59 -0400419 $remote ||= $Git::SVN::default_repo_id;
Eric Wonge98671e2007-02-14 02:21:19 -0800420 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
Eric Wong1c8443b2007-01-14 02:17:00 -0800421 }
Eric Wong2a3240b2007-01-04 18:09:56 -0800422}
423
Eric Wong1ce255d2007-01-14 23:21:16 -0800424sub cmd_set_tree {
Eric Wong3397f9d2006-02-16 01:24:16 -0800425 my (@commits) = @_;
426 if ($_stdin || !@commits) {
427 print "Reading from stdin...\n";
428 @commits = ();
429 while (<STDIN>) {
Eric Wong1ca72ae2006-03-03 01:20:09 -0800430 if (/\b($sha1_short)\b/o) {
Eric Wong3397f9d2006-02-16 01:24:16 -0800431 unshift @commits, $1;
432 }
433 }
434 }
435 my @revs;
Eric Wong8de010a2006-02-20 10:57:26 -0800436 foreach my $c (@commits) {
Eric Wongaef4e922006-12-15 10:59:54 -0800437 my @tmp = command('rev-parse',$c);
Eric Wong8de010a2006-02-20 10:57:26 -0800438 if (scalar @tmp == 1) {
439 push @revs, $tmp[0];
440 } elsif (scalar @tmp > 1) {
Eric Wongaef4e922006-12-15 10:59:54 -0800441 push @revs, reverse(command('rev-list',@tmp));
Eric Wong8de010a2006-02-20 10:57:26 -0800442 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200443 fatal "Failed to rev-parse $c";
Eric Wong8de010a2006-02-20 10:57:26 -0800444 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800445 }
Eric Wong1ce255d2007-01-14 23:21:16 -0800446 my $gs = Git::SVN->new;
447 my ($r_last, $cmt_last) = $gs->last_rev_commit;
448 $gs->fetch;
Eric Wong97f69872007-01-25 11:53:13 -0800449 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
Eric Wong1ce255d2007-01-14 23:21:16 -0800450 fatal "There are new revisions that were fetched ",
451 "and need to be merged (or acknowledged) ",
452 "before committing.\nlast rev: $r_last\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200453 " current: $gs->{last_rev}";
Eric Wong1ce255d2007-01-14 23:21:16 -0800454 }
455 $gs->set_tree($_) foreach @revs;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700456 print "Done committing ",scalar @revs," revisions to SVN\n";
Eric Wong3157dd92007-12-13 08:27:34 -0800457 unlink $gs->{index};
Eric Wonga5e0ced2006-06-12 15:23:48 -0700458}
459
Eric Wongd7ad3be2007-01-14 03:14:28 -0800460sub cmd_dcommit {
461 my $head = shift;
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100462 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
David Reiss826a9332007-11-13 13:47:26 -0800463 'Cannot dcommit with a dirty index. Commit your changes first, '
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100464 . "or stash them with `git stash'.\n";
Eric Wongd7ad3be2007-01-14 03:14:28 -0800465 $head ||= 'HEAD';
Thomas Rast5eec27e2009-05-29 17:09:42 +0200466
467 my $old_head;
468 if ($head ne 'HEAD') {
469 $old_head = eval {
470 command_oneline([qw/symbolic-ref -q HEAD/])
471 };
472 if ($old_head) {
473 $old_head =~ s{^refs/heads/}{};
474 } else {
475 $old_head = eval { command_oneline(qw/rev-parse HEAD/) };
476 }
477 command(['checkout', $head], STDERR => 0);
478 }
479
Eric Wonga8ae2622007-02-13 14:22:11 -0800480 my @refs;
Thomas Rast5eec27e2009-05-29 17:09:42 +0200481 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD', \@refs);
Thomas Rast2cb61102008-08-31 15:50:59 +0200482 unless ($gs) {
483 die "Unable to determine upstream SVN information from ",
484 "$head history.\nPerhaps the repository is empty.";
485 }
Peter Oberndorfer0df84052009-02-23 12:02:53 +0100486
487 if (defined $_commit_url) {
488 $url = $_commit_url;
489 } else {
490 $url = eval { command_oneline('config', '--get',
491 "svn-remote.$gs->{repo_id}.commiturl") };
492 if (!$url) {
493 $url = $gs->full_url
494 }
495 }
496
Eric Wongba24e742008-08-07 02:06:16 -0700497 my $last_rev = $_revision if defined $_revision;
Matthieu Moy59b0c242008-04-24 20:06:36 +0200498 if ($url) {
499 print "Committing to $url ...\n";
500 }
Eric Wong733a65a2007-06-13 02:23:28 -0700501 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
Eric Wong751eb392007-08-31 18:16:12 -0700502 if ($_no_rebase && scalar(@$linear_refs) > 1) {
503 warn "Attempting to commit more than one change while ",
504 "--no-rebase is enabled.\n",
505 "If these changes depend on each other, re-running ",
Eric Wong7dfa16b2008-01-02 10:09:49 -0800506 "without --no-rebase may be required."
Eric Wong751eb392007-08-31 18:16:12 -0700507 }
Eric Wong711521e2008-08-20 00:30:06 -0700508 my $expect_url = $url;
509 Git::SVN::remove_username($expect_url);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800510 while (1) {
511 my $d = shift @$linear_refs or last;
Eric Wong45bf4732006-11-09 01:19:37 -0800512 unless (defined $last_rev) {
513 (undef, $last_rev, undef) = cmt_metadata("$d~1");
514 unless (defined $last_rev) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800515 fatal "Unable to extract revision information ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200516 "from commit $d~1";
Eric Wong45bf4732006-11-09 01:19:37 -0800517 }
518 }
Eric Wongb22d4492006-08-26 00:01:23 -0700519 if ($_dry_run) {
520 print "diff-tree $d~1 $d\n";
521 } else {
Eric Wong751eb392007-08-31 18:16:12 -0700522 my $cmt_rev;
Eric Wongd7ad3be2007-01-14 03:14:28 -0800523 my %ed_opts = ( r => $last_rev,
Eric Wong61395352007-01-27 14:33:08 -0800524 log => get_commit_entry($d)->{log},
Eric Wongba24e742008-08-07 02:06:16 -0700525 ra => Git::SVN::Ra->new($url),
Konstantin V. Arkhipov3caf3202007-11-14 03:52:02 +0300526 config => SVN::Core::config_get_config(
527 $Git::SVN::Ra::config_dir
528 ),
Eric Wong61395352007-01-27 14:33:08 -0800529 tree_a => "$d~1",
530 tree_b => $d,
531 editor_cb => sub {
532 print "Committed r$_[0]\n";
Eric Wong751eb392007-08-31 18:16:12 -0700533 $cmt_rev = $_[0];
534 },
Eric Wonga8ae2622007-02-13 14:22:11 -0800535 svn_path => '');
Eric Wong61395352007-01-27 14:33:08 -0800536 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800537 print "No changes\n$d~1 == $d\n";
Eric Wong733a65a2007-06-13 02:23:28 -0700538 } elsif ($parents->{$d} && @{$parents->{$d}}) {
Eric Wong751eb392007-08-31 18:16:12 -0700539 $gs->{inject_parents_dcommit}->{$cmt_rev} =
Eric Wong733a65a2007-06-13 02:23:28 -0700540 $parents->{$d};
Eric Wongd7ad3be2007-01-14 03:14:28 -0800541 }
Eric Wong751eb392007-08-31 18:16:12 -0700542 $_fetch_all ? $gs->fetch_all : $gs->fetch;
Eric Wong7dfa16b2008-01-02 10:09:49 -0800543 $last_rev = $cmt_rev;
Eric Wong751eb392007-08-31 18:16:12 -0700544 next if $_no_rebase;
545
546 # we always want to rebase against the current HEAD,
547 # not any head that was passed to us
Eric Wongc74d9ac2007-11-05 03:21:47 -0800548 my @diff = command('diff-tree', $d,
Eric Wong751eb392007-08-31 18:16:12 -0700549 $gs->refname, '--');
550 my @finish;
551 if (@diff) {
552 @finish = rebase_cmd();
Eric Wongc74d9ac2007-11-05 03:21:47 -0800553 print STDERR "W: $d and ", $gs->refname,
Eric Wong751eb392007-08-31 18:16:12 -0700554 " differ, using @finish:\n",
Eric Wongc74d9ac2007-11-05 03:21:47 -0800555 join("\n", @diff), "\n";
Eric Wong751eb392007-08-31 18:16:12 -0700556 } else {
557 print "No changes between current HEAD and ",
558 $gs->refname,
559 "\nResetting to the latest ",
560 $gs->refname, "\n";
561 @finish = qw/reset --mixed/;
562 }
563 command_noisy(@finish, $gs->refname);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800564 if (@diff) {
565 @refs = ();
566 my ($url_, $rev_, $uuid_, $gs_) =
Thomas Rast5eec27e2009-05-29 17:09:42 +0200567 working_head_info('HEAD', \@refs);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800568 my ($linear_refs_, $parents_) =
569 linearize_history($gs_, \@refs);
570 if (scalar(@$linear_refs) !=
571 scalar(@$linear_refs_)) {
572 fatal "# of revisions changed ",
573 "\nbefore:\n",
574 join("\n", @$linear_refs),
575 "\n\nafter:\n",
576 join("\n", @$linear_refs_), "\n",
577 'If you are attempting to commit ',
578 "merges, try running:\n\t",
579 'git rebase --interactive',
580 '--preserve-merges ',
581 $gs->refname,
582 "\nBefore dcommitting";
583 }
Eric Wong711521e2008-08-20 00:30:06 -0700584 if ($url_ ne $expect_url) {
Eric Wongc74d9ac2007-11-05 03:21:47 -0800585 fatal "URL mismatch after rebase: ",
Eric Wong711521e2008-08-20 00:30:06 -0700586 "$url_ != $expect_url";
Eric Wongc74d9ac2007-11-05 03:21:47 -0800587 }
588 if ($uuid_ ne $uuid) {
589 fatal "uuid mismatch after rebase: ",
590 "$uuid_ != $uuid";
591 }
592 # remap parents
593 my (%p, @l, $i);
594 for ($i = 0; $i < scalar @$linear_refs; $i++) {
595 my $new = $linear_refs_->[$i] or next;
596 $p{$new} =
597 $parents->{$linear_refs->[$i]};
598 push @l, $new;
599 }
600 $parents = \%p;
601 $linear_refs = \@l;
602 }
Eric Wongb22d4492006-08-26 00:01:23 -0700603 }
604 }
Thomas Rast5eec27e2009-05-29 17:09:42 +0200605
606 if ($old_head) {
607 my $new_head = command_oneline(qw/rev-parse HEAD/);
608 my $new_is_symbolic = eval {
609 command_oneline(qw/symbolic-ref -q HEAD/);
610 };
611 if ($new_is_symbolic) {
612 print "dcommitted the branch ", $head, "\n";
613 } else {
614 print "dcommitted on a detached HEAD because you gave ",
615 "a revision argument.\n",
616 "The rewritten commit is: ", $new_head, "\n";
617 }
618 command(['checkout', $old_head], STDERR => 0);
619 }
620
Eric Wong3157dd92007-12-13 08:27:34 -0800621 unlink $gs->{index};
Eric Wongb22d4492006-08-26 00:01:23 -0700622}
623
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700624sub cmd_branch {
625 my ($branch_name, $head) = @_;
626
627 unless (defined $branch_name && length $branch_name) {
628 die(($_tag ? "tag" : "branch") . " name required\n");
629 }
630 $head ||= 'HEAD';
631
632 my ($src, $rev, undef, $gs) = working_head_info($head);
633
Deskin Millera0fbc872008-12-01 21:43:00 -0500634 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
Marc Branchaud62244062009-06-23 13:02:08 -0400635 my $allglobs = $remote->{ $_tag ? 'tags' : 'branches' };
636 my $glob;
637 if ($#{$allglobs} == 0) {
638 $glob = $allglobs->[0];
639 } else {
640 unless(defined $_branch_dest) {
641 die "Multiple ",
642 $_tag ? "tag" : "branch",
643 " paths defined for Subversion repository.\n",
644 "You must specify where you want to create the ",
645 $_tag ? "tag" : "branch",
646 " with the --destination argument.\n";
647 }
648 foreach my $g (@{$allglobs}) {
Eric Wongf7050592009-06-25 02:28:15 -0700649 # SVN::Git::Editor could probably be moved to Git.pm..
650 my $re = SVN::Git::Editor::glob2pat($g->{path}->{left});
651 if ($_branch_dest =~ /$re/) {
Marc Branchaud62244062009-06-23 13:02:08 -0400652 $glob = $g;
653 last;
654 }
655 }
656 unless (defined $glob) {
657 die "Unknown ",
658 $_tag ? "tag" : "branch",
659 " destination $_branch_dest\n";
660 }
661 }
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700662 my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
663 my $dst = join '/', $remote->{url}, $lft, $branch_name, ($rgt || ());
664
665 my $ctx = SVN::Client->new(
666 auth => Git::SVN::Ra::_auth_providers(),
667 log_msg => sub {
668 ${ $_[0] } = defined $_message
669 ? $_message
670 : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
671 . $branch_name;
672 },
673 );
674
675 eval {
676 $ctx->ls($dst, 'HEAD', 0);
677 } and die "branch ${branch_name} already exists\n";
678
679 print "Copying ${src} at r${rev} to ${dst}...\n";
680 $ctx->copy($src, $rev, $dst)
681 unless $_dry_run;
682
683 $gs->fetch_all;
684}
685
Adam Roben26e60162007-04-27 11:57:53 -0700686sub cmd_find_rev {
Marc-Andre Lureauea14e6c2008-03-11 10:00:45 +0200687 my $revision_or_hash = shift or die "SVN or git revision required ",
688 "as a command-line argument\n";
Adam Roben26e60162007-04-27 11:57:53 -0700689 my $result;
690 if ($revision_or_hash =~ /^r\d+$/) {
Adam Robenb3cb7e42007-04-29 01:35:27 -0700691 my $head = shift;
692 $head ||= 'HEAD';
693 my @refs;
João Abecasis63c56022008-07-14 16:28:04 +0100694 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
Adam Robenb3cb7e42007-04-29 01:35:27 -0700695 unless ($gs) {
696 die "Unable to determine upstream SVN information from ",
697 "$head history\n";
Adam Roben26e60162007-04-27 11:57:53 -0700698 }
Adam Robenb3cb7e42007-04-29 01:35:27 -0700699 my $desired_revision = substr($revision_or_hash, 1);
João Abecasis63c56022008-07-14 16:28:04 +0100700 $result = $gs->rev_map_get($desired_revision, $uuid);
Adam Roben26e60162007-04-27 11:57:53 -0700701 } else {
702 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
703 $result = $rev;
704 }
705 print "$result\n" if $result;
706}
707
Eric Wong905f8b72007-02-16 03:22:40 -0800708sub cmd_rebase {
709 command_noisy(qw/update-index --refresh/);
Eric Wong13c823f2007-04-08 00:59:19 -0700710 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
711 unless ($gs) {
Eric Wong905f8b72007-02-16 03:22:40 -0800712 die "Unable to determine upstream SVN information from ",
713 "working tree history\n";
714 }
Seth Falcon7d45e142008-05-19 20:29:17 -0700715 if ($_dry_run) {
716 print "Remote Branch: " . $gs->refname . "\n";
717 print "SVN URL: " . $url . "\n";
718 return;
719 }
Eric Wong905f8b72007-02-16 03:22:40 -0800720 if (command(qw/diff-index HEAD --/)) {
721 print STDERR "Cannot rebase with uncommited changes:\n";
722 command_noisy('status');
723 exit 1;
724 }
Eric Wongdee41f32007-03-13 11:40:36 -0700725 unless ($_local) {
Steven Grimmcec0d5a2007-11-29 11:54:39 -0800726 # rebase will checkout for us, so no need to do it explicitly
727 $_no_checkout = 'true';
Eric Wongdee41f32007-03-13 11:40:36 -0700728 $_fetch_all ? $gs->fetch_all : $gs->fetch;
729 }
Eric Wong905f8b72007-02-16 03:22:40 -0800730 command_noisy(rebase_cmd(), $gs->refname);
731}
732
Eric Wong5969cbe2007-01-11 17:58:39 -0800733sub cmd_show_ignore {
Eric Wong13c823f2007-04-08 00:59:19 -0700734 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
735 $gs ||= Git::SVN->new;
Eric Wong5969cbe2007-01-11 17:58:39 -0800736 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200737 $gs->prop_walk($gs->{path}, $r, sub {
738 my ($gs, $path, $props) = @_;
739 print STDOUT "\n# $path\n";
740 my $s = $props->{'svn:ignore'} or return;
741 $s =~ s/[\r\n]+/\n/g;
742 chomp $s;
743 $s =~ s#^#$path#gm;
744 print STDOUT "$s\n";
745 });
Eric Wonga5e0ced2006-06-12 15:23:48 -0700746}
747
Vineet Kumar2d879792007-11-19 14:56:15 -0800748sub cmd_show_externals {
749 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
750 $gs ||= Git::SVN->new;
751 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
752 $gs->prop_walk($gs->{path}, $r, sub {
753 my ($gs, $path, $props) = @_;
754 print STDOUT "\n# $path\n";
755 my $s = $props->{'svn:externals'} or return;
756 $s =~ s/[\r\n]+/\n/g;
757 chomp $s;
758 $s =~ s#^#$path#gm;
759 print STDOUT "$s\n";
760 });
761}
762
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200763sub cmd_create_ignore {
764 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
765 $gs ||= Git::SVN->new;
766 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
767 $gs->prop_walk($gs->{path}, $r, sub {
768 my ($gs, $path, $props) = @_;
769 # $path is of the form /path/to/dir/
Brian Gernhardt7d9fd452009-02-19 13:08:04 -0500770 $path = '.' . $path;
771 # SVN can have attributes on empty directories,
772 # which git won't track
773 mkpath([$path]) unless -d $path;
774 my $ignore = $path . '.gitignore';
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200775 my $s = $props->{'svn:ignore'} or return;
776 open(GITIGNORE, '>', $ignore)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200777 or fatal("Failed to open `$ignore' for writing: $!");
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200778 $s =~ s/[\r\n]+/\n/g;
779 chomp $s;
780 # Prefix all patterns so that the ignore doesn't apply
781 # to sub-directories.
782 $s =~ s#^#/#gm;
783 print GITIGNORE "$s\n";
784 close(GITIGNORE)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200785 or fatal("Failed to close `$ignore': $!");
Gustaf Hendebyc4c66b22008-05-05 00:33:09 +0200786 command_noisy('add', '-f', $ignore);
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200787 });
788}
789
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800790sub canonicalize_path {
791 my ($path) = @_;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800792 my $dot_slash_added = 0;
793 if (substr($path, 0, 1) ne "/") {
794 $path = "./" . $path;
795 $dot_slash_added = 1;
796 }
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800797 # File::Spec->canonpath doesn't collapse x/../y into y (for a
798 # good reason), so let's do this manually.
799 $path =~ s#/+#/#g;
800 $path =~ s#/\.(?:/|$)#/#g;
801 $path =~ s#/[^/]+/\.\.##g;
802 $path =~ s#/$##g;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800803 $path =~ s#^\./## if $dot_slash_added;
Gerrit Pape2fe403e2008-07-06 19:28:50 +0000804 $path =~ s#^/##;
805 $path =~ s#^\.$##;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800806 return $path;
807}
808
Benoit Sigoure15153452007-10-16 16:36:50 +0200809# get_svnprops(PATH)
810# ------------------
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200811# Helper for cmd_propget and cmd_proplist below.
Benoit Sigoure15153452007-10-16 16:36:50 +0200812sub get_svnprops {
813 my $path = shift;
814 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
815 $gs ||= Git::SVN->new;
816
817 # prefix THE PATH by the sub-directory from which the user
818 # invoked us.
819 $path = $cmd_dir_prefix . $path;
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200820 fatal("No such file or directory: $path") unless -e $path;
Benoit Sigoure15153452007-10-16 16:36:50 +0200821 my $is_dir = -d $path ? 1 : 0;
822 $path = $gs->{path} . '/' . $path;
823
824 # canonicalize the path (otherwise libsvn will abort or fail to
825 # find the file)
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800826 $path = canonicalize_path($path);
Benoit Sigoure15153452007-10-16 16:36:50 +0200827
828 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
829 my $props;
830 if ($is_dir) {
831 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
832 }
833 else {
834 (undef, $props) = $gs->ra->get_file($path, $r, undef);
835 }
836 return $props;
837}
838
839# cmd_propget (PROP, PATH)
840# ------------------------
841# Print the SVN property PROP for PATH.
842sub cmd_propget {
843 my ($prop, $path) = @_;
844 $path = '.' if not defined $path;
845 usage(1) if not defined $prop;
846 my $props = get_svnprops($path);
847 if (not defined $props->{$prop}) {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200848 fatal("`$path' does not have a `$prop' SVN property.");
Benoit Sigoure15153452007-10-16 16:36:50 +0200849 }
850 print $props->{$prop} . "\n";
851}
852
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200853# cmd_proplist (PATH)
854# -------------------
855# Print the list of SVN properties for PATH.
856sub cmd_proplist {
857 my $path = shift;
858 $path = '.' if not defined $path;
859 my $props = get_svnprops($path);
860 print "Properties on '$path':\n";
861 foreach (sort keys %{$props}) {
862 print " $_\n";
863 }
864}
865
Eric Wong8164b652007-01-11 15:35:55 -0800866sub cmd_multi_init {
Eric Wong9d55b412006-06-12 15:53:13 -0700867 my $url = shift;
Marc Branchaud62244062009-06-23 13:02:08 -0400868 unless (defined $_trunk || @_branches || @_tags) {
Eric Wong98327e52007-01-04 18:02:00 -0800869 usage(1);
870 }
Eric Wongdc431662007-05-19 03:59:02 -0700871
872 # there are currently some bugs that prevent multi-init/multi-fetch
873 # setups from working well without this.
874 $Git::SVN::_minimize_url = 1;
875
Eric Wong8164b652007-01-11 15:35:55 -0800876 $_prefix = '' unless defined $_prefix;
Eric Wongdadc6d22007-02-14 12:27:41 -0800877 if (defined $url) {
878 $url =~ s#/+$##;
879 init_subdir(@_);
880 }
Eric Wongf30603f2007-02-23 01:26:26 -0800881 do_git_init_db();
Eric Wong98327e52007-01-04 18:02:00 -0800882 if (defined $_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800883 my $trunk_ref = $_prefix . 'trunk';
884 # try both old-style and new-style lookups:
885 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
Eric Wong8164b652007-01-11 15:35:55 -0800886 unless ($gs_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800887 my ($trunk_url, $trunk_path) =
888 complete_svn_url($url, $_trunk);
889 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
890 undef, $trunk_ref);
Eric Wong98327e52007-01-04 18:02:00 -0800891 }
Eric Wongc35b96e2006-10-11 11:53:21 -0700892 }
Marc Branchaud62244062009-06-23 13:02:08 -0400893 return unless @_branches || @_tags;
Eric Wonge7db67e2007-01-11 17:09:26 -0800894 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
Marc Branchaud62244062009-06-23 13:02:08 -0400895 foreach my $path (@_branches) {
896 complete_url_ls_init($ra, $path, '--branches/-b', $_prefix);
897 }
898 foreach my $path (@_tags) {
899 complete_url_ls_init($ra, $path, '--tags/-t', $_prefix.'tags/');
900 }
Eric Wong9d55b412006-06-12 15:53:13 -0700901}
902
Eric Wong1c8443b2007-01-14 02:17:00 -0800903sub cmd_multi_fetch {
Eric Wong0af9c9f2007-01-27 22:28:56 -0800904 my $remotes = Git::SVN::read_all_remotes();
905 foreach my $repo_id (sort keys %$remotes) {
Eric Wongdb03cd22007-02-13 00:38:02 -0800906 if ($remotes->{$repo_id}->{url}) {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800907 Git::SVN::fetch_all($repo_id, $remotes);
908 }
Eric Wong706587f2007-01-18 17:50:01 -0800909 }
Eric Wong9d55b412006-06-12 15:53:13 -0700910}
911
Eric Wong44320b92007-01-13 22:35:53 -0800912# this command is special because it requires no metadata
913sub cmd_commit_diff {
914 my ($ta, $tb, $url) = @_;
915 my $usage = "Usage: $0 commit-diff -r<revision> ".
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200916 "<tree-ish> <tree-ish> [<URL>]";
Eric Wong44320b92007-01-13 22:35:53 -0800917 fatal($usage) if (!defined $ta || !defined $tb);
Karl Hasselströmd72ab8c2008-05-17 17:07:09 +0200918 my $svn_path = '';
Eric Wong44320b92007-01-13 22:35:53 -0800919 if (!defined $url) {
920 my $gs = eval { Git::SVN->new };
921 if (!$gs) {
922 fatal("Needed URL or usable git-svn --id in ",
923 "the command-line\n", $usage);
924 }
925 $url = $gs->{url};
Eric Wongd3a840d2007-01-26 01:32:45 -0800926 $svn_path = $gs->{path};
Eric Wong44320b92007-01-13 22:35:53 -0800927 }
928 unless (defined $_revision) {
929 fatal("-r|--revision is a required argument\n", $usage);
930 }
931 if (defined $_message && defined $_file) {
932 fatal("Both --message/-m and --file/-F specified ",
933 "for the commit message.\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200934 "I have no idea what you mean");
Eric Wong44320b92007-01-13 22:35:53 -0800935 }
936 if (defined $_file) {
937 $_message = file_to_s($_file);
938 } else {
939 $_message ||= get_commit_entry($tb)->{log};
940 }
941 my $ra ||= Git::SVN::Ra->new($url);
942 my $r = $_revision;
943 if ($r eq 'HEAD') {
944 $r = $ra->get_latest_revnum;
945 } elsif ($r !~ /^\d+$/) {
946 die "revision argument: $r not understood by git-svn\n";
947 }
Eric Wong61395352007-01-27 14:33:08 -0800948 my %ed_opts = ( r => $r,
949 log => $_message,
950 ra => $ra,
951 tree_a => $ta,
952 tree_b => $tb,
953 editor_cb => sub { print "Committed r$_[0]\n" },
954 svn_path => $svn_path );
955 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong44320b92007-01-13 22:35:53 -0800956 print "No changes\n$ta == $tb\n";
957 }
Eric Wong44320b92007-01-13 22:35:53 -0800958}
959
Thomas Rast05427b92008-08-26 21:32:37 +0200960sub escape_uri_only {
961 my ($uri) = @_;
962 my @tmp;
963 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -0700964 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Thomas Rast05427b92008-08-26 21:32:37 +0200965 push @tmp, $_;
966 }
967 join('/', @tmp);
968}
969
970sub escape_url {
971 my ($url) = @_;
972 if ($url =~ m#^([^:]+)://([^/]*)(.*)$#) {
973 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
974 $url = "$scheme://$domain$uri";
975 }
976 $url;
977}
978
David D. Kilzere6fefa92007-11-21 11:57:18 -0800979sub cmd_info {
Eric Wongbd2d4f92008-08-05 00:35:16 -0700980 my $path = canonicalize_path(defined($_[0]) ? $_[0] : ".");
Thomas Rastedde9112008-08-26 21:32:36 +0200981 my $fullpath = canonicalize_path($cmd_dir_prefix . $path);
Eric Wongbd2d4f92008-08-05 00:35:16 -0700982 if (exists $_[1]) {
David D. Kilzere6fefa92007-11-21 11:57:18 -0800983 die "Too many arguments specified\n";
984 }
985
986 my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
987
988 if (!$file_type && !$diff_status) {
Thomas Rast2cf3e3a2008-08-29 15:42:48 +0200989 print STDERR "svn: '$path' is not under version control\n";
990 exit 1;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800991 }
992
993 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
994 unless ($gs) {
995 die "Unable to determine upstream SVN information from ",
996 "working tree history\n";
997 }
Eric Wongbd2d4f92008-08-05 00:35:16 -0700998
999 # canonicalize_path() will return "" to make libsvn 1.5.x happy,
1000 $path = "." if $path eq "";
1001
Thomas Rastedde9112008-08-26 21:32:36 +02001002 my $full_url = $url . ($fullpath eq "" ? "" : "/$fullpath");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001003
David D. Kilzer8b014d72007-11-21 11:57:19 -08001004 if ($_url) {
Thomas Rast05427b92008-08-26 21:32:37 +02001005 print escape_url($full_url), "\n";
David D. Kilzer8b014d72007-11-21 11:57:19 -08001006 return;
1007 }
1008
David D. Kilzere6fefa92007-11-21 11:57:18 -08001009 my $result = "Path: $path\n";
1010 $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
Thomas Rast05427b92008-08-26 21:32:37 +02001011 $result .= "URL: " . escape_url($full_url) . "\n";
David D. Kilzere6fefa92007-11-21 11:57:18 -08001012
Eric Wonga5460eb2007-11-21 18:20:57 -08001013 eval {
1014 my $repos_root = $gs->repos_root;
1015 Git::SVN::remove_username($repos_root);
Thomas Rast05427b92008-08-26 21:32:37 +02001016 $result .= "Repository Root: " . escape_url($repos_root) . "\n";
Eric Wonga5460eb2007-11-21 18:20:57 -08001017 };
1018 if ($@) {
1019 $result .= "Repository Root: (offline)\n";
1020 }
Marcel Koeppen22ba47f2009-01-19 03:02:01 +01001021 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" &&
1022 ($SVN::Core::VERSION le '1.5.4' || $file_type ne "dir");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001023 $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
1024
1025 $result .= "Node Kind: " .
1026 ($file_type eq "dir" ? "directory" : "file") . "\n";
1027
1028 my $schedule = $diff_status eq "A"
1029 ? "add"
1030 : ($diff_status eq "D" ? "delete" : "normal");
1031 $result .= "Schedule: $schedule\n";
1032
1033 if ($diff_status eq "A") {
1034 print $result, "\n";
1035 return;
1036 }
1037
1038 my ($lc_author, $lc_rev, $lc_date_utc);
Thomas Rastedde9112008-08-26 21:32:36 +02001039 my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001040 my $log = command_output_pipe(@args);
1041 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
1042 while (<$log>) {
1043 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
1044 $lc_author = $1;
1045 $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
1046 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
1047 (undef, $lc_rev, undef) = ::extract_metadata($1);
1048 }
1049 }
1050 close $log;
1051
1052 Git::SVN::Log::set_local_timezone();
1053
1054 $result .= "Last Changed Author: $lc_author\n";
1055 $result .= "Last Changed Rev: $lc_rev\n";
1056 $result .= "Last Changed Date: " .
1057 Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
1058
1059 if ($file_type ne "dir") {
1060 my $text_last_updated_date =
1061 ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
1062 $result .=
1063 "Text Last Updated: " .
1064 Git::SVN::Log::format_svn_date($text_last_updated_date) .
1065 "\n";
1066 my $checksum;
1067 if ($diff_status eq "D") {
1068 my ($fh, $ctx) =
1069 command_output_pipe(qw(cat-file blob), "HEAD:$path");
1070 if ($file_type eq "link") {
1071 my $file_name = <$fh>;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001072 $checksum = md5sum("link $file_name");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001073 } else {
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001074 $checksum = md5sum($fh);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001075 }
1076 command_close_pipe($fh, $ctx);
1077 } elsif ($file_type eq "link") {
1078 my $file_name =
1079 command(qw(cat-file blob), "HEAD:$path");
1080 $checksum =
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001081 md5sum("link " . $file_name);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001082 } else {
1083 open FILE, "<", $path or die $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001084 $checksum = md5sum(\*FILE);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001085 close FILE or die $!;
1086 }
1087 $result .= "Checksum: " . $checksum . "\n";
1088 }
1089
1090 print $result, "\n";
1091}
1092
Ben Jackson195643f2009-06-03 20:45:52 -07001093sub cmd_reset {
1094 my $target = shift || $_revision or die "SVN revision required\n";
1095 $target = $1 if $target =~ /^r(\d+)$/;
1096 $target =~ /^\d+$/ or die "Numeric SVN revision expected\n";
1097 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1098 unless ($gs) {
1099 die "Unable to determine upstream SVN information from ".
1100 "history\n";
1101 }
1102 my ($r, $c) = $gs->find_rev_before($target, not $_fetch_parent);
1103 $gs->rev_map_set($r, $c, 'reset', $uuid);
1104 print "r$r = $c ($gs->{ref_id})\n";
1105}
1106
Eric Wong3397f9d2006-02-16 01:24:16 -08001107########################### utility functions #########################
1108
Eric Wong905f8b72007-02-16 03:22:40 -08001109sub rebase_cmd {
1110 my @cmd = qw/rebase/;
1111 push @cmd, '-v' if $_verbose;
1112 push @cmd, qw/--merge/ if $_merge;
1113 push @cmd, "--strategy=$_strategy" if $_strategy;
1114 @cmd;
1115}
1116
Eric Wong1e889ef2007-02-16 01:45:13 -08001117sub post_fetch_checkout {
1118 return if $_no_checkout;
1119 my $gs = $Git::SVN::_head or return;
1120 return if verify_ref('refs/heads/master^0');
1121
1122 my $valid_head = verify_ref('HEAD^0');
1123 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
1124 return if ($valid_head || !verify_ref('HEAD^0'));
1125
1126 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
1127 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
1128 return if -f $index;
1129
Matthias Lederhofer7ae3df82007-06-03 16:48:16 +02001130 return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
Eric Wong1e889ef2007-02-16 01:45:13 -08001131 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
1132 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
1133 print STDERR "Checked out HEAD:\n ",
1134 $gs->full_url, " r", $gs->last_rev, "\n";
1135}
1136
Eric Wong98327e52007-01-04 18:02:00 -08001137sub complete_svn_url {
1138 my ($url, $path) = @_;
1139 $path =~ s#/+$##;
Eric Wong98327e52007-01-04 18:02:00 -08001140 if ($path !~ m#^[a-z\+]+://#) {
Eric Wong98327e52007-01-04 18:02:00 -08001141 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
1142 fatal("E: '$path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001143 "and a separate URL is not specified");
Eric Wong98327e52007-01-04 18:02:00 -08001144 }
Eric Wong706587f2007-01-18 17:50:01 -08001145 return ($url, $path);
Eric Wong98327e52007-01-04 18:02:00 -08001146 }
Eric Wong706587f2007-01-18 17:50:01 -08001147 return ($path, '');
Eric Wong98327e52007-01-04 18:02:00 -08001148}
1149
Eric Wong9d55b412006-06-12 15:53:13 -07001150sub complete_url_ls_init {
Eric Wong706587f2007-01-18 17:50:01 -08001151 my ($ra, $repo_path, $switch, $pfx) = @_;
1152 unless ($repo_path) {
Eric Wong9d55b412006-06-12 15:53:13 -07001153 print STDERR "W: $switch not specified\n";
1154 return;
1155 }
Eric Wong706587f2007-01-18 17:50:01 -08001156 $repo_path =~ s#/+$##;
1157 if ($repo_path =~ m#^[a-z\+]+://#) {
1158 $ra = Git::SVN::Ra->new($repo_path);
1159 $repo_path = '';
Eric Wonge7db67e2007-01-11 17:09:26 -08001160 } else {
Eric Wong706587f2007-01-18 17:50:01 -08001161 $repo_path =~ s#^/+##;
Eric Wonge7db67e2007-01-11 17:09:26 -08001162 unless ($ra) {
Eric Wong706587f2007-01-18 17:50:01 -08001163 fatal("E: '$repo_path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001164 "and a separate URL is not specified");
Eric Wong9d55b412006-06-12 15:53:13 -07001165 }
Eric Wonge7db67e2007-01-11 17:09:26 -08001166 }
Eric Wong706587f2007-01-18 17:50:01 -08001167 my $url = $ra->{url};
Eric Wongb4d57e52007-02-14 15:10:44 -08001168 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
1169 my $k = "svn-remote.$gs->{repo_id}.url";
1170 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
1171 if ($orig_url && ($orig_url ne $gs->{url})) {
1172 die "$k already set: $orig_url\n",
1173 "wanted to set to: $gs->{url}\n";
Eric Wong88cf4102007-02-01 03:59:07 -08001174 }
Eric Wongb4d57e52007-02-14 15:10:44 -08001175 command_oneline('config', $k, $gs->{url}) unless $orig_url;
Eric Wonged0b9d42008-03-14 11:01:23 -07001176 my $remote_path = "$ra->{svn_path}/$repo_path";
Eric Wongb4d57e52007-02-14 15:10:44 -08001177 $remote_path =~ s#/+#/#g;
1178 $remote_path =~ s#^/##g;
Eric Wonged0b9d42008-03-14 11:01:23 -07001179 $remote_path .= "/*" if $remote_path !~ /\*/;
Eric Wongb4d57e52007-02-14 15:10:44 -08001180 my ($n) = ($switch =~ /^--(\w+)/);
1181 if (length $pfx && $pfx !~ m#/$#) {
1182 die "--prefix='$pfx' must have a trailing slash '/'\n";
Eric Wong9d55b412006-06-12 15:53:13 -07001183 }
Marcus Griep570d35c2008-08-08 01:41:57 -07001184 command_noisy('config',
Marc Branchaud62244062009-06-23 13:02:08 -04001185 '--add',
Marcus Griep570d35c2008-08-08 01:41:57 -07001186 "svn-remote.$gs->{repo_id}.$n",
1187 "$remote_path:refs/remotes/$pfx*" .
1188 ('/*' x (($remote_path =~ tr/*/*/) - 1)) );
Eric Wong9d55b412006-06-12 15:53:13 -07001189}
1190
Eric Wongaef4e922006-12-15 10:59:54 -08001191sub verify_ref {
1192 my ($ref) = @_;
Eric Wong2c5c1d52006-12-28 01:16:21 -08001193 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
1194 { STDERR => 0 }); };
Eric Wongaef4e922006-12-15 10:59:54 -08001195}
1196
Eric Wonga5e0ced2006-06-12 15:23:48 -07001197sub get_tree_from_treeish {
Eric Wongcf52b8f2006-02-20 10:57:28 -08001198 my ($treeish) = @_;
Eric Wong44320b92007-01-13 22:35:53 -08001199 # $treeish can be a symbolic ref, too:
Eric Wongaef4e922006-12-15 10:59:54 -08001200 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001201 my $expected;
1202 while ($type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001203 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001204 }
1205 if ($type eq 'commit') {
Eric Wongaef4e922006-12-15 10:59:54 -08001206 $expected = (grep /^tree /, command(qw/cat-file commit/,
1207 $treeish))[0];
Eric Wong44320b92007-01-13 22:35:53 -08001208 ($expected) = ($expected =~ /^tree ($sha1)$/o);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001209 die "Unable to get tree from $treeish\n" unless $expected;
1210 } elsif ($type eq 'tree') {
1211 $expected = $treeish;
1212 } else {
1213 die "$treeish is a $type, expected tree, tag or commit\n";
1214 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07001215 return $expected;
1216}
Eric Wongcf52b8f2006-02-20 10:57:28 -08001217
Eric Wong44320b92007-01-13 22:35:53 -08001218sub get_commit_entry {
1219 my ($treeish) = shift;
1220 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
1221 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1222 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1223 open my $log_fh, '>', $commit_editmsg or croak $!;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001224
Eric Wong44320b92007-01-13 22:35:53 -08001225 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wong4ad45152006-07-09 20:20:48 -07001226 if ($type eq 'commit' || $type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001227 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
Eric Wong44320b92007-01-13 22:35:53 -08001228 $type, $treeish);
Eric Wong3397f9d2006-02-16 01:24:16 -08001229 my $in_msg = 0;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001230 my $author;
1231 my $saw_from = 0;
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001232 my $msgbuf = "";
Eric Wong3397f9d2006-02-16 01:24:16 -08001233 while (<$msg_fh>) {
1234 if (!$in_msg) {
1235 $in_msg = 1 if (/^\s*$/);
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001236 $author = $1 if (/^author (.*>)/);
Eric Wongdf746c52006-03-03 01:20:08 -08001237 } elsif (/^git-svn-id: /) {
Eric Wong44320b92007-01-13 22:35:53 -08001238 # skip this for now, we regenerate the
1239 # correct one on re-fetch anyways
1240 # TODO: set *:merge properties or like...
Eric Wong3397f9d2006-02-16 01:24:16 -08001241 } else {
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001242 if (/^From:/ || /^Signed-off-by:/) {
1243 $saw_from = 1;
1244 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001245 $msgbuf .= $_;
Eric Wong3397f9d2006-02-16 01:24:16 -08001246 }
1247 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001248 $msgbuf =~ s/\s+$//s;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001249 if ($Git::SVN::_add_author_from && defined($author)
1250 && !$saw_from) {
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001251 $msgbuf .= "\n\nFrom: $author";
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001252 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001253 print $log_fh $msgbuf or croak $!;
Eric Wongaef4e922006-12-15 10:59:54 -08001254 command_close_pipe($msg_fh, $ctx);
Eric Wong3397f9d2006-02-16 01:24:16 -08001255 }
Eric Wong44320b92007-01-13 22:35:53 -08001256 close $log_fh or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -08001257
1258 if ($_edit || ($type eq 'tree')) {
1259 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
Eric Wong44320b92007-01-13 22:35:53 -08001260 # TODO: strip out spaces, comments, like git-commit.sh
1261 system($editor, $commit_editmsg);
Eric Wong3397f9d2006-02-16 01:24:16 -08001262 }
Eric Wong44320b92007-01-13 22:35:53 -08001263 rename $commit_editmsg, $commit_msg or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07001264 {
Eric Wongb510df82009-05-28 00:56:23 -07001265 require Encode;
Eric Wong16fc08e2008-10-29 23:49:26 -07001266 # SVN requires messages to be UTF-8 when entering the repo
1267 local $/;
1268 open $log_fh, '<', $commit_msg or croak $!;
1269 binmode $log_fh;
1270 chomp($log_entry{log} = <$log_fh>);
1271
Eric Wongb510df82009-05-28 00:56:23 -07001272 my $enc = Git::config('i18n.commitencoding') || 'UTF-8';
1273 my $msg = $log_entry{log};
1274
1275 eval { $msg = Encode::decode($enc, $msg, 1) };
1276 if ($@) {
1277 die "Could not decode as $enc:\n", $msg,
1278 "\nPerhaps you need to set i18n.commitencoding\n";
Eric Wong16fc08e2008-10-29 23:49:26 -07001279 }
Eric Wongb510df82009-05-28 00:56:23 -07001280
1281 eval { $msg = Encode::encode('UTF-8', $msg, 1) };
1282 die "Could not encode as UTF-8:\n$msg\n" if $@;
1283
1284 $log_entry{log} = $msg;
1285
Eric Wong16fc08e2008-10-29 23:49:26 -07001286 close $log_fh or croak $!;
1287 }
Eric Wong44320b92007-01-13 22:35:53 -08001288 unlink $commit_msg;
1289 \%log_entry;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001290}
1291
Eric Wong3397f9d2006-02-16 01:24:16 -08001292sub s_to_file {
1293 my ($str, $file, $mode) = @_;
1294 open my $fd,'>',$file or croak $!;
1295 print $fd $str,"\n" or croak $!;
1296 close $fd or croak $!;
1297 chmod ($mode &~ umask, $file) if (defined $mode);
1298}
1299
1300sub file_to_s {
1301 my $file = shift;
1302 open my $fd,'<',$file or croak "$!: file: $file\n";
1303 local $/;
1304 my $ret = <$fd>;
1305 close $fd or croak $!;
1306 $ret =~ s/\s*$//s;
1307 return $ret;
1308}
1309
Eric Wongeeb0abe2006-03-03 01:20:08 -08001310# '<svn username> = real-name <email address>' mapping based on git-svnimport:
1311sub load_authors {
1312 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001313 my $log = $cmd eq 'log';
Eric Wongeeb0abe2006-03-03 01:20:08 -08001314 while (<$authors>) {
1315 chomp;
Richard MUSIL575d0252007-07-17 19:02:57 +02001316 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
Eric Wongeeb0abe2006-03-03 01:20:08 -08001317 my ($user, $name, $email) = ($1, $2, $3);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001318 if ($log) {
1319 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
1320 } else {
1321 $users{$user} = [$name, $email];
1322 }
Eric Wong79bb8d82006-06-01 02:35:44 -07001323 }
1324 close $authors or croak $!;
1325}
1326
Tom Princee0d10e12007-01-28 16:16:53 -08001327# convert GetOpt::Long specs for use by git-config
Eric Wongb8c92ca2006-05-24 01:40:37 -07001328sub read_repo_config {
Eric Wong706587f2007-01-18 17:50:01 -08001329 return unless -d $ENV{GIT_DIR};
Eric Wongb8c92ca2006-05-24 01:40:37 -07001330 my $opts = shift;
Eric Wong97ae0912007-02-11 15:21:24 -08001331 my @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001332 foreach my $o (keys %$opts) {
Eric Wong97ae0912007-02-11 15:21:24 -08001333 # if we have mixedCase and a long option-only, then
1334 # it's a config-only variable that we don't need for
1335 # the command-line.
1336 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001337 my $v = $opts->{$o};
Eric Wong97ae0912007-02-11 15:21:24 -08001338 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001339 $key =~ s/-//g;
Deskin Miller225f1d02008-10-23 15:21:34 -04001340 my $arg = 'git config';
Eric Wongb8c92ca2006-05-24 01:40:37 -07001341 $arg .= ' --int' if ($o =~ /[:=]i$/);
1342 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1343 if (ref $v eq 'ARRAY') {
1344 chomp(my @tmp = `$arg --get-all svn.$key`);
1345 @$v = @tmp if @tmp;
1346 } else {
1347 chomp(my $tmp = `$arg --get svn.$key`);
Eric Wong77742842007-02-10 21:07:12 -08001348 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
Eric Wongb8c92ca2006-05-24 01:40:37 -07001349 $$v = $tmp;
1350 }
1351 }
1352 }
Eric Wong97ae0912007-02-11 15:21:24 -08001353 delete @$opts{@config_only} if @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001354}
1355
Eric Wong79bb8d82006-06-01 02:35:44 -07001356sub extract_metadata {
Eric Wongc1927a82006-06-27 19:39:11 -07001357 my $id = shift or return (undef, undef, undef);
Sam Vilain3dfab992007-06-30 20:56:13 +12001358 my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
Eric Wong79bb8d82006-06-01 02:35:44 -07001359 \s([a-f\d\-]+)$/x);
Eric Wonge70dc782006-11-23 14:54:04 -08001360 if (!defined $rev || !$uuid || !$url) {
Eric Wong79bb8d82006-06-01 02:35:44 -07001361 # some of the original repositories I made had
Pavel Roskin82e5a822006-07-10 01:50:18 -04001362 # identifiers like this:
Sam Vilain3dfab992007-06-30 20:56:13 +12001363 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
Eric Wong79bb8d82006-06-01 02:35:44 -07001364 }
1365 return ($url, $rev, $uuid);
1366}
1367
Eric Wongc1927a82006-06-27 19:39:11 -07001368sub cmt_metadata {
1369 return extract_metadata((grep(/^git-svn-id: /,
Eric Wongaef4e922006-12-15 10:59:54 -08001370 command(qw/cat-file commit/, shift)))[-1]);
Eric Wongc1927a82006-06-27 19:39:11 -07001371}
1372
Boris Byk6ea42032009-04-11 00:32:41 +04001373sub cmt_sha2rev_batch {
1374 my %s2r;
1375 my ($pid, $in, $out, $ctx) = command_bidi_pipe(qw/cat-file --batch/);
1376 my $list = shift;
1377
1378 foreach my $sha (@{$list}) {
1379 my $first = 1;
1380 my $size = 0;
1381 print $out $sha, "\n";
1382
1383 while (my $line = <$in>) {
1384 if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) {
1385 last;
1386 } elsif ($first &&
1387 $line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) {
1388 $first = 0;
1389 $size = $1;
1390 next;
1391 } elsif ($line =~ /^(git-svn-id: )/) {
1392 my (undef, $rev, undef) =
1393 extract_metadata($line);
1394 $s2r{$sha} = $rev;
1395 }
1396
1397 $size -= length($line);
1398 last if ($size == 0);
1399 }
1400 }
1401
1402 command_close_bidi_pipe($pid, $in, $out, $ctx);
1403
1404 return \%s2r;
1405}
1406
Eric Wong905f8b72007-02-16 03:22:40 -08001407sub working_head_info {
1408 my ($head, $refs) = @_;
Pedro Melob6309ac2008-04-10 17:05:21 +01001409 my @args = ('log', '--no-color', '--first-parent', '--pretty=medium');
Lars Hjemli05b4df32007-09-05 11:35:29 +02001410 my ($fh, $ctx) = command_output_pipe(@args, $head);
Sam Vilain3dfab992007-06-30 20:56:13 +12001411 my $hash;
Sam Vilain40cb8f82007-06-30 20:56:14 +12001412 my %max;
Sam Vilain3dfab992007-06-30 20:56:13 +12001413 while (<$fh>) {
1414 if ( m{^commit ($::sha1)$} ) {
1415 unshift @$refs, $hash if $hash and $refs;
1416 $hash = $1;
1417 next;
1418 }
1419 next unless s{^\s*(git-svn-id:)}{$1};
1420 my ($url, $rev, $uuid) = extract_metadata($_);
Eric Wong13c823f2007-04-08 00:59:19 -07001421 if (defined $url && defined $rev) {
Sam Vilain40cb8f82007-06-30 20:56:14 +12001422 next if $max{$url} and $max{$url} < $rev;
Eric Wong13c823f2007-04-08 00:59:19 -07001423 if (my $gs = Git::SVN->find_by_url($url)) {
João Abecasis63c56022008-07-14 16:28:04 +01001424 my $c = $gs->rev_map_get($rev, $uuid);
Adam Robenb03c7a62007-04-25 11:50:32 -07001425 if ($c && $c eq $hash) {
Eric Wong13c823f2007-04-08 00:59:19 -07001426 close $fh; # break the pipe
1427 return ($url, $rev, $uuid, $gs);
Sam Vilain40cb8f82007-06-30 20:56:14 +12001428 } else {
Eric Wong060610c2007-12-08 23:27:41 -08001429 $max{$url} ||= $gs->rev_map_max;
Eric Wong13c823f2007-04-08 00:59:19 -07001430 }
1431 }
1432 }
Eric Wong905f8b72007-02-16 03:22:40 -08001433 }
Eric Wong13c823f2007-04-08 00:59:19 -07001434 command_close_pipe($fh, $ctx);
1435 (undef, undef, undef, undef);
Eric Wong905f8b72007-02-16 03:22:40 -08001436}
1437
Eric Wong733a65a2007-06-13 02:23:28 -07001438sub read_commit_parents {
1439 my ($parents, $c) = @_;
Eric Wong7b02b852007-09-08 16:33:08 -07001440 chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
1441 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1442 @{$parents->{$c}} = split(/ /, $p);
Eric Wong733a65a2007-06-13 02:23:28 -07001443}
1444
1445sub linearize_history {
1446 my ($gs, $refs) = @_;
1447 my %parents;
1448 foreach my $c (@$refs) {
1449 read_commit_parents(\%parents, $c);
1450 }
1451
1452 my @linear_refs;
1453 my %skip = ();
1454 my $last_svn_commit = $gs->last_commit;
1455 foreach my $c (reverse @$refs) {
1456 next if $c eq $last_svn_commit;
1457 last if $skip{$c};
1458
1459 unshift @linear_refs, $c;
1460 $skip{$c} = 1;
1461
1462 # we only want the first parent to diff against for linear
1463 # history, we save the rest to inject when we finalize the
1464 # svn commit
1465 my $fp_a = verify_ref("$c~1");
1466 my $fp_b = shift @{$parents{$c}} if $parents{$c};
1467 if (!$fp_a || !$fp_b) {
1468 die "Commit $c\n",
1469 "has no parent commit, and therefore ",
1470 "nothing to diff against.\n",
1471 "You should be working from a repository ",
1472 "originally created by git-svn\n";
1473 }
1474 if ($fp_a ne $fp_b) {
1475 die "$c~1 = $fp_a, however parsing commit $c ",
1476 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1477 }
1478
1479 foreach my $p (@{$parents{$c}}) {
1480 $skip{$p} = 1;
1481 }
1482 }
1483 (\@linear_refs, \%parents);
1484}
1485
David D. Kilzere6fefa92007-11-21 11:57:18 -08001486sub find_file_type_and_diff_status {
1487 my ($path) = @_;
Dmitry Potapov107cee52008-07-21 00:14:07 +04001488 return ('dir', '') if $path eq '';
David D. Kilzere6fefa92007-11-21 11:57:18 -08001489
1490 my $diff_output =
1491 command_oneline(qw(diff --cached --name-status --), $path) || "";
1492 my $diff_status = (split(' ', $diff_output))[0] || "";
1493
1494 my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || "";
1495
1496 return (undef, undef) if !$diff_status && !$ls_tree;
1497
1498 if ($diff_status eq "A") {
1499 return ("link", $diff_status) if -l $path;
1500 return ("dir", $diff_status) if -d $path;
1501 return ("file", $diff_status);
1502 }
1503
1504 my $mode = (split(' ', $ls_tree))[0] || "";
1505
1506 return ("link", $diff_status) if $mode eq "120000";
1507 return ("dir", $diff_status) if $mode eq "040000";
1508 return ("file", $diff_status);
1509}
1510
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001511sub md5sum {
1512 my $arg = shift;
1513 my $ref = ref $arg;
1514 my $md5 = Digest::MD5->new();
Marcus Griep0b191382008-08-12 12:00:53 -04001515 if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001516 $md5->addfile($arg) or croak $!;
1517 } elsif ($ref eq 'SCALAR') {
1518 $md5->add($$arg) or croak $!;
1519 } elsif (!$ref) {
1520 $md5->add($arg) or croak $!;
1521 } else {
1522 ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1523 }
1524 return $md5->hexdigest();
1525}
1526
Eric Wong9b981fc2007-01-11 12:14:21 -08001527package Git::SVN;
1528use strict;
1529use warnings;
Eric Wong060610c2007-12-08 23:27:41 -08001530use Fcntl qw/:DEFAULT :seek/;
1531use constant rev_map_fmt => 'NH40';
Eric Wongecc712d2007-01-31 12:28:10 -08001532use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
Eric Wong62e349d2007-02-16 19:57:29 -08001533 $_repack $_repack_flags $_use_svm_props $_head
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00001534 $_use_svnsync_props $no_reuse_existing $_minimize_url
Pete Harlane82f0d72009-01-17 20:10:14 -08001535 $_use_log_author $_add_author_from $_localtime/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001536use Carp qw/croak/;
1537use File::Path qw/mkpath/;
Eric Wong373274f2007-01-31 13:54:23 -08001538use File::Copy qw/copy/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001539use IPC::Open3;
1540
Karl Hasselström94bc9142008-02-03 17:56:18 +01001541my ($_gc_nr, $_gc_period);
1542
Eric Wong9b981fc2007-01-11 12:14:21 -08001543# properties that we do not log:
1544my %SKIP_PROP;
1545BEGIN {
1546 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
1547 svn:special svn:executable
1548 svn:entry:committed-rev
1549 svn:entry:last-author
1550 svn:entry:uuid
1551 svn:entry:committed-date/;
Eric Wong91b03282007-02-11 00:51:33 -08001552
1553 # some options are read globally, but can be overridden locally
1554 # per [svn-remote "..."] section. Command-line options will *NOT*
1555 # override options set in an [svn-remote "..."] section
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001556 no strict 'refs';
1557 for my $option (qw/follow_parent no_metadata use_svm_props
1558 use_svnsync_props/) {
1559 my $key = $option;
Eric Wong91b03282007-02-11 00:51:33 -08001560 $key =~ tr/_//d;
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001561 my $prop = "-$option";
1562 *$option = sub {
1563 my ($self) = @_;
1564 return $self->{$prop} if exists $self->{$prop};
1565 my $k = "svn-remote.$self->{repo_id}.$key";
1566 eval { command_oneline(qw/config --get/, $k) };
1567 if ($@) {
1568 $self->{$prop} = ${"Git::SVN::_$option"};
Eric Wong91b03282007-02-11 00:51:33 -08001569 } else {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001570 my $v = command_oneline(qw/config --bool/,$k);
1571 $self->{$prop} = $v eq 'false' ? 0 : 1;
Eric Wong91b03282007-02-11 00:51:33 -08001572 }
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001573 return $self->{$prop};
1574 }
Eric Wong91b03282007-02-11 00:51:33 -08001575 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001576}
1577
Marcus Griep0b191382008-08-12 12:00:53 -04001578
Eric Wong321b1842008-01-02 10:10:03 -08001579my (%LOCKFILES, %INDEX_FILES);
1580END {
1581 unlink keys %LOCKFILES if %LOCKFILES;
1582 unlink keys %INDEX_FILES if %INDEX_FILES;
1583}
Eric Wong373274f2007-01-31 13:54:23 -08001584
Eric Wong4bb9ed02007-02-03 13:29:17 -08001585sub resolve_local_globs {
1586 my ($url, $fetch, $glob_spec) = @_;
1587 return unless defined $glob_spec;
1588 my $ref = $glob_spec->{ref};
1589 my $path = $glob_spec->{path};
1590 foreach (command(qw#for-each-ref --format=%(refname) refs/remotes#)) {
1591 next unless m#^refs/remotes/$ref->{regex}$#;
1592 my $p = $1;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001593 my $pathname = desanitize_refname($path->full_path($p));
1594 my $refname = desanitize_refname($ref->full_path($p));
Eric Wong4bb9ed02007-02-03 13:29:17 -08001595 if (my $existing = $fetch->{$pathname}) {
1596 if ($existing ne $refname) {
1597 die "Refspec conflict:\n",
1598 "existing: refs/remotes/$existing\n",
1599 " globbed: refs/remotes/$refname\n";
1600 }
1601 my $u = (::cmt_metadata("refs/remotes/$refname"))[0];
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001602 $u =~ s!^\Q$url\E(/|$)!! or die
Eric Wong4bb9ed02007-02-03 13:29:17 -08001603 "refs/remotes/$refname: '$url' not found in '$u'\n";
1604 if ($pathname ne $u) {
1605 warn "W: Refspec glob conflict ",
1606 "(ref: refs/remotes/$refname):\n",
1607 "expected path: $pathname\n",
1608 " real path: $u\n",
1609 "Continuing ahead with $u\n";
1610 next;
1611 }
1612 } else {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001613 $fetch->{$pathname} = $refname;
1614 }
1615 }
1616}
1617
Eric Wonge98671e2007-02-14 02:21:19 -08001618sub parse_revision_argument {
1619 my ($base, $head) = @_;
1620 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
1621 return ($base, $head);
1622 }
1623 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
1624 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
1625 return ($head, $head) if ($::_revision eq 'HEAD');
1626 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
1627 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
1628 die "revision argument: $::_revision not understood by git-svn\n";
1629}
1630
Eric Wong0af9c9f2007-01-27 22:28:56 -08001631sub fetch_all {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001632 my ($repo_id, $remotes) = @_;
Eric Wong905f8b72007-02-16 03:22:40 -08001633 if (ref $repo_id) {
1634 my $gs = $repo_id;
1635 $repo_id = undef;
1636 $repo_id = $gs->{repo_id};
1637 }
1638 $remotes ||= read_all_remotes();
Eric Wong7447b4b2007-02-14 18:38:46 -08001639 my $remote = $remotes->{$repo_id} or
1640 die "[svn-remote \"$repo_id\"] unknown\n";
Eric Wonge5181922007-02-08 12:53:57 -08001641 my $fetch = $remote->{fetch};
Eric Wong7447b4b2007-02-14 18:38:46 -08001642 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
Eric Wonge5181922007-02-08 12:53:57 -08001643 my (@gs, @globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001644 my $ra = Git::SVN::Ra->new($url);
Eric Wong26a62d52007-02-12 13:25:25 -08001645 my $uuid = $ra->get_uuid;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001646 my $head = $ra->get_latest_revnum;
Eric Wong28710f72007-02-14 13:32:21 -08001647 my $base = defined $fetch ? $head : 0;
Eric Wonge5181922007-02-08 12:53:57 -08001648
1649 # read the max revs for wildcard expansion (branches/*, tags/*)
1650 foreach my $t (qw/branches tags/) {
1651 defined $remote->{$t} or next;
Marc Branchaud62244062009-06-23 13:02:08 -04001652 push @globs, @{$remote->{$t}};
1653
Eric Wong93f26892007-02-11 01:20:26 -08001654 my $max_rev = eval { tmp_config(qw/--int --get/,
1655 "svn-remote.$repo_id.${t}-maxRev") };
1656 if (defined $max_rev && ($max_rev < $base)) {
1657 $base = $max_rev;
Eric Wongd6d33462007-02-16 04:05:33 -08001658 } elsif (!defined $max_rev) {
1659 $base = 0;
Eric Wonge5181922007-02-08 12:53:57 -08001660 }
1661 }
1662
Eric Wongdb03cd22007-02-13 00:38:02 -08001663 if ($fetch) {
1664 foreach my $p (sort keys %$fetch) {
1665 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
Eric Wong060610c2007-12-08 23:27:41 -08001666 my $lr = $gs->rev_map_max;
Eric Wongdb03cd22007-02-13 00:38:02 -08001667 if (defined $lr) {
1668 $base = $lr if ($lr < $base);
1669 }
1670 push @gs, $gs;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001671 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08001672 }
Eric Wonge98671e2007-02-14 02:21:19 -08001673
1674 ($base, $head) = parse_revision_argument($base, $head);
Eric Wonge5181922007-02-08 12:53:57 -08001675 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001676}
1677
Eric Wong47e39c52007-01-21 04:27:09 -08001678sub read_all_remotes {
1679 my $r = {};
João Abecasis63c56022008-07-14 16:28:04 +01001680 my $use_svm_props = eval { command_oneline(qw/config --bool
1681 svn.useSvmProps/) };
1682 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
Eric Wong8b8fc062007-01-22 11:44:57 -08001683 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
Avery Pennarun61192162008-07-30 16:53:55 -04001684 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*(.+)\s*$!) {
1685 my ($remote, $local_ref, $_remote_ref) = ($1, $2, $3);
1686 die("svn-remote.$remote: remote ref '$_remote_ref' "
1687 . "must start with 'refs/remotes/'\n")
1688 unless $_remote_ref =~ m{^refs/remotes/(.+)};
1689 my $remote_ref = $1;
Eric Wong46cf98b2007-07-14 12:40:32 -07001690 $local_ref =~ s{^/}{};
1691 $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
João Abecasis63c56022008-07-14 16:28:04 +01001692 $r->{$remote}->{svm} = {} if $use_svm_props;
1693 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
1694 $r->{$1}->{svm} = {};
Eric Wong47e39c52007-01-21 04:27:09 -08001695 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1696 $r->{$1}->{url} = $2;
Eric Wong4bb9ed02007-02-03 13:29:17 -08001697 } elsif (m!^(.+)\.(branches|tags)=
1698 (.*):refs/remotes/(.+)\s*$/!x) {
1699 my ($p, $g) = ($3, $4);
Marc Branchaud62244062009-06-23 13:02:08 -04001700 my $rs = {
1701 t => $2,
1702 remote => $1,
1703 path => Git::SVN::GlobSpec->new($p),
1704 ref => Git::SVN::GlobSpec->new($g) };
Eric Wong4bb9ed02007-02-03 13:29:17 -08001705 if (length($rs->{ref}->{right}) != 0) {
1706 die "The '*' glob character must be the last ",
1707 "character of '$g'\n";
1708 }
Marc Branchaud62244062009-06-23 13:02:08 -04001709 push @{ $r->{$1}->{$2} }, $rs;
Eric Wong47e39c52007-01-21 04:27:09 -08001710 }
1711 }
João Abecasis63c56022008-07-14 16:28:04 +01001712
1713 map {
1714 if (defined $r->{$_}->{svm}) {
1715 my $svm;
1716 eval {
1717 my $section = "svn-remote.$_";
1718 $svm = {
1719 source => tmp_config('--get',
1720 "$section.svm-source"),
1721 replace => tmp_config('--get',
1722 "$section.svm-replace"),
1723 }
1724 };
1725 $r->{$_}->{svm} = $svm;
1726 }
1727 } keys %$r;
1728
Eric Wong47e39c52007-01-21 04:27:09 -08001729 $r;
1730}
1731
Eric Wongecc712d2007-01-31 12:28:10 -08001732sub init_vars {
Karl Hasselström94bc9142008-02-03 17:56:18 +01001733 $_gc_nr = $_gc_period = 1000;
Karl Hasselströmaf788a62008-02-03 17:56:12 +01001734 if (defined $_repack || defined $_repack_flags) {
1735 warn "Repack options are obsolete; they have no effect.\n";
1736 }
Eric Wongecc712d2007-01-31 12:28:10 -08001737}
1738
Eric Wongb805b442007-01-22 13:52:04 -08001739sub verify_remotes_sanity {
Eric Wong536c4b02007-01-23 11:35:53 -08001740 return unless -d $ENV{GIT_DIR};
Eric Wongb805b442007-01-22 13:52:04 -08001741 my %seen;
1742 foreach (command(qw/config -l/)) {
1743 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1744 if ($seen{$1}) {
1745 die "Remote ref refs/remote/$1 is tracked by",
1746 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1747 "Please resolve this ambiguity in ",
1748 "your git configuration file before ",
1749 "continuing\n";
1750 }
1751 $seen{$1} = $_;
1752 }
1753 }
1754}
1755
Eric Wonge6434f82007-01-23 16:29:23 -08001756sub find_existing_remote {
1757 my ($url, $remotes) = @_;
Eric Wongbefc9ad2007-02-17 02:53:07 -08001758 return undef if $no_reuse_existing;
Eric Wonge6434f82007-01-23 16:29:23 -08001759 my $existing;
1760 foreach my $repo_id (keys %$remotes) {
1761 my $u = $remotes->{$repo_id}->{url} or next;
1762 next if $u ne $url;
1763 $existing = $repo_id;
1764 last;
1765 }
1766 $existing;
1767}
1768
1769sub init_remote_config {
Eric Wongd8115c52007-02-01 03:30:31 -08001770 my ($self, $url, $no_write) = @_;
Eric Wonge6434f82007-01-23 16:29:23 -08001771 $url =~ s!/+$!!; # strip trailing slash
1772 my $r = read_all_remotes();
1773 my $existing = find_existing_remote($url, $r);
1774 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001775 unless ($no_write) {
1776 print STDERR "Using existing ",
1777 "[svn-remote \"$existing\"]\n";
1778 }
Eric Wonge6434f82007-01-23 16:29:23 -08001779 $self->{repo_id} = $existing;
Eric Wong4a1bb4c2007-05-13 09:58:14 -07001780 } elsif ($_minimize_url) {
Eric Wonge6434f82007-01-23 16:29:23 -08001781 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
1782 $existing = find_existing_remote($min_url, $r);
1783 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001784 unless ($no_write) {
1785 print STDERR "Using existing ",
1786 "[svn-remote \"$existing\"]\n";
1787 }
Eric Wonge6434f82007-01-23 16:29:23 -08001788 $self->{repo_id} = $existing;
1789 }
1790 if ($min_url ne $url) {
Eric Wonge5181922007-02-08 12:53:57 -08001791 unless ($no_write) {
1792 print STDERR "Using higher level of URL: ",
1793 "$url => $min_url\n";
1794 }
Eric Wonge6434f82007-01-23 16:29:23 -08001795 my $old_path = $self->{path};
1796 $self->{path} = $url;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001797 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
Eric Wonge6434f82007-01-23 16:29:23 -08001798 if (length $old_path) {
1799 $self->{path} .= "/$old_path";
1800 }
1801 $url = $min_url;
1802 }
1803 }
1804 my $orig_url;
1805 if (!$existing) {
1806 # verify that we aren't overwriting anything:
1807 $orig_url = eval {
1808 command_oneline('config', '--get',
1809 "svn-remote.$self->{repo_id}.url")
1810 };
1811 if ($orig_url && ($orig_url ne $url)) {
1812 die "svn-remote.$self->{repo_id}.url already set: ",
1813 "$orig_url\nwanted to set to: $url\n";
1814 }
1815 }
1816 my ($xrepo_id, $xpath) = find_ref($self->refname);
1817 if (defined $xpath) {
1818 die "svn-remote.$xrepo_id.fetch already set to track ",
1819 "$xpath:refs/remotes/", $self->refname, "\n";
1820 }
Eric Wongd8115c52007-02-01 03:30:31 -08001821 unless ($no_write) {
1822 command_noisy('config',
1823 "svn-remote.$self->{repo_id}.url", $url);
Eric Wong46cf98b2007-07-14 12:40:32 -07001824 $self->{path} =~ s{^/}{};
Eric Wongd8115c52007-02-01 03:30:31 -08001825 command_noisy('config', '--add',
1826 "svn-remote.$self->{repo_id}.fetch",
1827 "$self->{path}:".$self->refname);
1828 }
Eric Wonge6434f82007-01-23 16:29:23 -08001829 $self->{url} = $url;
1830}
1831
Eric Wonga8ae2622007-02-13 14:22:11 -08001832sub find_by_url { # repos_root and, path are optional
1833 my ($class, $full_url, $repos_root, $path) = @_;
Adam Roben56973d22007-04-25 12:42:58 -07001834
Eric Wong1a97a502007-02-20 00:43:19 -08001835 return undef unless defined $full_url;
Adam Roben56973d22007-04-25 12:42:58 -07001836 remove_username($full_url);
1837 remove_username($repos_root) if defined $repos_root;
Eric Wonga8ae2622007-02-13 14:22:11 -08001838 my $remotes = read_all_remotes();
1839 if (defined $full_url && defined $repos_root && !defined $path) {
1840 $path = $full_url;
1841 $path =~ s#^\Q$repos_root\E(?:/|$)##;
1842 }
1843 foreach my $repo_id (keys %$remotes) {
1844 my $u = $remotes->{$repo_id}->{url} or next;
Adam Roben56973d22007-04-25 12:42:58 -07001845 remove_username($u);
Eric Wonga8ae2622007-02-13 14:22:11 -08001846 next if defined $repos_root && $repos_root ne $u;
1847
1848 my $fetch = $remotes->{$repo_id}->{fetch} || {};
Marc Branchaud62244062009-06-23 13:02:08 -04001849 foreach my $t (qw/branches tags/) {
1850 foreach my $globspec (@{$remotes->{$repo_id}->{$t}}) {
1851 resolve_local_globs($u, $fetch, $globspec);
1852 }
Eric Wonga8ae2622007-02-13 14:22:11 -08001853 }
1854 my $p = $path;
John Goerzen0bb91d92008-03-08 16:04:05 -06001855 my $rwr = rewrite_root({repo_id => $repo_id});
João Abecasis63c56022008-07-14 16:28:04 +01001856 my $svm = $remotes->{$repo_id}->{svm}
1857 if defined $remotes->{$repo_id}->{svm};
Eric Wonga8ae2622007-02-13 14:22:11 -08001858 unless (defined $p) {
1859 $p = $full_url;
John Goerzen0bb91d92008-03-08 16:04:05 -06001860 my $z = $u;
João Abecasis63c56022008-07-14 16:28:04 +01001861 my $prefix = '';
John Goerzen0bb91d92008-03-08 16:04:05 -06001862 if ($rwr) {
1863 $z = $rwr;
Dévai Tamás1b7e5432009-02-12 00:14:02 +01001864 remove_username($z);
João Abecasis63c56022008-07-14 16:28:04 +01001865 } elsif (defined $svm) {
1866 $z = $svm->{source};
1867 $prefix = $svm->{replace};
1868 $prefix =~ s#^\Q$u\E(?:/|$)##;
1869 $prefix =~ s#/$##;
John Goerzen0bb91d92008-03-08 16:04:05 -06001870 }
João Abecasis63c56022008-07-14 16:28:04 +01001871 $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next;
Eric Wonga8ae2622007-02-13 14:22:11 -08001872 }
1873 foreach my $f (keys %$fetch) {
1874 next if $f ne $p;
1875 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
1876 }
1877 }
1878 undef;
1879}
1880
Eric Wong9b981fc2007-01-11 12:14:21 -08001881sub init {
Eric Wongd8115c52007-02-01 03:30:31 -08001882 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08001883 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong9b981fc2007-01-11 12:14:21 -08001884 if (defined $url) {
Eric Wongd8115c52007-02-01 03:30:31 -08001885 $self->init_remote_config($url, $no_write);
Eric Wong9b981fc2007-01-11 12:14:21 -08001886 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001887 $self;
1888}
1889
Eric Wong706587f2007-01-18 17:50:01 -08001890sub find_ref {
1891 my ($ref_id) = @_;
1892 foreach (command(qw/config -l/)) {
1893 next unless m!^svn-remote\.(.+)\.fetch=
1894 \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x;
1895 my ($repo_id, $path, $ref) = ($1, $2, $3);
1896 if ($ref eq $ref_id) {
1897 $path = '' if ($path =~ m#^\./?#);
1898 return ($repo_id, $path);
1899 }
1900 }
1901 (undef, undef, undef);
1902}
1903
Eric Wong9b981fc2007-01-11 12:14:21 -08001904sub new {
Eric Wong706587f2007-01-18 17:50:01 -08001905 my ($class, $ref_id, $repo_id, $path) = @_;
1906 if (defined $ref_id && !defined $repo_id && !defined $path) {
1907 ($repo_id, $path) = find_ref($ref_id);
1908 if (!defined $repo_id) {
1909 die "Could not find a \"svn-remote.*.fetch\" key ",
1910 "in the repository configuration matching: ",
1911 "refs/remotes/$ref_id\n";
1912 }
1913 }
1914 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong8b8fc062007-01-22 11:44:57 -08001915 if (!defined $self->{path} || !length $self->{path}) {
1916 my $fetch = command_oneline('config', '--get',
1917 "svn-remote.$repo_id.fetch",
1918 ":refs/remotes/$ref_id\$") or
1919 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1920 "\":refs/remotes/$ref_id\$\" in config\n";
1921 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
1922 }
Eric Wong706587f2007-01-18 17:50:01 -08001923 $self->{url} = command_oneline('config', '--get',
1924 "svn-remote.$repo_id.url") or
1925 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
Eric Wongd6d33462007-02-16 04:05:33 -08001926 $self->rebuild;
Eric Wong9b981fc2007-01-11 12:14:21 -08001927 $self;
1928}
1929
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001930sub refname {
1931 my ($refname) = "refs/remotes/$_[0]->{ref_id}" ;
1932
1933 # It cannot end with a slash /, we'll throw up on this because
1934 # SVN can't have directories with a slash in their name, either:
1935 if ($refname =~ m{/$}) {
1936 die "ref: '$refname' ends with a trailing slash, this is ",
1937 "not permitted by git nor Subversion\n";
1938 }
1939
1940 # It cannot have ASCII control character space, tilde ~, caret ^,
1941 # colon :, question-mark ?, asterisk *, space, or open bracket [
1942 # anywhere.
1943 #
1944 # Additionally, % must be escaped because it is used for escaping
1945 # and we want our escaped refname to be reversible
1946 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
1947
1948 # no slash-separated component can begin with a dot .
1949 # /.* becomes /%2E*
1950 $refname =~ s{/\.}{/%2E}g;
1951
1952 # It cannot have two consecutive dots .. anywhere
1953 # .. becomes %2E%2E
1954 $refname =~ s{\.\.}{%2E%2E}g;
1955
1956 return $refname;
1957}
1958
1959sub desanitize_refname {
1960 my ($refname) = @_;
1961 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
1962 return $refname;
1963}
Eric Wong9b981fc2007-01-11 12:14:21 -08001964
Eric Wong26a62d52007-02-12 13:25:25 -08001965sub svm_uuid {
1966 my ($self) = @_;
1967 return $self->{svm}->{uuid} if $self->svm;
1968 $self->ra;
1969 unless ($self->{svm}) {
1970 die "SVM UUID not cached, and reading remotely failed\n";
1971 }
1972 $self->{svm}->{uuid};
1973}
Eric Wong8a49ee92007-02-10 20:46:50 -08001974
Eric Wong26a62d52007-02-12 13:25:25 -08001975sub svm {
1976 my ($self) = @_;
1977 return $self->{svm} if $self->{svm};
1978 my $svm;
Eric Wong8a49ee92007-02-10 20:46:50 -08001979 # see if we have it in our config, first:
1980 eval {
Eric Wong26a62d52007-02-12 13:25:25 -08001981 my $section = "svn-remote.$self->{repo_id}";
1982 $svm = {
Eric Wong93f26892007-02-11 01:20:26 -08001983 source => tmp_config('--get', "$section.svm-source"),
1984 uuid => tmp_config('--get', "$section.svm-uuid"),
Eric Wongbefc9ad2007-02-17 02:53:07 -08001985 replace => tmp_config('--get', "$section.svm-replace"),
Eric Wong8a49ee92007-02-10 20:46:50 -08001986 }
1987 };
Eric Wongbefc9ad2007-02-17 02:53:07 -08001988 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
1989 $self->{svm} = $svm;
1990 }
Eric Wong26a62d52007-02-12 13:25:25 -08001991 $self->{svm};
1992}
1993
1994sub _set_svm_vars {
1995 my ($self, $ra) = @_;
Eric Wongdb03cd22007-02-13 00:38:02 -08001996 return $ra if $self->svm;
Eric Wong26a62d52007-02-12 13:25:25 -08001997
Eric Wongdb03cd22007-02-13 00:38:02 -08001998 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08001999 "(svm:source, svm:uuid) ",
Eric Wongdb03cd22007-02-13 00:38:02 -08002000 "from the following URLs:\n" );
2001 sub read_svm_props {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002002 my ($self, $ra, $path, $r) = @_;
2003 my $props = ($ra->get_dir($path, $r))[2];
Eric Wongdb03cd22007-02-13 00:38:02 -08002004 my $src = $props->{'svm:source'};
Eric Wong8a49ee92007-02-10 20:46:50 -08002005 my $uuid = $props->{'svm:uuid'};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002006 return undef if (!$src || !$uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08002007
Eric Wongbefc9ad2007-02-17 02:53:07 -08002008 chomp($src, $uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08002009
Eric Wong8a49ee92007-02-10 20:46:50 -08002010 $uuid =~ m{^[0-9a-f\-]{30,}$}
2011 or die "doesn't look right - svm:uuid is '$uuid'\n";
Eric Wongbefc9ad2007-02-17 02:53:07 -08002012
2013 # the '!' is used to mark the repos_root!/relative/path
2014 $src =~ s{/?!/?}{/};
Eric Wongdb03cd22007-02-13 00:38:02 -08002015 $src =~ s{/+$}{}; # no trailing slashes please
Eric Wongbefc9ad2007-02-17 02:53:07 -08002016 # username is of no interest
Eric Wongdb03cd22007-02-13 00:38:02 -08002017 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
Eric Wong8a49ee92007-02-10 20:46:50 -08002018
Eric Wongbefc9ad2007-02-17 02:53:07 -08002019 my $replace = $ra->{url};
2020 $replace .= "/$path" if length $path;
2021
Eric Wongdb03cd22007-02-13 00:38:02 -08002022 my $section = "svn-remote.$self->{repo_id}";
Eric Wongbefc9ad2007-02-17 02:53:07 -08002023 tmp_config("$section.svm-source", $src);
2024 tmp_config("$section.svm-replace", $replace);
2025 tmp_config("$section.svm-uuid", $uuid);
2026 $self->{svm} = {
2027 source => $src,
2028 uuid => $uuid,
2029 replace => $replace
2030 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002031 }
Eric Wongdb03cd22007-02-13 00:38:02 -08002032
2033 my $r = $ra->get_latest_revnum;
2034 my $path = $self->{path};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002035 my %tried;
Eric Wongdb03cd22007-02-13 00:38:02 -08002036 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002037 unless ($tried{"$self->{url}/$path"}) {
2038 return $ra if $self->read_svm_props($ra, $path, $r);
2039 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002040 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002041 $path =~ s#/?[^/]+$##;
Eric Wong8a49ee92007-02-10 20:46:50 -08002042 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002043 die "Path: '$path' should be ''\n" if $path ne '';
2044 return $ra if $self->read_svm_props($ra, $path, $r);
2045 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002046
2047 if ($ra->{repos_root} eq $self->{url}) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002048 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002049 }
2050
2051 # nope, make sure we're connected to the repository root:
2052 my $ok;
2053 my @tried_b;
2054 $path = $ra->{svn_path};
Eric Wongdb03cd22007-02-13 00:38:02 -08002055 $ra = Git::SVN::Ra->new($ra->{repos_root});
2056 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002057 unless ($tried{"$ra->{url}/$path"}) {
2058 $ok = $self->read_svm_props($ra, $path, $r);
2059 last if $ok;
2060 $tried{"$ra->{url}/$path"} = 1;
2061 }
2062 $path =~ s#/?[^/]+$##;
Eric Wongdb03cd22007-02-13 00:38:02 -08002063 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002064 die "Path: '$path' should be ''\n" if $path ne '';
2065 $ok ||= $self->read_svm_props($ra, $path, $r);
2066 $tried{"$ra->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002067 if (!$ok) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002068 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002069 }
2070 Git::SVN::Ra->new($self->{url});
Eric Wong8a49ee92007-02-10 20:46:50 -08002071}
2072
Eric Wong62e349d2007-02-16 19:57:29 -08002073sub svnsync {
2074 my ($self) = @_;
2075 return $self->{svnsync} if $self->{svnsync};
2076
2077 if ($self->no_metadata) {
2078 die "Can't have both 'noMetadata' and ",
2079 "'useSvnsyncProps' options set!\n";
2080 }
2081 if ($self->rewrite_root) {
2082 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
2083 "options set!\n";
2084 }
2085
2086 my $svnsync;
2087 # see if we have it in our config, first:
2088 eval {
2089 my $section = "svn-remote.$self->{repo_id}";
Eric Wong98fa5b62008-01-11 23:13:55 -08002090
2091 my $url = tmp_config('--get', "$section.svnsync-url");
2092 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
2093 die "doesn't look right - svn:sync-from-url is '$url'\n";
2094
2095 my $uuid = tmp_config('--get', "$section.svnsync-uuid");
2096 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
2097 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2098
2099 $svnsync = { url => $url, uuid => $uuid }
Eric Wong62e349d2007-02-16 19:57:29 -08002100 };
2101 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
2102 return $self->{svnsync} = $svnsync;
2103 }
2104
2105 my $err = "useSvnsyncProps set, but failed to read " .
2106 "svnsync property: svn:sync-from-";
2107 my $rp = $self->ra->rev_proplist(0);
2108
2109 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
Eric Wong98fa5b62008-01-11 23:13:55 -08002110 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
Eric Wong62e349d2007-02-16 19:57:29 -08002111 die "doesn't look right - svn:sync-from-url is '$url'\n";
2112
2113 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
Eric Wong98fa5b62008-01-11 23:13:55 -08002114 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
Eric Wong62e349d2007-02-16 19:57:29 -08002115 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2116
2117 my $section = "svn-remote.$self->{repo_id}";
2118 tmp_config('--add', "$section.svnsync-uuid", $uuid);
2119 tmp_config('--add', "$section.svnsync-url", $url);
2120 return $self->{svnsync} = { url => $url, uuid => $uuid };
2121}
2122
Eric Wong26a62d52007-02-12 13:25:25 -08002123# this allows us to memoize our SVN::Ra UUID locally and avoid a
2124# remote lookup (useful for 'git svn log').
2125sub ra_uuid {
2126 my ($self) = @_;
2127 unless ($self->{ra_uuid}) {
2128 my $key = "svn-remote.$self->{repo_id}.uuid";
2129 my $uuid = eval { tmp_config('--get', $key) };
2130 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
2131 $self->{ra_uuid} = $uuid;
2132 } else {
2133 die "ra_uuid called without URL\n" unless $self->{url};
2134 $self->{ra_uuid} = $self->ra->get_uuid;
2135 tmp_config('--add', $key, $self->{ra_uuid});
2136 }
2137 }
2138 $self->{ra_uuid};
2139}
2140
Eric Wonga5460eb2007-11-21 18:20:57 -08002141sub _set_repos_root {
2142 my ($self, $repos_root) = @_;
2143 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2144 $repos_root ||= $self->ra->{repos_root};
2145 tmp_config($k, $repos_root);
2146 $repos_root;
2147}
2148
2149sub repos_root {
2150 my ($self) = @_;
2151 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2152 eval { tmp_config('--get', $k) } || $self->_set_repos_root;
2153}
2154
Eric Wong9b981fc2007-01-11 12:14:21 -08002155sub ra {
2156 my ($self) = shift;
Eric Wong8a49ee92007-02-10 20:46:50 -08002157 my $ra = Git::SVN::Ra->new($self->{url});
Eric Wonga5460eb2007-11-21 18:20:57 -08002158 $self->_set_repos_root($ra->{repos_root});
Eric Wong91b03282007-02-11 00:51:33 -08002159 if ($self->use_svm_props && !$self->{svm}) {
2160 if ($self->no_metadata) {
Eric Wong97ae0912007-02-11 15:21:24 -08002161 die "Can't have both 'noMetadata' and ",
2162 "'useSvmProps' options set!\n";
Eric Wong62e349d2007-02-16 19:57:29 -08002163 } elsif ($self->use_svnsync_props) {
2164 die "Can't have both 'useSvnsyncProps' and ",
2165 "'useSvmProps' options set!\n";
Eric Wong91b03282007-02-11 00:51:33 -08002166 }
Eric Wong26a62d52007-02-12 13:25:25 -08002167 $ra = $self->_set_svm_vars($ra);
Eric Wong8a49ee92007-02-10 20:46:50 -08002168 $self->{-want_revprops} = 1;
2169 }
2170 $ra;
Eric Wong9b981fc2007-01-11 12:14:21 -08002171}
2172
Eric Wong15710b62007-01-22 02:20:33 -08002173sub rel_path {
2174 my ($self) = @_;
2175 my $repos_root = $self->ra->{repos_root};
2176 return $self->{path} if ($self->{url} eq $repos_root);
Eric Wong0b594512007-03-25 16:35:31 -07002177 my $url = $self->{url} .
2178 (length $self->{path} ? "/$self->{path}" : $self->{path});
2179 $url =~ s!^\Q$repos_root\E(?:/+|$)!!g;
2180 $url;
Eric Wong15710b62007-01-22 02:20:33 -08002181}
2182
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002183# prop_walk(PATH, REV, SUB)
2184# -------------------------
2185# Recursively traverse PATH at revision REV and invoke SUB for each
2186# directory that contains a SVN property. SUB will be invoked as
2187# follows: &SUB(gs, path, props); where `gs' is this instance of
2188# Git::SVN, `path' the path to the directory where the properties
2189# `props' were found. The `path' will be relative to point of checkout,
2190# that is, if url://repo/trunk is the current Git branch, and that
2191# directory contains a sub-directory `d', SUB will be invoked with `/d/'
2192# as `path' (note the trailing `/').
2193sub prop_walk {
2194 my ($self, $path, $rev, $sub) = @_;
2195
Kevin Ballard35cda062008-01-09 01:37:20 -05002196 $path =~ s#^/##;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002197 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
2198 $path =~ s#^/*#/#g;
Eric Wong9b981fc2007-01-11 12:14:21 -08002199 my $p = $path;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002200 # Strip the irrelevant part of the path.
2201 $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
2202 # Ensure the path is terminated by a `/'.
2203 $p =~ s#/*$#/#;
2204
2205 # The properties contain all the internal SVN stuff nobody
2206 # (usually) cares about.
2207 my $interesting_props = 0;
2208 foreach (keys %{$props}) {
2209 # If it doesn't start with `svn:', it must be a
2210 # user-defined property.
2211 ++$interesting_props and next if $_ !~ /^svn:/;
2212 # FIXME: Fragile, if SVN adds new public properties,
2213 # this needs to be updated.
2214 ++$interesting_props if /^svn:(?:ignore|keywords|executable
2215 |eol-style|mime-type
2216 |externals|needs-lock)$/x;
Eric Wong9b981fc2007-01-11 12:14:21 -08002217 }
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002218 &$sub($self, $p, $props) if $interesting_props;
2219
Eric Wong9b981fc2007-01-11 12:14:21 -08002220 foreach (sort keys %$dirent) {
Eric Wong0dc03d62007-05-13 01:04:43 -07002221 next if $dirent->{$_}->{kind} != $SVN::Node::dir;
Christian Engwerb7166cc2008-05-27 08:46:55 +00002222 $self->prop_walk($self->{path} . $p . $_, $rev, $sub);
Eric Wong9b981fc2007-01-11 12:14:21 -08002223 }
2224}
2225
Eric Wong3ebe8df2007-01-25 17:35:40 -08002226sub last_rev { ($_[0]->last_rev_commit)[0] }
2227sub last_commit { ($_[0]->last_rev_commit)[1] }
2228
Eric Wong9b981fc2007-01-11 12:14:21 -08002229# returns the newest SVN revision number and newest commit SHA1
2230sub last_rev_commit {
2231 my ($self) = @_;
2232 if (defined $self->{last_rev} && defined $self->{last_commit}) {
2233 return ($self->{last_rev}, $self->{last_commit});
2234 }
Eric Wongd2866f92007-01-11 12:26:16 -08002235 my $c = ::verify_ref($self->refname.'^0');
Eric Wong91b03282007-02-11 00:51:33 -08002236 if ($c && !$self->use_svm_props && !$self->no_metadata) {
Eric Wongd2866f92007-01-11 12:26:16 -08002237 my $rev = (::cmt_metadata($c))[1];
Eric Wong9b981fc2007-01-11 12:14:21 -08002238 if (defined $rev) {
2239 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2240 return ($rev, $c);
2241 }
2242 }
Eric Wong060610c2007-12-08 23:27:41 -08002243 my $map_path = $self->map_path;
2244 unless (-e $map_path) {
Eric Wong26a62d52007-02-12 13:25:25 -08002245 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
2246 return (undef, undef);
2247 }
Eric Wong66ab84b2007-12-08 23:27:42 -08002248 my ($rev, $commit) = $self->rev_map_max(1);
Eric Wong060610c2007-12-08 23:27:41 -08002249 ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
2250 return ($rev, $commit);
Eric Wong9b981fc2007-01-11 12:14:21 -08002251}
2252
Eric Wong3ebe8df2007-01-25 17:35:40 -08002253sub get_fetch_range {
2254 my ($self, $min, $max) = @_;
2255 $max ||= $self->ra->get_latest_revnum;
Eric Wong060610c2007-12-08 23:27:41 -08002256 $min ||= $self->rev_map_max;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002257 (++$min, $max);
Eric Wong9b981fc2007-01-11 12:14:21 -08002258}
2259
Eric Wong8a49ee92007-02-10 20:46:50 -08002260sub tmp_config {
Eric Wong93f26892007-02-11 01:20:26 -08002261 my (@args) = @_;
Eric Wongb7e53482007-02-16 04:09:28 -08002262 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
2263 my $config = "$ENV{GIT_DIR}/svn/.metadata";
Eric Wong38570a42007-06-13 02:37:05 -07002264 if (! -f $config && -f $old_def_config) {
Eric Wongb7e53482007-02-16 04:09:28 -08002265 rename $old_def_config, $config or
2266 die "Failed rename $old_def_config => $config: $!\n";
2267 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002268 my $old_config = $ENV{GIT_CONFIG};
Eric Wong93f26892007-02-11 01:20:26 -08002269 $ENV{GIT_CONFIG} = $config;
Eric Wong8a49ee92007-02-10 20:46:50 -08002270 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002271 my @ret = eval {
2272 unless (-f $config) {
2273 mkfile($config);
2274 open my $fh, '>', $config or
2275 die "Can't open $config: $!\n";
2276 print $fh "; This file is used internally by ",
2277 "git-svn\n" or die
2278 "Couldn't write to $config: $!\n";
2279 print $fh "; You should not have to edit it\n" or
2280 die "Couldn't write to $config: $!\n";
2281 close $fh or die "Couldn't close $config: $!\n";
2282 }
2283 command('config', @args);
2284 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002285 my $err = $@;
2286 if (defined $old_config) {
2287 $ENV{GIT_CONFIG} = $old_config;
2288 } else {
2289 delete $ENV{GIT_CONFIG};
2290 }
2291 die $err if $err;
2292 wantarray ? @ret : $ret[0];
2293}
2294
Eric Wong9b981fc2007-01-11 12:14:21 -08002295sub tmp_index_do {
2296 my ($self, $sub) = @_;
2297 my $old_index = $ENV{GIT_INDEX_FILE};
2298 $ENV{GIT_INDEX_FILE} = $self->{index};
Eric Wong8a49ee92007-02-10 20:46:50 -08002299 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002300 my @ret = eval {
2301 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
2302 mkpath([$dir]) unless -d $dir;
2303 &$sub;
2304 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002305 my $err = $@;
2306 if (defined $old_index) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002307 $ENV{GIT_INDEX_FILE} = $old_index;
2308 } else {
2309 delete $ENV{GIT_INDEX_FILE};
2310 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002311 die $err if $err;
Eric Wong9b981fc2007-01-11 12:14:21 -08002312 wantarray ? @ret : $ret[0];
2313}
2314
2315sub assert_index_clean {
2316 my ($self, $treeish) = @_;
2317
2318 $self->tmp_index_do(sub {
2319 command_noisy('read-tree', $treeish) unless -e $self->{index};
2320 my $x = command_oneline('write-tree');
2321 my ($y) = (command(qw/cat-file commit/, $treeish) =~
2322 /^tree ($::sha1)/mo);
Eric Wonge8d120b2007-02-14 16:29:52 -08002323 return if $y eq $x;
2324
2325 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2326 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2327 command_noisy('read-tree', $treeish);
Eric Wong9b981fc2007-01-11 12:14:21 -08002328 $x = command_oneline('write-tree');
2329 if ($y ne $x) {
2330 ::fatal "trees ($treeish) $y != $x\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02002331 "Something is seriously wrong...";
Eric Wong9b981fc2007-01-11 12:14:21 -08002332 }
2333 });
2334}
2335
2336sub get_commit_parents {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002337 my ($self, $log_entry) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002338 my (%seen, @ret, @tmp);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002339 # legacy support for 'set-tree'; this is only used by set_tree_cb:
2340 if (my $ip = $self->{inject_parents}) {
2341 if (my $commit = delete $ip->{$log_entry->{revision}}) {
2342 push @tmp, $commit;
Eric Wong9b981fc2007-01-11 12:14:21 -08002343 }
2344 }
Eric Wongd2866f92007-01-11 12:26:16 -08002345 if (my $cur = ::verify_ref($self->refname.'^0')) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002346 push @tmp, $cur;
2347 }
Eric Wong733a65a2007-06-13 02:23:28 -07002348 if (my $ipd = $self->{inject_parents_dcommit}) {
2349 if (my $commit = delete $ipd->{$log_entry->{revision}}) {
2350 push @tmp, @$commit;
2351 }
2352 }
Eric Wong44320b92007-01-13 22:35:53 -08002353 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
Eric Wong9b981fc2007-01-11 12:14:21 -08002354 while (my $p = shift @tmp) {
2355 next if $seen{$p};
2356 $seen{$p} = 1;
2357 push @ret, $p;
2358 # MAXPARENT is defined to 16 in commit-tree.c:
2359 last if @ret >= 16;
2360 }
2361 if (@tmp) {
Eric Wong44320b92007-01-13 22:35:53 -08002362 die "r$log_entry->{revision}: No room for parents:\n\t",
Eric Wong9b981fc2007-01-11 12:14:21 -08002363 join("\n\t", @tmp), "\n";
2364 }
2365 @ret;
2366}
2367
Eric Wongaea736c2007-02-16 19:15:21 -08002368sub rewrite_root {
2369 my ($self) = @_;
2370 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
2371 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2372 my $rwr = eval { command_oneline(qw/config --get/, $k) };
2373 if ($rwr) {
2374 $rwr =~ s#/+$##;
2375 if ($rwr !~ m#^[a-z\+]+://#) {
2376 die "$rwr is not a valid URL (key: $k)\n";
2377 }
2378 }
2379 $self->{-rewrite_root} = $rwr;
2380}
2381
2382sub metadata_url {
2383 my ($self) = @_;
2384 ($self->rewrite_root || $self->{url}) .
2385 (length $self->{path} ? '/' . $self->{path} : '');
2386}
2387
Eric Wong706587f2007-01-18 17:50:01 -08002388sub full_url {
Eric Wong9b981fc2007-01-11 12:14:21 -08002389 my ($self) = @_;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002390 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
Eric Wong9b981fc2007-01-11 12:14:21 -08002391}
2392
Eric Wongad948022007-12-15 19:08:22 -08002393
2394sub set_commit_header_env {
2395 my ($log_entry) = @_;
2396 my %env;
2397 foreach my $ned (qw/NAME EMAIL DATE/) {
2398 foreach my $ac (qw/AUTHOR COMMITTER/) {
2399 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2400 }
2401 }
2402
2403 $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
2404 $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
2405 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
2406
2407 $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
2408 ? $log_entry->{commit_name}
2409 : $log_entry->{name};
2410 $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
2411 ? $log_entry->{commit_email}
2412 : $log_entry->{email};
2413 \%env;
2414}
2415
2416sub restore_commit_header_env {
2417 my ($env) = @_;
2418 foreach my $ned (qw/NAME EMAIL DATE/) {
2419 foreach my $ac (qw/AUTHOR COMMITTER/) {
2420 my $k = "GIT_${ac}_${ned}";
2421 if (defined $env->{$k}) {
2422 $ENV{$k} = $env->{$k};
2423 } else {
2424 delete $ENV{$k};
2425 }
2426 }
2427 }
2428}
2429
Karl Hasselström94bc9142008-02-03 17:56:18 +01002430sub gc {
2431 command_noisy('gc', '--auto');
2432};
2433
Eric Wong9b981fc2007-01-11 12:14:21 -08002434sub do_git_commit {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002435 my ($self, $log_entry) = @_;
Eric Wong8a603772007-01-31 02:45:50 -08002436 my $lr = $self->last_rev;
2437 if (defined $lr && $lr >= $log_entry->{revision}) {
2438 die "Last fetched revision of ", $self->refname,
2439 " was r$lr, but we are about to fetch: ",
2440 "r$log_entry->{revision}!\n";
2441 }
Eric Wong060610c2007-12-08 23:27:41 -08002442 if (my $c = $self->rev_map_get($log_entry->{revision})) {
Eric Wong44320b92007-01-13 22:35:53 -08002443 croak "$log_entry->{revision} = $c already exists! ",
Eric Wong9b981fc2007-01-11 12:14:21 -08002444 "Why are we refetching it?\n";
2445 }
Eric Wongad948022007-12-15 19:08:22 -08002446 my $old_env = set_commit_header_env($log_entry);
Eric Wong44320b92007-01-13 22:35:53 -08002447 my $tree = $log_entry->{tree};
Eric Wong9b981fc2007-01-11 12:14:21 -08002448 if (!defined $tree) {
2449 $tree = $self->tmp_index_do(sub {
2450 command_oneline('write-tree') });
2451 }
2452 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2453
Deskin Millere855bfc2008-10-31 00:10:25 -04002454 my @exec = ('git', 'commit-tree', $tree);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002455 foreach ($self->get_commit_parents($log_entry)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002456 push @exec, '-p', $_;
2457 }
2458 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2459 or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07002460 binmode $msg_fh;
2461
2462 # we always get UTF-8 from SVN, but we may want our commits in
2463 # a different encoding.
2464 if (my $enc = Git::config('i18n.commitencoding')) {
2465 require Encode;
2466 Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
2467 }
Eric Wong44320b92007-01-13 22:35:53 -08002468 print $msg_fh $log_entry->{log} or croak $!;
Eric Wongad948022007-12-15 19:08:22 -08002469 restore_commit_header_env($old_env);
Eric Wong91b03282007-02-11 00:51:33 -08002470 unless ($self->no_metadata) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002471 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2472 or croak $!;
Eric Wong9760adc2007-01-31 03:06:56 -08002473 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002474 $msg_fh->flush == 0 or croak $!;
2475 close $msg_fh or croak $!;
2476 chomp(my $commit = do { local $/; <$out_fh> });
2477 close $out_fh or croak $!;
2478 waitpid $pid, 0;
2479 croak $? if $?;
2480 if ($commit !~ /^$::sha1$/o) {
2481 die "Failed to commit, invalid sha1: $commit\n";
2482 }
2483
Eric Wong060610c2007-12-08 23:27:41 -08002484 $self->rev_map_set($log_entry->{revision}, $commit, 1);
Eric Wong9b981fc2007-01-11 12:14:21 -08002485
Eric Wong44320b92007-01-13 22:35:53 -08002486 $self->{last_rev} = $log_entry->{revision};
Eric Wong9b981fc2007-01-11 12:14:21 -08002487 $self->{last_commit} = $commit;
Simon Arlott49750f32009-03-30 19:31:41 +01002488 print "r$log_entry->{revision}" unless $::_q > 1;
Eric Wong8a49ee92007-02-10 20:46:50 -08002489 if (defined $log_entry->{svm_revision}) {
Simon Arlott49750f32009-03-30 19:31:41 +01002490 print " (\@$log_entry->{svm_revision})" unless $::_q > 1;
Eric Wong060610c2007-12-08 23:27:41 -08002491 $self->rev_map_set($log_entry->{svm_revision}, $commit,
Eric Wong26a62d52007-02-12 13:25:25 -08002492 0, $self->svm_uuid);
Eric Wong8a49ee92007-02-10 20:46:50 -08002493 }
Simon Arlott49750f32009-03-30 19:31:41 +01002494 print " = $commit ($self->{ref_id})\n" unless $::_q > 1;
Karl Hasselström94bc9142008-02-03 17:56:18 +01002495 if (--$_gc_nr == 0) {
2496 $_gc_nr = $_gc_period;
2497 gc();
2498 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002499 return $commit;
2500}
2501
Eric Wongfbcc1732007-02-06 18:35:30 -08002502sub match_paths {
2503 my ($self, $paths, $r) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002504 return 1 if $self->{path} eq '';
Eric Wongd542aed2007-02-09 02:19:41 -08002505 if (my $path = $paths->{"/$self->{path}"}) {
2506 return ($path->{action} eq 'D') ? 0 : 1;
2507 }
Michael Lai9162b862009-03-09 11:45:47 -07002508 my $repos_root = $self->ra->{repos_root};
2509 my $extended_path = $self->{url} . '/' . $self->{path};
2510 $extended_path =~ s#^\Q$repos_root\E(/|$)##;
2511 $self->{path_regex} ||= qr/^\/\Q$extended_path\E\//;
Eric Wongfbcc1732007-02-06 18:35:30 -08002512 if (grep /$self->{path_regex}/, keys %$paths) {
2513 return 1;
2514 }
2515 my $c = '';
2516 foreach (split m#/#, $self->{path}) {
2517 $c .= "/$_";
Eric Wong74a81222007-02-10 13:28:50 -08002518 next unless ($paths->{$c} &&
2519 ($paths->{$c}->{action} =~ /^[AR]$/));
Eric Wonge5181922007-02-08 12:53:57 -08002520 if ($self->ra->check_path($self->{path}, $r) ==
2521 $SVN::Node::dir) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002522 return 1;
2523 }
2524 }
2525 return 0;
2526}
2527
Eric Wong15710b62007-01-22 02:20:33 -08002528sub find_parent_branch {
2529 my ($self, $paths, $rev) = @_;
Eric Wong91b03282007-02-11 00:51:33 -08002530 return undef unless $self->follow_parent;
Eric Wonge5a0b242007-01-25 15:44:54 -08002531 unless (defined $paths) {
Eric Wongc7eba712007-01-31 03:45:28 -08002532 my $err_handler = $SVN::Error::handler;
2533 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
Eric Wongd4eff2b2007-01-30 14:04:22 -08002534 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, sub {
2535 $paths =
2536 Git::SVN::Ra::dup_changed_paths($_[0]) });
Eric Wongc7eba712007-01-31 03:45:28 -08002537 $SVN::Error::handler = $err_handler;
Eric Wonge5a0b242007-01-25 15:44:54 -08002538 }
2539 return undef unless defined $paths;
Eric Wong15710b62007-01-22 02:20:33 -08002540
2541 # look for a parent from another branch:
Eric Wong7f578c52007-01-24 02:16:25 -08002542 my @b_path_components = split m#/#, $self->rel_path;
2543 my @a_path_components;
2544 my $i;
2545 while (@b_path_components) {
2546 $i = $paths->{'/'.join('/', @b_path_components)};
Eric Wong74a81222007-02-10 13:28:50 -08002547 last if $i && defined $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002548 unshift(@a_path_components, pop(@b_path_components));
2549 }
Eric Wong74a81222007-02-10 13:28:50 -08002550 return undef unless defined $i && defined $i->{copyfrom_path};
2551 my $branch_from = $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002552 if (@a_path_components) {
2553 print STDERR "branch_from: $branch_from => ";
2554 $branch_from .= '/'.join('/', @a_path_components);
2555 print STDERR $branch_from, "\n";
2556 }
Eric Wong3ebe8df2007-01-25 17:35:40 -08002557 my $r = $i->{copyfrom_rev};
Eric Wong15710b62007-01-22 02:20:33 -08002558 my $repos_root = $self->ra->{repos_root};
2559 my $url = $self->ra->{url};
2560 my $new_url = $repos_root . $branch_from;
2561 print STDERR "Found possible branch point: ",
2562 "$new_url => ", $self->full_url, ", $r\n";
2563 $branch_from =~ s#^/##;
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002564 my $gs = $self->other_gs($new_url, $url, $repos_root,
2565 $branch_from, $r, $self->{ref_id});
Eric Wong15710b62007-01-22 02:20:33 -08002566 my ($r0, $parent) = $gs->find_rev_before($r, 1);
Deskin Miller553589f2008-12-08 08:31:31 -05002567 {
2568 my ($base, $head);
2569 if (!defined $r0 || !defined $parent) {
2570 ($base, $head) = parse_revision_argument(0, $r);
2571 } else {
2572 if ($r0 < $r) {
2573 $gs->ra->get_log([$gs->{path}], $r0 + 1, $r, 1,
2574 0, 1, sub { $base = $_[1] - 1 });
2575 }
2576 }
2577 if (defined $base && $base <= $r) {
Eric Wongd627de62007-04-15 03:01:29 -07002578 $gs->fetch($base, $r);
2579 }
Deskin Miller553589f2008-12-08 08:31:31 -05002580 ($r0, $parent) = $gs->find_rev_before($r, 1);
Eric Wong15710b62007-01-22 02:20:33 -08002581 }
Eric Wongef70de92007-02-01 04:12:41 -08002582 if (defined $r0 && defined $parent) {
Eric Wong15710b62007-01-22 02:20:33 -08002583 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n";
Eric Wong15710b62007-01-22 02:20:33 -08002584 my $ed;
2585 if ($self->ra->can_do_switch) {
Eric Wong2e5e2482007-02-23 02:21:59 -08002586 $self->assert_index_clean($parent);
Eric Wong8b8fc062007-01-22 11:44:57 -08002587 print STDERR "Following parent with do_switch\n";
Eric Wong15710b62007-01-22 02:20:33 -08002588 # do_switch works with svn/trunk >= r22312, but that
Eric Wong2b27f6c2007-01-28 04:59:05 -08002589 # is not included with SVN 1.4.3 (the latest version
Eric Wong15710b62007-01-22 02:20:33 -08002590 # at the moment), so we can't rely on it
Eric Wong83c2fcf2009-02-22 20:25:00 -08002591 $self->{last_rev} = $r0;
Eric Wong15710b62007-01-22 02:20:33 -08002592 $self->{last_commit} = $parent;
Eric Wong8841b372009-02-11 01:56:58 -08002593 $ed = SVN::Git::Fetcher->new($self, $gs->{path});
Eric Wong8a603772007-01-31 02:45:50 -08002594 $gs->ra->gs_do_switch($r0, $rev, $gs,
Eric Wong15710b62007-01-22 02:20:33 -08002595 $self->full_url, $ed)
2596 or die "SVN connection failed somewhere...\n";
Steven Walter9ff74e92007-09-28 13:24:19 -04002597 } elsif ($self->ra->trees_match($new_url, $r0,
2598 $self->full_url, $rev)) {
2599 print STDERR "Trees match:\n",
2600 " $new_url\@$r0\n",
2601 " ${\$self->full_url}\@$rev\n",
2602 "Following parent with no changes\n";
2603 $self->tmp_index_do(sub {
2604 command_noisy('read-tree', $parent);
2605 });
2606 $self->{last_commit} = $parent;
Eric Wong15710b62007-01-22 02:20:33 -08002607 } else {
Eric Wong8b8fc062007-01-22 11:44:57 -08002608 print STDERR "Following parent with do_update\n";
Eric Wong15710b62007-01-22 02:20:33 -08002609 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08002610 $self->ra->gs_do_update($rev, $rev, $self, $ed)
Eric Wong15710b62007-01-22 02:20:33 -08002611 or die "SVN connection failed somewhere...\n";
2612 }
Eric Wongf7c3fc42007-01-29 18:34:55 -08002613 print STDERR "Successfully followed parent\n";
Eric Wong15710b62007-01-22 02:20:33 -08002614 return $self->make_log_entry($rev, [$parent], $ed);
2615 }
Eric Wong15710b62007-01-22 02:20:33 -08002616 return undef;
2617}
2618
Eric Wong9b981fc2007-01-11 12:14:21 -08002619sub do_fetch {
Eric Wong706587f2007-01-18 17:50:01 -08002620 my ($self, $paths, $rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08002621 my $ed;
Eric Wong9b981fc2007-01-11 12:14:21 -08002622 my ($last_rev, @parents);
Eric Wongb9dffd82007-02-09 01:28:30 -08002623 if (my $lc = $self->last_commit) {
2624 # we can have a branch that was deleted, then re-added
2625 # under the same name but copied from another path, in
2626 # which case we'll have multiple parents (we don't
2627 # want to break the original ref, nor lose copypath info):
2628 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2629 push @{$log_entry->{parents}}, $lc;
2630 return $log_entry;
2631 }
Eric Wong15710b62007-01-22 02:20:33 -08002632 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002633 $last_rev = $self->{last_rev};
Eric Wongb9dffd82007-02-09 01:28:30 -08002634 $ed->{c} = $lc;
2635 @parents = ($lc);
Eric Wong9b981fc2007-01-11 12:14:21 -08002636 } else {
2637 $last_rev = $rev;
Eric Wong15710b62007-01-22 02:20:33 -08002638 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2639 return $log_entry;
2640 }
2641 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002642 }
Eric Wong8a603772007-01-31 02:45:50 -08002643 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002644 die "SVN connection failed somewhere...\n";
2645 }
2646 $self->make_log_entry($rev, \@parents, $ed);
2647}
2648
Eric Wong97f69872007-01-25 11:53:13 -08002649sub get_untracked {
2650 my ($self, $ed) = @_;
2651 my @out;
2652 my $h = $ed->{empty};
Eric Wong9b981fc2007-01-11 12:14:21 -08002653 foreach (sort keys %$h) {
2654 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
Eric Wong97f69872007-01-25 11:53:13 -08002655 push @out, " $act: " . uri_encode($_);
Eric Wong9b981fc2007-01-11 12:14:21 -08002656 warn "W: $act: $_\n";
2657 }
2658 foreach my $t (qw/dir_prop file_prop/) {
Eric Wong97f69872007-01-25 11:53:13 -08002659 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002660 foreach my $path (sort keys %$h) {
2661 my $ppath = $path eq '' ? '.' : $path;
2662 foreach my $prop (sort keys %{$h->{$path}}) {
Eric Wong1ce255d2007-01-14 23:21:16 -08002663 next if $SKIP_PROP{$prop};
Eric Wong9b981fc2007-01-11 12:14:21 -08002664 my $v = $h->{$path}->{$prop};
Eric Wong97f69872007-01-25 11:53:13 -08002665 my $t_ppath_prop = "$t: " .
2666 uri_encode($ppath) . ' ' .
2667 uri_encode($prop);
Eric Wong9b981fc2007-01-11 12:14:21 -08002668 if (defined $v) {
Eric Wong97f69872007-01-25 11:53:13 -08002669 push @out, " +$t_ppath_prop " .
2670 uri_encode($v);
Eric Wong9b981fc2007-01-11 12:14:21 -08002671 } else {
Eric Wong97f69872007-01-25 11:53:13 -08002672 push @out, " -$t_ppath_prop";
Eric Wong9b981fc2007-01-11 12:14:21 -08002673 }
2674 }
2675 }
2676 }
2677 foreach my $t (qw/absent_file absent_directory/) {
Eric Wong97f69872007-01-25 11:53:13 -08002678 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002679 foreach my $parent (sort keys %$h) {
2680 foreach my $path (sort @{$h->{$parent}}) {
Eric Wong97f69872007-01-25 11:53:13 -08002681 push @out, " $t: " .
2682 uri_encode("$parent/$path");
Eric Wong9b981fc2007-01-11 12:14:21 -08002683 warn "W: $t: $parent/$path ",
2684 "Insufficient permissions?\n";
2685 }
2686 }
2687 }
Eric Wong97f69872007-01-25 11:53:13 -08002688 \@out;
Eric Wong9b981fc2007-01-11 12:14:21 -08002689}
2690
Pete Harlane82f0d72009-01-17 20:10:14 -08002691# parse_svn_date(DATE)
2692# --------------------
2693# Given a date (in UTC) from Subversion, return a string in the format
2694# "<TZ Offset> <local date/time>" that Git will use.
2695#
2696# By default the parsed date will be in UTC; if $Git::SVN::_localtime
2697# is true we'll convert it to the local timezone instead.
Eric Wong1c8443b2007-01-14 02:17:00 -08002698sub parse_svn_date {
2699 my $date = shift || return '+0000 1970-01-01 00:00:00';
2700 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
Junio C Hamanob94ead72009-02-18 10:48:01 -08002701 (\d\d)\:(\d\d)\:(\d\d)\.\d*Z$/x) or
Eric Wong1c8443b2007-01-14 02:17:00 -08002702 croak "Unable to parse date: $date\n";
Pete Harlane82f0d72009-01-17 20:10:14 -08002703 my $parsed_date; # Set next.
2704
2705 if ($Git::SVN::_localtime) {
2706 # Translate the Subversion datetime to an epoch time.
2707 # Begin by switching ourselves to $date's timezone, UTC.
2708 my $old_env_TZ = $ENV{TZ};
2709 $ENV{TZ} = 'UTC';
2710
2711 my $epoch_in_UTC =
2712 POSIX::strftime('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
2713
2714 # Determine our local timezone (including DST) at the
2715 # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the
2716 # value of TZ, if any, at the time we were run.
2717 if (defined $Git::SVN::Log::TZ) {
2718 $ENV{TZ} = $Git::SVN::Log::TZ;
2719 } else {
2720 delete $ENV{TZ};
2721 }
2722
2723 my $our_TZ =
2724 POSIX::strftime('%Z', $S, $M, $H, $d, $m - 1, $Y - 1900);
2725
2726 # This converts $epoch_in_UTC into our local timezone.
2727 my ($sec, $min, $hour, $mday, $mon, $year,
2728 $wday, $yday, $isdst) = localtime($epoch_in_UTC);
2729
2730 $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
2731 $our_TZ, $year + 1900, $mon + 1,
2732 $mday, $hour, $min, $sec);
2733
2734 # Reset us to the timezone in effect when we entered
2735 # this routine.
2736 if (defined $old_env_TZ) {
2737 $ENV{TZ} = $old_env_TZ;
2738 } else {
2739 delete $ENV{TZ};
2740 }
2741 } else {
2742 $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
2743 }
2744
2745 return $parsed_date;
Eric Wong1c8443b2007-01-14 02:17:00 -08002746}
2747
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002748sub other_gs {
2749 my ($self, $new_url, $url, $repos_root,
2750 $branch_from, $r, $old_ref_id) = @_;
2751 my $gs = Git::SVN->find_by_url($new_url, $repos_root, $branch_from);
2752 unless ($gs) {
2753 my $ref_id = $old_ref_id;
2754 $ref_id =~ s/\@\d+$//;
2755 $ref_id .= "\@$r";
2756 # just grow a tail if we're not unique enough :x
2757 $ref_id .= '-' while find_ref($ref_id);
2758 print STDERR "Initializing parent: $ref_id\n";
2759 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
2760 if ($u =~ s#^\Q$url\E(/|$)##) {
2761 $p = $u;
2762 $u = $url;
2763 $repo_id = $self->{repo_id};
2764 }
2765 $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1);
2766 }
2767 $gs
2768}
2769
Mark Lodato36db1ed2009-05-14 21:27:15 -04002770sub call_authors_prog {
2771 my ($orig_author) = @_;
2772 my $author = `$::_authors_prog $orig_author`;
2773 if ($? != 0) {
2774 die "$::_authors_prog failed with exit code $?\n"
2775 }
2776 if ($author =~ /^\s*(.+?)\s*<(.*)>\s*$/) {
2777 my ($name, $email) = ($1, $2);
2778 $email = undef if length $2 == 0;
2779 return [$name, $email];
2780 } else {
2781 die "Author: $orig_author: $::_authors_prog returned "
2782 . "invalid author format: $author\n";
2783 }
2784}
2785
Eric Wong1c8443b2007-01-14 02:17:00 -08002786sub check_author {
2787 my ($author) = @_;
2788 if (!defined $author || length $author == 0) {
2789 $author = '(no author)';
Mark Lodato36db1ed2009-05-14 21:27:15 -04002790 }
2791 if (!defined $::users{$author}) {
2792 if (defined $::_authors_prog) {
2793 $::users{$author} = call_authors_prog($author);
2794 } elsif (defined $::_authors) {
2795 die "Author: $author not defined in $::_authors file\n";
2796 }
Eric Wong1c8443b2007-01-14 02:17:00 -08002797 }
2798 $author;
2799}
2800
Eric Wong9b981fc2007-01-11 12:14:21 -08002801sub make_log_entry {
Eric Wong97f69872007-01-25 11:53:13 -08002802 my ($self, $rev, $parents, $ed) = @_;
2803 my $untracked = $self->get_untracked($ed);
2804
Eric Wong9b981fc2007-01-11 12:14:21 -08002805 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08002806 print $un "r$rev\n" or croak $!;
2807 print $un $_, "\n" foreach @$untracked;
2808 my %log_entry = ( parents => $parents || [], revision => $rev,
2809 log => '');
Eric Wongfbcc1732007-02-06 18:35:30 -08002810
Eric Wong8a49ee92007-02-10 20:46:50 -08002811 my $headrev;
Eric Wongfbcc1732007-02-06 18:35:30 -08002812 my $logged = delete $self->{logged_rev_props};
Eric Wong8a49ee92007-02-10 20:46:50 -08002813 if (!$logged || $self->{-want_revprops}) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002814 my $rp = $self->ra->rev_proplist($rev);
2815 foreach (sort keys %$rp) {
2816 my $v = $rp->{$_};
2817 if (/^svn:(author|date|log)$/) {
2818 $log_entry{$1} = $v;
Eric Wong8a49ee92007-02-10 20:46:50 -08002819 } elsif ($_ eq 'svm:headrev') {
2820 $headrev = $v;
Eric Wongfbcc1732007-02-06 18:35:30 -08002821 } else {
2822 print $un " rev_prop: ", uri_encode($_), ' ',
2823 uri_encode($v), "\n";
2824 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002825 }
Eric Wongfbcc1732007-02-06 18:35:30 -08002826 } else {
2827 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
Eric Wong9b981fc2007-01-11 12:14:21 -08002828 }
2829 close $un or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08002830
Eric Wong9b981fc2007-01-11 12:14:21 -08002831 $log_entry{date} = parse_svn_date($log_entry{date});
Eric Wong9b981fc2007-01-11 12:14:21 -08002832 $log_entry{log} .= "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002833 my $author = $log_entry{author} = check_author($log_entry{author});
2834 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002835 : ($author, undef);
2836
2837 my ($commit_name, $commit_email) = ($name, $email);
2838 if ($_use_log_author) {
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002839 my $name_field;
2840 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
2841 $name_field = $1;
2842 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
2843 $name_field = $1;
2844 }
2845 if (!defined $name_field) {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02002846 if (!defined $email) {
2847 $email = $name;
2848 }
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002849 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002850 ($name, $email) = ($1, $2);
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002851 } elsif ($name_field =~ /(.*)@/) {
2852 ($name, $email) = ($1, $name_field);
2853 } else {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02002854 ($name, $email) = ($name_field, $name_field);
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002855 }
2856 }
Eric Wong91b03282007-02-11 00:51:33 -08002857 if (defined $headrev && $self->use_svm_props) {
Eric Wongaea736c2007-02-16 19:15:21 -08002858 if ($self->rewrite_root) {
2859 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
2860 "options set!\n";
2861 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002862 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002863 # we don't want "SVM: initializing mirror for junk" ...
2864 return undef if $r == 0;
2865 my $svm = $self->svm;
2866 if ($uuid ne $svm->{uuid}) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002867 die "UUID mismatch on SVM path:\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08002868 "expected: $svm->{uuid}\n",
Eric Wong8a49ee92007-02-10 20:46:50 -08002869 " got: $uuid\n";
2870 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002871 my $full_url = $self->full_url;
2872 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
2873 die "Failed to replace '$svm->{replace}' with ",
2874 "'$svm->{source}' in $full_url\n";
Sam Vilain18ea92b2007-02-23 12:32:29 +13002875 # throw away username for storing in records
2876 remove_username($full_url);
Eric Wong8a49ee92007-02-10 20:46:50 -08002877 $log_entry{metadata} = "$full_url\@$r $uuid";
2878 $log_entry{svm_revision} = $r;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002879 $email ||= "$author\@$uuid";
2880 $commit_email ||= "$author\@$uuid";
Eric Wong62e349d2007-02-16 19:57:29 -08002881 } elsif ($self->use_svnsync_props) {
2882 my $full_url = $self->svnsync->{url};
2883 $full_url .= "/$self->{path}" if length $self->{path};
Adam Robence118732007-04-24 18:02:07 -07002884 remove_username($full_url);
Eric Wong62e349d2007-02-16 19:57:29 -08002885 my $uuid = $self->svnsync->{uuid};
2886 $log_entry{metadata} = "$full_url\@$rev $uuid";
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002887 $email ||= "$author\@$uuid";
2888 $commit_email ||= "$author\@$uuid";
Eric Wong8a49ee92007-02-10 20:46:50 -08002889 } else {
Adam Robence118732007-04-24 18:02:07 -07002890 my $url = $self->metadata_url;
2891 remove_username($url);
2892 $log_entry{metadata} = "$url\@$rev " .
Eric Wong26a62d52007-02-12 13:25:25 -08002893 $self->ra->get_uuid;
Eric Wongdb03cd22007-02-13 00:38:02 -08002894 $email ||= "$author\@" . $self->ra->get_uuid;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002895 $commit_email ||= "$author\@" . $self->ra->get_uuid;
Eric Wong8a49ee92007-02-10 20:46:50 -08002896 }
Eric Wongdb03cd22007-02-13 00:38:02 -08002897 $log_entry{name} = $name;
2898 $log_entry{email} = $email;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002899 $log_entry{commit_name} = $commit_name;
2900 $log_entry{commit_email} = $commit_email;
Eric Wong9b981fc2007-01-11 12:14:21 -08002901 \%log_entry;
2902}
2903
2904sub fetch {
Eric Wong3ebe8df2007-01-25 17:35:40 -08002905 my ($self, $min_rev, $max_rev, @parents) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002906 my ($last_rev, $last_commit) = $self->last_rev_commit;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002907 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
Eric Wonge5181922007-02-08 12:53:57 -08002908 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
Eric Wong9b981fc2007-01-11 12:14:21 -08002909}
2910
2911sub set_tree_cb {
2912 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
Eric Wong490f49e2007-02-10 13:58:33 -08002913 $self->{inject_parents} = { $rev => $tree };
2914 $self->fetch(undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08002915}
2916
2917sub set_tree {
2918 my ($self, $tree) = (shift, shift);
Eric Wong1ce255d2007-01-14 23:21:16 -08002919 my $log_entry = ::get_commit_entry($tree);
Eric Wong9b981fc2007-01-11 12:14:21 -08002920 unless ($self->{last_rev}) {
Luc Heinrich0a1a1c82008-09-29 15:58:18 +02002921 ::fatal("Must have an existing revision to commit");
Eric Wong9b981fc2007-01-11 12:14:21 -08002922 }
Eric Wong61395352007-01-27 14:33:08 -08002923 my %ed_opts = ( r => $self->{last_rev},
2924 log => $log_entry->{log},
2925 ra => $self->ra,
2926 tree_a => $self->{last_commit},
2927 tree_b => $tree,
2928 editor_cb => sub {
2929 $self->set_tree_cb($log_entry, $tree, @_) },
2930 svn_path => $self->{path} );
2931 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002932 print "No changes\nr$self->{last_rev} = $tree\n";
2933 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002934}
2935
Eric Wong060610c2007-12-08 23:27:41 -08002936sub rebuild_from_rev_db {
2937 my ($self, $path) = @_;
2938 my $r = -1;
2939 open my $fh, '<', $path or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02002940 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002941 while (<$fh>) {
2942 length($_) == 41 or croak "inconsistent size in ($_) != 41";
2943 chomp($_);
2944 ++$r;
2945 next if $_ eq ('0' x 40);
2946 $self->rev_map_set($r, $_);
2947 print "r$r = $_\n";
2948 }
2949 close $fh or croak "close: $!";
2950 unlink $path or croak "unlink: $!";
2951}
2952
Eric Wongf0ecca12007-01-30 13:11:14 -08002953sub rebuild {
2954 my ($self) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08002955 my $map_path = $self->map_path;
Deskin Miller2beec892008-09-15 21:12:58 -04002956 my $partial = (-e $map_path && ! -z $map_path);
Eric Wongd6d33462007-02-16 04:05:33 -08002957 return unless ::verify_ref($self->refname.'^0');
Deskin Miller2beec892008-09-15 21:12:58 -04002958 if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
Eric Wong060610c2007-12-08 23:27:41 -08002959 my $rev_db = $self->rev_db_path;
2960 $self->rebuild_from_rev_db($rev_db);
2961 if ($self->use_svm_props) {
2962 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
2963 $self->rebuild_from_rev_db($svm_rev_db);
2964 }
2965 $self->unlink_rev_db_symlink;
Eric Wong26a62d52007-02-12 13:25:25 -08002966 return;
2967 }
Deskin Miller2beec892008-09-15 21:12:58 -04002968 print "Rebuilding $map_path ...\n" if (!$partial);
2969 my ($base_rev, $head) = ($partial ? $self->rev_map_max_norebuild(1) :
2970 (undef, undef));
Eric Wong060610c2007-12-08 23:27:41 -08002971 my ($log, $ctx) =
2972 command_output_pipe(qw/rev-list --pretty=raw --no-color --reverse/,
Deskin Miller2beec892008-09-15 21:12:58 -04002973 ($head ? "$head.." : "") . $self->refname,
2974 '--');
Jan Krüger74b1e122008-06-24 02:17:36 +02002975 my $metadata_url = $self->metadata_url;
2976 remove_username($metadata_url);
Eric Wong060610c2007-12-08 23:27:41 -08002977 my $svn_uuid = $self->ra_uuid;
Sam Vilain3dfab992007-06-30 20:56:13 +12002978 my $c;
2979 while (<$log>) {
2980 if ( m{^commit ($::sha1)$} ) {
2981 $c = $1;
2982 next;
2983 }
2984 next unless s{^\s*(git-svn-id:)}{$1};
2985 my ($url, $rev, $uuid) = ::extract_metadata($_);
Sam Vilain18ea92b2007-02-23 12:32:29 +13002986 remove_username($url);
Eric Wongf0ecca12007-01-30 13:11:14 -08002987
2988 # ignore merges (from set-tree)
2989 next if (!defined $rev || !$uuid);
2990
2991 # if we merged or otherwise started elsewhere, this is
2992 # how we break out of it
Eric Wong060610c2007-12-08 23:27:41 -08002993 if (($uuid ne $svn_uuid) ||
Jan Krüger74b1e122008-06-24 02:17:36 +02002994 ($metadata_url && $url && ($url ne $metadata_url))) {
Eric Wongf0ecca12007-01-30 13:11:14 -08002995 next;
2996 }
Deskin Miller2beec892008-09-15 21:12:58 -04002997 if ($partial && $head) {
2998 print "Partial-rebuilding $map_path ...\n";
2999 print "Currently at $base_rev = $head\n";
3000 $head = undef;
3001 }
Eric Wongf0ecca12007-01-30 13:11:14 -08003002
Eric Wong060610c2007-12-08 23:27:41 -08003003 $self->rev_map_set($rev, $c);
Eric Wongf0ecca12007-01-30 13:11:14 -08003004 print "r$rev = $c\n";
3005 }
Sam Vilain3dfab992007-06-30 20:56:13 +12003006 command_close_pipe($log, $ctx);
Deskin Miller2beec892008-09-15 21:12:58 -04003007 print "Done rebuilding $map_path\n" if (!$partial || !$head);
Eric Wong060610c2007-12-08 23:27:41 -08003008 my $rev_db_path = $self->rev_db_path;
3009 if (-f $self->rev_db_path) {
3010 unlink $self->rev_db_path or croak "unlink: $!";
3011 }
3012 $self->unlink_rev_db_symlink;
Eric Wongf0ecca12007-01-30 13:11:14 -08003013}
3014
Eric Wong060610c2007-12-08 23:27:41 -08003015# rev_map:
Eric Wong9b981fc2007-01-11 12:14:21 -08003016# Tie::File seems to be prone to offset errors if revisions get sparse,
3017# it's not that fast, either. Tie::File is also not in Perl 5.6. So
3018# one of my favorite modules is out :< Next up would be one of the DBM
Eric Wong060610c2007-12-08 23:27:41 -08003019# modules, but I'm not sure which is most portable...
3020#
3021# This is the replacement for the rev_db format, which was too big
3022# and inefficient for large repositories with a lot of sparse history
3023# (mainly tags)
3024#
3025# The format is this:
3026# - 24 bytes for every record,
3027# * 4 bytes for the integer representing an SVN revision number
3028# * 20 bytes representing the sha1 of a git commit
3029# - No empty padding records like the old format
Eric Wong66ab84b2007-12-08 23:27:42 -08003030# (except the last record, which can be overwritten)
Eric Wong060610c2007-12-08 23:27:41 -08003031# - new records are written append-only since SVN revision numbers
3032# increase monotonically
3033# - lookups on SVN revision number are done via a binary search
Eric Wong66ab84b2007-12-08 23:27:42 -08003034# - Piping the file to xxd -c24 is a good way of dumping it for
3035# viewing or editing (piped back through xxd -r), should the need
3036# ever arise.
3037# - The last record can be padding revision with an all-zero sha1
3038# This is used to optimize fetch performance when using multiple
3039# "fetch" directives in .git/config
Eric Wong060610c2007-12-08 23:27:41 -08003040#
Eric Wong97ae0912007-02-11 15:21:24 -08003041# These files are disposable unless noMetadata or useSvmProps is set
Eric Wong9b981fc2007-01-11 12:14:21 -08003042
Eric Wong060610c2007-12-08 23:27:41 -08003043sub _rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08003044 my ($fh, $rev, $commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003045
Michael Weber4f7ec792008-04-18 15:12:04 +02003046 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003047 my $size = (stat($fh))[7];
3048 ($size % 24) == 0 or croak "inconsistent size: $size";
3049
Eric Wong66ab84b2007-12-08 23:27:42 -08003050 my $wr_offset = 0;
Eric Wong060610c2007-12-08 23:27:41 -08003051 if ($size > 0) {
3052 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
3053 my $read = sysread($fh, my $buf, 24) or croak "read: $!";
3054 $read == 24 or croak "read only $read bytes (!= 24)";
3055 my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08003056 if ($last_commit eq ('0' x40)) {
3057 if ($size >= 48) {
3058 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
3059 $read = sysread($fh, $buf, 24) or
3060 croak "read: $!";
3061 $read == 24 or
3062 croak "read only $read bytes (!= 24)";
3063 ($last_rev, $last_commit) =
3064 unpack(rev_map_fmt, $buf);
3065 if ($last_commit eq ('0' x40)) {
3066 croak "inconsistent .rev_map\n";
3067 }
3068 }
3069 if ($last_rev >= $rev) {
3070 croak "last_rev is higher!: $last_rev >= $rev";
3071 }
3072 $wr_offset = -24;
Eric Wong47a0b752007-01-31 05:13:30 -08003073 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003074 }
Eric Wong66ab84b2007-12-08 23:27:42 -08003075 sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003076 syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or
3077 croak "write: $!";
Eric Wong26a62d52007-02-12 13:25:25 -08003078}
3079
Ben Jackson195643f2009-06-03 20:45:52 -07003080sub _rev_map_reset {
3081 my ($fh, $rev, $commit) = @_;
3082 my $c = _rev_map_get($fh, $rev);
3083 $c eq $commit or die "_rev_map_reset(@_) commit $c does not match!\n";
3084 my $offset = sysseek($fh, 0, SEEK_CUR) or croak "seek: $!";
3085 truncate $fh, $offset or croak "truncate: $!";
3086}
3087
Eric Wong26a62d52007-02-12 13:25:25 -08003088sub mkfile {
3089 my ($path) = @_;
3090 unless (-e $path) {
3091 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
3092 mkpath([$dir]) unless -d $dir;
3093 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
3094 close $fh or die "Couldn't close (create) $path: $!\n";
3095 }
3096}
3097
Eric Wong060610c2007-12-08 23:27:41 -08003098sub rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08003099 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
3100 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
Eric Wong060610c2007-12-08 23:27:41 -08003101 my $db = $self->map_path($uuid);
Eric Wong26a62d52007-02-12 13:25:25 -08003102 my $db_lock = "$db.lock";
3103 my $sig;
Ben Jackson195643f2009-06-03 20:45:52 -07003104 $update_ref ||= 0;
Eric Wong26a62d52007-02-12 13:25:25 -08003105 if ($update_ref) {
3106 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
3107 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
3108 }
3109 mkfile($db);
3110
3111 $LOCKFILES{$db_lock} = 1;
3112 my $sync;
3113 # both of these options make our .rev_db file very, very important
3114 # and we can't afford to lose it because rebuild() won't work
3115 if ($self->use_svm_props || $self->no_metadata) {
3116 $sync = 1;
Eric Wong060610c2007-12-08 23:27:41 -08003117 copy($db, $db_lock) or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08003118 "Failed to copy: ",
3119 "$db => $db_lock ($!)\n";
3120 } else {
Eric Wong060610c2007-12-08 23:27:41 -08003121 rename $db, $db_lock or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08003122 "Failed to rename: ",
3123 "$db => $db_lock ($!)\n";
3124 }
Eric Wong060610c2007-12-08 23:27:41 -08003125
Eric Wong66ab84b2007-12-08 23:27:42 -08003126 sysopen(my $fh, $db_lock, O_RDWR | O_CREAT)
Eric Wong060610c2007-12-08 23:27:41 -08003127 or croak "Couldn't open $db_lock: $!\n";
Ben Jackson195643f2009-06-03 20:45:52 -07003128 $update_ref eq 'reset' ? _rev_map_reset($fh, $rev, $commit) :
3129 _rev_map_set($fh, $rev, $commit);
Eric Wong97ae0912007-02-11 15:21:24 -08003130 if ($sync) {
3131 $fh->flush or die "Couldn't flush $db_lock: $!\n";
3132 $fh->sync or die "Couldn't sync $db_lock: $!\n";
3133 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003134 close $fh or croak $!;
Eric Wong373274f2007-01-31 13:54:23 -08003135 if ($update_ref) {
Eric Wong1e889ef2007-02-16 01:45:13 -08003136 $_head = $self;
Ben Jackson195643f2009-06-03 20:45:52 -07003137 my $note = "";
3138 $note = " ($update_ref)" if ($update_ref !~ /^\d*$/);
3139 command_noisy('update-ref', '-m', "r$rev$note",
Eric Wong373274f2007-01-31 13:54:23 -08003140 $self->refname, $commit);
3141 }
Eric Wong060610c2007-12-08 23:27:41 -08003142 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
Eric Wong373274f2007-01-31 13:54:23 -08003143 "$db_lock => $db ($!)\n";
3144 delete $LOCKFILES{$db_lock};
3145 if ($update_ref) {
3146 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
3147 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
3148 kill $sig, $$ if defined $sig;
3149 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003150}
3151
Eric Wong66ab84b2007-12-08 23:27:42 -08003152# If want_commit, this will return an array of (rev, commit) where
3153# commit _must_ be a valid commit in the archive.
3154# Otherwise, it'll return the max revision (whether or not the
3155# commit is valid or just a 0x40 placeholder).
Eric Wong060610c2007-12-08 23:27:41 -08003156sub rev_map_max {
Eric Wong66ab84b2007-12-08 23:27:42 -08003157 my ($self, $want_commit) = @_;
Eric Wongd6d33462007-02-16 04:05:33 -08003158 $self->rebuild;
Deskin Miller2beec892008-09-15 21:12:58 -04003159 my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
3160 $want_commit ? ($r, $c) : $r;
3161}
3162
3163sub rev_map_max_norebuild {
3164 my ($self, $want_commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003165 my $map_path = $self->map_path;
Eric Wong66ab84b2007-12-08 23:27:42 -08003166 stat $map_path or return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08003167 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02003168 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003169 my $size = (stat($fh))[7];
3170 ($size % 24) == 0 or croak "inconsistent size: $size";
3171
3172 if ($size == 0) {
3173 close $fh or croak "close: $!";
Eric Wong66ab84b2007-12-08 23:27:42 -08003174 return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08003175 }
3176
Eric Wong66ab84b2007-12-08 23:27:42 -08003177 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003178 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003179 my ($r, $c) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08003180 if ($want_commit && $c eq ('0' x40)) {
3181 if ($size < 48) {
3182 return $want_commit ? (0, undef) : 0;
3183 }
3184 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
3185 sysread($fh, $buf, 24) == 24 or croak "read: $!";
3186 ($r, $c) = unpack(rev_map_fmt, $buf);
3187 if ($c eq ('0'x40)) {
3188 croak "Penultimate record is all-zeroes in $map_path";
3189 }
3190 }
3191 close $fh or croak "close: $!";
3192 $want_commit ? ($r, $c) : $r;
Eric Wong9c93fee2007-01-31 17:22:31 -08003193}
3194
Eric Wong060610c2007-12-08 23:27:41 -08003195sub rev_map_get {
Eric Wong26a62d52007-02-12 13:25:25 -08003196 my ($self, $rev, $uuid) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003197 my $map_path = $self->map_path($uuid);
3198 return undef unless -e $map_path;
3199
3200 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Ben Jackson195643f2009-06-03 20:45:52 -07003201 my $c = _rev_map_get($fh, $rev);
3202 close($fh) or croak "close: $!";
3203 $c
3204}
3205
3206sub _rev_map_get {
3207 my ($fh, $rev) = @_;
3208
Michael Weber4f7ec792008-04-18 15:12:04 +02003209 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003210 my $size = (stat($fh))[7];
3211 ($size % 24) == 0 or croak "inconsistent size: $size";
3212
3213 if ($size == 0) {
Eric Wong060610c2007-12-08 23:27:41 -08003214 return undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003215 }
Eric Wong060610c2007-12-08 23:27:41 -08003216
3217 my ($l, $u) = (0, $size - 24);
3218 my ($r, $c, $buf);
3219
3220 while ($l <= $u) {
3221 my $i = int(($l/24 + $u/24) / 2) * 24;
3222 sysseek($fh, $i, SEEK_SET) or croak "seek: $!";
3223 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
3224 my ($r, $c) = unpack('NH40', $buf);
3225
3226 if ($r < $rev) {
3227 $l = $i + 24;
3228 } elsif ($r > $rev) {
3229 $u = $i - 24;
3230 } else { # $r == $rev
Eric Wong66ab84b2007-12-08 23:27:42 -08003231 return $c eq ('0' x 40) ? undef : $c;
Eric Wong060610c2007-12-08 23:27:41 -08003232 }
3233 }
Eric Wong060610c2007-12-08 23:27:41 -08003234 undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003235}
3236
David D Kilzer111947e2007-11-11 22:56:52 -08003237# Finds the first svn revision that exists on (if $eq_ok is true) or
3238# before $rev for the current branch. It will not search any lower
3239# than $min_rev. Returns the git commit hash and svn revision number
3240# if found, else (undef, undef).
Eric Wong15710b62007-01-22 02:20:33 -08003241sub find_rev_before {
David D Kilzer111947e2007-11-11 22:56:52 -08003242 my ($self, $rev, $eq_ok, $min_rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08003243 --$rev unless $eq_ok;
David D Kilzer111947e2007-11-11 22:56:52 -08003244 $min_rev ||= 1;
Ben Jacksonca5e8802009-06-03 20:45:51 -07003245 my $max_rev = $self->rev_map_max;
3246 $rev = $max_rev if ($rev > $max_rev);
David D Kilzer111947e2007-11-11 22:56:52 -08003247 while ($rev >= $min_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003248 if (my $c = $self->rev_map_get($rev)) {
Eric Wong15710b62007-01-22 02:20:33 -08003249 return ($rev, $c);
3250 }
3251 --$rev;
3252 }
3253 return (undef, undef);
3254}
3255
David D Kilzer111947e2007-11-11 22:56:52 -08003256# Finds the first svn revision that exists on (if $eq_ok is true) or
3257# after $rev for the current branch. It will not search any higher
3258# than $max_rev. Returns the git commit hash and svn revision number
3259# if found, else (undef, undef).
3260sub find_rev_after {
3261 my ($self, $rev, $eq_ok, $max_rev) = @_;
3262 ++$rev unless $eq_ok;
Eric Wong060610c2007-12-08 23:27:41 -08003263 $max_rev ||= $self->rev_map_max;
David D Kilzer111947e2007-11-11 22:56:52 -08003264 while ($rev <= $max_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003265 if (my $c = $self->rev_map_get($rev)) {
David D Kilzer111947e2007-11-11 22:56:52 -08003266 return ($rev, $c);
3267 }
3268 ++$rev;
3269 }
3270 return (undef, undef);
3271}
3272
Eric Wong9b981fc2007-01-11 12:14:21 -08003273sub _new {
Eric Wong706587f2007-01-18 17:50:01 -08003274 my ($class, $repo_id, $ref_id, $path) = @_;
3275 unless (defined $repo_id && length $repo_id) {
3276 $repo_id = $Git::SVN::default_repo_id;
3277 }
3278 unless (defined $ref_id && length $ref_id) {
Eric Wong8b8fc062007-01-22 11:44:57 -08003279 $_[2] = $ref_id = $Git::SVN::default_ref_id;
Eric Wong706587f2007-01-18 17:50:01 -08003280 }
Eric Wong7829f202008-06-28 20:40:32 -07003281 $_[1] = $repo_id;
Eric Wong706587f2007-01-18 17:50:01 -08003282 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
3283 $_[3] = $path = '' unless (defined $path);
Eric Wongb4d57e52007-02-14 15:10:44 -08003284 mkpath(["$ENV{GIT_DIR}/svn"]);
Eric Wong26a62d52007-02-12 13:25:25 -08003285 bless {
3286 ref_id => $ref_id, dir => $dir, index => "$dir/index",
Eric Wong8a49ee92007-02-10 20:46:50 -08003287 path => $path, config => "$ENV{GIT_DIR}/svn/config",
Eric Wong060610c2007-12-08 23:27:41 -08003288 map_root => "$dir/.rev_map", repo_id => $repo_id }, $class;
Eric Wong26a62d52007-02-12 13:25:25 -08003289}
3290
Eric Wong060610c2007-12-08 23:27:41 -08003291# for read-only access of old .rev_db formats
3292sub unlink_rev_db_symlink {
3293 my ($self) = @_;
3294 my $link = $self->rev_db_path;
3295 $link =~ s/\.[\w-]+$// or croak "missing UUID at the end of $link";
3296 if (-l $link) {
3297 unlink $link or croak "unlink: $link failed!";
3298 }
3299}
3300
3301sub rev_db_path {
3302 my ($self, $uuid) = @_;
3303 my $db_path = $self->map_path($uuid);
3304 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
3305 or croak "map_path: $db_path does not contain '/.rev_map.' !";
3306 $db_path;
3307}
3308
3309# the new replacement for .rev_db
3310sub map_path {
Eric Wong26a62d52007-02-12 13:25:25 -08003311 my ($self, $uuid) = @_;
3312 $uuid ||= $self->ra_uuid;
Eric Wong060610c2007-12-08 23:27:41 -08003313 "$self->{map_root}.$uuid";
Eric Wong9b981fc2007-01-11 12:14:21 -08003314}
3315
Eric Wong1c8443b2007-01-14 02:17:00 -08003316sub uri_encode {
3317 my ($f) = @_;
3318 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
3319 $f
3320}
Eric Wong9b981fc2007-01-11 12:14:21 -08003321
Sam Vilain18ea92b2007-02-23 12:32:29 +13003322sub remove_username {
3323 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
3324}
3325
Eric Wongd976acf2007-01-04 00:45:03 -08003326package Git::SVN::Prompt;
3327use strict;
3328use warnings;
3329require SVN::Core;
3330use vars qw/$_no_auth_cache $_username/;
3331
3332sub simple {
Eric Wong30d055a2006-11-24 01:38:04 -08003333 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
3334 $may_save = undef if $_no_auth_cache;
3335 $default_username = $_username if defined $_username;
3336 if (defined $default_username && length $default_username) {
3337 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003338 print STDERR "Authentication realm: $realm\n";
3339 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003340 }
3341 $cred->username($default_username);
3342 } else {
Eric Wongd976acf2007-01-04 00:45:03 -08003343 username($cred, $realm, $may_save, $pool);
Eric Wong30d055a2006-11-24 01:38:04 -08003344 }
3345 $cred->password(_read_password("Password for '" .
3346 $cred->username . "': ", $realm));
3347 $cred->may_save($may_save);
3348 $SVN::_Core::SVN_NO_ERROR;
3349}
3350
Eric Wongd976acf2007-01-04 00:45:03 -08003351sub ssl_server_trust {
Eric Wong30d055a2006-11-24 01:38:04 -08003352 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
3353 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003354 print STDERR "Error validating server certificate for '$realm':\n";
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003355 {
3356 no warnings 'once';
3357 # All variables SVN::Auth::SSL::* are used only once,
3358 # so we're shutting up Perl warnings about this.
3359 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
3360 print STDERR " - The certificate is not issued ",
3361 "by a trusted authority. Use the\n",
3362 " fingerprint to validate ",
3363 "the certificate manually!\n";
3364 }
3365 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
3366 print STDERR " - The certificate hostname ",
3367 "does not match.\n";
3368 }
3369 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
3370 print STDERR " - The certificate is not yet valid.\n";
3371 }
3372 if ($failures & $SVN::Auth::SSL::EXPIRED) {
3373 print STDERR " - The certificate has expired.\n";
3374 }
3375 if ($failures & $SVN::Auth::SSL::OTHER) {
3376 print STDERR " - The certificate has ",
3377 "an unknown error.\n";
3378 }
3379 } # no warnings 'once'
Eric Wong6f729592007-01-15 20:15:55 -08003380 printf STDERR
3381 "Certificate information:\n".
Eric Wong30d055a2006-11-24 01:38:04 -08003382 " - Hostname: %s\n".
3383 " - Valid: from %s until %s\n".
3384 " - Issuer: %s\n".
3385 " - Fingerprint: %s\n",
3386 map $cert_info->$_, qw(hostname valid_from valid_until
Eric Wong6f729592007-01-15 20:15:55 -08003387 issuer_dname fingerprint);
Eric Wong30d055a2006-11-24 01:38:04 -08003388 my $choice;
3389prompt:
Eric Wong6f729592007-01-15 20:15:55 -08003390 print STDERR $may_save ?
Eric Wong30d055a2006-11-24 01:38:04 -08003391 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
3392 "(R)eject or accept (t)emporarily? ";
Eric Wong6f729592007-01-15 20:15:55 -08003393 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003394 $choice = lc(substr(<STDIN> || 'R', 0, 1));
3395 if ($choice =~ /^t$/i) {
3396 $cred->may_save(undef);
3397 } elsif ($choice =~ /^r$/i) {
3398 return -1;
3399 } elsif ($may_save && $choice =~ /^p$/i) {
3400 $cred->may_save($may_save);
3401 } else {
3402 goto prompt;
3403 }
3404 $cred->accepted_failures($failures);
3405 $SVN::_Core::SVN_NO_ERROR;
3406}
3407
Eric Wongd976acf2007-01-04 00:45:03 -08003408sub ssl_client_cert {
Eric Wong30d055a2006-11-24 01:38:04 -08003409 my ($cred, $realm, $may_save, $pool) = @_;
3410 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003411 print STDERR "Client certificate filename: ";
3412 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003413 chomp(my $filename = <STDIN>);
3414 $cred->cert_file($filename);
3415 $cred->may_save($may_save);
3416 $SVN::_Core::SVN_NO_ERROR;
3417}
3418
Eric Wongd976acf2007-01-04 00:45:03 -08003419sub ssl_client_cert_pw {
Eric Wong30d055a2006-11-24 01:38:04 -08003420 my ($cred, $realm, $may_save, $pool) = @_;
3421 $may_save = undef if $_no_auth_cache;
3422 $cred->password(_read_password("Password: ", $realm));
3423 $cred->may_save($may_save);
3424 $SVN::_Core::SVN_NO_ERROR;
3425}
3426
Eric Wongd976acf2007-01-04 00:45:03 -08003427sub username {
Eric Wong30d055a2006-11-24 01:38:04 -08003428 my ($cred, $realm, $may_save, $pool) = @_;
3429 $may_save = undef if $_no_auth_cache;
3430 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003431 print STDERR "Authentication realm: $realm\n";
Eric Wong30d055a2006-11-24 01:38:04 -08003432 }
3433 my $username;
3434 if (defined $_username) {
3435 $username = $_username;
3436 } else {
Eric Wong6f729592007-01-15 20:15:55 -08003437 print STDERR "Username: ";
3438 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003439 chomp($username = <STDIN>);
3440 }
3441 $cred->username($username);
3442 $cred->may_save($may_save);
3443 $SVN::_Core::SVN_NO_ERROR;
3444}
3445
3446sub _read_password {
3447 my ($prompt, $realm) = @_;
Eric Wong6f729592007-01-15 20:15:55 -08003448 print STDERR $prompt;
3449 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003450 require Term::ReadKey;
3451 Term::ReadKey::ReadMode('noecho');
3452 my $password = '';
3453 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
3454 last if $key =~ /[\012\015]/; # \n\r
3455 $password .= $key;
3456 }
3457 Term::ReadKey::ReadMode('restore');
Eric Wong6f729592007-01-15 20:15:55 -08003458 print STDERR "\n";
3459 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003460 $password;
3461}
3462
Eric Wong27a1a802006-11-27 21:44:48 -08003463package SVN::Git::Fetcher;
3464use vars qw/@ISA/;
3465use strict;
3466use warnings;
3467use Carp qw/croak/;
Adam Robenffe256f2008-05-23 16:19:41 +02003468use File::Temp qw/tempfile/;
Eric Wong27a1a802006-11-27 21:44:48 -08003469use IO::File qw//;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003470use vars qw/$_ignore_regex/;
Eric Wong27a1a802006-11-27 21:44:48 -08003471
3472# file baton members: path, mode_a, mode_b, pool, fh, blob, base
3473sub new {
Eric Wong8841b372009-02-11 01:56:58 -08003474 my ($class, $git_svn, $switch_path) = @_;
Eric Wong27a1a802006-11-27 21:44:48 -08003475 my $self = SVN::Delta::Editor->new;
3476 bless $self, $class;
Eric Wongdbc6c742009-01-11 16:51:10 -08003477 if (exists $git_svn->{last_commit}) {
3478 $self->{c} = $git_svn->{last_commit};
Eric Wong8841b372009-02-11 01:56:58 -08003479 $self->{empty_symlinks} =
3480 _mark_empty_symlinks($git_svn, $switch_path);
Eric Wongdbc6c742009-01-11 16:51:10 -08003481 }
Ben Jackson0d8bee72009-04-11 10:46:17 -07003482 $self->{ignore_regex} = eval { command_oneline('config', '--get',
3483 "svn-remote.$git_svn->{repo_id}.ignore-paths") };
Eric Wongd2a9a872006-12-12 14:47:00 -08003484 $self->{empty} = {};
3485 $self->{dir_prop} = {};
3486 $self->{file_prop} = {};
3487 $self->{absent_dir} = {};
3488 $self->{absent_file} = {};
Eric Wongef3cfaa2007-01-24 03:30:57 -08003489 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
Eric Wong27a1a802006-11-27 21:44:48 -08003490 $self;
3491}
3492
Eric Wongdbc6c742009-01-11 16:51:10 -08003493# this uses the Ra object, so it must be called before do_{switch,update},
3494# not inside them (when the Git::SVN::Fetcher object is passed) to
3495# do_{switch,update}
3496sub _mark_empty_symlinks {
Eric Wong8841b372009-02-11 01:56:58 -08003497 my ($git_svn, $switch_path) = @_;
Eric Wong4c58a712009-01-31 17:31:12 -08003498 my $bool = Git::config_bool('svn.brokenSymlinkWorkaround');
Eric Wong48679e52009-02-27 19:40:16 -08003499 return {} if (!defined($bool)) || (defined($bool) && ! $bool);
Eric Wong4c58a712009-01-31 17:31:12 -08003500
Eric Wongdbc6c742009-01-11 16:51:10 -08003501 my %ret;
3502 my ($rev, $cmt) = $git_svn->last_rev_commit;
3503 return {} unless ($rev && $cmt);
3504
Eric Wong4c58a712009-01-31 17:31:12 -08003505 # allow the warning to be printed for each revision we fetch to
3506 # ensure the user sees it. The user can also disable the workaround
3507 # on the repository even while git svn is running and the next
3508 # revision fetched will skip this expensive function.
3509 my $printed_warning;
Eric Wongdbc6c742009-01-11 16:51:10 -08003510 chomp(my $empty_blob = `git hash-object -t blob --stdin < /dev/null`);
3511 my ($ls, $ctx) = command_output_pipe(qw/ls-tree -r -z/, $cmt);
3512 local $/ = "\0";
Eric Wong8841b372009-02-11 01:56:58 -08003513 my $pfx = defined($switch_path) ? $switch_path : $git_svn->{path};
Eric Wongdbc6c742009-01-11 16:51:10 -08003514 $pfx .= '/' if length($pfx);
3515 while (<$ls>) {
3516 chomp;
3517 s/\A100644 blob $empty_blob\t//o or next;
Eric Wong4c58a712009-01-31 17:31:12 -08003518 unless ($printed_warning) {
3519 print STDERR "Scanning for empty symlinks, ",
3520 "this may take a while if you have ",
3521 "many empty files\n",
3522 "You may disable this with `",
3523 "git config svn.brokenSymlinkWorkaround ",
3524 "false'.\n",
3525 "This may be done in a different ",
3526 "terminal without restarting ",
3527 "git svn\n";
3528 $printed_warning = 1;
3529 }
Eric Wongdbc6c742009-01-11 16:51:10 -08003530 my $path = $_;
3531 my (undef, $props) =
3532 $git_svn->ra->get_file($pfx.$path, $rev, undef);
3533 if ($props->{'svn:special'}) {
3534 $ret{$path} = 1;
3535 }
3536 }
3537 command_close_pipe($ls, $ctx);
3538 \%ret;
3539}
3540
Eric Wongb03a71a2009-01-11 18:23:38 -08003541# returns true if a given path is inside a ".git" directory
3542sub in_dot_git {
3543 $_[0] =~ m{(?:^|/)\.git(?:/|$)};
3544}
3545
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003546# return value: 0 -- don't ignore, 1 -- ignore
3547sub is_path_ignored {
Ben Jackson0d8bee72009-04-11 10:46:17 -07003548 my ($self, $path) = @_;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003549 return 1 if in_dot_git($path);
Ben Jackson0d8bee72009-04-11 10:46:17 -07003550 return 1 if defined($self->{ignore_regex}) &&
3551 $path =~ m!$self->{ignore_regex}!;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003552 return 0 unless defined($_ignore_regex);
3553 return 1 if $path =~ m!$_ignore_regex!o;
3554 return 0;
3555}
3556
Eric Wong8b8fc062007-01-22 11:44:57 -08003557sub set_path_strip {
3558 my ($self, $path) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003559 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
Eric Wong8b8fc062007-01-22 11:44:57 -08003560}
3561
Eric Wongd2a9a872006-12-12 14:47:00 -08003562sub open_root {
3563 { path => '' };
3564}
3565
3566sub open_directory {
3567 my ($self, $path, $pb, $rev) = @_;
3568 { path => $path };
3569}
3570
Eric Wong706587f2007-01-18 17:50:01 -08003571sub git_path {
3572 my ($self, $path) = @_;
Eric Wong2b27f6c2007-01-28 04:59:05 -08003573 if ($self->{path_strip}) {
3574 $path =~ s!$self->{path_strip}!! or
3575 die "Failed to strip path '$path' ($self->{path_strip})\n";
3576 }
Eric Wong706587f2007-01-18 17:50:01 -08003577 $path;
3578}
3579
Eric Wong27a1a802006-11-27 21:44:48 -08003580sub delete_entry {
3581 my ($self, $path, $rev, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003582 return undef if $self->is_path_ignored($path);
Eric Wong4a87db02007-01-04 01:38:18 -08003583
Eric Wong706587f2007-01-18 17:50:01 -08003584 my $gpath = $self->git_path($path);
Eric Wong8a603772007-01-31 02:45:50 -08003585 return undef if ($gpath eq '');
3586
Eric Wong4a87db02007-01-04 01:38:18 -08003587 # remove entire directories.
Eric Wong4f821012009-03-28 22:10:08 -07003588 my ($tree) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
3589 =~ /\A040000 tree ([a-f\d]{40})\t\Q$gpath\E\0/);
3590 if ($tree) {
Eric Wong4a87db02007-01-04 01:38:18 -08003591 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3592 -r --name-only -z/,
Eric Wong4f821012009-03-28 22:10:08 -07003593 $tree);
Eric Wong4a87db02007-01-04 01:38:18 -08003594 local $/ = "\0";
3595 while (<$ls>) {
Eric Wongef3cfaa2007-01-24 03:30:57 -08003596 chomp;
Eric Wong4f821012009-03-28 22:10:08 -07003597 my $rmpath = "$gpath/$_";
3598 $self->{gii}->remove($rmpath);
3599 print "\tD\t$rmpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003600 }
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003601 print "\tD\t$gpath/\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003602 command_close_pipe($ls, $ctx);
3603 $self->{empty}->{$path} = 0
3604 } else {
Eric Wongef3cfaa2007-01-24 03:30:57 -08003605 $self->{gii}->remove($gpath);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003606 print "\tD\t$gpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003607 }
Eric Wong27a1a802006-11-27 21:44:48 -08003608 undef;
3609}
3610
3611sub open_file {
3612 my ($self, $path, $pb, $rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08003613 my ($mode, $blob);
3614
Ben Jackson0d8bee72009-04-11 10:46:17 -07003615 goto out if $self->is_path_ignored($path);
Eric Wongb03a71a2009-01-11 18:23:38 -08003616
Eric Wong706587f2007-01-18 17:50:01 -08003617 my $gpath = $self->git_path($path);
Eric Wong4f821012009-03-28 22:10:08 -07003618 ($mode, $blob) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
3619 =~ /\A(\d{6}) blob ([a-f\d]{40})\t\Q$gpath\E\0/);
Eric Wong006ede52006-12-08 01:55:19 -08003620 unless (defined $mode && defined $blob) {
3621 die "$path was not found in commit $self->{c} (r$rev)\n";
3622 }
Eric Wongdbc6c742009-01-11 16:51:10 -08003623 if ($mode eq '100644' && $self->{empty_symlinks}->{$path}) {
3624 $mode = '120000';
3625 }
Eric Wongb03a71a2009-01-11 18:23:38 -08003626out:
Eric Wong27a1a802006-11-27 21:44:48 -08003627 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
Eric Wong0864e3b2006-11-28 02:50:17 -08003628 pool => SVN::Pool->new, action => 'M' };
Eric Wong27a1a802006-11-27 21:44:48 -08003629}
3630
3631sub add_file {
3632 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08003633 my $mode;
3634
Ben Jackson0d8bee72009-04-11 10:46:17 -07003635 if (!$self->is_path_ignored($path)) {
Eric Wongb03a71a2009-01-11 18:23:38 -08003636 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3637 delete $self->{empty}->{$dir};
3638 $mode = '100644';
3639 }
3640 { path => $path, mode_a => $mode, mode_b => $mode,
Eric Wong0864e3b2006-11-28 02:50:17 -08003641 pool => SVN::Pool->new, action => 'A' };
Eric Wong27a1a802006-11-27 21:44:48 -08003642}
3643
Eric Wongd2a9a872006-12-12 14:47:00 -08003644sub add_directory {
3645 my ($self, $path, $cp_path, $cp_rev) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003646 goto out if $self->is_path_ignored($path);
Eric Wong12a6d752007-12-14 08:39:09 -08003647 my $gpath = $self->git_path($path);
3648 if ($gpath eq '') {
3649 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3650 -r --name-only -z/,
3651 $self->{c});
3652 local $/ = "\0";
3653 while (<$ls>) {
3654 chomp;
3655 $self->{gii}->remove($_);
3656 print "\tD\t$_\n" unless $::_q;
3657 }
3658 command_close_pipe($ls, $ctx);
3659 $self->{empty}->{$path} = 0;
3660 }
Eric Wongd2a9a872006-12-12 14:47:00 -08003661 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3662 delete $self->{empty}->{$dir};
3663 $self->{empty}->{$path} = 1;
Eric Wongb03a71a2009-01-11 18:23:38 -08003664out:
Eric Wongd2a9a872006-12-12 14:47:00 -08003665 { path => $path };
3666}
3667
3668sub change_dir_prop {
3669 my ($self, $db, $prop, $value) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003670 return undef if $self->is_path_ignored($db->{path});
Eric Wongd2a9a872006-12-12 14:47:00 -08003671 $self->{dir_prop}->{$db->{path}} ||= {};
3672 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
3673 undef;
3674}
3675
3676sub absent_directory {
3677 my ($self, $path, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003678 return undef if $self->is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08003679 $self->{absent_dir}->{$pb->{path}} ||= [];
3680 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
3681 undef;
3682}
3683
3684sub absent_file {
3685 my ($self, $path, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003686 return undef if $self->is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08003687 $self->{absent_file}->{$pb->{path}} ||= [];
3688 push @{$self->{absent_file}->{$pb->{path}}}, $path;
3689 undef;
3690}
3691
Eric Wong27a1a802006-11-27 21:44:48 -08003692sub change_file_prop {
3693 my ($self, $fb, $prop, $value) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003694 return undef if $self->is_path_ignored($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08003695 if ($prop eq 'svn:executable') {
3696 if ($fb->{mode_b} != 120000) {
3697 $fb->{mode_b} = defined $value ? 100755 : 100644;
3698 }
3699 } elsif ($prop eq 'svn:special') {
3700 $fb->{mode_b} = defined $value ? 120000 : 100644;
Eric Wongd2a9a872006-12-12 14:47:00 -08003701 } else {
3702 $self->{file_prop}->{$fb->{path}} ||= {};
3703 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
Eric Wong27a1a802006-11-27 21:44:48 -08003704 }
3705 undef;
3706}
3707
3708sub apply_textdelta {
3709 my ($self, $fb, $exp) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003710 return undef if $self->is_path_ignored($fb->{path});
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003711 my $fh = $::_repository->temp_acquire('svn_delta');
Eric Wong27a1a802006-11-27 21:44:48 -08003712 # $fh gets auto-closed() by SVN::TxDelta::apply(),
3713 # (but $base does not,) so dup() it for reading in close_file
3714 open my $dup, '<&', $fh or croak $!;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003715 my $base = $::_repository->temp_acquire('git_blob');
Eric Wongb03a71a2009-01-11 18:23:38 -08003716
Eric Wong27a1a802006-11-27 21:44:48 -08003717 if ($fb->{blob}) {
Eric Wongbaf5fa82009-01-11 16:51:11 -08003718 my ($base_is_link, $size);
3719
Eric Wongdbc6c742009-01-11 16:51:10 -08003720 if ($fb->{mode_a} eq '120000' &&
3721 ! $self->{empty_symlinks}->{$fb->{path}}) {
3722 print $base 'link ' or die "print $!\n";
Eric Wongbaf5fa82009-01-11 16:51:11 -08003723 $base_is_link = 1;
Eric Wongdbc6c742009-01-11 16:51:10 -08003724 }
Eric Wongbaf5fa82009-01-11 16:51:11 -08003725 retry:
3726 $size = $::_repository->cat_blob($fb->{blob}, $base);
Junio C Hamanod683a0e2008-05-27 23:33:22 -07003727 die "Failed to read object $fb->{blob}" if ($size < 0);
Eric Wong27a1a802006-11-27 21:44:48 -08003728
3729 if (defined $exp) {
3730 seek $base, 0, 0 or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08003731 my $got = ::md5sum($base);
Eric Wongbaf5fa82009-01-11 16:51:11 -08003732 if ($got ne $exp) {
3733 my $err = "Checksum mismatch: ".
3734 "$fb->{path} $fb->{blob}\n" .
3735 "expected: $exp\n" .
3736 " got: $got\n";
3737 if ($base_is_link) {
3738 warn $err,
3739 "Retrying... (possibly ",
3740 "a bad symlink from SVN)\n";
3741 $::_repository->temp_reset($base);
3742 $base_is_link = 0;
3743 goto retry;
3744 }
3745 die $err;
3746 }
Eric Wong27a1a802006-11-27 21:44:48 -08003747 }
3748 }
3749 seek $base, 0, 0 or croak $!;
Marcus Griep0b191382008-08-12 12:00:53 -04003750 $fb->{fh} = $fh;
Eric Wong27a1a802006-11-27 21:44:48 -08003751 $fb->{base} = $base;
Marcus Griep0b191382008-08-12 12:00:53 -04003752 [ SVN::TxDelta::apply($base, $dup, undef, $fb->{path}, $fb->{pool}) ];
Eric Wong27a1a802006-11-27 21:44:48 -08003753}
3754
3755sub close_file {
3756 my ($self, $fb, $exp) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003757 return undef if $self->is_path_ignored($fb->{path});
Eric Wongb03a71a2009-01-11 18:23:38 -08003758
Eric Wong27a1a802006-11-27 21:44:48 -08003759 my $hash;
Eric Wong706587f2007-01-18 17:50:01 -08003760 my $path = $self->git_path($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08003761 if (my $fh = $fb->{fh}) {
Eric Wong7faf0682007-05-27 15:59:01 -07003762 if (defined $exp) {
3763 seek($fh, 0, 0) or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08003764 my $got = ::md5sum($fh);
Eric Wong7faf0682007-05-27 15:59:01 -07003765 if ($got ne $exp) {
3766 die "Checksum mismatch: $path\n",
3767 "expected: $exp\n got: $got\n";
3768 }
3769 }
Eric Wong27a1a802006-11-27 21:44:48 -08003770 if ($fb->{mode_b} == 120000) {
Marcus Griep510b0942008-08-12 12:45:39 -04003771 sysseek($fh, 0, 0) or croak $!;
Eric Wongdbc6c742009-01-11 16:51:10 -08003772 my $rd = sysread($fh, my $buf, 5);
Marcus Griep510b0942008-08-12 12:45:39 -04003773
Eric Wongdbc6c742009-01-11 16:51:10 -08003774 if (!defined $rd) {
3775 croak "sysread: $!\n";
3776 } elsif ($rd == 0) {
Marcus Griep510b0942008-08-12 12:45:39 -04003777 warn "$path has mode 120000",
Eric Wongdbc6c742009-01-11 16:51:10 -08003778 " but it points to nothing\n",
3779 "converting to an empty file with mode",
3780 " 100644\n";
3781 $fb->{mode_b} = '100644';
3782 } elsif ($buf ne 'link ') {
3783 warn "$path has mode 120000",
3784 " but is not a link\n";
Marcus Griep510b0942008-08-12 12:45:39 -04003785 } else {
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003786 my $tmp_fh = $::_repository->temp_acquire(
3787 'svn_hash');
Marcus Griep510b0942008-08-12 12:45:39 -04003788 my $res;
3789 while ($res = sysread($fh, my $str, 1024)) {
3790 my $out = syswrite($tmp_fh, $str, $res);
3791 defined($out) && $out == $res
3792 or croak("write ",
Marcus Griep836ff952008-09-08 12:53:01 -04003793 Git::temp_path($tmp_fh),
Marcus Griep510b0942008-08-12 12:45:39 -04003794 ": $!\n");
3795 }
3796 defined $res or croak $!;
3797
3798 ($fh, $tmp_fh) = ($tmp_fh, $fh);
3799 Git::temp_release($tmp_fh, 1);
Eric Wong7fc35e02007-12-19 00:06:45 -08003800 }
Eric Wong27a1a802006-11-27 21:44:48 -08003801 }
Adam Robenffe256f2008-05-23 16:19:41 +02003802
Marcus Griep0b191382008-08-12 12:00:53 -04003803 $hash = $::_repository->hash_and_insert_object(
Marcus Griep836ff952008-09-08 12:53:01 -04003804 Git::temp_path($fh));
Eric Wong27a1a802006-11-27 21:44:48 -08003805 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
Marcus Griep0b191382008-08-12 12:00:53 -04003806
3807 Git::temp_release($fb->{base}, 1);
Marcus Griep510b0942008-08-12 12:45:39 -04003808 Git::temp_release($fh, 1);
Eric Wong27a1a802006-11-27 21:44:48 -08003809 } else {
3810 $hash = $fb->{blob} or die "no blob information\n";
3811 }
3812 $fb->{pool}->clear;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003813 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003814 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
Eric Wong27a1a802006-11-27 21:44:48 -08003815 undef;
3816}
3817
3818sub abort_edit {
3819 my $self = shift;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003820 $self->{nr} = $self->{gii}->{nr};
3821 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08003822 $self->SUPER::abort_edit(@_);
3823}
3824
3825sub close_edit {
3826 my $self = shift;
Eric Wongdad73c02006-11-28 14:06:05 -08003827 $self->{git_commit_ok} = 1;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003828 $self->{nr} = $self->{gii}->{nr};
3829 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08003830 $self->SUPER::close_edit(@_);
3831}
Eric Wong1a82e792006-06-16 02:55:13 -07003832
Eric Wonga5e0ced2006-06-12 15:23:48 -07003833package SVN::Git::Editor;
Eric Wong24e22aa2007-01-29 00:07:49 -08003834use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003835use strict;
3836use warnings;
3837use Carp qw/croak/;
3838use IO::File;
3839
3840sub new {
Eric Wong61395352007-01-27 14:33:08 -08003841 my ($class, $opts) = @_;
3842 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
3843 die "$_ required!\n" unless (defined $opts->{$_});
Eric Wonga5e0ced2006-06-12 15:23:48 -07003844 }
Eric Wong61395352007-01-27 14:33:08 -08003845
3846 my $pool = SVN::Pool->new;
3847 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
3848 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
3849 $opts->{r}, $mods);
3850
3851 # $opts->{ra} functions should not be used after this:
3852 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
3853 $opts->{editor_cb}, $pool);
3854 my $self = SVN::Delta::Editor->new(@ce, $pool);
3855 bless $self, $class;
3856 foreach (qw/svn_path r tree_a tree_b/) {
3857 $self->{$_} = $opts->{$_};
3858 }
3859 $self->{url} = $opts->{ra}->{url};
3860 $self->{mods} = $mods;
3861 $self->{types} = $types;
3862 $self->{pool} = $pool;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003863 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
3864 $self->{rm} = { };
Eric Wongd3a840d2007-01-26 01:32:45 -08003865 $self->{path_prefix} = length $self->{svn_path} ?
3866 "$self->{svn_path}/" : '';
Brad King128de652008-07-25 11:32:37 -04003867 $self->{config} = $opts->{config};
Eric Wonga5e0ced2006-06-12 15:23:48 -07003868 return $self;
3869}
3870
Eric Wong61395352007-01-27 14:33:08 -08003871sub generate_diff {
3872 my ($tree_a, $tree_b) = @_;
3873 my @diff_tree = qw(diff-tree -z -r);
Eric Wong24e22aa2007-01-29 00:07:49 -08003874 if ($_cp_similarity) {
3875 push @diff_tree, "-C$_cp_similarity";
Eric Wong61395352007-01-27 14:33:08 -08003876 } else {
3877 push @diff_tree, '-C';
3878 }
Eric Wong24e22aa2007-01-29 00:07:49 -08003879 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
3880 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
Eric Wong61395352007-01-27 14:33:08 -08003881 push @diff_tree, $tree_a, $tree_b;
3882 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
3883 local $/ = "\0";
3884 my $state = 'meta';
3885 my @mods;
3886 while (<$diff_fh>) {
3887 chomp $_; # this gets rid of the trailing "\0"
3888 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02003889 ($::sha1)\s($::sha1)\s
Eric Wong61395352007-01-27 14:33:08 -08003890 ([MTCRAD])\d*$/xo) {
3891 push @mods, { mode_a => $1, mode_b => $2,
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02003892 sha1_a => $3, sha1_b => $4,
3893 chg => $5 };
3894 if ($5 =~ /^(?:C|R)$/) {
Eric Wong61395352007-01-27 14:33:08 -08003895 $state = 'file_a';
3896 } else {
3897 $state = 'file_b';
3898 }
3899 } elsif ($state eq 'file_a') {
3900 my $x = $mods[$#mods] or croak "Empty array\n";
3901 if ($x->{chg} !~ /^(?:C|R)$/) {
3902 croak "Error parsing $_, $x->{chg}\n";
3903 }
3904 $x->{file_a} = $_;
3905 $state = 'file_b';
3906 } elsif ($state eq 'file_b') {
3907 my $x = $mods[$#mods] or croak "Empty array\n";
3908 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
3909 croak "Error parsing $_, $x->{chg}\n";
3910 }
3911 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
3912 croak "Error parsing $_, $x->{chg}\n";
3913 }
3914 $x->{file_b} = $_;
3915 $state = 'meta';
3916 } else {
3917 croak "Error parsing $_\n";
3918 }
3919 }
3920 command_close_pipe($diff_fh, $ctx);
3921 \@mods;
3922}
3923
3924sub check_diff_paths {
3925 my ($ra, $pfx, $rev, $mods) = @_;
3926 my %types;
3927 $pfx .= '/' if length $pfx;
3928
3929 sub type_diff_paths {
3930 my ($ra, $types, $path, $rev) = @_;
3931 my @p = split m#/+#, $path;
3932 my $c = shift @p;
3933 unless (defined $types->{$c}) {
3934 $types->{$c} = $ra->check_path($c, $rev);
3935 }
3936 while (@p) {
3937 $c .= '/' . shift @p;
3938 next if defined $types->{$c};
3939 $types->{$c} = $ra->check_path($c, $rev);
3940 }
3941 }
3942
3943 foreach my $m (@$mods) {
3944 foreach my $f (qw/file_a file_b/) {
3945 next unless defined $m->{$f};
3946 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
3947 if (length $pfx.$dir && ! defined $types{$dir}) {
3948 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
3949 }
3950 }
3951 }
3952 \%types;
3953}
3954
Eric Wonga5e0ced2006-06-12 15:23:48 -07003955sub split_path {
3956 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
3957}
3958
3959sub repo_path {
Eric Wongd3a840d2007-01-26 01:32:45 -08003960 my ($self, $path) = @_;
3961 $self->{path_prefix}.(defined $path ? $path : '');
Eric Wonga5e0ced2006-06-12 15:23:48 -07003962}
3963
3964sub url_path {
3965 my ($self, $path) = @_;
Eric Wong29633bb2007-07-15 21:53:50 -07003966 if ($self->{url} =~ m#^https?://#) {
Eric Wong6a004d32008-10-21 14:12:15 -07003967 $path =~ s/([^~a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
Eric Wong29633bb2007-07-15 21:53:50 -07003968 }
Eric Wong6e8548c2007-01-27 01:32:00 -08003969 $self->{url} . '/' . $self->repo_path($path);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003970}
3971
3972sub rmdirs {
Eric Wong61395352007-01-27 14:33:08 -08003973 my ($self) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003974 my $rm = $self->{rm};
3975 delete $rm->{''}; # we never delete the url we're tracking
3976 return unless %$rm;
3977
3978 foreach (keys %$rm) {
3979 my @d = split m#/#, $_;
3980 my $c = shift @d;
3981 $rm->{$c} = 1;
3982 while (@d) {
3983 $c .= '/' . shift @d;
3984 $rm->{$c} = 1;
3985 }
3986 }
3987 delete $rm->{$self->{svn_path}};
3988 delete $rm->{''}; # we never delete the url we're tracking
3989 return unless %$rm;
3990
Eric Wong61395352007-01-27 14:33:08 -08003991 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
3992 $self->{tree_b});
Eric Wonga5e0ced2006-06-12 15:23:48 -07003993 local $/ = "\0";
3994 while (<$fh>) {
3995 chomp;
Eric Wong747fa122006-11-24 22:38:17 -08003996 my @dn = split m#/#, $_;
Eric Wongc07eee12006-06-19 17:59:35 -07003997 while (pop @dn) {
3998 delete $rm->{join '/', @dn};
3999 }
4000 unless (%$rm) {
Eric Wong22600a22007-02-01 13:12:26 -08004001 close $fh;
Eric Wongc07eee12006-06-19 17:59:35 -07004002 return;
4003 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07004004 }
Eric Wongaef4e922006-12-15 10:59:54 -08004005 command_close_pipe($fh, $ctx);
Eric Wongc07eee12006-06-19 17:59:35 -07004006
Eric Wonga5e0ced2006-06-12 15:23:48 -07004007 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
4008 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
4009 $self->close_directory($bat->{$d}, $p);
4010 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
Eric Wong44320b92007-01-13 22:35:53 -08004011 print "\tD+\t$d/\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004012 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
4013 delete $bat->{$d};
4014 }
4015}
4016
4017sub open_or_add_dir {
4018 my ($self, $full_path, $baton) = @_;
Eric Wong6e8548c2007-01-27 01:32:00 -08004019 my $t = $self->{types}->{$full_path};
4020 if (!defined $t) {
4021 die "$full_path not known in r$self->{r} or we have a bug!\n";
4022 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004023 {
4024 no warnings 'once';
4025 # SVN::Node::none and SVN::Node::file are used only once,
4026 # so we're shutting up Perl's warnings about them.
4027 if ($t == $SVN::Node::none) {
4028 return $self->add_directory($full_path, $baton,
4029 undef, -1, $self->{pool});
4030 } elsif ($t == $SVN::Node::dir) {
4031 return $self->open_directory($full_path, $baton,
4032 $self->{r}, $self->{pool});
4033 } # no warnings 'once'
4034 print STDERR "$full_path already exists in repository at ",
4035 "r$self->{r} and it is not a directory (",
4036 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
4037 } # no warnings 'once'
Eric Wonga5e0ced2006-06-12 15:23:48 -07004038 exit 1;
4039}
4040
4041sub ensure_path {
4042 my ($self, $path) = @_;
4043 my $bat = $self->{bat};
Eric Wong6e8548c2007-01-27 01:32:00 -08004044 my $repo_path = $self->repo_path($path);
4045 return $bat->{''} unless (length $repo_path);
4046 my @p = split m#/+#, $repo_path;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004047 my $c = shift @p;
4048 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
4049 while (@p) {
4050 my $c0 = $c;
4051 $c .= '/' . shift @p;
4052 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
4053 }
4054 return $bat->{$c};
4055}
4056
Brad King128de652008-07-25 11:32:37 -04004057# Subroutine to convert a globbing pattern to a regular expression.
4058# From perl cookbook.
4059sub glob2pat {
4060 my $globstr = shift;
4061 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
4062 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
4063 return '^' . $globstr . '$';
4064}
4065
4066sub check_autoprop {
4067 my ($self, $pattern, $properties, $file, $fbat) = @_;
4068 # Convert the globbing pattern to a regular expression.
4069 my $regex = glob2pat($pattern);
4070 # Check if the pattern matches the file name.
4071 if($file =~ m/($regex)/) {
4072 # Parse the list of properties to set.
4073 my @props = split(/;/, $properties);
4074 foreach my $prop (@props) {
4075 # Parse 'name=value' syntax and set the property.
4076 if ($prop =~ /([^=]+)=(.*)/) {
4077 my ($n,$v) = ($1,$2);
4078 for ($n, $v) {
4079 s/^\s+//; s/\s+$//;
4080 }
4081 $self->change_file_prop($fbat, $n, $v);
4082 }
4083 }
4084 }
4085}
4086
4087sub apply_autoprops {
4088 my ($self, $file, $fbat) = @_;
4089 my $conf_t = ${$self->{config}}{'config'};
4090 no warnings 'once';
4091 # Check [miscellany]/enable-auto-props in svn configuration.
4092 if (SVN::_Core::svn_config_get_bool(
4093 $conf_t,
4094 $SVN::_Core::SVN_CONFIG_SECTION_MISCELLANY,
4095 $SVN::_Core::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS,
4096 0)) {
4097 # Auto-props are enabled. Enumerate them to look for matches.
4098 my $callback = sub {
4099 $self->check_autoprop($_[0], $_[1], $file, $fbat);
4100 };
4101 SVN::_Core::svn_config_enumerate(
4102 $conf_t,
4103 $SVN::_Core::SVN_CONFIG_SECTION_AUTO_PROPS,
4104 $callback);
4105 }
4106}
4107
Eric Wonga5e0ced2006-06-12 15:23:48 -07004108sub A {
Eric Wong44320b92007-01-13 22:35:53 -08004109 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004110 my ($dir, $file) = split_path($m->{file_b});
4111 my $pbat = $self->ensure_path($dir);
4112 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4113 undef, -1);
Eric Wong44320b92007-01-13 22:35:53 -08004114 print "\tA\t$m->{file_b}\n" unless $::_q;
Brad King128de652008-07-25 11:32:37 -04004115 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004116 $self->chg_file($fbat, $m);
4117 $self->close_file($fbat,undef,$self->{pool});
4118}
4119
4120sub C {
Eric Wong44320b92007-01-13 22:35:53 -08004121 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004122 my ($dir, $file) = split_path($m->{file_b});
4123 my $pbat = $self->ensure_path($dir);
4124 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4125 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08004126 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004127 $self->chg_file($fbat, $m);
4128 $self->close_file($fbat,undef,$self->{pool});
4129}
4130
4131sub delete_entry {
4132 my ($self, $path, $pbat) = @_;
4133 my $rpath = $self->repo_path($path);
4134 my ($dir, $file) = split_path($rpath);
4135 $self->{rm}->{$dir} = 1;
4136 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
4137}
4138
4139sub R {
Eric Wong44320b92007-01-13 22:35:53 -08004140 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004141 my ($dir, $file) = split_path($m->{file_b});
4142 my $pbat = $self->ensure_path($dir);
4143 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4144 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08004145 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Paul Talacko7c4d0212008-09-06 18:50:38 -07004146 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004147 $self->chg_file($fbat, $m);
4148 $self->close_file($fbat,undef,$self->{pool});
4149
4150 ($dir, $file) = split_path($m->{file_a});
4151 $pbat = $self->ensure_path($dir);
4152 $self->delete_entry($m->{file_a}, $pbat);
4153}
4154
4155sub M {
Eric Wong44320b92007-01-13 22:35:53 -08004156 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004157 my ($dir, $file) = split_path($m->{file_b});
4158 my $pbat = $self->ensure_path($dir);
4159 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
4160 $pbat,$self->{r},$self->{pool});
Eric Wong44320b92007-01-13 22:35:53 -08004161 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004162 $self->chg_file($fbat, $m);
4163 $self->close_file($fbat,undef,$self->{pool});
4164}
4165
4166sub T { shift->M(@_) }
4167
4168sub change_file_prop {
4169 my ($self, $fbat, $pname, $pval) = @_;
4170 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
4171}
4172
Florian Weimer214a34d2008-08-31 17:45:04 +02004173sub _chg_file_get_blob ($$$$) {
4174 my ($self, $fbat, $m, $which) = @_;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004175 my $fh = $::_repository->temp_acquire("git_blob_$which");
Florian Weimer214a34d2008-08-31 17:45:04 +02004176 if ($m->{"mode_$which"} =~ /^120/) {
4177 print $fh 'link ' or croak $!;
4178 $self->change_file_prop($fbat,'svn:special','*');
4179 } elsif ($m->{mode_a} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
4180 $self->change_file_prop($fbat,'svn:special',undef);
4181 }
4182 my $blob = $m->{"sha1_$which"};
4183 return ($fh,) if ($blob =~ /^0{40}$/);
4184 my $size = $::_repository->cat_blob($blob, $fh);
4185 croak "Failed to read object $blob" if ($size < 0);
4186 $fh->flush == 0 or croak $!;
4187 seek $fh, 0, 0 or croak $!;
4188
4189 my $exp = ::md5sum($fh);
4190 seek $fh, 0, 0 or croak $!;
4191 return ($fh, $exp);
4192}
4193
Eric Wonga5e0ced2006-06-12 15:23:48 -07004194sub chg_file {
4195 my ($self, $fbat, $m) = @_;
4196 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
4197 $self->change_file_prop($fbat,'svn:executable','*');
4198 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
4199 $self->change_file_prop($fbat,'svn:executable',undef);
4200 }
Florian Weimer8598db932008-08-31 17:47:09 +02004201 my ($fh_a, $exp_a) = _chg_file_get_blob $self, $fbat, $m, 'a';
4202 my ($fh_b, $exp_b) = _chg_file_get_blob $self, $fbat, $m, 'b';
Eric Wongf7197df2006-10-14 15:48:35 -07004203 my $pool = SVN::Pool->new;
Florian Weimer8598db932008-08-31 17:47:09 +02004204 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
4205 if (-s $fh_a) {
4206 my $txstream = SVN::TxDelta::new ($fh_a, $fh_b, $pool);
Eric Wong991255c2008-08-31 19:45:07 -07004207 my $res = SVN::TxDelta::send_txstream($txstream, @$atd, $pool);
4208 if (defined $res) {
4209 die "Unexpected result from send_txstream: $res\n",
4210 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
4211 }
Florian Weimer8598db932008-08-31 17:47:09 +02004212 } else {
4213 my $got = SVN::TxDelta::send_stream($fh_b, @$atd, $pool);
4214 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
4215 if ($got ne $exp_b);
4216 }
4217 Git::temp_release($fh_b, 1);
4218 Git::temp_release($fh_a, 1);
Eric Wongf7197df2006-10-14 15:48:35 -07004219 $pool->clear;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004220}
4221
4222sub D {
Eric Wong44320b92007-01-13 22:35:53 -08004223 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004224 my ($dir, $file) = split_path($m->{file_b});
4225 my $pbat = $self->ensure_path($dir);
Eric Wong44320b92007-01-13 22:35:53 -08004226 print "\tD\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004227 $self->delete_entry($m->{file_b}, $pbat);
4228}
4229
4230sub close_edit {
4231 my ($self) = @_;
4232 my ($p,$bat) = ($self->{pool}, $self->{bat});
4233 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
Eric Wong64427542007-05-19 02:58:37 -07004234 next if $_ eq '';
Eric Wonga5e0ced2006-06-12 15:23:48 -07004235 $self->close_directory($bat->{$_}, $p);
4236 }
Eric Wong64427542007-05-19 02:58:37 -07004237 $self->close_directory($bat->{''}, $p);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004238 $self->SUPER::close_edit($p);
4239 $p->clear;
4240}
4241
4242sub abort_edit {
4243 my ($self) = @_;
4244 $self->SUPER::abort_edit($self->{pool});
Eric Wong61395352007-01-27 14:33:08 -08004245}
4246
4247sub DESTROY {
4248 my $self = shift;
4249 $self->SUPER::DESTROY(@_);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004250 $self->{pool}->clear;
4251}
4252
Eric Wong44320b92007-01-13 22:35:53 -08004253# this drives the editor
4254sub apply_diff {
Eric Wong61395352007-01-27 14:33:08 -08004255 my ($self) = @_;
4256 my $mods = $self->{mods};
Eric Wong44320b92007-01-13 22:35:53 -08004257 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
Eric Wong6e8548c2007-01-27 01:32:00 -08004258 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
Eric Wong44320b92007-01-13 22:35:53 -08004259 my $f = $m->{chg};
4260 if (defined $o{$f}) {
4261 $self->$f($m);
4262 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004263 fatal("Invalid change type: $f");
Eric Wong44320b92007-01-13 22:35:53 -08004264 }
4265 }
Eric Wong24e22aa2007-01-29 00:07:49 -08004266 $self->rmdirs if $_rmdir;
Eric Wong6e8548c2007-01-27 01:32:00 -08004267 if (@$mods == 0) {
Eric Wong44320b92007-01-13 22:35:53 -08004268 $self->abort_edit;
4269 } else {
4270 $self->close_edit;
4271 }
Eric Wong6e8548c2007-01-27 01:32:00 -08004272 return scalar @$mods;
Eric Wong44320b92007-01-13 22:35:53 -08004273}
4274
Eric Wongd81bf822007-01-10 01:22:38 -08004275package Git::SVN::Ra;
Eric Wong6af1db42007-02-14 16:04:10 -08004276use vars qw/@ISA $config_dir $_log_window_size/;
Eric Wongd81bf822007-01-10 01:22:38 -08004277use strict;
4278use warnings;
Eric Wonga51cdb02007-09-07 04:00:40 -07004279my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
Eric Wongd81bf822007-01-10 01:22:38 -08004280
4281BEGIN {
4282 # enforce temporary pool usage for some simple functions
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004283 no strict 'refs';
Eric Wongbf8a40b2009-01-25 15:35:52 -08004284 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root
4285 get_file/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004286 my $SUPER = "SUPER::$f";
4287 *$f = sub {
4288 my $self = shift;
4289 my $pool = SVN::Pool->new;
4290 my @ret = $self->$SUPER(@_,$pool);
4291 $pool->clear;
4292 wantarray ? @ret : $ret[0];
4293 };
Eric Wongd81bf822007-01-10 01:22:38 -08004294 }
Eric Wongd81bf822007-01-10 01:22:38 -08004295}
4296
Steven Walter9ff74e92007-09-28 13:24:19 -04004297sub _auth_providers () {
4298 [
4299 SVN::Client::get_simple_provider(),
4300 SVN::Client::get_ssl_server_trust_file_provider(),
4301 SVN::Client::get_simple_prompt_provider(
4302 \&Git::SVN::Prompt::simple, 2),
4303 SVN::Client::get_ssl_client_cert_file_provider(),
4304 SVN::Client::get_ssl_client_cert_prompt_provider(
4305 \&Git::SVN::Prompt::ssl_client_cert, 2),
Sebastian Noack77266e92008-02-25 15:56:28 +01004306 SVN::Client::get_ssl_client_cert_pw_file_provider(),
Steven Walter9ff74e92007-09-28 13:24:19 -04004307 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
4308 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
4309 SVN::Client::get_username_provider(),
4310 SVN::Client::get_ssl_server_trust_prompt_provider(
4311 \&Git::SVN::Prompt::ssl_server_trust),
4312 SVN::Client::get_username_prompt_provider(
4313 \&Git::SVN::Prompt::username, 2)
4314 ]
4315}
4316
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004317sub escape_uri_only {
4318 my ($uri) = @_;
4319 my @tmp;
4320 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -07004321 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004322 push @tmp, $_;
4323 }
4324 join('/', @tmp);
4325}
4326
4327sub escape_url {
4328 my ($url) = @_;
4329 if ($url =~ m#^(https?)://([^/]+)(.*)$#) {
4330 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
4331 $url = "$scheme://$domain$uri";
4332 }
4333 $url;
4334}
4335
Eric Wongd81bf822007-01-10 01:22:38 -08004336sub new {
4337 my ($class, $url) = @_;
Eric Wongf6f09872007-01-18 18:22:18 -08004338 $url =~ s!/+$!!;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004339 return $RA if ($RA && $RA->{url} eq $url);
Eric Wongf6f09872007-01-18 18:22:18 -08004340
Eric Wongd81bf822007-01-10 01:22:38 -08004341 SVN::_Core::svn_config_ensure($config_dir, undef);
Steven Walter9ff74e92007-09-28 13:24:19 -04004342 my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
Eric Wongd81bf822007-01-10 01:22:38 -08004343 my $config = SVN::Core::config_get_config($config_dir);
Eric Wong7730fbe2007-07-04 14:07:42 -07004344 $RA = undef;
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004345 my $dont_store_passwords = 1;
4346 my $conf_t = ${$config}{'config'};
4347 {
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004348 no warnings 'once';
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004349 # The usage of $SVN::_Core::SVN_CONFIG_* variables
4350 # produces warnings that variables are used only once.
4351 # I had not found the better way to shut them up, so
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004352 # the warnings of type 'once' are disabled in this block.
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004353 if (SVN::_Core::svn_config_get_bool($conf_t,
4354 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4355 $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
4356 1) == 0) {
4357 SVN::_Core::svn_auth_set_parameter($baton,
4358 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
4359 bless (\$dont_store_passwords, "_p_void"));
4360 }
4361 if (SVN::_Core::svn_config_get_bool($conf_t,
4362 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4363 $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
4364 1) == 0) {
4365 $Git::SVN::Prompt::_no_auth_cache = 1;
4366 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004367 } # no warnings 'once'
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004368 my $self = SVN::Ra->new(url => escape_url($url), auth => $baton,
Eric Wongd81bf822007-01-10 01:22:38 -08004369 config => $config,
4370 pool => SVN::Pool->new,
4371 auth_provider_callbacks => $callbacks);
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004372 $self->{url} = $url;
Eric Wongd81bf822007-01-10 01:22:38 -08004373 $self->{svn_path} = $url;
4374 $self->{repos_root} = $self->get_repos_root;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08004375 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
Eric Wong0dc03d62007-05-13 01:04:43 -07004376 $self->{cache} = { check_path => { r => 0, data => {} },
4377 get_dir => { r => 0, data => {} } };
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004378 $RA = bless $self, $class;
Eric Wongd81bf822007-01-10 01:22:38 -08004379}
4380
Eric Wong0dc03d62007-05-13 01:04:43 -07004381sub check_path {
4382 my ($self, $path, $r) = @_;
4383 my $cache = $self->{cache}->{check_path};
4384 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
4385 return $cache->{data}->{$path};
4386 }
4387 my $pool = SVN::Pool->new;
4388 my $t = $self->SUPER::check_path($path, $r, $pool);
4389 $pool->clear;
4390 if ($r != $cache->{r}) {
4391 %{$cache->{data}} = ();
4392 $cache->{r} = $r;
4393 }
4394 $cache->{data}->{$path} = $t;
4395}
4396
4397sub get_dir {
4398 my ($self, $dir, $r) = @_;
4399 my $cache = $self->{cache}->{get_dir};
4400 if ($r == $cache->{r}) {
4401 if (my $x = $cache->{data}->{$dir}) {
4402 return wantarray ? @$x : $x->[0];
4403 }
4404 }
4405 my $pool = SVN::Pool->new;
4406 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
4407 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
4408 $pool->clear;
4409 if ($r != $cache->{r}) {
4410 %{$cache->{data}} = ();
4411 $cache->{r} = $r;
4412 }
4413 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
4414 wantarray ? (\%dirents, $r, $props) : \%dirents;
4415}
4416
Eric Wongd81bf822007-01-10 01:22:38 -08004417sub DESTROY {
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004418 # do not call the real DESTROY since we store ourselves in $RA
Eric Wongd81bf822007-01-10 01:22:38 -08004419}
4420
Eric Wong1ef626b2009-01-17 22:11:44 -08004421# get_log(paths, start, end, limit,
4422# discover_changed_paths, strict_node_history, receiver)
Eric Wongd81bf822007-01-10 01:22:38 -08004423sub get_log {
4424 my ($self, @args) = @_;
4425 my $pool = SVN::Pool->new;
Eric Wong1ef626b2009-01-17 22:11:44 -08004426
4427 # the limit parameter was not supported in SVN 1.1.x, so we
4428 # drop it. Therefore, the receiver callback passed to it
4429 # is made aware of this limitation by being wrapped if
4430 # the limit passed to is being wrapped.
4431 if ($SVN::Core::VERSION le '1.2.0') {
4432 my $limit = splice(@args, 3, 1);
4433 if ($limit > 0) {
4434 my $receiver = pop @args;
4435 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
4436 }
4437 }
Eric Wongd81bf822007-01-10 01:22:38 -08004438 my $ret = $self->SUPER::get_log(@args, $pool);
4439 $pool->clear;
4440 $ret;
4441}
4442
Steven Walter9ff74e92007-09-28 13:24:19 -04004443sub trees_match {
4444 my ($self, $url1, $rev1, $url2, $rev2) = @_;
4445 my $ctx = SVN::Client->new(auth => _auth_providers);
4446 my $out = IO::File->new_tmpfile;
4447
4448 # older SVN (1.1.x) doesn't take $pool as the last parameter for
4449 # $ctx->diff(), so we'll create a default one
4450 my $pool = SVN::Pool->new_default_sub;
4451
4452 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
4453 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
4454 $out->flush;
4455 my $ret = (($out->stat)[7] == 0);
4456 close $out or croak $!;
4457
4458 $ret;
4459}
4460
Eric Wongd81bf822007-01-10 01:22:38 -08004461sub get_commit_editor {
Eric Wong44320b92007-01-13 22:35:53 -08004462 my ($self, $log, $cb, $pool) = @_;
Eric Wongd81bf822007-01-10 01:22:38 -08004463 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
Eric Wong44320b92007-01-13 22:35:53 -08004464 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004465}
4466
Eric Wongd81bf822007-01-10 01:22:38 -08004467sub gs_do_update {
Eric Wong8a603772007-01-31 02:45:50 -08004468 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
4469 my $new = ($rev_a == $rev_b);
4470 my $path = $gs->{path};
4471
Eric Wong2e5e2482007-02-23 02:21:59 -08004472 if ($new && -e $gs->{index}) {
4473 unlink $gs->{index} or die
4474 "Couldn't unlink index: $gs->{index}: $!\n";
4475 }
Eric Wongd81bf822007-01-10 01:22:38 -08004476 my $pool = SVN::Pool->new;
Eric Wong8b8fc062007-01-22 11:44:57 -08004477 $editor->set_path_strip($path);
Eric Wong2b27f6c2007-01-28 04:59:05 -08004478 my (@pc) = split m#/#, $path;
4479 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
Eric Wong8a603772007-01-31 02:45:50 -08004480 1, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004481 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong2b27f6c2007-01-28 04:59:05 -08004482
4483 # Since we can't rely on svn_ra_reparent being available, we'll
4484 # just have to do some magic with set_path to make it so
4485 # we only want a partial path.
4486 my $sp = '';
4487 my $final = join('/', @pc);
4488 while (@pc) {
4489 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
4490 $sp .= '/' if length $sp;
4491 $sp .= shift @pc;
4492 }
4493 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
4494
Eric Wong2b27f6c2007-01-28 04:59:05 -08004495 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
4496
Eric Wongd81bf822007-01-10 01:22:38 -08004497 $reporter->finish_report($pool);
4498 $pool->clear;
4499 $editor->{git_commit_ok};
4500}
4501
Eric Wong2b27f6c2007-01-28 04:59:05 -08004502# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
4503# svn_ra_reparent didn't work before 1.4)
Eric Wongd81bf822007-01-10 01:22:38 -08004504sub gs_do_switch {
Eric Wong8a603772007-01-31 02:45:50 -08004505 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
4506 my $path = $gs->{path};
Eric Wongd81bf822007-01-10 01:22:38 -08004507 my $pool = SVN::Pool->new;
Eric Wong2b27f6c2007-01-28 04:59:05 -08004508
4509 my $full_url = $self->{url};
4510 my $old_url = $full_url;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004511 $full_url .= '/' . escape_uri_only($path) if length $path;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004512 my ($ra, $reparented);
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004513
4514 if ($old_url =~ m#^svn(\+ssh)?://#) {
4515 $_[0] = undef;
4516 $self = undef;
4517 $RA = undef;
4518 $ra = Git::SVN::Ra->new($full_url);
4519 $ra_invalid = 1;
4520 } elsif ($old_url ne $full_url) {
4521 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, $pool);
4522 $self->{url} = $full_url;
4523 $reparented = 1;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004524 }
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004525
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004526 $ra ||= $self;
Eric Wongf4392df2008-09-06 20:18:18 -07004527 $url_b = escape_url($url_b);
Eric Wong8a603772007-01-31 02:45:50 -08004528 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004529 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong8b8fc062007-01-22 11:44:57 -08004530 $reporter->set_path('', $rev_a, 0, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004531 $reporter->finish_report($pool);
Eric Wong2b27f6c2007-01-28 04:59:05 -08004532
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004533 if ($reparented) {
4534 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
4535 $self->{url} = $old_url;
4536 }
Eric Wong2b27f6c2007-01-28 04:59:05 -08004537
Eric Wongd81bf822007-01-10 01:22:38 -08004538 $pool->clear;
4539 $editor->{git_commit_ok};
4540}
4541
Eric Wongb54a9012007-06-13 02:37:03 -07004542sub longest_common_path {
4543 my ($gsv, $globs) = @_;
Eric Wongd2ae1432007-02-07 11:50:16 -08004544 my %common;
Eric Wonge5181922007-02-08 12:53:57 -08004545 my $common_max = scalar @$gsv;
4546
4547 foreach my $gs (@$gsv) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004548 my @tmp = split m#/#, $gs->{path};
4549 my $p = '';
4550 foreach (@tmp) {
4551 $p .= length($p) ? "/$_" : $_;
4552 $common{$p} ||= 0;
4553 $common{$p}++;
4554 }
4555 }
Eric Wonge5181922007-02-08 12:53:57 -08004556 $globs ||= [];
4557 $common_max += scalar @$globs;
4558 foreach my $glob (@$globs) {
4559 my @tmp = split m#/#, $glob->{path}->{left};
4560 my $p = '';
4561 foreach (@tmp) {
4562 $p .= length($p) ? "/$_" : $_;
4563 $common{$p} ||= 0;
4564 $common{$p}++;
4565 }
4566 }
4567
Eric Wongd2ae1432007-02-07 11:50:16 -08004568 my $longest_path = '';
4569 foreach (sort {length $b <=> length $a} keys %common) {
Eric Wonge5181922007-02-08 12:53:57 -08004570 if ($common{$_} == $common_max) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004571 $longest_path = $_;
4572 last;
4573 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004574 }
Eric Wongb54a9012007-06-13 02:37:03 -07004575 $longest_path;
4576}
4577
4578sub gs_fetch_loop_common {
4579 my ($self, $base, $head, $gsv, $globs) = @_;
4580 return if ($base > $head);
4581 my $inc = $_log_window_size;
4582 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
4583 my $longest_path = longest_common_path($gsv, $globs);
Eric Wonga51cdb02007-09-07 04:00:40 -07004584 my $ra_url = $self->{url};
Alex Vandiverc69700f2009-05-06 16:18:52 -04004585 my $find_trailing_edge;
Eric Wong0af9c9f2007-01-27 22:28:56 -08004586 while (1) {
Eric Wongd4eff2b2007-01-30 14:04:22 -08004587 my %revs;
Eric Wongd2ae1432007-02-07 11:50:16 -08004588 my $err;
Eric Wongf7c3fc42007-01-29 18:34:55 -08004589 my $err_handler = $SVN::Error::handler;
Eric Wongd2ae1432007-02-07 11:50:16 -08004590 $SVN::Error::handler = sub {
4591 ($err) = @_;
4592 skip_unknown_revs($err);
4593 };
4594 sub _cb {
4595 my ($paths, $r, $author, $date, $log) = @_;
4596 [ dup_changed_paths($paths),
4597 { author => $author, date => $date, log => $log } ];
4598 }
4599 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
4600 sub { $revs{$_[1]} = _cb(@_) });
Deskin Miller99366562009-02-08 19:33:18 -05004601 if ($err) {
4602 print "Checked through r$max\r";
Alex Vandiverc69700f2009-05-06 16:18:52 -04004603 } else {
4604 $find_trailing_edge = 1;
Deskin Miller99366562009-02-08 19:33:18 -05004605 }
Alex Vandiverc69700f2009-05-06 16:18:52 -04004606 if ($err and $find_trailing_edge) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004607 print STDERR "Path '$longest_path' ",
4608 "was probably deleted:\n",
4609 $err->expanded_message,
4610 "\nWill attempt to follow ",
4611 "revisions r$min .. r$max ",
4612 "committed before the deletion\n";
4613 my $hi = $max;
4614 while (--$hi >= $min) {
4615 my $ok;
4616 $self->get_log([$longest_path], $min, $hi,
4617 0, 1, 1, sub {
Alex Vandiverb6c61772009-05-06 16:18:53 -04004618 $ok = $_[1];
Eric Wongd2ae1432007-02-07 11:50:16 -08004619 $revs{$_[1]} = _cb(@_) });
4620 if ($ok) {
4621 print STDERR "r$min .. r$ok OK\n";
4622 last;
4623 }
4624 }
Alex Vandiverc69700f2009-05-06 16:18:52 -04004625 $find_trailing_edge = 0;
Eric Wongd2ae1432007-02-07 11:50:16 -08004626 }
Eric Wongd4eff2b2007-01-30 14:04:22 -08004627 $SVN::Error::handler = $err_handler;
Eric Wongfbcc1732007-02-06 18:35:30 -08004628
Eric Wonge5181922007-02-08 12:53:57 -08004629 my %exists = map { $_->{path} => $_ } @$gsv;
Eric Wongd4eff2b2007-01-30 14:04:22 -08004630 foreach my $r (sort {$a <=> $b} keys %revs) {
Eric Wongfbcc1732007-02-06 18:35:30 -08004631 my ($paths, $logged) = @{$revs{$r}};
Eric Wonge5181922007-02-08 12:53:57 -08004632
4633 foreach my $gs ($self->match_globs(\%exists, $paths,
4634 $globs, $r)) {
Eric Wong060610c2007-12-08 23:27:41 -08004635 if ($gs->rev_map_max >= $r) {
Eric Wongfbcc1732007-02-06 18:35:30 -08004636 next;
4637 }
4638 next unless $gs->match_paths($paths, $r);
4639 $gs->{logged_rev_props} = $logged;
Eric Wonge8d120b2007-02-14 16:29:52 -08004640 if (my $last_commit = $gs->last_commit) {
4641 $gs->assert_index_clean($last_commit);
4642 }
Eric Wongfbcc1732007-02-06 18:35:30 -08004643 my $log_entry = $gs->do_fetch($paths, $r);
4644 if ($log_entry) {
Eric Wong0af9c9f2007-01-27 22:28:56 -08004645 $gs->do_git_commit($log_entry);
4646 }
Eric Wong321b1842008-01-02 10:10:03 -08004647 $INDEX_FILES{$gs->{index}} = 1;
Eric Wong0af9c9f2007-01-27 22:28:56 -08004648 }
Eric Wonge5181922007-02-08 12:53:57 -08004649 foreach my $g (@$globs) {
Eric Wong93f26892007-02-11 01:20:26 -08004650 my $k = "svn-remote.$g->{remote}." .
4651 "$g->{t}-maxRev";
4652 Git::SVN::tmp_config($k, $r);
Eric Wonge5181922007-02-08 12:53:57 -08004653 }
Eric Wonga51cdb02007-09-07 04:00:40 -07004654 if ($ra_invalid) {
4655 $_[0] = undef;
4656 $self = undef;
4657 $RA = undef;
4658 $self = Git::SVN::Ra->new($ra_url);
4659 $ra_invalid = undef;
4660 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004661 }
Eric Wong9c93fee2007-01-31 17:22:31 -08004662 # pre-fill the .rev_db since it'll eventually get filled in
4663 # with '0' x40 if something new gets committed
Eric Wonge5181922007-02-08 12:53:57 -08004664 foreach my $gs (@$gsv) {
Eric Wong66ab84b2007-12-08 23:27:42 -08004665 next if $gs->rev_map_max >= $max;
4666 next if defined $gs->rev_map_get($max);
4667 $gs->rev_map_set($max, 0 x40);
Eric Wong9c93fee2007-01-31 17:22:31 -08004668 }
Eric Wongc3560e52007-02-12 16:03:32 -08004669 foreach my $g (@$globs) {
4670 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
4671 Git::SVN::tmp_config($k, $max);
4672 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004673 last if $max >= $head;
4674 $min = $max + 1;
4675 $max += $inc;
4676 $max = $head if ($max > $head);
4677 }
Karl Hasselström94bc9142008-02-03 17:56:18 +01004678 Git::SVN::gc();
Eric Wong0af9c9f2007-01-27 22:28:56 -08004679}
4680
Marcus Griep570d35c2008-08-08 01:41:57 -07004681sub get_dir_globbed {
4682 my ($self, $left, $depth, $r) = @_;
4683
4684 my @x = eval { $self->get_dir($left, $r) };
4685 return unless scalar @x == 3;
4686 my $dirents = $x[0];
4687 my @finalents;
4688 foreach my $de (keys %$dirents) {
4689 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
4690 if ($depth > 1) {
4691 my @args = ("$left/$de", $depth - 1, $r);
4692 foreach my $dir ($self->get_dir_globbed(@args)) {
4693 push @finalents, "$de/$dir";
4694 }
4695 } else {
4696 push @finalents, $de;
4697 }
4698 }
4699 @finalents;
4700}
4701
Eric Wonge5181922007-02-08 12:53:57 -08004702sub match_globs {
4703 my ($self, $exists, $paths, $globs, $r) = @_;
Eric Wong74a81222007-02-10 13:28:50 -08004704
4705 sub get_dir_check {
4706 my ($self, $exists, $g, $r) = @_;
Marcus Griep570d35c2008-08-08 01:41:57 -07004707
4708 my @dirs = $self->get_dir_globbed($g->{path}->{left},
4709 $g->{path}->{depth},
4710 $r);
4711
4712 foreach my $de (@dirs) {
Eric Wong74a81222007-02-10 13:28:50 -08004713 my $p = $g->{path}->full_path($de);
4714 next if $exists->{$p};
4715 next if (length $g->{path}->{right} &&
4716 ($self->check_path($p, $r) !=
4717 $SVN::Node::dir));
4718 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
4719 $g->{ref}->full_path($de), 1);
4720 }
4721 }
Eric Wonge5181922007-02-08 12:53:57 -08004722 foreach my $g (@$globs) {
Eric Wong74a81222007-02-10 13:28:50 -08004723 if (my $path = $paths->{"/$g->{path}->{left}"}) {
4724 if ($path->{action} =~ /^[AR]$/) {
4725 get_dir_check($self, $exists, $g, $r);
4726 }
4727 }
Eric Wonge5181922007-02-08 12:53:57 -08004728 foreach (keys %$paths) {
Eric Wong28710f72007-02-14 13:32:21 -08004729 if (/$g->{path}->{left_regex}/ &&
4730 !/$g->{path}->{regex}/) {
Eric Wong74a81222007-02-10 13:28:50 -08004731 next if $paths->{$_}->{action} !~ /^[AR]$/;
4732 get_dir_check($self, $exists, $g, $r);
4733 }
Eric Wonge5181922007-02-08 12:53:57 -08004734 next unless /$g->{path}->{regex}/;
4735 my $p = $1;
4736 my $pathname = $g->{path}->full_path($p);
4737 next if $exists->{$pathname};
Eric Wong0c1ec5a2007-04-18 00:17:33 -07004738 next if ($self->check_path($pathname, $r) !=
4739 $SVN::Node::dir);
Eric Wonge5181922007-02-08 12:53:57 -08004740 $exists->{$pathname} = Git::SVN->init(
4741 $self->{url}, $pathname, undef,
4742 $g->{ref}->full_path($p), 1);
4743 }
4744 my $c = '';
4745 foreach (split m#/#, $g->{path}->{left}) {
4746 $c .= "/$_";
4747 next unless ($paths->{$c} &&
Eric Wong74a81222007-02-10 13:28:50 -08004748 ($paths->{$c}->{action} =~ /^[AR]$/));
4749 get_dir_check($self, $exists, $g, $r);
Eric Wonge5181922007-02-08 12:53:57 -08004750 }
4751 }
4752 values %$exists;
4753}
4754
Eric Wonge6434f82007-01-23 16:29:23 -08004755sub minimize_url {
4756 my ($self) = @_;
4757 return $self->{url} if ($self->{url} eq $self->{repos_root});
4758 my $url = $self->{repos_root};
4759 my @components = split(m!/!, $self->{svn_path});
4760 my $c = '';
4761 do {
4762 $url .= "/$c" if length $c;
4763 eval { (ref $self)->new($url)->get_latest_revnum };
4764 } while ($@ && ($c = shift @components));
4765 $url;
4766}
4767
Eric Wongd81bf822007-01-10 01:22:38 -08004768sub can_do_switch {
4769 my $self = shift;
4770 unless (defined $can_do_switch) {
4771 my $pool = SVN::Pool->new;
4772 my $rep = eval {
4773 $self->do_switch(1, '', 0, $self->{url},
4774 SVN::Delta::Editor->new, $pool);
4775 };
4776 if ($@) {
4777 $can_do_switch = 0;
4778 } else {
4779 $rep->abort_report($pool);
4780 $can_do_switch = 1;
4781 }
4782 $pool->clear;
4783 }
4784 $can_do_switch;
4785}
4786
Eric Wong0af9c9f2007-01-27 22:28:56 -08004787sub skip_unknown_revs {
4788 my ($err) = @_;
4789 my $errno = $err->apr_err();
4790 # Maybe the branch we're tracking didn't
4791 # exist when the repo started, so it's
4792 # not an error if it doesn't, just continue
4793 #
4794 # Wonderfully consistent library, eh?
4795 # 160013 - svn:// and file://
4796 # 175002 - http(s)://
4797 # 175007 - http(s):// (this repo required authorization, too...)
4798 # More codes may be discovered later...
4799 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
Eric Wonga6a15a92007-03-30 17:54:48 -07004800 my $err_key = $err->expanded_message;
4801 # revision numbers change every time, filter them out
4802 $err_key =~ s/\d+/\0/g;
4803 $err_key = "$errno\0$err_key";
4804 unless ($ignored_err{$err_key}) {
4805 warn "W: Ignoring error from SVN, path probably ",
4806 "does not exist: ($errno): ",
4807 $err->expanded_message,"\n";
Eric Wongeee8a172008-01-07 02:40:40 -08004808 warn "W: Do not be alarmed at the above message ",
4809 "git-svn is just searching aggressively for ",
4810 "old history.\n",
4811 "This may take a while on large repositories\n";
Eric Wonga6a15a92007-03-30 17:54:48 -07004812 $ignored_err{$err_key} = 1;
4813 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004814 return;
4815 }
4816 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
4817}
4818
4819# svn_log_changed_path_t objects passed to get_log are likely to be
4820# overwritten even if only the refs are copied to an external variable,
4821# so we should dup the structures in their entirety. Using an externally
4822# passed pool (instead of our temporary and quickly cleared pool in
4823# Git::SVN::Ra) does not help matters at all...
4824sub dup_changed_paths {
4825 my ($paths) = @_;
4826 return undef unless $paths;
4827 my %ret;
4828 foreach my $p (keys %$paths) {
4829 my $i = $paths->{$p};
4830 my %s = map { $_ => $i->$_ }
4831 qw/copyfrom_path copyfrom_rev action/;
4832 $ret{$p} = \%s;
4833 }
4834 \%ret;
4835}
4836
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004837package Git::SVN::Log;
4838use strict;
4839use warnings;
4840use POSIX qw/strftime/;
Ben Waltone8717842009-02-24 14:44:49 -05004841use Time::Local;
David D Kilzer111947e2007-11-11 22:56:52 -08004842use constant commit_log_separator => ('-' x 72) . "\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004843use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
4844 %rusers $show_commit $incremental/;
4845my $l_fmt;
4846
4847sub cmt_showable {
4848 my ($c) = @_;
4849 return 1 if defined $c->{r};
Eric Wongc16d0872007-04-08 00:59:22 -07004850
4851 # big commit message got truncated by the 16k pretty buffer in rev-list
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004852 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
4853 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
Eric Wongc16d0872007-04-08 00:59:22 -07004854 @{$c->{l}} = ();
Eric Wong44320b92007-01-13 22:35:53 -08004855 my @log = command(qw/cat-file commit/, $c->{c});
Eric Wongc16d0872007-04-08 00:59:22 -07004856
4857 # shift off the headers
4858 shift @log while ($log[0] ne '');
Eric Wong44320b92007-01-13 22:35:53 -08004859 shift @log;
Eric Wongc16d0872007-04-08 00:59:22 -07004860
4861 # TODO: make $c->{l} not have a trailing newline in the future
4862 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004863
4864 (undef, $c->{r}, undef) = ::extract_metadata(
Eric Wong44320b92007-01-13 22:35:53 -08004865 (grep(/^git-svn-id: /, @log))[-1]);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004866 }
4867 return defined $c->{r};
4868}
4869
4870sub log_use_color {
Jeff Kingcd459e32007-12-11 01:28:42 -05004871 return $color || Git->repository->get_colorbool('color.diff');
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004872}
4873
4874sub git_svn_log_cmd {
Eric Wong3bc718b2007-02-13 17:09:40 -08004875 my ($r_min, $r_max, @args) = @_;
4876 my $head = 'HEAD';
Eric Wong6ed77262007-08-15 09:55:18 -07004877 my (@files, @log_opts);
Eric Wong3bc718b2007-02-13 17:09:40 -08004878 foreach my $x (@args) {
Eric Wong6ed77262007-08-15 09:55:18 -07004879 if ($x eq '--' || @files) {
4880 push @files, $x;
4881 } else {
4882 if (::verify_ref("$x^0")) {
4883 $head = $x;
4884 } else {
4885 push @log_opts, $x;
4886 }
4887 }
Eric Wong3bc718b2007-02-13 17:09:40 -08004888 }
4889
Eric Wong13c823f2007-04-08 00:59:19 -07004890 my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
4891 $gs ||= Git::SVN->_new;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004892 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
4893 $gs->refname);
4894 push @cmd, '-r' unless $non_recursive;
4895 push @cmd, qw/--raw --name-status/ if $verbose;
4896 push @cmd, '--color' if log_use_color();
Eric Wong6ed77262007-08-15 09:55:18 -07004897 push @cmd, @log_opts;
4898 if (defined $r_max && $r_max == $r_min) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004899 push @cmd, '--max-count=1';
Eric Wong060610c2007-12-08 23:27:41 -08004900 if (my $c = $gs->rev_map_get($r_max)) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004901 push @cmd, $c;
4902 }
Eric Wong6ed77262007-08-15 09:55:18 -07004903 } elsif (defined $r_max) {
David D Kilzer111947e2007-11-11 22:56:52 -08004904 if ($r_max < $r_min) {
4905 ($r_min, $r_max) = ($r_max, $r_min);
4906 }
4907 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
4908 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
4909 # If there are no commits in the range, both $c_max and $c_min
4910 # will be undefined. If there is at least 1 commit in the
4911 # range, both will be defined.
4912 return () if !defined $c_min || !defined $c_max;
4913 if ($c_min eq $c_max) {
4914 push @cmd, '--max-count=1', $c_min;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004915 } else {
David D Kilzer111947e2007-11-11 22:56:52 -08004916 push @cmd, '--boundary', "$c_min..$c_max";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004917 }
4918 }
Eric Wong6ed77262007-08-15 09:55:18 -07004919 return (@cmd, @files);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004920}
4921
4922# adapted from pager.c
4923sub config_pager {
4924 $pager ||= $ENV{GIT_PAGER} || $ENV{PAGER};
4925 if (!defined $pager) {
4926 $pager = 'less';
4927 } elsif (length $pager == 0 || $pager eq 'cat') {
4928 $pager = undef;
4929 }
Jeff Kingcd459e32007-12-11 01:28:42 -05004930 $ENV{GIT_PAGER_IN_USE} = defined($pager);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004931}
4932
4933sub run_pager {
Eric Wongb0193042007-09-21 18:48:45 -07004934 return unless -t *STDOUT && defined $pager;
Marcus Griep971e6282008-09-10 11:09:46 -04004935 pipe my ($rfd, $wfd) or return;
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004936 defined(my $pid = fork) or ::fatal "Can't fork: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004937 if (!$pid) {
4938 open STDOUT, '>&', $wfd or
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004939 ::fatal "Can't redirect to stdout: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004940 return;
4941 }
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004942 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004943 $ENV{LESS} ||= 'FRSX';
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004944 exec $pager or ::fatal "Can't run pager: $! ($pager)";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004945}
4946
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004947sub format_svn_date {
Ben Waltone8717842009-02-24 14:44:49 -05004948 # some systmes don't handle or mishandle %z, so be creative.
Ben Walton736e6192009-02-27 22:11:45 -05004949 my $t = shift || time;
Ben Waltone8717842009-02-24 14:44:49 -05004950 my $gm = timelocal(gmtime($t));
4951 my $sign = qw( + + - )[ $t <=> $gm ];
4952 my $gmoff = sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
4953 return strftime("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004954}
4955
4956sub parse_git_date {
4957 my ($t, $tz) = @_;
4958 # Date::Parse isn't in the standard Perl distro :(
4959 if ($tz =~ s/^\+//) {
4960 $t += tz_to_s_offset($tz);
4961 } elsif ($tz =~ s/^\-//) {
4962 $t -= tz_to_s_offset($tz);
4963 }
4964 return $t;
4965}
4966
4967sub set_local_timezone {
4968 if (defined $TZ) {
4969 $ENV{TZ} = $TZ;
4970 } else {
4971 delete $ENV{TZ};
4972 }
4973}
4974
Eric Wong21819a32007-01-27 14:38:10 -08004975sub tz_to_s_offset {
4976 my ($tz) = @_;
4977 $tz =~ s/(\d\d)$//;
4978 return ($1 * 60) + ($tz * 3600);
4979}
4980
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004981sub get_author_info {
4982 my ($dest, $author, $t, $tz) = @_;
4983 $author =~ s/(?:^\s*|\s*$)//g;
4984 $dest->{a_raw} = $author;
4985 my $au;
Eric Wong1c8443b2007-01-14 02:17:00 -08004986 if ($::_authors) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004987 $au = $rusers{$author} || undef;
4988 }
4989 if (!$au) {
4990 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
4991 }
4992 $dest->{t} = $t;
4993 $dest->{tz} = $tz;
4994 $dest->{a} = $au;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004995 $dest->{t_utc} = parse_git_date($t, $tz);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004996}
4997
4998sub process_commit {
4999 my ($c, $r_min, $r_max, $defer) = @_;
5000 if (defined $r_min && defined $r_max) {
5001 if ($r_min == $c->{r} && $r_min == $r_max) {
5002 show_commit($c);
5003 return 0;
5004 }
5005 return 1 if $r_min == $r_max;
5006 if ($r_min < $r_max) {
5007 # we need to reverse the print order
5008 return 0 if (defined $limit && --$limit < 0);
5009 push @$defer, $c;
5010 return 1;
5011 }
5012 if ($r_min != $r_max) {
5013 return 1 if ($r_min < $c->{r});
5014 return 1 if ($r_max > $c->{r});
5015 }
5016 }
5017 return 0 if (defined $limit && --$limit < 0);
5018 show_commit($c);
5019 return 1;
5020}
5021
5022sub show_commit {
5023 my $c = shift;
5024 if ($oneline) {
5025 my $x = "\n";
5026 if (my $l = $c->{l}) {
5027 while ($l->[0] =~ /^\s*$/) { shift @$l }
5028 $x = $l->[0];
5029 }
5030 $l_fmt ||= 'A' . length($c->{r});
5031 print 'r',pack($l_fmt, $c->{r}),' | ';
5032 print "$c->{c} | " if $show_commit;
5033 print $x;
5034 } else {
5035 show_commit_normal($c);
5036 }
5037}
5038
5039sub show_commit_changed_paths {
5040 my ($c) = @_;
5041 return unless $c->{changed};
5042 print "Changed paths:\n", @{$c->{changed}};
5043}
5044
5045sub show_commit_normal {
5046 my ($c) = @_;
David D Kilzer111947e2007-11-11 22:56:52 -08005047 print commit_log_separator, "r$c->{r} | ";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005048 print "$c->{c} | " if $show_commit;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005049 print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005050 my $nr_line = 0;
5051
5052 if (my $l = $c->{l}) {
5053 while ($l->[$#$l] eq "\n" && $#$l > 0
5054 && $l->[($#$l - 1)] eq "\n") {
5055 pop @$l;
5056 }
5057 $nr_line = scalar @$l;
5058 if (!$nr_line) {
5059 print "1 line\n\n\n";
5060 } else {
5061 if ($nr_line == 1) {
5062 $nr_line = '1 line';
5063 } else {
5064 $nr_line .= ' lines';
5065 }
5066 print $nr_line, "\n";
5067 show_commit_changed_paths($c);
5068 print "\n";
5069 print $_ foreach @$l;
5070 }
5071 } else {
5072 print "1 line\n";
5073 show_commit_changed_paths($c);
5074 print "\n";
5075
5076 }
Eric Wong488a63e2007-02-15 00:40:42 -08005077 foreach my $x (qw/raw stat diff/) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005078 if ($c->{$x}) {
5079 print "\n";
5080 print $_ foreach @{$c->{$x}}
5081 }
5082 }
5083}
5084
5085sub cmd_show_log {
5086 my (@args) = @_;
5087 my ($r_min, $r_max);
5088 my $r_last = -1; # prevent dupes
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005089 set_local_timezone();
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005090 if (defined $::_revision) {
5091 if ($::_revision =~ /^(\d+):(\d+)$/) {
5092 ($r_min, $r_max) = ($1, $2);
5093 } elsif ($::_revision =~ /^\d+$/) {
5094 $r_min = $r_max = $::_revision;
5095 } else {
5096 ::fatal "-r$::_revision is not supported, use ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005097 "standard 'git log' arguments instead";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005098 }
5099 }
5100
5101 config_pager();
Eric Wong6ed77262007-08-15 09:55:18 -07005102 @args = git_svn_log_cmd($r_min, $r_max, @args);
David D Kilzer111947e2007-11-11 22:56:52 -08005103 if (!@args) {
5104 print commit_log_separator unless $incremental || $oneline;
5105 return;
5106 }
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005107 my $log = command_output_pipe(@args);
5108 run_pager();
Eric Wong488a63e2007-02-15 00:40:42 -08005109 my (@k, $c, $d, $stat);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005110 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
5111 while (<$log>) {
David D Kilzer60f3ff12007-11-10 22:10:34 -08005112 if (/^${esc_color}commit -?($::sha1_short)/o) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005113 my $cmt = $1;
5114 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
5115 $r_last = $c->{r};
5116 process_commit($c, $r_min, $r_max, \@k) or
5117 goto out;
5118 }
5119 $d = undef;
5120 $c = { c => $cmt };
5121 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
5122 get_author_info($c, $1, $2, $3);
5123 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
5124 # ignore
5125 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
5126 push @{$c->{raw}}, $_;
5127 } elsif (/^${esc_color}[ACRMDT]\t/) {
5128 # we could add $SVN->{svn_path} here, but that requires
5129 # remote access at the moment (repo_path_split)...
5130 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
5131 push @{$c->{changed}}, $_;
5132 } elsif (/^${esc_color}diff /o) {
5133 $d = 1;
5134 push @{$c->{diff}}, $_;
5135 } elsif ($d) {
5136 push @{$c->{diff}}, $_;
Eric Wong488a63e2007-02-15 00:40:42 -08005137 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
5138 $esc_color*[\+\-]*$esc_color$/x) {
5139 $stat = 1;
5140 push @{$c->{stat}}, $_;
5141 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
5142 push @{$c->{stat}}, $_;
5143 $stat = undef;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005144 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
5145 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
5146 } elsif (s/^${esc_color} //o) {
5147 push @{$c->{l}}, $_;
5148 }
5149 }
5150 if ($c && defined $c->{r} && $c->{r} != $r_last) {
5151 $r_last = $c->{r};
5152 process_commit($c, $r_min, $r_max, \@k);
5153 }
5154 if (@k) {
David D Kilzer111947e2007-11-11 22:56:52 -08005155 ($r_min, $r_max) = ($r_max, $r_min);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005156 process_commit($_, $r_min, $r_max) foreach reverse @k;
5157 }
5158out:
Eric Wongc843c462007-01-12 03:07:31 -08005159 close $log;
David D Kilzer111947e2007-11-11 22:56:52 -08005160 print commit_log_separator unless $incremental || $oneline;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005161}
5162
Tim Stoakes6fb53752008-02-10 15:21:08 +10305163sub cmd_blame {
Steven Grimm4be40382008-05-10 22:11:18 -07005164 my $path = pop;
Tim Stoakes6fb53752008-02-10 15:21:08 +10305165
5166 config_pager();
5167 run_pager();
5168
Steven Grimm4be40382008-05-10 22:11:18 -07005169 my ($fh, $ctx, $rev);
5170
5171 if ($_git_format) {
5172 ($fh, $ctx) = command_output_pipe('blame', @_, $path);
5173 while (my $line = <$fh>) {
5174 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
5175 # Uncommitted edits show up as a rev ID of
5176 # all zeros, which we can't look up with
5177 # cmt_metadata
5178 if ($1 !~ /^0+$/) {
5179 (undef, $rev, undef) =
5180 ::cmt_metadata($1);
5181 $rev = '0' if (!$rev);
5182 } else {
5183 $rev = '0';
5184 }
5185 $rev = sprintf('%-10s', $rev);
5186 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
5187 }
5188 print $line;
Tim Stoakes6fb53752008-02-10 15:21:08 +10305189 }
Steven Grimm4be40382008-05-10 22:11:18 -07005190 } else {
5191 ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD',
5192 '--', $path);
5193 my ($sha1);
5194 my %authors;
Boris Byk6ea42032009-04-11 00:32:41 +04005195 my @buffer;
5196 my %dsha; #distinct sha keys
5197
Steven Grimm4be40382008-05-10 22:11:18 -07005198 while (my $line = <$fh>) {
Boris Byk6ea42032009-04-11 00:32:41 +04005199 push @buffer, $line;
Steven Grimm4be40382008-05-10 22:11:18 -07005200 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
Boris Byk6ea42032009-04-11 00:32:41 +04005201 $dsha{$1} = 1;
5202 }
5203 }
5204
5205 my $s2r = ::cmt_sha2rev_batch([keys %dsha]);
5206
5207 foreach my $line (@buffer) {
5208 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
5209 $rev = $s2r->{$1};
5210 $rev = '0' if (!$rev)
Steven Grimm4be40382008-05-10 22:11:18 -07005211 }
5212 elsif ($line =~ /^author (.*)/) {
5213 $authors{$rev} = $1;
5214 $authors{$rev} =~ s/\s/_/g;
5215 }
5216 elsif ($line =~ /^\t(.*)$/) {
5217 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
5218 }
5219 }
Tim Stoakes6fb53752008-02-10 15:21:08 +10305220 }
5221 command_close_pipe($fh, $ctx);
5222}
5223
Eric Wong706587f2007-01-18 17:50:01 -08005224package Git::SVN::Migration;
5225# these version numbers do NOT correspond to actual version numbers
5226# of git nor git-svn. They are just relative.
5227#
5228# v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
5229#
5230# v1 layout: .git/$id/info/url, refs/remotes/$id
5231#
5232# v2 layout: .git/svn/$id/info/url, refs/remotes/$id
5233#
5234# v3 layout: .git/svn/$id, refs/remotes/$id
5235# - info/url may remain for backwards compatibility
5236# - this is what we migrate up to this layout automatically,
5237# - this will be used by git svn init on single branches
Eric Wong26a62d52007-02-12 13:25:25 -08005238# v3.1 layout (auto migrated):
5239# - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
5240# for backwards compatibility
Eric Wong706587f2007-01-18 17:50:01 -08005241#
5242# v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
5243# - this is only created for newly multi-init-ed
5244# repositories. Similar in spirit to the
5245# --use-separate-remotes option in git-clone (now default)
5246# - we do not automatically migrate to this (following
5247# the example set by core git)
Eric Wong060610c2007-12-08 23:27:41 -08005248#
5249# v5 layout: .rev_db.$UUID => .rev_map.$UUID
5250# - newer, more-efficient format that uses 24-bytes per record
5251# with no filler space.
5252# - use xxd -c24 < .rev_map.$UUID to view and debug
5253# - This is a one-way migration, repositories updated to the
5254# new format will not be able to use old git-svn without
5255# rebuilding the .rev_db. Rebuilding the rev_db is not
5256# possible if noMetadata or useSvmProps are set; but should
5257# be no problem for users that use the (sensible) defaults.
Eric Wong706587f2007-01-18 17:50:01 -08005258use strict;
5259use warnings;
5260use Carp qw/croak/;
5261use File::Path qw/mkpath/;
Eric Wong47e39c52007-01-21 04:27:09 -08005262use File::Basename qw/dirname basename/;
5263use vars qw/$_minimize/;
Eric Wong706587f2007-01-18 17:50:01 -08005264
5265sub migrate_from_v0 {
5266 my $git_dir = $ENV{GIT_DIR};
5267 return undef unless -d $git_dir;
5268 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5269 my $migrated = 0;
5270 while (<$fh>) {
5271 chomp;
5272 my ($id, $orig_ref) = ($_, $_);
5273 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
5274 next unless -f "$git_dir/$id/info/url";
5275 my $new_ref = "refs/remotes/$id";
5276 if (::verify_ref("$new_ref^0")) {
5277 print STDERR "W: $orig_ref is probably an old ",
5278 "branch used by an ancient version of ",
5279 "git-svn.\n",
5280 "However, $new_ref also exists.\n",
5281 "We will not be able ",
5282 "to use this branch until this ",
5283 "ambiguity is resolved.\n";
5284 next;
5285 }
5286 print STDERR "Migrating from v0 layout...\n" if !$migrated;
5287 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
5288 command_noisy('update-ref', $new_ref, $orig_ref);
5289 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
5290 $migrated++;
5291 }
5292 command_close_pipe($fh, $ctx);
5293 print STDERR "Done migrating from v0 layout...\n" if $migrated;
5294 $migrated;
5295}
5296
5297sub migrate_from_v1 {
5298 my $git_dir = $ENV{GIT_DIR};
5299 my $migrated = 0;
5300 return $migrated unless -d $git_dir;
5301 my $svn_dir = "$git_dir/svn";
5302
5303 # just in case somebody used 'svn' as their $id at some point...
5304 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
5305
5306 print STDERR "Migrating from a git-svn v1 layout...\n";
5307 mkpath([$svn_dir]);
5308 print STDERR "Data from a previous version of git-svn exists, but\n\t",
5309 "$svn_dir\n\t(required for this version ",
Frederik Schwarzer8f510be2008-07-14 18:30:24 +02005310 "($::VERSION) of git-svn) does not exist.\n";
Eric Wong706587f2007-01-18 17:50:01 -08005311 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5312 while (<$fh>) {
5313 my $x = $_;
5314 next unless $x =~ s#^refs/remotes/##;
5315 chomp $x;
5316 next unless -f "$git_dir/$x/info/url";
5317 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
5318 next unless $u;
5319 my $dn = dirname("$git_dir/svn/$x");
5320 mkpath([$dn]) unless -d $dn;
5321 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
5322 mkpath(["$git_dir/svn/svn"]);
5323 print STDERR " - $git_dir/$x/info => ",
5324 "$git_dir/svn/$x/info\n";
5325 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
5326 croak "$!: $x";
5327 # don't worry too much about these, they probably
5328 # don't exist with repos this old (save for index,
5329 # and we can easily regenerate that)
5330 foreach my $f (qw/unhandled.log index .rev_db/) {
5331 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
5332 }
5333 } else {
5334 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
5335 rename "$git_dir/$x", "$git_dir/svn/$x" or
5336 croak "$!: $x";
5337 }
5338 $migrated++;
5339 }
5340 command_close_pipe($fh, $ctx);
5341 print STDERR "Done migrating from a git-svn v1 layout\n";
5342 $migrated;
5343}
5344
5345sub read_old_urls {
5346 my ($l_map, $pfx, $path) = @_;
5347 my @dir;
5348 foreach (<$path/*>) {
5349 if (-r "$_/info/url") {
5350 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
5351 my $ref_id = $pfx . basename $_;
5352 my $url = ::file_to_s("$_/info/url");
5353 $l_map->{$ref_id} = $url;
5354 } elsif (-d $_) {
5355 push @dir, $_;
5356 }
5357 }
5358 foreach (@dir) {
5359 my $x = $_;
5360 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
5361 read_old_urls($l_map, $x, $_);
5362 }
5363}
5364
5365sub migrate_from_v2 {
5366 my @cfg = command(qw/config -l/);
5367 return if grep /^svn-remote\..+\.url=/, @cfg;
5368 my %l_map;
5369 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
5370 my $migrated = 0;
5371
5372 foreach my $ref_id (sort keys %l_map) {
Eric Wong471bc002007-02-01 04:06:27 -08005373 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
5374 if ($@) {
5375 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
5376 }
Eric Wong706587f2007-01-18 17:50:01 -08005377 $migrated++;
5378 }
5379 $migrated;
5380}
5381
Eric Wong47e39c52007-01-21 04:27:09 -08005382sub minimize_connections {
5383 my $r = Git::SVN::read_all_remotes();
5384 my $new_urls = {};
5385 my $root_repos = {};
5386 foreach my $repo_id (keys %$r) {
5387 my $url = $r->{$repo_id}->{url} or next;
5388 my $fetch = $r->{$repo_id}->{fetch} or next;
5389 my $ra = Git::SVN::Ra->new($url);
5390
5391 # skip existing cases where we already connect to the root
5392 if (($ra->{url} eq $ra->{repos_root}) ||
Eric Wong7829f202008-06-28 20:40:32 -07005393 ($ra->{repos_root} eq $repo_id)) {
Eric Wong47e39c52007-01-21 04:27:09 -08005394 $root_repos->{$ra->{url}} = $repo_id;
5395 next;
5396 }
5397
5398 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
5399 my $root_path = $ra->{url};
Eric Wong4e9f6cc2007-02-09 12:17:57 -08005400 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
Eric Wong47e39c52007-01-21 04:27:09 -08005401 foreach my $path (keys %$fetch) {
5402 my $ref_id = $fetch->{$path};
5403 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
5404
5405 # make sure we can read when connecting to
5406 # a higher level of a repository
5407 my ($last_rev, undef) = $gs->last_rev_commit;
5408 if (!defined $last_rev) {
5409 $last_rev = eval {
5410 $root_ra->get_latest_revnum;
5411 };
5412 next if $@;
5413 }
5414 my $new = $root_path;
5415 $new .= length $path ? "/$path" : '';
5416 eval {
5417 $root_ra->get_log([$new], $last_rev, $last_rev,
5418 0, 0, 1, sub { });
5419 };
5420 next if $@;
5421 $new_urls->{$ra->{repos_root}}->{$new} =
5422 { ref_id => $ref_id,
5423 old_repo_id => $repo_id,
5424 old_path => $path };
5425 }
5426 }
5427
5428 my @emptied;
5429 foreach my $url (keys %$new_urls) {
5430 # see if we can re-use an existing [svn-remote "repo_id"]
5431 # instead of creating a(n ugly) new section:
Eric Wong7829f202008-06-28 20:40:32 -07005432 my $repo_id = $root_repos->{$url} || $url;
Eric Wong47e39c52007-01-21 04:27:09 -08005433
5434 my $fetch = $new_urls->{$url};
5435 foreach my $path (keys %$fetch) {
5436 my $x = $fetch->{$path};
5437 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
5438 my $pfx = "svn-remote.$x->{old_repo_id}";
5439
5440 my $old_fetch = quotemeta("$x->{old_path}:".
5441 "refs/remotes/$x->{ref_id}");
Eric Wong8b8fc062007-01-22 11:44:57 -08005442 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08005443 "$pfx.fetch", '^'. $old_fetch . '$');
5444 delete $r->{$x->{old_repo_id}}->
5445 {fetch}->{$x->{old_path}};
5446 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
Eric Wong8b8fc062007-01-22 11:44:57 -08005447 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08005448 "$pfx.url");
5449 push @emptied, $x->{old_repo_id}
5450 }
5451 }
5452 }
5453 if (@emptied) {
Johannes Schindelin8befc502008-12-14 23:10:52 +01005454 my $file = $ENV{GIT_CONFIG} || "$ENV{GIT_DIR}/config";
Eric Wong47e39c52007-01-21 04:27:09 -08005455 print STDERR <<EOF;
5456The following [svn-remote] sections in your config file ($file) are empty
5457and can be safely removed:
5458EOF
5459 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
5460 }
5461}
5462
Eric Wong706587f2007-01-18 17:50:01 -08005463sub migration_check {
5464 migrate_from_v0();
5465 migrate_from_v1();
5466 migrate_from_v2();
Eric Wong47e39c52007-01-21 04:27:09 -08005467 minimize_connections() if $_minimize;
Eric Wong706587f2007-01-18 17:50:01 -08005468}
5469
Eric Wongef3cfaa2007-01-24 03:30:57 -08005470package Git::IndexInfo;
5471use strict;
5472use warnings;
5473use Git qw/command_input_pipe command_close_pipe/;
5474
5475sub new {
5476 my ($class) = @_;
5477 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
5478 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
5479}
5480
5481sub remove {
5482 my ($self, $path) = @_;
5483 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
5484 return ++$self->{nr};
5485 }
5486 undef;
5487}
5488
5489sub update {
5490 my ($self, $mode, $hash, $path) = @_;
5491 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
5492 return ++$self->{nr};
5493 }
5494 undef;
5495}
5496
5497sub DESTROY {
5498 my ($self) = @_;
5499 command_close_pipe($self->{gui}, $self->{ctx});
5500}
5501
Eric Wong4bb9ed02007-02-03 13:29:17 -08005502package Git::SVN::GlobSpec;
5503use strict;
5504use warnings;
5505
5506sub new {
5507 my ($class, $glob) = @_;
Eric Wong4bb9ed02007-02-03 13:29:17 -08005508 my $re = $glob;
5509 $re =~ s!/+$!!g; # no need for trailing slashes
Marcus Griep570d35c2008-08-08 01:41:57 -07005510 $re =~ m!^([^*]*)(\*(?:/\*)*)([^*]*)$!;
5511 my $temp = $re;
5512 my ($left, $right) = ($1, $3);
5513 $re = $2;
5514 my $depth = $re =~ tr/*/*/;
5515 if ($depth != $temp =~ tr/*/*/) {
5516 die "Only one set of wildcard directories " .
5517 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
5518 }
5519 if ($depth == 0) {
Eric Wonge5181922007-02-08 12:53:57 -08005520 die "One '*' is needed for glob: '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08005521 }
Marcus Griep570d35c2008-08-08 01:41:57 -07005522 $re =~ s!\*!\[^/\]*!g;
5523 $re = quotemeta($left) . "($re)" . quotemeta($right);
Eric Wong4e9f6cc2007-02-09 12:17:57 -08005524 if (length $left && !($left =~ s!/+$!!g)) {
5525 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
5526 }
5527 if (length $right && !($right =~ s!^/+!!g)) {
5528 die "Missing leading '/' on right side of: '$glob' ($right)\n";
5529 }
Eric Wong74a81222007-02-10 13:28:50 -08005530 my $left_re = qr/^\/\Q$left\E(\/|$)/;
5531 bless { left => $left, right => $right, left_regex => $left_re,
Marcus Griep570d35c2008-08-08 01:41:57 -07005532 regex => qr/$re/, glob => $glob, depth => $depth }, $class;
Eric Wong4bb9ed02007-02-03 13:29:17 -08005533}
5534
5535sub full_path {
5536 my ($self, $path) = @_;
5537 return (length $self->{left} ? "$self->{left}/" : '') .
5538 $path . (length $self->{right} ? "/$self->{right}" : '');
5539}
5540
Eric Wong3397f9d2006-02-16 01:24:16 -08005541__END__
5542
5543Data structures:
5544
Eric Wong4bb9ed02007-02-03 13:29:17 -08005545
5546$remotes = { # returned by read_all_remotes()
5547 'svn' => {
5548 # svn-remote.svn.url=https://svn.musicpd.org
5549 url => 'https://svn.musicpd.org',
5550 # svn-remote.svn.fetch=mpd/trunk:trunk
5551 fetch => {
5552 'mpd/trunk' => 'trunk',
5553 },
5554 # svn-remote.svn.tags=mpd/tags/*:tags/*
5555 tags => {
5556 path => {
5557 left => 'mpd/tags',
5558 right => '',
5559 regex => qr!mpd/tags/([^/]+)$!,
5560 glob => 'tags/*',
5561 },
5562 ref => {
5563 left => 'tags',
5564 right => '',
5565 regex => qr!tags/([^/]+)$!,
5566 glob => 'tags/*',
5567 },
5568 }
5569 }
5570};
5571
Eric Wong44320b92007-01-13 22:35:53 -08005572$log_entry hashref as returned by libsvn_log_entry()
Eric Wong3397f9d2006-02-16 01:24:16 -08005573{
Eric Wong44320b92007-01-13 22:35:53 -08005574 log => 'whitespace-formatted log entry
Eric Wong3397f9d2006-02-16 01:24:16 -08005575', # trailing newline is preserved
5576 revision => '8', # integer
5577 date => '2004-02-24T17:01:44.108345Z', # commit date
5578 author => 'committer name'
5579};
5580
Eric Wong6e8548c2007-01-27 01:32:00 -08005581
5582# this is generated by generate_diff();
Eric Wong3397f9d2006-02-16 01:24:16 -08005583@mods = array of diff-index line hashes, each element represents one line
5584 of diff-index output
5585
5586diff-index line ($m hash)
5587{
5588 mode_a => first column of diff-index output, no leading ':',
5589 mode_b => second column of diff-index output,
5590 sha1_b => sha1sum of the final blob,
Eric Wongac8e0b92006-03-03 01:20:07 -08005591 chg => change type [MCRADT],
Eric Wong3397f9d2006-02-16 01:24:16 -08005592 file_a => original file name of a file (iff chg is 'C' or 'R')
5593 file_b => new/current file name of a file (any chg)
5594}
5595;
Eric Wonga5e0ced2006-06-12 15:23:48 -07005596
Eric Wonga00439a2006-06-27 19:39:13 -07005597# retval of read_url_paths{,_all}();
5598$l_map = {
5599 # repository root url
5600 'https://svn.musicpd.org' => {
5601 # repository path # GIT_SVN_ID
5602 'mpd/trunk' => 'trunk',
5603 'mpd/tags/0.11.5' => 'tags/0.11.5',
5604 },
5605}
5606
Eric Wonga5e0ced2006-06-12 15:23:48 -07005607Notes:
5608 I don't trust the each() function on unless I created %hash myself
5609 because the internal iterator may not have started at base.