blob: eb4b75aff347670b1817fb1c2584cff11a08fad5 [file] [log] [blame]
Eric Wong3397f9d2006-02-16 01:24:16 -08001#!/usr/bin/env perl
Eric Wong551ce282006-02-20 10:57:29 -08002# Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3# License: GPL v2 or later
Eric Wong3397f9d2006-02-16 01:24:16 -08004use warnings;
5use strict;
6use vars qw/ $AUTHOR $VERSION
Adam Robenffe256f2008-05-23 16:19:41 +02007 $sha1 $sha1_short $_revision $_repository
Mark Lodato36db1ed2009-05-14 21:27:15 -04008 $_q $_authors $_authors_prog %users/;
Eric Wong3397f9d2006-02-16 01:24:16 -08009$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
Eric Wong60d02cc2006-07-06 00:14:16 -070010$VERSION = '@@GIT_VERSION@@';
Eric Wong13ccd6d2006-03-29 22:37:18 -080011
Benoit Sigoure15153452007-10-16 16:36:50 +020012# From which subdir have we been invoked?
13my $cmd_dir_prefix = eval {
14 command_oneline([qw/rev-parse --show-prefix/], STDERR => 0)
15} || '';
16
Eric Wong5253dc32007-02-20 01:36:30 -080017my $git_dir_user_set = 1 if defined $ENV{GIT_DIR};
Eric Wong706587f2007-01-18 17:50:01 -080018$ENV{GIT_DIR} ||= '.git';
Eric Wong9fa00b62007-02-03 12:49:48 -080019$Git::SVN::default_repo_id = 'svn';
Eric Wong8b8fc062007-01-22 11:44:57 -080020$Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
Eric Wong6af1db42007-02-14 16:04:10 -080021$Git::SVN::Ra::_log_window_size = 100;
Eric Wong6b488292009-07-25 00:00:50 -070022$Git::SVN::_minimize_url = 'unset';
Eric Wong13ccd6d2006-03-29 22:37:18 -080023
Karthik Rf3a87d92009-08-18 18:54:40 -050024if (! exists $ENV{SVN_SSH}) {
25 if (exists $ENV{GIT_SSH}) {
26 $ENV{SVN_SSH} = $ENV{GIT_SSH};
27 if ($^O eq 'msys') {
28 $ENV{SVN_SSH} =~ s/\\/\\\\/g;
29 }
30 }
31}
32
Eric Wongf8c9d1d2007-01-12 02:35:20 -080033$Git::SVN::Log::TZ = $ENV{TZ};
Eric Wong3397f9d2006-02-16 01:24:16 -080034$ENV{TZ} = 'UTC';
Eric Wonga00439a2006-06-27 19:39:13 -070035$| = 1; # unbuffer STDOUT
Eric Wong3397f9d2006-02-16 01:24:16 -080036
Benoit Sigoure207f1a72007-10-16 16:36:52 +020037sub fatal (@) { print STDERR "@_\n"; exit 1 }
Eric Wongb9c85182006-12-15 23:58:07 -080038require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
39require SVN::Ra;
40require SVN::Delta;
41if ($SVN::Core::VERSION lt '1.1.0') {
Benoit Sigoure207f1a72007-10-16 16:36:52 +020042 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
Eric Wongb9c85182006-12-15 23:58:07 -080043}
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -050044my $can_compress = eval { require Compress::Zlib; 1};
Eric Wongd81bf822007-01-10 01:22:38 -080045push @Git::SVN::Ra::ISA, 'SVN::Ra';
Eric Wongb9c85182006-12-15 23:58:07 -080046push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
47push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
Eric Wong3397f9d2006-02-16 01:24:16 -080048use Carp qw/croak/;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -080049use Digest::MD5;
Eric Wong3397f9d2006-02-16 01:24:16 -080050use IO::File qw//;
51use File::Basename qw/dirname basename/;
52use File::Path qw/mkpath/;
Mark Lodato36db1ed2009-05-14 21:27:15 -040053use File::Spec;
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -050054use File::Find;
Eric Wong512b6202007-04-03 01:57:08 -070055use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
Eric Wong968bdf12006-06-15 13:36:12 -070056use IPC::Open3;
Eric Wong336f1712007-01-11 02:14:43 -080057use Git;
Eric Wonga5e0ced2006-06-12 15:23:48 -070058
Eric Wong336f1712007-01-11 02:14:43 -080059BEGIN {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120060 # import functions from Git into our packages, en masse
61 no strict 'refs';
Eric Wong336f1712007-01-11 02:14:43 -080062 foreach (qw/command command_oneline command_noisy command_output_pipe
Boris Byk6ea42032009-04-11 00:32:41 +040063 command_input_pipe command_close_pipe
64 command_bidi_pipe command_close_bidi_pipe/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120065 for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -080066 Git::SVN::Migration Git::SVN::Log Git::SVN),
Sam Vilainc5f71ad2007-06-15 15:43:59 +120067 __PACKAGE__) {
68 *{"${package}::$_"} = \&{"Git::$_"};
69 }
Eric Wong336f1712007-01-11 02:14:43 -080070 }
Eric Wong336f1712007-01-11 02:14:43 -080071}
72
Eric Wongb9c85182006-12-15 23:58:07 -080073my ($SVN);
Eric Wong83e99402006-10-11 18:19:55 -070074
Eric Wongf8c9d1d2007-01-12 02:35:20 -080075$sha1 = qr/[a-f\d]{40}/;
76$sha1_short = qr/[a-f\d]{4,40}/;
Eric Wong44320b92007-01-13 22:35:53 -080077my ($_stdin, $_help, $_edit,
Marc Branchaud62244062009-06-23 13:02:08 -040078 $_message, $_file, $_branch_dest,
Eric Wongd05d72e2007-01-15 22:59:26 -080079 $_template, $_shared,
Jason Merrillc2abd832009-04-06 16:37:59 -040080 $_version, $_fetch_all, $_no_rebase, $_fetch_parent,
Eric Wongdee41f32007-03-13 11:40:36 -070081 $_merge, $_strategy, $_dry_run, $_local,
Steven Grimm4be40382008-05-10 22:11:18 -070082 $_prefix, $_no_checkout, $_url, $_verbose,
Florian Ragwitz5de70ef2008-10-04 19:35:17 -070083 $_git_format, $_commit_url, $_tag);
Eric Wong0bed5ea2007-02-09 02:45:03 -080084$Git::SVN::_follow_parent = 1;
Simon Arlott49750f32009-03-30 19:31:41 +010085$_q ||= 0;
Eric Wong706587f2007-01-18 17:50:01 -080086my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
87 'config-dir=s' => \$Git::SVN::Ra::config_dir,
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +020088 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
89 'ignore-paths=s' => \$SVN::Git::Fetcher::_ignore_regex );
Eric Wong0bed5ea2007-02-09 02:45:03 -080090my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
Eric Wongdc5869c2006-05-24 02:07:32 -070091 'authors-file|A=s' => \$_authors,
Mark Lodato36db1ed2009-05-14 21:27:15 -040092 'authors-prog=s' => \$_authors_prog,
Eric Wongecc712d2007-01-31 12:28:10 -080093 'repack:i' => \$Git::SVN::_repack,
Eric Wong97ae0912007-02-11 15:21:24 -080094 'noMetadata' => \$Git::SVN::_no_metadata,
95 'useSvmProps' => \$Git::SVN::_use_svm_props,
Eric Wong62e349d2007-02-16 19:57:29 -080096 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props,
Eric Wong6af1db42007-02-14 16:04:10 -080097 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size,
Eric Wong1e889ef2007-02-16 01:45:13 -080098 'no-checkout' => \$_no_checkout,
Simon Arlott49750f32009-03-30 19:31:41 +010099 'quiet|q+' => \$_q,
Eric Wongecc712d2007-01-31 12:28:10 -0800100 'repack-flags|repack-args|repack-opts=s' =>
101 \$Git::SVN::_repack_flags,
Andy Whitcroft70ae04e2007-11-22 13:44:42 +0000102 'use-log-author' => \$Git::SVN::_use_log_author,
Avery Pennarun6aa9ba12008-04-15 21:04:17 -0400103 'add-author-from' => \$Git::SVN::_add_author_from,
Pete Harlane82f0d72009-01-17 20:10:14 -0800104 'localtime' => \$Git::SVN::_localtime,
Eric Wong706587f2007-01-18 17:50:01 -0800105 %remote_opts );
Eric Wong36f5b1f2006-05-23 19:23:41 -0700106
Marc Branchaud62244062009-06-23 13:02:08 -0400107my ($_trunk, @_tags, @_branches, $_stdlayout);
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800108my %icv;
Eric Wongdadc6d22007-02-14 12:27:41 -0800109my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared,
Marc Branchaud62244062009-06-23 13:02:08 -0400110 'trunk|T=s' => \$_trunk, 'tags|t=s@' => \@_tags,
111 'branches|b=s@' => \@_branches, 'prefix=s' => \$_prefix,
martin f. krafft8f728fb2007-07-14 11:25:28 +0200112 'stdlayout|s' => \$_stdlayout,
Eric Wong6b488292009-07-25 00:00:50 -0700113 'minimize-url|m!' => \$Git::SVN::_minimize_url,
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800114 'no-metadata' => sub { $icv{noMetadata} = 1 },
115 'use-svm-props' => sub { $icv{useSvmProps} = 1 },
116 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 },
117 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] },
Eric Wongdadc6d22007-02-14 12:27:41 -0800118 %remote_opts );
Eric Wong27e9fb82006-06-27 19:39:12 -0700119my %cmt_opts = ( 'edit|e' => \$_edit,
Eric Wong24e22aa2007-01-29 00:07:49 -0800120 'rmdir' => \$SVN::Git::Editor::_rmdir,
121 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder,
122 'l=i' => \$SVN::Git::Editor::_rename_limit,
123 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity
Eric Wong27e9fb82006-06-27 19:39:12 -0700124);
Eric Wong9d55b412006-06-12 15:53:13 -0700125
Eric Wong3397f9d2006-02-16 01:24:16 -0800126my %cmd = (
Eric Wong2a3240b2007-01-04 18:09:56 -0800127 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
Eric Wonge98671e2007-02-14 02:21:19 -0800128 { 'revision|r=s' => \$_revision,
Eric Wong905f8b72007-02-16 03:22:40 -0800129 'fetch-all|all' => \$_fetch_all,
Jason Merrillc2abd832009-04-06 16:37:59 -0400130 'parent|p' => \$_fetch_parent,
Eric Wonge98671e2007-02-14 02:21:19 -0800131 %fc_opts } ],
Eric Wong0425ea92007-02-16 18:45:01 -0800132 clone => [ \&cmd_clone, "Initialize and fetch revisions",
133 { 'revision|r=s' => \$_revision,
134 %fc_opts, %init_opts } ],
Eric Wongd2866f92007-01-11 12:26:16 -0800135 init => [ \&cmd_init, "Initialize a repo for tracking" .
Eric Wongf8ab6b72006-05-31 15:49:56 -0700136 " (requires URL argument)",
Eric Wong9d55b412006-06-12 15:53:13 -0700137 \%init_opts ],
Eric Wongdadc6d22007-02-14 12:27:41 -0800138 'multi-init' => [ \&cmd_multi_init,
139 "Deprecated alias for ".
140 "'$0 init -T<trunk> -b<branches> -t<tags>'",
141 \%init_opts ],
Eric Wongd7ad3be2007-01-14 03:14:28 -0800142 dcommit => [ \&cmd_dcommit,
143 'Commit several diffs to merge with upstream',
Eric Wong3289e862006-12-15 23:58:08 -0800144 { 'merge|m|M' => \$_merge,
145 'strategy|s=s' => \$_strategy,
Eric Wong905f8b72007-02-16 03:22:40 -0800146 'verbose|v' => \$_verbose,
Eric Wong3289e862006-12-15 23:58:08 -0800147 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800148 'fetch-all|all' => \$_fetch_all,
Eric Wongba24e742008-08-07 02:06:16 -0700149 'commit-url=s' => \$_commit_url,
150 'revision|r=i' => \$_revision,
Karl Hasselström171af112007-05-03 07:51:35 +0200151 'no-rebase' => \$_no_rebase,
Eric Wong4b155222006-12-22 21:59:24 -0800152 %cmt_opts, %fc_opts } ],
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700153 branch => [ \&cmd_branch,
154 'Create a branch in the SVN repository',
155 { 'message|m=s' => \$_message,
Marc Branchaud62244062009-06-23 13:02:08 -0400156 'destination|d=s' => \$_branch_dest,
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700157 'dry-run|n' => \$_dry_run,
158 'tag|t' => \$_tag } ],
159 tag => [ sub { $_tag = 1; cmd_branch(@_) },
160 'Create a tag in the SVN repository',
161 { 'message|m=s' => \$_message,
Marc Branchaud62244062009-06-23 13:02:08 -0400162 'destination|d=s' => \$_branch_dest,
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700163 'dry-run|n' => \$_dry_run } ],
Eric Wong1ce255d2007-01-14 23:21:16 -0800164 'set-tree' => [ \&cmd_set_tree,
165 "Set an SVN repository to a git tree-ish",
Robin H. Johnsone84dc6d2009-05-05 11:16:14 -0700166 { 'stdin' => \$_stdin, %cmt_opts, %fc_opts, } ],
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200167 'create-ignore' => [ \&cmd_create_ignore,
168 'Create a .gitignore per svn:ignore',
169 { 'revision|r=i' => \$_revision
170 } ],
Benoit Sigoure15153452007-10-16 16:36:50 +0200171 'propget' => [ \&cmd_propget,
172 'Print the value of a property on a file or directory',
173 { 'revision|r=i' => \$_revision } ],
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200174 'proplist' => [ \&cmd_proplist,
175 'List all properties of a file or directory',
176 { 'revision|r=i' => \$_revision } ],
Eric Wong5969cbe2007-01-11 17:58:39 -0800177 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200178 { 'revision|r=i' => \$_revision
Lars Hjemli05b4df32007-09-05 11:35:29 +0200179 } ],
Vineet Kumar2d879792007-11-19 14:56:15 -0800180 'show-externals' => [ \&cmd_show_externals, "Show svn:externals listings",
181 { 'revision|r=i' => \$_revision
182 } ],
Eric Wong1c8443b2007-01-14 02:17:00 -0800183 'multi-fetch' => [ \&cmd_multi_fetch,
Eric Wonge98671e2007-02-14 02:21:19 -0800184 "Deprecated alias for $0 fetch --all",
185 { 'revision|r=s' => \$_revision, %fc_opts } ],
Eric Wong706587f2007-01-18 17:50:01 -0800186 'migrate' => [ sub { },
187 # no-op, we automatically run this anyways,
Eric Wong706587f2007-01-18 17:50:01 -0800188 'Migrate configuration/metadata/layout from
189 previous versions of git-svn',
Eric Wonga836a0e2007-02-14 19:34:56 -0800190 { 'minimize' => \$Git::SVN::Migration::_minimize,
191 %remote_opts } ],
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800192 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
193 { 'limit=i' => \$Git::SVN::Log::limit,
Eric Wong79bb8d82006-06-01 02:35:44 -0700194 'revision|r=s' => \$_revision,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800195 'verbose|v' => \$Git::SVN::Log::verbose,
196 'incremental' => \$Git::SVN::Log::incremental,
197 'oneline' => \$Git::SVN::Log::oneline,
198 'show-commit' => \$Git::SVN::Log::show_commit,
199 'non-recursive' => \$Git::SVN::Log::non_recursive,
Eric Wong79bb8d82006-06-01 02:35:44 -0700200 'authors-file|A=s' => \$_authors,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800201 'color' => \$Git::SVN::Log::color,
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200202 'pager=s' => \$Git::SVN::Log::pager
Eric Wong79bb8d82006-06-01 02:35:44 -0700203 } ],
Eric Wong222566e2008-08-08 01:41:58 -0700204 'find-rev' => [ \&cmd_find_rev,
205 "Translate between SVN revision numbers and tree-ish",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200206 {} ],
Eric Wong905f8b72007-02-16 03:22:40 -0800207 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
208 { 'merge|m|M' => \$_merge,
209 'verbose|v' => \$_verbose,
210 'strategy|s=s' => \$_strategy,
Eric Wongdee41f32007-03-13 11:40:36 -0700211 'local|l' => \$_local,
Eric Wong905f8b72007-02-16 03:22:40 -0800212 'fetch-all|all' => \$_fetch_all,
Seth Falcon7d45e142008-05-19 20:29:17 -0700213 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800214 %fc_opts } ],
Eric Wong44320b92007-01-13 22:35:53 -0800215 'commit-diff' => [ \&cmd_commit_diff,
216 'Commit a diff between two trees',
Eric Wong27e9fb82006-06-27 19:39:12 -0700217 { 'message|m=s' => \$_message,
218 'file|F=s' => \$_file,
Eric Wong45bf4732006-11-09 01:19:37 -0800219 'revision|r=s' => \$_revision,
Eric Wong27e9fb82006-06-27 19:39:12 -0700220 %cmt_opts } ],
David D. Kilzere6fefa92007-11-21 11:57:18 -0800221 'info' => [ \&cmd_info,
222 "Show info about the latest SVN revision
223 on the current branch",
David D. Kilzer8b014d72007-11-21 11:57:19 -0800224 { 'url' => \$_url, } ],
Tim Stoakes6fb53752008-02-10 15:21:08 +1030225 'blame' => [ \&Git::SVN::Log::cmd_blame,
226 "Show what revision and author last modified each line of a file",
Steven Grimm4be40382008-05-10 22:11:18 -0700227 { 'git-format' => \$_git_format } ],
Ben Jackson195643f2009-06-03 20:45:52 -0700228 'reset' => [ \&cmd_reset,
229 "Undo fetches back to the specified SVN revision",
230 { 'revision|r=s' => \$_revision,
231 'parent|p' => \$_fetch_parent } ],
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -0500232 'gc' => [ \&cmd_gc,
233 "Compress unhandled.log files in .git/svn and remove " .
234 "index files in .git/svn",
235 {} ],
Eric Wong3397f9d2006-02-16 01:24:16 -0800236);
Eric Wong9d55b412006-06-12 15:53:13 -0700237
Eric Wong3397f9d2006-02-16 01:24:16 -0800238my $cmd;
239for (my $i = 0; $i < @ARGV; $i++) {
240 if (defined $cmd{$ARGV[$i]}) {
241 $cmd = $ARGV[$i];
242 splice @ARGV, $i, 1;
243 last;
Ben Jackson9a8c92a2009-05-30 18:17:06 -0700244 } elsif ($ARGV[$i] eq 'help') {
245 $cmd = $ARGV[$i+1];
246 usage(0);
Eric Wong3397f9d2006-02-16 01:24:16 -0800247 }
248};
249
Eric Wong540424b2007-12-19 00:31:43 -0800250# make sure we're always running at the top-level working directory
251unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
Eric Wong5253dc32007-02-20 01:36:30 -0800252 unless (-d $ENV{GIT_DIR}) {
253 if ($git_dir_user_set) {
254 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
255 "but it is not a directory\n";
256 }
257 my $git_dir = delete $ENV{GIT_DIR};
Deskin Millerfe4003f2008-11-06 00:07:39 -0500258 my $cdup = undef;
259 git_cmd_try {
260 $cdup = command_oneline(qw/rev-parse --show-cdup/);
261 $git_dir = '.' unless ($cdup);
262 chomp $cdup if ($cdup);
263 $cdup = "." unless ($cdup && length $cdup);
264 } "Already at toplevel, but $git_dir not found\n";
Eric Wong5253dc32007-02-20 01:36:30 -0800265 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
266 unless (-d $git_dir) {
267 die "$git_dir still not found after going to ",
268 "'$cdup'\n";
269 }
270 $ENV{GIT_DIR} = $git_dir;
271 }
Adam Robenffe256f2008-05-23 16:19:41 +0200272 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wong5253dc32007-02-20 01:36:30 -0800273}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100274
275my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
276
277read_repo_config(\%opts);
Eric Wong222566e2008-08-08 01:41:58 -0700278if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
279 Getopt::Long::Configure('pass_through');
280}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100281my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
282 'minimize-connections' => \$Git::SVN::Migration::_minimize,
283 'id|i=s' => \$Git::SVN::default_ref_id,
284 'svn-remote|remote|R=s' => sub {
285 $Git::SVN::no_reuse_existing = 1;
286 $Git::SVN::default_repo_id = $_[1] });
287exit 1 if (!$rv && $cmd && $cmd ne 'log');
288
289usage(0) if $_help;
290version() if $_version;
291usage(1) unless defined $cmd;
292load_authors() if $_authors;
Mark Lodato36db1ed2009-05-14 21:27:15 -0400293if (defined $_authors_prog) {
294 $_authors_prog = "'" . File::Spec->rel2abs($_authors_prog) . "'";
295}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100296
Eric Wong0425ea92007-02-16 18:45:01 -0800297unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
Eric Wong706587f2007-01-18 17:50:01 -0800298 Git::SVN::Migration::migration_check();
299}
Eric Wongecc712d2007-01-31 12:28:10 -0800300Git::SVN::init_vars();
Eric Wongb805b442007-01-22 13:52:04 -0800301eval {
302 Git::SVN::verify_remotes_sanity();
303 $cmd{$cmd}->[0]->(@ARGV);
304};
305fatal $@ if $@;
Eric Wong1e889ef2007-02-16 01:45:13 -0800306post_fetch_checkout();
Eric Wong3397f9d2006-02-16 01:24:16 -0800307exit 0;
308
309####################### primary functions ######################
310sub usage {
311 my $exit = shift || 0;
312 my $fd = $exit ? \*STDERR : \*STDOUT;
313 print $fd <<"";
314git-svn - bidirectional operations between a single Subversion tree and git
Stephan Beyer1b1dd232008-07-13 15:36:15 +0200315Usage: git svn <command> [options] [arguments]\n
Eric Wong448c81b2006-03-03 01:20:09 -0800316
317 print $fd "Available commands:\n" unless $cmd;
Eric Wong3397f9d2006-02-16 01:24:16 -0800318
319 foreach (sort keys %cmd) {
Eric Wong448c81b2006-03-03 01:20:09 -0800320 next if $cmd && $cmd ne $_;
Eric Wonga836a0e2007-02-14 19:34:56 -0800321 next if /^multi-/; # don't show deprecated commands
Eric Wongb203b762006-10-11 14:53:36 -0700322 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
Benoit Sigoureaa807bc2007-11-03 19:53:34 +0100323 foreach (sort keys %{$cmd{$_}->[2]}) {
Eric Wong512b6202007-04-03 01:57:08 -0700324 # mixed-case options are for .git/config only
325 next if /[A-Z]/ && /^[a-z]+$/i;
Eric Wong448c81b2006-03-03 01:20:09 -0800326 # prints out arguments as they should be passed:
Eric Wongb8c92ca2006-05-24 01:40:37 -0700327 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
Eric Wongb203b762006-10-11 14:53:36 -0700328 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
Eric Wong448c81b2006-03-03 01:20:09 -0800329 "--$_" : "-$_" }
330 split /\|/,$_)," $x\n";
331 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800332 }
333 print $fd <<"";
Eric Wong448c81b2006-03-03 01:20:09 -0800334\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
335arbitrary identifier if you're tracking multiple SVN branches/repositories in
336one git repository and want to keep them separate. See git-svn(1) for more
337information.
Eric Wong3397f9d2006-02-16 01:24:16 -0800338
339 exit $exit;
340}
341
Eric Wong551ce282006-02-20 10:57:29 -0800342sub version {
Eric Wong7d60ab22006-12-28 01:16:20 -0800343 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
Eric Wong551ce282006-02-20 10:57:29 -0800344 exit 0;
345}
346
Eric Wong8164b652007-01-11 15:35:55 -0800347sub do_git_init_db {
348 unless (-d $ENV{GIT_DIR}) {
349 my @init_db = ('init');
350 push @init_db, "--template=$_template" if defined $_template;
Eric Wongdadc6d22007-02-14 12:27:41 -0800351 if (defined $_shared) {
352 if ($_shared =~ /[a-z]/) {
353 push @init_db, "--shared=$_shared";
354 } else {
355 push @init_db, "--shared";
356 }
357 }
Eric Wong8164b652007-01-11 15:35:55 -0800358 command_noisy(@init_db);
Adam Robenffe256f2008-05-23 16:19:41 +0200359 $_repository = Git->repository(Repository => ".git");
Eric Wong8164b652007-01-11 15:35:55 -0800360 }
Johannes Schindelind3c96342009-04-09 13:29:57 +0200361 command_noisy('config', 'core.autocrlf', 'false');
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800362 my $set;
363 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
364 foreach my $i (keys %icv) {
365 die "'$set' and '$i' cannot both be set\n" if $set;
366 next unless defined $icv{$i};
367 command_noisy('config', "$pfx.$i", $icv{$i});
368 $set = $i;
369 }
Ben Jackson88ec2052009-04-11 10:46:18 -0700370 my $ignore_regex = \$SVN::Git::Fetcher::_ignore_regex;
371 command_noisy('config', "$pfx.ignore-paths", $$ignore_regex)
372 if defined $$ignore_regex;
Eric Wong8164b652007-01-11 15:35:55 -0800373}
374
Eric Wongdadc6d22007-02-14 12:27:41 -0800375sub init_subdir {
376 my $repo_path = shift or return;
377 mkpath([$repo_path]) unless -d $repo_path;
378 chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
Eric Wongf30603f2007-02-23 01:26:26 -0800379 $ENV{GIT_DIR} = '.git';
Adam Robenffe256f2008-05-23 16:19:41 +0200380 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wongdadc6d22007-02-14 12:27:41 -0800381}
382
Eric Wong0425ea92007-02-16 18:45:01 -0800383sub cmd_clone {
384 my ($url, $path) = @_;
385 if (!defined $path &&
Marc Branchaud62244062009-06-23 13:02:08 -0400386 (defined $_trunk || @_branches || @_tags ||
martin f. krafft8f728fb2007-07-14 11:25:28 +0200387 defined $_stdlayout) &&
Eric Wong0425ea92007-02-16 18:45:01 -0800388 $url !~ m#^[a-z\+]+://#) {
389 $path = $url;
390 }
Eric Wong0425ea92007-02-16 18:45:01 -0800391 $path = basename($url) if !defined $path || !length $path;
Eric Wongf30603f2007-02-23 01:26:26 -0800392 cmd_init($url, $path);
Eric Wong0425ea92007-02-16 18:45:01 -0800393 Git::SVN::fetch_all($Git::SVN::default_repo_id);
Alex Vandiver42a5da12009-05-06 16:19:45 -0400394 command_oneline('config', 'svn.authorsfile', $_authors) if $_authors;
Eric Wong0425ea92007-02-16 18:45:01 -0800395}
396
Eric Wongd2866f92007-01-11 12:26:16 -0800397sub cmd_init {
martin f. krafft8f728fb2007-07-14 11:25:28 +0200398 if (defined $_stdlayout) {
399 $_trunk = 'trunk' if (!defined $_trunk);
Marc Branchaud62244062009-06-23 13:02:08 -0400400 @_tags = 'tags' if (! @_tags);
401 @_branches = 'branches' if (! @_branches);
martin f. krafft8f728fb2007-07-14 11:25:28 +0200402 }
Marc Branchaud62244062009-06-23 13:02:08 -0400403 if (defined $_trunk || @_branches || @_tags) {
Eric Wongdadc6d22007-02-14 12:27:41 -0800404 return cmd_multi_init(@_);
Eric Wong03e0ea82006-06-30 21:42:53 -0700405 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800406 my $url = shift or die "SVN repository location required ",
407 "as a command-line argument\n";
Ulrich Dangel50ff2362009-06-26 16:52:09 +0200408 $url = canonicalize_url($url);
Eric Wongdadc6d22007-02-14 12:27:41 -0800409 init_subdir(@_);
Eric Wong8164b652007-01-11 15:35:55 -0800410 do_git_init_db();
Eric Wong03e0ea82006-06-30 21:42:53 -0700411
Eric Wong6b488292009-07-25 00:00:50 -0700412 if ($Git::SVN::_minimize_url eq 'unset') {
413 $Git::SVN::_minimize_url = 0;
414 }
415
Eric Wong706587f2007-01-18 17:50:01 -0800416 Git::SVN->init($url);
Eric Wong3397f9d2006-02-16 01:24:16 -0800417}
418
Eric Wong2a3240b2007-01-04 18:09:56 -0800419sub cmd_fetch {
Eric Wonge98671e2007-02-14 02:21:19 -0800420 if (grep /^\d+=./, @_) {
421 die "'<rev>=<commit>' fetch arguments are ",
422 "no longer supported.\n";
Eric Wong07a1c952007-01-22 15:47:41 -0800423 }
Eric Wonge98671e2007-02-14 02:21:19 -0800424 my ($remote) = @_;
425 if (@_ > 1) {
Jason Merrillc2abd832009-04-06 16:37:59 -0400426 die "Usage: $0 fetch [--all] [--parent] [svn-remote]\n";
Eric Wonge98671e2007-02-14 02:21:19 -0800427 }
Jason Merrillc2abd832009-04-06 16:37:59 -0400428 if ($_fetch_parent) {
429 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
430 unless ($gs) {
431 die "Unable to determine upstream SVN information from ",
432 "working tree history\n";
433 }
434 # just fetch, don't checkout.
435 $_no_checkout = 'true';
436 $_fetch_all ? $gs->fetch_all : $gs->fetch;
437 } elsif ($_fetch_all) {
Eric Wonge98671e2007-02-14 02:21:19 -0800438 cmd_multi_fetch();
439 } else {
Jason Merrillc2abd832009-04-06 16:37:59 -0400440 $remote ||= $Git::SVN::default_repo_id;
Eric Wonge98671e2007-02-14 02:21:19 -0800441 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
Eric Wong1c8443b2007-01-14 02:17:00 -0800442 }
Eric Wong2a3240b2007-01-04 18:09:56 -0800443}
444
Eric Wong1ce255d2007-01-14 23:21:16 -0800445sub cmd_set_tree {
Eric Wong3397f9d2006-02-16 01:24:16 -0800446 my (@commits) = @_;
447 if ($_stdin || !@commits) {
448 print "Reading from stdin...\n";
449 @commits = ();
450 while (<STDIN>) {
Eric Wong1ca72ae2006-03-03 01:20:09 -0800451 if (/\b($sha1_short)\b/o) {
Eric Wong3397f9d2006-02-16 01:24:16 -0800452 unshift @commits, $1;
453 }
454 }
455 }
456 my @revs;
Eric Wong8de010a2006-02-20 10:57:26 -0800457 foreach my $c (@commits) {
Eric Wongaef4e922006-12-15 10:59:54 -0800458 my @tmp = command('rev-parse',$c);
Eric Wong8de010a2006-02-20 10:57:26 -0800459 if (scalar @tmp == 1) {
460 push @revs, $tmp[0];
461 } elsif (scalar @tmp > 1) {
Eric Wongaef4e922006-12-15 10:59:54 -0800462 push @revs, reverse(command('rev-list',@tmp));
Eric Wong8de010a2006-02-20 10:57:26 -0800463 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200464 fatal "Failed to rev-parse $c";
Eric Wong8de010a2006-02-20 10:57:26 -0800465 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800466 }
Eric Wong1ce255d2007-01-14 23:21:16 -0800467 my $gs = Git::SVN->new;
468 my ($r_last, $cmt_last) = $gs->last_rev_commit;
469 $gs->fetch;
Eric Wong97f69872007-01-25 11:53:13 -0800470 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
Eric Wong1ce255d2007-01-14 23:21:16 -0800471 fatal "There are new revisions that were fetched ",
472 "and need to be merged (or acknowledged) ",
473 "before committing.\nlast rev: $r_last\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200474 " current: $gs->{last_rev}";
Eric Wong1ce255d2007-01-14 23:21:16 -0800475 }
476 $gs->set_tree($_) foreach @revs;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700477 print "Done committing ",scalar @revs," revisions to SVN\n";
Eric Wong3157dd92007-12-13 08:27:34 -0800478 unlink $gs->{index};
Eric Wonga5e0ced2006-06-12 15:23:48 -0700479}
480
Eric Wongd7ad3be2007-01-14 03:14:28 -0800481sub cmd_dcommit {
482 my $head = shift;
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100483 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
David Reiss826a9332007-11-13 13:47:26 -0800484 'Cannot dcommit with a dirty index. Commit your changes first, '
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100485 . "or stash them with `git stash'.\n";
Eric Wongd7ad3be2007-01-14 03:14:28 -0800486 $head ||= 'HEAD';
Thomas Rast5eec27e2009-05-29 17:09:42 +0200487
488 my $old_head;
489 if ($head ne 'HEAD') {
490 $old_head = eval {
491 command_oneline([qw/symbolic-ref -q HEAD/])
492 };
493 if ($old_head) {
494 $old_head =~ s{^refs/heads/}{};
495 } else {
496 $old_head = eval { command_oneline(qw/rev-parse HEAD/) };
497 }
498 command(['checkout', $head], STDERR => 0);
499 }
500
Eric Wonga8ae2622007-02-13 14:22:11 -0800501 my @refs;
Thomas Rast5eec27e2009-05-29 17:09:42 +0200502 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD', \@refs);
Thomas Rast2cb61102008-08-31 15:50:59 +0200503 unless ($gs) {
504 die "Unable to determine upstream SVN information from ",
505 "$head history.\nPerhaps the repository is empty.";
506 }
Peter Oberndorfer0df84052009-02-23 12:02:53 +0100507
508 if (defined $_commit_url) {
509 $url = $_commit_url;
510 } else {
511 $url = eval { command_oneline('config', '--get',
512 "svn-remote.$gs->{repo_id}.commiturl") };
513 if (!$url) {
514 $url = $gs->full_url
515 }
516 }
517
Eric Wongba24e742008-08-07 02:06:16 -0700518 my $last_rev = $_revision if defined $_revision;
Matthieu Moy59b0c242008-04-24 20:06:36 +0200519 if ($url) {
520 print "Committing to $url ...\n";
521 }
Eric Wong733a65a2007-06-13 02:23:28 -0700522 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
Eric Wong751eb392007-08-31 18:16:12 -0700523 if ($_no_rebase && scalar(@$linear_refs) > 1) {
524 warn "Attempting to commit more than one change while ",
525 "--no-rebase is enabled.\n",
526 "If these changes depend on each other, re-running ",
Eric Wong7dfa16b2008-01-02 10:09:49 -0800527 "without --no-rebase may be required."
Eric Wong751eb392007-08-31 18:16:12 -0700528 }
Eric Wong711521e2008-08-20 00:30:06 -0700529 my $expect_url = $url;
530 Git::SVN::remove_username($expect_url);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800531 while (1) {
532 my $d = shift @$linear_refs or last;
Eric Wong45bf4732006-11-09 01:19:37 -0800533 unless (defined $last_rev) {
534 (undef, $last_rev, undef) = cmt_metadata("$d~1");
535 unless (defined $last_rev) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800536 fatal "Unable to extract revision information ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200537 "from commit $d~1";
Eric Wong45bf4732006-11-09 01:19:37 -0800538 }
539 }
Eric Wongb22d4492006-08-26 00:01:23 -0700540 if ($_dry_run) {
541 print "diff-tree $d~1 $d\n";
542 } else {
Eric Wong751eb392007-08-31 18:16:12 -0700543 my $cmt_rev;
Eric Wongd7ad3be2007-01-14 03:14:28 -0800544 my %ed_opts = ( r => $last_rev,
Eric Wong61395352007-01-27 14:33:08 -0800545 log => get_commit_entry($d)->{log},
Eric Wongba24e742008-08-07 02:06:16 -0700546 ra => Git::SVN::Ra->new($url),
Konstantin V. Arkhipov3caf3202007-11-14 03:52:02 +0300547 config => SVN::Core::config_get_config(
548 $Git::SVN::Ra::config_dir
549 ),
Eric Wong61395352007-01-27 14:33:08 -0800550 tree_a => "$d~1",
551 tree_b => $d,
552 editor_cb => sub {
553 print "Committed r$_[0]\n";
Eric Wong751eb392007-08-31 18:16:12 -0700554 $cmt_rev = $_[0];
555 },
Eric Wonga8ae2622007-02-13 14:22:11 -0800556 svn_path => '');
Eric Wong61395352007-01-27 14:33:08 -0800557 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800558 print "No changes\n$d~1 == $d\n";
Eric Wong733a65a2007-06-13 02:23:28 -0700559 } elsif ($parents->{$d} && @{$parents->{$d}}) {
Eric Wong751eb392007-08-31 18:16:12 -0700560 $gs->{inject_parents_dcommit}->{$cmt_rev} =
Eric Wong733a65a2007-06-13 02:23:28 -0700561 $parents->{$d};
Eric Wongd7ad3be2007-01-14 03:14:28 -0800562 }
Eric Wong751eb392007-08-31 18:16:12 -0700563 $_fetch_all ? $gs->fetch_all : $gs->fetch;
Eric Wong7dfa16b2008-01-02 10:09:49 -0800564 $last_rev = $cmt_rev;
Eric Wong751eb392007-08-31 18:16:12 -0700565 next if $_no_rebase;
566
567 # we always want to rebase against the current HEAD,
568 # not any head that was passed to us
Eric Wongc74d9ac2007-11-05 03:21:47 -0800569 my @diff = command('diff-tree', $d,
Eric Wong751eb392007-08-31 18:16:12 -0700570 $gs->refname, '--');
571 my @finish;
572 if (@diff) {
573 @finish = rebase_cmd();
Eric Wongc74d9ac2007-11-05 03:21:47 -0800574 print STDERR "W: $d and ", $gs->refname,
Eric Wong751eb392007-08-31 18:16:12 -0700575 " differ, using @finish:\n",
Eric Wongc74d9ac2007-11-05 03:21:47 -0800576 join("\n", @diff), "\n";
Eric Wong751eb392007-08-31 18:16:12 -0700577 } else {
578 print "No changes between current HEAD and ",
579 $gs->refname,
580 "\nResetting to the latest ",
581 $gs->refname, "\n";
582 @finish = qw/reset --mixed/;
583 }
584 command_noisy(@finish, $gs->refname);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800585 if (@diff) {
586 @refs = ();
587 my ($url_, $rev_, $uuid_, $gs_) =
Thomas Rast5eec27e2009-05-29 17:09:42 +0200588 working_head_info('HEAD', \@refs);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800589 my ($linear_refs_, $parents_) =
590 linearize_history($gs_, \@refs);
591 if (scalar(@$linear_refs) !=
592 scalar(@$linear_refs_)) {
593 fatal "# of revisions changed ",
594 "\nbefore:\n",
595 join("\n", @$linear_refs),
596 "\n\nafter:\n",
597 join("\n", @$linear_refs_), "\n",
598 'If you are attempting to commit ',
599 "merges, try running:\n\t",
600 'git rebase --interactive',
601 '--preserve-merges ',
602 $gs->refname,
603 "\nBefore dcommitting";
604 }
Eric Wong711521e2008-08-20 00:30:06 -0700605 if ($url_ ne $expect_url) {
Alexander Gavrilovc03c1f72009-10-09 11:01:04 +0400606 if ($url_ eq $gs->metadata_url) {
607 print
608 "Accepting rewritten URL:",
609 " $url_\n";
610 } else {
611 fatal
612 "URL mismatch after rebase:",
613 " $url_ != $expect_url";
614 }
Eric Wongc74d9ac2007-11-05 03:21:47 -0800615 }
616 if ($uuid_ ne $uuid) {
617 fatal "uuid mismatch after rebase: ",
618 "$uuid_ != $uuid";
619 }
620 # remap parents
621 my (%p, @l, $i);
622 for ($i = 0; $i < scalar @$linear_refs; $i++) {
623 my $new = $linear_refs_->[$i] or next;
624 $p{$new} =
625 $parents->{$linear_refs->[$i]};
626 push @l, $new;
627 }
628 $parents = \%p;
629 $linear_refs = \@l;
630 }
Eric Wongb22d4492006-08-26 00:01:23 -0700631 }
632 }
Thomas Rast5eec27e2009-05-29 17:09:42 +0200633
634 if ($old_head) {
635 my $new_head = command_oneline(qw/rev-parse HEAD/);
636 my $new_is_symbolic = eval {
637 command_oneline(qw/symbolic-ref -q HEAD/);
638 };
639 if ($new_is_symbolic) {
640 print "dcommitted the branch ", $head, "\n";
641 } else {
642 print "dcommitted on a detached HEAD because you gave ",
643 "a revision argument.\n",
644 "The rewritten commit is: ", $new_head, "\n";
645 }
646 command(['checkout', $old_head], STDERR => 0);
647 }
648
Eric Wong3157dd92007-12-13 08:27:34 -0800649 unlink $gs->{index};
Eric Wongb22d4492006-08-26 00:01:23 -0700650}
651
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700652sub cmd_branch {
653 my ($branch_name, $head) = @_;
654
655 unless (defined $branch_name && length $branch_name) {
656 die(($_tag ? "tag" : "branch") . " name required\n");
657 }
658 $head ||= 'HEAD';
659
660 my ($src, $rev, undef, $gs) = working_head_info($head);
661
Deskin Millera0fbc872008-12-01 21:43:00 -0500662 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
Marc Branchaud62244062009-06-23 13:02:08 -0400663 my $allglobs = $remote->{ $_tag ? 'tags' : 'branches' };
664 my $glob;
665 if ($#{$allglobs} == 0) {
666 $glob = $allglobs->[0];
667 } else {
668 unless(defined $_branch_dest) {
669 die "Multiple ",
670 $_tag ? "tag" : "branch",
671 " paths defined for Subversion repository.\n",
672 "You must specify where you want to create the ",
673 $_tag ? "tag" : "branch",
674 " with the --destination argument.\n";
675 }
676 foreach my $g (@{$allglobs}) {
Eric Wongf7050592009-06-25 02:28:15 -0700677 # SVN::Git::Editor could probably be moved to Git.pm..
678 my $re = SVN::Git::Editor::glob2pat($g->{path}->{left});
679 if ($_branch_dest =~ /$re/) {
Marc Branchaud62244062009-06-23 13:02:08 -0400680 $glob = $g;
681 last;
682 }
683 }
684 unless (defined $glob) {
Eric Wongeaa14ff2009-07-25 01:36:06 -0700685 my $dest_re = qr/\b\Q$_branch_dest\E\b/;
686 foreach my $g (@{$allglobs}) {
687 $g->{path}->{left} =~ /$dest_re/ or next;
688 if (defined $glob) {
689 die "Ambiguous destination: ",
690 $_branch_dest, "\nmatches both '",
691 $glob->{path}->{left}, "' and '",
692 $g->{path}->{left}, "'\n";
693 }
694 $glob = $g;
695 }
696 unless (defined $glob) {
697 die "Unknown ",
698 $_tag ? "tag" : "branch",
699 " destination $_branch_dest\n";
700 }
Marc Branchaud62244062009-06-23 13:02:08 -0400701 }
702 }
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700703 my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
704 my $dst = join '/', $remote->{url}, $lft, $branch_name, ($rgt || ());
705
706 my $ctx = SVN::Client->new(
707 auth => Git::SVN::Ra::_auth_providers(),
708 log_msg => sub {
709 ${ $_[0] } = defined $_message
710 ? $_message
711 : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
712 . $branch_name;
713 },
714 );
715
716 eval {
717 $ctx->ls($dst, 'HEAD', 0);
718 } and die "branch ${branch_name} already exists\n";
719
720 print "Copying ${src} at r${rev} to ${dst}...\n";
721 $ctx->copy($src, $rev, $dst)
722 unless $_dry_run;
723
724 $gs->fetch_all;
725}
726
Adam Roben26e60162007-04-27 11:57:53 -0700727sub cmd_find_rev {
Marc-Andre Lureauea14e6c2008-03-11 10:00:45 +0200728 my $revision_or_hash = shift or die "SVN or git revision required ",
729 "as a command-line argument\n";
Adam Roben26e60162007-04-27 11:57:53 -0700730 my $result;
731 if ($revision_or_hash =~ /^r\d+$/) {
Adam Robenb3cb7e42007-04-29 01:35:27 -0700732 my $head = shift;
733 $head ||= 'HEAD';
734 my @refs;
João Abecasis63c56022008-07-14 16:28:04 +0100735 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
Adam Robenb3cb7e42007-04-29 01:35:27 -0700736 unless ($gs) {
737 die "Unable to determine upstream SVN information from ",
738 "$head history\n";
Adam Roben26e60162007-04-27 11:57:53 -0700739 }
Adam Robenb3cb7e42007-04-29 01:35:27 -0700740 my $desired_revision = substr($revision_or_hash, 1);
João Abecasis63c56022008-07-14 16:28:04 +0100741 $result = $gs->rev_map_get($desired_revision, $uuid);
Adam Roben26e60162007-04-27 11:57:53 -0700742 } else {
743 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
744 $result = $rev;
745 }
746 print "$result\n" if $result;
747}
748
Eric Wong905f8b72007-02-16 03:22:40 -0800749sub cmd_rebase {
750 command_noisy(qw/update-index --refresh/);
Eric Wong13c823f2007-04-08 00:59:19 -0700751 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
752 unless ($gs) {
Eric Wong905f8b72007-02-16 03:22:40 -0800753 die "Unable to determine upstream SVN information from ",
754 "working tree history\n";
755 }
Seth Falcon7d45e142008-05-19 20:29:17 -0700756 if ($_dry_run) {
757 print "Remote Branch: " . $gs->refname . "\n";
758 print "SVN URL: " . $url . "\n";
759 return;
760 }
Eric Wong905f8b72007-02-16 03:22:40 -0800761 if (command(qw/diff-index HEAD --/)) {
762 print STDERR "Cannot rebase with uncommited changes:\n";
763 command_noisy('status');
764 exit 1;
765 }
Eric Wongdee41f32007-03-13 11:40:36 -0700766 unless ($_local) {
Steven Grimmcec0d5a2007-11-29 11:54:39 -0800767 # rebase will checkout for us, so no need to do it explicitly
768 $_no_checkout = 'true';
Eric Wongdee41f32007-03-13 11:40:36 -0700769 $_fetch_all ? $gs->fetch_all : $gs->fetch;
770 }
Eric Wong905f8b72007-02-16 03:22:40 -0800771 command_noisy(rebase_cmd(), $gs->refname);
772}
773
Eric Wong5969cbe2007-01-11 17:58:39 -0800774sub cmd_show_ignore {
Eric Wong13c823f2007-04-08 00:59:19 -0700775 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
776 $gs ||= Git::SVN->new;
Eric Wong5969cbe2007-01-11 17:58:39 -0800777 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200778 $gs->prop_walk($gs->{path}, $r, sub {
779 my ($gs, $path, $props) = @_;
780 print STDOUT "\n# $path\n";
781 my $s = $props->{'svn:ignore'} or return;
782 $s =~ s/[\r\n]+/\n/g;
Michael Haggertya7d72542009-08-07 21:21:21 +0200783 $s =~ s/^\n+//;
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200784 chomp $s;
785 $s =~ s#^#$path#gm;
786 print STDOUT "$s\n";
787 });
Eric Wonga5e0ced2006-06-12 15:23:48 -0700788}
789
Vineet Kumar2d879792007-11-19 14:56:15 -0800790sub cmd_show_externals {
791 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
792 $gs ||= Git::SVN->new;
793 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
794 $gs->prop_walk($gs->{path}, $r, sub {
795 my ($gs, $path, $props) = @_;
796 print STDOUT "\n# $path\n";
797 my $s = $props->{'svn:externals'} or return;
798 $s =~ s/[\r\n]+/\n/g;
799 chomp $s;
800 $s =~ s#^#$path#gm;
801 print STDOUT "$s\n";
802 });
803}
804
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200805sub cmd_create_ignore {
806 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
807 $gs ||= Git::SVN->new;
808 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
809 $gs->prop_walk($gs->{path}, $r, sub {
810 my ($gs, $path, $props) = @_;
811 # $path is of the form /path/to/dir/
Brian Gernhardt7d9fd452009-02-19 13:08:04 -0500812 $path = '.' . $path;
813 # SVN can have attributes on empty directories,
814 # which git won't track
815 mkpath([$path]) unless -d $path;
816 my $ignore = $path . '.gitignore';
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200817 my $s = $props->{'svn:ignore'} or return;
818 open(GITIGNORE, '>', $ignore)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200819 or fatal("Failed to open `$ignore' for writing: $!");
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200820 $s =~ s/[\r\n]+/\n/g;
Michael Haggertya7d72542009-08-07 21:21:21 +0200821 $s =~ s/^\n+//;
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200822 chomp $s;
823 # Prefix all patterns so that the ignore doesn't apply
824 # to sub-directories.
825 $s =~ s#^#/#gm;
826 print GITIGNORE "$s\n";
827 close(GITIGNORE)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200828 or fatal("Failed to close `$ignore': $!");
Gustaf Hendebyc4c66b22008-05-05 00:33:09 +0200829 command_noisy('add', '-f', $ignore);
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200830 });
831}
832
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800833sub canonicalize_path {
834 my ($path) = @_;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800835 my $dot_slash_added = 0;
836 if (substr($path, 0, 1) ne "/") {
837 $path = "./" . $path;
838 $dot_slash_added = 1;
839 }
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800840 # File::Spec->canonpath doesn't collapse x/../y into y (for a
841 # good reason), so let's do this manually.
842 $path =~ s#/+#/#g;
843 $path =~ s#/\.(?:/|$)#/#g;
844 $path =~ s#/[^/]+/\.\.##g;
845 $path =~ s#/$##g;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800846 $path =~ s#^\./## if $dot_slash_added;
Gerrit Pape2fe403e2008-07-06 19:28:50 +0000847 $path =~ s#^/##;
848 $path =~ s#^\.$##;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800849 return $path;
850}
851
Ulrich Dangel50ff2362009-06-26 16:52:09 +0200852sub canonicalize_url {
853 my ($url) = @_;
854 $url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
855 return $url;
856}
857
Benoit Sigoure15153452007-10-16 16:36:50 +0200858# get_svnprops(PATH)
859# ------------------
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200860# Helper for cmd_propget and cmd_proplist below.
Benoit Sigoure15153452007-10-16 16:36:50 +0200861sub get_svnprops {
862 my $path = shift;
863 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
864 $gs ||= Git::SVN->new;
865
866 # prefix THE PATH by the sub-directory from which the user
867 # invoked us.
868 $path = $cmd_dir_prefix . $path;
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200869 fatal("No such file or directory: $path") unless -e $path;
Benoit Sigoure15153452007-10-16 16:36:50 +0200870 my $is_dir = -d $path ? 1 : 0;
871 $path = $gs->{path} . '/' . $path;
872
873 # canonicalize the path (otherwise libsvn will abort or fail to
874 # find the file)
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800875 $path = canonicalize_path($path);
Benoit Sigoure15153452007-10-16 16:36:50 +0200876
877 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
878 my $props;
879 if ($is_dir) {
880 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
881 }
882 else {
883 (undef, $props) = $gs->ra->get_file($path, $r, undef);
884 }
885 return $props;
886}
887
888# cmd_propget (PROP, PATH)
889# ------------------------
890# Print the SVN property PROP for PATH.
891sub cmd_propget {
892 my ($prop, $path) = @_;
893 $path = '.' if not defined $path;
894 usage(1) if not defined $prop;
895 my $props = get_svnprops($path);
896 if (not defined $props->{$prop}) {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200897 fatal("`$path' does not have a `$prop' SVN property.");
Benoit Sigoure15153452007-10-16 16:36:50 +0200898 }
899 print $props->{$prop} . "\n";
900}
901
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200902# cmd_proplist (PATH)
903# -------------------
904# Print the list of SVN properties for PATH.
905sub cmd_proplist {
906 my $path = shift;
907 $path = '.' if not defined $path;
908 my $props = get_svnprops($path);
909 print "Properties on '$path':\n";
910 foreach (sort keys %{$props}) {
911 print " $_\n";
912 }
913}
914
Eric Wong8164b652007-01-11 15:35:55 -0800915sub cmd_multi_init {
Eric Wong9d55b412006-06-12 15:53:13 -0700916 my $url = shift;
Marc Branchaud62244062009-06-23 13:02:08 -0400917 unless (defined $_trunk || @_branches || @_tags) {
Eric Wong98327e52007-01-04 18:02:00 -0800918 usage(1);
919 }
Eric Wongdc431662007-05-19 03:59:02 -0700920
Eric Wong8164b652007-01-11 15:35:55 -0800921 $_prefix = '' unless defined $_prefix;
Eric Wongdadc6d22007-02-14 12:27:41 -0800922 if (defined $url) {
Ulrich Dangel50ff2362009-06-26 16:52:09 +0200923 $url = canonicalize_url($url);
Eric Wongdadc6d22007-02-14 12:27:41 -0800924 init_subdir(@_);
925 }
Eric Wongf30603f2007-02-23 01:26:26 -0800926 do_git_init_db();
Eric Wong98327e52007-01-04 18:02:00 -0800927 if (defined $_trunk) {
Adam Brewster6f5748e2009-08-11 23:14:27 -0400928 my $trunk_ref = 'refs/remotes/' . $_prefix . 'trunk';
Eric Wong706587f2007-01-18 17:50:01 -0800929 # try both old-style and new-style lookups:
930 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
Eric Wong8164b652007-01-11 15:35:55 -0800931 unless ($gs_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800932 my ($trunk_url, $trunk_path) =
933 complete_svn_url($url, $_trunk);
934 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
935 undef, $trunk_ref);
Eric Wong98327e52007-01-04 18:02:00 -0800936 }
Eric Wongc35b96e2006-10-11 11:53:21 -0700937 }
Marc Branchaud62244062009-06-23 13:02:08 -0400938 return unless @_branches || @_tags;
Eric Wonge7db67e2007-01-11 17:09:26 -0800939 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
Marc Branchaud62244062009-06-23 13:02:08 -0400940 foreach my $path (@_branches) {
941 complete_url_ls_init($ra, $path, '--branches/-b', $_prefix);
942 }
943 foreach my $path (@_tags) {
944 complete_url_ls_init($ra, $path, '--tags/-t', $_prefix.'tags/');
945 }
Eric Wong9d55b412006-06-12 15:53:13 -0700946}
947
Eric Wong1c8443b2007-01-14 02:17:00 -0800948sub cmd_multi_fetch {
Eric Wong0af9c9f2007-01-27 22:28:56 -0800949 my $remotes = Git::SVN::read_all_remotes();
950 foreach my $repo_id (sort keys %$remotes) {
Eric Wongdb03cd22007-02-13 00:38:02 -0800951 if ($remotes->{$repo_id}->{url}) {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800952 Git::SVN::fetch_all($repo_id, $remotes);
953 }
Eric Wong706587f2007-01-18 17:50:01 -0800954 }
Eric Wong9d55b412006-06-12 15:53:13 -0700955}
956
Eric Wong44320b92007-01-13 22:35:53 -0800957# this command is special because it requires no metadata
958sub cmd_commit_diff {
959 my ($ta, $tb, $url) = @_;
960 my $usage = "Usage: $0 commit-diff -r<revision> ".
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200961 "<tree-ish> <tree-ish> [<URL>]";
Eric Wong44320b92007-01-13 22:35:53 -0800962 fatal($usage) if (!defined $ta || !defined $tb);
Karl Hasselströmd72ab8c2008-05-17 17:07:09 +0200963 my $svn_path = '';
Eric Wong44320b92007-01-13 22:35:53 -0800964 if (!defined $url) {
965 my $gs = eval { Git::SVN->new };
966 if (!$gs) {
967 fatal("Needed URL or usable git-svn --id in ",
968 "the command-line\n", $usage);
969 }
970 $url = $gs->{url};
Eric Wongd3a840d2007-01-26 01:32:45 -0800971 $svn_path = $gs->{path};
Eric Wong44320b92007-01-13 22:35:53 -0800972 }
973 unless (defined $_revision) {
974 fatal("-r|--revision is a required argument\n", $usage);
975 }
976 if (defined $_message && defined $_file) {
977 fatal("Both --message/-m and --file/-F specified ",
978 "for the commit message.\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200979 "I have no idea what you mean");
Eric Wong44320b92007-01-13 22:35:53 -0800980 }
981 if (defined $_file) {
982 $_message = file_to_s($_file);
983 } else {
984 $_message ||= get_commit_entry($tb)->{log};
985 }
986 my $ra ||= Git::SVN::Ra->new($url);
987 my $r = $_revision;
988 if ($r eq 'HEAD') {
989 $r = $ra->get_latest_revnum;
990 } elsif ($r !~ /^\d+$/) {
991 die "revision argument: $r not understood by git-svn\n";
992 }
Eric Wong61395352007-01-27 14:33:08 -0800993 my %ed_opts = ( r => $r,
994 log => $_message,
995 ra => $ra,
996 tree_a => $ta,
997 tree_b => $tb,
998 editor_cb => sub { print "Committed r$_[0]\n" },
999 svn_path => $svn_path );
1000 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong44320b92007-01-13 22:35:53 -08001001 print "No changes\n$ta == $tb\n";
1002 }
Eric Wong44320b92007-01-13 22:35:53 -08001003}
1004
Thomas Rast05427b92008-08-26 21:32:37 +02001005sub escape_uri_only {
1006 my ($uri) = @_;
1007 my @tmp;
1008 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -07001009 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Thomas Rast05427b92008-08-26 21:32:37 +02001010 push @tmp, $_;
1011 }
1012 join('/', @tmp);
1013}
1014
1015sub escape_url {
1016 my ($url) = @_;
1017 if ($url =~ m#^([^:]+)://([^/]*)(.*)$#) {
1018 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
1019 $url = "$scheme://$domain$uri";
1020 }
1021 $url;
1022}
1023
David D. Kilzere6fefa92007-11-21 11:57:18 -08001024sub cmd_info {
Eric Wongbd2d4f92008-08-05 00:35:16 -07001025 my $path = canonicalize_path(defined($_[0]) ? $_[0] : ".");
Thomas Rastedde9112008-08-26 21:32:36 +02001026 my $fullpath = canonicalize_path($cmd_dir_prefix . $path);
Eric Wongbd2d4f92008-08-05 00:35:16 -07001027 if (exists $_[1]) {
David D. Kilzere6fefa92007-11-21 11:57:18 -08001028 die "Too many arguments specified\n";
1029 }
1030
1031 my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
1032
1033 if (!$file_type && !$diff_status) {
Thomas Rast2cf3e3a2008-08-29 15:42:48 +02001034 print STDERR "svn: '$path' is not under version control\n";
1035 exit 1;
David D. Kilzere6fefa92007-11-21 11:57:18 -08001036 }
1037
1038 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1039 unless ($gs) {
1040 die "Unable to determine upstream SVN information from ",
1041 "working tree history\n";
1042 }
Eric Wongbd2d4f92008-08-05 00:35:16 -07001043
1044 # canonicalize_path() will return "" to make libsvn 1.5.x happy,
1045 $path = "." if $path eq "";
1046
Thomas Rastedde9112008-08-26 21:32:36 +02001047 my $full_url = $url . ($fullpath eq "" ? "" : "/$fullpath");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001048
David D. Kilzer8b014d72007-11-21 11:57:19 -08001049 if ($_url) {
Thomas Rast05427b92008-08-26 21:32:37 +02001050 print escape_url($full_url), "\n";
David D. Kilzer8b014d72007-11-21 11:57:19 -08001051 return;
1052 }
1053
David D. Kilzere6fefa92007-11-21 11:57:18 -08001054 my $result = "Path: $path\n";
1055 $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
Thomas Rast05427b92008-08-26 21:32:37 +02001056 $result .= "URL: " . escape_url($full_url) . "\n";
David D. Kilzere6fefa92007-11-21 11:57:18 -08001057
Eric Wonga5460eb2007-11-21 18:20:57 -08001058 eval {
1059 my $repos_root = $gs->repos_root;
1060 Git::SVN::remove_username($repos_root);
Thomas Rast05427b92008-08-26 21:32:37 +02001061 $result .= "Repository Root: " . escape_url($repos_root) . "\n";
Eric Wonga5460eb2007-11-21 18:20:57 -08001062 };
1063 if ($@) {
1064 $result .= "Repository Root: (offline)\n";
1065 }
Marcel Koeppen22ba47f2009-01-19 03:02:01 +01001066 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" &&
1067 ($SVN::Core::VERSION le '1.5.4' || $file_type ne "dir");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001068 $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
1069
1070 $result .= "Node Kind: " .
1071 ($file_type eq "dir" ? "directory" : "file") . "\n";
1072
1073 my $schedule = $diff_status eq "A"
1074 ? "add"
1075 : ($diff_status eq "D" ? "delete" : "normal");
1076 $result .= "Schedule: $schedule\n";
1077
1078 if ($diff_status eq "A") {
1079 print $result, "\n";
1080 return;
1081 }
1082
1083 my ($lc_author, $lc_rev, $lc_date_utc);
Thomas Rastedde9112008-08-26 21:32:36 +02001084 my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001085 my $log = command_output_pipe(@args);
1086 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
1087 while (<$log>) {
1088 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
1089 $lc_author = $1;
1090 $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
1091 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
1092 (undef, $lc_rev, undef) = ::extract_metadata($1);
1093 }
1094 }
1095 close $log;
1096
1097 Git::SVN::Log::set_local_timezone();
1098
1099 $result .= "Last Changed Author: $lc_author\n";
1100 $result .= "Last Changed Rev: $lc_rev\n";
1101 $result .= "Last Changed Date: " .
1102 Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
1103
1104 if ($file_type ne "dir") {
1105 my $text_last_updated_date =
1106 ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
1107 $result .=
1108 "Text Last Updated: " .
1109 Git::SVN::Log::format_svn_date($text_last_updated_date) .
1110 "\n";
1111 my $checksum;
1112 if ($diff_status eq "D") {
1113 my ($fh, $ctx) =
1114 command_output_pipe(qw(cat-file blob), "HEAD:$path");
1115 if ($file_type eq "link") {
1116 my $file_name = <$fh>;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001117 $checksum = md5sum("link $file_name");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001118 } else {
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001119 $checksum = md5sum($fh);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001120 }
1121 command_close_pipe($fh, $ctx);
1122 } elsif ($file_type eq "link") {
1123 my $file_name =
1124 command(qw(cat-file blob), "HEAD:$path");
1125 $checksum =
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001126 md5sum("link " . $file_name);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001127 } else {
1128 open FILE, "<", $path or die $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001129 $checksum = md5sum(\*FILE);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001130 close FILE or die $!;
1131 }
1132 $result .= "Checksum: " . $checksum . "\n";
1133 }
1134
1135 print $result, "\n";
1136}
1137
Ben Jackson195643f2009-06-03 20:45:52 -07001138sub cmd_reset {
1139 my $target = shift || $_revision or die "SVN revision required\n";
1140 $target = $1 if $target =~ /^r(\d+)$/;
1141 $target =~ /^\d+$/ or die "Numeric SVN revision expected\n";
1142 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1143 unless ($gs) {
1144 die "Unable to determine upstream SVN information from ".
1145 "history\n";
1146 }
1147 my ($r, $c) = $gs->find_rev_before($target, not $_fetch_parent);
1148 $gs->rev_map_set($r, $c, 'reset', $uuid);
1149 print "r$r = $c ($gs->{ref_id})\n";
1150}
1151
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -05001152sub cmd_gc {
1153 if (!$can_compress) {
1154 warn "Compress::Zlib could not be found; unhandled.log " .
1155 "files will not be compressed.\n";
1156 }
1157 find({ wanted => \&gc_directory, no_chdir => 1}, "$ENV{GIT_DIR}/svn");
1158}
1159
Eric Wong3397f9d2006-02-16 01:24:16 -08001160########################### utility functions #########################
1161
Eric Wong905f8b72007-02-16 03:22:40 -08001162sub rebase_cmd {
1163 my @cmd = qw/rebase/;
1164 push @cmd, '-v' if $_verbose;
1165 push @cmd, qw/--merge/ if $_merge;
1166 push @cmd, "--strategy=$_strategy" if $_strategy;
1167 @cmd;
1168}
1169
Eric Wong1e889ef2007-02-16 01:45:13 -08001170sub post_fetch_checkout {
1171 return if $_no_checkout;
1172 my $gs = $Git::SVN::_head or return;
1173 return if verify_ref('refs/heads/master^0');
1174
Eric Wongb186a262009-08-12 16:01:59 -07001175 # look for "trunk" ref if it exists
1176 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
1177 my $fetch = $remote->{fetch};
1178 if ($fetch) {
1179 foreach my $p (keys %$fetch) {
1180 basename($fetch->{$p}) eq 'trunk' or next;
1181 $gs = Git::SVN->new($fetch->{$p}, $gs->{repo_id}, $p);
1182 last;
1183 }
1184 }
1185
Eric Wong1e889ef2007-02-16 01:45:13 -08001186 my $valid_head = verify_ref('HEAD^0');
1187 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
1188 return if ($valid_head || !verify_ref('HEAD^0'));
1189
1190 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
1191 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
1192 return if -f $index;
1193
Matthias Lederhofer7ae3df82007-06-03 16:48:16 +02001194 return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
Eric Wong1e889ef2007-02-16 01:45:13 -08001195 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
1196 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
1197 print STDERR "Checked out HEAD:\n ",
1198 $gs->full_url, " r", $gs->last_rev, "\n";
1199}
1200
Eric Wong98327e52007-01-04 18:02:00 -08001201sub complete_svn_url {
1202 my ($url, $path) = @_;
1203 $path =~ s#/+$##;
Eric Wong98327e52007-01-04 18:02:00 -08001204 if ($path !~ m#^[a-z\+]+://#) {
Eric Wong98327e52007-01-04 18:02:00 -08001205 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
1206 fatal("E: '$path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001207 "and a separate URL is not specified");
Eric Wong98327e52007-01-04 18:02:00 -08001208 }
Eric Wong706587f2007-01-18 17:50:01 -08001209 return ($url, $path);
Eric Wong98327e52007-01-04 18:02:00 -08001210 }
Eric Wong706587f2007-01-18 17:50:01 -08001211 return ($path, '');
Eric Wong98327e52007-01-04 18:02:00 -08001212}
1213
Eric Wong9d55b412006-06-12 15:53:13 -07001214sub complete_url_ls_init {
Eric Wong706587f2007-01-18 17:50:01 -08001215 my ($ra, $repo_path, $switch, $pfx) = @_;
1216 unless ($repo_path) {
Eric Wong9d55b412006-06-12 15:53:13 -07001217 print STDERR "W: $switch not specified\n";
1218 return;
1219 }
Eric Wong706587f2007-01-18 17:50:01 -08001220 $repo_path =~ s#/+$##;
1221 if ($repo_path =~ m#^[a-z\+]+://#) {
1222 $ra = Git::SVN::Ra->new($repo_path);
1223 $repo_path = '';
Eric Wonge7db67e2007-01-11 17:09:26 -08001224 } else {
Eric Wong706587f2007-01-18 17:50:01 -08001225 $repo_path =~ s#^/+##;
Eric Wonge7db67e2007-01-11 17:09:26 -08001226 unless ($ra) {
Eric Wong706587f2007-01-18 17:50:01 -08001227 fatal("E: '$repo_path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001228 "and a separate URL is not specified");
Eric Wong9d55b412006-06-12 15:53:13 -07001229 }
Eric Wonge7db67e2007-01-11 17:09:26 -08001230 }
Eric Wong706587f2007-01-18 17:50:01 -08001231 my $url = $ra->{url};
Eric Wongb4d57e52007-02-14 15:10:44 -08001232 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
1233 my $k = "svn-remote.$gs->{repo_id}.url";
1234 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
1235 if ($orig_url && ($orig_url ne $gs->{url})) {
1236 die "$k already set: $orig_url\n",
1237 "wanted to set to: $gs->{url}\n";
Eric Wong88cf4102007-02-01 03:59:07 -08001238 }
Eric Wongb4d57e52007-02-14 15:10:44 -08001239 command_oneline('config', $k, $gs->{url}) unless $orig_url;
Mattias Nissler0b2af452009-07-07 01:40:02 +02001240 my $remote_path = "$gs->{path}/$repo_path";
Eric Wong5268f9e2009-08-16 14:22:12 -07001241 $remote_path =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
Eric Wongb4d57e52007-02-14 15:10:44 -08001242 $remote_path =~ s#/+#/#g;
1243 $remote_path =~ s#^/##g;
Eric Wonged0b9d42008-03-14 11:01:23 -07001244 $remote_path .= "/*" if $remote_path !~ /\*/;
Eric Wongb4d57e52007-02-14 15:10:44 -08001245 my ($n) = ($switch =~ /^--(\w+)/);
1246 if (length $pfx && $pfx !~ m#/$#) {
1247 die "--prefix='$pfx' must have a trailing slash '/'\n";
Eric Wong9d55b412006-06-12 15:53:13 -07001248 }
Marcus Griep570d35c2008-08-08 01:41:57 -07001249 command_noisy('config',
Marc Branchaud62244062009-06-23 13:02:08 -04001250 '--add',
Marcus Griep570d35c2008-08-08 01:41:57 -07001251 "svn-remote.$gs->{repo_id}.$n",
1252 "$remote_path:refs/remotes/$pfx*" .
1253 ('/*' x (($remote_path =~ tr/*/*/) - 1)) );
Eric Wong9d55b412006-06-12 15:53:13 -07001254}
1255
Eric Wongaef4e922006-12-15 10:59:54 -08001256sub verify_ref {
1257 my ($ref) = @_;
Eric Wong2c5c1d52006-12-28 01:16:21 -08001258 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
1259 { STDERR => 0 }); };
Eric Wongaef4e922006-12-15 10:59:54 -08001260}
1261
Eric Wonga5e0ced2006-06-12 15:23:48 -07001262sub get_tree_from_treeish {
Eric Wongcf52b8f2006-02-20 10:57:28 -08001263 my ($treeish) = @_;
Eric Wong44320b92007-01-13 22:35:53 -08001264 # $treeish can be a symbolic ref, too:
Eric Wongaef4e922006-12-15 10:59:54 -08001265 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001266 my $expected;
1267 while ($type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001268 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001269 }
1270 if ($type eq 'commit') {
Eric Wongaef4e922006-12-15 10:59:54 -08001271 $expected = (grep /^tree /, command(qw/cat-file commit/,
1272 $treeish))[0];
Eric Wong44320b92007-01-13 22:35:53 -08001273 ($expected) = ($expected =~ /^tree ($sha1)$/o);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001274 die "Unable to get tree from $treeish\n" unless $expected;
1275 } elsif ($type eq 'tree') {
1276 $expected = $treeish;
1277 } else {
1278 die "$treeish is a $type, expected tree, tag or commit\n";
1279 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07001280 return $expected;
1281}
Eric Wongcf52b8f2006-02-20 10:57:28 -08001282
Eric Wong44320b92007-01-13 22:35:53 -08001283sub get_commit_entry {
1284 my ($treeish) = shift;
1285 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
1286 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1287 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1288 open my $log_fh, '>', $commit_editmsg or croak $!;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001289
Eric Wong44320b92007-01-13 22:35:53 -08001290 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wong4ad45152006-07-09 20:20:48 -07001291 if ($type eq 'commit' || $type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001292 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
Eric Wong44320b92007-01-13 22:35:53 -08001293 $type, $treeish);
Eric Wong3397f9d2006-02-16 01:24:16 -08001294 my $in_msg = 0;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001295 my $author;
1296 my $saw_from = 0;
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001297 my $msgbuf = "";
Eric Wong3397f9d2006-02-16 01:24:16 -08001298 while (<$msg_fh>) {
1299 if (!$in_msg) {
1300 $in_msg = 1 if (/^\s*$/);
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001301 $author = $1 if (/^author (.*>)/);
Eric Wongdf746c52006-03-03 01:20:08 -08001302 } elsif (/^git-svn-id: /) {
Eric Wong44320b92007-01-13 22:35:53 -08001303 # skip this for now, we regenerate the
1304 # correct one on re-fetch anyways
1305 # TODO: set *:merge properties or like...
Eric Wong3397f9d2006-02-16 01:24:16 -08001306 } else {
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001307 if (/^From:/ || /^Signed-off-by:/) {
1308 $saw_from = 1;
1309 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001310 $msgbuf .= $_;
Eric Wong3397f9d2006-02-16 01:24:16 -08001311 }
1312 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001313 $msgbuf =~ s/\s+$//s;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001314 if ($Git::SVN::_add_author_from && defined($author)
1315 && !$saw_from) {
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001316 $msgbuf .= "\n\nFrom: $author";
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001317 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001318 print $log_fh $msgbuf or croak $!;
Eric Wongaef4e922006-12-15 10:59:54 -08001319 command_close_pipe($msg_fh, $ctx);
Eric Wong3397f9d2006-02-16 01:24:16 -08001320 }
Eric Wong44320b92007-01-13 22:35:53 -08001321 close $log_fh or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -08001322
1323 if ($_edit || ($type eq 'tree')) {
1324 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
Eric Wong44320b92007-01-13 22:35:53 -08001325 # TODO: strip out spaces, comments, like git-commit.sh
1326 system($editor, $commit_editmsg);
Eric Wong3397f9d2006-02-16 01:24:16 -08001327 }
Eric Wong44320b92007-01-13 22:35:53 -08001328 rename $commit_editmsg, $commit_msg or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07001329 {
Eric Wongb510df82009-05-28 00:56:23 -07001330 require Encode;
Eric Wong16fc08e2008-10-29 23:49:26 -07001331 # SVN requires messages to be UTF-8 when entering the repo
1332 local $/;
1333 open $log_fh, '<', $commit_msg or croak $!;
1334 binmode $log_fh;
1335 chomp($log_entry{log} = <$log_fh>);
1336
Eric Wongb510df82009-05-28 00:56:23 -07001337 my $enc = Git::config('i18n.commitencoding') || 'UTF-8';
1338 my $msg = $log_entry{log};
1339
1340 eval { $msg = Encode::decode($enc, $msg, 1) };
1341 if ($@) {
1342 die "Could not decode as $enc:\n", $msg,
1343 "\nPerhaps you need to set i18n.commitencoding\n";
Eric Wong16fc08e2008-10-29 23:49:26 -07001344 }
Eric Wongb510df82009-05-28 00:56:23 -07001345
1346 eval { $msg = Encode::encode('UTF-8', $msg, 1) };
1347 die "Could not encode as UTF-8:\n$msg\n" if $@;
1348
1349 $log_entry{log} = $msg;
1350
Eric Wong16fc08e2008-10-29 23:49:26 -07001351 close $log_fh or croak $!;
1352 }
Eric Wong44320b92007-01-13 22:35:53 -08001353 unlink $commit_msg;
1354 \%log_entry;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001355}
1356
Eric Wong3397f9d2006-02-16 01:24:16 -08001357sub s_to_file {
1358 my ($str, $file, $mode) = @_;
1359 open my $fd,'>',$file or croak $!;
1360 print $fd $str,"\n" or croak $!;
1361 close $fd or croak $!;
1362 chmod ($mode &~ umask, $file) if (defined $mode);
1363}
1364
1365sub file_to_s {
1366 my $file = shift;
1367 open my $fd,'<',$file or croak "$!: file: $file\n";
1368 local $/;
1369 my $ret = <$fd>;
1370 close $fd or croak $!;
1371 $ret =~ s/\s*$//s;
1372 return $ret;
1373}
1374
Eric Wongeeb0abe2006-03-03 01:20:08 -08001375# '<svn username> = real-name <email address>' mapping based on git-svnimport:
1376sub load_authors {
1377 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001378 my $log = $cmd eq 'log';
Eric Wongeeb0abe2006-03-03 01:20:08 -08001379 while (<$authors>) {
1380 chomp;
Richard MUSIL575d0252007-07-17 19:02:57 +02001381 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
Eric Wongeeb0abe2006-03-03 01:20:08 -08001382 my ($user, $name, $email) = ($1, $2, $3);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001383 if ($log) {
1384 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
1385 } else {
1386 $users{$user} = [$name, $email];
1387 }
Eric Wong79bb8d82006-06-01 02:35:44 -07001388 }
1389 close $authors or croak $!;
1390}
1391
Tom Princee0d10e12007-01-28 16:16:53 -08001392# convert GetOpt::Long specs for use by git-config
Eric Wongb8c92ca2006-05-24 01:40:37 -07001393sub read_repo_config {
Eric Wong706587f2007-01-18 17:50:01 -08001394 return unless -d $ENV{GIT_DIR};
Eric Wongb8c92ca2006-05-24 01:40:37 -07001395 my $opts = shift;
Eric Wong97ae0912007-02-11 15:21:24 -08001396 my @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001397 foreach my $o (keys %$opts) {
Eric Wong97ae0912007-02-11 15:21:24 -08001398 # if we have mixedCase and a long option-only, then
1399 # it's a config-only variable that we don't need for
1400 # the command-line.
1401 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001402 my $v = $opts->{$o};
Eric Wong97ae0912007-02-11 15:21:24 -08001403 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001404 $key =~ s/-//g;
Deskin Miller225f1d02008-10-23 15:21:34 -04001405 my $arg = 'git config';
Eric Wongb8c92ca2006-05-24 01:40:37 -07001406 $arg .= ' --int' if ($o =~ /[:=]i$/);
1407 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1408 if (ref $v eq 'ARRAY') {
1409 chomp(my @tmp = `$arg --get-all svn.$key`);
1410 @$v = @tmp if @tmp;
1411 } else {
1412 chomp(my $tmp = `$arg --get svn.$key`);
Eric Wong77742842007-02-10 21:07:12 -08001413 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
Eric Wongb8c92ca2006-05-24 01:40:37 -07001414 $$v = $tmp;
1415 }
1416 }
1417 }
Eric Wong97ae0912007-02-11 15:21:24 -08001418 delete @$opts{@config_only} if @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001419}
1420
Eric Wong79bb8d82006-06-01 02:35:44 -07001421sub extract_metadata {
Eric Wongc1927a82006-06-27 19:39:11 -07001422 my $id = shift or return (undef, undef, undef);
Sam Vilain3dfab992007-06-30 20:56:13 +12001423 my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
Eric Wongb3e95932009-07-11 14:13:12 -07001424 \s([a-f\d\-]+)$/ix);
Eric Wonge70dc782006-11-23 14:54:04 -08001425 if (!defined $rev || !$uuid || !$url) {
Eric Wong79bb8d82006-06-01 02:35:44 -07001426 # some of the original repositories I made had
Pavel Roskin82e5a822006-07-10 01:50:18 -04001427 # identifiers like this:
Eric Wongb3e95932009-07-11 14:13:12 -07001428 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/i);
Eric Wong79bb8d82006-06-01 02:35:44 -07001429 }
1430 return ($url, $rev, $uuid);
1431}
1432
Eric Wongc1927a82006-06-27 19:39:11 -07001433sub cmt_metadata {
1434 return extract_metadata((grep(/^git-svn-id: /,
Eric Wongaef4e922006-12-15 10:59:54 -08001435 command(qw/cat-file commit/, shift)))[-1]);
Eric Wongc1927a82006-06-27 19:39:11 -07001436}
1437
Boris Byk6ea42032009-04-11 00:32:41 +04001438sub cmt_sha2rev_batch {
1439 my %s2r;
1440 my ($pid, $in, $out, $ctx) = command_bidi_pipe(qw/cat-file --batch/);
1441 my $list = shift;
1442
1443 foreach my $sha (@{$list}) {
1444 my $first = 1;
1445 my $size = 0;
1446 print $out $sha, "\n";
1447
1448 while (my $line = <$in>) {
1449 if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) {
1450 last;
1451 } elsif ($first &&
1452 $line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) {
1453 $first = 0;
1454 $size = $1;
1455 next;
1456 } elsif ($line =~ /^(git-svn-id: )/) {
1457 my (undef, $rev, undef) =
1458 extract_metadata($line);
1459 $s2r{$sha} = $rev;
1460 }
1461
1462 $size -= length($line);
1463 last if ($size == 0);
1464 }
1465 }
1466
1467 command_close_bidi_pipe($pid, $in, $out, $ctx);
1468
1469 return \%s2r;
1470}
1471
Eric Wong905f8b72007-02-16 03:22:40 -08001472sub working_head_info {
1473 my ($head, $refs) = @_;
Pedro Melob6309ac2008-04-10 17:05:21 +01001474 my @args = ('log', '--no-color', '--first-parent', '--pretty=medium');
Lars Hjemli05b4df32007-09-05 11:35:29 +02001475 my ($fh, $ctx) = command_output_pipe(@args, $head);
Sam Vilain3dfab992007-06-30 20:56:13 +12001476 my $hash;
Sam Vilain40cb8f82007-06-30 20:56:14 +12001477 my %max;
Sam Vilain3dfab992007-06-30 20:56:13 +12001478 while (<$fh>) {
1479 if ( m{^commit ($::sha1)$} ) {
1480 unshift @$refs, $hash if $hash and $refs;
1481 $hash = $1;
1482 next;
1483 }
1484 next unless s{^\s*(git-svn-id:)}{$1};
1485 my ($url, $rev, $uuid) = extract_metadata($_);
Eric Wong13c823f2007-04-08 00:59:19 -07001486 if (defined $url && defined $rev) {
Sam Vilain40cb8f82007-06-30 20:56:14 +12001487 next if $max{$url} and $max{$url} < $rev;
Eric Wong13c823f2007-04-08 00:59:19 -07001488 if (my $gs = Git::SVN->find_by_url($url)) {
João Abecasis63c56022008-07-14 16:28:04 +01001489 my $c = $gs->rev_map_get($rev, $uuid);
Adam Robenb03c7a62007-04-25 11:50:32 -07001490 if ($c && $c eq $hash) {
Eric Wong13c823f2007-04-08 00:59:19 -07001491 close $fh; # break the pipe
1492 return ($url, $rev, $uuid, $gs);
Sam Vilain40cb8f82007-06-30 20:56:14 +12001493 } else {
Eric Wong060610c2007-12-08 23:27:41 -08001494 $max{$url} ||= $gs->rev_map_max;
Eric Wong13c823f2007-04-08 00:59:19 -07001495 }
1496 }
1497 }
Eric Wong905f8b72007-02-16 03:22:40 -08001498 }
Eric Wong13c823f2007-04-08 00:59:19 -07001499 command_close_pipe($fh, $ctx);
1500 (undef, undef, undef, undef);
Eric Wong905f8b72007-02-16 03:22:40 -08001501}
1502
Eric Wong733a65a2007-06-13 02:23:28 -07001503sub read_commit_parents {
1504 my ($parents, $c) = @_;
Eric Wong7b02b852007-09-08 16:33:08 -07001505 chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
1506 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1507 @{$parents->{$c}} = split(/ /, $p);
Eric Wong733a65a2007-06-13 02:23:28 -07001508}
1509
1510sub linearize_history {
1511 my ($gs, $refs) = @_;
1512 my %parents;
1513 foreach my $c (@$refs) {
1514 read_commit_parents(\%parents, $c);
1515 }
1516
1517 my @linear_refs;
1518 my %skip = ();
1519 my $last_svn_commit = $gs->last_commit;
1520 foreach my $c (reverse @$refs) {
1521 next if $c eq $last_svn_commit;
1522 last if $skip{$c};
1523
1524 unshift @linear_refs, $c;
1525 $skip{$c} = 1;
1526
1527 # we only want the first parent to diff against for linear
1528 # history, we save the rest to inject when we finalize the
1529 # svn commit
1530 my $fp_a = verify_ref("$c~1");
1531 my $fp_b = shift @{$parents{$c}} if $parents{$c};
1532 if (!$fp_a || !$fp_b) {
1533 die "Commit $c\n",
1534 "has no parent commit, and therefore ",
1535 "nothing to diff against.\n",
1536 "You should be working from a repository ",
1537 "originally created by git-svn\n";
1538 }
1539 if ($fp_a ne $fp_b) {
1540 die "$c~1 = $fp_a, however parsing commit $c ",
1541 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1542 }
1543
1544 foreach my $p (@{$parents{$c}}) {
1545 $skip{$p} = 1;
1546 }
1547 }
1548 (\@linear_refs, \%parents);
1549}
1550
David D. Kilzere6fefa92007-11-21 11:57:18 -08001551sub find_file_type_and_diff_status {
1552 my ($path) = @_;
Dmitry Potapov107cee52008-07-21 00:14:07 +04001553 return ('dir', '') if $path eq '';
David D. Kilzere6fefa92007-11-21 11:57:18 -08001554
1555 my $diff_output =
1556 command_oneline(qw(diff --cached --name-status --), $path) || "";
1557 my $diff_status = (split(' ', $diff_output))[0] || "";
1558
1559 my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || "";
1560
1561 return (undef, undef) if !$diff_status && !$ls_tree;
1562
1563 if ($diff_status eq "A") {
1564 return ("link", $diff_status) if -l $path;
1565 return ("dir", $diff_status) if -d $path;
1566 return ("file", $diff_status);
1567 }
1568
1569 my $mode = (split(' ', $ls_tree))[0] || "";
1570
1571 return ("link", $diff_status) if $mode eq "120000";
1572 return ("dir", $diff_status) if $mode eq "040000";
1573 return ("file", $diff_status);
1574}
1575
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001576sub md5sum {
1577 my $arg = shift;
1578 my $ref = ref $arg;
1579 my $md5 = Digest::MD5->new();
Marcus Griep0b191382008-08-12 12:00:53 -04001580 if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001581 $md5->addfile($arg) or croak $!;
1582 } elsif ($ref eq 'SCALAR') {
1583 $md5->add($$arg) or croak $!;
1584 } elsif (!$ref) {
1585 $md5->add($arg) or croak $!;
1586 } else {
1587 ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1588 }
1589 return $md5->hexdigest();
1590}
1591
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -05001592sub gc_directory {
1593 if ($can_compress && -f $_ && basename($_) eq "unhandled.log") {
1594 my $out_filename = $_ . ".gz";
1595 open my $in_fh, "<", $_ or die "Unable to open $_: $!\n";
1596 binmode $in_fh;
1597 my $gz = Compress::Zlib::gzopen($out_filename, "ab") or
1598 die "Unable to open $out_filename: $!\n";
1599
1600 my $res;
1601 while ($res = sysread($in_fh, my $str, 1024)) {
1602 $gz->gzwrite($str) or
1603 die "Unable to write: ".$gz->gzerror()."!\n";
1604 }
1605 unlink $_ or die "unlink $File::Find::name: $!\n";
1606 } elsif (-f $_ && basename($_) eq "index") {
1607 unlink $_ or die "unlink $_: $!\n";
1608 }
1609}
1610
Eric Wong9b981fc2007-01-11 12:14:21 -08001611package Git::SVN;
1612use strict;
1613use warnings;
Eric Wong060610c2007-12-08 23:27:41 -08001614use Fcntl qw/:DEFAULT :seek/;
1615use constant rev_map_fmt => 'NH40';
Eric Wongecc712d2007-01-31 12:28:10 -08001616use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
Eric Wong62e349d2007-02-16 19:57:29 -08001617 $_repack $_repack_flags $_use_svm_props $_head
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00001618 $_use_svnsync_props $no_reuse_existing $_minimize_url
Pete Harlane82f0d72009-01-17 20:10:14 -08001619 $_use_log_author $_add_author_from $_localtime/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001620use Carp qw/croak/;
1621use File::Path qw/mkpath/;
Eric Wong373274f2007-01-31 13:54:23 -08001622use File::Copy qw/copy/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001623use IPC::Open3;
1624
Karl Hasselström94bc9142008-02-03 17:56:18 +01001625my ($_gc_nr, $_gc_period);
1626
Eric Wong9b981fc2007-01-11 12:14:21 -08001627# properties that we do not log:
1628my %SKIP_PROP;
1629BEGIN {
1630 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
1631 svn:special svn:executable
1632 svn:entry:committed-rev
1633 svn:entry:last-author
1634 svn:entry:uuid
1635 svn:entry:committed-date/;
Eric Wong91b03282007-02-11 00:51:33 -08001636
1637 # some options are read globally, but can be overridden locally
1638 # per [svn-remote "..."] section. Command-line options will *NOT*
1639 # override options set in an [svn-remote "..."] section
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001640 no strict 'refs';
1641 for my $option (qw/follow_parent no_metadata use_svm_props
1642 use_svnsync_props/) {
1643 my $key = $option;
Eric Wong91b03282007-02-11 00:51:33 -08001644 $key =~ tr/_//d;
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001645 my $prop = "-$option";
1646 *$option = sub {
1647 my ($self) = @_;
1648 return $self->{$prop} if exists $self->{$prop};
1649 my $k = "svn-remote.$self->{repo_id}.$key";
1650 eval { command_oneline(qw/config --get/, $k) };
1651 if ($@) {
1652 $self->{$prop} = ${"Git::SVN::_$option"};
Eric Wong91b03282007-02-11 00:51:33 -08001653 } else {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001654 my $v = command_oneline(qw/config --bool/,$k);
1655 $self->{$prop} = $v eq 'false' ? 0 : 1;
Eric Wong91b03282007-02-11 00:51:33 -08001656 }
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001657 return $self->{$prop};
1658 }
Eric Wong91b03282007-02-11 00:51:33 -08001659 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001660}
1661
Marcus Griep0b191382008-08-12 12:00:53 -04001662
Eric Wong321b1842008-01-02 10:10:03 -08001663my (%LOCKFILES, %INDEX_FILES);
1664END {
1665 unlink keys %LOCKFILES if %LOCKFILES;
1666 unlink keys %INDEX_FILES if %INDEX_FILES;
1667}
Eric Wong373274f2007-01-31 13:54:23 -08001668
Eric Wong4bb9ed02007-02-03 13:29:17 -08001669sub resolve_local_globs {
1670 my ($url, $fetch, $glob_spec) = @_;
1671 return unless defined $glob_spec;
1672 my $ref = $glob_spec->{ref};
1673 my $path = $glob_spec->{path};
Adam Brewster6f5748e2009-08-11 23:14:27 -04001674 foreach (command(qw#for-each-ref --format=%(refname) refs/#)) {
1675 next unless m#^$ref->{regex}$#;
Eric Wong4bb9ed02007-02-03 13:29:17 -08001676 my $p = $1;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001677 my $pathname = desanitize_refname($path->full_path($p));
1678 my $refname = desanitize_refname($ref->full_path($p));
Eric Wong4bb9ed02007-02-03 13:29:17 -08001679 if (my $existing = $fetch->{$pathname}) {
1680 if ($existing ne $refname) {
1681 die "Refspec conflict:\n",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001682 "existing: $existing\n",
1683 " globbed: $refname\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08001684 }
Adam Brewster6f5748e2009-08-11 23:14:27 -04001685 my $u = (::cmt_metadata("$refname"))[0];
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001686 $u =~ s!^\Q$url\E(/|$)!! or die
Adam Brewster6f5748e2009-08-11 23:14:27 -04001687 "$refname: '$url' not found in '$u'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08001688 if ($pathname ne $u) {
1689 warn "W: Refspec glob conflict ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001690 "(ref: $refname):\n",
Eric Wong4bb9ed02007-02-03 13:29:17 -08001691 "expected path: $pathname\n",
1692 " real path: $u\n",
1693 "Continuing ahead with $u\n";
1694 next;
1695 }
1696 } else {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001697 $fetch->{$pathname} = $refname;
1698 }
1699 }
1700}
1701
Eric Wonge98671e2007-02-14 02:21:19 -08001702sub parse_revision_argument {
1703 my ($base, $head) = @_;
1704 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
1705 return ($base, $head);
1706 }
1707 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
1708 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
1709 return ($head, $head) if ($::_revision eq 'HEAD');
1710 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
1711 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
1712 die "revision argument: $::_revision not understood by git-svn\n";
1713}
1714
Eric Wong0af9c9f2007-01-27 22:28:56 -08001715sub fetch_all {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001716 my ($repo_id, $remotes) = @_;
Eric Wong905f8b72007-02-16 03:22:40 -08001717 if (ref $repo_id) {
1718 my $gs = $repo_id;
1719 $repo_id = undef;
1720 $repo_id = $gs->{repo_id};
1721 }
1722 $remotes ||= read_all_remotes();
Eric Wong7447b4b2007-02-14 18:38:46 -08001723 my $remote = $remotes->{$repo_id} or
1724 die "[svn-remote \"$repo_id\"] unknown\n";
Eric Wonge5181922007-02-08 12:53:57 -08001725 my $fetch = $remote->{fetch};
Eric Wong7447b4b2007-02-14 18:38:46 -08001726 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
Eric Wonge5181922007-02-08 12:53:57 -08001727 my (@gs, @globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001728 my $ra = Git::SVN::Ra->new($url);
Eric Wong26a62d52007-02-12 13:25:25 -08001729 my $uuid = $ra->get_uuid;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001730 my $head = $ra->get_latest_revnum;
Eric Wong4aacaeb2009-07-20 02:06:24 -07001731 $ra->get_log("", $head, 0, 1, 0, 1, sub { $head = $_[1] });
Eric Wong28710f72007-02-14 13:32:21 -08001732 my $base = defined $fetch ? $head : 0;
Eric Wonge5181922007-02-08 12:53:57 -08001733
1734 # read the max revs for wildcard expansion (branches/*, tags/*)
1735 foreach my $t (qw/branches tags/) {
1736 defined $remote->{$t} or next;
Marc Branchaud62244062009-06-23 13:02:08 -04001737 push @globs, @{$remote->{$t}};
1738
Eric Wong93f26892007-02-11 01:20:26 -08001739 my $max_rev = eval { tmp_config(qw/--int --get/,
1740 "svn-remote.$repo_id.${t}-maxRev") };
1741 if (defined $max_rev && ($max_rev < $base)) {
1742 $base = $max_rev;
Eric Wongd6d33462007-02-16 04:05:33 -08001743 } elsif (!defined $max_rev) {
1744 $base = 0;
Eric Wonge5181922007-02-08 12:53:57 -08001745 }
1746 }
1747
Eric Wongdb03cd22007-02-13 00:38:02 -08001748 if ($fetch) {
1749 foreach my $p (sort keys %$fetch) {
1750 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
Eric Wong060610c2007-12-08 23:27:41 -08001751 my $lr = $gs->rev_map_max;
Eric Wongdb03cd22007-02-13 00:38:02 -08001752 if (defined $lr) {
1753 $base = $lr if ($lr < $base);
1754 }
1755 push @gs, $gs;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001756 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08001757 }
Eric Wonge98671e2007-02-14 02:21:19 -08001758
1759 ($base, $head) = parse_revision_argument($base, $head);
Eric Wonge5181922007-02-08 12:53:57 -08001760 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001761}
1762
Eric Wong47e39c52007-01-21 04:27:09 -08001763sub read_all_remotes {
1764 my $r = {};
João Abecasis63c56022008-07-14 16:28:04 +01001765 my $use_svm_props = eval { command_oneline(qw/config --bool
1766 svn.useSvmProps/) };
1767 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
Adam Brewster6f5748e2009-08-11 23:14:27 -04001768 my $svn_refspec = qr{\s*/?(.*?)\s*:\s*(.+?)\s*};
Eric Wong8b8fc062007-01-22 11:44:57 -08001769 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
Adam Brewster6f5748e2009-08-11 23:14:27 -04001770 if (m!^(.+)\.fetch=$svn_refspec$!) {
1771 my ($remote, $local_ref, $remote_ref) = ($1, $2, $3);
1772 die("svn-remote.$remote: remote ref '$remote_ref' "
1773 . "must start with 'refs/'\n")
1774 unless $remote_ref =~ m{^refs/};
Eric Wong46cf98b2007-07-14 12:40:32 -07001775 $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
João Abecasis63c56022008-07-14 16:28:04 +01001776 $r->{$remote}->{svm} = {} if $use_svm_props;
1777 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
1778 $r->{$1}->{svm} = {};
Eric Wong47e39c52007-01-21 04:27:09 -08001779 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1780 $r->{$1}->{url} = $2;
Adam Brewster6f5748e2009-08-11 23:14:27 -04001781 } elsif (m!^(.+)\.(branches|tags)=$svn_refspec$!) {
1782 my ($remote, $t, $local_ref, $remote_ref) =
1783 ($1, $2, $3, $4);
1784 die("svn-remote.$remote: remote ref '$remote_ref' ($t) "
1785 . "must start with 'refs/'\n")
1786 unless $remote_ref =~ m{^refs/};
Marc Branchaud62244062009-06-23 13:02:08 -04001787 my $rs = {
Adam Brewster6f5748e2009-08-11 23:14:27 -04001788 t => $t,
1789 remote => $remote,
1790 path => Git::SVN::GlobSpec->new($local_ref),
1791 ref => Git::SVN::GlobSpec->new($remote_ref) };
Eric Wong4bb9ed02007-02-03 13:29:17 -08001792 if (length($rs->{ref}->{right}) != 0) {
1793 die "The '*' glob character must be the last ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001794 "character of '$remote_ref'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08001795 }
Adam Brewster6f5748e2009-08-11 23:14:27 -04001796 push @{ $r->{$remote}->{$t} }, $rs;
Eric Wong47e39c52007-01-21 04:27:09 -08001797 }
1798 }
João Abecasis63c56022008-07-14 16:28:04 +01001799
1800 map {
1801 if (defined $r->{$_}->{svm}) {
1802 my $svm;
1803 eval {
1804 my $section = "svn-remote.$_";
1805 $svm = {
1806 source => tmp_config('--get',
1807 "$section.svm-source"),
1808 replace => tmp_config('--get',
1809 "$section.svm-replace"),
1810 }
1811 };
1812 $r->{$_}->{svm} = $svm;
1813 }
1814 } keys %$r;
1815
Eric Wong47e39c52007-01-21 04:27:09 -08001816 $r;
1817}
1818
Eric Wongecc712d2007-01-31 12:28:10 -08001819sub init_vars {
Karl Hasselström94bc9142008-02-03 17:56:18 +01001820 $_gc_nr = $_gc_period = 1000;
Karl Hasselströmaf788a62008-02-03 17:56:12 +01001821 if (defined $_repack || defined $_repack_flags) {
1822 warn "Repack options are obsolete; they have no effect.\n";
1823 }
Eric Wongecc712d2007-01-31 12:28:10 -08001824}
1825
Eric Wongb805b442007-01-22 13:52:04 -08001826sub verify_remotes_sanity {
Eric Wong536c4b02007-01-23 11:35:53 -08001827 return unless -d $ENV{GIT_DIR};
Eric Wongb805b442007-01-22 13:52:04 -08001828 my %seen;
1829 foreach (command(qw/config -l/)) {
1830 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1831 if ($seen{$1}) {
1832 die "Remote ref refs/remote/$1 is tracked by",
1833 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1834 "Please resolve this ambiguity in ",
1835 "your git configuration file before ",
1836 "continuing\n";
1837 }
1838 $seen{$1} = $_;
1839 }
1840 }
1841}
1842
Eric Wonge6434f82007-01-23 16:29:23 -08001843sub find_existing_remote {
1844 my ($url, $remotes) = @_;
Eric Wongbefc9ad2007-02-17 02:53:07 -08001845 return undef if $no_reuse_existing;
Eric Wonge6434f82007-01-23 16:29:23 -08001846 my $existing;
1847 foreach my $repo_id (keys %$remotes) {
1848 my $u = $remotes->{$repo_id}->{url} or next;
1849 next if $u ne $url;
1850 $existing = $repo_id;
1851 last;
1852 }
1853 $existing;
1854}
1855
1856sub init_remote_config {
Eric Wongd8115c52007-02-01 03:30:31 -08001857 my ($self, $url, $no_write) = @_;
Eric Wonge6434f82007-01-23 16:29:23 -08001858 $url =~ s!/+$!!; # strip trailing slash
1859 my $r = read_all_remotes();
1860 my $existing = find_existing_remote($url, $r);
1861 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001862 unless ($no_write) {
1863 print STDERR "Using existing ",
1864 "[svn-remote \"$existing\"]\n";
1865 }
Eric Wonge6434f82007-01-23 16:29:23 -08001866 $self->{repo_id} = $existing;
Eric Wong4a1bb4c2007-05-13 09:58:14 -07001867 } elsif ($_minimize_url) {
Eric Wonge6434f82007-01-23 16:29:23 -08001868 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
1869 $existing = find_existing_remote($min_url, $r);
1870 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001871 unless ($no_write) {
1872 print STDERR "Using existing ",
1873 "[svn-remote \"$existing\"]\n";
1874 }
Eric Wonge6434f82007-01-23 16:29:23 -08001875 $self->{repo_id} = $existing;
1876 }
1877 if ($min_url ne $url) {
Eric Wonge5181922007-02-08 12:53:57 -08001878 unless ($no_write) {
1879 print STDERR "Using higher level of URL: ",
1880 "$url => $min_url\n";
1881 }
Eric Wonge6434f82007-01-23 16:29:23 -08001882 my $old_path = $self->{path};
1883 $self->{path} = $url;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001884 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
Eric Wonge6434f82007-01-23 16:29:23 -08001885 if (length $old_path) {
1886 $self->{path} .= "/$old_path";
1887 }
1888 $url = $min_url;
1889 }
1890 }
1891 my $orig_url;
1892 if (!$existing) {
1893 # verify that we aren't overwriting anything:
1894 $orig_url = eval {
1895 command_oneline('config', '--get',
1896 "svn-remote.$self->{repo_id}.url")
1897 };
1898 if ($orig_url && ($orig_url ne $url)) {
1899 die "svn-remote.$self->{repo_id}.url already set: ",
1900 "$orig_url\nwanted to set to: $url\n";
1901 }
1902 }
1903 my ($xrepo_id, $xpath) = find_ref($self->refname);
Adam Brewster6f5748e2009-08-11 23:14:27 -04001904 if (!$no_write && defined $xpath) {
Eric Wonge6434f82007-01-23 16:29:23 -08001905 die "svn-remote.$xrepo_id.fetch already set to track ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001906 "$xpath:", $self->refname, "\n";
Eric Wonge6434f82007-01-23 16:29:23 -08001907 }
Eric Wongd8115c52007-02-01 03:30:31 -08001908 unless ($no_write) {
1909 command_noisy('config',
1910 "svn-remote.$self->{repo_id}.url", $url);
Eric Wong46cf98b2007-07-14 12:40:32 -07001911 $self->{path} =~ s{^/}{};
Eric Wong5268f9e2009-08-16 14:22:12 -07001912 $self->{path} =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
Eric Wongd8115c52007-02-01 03:30:31 -08001913 command_noisy('config', '--add',
1914 "svn-remote.$self->{repo_id}.fetch",
1915 "$self->{path}:".$self->refname);
1916 }
Eric Wonge6434f82007-01-23 16:29:23 -08001917 $self->{url} = $url;
1918}
1919
Eric Wonga8ae2622007-02-13 14:22:11 -08001920sub find_by_url { # repos_root and, path are optional
1921 my ($class, $full_url, $repos_root, $path) = @_;
Adam Roben56973d22007-04-25 12:42:58 -07001922
Eric Wong1a97a502007-02-20 00:43:19 -08001923 return undef unless defined $full_url;
Adam Roben56973d22007-04-25 12:42:58 -07001924 remove_username($full_url);
1925 remove_username($repos_root) if defined $repos_root;
Eric Wonga8ae2622007-02-13 14:22:11 -08001926 my $remotes = read_all_remotes();
1927 if (defined $full_url && defined $repos_root && !defined $path) {
1928 $path = $full_url;
1929 $path =~ s#^\Q$repos_root\E(?:/|$)##;
1930 }
1931 foreach my $repo_id (keys %$remotes) {
1932 my $u = $remotes->{$repo_id}->{url} or next;
Adam Roben56973d22007-04-25 12:42:58 -07001933 remove_username($u);
Eric Wonga8ae2622007-02-13 14:22:11 -08001934 next if defined $repos_root && $repos_root ne $u;
1935
1936 my $fetch = $remotes->{$repo_id}->{fetch} || {};
Marc Branchaud62244062009-06-23 13:02:08 -04001937 foreach my $t (qw/branches tags/) {
1938 foreach my $globspec (@{$remotes->{$repo_id}->{$t}}) {
1939 resolve_local_globs($u, $fetch, $globspec);
1940 }
Eric Wonga8ae2622007-02-13 14:22:11 -08001941 }
1942 my $p = $path;
John Goerzen0bb91d92008-03-08 16:04:05 -06001943 my $rwr = rewrite_root({repo_id => $repo_id});
João Abecasis63c56022008-07-14 16:28:04 +01001944 my $svm = $remotes->{$repo_id}->{svm}
1945 if defined $remotes->{$repo_id}->{svm};
Eric Wonga8ae2622007-02-13 14:22:11 -08001946 unless (defined $p) {
1947 $p = $full_url;
John Goerzen0bb91d92008-03-08 16:04:05 -06001948 my $z = $u;
João Abecasis63c56022008-07-14 16:28:04 +01001949 my $prefix = '';
John Goerzen0bb91d92008-03-08 16:04:05 -06001950 if ($rwr) {
1951 $z = $rwr;
Dévai Tamás1b7e5432009-02-12 00:14:02 +01001952 remove_username($z);
João Abecasis63c56022008-07-14 16:28:04 +01001953 } elsif (defined $svm) {
1954 $z = $svm->{source};
1955 $prefix = $svm->{replace};
1956 $prefix =~ s#^\Q$u\E(?:/|$)##;
1957 $prefix =~ s#/$##;
John Goerzen0bb91d92008-03-08 16:04:05 -06001958 }
João Abecasis63c56022008-07-14 16:28:04 +01001959 $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next;
Eric Wonga8ae2622007-02-13 14:22:11 -08001960 }
1961 foreach my $f (keys %$fetch) {
1962 next if $f ne $p;
1963 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
1964 }
1965 }
1966 undef;
1967}
1968
Eric Wong9b981fc2007-01-11 12:14:21 -08001969sub init {
Eric Wongd8115c52007-02-01 03:30:31 -08001970 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08001971 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong9b981fc2007-01-11 12:14:21 -08001972 if (defined $url) {
Eric Wongd8115c52007-02-01 03:30:31 -08001973 $self->init_remote_config($url, $no_write);
Eric Wong9b981fc2007-01-11 12:14:21 -08001974 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001975 $self;
1976}
1977
Eric Wong706587f2007-01-18 17:50:01 -08001978sub find_ref {
1979 my ($ref_id) = @_;
1980 foreach (command(qw/config -l/)) {
1981 next unless m!^svn-remote\.(.+)\.fetch=
Adam Brewster6f5748e2009-08-11 23:14:27 -04001982 \s*/?(.*?)\s*:\s*(.+?)\s*$!x;
Eric Wong706587f2007-01-18 17:50:01 -08001983 my ($repo_id, $path, $ref) = ($1, $2, $3);
1984 if ($ref eq $ref_id) {
1985 $path = '' if ($path =~ m#^\./?#);
1986 return ($repo_id, $path);
1987 }
1988 }
1989 (undef, undef, undef);
1990}
1991
Eric Wong9b981fc2007-01-11 12:14:21 -08001992sub new {
Eric Wong706587f2007-01-18 17:50:01 -08001993 my ($class, $ref_id, $repo_id, $path) = @_;
1994 if (defined $ref_id && !defined $repo_id && !defined $path) {
1995 ($repo_id, $path) = find_ref($ref_id);
1996 if (!defined $repo_id) {
1997 die "Could not find a \"svn-remote.*.fetch\" key ",
1998 "in the repository configuration matching: ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001999 "$ref_id\n";
Eric Wong706587f2007-01-18 17:50:01 -08002000 }
2001 }
2002 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong8b8fc062007-01-22 11:44:57 -08002003 if (!defined $self->{path} || !length $self->{path}) {
2004 my $fetch = command_oneline('config', '--get',
2005 "svn-remote.$repo_id.fetch",
Adam Brewster6f5748e2009-08-11 23:14:27 -04002006 ":$ref_id\$") or
Eric Wong8b8fc062007-01-22 11:44:57 -08002007 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04002008 "\":$ref_id\$\" in config\n";
Eric Wong8b8fc062007-01-22 11:44:57 -08002009 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
2010 }
Eric Wong706587f2007-01-18 17:50:01 -08002011 $self->{url} = command_oneline('config', '--get',
2012 "svn-remote.$repo_id.url") or
2013 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
Eric Wongd6d33462007-02-16 04:05:33 -08002014 $self->rebuild;
Eric Wong9b981fc2007-01-11 12:14:21 -08002015 $self;
2016}
2017
Robert Ewaldbf655fd2007-07-30 11:08:21 +02002018sub refname {
Adam Brewster6f5748e2009-08-11 23:14:27 -04002019 my ($refname) = $_[0]->{ref_id} ;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02002020
2021 # It cannot end with a slash /, we'll throw up on this because
2022 # SVN can't have directories with a slash in their name, either:
2023 if ($refname =~ m{/$}) {
2024 die "ref: '$refname' ends with a trailing slash, this is ",
2025 "not permitted by git nor Subversion\n";
2026 }
2027
2028 # It cannot have ASCII control character space, tilde ~, caret ^,
2029 # colon :, question-mark ?, asterisk *, space, or open bracket [
2030 # anywhere.
2031 #
2032 # Additionally, % must be escaped because it is used for escaping
2033 # and we want our escaped refname to be reversible
2034 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
2035
2036 # no slash-separated component can begin with a dot .
2037 # /.* becomes /%2E*
2038 $refname =~ s{/\.}{/%2E}g;
2039
2040 # It cannot have two consecutive dots .. anywhere
2041 # .. becomes %2E%2E
2042 $refname =~ s{\.\.}{%2E%2E}g;
2043
2044 return $refname;
2045}
2046
2047sub desanitize_refname {
2048 my ($refname) = @_;
2049 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
2050 return $refname;
2051}
Eric Wong9b981fc2007-01-11 12:14:21 -08002052
Eric Wong26a62d52007-02-12 13:25:25 -08002053sub svm_uuid {
2054 my ($self) = @_;
2055 return $self->{svm}->{uuid} if $self->svm;
2056 $self->ra;
2057 unless ($self->{svm}) {
2058 die "SVM UUID not cached, and reading remotely failed\n";
2059 }
2060 $self->{svm}->{uuid};
2061}
Eric Wong8a49ee92007-02-10 20:46:50 -08002062
Eric Wong26a62d52007-02-12 13:25:25 -08002063sub svm {
2064 my ($self) = @_;
2065 return $self->{svm} if $self->{svm};
2066 my $svm;
Eric Wong8a49ee92007-02-10 20:46:50 -08002067 # see if we have it in our config, first:
2068 eval {
Eric Wong26a62d52007-02-12 13:25:25 -08002069 my $section = "svn-remote.$self->{repo_id}";
2070 $svm = {
Eric Wong93f26892007-02-11 01:20:26 -08002071 source => tmp_config('--get', "$section.svm-source"),
2072 uuid => tmp_config('--get', "$section.svm-uuid"),
Eric Wongbefc9ad2007-02-17 02:53:07 -08002073 replace => tmp_config('--get', "$section.svm-replace"),
Eric Wong8a49ee92007-02-10 20:46:50 -08002074 }
2075 };
Eric Wongbefc9ad2007-02-17 02:53:07 -08002076 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
2077 $self->{svm} = $svm;
2078 }
Eric Wong26a62d52007-02-12 13:25:25 -08002079 $self->{svm};
2080}
2081
2082sub _set_svm_vars {
2083 my ($self, $ra) = @_;
Eric Wongdb03cd22007-02-13 00:38:02 -08002084 return $ra if $self->svm;
Eric Wong26a62d52007-02-12 13:25:25 -08002085
Eric Wongdb03cd22007-02-13 00:38:02 -08002086 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08002087 "(svm:source, svm:uuid) ",
Eric Wongdb03cd22007-02-13 00:38:02 -08002088 "from the following URLs:\n" );
2089 sub read_svm_props {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002090 my ($self, $ra, $path, $r) = @_;
2091 my $props = ($ra->get_dir($path, $r))[2];
Eric Wongdb03cd22007-02-13 00:38:02 -08002092 my $src = $props->{'svm:source'};
Eric Wong8a49ee92007-02-10 20:46:50 -08002093 my $uuid = $props->{'svm:uuid'};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002094 return undef if (!$src || !$uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08002095
Eric Wongbefc9ad2007-02-17 02:53:07 -08002096 chomp($src, $uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08002097
Eric Wongb3e95932009-07-11 14:13:12 -07002098 $uuid =~ m{^[0-9a-f\-]{30,}$}i
Eric Wong8a49ee92007-02-10 20:46:50 -08002099 or die "doesn't look right - svm:uuid is '$uuid'\n";
Eric Wongbefc9ad2007-02-17 02:53:07 -08002100
2101 # the '!' is used to mark the repos_root!/relative/path
2102 $src =~ s{/?!/?}{/};
Eric Wongdb03cd22007-02-13 00:38:02 -08002103 $src =~ s{/+$}{}; # no trailing slashes please
Eric Wongbefc9ad2007-02-17 02:53:07 -08002104 # username is of no interest
Eric Wongdb03cd22007-02-13 00:38:02 -08002105 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
Eric Wong8a49ee92007-02-10 20:46:50 -08002106
Eric Wongbefc9ad2007-02-17 02:53:07 -08002107 my $replace = $ra->{url};
2108 $replace .= "/$path" if length $path;
2109
Eric Wongdb03cd22007-02-13 00:38:02 -08002110 my $section = "svn-remote.$self->{repo_id}";
Eric Wongbefc9ad2007-02-17 02:53:07 -08002111 tmp_config("$section.svm-source", $src);
2112 tmp_config("$section.svm-replace", $replace);
2113 tmp_config("$section.svm-uuid", $uuid);
2114 $self->{svm} = {
2115 source => $src,
2116 uuid => $uuid,
2117 replace => $replace
2118 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002119 }
Eric Wongdb03cd22007-02-13 00:38:02 -08002120
2121 my $r = $ra->get_latest_revnum;
2122 my $path = $self->{path};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002123 my %tried;
Eric Wongdb03cd22007-02-13 00:38:02 -08002124 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002125 unless ($tried{"$self->{url}/$path"}) {
2126 return $ra if $self->read_svm_props($ra, $path, $r);
2127 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002128 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002129 $path =~ s#/?[^/]+$##;
Eric Wong8a49ee92007-02-10 20:46:50 -08002130 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002131 die "Path: '$path' should be ''\n" if $path ne '';
2132 return $ra if $self->read_svm_props($ra, $path, $r);
2133 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002134
2135 if ($ra->{repos_root} eq $self->{url}) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002136 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002137 }
2138
2139 # nope, make sure we're connected to the repository root:
2140 my $ok;
2141 my @tried_b;
2142 $path = $ra->{svn_path};
Eric Wongdb03cd22007-02-13 00:38:02 -08002143 $ra = Git::SVN::Ra->new($ra->{repos_root});
2144 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002145 unless ($tried{"$ra->{url}/$path"}) {
2146 $ok = $self->read_svm_props($ra, $path, $r);
2147 last if $ok;
2148 $tried{"$ra->{url}/$path"} = 1;
2149 }
2150 $path =~ s#/?[^/]+$##;
Eric Wongdb03cd22007-02-13 00:38:02 -08002151 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002152 die "Path: '$path' should be ''\n" if $path ne '';
2153 $ok ||= $self->read_svm_props($ra, $path, $r);
2154 $tried{"$ra->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002155 if (!$ok) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002156 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002157 }
2158 Git::SVN::Ra->new($self->{url});
Eric Wong8a49ee92007-02-10 20:46:50 -08002159}
2160
Eric Wong62e349d2007-02-16 19:57:29 -08002161sub svnsync {
2162 my ($self) = @_;
2163 return $self->{svnsync} if $self->{svnsync};
2164
2165 if ($self->no_metadata) {
2166 die "Can't have both 'noMetadata' and ",
2167 "'useSvnsyncProps' options set!\n";
2168 }
2169 if ($self->rewrite_root) {
2170 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
2171 "options set!\n";
2172 }
2173
2174 my $svnsync;
2175 # see if we have it in our config, first:
2176 eval {
2177 my $section = "svn-remote.$self->{repo_id}";
Eric Wong98fa5b62008-01-11 23:13:55 -08002178
2179 my $url = tmp_config('--get', "$section.svnsync-url");
2180 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
2181 die "doesn't look right - svn:sync-from-url is '$url'\n";
2182
2183 my $uuid = tmp_config('--get', "$section.svnsync-uuid");
Eric Wongb3e95932009-07-11 14:13:12 -07002184 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
Eric Wong98fa5b62008-01-11 23:13:55 -08002185 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2186
2187 $svnsync = { url => $url, uuid => $uuid }
Eric Wong62e349d2007-02-16 19:57:29 -08002188 };
2189 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
2190 return $self->{svnsync} = $svnsync;
2191 }
2192
2193 my $err = "useSvnsyncProps set, but failed to read " .
2194 "svnsync property: svn:sync-from-";
2195 my $rp = $self->ra->rev_proplist(0);
2196
2197 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
Eric Wong98fa5b62008-01-11 23:13:55 -08002198 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
Eric Wong62e349d2007-02-16 19:57:29 -08002199 die "doesn't look right - svn:sync-from-url is '$url'\n";
2200
2201 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
Eric Wongb3e95932009-07-11 14:13:12 -07002202 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
Eric Wong62e349d2007-02-16 19:57:29 -08002203 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2204
2205 my $section = "svn-remote.$self->{repo_id}";
2206 tmp_config('--add', "$section.svnsync-uuid", $uuid);
2207 tmp_config('--add', "$section.svnsync-url", $url);
2208 return $self->{svnsync} = { url => $url, uuid => $uuid };
2209}
2210
Eric Wong26a62d52007-02-12 13:25:25 -08002211# this allows us to memoize our SVN::Ra UUID locally and avoid a
2212# remote lookup (useful for 'git svn log').
2213sub ra_uuid {
2214 my ($self) = @_;
2215 unless ($self->{ra_uuid}) {
2216 my $key = "svn-remote.$self->{repo_id}.uuid";
2217 my $uuid = eval { tmp_config('--get', $key) };
Eric Wongb3e95932009-07-11 14:13:12 -07002218 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/i) {
Eric Wong26a62d52007-02-12 13:25:25 -08002219 $self->{ra_uuid} = $uuid;
2220 } else {
2221 die "ra_uuid called without URL\n" unless $self->{url};
2222 $self->{ra_uuid} = $self->ra->get_uuid;
2223 tmp_config('--add', $key, $self->{ra_uuid});
2224 }
2225 }
2226 $self->{ra_uuid};
2227}
2228
Eric Wonga5460eb2007-11-21 18:20:57 -08002229sub _set_repos_root {
2230 my ($self, $repos_root) = @_;
2231 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2232 $repos_root ||= $self->ra->{repos_root};
2233 tmp_config($k, $repos_root);
2234 $repos_root;
2235}
2236
2237sub repos_root {
2238 my ($self) = @_;
2239 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2240 eval { tmp_config('--get', $k) } || $self->_set_repos_root;
2241}
2242
Eric Wong9b981fc2007-01-11 12:14:21 -08002243sub ra {
2244 my ($self) = shift;
Eric Wong8a49ee92007-02-10 20:46:50 -08002245 my $ra = Git::SVN::Ra->new($self->{url});
Eric Wonga5460eb2007-11-21 18:20:57 -08002246 $self->_set_repos_root($ra->{repos_root});
Eric Wong91b03282007-02-11 00:51:33 -08002247 if ($self->use_svm_props && !$self->{svm}) {
2248 if ($self->no_metadata) {
Eric Wong97ae0912007-02-11 15:21:24 -08002249 die "Can't have both 'noMetadata' and ",
2250 "'useSvmProps' options set!\n";
Eric Wong62e349d2007-02-16 19:57:29 -08002251 } elsif ($self->use_svnsync_props) {
2252 die "Can't have both 'useSvnsyncProps' and ",
2253 "'useSvmProps' options set!\n";
Eric Wong91b03282007-02-11 00:51:33 -08002254 }
Eric Wong26a62d52007-02-12 13:25:25 -08002255 $ra = $self->_set_svm_vars($ra);
Eric Wong8a49ee92007-02-10 20:46:50 -08002256 $self->{-want_revprops} = 1;
2257 }
2258 $ra;
Eric Wong9b981fc2007-01-11 12:14:21 -08002259}
2260
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002261# prop_walk(PATH, REV, SUB)
2262# -------------------------
2263# Recursively traverse PATH at revision REV and invoke SUB for each
2264# directory that contains a SVN property. SUB will be invoked as
2265# follows: &SUB(gs, path, props); where `gs' is this instance of
2266# Git::SVN, `path' the path to the directory where the properties
2267# `props' were found. The `path' will be relative to point of checkout,
2268# that is, if url://repo/trunk is the current Git branch, and that
2269# directory contains a sub-directory `d', SUB will be invoked with `/d/'
2270# as `path' (note the trailing `/').
2271sub prop_walk {
2272 my ($self, $path, $rev, $sub) = @_;
2273
Kevin Ballard35cda062008-01-09 01:37:20 -05002274 $path =~ s#^/##;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002275 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
2276 $path =~ s#^/*#/#g;
Eric Wong9b981fc2007-01-11 12:14:21 -08002277 my $p = $path;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002278 # Strip the irrelevant part of the path.
2279 $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
2280 # Ensure the path is terminated by a `/'.
2281 $p =~ s#/*$#/#;
2282
2283 # The properties contain all the internal SVN stuff nobody
2284 # (usually) cares about.
2285 my $interesting_props = 0;
2286 foreach (keys %{$props}) {
2287 # If it doesn't start with `svn:', it must be a
2288 # user-defined property.
2289 ++$interesting_props and next if $_ !~ /^svn:/;
2290 # FIXME: Fragile, if SVN adds new public properties,
2291 # this needs to be updated.
2292 ++$interesting_props if /^svn:(?:ignore|keywords|executable
2293 |eol-style|mime-type
2294 |externals|needs-lock)$/x;
Eric Wong9b981fc2007-01-11 12:14:21 -08002295 }
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002296 &$sub($self, $p, $props) if $interesting_props;
2297
Eric Wong9b981fc2007-01-11 12:14:21 -08002298 foreach (sort keys %$dirent) {
Eric Wong0dc03d62007-05-13 01:04:43 -07002299 next if $dirent->{$_}->{kind} != $SVN::Node::dir;
Christian Engwerb7166cc2008-05-27 08:46:55 +00002300 $self->prop_walk($self->{path} . $p . $_, $rev, $sub);
Eric Wong9b981fc2007-01-11 12:14:21 -08002301 }
2302}
2303
Eric Wong3ebe8df2007-01-25 17:35:40 -08002304sub last_rev { ($_[0]->last_rev_commit)[0] }
2305sub last_commit { ($_[0]->last_rev_commit)[1] }
2306
Eric Wong9b981fc2007-01-11 12:14:21 -08002307# returns the newest SVN revision number and newest commit SHA1
2308sub last_rev_commit {
2309 my ($self) = @_;
2310 if (defined $self->{last_rev} && defined $self->{last_commit}) {
2311 return ($self->{last_rev}, $self->{last_commit});
2312 }
Eric Wongd2866f92007-01-11 12:26:16 -08002313 my $c = ::verify_ref($self->refname.'^0');
Eric Wong91b03282007-02-11 00:51:33 -08002314 if ($c && !$self->use_svm_props && !$self->no_metadata) {
Eric Wongd2866f92007-01-11 12:26:16 -08002315 my $rev = (::cmt_metadata($c))[1];
Eric Wong9b981fc2007-01-11 12:14:21 -08002316 if (defined $rev) {
2317 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2318 return ($rev, $c);
2319 }
2320 }
Eric Wong060610c2007-12-08 23:27:41 -08002321 my $map_path = $self->map_path;
2322 unless (-e $map_path) {
Eric Wong26a62d52007-02-12 13:25:25 -08002323 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
2324 return (undef, undef);
2325 }
Eric Wong66ab84b2007-12-08 23:27:42 -08002326 my ($rev, $commit) = $self->rev_map_max(1);
Eric Wong060610c2007-12-08 23:27:41 -08002327 ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
2328 return ($rev, $commit);
Eric Wong9b981fc2007-01-11 12:14:21 -08002329}
2330
Eric Wong3ebe8df2007-01-25 17:35:40 -08002331sub get_fetch_range {
2332 my ($self, $min, $max) = @_;
2333 $max ||= $self->ra->get_latest_revnum;
Eric Wong060610c2007-12-08 23:27:41 -08002334 $min ||= $self->rev_map_max;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002335 (++$min, $max);
Eric Wong9b981fc2007-01-11 12:14:21 -08002336}
2337
Eric Wong8a49ee92007-02-10 20:46:50 -08002338sub tmp_config {
Eric Wong93f26892007-02-11 01:20:26 -08002339 my (@args) = @_;
Eric Wongb7e53482007-02-16 04:09:28 -08002340 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
2341 my $config = "$ENV{GIT_DIR}/svn/.metadata";
Eric Wong38570a42007-06-13 02:37:05 -07002342 if (! -f $config && -f $old_def_config) {
Eric Wongb7e53482007-02-16 04:09:28 -08002343 rename $old_def_config, $config or
2344 die "Failed rename $old_def_config => $config: $!\n";
2345 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002346 my $old_config = $ENV{GIT_CONFIG};
Eric Wong93f26892007-02-11 01:20:26 -08002347 $ENV{GIT_CONFIG} = $config;
Eric Wong8a49ee92007-02-10 20:46:50 -08002348 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002349 my @ret = eval {
2350 unless (-f $config) {
2351 mkfile($config);
2352 open my $fh, '>', $config or
2353 die "Can't open $config: $!\n";
2354 print $fh "; This file is used internally by ",
2355 "git-svn\n" or die
2356 "Couldn't write to $config: $!\n";
2357 print $fh "; You should not have to edit it\n" or
2358 die "Couldn't write to $config: $!\n";
2359 close $fh or die "Couldn't close $config: $!\n";
2360 }
2361 command('config', @args);
2362 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002363 my $err = $@;
2364 if (defined $old_config) {
2365 $ENV{GIT_CONFIG} = $old_config;
2366 } else {
2367 delete $ENV{GIT_CONFIG};
2368 }
2369 die $err if $err;
2370 wantarray ? @ret : $ret[0];
2371}
2372
Eric Wong9b981fc2007-01-11 12:14:21 -08002373sub tmp_index_do {
2374 my ($self, $sub) = @_;
2375 my $old_index = $ENV{GIT_INDEX_FILE};
2376 $ENV{GIT_INDEX_FILE} = $self->{index};
Eric Wong8a49ee92007-02-10 20:46:50 -08002377 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002378 my @ret = eval {
2379 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
2380 mkpath([$dir]) unless -d $dir;
2381 &$sub;
2382 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002383 my $err = $@;
2384 if (defined $old_index) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002385 $ENV{GIT_INDEX_FILE} = $old_index;
2386 } else {
2387 delete $ENV{GIT_INDEX_FILE};
2388 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002389 die $err if $err;
Eric Wong9b981fc2007-01-11 12:14:21 -08002390 wantarray ? @ret : $ret[0];
2391}
2392
2393sub assert_index_clean {
2394 my ($self, $treeish) = @_;
2395
2396 $self->tmp_index_do(sub {
2397 command_noisy('read-tree', $treeish) unless -e $self->{index};
2398 my $x = command_oneline('write-tree');
2399 my ($y) = (command(qw/cat-file commit/, $treeish) =~
2400 /^tree ($::sha1)/mo);
Eric Wonge8d120b2007-02-14 16:29:52 -08002401 return if $y eq $x;
2402
2403 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2404 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2405 command_noisy('read-tree', $treeish);
Eric Wong9b981fc2007-01-11 12:14:21 -08002406 $x = command_oneline('write-tree');
2407 if ($y ne $x) {
2408 ::fatal "trees ($treeish) $y != $x\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02002409 "Something is seriously wrong...";
Eric Wong9b981fc2007-01-11 12:14:21 -08002410 }
2411 });
2412}
2413
2414sub get_commit_parents {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002415 my ($self, $log_entry) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002416 my (%seen, @ret, @tmp);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002417 # legacy support for 'set-tree'; this is only used by set_tree_cb:
2418 if (my $ip = $self->{inject_parents}) {
2419 if (my $commit = delete $ip->{$log_entry->{revision}}) {
2420 push @tmp, $commit;
Eric Wong9b981fc2007-01-11 12:14:21 -08002421 }
2422 }
Eric Wongd2866f92007-01-11 12:26:16 -08002423 if (my $cur = ::verify_ref($self->refname.'^0')) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002424 push @tmp, $cur;
2425 }
Eric Wong733a65a2007-06-13 02:23:28 -07002426 if (my $ipd = $self->{inject_parents_dcommit}) {
2427 if (my $commit = delete $ipd->{$log_entry->{revision}}) {
2428 push @tmp, @$commit;
2429 }
2430 }
Eric Wong44320b92007-01-13 22:35:53 -08002431 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
Eric Wong9b981fc2007-01-11 12:14:21 -08002432 while (my $p = shift @tmp) {
2433 next if $seen{$p};
2434 $seen{$p} = 1;
2435 push @ret, $p;
2436 # MAXPARENT is defined to 16 in commit-tree.c:
2437 last if @ret >= 16;
2438 }
2439 if (@tmp) {
Eric Wong44320b92007-01-13 22:35:53 -08002440 die "r$log_entry->{revision}: No room for parents:\n\t",
Eric Wong9b981fc2007-01-11 12:14:21 -08002441 join("\n\t", @tmp), "\n";
2442 }
2443 @ret;
2444}
2445
Eric Wongaea736c2007-02-16 19:15:21 -08002446sub rewrite_root {
2447 my ($self) = @_;
2448 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
2449 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2450 my $rwr = eval { command_oneline(qw/config --get/, $k) };
2451 if ($rwr) {
2452 $rwr =~ s#/+$##;
2453 if ($rwr !~ m#^[a-z\+]+://#) {
2454 die "$rwr is not a valid URL (key: $k)\n";
2455 }
2456 }
2457 $self->{-rewrite_root} = $rwr;
2458}
2459
2460sub metadata_url {
2461 my ($self) = @_;
2462 ($self->rewrite_root || $self->{url}) .
2463 (length $self->{path} ? '/' . $self->{path} : '');
2464}
2465
Eric Wong706587f2007-01-18 17:50:01 -08002466sub full_url {
Eric Wong9b981fc2007-01-11 12:14:21 -08002467 my ($self) = @_;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002468 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
Eric Wong9b981fc2007-01-11 12:14:21 -08002469}
2470
Eric Wongad948022007-12-15 19:08:22 -08002471
2472sub set_commit_header_env {
2473 my ($log_entry) = @_;
2474 my %env;
2475 foreach my $ned (qw/NAME EMAIL DATE/) {
2476 foreach my $ac (qw/AUTHOR COMMITTER/) {
2477 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2478 }
2479 }
2480
2481 $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
2482 $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
2483 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
2484
2485 $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
2486 ? $log_entry->{commit_name}
2487 : $log_entry->{name};
2488 $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
2489 ? $log_entry->{commit_email}
2490 : $log_entry->{email};
2491 \%env;
2492}
2493
2494sub restore_commit_header_env {
2495 my ($env) = @_;
2496 foreach my $ned (qw/NAME EMAIL DATE/) {
2497 foreach my $ac (qw/AUTHOR COMMITTER/) {
2498 my $k = "GIT_${ac}_${ned}";
2499 if (defined $env->{$k}) {
2500 $ENV{$k} = $env->{$k};
2501 } else {
2502 delete $ENV{$k};
2503 }
2504 }
2505 }
2506}
2507
Karl Hasselström94bc9142008-02-03 17:56:18 +01002508sub gc {
2509 command_noisy('gc', '--auto');
2510};
2511
Eric Wong9b981fc2007-01-11 12:14:21 -08002512sub do_git_commit {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002513 my ($self, $log_entry) = @_;
Eric Wong8a603772007-01-31 02:45:50 -08002514 my $lr = $self->last_rev;
2515 if (defined $lr && $lr >= $log_entry->{revision}) {
2516 die "Last fetched revision of ", $self->refname,
2517 " was r$lr, but we are about to fetch: ",
2518 "r$log_entry->{revision}!\n";
2519 }
Eric Wong060610c2007-12-08 23:27:41 -08002520 if (my $c = $self->rev_map_get($log_entry->{revision})) {
Eric Wong44320b92007-01-13 22:35:53 -08002521 croak "$log_entry->{revision} = $c already exists! ",
Eric Wong9b981fc2007-01-11 12:14:21 -08002522 "Why are we refetching it?\n";
2523 }
Eric Wongad948022007-12-15 19:08:22 -08002524 my $old_env = set_commit_header_env($log_entry);
Eric Wong44320b92007-01-13 22:35:53 -08002525 my $tree = $log_entry->{tree};
Eric Wong9b981fc2007-01-11 12:14:21 -08002526 if (!defined $tree) {
2527 $tree = $self->tmp_index_do(sub {
2528 command_oneline('write-tree') });
2529 }
2530 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2531
Deskin Millere855bfc2008-10-31 00:10:25 -04002532 my @exec = ('git', 'commit-tree', $tree);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002533 foreach ($self->get_commit_parents($log_entry)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002534 push @exec, '-p', $_;
2535 }
2536 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2537 or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07002538 binmode $msg_fh;
2539
2540 # we always get UTF-8 from SVN, but we may want our commits in
2541 # a different encoding.
2542 if (my $enc = Git::config('i18n.commitencoding')) {
2543 require Encode;
2544 Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
2545 }
Eric Wong44320b92007-01-13 22:35:53 -08002546 print $msg_fh $log_entry->{log} or croak $!;
Eric Wongad948022007-12-15 19:08:22 -08002547 restore_commit_header_env($old_env);
Eric Wong91b03282007-02-11 00:51:33 -08002548 unless ($self->no_metadata) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002549 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2550 or croak $!;
Eric Wong9760adc2007-01-31 03:06:56 -08002551 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002552 $msg_fh->flush == 0 or croak $!;
2553 close $msg_fh or croak $!;
2554 chomp(my $commit = do { local $/; <$out_fh> });
2555 close $out_fh or croak $!;
2556 waitpid $pid, 0;
2557 croak $? if $?;
2558 if ($commit !~ /^$::sha1$/o) {
2559 die "Failed to commit, invalid sha1: $commit\n";
2560 }
2561
Eric Wong060610c2007-12-08 23:27:41 -08002562 $self->rev_map_set($log_entry->{revision}, $commit, 1);
Eric Wong9b981fc2007-01-11 12:14:21 -08002563
Eric Wong44320b92007-01-13 22:35:53 -08002564 $self->{last_rev} = $log_entry->{revision};
Eric Wong9b981fc2007-01-11 12:14:21 -08002565 $self->{last_commit} = $commit;
Simon Arlott49750f32009-03-30 19:31:41 +01002566 print "r$log_entry->{revision}" unless $::_q > 1;
Eric Wong8a49ee92007-02-10 20:46:50 -08002567 if (defined $log_entry->{svm_revision}) {
Simon Arlott49750f32009-03-30 19:31:41 +01002568 print " (\@$log_entry->{svm_revision})" unless $::_q > 1;
Eric Wong060610c2007-12-08 23:27:41 -08002569 $self->rev_map_set($log_entry->{svm_revision}, $commit,
Eric Wong26a62d52007-02-12 13:25:25 -08002570 0, $self->svm_uuid);
Eric Wong8a49ee92007-02-10 20:46:50 -08002571 }
Simon Arlott49750f32009-03-30 19:31:41 +01002572 print " = $commit ($self->{ref_id})\n" unless $::_q > 1;
Karl Hasselström94bc9142008-02-03 17:56:18 +01002573 if (--$_gc_nr == 0) {
2574 $_gc_nr = $_gc_period;
2575 gc();
2576 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002577 return $commit;
2578}
2579
Eric Wongfbcc1732007-02-06 18:35:30 -08002580sub match_paths {
2581 my ($self, $paths, $r) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002582 return 1 if $self->{path} eq '';
Eric Wongd542aed2007-02-09 02:19:41 -08002583 if (my $path = $paths->{"/$self->{path}"}) {
2584 return ($path->{action} eq 'D') ? 0 : 1;
2585 }
Mattias Nissler0b2af452009-07-07 01:40:02 +02002586 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
Eric Wongfbcc1732007-02-06 18:35:30 -08002587 if (grep /$self->{path_regex}/, keys %$paths) {
2588 return 1;
2589 }
2590 my $c = '';
2591 foreach (split m#/#, $self->{path}) {
2592 $c .= "/$_";
Eric Wong74a81222007-02-10 13:28:50 -08002593 next unless ($paths->{$c} &&
2594 ($paths->{$c}->{action} =~ /^[AR]$/));
Eric Wonge5181922007-02-08 12:53:57 -08002595 if ($self->ra->check_path($self->{path}, $r) ==
2596 $SVN::Node::dir) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002597 return 1;
2598 }
2599 }
2600 return 0;
2601}
2602
Eric Wong15710b62007-01-22 02:20:33 -08002603sub find_parent_branch {
2604 my ($self, $paths, $rev) = @_;
Eric Wong91b03282007-02-11 00:51:33 -08002605 return undef unless $self->follow_parent;
Eric Wonge5a0b242007-01-25 15:44:54 -08002606 unless (defined $paths) {
Eric Wongc7eba712007-01-31 03:45:28 -08002607 my $err_handler = $SVN::Error::handler;
2608 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
Mattias Nissler3c49a032009-07-07 01:39:52 +02002609 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1,
2610 sub { $paths = $_[0] });
Eric Wongc7eba712007-01-31 03:45:28 -08002611 $SVN::Error::handler = $err_handler;
Eric Wonge5a0b242007-01-25 15:44:54 -08002612 }
2613 return undef unless defined $paths;
Eric Wong15710b62007-01-22 02:20:33 -08002614
2615 # look for a parent from another branch:
Mattias Nissler0b2af452009-07-07 01:40:02 +02002616 my @b_path_components = split m#/#, $self->{path};
Eric Wong7f578c52007-01-24 02:16:25 -08002617 my @a_path_components;
2618 my $i;
2619 while (@b_path_components) {
2620 $i = $paths->{'/'.join('/', @b_path_components)};
Eric Wong74a81222007-02-10 13:28:50 -08002621 last if $i && defined $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002622 unshift(@a_path_components, pop(@b_path_components));
2623 }
Eric Wong74a81222007-02-10 13:28:50 -08002624 return undef unless defined $i && defined $i->{copyfrom_path};
2625 my $branch_from = $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002626 if (@a_path_components) {
2627 print STDERR "branch_from: $branch_from => ";
2628 $branch_from .= '/'.join('/', @a_path_components);
2629 print STDERR $branch_from, "\n";
2630 }
Eric Wong3ebe8df2007-01-25 17:35:40 -08002631 my $r = $i->{copyfrom_rev};
Eric Wong15710b62007-01-22 02:20:33 -08002632 my $repos_root = $self->ra->{repos_root};
2633 my $url = $self->ra->{url};
Mattias Nissler0b2af452009-07-07 01:40:02 +02002634 my $new_url = $url . $branch_from;
Eric Wong15710b62007-01-22 02:20:33 -08002635 print STDERR "Found possible branch point: ",
Simon Arlott85886162009-10-09 13:21:13 +01002636 "$new_url => ", $self->full_url, ", $r\n"
2637 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002638 $branch_from =~ s#^/##;
Mattias Nissler0b2af452009-07-07 01:40:02 +02002639 my $gs = $self->other_gs($new_url, $url,
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002640 $branch_from, $r, $self->{ref_id});
Eric Wong15710b62007-01-22 02:20:33 -08002641 my ($r0, $parent) = $gs->find_rev_before($r, 1);
Deskin Miller553589f2008-12-08 08:31:31 -05002642 {
2643 my ($base, $head);
2644 if (!defined $r0 || !defined $parent) {
2645 ($base, $head) = parse_revision_argument(0, $r);
2646 } else {
2647 if ($r0 < $r) {
2648 $gs->ra->get_log([$gs->{path}], $r0 + 1, $r, 1,
2649 0, 1, sub { $base = $_[1] - 1 });
2650 }
2651 }
2652 if (defined $base && $base <= $r) {
Eric Wongd627de62007-04-15 03:01:29 -07002653 $gs->fetch($base, $r);
2654 }
Deskin Miller553589f2008-12-08 08:31:31 -05002655 ($r0, $parent) = $gs->find_rev_before($r, 1);
Eric Wong15710b62007-01-22 02:20:33 -08002656 }
Eric Wongef70de92007-02-01 04:12:41 -08002657 if (defined $r0 && defined $parent) {
Simon Arlott85886162009-10-09 13:21:13 +01002658 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n"
2659 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002660 my $ed;
2661 if ($self->ra->can_do_switch) {
Eric Wong2e5e2482007-02-23 02:21:59 -08002662 $self->assert_index_clean($parent);
Simon Arlott85886162009-10-09 13:21:13 +01002663 print STDERR "Following parent with do_switch\n"
2664 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002665 # do_switch works with svn/trunk >= r22312, but that
Eric Wong2b27f6c2007-01-28 04:59:05 -08002666 # is not included with SVN 1.4.3 (the latest version
Eric Wong15710b62007-01-22 02:20:33 -08002667 # at the moment), so we can't rely on it
Eric Wong83c2fcf2009-02-22 20:25:00 -08002668 $self->{last_rev} = $r0;
Eric Wong15710b62007-01-22 02:20:33 -08002669 $self->{last_commit} = $parent;
Eric Wong8841b372009-02-11 01:56:58 -08002670 $ed = SVN::Git::Fetcher->new($self, $gs->{path});
Eric Wong8a603772007-01-31 02:45:50 -08002671 $gs->ra->gs_do_switch($r0, $rev, $gs,
Eric Wong15710b62007-01-22 02:20:33 -08002672 $self->full_url, $ed)
2673 or die "SVN connection failed somewhere...\n";
Steven Walter9ff74e92007-09-28 13:24:19 -04002674 } elsif ($self->ra->trees_match($new_url, $r0,
2675 $self->full_url, $rev)) {
2676 print STDERR "Trees match:\n",
2677 " $new_url\@$r0\n",
2678 " ${\$self->full_url}\@$rev\n",
Simon Arlott85886162009-10-09 13:21:13 +01002679 "Following parent with no changes\n"
2680 unless $::_q > 1;
Steven Walter9ff74e92007-09-28 13:24:19 -04002681 $self->tmp_index_do(sub {
2682 command_noisy('read-tree', $parent);
2683 });
2684 $self->{last_commit} = $parent;
Eric Wong15710b62007-01-22 02:20:33 -08002685 } else {
Simon Arlott85886162009-10-09 13:21:13 +01002686 print STDERR "Following parent with do_update\n"
2687 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002688 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08002689 $self->ra->gs_do_update($rev, $rev, $self, $ed)
Eric Wong15710b62007-01-22 02:20:33 -08002690 or die "SVN connection failed somewhere...\n";
2691 }
Simon Arlott85886162009-10-09 13:21:13 +01002692 print STDERR "Successfully followed parent\n" unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002693 return $self->make_log_entry($rev, [$parent], $ed);
2694 }
Eric Wong15710b62007-01-22 02:20:33 -08002695 return undef;
2696}
2697
Eric Wong9b981fc2007-01-11 12:14:21 -08002698sub do_fetch {
Eric Wong706587f2007-01-18 17:50:01 -08002699 my ($self, $paths, $rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08002700 my $ed;
Eric Wong9b981fc2007-01-11 12:14:21 -08002701 my ($last_rev, @parents);
Eric Wongb9dffd82007-02-09 01:28:30 -08002702 if (my $lc = $self->last_commit) {
2703 # we can have a branch that was deleted, then re-added
2704 # under the same name but copied from another path, in
2705 # which case we'll have multiple parents (we don't
2706 # want to break the original ref, nor lose copypath info):
2707 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2708 push @{$log_entry->{parents}}, $lc;
2709 return $log_entry;
2710 }
Eric Wong15710b62007-01-22 02:20:33 -08002711 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002712 $last_rev = $self->{last_rev};
Eric Wongb9dffd82007-02-09 01:28:30 -08002713 $ed->{c} = $lc;
2714 @parents = ($lc);
Eric Wong9b981fc2007-01-11 12:14:21 -08002715 } else {
2716 $last_rev = $rev;
Eric Wong15710b62007-01-22 02:20:33 -08002717 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2718 return $log_entry;
2719 }
2720 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002721 }
Eric Wong8a603772007-01-31 02:45:50 -08002722 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002723 die "SVN connection failed somewhere...\n";
2724 }
2725 $self->make_log_entry($rev, \@parents, $ed);
2726}
2727
Eric Wong97f69872007-01-25 11:53:13 -08002728sub get_untracked {
2729 my ($self, $ed) = @_;
2730 my @out;
2731 my $h = $ed->{empty};
Eric Wong9b981fc2007-01-11 12:14:21 -08002732 foreach (sort keys %$h) {
2733 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
Eric Wong97f69872007-01-25 11:53:13 -08002734 push @out, " $act: " . uri_encode($_);
Eric Wong9b981fc2007-01-11 12:14:21 -08002735 warn "W: $act: $_\n";
2736 }
2737 foreach my $t (qw/dir_prop file_prop/) {
Eric Wong97f69872007-01-25 11:53:13 -08002738 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002739 foreach my $path (sort keys %$h) {
2740 my $ppath = $path eq '' ? '.' : $path;
2741 foreach my $prop (sort keys %{$h->{$path}}) {
Eric Wong1ce255d2007-01-14 23:21:16 -08002742 next if $SKIP_PROP{$prop};
Eric Wong9b981fc2007-01-11 12:14:21 -08002743 my $v = $h->{$path}->{$prop};
Eric Wong97f69872007-01-25 11:53:13 -08002744 my $t_ppath_prop = "$t: " .
2745 uri_encode($ppath) . ' ' .
2746 uri_encode($prop);
Eric Wong9b981fc2007-01-11 12:14:21 -08002747 if (defined $v) {
Eric Wong97f69872007-01-25 11:53:13 -08002748 push @out, " +$t_ppath_prop " .
2749 uri_encode($v);
Eric Wong9b981fc2007-01-11 12:14:21 -08002750 } else {
Eric Wong97f69872007-01-25 11:53:13 -08002751 push @out, " -$t_ppath_prop";
Eric Wong9b981fc2007-01-11 12:14:21 -08002752 }
2753 }
2754 }
2755 }
2756 foreach my $t (qw/absent_file absent_directory/) {
Eric Wong97f69872007-01-25 11:53:13 -08002757 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002758 foreach my $parent (sort keys %$h) {
2759 foreach my $path (sort @{$h->{$parent}}) {
Eric Wong97f69872007-01-25 11:53:13 -08002760 push @out, " $t: " .
2761 uri_encode("$parent/$path");
Eric Wong9b981fc2007-01-11 12:14:21 -08002762 warn "W: $t: $parent/$path ",
2763 "Insufficient permissions?\n";
2764 }
2765 }
2766 }
Eric Wong97f69872007-01-25 11:53:13 -08002767 \@out;
Eric Wong9b981fc2007-01-11 12:14:21 -08002768}
2769
Pete Harlane82f0d72009-01-17 20:10:14 -08002770# parse_svn_date(DATE)
2771# --------------------
2772# Given a date (in UTC) from Subversion, return a string in the format
2773# "<TZ Offset> <local date/time>" that Git will use.
2774#
2775# By default the parsed date will be in UTC; if $Git::SVN::_localtime
2776# is true we'll convert it to the local timezone instead.
Eric Wong1c8443b2007-01-14 02:17:00 -08002777sub parse_svn_date {
2778 my $date = shift || return '+0000 1970-01-01 00:00:00';
2779 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
Junio C Hamanob94ead72009-02-18 10:48:01 -08002780 (\d\d)\:(\d\d)\:(\d\d)\.\d*Z$/x) or
Eric Wong1c8443b2007-01-14 02:17:00 -08002781 croak "Unable to parse date: $date\n";
Pete Harlane82f0d72009-01-17 20:10:14 -08002782 my $parsed_date; # Set next.
2783
2784 if ($Git::SVN::_localtime) {
2785 # Translate the Subversion datetime to an epoch time.
2786 # Begin by switching ourselves to $date's timezone, UTC.
2787 my $old_env_TZ = $ENV{TZ};
2788 $ENV{TZ} = 'UTC';
2789
2790 my $epoch_in_UTC =
2791 POSIX::strftime('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
2792
2793 # Determine our local timezone (including DST) at the
2794 # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the
2795 # value of TZ, if any, at the time we were run.
2796 if (defined $Git::SVN::Log::TZ) {
2797 $ENV{TZ} = $Git::SVN::Log::TZ;
2798 } else {
2799 delete $ENV{TZ};
2800 }
2801
2802 my $our_TZ =
2803 POSIX::strftime('%Z', $S, $M, $H, $d, $m - 1, $Y - 1900);
2804
2805 # This converts $epoch_in_UTC into our local timezone.
2806 my ($sec, $min, $hour, $mday, $mon, $year,
2807 $wday, $yday, $isdst) = localtime($epoch_in_UTC);
2808
2809 $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
2810 $our_TZ, $year + 1900, $mon + 1,
2811 $mday, $hour, $min, $sec);
2812
2813 # Reset us to the timezone in effect when we entered
2814 # this routine.
2815 if (defined $old_env_TZ) {
2816 $ENV{TZ} = $old_env_TZ;
2817 } else {
2818 delete $ENV{TZ};
2819 }
2820 } else {
2821 $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
2822 }
2823
2824 return $parsed_date;
Eric Wong1c8443b2007-01-14 02:17:00 -08002825}
2826
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002827sub other_gs {
Mattias Nissler0b2af452009-07-07 01:40:02 +02002828 my ($self, $new_url, $url,
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002829 $branch_from, $r, $old_ref_id) = @_;
Mattias Nissler0b2af452009-07-07 01:40:02 +02002830 my $gs = Git::SVN->find_by_url($new_url, $url, $branch_from);
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002831 unless ($gs) {
2832 my $ref_id = $old_ref_id;
2833 $ref_id =~ s/\@\d+$//;
2834 $ref_id .= "\@$r";
2835 # just grow a tail if we're not unique enough :x
2836 $ref_id .= '-' while find_ref($ref_id);
Simon Arlott85886162009-10-09 13:21:13 +01002837 print STDERR "Initializing parent: $ref_id\n" unless $::_q > 1;
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002838 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
2839 if ($u =~ s#^\Q$url\E(/|$)##) {
2840 $p = $u;
2841 $u = $url;
2842 $repo_id = $self->{repo_id};
2843 }
2844 $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1);
2845 }
2846 $gs
2847}
2848
Mark Lodato36db1ed2009-05-14 21:27:15 -04002849sub call_authors_prog {
2850 my ($orig_author) = @_;
Mark Lodatod3d7d472009-09-12 20:33:23 -04002851 $orig_author = command_oneline('rev-parse', '--sq-quote', $orig_author);
Mark Lodato36db1ed2009-05-14 21:27:15 -04002852 my $author = `$::_authors_prog $orig_author`;
2853 if ($? != 0) {
2854 die "$::_authors_prog failed with exit code $?\n"
2855 }
2856 if ($author =~ /^\s*(.+?)\s*<(.*)>\s*$/) {
2857 my ($name, $email) = ($1, $2);
2858 $email = undef if length $2 == 0;
2859 return [$name, $email];
2860 } else {
2861 die "Author: $orig_author: $::_authors_prog returned "
2862 . "invalid author format: $author\n";
2863 }
2864}
2865
Eric Wong1c8443b2007-01-14 02:17:00 -08002866sub check_author {
2867 my ($author) = @_;
2868 if (!defined $author || length $author == 0) {
2869 $author = '(no author)';
Mark Lodato36db1ed2009-05-14 21:27:15 -04002870 }
2871 if (!defined $::users{$author}) {
2872 if (defined $::_authors_prog) {
2873 $::users{$author} = call_authors_prog($author);
2874 } elsif (defined $::_authors) {
2875 die "Author: $author not defined in $::_authors file\n";
2876 }
Eric Wong1c8443b2007-01-14 02:17:00 -08002877 }
2878 $author;
2879}
2880
Eric Wong9b981fc2007-01-11 12:14:21 -08002881sub make_log_entry {
Eric Wong97f69872007-01-25 11:53:13 -08002882 my ($self, $rev, $parents, $ed) = @_;
2883 my $untracked = $self->get_untracked($ed);
2884
Eric Wong9b981fc2007-01-11 12:14:21 -08002885 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08002886 print $un "r$rev\n" or croak $!;
2887 print $un $_, "\n" foreach @$untracked;
2888 my %log_entry = ( parents => $parents || [], revision => $rev,
2889 log => '');
Eric Wongfbcc1732007-02-06 18:35:30 -08002890
Eric Wong8a49ee92007-02-10 20:46:50 -08002891 my $headrev;
Eric Wongfbcc1732007-02-06 18:35:30 -08002892 my $logged = delete $self->{logged_rev_props};
Eric Wong8a49ee92007-02-10 20:46:50 -08002893 if (!$logged || $self->{-want_revprops}) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002894 my $rp = $self->ra->rev_proplist($rev);
2895 foreach (sort keys %$rp) {
2896 my $v = $rp->{$_};
2897 if (/^svn:(author|date|log)$/) {
2898 $log_entry{$1} = $v;
Eric Wong8a49ee92007-02-10 20:46:50 -08002899 } elsif ($_ eq 'svm:headrev') {
2900 $headrev = $v;
Eric Wongfbcc1732007-02-06 18:35:30 -08002901 } else {
2902 print $un " rev_prop: ", uri_encode($_), ' ',
2903 uri_encode($v), "\n";
2904 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002905 }
Eric Wongfbcc1732007-02-06 18:35:30 -08002906 } else {
2907 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
Eric Wong9b981fc2007-01-11 12:14:21 -08002908 }
2909 close $un or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08002910
Eric Wong9b981fc2007-01-11 12:14:21 -08002911 $log_entry{date} = parse_svn_date($log_entry{date});
Eric Wong9b981fc2007-01-11 12:14:21 -08002912 $log_entry{log} .= "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002913 my $author = $log_entry{author} = check_author($log_entry{author});
2914 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002915 : ($author, undef);
2916
2917 my ($commit_name, $commit_email) = ($name, $email);
2918 if ($_use_log_author) {
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002919 my $name_field;
2920 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
2921 $name_field = $1;
2922 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
2923 $name_field = $1;
2924 }
2925 if (!defined $name_field) {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02002926 if (!defined $email) {
2927 $email = $name;
2928 }
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002929 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002930 ($name, $email) = ($1, $2);
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002931 } elsif ($name_field =~ /(.*)@/) {
2932 ($name, $email) = ($1, $name_field);
2933 } else {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02002934 ($name, $email) = ($name_field, $name_field);
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002935 }
2936 }
Eric Wong91b03282007-02-11 00:51:33 -08002937 if (defined $headrev && $self->use_svm_props) {
Eric Wongaea736c2007-02-16 19:15:21 -08002938 if ($self->rewrite_root) {
2939 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
2940 "options set!\n";
2941 }
Eric Wongb3e95932009-07-11 14:13:12 -07002942 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$}i;
Eric Wongbefc9ad2007-02-17 02:53:07 -08002943 # we don't want "SVM: initializing mirror for junk" ...
2944 return undef if $r == 0;
2945 my $svm = $self->svm;
2946 if ($uuid ne $svm->{uuid}) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002947 die "UUID mismatch on SVM path:\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08002948 "expected: $svm->{uuid}\n",
Eric Wong8a49ee92007-02-10 20:46:50 -08002949 " got: $uuid\n";
2950 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002951 my $full_url = $self->full_url;
2952 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
2953 die "Failed to replace '$svm->{replace}' with ",
2954 "'$svm->{source}' in $full_url\n";
Sam Vilain18ea92b2007-02-23 12:32:29 +13002955 # throw away username for storing in records
2956 remove_username($full_url);
Eric Wong8a49ee92007-02-10 20:46:50 -08002957 $log_entry{metadata} = "$full_url\@$r $uuid";
2958 $log_entry{svm_revision} = $r;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002959 $email ||= "$author\@$uuid";
2960 $commit_email ||= "$author\@$uuid";
Eric Wong62e349d2007-02-16 19:57:29 -08002961 } elsif ($self->use_svnsync_props) {
2962 my $full_url = $self->svnsync->{url};
2963 $full_url .= "/$self->{path}" if length $self->{path};
Adam Robence118732007-04-24 18:02:07 -07002964 remove_username($full_url);
Eric Wong62e349d2007-02-16 19:57:29 -08002965 my $uuid = $self->svnsync->{uuid};
2966 $log_entry{metadata} = "$full_url\@$rev $uuid";
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002967 $email ||= "$author\@$uuid";
2968 $commit_email ||= "$author\@$uuid";
Eric Wong8a49ee92007-02-10 20:46:50 -08002969 } else {
Adam Robence118732007-04-24 18:02:07 -07002970 my $url = $self->metadata_url;
2971 remove_username($url);
2972 $log_entry{metadata} = "$url\@$rev " .
Eric Wong26a62d52007-02-12 13:25:25 -08002973 $self->ra->get_uuid;
Eric Wongdb03cd22007-02-13 00:38:02 -08002974 $email ||= "$author\@" . $self->ra->get_uuid;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002975 $commit_email ||= "$author\@" . $self->ra->get_uuid;
Eric Wong8a49ee92007-02-10 20:46:50 -08002976 }
Eric Wongdb03cd22007-02-13 00:38:02 -08002977 $log_entry{name} = $name;
2978 $log_entry{email} = $email;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002979 $log_entry{commit_name} = $commit_name;
2980 $log_entry{commit_email} = $commit_email;
Eric Wong9b981fc2007-01-11 12:14:21 -08002981 \%log_entry;
2982}
2983
2984sub fetch {
Eric Wong3ebe8df2007-01-25 17:35:40 -08002985 my ($self, $min_rev, $max_rev, @parents) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002986 my ($last_rev, $last_commit) = $self->last_rev_commit;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002987 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
Eric Wonge5181922007-02-08 12:53:57 -08002988 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
Eric Wong9b981fc2007-01-11 12:14:21 -08002989}
2990
2991sub set_tree_cb {
2992 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
Eric Wong490f49e2007-02-10 13:58:33 -08002993 $self->{inject_parents} = { $rev => $tree };
2994 $self->fetch(undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08002995}
2996
2997sub set_tree {
2998 my ($self, $tree) = (shift, shift);
Eric Wong1ce255d2007-01-14 23:21:16 -08002999 my $log_entry = ::get_commit_entry($tree);
Eric Wong9b981fc2007-01-11 12:14:21 -08003000 unless ($self->{last_rev}) {
Luc Heinrich0a1a1c82008-09-29 15:58:18 +02003001 ::fatal("Must have an existing revision to commit");
Eric Wong9b981fc2007-01-11 12:14:21 -08003002 }
Eric Wong61395352007-01-27 14:33:08 -08003003 my %ed_opts = ( r => $self->{last_rev},
3004 log => $log_entry->{log},
3005 ra => $self->ra,
3006 tree_a => $self->{last_commit},
3007 tree_b => $tree,
3008 editor_cb => sub {
3009 $self->set_tree_cb($log_entry, $tree, @_) },
3010 svn_path => $self->{path} );
3011 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong9b981fc2007-01-11 12:14:21 -08003012 print "No changes\nr$self->{last_rev} = $tree\n";
3013 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003014}
3015
Eric Wong060610c2007-12-08 23:27:41 -08003016sub rebuild_from_rev_db {
3017 my ($self, $path) = @_;
3018 my $r = -1;
3019 open my $fh, '<', $path or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02003020 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003021 while (<$fh>) {
3022 length($_) == 41 or croak "inconsistent size in ($_) != 41";
3023 chomp($_);
3024 ++$r;
3025 next if $_ eq ('0' x 40);
3026 $self->rev_map_set($r, $_);
3027 print "r$r = $_\n";
3028 }
3029 close $fh or croak "close: $!";
3030 unlink $path or croak "unlink: $!";
3031}
3032
Eric Wongf0ecca12007-01-30 13:11:14 -08003033sub rebuild {
3034 my ($self) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003035 my $map_path = $self->map_path;
Deskin Miller2beec892008-09-15 21:12:58 -04003036 my $partial = (-e $map_path && ! -z $map_path);
Eric Wongd6d33462007-02-16 04:05:33 -08003037 return unless ::verify_ref($self->refname.'^0');
Deskin Miller2beec892008-09-15 21:12:58 -04003038 if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
Eric Wong060610c2007-12-08 23:27:41 -08003039 my $rev_db = $self->rev_db_path;
3040 $self->rebuild_from_rev_db($rev_db);
3041 if ($self->use_svm_props) {
3042 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
3043 $self->rebuild_from_rev_db($svm_rev_db);
3044 }
3045 $self->unlink_rev_db_symlink;
Eric Wong26a62d52007-02-12 13:25:25 -08003046 return;
3047 }
Deskin Miller2beec892008-09-15 21:12:58 -04003048 print "Rebuilding $map_path ...\n" if (!$partial);
3049 my ($base_rev, $head) = ($partial ? $self->rev_map_max_norebuild(1) :
3050 (undef, undef));
Eric Wong060610c2007-12-08 23:27:41 -08003051 my ($log, $ctx) =
3052 command_output_pipe(qw/rev-list --pretty=raw --no-color --reverse/,
Deskin Miller2beec892008-09-15 21:12:58 -04003053 ($head ? "$head.." : "") . $self->refname,
3054 '--');
Jan Krüger74b1e122008-06-24 02:17:36 +02003055 my $metadata_url = $self->metadata_url;
3056 remove_username($metadata_url);
Eric Wong060610c2007-12-08 23:27:41 -08003057 my $svn_uuid = $self->ra_uuid;
Sam Vilain3dfab992007-06-30 20:56:13 +12003058 my $c;
3059 while (<$log>) {
3060 if ( m{^commit ($::sha1)$} ) {
3061 $c = $1;
3062 next;
3063 }
3064 next unless s{^\s*(git-svn-id:)}{$1};
3065 my ($url, $rev, $uuid) = ::extract_metadata($_);
Sam Vilain18ea92b2007-02-23 12:32:29 +13003066 remove_username($url);
Eric Wongf0ecca12007-01-30 13:11:14 -08003067
3068 # ignore merges (from set-tree)
3069 next if (!defined $rev || !$uuid);
3070
3071 # if we merged or otherwise started elsewhere, this is
3072 # how we break out of it
Eric Wong060610c2007-12-08 23:27:41 -08003073 if (($uuid ne $svn_uuid) ||
Jan Krüger74b1e122008-06-24 02:17:36 +02003074 ($metadata_url && $url && ($url ne $metadata_url))) {
Eric Wongf0ecca12007-01-30 13:11:14 -08003075 next;
3076 }
Deskin Miller2beec892008-09-15 21:12:58 -04003077 if ($partial && $head) {
3078 print "Partial-rebuilding $map_path ...\n";
3079 print "Currently at $base_rev = $head\n";
3080 $head = undef;
3081 }
Eric Wongf0ecca12007-01-30 13:11:14 -08003082
Eric Wong060610c2007-12-08 23:27:41 -08003083 $self->rev_map_set($rev, $c);
Eric Wongf0ecca12007-01-30 13:11:14 -08003084 print "r$rev = $c\n";
3085 }
Sam Vilain3dfab992007-06-30 20:56:13 +12003086 command_close_pipe($log, $ctx);
Deskin Miller2beec892008-09-15 21:12:58 -04003087 print "Done rebuilding $map_path\n" if (!$partial || !$head);
Eric Wong060610c2007-12-08 23:27:41 -08003088 my $rev_db_path = $self->rev_db_path;
3089 if (-f $self->rev_db_path) {
3090 unlink $self->rev_db_path or croak "unlink: $!";
3091 }
3092 $self->unlink_rev_db_symlink;
Eric Wongf0ecca12007-01-30 13:11:14 -08003093}
3094
Eric Wong060610c2007-12-08 23:27:41 -08003095# rev_map:
Eric Wong9b981fc2007-01-11 12:14:21 -08003096# Tie::File seems to be prone to offset errors if revisions get sparse,
3097# it's not that fast, either. Tie::File is also not in Perl 5.6. So
3098# one of my favorite modules is out :< Next up would be one of the DBM
Eric Wong060610c2007-12-08 23:27:41 -08003099# modules, but I'm not sure which is most portable...
3100#
3101# This is the replacement for the rev_db format, which was too big
3102# and inefficient for large repositories with a lot of sparse history
3103# (mainly tags)
3104#
3105# The format is this:
3106# - 24 bytes for every record,
3107# * 4 bytes for the integer representing an SVN revision number
3108# * 20 bytes representing the sha1 of a git commit
3109# - No empty padding records like the old format
Eric Wong66ab84b2007-12-08 23:27:42 -08003110# (except the last record, which can be overwritten)
Eric Wong060610c2007-12-08 23:27:41 -08003111# - new records are written append-only since SVN revision numbers
3112# increase monotonically
3113# - lookups on SVN revision number are done via a binary search
Eric Wong66ab84b2007-12-08 23:27:42 -08003114# - Piping the file to xxd -c24 is a good way of dumping it for
3115# viewing or editing (piped back through xxd -r), should the need
3116# ever arise.
3117# - The last record can be padding revision with an all-zero sha1
3118# This is used to optimize fetch performance when using multiple
3119# "fetch" directives in .git/config
Eric Wong060610c2007-12-08 23:27:41 -08003120#
Eric Wong97ae0912007-02-11 15:21:24 -08003121# These files are disposable unless noMetadata or useSvmProps is set
Eric Wong9b981fc2007-01-11 12:14:21 -08003122
Eric Wong060610c2007-12-08 23:27:41 -08003123sub _rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08003124 my ($fh, $rev, $commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003125
Michael Weber4f7ec792008-04-18 15:12:04 +02003126 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003127 my $size = (stat($fh))[7];
3128 ($size % 24) == 0 or croak "inconsistent size: $size";
3129
Eric Wong66ab84b2007-12-08 23:27:42 -08003130 my $wr_offset = 0;
Eric Wong060610c2007-12-08 23:27:41 -08003131 if ($size > 0) {
3132 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
3133 my $read = sysread($fh, my $buf, 24) or croak "read: $!";
3134 $read == 24 or croak "read only $read bytes (!= 24)";
3135 my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08003136 if ($last_commit eq ('0' x40)) {
3137 if ($size >= 48) {
3138 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
3139 $read = sysread($fh, $buf, 24) or
3140 croak "read: $!";
3141 $read == 24 or
3142 croak "read only $read bytes (!= 24)";
3143 ($last_rev, $last_commit) =
3144 unpack(rev_map_fmt, $buf);
3145 if ($last_commit eq ('0' x40)) {
3146 croak "inconsistent .rev_map\n";
3147 }
3148 }
3149 if ($last_rev >= $rev) {
3150 croak "last_rev is higher!: $last_rev >= $rev";
3151 }
3152 $wr_offset = -24;
Eric Wong47a0b752007-01-31 05:13:30 -08003153 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003154 }
Eric Wong66ab84b2007-12-08 23:27:42 -08003155 sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003156 syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or
3157 croak "write: $!";
Eric Wong26a62d52007-02-12 13:25:25 -08003158}
3159
Ben Jackson195643f2009-06-03 20:45:52 -07003160sub _rev_map_reset {
3161 my ($fh, $rev, $commit) = @_;
3162 my $c = _rev_map_get($fh, $rev);
3163 $c eq $commit or die "_rev_map_reset(@_) commit $c does not match!\n";
3164 my $offset = sysseek($fh, 0, SEEK_CUR) or croak "seek: $!";
3165 truncate $fh, $offset or croak "truncate: $!";
3166}
3167
Eric Wong26a62d52007-02-12 13:25:25 -08003168sub mkfile {
3169 my ($path) = @_;
3170 unless (-e $path) {
3171 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
3172 mkpath([$dir]) unless -d $dir;
3173 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
3174 close $fh or die "Couldn't close (create) $path: $!\n";
3175 }
3176}
3177
Eric Wong060610c2007-12-08 23:27:41 -08003178sub rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08003179 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
3180 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
Eric Wong060610c2007-12-08 23:27:41 -08003181 my $db = $self->map_path($uuid);
Eric Wong26a62d52007-02-12 13:25:25 -08003182 my $db_lock = "$db.lock";
3183 my $sig;
Ben Jackson195643f2009-06-03 20:45:52 -07003184 $update_ref ||= 0;
Eric Wong26a62d52007-02-12 13:25:25 -08003185 if ($update_ref) {
3186 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
3187 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
3188 }
3189 mkfile($db);
3190
3191 $LOCKFILES{$db_lock} = 1;
3192 my $sync;
3193 # both of these options make our .rev_db file very, very important
3194 # and we can't afford to lose it because rebuild() won't work
3195 if ($self->use_svm_props || $self->no_metadata) {
3196 $sync = 1;
Eric Wong060610c2007-12-08 23:27:41 -08003197 copy($db, $db_lock) or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08003198 "Failed to copy: ",
3199 "$db => $db_lock ($!)\n";
3200 } else {
Eric Wong060610c2007-12-08 23:27:41 -08003201 rename $db, $db_lock or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08003202 "Failed to rename: ",
3203 "$db => $db_lock ($!)\n";
3204 }
Eric Wong060610c2007-12-08 23:27:41 -08003205
Eric Wong66ab84b2007-12-08 23:27:42 -08003206 sysopen(my $fh, $db_lock, O_RDWR | O_CREAT)
Eric Wong060610c2007-12-08 23:27:41 -08003207 or croak "Couldn't open $db_lock: $!\n";
Ben Jackson195643f2009-06-03 20:45:52 -07003208 $update_ref eq 'reset' ? _rev_map_reset($fh, $rev, $commit) :
3209 _rev_map_set($fh, $rev, $commit);
Eric Wong97ae0912007-02-11 15:21:24 -08003210 if ($sync) {
3211 $fh->flush or die "Couldn't flush $db_lock: $!\n";
3212 $fh->sync or die "Couldn't sync $db_lock: $!\n";
3213 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003214 close $fh or croak $!;
Eric Wong373274f2007-01-31 13:54:23 -08003215 if ($update_ref) {
Eric Wong1e889ef2007-02-16 01:45:13 -08003216 $_head = $self;
Ben Jackson195643f2009-06-03 20:45:52 -07003217 my $note = "";
3218 $note = " ($update_ref)" if ($update_ref !~ /^\d*$/);
3219 command_noisy('update-ref', '-m', "r$rev$note",
Eric Wong373274f2007-01-31 13:54:23 -08003220 $self->refname, $commit);
3221 }
Eric Wong060610c2007-12-08 23:27:41 -08003222 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
Eric Wong373274f2007-01-31 13:54:23 -08003223 "$db_lock => $db ($!)\n";
3224 delete $LOCKFILES{$db_lock};
3225 if ($update_ref) {
3226 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
3227 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
3228 kill $sig, $$ if defined $sig;
3229 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003230}
3231
Eric Wong66ab84b2007-12-08 23:27:42 -08003232# If want_commit, this will return an array of (rev, commit) where
3233# commit _must_ be a valid commit in the archive.
3234# Otherwise, it'll return the max revision (whether or not the
3235# commit is valid or just a 0x40 placeholder).
Eric Wong060610c2007-12-08 23:27:41 -08003236sub rev_map_max {
Eric Wong66ab84b2007-12-08 23:27:42 -08003237 my ($self, $want_commit) = @_;
Eric Wongd6d33462007-02-16 04:05:33 -08003238 $self->rebuild;
Deskin Miller2beec892008-09-15 21:12:58 -04003239 my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
3240 $want_commit ? ($r, $c) : $r;
3241}
3242
3243sub rev_map_max_norebuild {
3244 my ($self, $want_commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003245 my $map_path = $self->map_path;
Eric Wong66ab84b2007-12-08 23:27:42 -08003246 stat $map_path or return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08003247 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02003248 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003249 my $size = (stat($fh))[7];
3250 ($size % 24) == 0 or croak "inconsistent size: $size";
3251
3252 if ($size == 0) {
3253 close $fh or croak "close: $!";
Eric Wong66ab84b2007-12-08 23:27:42 -08003254 return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08003255 }
3256
Eric Wong66ab84b2007-12-08 23:27:42 -08003257 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003258 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003259 my ($r, $c) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08003260 if ($want_commit && $c eq ('0' x40)) {
3261 if ($size < 48) {
3262 return $want_commit ? (0, undef) : 0;
3263 }
3264 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
3265 sysread($fh, $buf, 24) == 24 or croak "read: $!";
3266 ($r, $c) = unpack(rev_map_fmt, $buf);
3267 if ($c eq ('0'x40)) {
3268 croak "Penultimate record is all-zeroes in $map_path";
3269 }
3270 }
3271 close $fh or croak "close: $!";
3272 $want_commit ? ($r, $c) : $r;
Eric Wong9c93fee2007-01-31 17:22:31 -08003273}
3274
Eric Wong060610c2007-12-08 23:27:41 -08003275sub rev_map_get {
Eric Wong26a62d52007-02-12 13:25:25 -08003276 my ($self, $rev, $uuid) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003277 my $map_path = $self->map_path($uuid);
3278 return undef unless -e $map_path;
3279
3280 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Ben Jackson195643f2009-06-03 20:45:52 -07003281 my $c = _rev_map_get($fh, $rev);
3282 close($fh) or croak "close: $!";
3283 $c
3284}
3285
3286sub _rev_map_get {
3287 my ($fh, $rev) = @_;
3288
Michael Weber4f7ec792008-04-18 15:12:04 +02003289 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003290 my $size = (stat($fh))[7];
3291 ($size % 24) == 0 or croak "inconsistent size: $size";
3292
3293 if ($size == 0) {
Eric Wong060610c2007-12-08 23:27:41 -08003294 return undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003295 }
Eric Wong060610c2007-12-08 23:27:41 -08003296
3297 my ($l, $u) = (0, $size - 24);
3298 my ($r, $c, $buf);
3299
3300 while ($l <= $u) {
3301 my $i = int(($l/24 + $u/24) / 2) * 24;
3302 sysseek($fh, $i, SEEK_SET) or croak "seek: $!";
3303 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
Eric Wongc83f4e62009-08-12 22:20:02 -07003304 my ($r, $c) = unpack(rev_map_fmt, $buf);
Eric Wong060610c2007-12-08 23:27:41 -08003305
3306 if ($r < $rev) {
3307 $l = $i + 24;
3308 } elsif ($r > $rev) {
3309 $u = $i - 24;
3310 } else { # $r == $rev
Eric Wong66ab84b2007-12-08 23:27:42 -08003311 return $c eq ('0' x 40) ? undef : $c;
Eric Wong060610c2007-12-08 23:27:41 -08003312 }
3313 }
Eric Wong060610c2007-12-08 23:27:41 -08003314 undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003315}
3316
David D Kilzer111947e2007-11-11 22:56:52 -08003317# Finds the first svn revision that exists on (if $eq_ok is true) or
3318# before $rev for the current branch. It will not search any lower
3319# than $min_rev. Returns the git commit hash and svn revision number
3320# if found, else (undef, undef).
Eric Wong15710b62007-01-22 02:20:33 -08003321sub find_rev_before {
David D Kilzer111947e2007-11-11 22:56:52 -08003322 my ($self, $rev, $eq_ok, $min_rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08003323 --$rev unless $eq_ok;
David D Kilzer111947e2007-11-11 22:56:52 -08003324 $min_rev ||= 1;
Ben Jacksonca5e8802009-06-03 20:45:51 -07003325 my $max_rev = $self->rev_map_max;
3326 $rev = $max_rev if ($rev > $max_rev);
David D Kilzer111947e2007-11-11 22:56:52 -08003327 while ($rev >= $min_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003328 if (my $c = $self->rev_map_get($rev)) {
Eric Wong15710b62007-01-22 02:20:33 -08003329 return ($rev, $c);
3330 }
3331 --$rev;
3332 }
3333 return (undef, undef);
3334}
3335
David D Kilzer111947e2007-11-11 22:56:52 -08003336# Finds the first svn revision that exists on (if $eq_ok is true) or
3337# after $rev for the current branch. It will not search any higher
3338# than $max_rev. Returns the git commit hash and svn revision number
3339# if found, else (undef, undef).
3340sub find_rev_after {
3341 my ($self, $rev, $eq_ok, $max_rev) = @_;
3342 ++$rev unless $eq_ok;
Eric Wong060610c2007-12-08 23:27:41 -08003343 $max_rev ||= $self->rev_map_max;
David D Kilzer111947e2007-11-11 22:56:52 -08003344 while ($rev <= $max_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003345 if (my $c = $self->rev_map_get($rev)) {
David D Kilzer111947e2007-11-11 22:56:52 -08003346 return ($rev, $c);
3347 }
3348 ++$rev;
3349 }
3350 return (undef, undef);
3351}
3352
Eric Wong9b981fc2007-01-11 12:14:21 -08003353sub _new {
Eric Wong706587f2007-01-18 17:50:01 -08003354 my ($class, $repo_id, $ref_id, $path) = @_;
3355 unless (defined $repo_id && length $repo_id) {
3356 $repo_id = $Git::SVN::default_repo_id;
3357 }
3358 unless (defined $ref_id && length $ref_id) {
Adam Brewster63de84a2009-08-03 21:40:38 -04003359 $_prefix = '' unless defined($_prefix);
Adam Brewster6f5748e2009-08-11 23:14:27 -04003360 $_[2] = $ref_id =
3361 "refs/remotes/$_prefix$Git::SVN::default_ref_id";
Eric Wong706587f2007-01-18 17:50:01 -08003362 }
Eric Wong7829f202008-06-28 20:40:32 -07003363 $_[1] = $repo_id;
Eric Wong706587f2007-01-18 17:50:01 -08003364 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
Adam Brewster6f5748e2009-08-11 23:14:27 -04003365
3366 # Older repos imported by us used $GIT_DIR/svn/foo instead of
3367 # $GIT_DIR/svn/refs/remotes/foo when tracking refs/remotes/foo
3368 if ($ref_id =~ m{^refs/remotes/(.*)}) {
3369 my $old_dir = "$ENV{GIT_DIR}/svn/$1";
3370 if (-d $old_dir && ! -d $dir) {
3371 $dir = $old_dir;
3372 }
3373 }
3374
Eric Wong706587f2007-01-18 17:50:01 -08003375 $_[3] = $path = '' unless (defined $path);
Adam Brewster6f5748e2009-08-11 23:14:27 -04003376 mkpath([$dir]);
Eric Wong26a62d52007-02-12 13:25:25 -08003377 bless {
3378 ref_id => $ref_id, dir => $dir, index => "$dir/index",
Eric Wong8a49ee92007-02-10 20:46:50 -08003379 path => $path, config => "$ENV{GIT_DIR}/svn/config",
Eric Wong060610c2007-12-08 23:27:41 -08003380 map_root => "$dir/.rev_map", repo_id => $repo_id }, $class;
Eric Wong26a62d52007-02-12 13:25:25 -08003381}
3382
Eric Wong060610c2007-12-08 23:27:41 -08003383# for read-only access of old .rev_db formats
3384sub unlink_rev_db_symlink {
3385 my ($self) = @_;
3386 my $link = $self->rev_db_path;
3387 $link =~ s/\.[\w-]+$// or croak "missing UUID at the end of $link";
3388 if (-l $link) {
3389 unlink $link or croak "unlink: $link failed!";
3390 }
3391}
3392
3393sub rev_db_path {
3394 my ($self, $uuid) = @_;
3395 my $db_path = $self->map_path($uuid);
3396 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
3397 or croak "map_path: $db_path does not contain '/.rev_map.' !";
3398 $db_path;
3399}
3400
3401# the new replacement for .rev_db
3402sub map_path {
Eric Wong26a62d52007-02-12 13:25:25 -08003403 my ($self, $uuid) = @_;
3404 $uuid ||= $self->ra_uuid;
Eric Wong060610c2007-12-08 23:27:41 -08003405 "$self->{map_root}.$uuid";
Eric Wong9b981fc2007-01-11 12:14:21 -08003406}
3407
Eric Wong1c8443b2007-01-14 02:17:00 -08003408sub uri_encode {
3409 my ($f) = @_;
3410 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
3411 $f
3412}
Eric Wong9b981fc2007-01-11 12:14:21 -08003413
Sam Vilain18ea92b2007-02-23 12:32:29 +13003414sub remove_username {
3415 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
3416}
3417
Eric Wongd976acf2007-01-04 00:45:03 -08003418package Git::SVN::Prompt;
3419use strict;
3420use warnings;
3421require SVN::Core;
3422use vars qw/$_no_auth_cache $_username/;
3423
3424sub simple {
Eric Wong30d055a2006-11-24 01:38:04 -08003425 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
3426 $may_save = undef if $_no_auth_cache;
3427 $default_username = $_username if defined $_username;
3428 if (defined $default_username && length $default_username) {
3429 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003430 print STDERR "Authentication realm: $realm\n";
3431 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003432 }
3433 $cred->username($default_username);
3434 } else {
Eric Wongd976acf2007-01-04 00:45:03 -08003435 username($cred, $realm, $may_save, $pool);
Eric Wong30d055a2006-11-24 01:38:04 -08003436 }
3437 $cred->password(_read_password("Password for '" .
3438 $cred->username . "': ", $realm));
3439 $cred->may_save($may_save);
3440 $SVN::_Core::SVN_NO_ERROR;
3441}
3442
Eric Wongd976acf2007-01-04 00:45:03 -08003443sub ssl_server_trust {
Eric Wong30d055a2006-11-24 01:38:04 -08003444 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
3445 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003446 print STDERR "Error validating server certificate for '$realm':\n";
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003447 {
3448 no warnings 'once';
3449 # All variables SVN::Auth::SSL::* are used only once,
3450 # so we're shutting up Perl warnings about this.
3451 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
3452 print STDERR " - The certificate is not issued ",
3453 "by a trusted authority. Use the\n",
3454 " fingerprint to validate ",
3455 "the certificate manually!\n";
3456 }
3457 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
3458 print STDERR " - The certificate hostname ",
3459 "does not match.\n";
3460 }
3461 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
3462 print STDERR " - The certificate is not yet valid.\n";
3463 }
3464 if ($failures & $SVN::Auth::SSL::EXPIRED) {
3465 print STDERR " - The certificate has expired.\n";
3466 }
3467 if ($failures & $SVN::Auth::SSL::OTHER) {
3468 print STDERR " - The certificate has ",
3469 "an unknown error.\n";
3470 }
3471 } # no warnings 'once'
Eric Wong6f729592007-01-15 20:15:55 -08003472 printf STDERR
3473 "Certificate information:\n".
Eric Wong30d055a2006-11-24 01:38:04 -08003474 " - Hostname: %s\n".
3475 " - Valid: from %s until %s\n".
3476 " - Issuer: %s\n".
3477 " - Fingerprint: %s\n",
3478 map $cert_info->$_, qw(hostname valid_from valid_until
Eric Wong6f729592007-01-15 20:15:55 -08003479 issuer_dname fingerprint);
Eric Wong30d055a2006-11-24 01:38:04 -08003480 my $choice;
3481prompt:
Eric Wong6f729592007-01-15 20:15:55 -08003482 print STDERR $may_save ?
Eric Wong30d055a2006-11-24 01:38:04 -08003483 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
3484 "(R)eject or accept (t)emporarily? ";
Eric Wong6f729592007-01-15 20:15:55 -08003485 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003486 $choice = lc(substr(<STDIN> || 'R', 0, 1));
3487 if ($choice =~ /^t$/i) {
3488 $cred->may_save(undef);
3489 } elsif ($choice =~ /^r$/i) {
3490 return -1;
3491 } elsif ($may_save && $choice =~ /^p$/i) {
3492 $cred->may_save($may_save);
3493 } else {
3494 goto prompt;
3495 }
3496 $cred->accepted_failures($failures);
3497 $SVN::_Core::SVN_NO_ERROR;
3498}
3499
Eric Wongd976acf2007-01-04 00:45:03 -08003500sub ssl_client_cert {
Eric Wong30d055a2006-11-24 01:38:04 -08003501 my ($cred, $realm, $may_save, $pool) = @_;
3502 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003503 print STDERR "Client certificate filename: ";
3504 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003505 chomp(my $filename = <STDIN>);
3506 $cred->cert_file($filename);
3507 $cred->may_save($may_save);
3508 $SVN::_Core::SVN_NO_ERROR;
3509}
3510
Eric Wongd976acf2007-01-04 00:45:03 -08003511sub ssl_client_cert_pw {
Eric Wong30d055a2006-11-24 01:38:04 -08003512 my ($cred, $realm, $may_save, $pool) = @_;
3513 $may_save = undef if $_no_auth_cache;
3514 $cred->password(_read_password("Password: ", $realm));
3515 $cred->may_save($may_save);
3516 $SVN::_Core::SVN_NO_ERROR;
3517}
3518
Eric Wongd976acf2007-01-04 00:45:03 -08003519sub username {
Eric Wong30d055a2006-11-24 01:38:04 -08003520 my ($cred, $realm, $may_save, $pool) = @_;
3521 $may_save = undef if $_no_auth_cache;
3522 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003523 print STDERR "Authentication realm: $realm\n";
Eric Wong30d055a2006-11-24 01:38:04 -08003524 }
3525 my $username;
3526 if (defined $_username) {
3527 $username = $_username;
3528 } else {
Eric Wong6f729592007-01-15 20:15:55 -08003529 print STDERR "Username: ";
3530 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003531 chomp($username = <STDIN>);
3532 }
3533 $cred->username($username);
3534 $cred->may_save($may_save);
3535 $SVN::_Core::SVN_NO_ERROR;
3536}
3537
3538sub _read_password {
3539 my ($prompt, $realm) = @_;
Eric Wong6f729592007-01-15 20:15:55 -08003540 print STDERR $prompt;
3541 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003542 require Term::ReadKey;
3543 Term::ReadKey::ReadMode('noecho');
3544 my $password = '';
3545 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
3546 last if $key =~ /[\012\015]/; # \n\r
3547 $password .= $key;
3548 }
3549 Term::ReadKey::ReadMode('restore');
Eric Wong6f729592007-01-15 20:15:55 -08003550 print STDERR "\n";
3551 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003552 $password;
3553}
3554
Eric Wong27a1a802006-11-27 21:44:48 -08003555package SVN::Git::Fetcher;
3556use vars qw/@ISA/;
3557use strict;
3558use warnings;
3559use Carp qw/croak/;
Adam Robenffe256f2008-05-23 16:19:41 +02003560use File::Temp qw/tempfile/;
Eric Wong27a1a802006-11-27 21:44:48 -08003561use IO::File qw//;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003562use vars qw/$_ignore_regex/;
Eric Wong27a1a802006-11-27 21:44:48 -08003563
3564# file baton members: path, mode_a, mode_b, pool, fh, blob, base
3565sub new {
Eric Wong8841b372009-02-11 01:56:58 -08003566 my ($class, $git_svn, $switch_path) = @_;
Eric Wong27a1a802006-11-27 21:44:48 -08003567 my $self = SVN::Delta::Editor->new;
3568 bless $self, $class;
Eric Wongdbc6c742009-01-11 16:51:10 -08003569 if (exists $git_svn->{last_commit}) {
3570 $self->{c} = $git_svn->{last_commit};
Eric Wong8841b372009-02-11 01:56:58 -08003571 $self->{empty_symlinks} =
3572 _mark_empty_symlinks($git_svn, $switch_path);
Eric Wongdbc6c742009-01-11 16:51:10 -08003573 }
Ben Jackson0d8bee72009-04-11 10:46:17 -07003574 $self->{ignore_regex} = eval { command_oneline('config', '--get',
3575 "svn-remote.$git_svn->{repo_id}.ignore-paths") };
Eric Wongd2a9a872006-12-12 14:47:00 -08003576 $self->{empty} = {};
3577 $self->{dir_prop} = {};
3578 $self->{file_prop} = {};
3579 $self->{absent_dir} = {};
3580 $self->{absent_file} = {};
Eric Wongef3cfaa2007-01-24 03:30:57 -08003581 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
Eric Wong27a1a802006-11-27 21:44:48 -08003582 $self;
3583}
3584
Eric Wongdbc6c742009-01-11 16:51:10 -08003585# this uses the Ra object, so it must be called before do_{switch,update},
3586# not inside them (when the Git::SVN::Fetcher object is passed) to
3587# do_{switch,update}
3588sub _mark_empty_symlinks {
Eric Wong8841b372009-02-11 01:56:58 -08003589 my ($git_svn, $switch_path) = @_;
Eric Wong4c58a712009-01-31 17:31:12 -08003590 my $bool = Git::config_bool('svn.brokenSymlinkWorkaround');
Eric Wong48679e52009-02-27 19:40:16 -08003591 return {} if (!defined($bool)) || (defined($bool) && ! $bool);
Eric Wong4c58a712009-01-31 17:31:12 -08003592
Eric Wongdbc6c742009-01-11 16:51:10 -08003593 my %ret;
3594 my ($rev, $cmt) = $git_svn->last_rev_commit;
3595 return {} unless ($rev && $cmt);
3596
Eric Wong4c58a712009-01-31 17:31:12 -08003597 # allow the warning to be printed for each revision we fetch to
3598 # ensure the user sees it. The user can also disable the workaround
3599 # on the repository even while git svn is running and the next
3600 # revision fetched will skip this expensive function.
3601 my $printed_warning;
Eric Wongdbc6c742009-01-11 16:51:10 -08003602 chomp(my $empty_blob = `git hash-object -t blob --stdin < /dev/null`);
3603 my ($ls, $ctx) = command_output_pipe(qw/ls-tree -r -z/, $cmt);
3604 local $/ = "\0";
Eric Wong8841b372009-02-11 01:56:58 -08003605 my $pfx = defined($switch_path) ? $switch_path : $git_svn->{path};
Eric Wongdbc6c742009-01-11 16:51:10 -08003606 $pfx .= '/' if length($pfx);
3607 while (<$ls>) {
3608 chomp;
3609 s/\A100644 blob $empty_blob\t//o or next;
Eric Wong4c58a712009-01-31 17:31:12 -08003610 unless ($printed_warning) {
3611 print STDERR "Scanning for empty symlinks, ",
3612 "this may take a while if you have ",
3613 "many empty files\n",
3614 "You may disable this with `",
3615 "git config svn.brokenSymlinkWorkaround ",
3616 "false'.\n",
3617 "This may be done in a different ",
3618 "terminal without restarting ",
3619 "git svn\n";
3620 $printed_warning = 1;
3621 }
Eric Wongdbc6c742009-01-11 16:51:10 -08003622 my $path = $_;
3623 my (undef, $props) =
3624 $git_svn->ra->get_file($pfx.$path, $rev, undef);
3625 if ($props->{'svn:special'}) {
3626 $ret{$path} = 1;
3627 }
3628 }
3629 command_close_pipe($ls, $ctx);
3630 \%ret;
3631}
3632
Eric Wongb03a71a2009-01-11 18:23:38 -08003633# returns true if a given path is inside a ".git" directory
3634sub in_dot_git {
3635 $_[0] =~ m{(?:^|/)\.git(?:/|$)};
3636}
3637
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003638# return value: 0 -- don't ignore, 1 -- ignore
3639sub is_path_ignored {
Ben Jackson0d8bee72009-04-11 10:46:17 -07003640 my ($self, $path) = @_;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003641 return 1 if in_dot_git($path);
Ben Jackson0d8bee72009-04-11 10:46:17 -07003642 return 1 if defined($self->{ignore_regex}) &&
3643 $path =~ m!$self->{ignore_regex}!;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003644 return 0 unless defined($_ignore_regex);
3645 return 1 if $path =~ m!$_ignore_regex!o;
3646 return 0;
3647}
3648
Eric Wong8b8fc062007-01-22 11:44:57 -08003649sub set_path_strip {
3650 my ($self, $path) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003651 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
Eric Wong8b8fc062007-01-22 11:44:57 -08003652}
3653
Eric Wongd2a9a872006-12-12 14:47:00 -08003654sub open_root {
3655 { path => '' };
3656}
3657
3658sub open_directory {
3659 my ($self, $path, $pb, $rev) = @_;
3660 { path => $path };
3661}
3662
Eric Wong706587f2007-01-18 17:50:01 -08003663sub git_path {
3664 my ($self, $path) = @_;
Eric Wong2b27f6c2007-01-28 04:59:05 -08003665 if ($self->{path_strip}) {
3666 $path =~ s!$self->{path_strip}!! or
3667 die "Failed to strip path '$path' ($self->{path_strip})\n";
3668 }
Eric Wong706587f2007-01-18 17:50:01 -08003669 $path;
3670}
3671
Eric Wong27a1a802006-11-27 21:44:48 -08003672sub delete_entry {
3673 my ($self, $path, $rev, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003674 return undef if $self->is_path_ignored($path);
Eric Wong4a87db02007-01-04 01:38:18 -08003675
Eric Wong706587f2007-01-18 17:50:01 -08003676 my $gpath = $self->git_path($path);
Eric Wong8a603772007-01-31 02:45:50 -08003677 return undef if ($gpath eq '');
3678
Eric Wong4a87db02007-01-04 01:38:18 -08003679 # remove entire directories.
Eric Wong4f821012009-03-28 22:10:08 -07003680 my ($tree) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
3681 =~ /\A040000 tree ([a-f\d]{40})\t\Q$gpath\E\0/);
3682 if ($tree) {
Eric Wong4a87db02007-01-04 01:38:18 -08003683 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3684 -r --name-only -z/,
Eric Wong4f821012009-03-28 22:10:08 -07003685 $tree);
Eric Wong4a87db02007-01-04 01:38:18 -08003686 local $/ = "\0";
3687 while (<$ls>) {
Eric Wongef3cfaa2007-01-24 03:30:57 -08003688 chomp;
Eric Wong4f821012009-03-28 22:10:08 -07003689 my $rmpath = "$gpath/$_";
3690 $self->{gii}->remove($rmpath);
3691 print "\tD\t$rmpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003692 }
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003693 print "\tD\t$gpath/\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003694 command_close_pipe($ls, $ctx);
3695 $self->{empty}->{$path} = 0
3696 } else {
Eric Wongef3cfaa2007-01-24 03:30:57 -08003697 $self->{gii}->remove($gpath);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003698 print "\tD\t$gpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003699 }
Eric Wong27a1a802006-11-27 21:44:48 -08003700 undef;
3701}
3702
3703sub open_file {
3704 my ($self, $path, $pb, $rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08003705 my ($mode, $blob);
3706
Ben Jackson0d8bee72009-04-11 10:46:17 -07003707 goto out if $self->is_path_ignored($path);
Eric Wongb03a71a2009-01-11 18:23:38 -08003708
Eric Wong706587f2007-01-18 17:50:01 -08003709 my $gpath = $self->git_path($path);
Eric Wong4f821012009-03-28 22:10:08 -07003710 ($mode, $blob) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
3711 =~ /\A(\d{6}) blob ([a-f\d]{40})\t\Q$gpath\E\0/);
Eric Wong006ede52006-12-08 01:55:19 -08003712 unless (defined $mode && defined $blob) {
3713 die "$path was not found in commit $self->{c} (r$rev)\n";
3714 }
Eric Wongdbc6c742009-01-11 16:51:10 -08003715 if ($mode eq '100644' && $self->{empty_symlinks}->{$path}) {
3716 $mode = '120000';
3717 }
Eric Wongb03a71a2009-01-11 18:23:38 -08003718out:
Eric Wong27a1a802006-11-27 21:44:48 -08003719 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
Eric Wong0864e3b2006-11-28 02:50:17 -08003720 pool => SVN::Pool->new, action => 'M' };
Eric Wong27a1a802006-11-27 21:44:48 -08003721}
3722
3723sub add_file {
3724 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08003725 my $mode;
3726
Ben Jackson0d8bee72009-04-11 10:46:17 -07003727 if (!$self->is_path_ignored($path)) {
Eric Wongb03a71a2009-01-11 18:23:38 -08003728 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3729 delete $self->{empty}->{$dir};
3730 $mode = '100644';
3731 }
3732 { path => $path, mode_a => $mode, mode_b => $mode,
Eric Wong0864e3b2006-11-28 02:50:17 -08003733 pool => SVN::Pool->new, action => 'A' };
Eric Wong27a1a802006-11-27 21:44:48 -08003734}
3735
Eric Wongd2a9a872006-12-12 14:47:00 -08003736sub add_directory {
3737 my ($self, $path, $cp_path, $cp_rev) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003738 goto out if $self->is_path_ignored($path);
Eric Wong12a6d752007-12-14 08:39:09 -08003739 my $gpath = $self->git_path($path);
3740 if ($gpath eq '') {
3741 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3742 -r --name-only -z/,
3743 $self->{c});
3744 local $/ = "\0";
3745 while (<$ls>) {
3746 chomp;
3747 $self->{gii}->remove($_);
3748 print "\tD\t$_\n" unless $::_q;
3749 }
3750 command_close_pipe($ls, $ctx);
3751 $self->{empty}->{$path} = 0;
3752 }
Eric Wongd2a9a872006-12-12 14:47:00 -08003753 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3754 delete $self->{empty}->{$dir};
3755 $self->{empty}->{$path} = 1;
Eric Wongb03a71a2009-01-11 18:23:38 -08003756out:
Eric Wongd2a9a872006-12-12 14:47:00 -08003757 { path => $path };
3758}
3759
3760sub change_dir_prop {
3761 my ($self, $db, $prop, $value) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003762 return undef if $self->is_path_ignored($db->{path});
Eric Wongd2a9a872006-12-12 14:47:00 -08003763 $self->{dir_prop}->{$db->{path}} ||= {};
3764 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
3765 undef;
3766}
3767
3768sub absent_directory {
3769 my ($self, $path, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003770 return undef if $self->is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08003771 $self->{absent_dir}->{$pb->{path}} ||= [];
3772 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
3773 undef;
3774}
3775
3776sub absent_file {
3777 my ($self, $path, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003778 return undef if $self->is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08003779 $self->{absent_file}->{$pb->{path}} ||= [];
3780 push @{$self->{absent_file}->{$pb->{path}}}, $path;
3781 undef;
3782}
3783
Eric Wong27a1a802006-11-27 21:44:48 -08003784sub change_file_prop {
3785 my ($self, $fb, $prop, $value) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003786 return undef if $self->is_path_ignored($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08003787 if ($prop eq 'svn:executable') {
3788 if ($fb->{mode_b} != 120000) {
3789 $fb->{mode_b} = defined $value ? 100755 : 100644;
3790 }
3791 } elsif ($prop eq 'svn:special') {
3792 $fb->{mode_b} = defined $value ? 120000 : 100644;
Eric Wongd2a9a872006-12-12 14:47:00 -08003793 } else {
3794 $self->{file_prop}->{$fb->{path}} ||= {};
3795 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
Eric Wong27a1a802006-11-27 21:44:48 -08003796 }
3797 undef;
3798}
3799
3800sub apply_textdelta {
3801 my ($self, $fb, $exp) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003802 return undef if $self->is_path_ignored($fb->{path});
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003803 my $fh = $::_repository->temp_acquire('svn_delta');
Eric Wong27a1a802006-11-27 21:44:48 -08003804 # $fh gets auto-closed() by SVN::TxDelta::apply(),
3805 # (but $base does not,) so dup() it for reading in close_file
3806 open my $dup, '<&', $fh or croak $!;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003807 my $base = $::_repository->temp_acquire('git_blob');
Eric Wongb03a71a2009-01-11 18:23:38 -08003808
Eric Wong27a1a802006-11-27 21:44:48 -08003809 if ($fb->{blob}) {
Eric Wongbaf5fa82009-01-11 16:51:11 -08003810 my ($base_is_link, $size);
3811
Eric Wongdbc6c742009-01-11 16:51:10 -08003812 if ($fb->{mode_a} eq '120000' &&
3813 ! $self->{empty_symlinks}->{$fb->{path}}) {
3814 print $base 'link ' or die "print $!\n";
Eric Wongbaf5fa82009-01-11 16:51:11 -08003815 $base_is_link = 1;
Eric Wongdbc6c742009-01-11 16:51:10 -08003816 }
Eric Wongbaf5fa82009-01-11 16:51:11 -08003817 retry:
3818 $size = $::_repository->cat_blob($fb->{blob}, $base);
Junio C Hamanod683a0e2008-05-27 23:33:22 -07003819 die "Failed to read object $fb->{blob}" if ($size < 0);
Eric Wong27a1a802006-11-27 21:44:48 -08003820
3821 if (defined $exp) {
3822 seek $base, 0, 0 or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08003823 my $got = ::md5sum($base);
Eric Wongbaf5fa82009-01-11 16:51:11 -08003824 if ($got ne $exp) {
3825 my $err = "Checksum mismatch: ".
3826 "$fb->{path} $fb->{blob}\n" .
3827 "expected: $exp\n" .
3828 " got: $got\n";
3829 if ($base_is_link) {
3830 warn $err,
3831 "Retrying... (possibly ",
3832 "a bad symlink from SVN)\n";
3833 $::_repository->temp_reset($base);
3834 $base_is_link = 0;
3835 goto retry;
3836 }
3837 die $err;
3838 }
Eric Wong27a1a802006-11-27 21:44:48 -08003839 }
3840 }
3841 seek $base, 0, 0 or croak $!;
Marcus Griep0b191382008-08-12 12:00:53 -04003842 $fb->{fh} = $fh;
Eric Wong27a1a802006-11-27 21:44:48 -08003843 $fb->{base} = $base;
Marcus Griep0b191382008-08-12 12:00:53 -04003844 [ SVN::TxDelta::apply($base, $dup, undef, $fb->{path}, $fb->{pool}) ];
Eric Wong27a1a802006-11-27 21:44:48 -08003845}
3846
3847sub close_file {
3848 my ($self, $fb, $exp) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003849 return undef if $self->is_path_ignored($fb->{path});
Eric Wongb03a71a2009-01-11 18:23:38 -08003850
Eric Wong27a1a802006-11-27 21:44:48 -08003851 my $hash;
Eric Wong706587f2007-01-18 17:50:01 -08003852 my $path = $self->git_path($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08003853 if (my $fh = $fb->{fh}) {
Eric Wong7faf0682007-05-27 15:59:01 -07003854 if (defined $exp) {
3855 seek($fh, 0, 0) or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08003856 my $got = ::md5sum($fh);
Eric Wong7faf0682007-05-27 15:59:01 -07003857 if ($got ne $exp) {
3858 die "Checksum mismatch: $path\n",
3859 "expected: $exp\n got: $got\n";
3860 }
3861 }
Eric Wong27a1a802006-11-27 21:44:48 -08003862 if ($fb->{mode_b} == 120000) {
Marcus Griep510b0942008-08-12 12:45:39 -04003863 sysseek($fh, 0, 0) or croak $!;
Eric Wongdbc6c742009-01-11 16:51:10 -08003864 my $rd = sysread($fh, my $buf, 5);
Marcus Griep510b0942008-08-12 12:45:39 -04003865
Eric Wongdbc6c742009-01-11 16:51:10 -08003866 if (!defined $rd) {
3867 croak "sysread: $!\n";
3868 } elsif ($rd == 0) {
Marcus Griep510b0942008-08-12 12:45:39 -04003869 warn "$path has mode 120000",
Eric Wongdbc6c742009-01-11 16:51:10 -08003870 " but it points to nothing\n",
3871 "converting to an empty file with mode",
3872 " 100644\n";
3873 $fb->{mode_b} = '100644';
3874 } elsif ($buf ne 'link ') {
3875 warn "$path has mode 120000",
3876 " but is not a link\n";
Marcus Griep510b0942008-08-12 12:45:39 -04003877 } else {
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003878 my $tmp_fh = $::_repository->temp_acquire(
3879 'svn_hash');
Marcus Griep510b0942008-08-12 12:45:39 -04003880 my $res;
3881 while ($res = sysread($fh, my $str, 1024)) {
3882 my $out = syswrite($tmp_fh, $str, $res);
3883 defined($out) && $out == $res
3884 or croak("write ",
Marcus Griep836ff952008-09-08 12:53:01 -04003885 Git::temp_path($tmp_fh),
Marcus Griep510b0942008-08-12 12:45:39 -04003886 ": $!\n");
3887 }
3888 defined $res or croak $!;
3889
3890 ($fh, $tmp_fh) = ($tmp_fh, $fh);
3891 Git::temp_release($tmp_fh, 1);
Eric Wong7fc35e02007-12-19 00:06:45 -08003892 }
Eric Wong27a1a802006-11-27 21:44:48 -08003893 }
Adam Robenffe256f2008-05-23 16:19:41 +02003894
Marcus Griep0b191382008-08-12 12:00:53 -04003895 $hash = $::_repository->hash_and_insert_object(
Marcus Griep836ff952008-09-08 12:53:01 -04003896 Git::temp_path($fh));
Eric Wong27a1a802006-11-27 21:44:48 -08003897 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
Marcus Griep0b191382008-08-12 12:00:53 -04003898
3899 Git::temp_release($fb->{base}, 1);
Marcus Griep510b0942008-08-12 12:45:39 -04003900 Git::temp_release($fh, 1);
Eric Wong27a1a802006-11-27 21:44:48 -08003901 } else {
3902 $hash = $fb->{blob} or die "no blob information\n";
3903 }
3904 $fb->{pool}->clear;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003905 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003906 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
Eric Wong27a1a802006-11-27 21:44:48 -08003907 undef;
3908}
3909
3910sub abort_edit {
3911 my $self = shift;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003912 $self->{nr} = $self->{gii}->{nr};
3913 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08003914 $self->SUPER::abort_edit(@_);
3915}
3916
3917sub close_edit {
3918 my $self = shift;
Eric Wongdad73c02006-11-28 14:06:05 -08003919 $self->{git_commit_ok} = 1;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003920 $self->{nr} = $self->{gii}->{nr};
3921 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08003922 $self->SUPER::close_edit(@_);
3923}
Eric Wong1a82e792006-06-16 02:55:13 -07003924
Eric Wonga5e0ced2006-06-12 15:23:48 -07003925package SVN::Git::Editor;
Eric Wong24e22aa2007-01-29 00:07:49 -08003926use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003927use strict;
3928use warnings;
3929use Carp qw/croak/;
3930use IO::File;
3931
3932sub new {
Eric Wong61395352007-01-27 14:33:08 -08003933 my ($class, $opts) = @_;
3934 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
3935 die "$_ required!\n" unless (defined $opts->{$_});
Eric Wonga5e0ced2006-06-12 15:23:48 -07003936 }
Eric Wong61395352007-01-27 14:33:08 -08003937
3938 my $pool = SVN::Pool->new;
3939 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
3940 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
3941 $opts->{r}, $mods);
3942
3943 # $opts->{ra} functions should not be used after this:
3944 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
3945 $opts->{editor_cb}, $pool);
3946 my $self = SVN::Delta::Editor->new(@ce, $pool);
3947 bless $self, $class;
3948 foreach (qw/svn_path r tree_a tree_b/) {
3949 $self->{$_} = $opts->{$_};
3950 }
3951 $self->{url} = $opts->{ra}->{url};
3952 $self->{mods} = $mods;
3953 $self->{types} = $types;
3954 $self->{pool} = $pool;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003955 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
3956 $self->{rm} = { };
Eric Wongd3a840d2007-01-26 01:32:45 -08003957 $self->{path_prefix} = length $self->{svn_path} ?
3958 "$self->{svn_path}/" : '';
Brad King128de652008-07-25 11:32:37 -04003959 $self->{config} = $opts->{config};
Eric Wonga5e0ced2006-06-12 15:23:48 -07003960 return $self;
3961}
3962
Eric Wong61395352007-01-27 14:33:08 -08003963sub generate_diff {
3964 my ($tree_a, $tree_b) = @_;
3965 my @diff_tree = qw(diff-tree -z -r);
Eric Wong24e22aa2007-01-29 00:07:49 -08003966 if ($_cp_similarity) {
3967 push @diff_tree, "-C$_cp_similarity";
Eric Wong61395352007-01-27 14:33:08 -08003968 } else {
3969 push @diff_tree, '-C';
3970 }
Eric Wong24e22aa2007-01-29 00:07:49 -08003971 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
3972 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
Eric Wong61395352007-01-27 14:33:08 -08003973 push @diff_tree, $tree_a, $tree_b;
3974 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
3975 local $/ = "\0";
3976 my $state = 'meta';
3977 my @mods;
3978 while (<$diff_fh>) {
3979 chomp $_; # this gets rid of the trailing "\0"
3980 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02003981 ($::sha1)\s($::sha1)\s
Eric Wong61395352007-01-27 14:33:08 -08003982 ([MTCRAD])\d*$/xo) {
3983 push @mods, { mode_a => $1, mode_b => $2,
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02003984 sha1_a => $3, sha1_b => $4,
3985 chg => $5 };
3986 if ($5 =~ /^(?:C|R)$/) {
Eric Wong61395352007-01-27 14:33:08 -08003987 $state = 'file_a';
3988 } else {
3989 $state = 'file_b';
3990 }
3991 } elsif ($state eq 'file_a') {
3992 my $x = $mods[$#mods] or croak "Empty array\n";
3993 if ($x->{chg} !~ /^(?:C|R)$/) {
3994 croak "Error parsing $_, $x->{chg}\n";
3995 }
3996 $x->{file_a} = $_;
3997 $state = 'file_b';
3998 } elsif ($state eq 'file_b') {
3999 my $x = $mods[$#mods] or croak "Empty array\n";
4000 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
4001 croak "Error parsing $_, $x->{chg}\n";
4002 }
4003 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
4004 croak "Error parsing $_, $x->{chg}\n";
4005 }
4006 $x->{file_b} = $_;
4007 $state = 'meta';
4008 } else {
4009 croak "Error parsing $_\n";
4010 }
4011 }
4012 command_close_pipe($diff_fh, $ctx);
4013 \@mods;
4014}
4015
4016sub check_diff_paths {
4017 my ($ra, $pfx, $rev, $mods) = @_;
4018 my %types;
4019 $pfx .= '/' if length $pfx;
4020
4021 sub type_diff_paths {
4022 my ($ra, $types, $path, $rev) = @_;
4023 my @p = split m#/+#, $path;
4024 my $c = shift @p;
4025 unless (defined $types->{$c}) {
4026 $types->{$c} = $ra->check_path($c, $rev);
4027 }
4028 while (@p) {
4029 $c .= '/' . shift @p;
4030 next if defined $types->{$c};
4031 $types->{$c} = $ra->check_path($c, $rev);
4032 }
4033 }
4034
4035 foreach my $m (@$mods) {
4036 foreach my $f (qw/file_a file_b/) {
4037 next unless defined $m->{$f};
4038 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
4039 if (length $pfx.$dir && ! defined $types{$dir}) {
4040 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
4041 }
4042 }
4043 }
4044 \%types;
4045}
4046
Eric Wonga5e0ced2006-06-12 15:23:48 -07004047sub split_path {
4048 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
4049}
4050
4051sub repo_path {
Eric Wongd3a840d2007-01-26 01:32:45 -08004052 my ($self, $path) = @_;
4053 $self->{path_prefix}.(defined $path ? $path : '');
Eric Wonga5e0ced2006-06-12 15:23:48 -07004054}
4055
4056sub url_path {
4057 my ($self, $path) = @_;
Eric Wong29633bb2007-07-15 21:53:50 -07004058 if ($self->{url} =~ m#^https?://#) {
Eric Wong884cce52009-07-25 02:29:28 -07004059 $path =~ s!([^~a-zA-Z0-9_./-])!uc sprintf("%%%02x",ord($1))!eg;
Eric Wong29633bb2007-07-15 21:53:50 -07004060 }
Eric Wong6e8548c2007-01-27 01:32:00 -08004061 $self->{url} . '/' . $self->repo_path($path);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004062}
4063
4064sub rmdirs {
Eric Wong61395352007-01-27 14:33:08 -08004065 my ($self) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004066 my $rm = $self->{rm};
4067 delete $rm->{''}; # we never delete the url we're tracking
4068 return unless %$rm;
4069
4070 foreach (keys %$rm) {
4071 my @d = split m#/#, $_;
4072 my $c = shift @d;
4073 $rm->{$c} = 1;
4074 while (@d) {
4075 $c .= '/' . shift @d;
4076 $rm->{$c} = 1;
4077 }
4078 }
4079 delete $rm->{$self->{svn_path}};
4080 delete $rm->{''}; # we never delete the url we're tracking
4081 return unless %$rm;
4082
Eric Wong61395352007-01-27 14:33:08 -08004083 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
4084 $self->{tree_b});
Eric Wonga5e0ced2006-06-12 15:23:48 -07004085 local $/ = "\0";
4086 while (<$fh>) {
4087 chomp;
Eric Wong747fa122006-11-24 22:38:17 -08004088 my @dn = split m#/#, $_;
Eric Wongc07eee12006-06-19 17:59:35 -07004089 while (pop @dn) {
4090 delete $rm->{join '/', @dn};
4091 }
4092 unless (%$rm) {
Eric Wong22600a22007-02-01 13:12:26 -08004093 close $fh;
Eric Wongc07eee12006-06-19 17:59:35 -07004094 return;
4095 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07004096 }
Eric Wongaef4e922006-12-15 10:59:54 -08004097 command_close_pipe($fh, $ctx);
Eric Wongc07eee12006-06-19 17:59:35 -07004098
Eric Wonga5e0ced2006-06-12 15:23:48 -07004099 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
4100 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
4101 $self->close_directory($bat->{$d}, $p);
4102 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
Eric Wong44320b92007-01-13 22:35:53 -08004103 print "\tD+\t$d/\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004104 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
4105 delete $bat->{$d};
4106 }
4107}
4108
4109sub open_or_add_dir {
4110 my ($self, $full_path, $baton) = @_;
Eric Wong6e8548c2007-01-27 01:32:00 -08004111 my $t = $self->{types}->{$full_path};
4112 if (!defined $t) {
4113 die "$full_path not known in r$self->{r} or we have a bug!\n";
4114 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004115 {
4116 no warnings 'once';
4117 # SVN::Node::none and SVN::Node::file are used only once,
4118 # so we're shutting up Perl's warnings about them.
4119 if ($t == $SVN::Node::none) {
4120 return $self->add_directory($full_path, $baton,
4121 undef, -1, $self->{pool});
4122 } elsif ($t == $SVN::Node::dir) {
4123 return $self->open_directory($full_path, $baton,
4124 $self->{r}, $self->{pool});
4125 } # no warnings 'once'
4126 print STDERR "$full_path already exists in repository at ",
4127 "r$self->{r} and it is not a directory (",
4128 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
4129 } # no warnings 'once'
Eric Wonga5e0ced2006-06-12 15:23:48 -07004130 exit 1;
4131}
4132
4133sub ensure_path {
4134 my ($self, $path) = @_;
4135 my $bat = $self->{bat};
Eric Wong6e8548c2007-01-27 01:32:00 -08004136 my $repo_path = $self->repo_path($path);
4137 return $bat->{''} unless (length $repo_path);
4138 my @p = split m#/+#, $repo_path;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004139 my $c = shift @p;
4140 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
4141 while (@p) {
4142 my $c0 = $c;
4143 $c .= '/' . shift @p;
4144 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
4145 }
4146 return $bat->{$c};
4147}
4148
Brad King128de652008-07-25 11:32:37 -04004149# Subroutine to convert a globbing pattern to a regular expression.
4150# From perl cookbook.
4151sub glob2pat {
4152 my $globstr = shift;
4153 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
4154 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
4155 return '^' . $globstr . '$';
4156}
4157
4158sub check_autoprop {
4159 my ($self, $pattern, $properties, $file, $fbat) = @_;
4160 # Convert the globbing pattern to a regular expression.
4161 my $regex = glob2pat($pattern);
4162 # Check if the pattern matches the file name.
4163 if($file =~ m/($regex)/) {
4164 # Parse the list of properties to set.
4165 my @props = split(/;/, $properties);
4166 foreach my $prop (@props) {
4167 # Parse 'name=value' syntax and set the property.
4168 if ($prop =~ /([^=]+)=(.*)/) {
4169 my ($n,$v) = ($1,$2);
4170 for ($n, $v) {
4171 s/^\s+//; s/\s+$//;
4172 }
4173 $self->change_file_prop($fbat, $n, $v);
4174 }
4175 }
4176 }
4177}
4178
4179sub apply_autoprops {
4180 my ($self, $file, $fbat) = @_;
4181 my $conf_t = ${$self->{config}}{'config'};
4182 no warnings 'once';
4183 # Check [miscellany]/enable-auto-props in svn configuration.
4184 if (SVN::_Core::svn_config_get_bool(
4185 $conf_t,
4186 $SVN::_Core::SVN_CONFIG_SECTION_MISCELLANY,
4187 $SVN::_Core::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS,
4188 0)) {
4189 # Auto-props are enabled. Enumerate them to look for matches.
4190 my $callback = sub {
4191 $self->check_autoprop($_[0], $_[1], $file, $fbat);
4192 };
4193 SVN::_Core::svn_config_enumerate(
4194 $conf_t,
4195 $SVN::_Core::SVN_CONFIG_SECTION_AUTO_PROPS,
4196 $callback);
4197 }
4198}
4199
Eric Wonga5e0ced2006-06-12 15:23:48 -07004200sub A {
Eric Wong44320b92007-01-13 22:35:53 -08004201 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004202 my ($dir, $file) = split_path($m->{file_b});
4203 my $pbat = $self->ensure_path($dir);
4204 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4205 undef, -1);
Eric Wong44320b92007-01-13 22:35:53 -08004206 print "\tA\t$m->{file_b}\n" unless $::_q;
Brad King128de652008-07-25 11:32:37 -04004207 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004208 $self->chg_file($fbat, $m);
4209 $self->close_file($fbat,undef,$self->{pool});
4210}
4211
4212sub C {
Eric Wong44320b92007-01-13 22:35:53 -08004213 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004214 my ($dir, $file) = split_path($m->{file_b});
4215 my $pbat = $self->ensure_path($dir);
4216 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4217 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08004218 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004219 $self->chg_file($fbat, $m);
4220 $self->close_file($fbat,undef,$self->{pool});
4221}
4222
4223sub delete_entry {
4224 my ($self, $path, $pbat) = @_;
4225 my $rpath = $self->repo_path($path);
4226 my ($dir, $file) = split_path($rpath);
4227 $self->{rm}->{$dir} = 1;
4228 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
4229}
4230
4231sub R {
Eric Wong44320b92007-01-13 22:35:53 -08004232 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004233 my ($dir, $file) = split_path($m->{file_b});
4234 my $pbat = $self->ensure_path($dir);
4235 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4236 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08004237 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Paul Talacko7c4d0212008-09-06 18:50:38 -07004238 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004239 $self->chg_file($fbat, $m);
4240 $self->close_file($fbat,undef,$self->{pool});
4241
4242 ($dir, $file) = split_path($m->{file_a});
4243 $pbat = $self->ensure_path($dir);
4244 $self->delete_entry($m->{file_a}, $pbat);
4245}
4246
4247sub M {
Eric Wong44320b92007-01-13 22:35:53 -08004248 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004249 my ($dir, $file) = split_path($m->{file_b});
4250 my $pbat = $self->ensure_path($dir);
4251 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
4252 $pbat,$self->{r},$self->{pool});
Eric Wong44320b92007-01-13 22:35:53 -08004253 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004254 $self->chg_file($fbat, $m);
4255 $self->close_file($fbat,undef,$self->{pool});
4256}
4257
4258sub T { shift->M(@_) }
4259
4260sub change_file_prop {
4261 my ($self, $fbat, $pname, $pval) = @_;
4262 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
4263}
4264
Florian Weimer214a34d2008-08-31 17:45:04 +02004265sub _chg_file_get_blob ($$$$) {
4266 my ($self, $fbat, $m, $which) = @_;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004267 my $fh = $::_repository->temp_acquire("git_blob_$which");
Florian Weimer214a34d2008-08-31 17:45:04 +02004268 if ($m->{"mode_$which"} =~ /^120/) {
4269 print $fh 'link ' or croak $!;
4270 $self->change_file_prop($fbat,'svn:special','*');
4271 } elsif ($m->{mode_a} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
4272 $self->change_file_prop($fbat,'svn:special',undef);
4273 }
4274 my $blob = $m->{"sha1_$which"};
4275 return ($fh,) if ($blob =~ /^0{40}$/);
4276 my $size = $::_repository->cat_blob($blob, $fh);
4277 croak "Failed to read object $blob" if ($size < 0);
4278 $fh->flush == 0 or croak $!;
4279 seek $fh, 0, 0 or croak $!;
4280
4281 my $exp = ::md5sum($fh);
4282 seek $fh, 0, 0 or croak $!;
4283 return ($fh, $exp);
4284}
4285
Eric Wonga5e0ced2006-06-12 15:23:48 -07004286sub chg_file {
4287 my ($self, $fbat, $m) = @_;
4288 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
4289 $self->change_file_prop($fbat,'svn:executable','*');
4290 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
4291 $self->change_file_prop($fbat,'svn:executable',undef);
4292 }
Florian Weimer8598db932008-08-31 17:47:09 +02004293 my ($fh_a, $exp_a) = _chg_file_get_blob $self, $fbat, $m, 'a';
4294 my ($fh_b, $exp_b) = _chg_file_get_blob $self, $fbat, $m, 'b';
Eric Wongf7197df2006-10-14 15:48:35 -07004295 my $pool = SVN::Pool->new;
Florian Weimer8598db932008-08-31 17:47:09 +02004296 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
4297 if (-s $fh_a) {
4298 my $txstream = SVN::TxDelta::new ($fh_a, $fh_b, $pool);
Eric Wong991255c2008-08-31 19:45:07 -07004299 my $res = SVN::TxDelta::send_txstream($txstream, @$atd, $pool);
4300 if (defined $res) {
4301 die "Unexpected result from send_txstream: $res\n",
4302 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
4303 }
Florian Weimer8598db932008-08-31 17:47:09 +02004304 } else {
4305 my $got = SVN::TxDelta::send_stream($fh_b, @$atd, $pool);
4306 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
4307 if ($got ne $exp_b);
4308 }
4309 Git::temp_release($fh_b, 1);
4310 Git::temp_release($fh_a, 1);
Eric Wongf7197df2006-10-14 15:48:35 -07004311 $pool->clear;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004312}
4313
4314sub D {
Eric Wong44320b92007-01-13 22:35:53 -08004315 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004316 my ($dir, $file) = split_path($m->{file_b});
4317 my $pbat = $self->ensure_path($dir);
Eric Wong44320b92007-01-13 22:35:53 -08004318 print "\tD\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004319 $self->delete_entry($m->{file_b}, $pbat);
4320}
4321
4322sub close_edit {
4323 my ($self) = @_;
4324 my ($p,$bat) = ($self->{pool}, $self->{bat});
4325 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
Eric Wong64427542007-05-19 02:58:37 -07004326 next if $_ eq '';
Eric Wonga5e0ced2006-06-12 15:23:48 -07004327 $self->close_directory($bat->{$_}, $p);
4328 }
Eric Wong64427542007-05-19 02:58:37 -07004329 $self->close_directory($bat->{''}, $p);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004330 $self->SUPER::close_edit($p);
4331 $p->clear;
4332}
4333
4334sub abort_edit {
4335 my ($self) = @_;
4336 $self->SUPER::abort_edit($self->{pool});
Eric Wong61395352007-01-27 14:33:08 -08004337}
4338
4339sub DESTROY {
4340 my $self = shift;
4341 $self->SUPER::DESTROY(@_);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004342 $self->{pool}->clear;
4343}
4344
Eric Wong44320b92007-01-13 22:35:53 -08004345# this drives the editor
4346sub apply_diff {
Eric Wong61395352007-01-27 14:33:08 -08004347 my ($self) = @_;
4348 my $mods = $self->{mods};
Eric Wong44320b92007-01-13 22:35:53 -08004349 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
Eric Wong6e8548c2007-01-27 01:32:00 -08004350 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
Eric Wong44320b92007-01-13 22:35:53 -08004351 my $f = $m->{chg};
4352 if (defined $o{$f}) {
4353 $self->$f($m);
4354 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004355 fatal("Invalid change type: $f");
Eric Wong44320b92007-01-13 22:35:53 -08004356 }
4357 }
Eric Wong24e22aa2007-01-29 00:07:49 -08004358 $self->rmdirs if $_rmdir;
Eric Wong6e8548c2007-01-27 01:32:00 -08004359 if (@$mods == 0) {
Eric Wong44320b92007-01-13 22:35:53 -08004360 $self->abort_edit;
4361 } else {
4362 $self->close_edit;
4363 }
Eric Wong6e8548c2007-01-27 01:32:00 -08004364 return scalar @$mods;
Eric Wong44320b92007-01-13 22:35:53 -08004365}
4366
Eric Wongd81bf822007-01-10 01:22:38 -08004367package Git::SVN::Ra;
Eric Wong6af1db42007-02-14 16:04:10 -08004368use vars qw/@ISA $config_dir $_log_window_size/;
Eric Wongd81bf822007-01-10 01:22:38 -08004369use strict;
4370use warnings;
Eric Wonga51cdb02007-09-07 04:00:40 -07004371my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
Eric Wongd81bf822007-01-10 01:22:38 -08004372
4373BEGIN {
4374 # enforce temporary pool usage for some simple functions
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004375 no strict 'refs';
Eric Wongbf8a40b2009-01-25 15:35:52 -08004376 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root
4377 get_file/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004378 my $SUPER = "SUPER::$f";
4379 *$f = sub {
4380 my $self = shift;
4381 my $pool = SVN::Pool->new;
4382 my @ret = $self->$SUPER(@_,$pool);
4383 $pool->clear;
4384 wantarray ? @ret : $ret[0];
4385 };
Eric Wongd81bf822007-01-10 01:22:38 -08004386 }
Eric Wongd81bf822007-01-10 01:22:38 -08004387}
4388
Steven Walter9ff74e92007-09-28 13:24:19 -04004389sub _auth_providers () {
4390 [
4391 SVN::Client::get_simple_provider(),
4392 SVN::Client::get_ssl_server_trust_file_provider(),
4393 SVN::Client::get_simple_prompt_provider(
4394 \&Git::SVN::Prompt::simple, 2),
4395 SVN::Client::get_ssl_client_cert_file_provider(),
4396 SVN::Client::get_ssl_client_cert_prompt_provider(
4397 \&Git::SVN::Prompt::ssl_client_cert, 2),
Sebastian Noack77266e92008-02-25 15:56:28 +01004398 SVN::Client::get_ssl_client_cert_pw_file_provider(),
Steven Walter9ff74e92007-09-28 13:24:19 -04004399 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
4400 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
4401 SVN::Client::get_username_provider(),
4402 SVN::Client::get_ssl_server_trust_prompt_provider(
4403 \&Git::SVN::Prompt::ssl_server_trust),
4404 SVN::Client::get_username_prompt_provider(
4405 \&Git::SVN::Prompt::username, 2)
4406 ]
4407}
4408
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004409sub escape_uri_only {
4410 my ($uri) = @_;
4411 my @tmp;
4412 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -07004413 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004414 push @tmp, $_;
4415 }
4416 join('/', @tmp);
4417}
4418
4419sub escape_url {
4420 my ($url) = @_;
4421 if ($url =~ m#^(https?)://([^/]+)(.*)$#) {
4422 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
4423 $url = "$scheme://$domain$uri";
4424 }
4425 $url;
4426}
4427
Eric Wongd81bf822007-01-10 01:22:38 -08004428sub new {
4429 my ($class, $url) = @_;
Eric Wongf6f09872007-01-18 18:22:18 -08004430 $url =~ s!/+$!!;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004431 return $RA if ($RA && $RA->{url} eq $url);
Eric Wongf6f09872007-01-18 18:22:18 -08004432
Eric Wongd81bf822007-01-10 01:22:38 -08004433 SVN::_Core::svn_config_ensure($config_dir, undef);
Steven Walter9ff74e92007-09-28 13:24:19 -04004434 my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
Eric Wongd81bf822007-01-10 01:22:38 -08004435 my $config = SVN::Core::config_get_config($config_dir);
Eric Wong7730fbe2007-07-04 14:07:42 -07004436 $RA = undef;
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004437 my $dont_store_passwords = 1;
4438 my $conf_t = ${$config}{'config'};
4439 {
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004440 no warnings 'once';
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004441 # The usage of $SVN::_Core::SVN_CONFIG_* variables
4442 # produces warnings that variables are used only once.
4443 # I had not found the better way to shut them up, so
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004444 # the warnings of type 'once' are disabled in this block.
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004445 if (SVN::_Core::svn_config_get_bool($conf_t,
4446 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4447 $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
4448 1) == 0) {
4449 SVN::_Core::svn_auth_set_parameter($baton,
4450 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
4451 bless (\$dont_store_passwords, "_p_void"));
4452 }
4453 if (SVN::_Core::svn_config_get_bool($conf_t,
4454 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4455 $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
4456 1) == 0) {
4457 $Git::SVN::Prompt::_no_auth_cache = 1;
4458 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004459 } # no warnings 'once'
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004460 my $self = SVN::Ra->new(url => escape_url($url), auth => $baton,
Eric Wongd81bf822007-01-10 01:22:38 -08004461 config => $config,
4462 pool => SVN::Pool->new,
4463 auth_provider_callbacks => $callbacks);
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004464 $self->{url} = $url;
Eric Wongd81bf822007-01-10 01:22:38 -08004465 $self->{svn_path} = $url;
4466 $self->{repos_root} = $self->get_repos_root;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08004467 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
Eric Wong0dc03d62007-05-13 01:04:43 -07004468 $self->{cache} = { check_path => { r => 0, data => {} },
4469 get_dir => { r => 0, data => {} } };
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004470 $RA = bless $self, $class;
Eric Wongd81bf822007-01-10 01:22:38 -08004471}
4472
Eric Wong0dc03d62007-05-13 01:04:43 -07004473sub check_path {
4474 my ($self, $path, $r) = @_;
4475 my $cache = $self->{cache}->{check_path};
4476 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
4477 return $cache->{data}->{$path};
4478 }
4479 my $pool = SVN::Pool->new;
4480 my $t = $self->SUPER::check_path($path, $r, $pool);
4481 $pool->clear;
4482 if ($r != $cache->{r}) {
4483 %{$cache->{data}} = ();
4484 $cache->{r} = $r;
4485 }
4486 $cache->{data}->{$path} = $t;
4487}
4488
4489sub get_dir {
4490 my ($self, $dir, $r) = @_;
4491 my $cache = $self->{cache}->{get_dir};
4492 if ($r == $cache->{r}) {
4493 if (my $x = $cache->{data}->{$dir}) {
4494 return wantarray ? @$x : $x->[0];
4495 }
4496 }
4497 my $pool = SVN::Pool->new;
4498 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
4499 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
4500 $pool->clear;
4501 if ($r != $cache->{r}) {
4502 %{$cache->{data}} = ();
4503 $cache->{r} = $r;
4504 }
4505 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
4506 wantarray ? (\%dirents, $r, $props) : \%dirents;
4507}
4508
Eric Wongd81bf822007-01-10 01:22:38 -08004509sub DESTROY {
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004510 # do not call the real DESTROY since we store ourselves in $RA
Eric Wongd81bf822007-01-10 01:22:38 -08004511}
4512
Eric Wong1ef626b2009-01-17 22:11:44 -08004513# get_log(paths, start, end, limit,
4514# discover_changed_paths, strict_node_history, receiver)
Eric Wongd81bf822007-01-10 01:22:38 -08004515sub get_log {
4516 my ($self, @args) = @_;
4517 my $pool = SVN::Pool->new;
Eric Wong1ef626b2009-01-17 22:11:44 -08004518
Mattias Nissler3c49a032009-07-07 01:39:52 +02004519 # svn_log_changed_path_t objects passed to get_log are likely to be
4520 # overwritten even if only the refs are copied to an external variable,
4521 # so we should dup the structures in their entirety. Using an
4522 # externally passed pool (instead of our temporary and quickly cleared
4523 # pool in Git::SVN::Ra) does not help matters at all...
4524 my $receiver = pop @args;
Mattias Nissler0b2af452009-07-07 01:40:02 +02004525 my $prefix = "/".$self->{svn_path};
4526 $prefix =~ s#/+($)##;
4527 my $prefix_regex = qr#^\Q$prefix\E#;
Mattias Nissler3c49a032009-07-07 01:39:52 +02004528 push(@args, sub {
4529 my ($paths) = $_[0];
4530 return &$receiver(@_) unless $paths;
4531 $_[0] = ();
4532 foreach my $p (keys %$paths) {
4533 my $i = $paths->{$p};
Mattias Nissler0b2af452009-07-07 01:40:02 +02004534 # Make path relative to our url, not repos_root
4535 $p =~ s/$prefix_regex//;
4536 my %s = map { $_ => $i->$_; }
4537 qw/copyfrom_path copyfrom_rev action/;
4538 if ($s{'copyfrom_path'}) {
4539 $s{'copyfrom_path'} =~ s/$prefix_regex//;
4540 }
Mattias Nissler3c49a032009-07-07 01:39:52 +02004541 $_[0]{$p} = \%s;
4542 }
4543 &$receiver(@_);
4544 });
4545
4546
Eric Wong1ef626b2009-01-17 22:11:44 -08004547 # the limit parameter was not supported in SVN 1.1.x, so we
4548 # drop it. Therefore, the receiver callback passed to it
4549 # is made aware of this limitation by being wrapped if
4550 # the limit passed to is being wrapped.
4551 if ($SVN::Core::VERSION le '1.2.0') {
4552 my $limit = splice(@args, 3, 1);
4553 if ($limit > 0) {
4554 my $receiver = pop @args;
4555 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
4556 }
4557 }
Eric Wongd81bf822007-01-10 01:22:38 -08004558 my $ret = $self->SUPER::get_log(@args, $pool);
4559 $pool->clear;
4560 $ret;
4561}
4562
Steven Walter9ff74e92007-09-28 13:24:19 -04004563sub trees_match {
4564 my ($self, $url1, $rev1, $url2, $rev2) = @_;
4565 my $ctx = SVN::Client->new(auth => _auth_providers);
4566 my $out = IO::File->new_tmpfile;
4567
4568 # older SVN (1.1.x) doesn't take $pool as the last parameter for
4569 # $ctx->diff(), so we'll create a default one
4570 my $pool = SVN::Pool->new_default_sub;
4571
4572 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
4573 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
4574 $out->flush;
4575 my $ret = (($out->stat)[7] == 0);
4576 close $out or croak $!;
4577
4578 $ret;
4579}
4580
Eric Wongd81bf822007-01-10 01:22:38 -08004581sub get_commit_editor {
Eric Wong44320b92007-01-13 22:35:53 -08004582 my ($self, $log, $cb, $pool) = @_;
Eric Wongd81bf822007-01-10 01:22:38 -08004583 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
Eric Wong44320b92007-01-13 22:35:53 -08004584 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004585}
4586
Eric Wongd81bf822007-01-10 01:22:38 -08004587sub gs_do_update {
Eric Wong8a603772007-01-31 02:45:50 -08004588 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
4589 my $new = ($rev_a == $rev_b);
4590 my $path = $gs->{path};
4591
Eric Wong2e5e2482007-02-23 02:21:59 -08004592 if ($new && -e $gs->{index}) {
4593 unlink $gs->{index} or die
4594 "Couldn't unlink index: $gs->{index}: $!\n";
4595 }
Eric Wongd81bf822007-01-10 01:22:38 -08004596 my $pool = SVN::Pool->new;
Eric Wong8b8fc062007-01-22 11:44:57 -08004597 $editor->set_path_strip($path);
Eric Wong2b27f6c2007-01-28 04:59:05 -08004598 my (@pc) = split m#/#, $path;
4599 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
Eric Wong8a603772007-01-31 02:45:50 -08004600 1, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004601 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong2b27f6c2007-01-28 04:59:05 -08004602
4603 # Since we can't rely on svn_ra_reparent being available, we'll
4604 # just have to do some magic with set_path to make it so
4605 # we only want a partial path.
4606 my $sp = '';
4607 my $final = join('/', @pc);
4608 while (@pc) {
4609 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
4610 $sp .= '/' if length $sp;
4611 $sp .= shift @pc;
4612 }
4613 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
4614
Eric Wong2b27f6c2007-01-28 04:59:05 -08004615 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
4616
Eric Wongd81bf822007-01-10 01:22:38 -08004617 $reporter->finish_report($pool);
4618 $pool->clear;
4619 $editor->{git_commit_ok};
4620}
4621
Eric Wong2b27f6c2007-01-28 04:59:05 -08004622# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
4623# svn_ra_reparent didn't work before 1.4)
Eric Wongd81bf822007-01-10 01:22:38 -08004624sub gs_do_switch {
Eric Wong8a603772007-01-31 02:45:50 -08004625 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
4626 my $path = $gs->{path};
Eric Wongd81bf822007-01-10 01:22:38 -08004627 my $pool = SVN::Pool->new;
Eric Wong2b27f6c2007-01-28 04:59:05 -08004628
4629 my $full_url = $self->{url};
4630 my $old_url = $full_url;
Eric Wong2a679c72009-07-19 22:08:45 -07004631 $full_url .= '/' . $path if length $path;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004632 my ($ra, $reparented);
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004633
Eric Wong2a679c72009-07-19 22:08:45 -07004634 if ($old_url =~ m#^svn(\+ssh)?://# ||
4635 ($full_url =~ m#^https?://# &&
4636 escape_url($full_url) ne $full_url)) {
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004637 $_[0] = undef;
4638 $self = undef;
4639 $RA = undef;
4640 $ra = Git::SVN::Ra->new($full_url);
4641 $ra_invalid = 1;
4642 } elsif ($old_url ne $full_url) {
4643 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, $pool);
4644 $self->{url} = $full_url;
4645 $reparented = 1;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004646 }
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004647
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004648 $ra ||= $self;
Eric Wongf4392df2008-09-06 20:18:18 -07004649 $url_b = escape_url($url_b);
Eric Wong8a603772007-01-31 02:45:50 -08004650 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004651 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong8b8fc062007-01-22 11:44:57 -08004652 $reporter->set_path('', $rev_a, 0, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004653 $reporter->finish_report($pool);
Eric Wong2b27f6c2007-01-28 04:59:05 -08004654
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004655 if ($reparented) {
4656 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
4657 $self->{url} = $old_url;
4658 }
Eric Wong2b27f6c2007-01-28 04:59:05 -08004659
Eric Wongd81bf822007-01-10 01:22:38 -08004660 $pool->clear;
4661 $editor->{git_commit_ok};
4662}
4663
Eric Wongb54a9012007-06-13 02:37:03 -07004664sub longest_common_path {
4665 my ($gsv, $globs) = @_;
Eric Wongd2ae1432007-02-07 11:50:16 -08004666 my %common;
Eric Wonge5181922007-02-08 12:53:57 -08004667 my $common_max = scalar @$gsv;
4668
4669 foreach my $gs (@$gsv) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004670 my @tmp = split m#/#, $gs->{path};
4671 my $p = '';
4672 foreach (@tmp) {
4673 $p .= length($p) ? "/$_" : $_;
4674 $common{$p} ||= 0;
4675 $common{$p}++;
4676 }
4677 }
Eric Wonge5181922007-02-08 12:53:57 -08004678 $globs ||= [];
4679 $common_max += scalar @$globs;
4680 foreach my $glob (@$globs) {
4681 my @tmp = split m#/#, $glob->{path}->{left};
4682 my $p = '';
4683 foreach (@tmp) {
4684 $p .= length($p) ? "/$_" : $_;
4685 $common{$p} ||= 0;
4686 $common{$p}++;
4687 }
4688 }
4689
Eric Wongd2ae1432007-02-07 11:50:16 -08004690 my $longest_path = '';
4691 foreach (sort {length $b <=> length $a} keys %common) {
Eric Wonge5181922007-02-08 12:53:57 -08004692 if ($common{$_} == $common_max) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004693 $longest_path = $_;
4694 last;
4695 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004696 }
Eric Wongb54a9012007-06-13 02:37:03 -07004697 $longest_path;
4698}
4699
4700sub gs_fetch_loop_common {
4701 my ($self, $base, $head, $gsv, $globs) = @_;
4702 return if ($base > $head);
4703 my $inc = $_log_window_size;
4704 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
4705 my $longest_path = longest_common_path($gsv, $globs);
Eric Wonga51cdb02007-09-07 04:00:40 -07004706 my $ra_url = $self->{url};
Alex Vandiverc69700f2009-05-06 16:18:52 -04004707 my $find_trailing_edge;
Eric Wong0af9c9f2007-01-27 22:28:56 -08004708 while (1) {
Eric Wongd4eff2b2007-01-30 14:04:22 -08004709 my %revs;
Eric Wongd2ae1432007-02-07 11:50:16 -08004710 my $err;
Eric Wongf7c3fc42007-01-29 18:34:55 -08004711 my $err_handler = $SVN::Error::handler;
Eric Wongd2ae1432007-02-07 11:50:16 -08004712 $SVN::Error::handler = sub {
4713 ($err) = @_;
4714 skip_unknown_revs($err);
4715 };
4716 sub _cb {
4717 my ($paths, $r, $author, $date, $log) = @_;
Mattias Nissler3c49a032009-07-07 01:39:52 +02004718 [ $paths,
Eric Wongd2ae1432007-02-07 11:50:16 -08004719 { author => $author, date => $date, log => $log } ];
4720 }
4721 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
4722 sub { $revs{$_[1]} = _cb(@_) });
Deskin Miller99366562009-02-08 19:33:18 -05004723 if ($err) {
4724 print "Checked through r$max\r";
Alex Vandiverc69700f2009-05-06 16:18:52 -04004725 } else {
4726 $find_trailing_edge = 1;
Deskin Miller99366562009-02-08 19:33:18 -05004727 }
Alex Vandiverc69700f2009-05-06 16:18:52 -04004728 if ($err and $find_trailing_edge) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004729 print STDERR "Path '$longest_path' ",
4730 "was probably deleted:\n",
4731 $err->expanded_message,
4732 "\nWill attempt to follow ",
4733 "revisions r$min .. r$max ",
4734 "committed before the deletion\n";
4735 my $hi = $max;
4736 while (--$hi >= $min) {
4737 my $ok;
4738 $self->get_log([$longest_path], $min, $hi,
4739 0, 1, 1, sub {
Alex Vandiverb6c61772009-05-06 16:18:53 -04004740 $ok = $_[1];
Eric Wongd2ae1432007-02-07 11:50:16 -08004741 $revs{$_[1]} = _cb(@_) });
4742 if ($ok) {
4743 print STDERR "r$min .. r$ok OK\n";
4744 last;
4745 }
4746 }
Alex Vandiverc69700f2009-05-06 16:18:52 -04004747 $find_trailing_edge = 0;
Eric Wongd2ae1432007-02-07 11:50:16 -08004748 }
Eric Wongd4eff2b2007-01-30 14:04:22 -08004749 $SVN::Error::handler = $err_handler;
Eric Wongfbcc1732007-02-06 18:35:30 -08004750
Eric Wonge5181922007-02-08 12:53:57 -08004751 my %exists = map { $_->{path} => $_ } @$gsv;
Eric Wongd4eff2b2007-01-30 14:04:22 -08004752 foreach my $r (sort {$a <=> $b} keys %revs) {
Eric Wongfbcc1732007-02-06 18:35:30 -08004753 my ($paths, $logged) = @{$revs{$r}};
Eric Wonge5181922007-02-08 12:53:57 -08004754
4755 foreach my $gs ($self->match_globs(\%exists, $paths,
4756 $globs, $r)) {
Eric Wong060610c2007-12-08 23:27:41 -08004757 if ($gs->rev_map_max >= $r) {
Eric Wongfbcc1732007-02-06 18:35:30 -08004758 next;
4759 }
4760 next unless $gs->match_paths($paths, $r);
4761 $gs->{logged_rev_props} = $logged;
Eric Wonge8d120b2007-02-14 16:29:52 -08004762 if (my $last_commit = $gs->last_commit) {
4763 $gs->assert_index_clean($last_commit);
4764 }
Eric Wongfbcc1732007-02-06 18:35:30 -08004765 my $log_entry = $gs->do_fetch($paths, $r);
4766 if ($log_entry) {
Eric Wong0af9c9f2007-01-27 22:28:56 -08004767 $gs->do_git_commit($log_entry);
4768 }
Eric Wong321b1842008-01-02 10:10:03 -08004769 $INDEX_FILES{$gs->{index}} = 1;
Eric Wong0af9c9f2007-01-27 22:28:56 -08004770 }
Eric Wonge5181922007-02-08 12:53:57 -08004771 foreach my $g (@$globs) {
Eric Wong93f26892007-02-11 01:20:26 -08004772 my $k = "svn-remote.$g->{remote}." .
4773 "$g->{t}-maxRev";
4774 Git::SVN::tmp_config($k, $r);
Eric Wonge5181922007-02-08 12:53:57 -08004775 }
Eric Wonga51cdb02007-09-07 04:00:40 -07004776 if ($ra_invalid) {
4777 $_[0] = undef;
4778 $self = undef;
4779 $RA = undef;
4780 $self = Git::SVN::Ra->new($ra_url);
4781 $ra_invalid = undef;
4782 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004783 }
Eric Wong9c93fee2007-01-31 17:22:31 -08004784 # pre-fill the .rev_db since it'll eventually get filled in
4785 # with '0' x40 if something new gets committed
Eric Wonge5181922007-02-08 12:53:57 -08004786 foreach my $gs (@$gsv) {
Eric Wong66ab84b2007-12-08 23:27:42 -08004787 next if $gs->rev_map_max >= $max;
4788 next if defined $gs->rev_map_get($max);
4789 $gs->rev_map_set($max, 0 x40);
Eric Wong9c93fee2007-01-31 17:22:31 -08004790 }
Eric Wongc3560e52007-02-12 16:03:32 -08004791 foreach my $g (@$globs) {
4792 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
4793 Git::SVN::tmp_config($k, $max);
4794 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004795 last if $max >= $head;
4796 $min = $max + 1;
4797 $max += $inc;
4798 $max = $head if ($max > $head);
4799 }
Karl Hasselström94bc9142008-02-03 17:56:18 +01004800 Git::SVN::gc();
Eric Wong0af9c9f2007-01-27 22:28:56 -08004801}
4802
Marcus Griep570d35c2008-08-08 01:41:57 -07004803sub get_dir_globbed {
4804 my ($self, $left, $depth, $r) = @_;
4805
4806 my @x = eval { $self->get_dir($left, $r) };
4807 return unless scalar @x == 3;
4808 my $dirents = $x[0];
4809 my @finalents;
4810 foreach my $de (keys %$dirents) {
4811 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
4812 if ($depth > 1) {
4813 my @args = ("$left/$de", $depth - 1, $r);
4814 foreach my $dir ($self->get_dir_globbed(@args)) {
4815 push @finalents, "$de/$dir";
4816 }
4817 } else {
4818 push @finalents, $de;
4819 }
4820 }
4821 @finalents;
4822}
4823
Eric Wonge5181922007-02-08 12:53:57 -08004824sub match_globs {
4825 my ($self, $exists, $paths, $globs, $r) = @_;
Eric Wong74a81222007-02-10 13:28:50 -08004826
4827 sub get_dir_check {
4828 my ($self, $exists, $g, $r) = @_;
Marcus Griep570d35c2008-08-08 01:41:57 -07004829
4830 my @dirs = $self->get_dir_globbed($g->{path}->{left},
4831 $g->{path}->{depth},
4832 $r);
4833
4834 foreach my $de (@dirs) {
Eric Wong74a81222007-02-10 13:28:50 -08004835 my $p = $g->{path}->full_path($de);
4836 next if $exists->{$p};
4837 next if (length $g->{path}->{right} &&
4838 ($self->check_path($p, $r) !=
4839 $SVN::Node::dir));
4840 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
4841 $g->{ref}->full_path($de), 1);
4842 }
4843 }
Eric Wonge5181922007-02-08 12:53:57 -08004844 foreach my $g (@$globs) {
Eric Wong74a81222007-02-10 13:28:50 -08004845 if (my $path = $paths->{"/$g->{path}->{left}"}) {
4846 if ($path->{action} =~ /^[AR]$/) {
4847 get_dir_check($self, $exists, $g, $r);
4848 }
4849 }
Eric Wonge5181922007-02-08 12:53:57 -08004850 foreach (keys %$paths) {
Eric Wong28710f72007-02-14 13:32:21 -08004851 if (/$g->{path}->{left_regex}/ &&
4852 !/$g->{path}->{regex}/) {
Eric Wong74a81222007-02-10 13:28:50 -08004853 next if $paths->{$_}->{action} !~ /^[AR]$/;
4854 get_dir_check($self, $exists, $g, $r);
4855 }
Eric Wonge5181922007-02-08 12:53:57 -08004856 next unless /$g->{path}->{regex}/;
4857 my $p = $1;
4858 my $pathname = $g->{path}->full_path($p);
4859 next if $exists->{$pathname};
Eric Wong0c1ec5a2007-04-18 00:17:33 -07004860 next if ($self->check_path($pathname, $r) !=
4861 $SVN::Node::dir);
Eric Wonge5181922007-02-08 12:53:57 -08004862 $exists->{$pathname} = Git::SVN->init(
4863 $self->{url}, $pathname, undef,
4864 $g->{ref}->full_path($p), 1);
4865 }
4866 my $c = '';
4867 foreach (split m#/#, $g->{path}->{left}) {
4868 $c .= "/$_";
4869 next unless ($paths->{$c} &&
Eric Wong74a81222007-02-10 13:28:50 -08004870 ($paths->{$c}->{action} =~ /^[AR]$/));
4871 get_dir_check($self, $exists, $g, $r);
Eric Wonge5181922007-02-08 12:53:57 -08004872 }
4873 }
4874 values %$exists;
4875}
4876
Eric Wonge6434f82007-01-23 16:29:23 -08004877sub minimize_url {
4878 my ($self) = @_;
4879 return $self->{url} if ($self->{url} eq $self->{repos_root});
4880 my $url = $self->{repos_root};
4881 my @components = split(m!/!, $self->{svn_path});
4882 my $c = '';
4883 do {
4884 $url .= "/$c" if length $c;
Eric Wong5f8b2cb2009-07-25 13:14:16 -07004885 eval {
4886 my $ra = (ref $self)->new($url);
4887 my $latest = $ra->get_latest_revnum;
4888 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
4889 };
Eric Wonge6434f82007-01-23 16:29:23 -08004890 } while ($@ && ($c = shift @components));
4891 $url;
4892}
4893
Eric Wongd81bf822007-01-10 01:22:38 -08004894sub can_do_switch {
4895 my $self = shift;
4896 unless (defined $can_do_switch) {
4897 my $pool = SVN::Pool->new;
4898 my $rep = eval {
4899 $self->do_switch(1, '', 0, $self->{url},
4900 SVN::Delta::Editor->new, $pool);
4901 };
4902 if ($@) {
4903 $can_do_switch = 0;
4904 } else {
4905 $rep->abort_report($pool);
4906 $can_do_switch = 1;
4907 }
4908 $pool->clear;
4909 }
4910 $can_do_switch;
4911}
4912
Eric Wong0af9c9f2007-01-27 22:28:56 -08004913sub skip_unknown_revs {
4914 my ($err) = @_;
4915 my $errno = $err->apr_err();
4916 # Maybe the branch we're tracking didn't
4917 # exist when the repo started, so it's
4918 # not an error if it doesn't, just continue
4919 #
4920 # Wonderfully consistent library, eh?
4921 # 160013 - svn:// and file://
4922 # 175002 - http(s)://
4923 # 175007 - http(s):// (this repo required authorization, too...)
4924 # More codes may be discovered later...
4925 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
Eric Wonga6a15a92007-03-30 17:54:48 -07004926 my $err_key = $err->expanded_message;
4927 # revision numbers change every time, filter them out
4928 $err_key =~ s/\d+/\0/g;
4929 $err_key = "$errno\0$err_key";
4930 unless ($ignored_err{$err_key}) {
4931 warn "W: Ignoring error from SVN, path probably ",
4932 "does not exist: ($errno): ",
4933 $err->expanded_message,"\n";
Eric Wongeee8a172008-01-07 02:40:40 -08004934 warn "W: Do not be alarmed at the above message ",
4935 "git-svn is just searching aggressively for ",
4936 "old history.\n",
4937 "This may take a while on large repositories\n";
Eric Wonga6a15a92007-03-30 17:54:48 -07004938 $ignored_err{$err_key} = 1;
4939 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004940 return;
4941 }
4942 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
4943}
4944
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004945package Git::SVN::Log;
4946use strict;
4947use warnings;
4948use POSIX qw/strftime/;
Ben Waltone8717842009-02-24 14:44:49 -05004949use Time::Local;
David D Kilzer111947e2007-11-11 22:56:52 -08004950use constant commit_log_separator => ('-' x 72) . "\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004951use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
4952 %rusers $show_commit $incremental/;
4953my $l_fmt;
4954
4955sub cmt_showable {
4956 my ($c) = @_;
4957 return 1 if defined $c->{r};
Eric Wongc16d0872007-04-08 00:59:22 -07004958
4959 # big commit message got truncated by the 16k pretty buffer in rev-list
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004960 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
4961 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
Eric Wongc16d0872007-04-08 00:59:22 -07004962 @{$c->{l}} = ();
Eric Wong44320b92007-01-13 22:35:53 -08004963 my @log = command(qw/cat-file commit/, $c->{c});
Eric Wongc16d0872007-04-08 00:59:22 -07004964
4965 # shift off the headers
4966 shift @log while ($log[0] ne '');
Eric Wong44320b92007-01-13 22:35:53 -08004967 shift @log;
Eric Wongc16d0872007-04-08 00:59:22 -07004968
4969 # TODO: make $c->{l} not have a trailing newline in the future
4970 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004971
4972 (undef, $c->{r}, undef) = ::extract_metadata(
Eric Wong44320b92007-01-13 22:35:53 -08004973 (grep(/^git-svn-id: /, @log))[-1]);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004974 }
4975 return defined $c->{r};
4976}
4977
4978sub log_use_color {
Jeff Kingcd459e32007-12-11 01:28:42 -05004979 return $color || Git->repository->get_colorbool('color.diff');
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004980}
4981
4982sub git_svn_log_cmd {
Eric Wong3bc718b2007-02-13 17:09:40 -08004983 my ($r_min, $r_max, @args) = @_;
4984 my $head = 'HEAD';
Eric Wong6ed77262007-08-15 09:55:18 -07004985 my (@files, @log_opts);
Eric Wong3bc718b2007-02-13 17:09:40 -08004986 foreach my $x (@args) {
Eric Wong6ed77262007-08-15 09:55:18 -07004987 if ($x eq '--' || @files) {
4988 push @files, $x;
4989 } else {
4990 if (::verify_ref("$x^0")) {
4991 $head = $x;
4992 } else {
4993 push @log_opts, $x;
4994 }
4995 }
Eric Wong3bc718b2007-02-13 17:09:40 -08004996 }
4997
Eric Wong13c823f2007-04-08 00:59:19 -07004998 my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
4999 $gs ||= Git::SVN->_new;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005000 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
5001 $gs->refname);
5002 push @cmd, '-r' unless $non_recursive;
5003 push @cmd, qw/--raw --name-status/ if $verbose;
5004 push @cmd, '--color' if log_use_color();
Eric Wong6ed77262007-08-15 09:55:18 -07005005 push @cmd, @log_opts;
5006 if (defined $r_max && $r_max == $r_min) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005007 push @cmd, '--max-count=1';
Eric Wong060610c2007-12-08 23:27:41 -08005008 if (my $c = $gs->rev_map_get($r_max)) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005009 push @cmd, $c;
5010 }
Eric Wong6ed77262007-08-15 09:55:18 -07005011 } elsif (defined $r_max) {
David D Kilzer111947e2007-11-11 22:56:52 -08005012 if ($r_max < $r_min) {
5013 ($r_min, $r_max) = ($r_max, $r_min);
5014 }
5015 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
5016 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
5017 # If there are no commits in the range, both $c_max and $c_min
5018 # will be undefined. If there is at least 1 commit in the
5019 # range, both will be defined.
5020 return () if !defined $c_min || !defined $c_max;
5021 if ($c_min eq $c_max) {
5022 push @cmd, '--max-count=1', $c_min;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005023 } else {
David D Kilzer111947e2007-11-11 22:56:52 -08005024 push @cmd, '--boundary', "$c_min..$c_max";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005025 }
5026 }
Eric Wong6ed77262007-08-15 09:55:18 -07005027 return (@cmd, @files);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005028}
5029
5030# adapted from pager.c
5031sub config_pager {
5032 $pager ||= $ENV{GIT_PAGER} || $ENV{PAGER};
5033 if (!defined $pager) {
5034 $pager = 'less';
5035 } elsif (length $pager == 0 || $pager eq 'cat') {
5036 $pager = undef;
5037 }
Jeff Kingcd459e32007-12-11 01:28:42 -05005038 $ENV{GIT_PAGER_IN_USE} = defined($pager);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005039}
5040
5041sub run_pager {
Eric Wongb0193042007-09-21 18:48:45 -07005042 return unless -t *STDOUT && defined $pager;
Marcus Griep971e6282008-09-10 11:09:46 -04005043 pipe my ($rfd, $wfd) or return;
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005044 defined(my $pid = fork) or ::fatal "Can't fork: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005045 if (!$pid) {
5046 open STDOUT, '>&', $wfd or
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005047 ::fatal "Can't redirect to stdout: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005048 return;
5049 }
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005050 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005051 $ENV{LESS} ||= 'FRSX';
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005052 exec $pager or ::fatal "Can't run pager: $! ($pager)";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005053}
5054
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005055sub format_svn_date {
Ben Waltone8717842009-02-24 14:44:49 -05005056 # some systmes don't handle or mishandle %z, so be creative.
Ben Walton736e6192009-02-27 22:11:45 -05005057 my $t = shift || time;
Ben Waltone8717842009-02-24 14:44:49 -05005058 my $gm = timelocal(gmtime($t));
5059 my $sign = qw( + + - )[ $t <=> $gm ];
5060 my $gmoff = sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
5061 return strftime("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005062}
5063
5064sub parse_git_date {
5065 my ($t, $tz) = @_;
5066 # Date::Parse isn't in the standard Perl distro :(
5067 if ($tz =~ s/^\+//) {
5068 $t += tz_to_s_offset($tz);
5069 } elsif ($tz =~ s/^\-//) {
5070 $t -= tz_to_s_offset($tz);
5071 }
5072 return $t;
5073}
5074
5075sub set_local_timezone {
5076 if (defined $TZ) {
5077 $ENV{TZ} = $TZ;
5078 } else {
5079 delete $ENV{TZ};
5080 }
5081}
5082
Eric Wong21819a32007-01-27 14:38:10 -08005083sub tz_to_s_offset {
5084 my ($tz) = @_;
5085 $tz =~ s/(\d\d)$//;
5086 return ($1 * 60) + ($tz * 3600);
5087}
5088
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005089sub get_author_info {
5090 my ($dest, $author, $t, $tz) = @_;
5091 $author =~ s/(?:^\s*|\s*$)//g;
5092 $dest->{a_raw} = $author;
5093 my $au;
Eric Wong1c8443b2007-01-14 02:17:00 -08005094 if ($::_authors) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005095 $au = $rusers{$author} || undef;
5096 }
5097 if (!$au) {
5098 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
5099 }
5100 $dest->{t} = $t;
5101 $dest->{tz} = $tz;
5102 $dest->{a} = $au;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005103 $dest->{t_utc} = parse_git_date($t, $tz);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005104}
5105
5106sub process_commit {
5107 my ($c, $r_min, $r_max, $defer) = @_;
5108 if (defined $r_min && defined $r_max) {
5109 if ($r_min == $c->{r} && $r_min == $r_max) {
5110 show_commit($c);
5111 return 0;
5112 }
5113 return 1 if $r_min == $r_max;
5114 if ($r_min < $r_max) {
5115 # we need to reverse the print order
5116 return 0 if (defined $limit && --$limit < 0);
5117 push @$defer, $c;
5118 return 1;
5119 }
5120 if ($r_min != $r_max) {
5121 return 1 if ($r_min < $c->{r});
5122 return 1 if ($r_max > $c->{r});
5123 }
5124 }
5125 return 0 if (defined $limit && --$limit < 0);
5126 show_commit($c);
5127 return 1;
5128}
5129
5130sub show_commit {
5131 my $c = shift;
5132 if ($oneline) {
5133 my $x = "\n";
5134 if (my $l = $c->{l}) {
5135 while ($l->[0] =~ /^\s*$/) { shift @$l }
5136 $x = $l->[0];
5137 }
5138 $l_fmt ||= 'A' . length($c->{r});
5139 print 'r',pack($l_fmt, $c->{r}),' | ';
5140 print "$c->{c} | " if $show_commit;
5141 print $x;
5142 } else {
5143 show_commit_normal($c);
5144 }
5145}
5146
5147sub show_commit_changed_paths {
5148 my ($c) = @_;
5149 return unless $c->{changed};
5150 print "Changed paths:\n", @{$c->{changed}};
5151}
5152
5153sub show_commit_normal {
5154 my ($c) = @_;
David D Kilzer111947e2007-11-11 22:56:52 -08005155 print commit_log_separator, "r$c->{r} | ";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005156 print "$c->{c} | " if $show_commit;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005157 print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005158 my $nr_line = 0;
5159
5160 if (my $l = $c->{l}) {
5161 while ($l->[$#$l] eq "\n" && $#$l > 0
5162 && $l->[($#$l - 1)] eq "\n") {
5163 pop @$l;
5164 }
5165 $nr_line = scalar @$l;
5166 if (!$nr_line) {
5167 print "1 line\n\n\n";
5168 } else {
5169 if ($nr_line == 1) {
5170 $nr_line = '1 line';
5171 } else {
5172 $nr_line .= ' lines';
5173 }
5174 print $nr_line, "\n";
5175 show_commit_changed_paths($c);
5176 print "\n";
5177 print $_ foreach @$l;
5178 }
5179 } else {
5180 print "1 line\n";
5181 show_commit_changed_paths($c);
5182 print "\n";
5183
5184 }
Eric Wong488a63e2007-02-15 00:40:42 -08005185 foreach my $x (qw/raw stat diff/) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005186 if ($c->{$x}) {
5187 print "\n";
5188 print $_ foreach @{$c->{$x}}
5189 }
5190 }
5191}
5192
5193sub cmd_show_log {
5194 my (@args) = @_;
5195 my ($r_min, $r_max);
5196 my $r_last = -1; # prevent dupes
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005197 set_local_timezone();
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005198 if (defined $::_revision) {
5199 if ($::_revision =~ /^(\d+):(\d+)$/) {
5200 ($r_min, $r_max) = ($1, $2);
5201 } elsif ($::_revision =~ /^\d+$/) {
5202 $r_min = $r_max = $::_revision;
5203 } else {
5204 ::fatal "-r$::_revision is not supported, use ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005205 "standard 'git log' arguments instead";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005206 }
5207 }
5208
5209 config_pager();
Eric Wong6ed77262007-08-15 09:55:18 -07005210 @args = git_svn_log_cmd($r_min, $r_max, @args);
David D Kilzer111947e2007-11-11 22:56:52 -08005211 if (!@args) {
5212 print commit_log_separator unless $incremental || $oneline;
5213 return;
5214 }
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005215 my $log = command_output_pipe(@args);
5216 run_pager();
Eric Wong488a63e2007-02-15 00:40:42 -08005217 my (@k, $c, $d, $stat);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005218 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
5219 while (<$log>) {
David D Kilzer60f3ff12007-11-10 22:10:34 -08005220 if (/^${esc_color}commit -?($::sha1_short)/o) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005221 my $cmt = $1;
5222 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
5223 $r_last = $c->{r};
5224 process_commit($c, $r_min, $r_max, \@k) or
5225 goto out;
5226 }
5227 $d = undef;
5228 $c = { c => $cmt };
5229 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
5230 get_author_info($c, $1, $2, $3);
5231 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
5232 # ignore
5233 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
5234 push @{$c->{raw}}, $_;
5235 } elsif (/^${esc_color}[ACRMDT]\t/) {
5236 # we could add $SVN->{svn_path} here, but that requires
5237 # remote access at the moment (repo_path_split)...
5238 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
5239 push @{$c->{changed}}, $_;
5240 } elsif (/^${esc_color}diff /o) {
5241 $d = 1;
5242 push @{$c->{diff}}, $_;
5243 } elsif ($d) {
5244 push @{$c->{diff}}, $_;
Eric Wong488a63e2007-02-15 00:40:42 -08005245 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
5246 $esc_color*[\+\-]*$esc_color$/x) {
5247 $stat = 1;
5248 push @{$c->{stat}}, $_;
5249 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
5250 push @{$c->{stat}}, $_;
5251 $stat = undef;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005252 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
5253 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
5254 } elsif (s/^${esc_color} //o) {
5255 push @{$c->{l}}, $_;
5256 }
5257 }
5258 if ($c && defined $c->{r} && $c->{r} != $r_last) {
5259 $r_last = $c->{r};
5260 process_commit($c, $r_min, $r_max, \@k);
5261 }
5262 if (@k) {
David D Kilzer111947e2007-11-11 22:56:52 -08005263 ($r_min, $r_max) = ($r_max, $r_min);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005264 process_commit($_, $r_min, $r_max) foreach reverse @k;
5265 }
5266out:
Eric Wongc843c462007-01-12 03:07:31 -08005267 close $log;
David D Kilzer111947e2007-11-11 22:56:52 -08005268 print commit_log_separator unless $incremental || $oneline;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005269}
5270
Tim Stoakes6fb53752008-02-10 15:21:08 +10305271sub cmd_blame {
Steven Grimm4be40382008-05-10 22:11:18 -07005272 my $path = pop;
Tim Stoakes6fb53752008-02-10 15:21:08 +10305273
5274 config_pager();
5275 run_pager();
5276
Steven Grimm4be40382008-05-10 22:11:18 -07005277 my ($fh, $ctx, $rev);
5278
5279 if ($_git_format) {
5280 ($fh, $ctx) = command_output_pipe('blame', @_, $path);
5281 while (my $line = <$fh>) {
5282 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
5283 # Uncommitted edits show up as a rev ID of
5284 # all zeros, which we can't look up with
5285 # cmt_metadata
5286 if ($1 !~ /^0+$/) {
5287 (undef, $rev, undef) =
5288 ::cmt_metadata($1);
5289 $rev = '0' if (!$rev);
5290 } else {
5291 $rev = '0';
5292 }
5293 $rev = sprintf('%-10s', $rev);
5294 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
5295 }
5296 print $line;
Tim Stoakes6fb53752008-02-10 15:21:08 +10305297 }
Steven Grimm4be40382008-05-10 22:11:18 -07005298 } else {
5299 ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD',
5300 '--', $path);
5301 my ($sha1);
5302 my %authors;
Boris Byk6ea42032009-04-11 00:32:41 +04005303 my @buffer;
5304 my %dsha; #distinct sha keys
5305
Steven Grimm4be40382008-05-10 22:11:18 -07005306 while (my $line = <$fh>) {
Boris Byk6ea42032009-04-11 00:32:41 +04005307 push @buffer, $line;
Steven Grimm4be40382008-05-10 22:11:18 -07005308 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
Boris Byk6ea42032009-04-11 00:32:41 +04005309 $dsha{$1} = 1;
5310 }
5311 }
5312
5313 my $s2r = ::cmt_sha2rev_batch([keys %dsha]);
5314
5315 foreach my $line (@buffer) {
5316 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
5317 $rev = $s2r->{$1};
5318 $rev = '0' if (!$rev)
Steven Grimm4be40382008-05-10 22:11:18 -07005319 }
5320 elsif ($line =~ /^author (.*)/) {
5321 $authors{$rev} = $1;
5322 $authors{$rev} =~ s/\s/_/g;
5323 }
5324 elsif ($line =~ /^\t(.*)$/) {
5325 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
5326 }
5327 }
Tim Stoakes6fb53752008-02-10 15:21:08 +10305328 }
5329 command_close_pipe($fh, $ctx);
5330}
5331
Eric Wong706587f2007-01-18 17:50:01 -08005332package Git::SVN::Migration;
5333# these version numbers do NOT correspond to actual version numbers
5334# of git nor git-svn. They are just relative.
5335#
5336# v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
5337#
5338# v1 layout: .git/$id/info/url, refs/remotes/$id
5339#
5340# v2 layout: .git/svn/$id/info/url, refs/remotes/$id
5341#
5342# v3 layout: .git/svn/$id, refs/remotes/$id
5343# - info/url may remain for backwards compatibility
5344# - this is what we migrate up to this layout automatically,
5345# - this will be used by git svn init on single branches
Eric Wong26a62d52007-02-12 13:25:25 -08005346# v3.1 layout (auto migrated):
5347# - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
5348# for backwards compatibility
Eric Wong706587f2007-01-18 17:50:01 -08005349#
5350# v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
5351# - this is only created for newly multi-init-ed
5352# repositories. Similar in spirit to the
5353# --use-separate-remotes option in git-clone (now default)
5354# - we do not automatically migrate to this (following
5355# the example set by core git)
Eric Wong060610c2007-12-08 23:27:41 -08005356#
5357# v5 layout: .rev_db.$UUID => .rev_map.$UUID
5358# - newer, more-efficient format that uses 24-bytes per record
5359# with no filler space.
5360# - use xxd -c24 < .rev_map.$UUID to view and debug
5361# - This is a one-way migration, repositories updated to the
5362# new format will not be able to use old git-svn without
5363# rebuilding the .rev_db. Rebuilding the rev_db is not
5364# possible if noMetadata or useSvmProps are set; but should
5365# be no problem for users that use the (sensible) defaults.
Eric Wong706587f2007-01-18 17:50:01 -08005366use strict;
5367use warnings;
5368use Carp qw/croak/;
5369use File::Path qw/mkpath/;
Eric Wong47e39c52007-01-21 04:27:09 -08005370use File::Basename qw/dirname basename/;
5371use vars qw/$_minimize/;
Eric Wong706587f2007-01-18 17:50:01 -08005372
5373sub migrate_from_v0 {
5374 my $git_dir = $ENV{GIT_DIR};
5375 return undef unless -d $git_dir;
5376 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5377 my $migrated = 0;
5378 while (<$fh>) {
5379 chomp;
5380 my ($id, $orig_ref) = ($_, $_);
5381 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
5382 next unless -f "$git_dir/$id/info/url";
5383 my $new_ref = "refs/remotes/$id";
5384 if (::verify_ref("$new_ref^0")) {
5385 print STDERR "W: $orig_ref is probably an old ",
5386 "branch used by an ancient version of ",
5387 "git-svn.\n",
5388 "However, $new_ref also exists.\n",
5389 "We will not be able ",
5390 "to use this branch until this ",
5391 "ambiguity is resolved.\n";
5392 next;
5393 }
5394 print STDERR "Migrating from v0 layout...\n" if !$migrated;
5395 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
5396 command_noisy('update-ref', $new_ref, $orig_ref);
5397 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
5398 $migrated++;
5399 }
5400 command_close_pipe($fh, $ctx);
5401 print STDERR "Done migrating from v0 layout...\n" if $migrated;
5402 $migrated;
5403}
5404
5405sub migrate_from_v1 {
5406 my $git_dir = $ENV{GIT_DIR};
5407 my $migrated = 0;
5408 return $migrated unless -d $git_dir;
5409 my $svn_dir = "$git_dir/svn";
5410
5411 # just in case somebody used 'svn' as their $id at some point...
5412 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
5413
5414 print STDERR "Migrating from a git-svn v1 layout...\n";
5415 mkpath([$svn_dir]);
5416 print STDERR "Data from a previous version of git-svn exists, but\n\t",
5417 "$svn_dir\n\t(required for this version ",
Frederik Schwarzer8f510be2008-07-14 18:30:24 +02005418 "($::VERSION) of git-svn) does not exist.\n";
Eric Wong706587f2007-01-18 17:50:01 -08005419 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5420 while (<$fh>) {
5421 my $x = $_;
5422 next unless $x =~ s#^refs/remotes/##;
5423 chomp $x;
5424 next unless -f "$git_dir/$x/info/url";
5425 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
5426 next unless $u;
5427 my $dn = dirname("$git_dir/svn/$x");
5428 mkpath([$dn]) unless -d $dn;
5429 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
5430 mkpath(["$git_dir/svn/svn"]);
5431 print STDERR " - $git_dir/$x/info => ",
5432 "$git_dir/svn/$x/info\n";
5433 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
5434 croak "$!: $x";
5435 # don't worry too much about these, they probably
5436 # don't exist with repos this old (save for index,
5437 # and we can easily regenerate that)
5438 foreach my $f (qw/unhandled.log index .rev_db/) {
5439 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
5440 }
5441 } else {
5442 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
5443 rename "$git_dir/$x", "$git_dir/svn/$x" or
5444 croak "$!: $x";
5445 }
5446 $migrated++;
5447 }
5448 command_close_pipe($fh, $ctx);
5449 print STDERR "Done migrating from a git-svn v1 layout\n";
5450 $migrated;
5451}
5452
5453sub read_old_urls {
5454 my ($l_map, $pfx, $path) = @_;
5455 my @dir;
5456 foreach (<$path/*>) {
5457 if (-r "$_/info/url") {
5458 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
5459 my $ref_id = $pfx . basename $_;
5460 my $url = ::file_to_s("$_/info/url");
5461 $l_map->{$ref_id} = $url;
5462 } elsif (-d $_) {
5463 push @dir, $_;
5464 }
5465 }
5466 foreach (@dir) {
5467 my $x = $_;
5468 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
5469 read_old_urls($l_map, $x, $_);
5470 }
5471}
5472
5473sub migrate_from_v2 {
5474 my @cfg = command(qw/config -l/);
5475 return if grep /^svn-remote\..+\.url=/, @cfg;
5476 my %l_map;
5477 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
5478 my $migrated = 0;
5479
5480 foreach my $ref_id (sort keys %l_map) {
Eric Wong471bc002007-02-01 04:06:27 -08005481 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
5482 if ($@) {
5483 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
5484 }
Eric Wong706587f2007-01-18 17:50:01 -08005485 $migrated++;
5486 }
5487 $migrated;
5488}
5489
Eric Wong47e39c52007-01-21 04:27:09 -08005490sub minimize_connections {
5491 my $r = Git::SVN::read_all_remotes();
5492 my $new_urls = {};
5493 my $root_repos = {};
5494 foreach my $repo_id (keys %$r) {
5495 my $url = $r->{$repo_id}->{url} or next;
5496 my $fetch = $r->{$repo_id}->{fetch} or next;
5497 my $ra = Git::SVN::Ra->new($url);
5498
5499 # skip existing cases where we already connect to the root
5500 if (($ra->{url} eq $ra->{repos_root}) ||
Eric Wong7829f202008-06-28 20:40:32 -07005501 ($ra->{repos_root} eq $repo_id)) {
Eric Wong47e39c52007-01-21 04:27:09 -08005502 $root_repos->{$ra->{url}} = $repo_id;
5503 next;
5504 }
5505
5506 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
5507 my $root_path = $ra->{url};
Eric Wong4e9f6cc2007-02-09 12:17:57 -08005508 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
Eric Wong47e39c52007-01-21 04:27:09 -08005509 foreach my $path (keys %$fetch) {
5510 my $ref_id = $fetch->{$path};
5511 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
5512
5513 # make sure we can read when connecting to
5514 # a higher level of a repository
5515 my ($last_rev, undef) = $gs->last_rev_commit;
5516 if (!defined $last_rev) {
5517 $last_rev = eval {
5518 $root_ra->get_latest_revnum;
5519 };
5520 next if $@;
5521 }
5522 my $new = $root_path;
5523 $new .= length $path ? "/$path" : '';
5524 eval {
5525 $root_ra->get_log([$new], $last_rev, $last_rev,
5526 0, 0, 1, sub { });
5527 };
5528 next if $@;
5529 $new_urls->{$ra->{repos_root}}->{$new} =
5530 { ref_id => $ref_id,
5531 old_repo_id => $repo_id,
5532 old_path => $path };
5533 }
5534 }
5535
5536 my @emptied;
5537 foreach my $url (keys %$new_urls) {
5538 # see if we can re-use an existing [svn-remote "repo_id"]
5539 # instead of creating a(n ugly) new section:
Eric Wong7829f202008-06-28 20:40:32 -07005540 my $repo_id = $root_repos->{$url} || $url;
Eric Wong47e39c52007-01-21 04:27:09 -08005541
5542 my $fetch = $new_urls->{$url};
5543 foreach my $path (keys %$fetch) {
5544 my $x = $fetch->{$path};
5545 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
5546 my $pfx = "svn-remote.$x->{old_repo_id}";
5547
5548 my $old_fetch = quotemeta("$x->{old_path}:".
Adam Brewster6f5748e2009-08-11 23:14:27 -04005549 "$x->{ref_id}");
Eric Wong8b8fc062007-01-22 11:44:57 -08005550 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08005551 "$pfx.fetch", '^'. $old_fetch . '$');
5552 delete $r->{$x->{old_repo_id}}->
5553 {fetch}->{$x->{old_path}};
5554 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
Eric Wong8b8fc062007-01-22 11:44:57 -08005555 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08005556 "$pfx.url");
5557 push @emptied, $x->{old_repo_id}
5558 }
5559 }
5560 }
5561 if (@emptied) {
Johannes Schindelin8befc502008-12-14 23:10:52 +01005562 my $file = $ENV{GIT_CONFIG} || "$ENV{GIT_DIR}/config";
Eric Wong47e39c52007-01-21 04:27:09 -08005563 print STDERR <<EOF;
5564The following [svn-remote] sections in your config file ($file) are empty
5565and can be safely removed:
5566EOF
5567 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
5568 }
5569}
5570
Eric Wong706587f2007-01-18 17:50:01 -08005571sub migration_check {
5572 migrate_from_v0();
5573 migrate_from_v1();
5574 migrate_from_v2();
Eric Wong47e39c52007-01-21 04:27:09 -08005575 minimize_connections() if $_minimize;
Eric Wong706587f2007-01-18 17:50:01 -08005576}
5577
Eric Wongef3cfaa2007-01-24 03:30:57 -08005578package Git::IndexInfo;
5579use strict;
5580use warnings;
5581use Git qw/command_input_pipe command_close_pipe/;
5582
5583sub new {
5584 my ($class) = @_;
5585 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
5586 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
5587}
5588
5589sub remove {
5590 my ($self, $path) = @_;
5591 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
5592 return ++$self->{nr};
5593 }
5594 undef;
5595}
5596
5597sub update {
5598 my ($self, $mode, $hash, $path) = @_;
5599 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
5600 return ++$self->{nr};
5601 }
5602 undef;
5603}
5604
5605sub DESTROY {
5606 my ($self) = @_;
5607 command_close_pipe($self->{gui}, $self->{ctx});
5608}
5609
Eric Wong4bb9ed02007-02-03 13:29:17 -08005610package Git::SVN::GlobSpec;
5611use strict;
5612use warnings;
5613
5614sub new {
5615 my ($class, $glob) = @_;
Eric Wong4bb9ed02007-02-03 13:29:17 -08005616 my $re = $glob;
5617 $re =~ s!/+$!!g; # no need for trailing slashes
Adam Brewster6f5748e2009-08-11 23:14:27 -04005618 $re =~ m!^([^*]*)(\*(?:/\*)*)(.*)$!;
Marcus Griep570d35c2008-08-08 01:41:57 -07005619 my $temp = $re;
5620 my ($left, $right) = ($1, $3);
5621 $re = $2;
5622 my $depth = $re =~ tr/*/*/;
5623 if ($depth != $temp =~ tr/*/*/) {
5624 die "Only one set of wildcard directories " .
5625 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
5626 }
5627 if ($depth == 0) {
Eric Wonge5181922007-02-08 12:53:57 -08005628 die "One '*' is needed for glob: '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08005629 }
Marcus Griep570d35c2008-08-08 01:41:57 -07005630 $re =~ s!\*!\[^/\]*!g;
5631 $re = quotemeta($left) . "($re)" . quotemeta($right);
Eric Wong4e9f6cc2007-02-09 12:17:57 -08005632 if (length $left && !($left =~ s!/+$!!g)) {
5633 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
5634 }
5635 if (length $right && !($right =~ s!^/+!!g)) {
5636 die "Missing leading '/' on right side of: '$glob' ($right)\n";
5637 }
Eric Wong74a81222007-02-10 13:28:50 -08005638 my $left_re = qr/^\/\Q$left\E(\/|$)/;
5639 bless { left => $left, right => $right, left_regex => $left_re,
Marcus Griep570d35c2008-08-08 01:41:57 -07005640 regex => qr/$re/, glob => $glob, depth => $depth }, $class;
Eric Wong4bb9ed02007-02-03 13:29:17 -08005641}
5642
5643sub full_path {
5644 my ($self, $path) = @_;
5645 return (length $self->{left} ? "$self->{left}/" : '') .
5646 $path . (length $self->{right} ? "/$self->{right}" : '');
5647}
5648
Eric Wong3397f9d2006-02-16 01:24:16 -08005649__END__
5650
5651Data structures:
5652
Eric Wong4bb9ed02007-02-03 13:29:17 -08005653
5654$remotes = { # returned by read_all_remotes()
5655 'svn' => {
5656 # svn-remote.svn.url=https://svn.musicpd.org
5657 url => 'https://svn.musicpd.org',
5658 # svn-remote.svn.fetch=mpd/trunk:trunk
5659 fetch => {
5660 'mpd/trunk' => 'trunk',
5661 },
5662 # svn-remote.svn.tags=mpd/tags/*:tags/*
5663 tags => {
5664 path => {
5665 left => 'mpd/tags',
5666 right => '',
5667 regex => qr!mpd/tags/([^/]+)$!,
5668 glob => 'tags/*',
5669 },
5670 ref => {
5671 left => 'tags',
5672 right => '',
5673 regex => qr!tags/([^/]+)$!,
5674 glob => 'tags/*',
5675 },
5676 }
5677 }
5678};
5679
Eric Wong44320b92007-01-13 22:35:53 -08005680$log_entry hashref as returned by libsvn_log_entry()
Eric Wong3397f9d2006-02-16 01:24:16 -08005681{
Eric Wong44320b92007-01-13 22:35:53 -08005682 log => 'whitespace-formatted log entry
Eric Wong3397f9d2006-02-16 01:24:16 -08005683', # trailing newline is preserved
5684 revision => '8', # integer
5685 date => '2004-02-24T17:01:44.108345Z', # commit date
5686 author => 'committer name'
5687};
5688
Eric Wong6e8548c2007-01-27 01:32:00 -08005689
5690# this is generated by generate_diff();
Eric Wong3397f9d2006-02-16 01:24:16 -08005691@mods = array of diff-index line hashes, each element represents one line
5692 of diff-index output
5693
5694diff-index line ($m hash)
5695{
5696 mode_a => first column of diff-index output, no leading ':',
5697 mode_b => second column of diff-index output,
5698 sha1_b => sha1sum of the final blob,
Eric Wongac8e0b92006-03-03 01:20:07 -08005699 chg => change type [MCRADT],
Eric Wong3397f9d2006-02-16 01:24:16 -08005700 file_a => original file name of a file (iff chg is 'C' or 'R')
5701 file_b => new/current file name of a file (any chg)
5702}
5703;
Eric Wonga5e0ced2006-06-12 15:23:48 -07005704
Eric Wonga00439a2006-06-27 19:39:13 -07005705# retval of read_url_paths{,_all}();
5706$l_map = {
5707 # repository root url
5708 'https://svn.musicpd.org' => {
5709 # repository path # GIT_SVN_ID
5710 'mpd/trunk' => 'trunk',
5711 'mpd/tags/0.11.5' => 'tags/0.11.5',
5712 },
5713}
5714
Eric Wonga5e0ced2006-06-12 15:23:48 -07005715Notes:
5716 I don't trust the each() function on unless I created %hash myself
5717 because the internal iterator may not have started at base.