blob: 177dd259cd53cde017d73db5ccbecc7ed7dfa573 [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
Ævar Arnfjörð Bjarmasond48b2842010-09-24 20:00:52 +00004use 5.008;
Eric Wong3397f9d2006-02-16 01:24:16 -08005use warnings;
6use strict;
7use vars qw/ $AUTHOR $VERSION
Adam Robenffe256f2008-05-23 16:19:41 +02008 $sha1 $sha1_short $_revision $_repository
Mark Lodato36db1ed2009-05-14 21:27:15 -04009 $_q $_authors $_authors_prog %users/;
Eric Wong3397f9d2006-02-16 01:24:16 -080010$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
Eric Wong60d02cc2006-07-06 00:14:16 -070011$VERSION = '@@GIT_VERSION@@';
Eric Wong13ccd6d2006-03-29 22:37:18 -080012
Benoit Sigoure15153452007-10-16 16:36:50 +020013# From which subdir have we been invoked?
14my $cmd_dir_prefix = eval {
15 command_oneline([qw/rev-parse --show-prefix/], STDERR => 0)
16} || '';
17
Eric Wong5253dc32007-02-20 01:36:30 -080018my $git_dir_user_set = 1 if defined $ENV{GIT_DIR};
Eric Wong706587f2007-01-18 17:50:01 -080019$ENV{GIT_DIR} ||= '.git';
Eric Wong9fa00b62007-02-03 12:49:48 -080020$Git::SVN::default_repo_id = 'svn';
Eric Wong8b8fc062007-01-22 11:44:57 -080021$Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
Eric Wong6af1db42007-02-14 16:04:10 -080022$Git::SVN::Ra::_log_window_size = 100;
Eric Wong6b488292009-07-25 00:00:50 -070023$Git::SVN::_minimize_url = 'unset';
Eric Wong13ccd6d2006-03-29 22:37:18 -080024
Karthik Rf3a87d92009-08-18 18:54:40 -050025if (! exists $ENV{SVN_SSH}) {
26 if (exists $ENV{GIT_SSH}) {
27 $ENV{SVN_SSH} = $ENV{GIT_SSH};
28 if ($^O eq 'msys') {
29 $ENV{SVN_SSH} =~ s/\\/\\\\/g;
Sebastian Schubertha004fb92010-01-23 15:20:28 +010030 $ENV{SVN_SSH} =~ s/(.*)/"$1"/;
Karthik Rf3a87d92009-08-18 18:54:40 -050031 }
32 }
33}
34
Eric Wongf8c9d1d2007-01-12 02:35:20 -080035$Git::SVN::Log::TZ = $ENV{TZ};
Eric Wong3397f9d2006-02-16 01:24:16 -080036$ENV{TZ} = 'UTC';
Eric Wonga00439a2006-06-27 19:39:13 -070037$| = 1; # unbuffer STDOUT
Eric Wong3397f9d2006-02-16 01:24:16 -080038
Benoit Sigoure207f1a72007-10-16 16:36:52 +020039sub fatal (@) { print STDERR "@_\n"; exit 1 }
josh robbd32fad22010-02-24 16:13:50 +130040sub _req_svn {
41 require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
42 require SVN::Ra;
43 require SVN::Delta;
44 if ($SVN::Core::VERSION lt '1.1.0') {
45 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
46 }
Eric Wongb9c85182006-12-15 23:58:07 -080047}
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -050048my $can_compress = eval { require Compress::Zlib; 1};
Eric Wongd81bf822007-01-10 01:22:38 -080049push @Git::SVN::Ra::ISA, 'SVN::Ra';
Eric Wongb9c85182006-12-15 23:58:07 -080050push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
51push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
Eric Wong3397f9d2006-02-16 01:24:16 -080052use Carp qw/croak/;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -080053use Digest::MD5;
Eric Wong3397f9d2006-02-16 01:24:16 -080054use IO::File qw//;
55use File::Basename qw/dirname basename/;
56use File::Path qw/mkpath/;
Mark Lodato36db1ed2009-05-14 21:27:15 -040057use File::Spec;
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -050058use File::Find;
Eric Wong512b6202007-04-03 01:57:08 -070059use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
Eric Wong968bdf12006-06-15 13:36:12 -070060use IPC::Open3;
Eric Wong336f1712007-01-11 02:14:43 -080061use Git;
Eric Wonga5e0ced2006-06-12 15:23:48 -070062
Eric Wong336f1712007-01-11 02:14:43 -080063BEGIN {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120064 # import functions from Git into our packages, en masse
65 no strict 'refs';
Eric Wong336f1712007-01-11 02:14:43 -080066 foreach (qw/command command_oneline command_noisy command_output_pipe
Boris Byk6ea42032009-04-11 00:32:41 +040067 command_input_pipe command_close_pipe
68 command_bidi_pipe command_close_bidi_pipe/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120069 for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -080070 Git::SVN::Migration Git::SVN::Log Git::SVN),
Sam Vilainc5f71ad2007-06-15 15:43:59 +120071 __PACKAGE__) {
72 *{"${package}::$_"} = \&{"Git::$_"};
73 }
Eric Wong336f1712007-01-11 02:14:43 -080074 }
Eric Wong336f1712007-01-11 02:14:43 -080075}
76
Eric Wongb9c85182006-12-15 23:58:07 -080077my ($SVN);
Eric Wong83e99402006-10-11 18:19:55 -070078
Eric Wongf8c9d1d2007-01-12 02:35:20 -080079$sha1 = qr/[a-f\d]{40}/;
80$sha1_short = qr/[a-f\d]{4,40}/;
Eric Wong44320b92007-01-13 22:35:53 -080081my ($_stdin, $_help, $_edit,
Marc Branchaud62244062009-06-23 13:02:08 -040082 $_message, $_file, $_branch_dest,
Eric Wongd05d72e2007-01-15 22:59:26 -080083 $_template, $_shared,
Jason Merrillc2abd832009-04-06 16:37:59 -040084 $_version, $_fetch_all, $_no_rebase, $_fetch_parent,
Eric Wongdee41f32007-03-13 11:40:36 -070085 $_merge, $_strategy, $_dry_run, $_local,
Steven Grimm4be40382008-05-10 22:11:18 -070086 $_prefix, $_no_checkout, $_url, $_verbose,
Steven Walter6abd9332010-09-24 23:51:50 -040087 $_git_format, $_commit_url, $_tag, $_merge_info);
Eric Wong0bed5ea2007-02-09 02:45:03 -080088$Git::SVN::_follow_parent = 1;
Simon Arlott49750f32009-03-30 19:31:41 +010089$_q ||= 0;
Eric Wong706587f2007-01-18 17:50:01 -080090my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
91 'config-dir=s' => \$Git::SVN::Ra::config_dir,
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +020092 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
93 'ignore-paths=s' => \$SVN::Git::Fetcher::_ignore_regex );
Eric Wong0bed5ea2007-02-09 02:45:03 -080094my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
Eric Wongdc5869c2006-05-24 02:07:32 -070095 'authors-file|A=s' => \$_authors,
Mark Lodato36db1ed2009-05-14 21:27:15 -040096 'authors-prog=s' => \$_authors_prog,
Eric Wongecc712d2007-01-31 12:28:10 -080097 'repack:i' => \$Git::SVN::_repack,
Eric Wong97ae0912007-02-11 15:21:24 -080098 'noMetadata' => \$Git::SVN::_no_metadata,
99 'useSvmProps' => \$Git::SVN::_use_svm_props,
Eric Wong62e349d2007-02-16 19:57:29 -0800100 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props,
Eric Wong6af1db42007-02-14 16:04:10 -0800101 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size,
Eric Wong1e889ef2007-02-16 01:45:13 -0800102 'no-checkout' => \$_no_checkout,
Simon Arlott49750f32009-03-30 19:31:41 +0100103 'quiet|q+' => \$_q,
Eric Wongecc712d2007-01-31 12:28:10 -0800104 'repack-flags|repack-args|repack-opts=s' =>
105 \$Git::SVN::_repack_flags,
Andy Whitcroft70ae04e2007-11-22 13:44:42 +0000106 'use-log-author' => \$Git::SVN::_use_log_author,
Avery Pennarun6aa9ba12008-04-15 21:04:17 -0400107 'add-author-from' => \$Git::SVN::_add_author_from,
Pete Harlane82f0d72009-01-17 20:10:14 -0800108 'localtime' => \$Git::SVN::_localtime,
Eric Wong706587f2007-01-18 17:50:01 -0800109 %remote_opts );
Eric Wong36f5b1f2006-05-23 19:23:41 -0700110
Marc Branchaud62244062009-06-23 13:02:08 -0400111my ($_trunk, @_tags, @_branches, $_stdlayout);
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800112my %icv;
Eric Wongdadc6d22007-02-14 12:27:41 -0800113my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared,
Marc Branchaud62244062009-06-23 13:02:08 -0400114 'trunk|T=s' => \$_trunk, 'tags|t=s@' => \@_tags,
115 'branches|b=s@' => \@_branches, 'prefix=s' => \$_prefix,
martin f. krafft8f728fb2007-07-14 11:25:28 +0200116 'stdlayout|s' => \$_stdlayout,
Eric Wong6b488292009-07-25 00:00:50 -0700117 'minimize-url|m!' => \$Git::SVN::_minimize_url,
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800118 'no-metadata' => sub { $icv{noMetadata} = 1 },
119 'use-svm-props' => sub { $icv{useSvmProps} = 1 },
120 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 },
121 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] },
Jay Soffian3e18ce12010-01-23 03:30:00 -0500122 'rewrite-uuid=s' => sub { $icv{rewriteUUID} = $_[1] },
Eric Wongdadc6d22007-02-14 12:27:41 -0800123 %remote_opts );
Eric Wong27e9fb82006-06-27 19:39:12 -0700124my %cmt_opts = ( 'edit|e' => \$_edit,
Eric Wong24e22aa2007-01-29 00:07:49 -0800125 'rmdir' => \$SVN::Git::Editor::_rmdir,
126 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder,
127 'l=i' => \$SVN::Git::Editor::_rename_limit,
128 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity
Eric Wong27e9fb82006-06-27 19:39:12 -0700129);
Eric Wong9d55b412006-06-12 15:53:13 -0700130
Eric Wong3397f9d2006-02-16 01:24:16 -0800131my %cmd = (
Eric Wong2a3240b2007-01-04 18:09:56 -0800132 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
Eric Wonge98671e2007-02-14 02:21:19 -0800133 { 'revision|r=s' => \$_revision,
Eric Wong905f8b72007-02-16 03:22:40 -0800134 'fetch-all|all' => \$_fetch_all,
Jason Merrillc2abd832009-04-06 16:37:59 -0400135 'parent|p' => \$_fetch_parent,
Eric Wonge98671e2007-02-14 02:21:19 -0800136 %fc_opts } ],
Eric Wong0425ea92007-02-16 18:45:01 -0800137 clone => [ \&cmd_clone, "Initialize and fetch revisions",
138 { 'revision|r=s' => \$_revision,
139 %fc_opts, %init_opts } ],
Eric Wongd2866f92007-01-11 12:26:16 -0800140 init => [ \&cmd_init, "Initialize a repo for tracking" .
Eric Wongf8ab6b72006-05-31 15:49:56 -0700141 " (requires URL argument)",
Eric Wong9d55b412006-06-12 15:53:13 -0700142 \%init_opts ],
Eric Wongdadc6d22007-02-14 12:27:41 -0800143 'multi-init' => [ \&cmd_multi_init,
144 "Deprecated alias for ".
145 "'$0 init -T<trunk> -b<branches> -t<tags>'",
146 \%init_opts ],
Eric Wongd7ad3be2007-01-14 03:14:28 -0800147 dcommit => [ \&cmd_dcommit,
148 'Commit several diffs to merge with upstream',
Eric Wong3289e862006-12-15 23:58:08 -0800149 { 'merge|m|M' => \$_merge,
150 'strategy|s=s' => \$_strategy,
Eric Wong905f8b72007-02-16 03:22:40 -0800151 'verbose|v' => \$_verbose,
Eric Wong3289e862006-12-15 23:58:08 -0800152 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800153 'fetch-all|all' => \$_fetch_all,
Eric Wongba24e742008-08-07 02:06:16 -0700154 'commit-url=s' => \$_commit_url,
155 'revision|r=i' => \$_revision,
Karl Hasselström171af112007-05-03 07:51:35 +0200156 'no-rebase' => \$_no_rebase,
Steven Walter6abd9332010-09-24 23:51:50 -0400157 'mergeinfo=s' => \$_merge_info,
Eric Wong4b155222006-12-22 21:59:24 -0800158 %cmt_opts, %fc_opts } ],
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700159 branch => [ \&cmd_branch,
160 'Create a branch 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,
Igor Mironov6594f0b2010-01-12 03:21:51 +1100164 'tag|t' => \$_tag,
165 'username=s' => \$Git::SVN::Prompt::_username,
166 'commit-url=s' => \$_commit_url } ],
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700167 tag => [ sub { $_tag = 1; cmd_branch(@_) },
168 'Create a tag in the SVN repository',
169 { 'message|m=s' => \$_message,
Marc Branchaud62244062009-06-23 13:02:08 -0400170 'destination|d=s' => \$_branch_dest,
Igor Mironov6594f0b2010-01-12 03:21:51 +1100171 'dry-run|n' => \$_dry_run,
172 'username=s' => \$Git::SVN::Prompt::_username,
173 'commit-url=s' => \$_commit_url } ],
Eric Wong1ce255d2007-01-14 23:21:16 -0800174 'set-tree' => [ \&cmd_set_tree,
175 "Set an SVN repository to a git tree-ish",
Robin H. Johnsone84dc6d2009-05-05 11:16:14 -0700176 { 'stdin' => \$_stdin, %cmt_opts, %fc_opts, } ],
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200177 'create-ignore' => [ \&cmd_create_ignore,
178 'Create a .gitignore per svn:ignore',
179 { 'revision|r=i' => \$_revision
180 } ],
Eric Wong6111b932009-11-15 18:57:16 -0800181 'mkdirs' => [ \&cmd_mkdirs ,
182 "recreate empty directories after a checkout",
183 { 'revision|r=i' => \$_revision } ],
Benoit Sigoure15153452007-10-16 16:36:50 +0200184 'propget' => [ \&cmd_propget,
185 'Print the value of a property on a file or directory',
186 { 'revision|r=i' => \$_revision } ],
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200187 'proplist' => [ \&cmd_proplist,
188 'List all properties of a file or directory',
189 { 'revision|r=i' => \$_revision } ],
Eric Wong5969cbe2007-01-11 17:58:39 -0800190 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200191 { 'revision|r=i' => \$_revision
Lars Hjemli05b4df32007-09-05 11:35:29 +0200192 } ],
Vineet Kumar2d879792007-11-19 14:56:15 -0800193 'show-externals' => [ \&cmd_show_externals, "Show svn:externals listings",
194 { 'revision|r=i' => \$_revision
195 } ],
Eric Wong1c8443b2007-01-14 02:17:00 -0800196 'multi-fetch' => [ \&cmd_multi_fetch,
Eric Wonge98671e2007-02-14 02:21:19 -0800197 "Deprecated alias for $0 fetch --all",
198 { 'revision|r=s' => \$_revision, %fc_opts } ],
Eric Wong706587f2007-01-18 17:50:01 -0800199 'migrate' => [ sub { },
200 # no-op, we automatically run this anyways,
Eric Wong706587f2007-01-18 17:50:01 -0800201 'Migrate configuration/metadata/layout from
202 previous versions of git-svn',
Eric Wonga836a0e2007-02-14 19:34:56 -0800203 { 'minimize' => \$Git::SVN::Migration::_minimize,
204 %remote_opts } ],
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800205 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
206 { 'limit=i' => \$Git::SVN::Log::limit,
Eric Wong79bb8d82006-06-01 02:35:44 -0700207 'revision|r=s' => \$_revision,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800208 'verbose|v' => \$Git::SVN::Log::verbose,
209 'incremental' => \$Git::SVN::Log::incremental,
210 'oneline' => \$Git::SVN::Log::oneline,
211 'show-commit' => \$Git::SVN::Log::show_commit,
212 'non-recursive' => \$Git::SVN::Log::non_recursive,
Eric Wong79bb8d82006-06-01 02:35:44 -0700213 'authors-file|A=s' => \$_authors,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800214 'color' => \$Git::SVN::Log::color,
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200215 'pager=s' => \$Git::SVN::Log::pager
Eric Wong79bb8d82006-06-01 02:35:44 -0700216 } ],
Eric Wong222566e2008-08-08 01:41:58 -0700217 'find-rev' => [ \&cmd_find_rev,
218 "Translate between SVN revision numbers and tree-ish",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200219 {} ],
Eric Wong905f8b72007-02-16 03:22:40 -0800220 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
221 { 'merge|m|M' => \$_merge,
222 'verbose|v' => \$_verbose,
223 'strategy|s=s' => \$_strategy,
Eric Wongdee41f32007-03-13 11:40:36 -0700224 'local|l' => \$_local,
Eric Wong905f8b72007-02-16 03:22:40 -0800225 'fetch-all|all' => \$_fetch_all,
Seth Falcon7d45e142008-05-19 20:29:17 -0700226 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800227 %fc_opts } ],
Eric Wong44320b92007-01-13 22:35:53 -0800228 'commit-diff' => [ \&cmd_commit_diff,
229 'Commit a diff between two trees',
Eric Wong27e9fb82006-06-27 19:39:12 -0700230 { 'message|m=s' => \$_message,
231 'file|F=s' => \$_file,
Eric Wong45bf4732006-11-09 01:19:37 -0800232 'revision|r=s' => \$_revision,
Eric Wong27e9fb82006-06-27 19:39:12 -0700233 %cmt_opts } ],
David D. Kilzere6fefa92007-11-21 11:57:18 -0800234 'info' => [ \&cmd_info,
235 "Show info about the latest SVN revision
236 on the current branch",
David D. Kilzer8b014d72007-11-21 11:57:19 -0800237 { 'url' => \$_url, } ],
Tim Stoakes6fb53752008-02-10 15:21:08 +1030238 'blame' => [ \&Git::SVN::Log::cmd_blame,
239 "Show what revision and author last modified each line of a file",
Steven Grimm4be40382008-05-10 22:11:18 -0700240 { 'git-format' => \$_git_format } ],
Ben Jackson195643f2009-06-03 20:45:52 -0700241 'reset' => [ \&cmd_reset,
242 "Undo fetches back to the specified SVN revision",
243 { 'revision|r=s' => \$_revision,
244 'parent|p' => \$_fetch_parent } ],
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -0500245 'gc' => [ \&cmd_gc,
246 "Compress unhandled.log files in .git/svn and remove " .
247 "index files in .git/svn",
248 {} ],
Eric Wong3397f9d2006-02-16 01:24:16 -0800249);
Eric Wong9d55b412006-06-12 15:53:13 -0700250
Eric Wong3397f9d2006-02-16 01:24:16 -0800251my $cmd;
252for (my $i = 0; $i < @ARGV; $i++) {
253 if (defined $cmd{$ARGV[$i]}) {
254 $cmd = $ARGV[$i];
255 splice @ARGV, $i, 1;
256 last;
Ben Jackson9a8c92a2009-05-30 18:17:06 -0700257 } elsif ($ARGV[$i] eq 'help') {
258 $cmd = $ARGV[$i+1];
259 usage(0);
Eric Wong3397f9d2006-02-16 01:24:16 -0800260 }
261};
262
Eric Wong540424b2007-12-19 00:31:43 -0800263# make sure we're always running at the top-level working directory
264unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
Eric Wong5253dc32007-02-20 01:36:30 -0800265 unless (-d $ENV{GIT_DIR}) {
266 if ($git_dir_user_set) {
267 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
268 "but it is not a directory\n";
269 }
270 my $git_dir = delete $ENV{GIT_DIR};
Deskin Millerfe4003f2008-11-06 00:07:39 -0500271 my $cdup = undef;
272 git_cmd_try {
273 $cdup = command_oneline(qw/rev-parse --show-cdup/);
274 $git_dir = '.' unless ($cdup);
275 chomp $cdup if ($cdup);
276 $cdup = "." unless ($cdup && length $cdup);
277 } "Already at toplevel, but $git_dir not found\n";
Eric Wong5253dc32007-02-20 01:36:30 -0800278 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
279 unless (-d $git_dir) {
280 die "$git_dir still not found after going to ",
281 "'$cdup'\n";
282 }
283 $ENV{GIT_DIR} = $git_dir;
284 }
Adam Robenffe256f2008-05-23 16:19:41 +0200285 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wong5253dc32007-02-20 01:36:30 -0800286}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100287
288my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
289
Eric Wong1a305822009-11-14 14:25:11 -0800290read_git_config(\%opts);
Eric Wong222566e2008-08-08 01:41:58 -0700291if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
292 Getopt::Long::Configure('pass_through');
293}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100294my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
295 'minimize-connections' => \$Git::SVN::Migration::_minimize,
296 'id|i=s' => \$Git::SVN::default_ref_id,
297 'svn-remote|remote|R=s' => sub {
298 $Git::SVN::no_reuse_existing = 1;
299 $Git::SVN::default_repo_id = $_[1] });
300exit 1 if (!$rv && $cmd && $cmd ne 'log');
301
302usage(0) if $_help;
303version() if $_version;
304usage(1) unless defined $cmd;
305load_authors() if $_authors;
Mark Lodato36db1ed2009-05-14 21:27:15 -0400306if (defined $_authors_prog) {
307 $_authors_prog = "'" . File::Spec->rel2abs($_authors_prog) . "'";
308}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100309
Eric Wong0425ea92007-02-16 18:45:01 -0800310unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
Eric Wong706587f2007-01-18 17:50:01 -0800311 Git::SVN::Migration::migration_check();
312}
Eric Wongecc712d2007-01-31 12:28:10 -0800313Git::SVN::init_vars();
Eric Wongb805b442007-01-22 13:52:04 -0800314eval {
315 Git::SVN::verify_remotes_sanity();
316 $cmd{$cmd}->[0]->(@ARGV);
317};
318fatal $@ if $@;
Eric Wong1e889ef2007-02-16 01:45:13 -0800319post_fetch_checkout();
Eric Wong3397f9d2006-02-16 01:24:16 -0800320exit 0;
321
322####################### primary functions ######################
323sub usage {
324 my $exit = shift || 0;
325 my $fd = $exit ? \*STDERR : \*STDOUT;
326 print $fd <<"";
327git-svn - bidirectional operations between a single Subversion tree and git
Stephan Beyer1b1dd232008-07-13 15:36:15 +0200328Usage: git svn <command> [options] [arguments]\n
Eric Wong448c81b2006-03-03 01:20:09 -0800329
330 print $fd "Available commands:\n" unless $cmd;
Eric Wong3397f9d2006-02-16 01:24:16 -0800331
332 foreach (sort keys %cmd) {
Eric Wong448c81b2006-03-03 01:20:09 -0800333 next if $cmd && $cmd ne $_;
Eric Wonga836a0e2007-02-14 19:34:56 -0800334 next if /^multi-/; # don't show deprecated commands
Eric Wongb203b762006-10-11 14:53:36 -0700335 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
Benoit Sigoureaa807bc2007-11-03 19:53:34 +0100336 foreach (sort keys %{$cmd{$_}->[2]}) {
Eric Wong512b6202007-04-03 01:57:08 -0700337 # mixed-case options are for .git/config only
338 next if /[A-Z]/ && /^[a-z]+$/i;
Eric Wong448c81b2006-03-03 01:20:09 -0800339 # prints out arguments as they should be passed:
Eric Wongb8c92ca2006-05-24 01:40:37 -0700340 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
Eric Wongb203b762006-10-11 14:53:36 -0700341 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
Eric Wong448c81b2006-03-03 01:20:09 -0800342 "--$_" : "-$_" }
343 split /\|/,$_)," $x\n";
344 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800345 }
346 print $fd <<"";
Eric Wong448c81b2006-03-03 01:20:09 -0800347\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
348arbitrary identifier if you're tracking multiple SVN branches/repositories in
349one git repository and want to keep them separate. See git-svn(1) for more
350information.
Eric Wong3397f9d2006-02-16 01:24:16 -0800351
352 exit $exit;
353}
354
Eric Wong551ce282006-02-20 10:57:29 -0800355sub version {
Michael J Gruberb0779242010-03-04 11:23:53 +0100356 ::_req_svn();
Eric Wong7d60ab22006-12-28 01:16:20 -0800357 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
Eric Wong551ce282006-02-20 10:57:29 -0800358 exit 0;
359}
360
Eric Wong8164b652007-01-11 15:35:55 -0800361sub do_git_init_db {
362 unless (-d $ENV{GIT_DIR}) {
363 my @init_db = ('init');
364 push @init_db, "--template=$_template" if defined $_template;
Eric Wongdadc6d22007-02-14 12:27:41 -0800365 if (defined $_shared) {
366 if ($_shared =~ /[a-z]/) {
367 push @init_db, "--shared=$_shared";
368 } else {
369 push @init_db, "--shared";
370 }
371 }
Eric Wong8164b652007-01-11 15:35:55 -0800372 command_noisy(@init_db);
Adam Robenffe256f2008-05-23 16:19:41 +0200373 $_repository = Git->repository(Repository => ".git");
Eric Wong8164b652007-01-11 15:35:55 -0800374 }
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800375 my $set;
376 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
377 foreach my $i (keys %icv) {
378 die "'$set' and '$i' cannot both be set\n" if $set;
379 next unless defined $icv{$i};
380 command_noisy('config', "$pfx.$i", $icv{$i});
381 $set = $i;
382 }
Ben Jackson88ec2052009-04-11 10:46:18 -0700383 my $ignore_regex = \$SVN::Git::Fetcher::_ignore_regex;
384 command_noisy('config', "$pfx.ignore-paths", $$ignore_regex)
385 if defined $$ignore_regex;
Eric Wong8164b652007-01-11 15:35:55 -0800386}
387
Eric Wongdadc6d22007-02-14 12:27:41 -0800388sub init_subdir {
389 my $repo_path = shift or return;
390 mkpath([$repo_path]) unless -d $repo_path;
391 chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
Eric Wongf30603f2007-02-23 01:26:26 -0800392 $ENV{GIT_DIR} = '.git';
Adam Robenffe256f2008-05-23 16:19:41 +0200393 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wongdadc6d22007-02-14 12:27:41 -0800394}
395
Eric Wong0425ea92007-02-16 18:45:01 -0800396sub cmd_clone {
397 my ($url, $path) = @_;
398 if (!defined $path &&
Marc Branchaud62244062009-06-23 13:02:08 -0400399 (defined $_trunk || @_branches || @_tags ||
martin f. krafft8f728fb2007-07-14 11:25:28 +0200400 defined $_stdlayout) &&
Eric Wong0425ea92007-02-16 18:45:01 -0800401 $url !~ m#^[a-z\+]+://#) {
402 $path = $url;
403 }
Eric Wong0425ea92007-02-16 18:45:01 -0800404 $path = basename($url) if !defined $path || !length $path;
Alex Vandiver2bc35dc2009-12-08 15:54:10 -0500405 my $authors_absolute = $_authors ? File::Spec->rel2abs($_authors) : "";
Eric Wongf30603f2007-02-23 01:26:26 -0800406 cmd_init($url, $path);
Alex Vandiver2bc35dc2009-12-08 15:54:10 -0500407 command_oneline('config', 'svn.authorsfile', $authors_absolute)
408 if $_authors;
Alex Vandiverf5841502009-12-08 15:54:11 -0500409 Git::SVN::fetch_all($Git::SVN::default_repo_id);
Eric Wong0425ea92007-02-16 18:45:01 -0800410}
411
Eric Wongd2866f92007-01-11 12:26:16 -0800412sub cmd_init {
martin f. krafft8f728fb2007-07-14 11:25:28 +0200413 if (defined $_stdlayout) {
414 $_trunk = 'trunk' if (!defined $_trunk);
Marc Branchaud62244062009-06-23 13:02:08 -0400415 @_tags = 'tags' if (! @_tags);
416 @_branches = 'branches' if (! @_branches);
martin f. krafft8f728fb2007-07-14 11:25:28 +0200417 }
Marc Branchaud62244062009-06-23 13:02:08 -0400418 if (defined $_trunk || @_branches || @_tags) {
Eric Wongdadc6d22007-02-14 12:27:41 -0800419 return cmd_multi_init(@_);
Eric Wong03e0ea82006-06-30 21:42:53 -0700420 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800421 my $url = shift or die "SVN repository location required ",
422 "as a command-line argument\n";
Ulrich Dangel50ff2362009-06-26 16:52:09 +0200423 $url = canonicalize_url($url);
Eric Wongdadc6d22007-02-14 12:27:41 -0800424 init_subdir(@_);
Eric Wong8164b652007-01-11 15:35:55 -0800425 do_git_init_db();
Eric Wong03e0ea82006-06-30 21:42:53 -0700426
Eric Wong6b488292009-07-25 00:00:50 -0700427 if ($Git::SVN::_minimize_url eq 'unset') {
428 $Git::SVN::_minimize_url = 0;
429 }
430
Eric Wong706587f2007-01-18 17:50:01 -0800431 Git::SVN->init($url);
Eric Wong3397f9d2006-02-16 01:24:16 -0800432}
433
Eric Wong2a3240b2007-01-04 18:09:56 -0800434sub cmd_fetch {
Eric Wonge98671e2007-02-14 02:21:19 -0800435 if (grep /^\d+=./, @_) {
436 die "'<rev>=<commit>' fetch arguments are ",
437 "no longer supported.\n";
Eric Wong07a1c952007-01-22 15:47:41 -0800438 }
Eric Wonge98671e2007-02-14 02:21:19 -0800439 my ($remote) = @_;
440 if (@_ > 1) {
Jason Merrillc2abd832009-04-06 16:37:59 -0400441 die "Usage: $0 fetch [--all] [--parent] [svn-remote]\n";
Eric Wonge98671e2007-02-14 02:21:19 -0800442 }
Eric Wong4d0157d2009-11-22 12:37:06 -0800443 $Git::SVN::no_reuse_existing = undef;
Jason Merrillc2abd832009-04-06 16:37:59 -0400444 if ($_fetch_parent) {
445 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
446 unless ($gs) {
447 die "Unable to determine upstream SVN information from ",
448 "working tree history\n";
449 }
450 # just fetch, don't checkout.
451 $_no_checkout = 'true';
452 $_fetch_all ? $gs->fetch_all : $gs->fetch;
453 } elsif ($_fetch_all) {
Eric Wonge98671e2007-02-14 02:21:19 -0800454 cmd_multi_fetch();
455 } else {
Jason Merrillc2abd832009-04-06 16:37:59 -0400456 $remote ||= $Git::SVN::default_repo_id;
Eric Wonge98671e2007-02-14 02:21:19 -0800457 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
Eric Wong1c8443b2007-01-14 02:17:00 -0800458 }
Eric Wong2a3240b2007-01-04 18:09:56 -0800459}
460
Eric Wong1ce255d2007-01-14 23:21:16 -0800461sub cmd_set_tree {
Eric Wong3397f9d2006-02-16 01:24:16 -0800462 my (@commits) = @_;
463 if ($_stdin || !@commits) {
464 print "Reading from stdin...\n";
465 @commits = ();
466 while (<STDIN>) {
Eric Wong1ca72ae2006-03-03 01:20:09 -0800467 if (/\b($sha1_short)\b/o) {
Eric Wong3397f9d2006-02-16 01:24:16 -0800468 unshift @commits, $1;
469 }
470 }
471 }
472 my @revs;
Eric Wong8de010a2006-02-20 10:57:26 -0800473 foreach my $c (@commits) {
Eric Wongaef4e922006-12-15 10:59:54 -0800474 my @tmp = command('rev-parse',$c);
Eric Wong8de010a2006-02-20 10:57:26 -0800475 if (scalar @tmp == 1) {
476 push @revs, $tmp[0];
477 } elsif (scalar @tmp > 1) {
Eric Wongaef4e922006-12-15 10:59:54 -0800478 push @revs, reverse(command('rev-list',@tmp));
Eric Wong8de010a2006-02-20 10:57:26 -0800479 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200480 fatal "Failed to rev-parse $c";
Eric Wong8de010a2006-02-20 10:57:26 -0800481 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800482 }
Eric Wong1ce255d2007-01-14 23:21:16 -0800483 my $gs = Git::SVN->new;
484 my ($r_last, $cmt_last) = $gs->last_rev_commit;
485 $gs->fetch;
Eric Wong97f69872007-01-25 11:53:13 -0800486 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
Eric Wong1ce255d2007-01-14 23:21:16 -0800487 fatal "There are new revisions that were fetched ",
488 "and need to be merged (or acknowledged) ",
489 "before committing.\nlast rev: $r_last\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200490 " current: $gs->{last_rev}";
Eric Wong1ce255d2007-01-14 23:21:16 -0800491 }
492 $gs->set_tree($_) foreach @revs;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700493 print "Done committing ",scalar @revs," revisions to SVN\n";
Eric Wong3157dd92007-12-13 08:27:34 -0800494 unlink $gs->{index};
Eric Wonga5e0ced2006-06-12 15:23:48 -0700495}
496
Eric Wongd7ad3be2007-01-14 03:14:28 -0800497sub cmd_dcommit {
498 my $head = shift;
David D. Kilzer181264a2010-08-02 12:58:19 -0700499 command_noisy(qw/update-index --refresh/);
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100500 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
David Reiss826a9332007-11-13 13:47:26 -0800501 'Cannot dcommit with a dirty index. Commit your changes first, '
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100502 . "or stash them with `git stash'.\n";
Eric Wongd7ad3be2007-01-14 03:14:28 -0800503 $head ||= 'HEAD';
Thomas Rast5eec27e2009-05-29 17:09:42 +0200504
505 my $old_head;
506 if ($head ne 'HEAD') {
507 $old_head = eval {
508 command_oneline([qw/symbolic-ref -q HEAD/])
509 };
510 if ($old_head) {
511 $old_head =~ s{^refs/heads/}{};
512 } else {
513 $old_head = eval { command_oneline(qw/rev-parse HEAD/) };
514 }
515 command(['checkout', $head], STDERR => 0);
516 }
517
Eric Wonga8ae2622007-02-13 14:22:11 -0800518 my @refs;
Thomas Rast5eec27e2009-05-29 17:09:42 +0200519 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD', \@refs);
Thomas Rast2cb61102008-08-31 15:50:59 +0200520 unless ($gs) {
521 die "Unable to determine upstream SVN information from ",
522 "$head history.\nPerhaps the repository is empty.";
523 }
Peter Oberndorfer0df84052009-02-23 12:02:53 +0100524
525 if (defined $_commit_url) {
526 $url = $_commit_url;
527 } else {
528 $url = eval { command_oneline('config', '--get',
529 "svn-remote.$gs->{repo_id}.commiturl") };
530 if (!$url) {
531 $url = $gs->full_url
532 }
533 }
534
Eric Wongba24e742008-08-07 02:06:16 -0700535 my $last_rev = $_revision if defined $_revision;
Matthieu Moy59b0c242008-04-24 20:06:36 +0200536 if ($url) {
537 print "Committing to $url ...\n";
538 }
Eric Wong733a65a2007-06-13 02:23:28 -0700539 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
Eric Wong751eb392007-08-31 18:16:12 -0700540 if ($_no_rebase && scalar(@$linear_refs) > 1) {
541 warn "Attempting to commit more than one change while ",
542 "--no-rebase is enabled.\n",
543 "If these changes depend on each other, re-running ",
Eric Wong7dfa16b2008-01-02 10:09:49 -0800544 "without --no-rebase may be required."
Eric Wong751eb392007-08-31 18:16:12 -0700545 }
Eric Wong711521e2008-08-20 00:30:06 -0700546 my $expect_url = $url;
547 Git::SVN::remove_username($expect_url);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800548 while (1) {
549 my $d = shift @$linear_refs or last;
Eric Wong45bf4732006-11-09 01:19:37 -0800550 unless (defined $last_rev) {
551 (undef, $last_rev, undef) = cmt_metadata("$d~1");
552 unless (defined $last_rev) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800553 fatal "Unable to extract revision information ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200554 "from commit $d~1";
Eric Wong45bf4732006-11-09 01:19:37 -0800555 }
556 }
Eric Wongb22d4492006-08-26 00:01:23 -0700557 if ($_dry_run) {
558 print "diff-tree $d~1 $d\n";
559 } else {
Eric Wong751eb392007-08-31 18:16:12 -0700560 my $cmt_rev;
Eric Wongd7ad3be2007-01-14 03:14:28 -0800561 my %ed_opts = ( r => $last_rev,
Eric Wong61395352007-01-27 14:33:08 -0800562 log => get_commit_entry($d)->{log},
Eric Wongba24e742008-08-07 02:06:16 -0700563 ra => Git::SVN::Ra->new($url),
Konstantin V. Arkhipov3caf3202007-11-14 03:52:02 +0300564 config => SVN::Core::config_get_config(
565 $Git::SVN::Ra::config_dir
566 ),
Eric Wong61395352007-01-27 14:33:08 -0800567 tree_a => "$d~1",
568 tree_b => $d,
569 editor_cb => sub {
570 print "Committed r$_[0]\n";
Eric Wong751eb392007-08-31 18:16:12 -0700571 $cmt_rev = $_[0];
572 },
Steven Walter6abd9332010-09-24 23:51:50 -0400573 mergeinfo => $_merge_info,
Eric Wonga8ae2622007-02-13 14:22:11 -0800574 svn_path => '');
Eric Wong61395352007-01-27 14:33:08 -0800575 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800576 print "No changes\n$d~1 == $d\n";
Eric Wong733a65a2007-06-13 02:23:28 -0700577 } elsif ($parents->{$d} && @{$parents->{$d}}) {
Eric Wong751eb392007-08-31 18:16:12 -0700578 $gs->{inject_parents_dcommit}->{$cmt_rev} =
Eric Wong733a65a2007-06-13 02:23:28 -0700579 $parents->{$d};
Eric Wongd7ad3be2007-01-14 03:14:28 -0800580 }
Eric Wong751eb392007-08-31 18:16:12 -0700581 $_fetch_all ? $gs->fetch_all : $gs->fetch;
Eric Wong7dfa16b2008-01-02 10:09:49 -0800582 $last_rev = $cmt_rev;
Eric Wong751eb392007-08-31 18:16:12 -0700583 next if $_no_rebase;
584
585 # we always want to rebase against the current HEAD,
586 # not any head that was passed to us
Eric Wongc74d9ac2007-11-05 03:21:47 -0800587 my @diff = command('diff-tree', $d,
Eric Wong751eb392007-08-31 18:16:12 -0700588 $gs->refname, '--');
589 my @finish;
590 if (@diff) {
591 @finish = rebase_cmd();
Eric Wongc74d9ac2007-11-05 03:21:47 -0800592 print STDERR "W: $d and ", $gs->refname,
Eric Wong751eb392007-08-31 18:16:12 -0700593 " differ, using @finish:\n",
Eric Wongc74d9ac2007-11-05 03:21:47 -0800594 join("\n", @diff), "\n";
Eric Wong751eb392007-08-31 18:16:12 -0700595 } else {
596 print "No changes between current HEAD and ",
597 $gs->refname,
598 "\nResetting to the latest ",
599 $gs->refname, "\n";
600 @finish = qw/reset --mixed/;
601 }
602 command_noisy(@finish, $gs->refname);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800603 if (@diff) {
604 @refs = ();
605 my ($url_, $rev_, $uuid_, $gs_) =
Thomas Rast5eec27e2009-05-29 17:09:42 +0200606 working_head_info('HEAD', \@refs);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800607 my ($linear_refs_, $parents_) =
608 linearize_history($gs_, \@refs);
609 if (scalar(@$linear_refs) !=
610 scalar(@$linear_refs_)) {
611 fatal "# of revisions changed ",
612 "\nbefore:\n",
613 join("\n", @$linear_refs),
614 "\n\nafter:\n",
615 join("\n", @$linear_refs_), "\n",
616 'If you are attempting to commit ',
617 "merges, try running:\n\t",
618 'git rebase --interactive',
619 '--preserve-merges ',
620 $gs->refname,
621 "\nBefore dcommitting";
622 }
Eric Wong711521e2008-08-20 00:30:06 -0700623 if ($url_ ne $expect_url) {
Alexander Gavrilovc03c1f72009-10-09 11:01:04 +0400624 if ($url_ eq $gs->metadata_url) {
625 print
626 "Accepting rewritten URL:",
627 " $url_\n";
628 } else {
629 fatal
630 "URL mismatch after rebase:",
631 " $url_ != $expect_url";
632 }
Eric Wongc74d9ac2007-11-05 03:21:47 -0800633 }
634 if ($uuid_ ne $uuid) {
635 fatal "uuid mismatch after rebase: ",
636 "$uuid_ != $uuid";
637 }
638 # remap parents
639 my (%p, @l, $i);
640 for ($i = 0; $i < scalar @$linear_refs; $i++) {
641 my $new = $linear_refs_->[$i] or next;
642 $p{$new} =
643 $parents->{$linear_refs->[$i]};
644 push @l, $new;
645 }
646 $parents = \%p;
647 $linear_refs = \@l;
648 }
Eric Wongb22d4492006-08-26 00:01:23 -0700649 }
650 }
Thomas Rast5eec27e2009-05-29 17:09:42 +0200651
652 if ($old_head) {
653 my $new_head = command_oneline(qw/rev-parse HEAD/);
654 my $new_is_symbolic = eval {
655 command_oneline(qw/symbolic-ref -q HEAD/);
656 };
657 if ($new_is_symbolic) {
658 print "dcommitted the branch ", $head, "\n";
659 } else {
660 print "dcommitted on a detached HEAD because you gave ",
661 "a revision argument.\n",
662 "The rewritten commit is: ", $new_head, "\n";
663 }
664 command(['checkout', $old_head], STDERR => 0);
665 }
666
Eric Wong3157dd92007-12-13 08:27:34 -0800667 unlink $gs->{index};
Eric Wongb22d4492006-08-26 00:01:23 -0700668}
669
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700670sub cmd_branch {
671 my ($branch_name, $head) = @_;
672
673 unless (defined $branch_name && length $branch_name) {
674 die(($_tag ? "tag" : "branch") . " name required\n");
675 }
676 $head ||= 'HEAD';
677
Eric Wong150d38c2009-12-22 22:40:18 -0800678 my (undef, $rev, undef, $gs) = working_head_info($head);
679 my $src = $gs->full_url;
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700680
Deskin Millera0fbc872008-12-01 21:43:00 -0500681 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
Marc Branchaud62244062009-06-23 13:02:08 -0400682 my $allglobs = $remote->{ $_tag ? 'tags' : 'branches' };
683 my $glob;
684 if ($#{$allglobs} == 0) {
685 $glob = $allglobs->[0];
686 } else {
687 unless(defined $_branch_dest) {
688 die "Multiple ",
689 $_tag ? "tag" : "branch",
690 " paths defined for Subversion repository.\n",
691 "You must specify where you want to create the ",
692 $_tag ? "tag" : "branch",
693 " with the --destination argument.\n";
694 }
695 foreach my $g (@{$allglobs}) {
Eric Wongf7050592009-06-25 02:28:15 -0700696 # SVN::Git::Editor could probably be moved to Git.pm..
697 my $re = SVN::Git::Editor::glob2pat($g->{path}->{left});
698 if ($_branch_dest =~ /$re/) {
Marc Branchaud62244062009-06-23 13:02:08 -0400699 $glob = $g;
700 last;
701 }
702 }
703 unless (defined $glob) {
Eric Wongeaa14ff2009-07-25 01:36:06 -0700704 my $dest_re = qr/\b\Q$_branch_dest\E\b/;
705 foreach my $g (@{$allglobs}) {
706 $g->{path}->{left} =~ /$dest_re/ or next;
707 if (defined $glob) {
708 die "Ambiguous destination: ",
709 $_branch_dest, "\nmatches both '",
710 $glob->{path}->{left}, "' and '",
711 $g->{path}->{left}, "'\n";
712 }
713 $glob = $g;
714 }
715 unless (defined $glob) {
716 die "Unknown ",
717 $_tag ? "tag" : "branch",
718 " destination $_branch_dest\n";
719 }
Marc Branchaud62244062009-06-23 13:02:08 -0400720 }
721 }
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700722 my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
Igor Mironov99bacd62010-01-12 03:21:23 +1100723 my $url;
724 if (defined $_commit_url) {
725 $url = $_commit_url;
726 } else {
727 $url = eval { command_oneline('config', '--get',
728 "svn-remote.$gs->{repo_id}.commiturl") };
729 if (!$url) {
730 $url = $remote->{url};
731 }
732 }
733 my $dst = join '/', $url, $lft, $branch_name, ($rgt || ());
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700734
Igor Mironova83b91e2010-01-12 03:20:43 +1100735 if ($dst =~ /^https:/ && $src =~ /^http:/) {
736 $src=~s/^http:/https:/;
737 }
738
josh robbd32fad22010-02-24 16:13:50 +1300739 ::_req_svn();
740
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700741 my $ctx = SVN::Client->new(
742 auth => Git::SVN::Ra::_auth_providers(),
743 log_msg => sub {
744 ${ $_[0] } = defined $_message
745 ? $_message
746 : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
747 . $branch_name;
748 },
749 );
750
751 eval {
752 $ctx->ls($dst, 'HEAD', 0);
753 } and die "branch ${branch_name} already exists\n";
754
755 print "Copying ${src} at r${rev} to ${dst}...\n";
756 $ctx->copy($src, $rev, $dst)
757 unless $_dry_run;
758
759 $gs->fetch_all;
760}
761
Adam Roben26e60162007-04-27 11:57:53 -0700762sub cmd_find_rev {
Marc-Andre Lureauea14e6c2008-03-11 10:00:45 +0200763 my $revision_or_hash = shift or die "SVN or git revision required ",
764 "as a command-line argument\n";
Adam Roben26e60162007-04-27 11:57:53 -0700765 my $result;
766 if ($revision_or_hash =~ /^r\d+$/) {
Adam Robenb3cb7e42007-04-29 01:35:27 -0700767 my $head = shift;
768 $head ||= 'HEAD';
769 my @refs;
João Abecasis63c56022008-07-14 16:28:04 +0100770 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
Adam Robenb3cb7e42007-04-29 01:35:27 -0700771 unless ($gs) {
772 die "Unable to determine upstream SVN information from ",
773 "$head history\n";
Adam Roben26e60162007-04-27 11:57:53 -0700774 }
Adam Robenb3cb7e42007-04-29 01:35:27 -0700775 my $desired_revision = substr($revision_or_hash, 1);
João Abecasis63c56022008-07-14 16:28:04 +0100776 $result = $gs->rev_map_get($desired_revision, $uuid);
Adam Roben26e60162007-04-27 11:57:53 -0700777 } else {
778 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
779 $result = $rev;
780 }
781 print "$result\n" if $result;
782}
783
Eric Wong905f8b72007-02-16 03:22:40 -0800784sub cmd_rebase {
785 command_noisy(qw/update-index --refresh/);
Eric Wong13c823f2007-04-08 00:59:19 -0700786 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
787 unless ($gs) {
Eric Wong905f8b72007-02-16 03:22:40 -0800788 die "Unable to determine upstream SVN information from ",
789 "working tree history\n";
790 }
Seth Falcon7d45e142008-05-19 20:29:17 -0700791 if ($_dry_run) {
792 print "Remote Branch: " . $gs->refname . "\n";
793 print "SVN URL: " . $url . "\n";
794 return;
795 }
Eric Wong905f8b72007-02-16 03:22:40 -0800796 if (command(qw/diff-index HEAD --/)) {
797 print STDERR "Cannot rebase with uncommited changes:\n";
798 command_noisy('status');
799 exit 1;
800 }
Eric Wongdee41f32007-03-13 11:40:36 -0700801 unless ($_local) {
Steven Grimmcec0d5a2007-11-29 11:54:39 -0800802 # rebase will checkout for us, so no need to do it explicitly
803 $_no_checkout = 'true';
Eric Wongdee41f32007-03-13 11:40:36 -0700804 $_fetch_all ? $gs->fetch_all : $gs->fetch;
805 }
Eric Wong905f8b72007-02-16 03:22:40 -0800806 command_noisy(rebase_cmd(), $gs->refname);
Eric Wong6111b932009-11-15 18:57:16 -0800807 $gs->mkemptydirs;
Eric Wong905f8b72007-02-16 03:22:40 -0800808}
809
Eric Wong5969cbe2007-01-11 17:58:39 -0800810sub cmd_show_ignore {
Eric Wong13c823f2007-04-08 00:59:19 -0700811 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
812 $gs ||= Git::SVN->new;
Eric Wong5969cbe2007-01-11 17:58:39 -0800813 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200814 $gs->prop_walk($gs->{path}, $r, sub {
815 my ($gs, $path, $props) = @_;
816 print STDOUT "\n# $path\n";
817 my $s = $props->{'svn:ignore'} or return;
818 $s =~ s/[\r\n]+/\n/g;
Michael Haggertya7d72542009-08-07 21:21:21 +0200819 $s =~ s/^\n+//;
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200820 chomp $s;
821 $s =~ s#^#$path#gm;
822 print STDOUT "$s\n";
823 });
Eric Wonga5e0ced2006-06-12 15:23:48 -0700824}
825
Vineet Kumar2d879792007-11-19 14:56:15 -0800826sub cmd_show_externals {
827 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
828 $gs ||= Git::SVN->new;
829 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
830 $gs->prop_walk($gs->{path}, $r, sub {
831 my ($gs, $path, $props) = @_;
832 print STDOUT "\n# $path\n";
833 my $s = $props->{'svn:externals'} or return;
834 $s =~ s/[\r\n]+/\n/g;
835 chomp $s;
836 $s =~ s#^#$path#gm;
837 print STDOUT "$s\n";
838 });
839}
840
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200841sub cmd_create_ignore {
842 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
843 $gs ||= Git::SVN->new;
844 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
845 $gs->prop_walk($gs->{path}, $r, sub {
846 my ($gs, $path, $props) = @_;
847 # $path is of the form /path/to/dir/
Brian Gernhardt7d9fd452009-02-19 13:08:04 -0500848 $path = '.' . $path;
849 # SVN can have attributes on empty directories,
850 # which git won't track
851 mkpath([$path]) unless -d $path;
852 my $ignore = $path . '.gitignore';
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200853 my $s = $props->{'svn:ignore'} or return;
854 open(GITIGNORE, '>', $ignore)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200855 or fatal("Failed to open `$ignore' for writing: $!");
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200856 $s =~ s/[\r\n]+/\n/g;
Michael Haggertya7d72542009-08-07 21:21:21 +0200857 $s =~ s/^\n+//;
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200858 chomp $s;
859 # Prefix all patterns so that the ignore doesn't apply
860 # to sub-directories.
861 $s =~ s#^#/#gm;
862 print GITIGNORE "$s\n";
863 close(GITIGNORE)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200864 or fatal("Failed to close `$ignore': $!");
Gustaf Hendebyc4c66b22008-05-05 00:33:09 +0200865 command_noisy('add', '-f', $ignore);
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200866 });
867}
868
Eric Wong6111b932009-11-15 18:57:16 -0800869sub cmd_mkdirs {
870 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
871 $gs ||= Git::SVN->new;
872 $gs->mkemptydirs($_revision);
873}
874
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800875sub canonicalize_path {
876 my ($path) = @_;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800877 my $dot_slash_added = 0;
878 if (substr($path, 0, 1) ne "/") {
879 $path = "./" . $path;
880 $dot_slash_added = 1;
881 }
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800882 # File::Spec->canonpath doesn't collapse x/../y into y (for a
883 # good reason), so let's do this manually.
884 $path =~ s#/+#/#g;
885 $path =~ s#/\.(?:/|$)#/#g;
886 $path =~ s#/[^/]+/\.\.##g;
887 $path =~ s#/$##g;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800888 $path =~ s#^\./## if $dot_slash_added;
Gerrit Pape2fe403e2008-07-06 19:28:50 +0000889 $path =~ s#^/##;
890 $path =~ s#^\.$##;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800891 return $path;
892}
893
Ulrich Dangel50ff2362009-06-26 16:52:09 +0200894sub canonicalize_url {
895 my ($url) = @_;
896 $url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
897 return $url;
898}
899
Benoit Sigoure15153452007-10-16 16:36:50 +0200900# get_svnprops(PATH)
901# ------------------
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200902# Helper for cmd_propget and cmd_proplist below.
Benoit Sigoure15153452007-10-16 16:36:50 +0200903sub get_svnprops {
904 my $path = shift;
905 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
906 $gs ||= Git::SVN->new;
907
908 # prefix THE PATH by the sub-directory from which the user
909 # invoked us.
910 $path = $cmd_dir_prefix . $path;
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200911 fatal("No such file or directory: $path") unless -e $path;
Benoit Sigoure15153452007-10-16 16:36:50 +0200912 my $is_dir = -d $path ? 1 : 0;
913 $path = $gs->{path} . '/' . $path;
914
915 # canonicalize the path (otherwise libsvn will abort or fail to
916 # find the file)
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800917 $path = canonicalize_path($path);
Benoit Sigoure15153452007-10-16 16:36:50 +0200918
919 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
920 my $props;
921 if ($is_dir) {
922 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
923 }
924 else {
925 (undef, $props) = $gs->ra->get_file($path, $r, undef);
926 }
927 return $props;
928}
929
930# cmd_propget (PROP, PATH)
931# ------------------------
932# Print the SVN property PROP for PATH.
933sub cmd_propget {
934 my ($prop, $path) = @_;
935 $path = '.' if not defined $path;
936 usage(1) if not defined $prop;
937 my $props = get_svnprops($path);
938 if (not defined $props->{$prop}) {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200939 fatal("`$path' does not have a `$prop' SVN property.");
Benoit Sigoure15153452007-10-16 16:36:50 +0200940 }
941 print $props->{$prop} . "\n";
942}
943
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200944# cmd_proplist (PATH)
945# -------------------
946# Print the list of SVN properties for PATH.
947sub cmd_proplist {
948 my $path = shift;
949 $path = '.' if not defined $path;
950 my $props = get_svnprops($path);
951 print "Properties on '$path':\n";
952 foreach (sort keys %{$props}) {
953 print " $_\n";
954 }
955}
956
Eric Wong8164b652007-01-11 15:35:55 -0800957sub cmd_multi_init {
Eric Wong9d55b412006-06-12 15:53:13 -0700958 my $url = shift;
Marc Branchaud62244062009-06-23 13:02:08 -0400959 unless (defined $_trunk || @_branches || @_tags) {
Eric Wong98327e52007-01-04 18:02:00 -0800960 usage(1);
961 }
Eric Wongdc431662007-05-19 03:59:02 -0700962
Eric Wong8164b652007-01-11 15:35:55 -0800963 $_prefix = '' unless defined $_prefix;
Eric Wongdadc6d22007-02-14 12:27:41 -0800964 if (defined $url) {
Ulrich Dangel50ff2362009-06-26 16:52:09 +0200965 $url = canonicalize_url($url);
Eric Wongdadc6d22007-02-14 12:27:41 -0800966 init_subdir(@_);
967 }
Eric Wongf30603f2007-02-23 01:26:26 -0800968 do_git_init_db();
Eric Wong98327e52007-01-04 18:02:00 -0800969 if (defined $_trunk) {
Jonathan Niederb4b33602010-06-13 06:27:43 -0500970 $_trunk =~ s#^/+##;
Adam Brewster6f5748e2009-08-11 23:14:27 -0400971 my $trunk_ref = 'refs/remotes/' . $_prefix . 'trunk';
Eric Wong706587f2007-01-18 17:50:01 -0800972 # try both old-style and new-style lookups:
973 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
Eric Wong8164b652007-01-11 15:35:55 -0800974 unless ($gs_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800975 my ($trunk_url, $trunk_path) =
976 complete_svn_url($url, $_trunk);
977 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
978 undef, $trunk_ref);
Eric Wong98327e52007-01-04 18:02:00 -0800979 }
Eric Wongc35b96e2006-10-11 11:53:21 -0700980 }
Marc Branchaud62244062009-06-23 13:02:08 -0400981 return unless @_branches || @_tags;
Eric Wonge7db67e2007-01-11 17:09:26 -0800982 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
Marc Branchaud62244062009-06-23 13:02:08 -0400983 foreach my $path (@_branches) {
984 complete_url_ls_init($ra, $path, '--branches/-b', $_prefix);
985 }
986 foreach my $path (@_tags) {
987 complete_url_ls_init($ra, $path, '--tags/-t', $_prefix.'tags/');
988 }
Eric Wong9d55b412006-06-12 15:53:13 -0700989}
990
Eric Wong1c8443b2007-01-14 02:17:00 -0800991sub cmd_multi_fetch {
Eric Wong4d0157d2009-11-22 12:37:06 -0800992 $Git::SVN::no_reuse_existing = undef;
Eric Wong0af9c9f2007-01-27 22:28:56 -0800993 my $remotes = Git::SVN::read_all_remotes();
994 foreach my $repo_id (sort keys %$remotes) {
Eric Wongdb03cd22007-02-13 00:38:02 -0800995 if ($remotes->{$repo_id}->{url}) {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800996 Git::SVN::fetch_all($repo_id, $remotes);
997 }
Eric Wong706587f2007-01-18 17:50:01 -0800998 }
Eric Wong9d55b412006-06-12 15:53:13 -0700999}
1000
Eric Wong44320b92007-01-13 22:35:53 -08001001# this command is special because it requires no metadata
1002sub cmd_commit_diff {
1003 my ($ta, $tb, $url) = @_;
1004 my $usage = "Usage: $0 commit-diff -r<revision> ".
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001005 "<tree-ish> <tree-ish> [<URL>]";
Eric Wong44320b92007-01-13 22:35:53 -08001006 fatal($usage) if (!defined $ta || !defined $tb);
Karl Hasselströmd72ab8c2008-05-17 17:07:09 +02001007 my $svn_path = '';
Eric Wong44320b92007-01-13 22:35:53 -08001008 if (!defined $url) {
1009 my $gs = eval { Git::SVN->new };
1010 if (!$gs) {
1011 fatal("Needed URL or usable git-svn --id in ",
1012 "the command-line\n", $usage);
1013 }
1014 $url = $gs->{url};
Eric Wongd3a840d2007-01-26 01:32:45 -08001015 $svn_path = $gs->{path};
Eric Wong44320b92007-01-13 22:35:53 -08001016 }
1017 unless (defined $_revision) {
1018 fatal("-r|--revision is a required argument\n", $usage);
1019 }
1020 if (defined $_message && defined $_file) {
1021 fatal("Both --message/-m and --file/-F specified ",
1022 "for the commit message.\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001023 "I have no idea what you mean");
Eric Wong44320b92007-01-13 22:35:53 -08001024 }
1025 if (defined $_file) {
1026 $_message = file_to_s($_file);
1027 } else {
1028 $_message ||= get_commit_entry($tb)->{log};
1029 }
1030 my $ra ||= Git::SVN::Ra->new($url);
1031 my $r = $_revision;
1032 if ($r eq 'HEAD') {
1033 $r = $ra->get_latest_revnum;
1034 } elsif ($r !~ /^\d+$/) {
1035 die "revision argument: $r not understood by git-svn\n";
1036 }
Eric Wong61395352007-01-27 14:33:08 -08001037 my %ed_opts = ( r => $r,
1038 log => $_message,
1039 ra => $ra,
1040 tree_a => $ta,
1041 tree_b => $tb,
1042 editor_cb => sub { print "Committed r$_[0]\n" },
1043 svn_path => $svn_path );
1044 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong44320b92007-01-13 22:35:53 -08001045 print "No changes\n$ta == $tb\n";
1046 }
Eric Wong44320b92007-01-13 22:35:53 -08001047}
1048
Thomas Rast05427b92008-08-26 21:32:37 +02001049sub escape_uri_only {
1050 my ($uri) = @_;
1051 my @tmp;
1052 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -07001053 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Thomas Rast05427b92008-08-26 21:32:37 +02001054 push @tmp, $_;
1055 }
1056 join('/', @tmp);
1057}
1058
1059sub escape_url {
1060 my ($url) = @_;
1061 if ($url =~ m#^([^:]+)://([^/]*)(.*)$#) {
1062 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
1063 $url = "$scheme://$domain$uri";
1064 }
1065 $url;
1066}
1067
David D. Kilzere6fefa92007-11-21 11:57:18 -08001068sub cmd_info {
Eric Wongbd2d4f92008-08-05 00:35:16 -07001069 my $path = canonicalize_path(defined($_[0]) ? $_[0] : ".");
Thomas Rastedde9112008-08-26 21:32:36 +02001070 my $fullpath = canonicalize_path($cmd_dir_prefix . $path);
Eric Wongbd2d4f92008-08-05 00:35:16 -07001071 if (exists $_[1]) {
David D. Kilzere6fefa92007-11-21 11:57:18 -08001072 die "Too many arguments specified\n";
1073 }
1074
1075 my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
1076
1077 if (!$file_type && !$diff_status) {
Thomas Rast2cf3e3a2008-08-29 15:42:48 +02001078 print STDERR "svn: '$path' is not under version control\n";
1079 exit 1;
David D. Kilzere6fefa92007-11-21 11:57:18 -08001080 }
1081
1082 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1083 unless ($gs) {
1084 die "Unable to determine upstream SVN information from ",
1085 "working tree history\n";
1086 }
Eric Wongbd2d4f92008-08-05 00:35:16 -07001087
1088 # canonicalize_path() will return "" to make libsvn 1.5.x happy,
1089 $path = "." if $path eq "";
1090
Thomas Rastedde9112008-08-26 21:32:36 +02001091 my $full_url = $url . ($fullpath eq "" ? "" : "/$fullpath");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001092
David D. Kilzer8b014d72007-11-21 11:57:19 -08001093 if ($_url) {
Thomas Rast05427b92008-08-26 21:32:37 +02001094 print escape_url($full_url), "\n";
David D. Kilzer8b014d72007-11-21 11:57:19 -08001095 return;
1096 }
1097
David D. Kilzere6fefa92007-11-21 11:57:18 -08001098 my $result = "Path: $path\n";
1099 $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
Thomas Rast05427b92008-08-26 21:32:37 +02001100 $result .= "URL: " . escape_url($full_url) . "\n";
David D. Kilzere6fefa92007-11-21 11:57:18 -08001101
Eric Wonga5460eb2007-11-21 18:20:57 -08001102 eval {
1103 my $repos_root = $gs->repos_root;
1104 Git::SVN::remove_username($repos_root);
Thomas Rast05427b92008-08-26 21:32:37 +02001105 $result .= "Repository Root: " . escape_url($repos_root) . "\n";
Eric Wonga5460eb2007-11-21 18:20:57 -08001106 };
1107 if ($@) {
1108 $result .= "Repository Root: (offline)\n";
1109 }
Michael J Gruberb91a8a32010-03-03 21:34:31 +01001110 ::_req_svn();
Marcel Koeppen22ba47f2009-01-19 03:02:01 +01001111 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" &&
1112 ($SVN::Core::VERSION le '1.5.4' || $file_type ne "dir");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001113 $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
1114
1115 $result .= "Node Kind: " .
1116 ($file_type eq "dir" ? "directory" : "file") . "\n";
1117
1118 my $schedule = $diff_status eq "A"
1119 ? "add"
1120 : ($diff_status eq "D" ? "delete" : "normal");
1121 $result .= "Schedule: $schedule\n";
1122
1123 if ($diff_status eq "A") {
1124 print $result, "\n";
1125 return;
1126 }
1127
1128 my ($lc_author, $lc_rev, $lc_date_utc);
Thomas Rastedde9112008-08-26 21:32:36 +02001129 my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001130 my $log = command_output_pipe(@args);
1131 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
1132 while (<$log>) {
1133 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
1134 $lc_author = $1;
1135 $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
1136 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
1137 (undef, $lc_rev, undef) = ::extract_metadata($1);
1138 }
1139 }
1140 close $log;
1141
1142 Git::SVN::Log::set_local_timezone();
1143
1144 $result .= "Last Changed Author: $lc_author\n";
1145 $result .= "Last Changed Rev: $lc_rev\n";
1146 $result .= "Last Changed Date: " .
1147 Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
1148
1149 if ($file_type ne "dir") {
1150 my $text_last_updated_date =
1151 ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
1152 $result .=
1153 "Text Last Updated: " .
1154 Git::SVN::Log::format_svn_date($text_last_updated_date) .
1155 "\n";
1156 my $checksum;
1157 if ($diff_status eq "D") {
1158 my ($fh, $ctx) =
1159 command_output_pipe(qw(cat-file blob), "HEAD:$path");
1160 if ($file_type eq "link") {
1161 my $file_name = <$fh>;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001162 $checksum = md5sum("link $file_name");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001163 } else {
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001164 $checksum = md5sum($fh);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001165 }
1166 command_close_pipe($fh, $ctx);
1167 } elsif ($file_type eq "link") {
1168 my $file_name =
1169 command(qw(cat-file blob), "HEAD:$path");
1170 $checksum =
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001171 md5sum("link " . $file_name);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001172 } else {
1173 open FILE, "<", $path or die $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001174 $checksum = md5sum(\*FILE);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001175 close FILE or die $!;
1176 }
1177 $result .= "Checksum: " . $checksum . "\n";
1178 }
1179
1180 print $result, "\n";
1181}
1182
Ben Jackson195643f2009-06-03 20:45:52 -07001183sub cmd_reset {
1184 my $target = shift || $_revision or die "SVN revision required\n";
1185 $target = $1 if $target =~ /^r(\d+)$/;
1186 $target =~ /^\d+$/ or die "Numeric SVN revision expected\n";
1187 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1188 unless ($gs) {
1189 die "Unable to determine upstream SVN information from ".
1190 "history\n";
1191 }
1192 my ($r, $c) = $gs->find_rev_before($target, not $_fetch_parent);
Jonathan Nieder70ee0b72010-05-04 16:36:47 -07001193 die "Cannot find SVN revision $target\n" unless defined($c);
Ben Jackson195643f2009-06-03 20:45:52 -07001194 $gs->rev_map_set($r, $c, 'reset', $uuid);
1195 print "r$r = $c ($gs->{ref_id})\n";
1196}
1197
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -05001198sub cmd_gc {
1199 if (!$can_compress) {
1200 warn "Compress::Zlib could not be found; unhandled.log " .
1201 "files will not be compressed.\n";
1202 }
1203 find({ wanted => \&gc_directory, no_chdir => 1}, "$ENV{GIT_DIR}/svn");
1204}
1205
Eric Wong3397f9d2006-02-16 01:24:16 -08001206########################### utility functions #########################
1207
Eric Wong905f8b72007-02-16 03:22:40 -08001208sub rebase_cmd {
1209 my @cmd = qw/rebase/;
1210 push @cmd, '-v' if $_verbose;
1211 push @cmd, qw/--merge/ if $_merge;
1212 push @cmd, "--strategy=$_strategy" if $_strategy;
1213 @cmd;
1214}
1215
Eric Wong1e889ef2007-02-16 01:45:13 -08001216sub post_fetch_checkout {
1217 return if $_no_checkout;
1218 my $gs = $Git::SVN::_head or return;
1219 return if verify_ref('refs/heads/master^0');
1220
Eric Wongb186a262009-08-12 16:01:59 -07001221 # look for "trunk" ref if it exists
1222 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
1223 my $fetch = $remote->{fetch};
1224 if ($fetch) {
1225 foreach my $p (keys %$fetch) {
1226 basename($fetch->{$p}) eq 'trunk' or next;
1227 $gs = Git::SVN->new($fetch->{$p}, $gs->{repo_id}, $p);
1228 last;
1229 }
1230 }
1231
Eric Wong1e889ef2007-02-16 01:45:13 -08001232 my $valid_head = verify_ref('HEAD^0');
1233 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
1234 return if ($valid_head || !verify_ref('HEAD^0'));
1235
1236 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
1237 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
1238 return if -f $index;
1239
Matthias Lederhofer7ae3df82007-06-03 16:48:16 +02001240 return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
Eric Wong1e889ef2007-02-16 01:45:13 -08001241 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
1242 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
1243 print STDERR "Checked out HEAD:\n ",
1244 $gs->full_url, " r", $gs->last_rev, "\n";
Eric Wong6111b932009-11-15 18:57:16 -08001245 $gs->mkemptydirs($gs->last_rev);
Eric Wong1e889ef2007-02-16 01:45:13 -08001246}
1247
Eric Wong98327e52007-01-04 18:02:00 -08001248sub complete_svn_url {
1249 my ($url, $path) = @_;
1250 $path =~ s#/+$##;
Eric Wong98327e52007-01-04 18:02:00 -08001251 if ($path !~ m#^[a-z\+]+://#) {
Eric Wong98327e52007-01-04 18:02:00 -08001252 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
1253 fatal("E: '$path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001254 "and a separate URL is not specified");
Eric Wong98327e52007-01-04 18:02:00 -08001255 }
Eric Wong706587f2007-01-18 17:50:01 -08001256 return ($url, $path);
Eric Wong98327e52007-01-04 18:02:00 -08001257 }
Eric Wong706587f2007-01-18 17:50:01 -08001258 return ($path, '');
Eric Wong98327e52007-01-04 18:02:00 -08001259}
1260
Eric Wong9d55b412006-06-12 15:53:13 -07001261sub complete_url_ls_init {
Eric Wong706587f2007-01-18 17:50:01 -08001262 my ($ra, $repo_path, $switch, $pfx) = @_;
1263 unless ($repo_path) {
Eric Wong9d55b412006-06-12 15:53:13 -07001264 print STDERR "W: $switch not specified\n";
1265 return;
1266 }
Eric Wong706587f2007-01-18 17:50:01 -08001267 $repo_path =~ s#/+$##;
1268 if ($repo_path =~ m#^[a-z\+]+://#) {
1269 $ra = Git::SVN::Ra->new($repo_path);
1270 $repo_path = '';
Eric Wonge7db67e2007-01-11 17:09:26 -08001271 } else {
Eric Wong706587f2007-01-18 17:50:01 -08001272 $repo_path =~ s#^/+##;
Eric Wonge7db67e2007-01-11 17:09:26 -08001273 unless ($ra) {
Eric Wong706587f2007-01-18 17:50:01 -08001274 fatal("E: '$repo_path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001275 "and a separate URL is not specified");
Eric Wong9d55b412006-06-12 15:53:13 -07001276 }
Eric Wonge7db67e2007-01-11 17:09:26 -08001277 }
Eric Wong706587f2007-01-18 17:50:01 -08001278 my $url = $ra->{url};
Eric Wongb4d57e52007-02-14 15:10:44 -08001279 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
1280 my $k = "svn-remote.$gs->{repo_id}.url";
1281 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
1282 if ($orig_url && ($orig_url ne $gs->{url})) {
1283 die "$k already set: $orig_url\n",
1284 "wanted to set to: $gs->{url}\n";
Eric Wong88cf4102007-02-01 03:59:07 -08001285 }
Eric Wongb4d57e52007-02-14 15:10:44 -08001286 command_oneline('config', $k, $gs->{url}) unless $orig_url;
Mattias Nissler0b2af452009-07-07 01:40:02 +02001287 my $remote_path = "$gs->{path}/$repo_path";
Eric Wong5268f9e2009-08-16 14:22:12 -07001288 $remote_path =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
Eric Wongb4d57e52007-02-14 15:10:44 -08001289 $remote_path =~ s#/+#/#g;
1290 $remote_path =~ s#^/##g;
Eric Wonged0b9d42008-03-14 11:01:23 -07001291 $remote_path .= "/*" if $remote_path !~ /\*/;
Eric Wongb4d57e52007-02-14 15:10:44 -08001292 my ($n) = ($switch =~ /^--(\w+)/);
1293 if (length $pfx && $pfx !~ m#/$#) {
1294 die "--prefix='$pfx' must have a trailing slash '/'\n";
Eric Wong9d55b412006-06-12 15:53:13 -07001295 }
Marcus Griep570d35c2008-08-08 01:41:57 -07001296 command_noisy('config',
Marc Branchaud62244062009-06-23 13:02:08 -04001297 '--add',
Marcus Griep570d35c2008-08-08 01:41:57 -07001298 "svn-remote.$gs->{repo_id}.$n",
1299 "$remote_path:refs/remotes/$pfx*" .
1300 ('/*' x (($remote_path =~ tr/*/*/) - 1)) );
Eric Wong9d55b412006-06-12 15:53:13 -07001301}
1302
Eric Wongaef4e922006-12-15 10:59:54 -08001303sub verify_ref {
1304 my ($ref) = @_;
Eric Wong2c5c1d52006-12-28 01:16:21 -08001305 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
1306 { STDERR => 0 }); };
Eric Wongaef4e922006-12-15 10:59:54 -08001307}
1308
Eric Wonga5e0ced2006-06-12 15:23:48 -07001309sub get_tree_from_treeish {
Eric Wongcf52b8f2006-02-20 10:57:28 -08001310 my ($treeish) = @_;
Eric Wong44320b92007-01-13 22:35:53 -08001311 # $treeish can be a symbolic ref, too:
Eric Wongaef4e922006-12-15 10:59:54 -08001312 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001313 my $expected;
1314 while ($type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001315 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001316 }
1317 if ($type eq 'commit') {
Eric Wongaef4e922006-12-15 10:59:54 -08001318 $expected = (grep /^tree /, command(qw/cat-file commit/,
1319 $treeish))[0];
Eric Wong44320b92007-01-13 22:35:53 -08001320 ($expected) = ($expected =~ /^tree ($sha1)$/o);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001321 die "Unable to get tree from $treeish\n" unless $expected;
1322 } elsif ($type eq 'tree') {
1323 $expected = $treeish;
1324 } else {
1325 die "$treeish is a $type, expected tree, tag or commit\n";
1326 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07001327 return $expected;
1328}
Eric Wongcf52b8f2006-02-20 10:57:28 -08001329
Eric Wong44320b92007-01-13 22:35:53 -08001330sub get_commit_entry {
1331 my ($treeish) = shift;
1332 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
1333 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1334 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1335 open my $log_fh, '>', $commit_editmsg or croak $!;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001336
Eric Wong44320b92007-01-13 22:35:53 -08001337 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wong4ad45152006-07-09 20:20:48 -07001338 if ($type eq 'commit' || $type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001339 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
Eric Wong44320b92007-01-13 22:35:53 -08001340 $type, $treeish);
Eric Wong3397f9d2006-02-16 01:24:16 -08001341 my $in_msg = 0;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001342 my $author;
1343 my $saw_from = 0;
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001344 my $msgbuf = "";
Eric Wong3397f9d2006-02-16 01:24:16 -08001345 while (<$msg_fh>) {
1346 if (!$in_msg) {
1347 $in_msg = 1 if (/^\s*$/);
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001348 $author = $1 if (/^author (.*>)/);
Eric Wongdf746c52006-03-03 01:20:08 -08001349 } elsif (/^git-svn-id: /) {
Eric Wong44320b92007-01-13 22:35:53 -08001350 # skip this for now, we regenerate the
1351 # correct one on re-fetch anyways
1352 # TODO: set *:merge properties or like...
Eric Wong3397f9d2006-02-16 01:24:16 -08001353 } else {
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001354 if (/^From:/ || /^Signed-off-by:/) {
1355 $saw_from = 1;
1356 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001357 $msgbuf .= $_;
Eric Wong3397f9d2006-02-16 01:24:16 -08001358 }
1359 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001360 $msgbuf =~ s/\s+$//s;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001361 if ($Git::SVN::_add_author_from && defined($author)
1362 && !$saw_from) {
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001363 $msgbuf .= "\n\nFrom: $author";
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001364 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001365 print $log_fh $msgbuf or croak $!;
Eric Wongaef4e922006-12-15 10:59:54 -08001366 command_close_pipe($msg_fh, $ctx);
Eric Wong3397f9d2006-02-16 01:24:16 -08001367 }
Eric Wong44320b92007-01-13 22:35:53 -08001368 close $log_fh or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -08001369
1370 if ($_edit || ($type eq 'tree')) {
Jonathan Niederb4479f02009-10-30 20:42:34 -05001371 chomp(my $editor = command_oneline(qw(var GIT_EDITOR)));
1372 system('sh', '-c', $editor.' "$@"', $editor, $commit_editmsg);
Eric Wong3397f9d2006-02-16 01:24:16 -08001373 }
Eric Wong44320b92007-01-13 22:35:53 -08001374 rename $commit_editmsg, $commit_msg or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07001375 {
Eric Wongb510df82009-05-28 00:56:23 -07001376 require Encode;
Eric Wong16fc08e2008-10-29 23:49:26 -07001377 # SVN requires messages to be UTF-8 when entering the repo
1378 local $/;
1379 open $log_fh, '<', $commit_msg or croak $!;
1380 binmode $log_fh;
1381 chomp($log_entry{log} = <$log_fh>);
1382
Eric Wongb510df82009-05-28 00:56:23 -07001383 my $enc = Git::config('i18n.commitencoding') || 'UTF-8';
1384 my $msg = $log_entry{log};
1385
1386 eval { $msg = Encode::decode($enc, $msg, 1) };
1387 if ($@) {
1388 die "Could not decode as $enc:\n", $msg,
1389 "\nPerhaps you need to set i18n.commitencoding\n";
Eric Wong16fc08e2008-10-29 23:49:26 -07001390 }
Eric Wongb510df82009-05-28 00:56:23 -07001391
1392 eval { $msg = Encode::encode('UTF-8', $msg, 1) };
1393 die "Could not encode as UTF-8:\n$msg\n" if $@;
1394
1395 $log_entry{log} = $msg;
1396
Eric Wong16fc08e2008-10-29 23:49:26 -07001397 close $log_fh or croak $!;
1398 }
Eric Wong44320b92007-01-13 22:35:53 -08001399 unlink $commit_msg;
1400 \%log_entry;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001401}
1402
Eric Wong3397f9d2006-02-16 01:24:16 -08001403sub s_to_file {
1404 my ($str, $file, $mode) = @_;
1405 open my $fd,'>',$file or croak $!;
1406 print $fd $str,"\n" or croak $!;
1407 close $fd or croak $!;
1408 chmod ($mode &~ umask, $file) if (defined $mode);
1409}
1410
1411sub file_to_s {
1412 my $file = shift;
1413 open my $fd,'<',$file or croak "$!: file: $file\n";
1414 local $/;
1415 my $ret = <$fd>;
1416 close $fd or croak $!;
1417 $ret =~ s/\s*$//s;
1418 return $ret;
1419}
1420
Eric Wongeeb0abe2006-03-03 01:20:08 -08001421# '<svn username> = real-name <email address>' mapping based on git-svnimport:
1422sub load_authors {
1423 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001424 my $log = $cmd eq 'log';
Eric Wongeeb0abe2006-03-03 01:20:08 -08001425 while (<$authors>) {
1426 chomp;
Richard MUSIL575d0252007-07-17 19:02:57 +02001427 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
Eric Wongeeb0abe2006-03-03 01:20:08 -08001428 my ($user, $name, $email) = ($1, $2, $3);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001429 if ($log) {
1430 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
1431 } else {
1432 $users{$user} = [$name, $email];
1433 }
Eric Wong79bb8d82006-06-01 02:35:44 -07001434 }
1435 close $authors or croak $!;
1436}
1437
Tom Princee0d10e12007-01-28 16:16:53 -08001438# convert GetOpt::Long specs for use by git-config
Eric Wong1a305822009-11-14 14:25:11 -08001439sub read_git_config {
Eric Wongb8c92ca2006-05-24 01:40:37 -07001440 my $opts = shift;
Eric Wong97ae0912007-02-11 15:21:24 -08001441 my @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001442 foreach my $o (keys %$opts) {
Eric Wong97ae0912007-02-11 15:21:24 -08001443 # if we have mixedCase and a long option-only, then
1444 # it's a config-only variable that we don't need for
1445 # the command-line.
1446 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001447 my $v = $opts->{$o};
Eric Wong97ae0912007-02-11 15:21:24 -08001448 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001449 $key =~ s/-//g;
Deskin Miller225f1d02008-10-23 15:21:34 -04001450 my $arg = 'git config';
Eric Wongb8c92ca2006-05-24 01:40:37 -07001451 $arg .= ' --int' if ($o =~ /[:=]i$/);
1452 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1453 if (ref $v eq 'ARRAY') {
1454 chomp(my @tmp = `$arg --get-all svn.$key`);
1455 @$v = @tmp if @tmp;
1456 } else {
1457 chomp(my $tmp = `$arg --get svn.$key`);
Eric Wong77742842007-02-10 21:07:12 -08001458 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
Eric Wongb8c92ca2006-05-24 01:40:37 -07001459 $$v = $tmp;
1460 }
1461 }
1462 }
Eric Wong97ae0912007-02-11 15:21:24 -08001463 delete @$opts{@config_only} if @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001464}
1465
Eric Wong79bb8d82006-06-01 02:35:44 -07001466sub extract_metadata {
Eric Wongc1927a82006-06-27 19:39:11 -07001467 my $id = shift or return (undef, undef, undef);
Sam Vilain3dfab992007-06-30 20:56:13 +12001468 my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
Eric Wongb3e95932009-07-11 14:13:12 -07001469 \s([a-f\d\-]+)$/ix);
Eric Wonge70dc782006-11-23 14:54:04 -08001470 if (!defined $rev || !$uuid || !$url) {
Eric Wong79bb8d82006-06-01 02:35:44 -07001471 # some of the original repositories I made had
Pavel Roskin82e5a822006-07-10 01:50:18 -04001472 # identifiers like this:
Eric Wongb3e95932009-07-11 14:13:12 -07001473 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/i);
Eric Wong79bb8d82006-06-01 02:35:44 -07001474 }
1475 return ($url, $rev, $uuid);
1476}
1477
Eric Wongc1927a82006-06-27 19:39:11 -07001478sub cmt_metadata {
1479 return extract_metadata((grep(/^git-svn-id: /,
Eric Wongaef4e922006-12-15 10:59:54 -08001480 command(qw/cat-file commit/, shift)))[-1]);
Eric Wongc1927a82006-06-27 19:39:11 -07001481}
1482
Boris Byk6ea42032009-04-11 00:32:41 +04001483sub cmt_sha2rev_batch {
1484 my %s2r;
1485 my ($pid, $in, $out, $ctx) = command_bidi_pipe(qw/cat-file --batch/);
1486 my $list = shift;
1487
1488 foreach my $sha (@{$list}) {
1489 my $first = 1;
1490 my $size = 0;
1491 print $out $sha, "\n";
1492
1493 while (my $line = <$in>) {
1494 if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) {
1495 last;
1496 } elsif ($first &&
1497 $line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) {
1498 $first = 0;
1499 $size = $1;
1500 next;
1501 } elsif ($line =~ /^(git-svn-id: )/) {
1502 my (undef, $rev, undef) =
1503 extract_metadata($line);
1504 $s2r{$sha} = $rev;
1505 }
1506
1507 $size -= length($line);
1508 last if ($size == 0);
1509 }
1510 }
1511
1512 command_close_bidi_pipe($pid, $in, $out, $ctx);
1513
1514 return \%s2r;
1515}
1516
Eric Wong905f8b72007-02-16 03:22:40 -08001517sub working_head_info {
1518 my ($head, $refs) = @_;
Mathias Lafeldt8565a562010-09-24 00:05:03 +02001519 my @args = qw/log --no-color --no-decorate --first-parent
1520 --pretty=medium/;
Lars Hjemli05b4df32007-09-05 11:35:29 +02001521 my ($fh, $ctx) = command_output_pipe(@args, $head);
Sam Vilain3dfab992007-06-30 20:56:13 +12001522 my $hash;
Sam Vilain40cb8f82007-06-30 20:56:14 +12001523 my %max;
Sam Vilain3dfab992007-06-30 20:56:13 +12001524 while (<$fh>) {
1525 if ( m{^commit ($::sha1)$} ) {
1526 unshift @$refs, $hash if $hash and $refs;
1527 $hash = $1;
1528 next;
1529 }
1530 next unless s{^\s*(git-svn-id:)}{$1};
1531 my ($url, $rev, $uuid) = extract_metadata($_);
Eric Wong13c823f2007-04-08 00:59:19 -07001532 if (defined $url && defined $rev) {
Sam Vilain40cb8f82007-06-30 20:56:14 +12001533 next if $max{$url} and $max{$url} < $rev;
Eric Wong13c823f2007-04-08 00:59:19 -07001534 if (my $gs = Git::SVN->find_by_url($url)) {
João Abecasis63c56022008-07-14 16:28:04 +01001535 my $c = $gs->rev_map_get($rev, $uuid);
Adam Robenb03c7a62007-04-25 11:50:32 -07001536 if ($c && $c eq $hash) {
Eric Wong13c823f2007-04-08 00:59:19 -07001537 close $fh; # break the pipe
1538 return ($url, $rev, $uuid, $gs);
Sam Vilain40cb8f82007-06-30 20:56:14 +12001539 } else {
Eric Wong060610c2007-12-08 23:27:41 -08001540 $max{$url} ||= $gs->rev_map_max;
Eric Wong13c823f2007-04-08 00:59:19 -07001541 }
1542 }
1543 }
Eric Wong905f8b72007-02-16 03:22:40 -08001544 }
Eric Wong13c823f2007-04-08 00:59:19 -07001545 command_close_pipe($fh, $ctx);
1546 (undef, undef, undef, undef);
Eric Wong905f8b72007-02-16 03:22:40 -08001547}
1548
Eric Wong733a65a2007-06-13 02:23:28 -07001549sub read_commit_parents {
1550 my ($parents, $c) = @_;
Eric Wong7b02b852007-09-08 16:33:08 -07001551 chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
1552 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1553 @{$parents->{$c}} = split(/ /, $p);
Eric Wong733a65a2007-06-13 02:23:28 -07001554}
1555
1556sub linearize_history {
1557 my ($gs, $refs) = @_;
1558 my %parents;
1559 foreach my $c (@$refs) {
1560 read_commit_parents(\%parents, $c);
1561 }
1562
1563 my @linear_refs;
1564 my %skip = ();
1565 my $last_svn_commit = $gs->last_commit;
1566 foreach my $c (reverse @$refs) {
1567 next if $c eq $last_svn_commit;
1568 last if $skip{$c};
1569
1570 unshift @linear_refs, $c;
1571 $skip{$c} = 1;
1572
1573 # we only want the first parent to diff against for linear
1574 # history, we save the rest to inject when we finalize the
1575 # svn commit
1576 my $fp_a = verify_ref("$c~1");
1577 my $fp_b = shift @{$parents{$c}} if $parents{$c};
1578 if (!$fp_a || !$fp_b) {
1579 die "Commit $c\n",
1580 "has no parent commit, and therefore ",
1581 "nothing to diff against.\n",
1582 "You should be working from a repository ",
1583 "originally created by git-svn\n";
1584 }
1585 if ($fp_a ne $fp_b) {
1586 die "$c~1 = $fp_a, however parsing commit $c ",
1587 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1588 }
1589
1590 foreach my $p (@{$parents{$c}}) {
1591 $skip{$p} = 1;
1592 }
1593 }
1594 (\@linear_refs, \%parents);
1595}
1596
David D. Kilzere6fefa92007-11-21 11:57:18 -08001597sub find_file_type_and_diff_status {
1598 my ($path) = @_;
Dmitry Potapov107cee52008-07-21 00:14:07 +04001599 return ('dir', '') if $path eq '';
David D. Kilzere6fefa92007-11-21 11:57:18 -08001600
1601 my $diff_output =
1602 command_oneline(qw(diff --cached --name-status --), $path) || "";
1603 my $diff_status = (split(' ', $diff_output))[0] || "";
1604
1605 my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || "";
1606
1607 return (undef, undef) if !$diff_status && !$ls_tree;
1608
1609 if ($diff_status eq "A") {
1610 return ("link", $diff_status) if -l $path;
1611 return ("dir", $diff_status) if -d $path;
1612 return ("file", $diff_status);
1613 }
1614
1615 my $mode = (split(' ', $ls_tree))[0] || "";
1616
1617 return ("link", $diff_status) if $mode eq "120000";
1618 return ("dir", $diff_status) if $mode eq "040000";
1619 return ("file", $diff_status);
1620}
1621
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001622sub md5sum {
1623 my $arg = shift;
1624 my $ref = ref $arg;
1625 my $md5 = Digest::MD5->new();
Marcus Griep0b191382008-08-12 12:00:53 -04001626 if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001627 $md5->addfile($arg) or croak $!;
1628 } elsif ($ref eq 'SCALAR') {
1629 $md5->add($$arg) or croak $!;
1630 } elsif (!$ref) {
1631 $md5->add($arg) or croak $!;
1632 } else {
1633 ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1634 }
1635 return $md5->hexdigest();
1636}
1637
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -05001638sub gc_directory {
1639 if ($can_compress && -f $_ && basename($_) eq "unhandled.log") {
1640 my $out_filename = $_ . ".gz";
1641 open my $in_fh, "<", $_ or die "Unable to open $_: $!\n";
1642 binmode $in_fh;
1643 my $gz = Compress::Zlib::gzopen($out_filename, "ab") or
1644 die "Unable to open $out_filename: $!\n";
1645
1646 my $res;
1647 while ($res = sysread($in_fh, my $str, 1024)) {
1648 $gz->gzwrite($str) or
1649 die "Unable to write: ".$gz->gzerror()."!\n";
1650 }
1651 unlink $_ or die "unlink $File::Find::name: $!\n";
1652 } elsif (-f $_ && basename($_) eq "index") {
1653 unlink $_ or die "unlink $_: $!\n";
1654 }
1655}
1656
Eric Wong9b981fc2007-01-11 12:14:21 -08001657package Git::SVN;
1658use strict;
1659use warnings;
Eric Wong060610c2007-12-08 23:27:41 -08001660use Fcntl qw/:DEFAULT :seek/;
1661use constant rev_map_fmt => 'NH40';
Eric Wongecc712d2007-01-31 12:28:10 -08001662use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
Eric Wong62e349d2007-02-16 19:57:29 -08001663 $_repack $_repack_flags $_use_svm_props $_head
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00001664 $_use_svnsync_props $no_reuse_existing $_minimize_url
Pete Harlane82f0d72009-01-17 20:10:14 -08001665 $_use_log_author $_add_author_from $_localtime/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001666use Carp qw/croak/;
1667use File::Path qw/mkpath/;
Eric Wong373274f2007-01-31 13:54:23 -08001668use File::Copy qw/copy/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001669use IPC::Open3;
Sam Vilain7d944c32009-12-20 00:55:13 +13001670use Memoize; # core since 5.8.0, Jul 2002
Andrew Myrick8bff7c52010-01-30 03:14:22 +00001671use Memoize::Storable;
Eric Wong9b981fc2007-01-11 12:14:21 -08001672
Karl Hasselström94bc9142008-02-03 17:56:18 +01001673my ($_gc_nr, $_gc_period);
1674
Eric Wong9b981fc2007-01-11 12:14:21 -08001675# properties that we do not log:
1676my %SKIP_PROP;
1677BEGIN {
1678 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
1679 svn:special svn:executable
1680 svn:entry:committed-rev
1681 svn:entry:last-author
1682 svn:entry:uuid
1683 svn:entry:committed-date/;
Eric Wong91b03282007-02-11 00:51:33 -08001684
1685 # some options are read globally, but can be overridden locally
1686 # per [svn-remote "..."] section. Command-line options will *NOT*
1687 # override options set in an [svn-remote "..."] section
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001688 no strict 'refs';
1689 for my $option (qw/follow_parent no_metadata use_svm_props
1690 use_svnsync_props/) {
1691 my $key = $option;
Eric Wong91b03282007-02-11 00:51:33 -08001692 $key =~ tr/_//d;
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001693 my $prop = "-$option";
1694 *$option = sub {
1695 my ($self) = @_;
1696 return $self->{$prop} if exists $self->{$prop};
1697 my $k = "svn-remote.$self->{repo_id}.$key";
1698 eval { command_oneline(qw/config --get/, $k) };
1699 if ($@) {
1700 $self->{$prop} = ${"Git::SVN::_$option"};
Eric Wong91b03282007-02-11 00:51:33 -08001701 } else {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001702 my $v = command_oneline(qw/config --bool/,$k);
1703 $self->{$prop} = $v eq 'false' ? 0 : 1;
Eric Wong91b03282007-02-11 00:51:33 -08001704 }
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001705 return $self->{$prop};
1706 }
Eric Wong91b03282007-02-11 00:51:33 -08001707 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001708}
1709
Marcus Griep0b191382008-08-12 12:00:53 -04001710
Eric Wong321b1842008-01-02 10:10:03 -08001711my (%LOCKFILES, %INDEX_FILES);
1712END {
1713 unlink keys %LOCKFILES if %LOCKFILES;
1714 unlink keys %INDEX_FILES if %INDEX_FILES;
1715}
Eric Wong373274f2007-01-31 13:54:23 -08001716
Eric Wong4bb9ed02007-02-03 13:29:17 -08001717sub resolve_local_globs {
1718 my ($url, $fetch, $glob_spec) = @_;
1719 return unless defined $glob_spec;
1720 my $ref = $glob_spec->{ref};
1721 my $path = $glob_spec->{path};
Adam Brewster6f5748e2009-08-11 23:14:27 -04001722 foreach (command(qw#for-each-ref --format=%(refname) refs/#)) {
1723 next unless m#^$ref->{regex}$#;
Eric Wong4bb9ed02007-02-03 13:29:17 -08001724 my $p = $1;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001725 my $pathname = desanitize_refname($path->full_path($p));
1726 my $refname = desanitize_refname($ref->full_path($p));
Eric Wong4bb9ed02007-02-03 13:29:17 -08001727 if (my $existing = $fetch->{$pathname}) {
1728 if ($existing ne $refname) {
1729 die "Refspec conflict:\n",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001730 "existing: $existing\n",
1731 " globbed: $refname\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08001732 }
Adam Brewster6f5748e2009-08-11 23:14:27 -04001733 my $u = (::cmt_metadata("$refname"))[0];
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001734 $u =~ s!^\Q$url\E(/|$)!! or die
Adam Brewster6f5748e2009-08-11 23:14:27 -04001735 "$refname: '$url' not found in '$u'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08001736 if ($pathname ne $u) {
1737 warn "W: Refspec glob conflict ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001738 "(ref: $refname):\n",
Eric Wong4bb9ed02007-02-03 13:29:17 -08001739 "expected path: $pathname\n",
1740 " real path: $u\n",
1741 "Continuing ahead with $u\n";
1742 next;
1743 }
1744 } else {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001745 $fetch->{$pathname} = $refname;
1746 }
1747 }
1748}
1749
Eric Wonge98671e2007-02-14 02:21:19 -08001750sub parse_revision_argument {
1751 my ($base, $head) = @_;
1752 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
1753 return ($base, $head);
1754 }
1755 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
1756 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
1757 return ($head, $head) if ($::_revision eq 'HEAD');
1758 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
1759 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
1760 die "revision argument: $::_revision not understood by git-svn\n";
1761}
1762
Eric Wong0af9c9f2007-01-27 22:28:56 -08001763sub fetch_all {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001764 my ($repo_id, $remotes) = @_;
Eric Wong905f8b72007-02-16 03:22:40 -08001765 if (ref $repo_id) {
1766 my $gs = $repo_id;
1767 $repo_id = undef;
1768 $repo_id = $gs->{repo_id};
1769 }
1770 $remotes ||= read_all_remotes();
Eric Wong7447b4b2007-02-14 18:38:46 -08001771 my $remote = $remotes->{$repo_id} or
1772 die "[svn-remote \"$repo_id\"] unknown\n";
Eric Wonge5181922007-02-08 12:53:57 -08001773 my $fetch = $remote->{fetch};
Eric Wong7447b4b2007-02-14 18:38:46 -08001774 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
Eric Wonge5181922007-02-08 12:53:57 -08001775 my (@gs, @globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001776 my $ra = Git::SVN::Ra->new($url);
Eric Wong26a62d52007-02-12 13:25:25 -08001777 my $uuid = $ra->get_uuid;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001778 my $head = $ra->get_latest_revnum;
Eric Wong577e9fc2009-12-21 02:06:04 -08001779
1780 # ignore errors, $head revision may not even exist anymore
1781 eval { $ra->get_log("", $head, 0, 1, 0, 1, sub { $head = $_[1] }) };
1782 warn "W: $@\n" if $@;
1783
Eric Wong28710f72007-02-14 13:32:21 -08001784 my $base = defined $fetch ? $head : 0;
Eric Wonge5181922007-02-08 12:53:57 -08001785
1786 # read the max revs for wildcard expansion (branches/*, tags/*)
1787 foreach my $t (qw/branches tags/) {
1788 defined $remote->{$t} or next;
Marc Branchaud62244062009-06-23 13:02:08 -04001789 push @globs, @{$remote->{$t}};
1790
Eric Wong93f26892007-02-11 01:20:26 -08001791 my $max_rev = eval { tmp_config(qw/--int --get/,
1792 "svn-remote.$repo_id.${t}-maxRev") };
1793 if (defined $max_rev && ($max_rev < $base)) {
1794 $base = $max_rev;
Eric Wongd6d33462007-02-16 04:05:33 -08001795 } elsif (!defined $max_rev) {
1796 $base = 0;
Eric Wonge5181922007-02-08 12:53:57 -08001797 }
1798 }
1799
Eric Wongdb03cd22007-02-13 00:38:02 -08001800 if ($fetch) {
1801 foreach my $p (sort keys %$fetch) {
1802 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
Eric Wong060610c2007-12-08 23:27:41 -08001803 my $lr = $gs->rev_map_max;
Eric Wongdb03cd22007-02-13 00:38:02 -08001804 if (defined $lr) {
1805 $base = $lr if ($lr < $base);
1806 }
1807 push @gs, $gs;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001808 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08001809 }
Eric Wonge98671e2007-02-14 02:21:19 -08001810
1811 ($base, $head) = parse_revision_argument($base, $head);
Eric Wonge5181922007-02-08 12:53:57 -08001812 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001813}
1814
Eric Wong47e39c52007-01-21 04:27:09 -08001815sub read_all_remotes {
1816 my $r = {};
João Abecasis63c56022008-07-14 16:28:04 +01001817 my $use_svm_props = eval { command_oneline(qw/config --bool
1818 svn.useSvmProps/) };
1819 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
Eric Wongffd5c8e2009-10-22 23:39:04 -07001820 my $svn_refspec = qr{\s*(.*?)\s*:\s*(.+?)\s*};
Eric Wong8b8fc062007-01-22 11:44:57 -08001821 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
Adam Brewster6f5748e2009-08-11 23:14:27 -04001822 if (m!^(.+)\.fetch=$svn_refspec$!) {
1823 my ($remote, $local_ref, $remote_ref) = ($1, $2, $3);
1824 die("svn-remote.$remote: remote ref '$remote_ref' "
1825 . "must start with 'refs/'\n")
1826 unless $remote_ref =~ m{^refs/};
Steven Walter46cb16f2010-08-03 19:21:25 -04001827 $local_ref = uri_decode($local_ref);
Eric Wong46cf98b2007-07-14 12:40:32 -07001828 $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
João Abecasis63c56022008-07-14 16:28:04 +01001829 $r->{$remote}->{svm} = {} if $use_svm_props;
1830 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
1831 $r->{$1}->{svm} = {};
Eric Wong47e39c52007-01-21 04:27:09 -08001832 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1833 $r->{$1}->{url} = $2;
Adam Brewster6f5748e2009-08-11 23:14:27 -04001834 } elsif (m!^(.+)\.(branches|tags)=$svn_refspec$!) {
1835 my ($remote, $t, $local_ref, $remote_ref) =
1836 ($1, $2, $3, $4);
1837 die("svn-remote.$remote: remote ref '$remote_ref' ($t) "
1838 . "must start with 'refs/'\n")
1839 unless $remote_ref =~ m{^refs/};
Steven Walter46cb16f2010-08-03 19:21:25 -04001840 $local_ref = uri_decode($local_ref);
Marc Branchaud62244062009-06-23 13:02:08 -04001841 my $rs = {
Adam Brewster6f5748e2009-08-11 23:14:27 -04001842 t => $t,
1843 remote => $remote,
Jay Soffian07576202010-01-23 03:30:01 -05001844 path => Git::SVN::GlobSpec->new($local_ref, 1),
1845 ref => Git::SVN::GlobSpec->new($remote_ref, 0) };
Eric Wong4bb9ed02007-02-03 13:29:17 -08001846 if (length($rs->{ref}->{right}) != 0) {
1847 die "The '*' glob character must be the last ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001848 "character of '$remote_ref'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08001849 }
Adam Brewster6f5748e2009-08-11 23:14:27 -04001850 push @{ $r->{$remote}->{$t} }, $rs;
Eric Wong47e39c52007-01-21 04:27:09 -08001851 }
1852 }
João Abecasis63c56022008-07-14 16:28:04 +01001853
1854 map {
1855 if (defined $r->{$_}->{svm}) {
1856 my $svm;
1857 eval {
1858 my $section = "svn-remote.$_";
1859 $svm = {
1860 source => tmp_config('--get',
1861 "$section.svm-source"),
1862 replace => tmp_config('--get',
1863 "$section.svm-replace"),
1864 }
1865 };
1866 $r->{$_}->{svm} = $svm;
1867 }
1868 } keys %$r;
1869
Eric Wong47e39c52007-01-21 04:27:09 -08001870 $r;
1871}
1872
Eric Wongecc712d2007-01-31 12:28:10 -08001873sub init_vars {
Karl Hasselström94bc9142008-02-03 17:56:18 +01001874 $_gc_nr = $_gc_period = 1000;
Karl Hasselströmaf788a62008-02-03 17:56:12 +01001875 if (defined $_repack || defined $_repack_flags) {
1876 warn "Repack options are obsolete; they have no effect.\n";
1877 }
Eric Wongecc712d2007-01-31 12:28:10 -08001878}
1879
Eric Wongb805b442007-01-22 13:52:04 -08001880sub verify_remotes_sanity {
Eric Wong536c4b02007-01-23 11:35:53 -08001881 return unless -d $ENV{GIT_DIR};
Eric Wongb805b442007-01-22 13:52:04 -08001882 my %seen;
1883 foreach (command(qw/config -l/)) {
1884 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1885 if ($seen{$1}) {
1886 die "Remote ref refs/remote/$1 is tracked by",
1887 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1888 "Please resolve this ambiguity in ",
1889 "your git configuration file before ",
1890 "continuing\n";
1891 }
1892 $seen{$1} = $_;
1893 }
1894 }
1895}
1896
Eric Wonge6434f82007-01-23 16:29:23 -08001897sub find_existing_remote {
1898 my ($url, $remotes) = @_;
Eric Wongbefc9ad2007-02-17 02:53:07 -08001899 return undef if $no_reuse_existing;
Eric Wonge6434f82007-01-23 16:29:23 -08001900 my $existing;
1901 foreach my $repo_id (keys %$remotes) {
1902 my $u = $remotes->{$repo_id}->{url} or next;
1903 next if $u ne $url;
1904 $existing = $repo_id;
1905 last;
1906 }
1907 $existing;
1908}
1909
1910sub init_remote_config {
Eric Wongd8115c52007-02-01 03:30:31 -08001911 my ($self, $url, $no_write) = @_;
Eric Wonge6434f82007-01-23 16:29:23 -08001912 $url =~ s!/+$!!; # strip trailing slash
1913 my $r = read_all_remotes();
1914 my $existing = find_existing_remote($url, $r);
1915 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001916 unless ($no_write) {
1917 print STDERR "Using existing ",
1918 "[svn-remote \"$existing\"]\n";
1919 }
Eric Wonge6434f82007-01-23 16:29:23 -08001920 $self->{repo_id} = $existing;
Eric Wong4a1bb4c2007-05-13 09:58:14 -07001921 } elsif ($_minimize_url) {
Eric Wonge6434f82007-01-23 16:29:23 -08001922 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
1923 $existing = find_existing_remote($min_url, $r);
1924 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001925 unless ($no_write) {
1926 print STDERR "Using existing ",
1927 "[svn-remote \"$existing\"]\n";
1928 }
Eric Wonge6434f82007-01-23 16:29:23 -08001929 $self->{repo_id} = $existing;
1930 }
1931 if ($min_url ne $url) {
Eric Wonge5181922007-02-08 12:53:57 -08001932 unless ($no_write) {
1933 print STDERR "Using higher level of URL: ",
1934 "$url => $min_url\n";
1935 }
Eric Wonge6434f82007-01-23 16:29:23 -08001936 my $old_path = $self->{path};
1937 $self->{path} = $url;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001938 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
Eric Wonge6434f82007-01-23 16:29:23 -08001939 if (length $old_path) {
1940 $self->{path} .= "/$old_path";
1941 }
1942 $url = $min_url;
1943 }
1944 }
1945 my $orig_url;
1946 if (!$existing) {
1947 # verify that we aren't overwriting anything:
1948 $orig_url = eval {
1949 command_oneline('config', '--get',
1950 "svn-remote.$self->{repo_id}.url")
1951 };
1952 if ($orig_url && ($orig_url ne $url)) {
1953 die "svn-remote.$self->{repo_id}.url already set: ",
1954 "$orig_url\nwanted to set to: $url\n";
1955 }
1956 }
1957 my ($xrepo_id, $xpath) = find_ref($self->refname);
Adam Brewster6f5748e2009-08-11 23:14:27 -04001958 if (!$no_write && defined $xpath) {
Eric Wonge6434f82007-01-23 16:29:23 -08001959 die "svn-remote.$xrepo_id.fetch already set to track ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001960 "$xpath:", $self->refname, "\n";
Eric Wonge6434f82007-01-23 16:29:23 -08001961 }
Eric Wongd8115c52007-02-01 03:30:31 -08001962 unless ($no_write) {
1963 command_noisy('config',
1964 "svn-remote.$self->{repo_id}.url", $url);
Eric Wong46cf98b2007-07-14 12:40:32 -07001965 $self->{path} =~ s{^/}{};
Eric Wong5268f9e2009-08-16 14:22:12 -07001966 $self->{path} =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
Eric Wongd8115c52007-02-01 03:30:31 -08001967 command_noisy('config', '--add',
1968 "svn-remote.$self->{repo_id}.fetch",
1969 "$self->{path}:".$self->refname);
1970 }
Eric Wonge6434f82007-01-23 16:29:23 -08001971 $self->{url} = $url;
1972}
1973
Eric Wonga8ae2622007-02-13 14:22:11 -08001974sub find_by_url { # repos_root and, path are optional
1975 my ($class, $full_url, $repos_root, $path) = @_;
Adam Roben56973d22007-04-25 12:42:58 -07001976
Eric Wong1a97a502007-02-20 00:43:19 -08001977 return undef unless defined $full_url;
Adam Roben56973d22007-04-25 12:42:58 -07001978 remove_username($full_url);
1979 remove_username($repos_root) if defined $repos_root;
Eric Wonga8ae2622007-02-13 14:22:11 -08001980 my $remotes = read_all_remotes();
1981 if (defined $full_url && defined $repos_root && !defined $path) {
1982 $path = $full_url;
1983 $path =~ s#^\Q$repos_root\E(?:/|$)##;
1984 }
1985 foreach my $repo_id (keys %$remotes) {
1986 my $u = $remotes->{$repo_id}->{url} or next;
Adam Roben56973d22007-04-25 12:42:58 -07001987 remove_username($u);
Eric Wonga8ae2622007-02-13 14:22:11 -08001988 next if defined $repos_root && $repos_root ne $u;
1989
1990 my $fetch = $remotes->{$repo_id}->{fetch} || {};
Marc Branchaud62244062009-06-23 13:02:08 -04001991 foreach my $t (qw/branches tags/) {
1992 foreach my $globspec (@{$remotes->{$repo_id}->{$t}}) {
1993 resolve_local_globs($u, $fetch, $globspec);
1994 }
Eric Wonga8ae2622007-02-13 14:22:11 -08001995 }
1996 my $p = $path;
John Goerzen0bb91d92008-03-08 16:04:05 -06001997 my $rwr = rewrite_root({repo_id => $repo_id});
João Abecasis63c56022008-07-14 16:28:04 +01001998 my $svm = $remotes->{$repo_id}->{svm}
1999 if defined $remotes->{$repo_id}->{svm};
Eric Wonga8ae2622007-02-13 14:22:11 -08002000 unless (defined $p) {
2001 $p = $full_url;
John Goerzen0bb91d92008-03-08 16:04:05 -06002002 my $z = $u;
João Abecasis63c56022008-07-14 16:28:04 +01002003 my $prefix = '';
John Goerzen0bb91d92008-03-08 16:04:05 -06002004 if ($rwr) {
2005 $z = $rwr;
Dévai Tamás1b7e5432009-02-12 00:14:02 +01002006 remove_username($z);
João Abecasis63c56022008-07-14 16:28:04 +01002007 } elsif (defined $svm) {
2008 $z = $svm->{source};
2009 $prefix = $svm->{replace};
2010 $prefix =~ s#^\Q$u\E(?:/|$)##;
2011 $prefix =~ s#/$##;
John Goerzen0bb91d92008-03-08 16:04:05 -06002012 }
João Abecasis63c56022008-07-14 16:28:04 +01002013 $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next;
Eric Wonga8ae2622007-02-13 14:22:11 -08002014 }
2015 foreach my $f (keys %$fetch) {
2016 next if $f ne $p;
2017 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
2018 }
2019 }
2020 undef;
2021}
2022
Eric Wong9b981fc2007-01-11 12:14:21 -08002023sub init {
Eric Wongd8115c52007-02-01 03:30:31 -08002024 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08002025 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong9b981fc2007-01-11 12:14:21 -08002026 if (defined $url) {
Eric Wongd8115c52007-02-01 03:30:31 -08002027 $self->init_remote_config($url, $no_write);
Eric Wong9b981fc2007-01-11 12:14:21 -08002028 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002029 $self;
2030}
2031
Eric Wong706587f2007-01-18 17:50:01 -08002032sub find_ref {
2033 my ($ref_id) = @_;
2034 foreach (command(qw/config -l/)) {
2035 next unless m!^svn-remote\.(.+)\.fetch=
Eric Wongffd5c8e2009-10-22 23:39:04 -07002036 \s*(.*?)\s*:\s*(.+?)\s*$!x;
Eric Wong706587f2007-01-18 17:50:01 -08002037 my ($repo_id, $path, $ref) = ($1, $2, $3);
2038 if ($ref eq $ref_id) {
2039 $path = '' if ($path =~ m#^\./?#);
2040 return ($repo_id, $path);
2041 }
2042 }
2043 (undef, undef, undef);
2044}
2045
Eric Wong9b981fc2007-01-11 12:14:21 -08002046sub new {
Eric Wong706587f2007-01-18 17:50:01 -08002047 my ($class, $ref_id, $repo_id, $path) = @_;
2048 if (defined $ref_id && !defined $repo_id && !defined $path) {
2049 ($repo_id, $path) = find_ref($ref_id);
2050 if (!defined $repo_id) {
2051 die "Could not find a \"svn-remote.*.fetch\" key ",
2052 "in the repository configuration matching: ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04002053 "$ref_id\n";
Eric Wong706587f2007-01-18 17:50:01 -08002054 }
2055 }
2056 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong8b8fc062007-01-22 11:44:57 -08002057 if (!defined $self->{path} || !length $self->{path}) {
2058 my $fetch = command_oneline('config', '--get',
2059 "svn-remote.$repo_id.fetch",
Adam Brewster6f5748e2009-08-11 23:14:27 -04002060 ":$ref_id\$") or
Eric Wong8b8fc062007-01-22 11:44:57 -08002061 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04002062 "\":$ref_id\$\" in config\n";
Eric Wong8b8fc062007-01-22 11:44:57 -08002063 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
2064 }
Eric Wongb1a954a2010-06-14 04:31:10 +00002065 $self->{path} =~ s{/+}{/}g;
2066 $self->{path} =~ s{\A/}{};
2067 $self->{path} =~ s{/\z}{};
Eric Wong706587f2007-01-18 17:50:01 -08002068 $self->{url} = command_oneline('config', '--get',
2069 "svn-remote.$repo_id.url") or
2070 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
Eric Wongd6d33462007-02-16 04:05:33 -08002071 $self->rebuild;
Eric Wong9b981fc2007-01-11 12:14:21 -08002072 $self;
2073}
2074
Robert Ewaldbf655fd2007-07-30 11:08:21 +02002075sub refname {
Adam Brewster6f5748e2009-08-11 23:14:27 -04002076 my ($refname) = $_[0]->{ref_id} ;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02002077
2078 # It cannot end with a slash /, we'll throw up on this because
2079 # SVN can't have directories with a slash in their name, either:
2080 if ($refname =~ m{/$}) {
2081 die "ref: '$refname' ends with a trailing slash, this is ",
2082 "not permitted by git nor Subversion\n";
2083 }
2084
2085 # It cannot have ASCII control character space, tilde ~, caret ^,
2086 # colon :, question-mark ?, asterisk *, space, or open bracket [
2087 # anywhere.
2088 #
2089 # Additionally, % must be escaped because it is used for escaping
2090 # and we want our escaped refname to be reversible
2091 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
2092
2093 # no slash-separated component can begin with a dot .
2094 # /.* becomes /%2E*
2095 $refname =~ s{/\.}{/%2E}g;
2096
2097 # It cannot have two consecutive dots .. anywhere
2098 # .. becomes %2E%2E
2099 $refname =~ s{\.\.}{%2E%2E}g;
2100
Torsten Schmutzler73d41952010-05-06 22:20:43 +02002101 # trailing dots and .lock are not allowed
2102 # .$ becomes %2E and .lock becomes %2Elock
2103 $refname =~ s{\.(?=$|lock$)}{%2E};
2104
2105 # the sequence @{ is used to access the reflog
2106 # @{ becomes %40{
2107 $refname =~ s{\@\{}{%40\{}g;
2108
Robert Ewaldbf655fd2007-07-30 11:08:21 +02002109 return $refname;
2110}
2111
2112sub desanitize_refname {
2113 my ($refname) = @_;
2114 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
2115 return $refname;
2116}
Eric Wong9b981fc2007-01-11 12:14:21 -08002117
Eric Wong26a62d52007-02-12 13:25:25 -08002118sub svm_uuid {
2119 my ($self) = @_;
2120 return $self->{svm}->{uuid} if $self->svm;
2121 $self->ra;
2122 unless ($self->{svm}) {
2123 die "SVM UUID not cached, and reading remotely failed\n";
2124 }
2125 $self->{svm}->{uuid};
2126}
Eric Wong8a49ee92007-02-10 20:46:50 -08002127
Eric Wong26a62d52007-02-12 13:25:25 -08002128sub svm {
2129 my ($self) = @_;
2130 return $self->{svm} if $self->{svm};
2131 my $svm;
Eric Wong8a49ee92007-02-10 20:46:50 -08002132 # see if we have it in our config, first:
2133 eval {
Eric Wong26a62d52007-02-12 13:25:25 -08002134 my $section = "svn-remote.$self->{repo_id}";
2135 $svm = {
Eric Wong93f26892007-02-11 01:20:26 -08002136 source => tmp_config('--get', "$section.svm-source"),
2137 uuid => tmp_config('--get', "$section.svm-uuid"),
Eric Wongbefc9ad2007-02-17 02:53:07 -08002138 replace => tmp_config('--get', "$section.svm-replace"),
Eric Wong8a49ee92007-02-10 20:46:50 -08002139 }
2140 };
Eric Wongbefc9ad2007-02-17 02:53:07 -08002141 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
2142 $self->{svm} = $svm;
2143 }
Eric Wong26a62d52007-02-12 13:25:25 -08002144 $self->{svm};
2145}
2146
2147sub _set_svm_vars {
2148 my ($self, $ra) = @_;
Eric Wongdb03cd22007-02-13 00:38:02 -08002149 return $ra if $self->svm;
Eric Wong26a62d52007-02-12 13:25:25 -08002150
Eric Wongdb03cd22007-02-13 00:38:02 -08002151 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08002152 "(svm:source, svm:uuid) ",
Eric Wongdb03cd22007-02-13 00:38:02 -08002153 "from the following URLs:\n" );
2154 sub read_svm_props {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002155 my ($self, $ra, $path, $r) = @_;
2156 my $props = ($ra->get_dir($path, $r))[2];
Eric Wongdb03cd22007-02-13 00:38:02 -08002157 my $src = $props->{'svm:source'};
Eric Wong8a49ee92007-02-10 20:46:50 -08002158 my $uuid = $props->{'svm:uuid'};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002159 return undef if (!$src || !$uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08002160
Eric Wongbefc9ad2007-02-17 02:53:07 -08002161 chomp($src, $uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08002162
Eric Wongb3e95932009-07-11 14:13:12 -07002163 $uuid =~ m{^[0-9a-f\-]{30,}$}i
Eric Wong8a49ee92007-02-10 20:46:50 -08002164 or die "doesn't look right - svm:uuid is '$uuid'\n";
Eric Wongbefc9ad2007-02-17 02:53:07 -08002165
2166 # the '!' is used to mark the repos_root!/relative/path
2167 $src =~ s{/?!/?}{/};
Eric Wongdb03cd22007-02-13 00:38:02 -08002168 $src =~ s{/+$}{}; # no trailing slashes please
Eric Wongbefc9ad2007-02-17 02:53:07 -08002169 # username is of no interest
Eric Wongdb03cd22007-02-13 00:38:02 -08002170 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
Eric Wong8a49ee92007-02-10 20:46:50 -08002171
Eric Wongbefc9ad2007-02-17 02:53:07 -08002172 my $replace = $ra->{url};
2173 $replace .= "/$path" if length $path;
2174
Eric Wongdb03cd22007-02-13 00:38:02 -08002175 my $section = "svn-remote.$self->{repo_id}";
Eric Wongbefc9ad2007-02-17 02:53:07 -08002176 tmp_config("$section.svm-source", $src);
2177 tmp_config("$section.svm-replace", $replace);
2178 tmp_config("$section.svm-uuid", $uuid);
2179 $self->{svm} = {
2180 source => $src,
2181 uuid => $uuid,
2182 replace => $replace
2183 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002184 }
Eric Wongdb03cd22007-02-13 00:38:02 -08002185
2186 my $r = $ra->get_latest_revnum;
2187 my $path = $self->{path};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002188 my %tried;
Eric Wongdb03cd22007-02-13 00:38:02 -08002189 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002190 unless ($tried{"$self->{url}/$path"}) {
2191 return $ra if $self->read_svm_props($ra, $path, $r);
2192 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002193 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002194 $path =~ s#/?[^/]+$##;
Eric Wong8a49ee92007-02-10 20:46:50 -08002195 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002196 die "Path: '$path' should be ''\n" if $path ne '';
2197 return $ra if $self->read_svm_props($ra, $path, $r);
2198 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002199
2200 if ($ra->{repos_root} eq $self->{url}) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002201 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002202 }
2203
2204 # nope, make sure we're connected to the repository root:
2205 my $ok;
2206 my @tried_b;
2207 $path = $ra->{svn_path};
Eric Wongdb03cd22007-02-13 00:38:02 -08002208 $ra = Git::SVN::Ra->new($ra->{repos_root});
2209 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002210 unless ($tried{"$ra->{url}/$path"}) {
2211 $ok = $self->read_svm_props($ra, $path, $r);
2212 last if $ok;
2213 $tried{"$ra->{url}/$path"} = 1;
2214 }
2215 $path =~ s#/?[^/]+$##;
Eric Wongdb03cd22007-02-13 00:38:02 -08002216 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002217 die "Path: '$path' should be ''\n" if $path ne '';
2218 $ok ||= $self->read_svm_props($ra, $path, $r);
2219 $tried{"$ra->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002220 if (!$ok) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002221 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002222 }
2223 Git::SVN::Ra->new($self->{url});
Eric Wong8a49ee92007-02-10 20:46:50 -08002224}
2225
Eric Wong62e349d2007-02-16 19:57:29 -08002226sub svnsync {
2227 my ($self) = @_;
2228 return $self->{svnsync} if $self->{svnsync};
2229
2230 if ($self->no_metadata) {
2231 die "Can't have both 'noMetadata' and ",
2232 "'useSvnsyncProps' options set!\n";
2233 }
2234 if ($self->rewrite_root) {
2235 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
2236 "options set!\n";
2237 }
Jay Soffian3e18ce12010-01-23 03:30:00 -05002238 if ($self->rewrite_uuid) {
2239 die "Can't have both 'useSvnsyncProps' and 'rewriteUUID' ",
2240 "options set!\n";
2241 }
Eric Wong62e349d2007-02-16 19:57:29 -08002242
2243 my $svnsync;
2244 # see if we have it in our config, first:
2245 eval {
2246 my $section = "svn-remote.$self->{repo_id}";
Eric Wong98fa5b62008-01-11 23:13:55 -08002247
2248 my $url = tmp_config('--get', "$section.svnsync-url");
2249 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
2250 die "doesn't look right - svn:sync-from-url is '$url'\n";
2251
2252 my $uuid = tmp_config('--get', "$section.svnsync-uuid");
Eric Wongb3e95932009-07-11 14:13:12 -07002253 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
Eric Wong98fa5b62008-01-11 23:13:55 -08002254 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2255
2256 $svnsync = { url => $url, uuid => $uuid }
Eric Wong62e349d2007-02-16 19:57:29 -08002257 };
2258 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
2259 return $self->{svnsync} = $svnsync;
2260 }
2261
2262 my $err = "useSvnsyncProps set, but failed to read " .
2263 "svnsync property: svn:sync-from-";
2264 my $rp = $self->ra->rev_proplist(0);
2265
2266 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
Eric Wong98fa5b62008-01-11 23:13:55 -08002267 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
Eric Wong62e349d2007-02-16 19:57:29 -08002268 die "doesn't look right - svn:sync-from-url is '$url'\n";
2269
2270 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
Eric Wongb3e95932009-07-11 14:13:12 -07002271 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
Eric Wong62e349d2007-02-16 19:57:29 -08002272 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2273
2274 my $section = "svn-remote.$self->{repo_id}";
2275 tmp_config('--add', "$section.svnsync-uuid", $uuid);
2276 tmp_config('--add', "$section.svnsync-url", $url);
2277 return $self->{svnsync} = { url => $url, uuid => $uuid };
2278}
2279
Eric Wong26a62d52007-02-12 13:25:25 -08002280# this allows us to memoize our SVN::Ra UUID locally and avoid a
2281# remote lookup (useful for 'git svn log').
2282sub ra_uuid {
2283 my ($self) = @_;
2284 unless ($self->{ra_uuid}) {
2285 my $key = "svn-remote.$self->{repo_id}.uuid";
2286 my $uuid = eval { tmp_config('--get', $key) };
Eric Wongb3e95932009-07-11 14:13:12 -07002287 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/i) {
Eric Wong26a62d52007-02-12 13:25:25 -08002288 $self->{ra_uuid} = $uuid;
2289 } else {
2290 die "ra_uuid called without URL\n" unless $self->{url};
2291 $self->{ra_uuid} = $self->ra->get_uuid;
2292 tmp_config('--add', $key, $self->{ra_uuid});
2293 }
2294 }
2295 $self->{ra_uuid};
2296}
2297
Eric Wonga5460eb2007-11-21 18:20:57 -08002298sub _set_repos_root {
2299 my ($self, $repos_root) = @_;
2300 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2301 $repos_root ||= $self->ra->{repos_root};
2302 tmp_config($k, $repos_root);
2303 $repos_root;
2304}
2305
2306sub repos_root {
2307 my ($self) = @_;
2308 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2309 eval { tmp_config('--get', $k) } || $self->_set_repos_root;
2310}
2311
Eric Wong9b981fc2007-01-11 12:14:21 -08002312sub ra {
2313 my ($self) = shift;
Eric Wong8a49ee92007-02-10 20:46:50 -08002314 my $ra = Git::SVN::Ra->new($self->{url});
Eric Wonga5460eb2007-11-21 18:20:57 -08002315 $self->_set_repos_root($ra->{repos_root});
Eric Wong91b03282007-02-11 00:51:33 -08002316 if ($self->use_svm_props && !$self->{svm}) {
2317 if ($self->no_metadata) {
Eric Wong97ae0912007-02-11 15:21:24 -08002318 die "Can't have both 'noMetadata' and ",
2319 "'useSvmProps' options set!\n";
Eric Wong62e349d2007-02-16 19:57:29 -08002320 } elsif ($self->use_svnsync_props) {
2321 die "Can't have both 'useSvnsyncProps' and ",
2322 "'useSvmProps' options set!\n";
Eric Wong91b03282007-02-11 00:51:33 -08002323 }
Eric Wong26a62d52007-02-12 13:25:25 -08002324 $ra = $self->_set_svm_vars($ra);
Eric Wong8a49ee92007-02-10 20:46:50 -08002325 $self->{-want_revprops} = 1;
2326 }
2327 $ra;
Eric Wong9b981fc2007-01-11 12:14:21 -08002328}
2329
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002330# prop_walk(PATH, REV, SUB)
2331# -------------------------
2332# Recursively traverse PATH at revision REV and invoke SUB for each
2333# directory that contains a SVN property. SUB will be invoked as
2334# follows: &SUB(gs, path, props); where `gs' is this instance of
2335# Git::SVN, `path' the path to the directory where the properties
2336# `props' were found. The `path' will be relative to point of checkout,
2337# that is, if url://repo/trunk is the current Git branch, and that
2338# directory contains a sub-directory `d', SUB will be invoked with `/d/'
2339# as `path' (note the trailing `/').
2340sub prop_walk {
2341 my ($self, $path, $rev, $sub) = @_;
2342
Kevin Ballard35cda062008-01-09 01:37:20 -05002343 $path =~ s#^/##;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002344 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
2345 $path =~ s#^/*#/#g;
Eric Wong9b981fc2007-01-11 12:14:21 -08002346 my $p = $path;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002347 # Strip the irrelevant part of the path.
2348 $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
2349 # Ensure the path is terminated by a `/'.
2350 $p =~ s#/*$#/#;
2351
2352 # The properties contain all the internal SVN stuff nobody
2353 # (usually) cares about.
2354 my $interesting_props = 0;
2355 foreach (keys %{$props}) {
2356 # If it doesn't start with `svn:', it must be a
2357 # user-defined property.
2358 ++$interesting_props and next if $_ !~ /^svn:/;
2359 # FIXME: Fragile, if SVN adds new public properties,
2360 # this needs to be updated.
2361 ++$interesting_props if /^svn:(?:ignore|keywords|executable
2362 |eol-style|mime-type
2363 |externals|needs-lock)$/x;
Eric Wong9b981fc2007-01-11 12:14:21 -08002364 }
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002365 &$sub($self, $p, $props) if $interesting_props;
2366
Eric Wong9b981fc2007-01-11 12:14:21 -08002367 foreach (sort keys %$dirent) {
Eric Wong0dc03d62007-05-13 01:04:43 -07002368 next if $dirent->{$_}->{kind} != $SVN::Node::dir;
Christian Engwerb7166cc2008-05-27 08:46:55 +00002369 $self->prop_walk($self->{path} . $p . $_, $rev, $sub);
Eric Wong9b981fc2007-01-11 12:14:21 -08002370 }
2371}
2372
Eric Wong3ebe8df2007-01-25 17:35:40 -08002373sub last_rev { ($_[0]->last_rev_commit)[0] }
2374sub last_commit { ($_[0]->last_rev_commit)[1] }
2375
Eric Wong9b981fc2007-01-11 12:14:21 -08002376# returns the newest SVN revision number and newest commit SHA1
2377sub last_rev_commit {
2378 my ($self) = @_;
2379 if (defined $self->{last_rev} && defined $self->{last_commit}) {
2380 return ($self->{last_rev}, $self->{last_commit});
2381 }
Eric Wongd2866f92007-01-11 12:26:16 -08002382 my $c = ::verify_ref($self->refname.'^0');
Eric Wong91b03282007-02-11 00:51:33 -08002383 if ($c && !$self->use_svm_props && !$self->no_metadata) {
Eric Wongd2866f92007-01-11 12:26:16 -08002384 my $rev = (::cmt_metadata($c))[1];
Eric Wong9b981fc2007-01-11 12:14:21 -08002385 if (defined $rev) {
2386 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2387 return ($rev, $c);
2388 }
2389 }
Eric Wong060610c2007-12-08 23:27:41 -08002390 my $map_path = $self->map_path;
2391 unless (-e $map_path) {
Eric Wong26a62d52007-02-12 13:25:25 -08002392 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
2393 return (undef, undef);
2394 }
Eric Wong66ab84b2007-12-08 23:27:42 -08002395 my ($rev, $commit) = $self->rev_map_max(1);
Eric Wong060610c2007-12-08 23:27:41 -08002396 ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
2397 return ($rev, $commit);
Eric Wong9b981fc2007-01-11 12:14:21 -08002398}
2399
Eric Wong3ebe8df2007-01-25 17:35:40 -08002400sub get_fetch_range {
2401 my ($self, $min, $max) = @_;
2402 $max ||= $self->ra->get_latest_revnum;
Eric Wong060610c2007-12-08 23:27:41 -08002403 $min ||= $self->rev_map_max;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002404 (++$min, $max);
Eric Wong9b981fc2007-01-11 12:14:21 -08002405}
2406
Eric Wong8a49ee92007-02-10 20:46:50 -08002407sub tmp_config {
Eric Wong93f26892007-02-11 01:20:26 -08002408 my (@args) = @_;
Eric Wongb7e53482007-02-16 04:09:28 -08002409 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
2410 my $config = "$ENV{GIT_DIR}/svn/.metadata";
Eric Wong38570a42007-06-13 02:37:05 -07002411 if (! -f $config && -f $old_def_config) {
Eric Wongb7e53482007-02-16 04:09:28 -08002412 rename $old_def_config, $config or
2413 die "Failed rename $old_def_config => $config: $!\n";
2414 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002415 my $old_config = $ENV{GIT_CONFIG};
Eric Wong93f26892007-02-11 01:20:26 -08002416 $ENV{GIT_CONFIG} = $config;
Eric Wong8a49ee92007-02-10 20:46:50 -08002417 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002418 my @ret = eval {
2419 unless (-f $config) {
2420 mkfile($config);
2421 open my $fh, '>', $config or
2422 die "Can't open $config: $!\n";
2423 print $fh "; This file is used internally by ",
2424 "git-svn\n" or die
2425 "Couldn't write to $config: $!\n";
2426 print $fh "; You should not have to edit it\n" or
2427 die "Couldn't write to $config: $!\n";
2428 close $fh or die "Couldn't close $config: $!\n";
2429 }
2430 command('config', @args);
2431 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002432 my $err = $@;
2433 if (defined $old_config) {
2434 $ENV{GIT_CONFIG} = $old_config;
2435 } else {
2436 delete $ENV{GIT_CONFIG};
2437 }
2438 die $err if $err;
2439 wantarray ? @ret : $ret[0];
2440}
2441
Eric Wong9b981fc2007-01-11 12:14:21 -08002442sub tmp_index_do {
2443 my ($self, $sub) = @_;
2444 my $old_index = $ENV{GIT_INDEX_FILE};
2445 $ENV{GIT_INDEX_FILE} = $self->{index};
Eric Wong8a49ee92007-02-10 20:46:50 -08002446 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002447 my @ret = eval {
2448 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
2449 mkpath([$dir]) unless -d $dir;
2450 &$sub;
2451 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002452 my $err = $@;
2453 if (defined $old_index) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002454 $ENV{GIT_INDEX_FILE} = $old_index;
2455 } else {
2456 delete $ENV{GIT_INDEX_FILE};
2457 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002458 die $err if $err;
Eric Wong9b981fc2007-01-11 12:14:21 -08002459 wantarray ? @ret : $ret[0];
2460}
2461
2462sub assert_index_clean {
2463 my ($self, $treeish) = @_;
2464
2465 $self->tmp_index_do(sub {
2466 command_noisy('read-tree', $treeish) unless -e $self->{index};
2467 my $x = command_oneline('write-tree');
2468 my ($y) = (command(qw/cat-file commit/, $treeish) =~
2469 /^tree ($::sha1)/mo);
Eric Wonge8d120b2007-02-14 16:29:52 -08002470 return if $y eq $x;
2471
2472 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2473 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2474 command_noisy('read-tree', $treeish);
Eric Wong9b981fc2007-01-11 12:14:21 -08002475 $x = command_oneline('write-tree');
2476 if ($y ne $x) {
2477 ::fatal "trees ($treeish) $y != $x\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02002478 "Something is seriously wrong...";
Eric Wong9b981fc2007-01-11 12:14:21 -08002479 }
2480 });
2481}
2482
2483sub get_commit_parents {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002484 my ($self, $log_entry) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002485 my (%seen, @ret, @tmp);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002486 # legacy support for 'set-tree'; this is only used by set_tree_cb:
2487 if (my $ip = $self->{inject_parents}) {
2488 if (my $commit = delete $ip->{$log_entry->{revision}}) {
2489 push @tmp, $commit;
Eric Wong9b981fc2007-01-11 12:14:21 -08002490 }
2491 }
Eric Wongd2866f92007-01-11 12:26:16 -08002492 if (my $cur = ::verify_ref($self->refname.'^0')) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002493 push @tmp, $cur;
2494 }
Eric Wong733a65a2007-06-13 02:23:28 -07002495 if (my $ipd = $self->{inject_parents_dcommit}) {
2496 if (my $commit = delete $ipd->{$log_entry->{revision}}) {
2497 push @tmp, @$commit;
2498 }
2499 }
Eric Wong44320b92007-01-13 22:35:53 -08002500 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
Eric Wong9b981fc2007-01-11 12:14:21 -08002501 while (my $p = shift @tmp) {
2502 next if $seen{$p};
2503 $seen{$p} = 1;
2504 push @ret, $p;
Eric Wong9b981fc2007-01-11 12:14:21 -08002505 }
2506 @ret;
2507}
2508
Eric Wongaea736c2007-02-16 19:15:21 -08002509sub rewrite_root {
2510 my ($self) = @_;
2511 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
2512 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2513 my $rwr = eval { command_oneline(qw/config --get/, $k) };
2514 if ($rwr) {
2515 $rwr =~ s#/+$##;
2516 if ($rwr !~ m#^[a-z\+]+://#) {
2517 die "$rwr is not a valid URL (key: $k)\n";
2518 }
2519 }
2520 $self->{-rewrite_root} = $rwr;
2521}
2522
Jay Soffian3e18ce12010-01-23 03:30:00 -05002523sub rewrite_uuid {
2524 my ($self) = @_;
2525 return $self->{-rewrite_uuid} if exists $self->{-rewrite_uuid};
2526 my $k = "svn-remote.$self->{repo_id}.rewriteUUID";
2527 my $rwid = eval { command_oneline(qw/config --get/, $k) };
2528 if ($rwid) {
2529 $rwid =~ s#/+$##;
2530 if ($rwid !~ m#^[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}$#) {
2531 die "$rwid is not a valid UUID (key: $k)\n";
2532 }
2533 }
2534 $self->{-rewrite_uuid} = $rwid;
2535}
2536
Eric Wongaea736c2007-02-16 19:15:21 -08002537sub metadata_url {
2538 my ($self) = @_;
2539 ($self->rewrite_root || $self->{url}) .
2540 (length $self->{path} ? '/' . $self->{path} : '');
2541}
2542
Eric Wong706587f2007-01-18 17:50:01 -08002543sub full_url {
Eric Wong9b981fc2007-01-11 12:14:21 -08002544 my ($self) = @_;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002545 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
Eric Wong9b981fc2007-01-11 12:14:21 -08002546}
2547
Eric Wongad948022007-12-15 19:08:22 -08002548
2549sub set_commit_header_env {
2550 my ($log_entry) = @_;
2551 my %env;
2552 foreach my $ned (qw/NAME EMAIL DATE/) {
2553 foreach my $ac (qw/AUTHOR COMMITTER/) {
2554 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2555 }
2556 }
2557
2558 $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
2559 $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
2560 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
2561
2562 $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
2563 ? $log_entry->{commit_name}
2564 : $log_entry->{name};
2565 $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
2566 ? $log_entry->{commit_email}
2567 : $log_entry->{email};
2568 \%env;
2569}
2570
2571sub restore_commit_header_env {
2572 my ($env) = @_;
2573 foreach my $ned (qw/NAME EMAIL DATE/) {
2574 foreach my $ac (qw/AUTHOR COMMITTER/) {
2575 my $k = "GIT_${ac}_${ned}";
2576 if (defined $env->{$k}) {
2577 $ENV{$k} = $env->{$k};
2578 } else {
2579 delete $ENV{$k};
2580 }
2581 }
2582 }
2583}
2584
Karl Hasselström94bc9142008-02-03 17:56:18 +01002585sub gc {
2586 command_noisy('gc', '--auto');
2587};
2588
Eric Wong9b981fc2007-01-11 12:14:21 -08002589sub do_git_commit {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002590 my ($self, $log_entry) = @_;
Eric Wong8a603772007-01-31 02:45:50 -08002591 my $lr = $self->last_rev;
2592 if (defined $lr && $lr >= $log_entry->{revision}) {
2593 die "Last fetched revision of ", $self->refname,
2594 " was r$lr, but we are about to fetch: ",
2595 "r$log_entry->{revision}!\n";
2596 }
Eric Wong060610c2007-12-08 23:27:41 -08002597 if (my $c = $self->rev_map_get($log_entry->{revision})) {
Eric Wong44320b92007-01-13 22:35:53 -08002598 croak "$log_entry->{revision} = $c already exists! ",
Eric Wong9b981fc2007-01-11 12:14:21 -08002599 "Why are we refetching it?\n";
2600 }
Eric Wongad948022007-12-15 19:08:22 -08002601 my $old_env = set_commit_header_env($log_entry);
Eric Wong44320b92007-01-13 22:35:53 -08002602 my $tree = $log_entry->{tree};
Eric Wong9b981fc2007-01-11 12:14:21 -08002603 if (!defined $tree) {
2604 $tree = $self->tmp_index_do(sub {
2605 command_oneline('write-tree') });
2606 }
2607 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2608
Deskin Millere855bfc2008-10-31 00:10:25 -04002609 my @exec = ('git', 'commit-tree', $tree);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002610 foreach ($self->get_commit_parents($log_entry)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002611 push @exec, '-p', $_;
2612 }
2613 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2614 or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07002615 binmode $msg_fh;
2616
2617 # we always get UTF-8 from SVN, but we may want our commits in
2618 # a different encoding.
2619 if (my $enc = Git::config('i18n.commitencoding')) {
2620 require Encode;
2621 Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
2622 }
Eric Wong44320b92007-01-13 22:35:53 -08002623 print $msg_fh $log_entry->{log} or croak $!;
Eric Wongad948022007-12-15 19:08:22 -08002624 restore_commit_header_env($old_env);
Eric Wong91b03282007-02-11 00:51:33 -08002625 unless ($self->no_metadata) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002626 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2627 or croak $!;
Eric Wong9760adc2007-01-31 03:06:56 -08002628 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002629 $msg_fh->flush == 0 or croak $!;
2630 close $msg_fh or croak $!;
2631 chomp(my $commit = do { local $/; <$out_fh> });
2632 close $out_fh or croak $!;
2633 waitpid $pid, 0;
2634 croak $? if $?;
2635 if ($commit !~ /^$::sha1$/o) {
2636 die "Failed to commit, invalid sha1: $commit\n";
2637 }
2638
Eric Wong060610c2007-12-08 23:27:41 -08002639 $self->rev_map_set($log_entry->{revision}, $commit, 1);
Eric Wong9b981fc2007-01-11 12:14:21 -08002640
Eric Wong44320b92007-01-13 22:35:53 -08002641 $self->{last_rev} = $log_entry->{revision};
Eric Wong9b981fc2007-01-11 12:14:21 -08002642 $self->{last_commit} = $commit;
Simon Arlott49750f32009-03-30 19:31:41 +01002643 print "r$log_entry->{revision}" unless $::_q > 1;
Eric Wong8a49ee92007-02-10 20:46:50 -08002644 if (defined $log_entry->{svm_revision}) {
Simon Arlott49750f32009-03-30 19:31:41 +01002645 print " (\@$log_entry->{svm_revision})" unless $::_q > 1;
Eric Wong060610c2007-12-08 23:27:41 -08002646 $self->rev_map_set($log_entry->{svm_revision}, $commit,
Eric Wong26a62d52007-02-12 13:25:25 -08002647 0, $self->svm_uuid);
Eric Wong8a49ee92007-02-10 20:46:50 -08002648 }
Simon Arlott49750f32009-03-30 19:31:41 +01002649 print " = $commit ($self->{ref_id})\n" unless $::_q > 1;
Karl Hasselström94bc9142008-02-03 17:56:18 +01002650 if (--$_gc_nr == 0) {
2651 $_gc_nr = $_gc_period;
2652 gc();
2653 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002654 return $commit;
2655}
2656
Eric Wongfbcc1732007-02-06 18:35:30 -08002657sub match_paths {
2658 my ($self, $paths, $r) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002659 return 1 if $self->{path} eq '';
Eric Wongd542aed2007-02-09 02:19:41 -08002660 if (my $path = $paths->{"/$self->{path}"}) {
2661 return ($path->{action} eq 'D') ? 0 : 1;
2662 }
Mattias Nissler0b2af452009-07-07 01:40:02 +02002663 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
Eric Wongfbcc1732007-02-06 18:35:30 -08002664 if (grep /$self->{path_regex}/, keys %$paths) {
2665 return 1;
2666 }
2667 my $c = '';
2668 foreach (split m#/#, $self->{path}) {
2669 $c .= "/$_";
Eric Wong74a81222007-02-10 13:28:50 -08002670 next unless ($paths->{$c} &&
2671 ($paths->{$c}->{action} =~ /^[AR]$/));
Eric Wonge5181922007-02-08 12:53:57 -08002672 if ($self->ra->check_path($self->{path}, $r) ==
2673 $SVN::Node::dir) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002674 return 1;
2675 }
2676 }
2677 return 0;
2678}
2679
Eric Wong15710b62007-01-22 02:20:33 -08002680sub find_parent_branch {
2681 my ($self, $paths, $rev) = @_;
Eric Wong91b03282007-02-11 00:51:33 -08002682 return undef unless $self->follow_parent;
Eric Wonge5a0b242007-01-25 15:44:54 -08002683 unless (defined $paths) {
Eric Wongc7eba712007-01-31 03:45:28 -08002684 my $err_handler = $SVN::Error::handler;
2685 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
Mattias Nissler3c49a032009-07-07 01:39:52 +02002686 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1,
2687 sub { $paths = $_[0] });
Eric Wongc7eba712007-01-31 03:45:28 -08002688 $SVN::Error::handler = $err_handler;
Eric Wonge5a0b242007-01-25 15:44:54 -08002689 }
2690 return undef unless defined $paths;
Eric Wong15710b62007-01-22 02:20:33 -08002691
2692 # look for a parent from another branch:
Mattias Nissler0b2af452009-07-07 01:40:02 +02002693 my @b_path_components = split m#/#, $self->{path};
Eric Wong7f578c52007-01-24 02:16:25 -08002694 my @a_path_components;
2695 my $i;
2696 while (@b_path_components) {
2697 $i = $paths->{'/'.join('/', @b_path_components)};
Eric Wong74a81222007-02-10 13:28:50 -08002698 last if $i && defined $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002699 unshift(@a_path_components, pop(@b_path_components));
2700 }
Eric Wong74a81222007-02-10 13:28:50 -08002701 return undef unless defined $i && defined $i->{copyfrom_path};
2702 my $branch_from = $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002703 if (@a_path_components) {
2704 print STDERR "branch_from: $branch_from => ";
2705 $branch_from .= '/'.join('/', @a_path_components);
2706 print STDERR $branch_from, "\n";
2707 }
Eric Wong3ebe8df2007-01-25 17:35:40 -08002708 my $r = $i->{copyfrom_rev};
Eric Wong15710b62007-01-22 02:20:33 -08002709 my $repos_root = $self->ra->{repos_root};
2710 my $url = $self->ra->{url};
Mattias Nissler0b2af452009-07-07 01:40:02 +02002711 my $new_url = $url . $branch_from;
Eric Wong15710b62007-01-22 02:20:33 -08002712 print STDERR "Found possible branch point: ",
Simon Arlott85886162009-10-09 13:21:13 +01002713 "$new_url => ", $self->full_url, ", $r\n"
2714 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002715 $branch_from =~ s#^/##;
Mattias Nissler0b2af452009-07-07 01:40:02 +02002716 my $gs = $self->other_gs($new_url, $url,
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002717 $branch_from, $r, $self->{ref_id});
Eric Wong15710b62007-01-22 02:20:33 -08002718 my ($r0, $parent) = $gs->find_rev_before($r, 1);
Deskin Miller553589f2008-12-08 08:31:31 -05002719 {
2720 my ($base, $head);
2721 if (!defined $r0 || !defined $parent) {
2722 ($base, $head) = parse_revision_argument(0, $r);
2723 } else {
2724 if ($r0 < $r) {
2725 $gs->ra->get_log([$gs->{path}], $r0 + 1, $r, 1,
2726 0, 1, sub { $base = $_[1] - 1 });
2727 }
2728 }
2729 if (defined $base && $base <= $r) {
Eric Wongd627de62007-04-15 03:01:29 -07002730 $gs->fetch($base, $r);
2731 }
Deskin Miller553589f2008-12-08 08:31:31 -05002732 ($r0, $parent) = $gs->find_rev_before($r, 1);
Eric Wong15710b62007-01-22 02:20:33 -08002733 }
Eric Wongef70de92007-02-01 04:12:41 -08002734 if (defined $r0 && defined $parent) {
Simon Arlott85886162009-10-09 13:21:13 +01002735 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n"
2736 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002737 my $ed;
2738 if ($self->ra->can_do_switch) {
Eric Wong2e5e2482007-02-23 02:21:59 -08002739 $self->assert_index_clean($parent);
Simon Arlott85886162009-10-09 13:21:13 +01002740 print STDERR "Following parent with do_switch\n"
2741 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002742 # do_switch works with svn/trunk >= r22312, but that
Eric Wong2b27f6c2007-01-28 04:59:05 -08002743 # is not included with SVN 1.4.3 (the latest version
Eric Wong15710b62007-01-22 02:20:33 -08002744 # at the moment), so we can't rely on it
Eric Wong83c2fcf2009-02-22 20:25:00 -08002745 $self->{last_rev} = $r0;
Eric Wong15710b62007-01-22 02:20:33 -08002746 $self->{last_commit} = $parent;
Eric Wong8841b372009-02-11 01:56:58 -08002747 $ed = SVN::Git::Fetcher->new($self, $gs->{path});
Eric Wong8a603772007-01-31 02:45:50 -08002748 $gs->ra->gs_do_switch($r0, $rev, $gs,
Eric Wong15710b62007-01-22 02:20:33 -08002749 $self->full_url, $ed)
2750 or die "SVN connection failed somewhere...\n";
Steven Walter9ff74e92007-09-28 13:24:19 -04002751 } elsif ($self->ra->trees_match($new_url, $r0,
2752 $self->full_url, $rev)) {
2753 print STDERR "Trees match:\n",
2754 " $new_url\@$r0\n",
2755 " ${\$self->full_url}\@$rev\n",
Simon Arlott85886162009-10-09 13:21:13 +01002756 "Following parent with no changes\n"
2757 unless $::_q > 1;
Steven Walter9ff74e92007-09-28 13:24:19 -04002758 $self->tmp_index_do(sub {
2759 command_noisy('read-tree', $parent);
2760 });
2761 $self->{last_commit} = $parent;
Eric Wong15710b62007-01-22 02:20:33 -08002762 } else {
Simon Arlott85886162009-10-09 13:21:13 +01002763 print STDERR "Following parent with do_update\n"
2764 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002765 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08002766 $self->ra->gs_do_update($rev, $rev, $self, $ed)
Eric Wong15710b62007-01-22 02:20:33 -08002767 or die "SVN connection failed somewhere...\n";
2768 }
Simon Arlott85886162009-10-09 13:21:13 +01002769 print STDERR "Successfully followed parent\n" unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002770 return $self->make_log_entry($rev, [$parent], $ed);
2771 }
Eric Wong15710b62007-01-22 02:20:33 -08002772 return undef;
2773}
2774
Eric Wong9b981fc2007-01-11 12:14:21 -08002775sub do_fetch {
Eric Wong706587f2007-01-18 17:50:01 -08002776 my ($self, $paths, $rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08002777 my $ed;
Eric Wong9b981fc2007-01-11 12:14:21 -08002778 my ($last_rev, @parents);
Eric Wongb9dffd82007-02-09 01:28:30 -08002779 if (my $lc = $self->last_commit) {
2780 # we can have a branch that was deleted, then re-added
2781 # under the same name but copied from another path, in
2782 # which case we'll have multiple parents (we don't
2783 # want to break the original ref, nor lose copypath info):
2784 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2785 push @{$log_entry->{parents}}, $lc;
2786 return $log_entry;
2787 }
Eric Wong15710b62007-01-22 02:20:33 -08002788 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002789 $last_rev = $self->{last_rev};
Eric Wongb9dffd82007-02-09 01:28:30 -08002790 $ed->{c} = $lc;
2791 @parents = ($lc);
Eric Wong9b981fc2007-01-11 12:14:21 -08002792 } else {
2793 $last_rev = $rev;
Eric Wong15710b62007-01-22 02:20:33 -08002794 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2795 return $log_entry;
2796 }
2797 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002798 }
Eric Wong8a603772007-01-31 02:45:50 -08002799 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002800 die "SVN connection failed somewhere...\n";
2801 }
2802 $self->make_log_entry($rev, \@parents, $ed);
2803}
2804
Eric Wong6111b932009-11-15 18:57:16 -08002805sub mkemptydirs {
2806 my ($self, $r) = @_;
Eric Wong6111b932009-11-15 18:57:16 -08002807
Eric Wonga5b80d92009-12-19 13:49:00 -08002808 sub scan {
2809 my ($r, $empty_dirs, $line) = @_;
2810 if (defined $r && $line =~ /^r(\d+)$/) {
2811 return 0 if $1 > $r;
2812 } elsif ($line =~ /^ \+empty_dir: (.+)$/) {
2813 $empty_dirs->{$1} = 1;
2814 } elsif ($line =~ /^ \-empty_dir: (.+)$/) {
2815 my @d = grep {m[^\Q$1\E(/|$)]} (keys %$empty_dirs);
2816 delete @$empty_dirs{@d};
2817 }
2818 1; # continue
2819 };
2820
2821 my %empty_dirs = ();
2822 my $gz_file = "$self->{dir}/unhandled.log.gz";
2823 if (-f $gz_file) {
2824 if (!$can_compress) {
2825 warn "Compress::Zlib could not be found; ",
2826 "empty directories in $gz_file will not be read\n";
2827 } else {
2828 my $gz = Compress::Zlib::gzopen($gz_file, "rb") or
2829 die "Unable to open $gz_file: $!\n";
2830 my $line;
2831 while ($gz->gzreadline($line) > 0) {
2832 scan($r, \%empty_dirs, $line) or last;
2833 }
2834 $gz->gzclose;
Eric Wong6111b932009-11-15 18:57:16 -08002835 }
2836 }
Eric Wonga5b80d92009-12-19 13:49:00 -08002837
2838 if (open my $fh, '<', "$self->{dir}/unhandled.log") {
2839 binmode $fh or croak "binmode: $!";
2840 while (<$fh>) {
2841 scan($r, \%empty_dirs, $_) or last;
2842 }
2843 close $fh;
2844 }
Eric Wong9be30ee2009-11-22 18:11:32 -08002845
2846 my $strip = qr/\A\Q$self->{path}\E(?:\/|$)/;
Eric Wong6111b932009-11-15 18:57:16 -08002847 foreach my $d (sort keys %empty_dirs) {
2848 $d = uri_decode($d);
Eric Wong9be30ee2009-11-22 18:11:32 -08002849 $d =~ s/$strip//;
Michael J. Kiwala7c42e392010-06-01 16:24:57 -05002850 next unless length($d);
Eric Wong6111b932009-11-15 18:57:16 -08002851 next if -d $d;
Michael J. Kiwala7c42e392010-06-01 16:24:57 -05002852 if (-e $d) {
Eric Wong6111b932009-11-15 18:57:16 -08002853 warn "$d exists but is not a directory\n";
2854 } else {
2855 print "creating empty directory: $d\n";
2856 mkpath([$d]);
2857 }
2858 }
2859}
2860
Eric Wong97f69872007-01-25 11:53:13 -08002861sub get_untracked {
2862 my ($self, $ed) = @_;
2863 my @out;
2864 my $h = $ed->{empty};
Eric Wong9b981fc2007-01-11 12:14:21 -08002865 foreach (sort keys %$h) {
2866 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
Eric Wong97f69872007-01-25 11:53:13 -08002867 push @out, " $act: " . uri_encode($_);
Eric Wong9b981fc2007-01-11 12:14:21 -08002868 warn "W: $act: $_\n";
2869 }
2870 foreach my $t (qw/dir_prop file_prop/) {
Eric Wong97f69872007-01-25 11:53:13 -08002871 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002872 foreach my $path (sort keys %$h) {
2873 my $ppath = $path eq '' ? '.' : $path;
2874 foreach my $prop (sort keys %{$h->{$path}}) {
Eric Wong1ce255d2007-01-14 23:21:16 -08002875 next if $SKIP_PROP{$prop};
Eric Wong9b981fc2007-01-11 12:14:21 -08002876 my $v = $h->{$path}->{$prop};
Eric Wong97f69872007-01-25 11:53:13 -08002877 my $t_ppath_prop = "$t: " .
2878 uri_encode($ppath) . ' ' .
2879 uri_encode($prop);
Eric Wong9b981fc2007-01-11 12:14:21 -08002880 if (defined $v) {
Eric Wong97f69872007-01-25 11:53:13 -08002881 push @out, " +$t_ppath_prop " .
2882 uri_encode($v);
Eric Wong9b981fc2007-01-11 12:14:21 -08002883 } else {
Eric Wong97f69872007-01-25 11:53:13 -08002884 push @out, " -$t_ppath_prop";
Eric Wong9b981fc2007-01-11 12:14:21 -08002885 }
2886 }
2887 }
2888 }
2889 foreach my $t (qw/absent_file absent_directory/) {
Eric Wong97f69872007-01-25 11:53:13 -08002890 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002891 foreach my $parent (sort keys %$h) {
2892 foreach my $path (sort @{$h->{$parent}}) {
Eric Wong97f69872007-01-25 11:53:13 -08002893 push @out, " $t: " .
2894 uri_encode("$parent/$path");
Eric Wong9b981fc2007-01-11 12:14:21 -08002895 warn "W: $t: $parent/$path ",
2896 "Insufficient permissions?\n";
2897 }
2898 }
2899 }
Eric Wong97f69872007-01-25 11:53:13 -08002900 \@out;
Eric Wong9b981fc2007-01-11 12:14:21 -08002901}
2902
Pete Harlane82f0d72009-01-17 20:10:14 -08002903# parse_svn_date(DATE)
2904# --------------------
2905# Given a date (in UTC) from Subversion, return a string in the format
2906# "<TZ Offset> <local date/time>" that Git will use.
2907#
2908# By default the parsed date will be in UTC; if $Git::SVN::_localtime
2909# is true we'll convert it to the local timezone instead.
Eric Wong1c8443b2007-01-14 02:17:00 -08002910sub parse_svn_date {
2911 my $date = shift || return '+0000 1970-01-01 00:00:00';
2912 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
Junio C Hamanob94ead72009-02-18 10:48:01 -08002913 (\d\d)\:(\d\d)\:(\d\d)\.\d*Z$/x) or
Eric Wong1c8443b2007-01-14 02:17:00 -08002914 croak "Unable to parse date: $date\n";
Pete Harlane82f0d72009-01-17 20:10:14 -08002915 my $parsed_date; # Set next.
2916
2917 if ($Git::SVN::_localtime) {
2918 # Translate the Subversion datetime to an epoch time.
2919 # Begin by switching ourselves to $date's timezone, UTC.
2920 my $old_env_TZ = $ENV{TZ};
2921 $ENV{TZ} = 'UTC';
2922
2923 my $epoch_in_UTC =
2924 POSIX::strftime('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
2925
2926 # Determine our local timezone (including DST) at the
2927 # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the
2928 # value of TZ, if any, at the time we were run.
2929 if (defined $Git::SVN::Log::TZ) {
2930 $ENV{TZ} = $Git::SVN::Log::TZ;
2931 } else {
2932 delete $ENV{TZ};
2933 }
2934
2935 my $our_TZ =
2936 POSIX::strftime('%Z', $S, $M, $H, $d, $m - 1, $Y - 1900);
2937
2938 # This converts $epoch_in_UTC into our local timezone.
2939 my ($sec, $min, $hour, $mday, $mon, $year,
2940 $wday, $yday, $isdst) = localtime($epoch_in_UTC);
2941
2942 $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
2943 $our_TZ, $year + 1900, $mon + 1,
2944 $mday, $hour, $min, $sec);
2945
2946 # Reset us to the timezone in effect when we entered
2947 # this routine.
2948 if (defined $old_env_TZ) {
2949 $ENV{TZ} = $old_env_TZ;
2950 } else {
2951 delete $ENV{TZ};
2952 }
2953 } else {
2954 $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
2955 }
2956
2957 return $parsed_date;
Eric Wong1c8443b2007-01-14 02:17:00 -08002958}
2959
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002960sub other_gs {
Mattias Nissler0b2af452009-07-07 01:40:02 +02002961 my ($self, $new_url, $url,
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002962 $branch_from, $r, $old_ref_id) = @_;
Mattias Nissler0b2af452009-07-07 01:40:02 +02002963 my $gs = Git::SVN->find_by_url($new_url, $url, $branch_from);
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002964 unless ($gs) {
2965 my $ref_id = $old_ref_id;
David D. Kilzer54fb7f92010-08-15 06:15:54 -07002966 $ref_id =~ s/\@\d+-*$//;
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002967 $ref_id .= "\@$r";
2968 # just grow a tail if we're not unique enough :x
2969 $ref_id .= '-' while find_ref($ref_id);
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002970 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
2971 if ($u =~ s#^\Q$url\E(/|$)##) {
2972 $p = $u;
2973 $u = $url;
2974 $repo_id = $self->{repo_id};
2975 }
David D. Kilzer3235b702010-08-15 06:15:55 -07002976 while (1) {
2977 # It is possible to tag two different subdirectories at
2978 # the same revision. If the url for an existing ref
2979 # does not match, we must either find a ref with a
2980 # matching url or create a new ref by growing a tail.
2981 $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1);
2982 my (undef, $max_commit) = $gs->rev_map_max(1);
2983 last if (!$max_commit);
2984 my ($url) = ::cmt_metadata($max_commit);
2985 last if ($url eq $gs->full_url);
2986 $ref_id .= '-';
2987 }
2988 print STDERR "Initializing parent: $ref_id\n" unless $::_q > 1;
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002989 }
2990 $gs
2991}
2992
Mark Lodato36db1ed2009-05-14 21:27:15 -04002993sub call_authors_prog {
2994 my ($orig_author) = @_;
Mark Lodatod3d7d472009-09-12 20:33:23 -04002995 $orig_author = command_oneline('rev-parse', '--sq-quote', $orig_author);
Mark Lodato36db1ed2009-05-14 21:27:15 -04002996 my $author = `$::_authors_prog $orig_author`;
2997 if ($? != 0) {
2998 die "$::_authors_prog failed with exit code $?\n"
2999 }
3000 if ($author =~ /^\s*(.+?)\s*<(.*)>\s*$/) {
3001 my ($name, $email) = ($1, $2);
3002 $email = undef if length $2 == 0;
3003 return [$name, $email];
3004 } else {
3005 die "Author: $orig_author: $::_authors_prog returned "
3006 . "invalid author format: $author\n";
3007 }
3008}
3009
Eric Wong1c8443b2007-01-14 02:17:00 -08003010sub check_author {
3011 my ($author) = @_;
3012 if (!defined $author || length $author == 0) {
3013 $author = '(no author)';
Mark Lodato36db1ed2009-05-14 21:27:15 -04003014 }
3015 if (!defined $::users{$author}) {
3016 if (defined $::_authors_prog) {
3017 $::users{$author} = call_authors_prog($author);
3018 } elsif (defined $::_authors) {
3019 die "Author: $author not defined in $::_authors file\n";
3020 }
Eric Wong1c8443b2007-01-14 02:17:00 -08003021 }
3022 $author;
3023}
3024
Sam Vilainf1264bd2009-10-20 15:42:01 +13003025sub find_extra_svk_parents {
3026 my ($self, $ed, $tickets, $parents) = @_;
3027 # aha! svk:merge property changed...
3028 my @tickets = split "\n", $tickets;
3029 my @known_parents;
3030 for my $ticket ( @tickets ) {
3031 my ($uuid, $path, $rev) = split /:/, $ticket;
3032 if ( $uuid eq $self->ra_uuid ) {
Tuomas Suutaribf60fff2010-02-24 20:09:01 +02003033 my $url = $self->{url};
Sam Vilainf1264bd2009-10-20 15:42:01 +13003034 my $repos_root = $url;
3035 my $branch_from = $path;
3036 $branch_from =~ s{^/}{};
3037 my $gs = $self->other_gs($repos_root."/".$branch_from,
3038 $url,
3039 $branch_from,
3040 $rev,
3041 $self->{ref_id});
3042 if ( my $commit = $gs->rev_map_get($rev, $uuid) ) {
3043 # wahey! we found it, but it might be
3044 # an old one (!)
Alex Vandivere9e4c8b2009-11-29 02:20:21 -05003045 push @known_parents, [ $rev, $commit ];
Sam Vilainf1264bd2009-10-20 15:42:01 +13003046 }
3047 }
3048 }
Alex Vandivere9e4c8b2009-11-29 02:20:21 -05003049 # Ordering matters; highest-numbered commit merge tickets
3050 # first, as they may account for later merge ticket additions
3051 # or changes.
3052 @known_parents = map {$_->[1]} sort {$b->[0] <=> $a->[0]} @known_parents;
Sam Vilainf1264bd2009-10-20 15:42:01 +13003053 for my $parent ( @known_parents ) {
3054 my @cmd = ('rev-list', $parent, map { "^$_" } @$parents );
3055 my ($msg_fh, $ctx) = command_output_pipe(@cmd);
3056 my $new;
3057 while ( <$msg_fh> ) {
3058 $new=1;last;
3059 }
3060 command_close_pipe($msg_fh, $ctx);
3061 if ( $new ) {
3062 print STDERR
3063 "Found merge parent (svk:merge ticket): $parent\n";
3064 push @$parents, $parent;
3065 }
3066 }
3067}
3068
Sam Vilain7d944c32009-12-20 00:55:13 +13003069sub lookup_svn_merge {
3070 my $uuid = shift;
3071 my $url = shift;
3072 my $merge = shift;
3073
3074 my ($source, $revs) = split ":", $merge;
3075 my $path = $source;
3076 $path =~ s{^/}{};
3077 my $gs = Git::SVN->find_by_url($url.$source, $url, $path);
3078 if ( !$gs ) {
3079 warn "Couldn't find revmap for $url$source\n";
3080 return;
3081 }
3082 my @ranges = split ",", $revs;
3083 my ($tip, $tip_commit);
3084 my @merged_commit_ranges;
3085 # find the tip
3086 for my $range ( @ranges ) {
3087 my ($bottom, $top) = split "-", $range;
3088 $top ||= $bottom;
Sam Vilain33973a52009-12-20 05:22:42 +13003089 my $bottom_commit = $gs->find_rev_after( $bottom, 1, $top );
3090 my $top_commit = $gs->find_rev_before( $top, 1, $bottom );
Sam Vilain7d944c32009-12-20 00:55:13 +13003091
3092 unless ($top_commit and $bottom_commit) {
3093 warn "W:unknown path/rev in svn:mergeinfo "
3094 ."dirprop: $source:$range\n";
3095 next;
3096 }
3097
3098 push @merged_commit_ranges,
Sam Vilain33973a52009-12-20 05:22:42 +13003099 "$bottom_commit^..$top_commit";
Sam Vilain7d944c32009-12-20 00:55:13 +13003100
3101 if ( !defined $tip or $top > $tip ) {
3102 $tip = $top;
3103 $tip_commit = $top_commit;
3104 }
3105 }
3106 return ($tip_commit, @merged_commit_ranges);
3107}
Sam Vilain7a955a52009-12-20 05:26:26 +13003108
3109sub _rev_list {
3110 my ($msg_fh, $ctx) = command_output_pipe(
3111 "rev-list", @_,
3112 );
3113 my @rv;
3114 while ( <$msg_fh> ) {
3115 chomp;
3116 push @rv, $_;
3117 }
3118 command_close_pipe($msg_fh, $ctx);
3119 @rv;
3120}
3121
3122sub check_cherry_pick {
3123 my $base = shift;
3124 my $tip = shift;
Steven Waltera3c75052010-09-02 18:32:06 -04003125 my $parents = shift;
Sam Vilain7a955a52009-12-20 05:26:26 +13003126 my @ranges = @_;
3127 my %commits = map { $_ => 1 }
Steven Waltera3c75052010-09-02 18:32:06 -04003128 _rev_list("--no-merges", $tip, "--not", $base, @$parents);
Sam Vilain7a955a52009-12-20 05:26:26 +13003129 for my $range ( @ranges ) {
3130 delete @commits{_rev_list($range)};
3131 }
Andrew Myrick1cef6502010-01-06 16:25:21 -08003132 for my $commit (keys %commits) {
3133 if (has_no_changes($commit)) {
3134 delete $commits{$commit};
3135 }
3136 }
Sam Vilain7a955a52009-12-20 05:26:26 +13003137 return (keys %commits);
3138}
3139
Andrew Myrick1cef6502010-01-06 16:25:21 -08003140sub has_no_changes {
3141 my $commit = shift;
3142
3143 my @revs = split / /, command_oneline(
3144 qw(rev-list --parents -1 -m), $commit);
3145
3146 # Commits with no parents, e.g. the start of a partial branch,
3147 # have changes by definition.
3148 return 1 if (@revs < 2);
3149
3150 # Commits with multiple parents, e.g a merge, have no changes
3151 # by definition.
3152 return 0 if (@revs > 2);
3153
3154 return (command_oneline("rev-parse", "$commit^{tree}") eq
3155 command_oneline("rev-parse", "$commit~1^{tree}"));
3156}
3157
Andrew Myrick8bff7c52010-01-30 03:14:22 +00003158# The GIT_DIR environment variable is not always set until after the command
3159# line arguments are processed, so we can't memoize in a BEGIN block.
3160{
3161 my $memoized = 0;
3162
3163 sub memoize_svn_mergeinfo_functions {
3164 return if $memoized;
3165 $memoized = 1;
3166
3167 my $cache_path = "$ENV{GIT_DIR}/svn/.caches/";
3168 mkpath([$cache_path]) unless -d $cache_path;
3169
3170 tie my %lookup_svn_merge_cache => 'Memoize::Storable',
3171 "$cache_path/lookup_svn_merge.db", 'nstore';
3172 memoize 'lookup_svn_merge',
3173 SCALAR_CACHE => 'FAULT',
3174 LIST_CACHE => ['HASH' => \%lookup_svn_merge_cache],
3175 ;
3176
3177 tie my %check_cherry_pick_cache => 'Memoize::Storable',
3178 "$cache_path/check_cherry_pick.db", 'nstore';
3179 memoize 'check_cherry_pick',
3180 SCALAR_CACHE => 'FAULT',
3181 LIST_CACHE => ['HASH' => \%check_cherry_pick_cache],
3182 ;
3183
3184 tie my %has_no_changes_cache => 'Memoize::Storable',
3185 "$cache_path/has_no_changes.db", 'nstore';
3186 memoize 'has_no_changes',
3187 SCALAR_CACHE => ['HASH' => \%has_no_changes_cache],
3188 LIST_CACHE => 'FAULT',
3189 ;
3190 }
Sergey Vlasov8ac3a662010-07-18 16:17:49 +04003191
3192 sub unmemoize_svn_mergeinfo_functions {
3193 return if not $memoized;
3194 $memoized = 0;
3195
3196 Memoize::unmemoize 'lookup_svn_merge';
3197 Memoize::unmemoize 'check_cherry_pick';
3198 Memoize::unmemoize 'has_no_changes';
3199 }
3200}
3201
3202END {
3203 # Force cache writeout explicitly instead of waiting for
3204 # global destruction to avoid segfault in Storable:
3205 # http://rt.cpan.org/Public/Bug/Display.html?id=36087
3206 unmemoize_svn_mergeinfo_functions();
Sam Vilain7d944c32009-12-20 00:55:13 +13003207}
3208
Sam Vilainea020cb2009-12-20 05:25:31 +13003209sub parents_exclude {
3210 my $parents = shift;
3211 my @commits = @_;
3212 return unless @commits;
3213
3214 my @excluded;
3215 my $excluded;
3216 do {
3217 my @cmd = ('rev-list', "-1", @commits, "--not", @$parents );
3218 $excluded = command_oneline(@cmd);
3219 if ( $excluded ) {
3220 my @new;
3221 my $found;
3222 for my $commit ( @commits ) {
3223 if ( $commit eq $excluded ) {
3224 push @excluded, $commit;
3225 $found++;
3226 last;
3227 }
3228 else {
3229 push @new, $commit;
3230 }
3231 }
3232 die "saw commit '$excluded' in rev-list output, "
3233 ."but we didn't ask for that commit (wanted: @commits --not @$parents)"
3234 unless $found;
3235 @commits = @new;
3236 }
3237 }
3238 while ($excluded and @commits);
3239
3240 return @excluded;
3241}
3242
3243
Sam Vilaindff589e2009-10-20 15:42:03 +13003244# note: this function should only be called if the various dirprops
3245# have actually changed
3246sub find_extra_svn_parents {
3247 my ($self, $ed, $mergeinfo, $parents) = @_;
3248 # aha! svk:merge property changed...
3249
Andrew Myrick8bff7c52010-01-30 03:14:22 +00003250 memoize_svn_mergeinfo_functions();
3251
Sam Vilaindff589e2009-10-20 15:42:03 +13003252 # We first search for merged tips which are not in our
3253 # history. Then, we figure out which git revisions are in
3254 # that tip, but not this revision. If all of those revisions
3255 # are now marked as merge, we can add the tip as a parent.
3256 my @merges = split "\n", $mergeinfo;
3257 my @merge_tips;
Tuomas Suutaribf60fff2010-02-24 20:09:01 +02003258 my $url = $self->{url};
Sam Vilain7d944c32009-12-20 00:55:13 +13003259 my $uuid = $self->ra_uuid;
Sam Vilainea020cb2009-12-20 05:25:31 +13003260 my %ranges;
Sam Vilaindff589e2009-10-20 15:42:03 +13003261 for my $merge ( @merges ) {
Sam Vilain7d944c32009-12-20 00:55:13 +13003262 my ($tip_commit, @ranges) =
3263 lookup_svn_merge( $uuid, $url, $merge );
Sam Vilaindff589e2009-10-20 15:42:03 +13003264 unless (!$tip_commit or
3265 grep { $_ eq $tip_commit } @$parents ) {
3266 push @merge_tips, $tip_commit;
Sam Vilainea020cb2009-12-20 05:25:31 +13003267 $ranges{$tip_commit} = \@ranges;
Sam Vilaindff589e2009-10-20 15:42:03 +13003268 } else {
3269 push @merge_tips, undef;
3270 }
3271 }
Sam Vilainea020cb2009-12-20 05:25:31 +13003272
3273 my %excluded = map { $_ => 1 }
3274 parents_exclude($parents, grep { defined } @merge_tips);
3275
3276 # check merge tips for new parents
3277 my @new_parents;
Sam Vilaindff589e2009-10-20 15:42:03 +13003278 for my $merge_tip ( @merge_tips ) {
3279 my $spec = shift @merges;
Sam Vilainea020cb2009-12-20 05:25:31 +13003280 next unless $merge_tip and $excluded{$merge_tip};
3281
3282 my $ranges = $ranges{$merge_tip};
3283
Sam Vilain7a955a52009-12-20 05:26:26 +13003284 # check out 'new' tips
Andrew Myrick41c01692010-01-06 16:25:22 -08003285 my $merge_base;
3286 eval {
3287 $merge_base = command_oneline(
3288 "merge-base",
3289 @$parents, $merge_tip,
3290 );
3291 };
3292 if ($@) {
3293 die "An error occurred during merge-base"
3294 unless $@->isa("Git::Error::Command");
3295
3296 warn "W: Cannot find common ancestor between ".
3297 "@$parents and $merge_tip. Ignoring merge info.\n";
3298 next;
3299 }
Sam Vilain7a955a52009-12-20 05:26:26 +13003300
3301 # double check that there are no missing non-merge commits
3302 my (@incomplete) = check_cherry_pick(
3303 $merge_base, $merge_tip,
Steven Waltera3c75052010-09-02 18:32:06 -04003304 $parents,
Sam Vilain7a955a52009-12-20 05:26:26 +13003305 @$ranges,
3306 );
3307
3308 if ( @incomplete ) {
3309 warn "W:svn cherry-pick ignored ($spec) - missing "
3310 .@incomplete." commit(s) (eg $incomplete[0])\n";
3311 } else {
3312 warn
3313 "Found merge parent (svn:mergeinfo prop): ",
3314 $merge_tip, "\n";
3315 push @new_parents, $merge_tip;
Sam Vilaindff589e2009-10-20 15:42:03 +13003316 }
Sam Vilain7a955a52009-12-20 05:26:26 +13003317 }
3318
3319 # cater for merges which merge commits from multiple branches
3320 if ( @new_parents > 1 ) {
3321 for ( my $i = 0; $i <= $#new_parents; $i++ ) {
3322 for ( my $j = 0; $j <= $#new_parents; $j++ ) {
3323 next if $i == $j;
3324 next unless $new_parents[$i];
3325 next unless $new_parents[$j];
3326 my $revs = command_oneline(
Eric Wong0fe19752009-12-22 12:15:40 -08003327 "rev-list", "-1",
3328 "$new_parents[$i]..$new_parents[$j]",
Sam Vilain7a955a52009-12-20 05:26:26 +13003329 );
3330 if ( !$revs ) {
Tuomas Suutari6a2009e2010-02-22 20:12:53 +02003331 undef($new_parents[$j]);
Sam Vilain7a955a52009-12-20 05:26:26 +13003332 }
Sam Vilaindff589e2009-10-20 15:42:03 +13003333 }
3334 }
3335 }
Sam Vilain7a955a52009-12-20 05:26:26 +13003336 push @$parents, grep { defined } @new_parents;
Sam Vilaindff589e2009-10-20 15:42:03 +13003337}
3338
Eric Wong9b981fc2007-01-11 12:14:21 -08003339sub make_log_entry {
Eric Wong97f69872007-01-25 11:53:13 -08003340 my ($self, $rev, $parents, $ed) = @_;
3341 my $untracked = $self->get_untracked($ed);
3342
Sam Vilainf1264bd2009-10-20 15:42:01 +13003343 my @parents = @$parents;
3344 my $ps = $ed->{path_strip} || "";
3345 for my $path ( grep { m/$ps/ } %{$ed->{dir_prop}} ) {
3346 my $props = $ed->{dir_prop}{$path};
3347 if ( $props->{"svk:merge"} ) {
3348 $self->find_extra_svk_parents
3349 ($ed, $props->{"svk:merge"}, \@parents);
3350 }
Sam Vilaindff589e2009-10-20 15:42:03 +13003351 if ( $props->{"svn:mergeinfo"} ) {
3352 $self->find_extra_svn_parents
3353 ($ed,
3354 $props->{"svn:mergeinfo"},
3355 \@parents);
3356 }
Sam Vilainf1264bd2009-10-20 15:42:01 +13003357 }
3358
Eric Wong9b981fc2007-01-11 12:14:21 -08003359 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08003360 print $un "r$rev\n" or croak $!;
3361 print $un $_, "\n" foreach @$untracked;
Sam Vilainf1264bd2009-10-20 15:42:01 +13003362 my %log_entry = ( parents => \@parents, revision => $rev,
Eric Wong97f69872007-01-25 11:53:13 -08003363 log => '');
Eric Wongfbcc1732007-02-06 18:35:30 -08003364
Eric Wong8a49ee92007-02-10 20:46:50 -08003365 my $headrev;
Eric Wongfbcc1732007-02-06 18:35:30 -08003366 my $logged = delete $self->{logged_rev_props};
Eric Wong8a49ee92007-02-10 20:46:50 -08003367 if (!$logged || $self->{-want_revprops}) {
Eric Wongfbcc1732007-02-06 18:35:30 -08003368 my $rp = $self->ra->rev_proplist($rev);
3369 foreach (sort keys %$rp) {
3370 my $v = $rp->{$_};
3371 if (/^svn:(author|date|log)$/) {
3372 $log_entry{$1} = $v;
Eric Wong8a49ee92007-02-10 20:46:50 -08003373 } elsif ($_ eq 'svm:headrev') {
3374 $headrev = $v;
Eric Wongfbcc1732007-02-06 18:35:30 -08003375 } else {
3376 print $un " rev_prop: ", uri_encode($_), ' ',
3377 uri_encode($v), "\n";
3378 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003379 }
Eric Wongfbcc1732007-02-06 18:35:30 -08003380 } else {
3381 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
Eric Wong9b981fc2007-01-11 12:14:21 -08003382 }
3383 close $un or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08003384
Eric Wong9b981fc2007-01-11 12:14:21 -08003385 $log_entry{date} = parse_svn_date($log_entry{date});
Eric Wong9b981fc2007-01-11 12:14:21 -08003386 $log_entry{log} .= "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08003387 my $author = $log_entry{author} = check_author($log_entry{author});
3388 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003389 : ($author, undef);
3390
3391 my ($commit_name, $commit_email) = ($name, $email);
3392 if ($_use_log_author) {
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00003393 my $name_field;
3394 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
3395 $name_field = $1;
3396 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
3397 $name_field = $1;
3398 }
3399 if (!defined $name_field) {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02003400 if (!defined $email) {
3401 $email = $name;
3402 }
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00003403 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003404 ($name, $email) = ($1, $2);
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00003405 } elsif ($name_field =~ /(.*)@/) {
3406 ($name, $email) = ($1, $name_field);
3407 } else {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02003408 ($name, $email) = ($name_field, $name_field);
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003409 }
3410 }
Eric Wong91b03282007-02-11 00:51:33 -08003411 if (defined $headrev && $self->use_svm_props) {
Eric Wongaea736c2007-02-16 19:15:21 -08003412 if ($self->rewrite_root) {
3413 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
3414 "options set!\n";
3415 }
Jay Soffian3e18ce12010-01-23 03:30:00 -05003416 if ($self->rewrite_uuid) {
3417 die "Can't have both 'useSvmProps' and 'rewriteUUID' ",
3418 "options set!\n";
3419 }
Eric Wongb3e95932009-07-11 14:13:12 -07003420 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$}i;
Eric Wongbefc9ad2007-02-17 02:53:07 -08003421 # we don't want "SVM: initializing mirror for junk" ...
3422 return undef if $r == 0;
3423 my $svm = $self->svm;
3424 if ($uuid ne $svm->{uuid}) {
Eric Wong8a49ee92007-02-10 20:46:50 -08003425 die "UUID mismatch on SVM path:\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08003426 "expected: $svm->{uuid}\n",
Eric Wong8a49ee92007-02-10 20:46:50 -08003427 " got: $uuid\n";
3428 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08003429 my $full_url = $self->full_url;
3430 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
3431 die "Failed to replace '$svm->{replace}' with ",
3432 "'$svm->{source}' in $full_url\n";
Sam Vilain18ea92b2007-02-23 12:32:29 +13003433 # throw away username for storing in records
3434 remove_username($full_url);
Eric Wong8a49ee92007-02-10 20:46:50 -08003435 $log_entry{metadata} = "$full_url\@$r $uuid";
3436 $log_entry{svm_revision} = $r;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003437 $email ||= "$author\@$uuid";
3438 $commit_email ||= "$author\@$uuid";
Eric Wong62e349d2007-02-16 19:57:29 -08003439 } elsif ($self->use_svnsync_props) {
3440 my $full_url = $self->svnsync->{url};
3441 $full_url .= "/$self->{path}" if length $self->{path};
Adam Robence118732007-04-24 18:02:07 -07003442 remove_username($full_url);
Eric Wong62e349d2007-02-16 19:57:29 -08003443 my $uuid = $self->svnsync->{uuid};
3444 $log_entry{metadata} = "$full_url\@$rev $uuid";
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003445 $email ||= "$author\@$uuid";
3446 $commit_email ||= "$author\@$uuid";
Eric Wong8a49ee92007-02-10 20:46:50 -08003447 } else {
Adam Robence118732007-04-24 18:02:07 -07003448 my $url = $self->metadata_url;
3449 remove_username($url);
Jay Soffian3e18ce12010-01-23 03:30:00 -05003450 my $uuid = $self->rewrite_uuid || $self->ra->get_uuid;
3451 $log_entry{metadata} = "$url\@$rev " . $uuid;
3452 $email ||= "$author\@" . $uuid;
3453 $commit_email ||= "$author\@" . $uuid;
Eric Wong8a49ee92007-02-10 20:46:50 -08003454 }
Eric Wongdb03cd22007-02-13 00:38:02 -08003455 $log_entry{name} = $name;
3456 $log_entry{email} = $email;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003457 $log_entry{commit_name} = $commit_name;
3458 $log_entry{commit_email} = $commit_email;
Eric Wong9b981fc2007-01-11 12:14:21 -08003459 \%log_entry;
3460}
3461
3462sub fetch {
Eric Wong3ebe8df2007-01-25 17:35:40 -08003463 my ($self, $min_rev, $max_rev, @parents) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08003464 my ($last_rev, $last_commit) = $self->last_rev_commit;
Eric Wong3ebe8df2007-01-25 17:35:40 -08003465 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
Eric Wonge5181922007-02-08 12:53:57 -08003466 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
Eric Wong9b981fc2007-01-11 12:14:21 -08003467}
3468
3469sub set_tree_cb {
3470 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
Eric Wong490f49e2007-02-10 13:58:33 -08003471 $self->{inject_parents} = { $rev => $tree };
3472 $self->fetch(undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08003473}
3474
3475sub set_tree {
3476 my ($self, $tree) = (shift, shift);
Eric Wong1ce255d2007-01-14 23:21:16 -08003477 my $log_entry = ::get_commit_entry($tree);
Eric Wong9b981fc2007-01-11 12:14:21 -08003478 unless ($self->{last_rev}) {
Luc Heinrich0a1a1c82008-09-29 15:58:18 +02003479 ::fatal("Must have an existing revision to commit");
Eric Wong9b981fc2007-01-11 12:14:21 -08003480 }
Eric Wong61395352007-01-27 14:33:08 -08003481 my %ed_opts = ( r => $self->{last_rev},
3482 log => $log_entry->{log},
3483 ra => $self->ra,
3484 tree_a => $self->{last_commit},
3485 tree_b => $tree,
3486 editor_cb => sub {
3487 $self->set_tree_cb($log_entry, $tree, @_) },
3488 svn_path => $self->{path} );
3489 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong9b981fc2007-01-11 12:14:21 -08003490 print "No changes\nr$self->{last_rev} = $tree\n";
3491 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003492}
3493
Eric Wong060610c2007-12-08 23:27:41 -08003494sub rebuild_from_rev_db {
3495 my ($self, $path) = @_;
3496 my $r = -1;
3497 open my $fh, '<', $path or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02003498 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003499 while (<$fh>) {
3500 length($_) == 41 or croak "inconsistent size in ($_) != 41";
3501 chomp($_);
3502 ++$r;
3503 next if $_ eq ('0' x 40);
3504 $self->rev_map_set($r, $_);
3505 print "r$r = $_\n";
3506 }
3507 close $fh or croak "close: $!";
3508 unlink $path or croak "unlink: $!";
3509}
3510
Eric Wongf0ecca12007-01-30 13:11:14 -08003511sub rebuild {
3512 my ($self) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003513 my $map_path = $self->map_path;
Deskin Miller2beec892008-09-15 21:12:58 -04003514 my $partial = (-e $map_path && ! -z $map_path);
Eric Wongd6d33462007-02-16 04:05:33 -08003515 return unless ::verify_ref($self->refname.'^0');
Deskin Miller2beec892008-09-15 21:12:58 -04003516 if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
Eric Wong060610c2007-12-08 23:27:41 -08003517 my $rev_db = $self->rev_db_path;
3518 $self->rebuild_from_rev_db($rev_db);
3519 if ($self->use_svm_props) {
3520 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
3521 $self->rebuild_from_rev_db($svm_rev_db);
3522 }
3523 $self->unlink_rev_db_symlink;
Eric Wong26a62d52007-02-12 13:25:25 -08003524 return;
3525 }
Deskin Miller2beec892008-09-15 21:12:58 -04003526 print "Rebuilding $map_path ...\n" if (!$partial);
3527 my ($base_rev, $head) = ($partial ? $self->rev_map_max_norebuild(1) :
3528 (undef, undef));
Eric Wong060610c2007-12-08 23:27:41 -08003529 my ($log, $ctx) =
3530 command_output_pipe(qw/rev-list --pretty=raw --no-color --reverse/,
Deskin Miller2beec892008-09-15 21:12:58 -04003531 ($head ? "$head.." : "") . $self->refname,
3532 '--');
Jan Krüger74b1e122008-06-24 02:17:36 +02003533 my $metadata_url = $self->metadata_url;
3534 remove_username($metadata_url);
Jay Soffian3e18ce12010-01-23 03:30:00 -05003535 my $svn_uuid = $self->rewrite_uuid || $self->ra_uuid;
Sam Vilain3dfab992007-06-30 20:56:13 +12003536 my $c;
3537 while (<$log>) {
3538 if ( m{^commit ($::sha1)$} ) {
3539 $c = $1;
3540 next;
3541 }
3542 next unless s{^\s*(git-svn-id:)}{$1};
3543 my ($url, $rev, $uuid) = ::extract_metadata($_);
Sam Vilain18ea92b2007-02-23 12:32:29 +13003544 remove_username($url);
Eric Wongf0ecca12007-01-30 13:11:14 -08003545
3546 # ignore merges (from set-tree)
3547 next if (!defined $rev || !$uuid);
3548
3549 # if we merged or otherwise started elsewhere, this is
3550 # how we break out of it
Eric Wong060610c2007-12-08 23:27:41 -08003551 if (($uuid ne $svn_uuid) ||
Jan Krüger74b1e122008-06-24 02:17:36 +02003552 ($metadata_url && $url && ($url ne $metadata_url))) {
Eric Wongf0ecca12007-01-30 13:11:14 -08003553 next;
3554 }
Deskin Miller2beec892008-09-15 21:12:58 -04003555 if ($partial && $head) {
3556 print "Partial-rebuilding $map_path ...\n";
3557 print "Currently at $base_rev = $head\n";
3558 $head = undef;
3559 }
Eric Wongf0ecca12007-01-30 13:11:14 -08003560
Eric Wong060610c2007-12-08 23:27:41 -08003561 $self->rev_map_set($rev, $c);
Eric Wongf0ecca12007-01-30 13:11:14 -08003562 print "r$rev = $c\n";
3563 }
Sam Vilain3dfab992007-06-30 20:56:13 +12003564 command_close_pipe($log, $ctx);
Deskin Miller2beec892008-09-15 21:12:58 -04003565 print "Done rebuilding $map_path\n" if (!$partial || !$head);
Eric Wong060610c2007-12-08 23:27:41 -08003566 my $rev_db_path = $self->rev_db_path;
3567 if (-f $self->rev_db_path) {
3568 unlink $self->rev_db_path or croak "unlink: $!";
3569 }
3570 $self->unlink_rev_db_symlink;
Eric Wongf0ecca12007-01-30 13:11:14 -08003571}
3572
Eric Wong060610c2007-12-08 23:27:41 -08003573# rev_map:
Eric Wong9b981fc2007-01-11 12:14:21 -08003574# Tie::File seems to be prone to offset errors if revisions get sparse,
3575# it's not that fast, either. Tie::File is also not in Perl 5.6. So
3576# one of my favorite modules is out :< Next up would be one of the DBM
Eric Wong060610c2007-12-08 23:27:41 -08003577# modules, but I'm not sure which is most portable...
3578#
3579# This is the replacement for the rev_db format, which was too big
3580# and inefficient for large repositories with a lot of sparse history
3581# (mainly tags)
3582#
3583# The format is this:
3584# - 24 bytes for every record,
3585# * 4 bytes for the integer representing an SVN revision number
3586# * 20 bytes representing the sha1 of a git commit
3587# - No empty padding records like the old format
Eric Wong66ab84b2007-12-08 23:27:42 -08003588# (except the last record, which can be overwritten)
Eric Wong060610c2007-12-08 23:27:41 -08003589# - new records are written append-only since SVN revision numbers
3590# increase monotonically
3591# - lookups on SVN revision number are done via a binary search
Eric Wong66ab84b2007-12-08 23:27:42 -08003592# - Piping the file to xxd -c24 is a good way of dumping it for
3593# viewing or editing (piped back through xxd -r), should the need
3594# ever arise.
3595# - The last record can be padding revision with an all-zero sha1
3596# This is used to optimize fetch performance when using multiple
3597# "fetch" directives in .git/config
Eric Wong060610c2007-12-08 23:27:41 -08003598#
Eric Wong97ae0912007-02-11 15:21:24 -08003599# These files are disposable unless noMetadata or useSvmProps is set
Eric Wong9b981fc2007-01-11 12:14:21 -08003600
Eric Wong060610c2007-12-08 23:27:41 -08003601sub _rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08003602 my ($fh, $rev, $commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003603
Michael Weber4f7ec792008-04-18 15:12:04 +02003604 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003605 my $size = (stat($fh))[7];
3606 ($size % 24) == 0 or croak "inconsistent size: $size";
3607
Eric Wong66ab84b2007-12-08 23:27:42 -08003608 my $wr_offset = 0;
Eric Wong060610c2007-12-08 23:27:41 -08003609 if ($size > 0) {
3610 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
3611 my $read = sysread($fh, my $buf, 24) or croak "read: $!";
3612 $read == 24 or croak "read only $read bytes (!= 24)";
3613 my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08003614 if ($last_commit eq ('0' x40)) {
3615 if ($size >= 48) {
3616 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
3617 $read = sysread($fh, $buf, 24) or
3618 croak "read: $!";
3619 $read == 24 or
3620 croak "read only $read bytes (!= 24)";
3621 ($last_rev, $last_commit) =
3622 unpack(rev_map_fmt, $buf);
3623 if ($last_commit eq ('0' x40)) {
3624 croak "inconsistent .rev_map\n";
3625 }
3626 }
3627 if ($last_rev >= $rev) {
3628 croak "last_rev is higher!: $last_rev >= $rev";
3629 }
3630 $wr_offset = -24;
Eric Wong47a0b752007-01-31 05:13:30 -08003631 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003632 }
Eric Wong66ab84b2007-12-08 23:27:42 -08003633 sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003634 syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or
3635 croak "write: $!";
Eric Wong26a62d52007-02-12 13:25:25 -08003636}
3637
Ben Jackson195643f2009-06-03 20:45:52 -07003638sub _rev_map_reset {
3639 my ($fh, $rev, $commit) = @_;
3640 my $c = _rev_map_get($fh, $rev);
3641 $c eq $commit or die "_rev_map_reset(@_) commit $c does not match!\n";
3642 my $offset = sysseek($fh, 0, SEEK_CUR) or croak "seek: $!";
3643 truncate $fh, $offset or croak "truncate: $!";
3644}
3645
Eric Wong26a62d52007-02-12 13:25:25 -08003646sub mkfile {
3647 my ($path) = @_;
3648 unless (-e $path) {
3649 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
3650 mkpath([$dir]) unless -d $dir;
3651 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
3652 close $fh or die "Couldn't close (create) $path: $!\n";
3653 }
3654}
3655
Eric Wong060610c2007-12-08 23:27:41 -08003656sub rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08003657 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
Jonathan Nieder70ee0b72010-05-04 16:36:47 -07003658 defined $commit or die "missing arg3\n";
Eric Wong26a62d52007-02-12 13:25:25 -08003659 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
Eric Wong060610c2007-12-08 23:27:41 -08003660 my $db = $self->map_path($uuid);
Eric Wong26a62d52007-02-12 13:25:25 -08003661 my $db_lock = "$db.lock";
3662 my $sig;
Ben Jackson195643f2009-06-03 20:45:52 -07003663 $update_ref ||= 0;
Eric Wong26a62d52007-02-12 13:25:25 -08003664 if ($update_ref) {
3665 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
3666 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
3667 }
3668 mkfile($db);
3669
3670 $LOCKFILES{$db_lock} = 1;
3671 my $sync;
3672 # both of these options make our .rev_db file very, very important
3673 # and we can't afford to lose it because rebuild() won't work
3674 if ($self->use_svm_props || $self->no_metadata) {
3675 $sync = 1;
Eric Wong060610c2007-12-08 23:27:41 -08003676 copy($db, $db_lock) or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08003677 "Failed to copy: ",
3678 "$db => $db_lock ($!)\n";
3679 } else {
Eric Wong060610c2007-12-08 23:27:41 -08003680 rename $db, $db_lock or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08003681 "Failed to rename: ",
3682 "$db => $db_lock ($!)\n";
3683 }
Eric Wong060610c2007-12-08 23:27:41 -08003684
Eric Wong66ab84b2007-12-08 23:27:42 -08003685 sysopen(my $fh, $db_lock, O_RDWR | O_CREAT)
Eric Wong060610c2007-12-08 23:27:41 -08003686 or croak "Couldn't open $db_lock: $!\n";
Ben Jackson195643f2009-06-03 20:45:52 -07003687 $update_ref eq 'reset' ? _rev_map_reset($fh, $rev, $commit) :
3688 _rev_map_set($fh, $rev, $commit);
Eric Wong97ae0912007-02-11 15:21:24 -08003689 if ($sync) {
3690 $fh->flush or die "Couldn't flush $db_lock: $!\n";
3691 $fh->sync or die "Couldn't sync $db_lock: $!\n";
3692 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003693 close $fh or croak $!;
Eric Wong373274f2007-01-31 13:54:23 -08003694 if ($update_ref) {
Eric Wong1e889ef2007-02-16 01:45:13 -08003695 $_head = $self;
Ben Jackson195643f2009-06-03 20:45:52 -07003696 my $note = "";
3697 $note = " ($update_ref)" if ($update_ref !~ /^\d*$/);
3698 command_noisy('update-ref', '-m', "r$rev$note",
Eric Wong373274f2007-01-31 13:54:23 -08003699 $self->refname, $commit);
3700 }
Eric Wong060610c2007-12-08 23:27:41 -08003701 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
Eric Wong373274f2007-01-31 13:54:23 -08003702 "$db_lock => $db ($!)\n";
3703 delete $LOCKFILES{$db_lock};
3704 if ($update_ref) {
3705 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
3706 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
3707 kill $sig, $$ if defined $sig;
3708 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003709}
3710
Eric Wong66ab84b2007-12-08 23:27:42 -08003711# If want_commit, this will return an array of (rev, commit) where
3712# commit _must_ be a valid commit in the archive.
3713# Otherwise, it'll return the max revision (whether or not the
3714# commit is valid or just a 0x40 placeholder).
Eric Wong060610c2007-12-08 23:27:41 -08003715sub rev_map_max {
Eric Wong66ab84b2007-12-08 23:27:42 -08003716 my ($self, $want_commit) = @_;
Eric Wongd6d33462007-02-16 04:05:33 -08003717 $self->rebuild;
Deskin Miller2beec892008-09-15 21:12:58 -04003718 my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
3719 $want_commit ? ($r, $c) : $r;
3720}
3721
3722sub rev_map_max_norebuild {
3723 my ($self, $want_commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003724 my $map_path = $self->map_path;
Eric Wong66ab84b2007-12-08 23:27:42 -08003725 stat $map_path or return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08003726 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02003727 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003728 my $size = (stat($fh))[7];
3729 ($size % 24) == 0 or croak "inconsistent size: $size";
3730
3731 if ($size == 0) {
3732 close $fh or croak "close: $!";
Eric Wong66ab84b2007-12-08 23:27:42 -08003733 return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08003734 }
3735
Eric Wong66ab84b2007-12-08 23:27:42 -08003736 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003737 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003738 my ($r, $c) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08003739 if ($want_commit && $c eq ('0' x40)) {
3740 if ($size < 48) {
3741 return $want_commit ? (0, undef) : 0;
3742 }
3743 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
3744 sysread($fh, $buf, 24) == 24 or croak "read: $!";
3745 ($r, $c) = unpack(rev_map_fmt, $buf);
3746 if ($c eq ('0'x40)) {
3747 croak "Penultimate record is all-zeroes in $map_path";
3748 }
3749 }
3750 close $fh or croak "close: $!";
3751 $want_commit ? ($r, $c) : $r;
Eric Wong9c93fee2007-01-31 17:22:31 -08003752}
3753
Eric Wong060610c2007-12-08 23:27:41 -08003754sub rev_map_get {
Eric Wong26a62d52007-02-12 13:25:25 -08003755 my ($self, $rev, $uuid) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003756 my $map_path = $self->map_path($uuid);
3757 return undef unless -e $map_path;
3758
3759 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Ben Jackson195643f2009-06-03 20:45:52 -07003760 my $c = _rev_map_get($fh, $rev);
3761 close($fh) or croak "close: $!";
3762 $c
3763}
3764
3765sub _rev_map_get {
3766 my ($fh, $rev) = @_;
3767
Michael Weber4f7ec792008-04-18 15:12:04 +02003768 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003769 my $size = (stat($fh))[7];
3770 ($size % 24) == 0 or croak "inconsistent size: $size";
3771
3772 if ($size == 0) {
Eric Wong060610c2007-12-08 23:27:41 -08003773 return undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003774 }
Eric Wong060610c2007-12-08 23:27:41 -08003775
3776 my ($l, $u) = (0, $size - 24);
3777 my ($r, $c, $buf);
3778
3779 while ($l <= $u) {
3780 my $i = int(($l/24 + $u/24) / 2) * 24;
3781 sysseek($fh, $i, SEEK_SET) or croak "seek: $!";
3782 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
Eric Wongc83f4e62009-08-12 22:20:02 -07003783 my ($r, $c) = unpack(rev_map_fmt, $buf);
Eric Wong060610c2007-12-08 23:27:41 -08003784
3785 if ($r < $rev) {
3786 $l = $i + 24;
3787 } elsif ($r > $rev) {
3788 $u = $i - 24;
3789 } else { # $r == $rev
Eric Wong66ab84b2007-12-08 23:27:42 -08003790 return $c eq ('0' x 40) ? undef : $c;
Eric Wong060610c2007-12-08 23:27:41 -08003791 }
3792 }
Eric Wong060610c2007-12-08 23:27:41 -08003793 undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003794}
3795
David D Kilzer111947e2007-11-11 22:56:52 -08003796# Finds the first svn revision that exists on (if $eq_ok is true) or
3797# before $rev for the current branch. It will not search any lower
3798# than $min_rev. Returns the git commit hash and svn revision number
3799# if found, else (undef, undef).
Eric Wong15710b62007-01-22 02:20:33 -08003800sub find_rev_before {
David D Kilzer111947e2007-11-11 22:56:52 -08003801 my ($self, $rev, $eq_ok, $min_rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08003802 --$rev unless $eq_ok;
David D Kilzer111947e2007-11-11 22:56:52 -08003803 $min_rev ||= 1;
Ben Jacksonca5e8802009-06-03 20:45:51 -07003804 my $max_rev = $self->rev_map_max;
3805 $rev = $max_rev if ($rev > $max_rev);
David D Kilzer111947e2007-11-11 22:56:52 -08003806 while ($rev >= $min_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003807 if (my $c = $self->rev_map_get($rev)) {
Eric Wong15710b62007-01-22 02:20:33 -08003808 return ($rev, $c);
3809 }
3810 --$rev;
3811 }
3812 return (undef, undef);
3813}
3814
David D Kilzer111947e2007-11-11 22:56:52 -08003815# Finds the first svn revision that exists on (if $eq_ok is true) or
3816# after $rev for the current branch. It will not search any higher
3817# than $max_rev. Returns the git commit hash and svn revision number
3818# if found, else (undef, undef).
3819sub find_rev_after {
3820 my ($self, $rev, $eq_ok, $max_rev) = @_;
3821 ++$rev unless $eq_ok;
Eric Wong060610c2007-12-08 23:27:41 -08003822 $max_rev ||= $self->rev_map_max;
David D Kilzer111947e2007-11-11 22:56:52 -08003823 while ($rev <= $max_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003824 if (my $c = $self->rev_map_get($rev)) {
David D Kilzer111947e2007-11-11 22:56:52 -08003825 return ($rev, $c);
3826 }
3827 ++$rev;
3828 }
3829 return (undef, undef);
3830}
3831
Eric Wong9b981fc2007-01-11 12:14:21 -08003832sub _new {
Eric Wong706587f2007-01-18 17:50:01 -08003833 my ($class, $repo_id, $ref_id, $path) = @_;
3834 unless (defined $repo_id && length $repo_id) {
3835 $repo_id = $Git::SVN::default_repo_id;
3836 }
3837 unless (defined $ref_id && length $ref_id) {
Adam Brewster63de84a2009-08-03 21:40:38 -04003838 $_prefix = '' unless defined($_prefix);
Adam Brewster6f5748e2009-08-11 23:14:27 -04003839 $_[2] = $ref_id =
3840 "refs/remotes/$_prefix$Git::SVN::default_ref_id";
Eric Wong706587f2007-01-18 17:50:01 -08003841 }
Eric Wong7829f202008-06-28 20:40:32 -07003842 $_[1] = $repo_id;
Eric Wong706587f2007-01-18 17:50:01 -08003843 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
Adam Brewster6f5748e2009-08-11 23:14:27 -04003844
3845 # Older repos imported by us used $GIT_DIR/svn/foo instead of
3846 # $GIT_DIR/svn/refs/remotes/foo when tracking refs/remotes/foo
3847 if ($ref_id =~ m{^refs/remotes/(.*)}) {
3848 my $old_dir = "$ENV{GIT_DIR}/svn/$1";
3849 if (-d $old_dir && ! -d $dir) {
3850 $dir = $old_dir;
3851 }
3852 }
3853
Eric Wong706587f2007-01-18 17:50:01 -08003854 $_[3] = $path = '' unless (defined $path);
Adam Brewster6f5748e2009-08-11 23:14:27 -04003855 mkpath([$dir]);
Eric Wong26a62d52007-02-12 13:25:25 -08003856 bless {
3857 ref_id => $ref_id, dir => $dir, index => "$dir/index",
Eric Wong8a49ee92007-02-10 20:46:50 -08003858 path => $path, config => "$ENV{GIT_DIR}/svn/config",
Eric Wong060610c2007-12-08 23:27:41 -08003859 map_root => "$dir/.rev_map", repo_id => $repo_id }, $class;
Eric Wong26a62d52007-02-12 13:25:25 -08003860}
3861
Eric Wong060610c2007-12-08 23:27:41 -08003862# for read-only access of old .rev_db formats
3863sub unlink_rev_db_symlink {
3864 my ($self) = @_;
3865 my $link = $self->rev_db_path;
3866 $link =~ s/\.[\w-]+$// or croak "missing UUID at the end of $link";
3867 if (-l $link) {
3868 unlink $link or croak "unlink: $link failed!";
3869 }
3870}
3871
3872sub rev_db_path {
3873 my ($self, $uuid) = @_;
3874 my $db_path = $self->map_path($uuid);
3875 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
3876 or croak "map_path: $db_path does not contain '/.rev_map.' !";
3877 $db_path;
3878}
3879
3880# the new replacement for .rev_db
3881sub map_path {
Eric Wong26a62d52007-02-12 13:25:25 -08003882 my ($self, $uuid) = @_;
3883 $uuid ||= $self->ra_uuid;
Eric Wong060610c2007-12-08 23:27:41 -08003884 "$self->{map_root}.$uuid";
Eric Wong9b981fc2007-01-11 12:14:21 -08003885}
3886
Eric Wong1c8443b2007-01-14 02:17:00 -08003887sub uri_encode {
3888 my ($f) = @_;
3889 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
3890 $f
3891}
Eric Wong9b981fc2007-01-11 12:14:21 -08003892
Eric Wong6111b932009-11-15 18:57:16 -08003893sub uri_decode {
3894 my ($f) = @_;
3895 $f =~ s#%([0-9a-fA-F]{2})#chr(hex($1))#eg;
3896 $f
3897}
3898
Sam Vilain18ea92b2007-02-23 12:32:29 +13003899sub remove_username {
3900 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
3901}
3902
Eric Wongd976acf2007-01-04 00:45:03 -08003903package Git::SVN::Prompt;
3904use strict;
3905use warnings;
3906require SVN::Core;
3907use vars qw/$_no_auth_cache $_username/;
3908
3909sub simple {
Eric Wong30d055a2006-11-24 01:38:04 -08003910 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
3911 $may_save = undef if $_no_auth_cache;
3912 $default_username = $_username if defined $_username;
3913 if (defined $default_username && length $default_username) {
3914 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003915 print STDERR "Authentication realm: $realm\n";
3916 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003917 }
3918 $cred->username($default_username);
3919 } else {
Eric Wongd976acf2007-01-04 00:45:03 -08003920 username($cred, $realm, $may_save, $pool);
Eric Wong30d055a2006-11-24 01:38:04 -08003921 }
3922 $cred->password(_read_password("Password for '" .
3923 $cred->username . "': ", $realm));
3924 $cred->may_save($may_save);
3925 $SVN::_Core::SVN_NO_ERROR;
3926}
3927
Eric Wongd976acf2007-01-04 00:45:03 -08003928sub ssl_server_trust {
Eric Wong30d055a2006-11-24 01:38:04 -08003929 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
3930 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003931 print STDERR "Error validating server certificate for '$realm':\n";
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003932 {
3933 no warnings 'once';
3934 # All variables SVN::Auth::SSL::* are used only once,
3935 # so we're shutting up Perl warnings about this.
3936 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
3937 print STDERR " - The certificate is not issued ",
3938 "by a trusted authority. Use the\n",
3939 " fingerprint to validate ",
3940 "the certificate manually!\n";
3941 }
3942 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
3943 print STDERR " - The certificate hostname ",
3944 "does not match.\n";
3945 }
3946 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
3947 print STDERR " - The certificate is not yet valid.\n";
3948 }
3949 if ($failures & $SVN::Auth::SSL::EXPIRED) {
3950 print STDERR " - The certificate has expired.\n";
3951 }
3952 if ($failures & $SVN::Auth::SSL::OTHER) {
3953 print STDERR " - The certificate has ",
3954 "an unknown error.\n";
3955 }
3956 } # no warnings 'once'
Eric Wong6f729592007-01-15 20:15:55 -08003957 printf STDERR
3958 "Certificate information:\n".
Eric Wong30d055a2006-11-24 01:38:04 -08003959 " - Hostname: %s\n".
3960 " - Valid: from %s until %s\n".
3961 " - Issuer: %s\n".
3962 " - Fingerprint: %s\n",
3963 map $cert_info->$_, qw(hostname valid_from valid_until
Eric Wong6f729592007-01-15 20:15:55 -08003964 issuer_dname fingerprint);
Eric Wong30d055a2006-11-24 01:38:04 -08003965 my $choice;
3966prompt:
Eric Wong6f729592007-01-15 20:15:55 -08003967 print STDERR $may_save ?
Eric Wong30d055a2006-11-24 01:38:04 -08003968 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
3969 "(R)eject or accept (t)emporarily? ";
Eric Wong6f729592007-01-15 20:15:55 -08003970 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003971 $choice = lc(substr(<STDIN> || 'R', 0, 1));
3972 if ($choice =~ /^t$/i) {
3973 $cred->may_save(undef);
3974 } elsif ($choice =~ /^r$/i) {
3975 return -1;
3976 } elsif ($may_save && $choice =~ /^p$/i) {
3977 $cred->may_save($may_save);
3978 } else {
3979 goto prompt;
3980 }
3981 $cred->accepted_failures($failures);
3982 $SVN::_Core::SVN_NO_ERROR;
3983}
3984
Eric Wongd976acf2007-01-04 00:45:03 -08003985sub ssl_client_cert {
Eric Wong30d055a2006-11-24 01:38:04 -08003986 my ($cred, $realm, $may_save, $pool) = @_;
3987 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003988 print STDERR "Client certificate filename: ";
3989 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003990 chomp(my $filename = <STDIN>);
3991 $cred->cert_file($filename);
3992 $cred->may_save($may_save);
3993 $SVN::_Core::SVN_NO_ERROR;
3994}
3995
Eric Wongd976acf2007-01-04 00:45:03 -08003996sub ssl_client_cert_pw {
Eric Wong30d055a2006-11-24 01:38:04 -08003997 my ($cred, $realm, $may_save, $pool) = @_;
3998 $may_save = undef if $_no_auth_cache;
3999 $cred->password(_read_password("Password: ", $realm));
4000 $cred->may_save($may_save);
4001 $SVN::_Core::SVN_NO_ERROR;
4002}
4003
Eric Wongd976acf2007-01-04 00:45:03 -08004004sub username {
Eric Wong30d055a2006-11-24 01:38:04 -08004005 my ($cred, $realm, $may_save, $pool) = @_;
4006 $may_save = undef if $_no_auth_cache;
4007 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08004008 print STDERR "Authentication realm: $realm\n";
Eric Wong30d055a2006-11-24 01:38:04 -08004009 }
4010 my $username;
4011 if (defined $_username) {
4012 $username = $_username;
4013 } else {
Eric Wong6f729592007-01-15 20:15:55 -08004014 print STDERR "Username: ";
4015 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08004016 chomp($username = <STDIN>);
4017 }
4018 $cred->username($username);
4019 $cred->may_save($may_save);
4020 $SVN::_Core::SVN_NO_ERROR;
4021}
4022
4023sub _read_password {
4024 my ($prompt, $realm) = @_;
Eric Wong30d055a2006-11-24 01:38:04 -08004025 my $password = '';
Frank Li56a853b2010-03-02 19:47:52 +08004026 if (exists $ENV{GIT_ASKPASS}) {
4027 open(PH, "-|", $ENV{GIT_ASKPASS}, $prompt);
4028 $password = <PH>;
4029 $password =~ s/[\012\015]//; # \n\r
4030 close(PH);
4031 } else {
4032 print STDERR $prompt;
4033 STDERR->flush;
4034 require Term::ReadKey;
4035 Term::ReadKey::ReadMode('noecho');
4036 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
4037 last if $key =~ /[\012\015]/; # \n\r
4038 $password .= $key;
4039 }
4040 Term::ReadKey::ReadMode('restore');
4041 print STDERR "\n";
4042 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08004043 }
Eric Wong30d055a2006-11-24 01:38:04 -08004044 $password;
4045}
4046
Eric Wong27a1a802006-11-27 21:44:48 -08004047package SVN::Git::Fetcher;
4048use vars qw/@ISA/;
4049use strict;
4050use warnings;
4051use Carp qw/croak/;
4052use IO::File qw//;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02004053use vars qw/$_ignore_regex/;
Eric Wong27a1a802006-11-27 21:44:48 -08004054
4055# file baton members: path, mode_a, mode_b, pool, fh, blob, base
4056sub new {
Eric Wong8841b372009-02-11 01:56:58 -08004057 my ($class, $git_svn, $switch_path) = @_;
Eric Wong27a1a802006-11-27 21:44:48 -08004058 my $self = SVN::Delta::Editor->new;
4059 bless $self, $class;
Eric Wongdbc6c742009-01-11 16:51:10 -08004060 if (exists $git_svn->{last_commit}) {
4061 $self->{c} = $git_svn->{last_commit};
Eric Wong8841b372009-02-11 01:56:58 -08004062 $self->{empty_symlinks} =
4063 _mark_empty_symlinks($git_svn, $switch_path);
Eric Wongdbc6c742009-01-11 16:51:10 -08004064 }
Ben Jackson0d8bee72009-04-11 10:46:17 -07004065 $self->{ignore_regex} = eval { command_oneline('config', '--get',
4066 "svn-remote.$git_svn->{repo_id}.ignore-paths") };
Eric Wongd2a9a872006-12-12 14:47:00 -08004067 $self->{empty} = {};
4068 $self->{dir_prop} = {};
4069 $self->{file_prop} = {};
4070 $self->{absent_dir} = {};
4071 $self->{absent_file} = {};
Eric Wongef3cfaa2007-01-24 03:30:57 -08004072 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
Dmitry Statyvka3713e222010-07-30 04:30:13 +02004073 $self->{pathnameencoding} = Git::config('svn.pathnameencoding');
Eric Wong27a1a802006-11-27 21:44:48 -08004074 $self;
4075}
4076
Eric Wongdbc6c742009-01-11 16:51:10 -08004077# this uses the Ra object, so it must be called before do_{switch,update},
4078# not inside them (when the Git::SVN::Fetcher object is passed) to
4079# do_{switch,update}
4080sub _mark_empty_symlinks {
Eric Wong8841b372009-02-11 01:56:58 -08004081 my ($git_svn, $switch_path) = @_;
Eric Wong4c58a712009-01-31 17:31:12 -08004082 my $bool = Git::config_bool('svn.brokenSymlinkWorkaround');
Eric Wong48679e52009-02-27 19:40:16 -08004083 return {} if (!defined($bool)) || (defined($bool) && ! $bool);
Eric Wong4c58a712009-01-31 17:31:12 -08004084
Eric Wongdbc6c742009-01-11 16:51:10 -08004085 my %ret;
4086 my ($rev, $cmt) = $git_svn->last_rev_commit;
4087 return {} unless ($rev && $cmt);
4088
Eric Wong4c58a712009-01-31 17:31:12 -08004089 # allow the warning to be printed for each revision we fetch to
4090 # ensure the user sees it. The user can also disable the workaround
4091 # on the repository even while git svn is running and the next
4092 # revision fetched will skip this expensive function.
4093 my $printed_warning;
Eric Wongdbc6c742009-01-11 16:51:10 -08004094 chomp(my $empty_blob = `git hash-object -t blob --stdin < /dev/null`);
4095 my ($ls, $ctx) = command_output_pipe(qw/ls-tree -r -z/, $cmt);
4096 local $/ = "\0";
Eric Wong8841b372009-02-11 01:56:58 -08004097 my $pfx = defined($switch_path) ? $switch_path : $git_svn->{path};
Eric Wongdbc6c742009-01-11 16:51:10 -08004098 $pfx .= '/' if length($pfx);
4099 while (<$ls>) {
4100 chomp;
4101 s/\A100644 blob $empty_blob\t//o or next;
Eric Wong4c58a712009-01-31 17:31:12 -08004102 unless ($printed_warning) {
4103 print STDERR "Scanning for empty symlinks, ",
4104 "this may take a while if you have ",
4105 "many empty files\n",
4106 "You may disable this with `",
4107 "git config svn.brokenSymlinkWorkaround ",
4108 "false'.\n",
4109 "This may be done in a different ",
4110 "terminal without restarting ",
4111 "git svn\n";
4112 $printed_warning = 1;
4113 }
Eric Wongdbc6c742009-01-11 16:51:10 -08004114 my $path = $_;
4115 my (undef, $props) =
4116 $git_svn->ra->get_file($pfx.$path, $rev, undef);
4117 if ($props->{'svn:special'}) {
4118 $ret{$path} = 1;
4119 }
4120 }
4121 command_close_pipe($ls, $ctx);
4122 \%ret;
4123}
4124
Eric Wongb03a71a2009-01-11 18:23:38 -08004125# returns true if a given path is inside a ".git" directory
4126sub in_dot_git {
4127 $_[0] =~ m{(?:^|/)\.git(?:/|$)};
4128}
4129
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02004130# return value: 0 -- don't ignore, 1 -- ignore
4131sub is_path_ignored {
Ben Jackson0d8bee72009-04-11 10:46:17 -07004132 my ($self, $path) = @_;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02004133 return 1 if in_dot_git($path);
Ben Jackson0d8bee72009-04-11 10:46:17 -07004134 return 1 if defined($self->{ignore_regex}) &&
4135 $path =~ m!$self->{ignore_regex}!;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02004136 return 0 unless defined($_ignore_regex);
4137 return 1 if $path =~ m!$_ignore_regex!o;
4138 return 0;
4139}
4140
Eric Wong8b8fc062007-01-22 11:44:57 -08004141sub set_path_strip {
4142 my ($self, $path) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08004143 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
Eric Wong8b8fc062007-01-22 11:44:57 -08004144}
4145
Eric Wongd2a9a872006-12-12 14:47:00 -08004146sub open_root {
4147 { path => '' };
4148}
4149
4150sub open_directory {
4151 my ($self, $path, $pb, $rev) = @_;
4152 { path => $path };
4153}
4154
Eric Wong706587f2007-01-18 17:50:01 -08004155sub git_path {
4156 my ($self, $path) = @_;
Dmitry Statyvka3713e222010-07-30 04:30:13 +02004157 if (my $enc = $self->{pathnameencoding}) {
4158 require Encode;
4159 Encode::from_to($path, 'UTF-8', $enc);
4160 }
Eric Wong2b27f6c2007-01-28 04:59:05 -08004161 if ($self->{path_strip}) {
4162 $path =~ s!$self->{path_strip}!! or
4163 die "Failed to strip path '$path' ($self->{path_strip})\n";
4164 }
Eric Wong706587f2007-01-18 17:50:01 -08004165 $path;
4166}
4167
Eric Wong27a1a802006-11-27 21:44:48 -08004168sub delete_entry {
4169 my ($self, $path, $rev, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004170 return undef if $self->is_path_ignored($path);
Eric Wong4a87db02007-01-04 01:38:18 -08004171
Eric Wong706587f2007-01-18 17:50:01 -08004172 my $gpath = $self->git_path($path);
Eric Wong8a603772007-01-31 02:45:50 -08004173 return undef if ($gpath eq '');
4174
Eric Wong4a87db02007-01-04 01:38:18 -08004175 # remove entire directories.
Eric Wong4f821012009-03-28 22:10:08 -07004176 my ($tree) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
4177 =~ /\A040000 tree ([a-f\d]{40})\t\Q$gpath\E\0/);
4178 if ($tree) {
Eric Wong4a87db02007-01-04 01:38:18 -08004179 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
4180 -r --name-only -z/,
Eric Wong4f821012009-03-28 22:10:08 -07004181 $tree);
Eric Wong4a87db02007-01-04 01:38:18 -08004182 local $/ = "\0";
4183 while (<$ls>) {
Eric Wongef3cfaa2007-01-24 03:30:57 -08004184 chomp;
Eric Wong4f821012009-03-28 22:10:08 -07004185 my $rmpath = "$gpath/$_";
4186 $self->{gii}->remove($rmpath);
4187 print "\tD\t$rmpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08004188 }
Eric Wong9e3cdbd2007-02-09 12:23:47 -08004189 print "\tD\t$gpath/\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08004190 command_close_pipe($ls, $ctx);
Eric Wong4a87db02007-01-04 01:38:18 -08004191 } else {
Eric Wongef3cfaa2007-01-24 03:30:57 -08004192 $self->{gii}->remove($gpath);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08004193 print "\tD\t$gpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08004194 }
Eric Wongf9ad77a2009-12-07 20:49:38 -08004195 $self->{empty}->{$path} = 0;
Eric Wong27a1a802006-11-27 21:44:48 -08004196 undef;
4197}
4198
4199sub open_file {
4200 my ($self, $path, $pb, $rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08004201 my ($mode, $blob);
4202
Ben Jackson0d8bee72009-04-11 10:46:17 -07004203 goto out if $self->is_path_ignored($path);
Eric Wongb03a71a2009-01-11 18:23:38 -08004204
Eric Wong706587f2007-01-18 17:50:01 -08004205 my $gpath = $self->git_path($path);
Eric Wong4f821012009-03-28 22:10:08 -07004206 ($mode, $blob) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
4207 =~ /\A(\d{6}) blob ([a-f\d]{40})\t\Q$gpath\E\0/);
Eric Wong006ede52006-12-08 01:55:19 -08004208 unless (defined $mode && defined $blob) {
4209 die "$path was not found in commit $self->{c} (r$rev)\n";
4210 }
Eric Wongdbc6c742009-01-11 16:51:10 -08004211 if ($mode eq '100644' && $self->{empty_symlinks}->{$path}) {
4212 $mode = '120000';
4213 }
Eric Wongb03a71a2009-01-11 18:23:38 -08004214out:
Eric Wong27a1a802006-11-27 21:44:48 -08004215 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
Eric Wong0864e3b2006-11-28 02:50:17 -08004216 pool => SVN::Pool->new, action => 'M' };
Eric Wong27a1a802006-11-27 21:44:48 -08004217}
4218
4219sub add_file {
4220 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08004221 my $mode;
4222
Ben Jackson0d8bee72009-04-11 10:46:17 -07004223 if (!$self->is_path_ignored($path)) {
Eric Wongb03a71a2009-01-11 18:23:38 -08004224 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
4225 delete $self->{empty}->{$dir};
4226 $mode = '100644';
4227 }
4228 { path => $path, mode_a => $mode, mode_b => $mode,
Eric Wong0864e3b2006-11-28 02:50:17 -08004229 pool => SVN::Pool->new, action => 'A' };
Eric Wong27a1a802006-11-27 21:44:48 -08004230}
4231
Eric Wongd2a9a872006-12-12 14:47:00 -08004232sub add_directory {
4233 my ($self, $path, $cp_path, $cp_rev) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004234 goto out if $self->is_path_ignored($path);
Eric Wong12a6d752007-12-14 08:39:09 -08004235 my $gpath = $self->git_path($path);
4236 if ($gpath eq '') {
4237 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
4238 -r --name-only -z/,
4239 $self->{c});
4240 local $/ = "\0";
4241 while (<$ls>) {
4242 chomp;
4243 $self->{gii}->remove($_);
4244 print "\tD\t$_\n" unless $::_q;
4245 }
4246 command_close_pipe($ls, $ctx);
4247 $self->{empty}->{$path} = 0;
4248 }
Eric Wongd2a9a872006-12-12 14:47:00 -08004249 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
4250 delete $self->{empty}->{$dir};
4251 $self->{empty}->{$path} = 1;
Eric Wongb03a71a2009-01-11 18:23:38 -08004252out:
Eric Wongd2a9a872006-12-12 14:47:00 -08004253 { path => $path };
4254}
4255
4256sub change_dir_prop {
4257 my ($self, $db, $prop, $value) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004258 return undef if $self->is_path_ignored($db->{path});
Eric Wongd2a9a872006-12-12 14:47:00 -08004259 $self->{dir_prop}->{$db->{path}} ||= {};
4260 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
4261 undef;
4262}
4263
4264sub absent_directory {
4265 my ($self, $path, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004266 return undef if $self->is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08004267 $self->{absent_dir}->{$pb->{path}} ||= [];
4268 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
4269 undef;
4270}
4271
4272sub absent_file {
4273 my ($self, $path, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004274 return undef if $self->is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08004275 $self->{absent_file}->{$pb->{path}} ||= [];
4276 push @{$self->{absent_file}->{$pb->{path}}}, $path;
4277 undef;
4278}
4279
Eric Wong27a1a802006-11-27 21:44:48 -08004280sub change_file_prop {
4281 my ($self, $fb, $prop, $value) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004282 return undef if $self->is_path_ignored($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08004283 if ($prop eq 'svn:executable') {
4284 if ($fb->{mode_b} != 120000) {
4285 $fb->{mode_b} = defined $value ? 100755 : 100644;
4286 }
4287 } elsif ($prop eq 'svn:special') {
4288 $fb->{mode_b} = defined $value ? 120000 : 100644;
Eric Wongd2a9a872006-12-12 14:47:00 -08004289 } else {
4290 $self->{file_prop}->{$fb->{path}} ||= {};
4291 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
Eric Wong27a1a802006-11-27 21:44:48 -08004292 }
4293 undef;
4294}
4295
4296sub apply_textdelta {
4297 my ($self, $fb, $exp) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004298 return undef if $self->is_path_ignored($fb->{path});
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004299 my $fh = $::_repository->temp_acquire('svn_delta');
Eric Wong27a1a802006-11-27 21:44:48 -08004300 # $fh gets auto-closed() by SVN::TxDelta::apply(),
4301 # (but $base does not,) so dup() it for reading in close_file
4302 open my $dup, '<&', $fh or croak $!;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004303 my $base = $::_repository->temp_acquire('git_blob');
Eric Wongb03a71a2009-01-11 18:23:38 -08004304
Eric Wong27a1a802006-11-27 21:44:48 -08004305 if ($fb->{blob}) {
Eric Wongbaf5fa82009-01-11 16:51:11 -08004306 my ($base_is_link, $size);
4307
Eric Wongdbc6c742009-01-11 16:51:10 -08004308 if ($fb->{mode_a} eq '120000' &&
4309 ! $self->{empty_symlinks}->{$fb->{path}}) {
4310 print $base 'link ' or die "print $!\n";
Eric Wongbaf5fa82009-01-11 16:51:11 -08004311 $base_is_link = 1;
Eric Wongdbc6c742009-01-11 16:51:10 -08004312 }
Eric Wongbaf5fa82009-01-11 16:51:11 -08004313 retry:
4314 $size = $::_repository->cat_blob($fb->{blob}, $base);
Junio C Hamanod683a0e2008-05-27 23:33:22 -07004315 die "Failed to read object $fb->{blob}" if ($size < 0);
Eric Wong27a1a802006-11-27 21:44:48 -08004316
4317 if (defined $exp) {
4318 seek $base, 0, 0 or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08004319 my $got = ::md5sum($base);
Eric Wongbaf5fa82009-01-11 16:51:11 -08004320 if ($got ne $exp) {
4321 my $err = "Checksum mismatch: ".
4322 "$fb->{path} $fb->{blob}\n" .
4323 "expected: $exp\n" .
4324 " got: $got\n";
4325 if ($base_is_link) {
4326 warn $err,
4327 "Retrying... (possibly ",
4328 "a bad symlink from SVN)\n";
4329 $::_repository->temp_reset($base);
4330 $base_is_link = 0;
4331 goto retry;
4332 }
4333 die $err;
4334 }
Eric Wong27a1a802006-11-27 21:44:48 -08004335 }
4336 }
4337 seek $base, 0, 0 or croak $!;
Marcus Griep0b191382008-08-12 12:00:53 -04004338 $fb->{fh} = $fh;
Eric Wong27a1a802006-11-27 21:44:48 -08004339 $fb->{base} = $base;
Marcus Griep0b191382008-08-12 12:00:53 -04004340 [ SVN::TxDelta::apply($base, $dup, undef, $fb->{path}, $fb->{pool}) ];
Eric Wong27a1a802006-11-27 21:44:48 -08004341}
4342
4343sub close_file {
4344 my ($self, $fb, $exp) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004345 return undef if $self->is_path_ignored($fb->{path});
Eric Wongb03a71a2009-01-11 18:23:38 -08004346
Eric Wong27a1a802006-11-27 21:44:48 -08004347 my $hash;
Eric Wong706587f2007-01-18 17:50:01 -08004348 my $path = $self->git_path($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08004349 if (my $fh = $fb->{fh}) {
Eric Wong7faf0682007-05-27 15:59:01 -07004350 if (defined $exp) {
4351 seek($fh, 0, 0) or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08004352 my $got = ::md5sum($fh);
Eric Wong7faf0682007-05-27 15:59:01 -07004353 if ($got ne $exp) {
4354 die "Checksum mismatch: $path\n",
4355 "expected: $exp\n got: $got\n";
4356 }
4357 }
Eric Wong27a1a802006-11-27 21:44:48 -08004358 if ($fb->{mode_b} == 120000) {
Marcus Griep510b0942008-08-12 12:45:39 -04004359 sysseek($fh, 0, 0) or croak $!;
Eric Wongdbc6c742009-01-11 16:51:10 -08004360 my $rd = sysread($fh, my $buf, 5);
Marcus Griep510b0942008-08-12 12:45:39 -04004361
Eric Wongdbc6c742009-01-11 16:51:10 -08004362 if (!defined $rd) {
4363 croak "sysread: $!\n";
4364 } elsif ($rd == 0) {
Marcus Griep510b0942008-08-12 12:45:39 -04004365 warn "$path has mode 120000",
Eric Wongdbc6c742009-01-11 16:51:10 -08004366 " but it points to nothing\n",
4367 "converting to an empty file with mode",
4368 " 100644\n";
4369 $fb->{mode_b} = '100644';
4370 } elsif ($buf ne 'link ') {
4371 warn "$path has mode 120000",
4372 " but is not a link\n";
Marcus Griep510b0942008-08-12 12:45:39 -04004373 } else {
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004374 my $tmp_fh = $::_repository->temp_acquire(
4375 'svn_hash');
Marcus Griep510b0942008-08-12 12:45:39 -04004376 my $res;
4377 while ($res = sysread($fh, my $str, 1024)) {
4378 my $out = syswrite($tmp_fh, $str, $res);
4379 defined($out) && $out == $res
4380 or croak("write ",
Marcus Griep836ff952008-09-08 12:53:01 -04004381 Git::temp_path($tmp_fh),
Marcus Griep510b0942008-08-12 12:45:39 -04004382 ": $!\n");
4383 }
4384 defined $res or croak $!;
4385
4386 ($fh, $tmp_fh) = ($tmp_fh, $fh);
4387 Git::temp_release($tmp_fh, 1);
Eric Wong7fc35e02007-12-19 00:06:45 -08004388 }
Eric Wong27a1a802006-11-27 21:44:48 -08004389 }
Adam Robenffe256f2008-05-23 16:19:41 +02004390
Marcus Griep0b191382008-08-12 12:00:53 -04004391 $hash = $::_repository->hash_and_insert_object(
Marcus Griep836ff952008-09-08 12:53:01 -04004392 Git::temp_path($fh));
Eric Wong27a1a802006-11-27 21:44:48 -08004393 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
Marcus Griep0b191382008-08-12 12:00:53 -04004394
4395 Git::temp_release($fb->{base}, 1);
Marcus Griep510b0942008-08-12 12:45:39 -04004396 Git::temp_release($fh, 1);
Eric Wong27a1a802006-11-27 21:44:48 -08004397 } else {
4398 $hash = $fb->{blob} or die "no blob information\n";
4399 }
4400 $fb->{pool}->clear;
Eric Wongef3cfaa2007-01-24 03:30:57 -08004401 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
Eric Wong9e3cdbd2007-02-09 12:23:47 -08004402 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
Eric Wong27a1a802006-11-27 21:44:48 -08004403 undef;
4404}
4405
4406sub abort_edit {
4407 my $self = shift;
Eric Wongef3cfaa2007-01-24 03:30:57 -08004408 $self->{nr} = $self->{gii}->{nr};
4409 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08004410 $self->SUPER::abort_edit(@_);
4411}
4412
4413sub close_edit {
4414 my $self = shift;
Eric Wongdad73c02006-11-28 14:06:05 -08004415 $self->{git_commit_ok} = 1;
Eric Wongef3cfaa2007-01-24 03:30:57 -08004416 $self->{nr} = $self->{gii}->{nr};
4417 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08004418 $self->SUPER::close_edit(@_);
4419}
Eric Wong1a82e792006-06-16 02:55:13 -07004420
Eric Wonga5e0ced2006-06-12 15:23:48 -07004421package SVN::Git::Editor;
Eric Wong24e22aa2007-01-29 00:07:49 -08004422use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004423use strict;
4424use warnings;
4425use Carp qw/croak/;
4426use IO::File;
4427
4428sub new {
Eric Wong61395352007-01-27 14:33:08 -08004429 my ($class, $opts) = @_;
4430 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
4431 die "$_ required!\n" unless (defined $opts->{$_});
Eric Wonga5e0ced2006-06-12 15:23:48 -07004432 }
Eric Wong61395352007-01-27 14:33:08 -08004433
4434 my $pool = SVN::Pool->new;
4435 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
4436 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
4437 $opts->{r}, $mods);
4438
4439 # $opts->{ra} functions should not be used after this:
4440 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
4441 $opts->{editor_cb}, $pool);
4442 my $self = SVN::Delta::Editor->new(@ce, $pool);
4443 bless $self, $class;
4444 foreach (qw/svn_path r tree_a tree_b/) {
4445 $self->{$_} = $opts->{$_};
4446 }
4447 $self->{url} = $opts->{ra}->{url};
4448 $self->{mods} = $mods;
4449 $self->{types} = $types;
4450 $self->{pool} = $pool;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004451 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
4452 $self->{rm} = { };
Eric Wongd3a840d2007-01-26 01:32:45 -08004453 $self->{path_prefix} = length $self->{svn_path} ?
4454 "$self->{svn_path}/" : '';
Brad King128de652008-07-25 11:32:37 -04004455 $self->{config} = $opts->{config};
Steven Walter6abd9332010-09-24 23:51:50 -04004456 $self->{mergeinfo} = $opts->{mergeinfo};
Eric Wonga5e0ced2006-06-12 15:23:48 -07004457 return $self;
4458}
4459
Eric Wong61395352007-01-27 14:33:08 -08004460sub generate_diff {
4461 my ($tree_a, $tree_b) = @_;
4462 my @diff_tree = qw(diff-tree -z -r);
Eric Wong24e22aa2007-01-29 00:07:49 -08004463 if ($_cp_similarity) {
4464 push @diff_tree, "-C$_cp_similarity";
Eric Wong61395352007-01-27 14:33:08 -08004465 } else {
4466 push @diff_tree, '-C';
4467 }
Eric Wong24e22aa2007-01-29 00:07:49 -08004468 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
4469 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
Eric Wong61395352007-01-27 14:33:08 -08004470 push @diff_tree, $tree_a, $tree_b;
4471 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
4472 local $/ = "\0";
4473 my $state = 'meta';
4474 my @mods;
4475 while (<$diff_fh>) {
4476 chomp $_; # this gets rid of the trailing "\0"
4477 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02004478 ($::sha1)\s($::sha1)\s
Eric Wong61395352007-01-27 14:33:08 -08004479 ([MTCRAD])\d*$/xo) {
4480 push @mods, { mode_a => $1, mode_b => $2,
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02004481 sha1_a => $3, sha1_b => $4,
4482 chg => $5 };
4483 if ($5 =~ /^(?:C|R)$/) {
Eric Wong61395352007-01-27 14:33:08 -08004484 $state = 'file_a';
4485 } else {
4486 $state = 'file_b';
4487 }
4488 } elsif ($state eq 'file_a') {
4489 my $x = $mods[$#mods] or croak "Empty array\n";
4490 if ($x->{chg} !~ /^(?:C|R)$/) {
4491 croak "Error parsing $_, $x->{chg}\n";
4492 }
4493 $x->{file_a} = $_;
4494 $state = 'file_b';
4495 } elsif ($state eq 'file_b') {
4496 my $x = $mods[$#mods] or croak "Empty array\n";
4497 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
4498 croak "Error parsing $_, $x->{chg}\n";
4499 }
4500 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
4501 croak "Error parsing $_, $x->{chg}\n";
4502 }
4503 $x->{file_b} = $_;
4504 $state = 'meta';
4505 } else {
4506 croak "Error parsing $_\n";
4507 }
4508 }
4509 command_close_pipe($diff_fh, $ctx);
4510 \@mods;
4511}
4512
4513sub check_diff_paths {
4514 my ($ra, $pfx, $rev, $mods) = @_;
4515 my %types;
4516 $pfx .= '/' if length $pfx;
4517
4518 sub type_diff_paths {
4519 my ($ra, $types, $path, $rev) = @_;
4520 my @p = split m#/+#, $path;
4521 my $c = shift @p;
4522 unless (defined $types->{$c}) {
4523 $types->{$c} = $ra->check_path($c, $rev);
4524 }
4525 while (@p) {
4526 $c .= '/' . shift @p;
4527 next if defined $types->{$c};
4528 $types->{$c} = $ra->check_path($c, $rev);
4529 }
4530 }
4531
4532 foreach my $m (@$mods) {
4533 foreach my $f (qw/file_a file_b/) {
4534 next unless defined $m->{$f};
4535 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
4536 if (length $pfx.$dir && ! defined $types{$dir}) {
4537 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
4538 }
4539 }
4540 }
4541 \%types;
4542}
4543
Eric Wonga5e0ced2006-06-12 15:23:48 -07004544sub split_path {
4545 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
4546}
4547
4548sub repo_path {
Eric Wongd3a840d2007-01-26 01:32:45 -08004549 my ($self, $path) = @_;
Dmitry Statyvka3713e222010-07-30 04:30:13 +02004550 if (my $enc = $self->{pathnameencoding}) {
4551 require Encode;
4552 Encode::from_to($path, $enc, 'UTF-8');
4553 }
Eric Wongd3a840d2007-01-26 01:32:45 -08004554 $self->{path_prefix}.(defined $path ? $path : '');
Eric Wonga5e0ced2006-06-12 15:23:48 -07004555}
4556
4557sub url_path {
4558 my ($self, $path) = @_;
Eric Wong29633bb2007-07-15 21:53:50 -07004559 if ($self->{url} =~ m#^https?://#) {
Eric Wong884cce52009-07-25 02:29:28 -07004560 $path =~ s!([^~a-zA-Z0-9_./-])!uc sprintf("%%%02x",ord($1))!eg;
Eric Wong29633bb2007-07-15 21:53:50 -07004561 }
Eric Wong6e8548c2007-01-27 01:32:00 -08004562 $self->{url} . '/' . $self->repo_path($path);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004563}
4564
4565sub rmdirs {
Eric Wong61395352007-01-27 14:33:08 -08004566 my ($self) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004567 my $rm = $self->{rm};
4568 delete $rm->{''}; # we never delete the url we're tracking
4569 return unless %$rm;
4570
4571 foreach (keys %$rm) {
4572 my @d = split m#/#, $_;
4573 my $c = shift @d;
4574 $rm->{$c} = 1;
4575 while (@d) {
4576 $c .= '/' . shift @d;
4577 $rm->{$c} = 1;
4578 }
4579 }
4580 delete $rm->{$self->{svn_path}};
4581 delete $rm->{''}; # we never delete the url we're tracking
4582 return unless %$rm;
4583
Eric Wong61395352007-01-27 14:33:08 -08004584 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
4585 $self->{tree_b});
Eric Wonga5e0ced2006-06-12 15:23:48 -07004586 local $/ = "\0";
4587 while (<$fh>) {
4588 chomp;
Eric Wong747fa122006-11-24 22:38:17 -08004589 my @dn = split m#/#, $_;
Eric Wongc07eee12006-06-19 17:59:35 -07004590 while (pop @dn) {
4591 delete $rm->{join '/', @dn};
4592 }
4593 unless (%$rm) {
Eric Wong22600a22007-02-01 13:12:26 -08004594 close $fh;
Eric Wongc07eee12006-06-19 17:59:35 -07004595 return;
4596 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07004597 }
Eric Wongaef4e922006-12-15 10:59:54 -08004598 command_close_pipe($fh, $ctx);
Eric Wongc07eee12006-06-19 17:59:35 -07004599
Eric Wonga5e0ced2006-06-12 15:23:48 -07004600 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
4601 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
4602 $self->close_directory($bat->{$d}, $p);
4603 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
Eric Wong44320b92007-01-13 22:35:53 -08004604 print "\tD+\t$d/\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004605 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
4606 delete $bat->{$d};
4607 }
4608}
4609
4610sub open_or_add_dir {
4611 my ($self, $full_path, $baton) = @_;
Eric Wong6e8548c2007-01-27 01:32:00 -08004612 my $t = $self->{types}->{$full_path};
4613 if (!defined $t) {
4614 die "$full_path not known in r$self->{r} or we have a bug!\n";
4615 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004616 {
4617 no warnings 'once';
4618 # SVN::Node::none and SVN::Node::file are used only once,
4619 # so we're shutting up Perl's warnings about them.
4620 if ($t == $SVN::Node::none) {
4621 return $self->add_directory($full_path, $baton,
4622 undef, -1, $self->{pool});
4623 } elsif ($t == $SVN::Node::dir) {
4624 return $self->open_directory($full_path, $baton,
4625 $self->{r}, $self->{pool});
4626 } # no warnings 'once'
4627 print STDERR "$full_path already exists in repository at ",
4628 "r$self->{r} and it is not a directory (",
4629 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
4630 } # no warnings 'once'
Eric Wonga5e0ced2006-06-12 15:23:48 -07004631 exit 1;
4632}
4633
4634sub ensure_path {
4635 my ($self, $path) = @_;
4636 my $bat = $self->{bat};
Eric Wong6e8548c2007-01-27 01:32:00 -08004637 my $repo_path = $self->repo_path($path);
4638 return $bat->{''} unless (length $repo_path);
4639 my @p = split m#/+#, $repo_path;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004640 my $c = shift @p;
4641 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
4642 while (@p) {
4643 my $c0 = $c;
4644 $c .= '/' . shift @p;
4645 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
4646 }
4647 return $bat->{$c};
4648}
4649
Brad King128de652008-07-25 11:32:37 -04004650# Subroutine to convert a globbing pattern to a regular expression.
4651# From perl cookbook.
4652sub glob2pat {
4653 my $globstr = shift;
4654 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
4655 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
4656 return '^' . $globstr . '$';
4657}
4658
4659sub check_autoprop {
4660 my ($self, $pattern, $properties, $file, $fbat) = @_;
4661 # Convert the globbing pattern to a regular expression.
4662 my $regex = glob2pat($pattern);
4663 # Check if the pattern matches the file name.
4664 if($file =~ m/($regex)/) {
4665 # Parse the list of properties to set.
4666 my @props = split(/;/, $properties);
4667 foreach my $prop (@props) {
4668 # Parse 'name=value' syntax and set the property.
4669 if ($prop =~ /([^=]+)=(.*)/) {
4670 my ($n,$v) = ($1,$2);
4671 for ($n, $v) {
4672 s/^\s+//; s/\s+$//;
4673 }
4674 $self->change_file_prop($fbat, $n, $v);
4675 }
4676 }
4677 }
4678}
4679
4680sub apply_autoprops {
4681 my ($self, $file, $fbat) = @_;
4682 my $conf_t = ${$self->{config}}{'config'};
4683 no warnings 'once';
4684 # Check [miscellany]/enable-auto-props in svn configuration.
4685 if (SVN::_Core::svn_config_get_bool(
4686 $conf_t,
4687 $SVN::_Core::SVN_CONFIG_SECTION_MISCELLANY,
4688 $SVN::_Core::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS,
4689 0)) {
4690 # Auto-props are enabled. Enumerate them to look for matches.
4691 my $callback = sub {
4692 $self->check_autoprop($_[0], $_[1], $file, $fbat);
4693 };
4694 SVN::_Core::svn_config_enumerate(
4695 $conf_t,
4696 $SVN::_Core::SVN_CONFIG_SECTION_AUTO_PROPS,
4697 $callback);
4698 }
4699}
4700
Eric Wonga5e0ced2006-06-12 15:23:48 -07004701sub A {
Eric Wong44320b92007-01-13 22:35:53 -08004702 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004703 my ($dir, $file) = split_path($m->{file_b});
4704 my $pbat = $self->ensure_path($dir);
4705 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4706 undef, -1);
Eric Wong44320b92007-01-13 22:35:53 -08004707 print "\tA\t$m->{file_b}\n" unless $::_q;
Brad King128de652008-07-25 11:32:37 -04004708 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004709 $self->chg_file($fbat, $m);
4710 $self->close_file($fbat,undef,$self->{pool});
4711}
4712
4713sub C {
Eric Wong44320b92007-01-13 22:35:53 -08004714 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004715 my ($dir, $file) = split_path($m->{file_b});
4716 my $pbat = $self->ensure_path($dir);
4717 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4718 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08004719 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004720 $self->chg_file($fbat, $m);
4721 $self->close_file($fbat,undef,$self->{pool});
4722}
4723
4724sub delete_entry {
4725 my ($self, $path, $pbat) = @_;
4726 my $rpath = $self->repo_path($path);
4727 my ($dir, $file) = split_path($rpath);
4728 $self->{rm}->{$dir} = 1;
4729 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
4730}
4731
4732sub R {
Eric Wong44320b92007-01-13 22:35:53 -08004733 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004734 my ($dir, $file) = split_path($m->{file_b});
4735 my $pbat = $self->ensure_path($dir);
4736 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4737 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08004738 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Paul Talacko7c4d0212008-09-06 18:50:38 -07004739 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004740 $self->chg_file($fbat, $m);
4741 $self->close_file($fbat,undef,$self->{pool});
4742
4743 ($dir, $file) = split_path($m->{file_a});
4744 $pbat = $self->ensure_path($dir);
4745 $self->delete_entry($m->{file_a}, $pbat);
4746}
4747
4748sub M {
Eric Wong44320b92007-01-13 22:35:53 -08004749 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004750 my ($dir, $file) = split_path($m->{file_b});
4751 my $pbat = $self->ensure_path($dir);
4752 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
4753 $pbat,$self->{r},$self->{pool});
Eric Wong44320b92007-01-13 22:35:53 -08004754 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004755 $self->chg_file($fbat, $m);
4756 $self->close_file($fbat,undef,$self->{pool});
4757}
4758
4759sub T { shift->M(@_) }
4760
4761sub change_file_prop {
4762 my ($self, $fbat, $pname, $pval) = @_;
4763 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
4764}
4765
Steven Walter6abd9332010-09-24 23:51:50 -04004766sub change_dir_prop {
4767 my ($self, $pbat, $pname, $pval) = @_;
4768 $self->SUPER::change_dir_prop($pbat, $pname, $pval, $self->{pool});
4769}
4770
Florian Weimer214a34d2008-08-31 17:45:04 +02004771sub _chg_file_get_blob ($$$$) {
4772 my ($self, $fbat, $m, $which) = @_;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004773 my $fh = $::_repository->temp_acquire("git_blob_$which");
Florian Weimer214a34d2008-08-31 17:45:04 +02004774 if ($m->{"mode_$which"} =~ /^120/) {
4775 print $fh 'link ' or croak $!;
4776 $self->change_file_prop($fbat,'svn:special','*');
4777 } elsif ($m->{mode_a} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
4778 $self->change_file_prop($fbat,'svn:special',undef);
4779 }
4780 my $blob = $m->{"sha1_$which"};
4781 return ($fh,) if ($blob =~ /^0{40}$/);
4782 my $size = $::_repository->cat_blob($blob, $fh);
4783 croak "Failed to read object $blob" if ($size < 0);
4784 $fh->flush == 0 or croak $!;
4785 seek $fh, 0, 0 or croak $!;
4786
4787 my $exp = ::md5sum($fh);
4788 seek $fh, 0, 0 or croak $!;
4789 return ($fh, $exp);
4790}
4791
Eric Wonga5e0ced2006-06-12 15:23:48 -07004792sub chg_file {
4793 my ($self, $fbat, $m) = @_;
4794 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
4795 $self->change_file_prop($fbat,'svn:executable','*');
4796 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
4797 $self->change_file_prop($fbat,'svn:executable',undef);
4798 }
Florian Weimer8598db932008-08-31 17:47:09 +02004799 my ($fh_a, $exp_a) = _chg_file_get_blob $self, $fbat, $m, 'a';
4800 my ($fh_b, $exp_b) = _chg_file_get_blob $self, $fbat, $m, 'b';
Eric Wongf7197df2006-10-14 15:48:35 -07004801 my $pool = SVN::Pool->new;
Florian Weimer8598db932008-08-31 17:47:09 +02004802 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
4803 if (-s $fh_a) {
4804 my $txstream = SVN::TxDelta::new ($fh_a, $fh_b, $pool);
Eric Wong991255c2008-08-31 19:45:07 -07004805 my $res = SVN::TxDelta::send_txstream($txstream, @$atd, $pool);
4806 if (defined $res) {
4807 die "Unexpected result from send_txstream: $res\n",
4808 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
4809 }
Florian Weimer8598db932008-08-31 17:47:09 +02004810 } else {
4811 my $got = SVN::TxDelta::send_stream($fh_b, @$atd, $pool);
4812 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
4813 if ($got ne $exp_b);
4814 }
4815 Git::temp_release($fh_b, 1);
4816 Git::temp_release($fh_a, 1);
Eric Wongf7197df2006-10-14 15:48:35 -07004817 $pool->clear;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004818}
4819
4820sub D {
Eric Wong44320b92007-01-13 22:35:53 -08004821 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004822 my ($dir, $file) = split_path($m->{file_b});
4823 my $pbat = $self->ensure_path($dir);
Eric Wong44320b92007-01-13 22:35:53 -08004824 print "\tD\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004825 $self->delete_entry($m->{file_b}, $pbat);
4826}
4827
4828sub close_edit {
4829 my ($self) = @_;
4830 my ($p,$bat) = ($self->{pool}, $self->{bat});
4831 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
Eric Wong64427542007-05-19 02:58:37 -07004832 next if $_ eq '';
Eric Wonga5e0ced2006-06-12 15:23:48 -07004833 $self->close_directory($bat->{$_}, $p);
4834 }
Eric Wong64427542007-05-19 02:58:37 -07004835 $self->close_directory($bat->{''}, $p);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004836 $self->SUPER::close_edit($p);
4837 $p->clear;
4838}
4839
4840sub abort_edit {
4841 my ($self) = @_;
4842 $self->SUPER::abort_edit($self->{pool});
Eric Wong61395352007-01-27 14:33:08 -08004843}
4844
4845sub DESTROY {
4846 my $self = shift;
4847 $self->SUPER::DESTROY(@_);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004848 $self->{pool}->clear;
4849}
4850
Eric Wong44320b92007-01-13 22:35:53 -08004851# this drives the editor
4852sub apply_diff {
Eric Wong61395352007-01-27 14:33:08 -08004853 my ($self) = @_;
4854 my $mods = $self->{mods};
Eric Wong44320b92007-01-13 22:35:53 -08004855 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
Eric Wong6e8548c2007-01-27 01:32:00 -08004856 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
Eric Wong44320b92007-01-13 22:35:53 -08004857 my $f = $m->{chg};
4858 if (defined $o{$f}) {
4859 $self->$f($m);
4860 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004861 fatal("Invalid change type: $f");
Eric Wong44320b92007-01-13 22:35:53 -08004862 }
4863 }
Steven Walter6abd9332010-09-24 23:51:50 -04004864
4865 if (defined($self->{mergeinfo})) {
4866 $self->change_dir_prop($self->{bat}{''}, "svn:mergeinfo",
4867 $self->{mergeinfo});
4868 }
Eric Wong24e22aa2007-01-29 00:07:49 -08004869 $self->rmdirs if $_rmdir;
Eric Wong6e8548c2007-01-27 01:32:00 -08004870 if (@$mods == 0) {
Eric Wong44320b92007-01-13 22:35:53 -08004871 $self->abort_edit;
4872 } else {
4873 $self->close_edit;
4874 }
Eric Wong6e8548c2007-01-27 01:32:00 -08004875 return scalar @$mods;
Eric Wong44320b92007-01-13 22:35:53 -08004876}
4877
Eric Wongd81bf822007-01-10 01:22:38 -08004878package Git::SVN::Ra;
Eric Wong6af1db42007-02-14 16:04:10 -08004879use vars qw/@ISA $config_dir $_log_window_size/;
Eric Wongd81bf822007-01-10 01:22:38 -08004880use strict;
4881use warnings;
Eric Wonga51cdb02007-09-07 04:00:40 -07004882my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
Eric Wongd81bf822007-01-10 01:22:38 -08004883
4884BEGIN {
4885 # enforce temporary pool usage for some simple functions
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004886 no strict 'refs';
Eric Wongbf8a40b2009-01-25 15:35:52 -08004887 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root
4888 get_file/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004889 my $SUPER = "SUPER::$f";
4890 *$f = sub {
4891 my $self = shift;
4892 my $pool = SVN::Pool->new;
4893 my @ret = $self->$SUPER(@_,$pool);
4894 $pool->clear;
4895 wantarray ? @ret : $ret[0];
4896 };
Eric Wongd81bf822007-01-10 01:22:38 -08004897 }
Eric Wongd81bf822007-01-10 01:22:38 -08004898}
4899
Steven Walter9ff74e92007-09-28 13:24:19 -04004900sub _auth_providers () {
4901 [
4902 SVN::Client::get_simple_provider(),
4903 SVN::Client::get_ssl_server_trust_file_provider(),
4904 SVN::Client::get_simple_prompt_provider(
4905 \&Git::SVN::Prompt::simple, 2),
4906 SVN::Client::get_ssl_client_cert_file_provider(),
4907 SVN::Client::get_ssl_client_cert_prompt_provider(
4908 \&Git::SVN::Prompt::ssl_client_cert, 2),
Sebastian Noack77266e92008-02-25 15:56:28 +01004909 SVN::Client::get_ssl_client_cert_pw_file_provider(),
Steven Walter9ff74e92007-09-28 13:24:19 -04004910 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
4911 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
4912 SVN::Client::get_username_provider(),
4913 SVN::Client::get_ssl_server_trust_prompt_provider(
4914 \&Git::SVN::Prompt::ssl_server_trust),
4915 SVN::Client::get_username_prompt_provider(
4916 \&Git::SVN::Prompt::username, 2)
4917 ]
4918}
4919
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004920sub escape_uri_only {
4921 my ($uri) = @_;
4922 my @tmp;
4923 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -07004924 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004925 push @tmp, $_;
4926 }
4927 join('/', @tmp);
4928}
4929
4930sub escape_url {
4931 my ($url) = @_;
4932 if ($url =~ m#^(https?)://([^/]+)(.*)$#) {
4933 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
4934 $url = "$scheme://$domain$uri";
4935 }
4936 $url;
4937}
4938
Eric Wongd81bf822007-01-10 01:22:38 -08004939sub new {
4940 my ($class, $url) = @_;
Eric Wongf6f09872007-01-18 18:22:18 -08004941 $url =~ s!/+$!!;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004942 return $RA if ($RA && $RA->{url} eq $url);
Eric Wongf6f09872007-01-18 18:22:18 -08004943
josh robbd32fad22010-02-24 16:13:50 +13004944 ::_req_svn();
4945
Eric Wongd81bf822007-01-10 01:22:38 -08004946 SVN::_Core::svn_config_ensure($config_dir, undef);
Steven Walter9ff74e92007-09-28 13:24:19 -04004947 my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
Eric Wongd81bf822007-01-10 01:22:38 -08004948 my $config = SVN::Core::config_get_config($config_dir);
Eric Wong7730fbe2007-07-04 14:07:42 -07004949 $RA = undef;
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004950 my $dont_store_passwords = 1;
4951 my $conf_t = ${$config}{'config'};
4952 {
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004953 no warnings 'once';
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004954 # The usage of $SVN::_Core::SVN_CONFIG_* variables
4955 # produces warnings that variables are used only once.
4956 # I had not found the better way to shut them up, so
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004957 # the warnings of type 'once' are disabled in this block.
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004958 if (SVN::_Core::svn_config_get_bool($conf_t,
4959 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4960 $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
4961 1) == 0) {
4962 SVN::_Core::svn_auth_set_parameter($baton,
4963 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
4964 bless (\$dont_store_passwords, "_p_void"));
4965 }
4966 if (SVN::_Core::svn_config_get_bool($conf_t,
4967 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4968 $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
4969 1) == 0) {
4970 $Git::SVN::Prompt::_no_auth_cache = 1;
4971 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004972 } # no warnings 'once'
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004973 my $self = SVN::Ra->new(url => escape_url($url), auth => $baton,
Eric Wongd81bf822007-01-10 01:22:38 -08004974 config => $config,
4975 pool => SVN::Pool->new,
4976 auth_provider_callbacks => $callbacks);
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004977 $self->{url} = $url;
Eric Wongd81bf822007-01-10 01:22:38 -08004978 $self->{svn_path} = $url;
4979 $self->{repos_root} = $self->get_repos_root;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08004980 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
Eric Wong0dc03d62007-05-13 01:04:43 -07004981 $self->{cache} = { check_path => { r => 0, data => {} },
4982 get_dir => { r => 0, data => {} } };
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004983 $RA = bless $self, $class;
Eric Wongd81bf822007-01-10 01:22:38 -08004984}
4985
Eric Wong0dc03d62007-05-13 01:04:43 -07004986sub check_path {
4987 my ($self, $path, $r) = @_;
4988 my $cache = $self->{cache}->{check_path};
4989 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
4990 return $cache->{data}->{$path};
4991 }
4992 my $pool = SVN::Pool->new;
4993 my $t = $self->SUPER::check_path($path, $r, $pool);
4994 $pool->clear;
4995 if ($r != $cache->{r}) {
4996 %{$cache->{data}} = ();
4997 $cache->{r} = $r;
4998 }
4999 $cache->{data}->{$path} = $t;
5000}
5001
5002sub get_dir {
5003 my ($self, $dir, $r) = @_;
5004 my $cache = $self->{cache}->{get_dir};
5005 if ($r == $cache->{r}) {
5006 if (my $x = $cache->{data}->{$dir}) {
5007 return wantarray ? @$x : $x->[0];
5008 }
5009 }
5010 my $pool = SVN::Pool->new;
5011 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
5012 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
5013 $pool->clear;
5014 if ($r != $cache->{r}) {
5015 %{$cache->{data}} = ();
5016 $cache->{r} = $r;
5017 }
5018 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
5019 wantarray ? (\%dirents, $r, $props) : \%dirents;
5020}
5021
Eric Wongd81bf822007-01-10 01:22:38 -08005022sub DESTROY {
Eric Wong5d3b7cd2007-01-29 19:16:01 -08005023 # do not call the real DESTROY since we store ourselves in $RA
Eric Wongd81bf822007-01-10 01:22:38 -08005024}
5025
Eric Wong1ef626b2009-01-17 22:11:44 -08005026# get_log(paths, start, end, limit,
5027# discover_changed_paths, strict_node_history, receiver)
Eric Wongd81bf822007-01-10 01:22:38 -08005028sub get_log {
5029 my ($self, @args) = @_;
5030 my $pool = SVN::Pool->new;
Eric Wong1ef626b2009-01-17 22:11:44 -08005031
Mattias Nissler3c49a032009-07-07 01:39:52 +02005032 # svn_log_changed_path_t objects passed to get_log are likely to be
5033 # overwritten even if only the refs are copied to an external variable,
5034 # so we should dup the structures in their entirety. Using an
5035 # externally passed pool (instead of our temporary and quickly cleared
5036 # pool in Git::SVN::Ra) does not help matters at all...
5037 my $receiver = pop @args;
Mattias Nissler0b2af452009-07-07 01:40:02 +02005038 my $prefix = "/".$self->{svn_path};
5039 $prefix =~ s#/+($)##;
5040 my $prefix_regex = qr#^\Q$prefix\E#;
Mattias Nissler3c49a032009-07-07 01:39:52 +02005041 push(@args, sub {
5042 my ($paths) = $_[0];
5043 return &$receiver(@_) unless $paths;
5044 $_[0] = ();
5045 foreach my $p (keys %$paths) {
5046 my $i = $paths->{$p};
Mattias Nissler0b2af452009-07-07 01:40:02 +02005047 # Make path relative to our url, not repos_root
5048 $p =~ s/$prefix_regex//;
5049 my %s = map { $_ => $i->$_; }
5050 qw/copyfrom_path copyfrom_rev action/;
5051 if ($s{'copyfrom_path'}) {
5052 $s{'copyfrom_path'} =~ s/$prefix_regex//;
5053 }
Mattias Nissler3c49a032009-07-07 01:39:52 +02005054 $_[0]{$p} = \%s;
5055 }
5056 &$receiver(@_);
5057 });
5058
5059
Eric Wong1ef626b2009-01-17 22:11:44 -08005060 # the limit parameter was not supported in SVN 1.1.x, so we
5061 # drop it. Therefore, the receiver callback passed to it
5062 # is made aware of this limitation by being wrapped if
5063 # the limit passed to is being wrapped.
5064 if ($SVN::Core::VERSION le '1.2.0') {
5065 my $limit = splice(@args, 3, 1);
5066 if ($limit > 0) {
5067 my $receiver = pop @args;
5068 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
5069 }
5070 }
Eric Wongd81bf822007-01-10 01:22:38 -08005071 my $ret = $self->SUPER::get_log(@args, $pool);
5072 $pool->clear;
5073 $ret;
5074}
5075
Steven Walter9ff74e92007-09-28 13:24:19 -04005076sub trees_match {
5077 my ($self, $url1, $rev1, $url2, $rev2) = @_;
5078 my $ctx = SVN::Client->new(auth => _auth_providers);
5079 my $out = IO::File->new_tmpfile;
5080
5081 # older SVN (1.1.x) doesn't take $pool as the last parameter for
5082 # $ctx->diff(), so we'll create a default one
5083 my $pool = SVN::Pool->new_default_sub;
5084
5085 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
5086 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
5087 $out->flush;
5088 my $ret = (($out->stat)[7] == 0);
5089 close $out or croak $!;
5090
5091 $ret;
5092}
5093
Eric Wongd81bf822007-01-10 01:22:38 -08005094sub get_commit_editor {
Eric Wong44320b92007-01-13 22:35:53 -08005095 my ($self, $log, $cb, $pool) = @_;
Eric Wongd81bf822007-01-10 01:22:38 -08005096 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
Eric Wong44320b92007-01-13 22:35:53 -08005097 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08005098}
5099
Eric Wongd81bf822007-01-10 01:22:38 -08005100sub gs_do_update {
Eric Wong8a603772007-01-31 02:45:50 -08005101 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
5102 my $new = ($rev_a == $rev_b);
5103 my $path = $gs->{path};
5104
Eric Wong2e5e2482007-02-23 02:21:59 -08005105 if ($new && -e $gs->{index}) {
5106 unlink $gs->{index} or die
5107 "Couldn't unlink index: $gs->{index}: $!\n";
5108 }
Eric Wongd81bf822007-01-10 01:22:38 -08005109 my $pool = SVN::Pool->new;
Eric Wong8b8fc062007-01-22 11:44:57 -08005110 $editor->set_path_strip($path);
Eric Wong2b27f6c2007-01-28 04:59:05 -08005111 my (@pc) = split m#/#, $path;
5112 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
Eric Wong8a603772007-01-31 02:45:50 -08005113 1, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08005114 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong2b27f6c2007-01-28 04:59:05 -08005115
5116 # Since we can't rely on svn_ra_reparent being available, we'll
5117 # just have to do some magic with set_path to make it so
5118 # we only want a partial path.
5119 my $sp = '';
5120 my $final = join('/', @pc);
5121 while (@pc) {
5122 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
5123 $sp .= '/' if length $sp;
5124 $sp .= shift @pc;
5125 }
5126 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
5127
Eric Wong2b27f6c2007-01-28 04:59:05 -08005128 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
5129
Eric Wongd81bf822007-01-10 01:22:38 -08005130 $reporter->finish_report($pool);
5131 $pool->clear;
5132 $editor->{git_commit_ok};
5133}
5134
Eric Wong2b27f6c2007-01-28 04:59:05 -08005135# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
5136# svn_ra_reparent didn't work before 1.4)
Eric Wongd81bf822007-01-10 01:22:38 -08005137sub gs_do_switch {
Eric Wong8a603772007-01-31 02:45:50 -08005138 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
5139 my $path = $gs->{path};
Eric Wongd81bf822007-01-10 01:22:38 -08005140 my $pool = SVN::Pool->new;
Eric Wong2b27f6c2007-01-28 04:59:05 -08005141
5142 my $full_url = $self->{url};
5143 my $old_url = $full_url;
Eric Wong2a679c72009-07-19 22:08:45 -07005144 $full_url .= '/' . $path if length $path;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08005145 my ($ra, $reparented);
Alec Berrymanad0a82b2008-09-14 17:14:16 -04005146
Eric Wong2a679c72009-07-19 22:08:45 -07005147 if ($old_url =~ m#^svn(\+ssh)?://# ||
5148 ($full_url =~ m#^https?://# &&
5149 escape_url($full_url) ne $full_url)) {
Alec Berrymanad0a82b2008-09-14 17:14:16 -04005150 $_[0] = undef;
5151 $self = undef;
5152 $RA = undef;
5153 $ra = Git::SVN::Ra->new($full_url);
5154 $ra_invalid = 1;
5155 } elsif ($old_url ne $full_url) {
5156 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, $pool);
5157 $self->{url} = $full_url;
5158 $reparented = 1;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08005159 }
Alec Berrymanad0a82b2008-09-14 17:14:16 -04005160
Eric Wong5d3b7cd2007-01-29 19:16:01 -08005161 $ra ||= $self;
Eric Wongf4392df2008-09-06 20:18:18 -07005162 $url_b = escape_url($url_b);
Eric Wong8a603772007-01-31 02:45:50 -08005163 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08005164 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong8b8fc062007-01-22 11:44:57 -08005165 $reporter->set_path('', $rev_a, 0, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08005166 $reporter->finish_report($pool);
Eric Wong2b27f6c2007-01-28 04:59:05 -08005167
Eric Wong5d3b7cd2007-01-29 19:16:01 -08005168 if ($reparented) {
5169 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
5170 $self->{url} = $old_url;
5171 }
Eric Wong2b27f6c2007-01-28 04:59:05 -08005172
Eric Wongd81bf822007-01-10 01:22:38 -08005173 $pool->clear;
5174 $editor->{git_commit_ok};
5175}
5176
Eric Wongb54a9012007-06-13 02:37:03 -07005177sub longest_common_path {
5178 my ($gsv, $globs) = @_;
Eric Wongd2ae1432007-02-07 11:50:16 -08005179 my %common;
Eric Wonge5181922007-02-08 12:53:57 -08005180 my $common_max = scalar @$gsv;
5181
5182 foreach my $gs (@$gsv) {
Eric Wongd2ae1432007-02-07 11:50:16 -08005183 my @tmp = split m#/#, $gs->{path};
5184 my $p = '';
5185 foreach (@tmp) {
5186 $p .= length($p) ? "/$_" : $_;
5187 $common{$p} ||= 0;
5188 $common{$p}++;
5189 }
5190 }
Eric Wonge5181922007-02-08 12:53:57 -08005191 $globs ||= [];
5192 $common_max += scalar @$globs;
5193 foreach my $glob (@$globs) {
5194 my @tmp = split m#/#, $glob->{path}->{left};
5195 my $p = '';
5196 foreach (@tmp) {
5197 $p .= length($p) ? "/$_" : $_;
5198 $common{$p} ||= 0;
5199 $common{$p}++;
5200 }
5201 }
5202
Eric Wongd2ae1432007-02-07 11:50:16 -08005203 my $longest_path = '';
5204 foreach (sort {length $b <=> length $a} keys %common) {
Eric Wonge5181922007-02-08 12:53:57 -08005205 if ($common{$_} == $common_max) {
Eric Wongd2ae1432007-02-07 11:50:16 -08005206 $longest_path = $_;
5207 last;
5208 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08005209 }
Eric Wongb54a9012007-06-13 02:37:03 -07005210 $longest_path;
5211}
5212
5213sub gs_fetch_loop_common {
5214 my ($self, $base, $head, $gsv, $globs) = @_;
5215 return if ($base > $head);
5216 my $inc = $_log_window_size;
5217 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
5218 my $longest_path = longest_common_path($gsv, $globs);
Eric Wonga51cdb02007-09-07 04:00:40 -07005219 my $ra_url = $self->{url};
Alex Vandiverc69700f2009-05-06 16:18:52 -04005220 my $find_trailing_edge;
Eric Wong0af9c9f2007-01-27 22:28:56 -08005221 while (1) {
Eric Wongd4eff2b2007-01-30 14:04:22 -08005222 my %revs;
Eric Wongd2ae1432007-02-07 11:50:16 -08005223 my $err;
Eric Wongf7c3fc42007-01-29 18:34:55 -08005224 my $err_handler = $SVN::Error::handler;
Eric Wongd2ae1432007-02-07 11:50:16 -08005225 $SVN::Error::handler = sub {
5226 ($err) = @_;
5227 skip_unknown_revs($err);
5228 };
5229 sub _cb {
5230 my ($paths, $r, $author, $date, $log) = @_;
Mattias Nissler3c49a032009-07-07 01:39:52 +02005231 [ $paths,
Eric Wongd2ae1432007-02-07 11:50:16 -08005232 { author => $author, date => $date, log => $log } ];
5233 }
5234 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
5235 sub { $revs{$_[1]} = _cb(@_) });
Deskin Miller99366562009-02-08 19:33:18 -05005236 if ($err) {
5237 print "Checked through r$max\r";
Alex Vandiverc69700f2009-05-06 16:18:52 -04005238 } else {
5239 $find_trailing_edge = 1;
Deskin Miller99366562009-02-08 19:33:18 -05005240 }
Alex Vandiverc69700f2009-05-06 16:18:52 -04005241 if ($err and $find_trailing_edge) {
Eric Wongd2ae1432007-02-07 11:50:16 -08005242 print STDERR "Path '$longest_path' ",
5243 "was probably deleted:\n",
5244 $err->expanded_message,
5245 "\nWill attempt to follow ",
5246 "revisions r$min .. r$max ",
5247 "committed before the deletion\n";
5248 my $hi = $max;
5249 while (--$hi >= $min) {
5250 my $ok;
5251 $self->get_log([$longest_path], $min, $hi,
5252 0, 1, 1, sub {
Alex Vandiverb6c61772009-05-06 16:18:53 -04005253 $ok = $_[1];
Eric Wongd2ae1432007-02-07 11:50:16 -08005254 $revs{$_[1]} = _cb(@_) });
5255 if ($ok) {
5256 print STDERR "r$min .. r$ok OK\n";
5257 last;
5258 }
5259 }
Alex Vandiverc69700f2009-05-06 16:18:52 -04005260 $find_trailing_edge = 0;
Eric Wongd2ae1432007-02-07 11:50:16 -08005261 }
Eric Wongd4eff2b2007-01-30 14:04:22 -08005262 $SVN::Error::handler = $err_handler;
Eric Wongfbcc1732007-02-06 18:35:30 -08005263
Eric Wonge5181922007-02-08 12:53:57 -08005264 my %exists = map { $_->{path} => $_ } @$gsv;
Eric Wongd4eff2b2007-01-30 14:04:22 -08005265 foreach my $r (sort {$a <=> $b} keys %revs) {
Eric Wongfbcc1732007-02-06 18:35:30 -08005266 my ($paths, $logged) = @{$revs{$r}};
Eric Wonge5181922007-02-08 12:53:57 -08005267
5268 foreach my $gs ($self->match_globs(\%exists, $paths,
5269 $globs, $r)) {
Eric Wong060610c2007-12-08 23:27:41 -08005270 if ($gs->rev_map_max >= $r) {
Eric Wongfbcc1732007-02-06 18:35:30 -08005271 next;
5272 }
5273 next unless $gs->match_paths($paths, $r);
5274 $gs->{logged_rev_props} = $logged;
Eric Wonge8d120b2007-02-14 16:29:52 -08005275 if (my $last_commit = $gs->last_commit) {
5276 $gs->assert_index_clean($last_commit);
5277 }
Eric Wongfbcc1732007-02-06 18:35:30 -08005278 my $log_entry = $gs->do_fetch($paths, $r);
5279 if ($log_entry) {
Eric Wong0af9c9f2007-01-27 22:28:56 -08005280 $gs->do_git_commit($log_entry);
5281 }
Eric Wong321b1842008-01-02 10:10:03 -08005282 $INDEX_FILES{$gs->{index}} = 1;
Eric Wong0af9c9f2007-01-27 22:28:56 -08005283 }
Eric Wonge5181922007-02-08 12:53:57 -08005284 foreach my $g (@$globs) {
Eric Wong93f26892007-02-11 01:20:26 -08005285 my $k = "svn-remote.$g->{remote}." .
5286 "$g->{t}-maxRev";
5287 Git::SVN::tmp_config($k, $r);
Eric Wonge5181922007-02-08 12:53:57 -08005288 }
Eric Wonga51cdb02007-09-07 04:00:40 -07005289 if ($ra_invalid) {
5290 $_[0] = undef;
5291 $self = undef;
5292 $RA = undef;
5293 $self = Git::SVN::Ra->new($ra_url);
5294 $ra_invalid = undef;
5295 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08005296 }
Eric Wong9c93fee2007-01-31 17:22:31 -08005297 # pre-fill the .rev_db since it'll eventually get filled in
5298 # with '0' x40 if something new gets committed
Eric Wonge5181922007-02-08 12:53:57 -08005299 foreach my $gs (@$gsv) {
Eric Wong66ab84b2007-12-08 23:27:42 -08005300 next if $gs->rev_map_max >= $max;
5301 next if defined $gs->rev_map_get($max);
5302 $gs->rev_map_set($max, 0 x40);
Eric Wong9c93fee2007-01-31 17:22:31 -08005303 }
Eric Wongc3560e52007-02-12 16:03:32 -08005304 foreach my $g (@$globs) {
5305 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
5306 Git::SVN::tmp_config($k, $max);
5307 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08005308 last if $max >= $head;
5309 $min = $max + 1;
5310 $max += $inc;
5311 $max = $head if ($max > $head);
5312 }
Karl Hasselström94bc9142008-02-03 17:56:18 +01005313 Git::SVN::gc();
Eric Wong0af9c9f2007-01-27 22:28:56 -08005314}
5315
Marcus Griep570d35c2008-08-08 01:41:57 -07005316sub get_dir_globbed {
5317 my ($self, $left, $depth, $r) = @_;
5318
5319 my @x = eval { $self->get_dir($left, $r) };
5320 return unless scalar @x == 3;
5321 my $dirents = $x[0];
5322 my @finalents;
5323 foreach my $de (keys %$dirents) {
5324 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
5325 if ($depth > 1) {
5326 my @args = ("$left/$de", $depth - 1, $r);
5327 foreach my $dir ($self->get_dir_globbed(@args)) {
5328 push @finalents, "$de/$dir";
5329 }
5330 } else {
5331 push @finalents, $de;
5332 }
5333 }
5334 @finalents;
5335}
5336
Eric Wonge5181922007-02-08 12:53:57 -08005337sub match_globs {
5338 my ($self, $exists, $paths, $globs, $r) = @_;
Eric Wong74a81222007-02-10 13:28:50 -08005339
5340 sub get_dir_check {
5341 my ($self, $exists, $g, $r) = @_;
Marcus Griep570d35c2008-08-08 01:41:57 -07005342
5343 my @dirs = $self->get_dir_globbed($g->{path}->{left},
5344 $g->{path}->{depth},
5345 $r);
5346
5347 foreach my $de (@dirs) {
Eric Wong74a81222007-02-10 13:28:50 -08005348 my $p = $g->{path}->full_path($de);
5349 next if $exists->{$p};
5350 next if (length $g->{path}->{right} &&
5351 ($self->check_path($p, $r) !=
5352 $SVN::Node::dir));
Jay Soffian07576202010-01-23 03:30:01 -05005353 next unless $p =~ /$g->{path}->{regex}/;
Eric Wong74a81222007-02-10 13:28:50 -08005354 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
5355 $g->{ref}->full_path($de), 1);
5356 }
5357 }
Eric Wonge5181922007-02-08 12:53:57 -08005358 foreach my $g (@$globs) {
Eric Wong74a81222007-02-10 13:28:50 -08005359 if (my $path = $paths->{"/$g->{path}->{left}"}) {
5360 if ($path->{action} =~ /^[AR]$/) {
5361 get_dir_check($self, $exists, $g, $r);
5362 }
5363 }
Eric Wonge5181922007-02-08 12:53:57 -08005364 foreach (keys %$paths) {
Eric Wong28710f72007-02-14 13:32:21 -08005365 if (/$g->{path}->{left_regex}/ &&
5366 !/$g->{path}->{regex}/) {
Eric Wong74a81222007-02-10 13:28:50 -08005367 next if $paths->{$_}->{action} !~ /^[AR]$/;
5368 get_dir_check($self, $exists, $g, $r);
5369 }
Eric Wonge5181922007-02-08 12:53:57 -08005370 next unless /$g->{path}->{regex}/;
5371 my $p = $1;
5372 my $pathname = $g->{path}->full_path($p);
5373 next if $exists->{$pathname};
Eric Wong0c1ec5a2007-04-18 00:17:33 -07005374 next if ($self->check_path($pathname, $r) !=
5375 $SVN::Node::dir);
Eric Wonge5181922007-02-08 12:53:57 -08005376 $exists->{$pathname} = Git::SVN->init(
5377 $self->{url}, $pathname, undef,
5378 $g->{ref}->full_path($p), 1);
5379 }
5380 my $c = '';
5381 foreach (split m#/#, $g->{path}->{left}) {
5382 $c .= "/$_";
5383 next unless ($paths->{$c} &&
Eric Wong74a81222007-02-10 13:28:50 -08005384 ($paths->{$c}->{action} =~ /^[AR]$/));
5385 get_dir_check($self, $exists, $g, $r);
Eric Wonge5181922007-02-08 12:53:57 -08005386 }
5387 }
5388 values %$exists;
5389}
5390
Eric Wonge6434f82007-01-23 16:29:23 -08005391sub minimize_url {
5392 my ($self) = @_;
5393 return $self->{url} if ($self->{url} eq $self->{repos_root});
5394 my $url = $self->{repos_root};
5395 my @components = split(m!/!, $self->{svn_path});
5396 my $c = '';
5397 do {
5398 $url .= "/$c" if length $c;
Eric Wong5f8b2cb2009-07-25 13:14:16 -07005399 eval {
5400 my $ra = (ref $self)->new($url);
5401 my $latest = $ra->get_latest_revnum;
5402 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
5403 };
Eric Wonge6434f82007-01-23 16:29:23 -08005404 } while ($@ && ($c = shift @components));
5405 $url;
5406}
5407
Eric Wongd81bf822007-01-10 01:22:38 -08005408sub can_do_switch {
5409 my $self = shift;
5410 unless (defined $can_do_switch) {
5411 my $pool = SVN::Pool->new;
5412 my $rep = eval {
5413 $self->do_switch(1, '', 0, $self->{url},
5414 SVN::Delta::Editor->new, $pool);
5415 };
5416 if ($@) {
5417 $can_do_switch = 0;
5418 } else {
5419 $rep->abort_report($pool);
5420 $can_do_switch = 1;
5421 }
5422 $pool->clear;
5423 }
5424 $can_do_switch;
5425}
5426
Eric Wong0af9c9f2007-01-27 22:28:56 -08005427sub skip_unknown_revs {
5428 my ($err) = @_;
5429 my $errno = $err->apr_err();
5430 # Maybe the branch we're tracking didn't
5431 # exist when the repo started, so it's
5432 # not an error if it doesn't, just continue
5433 #
5434 # Wonderfully consistent library, eh?
5435 # 160013 - svn:// and file://
5436 # 175002 - http(s)://
5437 # 175007 - http(s):// (this repo required authorization, too...)
5438 # More codes may be discovered later...
5439 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
Eric Wonga6a15a92007-03-30 17:54:48 -07005440 my $err_key = $err->expanded_message;
5441 # revision numbers change every time, filter them out
5442 $err_key =~ s/\d+/\0/g;
5443 $err_key = "$errno\0$err_key";
5444 unless ($ignored_err{$err_key}) {
5445 warn "W: Ignoring error from SVN, path probably ",
5446 "does not exist: ($errno): ",
5447 $err->expanded_message,"\n";
Eric Wongeee8a172008-01-07 02:40:40 -08005448 warn "W: Do not be alarmed at the above message ",
5449 "git-svn is just searching aggressively for ",
5450 "old history.\n",
5451 "This may take a while on large repositories\n";
Eric Wonga6a15a92007-03-30 17:54:48 -07005452 $ignored_err{$err_key} = 1;
5453 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08005454 return;
5455 }
5456 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
5457}
5458
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005459package Git::SVN::Log;
5460use strict;
5461use warnings;
5462use POSIX qw/strftime/;
Ben Waltone8717842009-02-24 14:44:49 -05005463use Time::Local;
David D Kilzer111947e2007-11-11 22:56:52 -08005464use constant commit_log_separator => ('-' x 72) . "\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005465use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
5466 %rusers $show_commit $incremental/;
5467my $l_fmt;
5468
5469sub cmt_showable {
5470 my ($c) = @_;
5471 return 1 if defined $c->{r};
Eric Wongc16d0872007-04-08 00:59:22 -07005472
5473 # big commit message got truncated by the 16k pretty buffer in rev-list
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005474 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
5475 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
Eric Wongc16d0872007-04-08 00:59:22 -07005476 @{$c->{l}} = ();
Eric Wong44320b92007-01-13 22:35:53 -08005477 my @log = command(qw/cat-file commit/, $c->{c});
Eric Wongc16d0872007-04-08 00:59:22 -07005478
5479 # shift off the headers
5480 shift @log while ($log[0] ne '');
Eric Wong44320b92007-01-13 22:35:53 -08005481 shift @log;
Eric Wongc16d0872007-04-08 00:59:22 -07005482
5483 # TODO: make $c->{l} not have a trailing newline in the future
5484 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005485
5486 (undef, $c->{r}, undef) = ::extract_metadata(
Eric Wong44320b92007-01-13 22:35:53 -08005487 (grep(/^git-svn-id: /, @log))[-1]);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005488 }
5489 return defined $c->{r};
5490}
5491
5492sub log_use_color {
Jeff Kingcd459e32007-12-11 01:28:42 -05005493 return $color || Git->repository->get_colorbool('color.diff');
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005494}
5495
5496sub git_svn_log_cmd {
Eric Wong3bc718b2007-02-13 17:09:40 -08005497 my ($r_min, $r_max, @args) = @_;
5498 my $head = 'HEAD';
Eric Wong6ed77262007-08-15 09:55:18 -07005499 my (@files, @log_opts);
Eric Wong3bc718b2007-02-13 17:09:40 -08005500 foreach my $x (@args) {
Eric Wong6ed77262007-08-15 09:55:18 -07005501 if ($x eq '--' || @files) {
5502 push @files, $x;
5503 } else {
5504 if (::verify_ref("$x^0")) {
5505 $head = $x;
5506 } else {
5507 push @log_opts, $x;
5508 }
5509 }
Eric Wong3bc718b2007-02-13 17:09:40 -08005510 }
5511
Eric Wong13c823f2007-04-08 00:59:19 -07005512 my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
5513 $gs ||= Git::SVN->_new;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005514 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
5515 $gs->refname);
5516 push @cmd, '-r' unless $non_recursive;
5517 push @cmd, qw/--raw --name-status/ if $verbose;
5518 push @cmd, '--color' if log_use_color();
Eric Wong6ed77262007-08-15 09:55:18 -07005519 push @cmd, @log_opts;
5520 if (defined $r_max && $r_max == $r_min) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005521 push @cmd, '--max-count=1';
Eric Wong060610c2007-12-08 23:27:41 -08005522 if (my $c = $gs->rev_map_get($r_max)) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005523 push @cmd, $c;
5524 }
Eric Wong6ed77262007-08-15 09:55:18 -07005525 } elsif (defined $r_max) {
David D Kilzer111947e2007-11-11 22:56:52 -08005526 if ($r_max < $r_min) {
5527 ($r_min, $r_max) = ($r_max, $r_min);
5528 }
5529 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
5530 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
5531 # If there are no commits in the range, both $c_max and $c_min
5532 # will be undefined. If there is at least 1 commit in the
5533 # range, both will be defined.
5534 return () if !defined $c_min || !defined $c_max;
5535 if ($c_min eq $c_max) {
5536 push @cmd, '--max-count=1', $c_min;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005537 } else {
David D Kilzer111947e2007-11-11 22:56:52 -08005538 push @cmd, '--boundary', "$c_min..$c_max";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005539 }
5540 }
Eric Wong6ed77262007-08-15 09:55:18 -07005541 return (@cmd, @files);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005542}
5543
5544# adapted from pager.c
5545sub config_pager {
Jonathan Nieder190c1cd2010-02-14 06:06:10 -06005546 if (! -t *STDOUT) {
5547 $ENV{GIT_PAGER_IN_USE} = 'false';
5548 $pager = undef;
5549 return;
5550 }
5551 chomp($pager = command_oneline(qw(var GIT_PAGER)));
Jonathan Niederdec543e2009-10-30 20:43:19 -05005552 if ($pager eq 'cat') {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005553 $pager = undef;
5554 }
Jeff Kingcd459e32007-12-11 01:28:42 -05005555 $ENV{GIT_PAGER_IN_USE} = defined($pager);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005556}
5557
5558sub run_pager {
Jonathan Nieder190c1cd2010-02-14 06:06:10 -06005559 return unless defined $pager;
Marcus Griep971e6282008-09-10 11:09:46 -04005560 pipe my ($rfd, $wfd) or return;
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005561 defined(my $pid = fork) or ::fatal "Can't fork: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005562 if (!$pid) {
5563 open STDOUT, '>&', $wfd or
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005564 ::fatal "Can't redirect to stdout: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005565 return;
5566 }
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005567 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005568 $ENV{LESS} ||= 'FRSX';
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005569 exec $pager or ::fatal "Can't run pager: $! ($pager)";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005570}
5571
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005572sub format_svn_date {
Ben Waltone8717842009-02-24 14:44:49 -05005573 # some systmes don't handle or mishandle %z, so be creative.
Ben Walton736e6192009-02-27 22:11:45 -05005574 my $t = shift || time;
Ben Waltone8717842009-02-24 14:44:49 -05005575 my $gm = timelocal(gmtime($t));
5576 my $sign = qw( + + - )[ $t <=> $gm ];
5577 my $gmoff = sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
5578 return strftime("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005579}
5580
5581sub parse_git_date {
5582 my ($t, $tz) = @_;
5583 # Date::Parse isn't in the standard Perl distro :(
5584 if ($tz =~ s/^\+//) {
5585 $t += tz_to_s_offset($tz);
5586 } elsif ($tz =~ s/^\-//) {
5587 $t -= tz_to_s_offset($tz);
5588 }
5589 return $t;
5590}
5591
5592sub set_local_timezone {
5593 if (defined $TZ) {
5594 $ENV{TZ} = $TZ;
5595 } else {
5596 delete $ENV{TZ};
5597 }
5598}
5599
Eric Wong21819a32007-01-27 14:38:10 -08005600sub tz_to_s_offset {
5601 my ($tz) = @_;
5602 $tz =~ s/(\d\d)$//;
5603 return ($1 * 60) + ($tz * 3600);
5604}
5605
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005606sub get_author_info {
5607 my ($dest, $author, $t, $tz) = @_;
5608 $author =~ s/(?:^\s*|\s*$)//g;
5609 $dest->{a_raw} = $author;
5610 my $au;
Eric Wong1c8443b2007-01-14 02:17:00 -08005611 if ($::_authors) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005612 $au = $rusers{$author} || undef;
5613 }
5614 if (!$au) {
5615 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
5616 }
5617 $dest->{t} = $t;
5618 $dest->{tz} = $tz;
5619 $dest->{a} = $au;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005620 $dest->{t_utc} = parse_git_date($t, $tz);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005621}
5622
5623sub process_commit {
5624 my ($c, $r_min, $r_max, $defer) = @_;
5625 if (defined $r_min && defined $r_max) {
5626 if ($r_min == $c->{r} && $r_min == $r_max) {
5627 show_commit($c);
5628 return 0;
5629 }
5630 return 1 if $r_min == $r_max;
5631 if ($r_min < $r_max) {
5632 # we need to reverse the print order
5633 return 0 if (defined $limit && --$limit < 0);
5634 push @$defer, $c;
5635 return 1;
5636 }
5637 if ($r_min != $r_max) {
5638 return 1 if ($r_min < $c->{r});
5639 return 1 if ($r_max > $c->{r});
5640 }
5641 }
5642 return 0 if (defined $limit && --$limit < 0);
5643 show_commit($c);
5644 return 1;
5645}
5646
5647sub show_commit {
5648 my $c = shift;
5649 if ($oneline) {
5650 my $x = "\n";
5651 if (my $l = $c->{l}) {
5652 while ($l->[0] =~ /^\s*$/) { shift @$l }
5653 $x = $l->[0];
5654 }
5655 $l_fmt ||= 'A' . length($c->{r});
5656 print 'r',pack($l_fmt, $c->{r}),' | ';
5657 print "$c->{c} | " if $show_commit;
5658 print $x;
5659 } else {
5660 show_commit_normal($c);
5661 }
5662}
5663
5664sub show_commit_changed_paths {
5665 my ($c) = @_;
5666 return unless $c->{changed};
5667 print "Changed paths:\n", @{$c->{changed}};
5668}
5669
5670sub show_commit_normal {
5671 my ($c) = @_;
David D Kilzer111947e2007-11-11 22:56:52 -08005672 print commit_log_separator, "r$c->{r} | ";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005673 print "$c->{c} | " if $show_commit;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005674 print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005675 my $nr_line = 0;
5676
5677 if (my $l = $c->{l}) {
5678 while ($l->[$#$l] eq "\n" && $#$l > 0
5679 && $l->[($#$l - 1)] eq "\n") {
5680 pop @$l;
5681 }
5682 $nr_line = scalar @$l;
5683 if (!$nr_line) {
5684 print "1 line\n\n\n";
5685 } else {
5686 if ($nr_line == 1) {
5687 $nr_line = '1 line';
5688 } else {
5689 $nr_line .= ' lines';
5690 }
5691 print $nr_line, "\n";
5692 show_commit_changed_paths($c);
5693 print "\n";
5694 print $_ foreach @$l;
5695 }
5696 } else {
5697 print "1 line\n";
5698 show_commit_changed_paths($c);
5699 print "\n";
5700
5701 }
Eric Wong488a63e2007-02-15 00:40:42 -08005702 foreach my $x (qw/raw stat diff/) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005703 if ($c->{$x}) {
5704 print "\n";
5705 print $_ foreach @{$c->{$x}}
5706 }
5707 }
5708}
5709
5710sub cmd_show_log {
5711 my (@args) = @_;
5712 my ($r_min, $r_max);
5713 my $r_last = -1; # prevent dupes
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005714 set_local_timezone();
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005715 if (defined $::_revision) {
5716 if ($::_revision =~ /^(\d+):(\d+)$/) {
5717 ($r_min, $r_max) = ($1, $2);
5718 } elsif ($::_revision =~ /^\d+$/) {
5719 $r_min = $r_max = $::_revision;
5720 } else {
5721 ::fatal "-r$::_revision is not supported, use ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005722 "standard 'git log' arguments instead";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005723 }
5724 }
5725
5726 config_pager();
Eric Wong6ed77262007-08-15 09:55:18 -07005727 @args = git_svn_log_cmd($r_min, $r_max, @args);
David D Kilzer111947e2007-11-11 22:56:52 -08005728 if (!@args) {
5729 print commit_log_separator unless $incremental || $oneline;
5730 return;
5731 }
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005732 my $log = command_output_pipe(@args);
5733 run_pager();
Eric Wong488a63e2007-02-15 00:40:42 -08005734 my (@k, $c, $d, $stat);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005735 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
5736 while (<$log>) {
David D Kilzer60f3ff12007-11-10 22:10:34 -08005737 if (/^${esc_color}commit -?($::sha1_short)/o) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005738 my $cmt = $1;
5739 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
5740 $r_last = $c->{r};
5741 process_commit($c, $r_min, $r_max, \@k) or
5742 goto out;
5743 }
5744 $d = undef;
5745 $c = { c => $cmt };
5746 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
5747 get_author_info($c, $1, $2, $3);
5748 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
5749 # ignore
5750 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
5751 push @{$c->{raw}}, $_;
5752 } elsif (/^${esc_color}[ACRMDT]\t/) {
5753 # we could add $SVN->{svn_path} here, but that requires
5754 # remote access at the moment (repo_path_split)...
5755 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
5756 push @{$c->{changed}}, $_;
5757 } elsif (/^${esc_color}diff /o) {
5758 $d = 1;
5759 push @{$c->{diff}}, $_;
5760 } elsif ($d) {
5761 push @{$c->{diff}}, $_;
Eric Wong488a63e2007-02-15 00:40:42 -08005762 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
5763 $esc_color*[\+\-]*$esc_color$/x) {
5764 $stat = 1;
5765 push @{$c->{stat}}, $_;
5766 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
5767 push @{$c->{stat}}, $_;
5768 $stat = undef;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005769 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
5770 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
5771 } elsif (s/^${esc_color} //o) {
5772 push @{$c->{l}}, $_;
5773 }
5774 }
5775 if ($c && defined $c->{r} && $c->{r} != $r_last) {
5776 $r_last = $c->{r};
5777 process_commit($c, $r_min, $r_max, \@k);
5778 }
5779 if (@k) {
David D Kilzer111947e2007-11-11 22:56:52 -08005780 ($r_min, $r_max) = ($r_max, $r_min);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005781 process_commit($_, $r_min, $r_max) foreach reverse @k;
5782 }
5783out:
Eric Wongc843c462007-01-12 03:07:31 -08005784 close $log;
David D Kilzer111947e2007-11-11 22:56:52 -08005785 print commit_log_separator unless $incremental || $oneline;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005786}
5787
Tim Stoakes6fb53752008-02-10 15:21:08 +10305788sub cmd_blame {
Steven Grimm4be40382008-05-10 22:11:18 -07005789 my $path = pop;
Tim Stoakes6fb53752008-02-10 15:21:08 +10305790
5791 config_pager();
5792 run_pager();
5793
Steven Grimm4be40382008-05-10 22:11:18 -07005794 my ($fh, $ctx, $rev);
5795
5796 if ($_git_format) {
5797 ($fh, $ctx) = command_output_pipe('blame', @_, $path);
5798 while (my $line = <$fh>) {
5799 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
5800 # Uncommitted edits show up as a rev ID of
5801 # all zeros, which we can't look up with
5802 # cmt_metadata
5803 if ($1 !~ /^0+$/) {
5804 (undef, $rev, undef) =
5805 ::cmt_metadata($1);
5806 $rev = '0' if (!$rev);
5807 } else {
5808 $rev = '0';
5809 }
5810 $rev = sprintf('%-10s', $rev);
5811 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
5812 }
5813 print $line;
Tim Stoakes6fb53752008-02-10 15:21:08 +10305814 }
Steven Grimm4be40382008-05-10 22:11:18 -07005815 } else {
5816 ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD',
5817 '--', $path);
5818 my ($sha1);
5819 my %authors;
Boris Byk6ea42032009-04-11 00:32:41 +04005820 my @buffer;
5821 my %dsha; #distinct sha keys
5822
Steven Grimm4be40382008-05-10 22:11:18 -07005823 while (my $line = <$fh>) {
Boris Byk6ea42032009-04-11 00:32:41 +04005824 push @buffer, $line;
Steven Grimm4be40382008-05-10 22:11:18 -07005825 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
Boris Byk6ea42032009-04-11 00:32:41 +04005826 $dsha{$1} = 1;
5827 }
5828 }
5829
5830 my $s2r = ::cmt_sha2rev_batch([keys %dsha]);
5831
5832 foreach my $line (@buffer) {
5833 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
5834 $rev = $s2r->{$1};
5835 $rev = '0' if (!$rev)
Steven Grimm4be40382008-05-10 22:11:18 -07005836 }
5837 elsif ($line =~ /^author (.*)/) {
5838 $authors{$rev} = $1;
5839 $authors{$rev} =~ s/\s/_/g;
5840 }
5841 elsif ($line =~ /^\t(.*)$/) {
5842 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
5843 }
5844 }
Tim Stoakes6fb53752008-02-10 15:21:08 +10305845 }
5846 command_close_pipe($fh, $ctx);
5847}
5848
Eric Wong706587f2007-01-18 17:50:01 -08005849package Git::SVN::Migration;
5850# these version numbers do NOT correspond to actual version numbers
5851# of git nor git-svn. They are just relative.
5852#
5853# v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
5854#
5855# v1 layout: .git/$id/info/url, refs/remotes/$id
5856#
5857# v2 layout: .git/svn/$id/info/url, refs/remotes/$id
5858#
5859# v3 layout: .git/svn/$id, refs/remotes/$id
5860# - info/url may remain for backwards compatibility
5861# - this is what we migrate up to this layout automatically,
5862# - this will be used by git svn init on single branches
Eric Wong26a62d52007-02-12 13:25:25 -08005863# v3.1 layout (auto migrated):
5864# - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
5865# for backwards compatibility
Eric Wong706587f2007-01-18 17:50:01 -08005866#
5867# v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
5868# - this is only created for newly multi-init-ed
5869# repositories. Similar in spirit to the
5870# --use-separate-remotes option in git-clone (now default)
5871# - we do not automatically migrate to this (following
5872# the example set by core git)
Eric Wong060610c2007-12-08 23:27:41 -08005873#
5874# v5 layout: .rev_db.$UUID => .rev_map.$UUID
5875# - newer, more-efficient format that uses 24-bytes per record
5876# with no filler space.
5877# - use xxd -c24 < .rev_map.$UUID to view and debug
5878# - This is a one-way migration, repositories updated to the
5879# new format will not be able to use old git-svn without
5880# rebuilding the .rev_db. Rebuilding the rev_db is not
5881# possible if noMetadata or useSvmProps are set; but should
5882# be no problem for users that use the (sensible) defaults.
Eric Wong706587f2007-01-18 17:50:01 -08005883use strict;
5884use warnings;
5885use Carp qw/croak/;
5886use File::Path qw/mkpath/;
Eric Wong47e39c52007-01-21 04:27:09 -08005887use File::Basename qw/dirname basename/;
5888use vars qw/$_minimize/;
Eric Wong706587f2007-01-18 17:50:01 -08005889
5890sub migrate_from_v0 {
5891 my $git_dir = $ENV{GIT_DIR};
5892 return undef unless -d $git_dir;
5893 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5894 my $migrated = 0;
5895 while (<$fh>) {
5896 chomp;
5897 my ($id, $orig_ref) = ($_, $_);
5898 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
5899 next unless -f "$git_dir/$id/info/url";
5900 my $new_ref = "refs/remotes/$id";
5901 if (::verify_ref("$new_ref^0")) {
5902 print STDERR "W: $orig_ref is probably an old ",
5903 "branch used by an ancient version of ",
5904 "git-svn.\n",
5905 "However, $new_ref also exists.\n",
5906 "We will not be able ",
5907 "to use this branch until this ",
5908 "ambiguity is resolved.\n";
5909 next;
5910 }
5911 print STDERR "Migrating from v0 layout...\n" if !$migrated;
5912 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
5913 command_noisy('update-ref', $new_ref, $orig_ref);
5914 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
5915 $migrated++;
5916 }
5917 command_close_pipe($fh, $ctx);
5918 print STDERR "Done migrating from v0 layout...\n" if $migrated;
5919 $migrated;
5920}
5921
5922sub migrate_from_v1 {
5923 my $git_dir = $ENV{GIT_DIR};
5924 my $migrated = 0;
5925 return $migrated unless -d $git_dir;
5926 my $svn_dir = "$git_dir/svn";
5927
5928 # just in case somebody used 'svn' as their $id at some point...
5929 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
5930
5931 print STDERR "Migrating from a git-svn v1 layout...\n";
5932 mkpath([$svn_dir]);
5933 print STDERR "Data from a previous version of git-svn exists, but\n\t",
5934 "$svn_dir\n\t(required for this version ",
Frederik Schwarzer8f510be2008-07-14 18:30:24 +02005935 "($::VERSION) of git-svn) does not exist.\n";
Eric Wong706587f2007-01-18 17:50:01 -08005936 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5937 while (<$fh>) {
5938 my $x = $_;
5939 next unless $x =~ s#^refs/remotes/##;
5940 chomp $x;
5941 next unless -f "$git_dir/$x/info/url";
5942 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
5943 next unless $u;
5944 my $dn = dirname("$git_dir/svn/$x");
5945 mkpath([$dn]) unless -d $dn;
5946 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
5947 mkpath(["$git_dir/svn/svn"]);
5948 print STDERR " - $git_dir/$x/info => ",
5949 "$git_dir/svn/$x/info\n";
5950 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
5951 croak "$!: $x";
5952 # don't worry too much about these, they probably
5953 # don't exist with repos this old (save for index,
5954 # and we can easily regenerate that)
5955 foreach my $f (qw/unhandled.log index .rev_db/) {
5956 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
5957 }
5958 } else {
5959 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
5960 rename "$git_dir/$x", "$git_dir/svn/$x" or
5961 croak "$!: $x";
5962 }
5963 $migrated++;
5964 }
5965 command_close_pipe($fh, $ctx);
5966 print STDERR "Done migrating from a git-svn v1 layout\n";
5967 $migrated;
5968}
5969
5970sub read_old_urls {
5971 my ($l_map, $pfx, $path) = @_;
5972 my @dir;
5973 foreach (<$path/*>) {
5974 if (-r "$_/info/url") {
5975 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
5976 my $ref_id = $pfx . basename $_;
5977 my $url = ::file_to_s("$_/info/url");
5978 $l_map->{$ref_id} = $url;
5979 } elsif (-d $_) {
5980 push @dir, $_;
5981 }
5982 }
5983 foreach (@dir) {
5984 my $x = $_;
5985 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
5986 read_old_urls($l_map, $x, $_);
5987 }
5988}
5989
5990sub migrate_from_v2 {
5991 my @cfg = command(qw/config -l/);
5992 return if grep /^svn-remote\..+\.url=/, @cfg;
5993 my %l_map;
5994 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
5995 my $migrated = 0;
5996
5997 foreach my $ref_id (sort keys %l_map) {
Eric Wong471bc002007-02-01 04:06:27 -08005998 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
5999 if ($@) {
6000 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
6001 }
Eric Wong706587f2007-01-18 17:50:01 -08006002 $migrated++;
6003 }
6004 $migrated;
6005}
6006
Eric Wong47e39c52007-01-21 04:27:09 -08006007sub minimize_connections {
6008 my $r = Git::SVN::read_all_remotes();
6009 my $new_urls = {};
6010 my $root_repos = {};
6011 foreach my $repo_id (keys %$r) {
6012 my $url = $r->{$repo_id}->{url} or next;
6013 my $fetch = $r->{$repo_id}->{fetch} or next;
6014 my $ra = Git::SVN::Ra->new($url);
6015
6016 # skip existing cases where we already connect to the root
6017 if (($ra->{url} eq $ra->{repos_root}) ||
Eric Wong7829f202008-06-28 20:40:32 -07006018 ($ra->{repos_root} eq $repo_id)) {
Eric Wong47e39c52007-01-21 04:27:09 -08006019 $root_repos->{$ra->{url}} = $repo_id;
6020 next;
6021 }
6022
6023 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
6024 my $root_path = $ra->{url};
Eric Wong4e9f6cc2007-02-09 12:17:57 -08006025 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
Eric Wong47e39c52007-01-21 04:27:09 -08006026 foreach my $path (keys %$fetch) {
6027 my $ref_id = $fetch->{$path};
6028 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
6029
6030 # make sure we can read when connecting to
6031 # a higher level of a repository
6032 my ($last_rev, undef) = $gs->last_rev_commit;
6033 if (!defined $last_rev) {
6034 $last_rev = eval {
6035 $root_ra->get_latest_revnum;
6036 };
6037 next if $@;
6038 }
6039 my $new = $root_path;
6040 $new .= length $path ? "/$path" : '';
6041 eval {
6042 $root_ra->get_log([$new], $last_rev, $last_rev,
6043 0, 0, 1, sub { });
6044 };
6045 next if $@;
6046 $new_urls->{$ra->{repos_root}}->{$new} =
6047 { ref_id => $ref_id,
6048 old_repo_id => $repo_id,
6049 old_path => $path };
6050 }
6051 }
6052
6053 my @emptied;
6054 foreach my $url (keys %$new_urls) {
6055 # see if we can re-use an existing [svn-remote "repo_id"]
6056 # instead of creating a(n ugly) new section:
Eric Wong7829f202008-06-28 20:40:32 -07006057 my $repo_id = $root_repos->{$url} || $url;
Eric Wong47e39c52007-01-21 04:27:09 -08006058
6059 my $fetch = $new_urls->{$url};
6060 foreach my $path (keys %$fetch) {
6061 my $x = $fetch->{$path};
6062 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
6063 my $pfx = "svn-remote.$x->{old_repo_id}";
6064
6065 my $old_fetch = quotemeta("$x->{old_path}:".
Adam Brewster6f5748e2009-08-11 23:14:27 -04006066 "$x->{ref_id}");
Eric Wong8b8fc062007-01-22 11:44:57 -08006067 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08006068 "$pfx.fetch", '^'. $old_fetch . '$');
6069 delete $r->{$x->{old_repo_id}}->
6070 {fetch}->{$x->{old_path}};
6071 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
Eric Wong8b8fc062007-01-22 11:44:57 -08006072 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08006073 "$pfx.url");
6074 push @emptied, $x->{old_repo_id}
6075 }
6076 }
6077 }
6078 if (@emptied) {
Johannes Schindelin8befc502008-12-14 23:10:52 +01006079 my $file = $ENV{GIT_CONFIG} || "$ENV{GIT_DIR}/config";
Eric Wong47e39c52007-01-21 04:27:09 -08006080 print STDERR <<EOF;
6081The following [svn-remote] sections in your config file ($file) are empty
6082and can be safely removed:
6083EOF
6084 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
6085 }
6086}
6087
Eric Wong706587f2007-01-18 17:50:01 -08006088sub migration_check {
6089 migrate_from_v0();
6090 migrate_from_v1();
6091 migrate_from_v2();
Eric Wong47e39c52007-01-21 04:27:09 -08006092 minimize_connections() if $_minimize;
Eric Wong706587f2007-01-18 17:50:01 -08006093}
6094
Eric Wongef3cfaa2007-01-24 03:30:57 -08006095package Git::IndexInfo;
6096use strict;
6097use warnings;
6098use Git qw/command_input_pipe command_close_pipe/;
6099
6100sub new {
6101 my ($class) = @_;
6102 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
6103 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
6104}
6105
6106sub remove {
6107 my ($self, $path) = @_;
6108 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
6109 return ++$self->{nr};
6110 }
6111 undef;
6112}
6113
6114sub update {
6115 my ($self, $mode, $hash, $path) = @_;
6116 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
6117 return ++$self->{nr};
6118 }
6119 undef;
6120}
6121
6122sub DESTROY {
6123 my ($self) = @_;
6124 command_close_pipe($self->{gui}, $self->{ctx});
6125}
6126
Eric Wong4bb9ed02007-02-03 13:29:17 -08006127package Git::SVN::GlobSpec;
6128use strict;
6129use warnings;
6130
6131sub new {
Jay Soffian07576202010-01-23 03:30:01 -05006132 my ($class, $glob, $pattern_ok) = @_;
Eric Wong4bb9ed02007-02-03 13:29:17 -08006133 my $re = $glob;
6134 $re =~ s!/+$!!g; # no need for trailing slashes
Jay Soffian07576202010-01-23 03:30:01 -05006135 my (@left, @right, @patterns);
6136 my $state = "left";
6137 my $die_msg = "Only one set of wildcard directories " .
6138 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
6139 for my $part (split(m|/|, $glob)) {
6140 if ($part =~ /\*/ && $part ne "*") {
6141 die "Invalid pattern in '$glob': $part\n";
6142 } elsif ($pattern_ok && $part =~ /[{}]/ &&
6143 $part !~ /^\{[^{}]+\}/) {
6144 die "Invalid pattern in '$glob': $part\n";
6145 }
6146 if ($part eq "*") {
6147 die $die_msg if $state eq "right";
6148 $state = "pattern";
6149 push(@patterns, "[^/]*");
6150 } elsif ($pattern_ok && $part =~ /^\{(.*)\}$/) {
6151 die $die_msg if $state eq "right";
6152 $state = "pattern";
6153 my $p = quotemeta($1);
6154 $p =~ s/\\,/|/g;
6155 push(@patterns, "(?:$p)");
6156 } else {
6157 if ($state eq "left") {
6158 push(@left, $part);
6159 } else {
6160 push(@right, $part);
6161 $state = "right";
6162 }
6163 }
Marcus Griep570d35c2008-08-08 01:41:57 -07006164 }
Jay Soffian07576202010-01-23 03:30:01 -05006165 my $depth = @patterns;
Marcus Griep570d35c2008-08-08 01:41:57 -07006166 if ($depth == 0) {
Jay Soffian07576202010-01-23 03:30:01 -05006167 die "One '*' is needed in glob: '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08006168 }
Jay Soffian07576202010-01-23 03:30:01 -05006169 my $left = join('/', @left);
6170 my $right = join('/', @right);
6171 $re = join('/', @patterns);
6172 $re = join('\/',
6173 grep(length, quotemeta($left), "($re)", quotemeta($right)));
Eric Wong74a81222007-02-10 13:28:50 -08006174 my $left_re = qr/^\/\Q$left\E(\/|$)/;
6175 bless { left => $left, right => $right, left_regex => $left_re,
Marcus Griep570d35c2008-08-08 01:41:57 -07006176 regex => qr/$re/, glob => $glob, depth => $depth }, $class;
Eric Wong4bb9ed02007-02-03 13:29:17 -08006177}
6178
6179sub full_path {
6180 my ($self, $path) = @_;
6181 return (length $self->{left} ? "$self->{left}/" : '') .
6182 $path . (length $self->{right} ? "/$self->{right}" : '');
6183}
6184
Eric Wong3397f9d2006-02-16 01:24:16 -08006185__END__
6186
6187Data structures:
6188
Eric Wong4bb9ed02007-02-03 13:29:17 -08006189
6190$remotes = { # returned by read_all_remotes()
6191 'svn' => {
6192 # svn-remote.svn.url=https://svn.musicpd.org
6193 url => 'https://svn.musicpd.org',
6194 # svn-remote.svn.fetch=mpd/trunk:trunk
6195 fetch => {
6196 'mpd/trunk' => 'trunk',
6197 },
6198 # svn-remote.svn.tags=mpd/tags/*:tags/*
6199 tags => {
6200 path => {
6201 left => 'mpd/tags',
6202 right => '',
6203 regex => qr!mpd/tags/([^/]+)$!,
6204 glob => 'tags/*',
6205 },
6206 ref => {
6207 left => 'tags',
6208 right => '',
6209 regex => qr!tags/([^/]+)$!,
6210 glob => 'tags/*',
6211 },
6212 }
6213 }
6214};
6215
Eric Wong44320b92007-01-13 22:35:53 -08006216$log_entry hashref as returned by libsvn_log_entry()
Eric Wong3397f9d2006-02-16 01:24:16 -08006217{
Eric Wong44320b92007-01-13 22:35:53 -08006218 log => 'whitespace-formatted log entry
Eric Wong3397f9d2006-02-16 01:24:16 -08006219', # trailing newline is preserved
6220 revision => '8', # integer
6221 date => '2004-02-24T17:01:44.108345Z', # commit date
6222 author => 'committer name'
6223};
6224
Eric Wong6e8548c2007-01-27 01:32:00 -08006225
6226# this is generated by generate_diff();
Eric Wong3397f9d2006-02-16 01:24:16 -08006227@mods = array of diff-index line hashes, each element represents one line
6228 of diff-index output
6229
6230diff-index line ($m hash)
6231{
6232 mode_a => first column of diff-index output, no leading ':',
6233 mode_b => second column of diff-index output,
6234 sha1_b => sha1sum of the final blob,
Eric Wongac8e0b92006-03-03 01:20:07 -08006235 chg => change type [MCRADT],
Eric Wong3397f9d2006-02-16 01:24:16 -08006236 file_a => original file name of a file (iff chg is 'C' or 'R')
6237 file_b => new/current file name of a file (any chg)
6238}
6239;
Eric Wonga5e0ced2006-06-12 15:23:48 -07006240
Eric Wonga00439a2006-06-27 19:39:13 -07006241# retval of read_url_paths{,_all}();
6242$l_map = {
6243 # repository root url
6244 'https://svn.musicpd.org' => {
6245 # repository path # GIT_SVN_ID
6246 'mpd/trunk' => 'trunk',
6247 'mpd/tags/0.11.5' => 'tags/0.11.5',
6248 },
6249}
6250
Eric Wonga5e0ced2006-06-12 15:23:48 -07006251Notes:
6252 I don't trust the each() function on unless I created %hash myself
6253 because the internal iterator may not have started at base.