blob: da3fea8bd2c857bbc2e841dec7545318b3b9354e [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;
James Y Knightf5549af2011-04-04 15:09:08 -040062use Memoize; # core since 5.8.0, Jul 2002
Eric Wonga5e0ced2006-06-12 15:23:48 -070063
Eric Wong336f1712007-01-11 02:14:43 -080064BEGIN {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120065 # import functions from Git into our packages, en masse
66 no strict 'refs';
Eric Wong336f1712007-01-11 02:14:43 -080067 foreach (qw/command command_oneline command_noisy command_output_pipe
Boris Byk6ea42032009-04-11 00:32:41 +040068 command_input_pipe command_close_pipe
69 command_bidi_pipe command_close_bidi_pipe/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120070 for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -080071 Git::SVN::Migration Git::SVN::Log Git::SVN),
Sam Vilainc5f71ad2007-06-15 15:43:59 +120072 __PACKAGE__) {
73 *{"${package}::$_"} = \&{"Git::$_"};
74 }
Eric Wong336f1712007-01-11 02:14:43 -080075 }
James Y Knightf5549af2011-04-04 15:09:08 -040076 Memoize::memoize 'Git::config';
77 Memoize::memoize 'Git::config_bool';
Eric Wong336f1712007-01-11 02:14:43 -080078}
79
Eric Wongb9c85182006-12-15 23:58:07 -080080my ($SVN);
Eric Wong83e99402006-10-11 18:19:55 -070081
Eric Wongf8c9d1d2007-01-12 02:35:20 -080082$sha1 = qr/[a-f\d]{40}/;
83$sha1_short = qr/[a-f\d]{4,40}/;
Eric Wong44320b92007-01-13 22:35:53 -080084my ($_stdin, $_help, $_edit,
Marc Branchaud62244062009-06-23 13:02:08 -040085 $_message, $_file, $_branch_dest,
Eric Wongd05d72e2007-01-15 22:59:26 -080086 $_template, $_shared,
Jason Merrillc2abd832009-04-06 16:37:59 -040087 $_version, $_fetch_all, $_no_rebase, $_fetch_parent,
Eric Wongdee41f32007-03-13 11:40:36 -070088 $_merge, $_strategy, $_dry_run, $_local,
Steven Grimm4be40382008-05-10 22:11:18 -070089 $_prefix, $_no_checkout, $_url, $_verbose,
Steven Walter6abd9332010-09-24 23:51:50 -040090 $_git_format, $_commit_url, $_tag, $_merge_info);
Eric Wong0bed5ea2007-02-09 02:45:03 -080091$Git::SVN::_follow_parent = 1;
Simon Arlott49750f32009-03-30 19:31:41 +010092$_q ||= 0;
Eric Wong706587f2007-01-18 17:50:01 -080093my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
94 'config-dir=s' => \$Git::SVN::Ra::config_dir,
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +020095 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
96 'ignore-paths=s' => \$SVN::Git::Fetcher::_ignore_regex );
Eric Wong0bed5ea2007-02-09 02:45:03 -080097my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
Eric Wongdc5869c2006-05-24 02:07:32 -070098 'authors-file|A=s' => \$_authors,
Mark Lodato36db1ed2009-05-14 21:27:15 -040099 'authors-prog=s' => \$_authors_prog,
Eric Wongecc712d2007-01-31 12:28:10 -0800100 'repack:i' => \$Git::SVN::_repack,
Eric Wong97ae0912007-02-11 15:21:24 -0800101 'noMetadata' => \$Git::SVN::_no_metadata,
102 'useSvmProps' => \$Git::SVN::_use_svm_props,
Eric Wong62e349d2007-02-16 19:57:29 -0800103 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props,
Eric Wong6af1db42007-02-14 16:04:10 -0800104 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size,
Eric Wong1e889ef2007-02-16 01:45:13 -0800105 'no-checkout' => \$_no_checkout,
Simon Arlott49750f32009-03-30 19:31:41 +0100106 'quiet|q+' => \$_q,
Eric Wongecc712d2007-01-31 12:28:10 -0800107 'repack-flags|repack-args|repack-opts=s' =>
108 \$Git::SVN::_repack_flags,
Andy Whitcroft70ae04e2007-11-22 13:44:42 +0000109 'use-log-author' => \$Git::SVN::_use_log_author,
Avery Pennarun6aa9ba12008-04-15 21:04:17 -0400110 'add-author-from' => \$Git::SVN::_add_author_from,
Pete Harlane82f0d72009-01-17 20:10:14 -0800111 'localtime' => \$Git::SVN::_localtime,
Eric Wong706587f2007-01-18 17:50:01 -0800112 %remote_opts );
Eric Wong36f5b1f2006-05-23 19:23:41 -0700113
Marc Branchaud62244062009-06-23 13:02:08 -0400114my ($_trunk, @_tags, @_branches, $_stdlayout);
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800115my %icv;
Eric Wongdadc6d22007-02-14 12:27:41 -0800116my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared,
Marc Branchaud62244062009-06-23 13:02:08 -0400117 'trunk|T=s' => \$_trunk, 'tags|t=s@' => \@_tags,
118 'branches|b=s@' => \@_branches, 'prefix=s' => \$_prefix,
martin f. krafft8f728fb2007-07-14 11:25:28 +0200119 'stdlayout|s' => \$_stdlayout,
Eric Wong6b488292009-07-25 00:00:50 -0700120 'minimize-url|m!' => \$Git::SVN::_minimize_url,
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800121 'no-metadata' => sub { $icv{noMetadata} = 1 },
122 'use-svm-props' => sub { $icv{useSvmProps} = 1 },
123 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 },
124 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] },
Jay Soffian3e18ce12010-01-23 03:30:00 -0500125 'rewrite-uuid=s' => sub { $icv{rewriteUUID} = $_[1] },
Eric Wongdadc6d22007-02-14 12:27:41 -0800126 %remote_opts );
Eric Wong27e9fb82006-06-27 19:39:12 -0700127my %cmt_opts = ( 'edit|e' => \$_edit,
Eric Wong24e22aa2007-01-29 00:07:49 -0800128 'rmdir' => \$SVN::Git::Editor::_rmdir,
129 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder,
130 'l=i' => \$SVN::Git::Editor::_rename_limit,
131 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity
Eric Wong27e9fb82006-06-27 19:39:12 -0700132);
Eric Wong9d55b412006-06-12 15:53:13 -0700133
Eric Wong3397f9d2006-02-16 01:24:16 -0800134my %cmd = (
Eric Wong2a3240b2007-01-04 18:09:56 -0800135 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
Eric Wonge98671e2007-02-14 02:21:19 -0800136 { 'revision|r=s' => \$_revision,
Eric Wong905f8b72007-02-16 03:22:40 -0800137 'fetch-all|all' => \$_fetch_all,
Jason Merrillc2abd832009-04-06 16:37:59 -0400138 'parent|p' => \$_fetch_parent,
Eric Wonge98671e2007-02-14 02:21:19 -0800139 %fc_opts } ],
Eric Wong0425ea92007-02-16 18:45:01 -0800140 clone => [ \&cmd_clone, "Initialize and fetch revisions",
141 { 'revision|r=s' => \$_revision,
142 %fc_opts, %init_opts } ],
Eric Wongd2866f92007-01-11 12:26:16 -0800143 init => [ \&cmd_init, "Initialize a repo for tracking" .
Eric Wongf8ab6b72006-05-31 15:49:56 -0700144 " (requires URL argument)",
Eric Wong9d55b412006-06-12 15:53:13 -0700145 \%init_opts ],
Eric Wongdadc6d22007-02-14 12:27:41 -0800146 'multi-init' => [ \&cmd_multi_init,
147 "Deprecated alias for ".
148 "'$0 init -T<trunk> -b<branches> -t<tags>'",
149 \%init_opts ],
Eric Wongd7ad3be2007-01-14 03:14:28 -0800150 dcommit => [ \&cmd_dcommit,
151 'Commit several diffs to merge with upstream',
Eric Wong3289e862006-12-15 23:58:08 -0800152 { 'merge|m|M' => \$_merge,
153 'strategy|s=s' => \$_strategy,
Eric Wong905f8b72007-02-16 03:22:40 -0800154 'verbose|v' => \$_verbose,
Eric Wong3289e862006-12-15 23:58:08 -0800155 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800156 'fetch-all|all' => \$_fetch_all,
Eric Wongba24e742008-08-07 02:06:16 -0700157 'commit-url=s' => \$_commit_url,
158 'revision|r=i' => \$_revision,
Karl Hasselström171af112007-05-03 07:51:35 +0200159 'no-rebase' => \$_no_rebase,
Steven Walter6abd9332010-09-24 23:51:50 -0400160 'mergeinfo=s' => \$_merge_info,
Eric Wong4b155222006-12-22 21:59:24 -0800161 %cmt_opts, %fc_opts } ],
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700162 branch => [ \&cmd_branch,
163 'Create a branch in the SVN repository',
164 { 'message|m=s' => \$_message,
Marc Branchaud62244062009-06-23 13:02:08 -0400165 'destination|d=s' => \$_branch_dest,
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700166 'dry-run|n' => \$_dry_run,
Igor Mironov6594f0b2010-01-12 03:21:51 +1100167 'tag|t' => \$_tag,
168 'username=s' => \$Git::SVN::Prompt::_username,
169 'commit-url=s' => \$_commit_url } ],
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700170 tag => [ sub { $_tag = 1; cmd_branch(@_) },
171 'Create a tag in the SVN repository',
172 { 'message|m=s' => \$_message,
Marc Branchaud62244062009-06-23 13:02:08 -0400173 'destination|d=s' => \$_branch_dest,
Igor Mironov6594f0b2010-01-12 03:21:51 +1100174 'dry-run|n' => \$_dry_run,
175 'username=s' => \$Git::SVN::Prompt::_username,
176 'commit-url=s' => \$_commit_url } ],
Eric Wong1ce255d2007-01-14 23:21:16 -0800177 'set-tree' => [ \&cmd_set_tree,
178 "Set an SVN repository to a git tree-ish",
Robin H. Johnsone84dc6d2009-05-05 11:16:14 -0700179 { 'stdin' => \$_stdin, %cmt_opts, %fc_opts, } ],
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200180 'create-ignore' => [ \&cmd_create_ignore,
181 'Create a .gitignore per svn:ignore',
182 { 'revision|r=i' => \$_revision
183 } ],
Eric Wong6111b932009-11-15 18:57:16 -0800184 'mkdirs' => [ \&cmd_mkdirs ,
185 "recreate empty directories after a checkout",
186 { 'revision|r=i' => \$_revision } ],
Benoit Sigoure15153452007-10-16 16:36:50 +0200187 'propget' => [ \&cmd_propget,
188 'Print the value of a property on a file or directory',
189 { 'revision|r=i' => \$_revision } ],
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200190 'proplist' => [ \&cmd_proplist,
191 'List all properties of a file or directory',
192 { 'revision|r=i' => \$_revision } ],
Eric Wong5969cbe2007-01-11 17:58:39 -0800193 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200194 { 'revision|r=i' => \$_revision
Lars Hjemli05b4df32007-09-05 11:35:29 +0200195 } ],
Vineet Kumar2d879792007-11-19 14:56:15 -0800196 'show-externals' => [ \&cmd_show_externals, "Show svn:externals listings",
197 { 'revision|r=i' => \$_revision
198 } ],
Eric Wong1c8443b2007-01-14 02:17:00 -0800199 'multi-fetch' => [ \&cmd_multi_fetch,
Eric Wonge98671e2007-02-14 02:21:19 -0800200 "Deprecated alias for $0 fetch --all",
201 { 'revision|r=s' => \$_revision, %fc_opts } ],
Eric Wong706587f2007-01-18 17:50:01 -0800202 'migrate' => [ sub { },
203 # no-op, we automatically run this anyways,
Eric Wong706587f2007-01-18 17:50:01 -0800204 'Migrate configuration/metadata/layout from
205 previous versions of git-svn',
Eric Wonga836a0e2007-02-14 19:34:56 -0800206 { 'minimize' => \$Git::SVN::Migration::_minimize,
207 %remote_opts } ],
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800208 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
209 { 'limit=i' => \$Git::SVN::Log::limit,
Eric Wong79bb8d82006-06-01 02:35:44 -0700210 'revision|r=s' => \$_revision,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800211 'verbose|v' => \$Git::SVN::Log::verbose,
212 'incremental' => \$Git::SVN::Log::incremental,
213 'oneline' => \$Git::SVN::Log::oneline,
214 'show-commit' => \$Git::SVN::Log::show_commit,
215 'non-recursive' => \$Git::SVN::Log::non_recursive,
Eric Wong79bb8d82006-06-01 02:35:44 -0700216 'authors-file|A=s' => \$_authors,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800217 'color' => \$Git::SVN::Log::color,
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200218 'pager=s' => \$Git::SVN::Log::pager
Eric Wong79bb8d82006-06-01 02:35:44 -0700219 } ],
Eric Wong222566e2008-08-08 01:41:58 -0700220 'find-rev' => [ \&cmd_find_rev,
221 "Translate between SVN revision numbers and tree-ish",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200222 {} ],
Eric Wong905f8b72007-02-16 03:22:40 -0800223 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
224 { 'merge|m|M' => \$_merge,
225 'verbose|v' => \$_verbose,
226 'strategy|s=s' => \$_strategy,
Eric Wongdee41f32007-03-13 11:40:36 -0700227 'local|l' => \$_local,
Eric Wong905f8b72007-02-16 03:22:40 -0800228 'fetch-all|all' => \$_fetch_all,
Seth Falcon7d45e142008-05-19 20:29:17 -0700229 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800230 %fc_opts } ],
Eric Wong44320b92007-01-13 22:35:53 -0800231 'commit-diff' => [ \&cmd_commit_diff,
232 'Commit a diff between two trees',
Eric Wong27e9fb82006-06-27 19:39:12 -0700233 { 'message|m=s' => \$_message,
234 'file|F=s' => \$_file,
Eric Wong45bf4732006-11-09 01:19:37 -0800235 'revision|r=s' => \$_revision,
Eric Wong27e9fb82006-06-27 19:39:12 -0700236 %cmt_opts } ],
David D. Kilzere6fefa92007-11-21 11:57:18 -0800237 'info' => [ \&cmd_info,
238 "Show info about the latest SVN revision
239 on the current branch",
David D. Kilzer8b014d72007-11-21 11:57:19 -0800240 { 'url' => \$_url, } ],
Tim Stoakes6fb53752008-02-10 15:21:08 +1030241 'blame' => [ \&Git::SVN::Log::cmd_blame,
242 "Show what revision and author last modified each line of a file",
Steven Grimm4be40382008-05-10 22:11:18 -0700243 { 'git-format' => \$_git_format } ],
Ben Jackson195643f2009-06-03 20:45:52 -0700244 'reset' => [ \&cmd_reset,
245 "Undo fetches back to the specified SVN revision",
246 { 'revision|r=s' => \$_revision,
247 'parent|p' => \$_fetch_parent } ],
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -0500248 'gc' => [ \&cmd_gc,
249 "Compress unhandled.log files in .git/svn and remove " .
250 "index files in .git/svn",
251 {} ],
Eric Wong3397f9d2006-02-16 01:24:16 -0800252);
Eric Wong9d55b412006-06-12 15:53:13 -0700253
Eric Wong3397f9d2006-02-16 01:24:16 -0800254my $cmd;
255for (my $i = 0; $i < @ARGV; $i++) {
256 if (defined $cmd{$ARGV[$i]}) {
257 $cmd = $ARGV[$i];
258 splice @ARGV, $i, 1;
259 last;
Ben Jackson9a8c92a2009-05-30 18:17:06 -0700260 } elsif ($ARGV[$i] eq 'help') {
261 $cmd = $ARGV[$i+1];
262 usage(0);
Eric Wong3397f9d2006-02-16 01:24:16 -0800263 }
264};
265
Eric Wong540424b2007-12-19 00:31:43 -0800266# make sure we're always running at the top-level working directory
267unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
Eric Wong5253dc32007-02-20 01:36:30 -0800268 unless (-d $ENV{GIT_DIR}) {
269 if ($git_dir_user_set) {
270 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
271 "but it is not a directory\n";
272 }
273 my $git_dir = delete $ENV{GIT_DIR};
Deskin Millerfe4003f2008-11-06 00:07:39 -0500274 my $cdup = undef;
275 git_cmd_try {
276 $cdup = command_oneline(qw/rev-parse --show-cdup/);
277 $git_dir = '.' unless ($cdup);
278 chomp $cdup if ($cdup);
279 $cdup = "." unless ($cdup && length $cdup);
280 } "Already at toplevel, but $git_dir not found\n";
Eric Wong5253dc32007-02-20 01:36:30 -0800281 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
282 unless (-d $git_dir) {
283 die "$git_dir still not found after going to ",
284 "'$cdup'\n";
285 }
286 $ENV{GIT_DIR} = $git_dir;
287 }
Adam Robenffe256f2008-05-23 16:19:41 +0200288 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wong5253dc32007-02-20 01:36:30 -0800289}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100290
291my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
292
Eric Wong1a305822009-11-14 14:25:11 -0800293read_git_config(\%opts);
Eric Wong222566e2008-08-08 01:41:58 -0700294if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
295 Getopt::Long::Configure('pass_through');
296}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100297my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
298 'minimize-connections' => \$Git::SVN::Migration::_minimize,
299 'id|i=s' => \$Git::SVN::default_ref_id,
300 'svn-remote|remote|R=s' => sub {
301 $Git::SVN::no_reuse_existing = 1;
302 $Git::SVN::default_repo_id = $_[1] });
303exit 1 if (!$rv && $cmd && $cmd ne 'log');
304
305usage(0) if $_help;
306version() if $_version;
307usage(1) unless defined $cmd;
308load_authors() if $_authors;
Mark Lodato36db1ed2009-05-14 21:27:15 -0400309if (defined $_authors_prog) {
310 $_authors_prog = "'" . File::Spec->rel2abs($_authors_prog) . "'";
311}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100312
Eric Wong0425ea92007-02-16 18:45:01 -0800313unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
Eric Wong706587f2007-01-18 17:50:01 -0800314 Git::SVN::Migration::migration_check();
315}
Eric Wongecc712d2007-01-31 12:28:10 -0800316Git::SVN::init_vars();
Eric Wongb805b442007-01-22 13:52:04 -0800317eval {
318 Git::SVN::verify_remotes_sanity();
319 $cmd{$cmd}->[0]->(@ARGV);
320};
321fatal $@ if $@;
Eric Wong1e889ef2007-02-16 01:45:13 -0800322post_fetch_checkout();
Eric Wong3397f9d2006-02-16 01:24:16 -0800323exit 0;
324
325####################### primary functions ######################
326sub usage {
327 my $exit = shift || 0;
328 my $fd = $exit ? \*STDERR : \*STDOUT;
329 print $fd <<"";
330git-svn - bidirectional operations between a single Subversion tree and git
Stephan Beyer1b1dd232008-07-13 15:36:15 +0200331Usage: git svn <command> [options] [arguments]\n
Eric Wong448c81b2006-03-03 01:20:09 -0800332
333 print $fd "Available commands:\n" unless $cmd;
Eric Wong3397f9d2006-02-16 01:24:16 -0800334
335 foreach (sort keys %cmd) {
Eric Wong448c81b2006-03-03 01:20:09 -0800336 next if $cmd && $cmd ne $_;
Eric Wonga836a0e2007-02-14 19:34:56 -0800337 next if /^multi-/; # don't show deprecated commands
Eric Wongb203b762006-10-11 14:53:36 -0700338 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
Benoit Sigoureaa807bc2007-11-03 19:53:34 +0100339 foreach (sort keys %{$cmd{$_}->[2]}) {
Eric Wong512b6202007-04-03 01:57:08 -0700340 # mixed-case options are for .git/config only
341 next if /[A-Z]/ && /^[a-z]+$/i;
Eric Wong448c81b2006-03-03 01:20:09 -0800342 # prints out arguments as they should be passed:
Eric Wongb8c92ca2006-05-24 01:40:37 -0700343 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
Eric Wongb203b762006-10-11 14:53:36 -0700344 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
Eric Wong448c81b2006-03-03 01:20:09 -0800345 "--$_" : "-$_" }
346 split /\|/,$_)," $x\n";
347 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800348 }
349 print $fd <<"";
Eric Wong448c81b2006-03-03 01:20:09 -0800350\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
351arbitrary identifier if you're tracking multiple SVN branches/repositories in
352one git repository and want to keep them separate. See git-svn(1) for more
353information.
Eric Wong3397f9d2006-02-16 01:24:16 -0800354
355 exit $exit;
356}
357
Eric Wong551ce282006-02-20 10:57:29 -0800358sub version {
Michael J Gruberb0779242010-03-04 11:23:53 +0100359 ::_req_svn();
Eric Wong7d60ab22006-12-28 01:16:20 -0800360 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
Eric Wong551ce282006-02-20 10:57:29 -0800361 exit 0;
362}
363
Eric Wong8164b652007-01-11 15:35:55 -0800364sub do_git_init_db {
365 unless (-d $ENV{GIT_DIR}) {
366 my @init_db = ('init');
367 push @init_db, "--template=$_template" if defined $_template;
Eric Wongdadc6d22007-02-14 12:27:41 -0800368 if (defined $_shared) {
369 if ($_shared =~ /[a-z]/) {
370 push @init_db, "--shared=$_shared";
371 } else {
372 push @init_db, "--shared";
373 }
374 }
Eric Wong8164b652007-01-11 15:35:55 -0800375 command_noisy(@init_db);
Adam Robenffe256f2008-05-23 16:19:41 +0200376 $_repository = Git->repository(Repository => ".git");
Eric Wong8164b652007-01-11 15:35:55 -0800377 }
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800378 my $set;
379 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
380 foreach my $i (keys %icv) {
381 die "'$set' and '$i' cannot both be set\n" if $set;
382 next unless defined $icv{$i};
383 command_noisy('config', "$pfx.$i", $icv{$i});
384 $set = $i;
385 }
Ben Jackson88ec2052009-04-11 10:46:18 -0700386 my $ignore_regex = \$SVN::Git::Fetcher::_ignore_regex;
387 command_noisy('config', "$pfx.ignore-paths", $$ignore_regex)
388 if defined $$ignore_regex;
Eric Wong8164b652007-01-11 15:35:55 -0800389}
390
Eric Wongdadc6d22007-02-14 12:27:41 -0800391sub init_subdir {
392 my $repo_path = shift or return;
393 mkpath([$repo_path]) unless -d $repo_path;
394 chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
Eric Wongf30603f2007-02-23 01:26:26 -0800395 $ENV{GIT_DIR} = '.git';
Adam Robenffe256f2008-05-23 16:19:41 +0200396 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wongdadc6d22007-02-14 12:27:41 -0800397}
398
Eric Wong0425ea92007-02-16 18:45:01 -0800399sub cmd_clone {
400 my ($url, $path) = @_;
401 if (!defined $path &&
Marc Branchaud62244062009-06-23 13:02:08 -0400402 (defined $_trunk || @_branches || @_tags ||
martin f. krafft8f728fb2007-07-14 11:25:28 +0200403 defined $_stdlayout) &&
Eric Wong0425ea92007-02-16 18:45:01 -0800404 $url !~ m#^[a-z\+]+://#) {
405 $path = $url;
406 }
Eric Wong0425ea92007-02-16 18:45:01 -0800407 $path = basename($url) if !defined $path || !length $path;
Alex Vandiver2bc35dc2009-12-08 15:54:10 -0500408 my $authors_absolute = $_authors ? File::Spec->rel2abs($_authors) : "";
Eric Wongf30603f2007-02-23 01:26:26 -0800409 cmd_init($url, $path);
Alex Vandiver2bc35dc2009-12-08 15:54:10 -0500410 command_oneline('config', 'svn.authorsfile', $authors_absolute)
411 if $_authors;
Alex Vandiverf5841502009-12-08 15:54:11 -0500412 Git::SVN::fetch_all($Git::SVN::default_repo_id);
Eric Wong0425ea92007-02-16 18:45:01 -0800413}
414
Eric Wongd2866f92007-01-11 12:26:16 -0800415sub cmd_init {
martin f. krafft8f728fb2007-07-14 11:25:28 +0200416 if (defined $_stdlayout) {
417 $_trunk = 'trunk' if (!defined $_trunk);
Marc Branchaud62244062009-06-23 13:02:08 -0400418 @_tags = 'tags' if (! @_tags);
419 @_branches = 'branches' if (! @_branches);
martin f. krafft8f728fb2007-07-14 11:25:28 +0200420 }
Marc Branchaud62244062009-06-23 13:02:08 -0400421 if (defined $_trunk || @_branches || @_tags) {
Eric Wongdadc6d22007-02-14 12:27:41 -0800422 return cmd_multi_init(@_);
Eric Wong03e0ea82006-06-30 21:42:53 -0700423 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800424 my $url = shift or die "SVN repository location required ",
425 "as a command-line argument\n";
Ulrich Dangel50ff2362009-06-26 16:52:09 +0200426 $url = canonicalize_url($url);
Eric Wongdadc6d22007-02-14 12:27:41 -0800427 init_subdir(@_);
Eric Wong8164b652007-01-11 15:35:55 -0800428 do_git_init_db();
Eric Wong03e0ea82006-06-30 21:42:53 -0700429
Eric Wong6b488292009-07-25 00:00:50 -0700430 if ($Git::SVN::_minimize_url eq 'unset') {
431 $Git::SVN::_minimize_url = 0;
432 }
433
Eric Wong706587f2007-01-18 17:50:01 -0800434 Git::SVN->init($url);
Eric Wong3397f9d2006-02-16 01:24:16 -0800435}
436
Eric Wong2a3240b2007-01-04 18:09:56 -0800437sub cmd_fetch {
Eric Wonge98671e2007-02-14 02:21:19 -0800438 if (grep /^\d+=./, @_) {
439 die "'<rev>=<commit>' fetch arguments are ",
440 "no longer supported.\n";
Eric Wong07a1c952007-01-22 15:47:41 -0800441 }
Eric Wonge98671e2007-02-14 02:21:19 -0800442 my ($remote) = @_;
443 if (@_ > 1) {
Jason Merrillc2abd832009-04-06 16:37:59 -0400444 die "Usage: $0 fetch [--all] [--parent] [svn-remote]\n";
Eric Wonge98671e2007-02-14 02:21:19 -0800445 }
Eric Wong4d0157d2009-11-22 12:37:06 -0800446 $Git::SVN::no_reuse_existing = undef;
Jason Merrillc2abd832009-04-06 16:37:59 -0400447 if ($_fetch_parent) {
448 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
449 unless ($gs) {
450 die "Unable to determine upstream SVN information from ",
451 "working tree history\n";
452 }
453 # just fetch, don't checkout.
454 $_no_checkout = 'true';
455 $_fetch_all ? $gs->fetch_all : $gs->fetch;
456 } elsif ($_fetch_all) {
Eric Wonge98671e2007-02-14 02:21:19 -0800457 cmd_multi_fetch();
458 } else {
Jason Merrillc2abd832009-04-06 16:37:59 -0400459 $remote ||= $Git::SVN::default_repo_id;
Eric Wonge98671e2007-02-14 02:21:19 -0800460 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
Eric Wong1c8443b2007-01-14 02:17:00 -0800461 }
Eric Wong2a3240b2007-01-04 18:09:56 -0800462}
463
Eric Wong1ce255d2007-01-14 23:21:16 -0800464sub cmd_set_tree {
Eric Wong3397f9d2006-02-16 01:24:16 -0800465 my (@commits) = @_;
466 if ($_stdin || !@commits) {
467 print "Reading from stdin...\n";
468 @commits = ();
469 while (<STDIN>) {
Eric Wong1ca72ae2006-03-03 01:20:09 -0800470 if (/\b($sha1_short)\b/o) {
Eric Wong3397f9d2006-02-16 01:24:16 -0800471 unshift @commits, $1;
472 }
473 }
474 }
475 my @revs;
Eric Wong8de010a2006-02-20 10:57:26 -0800476 foreach my $c (@commits) {
Eric Wongaef4e922006-12-15 10:59:54 -0800477 my @tmp = command('rev-parse',$c);
Eric Wong8de010a2006-02-20 10:57:26 -0800478 if (scalar @tmp == 1) {
479 push @revs, $tmp[0];
480 } elsif (scalar @tmp > 1) {
Eric Wongaef4e922006-12-15 10:59:54 -0800481 push @revs, reverse(command('rev-list',@tmp));
Eric Wong8de010a2006-02-20 10:57:26 -0800482 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200483 fatal "Failed to rev-parse $c";
Eric Wong8de010a2006-02-20 10:57:26 -0800484 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800485 }
Eric Wong1ce255d2007-01-14 23:21:16 -0800486 my $gs = Git::SVN->new;
487 my ($r_last, $cmt_last) = $gs->last_rev_commit;
488 $gs->fetch;
Eric Wong97f69872007-01-25 11:53:13 -0800489 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
Eric Wong1ce255d2007-01-14 23:21:16 -0800490 fatal "There are new revisions that were fetched ",
491 "and need to be merged (or acknowledged) ",
492 "before committing.\nlast rev: $r_last\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200493 " current: $gs->{last_rev}";
Eric Wong1ce255d2007-01-14 23:21:16 -0800494 }
495 $gs->set_tree($_) foreach @revs;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700496 print "Done committing ",scalar @revs," revisions to SVN\n";
Eric Wong3157dd92007-12-13 08:27:34 -0800497 unlink $gs->{index};
Eric Wonga5e0ced2006-06-12 15:23:48 -0700498}
499
Eric Wongd7ad3be2007-01-14 03:14:28 -0800500sub cmd_dcommit {
501 my $head = shift;
David D. Kilzer181264a2010-08-02 12:58:19 -0700502 command_noisy(qw/update-index --refresh/);
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100503 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
David Reiss826a9332007-11-13 13:47:26 -0800504 'Cannot dcommit with a dirty index. Commit your changes first, '
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100505 . "or stash them with `git stash'.\n";
Eric Wongd7ad3be2007-01-14 03:14:28 -0800506 $head ||= 'HEAD';
Thomas Rast5eec27e2009-05-29 17:09:42 +0200507
508 my $old_head;
509 if ($head ne 'HEAD') {
510 $old_head = eval {
511 command_oneline([qw/symbolic-ref -q HEAD/])
512 };
513 if ($old_head) {
514 $old_head =~ s{^refs/heads/}{};
515 } else {
516 $old_head = eval { command_oneline(qw/rev-parse HEAD/) };
517 }
518 command(['checkout', $head], STDERR => 0);
519 }
520
Eric Wonga8ae2622007-02-13 14:22:11 -0800521 my @refs;
Thomas Rast5eec27e2009-05-29 17:09:42 +0200522 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD', \@refs);
Thomas Rast2cb61102008-08-31 15:50:59 +0200523 unless ($gs) {
524 die "Unable to determine upstream SVN information from ",
525 "$head history.\nPerhaps the repository is empty.";
526 }
Peter Oberndorfer0df84052009-02-23 12:02:53 +0100527
528 if (defined $_commit_url) {
529 $url = $_commit_url;
530 } else {
531 $url = eval { command_oneline('config', '--get',
532 "svn-remote.$gs->{repo_id}.commiturl") };
533 if (!$url) {
Alejandro R. Sedeño12a296b2011-04-08 10:57:54 -0400534 $url = $gs->full_pushurl
Peter Oberndorfer0df84052009-02-23 12:02:53 +0100535 }
536 }
537
Eric Wongba24e742008-08-07 02:06:16 -0700538 my $last_rev = $_revision if defined $_revision;
Matthieu Moy59b0c242008-04-24 20:06:36 +0200539 if ($url) {
540 print "Committing to $url ...\n";
541 }
Eric Wong733a65a2007-06-13 02:23:28 -0700542 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
Eric Wong751eb392007-08-31 18:16:12 -0700543 if ($_no_rebase && scalar(@$linear_refs) > 1) {
544 warn "Attempting to commit more than one change while ",
545 "--no-rebase is enabled.\n",
546 "If these changes depend on each other, re-running ",
Eric Wong7dfa16b2008-01-02 10:09:49 -0800547 "without --no-rebase may be required."
Eric Wong751eb392007-08-31 18:16:12 -0700548 }
Eric Wong711521e2008-08-20 00:30:06 -0700549 my $expect_url = $url;
550 Git::SVN::remove_username($expect_url);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800551 while (1) {
552 my $d = shift @$linear_refs or last;
Eric Wong45bf4732006-11-09 01:19:37 -0800553 unless (defined $last_rev) {
554 (undef, $last_rev, undef) = cmt_metadata("$d~1");
555 unless (defined $last_rev) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800556 fatal "Unable to extract revision information ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200557 "from commit $d~1";
Eric Wong45bf4732006-11-09 01:19:37 -0800558 }
559 }
Eric Wongb22d4492006-08-26 00:01:23 -0700560 if ($_dry_run) {
561 print "diff-tree $d~1 $d\n";
562 } else {
Eric Wong751eb392007-08-31 18:16:12 -0700563 my $cmt_rev;
Eric Wongd7ad3be2007-01-14 03:14:28 -0800564 my %ed_opts = ( r => $last_rev,
Eric Wong61395352007-01-27 14:33:08 -0800565 log => get_commit_entry($d)->{log},
Eric Wongba24e742008-08-07 02:06:16 -0700566 ra => Git::SVN::Ra->new($url),
Konstantin V. Arkhipov3caf3202007-11-14 03:52:02 +0300567 config => SVN::Core::config_get_config(
568 $Git::SVN::Ra::config_dir
569 ),
Eric Wong61395352007-01-27 14:33:08 -0800570 tree_a => "$d~1",
571 tree_b => $d,
572 editor_cb => sub {
573 print "Committed r$_[0]\n";
Eric Wong751eb392007-08-31 18:16:12 -0700574 $cmt_rev = $_[0];
575 },
Steven Walter6abd9332010-09-24 23:51:50 -0400576 mergeinfo => $_merge_info,
Eric Wonga8ae2622007-02-13 14:22:11 -0800577 svn_path => '');
Eric Wong61395352007-01-27 14:33:08 -0800578 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800579 print "No changes\n$d~1 == $d\n";
Eric Wong733a65a2007-06-13 02:23:28 -0700580 } elsif ($parents->{$d} && @{$parents->{$d}}) {
Eric Wong751eb392007-08-31 18:16:12 -0700581 $gs->{inject_parents_dcommit}->{$cmt_rev} =
Eric Wong733a65a2007-06-13 02:23:28 -0700582 $parents->{$d};
Eric Wongd7ad3be2007-01-14 03:14:28 -0800583 }
Eric Wong751eb392007-08-31 18:16:12 -0700584 $_fetch_all ? $gs->fetch_all : $gs->fetch;
Eric Wong7dfa16b2008-01-02 10:09:49 -0800585 $last_rev = $cmt_rev;
Eric Wong751eb392007-08-31 18:16:12 -0700586 next if $_no_rebase;
587
588 # we always want to rebase against the current HEAD,
589 # not any head that was passed to us
Eric Wongc74d9ac2007-11-05 03:21:47 -0800590 my @diff = command('diff-tree', $d,
Eric Wong751eb392007-08-31 18:16:12 -0700591 $gs->refname, '--');
592 my @finish;
593 if (@diff) {
594 @finish = rebase_cmd();
Eric Wongc74d9ac2007-11-05 03:21:47 -0800595 print STDERR "W: $d and ", $gs->refname,
Eric Wong751eb392007-08-31 18:16:12 -0700596 " differ, using @finish:\n",
Eric Wongc74d9ac2007-11-05 03:21:47 -0800597 join("\n", @diff), "\n";
Eric Wong751eb392007-08-31 18:16:12 -0700598 } else {
599 print "No changes between current HEAD and ",
600 $gs->refname,
601 "\nResetting to the latest ",
602 $gs->refname, "\n";
603 @finish = qw/reset --mixed/;
604 }
605 command_noisy(@finish, $gs->refname);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800606 if (@diff) {
607 @refs = ();
608 my ($url_, $rev_, $uuid_, $gs_) =
Thomas Rast5eec27e2009-05-29 17:09:42 +0200609 working_head_info('HEAD', \@refs);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800610 my ($linear_refs_, $parents_) =
611 linearize_history($gs_, \@refs);
612 if (scalar(@$linear_refs) !=
613 scalar(@$linear_refs_)) {
614 fatal "# of revisions changed ",
615 "\nbefore:\n",
616 join("\n", @$linear_refs),
617 "\n\nafter:\n",
618 join("\n", @$linear_refs_), "\n",
619 'If you are attempting to commit ',
620 "merges, try running:\n\t",
621 'git rebase --interactive',
622 '--preserve-merges ',
623 $gs->refname,
624 "\nBefore dcommitting";
625 }
Eric Wong711521e2008-08-20 00:30:06 -0700626 if ($url_ ne $expect_url) {
Alexander Gavrilovc03c1f72009-10-09 11:01:04 +0400627 if ($url_ eq $gs->metadata_url) {
628 print
629 "Accepting rewritten URL:",
630 " $url_\n";
631 } else {
632 fatal
633 "URL mismatch after rebase:",
634 " $url_ != $expect_url";
635 }
Eric Wongc74d9ac2007-11-05 03:21:47 -0800636 }
637 if ($uuid_ ne $uuid) {
638 fatal "uuid mismatch after rebase: ",
639 "$uuid_ != $uuid";
640 }
641 # remap parents
642 my (%p, @l, $i);
643 for ($i = 0; $i < scalar @$linear_refs; $i++) {
644 my $new = $linear_refs_->[$i] or next;
645 $p{$new} =
646 $parents->{$linear_refs->[$i]};
647 push @l, $new;
648 }
649 $parents = \%p;
650 $linear_refs = \@l;
651 }
Eric Wongb22d4492006-08-26 00:01:23 -0700652 }
653 }
Thomas Rast5eec27e2009-05-29 17:09:42 +0200654
655 if ($old_head) {
656 my $new_head = command_oneline(qw/rev-parse HEAD/);
657 my $new_is_symbolic = eval {
658 command_oneline(qw/symbolic-ref -q HEAD/);
659 };
660 if ($new_is_symbolic) {
661 print "dcommitted the branch ", $head, "\n";
662 } else {
663 print "dcommitted on a detached HEAD because you gave ",
664 "a revision argument.\n",
665 "The rewritten commit is: ", $new_head, "\n";
666 }
667 command(['checkout', $old_head], STDERR => 0);
668 }
669
Eric Wong3157dd92007-12-13 08:27:34 -0800670 unlink $gs->{index};
Eric Wongb22d4492006-08-26 00:01:23 -0700671}
672
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700673sub cmd_branch {
674 my ($branch_name, $head) = @_;
675
676 unless (defined $branch_name && length $branch_name) {
677 die(($_tag ? "tag" : "branch") . " name required\n");
678 }
679 $head ||= 'HEAD';
680
Eric Wong150d38c2009-12-22 22:40:18 -0800681 my (undef, $rev, undef, $gs) = working_head_info($head);
Alejandro R. Sedeño12a296b2011-04-08 10:57:54 -0400682 my $src = $gs->full_pushurl;
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700683
Deskin Millera0fbc872008-12-01 21:43:00 -0500684 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
Marc Branchaud62244062009-06-23 13:02:08 -0400685 my $allglobs = $remote->{ $_tag ? 'tags' : 'branches' };
686 my $glob;
687 if ($#{$allglobs} == 0) {
688 $glob = $allglobs->[0];
689 } else {
690 unless(defined $_branch_dest) {
691 die "Multiple ",
692 $_tag ? "tag" : "branch",
693 " paths defined for Subversion repository.\n",
694 "You must specify where you want to create the ",
695 $_tag ? "tag" : "branch",
696 " with the --destination argument.\n";
697 }
698 foreach my $g (@{$allglobs}) {
Eric Wongf7050592009-06-25 02:28:15 -0700699 # SVN::Git::Editor could probably be moved to Git.pm..
700 my $re = SVN::Git::Editor::glob2pat($g->{path}->{left});
701 if ($_branch_dest =~ /$re/) {
Marc Branchaud62244062009-06-23 13:02:08 -0400702 $glob = $g;
703 last;
704 }
705 }
706 unless (defined $glob) {
Eric Wongeaa14ff2009-07-25 01:36:06 -0700707 my $dest_re = qr/\b\Q$_branch_dest\E\b/;
708 foreach my $g (@{$allglobs}) {
709 $g->{path}->{left} =~ /$dest_re/ or next;
710 if (defined $glob) {
711 die "Ambiguous destination: ",
712 $_branch_dest, "\nmatches both '",
713 $glob->{path}->{left}, "' and '",
714 $g->{path}->{left}, "'\n";
715 }
716 $glob = $g;
717 }
718 unless (defined $glob) {
719 die "Unknown ",
720 $_tag ? "tag" : "branch",
721 " destination $_branch_dest\n";
722 }
Marc Branchaud62244062009-06-23 13:02:08 -0400723 }
724 }
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700725 my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
Igor Mironov99bacd62010-01-12 03:21:23 +1100726 my $url;
727 if (defined $_commit_url) {
728 $url = $_commit_url;
729 } else {
730 $url = eval { command_oneline('config', '--get',
731 "svn-remote.$gs->{repo_id}.commiturl") };
732 if (!$url) {
Alejandro R. Sedeño12a296b2011-04-08 10:57:54 -0400733 $url = $remote->{pushurl} || $remote->{url};
Igor Mironov99bacd62010-01-12 03:21:23 +1100734 }
735 }
736 my $dst = join '/', $url, $lft, $branch_name, ($rgt || ());
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700737
Igor Mironova83b91e2010-01-12 03:20:43 +1100738 if ($dst =~ /^https:/ && $src =~ /^http:/) {
739 $src=~s/^http:/https:/;
740 }
741
josh robbd32fad22010-02-24 16:13:50 +1300742 ::_req_svn();
743
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700744 my $ctx = SVN::Client->new(
745 auth => Git::SVN::Ra::_auth_providers(),
746 log_msg => sub {
747 ${ $_[0] } = defined $_message
748 ? $_message
749 : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
750 . $branch_name;
751 },
752 );
753
754 eval {
755 $ctx->ls($dst, 'HEAD', 0);
756 } and die "branch ${branch_name} already exists\n";
757
758 print "Copying ${src} at r${rev} to ${dst}...\n";
759 $ctx->copy($src, $rev, $dst)
760 unless $_dry_run;
761
762 $gs->fetch_all;
763}
764
Adam Roben26e60162007-04-27 11:57:53 -0700765sub cmd_find_rev {
Marc-Andre Lureauea14e6c2008-03-11 10:00:45 +0200766 my $revision_or_hash = shift or die "SVN or git revision required ",
767 "as a command-line argument\n";
Adam Roben26e60162007-04-27 11:57:53 -0700768 my $result;
769 if ($revision_or_hash =~ /^r\d+$/) {
Adam Robenb3cb7e42007-04-29 01:35:27 -0700770 my $head = shift;
771 $head ||= 'HEAD';
772 my @refs;
João Abecasis63c56022008-07-14 16:28:04 +0100773 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
Adam Robenb3cb7e42007-04-29 01:35:27 -0700774 unless ($gs) {
775 die "Unable to determine upstream SVN information from ",
776 "$head history\n";
Adam Roben26e60162007-04-27 11:57:53 -0700777 }
Adam Robenb3cb7e42007-04-29 01:35:27 -0700778 my $desired_revision = substr($revision_or_hash, 1);
João Abecasis63c56022008-07-14 16:28:04 +0100779 $result = $gs->rev_map_get($desired_revision, $uuid);
Adam Roben26e60162007-04-27 11:57:53 -0700780 } else {
781 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
782 $result = $rev;
783 }
784 print "$result\n" if $result;
785}
786
Eric Wong905f8b72007-02-16 03:22:40 -0800787sub cmd_rebase {
788 command_noisy(qw/update-index --refresh/);
Eric Wong13c823f2007-04-08 00:59:19 -0700789 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
790 unless ($gs) {
Eric Wong905f8b72007-02-16 03:22:40 -0800791 die "Unable to determine upstream SVN information from ",
792 "working tree history\n";
793 }
Seth Falcon7d45e142008-05-19 20:29:17 -0700794 if ($_dry_run) {
795 print "Remote Branch: " . $gs->refname . "\n";
796 print "SVN URL: " . $url . "\n";
797 return;
798 }
Eric Wong905f8b72007-02-16 03:22:40 -0800799 if (command(qw/diff-index HEAD --/)) {
800 print STDERR "Cannot rebase with uncommited changes:\n";
801 command_noisy('status');
802 exit 1;
803 }
Eric Wongdee41f32007-03-13 11:40:36 -0700804 unless ($_local) {
Steven Grimmcec0d5a2007-11-29 11:54:39 -0800805 # rebase will checkout for us, so no need to do it explicitly
806 $_no_checkout = 'true';
Eric Wongdee41f32007-03-13 11:40:36 -0700807 $_fetch_all ? $gs->fetch_all : $gs->fetch;
808 }
Eric Wong905f8b72007-02-16 03:22:40 -0800809 command_noisy(rebase_cmd(), $gs->refname);
Eric Wong6111b932009-11-15 18:57:16 -0800810 $gs->mkemptydirs;
Eric Wong905f8b72007-02-16 03:22:40 -0800811}
812
Eric Wong5969cbe2007-01-11 17:58:39 -0800813sub cmd_show_ignore {
Eric Wong13c823f2007-04-08 00:59:19 -0700814 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
815 $gs ||= Git::SVN->new;
Eric Wong5969cbe2007-01-11 17:58:39 -0800816 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200817 $gs->prop_walk($gs->{path}, $r, sub {
818 my ($gs, $path, $props) = @_;
819 print STDOUT "\n# $path\n";
820 my $s = $props->{'svn:ignore'} or return;
821 $s =~ s/[\r\n]+/\n/g;
Michael Haggertya7d72542009-08-07 21:21:21 +0200822 $s =~ s/^\n+//;
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200823 chomp $s;
824 $s =~ s#^#$path#gm;
825 print STDOUT "$s\n";
826 });
Eric Wonga5e0ced2006-06-12 15:23:48 -0700827}
828
Vineet Kumar2d879792007-11-19 14:56:15 -0800829sub cmd_show_externals {
830 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
831 $gs ||= Git::SVN->new;
832 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
833 $gs->prop_walk($gs->{path}, $r, sub {
834 my ($gs, $path, $props) = @_;
835 print STDOUT "\n# $path\n";
836 my $s = $props->{'svn:externals'} or return;
837 $s =~ s/[\r\n]+/\n/g;
838 chomp $s;
839 $s =~ s#^#$path#gm;
840 print STDOUT "$s\n";
841 });
842}
843
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200844sub cmd_create_ignore {
845 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
846 $gs ||= Git::SVN->new;
847 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
848 $gs->prop_walk($gs->{path}, $r, sub {
849 my ($gs, $path, $props) = @_;
850 # $path is of the form /path/to/dir/
Brian Gernhardt7d9fd452009-02-19 13:08:04 -0500851 $path = '.' . $path;
852 # SVN can have attributes on empty directories,
853 # which git won't track
854 mkpath([$path]) unless -d $path;
855 my $ignore = $path . '.gitignore';
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200856 my $s = $props->{'svn:ignore'} or return;
857 open(GITIGNORE, '>', $ignore)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200858 or fatal("Failed to open `$ignore' for writing: $!");
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200859 $s =~ s/[\r\n]+/\n/g;
Michael Haggertya7d72542009-08-07 21:21:21 +0200860 $s =~ s/^\n+//;
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200861 chomp $s;
862 # Prefix all patterns so that the ignore doesn't apply
863 # to sub-directories.
864 $s =~ s#^#/#gm;
865 print GITIGNORE "$s\n";
866 close(GITIGNORE)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200867 or fatal("Failed to close `$ignore': $!");
Gustaf Hendebyc4c66b22008-05-05 00:33:09 +0200868 command_noisy('add', '-f', $ignore);
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200869 });
870}
871
Eric Wong6111b932009-11-15 18:57:16 -0800872sub cmd_mkdirs {
873 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
874 $gs ||= Git::SVN->new;
875 $gs->mkemptydirs($_revision);
876}
877
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800878sub canonicalize_path {
879 my ($path) = @_;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800880 my $dot_slash_added = 0;
881 if (substr($path, 0, 1) ne "/") {
882 $path = "./" . $path;
883 $dot_slash_added = 1;
884 }
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800885 # File::Spec->canonpath doesn't collapse x/../y into y (for a
886 # good reason), so let's do this manually.
887 $path =~ s#/+#/#g;
888 $path =~ s#/\.(?:/|$)#/#g;
889 $path =~ s#/[^/]+/\.\.##g;
890 $path =~ s#/$##g;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800891 $path =~ s#^\./## if $dot_slash_added;
Gerrit Pape2fe403e2008-07-06 19:28:50 +0000892 $path =~ s#^/##;
893 $path =~ s#^\.$##;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800894 return $path;
895}
896
Ulrich Dangel50ff2362009-06-26 16:52:09 +0200897sub canonicalize_url {
898 my ($url) = @_;
899 $url =~ s#^([^:]+://[^/]*/)(.*)$#$1 . canonicalize_path($2)#e;
900 return $url;
901}
902
Benoit Sigoure15153452007-10-16 16:36:50 +0200903# get_svnprops(PATH)
904# ------------------
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200905# Helper for cmd_propget and cmd_proplist below.
Benoit Sigoure15153452007-10-16 16:36:50 +0200906sub get_svnprops {
907 my $path = shift;
908 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
909 $gs ||= Git::SVN->new;
910
911 # prefix THE PATH by the sub-directory from which the user
912 # invoked us.
913 $path = $cmd_dir_prefix . $path;
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200914 fatal("No such file or directory: $path") unless -e $path;
Benoit Sigoure15153452007-10-16 16:36:50 +0200915 my $is_dir = -d $path ? 1 : 0;
916 $path = $gs->{path} . '/' . $path;
917
918 # canonicalize the path (otherwise libsvn will abort or fail to
919 # find the file)
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800920 $path = canonicalize_path($path);
Benoit Sigoure15153452007-10-16 16:36:50 +0200921
922 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
923 my $props;
924 if ($is_dir) {
925 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
926 }
927 else {
928 (undef, $props) = $gs->ra->get_file($path, $r, undef);
929 }
930 return $props;
931}
932
933# cmd_propget (PROP, PATH)
934# ------------------------
935# Print the SVN property PROP for PATH.
936sub cmd_propget {
937 my ($prop, $path) = @_;
938 $path = '.' if not defined $path;
939 usage(1) if not defined $prop;
940 my $props = get_svnprops($path);
941 if (not defined $props->{$prop}) {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200942 fatal("`$path' does not have a `$prop' SVN property.");
Benoit Sigoure15153452007-10-16 16:36:50 +0200943 }
944 print $props->{$prop} . "\n";
945}
946
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200947# cmd_proplist (PATH)
948# -------------------
949# Print the list of SVN properties for PATH.
950sub cmd_proplist {
951 my $path = shift;
952 $path = '.' if not defined $path;
953 my $props = get_svnprops($path);
954 print "Properties on '$path':\n";
955 foreach (sort keys %{$props}) {
956 print " $_\n";
957 }
958}
959
Eric Wong8164b652007-01-11 15:35:55 -0800960sub cmd_multi_init {
Eric Wong9d55b412006-06-12 15:53:13 -0700961 my $url = shift;
Marc Branchaud62244062009-06-23 13:02:08 -0400962 unless (defined $_trunk || @_branches || @_tags) {
Eric Wong98327e52007-01-04 18:02:00 -0800963 usage(1);
964 }
Eric Wongdc431662007-05-19 03:59:02 -0700965
Eric Wong8164b652007-01-11 15:35:55 -0800966 $_prefix = '' unless defined $_prefix;
Eric Wongdadc6d22007-02-14 12:27:41 -0800967 if (defined $url) {
Ulrich Dangel50ff2362009-06-26 16:52:09 +0200968 $url = canonicalize_url($url);
Eric Wongdadc6d22007-02-14 12:27:41 -0800969 init_subdir(@_);
970 }
Eric Wongf30603f2007-02-23 01:26:26 -0800971 do_git_init_db();
Eric Wong98327e52007-01-04 18:02:00 -0800972 if (defined $_trunk) {
Jonathan Niederb4b33602010-06-13 06:27:43 -0500973 $_trunk =~ s#^/+##;
Adam Brewster6f5748e2009-08-11 23:14:27 -0400974 my $trunk_ref = 'refs/remotes/' . $_prefix . 'trunk';
Eric Wong706587f2007-01-18 17:50:01 -0800975 # try both old-style and new-style lookups:
976 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
Eric Wong8164b652007-01-11 15:35:55 -0800977 unless ($gs_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800978 my ($trunk_url, $trunk_path) =
979 complete_svn_url($url, $_trunk);
980 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
981 undef, $trunk_ref);
Eric Wong98327e52007-01-04 18:02:00 -0800982 }
Eric Wongc35b96e2006-10-11 11:53:21 -0700983 }
Marc Branchaud62244062009-06-23 13:02:08 -0400984 return unless @_branches || @_tags;
Eric Wonge7db67e2007-01-11 17:09:26 -0800985 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
Marc Branchaud62244062009-06-23 13:02:08 -0400986 foreach my $path (@_branches) {
987 complete_url_ls_init($ra, $path, '--branches/-b', $_prefix);
988 }
989 foreach my $path (@_tags) {
990 complete_url_ls_init($ra, $path, '--tags/-t', $_prefix.'tags/');
991 }
Eric Wong9d55b412006-06-12 15:53:13 -0700992}
993
Eric Wong1c8443b2007-01-14 02:17:00 -0800994sub cmd_multi_fetch {
Eric Wong4d0157d2009-11-22 12:37:06 -0800995 $Git::SVN::no_reuse_existing = undef;
Eric Wong0af9c9f2007-01-27 22:28:56 -0800996 my $remotes = Git::SVN::read_all_remotes();
997 foreach my $repo_id (sort keys %$remotes) {
Eric Wongdb03cd22007-02-13 00:38:02 -0800998 if ($remotes->{$repo_id}->{url}) {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800999 Git::SVN::fetch_all($repo_id, $remotes);
1000 }
Eric Wong706587f2007-01-18 17:50:01 -08001001 }
Eric Wong9d55b412006-06-12 15:53:13 -07001002}
1003
Eric Wong44320b92007-01-13 22:35:53 -08001004# this command is special because it requires no metadata
1005sub cmd_commit_diff {
1006 my ($ta, $tb, $url) = @_;
1007 my $usage = "Usage: $0 commit-diff -r<revision> ".
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001008 "<tree-ish> <tree-ish> [<URL>]";
Eric Wong44320b92007-01-13 22:35:53 -08001009 fatal($usage) if (!defined $ta || !defined $tb);
Karl Hasselströmd72ab8c2008-05-17 17:07:09 +02001010 my $svn_path = '';
Eric Wong44320b92007-01-13 22:35:53 -08001011 if (!defined $url) {
1012 my $gs = eval { Git::SVN->new };
1013 if (!$gs) {
1014 fatal("Needed URL or usable git-svn --id in ",
1015 "the command-line\n", $usage);
1016 }
1017 $url = $gs->{url};
Eric Wongd3a840d2007-01-26 01:32:45 -08001018 $svn_path = $gs->{path};
Eric Wong44320b92007-01-13 22:35:53 -08001019 }
1020 unless (defined $_revision) {
1021 fatal("-r|--revision is a required argument\n", $usage);
1022 }
1023 if (defined $_message && defined $_file) {
1024 fatal("Both --message/-m and --file/-F specified ",
1025 "for the commit message.\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001026 "I have no idea what you mean");
Eric Wong44320b92007-01-13 22:35:53 -08001027 }
1028 if (defined $_file) {
1029 $_message = file_to_s($_file);
1030 } else {
1031 $_message ||= get_commit_entry($tb)->{log};
1032 }
1033 my $ra ||= Git::SVN::Ra->new($url);
1034 my $r = $_revision;
1035 if ($r eq 'HEAD') {
1036 $r = $ra->get_latest_revnum;
1037 } elsif ($r !~ /^\d+$/) {
1038 die "revision argument: $r not understood by git-svn\n";
1039 }
Eric Wong61395352007-01-27 14:33:08 -08001040 my %ed_opts = ( r => $r,
1041 log => $_message,
1042 ra => $ra,
1043 tree_a => $ta,
1044 tree_b => $tb,
1045 editor_cb => sub { print "Committed r$_[0]\n" },
1046 svn_path => $svn_path );
1047 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong44320b92007-01-13 22:35:53 -08001048 print "No changes\n$ta == $tb\n";
1049 }
Eric Wong44320b92007-01-13 22:35:53 -08001050}
1051
Thomas Rast05427b92008-08-26 21:32:37 +02001052sub escape_uri_only {
1053 my ($uri) = @_;
1054 my @tmp;
1055 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -07001056 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Thomas Rast05427b92008-08-26 21:32:37 +02001057 push @tmp, $_;
1058 }
1059 join('/', @tmp);
1060}
1061
1062sub escape_url {
1063 my ($url) = @_;
1064 if ($url =~ m#^([^:]+)://([^/]*)(.*)$#) {
1065 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
1066 $url = "$scheme://$domain$uri";
1067 }
1068 $url;
1069}
1070
David D. Kilzere6fefa92007-11-21 11:57:18 -08001071sub cmd_info {
Eric Wongbd2d4f92008-08-05 00:35:16 -07001072 my $path = canonicalize_path(defined($_[0]) ? $_[0] : ".");
Thomas Rastedde9112008-08-26 21:32:36 +02001073 my $fullpath = canonicalize_path($cmd_dir_prefix . $path);
Eric Wongbd2d4f92008-08-05 00:35:16 -07001074 if (exists $_[1]) {
David D. Kilzere6fefa92007-11-21 11:57:18 -08001075 die "Too many arguments specified\n";
1076 }
1077
1078 my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
1079
1080 if (!$file_type && !$diff_status) {
Thomas Rast2cf3e3a2008-08-29 15:42:48 +02001081 print STDERR "svn: '$path' is not under version control\n";
1082 exit 1;
David D. Kilzere6fefa92007-11-21 11:57:18 -08001083 }
1084
1085 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1086 unless ($gs) {
1087 die "Unable to determine upstream SVN information from ",
1088 "working tree history\n";
1089 }
Eric Wongbd2d4f92008-08-05 00:35:16 -07001090
1091 # canonicalize_path() will return "" to make libsvn 1.5.x happy,
1092 $path = "." if $path eq "";
1093
Thomas Rastedde9112008-08-26 21:32:36 +02001094 my $full_url = $url . ($fullpath eq "" ? "" : "/$fullpath");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001095
David D. Kilzer8b014d72007-11-21 11:57:19 -08001096 if ($_url) {
Thomas Rast05427b92008-08-26 21:32:37 +02001097 print escape_url($full_url), "\n";
David D. Kilzer8b014d72007-11-21 11:57:19 -08001098 return;
1099 }
1100
David D. Kilzere6fefa92007-11-21 11:57:18 -08001101 my $result = "Path: $path\n";
1102 $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
Thomas Rast05427b92008-08-26 21:32:37 +02001103 $result .= "URL: " . escape_url($full_url) . "\n";
David D. Kilzere6fefa92007-11-21 11:57:18 -08001104
Eric Wonga5460eb2007-11-21 18:20:57 -08001105 eval {
1106 my $repos_root = $gs->repos_root;
1107 Git::SVN::remove_username($repos_root);
Thomas Rast05427b92008-08-26 21:32:37 +02001108 $result .= "Repository Root: " . escape_url($repos_root) . "\n";
Eric Wonga5460eb2007-11-21 18:20:57 -08001109 };
1110 if ($@) {
1111 $result .= "Repository Root: (offline)\n";
1112 }
Michael J Gruberb91a8a32010-03-03 21:34:31 +01001113 ::_req_svn();
Marcel Koeppen22ba47f2009-01-19 03:02:01 +01001114 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" &&
1115 ($SVN::Core::VERSION le '1.5.4' || $file_type ne "dir");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001116 $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
1117
1118 $result .= "Node Kind: " .
1119 ($file_type eq "dir" ? "directory" : "file") . "\n";
1120
1121 my $schedule = $diff_status eq "A"
1122 ? "add"
1123 : ($diff_status eq "D" ? "delete" : "normal");
1124 $result .= "Schedule: $schedule\n";
1125
1126 if ($diff_status eq "A") {
1127 print $result, "\n";
1128 return;
1129 }
1130
1131 my ($lc_author, $lc_rev, $lc_date_utc);
Thomas Rastedde9112008-08-26 21:32:36 +02001132 my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001133 my $log = command_output_pipe(@args);
1134 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
1135 while (<$log>) {
1136 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
1137 $lc_author = $1;
1138 $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
1139 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
1140 (undef, $lc_rev, undef) = ::extract_metadata($1);
1141 }
1142 }
1143 close $log;
1144
1145 Git::SVN::Log::set_local_timezone();
1146
1147 $result .= "Last Changed Author: $lc_author\n";
1148 $result .= "Last Changed Rev: $lc_rev\n";
1149 $result .= "Last Changed Date: " .
1150 Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
1151
1152 if ($file_type ne "dir") {
1153 my $text_last_updated_date =
1154 ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
1155 $result .=
1156 "Text Last Updated: " .
1157 Git::SVN::Log::format_svn_date($text_last_updated_date) .
1158 "\n";
1159 my $checksum;
1160 if ($diff_status eq "D") {
1161 my ($fh, $ctx) =
1162 command_output_pipe(qw(cat-file blob), "HEAD:$path");
1163 if ($file_type eq "link") {
1164 my $file_name = <$fh>;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001165 $checksum = md5sum("link $file_name");
David D. Kilzere6fefa92007-11-21 11:57:18 -08001166 } else {
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001167 $checksum = md5sum($fh);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001168 }
1169 command_close_pipe($fh, $ctx);
1170 } elsif ($file_type eq "link") {
1171 my $file_name =
1172 command(qw(cat-file blob), "HEAD:$path");
1173 $checksum =
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001174 md5sum("link " . $file_name);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001175 } else {
1176 open FILE, "<", $path or die $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001177 $checksum = md5sum(\*FILE);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001178 close FILE or die $!;
1179 }
1180 $result .= "Checksum: " . $checksum . "\n";
1181 }
1182
1183 print $result, "\n";
1184}
1185
Ben Jackson195643f2009-06-03 20:45:52 -07001186sub cmd_reset {
1187 my $target = shift || $_revision or die "SVN revision required\n";
1188 $target = $1 if $target =~ /^r(\d+)$/;
1189 $target =~ /^\d+$/ or die "Numeric SVN revision expected\n";
1190 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
1191 unless ($gs) {
1192 die "Unable to determine upstream SVN information from ".
1193 "history\n";
1194 }
1195 my ($r, $c) = $gs->find_rev_before($target, not $_fetch_parent);
Jonathan Nieder70ee0b72010-05-04 16:36:47 -07001196 die "Cannot find SVN revision $target\n" unless defined($c);
Ben Jackson195643f2009-06-03 20:45:52 -07001197 $gs->rev_map_set($r, $c, 'reset', $uuid);
1198 print "r$r = $c ($gs->{ref_id})\n";
1199}
1200
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -05001201sub cmd_gc {
1202 if (!$can_compress) {
1203 warn "Compress::Zlib could not be found; unhandled.log " .
1204 "files will not be compressed.\n";
1205 }
1206 find({ wanted => \&gc_directory, no_chdir => 1}, "$ENV{GIT_DIR}/svn");
1207}
1208
Eric Wong3397f9d2006-02-16 01:24:16 -08001209########################### utility functions #########################
1210
Eric Wong905f8b72007-02-16 03:22:40 -08001211sub rebase_cmd {
1212 my @cmd = qw/rebase/;
1213 push @cmd, '-v' if $_verbose;
1214 push @cmd, qw/--merge/ if $_merge;
1215 push @cmd, "--strategy=$_strategy" if $_strategy;
1216 @cmd;
1217}
1218
Eric Wong1e889ef2007-02-16 01:45:13 -08001219sub post_fetch_checkout {
1220 return if $_no_checkout;
1221 my $gs = $Git::SVN::_head or return;
1222 return if verify_ref('refs/heads/master^0');
1223
Eric Wongb186a262009-08-12 16:01:59 -07001224 # look for "trunk" ref if it exists
1225 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
1226 my $fetch = $remote->{fetch};
1227 if ($fetch) {
1228 foreach my $p (keys %$fetch) {
1229 basename($fetch->{$p}) eq 'trunk' or next;
1230 $gs = Git::SVN->new($fetch->{$p}, $gs->{repo_id}, $p);
1231 last;
1232 }
1233 }
1234
Eric Wong1e889ef2007-02-16 01:45:13 -08001235 my $valid_head = verify_ref('HEAD^0');
1236 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
1237 return if ($valid_head || !verify_ref('HEAD^0'));
1238
1239 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
1240 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
1241 return if -f $index;
1242
Matthias Lederhofer7ae3df82007-06-03 16:48:16 +02001243 return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
Eric Wong1e889ef2007-02-16 01:45:13 -08001244 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
1245 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
1246 print STDERR "Checked out HEAD:\n ",
1247 $gs->full_url, " r", $gs->last_rev, "\n";
Eric Wong6111b932009-11-15 18:57:16 -08001248 $gs->mkemptydirs($gs->last_rev);
Eric Wong1e889ef2007-02-16 01:45:13 -08001249}
1250
Eric Wong98327e52007-01-04 18:02:00 -08001251sub complete_svn_url {
1252 my ($url, $path) = @_;
1253 $path =~ s#/+$##;
Eric Wong98327e52007-01-04 18:02:00 -08001254 if ($path !~ m#^[a-z\+]+://#) {
Eric Wong98327e52007-01-04 18:02:00 -08001255 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
1256 fatal("E: '$path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001257 "and a separate URL is not specified");
Eric Wong98327e52007-01-04 18:02:00 -08001258 }
Eric Wong706587f2007-01-18 17:50:01 -08001259 return ($url, $path);
Eric Wong98327e52007-01-04 18:02:00 -08001260 }
Eric Wong706587f2007-01-18 17:50:01 -08001261 return ($path, '');
Eric Wong98327e52007-01-04 18:02:00 -08001262}
1263
Eric Wong9d55b412006-06-12 15:53:13 -07001264sub complete_url_ls_init {
Eric Wong706587f2007-01-18 17:50:01 -08001265 my ($ra, $repo_path, $switch, $pfx) = @_;
1266 unless ($repo_path) {
Eric Wong9d55b412006-06-12 15:53:13 -07001267 print STDERR "W: $switch not specified\n";
1268 return;
1269 }
Eric Wong706587f2007-01-18 17:50:01 -08001270 $repo_path =~ s#/+$##;
1271 if ($repo_path =~ m#^[a-z\+]+://#) {
1272 $ra = Git::SVN::Ra->new($repo_path);
1273 $repo_path = '';
Eric Wonge7db67e2007-01-11 17:09:26 -08001274 } else {
Eric Wong706587f2007-01-18 17:50:01 -08001275 $repo_path =~ s#^/+##;
Eric Wonge7db67e2007-01-11 17:09:26 -08001276 unless ($ra) {
Eric Wong706587f2007-01-18 17:50:01 -08001277 fatal("E: '$repo_path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001278 "and a separate URL is not specified");
Eric Wong9d55b412006-06-12 15:53:13 -07001279 }
Eric Wonge7db67e2007-01-11 17:09:26 -08001280 }
Eric Wong706587f2007-01-18 17:50:01 -08001281 my $url = $ra->{url};
Eric Wongb4d57e52007-02-14 15:10:44 -08001282 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
1283 my $k = "svn-remote.$gs->{repo_id}.url";
1284 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
1285 if ($orig_url && ($orig_url ne $gs->{url})) {
1286 die "$k already set: $orig_url\n",
1287 "wanted to set to: $gs->{url}\n";
Eric Wong88cf4102007-02-01 03:59:07 -08001288 }
Eric Wongb4d57e52007-02-14 15:10:44 -08001289 command_oneline('config', $k, $gs->{url}) unless $orig_url;
Mattias Nissler0b2af452009-07-07 01:40:02 +02001290 my $remote_path = "$gs->{path}/$repo_path";
Eric Wong5268f9e2009-08-16 14:22:12 -07001291 $remote_path =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
Eric Wongb4d57e52007-02-14 15:10:44 -08001292 $remote_path =~ s#/+#/#g;
1293 $remote_path =~ s#^/##g;
Eric Wonged0b9d42008-03-14 11:01:23 -07001294 $remote_path .= "/*" if $remote_path !~ /\*/;
Eric Wongb4d57e52007-02-14 15:10:44 -08001295 my ($n) = ($switch =~ /^--(\w+)/);
1296 if (length $pfx && $pfx !~ m#/$#) {
1297 die "--prefix='$pfx' must have a trailing slash '/'\n";
Eric Wong9d55b412006-06-12 15:53:13 -07001298 }
Marcus Griep570d35c2008-08-08 01:41:57 -07001299 command_noisy('config',
Marc Branchaud62244062009-06-23 13:02:08 -04001300 '--add',
Marcus Griep570d35c2008-08-08 01:41:57 -07001301 "svn-remote.$gs->{repo_id}.$n",
1302 "$remote_path:refs/remotes/$pfx*" .
1303 ('/*' x (($remote_path =~ tr/*/*/) - 1)) );
Eric Wong9d55b412006-06-12 15:53:13 -07001304}
1305
Eric Wongaef4e922006-12-15 10:59:54 -08001306sub verify_ref {
1307 my ($ref) = @_;
Eric Wong2c5c1d52006-12-28 01:16:21 -08001308 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
1309 { STDERR => 0 }); };
Eric Wongaef4e922006-12-15 10:59:54 -08001310}
1311
Eric Wonga5e0ced2006-06-12 15:23:48 -07001312sub get_tree_from_treeish {
Eric Wongcf52b8f2006-02-20 10:57:28 -08001313 my ($treeish) = @_;
Eric Wong44320b92007-01-13 22:35:53 -08001314 # $treeish can be a symbolic ref, too:
Eric Wongaef4e922006-12-15 10:59:54 -08001315 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001316 my $expected;
1317 while ($type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001318 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001319 }
1320 if ($type eq 'commit') {
Eric Wongaef4e922006-12-15 10:59:54 -08001321 $expected = (grep /^tree /, command(qw/cat-file commit/,
1322 $treeish))[0];
Eric Wong44320b92007-01-13 22:35:53 -08001323 ($expected) = ($expected =~ /^tree ($sha1)$/o);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001324 die "Unable to get tree from $treeish\n" unless $expected;
1325 } elsif ($type eq 'tree') {
1326 $expected = $treeish;
1327 } else {
1328 die "$treeish is a $type, expected tree, tag or commit\n";
1329 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07001330 return $expected;
1331}
Eric Wongcf52b8f2006-02-20 10:57:28 -08001332
Eric Wong44320b92007-01-13 22:35:53 -08001333sub get_commit_entry {
1334 my ($treeish) = shift;
1335 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
1336 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1337 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1338 open my $log_fh, '>', $commit_editmsg or croak $!;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001339
Eric Wong44320b92007-01-13 22:35:53 -08001340 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wong4ad45152006-07-09 20:20:48 -07001341 if ($type eq 'commit' || $type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001342 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
Eric Wong44320b92007-01-13 22:35:53 -08001343 $type, $treeish);
Eric Wong3397f9d2006-02-16 01:24:16 -08001344 my $in_msg = 0;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001345 my $author;
1346 my $saw_from = 0;
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001347 my $msgbuf = "";
Eric Wong3397f9d2006-02-16 01:24:16 -08001348 while (<$msg_fh>) {
1349 if (!$in_msg) {
1350 $in_msg = 1 if (/^\s*$/);
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001351 $author = $1 if (/^author (.*>)/);
Eric Wongdf746c52006-03-03 01:20:08 -08001352 } elsif (/^git-svn-id: /) {
Eric Wong44320b92007-01-13 22:35:53 -08001353 # skip this for now, we regenerate the
1354 # correct one on re-fetch anyways
1355 # TODO: set *:merge properties or like...
Eric Wong3397f9d2006-02-16 01:24:16 -08001356 } else {
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001357 if (/^From:/ || /^Signed-off-by:/) {
1358 $saw_from = 1;
1359 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001360 $msgbuf .= $_;
Eric Wong3397f9d2006-02-16 01:24:16 -08001361 }
1362 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001363 $msgbuf =~ s/\s+$//s;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001364 if ($Git::SVN::_add_author_from && defined($author)
1365 && !$saw_from) {
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001366 $msgbuf .= "\n\nFrom: $author";
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001367 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001368 print $log_fh $msgbuf or croak $!;
Eric Wongaef4e922006-12-15 10:59:54 -08001369 command_close_pipe($msg_fh, $ctx);
Eric Wong3397f9d2006-02-16 01:24:16 -08001370 }
Eric Wong44320b92007-01-13 22:35:53 -08001371 close $log_fh or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -08001372
1373 if ($_edit || ($type eq 'tree')) {
Jonathan Niederb4479f02009-10-30 20:42:34 -05001374 chomp(my $editor = command_oneline(qw(var GIT_EDITOR)));
1375 system('sh', '-c', $editor.' "$@"', $editor, $commit_editmsg);
Eric Wong3397f9d2006-02-16 01:24:16 -08001376 }
Eric Wong44320b92007-01-13 22:35:53 -08001377 rename $commit_editmsg, $commit_msg or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07001378 {
Eric Wongb510df82009-05-28 00:56:23 -07001379 require Encode;
Eric Wong16fc08e2008-10-29 23:49:26 -07001380 # SVN requires messages to be UTF-8 when entering the repo
1381 local $/;
1382 open $log_fh, '<', $commit_msg or croak $!;
1383 binmode $log_fh;
1384 chomp($log_entry{log} = <$log_fh>);
1385
Eric Wongb510df82009-05-28 00:56:23 -07001386 my $enc = Git::config('i18n.commitencoding') || 'UTF-8';
1387 my $msg = $log_entry{log};
1388
1389 eval { $msg = Encode::decode($enc, $msg, 1) };
1390 if ($@) {
1391 die "Could not decode as $enc:\n", $msg,
1392 "\nPerhaps you need to set i18n.commitencoding\n";
Eric Wong16fc08e2008-10-29 23:49:26 -07001393 }
Eric Wongb510df82009-05-28 00:56:23 -07001394
1395 eval { $msg = Encode::encode('UTF-8', $msg, 1) };
1396 die "Could not encode as UTF-8:\n$msg\n" if $@;
1397
1398 $log_entry{log} = $msg;
1399
Eric Wong16fc08e2008-10-29 23:49:26 -07001400 close $log_fh or croak $!;
1401 }
Eric Wong44320b92007-01-13 22:35:53 -08001402 unlink $commit_msg;
1403 \%log_entry;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001404}
1405
Eric Wong3397f9d2006-02-16 01:24:16 -08001406sub s_to_file {
1407 my ($str, $file, $mode) = @_;
1408 open my $fd,'>',$file or croak $!;
1409 print $fd $str,"\n" or croak $!;
1410 close $fd or croak $!;
1411 chmod ($mode &~ umask, $file) if (defined $mode);
1412}
1413
1414sub file_to_s {
1415 my $file = shift;
1416 open my $fd,'<',$file or croak "$!: file: $file\n";
1417 local $/;
1418 my $ret = <$fd>;
1419 close $fd or croak $!;
1420 $ret =~ s/\s*$//s;
1421 return $ret;
1422}
1423
Eric Wongeeb0abe2006-03-03 01:20:08 -08001424# '<svn username> = real-name <email address>' mapping based on git-svnimport:
1425sub load_authors {
1426 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001427 my $log = $cmd eq 'log';
Eric Wongeeb0abe2006-03-03 01:20:08 -08001428 while (<$authors>) {
1429 chomp;
Richard MUSIL575d0252007-07-17 19:02:57 +02001430 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
Eric Wongeeb0abe2006-03-03 01:20:08 -08001431 my ($user, $name, $email) = ($1, $2, $3);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001432 if ($log) {
1433 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
1434 } else {
1435 $users{$user} = [$name, $email];
1436 }
Eric Wong79bb8d82006-06-01 02:35:44 -07001437 }
1438 close $authors or croak $!;
1439}
1440
Tom Princee0d10e12007-01-28 16:16:53 -08001441# convert GetOpt::Long specs for use by git-config
Eric Wong1a305822009-11-14 14:25:11 -08001442sub read_git_config {
Eric Wongb8c92ca2006-05-24 01:40:37 -07001443 my $opts = shift;
Eric Wong97ae0912007-02-11 15:21:24 -08001444 my @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001445 foreach my $o (keys %$opts) {
Eric Wong97ae0912007-02-11 15:21:24 -08001446 # if we have mixedCase and a long option-only, then
1447 # it's a config-only variable that we don't need for
1448 # the command-line.
1449 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001450 my $v = $opts->{$o};
Eric Wong97ae0912007-02-11 15:21:24 -08001451 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001452 $key =~ s/-//g;
Deskin Miller225f1d02008-10-23 15:21:34 -04001453 my $arg = 'git config';
Eric Wongb8c92ca2006-05-24 01:40:37 -07001454 $arg .= ' --int' if ($o =~ /[:=]i$/);
1455 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1456 if (ref $v eq 'ARRAY') {
1457 chomp(my @tmp = `$arg --get-all svn.$key`);
1458 @$v = @tmp if @tmp;
1459 } else {
1460 chomp(my $tmp = `$arg --get svn.$key`);
Eric Wong77742842007-02-10 21:07:12 -08001461 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
Eric Wongb8c92ca2006-05-24 01:40:37 -07001462 $$v = $tmp;
1463 }
1464 }
1465 }
Eric Wong97ae0912007-02-11 15:21:24 -08001466 delete @$opts{@config_only} if @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001467}
1468
Eric Wong79bb8d82006-06-01 02:35:44 -07001469sub extract_metadata {
Eric Wongc1927a82006-06-27 19:39:11 -07001470 my $id = shift or return (undef, undef, undef);
Sam Vilain3dfab992007-06-30 20:56:13 +12001471 my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
Eric Wongb3e95932009-07-11 14:13:12 -07001472 \s([a-f\d\-]+)$/ix);
Eric Wonge70dc782006-11-23 14:54:04 -08001473 if (!defined $rev || !$uuid || !$url) {
Eric Wong79bb8d82006-06-01 02:35:44 -07001474 # some of the original repositories I made had
Pavel Roskin82e5a822006-07-10 01:50:18 -04001475 # identifiers like this:
Eric Wongb3e95932009-07-11 14:13:12 -07001476 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/i);
Eric Wong79bb8d82006-06-01 02:35:44 -07001477 }
1478 return ($url, $rev, $uuid);
1479}
1480
Eric Wongc1927a82006-06-27 19:39:11 -07001481sub cmt_metadata {
1482 return extract_metadata((grep(/^git-svn-id: /,
Eric Wongaef4e922006-12-15 10:59:54 -08001483 command(qw/cat-file commit/, shift)))[-1]);
Eric Wongc1927a82006-06-27 19:39:11 -07001484}
1485
Boris Byk6ea42032009-04-11 00:32:41 +04001486sub cmt_sha2rev_batch {
1487 my %s2r;
1488 my ($pid, $in, $out, $ctx) = command_bidi_pipe(qw/cat-file --batch/);
1489 my $list = shift;
1490
1491 foreach my $sha (@{$list}) {
1492 my $first = 1;
1493 my $size = 0;
1494 print $out $sha, "\n";
1495
1496 while (my $line = <$in>) {
1497 if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) {
1498 last;
1499 } elsif ($first &&
1500 $line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) {
1501 $first = 0;
1502 $size = $1;
1503 next;
1504 } elsif ($line =~ /^(git-svn-id: )/) {
1505 my (undef, $rev, undef) =
1506 extract_metadata($line);
1507 $s2r{$sha} = $rev;
1508 }
1509
1510 $size -= length($line);
1511 last if ($size == 0);
1512 }
1513 }
1514
1515 command_close_bidi_pipe($pid, $in, $out, $ctx);
1516
1517 return \%s2r;
1518}
1519
Eric Wong905f8b72007-02-16 03:22:40 -08001520sub working_head_info {
1521 my ($head, $refs) = @_;
Mathias Lafeldt8565a562010-09-24 00:05:03 +02001522 my @args = qw/log --no-color --no-decorate --first-parent
1523 --pretty=medium/;
Lars Hjemli05b4df32007-09-05 11:35:29 +02001524 my ($fh, $ctx) = command_output_pipe(@args, $head);
Sam Vilain3dfab992007-06-30 20:56:13 +12001525 my $hash;
Sam Vilain40cb8f82007-06-30 20:56:14 +12001526 my %max;
Sam Vilain3dfab992007-06-30 20:56:13 +12001527 while (<$fh>) {
1528 if ( m{^commit ($::sha1)$} ) {
1529 unshift @$refs, $hash if $hash and $refs;
1530 $hash = $1;
1531 next;
1532 }
1533 next unless s{^\s*(git-svn-id:)}{$1};
1534 my ($url, $rev, $uuid) = extract_metadata($_);
Eric Wong13c823f2007-04-08 00:59:19 -07001535 if (defined $url && defined $rev) {
Sam Vilain40cb8f82007-06-30 20:56:14 +12001536 next if $max{$url} and $max{$url} < $rev;
Eric Wong13c823f2007-04-08 00:59:19 -07001537 if (my $gs = Git::SVN->find_by_url($url)) {
João Abecasis63c56022008-07-14 16:28:04 +01001538 my $c = $gs->rev_map_get($rev, $uuid);
Adam Robenb03c7a62007-04-25 11:50:32 -07001539 if ($c && $c eq $hash) {
Eric Wong13c823f2007-04-08 00:59:19 -07001540 close $fh; # break the pipe
1541 return ($url, $rev, $uuid, $gs);
Sam Vilain40cb8f82007-06-30 20:56:14 +12001542 } else {
Eric Wong060610c2007-12-08 23:27:41 -08001543 $max{$url} ||= $gs->rev_map_max;
Eric Wong13c823f2007-04-08 00:59:19 -07001544 }
1545 }
1546 }
Eric Wong905f8b72007-02-16 03:22:40 -08001547 }
Eric Wong13c823f2007-04-08 00:59:19 -07001548 command_close_pipe($fh, $ctx);
1549 (undef, undef, undef, undef);
Eric Wong905f8b72007-02-16 03:22:40 -08001550}
1551
Eric Wong733a65a2007-06-13 02:23:28 -07001552sub read_commit_parents {
1553 my ($parents, $c) = @_;
Eric Wong7b02b852007-09-08 16:33:08 -07001554 chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
1555 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1556 @{$parents->{$c}} = split(/ /, $p);
Eric Wong733a65a2007-06-13 02:23:28 -07001557}
1558
1559sub linearize_history {
1560 my ($gs, $refs) = @_;
1561 my %parents;
1562 foreach my $c (@$refs) {
1563 read_commit_parents(\%parents, $c);
1564 }
1565
1566 my @linear_refs;
1567 my %skip = ();
1568 my $last_svn_commit = $gs->last_commit;
1569 foreach my $c (reverse @$refs) {
1570 next if $c eq $last_svn_commit;
1571 last if $skip{$c};
1572
1573 unshift @linear_refs, $c;
1574 $skip{$c} = 1;
1575
1576 # we only want the first parent to diff against for linear
1577 # history, we save the rest to inject when we finalize the
1578 # svn commit
1579 my $fp_a = verify_ref("$c~1");
1580 my $fp_b = shift @{$parents{$c}} if $parents{$c};
1581 if (!$fp_a || !$fp_b) {
1582 die "Commit $c\n",
1583 "has no parent commit, and therefore ",
1584 "nothing to diff against.\n",
1585 "You should be working from a repository ",
1586 "originally created by git-svn\n";
1587 }
1588 if ($fp_a ne $fp_b) {
1589 die "$c~1 = $fp_a, however parsing commit $c ",
1590 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1591 }
1592
1593 foreach my $p (@{$parents{$c}}) {
1594 $skip{$p} = 1;
1595 }
1596 }
1597 (\@linear_refs, \%parents);
1598}
1599
David D. Kilzere6fefa92007-11-21 11:57:18 -08001600sub find_file_type_and_diff_status {
1601 my ($path) = @_;
Dmitry Potapov107cee52008-07-21 00:14:07 +04001602 return ('dir', '') if $path eq '';
David D. Kilzere6fefa92007-11-21 11:57:18 -08001603
1604 my $diff_output =
1605 command_oneline(qw(diff --cached --name-status --), $path) || "";
1606 my $diff_status = (split(' ', $diff_output))[0] || "";
1607
1608 my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || "";
1609
1610 return (undef, undef) if !$diff_status && !$ls_tree;
1611
1612 if ($diff_status eq "A") {
1613 return ("link", $diff_status) if -l $path;
1614 return ("dir", $diff_status) if -d $path;
1615 return ("file", $diff_status);
1616 }
1617
1618 my $mode = (split(' ', $ls_tree))[0] || "";
1619
1620 return ("link", $diff_status) if $mode eq "120000";
1621 return ("dir", $diff_status) if $mode eq "040000";
1622 return ("file", $diff_status);
1623}
1624
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001625sub md5sum {
1626 my $arg = shift;
1627 my $ref = ref $arg;
1628 my $md5 = Digest::MD5->new();
Marcus Griep0b191382008-08-12 12:00:53 -04001629 if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001630 $md5->addfile($arg) or croak $!;
1631 } elsif ($ref eq 'SCALAR') {
1632 $md5->add($$arg) or croak $!;
1633 } elsif (!$ref) {
1634 $md5->add($arg) or croak $!;
1635 } else {
1636 ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1637 }
1638 return $md5->hexdigest();
1639}
1640
Robert Allan Zeh2da9ee02009-07-19 18:00:52 -05001641sub gc_directory {
1642 if ($can_compress && -f $_ && basename($_) eq "unhandled.log") {
1643 my $out_filename = $_ . ".gz";
1644 open my $in_fh, "<", $_ or die "Unable to open $_: $!\n";
1645 binmode $in_fh;
1646 my $gz = Compress::Zlib::gzopen($out_filename, "ab") or
1647 die "Unable to open $out_filename: $!\n";
1648
1649 my $res;
1650 while ($res = sysread($in_fh, my $str, 1024)) {
1651 $gz->gzwrite($str) or
1652 die "Unable to write: ".$gz->gzerror()."!\n";
1653 }
1654 unlink $_ or die "unlink $File::Find::name: $!\n";
1655 } elsif (-f $_ && basename($_) eq "index") {
1656 unlink $_ or die "unlink $_: $!\n";
1657 }
1658}
1659
Eric Wong9b981fc2007-01-11 12:14:21 -08001660package Git::SVN;
1661use strict;
1662use warnings;
Eric Wong060610c2007-12-08 23:27:41 -08001663use Fcntl qw/:DEFAULT :seek/;
1664use constant rev_map_fmt => 'NH40';
Eric Wongecc712d2007-01-31 12:28:10 -08001665use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
Eric Wong62e349d2007-02-16 19:57:29 -08001666 $_repack $_repack_flags $_use_svm_props $_head
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00001667 $_use_svnsync_props $no_reuse_existing $_minimize_url
Pete Harlane82f0d72009-01-17 20:10:14 -08001668 $_use_log_author $_add_author_from $_localtime/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001669use Carp qw/croak/;
1670use File::Path qw/mkpath/;
Eric Wong373274f2007-01-31 13:54:23 -08001671use File::Copy qw/copy/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001672use IPC::Open3;
Sam Vilain7d944c32009-12-20 00:55:13 +13001673use Memoize; # core since 5.8.0, Jul 2002
Andrew Myrick8bff7c52010-01-30 03:14:22 +00001674use Memoize::Storable;
Eric Wong9b981fc2007-01-11 12:14:21 -08001675
Karl Hasselström94bc9142008-02-03 17:56:18 +01001676my ($_gc_nr, $_gc_period);
1677
Eric Wong9b981fc2007-01-11 12:14:21 -08001678# properties that we do not log:
1679my %SKIP_PROP;
1680BEGIN {
1681 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
1682 svn:special svn:executable
1683 svn:entry:committed-rev
1684 svn:entry:last-author
1685 svn:entry:uuid
1686 svn:entry:committed-date/;
Eric Wong91b03282007-02-11 00:51:33 -08001687
1688 # some options are read globally, but can be overridden locally
1689 # per [svn-remote "..."] section. Command-line options will *NOT*
1690 # override options set in an [svn-remote "..."] section
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001691 no strict 'refs';
1692 for my $option (qw/follow_parent no_metadata use_svm_props
1693 use_svnsync_props/) {
1694 my $key = $option;
Eric Wong91b03282007-02-11 00:51:33 -08001695 $key =~ tr/_//d;
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001696 my $prop = "-$option";
1697 *$option = sub {
1698 my ($self) = @_;
1699 return $self->{$prop} if exists $self->{$prop};
1700 my $k = "svn-remote.$self->{repo_id}.$key";
1701 eval { command_oneline(qw/config --get/, $k) };
1702 if ($@) {
1703 $self->{$prop} = ${"Git::SVN::_$option"};
Eric Wong91b03282007-02-11 00:51:33 -08001704 } else {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001705 my $v = command_oneline(qw/config --bool/,$k);
1706 $self->{$prop} = $v eq 'false' ? 0 : 1;
Eric Wong91b03282007-02-11 00:51:33 -08001707 }
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001708 return $self->{$prop};
1709 }
Eric Wong91b03282007-02-11 00:51:33 -08001710 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001711}
1712
Marcus Griep0b191382008-08-12 12:00:53 -04001713
Eric Wong321b1842008-01-02 10:10:03 -08001714my (%LOCKFILES, %INDEX_FILES);
1715END {
1716 unlink keys %LOCKFILES if %LOCKFILES;
1717 unlink keys %INDEX_FILES if %INDEX_FILES;
1718}
Eric Wong373274f2007-01-31 13:54:23 -08001719
Eric Wong4bb9ed02007-02-03 13:29:17 -08001720sub resolve_local_globs {
1721 my ($url, $fetch, $glob_spec) = @_;
1722 return unless defined $glob_spec;
1723 my $ref = $glob_spec->{ref};
1724 my $path = $glob_spec->{path};
Adam Brewster6f5748e2009-08-11 23:14:27 -04001725 foreach (command(qw#for-each-ref --format=%(refname) refs/#)) {
1726 next unless m#^$ref->{regex}$#;
Eric Wong4bb9ed02007-02-03 13:29:17 -08001727 my $p = $1;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001728 my $pathname = desanitize_refname($path->full_path($p));
1729 my $refname = desanitize_refname($ref->full_path($p));
Eric Wong4bb9ed02007-02-03 13:29:17 -08001730 if (my $existing = $fetch->{$pathname}) {
1731 if ($existing ne $refname) {
1732 die "Refspec conflict:\n",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001733 "existing: $existing\n",
1734 " globbed: $refname\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08001735 }
Adam Brewster6f5748e2009-08-11 23:14:27 -04001736 my $u = (::cmt_metadata("$refname"))[0];
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001737 $u =~ s!^\Q$url\E(/|$)!! or die
Adam Brewster6f5748e2009-08-11 23:14:27 -04001738 "$refname: '$url' not found in '$u'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08001739 if ($pathname ne $u) {
1740 warn "W: Refspec glob conflict ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001741 "(ref: $refname):\n",
Eric Wong4bb9ed02007-02-03 13:29:17 -08001742 "expected path: $pathname\n",
1743 " real path: $u\n",
1744 "Continuing ahead with $u\n";
1745 next;
1746 }
1747 } else {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001748 $fetch->{$pathname} = $refname;
1749 }
1750 }
1751}
1752
Eric Wonge98671e2007-02-14 02:21:19 -08001753sub parse_revision_argument {
1754 my ($base, $head) = @_;
1755 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
1756 return ($base, $head);
1757 }
1758 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
1759 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
1760 return ($head, $head) if ($::_revision eq 'HEAD');
1761 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
1762 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
1763 die "revision argument: $::_revision not understood by git-svn\n";
1764}
1765
Eric Wong0af9c9f2007-01-27 22:28:56 -08001766sub fetch_all {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001767 my ($repo_id, $remotes) = @_;
Eric Wong905f8b72007-02-16 03:22:40 -08001768 if (ref $repo_id) {
1769 my $gs = $repo_id;
1770 $repo_id = undef;
1771 $repo_id = $gs->{repo_id};
1772 }
1773 $remotes ||= read_all_remotes();
Eric Wong7447b4b2007-02-14 18:38:46 -08001774 my $remote = $remotes->{$repo_id} or
1775 die "[svn-remote \"$repo_id\"] unknown\n";
Eric Wonge5181922007-02-08 12:53:57 -08001776 my $fetch = $remote->{fetch};
Eric Wong7447b4b2007-02-14 18:38:46 -08001777 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
Eric Wonge5181922007-02-08 12:53:57 -08001778 my (@gs, @globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001779 my $ra = Git::SVN::Ra->new($url);
Eric Wong26a62d52007-02-12 13:25:25 -08001780 my $uuid = $ra->get_uuid;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001781 my $head = $ra->get_latest_revnum;
Eric Wong577e9fc2009-12-21 02:06:04 -08001782
1783 # ignore errors, $head revision may not even exist anymore
1784 eval { $ra->get_log("", $head, 0, 1, 0, 1, sub { $head = $_[1] }) };
1785 warn "W: $@\n" if $@;
1786
Eric Wong28710f72007-02-14 13:32:21 -08001787 my $base = defined $fetch ? $head : 0;
Eric Wonge5181922007-02-08 12:53:57 -08001788
1789 # read the max revs for wildcard expansion (branches/*, tags/*)
1790 foreach my $t (qw/branches tags/) {
1791 defined $remote->{$t} or next;
Marc Branchaud62244062009-06-23 13:02:08 -04001792 push @globs, @{$remote->{$t}};
1793
Eric Wong93f26892007-02-11 01:20:26 -08001794 my $max_rev = eval { tmp_config(qw/--int --get/,
1795 "svn-remote.$repo_id.${t}-maxRev") };
1796 if (defined $max_rev && ($max_rev < $base)) {
1797 $base = $max_rev;
Eric Wongd6d33462007-02-16 04:05:33 -08001798 } elsif (!defined $max_rev) {
1799 $base = 0;
Eric Wonge5181922007-02-08 12:53:57 -08001800 }
1801 }
1802
Eric Wongdb03cd22007-02-13 00:38:02 -08001803 if ($fetch) {
1804 foreach my $p (sort keys %$fetch) {
1805 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
Eric Wong060610c2007-12-08 23:27:41 -08001806 my $lr = $gs->rev_map_max;
Eric Wongdb03cd22007-02-13 00:38:02 -08001807 if (defined $lr) {
1808 $base = $lr if ($lr < $base);
1809 }
1810 push @gs, $gs;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001811 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08001812 }
Eric Wonge98671e2007-02-14 02:21:19 -08001813
1814 ($base, $head) = parse_revision_argument($base, $head);
Eric Wonge5181922007-02-08 12:53:57 -08001815 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001816}
1817
Eric Wong47e39c52007-01-21 04:27:09 -08001818sub read_all_remotes {
1819 my $r = {};
João Abecasis63c56022008-07-14 16:28:04 +01001820 my $use_svm_props = eval { command_oneline(qw/config --bool
1821 svn.useSvmProps/) };
1822 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
Eric Wongffd5c8e2009-10-22 23:39:04 -07001823 my $svn_refspec = qr{\s*(.*?)\s*:\s*(.+?)\s*};
Eric Wong8b8fc062007-01-22 11:44:57 -08001824 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
Adam Brewster6f5748e2009-08-11 23:14:27 -04001825 if (m!^(.+)\.fetch=$svn_refspec$!) {
1826 my ($remote, $local_ref, $remote_ref) = ($1, $2, $3);
1827 die("svn-remote.$remote: remote ref '$remote_ref' "
1828 . "must start with 'refs/'\n")
1829 unless $remote_ref =~ m{^refs/};
Steven Walter46cb16f2010-08-03 19:21:25 -04001830 $local_ref = uri_decode($local_ref);
Eric Wong46cf98b2007-07-14 12:40:32 -07001831 $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
João Abecasis63c56022008-07-14 16:28:04 +01001832 $r->{$remote}->{svm} = {} if $use_svm_props;
1833 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
1834 $r->{$1}->{svm} = {};
Eric Wong47e39c52007-01-21 04:27:09 -08001835 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1836 $r->{$1}->{url} = $2;
Alejandro R. Sedeño12a296b2011-04-08 10:57:54 -04001837 } elsif (m!^(.+)\.pushurl=\s*(.*)\s*$!) {
1838 $r->{$1}->{pushurl} = $2;
Adam Brewster6f5748e2009-08-11 23:14:27 -04001839 } elsif (m!^(.+)\.(branches|tags)=$svn_refspec$!) {
1840 my ($remote, $t, $local_ref, $remote_ref) =
1841 ($1, $2, $3, $4);
1842 die("svn-remote.$remote: remote ref '$remote_ref' ($t) "
1843 . "must start with 'refs/'\n")
1844 unless $remote_ref =~ m{^refs/};
Steven Walter46cb16f2010-08-03 19:21:25 -04001845 $local_ref = uri_decode($local_ref);
Marc Branchaud62244062009-06-23 13:02:08 -04001846 my $rs = {
Adam Brewster6f5748e2009-08-11 23:14:27 -04001847 t => $t,
1848 remote => $remote,
Jay Soffian07576202010-01-23 03:30:01 -05001849 path => Git::SVN::GlobSpec->new($local_ref, 1),
1850 ref => Git::SVN::GlobSpec->new($remote_ref, 0) };
Eric Wong4bb9ed02007-02-03 13:29:17 -08001851 if (length($rs->{ref}->{right}) != 0) {
1852 die "The '*' glob character must be the last ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001853 "character of '$remote_ref'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08001854 }
Adam Brewster6f5748e2009-08-11 23:14:27 -04001855 push @{ $r->{$remote}->{$t} }, $rs;
Eric Wong47e39c52007-01-21 04:27:09 -08001856 }
1857 }
João Abecasis63c56022008-07-14 16:28:04 +01001858
1859 map {
1860 if (defined $r->{$_}->{svm}) {
1861 my $svm;
1862 eval {
1863 my $section = "svn-remote.$_";
1864 $svm = {
1865 source => tmp_config('--get',
1866 "$section.svm-source"),
1867 replace => tmp_config('--get',
1868 "$section.svm-replace"),
1869 }
1870 };
1871 $r->{$_}->{svm} = $svm;
1872 }
1873 } keys %$r;
1874
Eric Wong47e39c52007-01-21 04:27:09 -08001875 $r;
1876}
1877
Eric Wongecc712d2007-01-31 12:28:10 -08001878sub init_vars {
Karl Hasselström94bc9142008-02-03 17:56:18 +01001879 $_gc_nr = $_gc_period = 1000;
Karl Hasselströmaf788a62008-02-03 17:56:12 +01001880 if (defined $_repack || defined $_repack_flags) {
1881 warn "Repack options are obsolete; they have no effect.\n";
1882 }
Eric Wongecc712d2007-01-31 12:28:10 -08001883}
1884
Eric Wongb805b442007-01-22 13:52:04 -08001885sub verify_remotes_sanity {
Eric Wong536c4b02007-01-23 11:35:53 -08001886 return unless -d $ENV{GIT_DIR};
Eric Wongb805b442007-01-22 13:52:04 -08001887 my %seen;
1888 foreach (command(qw/config -l/)) {
1889 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1890 if ($seen{$1}) {
1891 die "Remote ref refs/remote/$1 is tracked by",
1892 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1893 "Please resolve this ambiguity in ",
1894 "your git configuration file before ",
1895 "continuing\n";
1896 }
1897 $seen{$1} = $_;
1898 }
1899 }
1900}
1901
Eric Wonge6434f82007-01-23 16:29:23 -08001902sub find_existing_remote {
1903 my ($url, $remotes) = @_;
Eric Wongbefc9ad2007-02-17 02:53:07 -08001904 return undef if $no_reuse_existing;
Eric Wonge6434f82007-01-23 16:29:23 -08001905 my $existing;
1906 foreach my $repo_id (keys %$remotes) {
1907 my $u = $remotes->{$repo_id}->{url} or next;
1908 next if $u ne $url;
1909 $existing = $repo_id;
1910 last;
1911 }
1912 $existing;
1913}
1914
1915sub init_remote_config {
Eric Wongd8115c52007-02-01 03:30:31 -08001916 my ($self, $url, $no_write) = @_;
Eric Wonge6434f82007-01-23 16:29:23 -08001917 $url =~ s!/+$!!; # strip trailing slash
1918 my $r = read_all_remotes();
1919 my $existing = find_existing_remote($url, $r);
1920 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001921 unless ($no_write) {
1922 print STDERR "Using existing ",
1923 "[svn-remote \"$existing\"]\n";
1924 }
Eric Wonge6434f82007-01-23 16:29:23 -08001925 $self->{repo_id} = $existing;
Eric Wong4a1bb4c2007-05-13 09:58:14 -07001926 } elsif ($_minimize_url) {
Eric Wonge6434f82007-01-23 16:29:23 -08001927 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
1928 $existing = find_existing_remote($min_url, $r);
1929 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001930 unless ($no_write) {
1931 print STDERR "Using existing ",
1932 "[svn-remote \"$existing\"]\n";
1933 }
Eric Wonge6434f82007-01-23 16:29:23 -08001934 $self->{repo_id} = $existing;
1935 }
1936 if ($min_url ne $url) {
Eric Wonge5181922007-02-08 12:53:57 -08001937 unless ($no_write) {
1938 print STDERR "Using higher level of URL: ",
1939 "$url => $min_url\n";
1940 }
Eric Wonge6434f82007-01-23 16:29:23 -08001941 my $old_path = $self->{path};
1942 $self->{path} = $url;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001943 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
Eric Wonge6434f82007-01-23 16:29:23 -08001944 if (length $old_path) {
1945 $self->{path} .= "/$old_path";
1946 }
1947 $url = $min_url;
1948 }
1949 }
1950 my $orig_url;
1951 if (!$existing) {
1952 # verify that we aren't overwriting anything:
1953 $orig_url = eval {
1954 command_oneline('config', '--get',
1955 "svn-remote.$self->{repo_id}.url")
1956 };
1957 if ($orig_url && ($orig_url ne $url)) {
1958 die "svn-remote.$self->{repo_id}.url already set: ",
1959 "$orig_url\nwanted to set to: $url\n";
1960 }
1961 }
1962 my ($xrepo_id, $xpath) = find_ref($self->refname);
Adam Brewster6f5748e2009-08-11 23:14:27 -04001963 if (!$no_write && defined $xpath) {
Eric Wonge6434f82007-01-23 16:29:23 -08001964 die "svn-remote.$xrepo_id.fetch already set to track ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04001965 "$xpath:", $self->refname, "\n";
Eric Wonge6434f82007-01-23 16:29:23 -08001966 }
Eric Wongd8115c52007-02-01 03:30:31 -08001967 unless ($no_write) {
1968 command_noisy('config',
1969 "svn-remote.$self->{repo_id}.url", $url);
Eric Wong46cf98b2007-07-14 12:40:32 -07001970 $self->{path} =~ s{^/}{};
Eric Wong5268f9e2009-08-16 14:22:12 -07001971 $self->{path} =~ s{%([0-9A-F]{2})}{chr hex($1)}ieg;
Eric Wongd8115c52007-02-01 03:30:31 -08001972 command_noisy('config', '--add',
1973 "svn-remote.$self->{repo_id}.fetch",
1974 "$self->{path}:".$self->refname);
1975 }
Eric Wonge6434f82007-01-23 16:29:23 -08001976 $self->{url} = $url;
1977}
1978
Eric Wonga8ae2622007-02-13 14:22:11 -08001979sub find_by_url { # repos_root and, path are optional
1980 my ($class, $full_url, $repos_root, $path) = @_;
Adam Roben56973d22007-04-25 12:42:58 -07001981
Eric Wong1a97a502007-02-20 00:43:19 -08001982 return undef unless defined $full_url;
Adam Roben56973d22007-04-25 12:42:58 -07001983 remove_username($full_url);
1984 remove_username($repos_root) if defined $repos_root;
Eric Wonga8ae2622007-02-13 14:22:11 -08001985 my $remotes = read_all_remotes();
1986 if (defined $full_url && defined $repos_root && !defined $path) {
1987 $path = $full_url;
1988 $path =~ s#^\Q$repos_root\E(?:/|$)##;
1989 }
1990 foreach my $repo_id (keys %$remotes) {
1991 my $u = $remotes->{$repo_id}->{url} or next;
Adam Roben56973d22007-04-25 12:42:58 -07001992 remove_username($u);
Eric Wonga8ae2622007-02-13 14:22:11 -08001993 next if defined $repos_root && $repos_root ne $u;
1994
1995 my $fetch = $remotes->{$repo_id}->{fetch} || {};
Marc Branchaud62244062009-06-23 13:02:08 -04001996 foreach my $t (qw/branches tags/) {
1997 foreach my $globspec (@{$remotes->{$repo_id}->{$t}}) {
1998 resolve_local_globs($u, $fetch, $globspec);
1999 }
Eric Wonga8ae2622007-02-13 14:22:11 -08002000 }
2001 my $p = $path;
John Goerzen0bb91d92008-03-08 16:04:05 -06002002 my $rwr = rewrite_root({repo_id => $repo_id});
João Abecasis63c56022008-07-14 16:28:04 +01002003 my $svm = $remotes->{$repo_id}->{svm}
2004 if defined $remotes->{$repo_id}->{svm};
Eric Wonga8ae2622007-02-13 14:22:11 -08002005 unless (defined $p) {
2006 $p = $full_url;
John Goerzen0bb91d92008-03-08 16:04:05 -06002007 my $z = $u;
João Abecasis63c56022008-07-14 16:28:04 +01002008 my $prefix = '';
John Goerzen0bb91d92008-03-08 16:04:05 -06002009 if ($rwr) {
2010 $z = $rwr;
Dévai Tamás1b7e5432009-02-12 00:14:02 +01002011 remove_username($z);
João Abecasis63c56022008-07-14 16:28:04 +01002012 } elsif (defined $svm) {
2013 $z = $svm->{source};
2014 $prefix = $svm->{replace};
2015 $prefix =~ s#^\Q$u\E(?:/|$)##;
2016 $prefix =~ s#/$##;
John Goerzen0bb91d92008-03-08 16:04:05 -06002017 }
João Abecasis63c56022008-07-14 16:28:04 +01002018 $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next;
Eric Wonga8ae2622007-02-13 14:22:11 -08002019 }
2020 foreach my $f (keys %$fetch) {
2021 next if $f ne $p;
2022 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
2023 }
2024 }
2025 undef;
2026}
2027
Eric Wong9b981fc2007-01-11 12:14:21 -08002028sub init {
Eric Wongd8115c52007-02-01 03:30:31 -08002029 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08002030 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong9b981fc2007-01-11 12:14:21 -08002031 if (defined $url) {
Eric Wongd8115c52007-02-01 03:30:31 -08002032 $self->init_remote_config($url, $no_write);
Eric Wong9b981fc2007-01-11 12:14:21 -08002033 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002034 $self;
2035}
2036
Eric Wong706587f2007-01-18 17:50:01 -08002037sub find_ref {
2038 my ($ref_id) = @_;
2039 foreach (command(qw/config -l/)) {
2040 next unless m!^svn-remote\.(.+)\.fetch=
Eric Wongffd5c8e2009-10-22 23:39:04 -07002041 \s*(.*?)\s*:\s*(.+?)\s*$!x;
Eric Wong706587f2007-01-18 17:50:01 -08002042 my ($repo_id, $path, $ref) = ($1, $2, $3);
2043 if ($ref eq $ref_id) {
2044 $path = '' if ($path =~ m#^\./?#);
2045 return ($repo_id, $path);
2046 }
2047 }
2048 (undef, undef, undef);
2049}
2050
Eric Wong9b981fc2007-01-11 12:14:21 -08002051sub new {
Eric Wong706587f2007-01-18 17:50:01 -08002052 my ($class, $ref_id, $repo_id, $path) = @_;
2053 if (defined $ref_id && !defined $repo_id && !defined $path) {
2054 ($repo_id, $path) = find_ref($ref_id);
2055 if (!defined $repo_id) {
2056 die "Could not find a \"svn-remote.*.fetch\" key ",
2057 "in the repository configuration matching: ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04002058 "$ref_id\n";
Eric Wong706587f2007-01-18 17:50:01 -08002059 }
2060 }
2061 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong8b8fc062007-01-22 11:44:57 -08002062 if (!defined $self->{path} || !length $self->{path}) {
2063 my $fetch = command_oneline('config', '--get',
2064 "svn-remote.$repo_id.fetch",
Adam Brewster6f5748e2009-08-11 23:14:27 -04002065 ":$ref_id\$") or
Eric Wong8b8fc062007-01-22 11:44:57 -08002066 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
Adam Brewster6f5748e2009-08-11 23:14:27 -04002067 "\":$ref_id\$\" in config\n";
Eric Wong8b8fc062007-01-22 11:44:57 -08002068 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
2069 }
Eric Wongb1a954a2010-06-14 04:31:10 +00002070 $self->{path} =~ s{/+}{/}g;
2071 $self->{path} =~ s{\A/}{};
2072 $self->{path} =~ s{/\z}{};
Eric Wong706587f2007-01-18 17:50:01 -08002073 $self->{url} = command_oneline('config', '--get',
2074 "svn-remote.$repo_id.url") or
2075 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
Alejandro R. Sedeño12a296b2011-04-08 10:57:54 -04002076 $self->{pushurl} = eval { command_oneline('config', '--get',
2077 "svn-remote.$repo_id.pushurl") };
Eric Wongd6d33462007-02-16 04:05:33 -08002078 $self->rebuild;
Eric Wong9b981fc2007-01-11 12:14:21 -08002079 $self;
2080}
2081
Robert Ewaldbf655fd2007-07-30 11:08:21 +02002082sub refname {
Adam Brewster6f5748e2009-08-11 23:14:27 -04002083 my ($refname) = $_[0]->{ref_id} ;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02002084
2085 # It cannot end with a slash /, we'll throw up on this because
2086 # SVN can't have directories with a slash in their name, either:
2087 if ($refname =~ m{/$}) {
2088 die "ref: '$refname' ends with a trailing slash, this is ",
2089 "not permitted by git nor Subversion\n";
2090 }
2091
2092 # It cannot have ASCII control character space, tilde ~, caret ^,
2093 # colon :, question-mark ?, asterisk *, space, or open bracket [
2094 # anywhere.
2095 #
2096 # Additionally, % must be escaped because it is used for escaping
2097 # and we want our escaped refname to be reversible
2098 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
2099
2100 # no slash-separated component can begin with a dot .
2101 # /.* becomes /%2E*
2102 $refname =~ s{/\.}{/%2E}g;
2103
2104 # It cannot have two consecutive dots .. anywhere
2105 # .. becomes %2E%2E
2106 $refname =~ s{\.\.}{%2E%2E}g;
2107
Torsten Schmutzler73d41952010-05-06 22:20:43 +02002108 # trailing dots and .lock are not allowed
2109 # .$ becomes %2E and .lock becomes %2Elock
2110 $refname =~ s{\.(?=$|lock$)}{%2E};
2111
2112 # the sequence @{ is used to access the reflog
2113 # @{ becomes %40{
2114 $refname =~ s{\@\{}{%40\{}g;
2115
Robert Ewaldbf655fd2007-07-30 11:08:21 +02002116 return $refname;
2117}
2118
2119sub desanitize_refname {
2120 my ($refname) = @_;
2121 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
2122 return $refname;
2123}
Eric Wong9b981fc2007-01-11 12:14:21 -08002124
Eric Wong26a62d52007-02-12 13:25:25 -08002125sub svm_uuid {
2126 my ($self) = @_;
2127 return $self->{svm}->{uuid} if $self->svm;
2128 $self->ra;
2129 unless ($self->{svm}) {
2130 die "SVM UUID not cached, and reading remotely failed\n";
2131 }
2132 $self->{svm}->{uuid};
2133}
Eric Wong8a49ee92007-02-10 20:46:50 -08002134
Eric Wong26a62d52007-02-12 13:25:25 -08002135sub svm {
2136 my ($self) = @_;
2137 return $self->{svm} if $self->{svm};
2138 my $svm;
Eric Wong8a49ee92007-02-10 20:46:50 -08002139 # see if we have it in our config, first:
2140 eval {
Eric Wong26a62d52007-02-12 13:25:25 -08002141 my $section = "svn-remote.$self->{repo_id}";
2142 $svm = {
Eric Wong93f26892007-02-11 01:20:26 -08002143 source => tmp_config('--get', "$section.svm-source"),
2144 uuid => tmp_config('--get', "$section.svm-uuid"),
Eric Wongbefc9ad2007-02-17 02:53:07 -08002145 replace => tmp_config('--get', "$section.svm-replace"),
Eric Wong8a49ee92007-02-10 20:46:50 -08002146 }
2147 };
Eric Wongbefc9ad2007-02-17 02:53:07 -08002148 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
2149 $self->{svm} = $svm;
2150 }
Eric Wong26a62d52007-02-12 13:25:25 -08002151 $self->{svm};
2152}
2153
2154sub _set_svm_vars {
2155 my ($self, $ra) = @_;
Eric Wongdb03cd22007-02-13 00:38:02 -08002156 return $ra if $self->svm;
Eric Wong26a62d52007-02-12 13:25:25 -08002157
Eric Wongdb03cd22007-02-13 00:38:02 -08002158 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08002159 "(svm:source, svm:uuid) ",
Eric Wongdb03cd22007-02-13 00:38:02 -08002160 "from the following URLs:\n" );
2161 sub read_svm_props {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002162 my ($self, $ra, $path, $r) = @_;
2163 my $props = ($ra->get_dir($path, $r))[2];
Eric Wongdb03cd22007-02-13 00:38:02 -08002164 my $src = $props->{'svm:source'};
Eric Wong8a49ee92007-02-10 20:46:50 -08002165 my $uuid = $props->{'svm:uuid'};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002166 return undef if (!$src || !$uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08002167
Eric Wongbefc9ad2007-02-17 02:53:07 -08002168 chomp($src, $uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08002169
Eric Wongb3e95932009-07-11 14:13:12 -07002170 $uuid =~ m{^[0-9a-f\-]{30,}$}i
Eric Wong8a49ee92007-02-10 20:46:50 -08002171 or die "doesn't look right - svm:uuid is '$uuid'\n";
Eric Wongbefc9ad2007-02-17 02:53:07 -08002172
2173 # the '!' is used to mark the repos_root!/relative/path
2174 $src =~ s{/?!/?}{/};
Eric Wongdb03cd22007-02-13 00:38:02 -08002175 $src =~ s{/+$}{}; # no trailing slashes please
Eric Wongbefc9ad2007-02-17 02:53:07 -08002176 # username is of no interest
Eric Wongdb03cd22007-02-13 00:38:02 -08002177 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
Eric Wong8a49ee92007-02-10 20:46:50 -08002178
Eric Wongbefc9ad2007-02-17 02:53:07 -08002179 my $replace = $ra->{url};
2180 $replace .= "/$path" if length $path;
2181
Eric Wongdb03cd22007-02-13 00:38:02 -08002182 my $section = "svn-remote.$self->{repo_id}";
Eric Wongbefc9ad2007-02-17 02:53:07 -08002183 tmp_config("$section.svm-source", $src);
2184 tmp_config("$section.svm-replace", $replace);
2185 tmp_config("$section.svm-uuid", $uuid);
2186 $self->{svm} = {
2187 source => $src,
2188 uuid => $uuid,
2189 replace => $replace
2190 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002191 }
Eric Wongdb03cd22007-02-13 00:38:02 -08002192
2193 my $r = $ra->get_latest_revnum;
2194 my $path = $self->{path};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002195 my %tried;
Eric Wongdb03cd22007-02-13 00:38:02 -08002196 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002197 unless ($tried{"$self->{url}/$path"}) {
2198 return $ra if $self->read_svm_props($ra, $path, $r);
2199 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002200 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002201 $path =~ s#/?[^/]+$##;
Eric Wong8a49ee92007-02-10 20:46:50 -08002202 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002203 die "Path: '$path' should be ''\n" if $path ne '';
2204 return $ra if $self->read_svm_props($ra, $path, $r);
2205 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002206
2207 if ($ra->{repos_root} eq $self->{url}) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002208 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002209 }
2210
2211 # nope, make sure we're connected to the repository root:
2212 my $ok;
2213 my @tried_b;
2214 $path = $ra->{svn_path};
Eric Wongdb03cd22007-02-13 00:38:02 -08002215 $ra = Git::SVN::Ra->new($ra->{repos_root});
2216 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002217 unless ($tried{"$ra->{url}/$path"}) {
2218 $ok = $self->read_svm_props($ra, $path, $r);
2219 last if $ok;
2220 $tried{"$ra->{url}/$path"} = 1;
2221 }
2222 $path =~ s#/?[^/]+$##;
Eric Wongdb03cd22007-02-13 00:38:02 -08002223 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002224 die "Path: '$path' should be ''\n" if $path ne '';
2225 $ok ||= $self->read_svm_props($ra, $path, $r);
2226 $tried{"$ra->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08002227 if (!$ok) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08002228 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002229 }
2230 Git::SVN::Ra->new($self->{url});
Eric Wong8a49ee92007-02-10 20:46:50 -08002231}
2232
Eric Wong62e349d2007-02-16 19:57:29 -08002233sub svnsync {
2234 my ($self) = @_;
2235 return $self->{svnsync} if $self->{svnsync};
2236
2237 if ($self->no_metadata) {
2238 die "Can't have both 'noMetadata' and ",
2239 "'useSvnsyncProps' options set!\n";
2240 }
2241 if ($self->rewrite_root) {
2242 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
2243 "options set!\n";
2244 }
Jay Soffian3e18ce12010-01-23 03:30:00 -05002245 if ($self->rewrite_uuid) {
2246 die "Can't have both 'useSvnsyncProps' and 'rewriteUUID' ",
2247 "options set!\n";
2248 }
Eric Wong62e349d2007-02-16 19:57:29 -08002249
2250 my $svnsync;
2251 # see if we have it in our config, first:
2252 eval {
2253 my $section = "svn-remote.$self->{repo_id}";
Eric Wong98fa5b62008-01-11 23:13:55 -08002254
2255 my $url = tmp_config('--get', "$section.svnsync-url");
2256 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
2257 die "doesn't look right - svn:sync-from-url is '$url'\n";
2258
2259 my $uuid = tmp_config('--get', "$section.svnsync-uuid");
Eric Wongb3e95932009-07-11 14:13:12 -07002260 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
Eric Wong98fa5b62008-01-11 23:13:55 -08002261 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2262
2263 $svnsync = { url => $url, uuid => $uuid }
Eric Wong62e349d2007-02-16 19:57:29 -08002264 };
2265 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
2266 return $self->{svnsync} = $svnsync;
2267 }
2268
2269 my $err = "useSvnsyncProps set, but failed to read " .
2270 "svnsync property: svn:sync-from-";
2271 my $rp = $self->ra->rev_proplist(0);
2272
2273 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
Eric Wong98fa5b62008-01-11 23:13:55 -08002274 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
Eric Wong62e349d2007-02-16 19:57:29 -08002275 die "doesn't look right - svn:sync-from-url is '$url'\n";
2276
2277 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
Eric Wongb3e95932009-07-11 14:13:12 -07002278 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}i) or
Eric Wong62e349d2007-02-16 19:57:29 -08002279 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2280
2281 my $section = "svn-remote.$self->{repo_id}";
2282 tmp_config('--add', "$section.svnsync-uuid", $uuid);
2283 tmp_config('--add', "$section.svnsync-url", $url);
2284 return $self->{svnsync} = { url => $url, uuid => $uuid };
2285}
2286
Eric Wong26a62d52007-02-12 13:25:25 -08002287# this allows us to memoize our SVN::Ra UUID locally and avoid a
2288# remote lookup (useful for 'git svn log').
2289sub ra_uuid {
2290 my ($self) = @_;
2291 unless ($self->{ra_uuid}) {
2292 my $key = "svn-remote.$self->{repo_id}.uuid";
2293 my $uuid = eval { tmp_config('--get', $key) };
Eric Wongb3e95932009-07-11 14:13:12 -07002294 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/i) {
Eric Wong26a62d52007-02-12 13:25:25 -08002295 $self->{ra_uuid} = $uuid;
2296 } else {
2297 die "ra_uuid called without URL\n" unless $self->{url};
2298 $self->{ra_uuid} = $self->ra->get_uuid;
2299 tmp_config('--add', $key, $self->{ra_uuid});
2300 }
2301 }
2302 $self->{ra_uuid};
2303}
2304
Eric Wonga5460eb2007-11-21 18:20:57 -08002305sub _set_repos_root {
2306 my ($self, $repos_root) = @_;
2307 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2308 $repos_root ||= $self->ra->{repos_root};
2309 tmp_config($k, $repos_root);
2310 $repos_root;
2311}
2312
2313sub repos_root {
2314 my ($self) = @_;
2315 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2316 eval { tmp_config('--get', $k) } || $self->_set_repos_root;
2317}
2318
Eric Wong9b981fc2007-01-11 12:14:21 -08002319sub ra {
2320 my ($self) = shift;
Eric Wong8a49ee92007-02-10 20:46:50 -08002321 my $ra = Git::SVN::Ra->new($self->{url});
Eric Wonga5460eb2007-11-21 18:20:57 -08002322 $self->_set_repos_root($ra->{repos_root});
Eric Wong91b03282007-02-11 00:51:33 -08002323 if ($self->use_svm_props && !$self->{svm}) {
2324 if ($self->no_metadata) {
Eric Wong97ae0912007-02-11 15:21:24 -08002325 die "Can't have both 'noMetadata' and ",
2326 "'useSvmProps' options set!\n";
Eric Wong62e349d2007-02-16 19:57:29 -08002327 } elsif ($self->use_svnsync_props) {
2328 die "Can't have both 'useSvnsyncProps' and ",
2329 "'useSvmProps' options set!\n";
Eric Wong91b03282007-02-11 00:51:33 -08002330 }
Eric Wong26a62d52007-02-12 13:25:25 -08002331 $ra = $self->_set_svm_vars($ra);
Eric Wong8a49ee92007-02-10 20:46:50 -08002332 $self->{-want_revprops} = 1;
2333 }
2334 $ra;
Eric Wong9b981fc2007-01-11 12:14:21 -08002335}
2336
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002337# prop_walk(PATH, REV, SUB)
2338# -------------------------
2339# Recursively traverse PATH at revision REV and invoke SUB for each
2340# directory that contains a SVN property. SUB will be invoked as
2341# follows: &SUB(gs, path, props); where `gs' is this instance of
2342# Git::SVN, `path' the path to the directory where the properties
2343# `props' were found. The `path' will be relative to point of checkout,
2344# that is, if url://repo/trunk is the current Git branch, and that
2345# directory contains a sub-directory `d', SUB will be invoked with `/d/'
2346# as `path' (note the trailing `/').
2347sub prop_walk {
2348 my ($self, $path, $rev, $sub) = @_;
2349
Kevin Ballard35cda062008-01-09 01:37:20 -05002350 $path =~ s#^/##;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002351 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
2352 $path =~ s#^/*#/#g;
Eric Wong9b981fc2007-01-11 12:14:21 -08002353 my $p = $path;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002354 # Strip the irrelevant part of the path.
2355 $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
2356 # Ensure the path is terminated by a `/'.
2357 $p =~ s#/*$#/#;
2358
2359 # The properties contain all the internal SVN stuff nobody
2360 # (usually) cares about.
2361 my $interesting_props = 0;
2362 foreach (keys %{$props}) {
2363 # If it doesn't start with `svn:', it must be a
2364 # user-defined property.
2365 ++$interesting_props and next if $_ !~ /^svn:/;
2366 # FIXME: Fragile, if SVN adds new public properties,
2367 # this needs to be updated.
2368 ++$interesting_props if /^svn:(?:ignore|keywords|executable
2369 |eol-style|mime-type
2370 |externals|needs-lock)$/x;
Eric Wong9b981fc2007-01-11 12:14:21 -08002371 }
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002372 &$sub($self, $p, $props) if $interesting_props;
2373
Eric Wong9b981fc2007-01-11 12:14:21 -08002374 foreach (sort keys %$dirent) {
Eric Wong0dc03d62007-05-13 01:04:43 -07002375 next if $dirent->{$_}->{kind} != $SVN::Node::dir;
Christian Engwerb7166cc2008-05-27 08:46:55 +00002376 $self->prop_walk($self->{path} . $p . $_, $rev, $sub);
Eric Wong9b981fc2007-01-11 12:14:21 -08002377 }
2378}
2379
Eric Wong3ebe8df2007-01-25 17:35:40 -08002380sub last_rev { ($_[0]->last_rev_commit)[0] }
2381sub last_commit { ($_[0]->last_rev_commit)[1] }
2382
Eric Wong9b981fc2007-01-11 12:14:21 -08002383# returns the newest SVN revision number and newest commit SHA1
2384sub last_rev_commit {
2385 my ($self) = @_;
2386 if (defined $self->{last_rev} && defined $self->{last_commit}) {
2387 return ($self->{last_rev}, $self->{last_commit});
2388 }
Eric Wongd2866f92007-01-11 12:26:16 -08002389 my $c = ::verify_ref($self->refname.'^0');
Eric Wong91b03282007-02-11 00:51:33 -08002390 if ($c && !$self->use_svm_props && !$self->no_metadata) {
Eric Wongd2866f92007-01-11 12:26:16 -08002391 my $rev = (::cmt_metadata($c))[1];
Eric Wong9b981fc2007-01-11 12:14:21 -08002392 if (defined $rev) {
2393 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2394 return ($rev, $c);
2395 }
2396 }
Eric Wong060610c2007-12-08 23:27:41 -08002397 my $map_path = $self->map_path;
2398 unless (-e $map_path) {
Eric Wong26a62d52007-02-12 13:25:25 -08002399 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
2400 return (undef, undef);
2401 }
Eric Wong66ab84b2007-12-08 23:27:42 -08002402 my ($rev, $commit) = $self->rev_map_max(1);
Eric Wong060610c2007-12-08 23:27:41 -08002403 ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
2404 return ($rev, $commit);
Eric Wong9b981fc2007-01-11 12:14:21 -08002405}
2406
Eric Wong3ebe8df2007-01-25 17:35:40 -08002407sub get_fetch_range {
2408 my ($self, $min, $max) = @_;
2409 $max ||= $self->ra->get_latest_revnum;
Eric Wong060610c2007-12-08 23:27:41 -08002410 $min ||= $self->rev_map_max;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002411 (++$min, $max);
Eric Wong9b981fc2007-01-11 12:14:21 -08002412}
2413
Eric Wong8a49ee92007-02-10 20:46:50 -08002414sub tmp_config {
Eric Wong93f26892007-02-11 01:20:26 -08002415 my (@args) = @_;
Eric Wongb7e53482007-02-16 04:09:28 -08002416 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
2417 my $config = "$ENV{GIT_DIR}/svn/.metadata";
Eric Wong38570a42007-06-13 02:37:05 -07002418 if (! -f $config && -f $old_def_config) {
Eric Wongb7e53482007-02-16 04:09:28 -08002419 rename $old_def_config, $config or
2420 die "Failed rename $old_def_config => $config: $!\n";
2421 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002422 my $old_config = $ENV{GIT_CONFIG};
Eric Wong93f26892007-02-11 01:20:26 -08002423 $ENV{GIT_CONFIG} = $config;
Eric Wong8a49ee92007-02-10 20:46:50 -08002424 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002425 my @ret = eval {
2426 unless (-f $config) {
2427 mkfile($config);
2428 open my $fh, '>', $config or
2429 die "Can't open $config: $!\n";
2430 print $fh "; This file is used internally by ",
2431 "git-svn\n" or die
2432 "Couldn't write to $config: $!\n";
2433 print $fh "; You should not have to edit it\n" or
2434 die "Couldn't write to $config: $!\n";
2435 close $fh or die "Couldn't close $config: $!\n";
2436 }
2437 command('config', @args);
2438 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002439 my $err = $@;
2440 if (defined $old_config) {
2441 $ENV{GIT_CONFIG} = $old_config;
2442 } else {
2443 delete $ENV{GIT_CONFIG};
2444 }
2445 die $err if $err;
2446 wantarray ? @ret : $ret[0];
2447}
2448
Eric Wong9b981fc2007-01-11 12:14:21 -08002449sub tmp_index_do {
2450 my ($self, $sub) = @_;
2451 my $old_index = $ENV{GIT_INDEX_FILE};
2452 $ENV{GIT_INDEX_FILE} = $self->{index};
Eric Wong8a49ee92007-02-10 20:46:50 -08002453 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002454 my @ret = eval {
2455 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
2456 mkpath([$dir]) unless -d $dir;
2457 &$sub;
2458 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002459 my $err = $@;
2460 if (defined $old_index) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002461 $ENV{GIT_INDEX_FILE} = $old_index;
2462 } else {
2463 delete $ENV{GIT_INDEX_FILE};
2464 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002465 die $err if $err;
Eric Wong9b981fc2007-01-11 12:14:21 -08002466 wantarray ? @ret : $ret[0];
2467}
2468
2469sub assert_index_clean {
2470 my ($self, $treeish) = @_;
2471
2472 $self->tmp_index_do(sub {
2473 command_noisy('read-tree', $treeish) unless -e $self->{index};
2474 my $x = command_oneline('write-tree');
2475 my ($y) = (command(qw/cat-file commit/, $treeish) =~
2476 /^tree ($::sha1)/mo);
Eric Wonge8d120b2007-02-14 16:29:52 -08002477 return if $y eq $x;
2478
2479 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2480 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2481 command_noisy('read-tree', $treeish);
Eric Wong9b981fc2007-01-11 12:14:21 -08002482 $x = command_oneline('write-tree');
2483 if ($y ne $x) {
2484 ::fatal "trees ($treeish) $y != $x\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02002485 "Something is seriously wrong...";
Eric Wong9b981fc2007-01-11 12:14:21 -08002486 }
2487 });
2488}
2489
2490sub get_commit_parents {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002491 my ($self, $log_entry) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002492 my (%seen, @ret, @tmp);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002493 # legacy support for 'set-tree'; this is only used by set_tree_cb:
2494 if (my $ip = $self->{inject_parents}) {
2495 if (my $commit = delete $ip->{$log_entry->{revision}}) {
2496 push @tmp, $commit;
Eric Wong9b981fc2007-01-11 12:14:21 -08002497 }
2498 }
Eric Wongd2866f92007-01-11 12:26:16 -08002499 if (my $cur = ::verify_ref($self->refname.'^0')) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002500 push @tmp, $cur;
2501 }
Eric Wong733a65a2007-06-13 02:23:28 -07002502 if (my $ipd = $self->{inject_parents_dcommit}) {
2503 if (my $commit = delete $ipd->{$log_entry->{revision}}) {
2504 push @tmp, @$commit;
2505 }
2506 }
Eric Wong44320b92007-01-13 22:35:53 -08002507 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
Eric Wong9b981fc2007-01-11 12:14:21 -08002508 while (my $p = shift @tmp) {
2509 next if $seen{$p};
2510 $seen{$p} = 1;
2511 push @ret, $p;
Eric Wong9b981fc2007-01-11 12:14:21 -08002512 }
2513 @ret;
2514}
2515
Eric Wongaea736c2007-02-16 19:15:21 -08002516sub rewrite_root {
2517 my ($self) = @_;
2518 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
2519 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2520 my $rwr = eval { command_oneline(qw/config --get/, $k) };
2521 if ($rwr) {
2522 $rwr =~ s#/+$##;
2523 if ($rwr !~ m#^[a-z\+]+://#) {
2524 die "$rwr is not a valid URL (key: $k)\n";
2525 }
2526 }
2527 $self->{-rewrite_root} = $rwr;
2528}
2529
Jay Soffian3e18ce12010-01-23 03:30:00 -05002530sub rewrite_uuid {
2531 my ($self) = @_;
2532 return $self->{-rewrite_uuid} if exists $self->{-rewrite_uuid};
2533 my $k = "svn-remote.$self->{repo_id}.rewriteUUID";
2534 my $rwid = eval { command_oneline(qw/config --get/, $k) };
2535 if ($rwid) {
2536 $rwid =~ s#/+$##;
2537 if ($rwid !~ m#^[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}$#) {
2538 die "$rwid is not a valid UUID (key: $k)\n";
2539 }
2540 }
2541 $self->{-rewrite_uuid} = $rwid;
2542}
2543
Eric Wongaea736c2007-02-16 19:15:21 -08002544sub metadata_url {
2545 my ($self) = @_;
2546 ($self->rewrite_root || $self->{url}) .
2547 (length $self->{path} ? '/' . $self->{path} : '');
2548}
2549
Eric Wong706587f2007-01-18 17:50:01 -08002550sub full_url {
Eric Wong9b981fc2007-01-11 12:14:21 -08002551 my ($self) = @_;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002552 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
Eric Wong9b981fc2007-01-11 12:14:21 -08002553}
2554
Alejandro R. Sedeño12a296b2011-04-08 10:57:54 -04002555sub full_pushurl {
2556 my ($self) = @_;
2557 if ($self->{pushurl}) {
2558 return $self->{pushurl} . (length $self->{path} ? '/' .
2559 $self->{path} : '');
2560 } else {
2561 return $self->full_url;
2562 }
2563}
Eric Wongad948022007-12-15 19:08:22 -08002564
2565sub set_commit_header_env {
2566 my ($log_entry) = @_;
2567 my %env;
2568 foreach my $ned (qw/NAME EMAIL DATE/) {
2569 foreach my $ac (qw/AUTHOR COMMITTER/) {
2570 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2571 }
2572 }
2573
2574 $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
2575 $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
2576 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
2577
2578 $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
2579 ? $log_entry->{commit_name}
2580 : $log_entry->{name};
2581 $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
2582 ? $log_entry->{commit_email}
2583 : $log_entry->{email};
2584 \%env;
2585}
2586
2587sub restore_commit_header_env {
2588 my ($env) = @_;
2589 foreach my $ned (qw/NAME EMAIL DATE/) {
2590 foreach my $ac (qw/AUTHOR COMMITTER/) {
2591 my $k = "GIT_${ac}_${ned}";
2592 if (defined $env->{$k}) {
2593 $ENV{$k} = $env->{$k};
2594 } else {
2595 delete $ENV{$k};
2596 }
2597 }
2598 }
2599}
2600
Karl Hasselström94bc9142008-02-03 17:56:18 +01002601sub gc {
2602 command_noisy('gc', '--auto');
2603};
2604
Eric Wong9b981fc2007-01-11 12:14:21 -08002605sub do_git_commit {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002606 my ($self, $log_entry) = @_;
Eric Wong8a603772007-01-31 02:45:50 -08002607 my $lr = $self->last_rev;
2608 if (defined $lr && $lr >= $log_entry->{revision}) {
2609 die "Last fetched revision of ", $self->refname,
2610 " was r$lr, but we are about to fetch: ",
2611 "r$log_entry->{revision}!\n";
2612 }
Eric Wong060610c2007-12-08 23:27:41 -08002613 if (my $c = $self->rev_map_get($log_entry->{revision})) {
Eric Wong44320b92007-01-13 22:35:53 -08002614 croak "$log_entry->{revision} = $c already exists! ",
Eric Wong9b981fc2007-01-11 12:14:21 -08002615 "Why are we refetching it?\n";
2616 }
Eric Wongad948022007-12-15 19:08:22 -08002617 my $old_env = set_commit_header_env($log_entry);
Eric Wong44320b92007-01-13 22:35:53 -08002618 my $tree = $log_entry->{tree};
Eric Wong9b981fc2007-01-11 12:14:21 -08002619 if (!defined $tree) {
2620 $tree = $self->tmp_index_do(sub {
2621 command_oneline('write-tree') });
2622 }
2623 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2624
Deskin Millere855bfc2008-10-31 00:10:25 -04002625 my @exec = ('git', 'commit-tree', $tree);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002626 foreach ($self->get_commit_parents($log_entry)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002627 push @exec, '-p', $_;
2628 }
2629 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2630 or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07002631 binmode $msg_fh;
2632
2633 # we always get UTF-8 from SVN, but we may want our commits in
2634 # a different encoding.
2635 if (my $enc = Git::config('i18n.commitencoding')) {
2636 require Encode;
2637 Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
2638 }
Eric Wong44320b92007-01-13 22:35:53 -08002639 print $msg_fh $log_entry->{log} or croak $!;
Eric Wongad948022007-12-15 19:08:22 -08002640 restore_commit_header_env($old_env);
Eric Wong91b03282007-02-11 00:51:33 -08002641 unless ($self->no_metadata) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002642 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2643 or croak $!;
Eric Wong9760adc2007-01-31 03:06:56 -08002644 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002645 $msg_fh->flush == 0 or croak $!;
2646 close $msg_fh or croak $!;
2647 chomp(my $commit = do { local $/; <$out_fh> });
2648 close $out_fh or croak $!;
2649 waitpid $pid, 0;
2650 croak $? if $?;
2651 if ($commit !~ /^$::sha1$/o) {
2652 die "Failed to commit, invalid sha1: $commit\n";
2653 }
2654
Eric Wong060610c2007-12-08 23:27:41 -08002655 $self->rev_map_set($log_entry->{revision}, $commit, 1);
Eric Wong9b981fc2007-01-11 12:14:21 -08002656
Eric Wong44320b92007-01-13 22:35:53 -08002657 $self->{last_rev} = $log_entry->{revision};
Eric Wong9b981fc2007-01-11 12:14:21 -08002658 $self->{last_commit} = $commit;
Simon Arlott49750f32009-03-30 19:31:41 +01002659 print "r$log_entry->{revision}" unless $::_q > 1;
Eric Wong8a49ee92007-02-10 20:46:50 -08002660 if (defined $log_entry->{svm_revision}) {
Simon Arlott49750f32009-03-30 19:31:41 +01002661 print " (\@$log_entry->{svm_revision})" unless $::_q > 1;
Eric Wong060610c2007-12-08 23:27:41 -08002662 $self->rev_map_set($log_entry->{svm_revision}, $commit,
Eric Wong26a62d52007-02-12 13:25:25 -08002663 0, $self->svm_uuid);
Eric Wong8a49ee92007-02-10 20:46:50 -08002664 }
Simon Arlott49750f32009-03-30 19:31:41 +01002665 print " = $commit ($self->{ref_id})\n" unless $::_q > 1;
Karl Hasselström94bc9142008-02-03 17:56:18 +01002666 if (--$_gc_nr == 0) {
2667 $_gc_nr = $_gc_period;
2668 gc();
2669 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002670 return $commit;
2671}
2672
Eric Wongfbcc1732007-02-06 18:35:30 -08002673sub match_paths {
2674 my ($self, $paths, $r) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002675 return 1 if $self->{path} eq '';
Eric Wongd542aed2007-02-09 02:19:41 -08002676 if (my $path = $paths->{"/$self->{path}"}) {
2677 return ($path->{action} eq 'D') ? 0 : 1;
2678 }
Mattias Nissler0b2af452009-07-07 01:40:02 +02002679 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
Eric Wongfbcc1732007-02-06 18:35:30 -08002680 if (grep /$self->{path_regex}/, keys %$paths) {
2681 return 1;
2682 }
2683 my $c = '';
2684 foreach (split m#/#, $self->{path}) {
2685 $c .= "/$_";
Eric Wong74a81222007-02-10 13:28:50 -08002686 next unless ($paths->{$c} &&
2687 ($paths->{$c}->{action} =~ /^[AR]$/));
Eric Wonge5181922007-02-08 12:53:57 -08002688 if ($self->ra->check_path($self->{path}, $r) ==
2689 $SVN::Node::dir) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002690 return 1;
2691 }
2692 }
2693 return 0;
2694}
2695
Eric Wong15710b62007-01-22 02:20:33 -08002696sub find_parent_branch {
2697 my ($self, $paths, $rev) = @_;
Eric Wong91b03282007-02-11 00:51:33 -08002698 return undef unless $self->follow_parent;
Eric Wonge5a0b242007-01-25 15:44:54 -08002699 unless (defined $paths) {
Eric Wongc7eba712007-01-31 03:45:28 -08002700 my $err_handler = $SVN::Error::handler;
2701 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
Mattias Nissler3c49a032009-07-07 01:39:52 +02002702 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1,
2703 sub { $paths = $_[0] });
Eric Wongc7eba712007-01-31 03:45:28 -08002704 $SVN::Error::handler = $err_handler;
Eric Wonge5a0b242007-01-25 15:44:54 -08002705 }
2706 return undef unless defined $paths;
Eric Wong15710b62007-01-22 02:20:33 -08002707
2708 # look for a parent from another branch:
Mattias Nissler0b2af452009-07-07 01:40:02 +02002709 my @b_path_components = split m#/#, $self->{path};
Eric Wong7f578c52007-01-24 02:16:25 -08002710 my @a_path_components;
2711 my $i;
2712 while (@b_path_components) {
2713 $i = $paths->{'/'.join('/', @b_path_components)};
Eric Wong74a81222007-02-10 13:28:50 -08002714 last if $i && defined $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002715 unshift(@a_path_components, pop(@b_path_components));
2716 }
Eric Wong74a81222007-02-10 13:28:50 -08002717 return undef unless defined $i && defined $i->{copyfrom_path};
2718 my $branch_from = $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002719 if (@a_path_components) {
2720 print STDERR "branch_from: $branch_from => ";
2721 $branch_from .= '/'.join('/', @a_path_components);
2722 print STDERR $branch_from, "\n";
2723 }
Eric Wong3ebe8df2007-01-25 17:35:40 -08002724 my $r = $i->{copyfrom_rev};
Eric Wong15710b62007-01-22 02:20:33 -08002725 my $repos_root = $self->ra->{repos_root};
2726 my $url = $self->ra->{url};
Mattias Nissler0b2af452009-07-07 01:40:02 +02002727 my $new_url = $url . $branch_from;
Eric Wong15710b62007-01-22 02:20:33 -08002728 print STDERR "Found possible branch point: ",
Simon Arlott85886162009-10-09 13:21:13 +01002729 "$new_url => ", $self->full_url, ", $r\n"
2730 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002731 $branch_from =~ s#^/##;
Mattias Nissler0b2af452009-07-07 01:40:02 +02002732 my $gs = $self->other_gs($new_url, $url,
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002733 $branch_from, $r, $self->{ref_id});
Eric Wong15710b62007-01-22 02:20:33 -08002734 my ($r0, $parent) = $gs->find_rev_before($r, 1);
Deskin Miller553589f2008-12-08 08:31:31 -05002735 {
2736 my ($base, $head);
2737 if (!defined $r0 || !defined $parent) {
2738 ($base, $head) = parse_revision_argument(0, $r);
2739 } else {
2740 if ($r0 < $r) {
2741 $gs->ra->get_log([$gs->{path}], $r0 + 1, $r, 1,
2742 0, 1, sub { $base = $_[1] - 1 });
2743 }
2744 }
2745 if (defined $base && $base <= $r) {
Eric Wongd627de62007-04-15 03:01:29 -07002746 $gs->fetch($base, $r);
2747 }
Deskin Miller553589f2008-12-08 08:31:31 -05002748 ($r0, $parent) = $gs->find_rev_before($r, 1);
Eric Wong15710b62007-01-22 02:20:33 -08002749 }
Eric Wongef70de92007-02-01 04:12:41 -08002750 if (defined $r0 && defined $parent) {
Simon Arlott85886162009-10-09 13:21:13 +01002751 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n"
2752 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002753 my $ed;
2754 if ($self->ra->can_do_switch) {
Eric Wong2e5e2482007-02-23 02:21:59 -08002755 $self->assert_index_clean($parent);
Simon Arlott85886162009-10-09 13:21:13 +01002756 print STDERR "Following parent with do_switch\n"
2757 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002758 # do_switch works with svn/trunk >= r22312, but that
Eric Wong2b27f6c2007-01-28 04:59:05 -08002759 # is not included with SVN 1.4.3 (the latest version
Eric Wong15710b62007-01-22 02:20:33 -08002760 # at the moment), so we can't rely on it
Eric Wong83c2fcf2009-02-22 20:25:00 -08002761 $self->{last_rev} = $r0;
Eric Wong15710b62007-01-22 02:20:33 -08002762 $self->{last_commit} = $parent;
Eric Wong8841b372009-02-11 01:56:58 -08002763 $ed = SVN::Git::Fetcher->new($self, $gs->{path});
Eric Wong8a603772007-01-31 02:45:50 -08002764 $gs->ra->gs_do_switch($r0, $rev, $gs,
Eric Wong15710b62007-01-22 02:20:33 -08002765 $self->full_url, $ed)
2766 or die "SVN connection failed somewhere...\n";
Steven Walter9ff74e92007-09-28 13:24:19 -04002767 } elsif ($self->ra->trees_match($new_url, $r0,
2768 $self->full_url, $rev)) {
2769 print STDERR "Trees match:\n",
2770 " $new_url\@$r0\n",
2771 " ${\$self->full_url}\@$rev\n",
Simon Arlott85886162009-10-09 13:21:13 +01002772 "Following parent with no changes\n"
2773 unless $::_q > 1;
Steven Walter9ff74e92007-09-28 13:24:19 -04002774 $self->tmp_index_do(sub {
2775 command_noisy('read-tree', $parent);
2776 });
2777 $self->{last_commit} = $parent;
Eric Wong15710b62007-01-22 02:20:33 -08002778 } else {
Simon Arlott85886162009-10-09 13:21:13 +01002779 print STDERR "Following parent with do_update\n"
2780 unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002781 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08002782 $self->ra->gs_do_update($rev, $rev, $self, $ed)
Eric Wong15710b62007-01-22 02:20:33 -08002783 or die "SVN connection failed somewhere...\n";
2784 }
Simon Arlott85886162009-10-09 13:21:13 +01002785 print STDERR "Successfully followed parent\n" unless $::_q > 1;
Eric Wong15710b62007-01-22 02:20:33 -08002786 return $self->make_log_entry($rev, [$parent], $ed);
2787 }
Eric Wong15710b62007-01-22 02:20:33 -08002788 return undef;
2789}
2790
Eric Wong9b981fc2007-01-11 12:14:21 -08002791sub do_fetch {
Eric Wong706587f2007-01-18 17:50:01 -08002792 my ($self, $paths, $rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08002793 my $ed;
Eric Wong9b981fc2007-01-11 12:14:21 -08002794 my ($last_rev, @parents);
Eric Wongb9dffd82007-02-09 01:28:30 -08002795 if (my $lc = $self->last_commit) {
2796 # we can have a branch that was deleted, then re-added
2797 # under the same name but copied from another path, in
2798 # which case we'll have multiple parents (we don't
2799 # want to break the original ref, nor lose copypath info):
2800 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2801 push @{$log_entry->{parents}}, $lc;
2802 return $log_entry;
2803 }
Eric Wong15710b62007-01-22 02:20:33 -08002804 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002805 $last_rev = $self->{last_rev};
Eric Wongb9dffd82007-02-09 01:28:30 -08002806 $ed->{c} = $lc;
2807 @parents = ($lc);
Eric Wong9b981fc2007-01-11 12:14:21 -08002808 } else {
2809 $last_rev = $rev;
Eric Wong15710b62007-01-22 02:20:33 -08002810 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2811 return $log_entry;
2812 }
2813 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002814 }
Eric Wong8a603772007-01-31 02:45:50 -08002815 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002816 die "SVN connection failed somewhere...\n";
2817 }
2818 $self->make_log_entry($rev, \@parents, $ed);
2819}
2820
Eric Wong6111b932009-11-15 18:57:16 -08002821sub mkemptydirs {
2822 my ($self, $r) = @_;
Eric Wong6111b932009-11-15 18:57:16 -08002823
Eric Wonga5b80d92009-12-19 13:49:00 -08002824 sub scan {
2825 my ($r, $empty_dirs, $line) = @_;
2826 if (defined $r && $line =~ /^r(\d+)$/) {
2827 return 0 if $1 > $r;
2828 } elsif ($line =~ /^ \+empty_dir: (.+)$/) {
2829 $empty_dirs->{$1} = 1;
2830 } elsif ($line =~ /^ \-empty_dir: (.+)$/) {
2831 my @d = grep {m[^\Q$1\E(/|$)]} (keys %$empty_dirs);
2832 delete @$empty_dirs{@d};
2833 }
2834 1; # continue
2835 };
2836
2837 my %empty_dirs = ();
2838 my $gz_file = "$self->{dir}/unhandled.log.gz";
2839 if (-f $gz_file) {
2840 if (!$can_compress) {
2841 warn "Compress::Zlib could not be found; ",
2842 "empty directories in $gz_file will not be read\n";
2843 } else {
2844 my $gz = Compress::Zlib::gzopen($gz_file, "rb") or
2845 die "Unable to open $gz_file: $!\n";
2846 my $line;
2847 while ($gz->gzreadline($line) > 0) {
2848 scan($r, \%empty_dirs, $line) or last;
2849 }
2850 $gz->gzclose;
Eric Wong6111b932009-11-15 18:57:16 -08002851 }
2852 }
Eric Wonga5b80d92009-12-19 13:49:00 -08002853
2854 if (open my $fh, '<', "$self->{dir}/unhandled.log") {
2855 binmode $fh or croak "binmode: $!";
2856 while (<$fh>) {
2857 scan($r, \%empty_dirs, $_) or last;
2858 }
2859 close $fh;
2860 }
Eric Wong9be30ee2009-11-22 18:11:32 -08002861
2862 my $strip = qr/\A\Q$self->{path}\E(?:\/|$)/;
Eric Wong6111b932009-11-15 18:57:16 -08002863 foreach my $d (sort keys %empty_dirs) {
2864 $d = uri_decode($d);
Eric Wong9be30ee2009-11-22 18:11:32 -08002865 $d =~ s/$strip//;
Michael J. Kiwala7c42e392010-06-01 16:24:57 -05002866 next unless length($d);
Eric Wong6111b932009-11-15 18:57:16 -08002867 next if -d $d;
Michael J. Kiwala7c42e392010-06-01 16:24:57 -05002868 if (-e $d) {
Eric Wong6111b932009-11-15 18:57:16 -08002869 warn "$d exists but is not a directory\n";
2870 } else {
2871 print "creating empty directory: $d\n";
2872 mkpath([$d]);
2873 }
2874 }
2875}
2876
Eric Wong97f69872007-01-25 11:53:13 -08002877sub get_untracked {
2878 my ($self, $ed) = @_;
2879 my @out;
2880 my $h = $ed->{empty};
Eric Wong9b981fc2007-01-11 12:14:21 -08002881 foreach (sort keys %$h) {
2882 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
Eric Wong97f69872007-01-25 11:53:13 -08002883 push @out, " $act: " . uri_encode($_);
Eric Wong9b981fc2007-01-11 12:14:21 -08002884 warn "W: $act: $_\n";
2885 }
2886 foreach my $t (qw/dir_prop file_prop/) {
Eric Wong97f69872007-01-25 11:53:13 -08002887 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002888 foreach my $path (sort keys %$h) {
2889 my $ppath = $path eq '' ? '.' : $path;
2890 foreach my $prop (sort keys %{$h->{$path}}) {
Eric Wong1ce255d2007-01-14 23:21:16 -08002891 next if $SKIP_PROP{$prop};
Eric Wong9b981fc2007-01-11 12:14:21 -08002892 my $v = $h->{$path}->{$prop};
Eric Wong97f69872007-01-25 11:53:13 -08002893 my $t_ppath_prop = "$t: " .
2894 uri_encode($ppath) . ' ' .
2895 uri_encode($prop);
Eric Wong9b981fc2007-01-11 12:14:21 -08002896 if (defined $v) {
Eric Wong97f69872007-01-25 11:53:13 -08002897 push @out, " +$t_ppath_prop " .
2898 uri_encode($v);
Eric Wong9b981fc2007-01-11 12:14:21 -08002899 } else {
Eric Wong97f69872007-01-25 11:53:13 -08002900 push @out, " -$t_ppath_prop";
Eric Wong9b981fc2007-01-11 12:14:21 -08002901 }
2902 }
2903 }
2904 }
2905 foreach my $t (qw/absent_file absent_directory/) {
Eric Wong97f69872007-01-25 11:53:13 -08002906 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002907 foreach my $parent (sort keys %$h) {
2908 foreach my $path (sort @{$h->{$parent}}) {
Eric Wong97f69872007-01-25 11:53:13 -08002909 push @out, " $t: " .
2910 uri_encode("$parent/$path");
Eric Wong9b981fc2007-01-11 12:14:21 -08002911 warn "W: $t: $parent/$path ",
2912 "Insufficient permissions?\n";
2913 }
2914 }
2915 }
Eric Wong97f69872007-01-25 11:53:13 -08002916 \@out;
Eric Wong9b981fc2007-01-11 12:14:21 -08002917}
2918
Pete Harlane82f0d72009-01-17 20:10:14 -08002919# parse_svn_date(DATE)
2920# --------------------
2921# Given a date (in UTC) from Subversion, return a string in the format
2922# "<TZ Offset> <local date/time>" that Git will use.
2923#
2924# By default the parsed date will be in UTC; if $Git::SVN::_localtime
2925# is true we'll convert it to the local timezone instead.
Eric Wong1c8443b2007-01-14 02:17:00 -08002926sub parse_svn_date {
2927 my $date = shift || return '+0000 1970-01-01 00:00:00';
2928 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
Junio C Hamanob94ead72009-02-18 10:48:01 -08002929 (\d\d)\:(\d\d)\:(\d\d)\.\d*Z$/x) or
Eric Wong1c8443b2007-01-14 02:17:00 -08002930 croak "Unable to parse date: $date\n";
Pete Harlane82f0d72009-01-17 20:10:14 -08002931 my $parsed_date; # Set next.
2932
2933 if ($Git::SVN::_localtime) {
2934 # Translate the Subversion datetime to an epoch time.
2935 # Begin by switching ourselves to $date's timezone, UTC.
2936 my $old_env_TZ = $ENV{TZ};
2937 $ENV{TZ} = 'UTC';
2938
2939 my $epoch_in_UTC =
2940 POSIX::strftime('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
2941
2942 # Determine our local timezone (including DST) at the
2943 # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the
2944 # value of TZ, if any, at the time we were run.
2945 if (defined $Git::SVN::Log::TZ) {
2946 $ENV{TZ} = $Git::SVN::Log::TZ;
2947 } else {
2948 delete $ENV{TZ};
2949 }
2950
2951 my $our_TZ =
2952 POSIX::strftime('%Z', $S, $M, $H, $d, $m - 1, $Y - 1900);
2953
2954 # This converts $epoch_in_UTC into our local timezone.
2955 my ($sec, $min, $hour, $mday, $mon, $year,
2956 $wday, $yday, $isdst) = localtime($epoch_in_UTC);
2957
2958 $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
2959 $our_TZ, $year + 1900, $mon + 1,
2960 $mday, $hour, $min, $sec);
2961
2962 # Reset us to the timezone in effect when we entered
2963 # this routine.
2964 if (defined $old_env_TZ) {
2965 $ENV{TZ} = $old_env_TZ;
2966 } else {
2967 delete $ENV{TZ};
2968 }
2969 } else {
2970 $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
2971 }
2972
2973 return $parsed_date;
Eric Wong1c8443b2007-01-14 02:17:00 -08002974}
2975
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002976sub other_gs {
Mattias Nissler0b2af452009-07-07 01:40:02 +02002977 my ($self, $new_url, $url,
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002978 $branch_from, $r, $old_ref_id) = @_;
Mattias Nissler0b2af452009-07-07 01:40:02 +02002979 my $gs = Git::SVN->find_by_url($new_url, $url, $branch_from);
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002980 unless ($gs) {
2981 my $ref_id = $old_ref_id;
David D. Kilzer54fb7f92010-08-15 06:15:54 -07002982 $ref_id =~ s/\@\d+-*$//;
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002983 $ref_id .= "\@$r";
2984 # just grow a tail if we're not unique enough :x
2985 $ref_id .= '-' while find_ref($ref_id);
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002986 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
2987 if ($u =~ s#^\Q$url\E(/|$)##) {
2988 $p = $u;
2989 $u = $url;
2990 $repo_id = $self->{repo_id};
2991 }
David D. Kilzer3235b702010-08-15 06:15:55 -07002992 while (1) {
2993 # It is possible to tag two different subdirectories at
2994 # the same revision. If the url for an existing ref
2995 # does not match, we must either find a ref with a
2996 # matching url or create a new ref by growing a tail.
2997 $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1);
2998 my (undef, $max_commit) = $gs->rev_map_max(1);
2999 last if (!$max_commit);
3000 my ($url) = ::cmt_metadata($max_commit);
3001 last if ($url eq $gs->full_url);
3002 $ref_id .= '-';
3003 }
3004 print STDERR "Initializing parent: $ref_id\n" unless $::_q > 1;
Sam Vilain8e3f9b12007-06-26 19:23:59 +12003005 }
3006 $gs
3007}
3008
Mark Lodato36db1ed2009-05-14 21:27:15 -04003009sub call_authors_prog {
3010 my ($orig_author) = @_;
Mark Lodatod3d7d472009-09-12 20:33:23 -04003011 $orig_author = command_oneline('rev-parse', '--sq-quote', $orig_author);
Mark Lodato36db1ed2009-05-14 21:27:15 -04003012 my $author = `$::_authors_prog $orig_author`;
3013 if ($? != 0) {
3014 die "$::_authors_prog failed with exit code $?\n"
3015 }
3016 if ($author =~ /^\s*(.+?)\s*<(.*)>\s*$/) {
3017 my ($name, $email) = ($1, $2);
3018 $email = undef if length $2 == 0;
3019 return [$name, $email];
3020 } else {
3021 die "Author: $orig_author: $::_authors_prog returned "
3022 . "invalid author format: $author\n";
3023 }
3024}
3025
Eric Wong1c8443b2007-01-14 02:17:00 -08003026sub check_author {
3027 my ($author) = @_;
3028 if (!defined $author || length $author == 0) {
3029 $author = '(no author)';
Mark Lodato36db1ed2009-05-14 21:27:15 -04003030 }
3031 if (!defined $::users{$author}) {
3032 if (defined $::_authors_prog) {
3033 $::users{$author} = call_authors_prog($author);
3034 } elsif (defined $::_authors) {
3035 die "Author: $author not defined in $::_authors file\n";
3036 }
Eric Wong1c8443b2007-01-14 02:17:00 -08003037 }
3038 $author;
3039}
3040
Sam Vilainf1264bd2009-10-20 15:42:01 +13003041sub find_extra_svk_parents {
3042 my ($self, $ed, $tickets, $parents) = @_;
3043 # aha! svk:merge property changed...
3044 my @tickets = split "\n", $tickets;
3045 my @known_parents;
3046 for my $ticket ( @tickets ) {
3047 my ($uuid, $path, $rev) = split /:/, $ticket;
3048 if ( $uuid eq $self->ra_uuid ) {
Tuomas Suutaribf60fff2010-02-24 20:09:01 +02003049 my $url = $self->{url};
Sam Vilainf1264bd2009-10-20 15:42:01 +13003050 my $repos_root = $url;
3051 my $branch_from = $path;
3052 $branch_from =~ s{^/}{};
3053 my $gs = $self->other_gs($repos_root."/".$branch_from,
3054 $url,
3055 $branch_from,
3056 $rev,
3057 $self->{ref_id});
3058 if ( my $commit = $gs->rev_map_get($rev, $uuid) ) {
3059 # wahey! we found it, but it might be
3060 # an old one (!)
Alex Vandivere9e4c8b2009-11-29 02:20:21 -05003061 push @known_parents, [ $rev, $commit ];
Sam Vilainf1264bd2009-10-20 15:42:01 +13003062 }
3063 }
3064 }
Alex Vandivere9e4c8b2009-11-29 02:20:21 -05003065 # Ordering matters; highest-numbered commit merge tickets
3066 # first, as they may account for later merge ticket additions
3067 # or changes.
3068 @known_parents = map {$_->[1]} sort {$b->[0] <=> $a->[0]} @known_parents;
Sam Vilainf1264bd2009-10-20 15:42:01 +13003069 for my $parent ( @known_parents ) {
3070 my @cmd = ('rev-list', $parent, map { "^$_" } @$parents );
3071 my ($msg_fh, $ctx) = command_output_pipe(@cmd);
3072 my $new;
3073 while ( <$msg_fh> ) {
3074 $new=1;last;
3075 }
3076 command_close_pipe($msg_fh, $ctx);
3077 if ( $new ) {
3078 print STDERR
3079 "Found merge parent (svk:merge ticket): $parent\n";
3080 push @$parents, $parent;
3081 }
3082 }
3083}
3084
Sam Vilain7d944c32009-12-20 00:55:13 +13003085sub lookup_svn_merge {
3086 my $uuid = shift;
3087 my $url = shift;
3088 my $merge = shift;
3089
3090 my ($source, $revs) = split ":", $merge;
3091 my $path = $source;
3092 $path =~ s{^/}{};
3093 my $gs = Git::SVN->find_by_url($url.$source, $url, $path);
3094 if ( !$gs ) {
3095 warn "Couldn't find revmap for $url$source\n";
3096 return;
3097 }
3098 my @ranges = split ",", $revs;
3099 my ($tip, $tip_commit);
3100 my @merged_commit_ranges;
3101 # find the tip
3102 for my $range ( @ranges ) {
3103 my ($bottom, $top) = split "-", $range;
3104 $top ||= $bottom;
Sam Vilain33973a52009-12-20 05:22:42 +13003105 my $bottom_commit = $gs->find_rev_after( $bottom, 1, $top );
3106 my $top_commit = $gs->find_rev_before( $top, 1, $bottom );
Sam Vilain7d944c32009-12-20 00:55:13 +13003107
3108 unless ($top_commit and $bottom_commit) {
3109 warn "W:unknown path/rev in svn:mergeinfo "
3110 ."dirprop: $source:$range\n";
3111 next;
3112 }
3113
3114 push @merged_commit_ranges,
Sam Vilain33973a52009-12-20 05:22:42 +13003115 "$bottom_commit^..$top_commit";
Sam Vilain7d944c32009-12-20 00:55:13 +13003116
3117 if ( !defined $tip or $top > $tip ) {
3118 $tip = $top;
3119 $tip_commit = $top_commit;
3120 }
3121 }
3122 return ($tip_commit, @merged_commit_ranges);
3123}
Sam Vilain7a955a52009-12-20 05:26:26 +13003124
3125sub _rev_list {
3126 my ($msg_fh, $ctx) = command_output_pipe(
3127 "rev-list", @_,
3128 );
3129 my @rv;
3130 while ( <$msg_fh> ) {
3131 chomp;
3132 push @rv, $_;
3133 }
3134 command_close_pipe($msg_fh, $ctx);
3135 @rv;
3136}
3137
3138sub check_cherry_pick {
3139 my $base = shift;
3140 my $tip = shift;
Steven Waltera3c75052010-09-02 18:32:06 -04003141 my $parents = shift;
Sam Vilain7a955a52009-12-20 05:26:26 +13003142 my @ranges = @_;
3143 my %commits = map { $_ => 1 }
Steven Waltera3c75052010-09-02 18:32:06 -04003144 _rev_list("--no-merges", $tip, "--not", $base, @$parents);
Sam Vilain7a955a52009-12-20 05:26:26 +13003145 for my $range ( @ranges ) {
3146 delete @commits{_rev_list($range)};
3147 }
Andrew Myrick1cef6502010-01-06 16:25:21 -08003148 for my $commit (keys %commits) {
3149 if (has_no_changes($commit)) {
3150 delete $commits{$commit};
3151 }
3152 }
Sam Vilain7a955a52009-12-20 05:26:26 +13003153 return (keys %commits);
3154}
3155
Andrew Myrick1cef6502010-01-06 16:25:21 -08003156sub has_no_changes {
3157 my $commit = shift;
3158
3159 my @revs = split / /, command_oneline(
3160 qw(rev-list --parents -1 -m), $commit);
3161
3162 # Commits with no parents, e.g. the start of a partial branch,
3163 # have changes by definition.
3164 return 1 if (@revs < 2);
3165
3166 # Commits with multiple parents, e.g a merge, have no changes
3167 # by definition.
3168 return 0 if (@revs > 2);
3169
3170 return (command_oneline("rev-parse", "$commit^{tree}") eq
3171 command_oneline("rev-parse", "$commit~1^{tree}"));
3172}
3173
Andrew Myrick8bff7c52010-01-30 03:14:22 +00003174# The GIT_DIR environment variable is not always set until after the command
3175# line arguments are processed, so we can't memoize in a BEGIN block.
3176{
3177 my $memoized = 0;
3178
3179 sub memoize_svn_mergeinfo_functions {
3180 return if $memoized;
3181 $memoized = 1;
3182
3183 my $cache_path = "$ENV{GIT_DIR}/svn/.caches/";
3184 mkpath([$cache_path]) unless -d $cache_path;
3185
3186 tie my %lookup_svn_merge_cache => 'Memoize::Storable',
3187 "$cache_path/lookup_svn_merge.db", 'nstore';
3188 memoize 'lookup_svn_merge',
3189 SCALAR_CACHE => 'FAULT',
3190 LIST_CACHE => ['HASH' => \%lookup_svn_merge_cache],
3191 ;
3192
3193 tie my %check_cherry_pick_cache => 'Memoize::Storable',
3194 "$cache_path/check_cherry_pick.db", 'nstore';
3195 memoize 'check_cherry_pick',
3196 SCALAR_CACHE => 'FAULT',
3197 LIST_CACHE => ['HASH' => \%check_cherry_pick_cache],
3198 ;
3199
3200 tie my %has_no_changes_cache => 'Memoize::Storable',
3201 "$cache_path/has_no_changes.db", 'nstore';
3202 memoize 'has_no_changes',
3203 SCALAR_CACHE => ['HASH' => \%has_no_changes_cache],
3204 LIST_CACHE => 'FAULT',
3205 ;
3206 }
Sergey Vlasov8ac3a662010-07-18 16:17:49 +04003207
3208 sub unmemoize_svn_mergeinfo_functions {
3209 return if not $memoized;
3210 $memoized = 0;
3211
3212 Memoize::unmemoize 'lookup_svn_merge';
3213 Memoize::unmemoize 'check_cherry_pick';
3214 Memoize::unmemoize 'has_no_changes';
3215 }
James Y Knightf5549af2011-04-04 15:09:08 -04003216
3217 Memoize::memoize 'Git::SVN::repos_root';
Sergey Vlasov8ac3a662010-07-18 16:17:49 +04003218}
3219
3220END {
3221 # Force cache writeout explicitly instead of waiting for
3222 # global destruction to avoid segfault in Storable:
3223 # http://rt.cpan.org/Public/Bug/Display.html?id=36087
3224 unmemoize_svn_mergeinfo_functions();
Sam Vilain7d944c32009-12-20 00:55:13 +13003225}
3226
Sam Vilainea020cb2009-12-20 05:25:31 +13003227sub parents_exclude {
3228 my $parents = shift;
3229 my @commits = @_;
3230 return unless @commits;
3231
3232 my @excluded;
3233 my $excluded;
3234 do {
3235 my @cmd = ('rev-list', "-1", @commits, "--not", @$parents );
3236 $excluded = command_oneline(@cmd);
3237 if ( $excluded ) {
3238 my @new;
3239 my $found;
3240 for my $commit ( @commits ) {
3241 if ( $commit eq $excluded ) {
3242 push @excluded, $commit;
3243 $found++;
3244 last;
3245 }
3246 else {
3247 push @new, $commit;
3248 }
3249 }
3250 die "saw commit '$excluded' in rev-list output, "
3251 ."but we didn't ask for that commit (wanted: @commits --not @$parents)"
3252 unless $found;
3253 @commits = @new;
3254 }
3255 }
3256 while ($excluded and @commits);
3257
3258 return @excluded;
3259}
3260
3261
Sam Vilaindff589e2009-10-20 15:42:03 +13003262# note: this function should only be called if the various dirprops
3263# have actually changed
3264sub find_extra_svn_parents {
3265 my ($self, $ed, $mergeinfo, $parents) = @_;
3266 # aha! svk:merge property changed...
3267
Andrew Myrick8bff7c52010-01-30 03:14:22 +00003268 memoize_svn_mergeinfo_functions();
3269
Sam Vilaindff589e2009-10-20 15:42:03 +13003270 # We first search for merged tips which are not in our
3271 # history. Then, we figure out which git revisions are in
3272 # that tip, but not this revision. If all of those revisions
3273 # are now marked as merge, we can add the tip as a parent.
3274 my @merges = split "\n", $mergeinfo;
3275 my @merge_tips;
Tuomas Suutaribf60fff2010-02-24 20:09:01 +02003276 my $url = $self->{url};
Sam Vilain7d944c32009-12-20 00:55:13 +13003277 my $uuid = $self->ra_uuid;
Sam Vilainea020cb2009-12-20 05:25:31 +13003278 my %ranges;
Sam Vilaindff589e2009-10-20 15:42:03 +13003279 for my $merge ( @merges ) {
Sam Vilain7d944c32009-12-20 00:55:13 +13003280 my ($tip_commit, @ranges) =
3281 lookup_svn_merge( $uuid, $url, $merge );
Sam Vilaindff589e2009-10-20 15:42:03 +13003282 unless (!$tip_commit or
3283 grep { $_ eq $tip_commit } @$parents ) {
3284 push @merge_tips, $tip_commit;
Sam Vilainea020cb2009-12-20 05:25:31 +13003285 $ranges{$tip_commit} = \@ranges;
Sam Vilaindff589e2009-10-20 15:42:03 +13003286 } else {
3287 push @merge_tips, undef;
3288 }
3289 }
Sam Vilainea020cb2009-12-20 05:25:31 +13003290
3291 my %excluded = map { $_ => 1 }
3292 parents_exclude($parents, grep { defined } @merge_tips);
3293
3294 # check merge tips for new parents
3295 my @new_parents;
Sam Vilaindff589e2009-10-20 15:42:03 +13003296 for my $merge_tip ( @merge_tips ) {
3297 my $spec = shift @merges;
Sam Vilainea020cb2009-12-20 05:25:31 +13003298 next unless $merge_tip and $excluded{$merge_tip};
3299
3300 my $ranges = $ranges{$merge_tip};
3301
Sam Vilain7a955a52009-12-20 05:26:26 +13003302 # check out 'new' tips
Andrew Myrick41c01692010-01-06 16:25:22 -08003303 my $merge_base;
3304 eval {
3305 $merge_base = command_oneline(
3306 "merge-base",
3307 @$parents, $merge_tip,
3308 );
3309 };
3310 if ($@) {
3311 die "An error occurred during merge-base"
3312 unless $@->isa("Git::Error::Command");
3313
3314 warn "W: Cannot find common ancestor between ".
3315 "@$parents and $merge_tip. Ignoring merge info.\n";
3316 next;
3317 }
Sam Vilain7a955a52009-12-20 05:26:26 +13003318
3319 # double check that there are no missing non-merge commits
3320 my (@incomplete) = check_cherry_pick(
3321 $merge_base, $merge_tip,
Steven Waltera3c75052010-09-02 18:32:06 -04003322 $parents,
Sam Vilain7a955a52009-12-20 05:26:26 +13003323 @$ranges,
3324 );
3325
3326 if ( @incomplete ) {
3327 warn "W:svn cherry-pick ignored ($spec) - missing "
3328 .@incomplete." commit(s) (eg $incomplete[0])\n";
3329 } else {
3330 warn
3331 "Found merge parent (svn:mergeinfo prop): ",
3332 $merge_tip, "\n";
3333 push @new_parents, $merge_tip;
Sam Vilaindff589e2009-10-20 15:42:03 +13003334 }
Sam Vilain7a955a52009-12-20 05:26:26 +13003335 }
3336
3337 # cater for merges which merge commits from multiple branches
3338 if ( @new_parents > 1 ) {
3339 for ( my $i = 0; $i <= $#new_parents; $i++ ) {
3340 for ( my $j = 0; $j <= $#new_parents; $j++ ) {
3341 next if $i == $j;
3342 next unless $new_parents[$i];
3343 next unless $new_parents[$j];
3344 my $revs = command_oneline(
Eric Wong0fe19752009-12-22 12:15:40 -08003345 "rev-list", "-1",
3346 "$new_parents[$i]..$new_parents[$j]",
Sam Vilain7a955a52009-12-20 05:26:26 +13003347 );
3348 if ( !$revs ) {
Tuomas Suutari6a2009e2010-02-22 20:12:53 +02003349 undef($new_parents[$j]);
Sam Vilain7a955a52009-12-20 05:26:26 +13003350 }
Sam Vilaindff589e2009-10-20 15:42:03 +13003351 }
3352 }
3353 }
Sam Vilain7a955a52009-12-20 05:26:26 +13003354 push @$parents, grep { defined } @new_parents;
Sam Vilaindff589e2009-10-20 15:42:03 +13003355}
3356
Eric Wong9b981fc2007-01-11 12:14:21 -08003357sub make_log_entry {
Eric Wong97f69872007-01-25 11:53:13 -08003358 my ($self, $rev, $parents, $ed) = @_;
3359 my $untracked = $self->get_untracked($ed);
3360
Sam Vilainf1264bd2009-10-20 15:42:01 +13003361 my @parents = @$parents;
3362 my $ps = $ed->{path_strip} || "";
3363 for my $path ( grep { m/$ps/ } %{$ed->{dir_prop}} ) {
3364 my $props = $ed->{dir_prop}{$path};
3365 if ( $props->{"svk:merge"} ) {
3366 $self->find_extra_svk_parents
3367 ($ed, $props->{"svk:merge"}, \@parents);
3368 }
Sam Vilaindff589e2009-10-20 15:42:03 +13003369 if ( $props->{"svn:mergeinfo"} ) {
3370 $self->find_extra_svn_parents
3371 ($ed,
3372 $props->{"svn:mergeinfo"},
3373 \@parents);
3374 }
Sam Vilainf1264bd2009-10-20 15:42:01 +13003375 }
3376
Eric Wong9b981fc2007-01-11 12:14:21 -08003377 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08003378 print $un "r$rev\n" or croak $!;
3379 print $un $_, "\n" foreach @$untracked;
Sam Vilainf1264bd2009-10-20 15:42:01 +13003380 my %log_entry = ( parents => \@parents, revision => $rev,
Eric Wong97f69872007-01-25 11:53:13 -08003381 log => '');
Eric Wongfbcc1732007-02-06 18:35:30 -08003382
Eric Wong8a49ee92007-02-10 20:46:50 -08003383 my $headrev;
Eric Wongfbcc1732007-02-06 18:35:30 -08003384 my $logged = delete $self->{logged_rev_props};
Eric Wong8a49ee92007-02-10 20:46:50 -08003385 if (!$logged || $self->{-want_revprops}) {
Eric Wongfbcc1732007-02-06 18:35:30 -08003386 my $rp = $self->ra->rev_proplist($rev);
3387 foreach (sort keys %$rp) {
3388 my $v = $rp->{$_};
3389 if (/^svn:(author|date|log)$/) {
3390 $log_entry{$1} = $v;
Eric Wong8a49ee92007-02-10 20:46:50 -08003391 } elsif ($_ eq 'svm:headrev') {
3392 $headrev = $v;
Eric Wongfbcc1732007-02-06 18:35:30 -08003393 } else {
3394 print $un " rev_prop: ", uri_encode($_), ' ',
3395 uri_encode($v), "\n";
3396 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003397 }
Eric Wongfbcc1732007-02-06 18:35:30 -08003398 } else {
3399 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
Eric Wong9b981fc2007-01-11 12:14:21 -08003400 }
3401 close $un or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08003402
Eric Wong9b981fc2007-01-11 12:14:21 -08003403 $log_entry{date} = parse_svn_date($log_entry{date});
Eric Wong9b981fc2007-01-11 12:14:21 -08003404 $log_entry{log} .= "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08003405 my $author = $log_entry{author} = check_author($log_entry{author});
3406 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003407 : ($author, undef);
3408
3409 my ($commit_name, $commit_email) = ($name, $email);
3410 if ($_use_log_author) {
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00003411 my $name_field;
3412 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
3413 $name_field = $1;
3414 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
3415 $name_field = $1;
3416 }
3417 if (!defined $name_field) {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02003418 if (!defined $email) {
3419 $email = $name;
3420 }
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00003421 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003422 ($name, $email) = ($1, $2);
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00003423 } elsif ($name_field =~ /(.*)@/) {
3424 ($name, $email) = ($1, $name_field);
3425 } else {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02003426 ($name, $email) = ($name_field, $name_field);
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003427 }
3428 }
Eric Wong91b03282007-02-11 00:51:33 -08003429 if (defined $headrev && $self->use_svm_props) {
Eric Wongaea736c2007-02-16 19:15:21 -08003430 if ($self->rewrite_root) {
3431 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
3432 "options set!\n";
3433 }
Jay Soffian3e18ce12010-01-23 03:30:00 -05003434 if ($self->rewrite_uuid) {
3435 die "Can't have both 'useSvmProps' and 'rewriteUUID' ",
3436 "options set!\n";
3437 }
Eric Wongb3e95932009-07-11 14:13:12 -07003438 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$}i;
Eric Wongbefc9ad2007-02-17 02:53:07 -08003439 # we don't want "SVM: initializing mirror for junk" ...
3440 return undef if $r == 0;
3441 my $svm = $self->svm;
3442 if ($uuid ne $svm->{uuid}) {
Eric Wong8a49ee92007-02-10 20:46:50 -08003443 die "UUID mismatch on SVM path:\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08003444 "expected: $svm->{uuid}\n",
Eric Wong8a49ee92007-02-10 20:46:50 -08003445 " got: $uuid\n";
3446 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08003447 my $full_url = $self->full_url;
3448 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
3449 die "Failed to replace '$svm->{replace}' with ",
3450 "'$svm->{source}' in $full_url\n";
Sam Vilain18ea92b2007-02-23 12:32:29 +13003451 # throw away username for storing in records
3452 remove_username($full_url);
Eric Wong8a49ee92007-02-10 20:46:50 -08003453 $log_entry{metadata} = "$full_url\@$r $uuid";
3454 $log_entry{svm_revision} = $r;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003455 $email ||= "$author\@$uuid";
3456 $commit_email ||= "$author\@$uuid";
Eric Wong62e349d2007-02-16 19:57:29 -08003457 } elsif ($self->use_svnsync_props) {
3458 my $full_url = $self->svnsync->{url};
3459 $full_url .= "/$self->{path}" if length $self->{path};
Adam Robence118732007-04-24 18:02:07 -07003460 remove_username($full_url);
Eric Wong62e349d2007-02-16 19:57:29 -08003461 my $uuid = $self->svnsync->{uuid};
3462 $log_entry{metadata} = "$full_url\@$rev $uuid";
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003463 $email ||= "$author\@$uuid";
3464 $commit_email ||= "$author\@$uuid";
Eric Wong8a49ee92007-02-10 20:46:50 -08003465 } else {
Adam Robence118732007-04-24 18:02:07 -07003466 my $url = $self->metadata_url;
3467 remove_username($url);
Jay Soffian3e18ce12010-01-23 03:30:00 -05003468 my $uuid = $self->rewrite_uuid || $self->ra->get_uuid;
3469 $log_entry{metadata} = "$url\@$rev " . $uuid;
3470 $email ||= "$author\@" . $uuid;
3471 $commit_email ||= "$author\@" . $uuid;
Eric Wong8a49ee92007-02-10 20:46:50 -08003472 }
Eric Wongdb03cd22007-02-13 00:38:02 -08003473 $log_entry{name} = $name;
3474 $log_entry{email} = $email;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00003475 $log_entry{commit_name} = $commit_name;
3476 $log_entry{commit_email} = $commit_email;
Eric Wong9b981fc2007-01-11 12:14:21 -08003477 \%log_entry;
3478}
3479
3480sub fetch {
Eric Wong3ebe8df2007-01-25 17:35:40 -08003481 my ($self, $min_rev, $max_rev, @parents) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08003482 my ($last_rev, $last_commit) = $self->last_rev_commit;
Eric Wong3ebe8df2007-01-25 17:35:40 -08003483 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
Eric Wonge5181922007-02-08 12:53:57 -08003484 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
Eric Wong9b981fc2007-01-11 12:14:21 -08003485}
3486
3487sub set_tree_cb {
3488 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
Eric Wong490f49e2007-02-10 13:58:33 -08003489 $self->{inject_parents} = { $rev => $tree };
3490 $self->fetch(undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08003491}
3492
3493sub set_tree {
3494 my ($self, $tree) = (shift, shift);
Eric Wong1ce255d2007-01-14 23:21:16 -08003495 my $log_entry = ::get_commit_entry($tree);
Eric Wong9b981fc2007-01-11 12:14:21 -08003496 unless ($self->{last_rev}) {
Luc Heinrich0a1a1c82008-09-29 15:58:18 +02003497 ::fatal("Must have an existing revision to commit");
Eric Wong9b981fc2007-01-11 12:14:21 -08003498 }
Eric Wong61395352007-01-27 14:33:08 -08003499 my %ed_opts = ( r => $self->{last_rev},
3500 log => $log_entry->{log},
3501 ra => $self->ra,
3502 tree_a => $self->{last_commit},
3503 tree_b => $tree,
3504 editor_cb => sub {
3505 $self->set_tree_cb($log_entry, $tree, @_) },
3506 svn_path => $self->{path} );
3507 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong9b981fc2007-01-11 12:14:21 -08003508 print "No changes\nr$self->{last_rev} = $tree\n";
3509 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003510}
3511
Eric Wong060610c2007-12-08 23:27:41 -08003512sub rebuild_from_rev_db {
3513 my ($self, $path) = @_;
3514 my $r = -1;
3515 open my $fh, '<', $path or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02003516 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003517 while (<$fh>) {
3518 length($_) == 41 or croak "inconsistent size in ($_) != 41";
3519 chomp($_);
3520 ++$r;
3521 next if $_ eq ('0' x 40);
3522 $self->rev_map_set($r, $_);
3523 print "r$r = $_\n";
3524 }
3525 close $fh or croak "close: $!";
3526 unlink $path or croak "unlink: $!";
3527}
3528
Eric Wongf0ecca12007-01-30 13:11:14 -08003529sub rebuild {
3530 my ($self) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003531 my $map_path = $self->map_path;
Deskin Miller2beec892008-09-15 21:12:58 -04003532 my $partial = (-e $map_path && ! -z $map_path);
Eric Wongd6d33462007-02-16 04:05:33 -08003533 return unless ::verify_ref($self->refname.'^0');
Deskin Miller2beec892008-09-15 21:12:58 -04003534 if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
Eric Wong060610c2007-12-08 23:27:41 -08003535 my $rev_db = $self->rev_db_path;
3536 $self->rebuild_from_rev_db($rev_db);
3537 if ($self->use_svm_props) {
3538 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
3539 $self->rebuild_from_rev_db($svm_rev_db);
3540 }
3541 $self->unlink_rev_db_symlink;
Eric Wong26a62d52007-02-12 13:25:25 -08003542 return;
3543 }
Deskin Miller2beec892008-09-15 21:12:58 -04003544 print "Rebuilding $map_path ...\n" if (!$partial);
3545 my ($base_rev, $head) = ($partial ? $self->rev_map_max_norebuild(1) :
3546 (undef, undef));
Eric Wong060610c2007-12-08 23:27:41 -08003547 my ($log, $ctx) =
3548 command_output_pipe(qw/rev-list --pretty=raw --no-color --reverse/,
Deskin Miller2beec892008-09-15 21:12:58 -04003549 ($head ? "$head.." : "") . $self->refname,
3550 '--');
Jan Krüger74b1e122008-06-24 02:17:36 +02003551 my $metadata_url = $self->metadata_url;
3552 remove_username($metadata_url);
Jay Soffian3e18ce12010-01-23 03:30:00 -05003553 my $svn_uuid = $self->rewrite_uuid || $self->ra_uuid;
Sam Vilain3dfab992007-06-30 20:56:13 +12003554 my $c;
3555 while (<$log>) {
3556 if ( m{^commit ($::sha1)$} ) {
3557 $c = $1;
3558 next;
3559 }
3560 next unless s{^\s*(git-svn-id:)}{$1};
3561 my ($url, $rev, $uuid) = ::extract_metadata($_);
Sam Vilain18ea92b2007-02-23 12:32:29 +13003562 remove_username($url);
Eric Wongf0ecca12007-01-30 13:11:14 -08003563
3564 # ignore merges (from set-tree)
3565 next if (!defined $rev || !$uuid);
3566
3567 # if we merged or otherwise started elsewhere, this is
3568 # how we break out of it
Eric Wong060610c2007-12-08 23:27:41 -08003569 if (($uuid ne $svn_uuid) ||
Jan Krüger74b1e122008-06-24 02:17:36 +02003570 ($metadata_url && $url && ($url ne $metadata_url))) {
Eric Wongf0ecca12007-01-30 13:11:14 -08003571 next;
3572 }
Deskin Miller2beec892008-09-15 21:12:58 -04003573 if ($partial && $head) {
3574 print "Partial-rebuilding $map_path ...\n";
3575 print "Currently at $base_rev = $head\n";
3576 $head = undef;
3577 }
Eric Wongf0ecca12007-01-30 13:11:14 -08003578
Eric Wong060610c2007-12-08 23:27:41 -08003579 $self->rev_map_set($rev, $c);
Eric Wongf0ecca12007-01-30 13:11:14 -08003580 print "r$rev = $c\n";
3581 }
Sam Vilain3dfab992007-06-30 20:56:13 +12003582 command_close_pipe($log, $ctx);
Deskin Miller2beec892008-09-15 21:12:58 -04003583 print "Done rebuilding $map_path\n" if (!$partial || !$head);
Eric Wong060610c2007-12-08 23:27:41 -08003584 my $rev_db_path = $self->rev_db_path;
3585 if (-f $self->rev_db_path) {
3586 unlink $self->rev_db_path or croak "unlink: $!";
3587 }
3588 $self->unlink_rev_db_symlink;
Eric Wongf0ecca12007-01-30 13:11:14 -08003589}
3590
Eric Wong060610c2007-12-08 23:27:41 -08003591# rev_map:
Eric Wong9b981fc2007-01-11 12:14:21 -08003592# Tie::File seems to be prone to offset errors if revisions get sparse,
3593# it's not that fast, either. Tie::File is also not in Perl 5.6. So
3594# one of my favorite modules is out :< Next up would be one of the DBM
Eric Wong060610c2007-12-08 23:27:41 -08003595# modules, but I'm not sure which is most portable...
3596#
3597# This is the replacement for the rev_db format, which was too big
3598# and inefficient for large repositories with a lot of sparse history
3599# (mainly tags)
3600#
3601# The format is this:
3602# - 24 bytes for every record,
3603# * 4 bytes for the integer representing an SVN revision number
3604# * 20 bytes representing the sha1 of a git commit
3605# - No empty padding records like the old format
Eric Wong66ab84b2007-12-08 23:27:42 -08003606# (except the last record, which can be overwritten)
Eric Wong060610c2007-12-08 23:27:41 -08003607# - new records are written append-only since SVN revision numbers
3608# increase monotonically
3609# - lookups on SVN revision number are done via a binary search
Eric Wong66ab84b2007-12-08 23:27:42 -08003610# - Piping the file to xxd -c24 is a good way of dumping it for
3611# viewing or editing (piped back through xxd -r), should the need
3612# ever arise.
3613# - The last record can be padding revision with an all-zero sha1
3614# This is used to optimize fetch performance when using multiple
3615# "fetch" directives in .git/config
Eric Wong060610c2007-12-08 23:27:41 -08003616#
Eric Wong97ae0912007-02-11 15:21:24 -08003617# These files are disposable unless noMetadata or useSvmProps is set
Eric Wong9b981fc2007-01-11 12:14:21 -08003618
Eric Wong060610c2007-12-08 23:27:41 -08003619sub _rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08003620 my ($fh, $rev, $commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003621
Michael Weber4f7ec792008-04-18 15:12:04 +02003622 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003623 my $size = (stat($fh))[7];
3624 ($size % 24) == 0 or croak "inconsistent size: $size";
3625
Eric Wong66ab84b2007-12-08 23:27:42 -08003626 my $wr_offset = 0;
Eric Wong060610c2007-12-08 23:27:41 -08003627 if ($size > 0) {
3628 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
3629 my $read = sysread($fh, my $buf, 24) or croak "read: $!";
3630 $read == 24 or croak "read only $read bytes (!= 24)";
3631 my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08003632 if ($last_commit eq ('0' x40)) {
3633 if ($size >= 48) {
3634 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
3635 $read = sysread($fh, $buf, 24) or
3636 croak "read: $!";
3637 $read == 24 or
3638 croak "read only $read bytes (!= 24)";
3639 ($last_rev, $last_commit) =
3640 unpack(rev_map_fmt, $buf);
3641 if ($last_commit eq ('0' x40)) {
3642 croak "inconsistent .rev_map\n";
3643 }
3644 }
3645 if ($last_rev >= $rev) {
3646 croak "last_rev is higher!: $last_rev >= $rev";
3647 }
3648 $wr_offset = -24;
Eric Wong47a0b752007-01-31 05:13:30 -08003649 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003650 }
Eric Wong66ab84b2007-12-08 23:27:42 -08003651 sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003652 syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or
3653 croak "write: $!";
Eric Wong26a62d52007-02-12 13:25:25 -08003654}
3655
Ben Jackson195643f2009-06-03 20:45:52 -07003656sub _rev_map_reset {
3657 my ($fh, $rev, $commit) = @_;
3658 my $c = _rev_map_get($fh, $rev);
3659 $c eq $commit or die "_rev_map_reset(@_) commit $c does not match!\n";
3660 my $offset = sysseek($fh, 0, SEEK_CUR) or croak "seek: $!";
3661 truncate $fh, $offset or croak "truncate: $!";
3662}
3663
Eric Wong26a62d52007-02-12 13:25:25 -08003664sub mkfile {
3665 my ($path) = @_;
3666 unless (-e $path) {
3667 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
3668 mkpath([$dir]) unless -d $dir;
3669 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
3670 close $fh or die "Couldn't close (create) $path: $!\n";
3671 }
3672}
3673
Eric Wong060610c2007-12-08 23:27:41 -08003674sub rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08003675 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
Jonathan Nieder70ee0b72010-05-04 16:36:47 -07003676 defined $commit or die "missing arg3\n";
Eric Wong26a62d52007-02-12 13:25:25 -08003677 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
Eric Wong060610c2007-12-08 23:27:41 -08003678 my $db = $self->map_path($uuid);
Eric Wong26a62d52007-02-12 13:25:25 -08003679 my $db_lock = "$db.lock";
3680 my $sig;
Ben Jackson195643f2009-06-03 20:45:52 -07003681 $update_ref ||= 0;
Eric Wong26a62d52007-02-12 13:25:25 -08003682 if ($update_ref) {
3683 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
3684 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
3685 }
3686 mkfile($db);
3687
3688 $LOCKFILES{$db_lock} = 1;
3689 my $sync;
3690 # both of these options make our .rev_db file very, very important
3691 # and we can't afford to lose it because rebuild() won't work
3692 if ($self->use_svm_props || $self->no_metadata) {
3693 $sync = 1;
Eric Wong060610c2007-12-08 23:27:41 -08003694 copy($db, $db_lock) or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08003695 "Failed to copy: ",
3696 "$db => $db_lock ($!)\n";
3697 } else {
Eric Wong060610c2007-12-08 23:27:41 -08003698 rename $db, $db_lock or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08003699 "Failed to rename: ",
3700 "$db => $db_lock ($!)\n";
3701 }
Eric Wong060610c2007-12-08 23:27:41 -08003702
Eric Wong66ab84b2007-12-08 23:27:42 -08003703 sysopen(my $fh, $db_lock, O_RDWR | O_CREAT)
Eric Wong060610c2007-12-08 23:27:41 -08003704 or croak "Couldn't open $db_lock: $!\n";
Ben Jackson195643f2009-06-03 20:45:52 -07003705 $update_ref eq 'reset' ? _rev_map_reset($fh, $rev, $commit) :
3706 _rev_map_set($fh, $rev, $commit);
Eric Wong97ae0912007-02-11 15:21:24 -08003707 if ($sync) {
3708 $fh->flush or die "Couldn't flush $db_lock: $!\n";
3709 $fh->sync or die "Couldn't sync $db_lock: $!\n";
3710 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003711 close $fh or croak $!;
Eric Wong373274f2007-01-31 13:54:23 -08003712 if ($update_ref) {
Eric Wong1e889ef2007-02-16 01:45:13 -08003713 $_head = $self;
Ben Jackson195643f2009-06-03 20:45:52 -07003714 my $note = "";
3715 $note = " ($update_ref)" if ($update_ref !~ /^\d*$/);
3716 command_noisy('update-ref', '-m', "r$rev$note",
Eric Wong373274f2007-01-31 13:54:23 -08003717 $self->refname, $commit);
3718 }
Eric Wong060610c2007-12-08 23:27:41 -08003719 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
Eric Wong373274f2007-01-31 13:54:23 -08003720 "$db_lock => $db ($!)\n";
3721 delete $LOCKFILES{$db_lock};
3722 if ($update_ref) {
3723 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
3724 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
3725 kill $sig, $$ if defined $sig;
3726 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003727}
3728
Eric Wong66ab84b2007-12-08 23:27:42 -08003729# If want_commit, this will return an array of (rev, commit) where
3730# commit _must_ be a valid commit in the archive.
3731# Otherwise, it'll return the max revision (whether or not the
3732# commit is valid or just a 0x40 placeholder).
Eric Wong060610c2007-12-08 23:27:41 -08003733sub rev_map_max {
Eric Wong66ab84b2007-12-08 23:27:42 -08003734 my ($self, $want_commit) = @_;
Eric Wongd6d33462007-02-16 04:05:33 -08003735 $self->rebuild;
Deskin Miller2beec892008-09-15 21:12:58 -04003736 my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
3737 $want_commit ? ($r, $c) : $r;
3738}
3739
3740sub rev_map_max_norebuild {
3741 my ($self, $want_commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003742 my $map_path = $self->map_path;
Eric Wong66ab84b2007-12-08 23:27:42 -08003743 stat $map_path or return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08003744 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02003745 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003746 my $size = (stat($fh))[7];
3747 ($size % 24) == 0 or croak "inconsistent size: $size";
3748
3749 if ($size == 0) {
3750 close $fh or croak "close: $!";
Eric Wong66ab84b2007-12-08 23:27:42 -08003751 return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08003752 }
3753
Eric Wong66ab84b2007-12-08 23:27:42 -08003754 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003755 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003756 my ($r, $c) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08003757 if ($want_commit && $c eq ('0' x40)) {
3758 if ($size < 48) {
3759 return $want_commit ? (0, undef) : 0;
3760 }
3761 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
3762 sysread($fh, $buf, 24) == 24 or croak "read: $!";
3763 ($r, $c) = unpack(rev_map_fmt, $buf);
3764 if ($c eq ('0'x40)) {
3765 croak "Penultimate record is all-zeroes in $map_path";
3766 }
3767 }
3768 close $fh or croak "close: $!";
3769 $want_commit ? ($r, $c) : $r;
Eric Wong9c93fee2007-01-31 17:22:31 -08003770}
3771
Eric Wong060610c2007-12-08 23:27:41 -08003772sub rev_map_get {
Eric Wong26a62d52007-02-12 13:25:25 -08003773 my ($self, $rev, $uuid) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003774 my $map_path = $self->map_path($uuid);
3775 return undef unless -e $map_path;
3776
3777 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Ben Jackson195643f2009-06-03 20:45:52 -07003778 my $c = _rev_map_get($fh, $rev);
3779 close($fh) or croak "close: $!";
3780 $c
3781}
3782
3783sub _rev_map_get {
3784 my ($fh, $rev) = @_;
3785
Michael Weber4f7ec792008-04-18 15:12:04 +02003786 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003787 my $size = (stat($fh))[7];
3788 ($size % 24) == 0 or croak "inconsistent size: $size";
3789
3790 if ($size == 0) {
Eric Wong060610c2007-12-08 23:27:41 -08003791 return undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003792 }
Eric Wong060610c2007-12-08 23:27:41 -08003793
3794 my ($l, $u) = (0, $size - 24);
3795 my ($r, $c, $buf);
3796
3797 while ($l <= $u) {
3798 my $i = int(($l/24 + $u/24) / 2) * 24;
3799 sysseek($fh, $i, SEEK_SET) or croak "seek: $!";
3800 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
Eric Wongc83f4e62009-08-12 22:20:02 -07003801 my ($r, $c) = unpack(rev_map_fmt, $buf);
Eric Wong060610c2007-12-08 23:27:41 -08003802
3803 if ($r < $rev) {
3804 $l = $i + 24;
3805 } elsif ($r > $rev) {
3806 $u = $i - 24;
3807 } else { # $r == $rev
Eric Wong66ab84b2007-12-08 23:27:42 -08003808 return $c eq ('0' x 40) ? undef : $c;
Eric Wong060610c2007-12-08 23:27:41 -08003809 }
3810 }
Eric Wong060610c2007-12-08 23:27:41 -08003811 undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003812}
3813
David D Kilzer111947e2007-11-11 22:56:52 -08003814# Finds the first svn revision that exists on (if $eq_ok is true) or
3815# before $rev for the current branch. It will not search any lower
3816# than $min_rev. Returns the git commit hash and svn revision number
3817# if found, else (undef, undef).
Eric Wong15710b62007-01-22 02:20:33 -08003818sub find_rev_before {
David D Kilzer111947e2007-11-11 22:56:52 -08003819 my ($self, $rev, $eq_ok, $min_rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08003820 --$rev unless $eq_ok;
David D Kilzer111947e2007-11-11 22:56:52 -08003821 $min_rev ||= 1;
Ben Jacksonca5e8802009-06-03 20:45:51 -07003822 my $max_rev = $self->rev_map_max;
3823 $rev = $max_rev if ($rev > $max_rev);
David D Kilzer111947e2007-11-11 22:56:52 -08003824 while ($rev >= $min_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003825 if (my $c = $self->rev_map_get($rev)) {
Eric Wong15710b62007-01-22 02:20:33 -08003826 return ($rev, $c);
3827 }
3828 --$rev;
3829 }
3830 return (undef, undef);
3831}
3832
David D Kilzer111947e2007-11-11 22:56:52 -08003833# Finds the first svn revision that exists on (if $eq_ok is true) or
3834# after $rev for the current branch. It will not search any higher
3835# than $max_rev. Returns the git commit hash and svn revision number
3836# if found, else (undef, undef).
3837sub find_rev_after {
3838 my ($self, $rev, $eq_ok, $max_rev) = @_;
3839 ++$rev unless $eq_ok;
Eric Wong060610c2007-12-08 23:27:41 -08003840 $max_rev ||= $self->rev_map_max;
David D Kilzer111947e2007-11-11 22:56:52 -08003841 while ($rev <= $max_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003842 if (my $c = $self->rev_map_get($rev)) {
David D Kilzer111947e2007-11-11 22:56:52 -08003843 return ($rev, $c);
3844 }
3845 ++$rev;
3846 }
3847 return (undef, undef);
3848}
3849
Eric Wong9b981fc2007-01-11 12:14:21 -08003850sub _new {
Eric Wong706587f2007-01-18 17:50:01 -08003851 my ($class, $repo_id, $ref_id, $path) = @_;
3852 unless (defined $repo_id && length $repo_id) {
3853 $repo_id = $Git::SVN::default_repo_id;
3854 }
3855 unless (defined $ref_id && length $ref_id) {
Adam Brewster63de84a2009-08-03 21:40:38 -04003856 $_prefix = '' unless defined($_prefix);
Adam Brewster6f5748e2009-08-11 23:14:27 -04003857 $_[2] = $ref_id =
3858 "refs/remotes/$_prefix$Git::SVN::default_ref_id";
Eric Wong706587f2007-01-18 17:50:01 -08003859 }
Eric Wong7829f202008-06-28 20:40:32 -07003860 $_[1] = $repo_id;
Eric Wong706587f2007-01-18 17:50:01 -08003861 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
Adam Brewster6f5748e2009-08-11 23:14:27 -04003862
3863 # Older repos imported by us used $GIT_DIR/svn/foo instead of
3864 # $GIT_DIR/svn/refs/remotes/foo when tracking refs/remotes/foo
3865 if ($ref_id =~ m{^refs/remotes/(.*)}) {
3866 my $old_dir = "$ENV{GIT_DIR}/svn/$1";
3867 if (-d $old_dir && ! -d $dir) {
3868 $dir = $old_dir;
3869 }
3870 }
3871
Eric Wong706587f2007-01-18 17:50:01 -08003872 $_[3] = $path = '' unless (defined $path);
Adam Brewster6f5748e2009-08-11 23:14:27 -04003873 mkpath([$dir]);
Eric Wong26a62d52007-02-12 13:25:25 -08003874 bless {
3875 ref_id => $ref_id, dir => $dir, index => "$dir/index",
Eric Wong8a49ee92007-02-10 20:46:50 -08003876 path => $path, config => "$ENV{GIT_DIR}/svn/config",
Eric Wong060610c2007-12-08 23:27:41 -08003877 map_root => "$dir/.rev_map", repo_id => $repo_id }, $class;
Eric Wong26a62d52007-02-12 13:25:25 -08003878}
3879
Eric Wong060610c2007-12-08 23:27:41 -08003880# for read-only access of old .rev_db formats
3881sub unlink_rev_db_symlink {
3882 my ($self) = @_;
3883 my $link = $self->rev_db_path;
3884 $link =~ s/\.[\w-]+$// or croak "missing UUID at the end of $link";
3885 if (-l $link) {
3886 unlink $link or croak "unlink: $link failed!";
3887 }
3888}
3889
3890sub rev_db_path {
3891 my ($self, $uuid) = @_;
3892 my $db_path = $self->map_path($uuid);
3893 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
3894 or croak "map_path: $db_path does not contain '/.rev_map.' !";
3895 $db_path;
3896}
3897
3898# the new replacement for .rev_db
3899sub map_path {
Eric Wong26a62d52007-02-12 13:25:25 -08003900 my ($self, $uuid) = @_;
3901 $uuid ||= $self->ra_uuid;
Eric Wong060610c2007-12-08 23:27:41 -08003902 "$self->{map_root}.$uuid";
Eric Wong9b981fc2007-01-11 12:14:21 -08003903}
3904
Eric Wong1c8443b2007-01-14 02:17:00 -08003905sub uri_encode {
3906 my ($f) = @_;
3907 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
3908 $f
3909}
Eric Wong9b981fc2007-01-11 12:14:21 -08003910
Eric Wong6111b932009-11-15 18:57:16 -08003911sub uri_decode {
3912 my ($f) = @_;
3913 $f =~ s#%([0-9a-fA-F]{2})#chr(hex($1))#eg;
3914 $f
3915}
3916
Sam Vilain18ea92b2007-02-23 12:32:29 +13003917sub remove_username {
3918 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
3919}
3920
Eric Wongd976acf2007-01-04 00:45:03 -08003921package Git::SVN::Prompt;
3922use strict;
3923use warnings;
3924require SVN::Core;
3925use vars qw/$_no_auth_cache $_username/;
3926
3927sub simple {
Eric Wong30d055a2006-11-24 01:38:04 -08003928 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
3929 $may_save = undef if $_no_auth_cache;
3930 $default_username = $_username if defined $_username;
3931 if (defined $default_username && length $default_username) {
3932 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003933 print STDERR "Authentication realm: $realm\n";
3934 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003935 }
3936 $cred->username($default_username);
3937 } else {
Eric Wongd976acf2007-01-04 00:45:03 -08003938 username($cred, $realm, $may_save, $pool);
Eric Wong30d055a2006-11-24 01:38:04 -08003939 }
3940 $cred->password(_read_password("Password for '" .
3941 $cred->username . "': ", $realm));
3942 $cred->may_save($may_save);
3943 $SVN::_Core::SVN_NO_ERROR;
3944}
3945
Eric Wongd976acf2007-01-04 00:45:03 -08003946sub ssl_server_trust {
Eric Wong30d055a2006-11-24 01:38:04 -08003947 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
3948 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003949 print STDERR "Error validating server certificate for '$realm':\n";
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003950 {
3951 no warnings 'once';
3952 # All variables SVN::Auth::SSL::* are used only once,
3953 # so we're shutting up Perl warnings about this.
3954 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
3955 print STDERR " - The certificate is not issued ",
3956 "by a trusted authority. Use the\n",
3957 " fingerprint to validate ",
3958 "the certificate manually!\n";
3959 }
3960 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
3961 print STDERR " - The certificate hostname ",
3962 "does not match.\n";
3963 }
3964 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
3965 print STDERR " - The certificate is not yet valid.\n";
3966 }
3967 if ($failures & $SVN::Auth::SSL::EXPIRED) {
3968 print STDERR " - The certificate has expired.\n";
3969 }
3970 if ($failures & $SVN::Auth::SSL::OTHER) {
3971 print STDERR " - The certificate has ",
3972 "an unknown error.\n";
3973 }
3974 } # no warnings 'once'
Eric Wong6f729592007-01-15 20:15:55 -08003975 printf STDERR
3976 "Certificate information:\n".
Eric Wong30d055a2006-11-24 01:38:04 -08003977 " - Hostname: %s\n".
3978 " - Valid: from %s until %s\n".
3979 " - Issuer: %s\n".
3980 " - Fingerprint: %s\n",
3981 map $cert_info->$_, qw(hostname valid_from valid_until
Eric Wong6f729592007-01-15 20:15:55 -08003982 issuer_dname fingerprint);
Eric Wong30d055a2006-11-24 01:38:04 -08003983 my $choice;
3984prompt:
Eric Wong6f729592007-01-15 20:15:55 -08003985 print STDERR $may_save ?
Eric Wong30d055a2006-11-24 01:38:04 -08003986 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
3987 "(R)eject or accept (t)emporarily? ";
Eric Wong6f729592007-01-15 20:15:55 -08003988 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003989 $choice = lc(substr(<STDIN> || 'R', 0, 1));
3990 if ($choice =~ /^t$/i) {
3991 $cred->may_save(undef);
3992 } elsif ($choice =~ /^r$/i) {
3993 return -1;
3994 } elsif ($may_save && $choice =~ /^p$/i) {
3995 $cred->may_save($may_save);
3996 } else {
3997 goto prompt;
3998 }
3999 $cred->accepted_failures($failures);
4000 $SVN::_Core::SVN_NO_ERROR;
4001}
4002
Eric Wongd976acf2007-01-04 00:45:03 -08004003sub ssl_client_cert {
Eric Wong30d055a2006-11-24 01:38:04 -08004004 my ($cred, $realm, $may_save, $pool) = @_;
4005 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08004006 print STDERR "Client certificate filename: ";
4007 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08004008 chomp(my $filename = <STDIN>);
4009 $cred->cert_file($filename);
4010 $cred->may_save($may_save);
4011 $SVN::_Core::SVN_NO_ERROR;
4012}
4013
Eric Wongd976acf2007-01-04 00:45:03 -08004014sub ssl_client_cert_pw {
Eric Wong30d055a2006-11-24 01:38:04 -08004015 my ($cred, $realm, $may_save, $pool) = @_;
4016 $may_save = undef if $_no_auth_cache;
4017 $cred->password(_read_password("Password: ", $realm));
4018 $cred->may_save($may_save);
4019 $SVN::_Core::SVN_NO_ERROR;
4020}
4021
Eric Wongd976acf2007-01-04 00:45:03 -08004022sub username {
Eric Wong30d055a2006-11-24 01:38:04 -08004023 my ($cred, $realm, $may_save, $pool) = @_;
4024 $may_save = undef if $_no_auth_cache;
4025 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08004026 print STDERR "Authentication realm: $realm\n";
Eric Wong30d055a2006-11-24 01:38:04 -08004027 }
4028 my $username;
4029 if (defined $_username) {
4030 $username = $_username;
4031 } else {
Eric Wong6f729592007-01-15 20:15:55 -08004032 print STDERR "Username: ";
4033 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08004034 chomp($username = <STDIN>);
4035 }
4036 $cred->username($username);
4037 $cred->may_save($may_save);
4038 $SVN::_Core::SVN_NO_ERROR;
4039}
4040
4041sub _read_password {
4042 my ($prompt, $realm) = @_;
Eric Wong30d055a2006-11-24 01:38:04 -08004043 my $password = '';
Frank Li56a853b2010-03-02 19:47:52 +08004044 if (exists $ENV{GIT_ASKPASS}) {
4045 open(PH, "-|", $ENV{GIT_ASKPASS}, $prompt);
4046 $password = <PH>;
4047 $password =~ s/[\012\015]//; # \n\r
4048 close(PH);
4049 } else {
4050 print STDERR $prompt;
4051 STDERR->flush;
4052 require Term::ReadKey;
4053 Term::ReadKey::ReadMode('noecho');
4054 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
4055 last if $key =~ /[\012\015]/; # \n\r
4056 $password .= $key;
4057 }
4058 Term::ReadKey::ReadMode('restore');
4059 print STDERR "\n";
4060 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08004061 }
Eric Wong30d055a2006-11-24 01:38:04 -08004062 $password;
4063}
4064
Eric Wong27a1a802006-11-27 21:44:48 -08004065package SVN::Git::Fetcher;
4066use vars qw/@ISA/;
4067use strict;
4068use warnings;
4069use Carp qw/croak/;
4070use IO::File qw//;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02004071use vars qw/$_ignore_regex/;
Eric Wong27a1a802006-11-27 21:44:48 -08004072
4073# file baton members: path, mode_a, mode_b, pool, fh, blob, base
4074sub new {
Eric Wong8841b372009-02-11 01:56:58 -08004075 my ($class, $git_svn, $switch_path) = @_;
Eric Wong27a1a802006-11-27 21:44:48 -08004076 my $self = SVN::Delta::Editor->new;
4077 bless $self, $class;
Eric Wongdbc6c742009-01-11 16:51:10 -08004078 if (exists $git_svn->{last_commit}) {
4079 $self->{c} = $git_svn->{last_commit};
Eric Wong8841b372009-02-11 01:56:58 -08004080 $self->{empty_symlinks} =
4081 _mark_empty_symlinks($git_svn, $switch_path);
Eric Wongdbc6c742009-01-11 16:51:10 -08004082 }
Ben Jackson0d8bee72009-04-11 10:46:17 -07004083 $self->{ignore_regex} = eval { command_oneline('config', '--get',
4084 "svn-remote.$git_svn->{repo_id}.ignore-paths") };
Eric Wongd2a9a872006-12-12 14:47:00 -08004085 $self->{empty} = {};
4086 $self->{dir_prop} = {};
4087 $self->{file_prop} = {};
4088 $self->{absent_dir} = {};
4089 $self->{absent_file} = {};
Eric Wongef3cfaa2007-01-24 03:30:57 -08004090 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
Dmitry Statyvka3713e222010-07-30 04:30:13 +02004091 $self->{pathnameencoding} = Git::config('svn.pathnameencoding');
Eric Wong27a1a802006-11-27 21:44:48 -08004092 $self;
4093}
4094
Eric Wongdbc6c742009-01-11 16:51:10 -08004095# this uses the Ra object, so it must be called before do_{switch,update},
4096# not inside them (when the Git::SVN::Fetcher object is passed) to
4097# do_{switch,update}
4098sub _mark_empty_symlinks {
Eric Wong8841b372009-02-11 01:56:58 -08004099 my ($git_svn, $switch_path) = @_;
Eric Wong4c58a712009-01-31 17:31:12 -08004100 my $bool = Git::config_bool('svn.brokenSymlinkWorkaround');
Eric Wong48679e52009-02-27 19:40:16 -08004101 return {} if (!defined($bool)) || (defined($bool) && ! $bool);
Eric Wong4c58a712009-01-31 17:31:12 -08004102
Eric Wongdbc6c742009-01-11 16:51:10 -08004103 my %ret;
4104 my ($rev, $cmt) = $git_svn->last_rev_commit;
4105 return {} unless ($rev && $cmt);
4106
Eric Wong4c58a712009-01-31 17:31:12 -08004107 # allow the warning to be printed for each revision we fetch to
4108 # ensure the user sees it. The user can also disable the workaround
4109 # on the repository even while git svn is running and the next
4110 # revision fetched will skip this expensive function.
4111 my $printed_warning;
Eric Wongdbc6c742009-01-11 16:51:10 -08004112 chomp(my $empty_blob = `git hash-object -t blob --stdin < /dev/null`);
4113 my ($ls, $ctx) = command_output_pipe(qw/ls-tree -r -z/, $cmt);
4114 local $/ = "\0";
Eric Wong8841b372009-02-11 01:56:58 -08004115 my $pfx = defined($switch_path) ? $switch_path : $git_svn->{path};
Eric Wongdbc6c742009-01-11 16:51:10 -08004116 $pfx .= '/' if length($pfx);
4117 while (<$ls>) {
4118 chomp;
4119 s/\A100644 blob $empty_blob\t//o or next;
Eric Wong4c58a712009-01-31 17:31:12 -08004120 unless ($printed_warning) {
4121 print STDERR "Scanning for empty symlinks, ",
4122 "this may take a while if you have ",
4123 "many empty files\n",
4124 "You may disable this with `",
4125 "git config svn.brokenSymlinkWorkaround ",
4126 "false'.\n",
4127 "This may be done in a different ",
4128 "terminal without restarting ",
4129 "git svn\n";
4130 $printed_warning = 1;
4131 }
Eric Wongdbc6c742009-01-11 16:51:10 -08004132 my $path = $_;
4133 my (undef, $props) =
4134 $git_svn->ra->get_file($pfx.$path, $rev, undef);
4135 if ($props->{'svn:special'}) {
4136 $ret{$path} = 1;
4137 }
4138 }
4139 command_close_pipe($ls, $ctx);
4140 \%ret;
4141}
4142
Eric Wongb03a71a2009-01-11 18:23:38 -08004143# returns true if a given path is inside a ".git" directory
4144sub in_dot_git {
4145 $_[0] =~ m{(?:^|/)\.git(?:/|$)};
4146}
4147
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02004148# return value: 0 -- don't ignore, 1 -- ignore
4149sub is_path_ignored {
Ben Jackson0d8bee72009-04-11 10:46:17 -07004150 my ($self, $path) = @_;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02004151 return 1 if in_dot_git($path);
Ben Jackson0d8bee72009-04-11 10:46:17 -07004152 return 1 if defined($self->{ignore_regex}) &&
4153 $path =~ m!$self->{ignore_regex}!;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02004154 return 0 unless defined($_ignore_regex);
4155 return 1 if $path =~ m!$_ignore_regex!o;
4156 return 0;
4157}
4158
Eric Wong8b8fc062007-01-22 11:44:57 -08004159sub set_path_strip {
4160 my ($self, $path) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08004161 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
Eric Wong8b8fc062007-01-22 11:44:57 -08004162}
4163
Eric Wongd2a9a872006-12-12 14:47:00 -08004164sub open_root {
4165 { path => '' };
4166}
4167
4168sub open_directory {
4169 my ($self, $path, $pb, $rev) = @_;
4170 { path => $path };
4171}
4172
Eric Wong706587f2007-01-18 17:50:01 -08004173sub git_path {
4174 my ($self, $path) = @_;
Dmitry Statyvka3713e222010-07-30 04:30:13 +02004175 if (my $enc = $self->{pathnameencoding}) {
4176 require Encode;
4177 Encode::from_to($path, 'UTF-8', $enc);
4178 }
Eric Wong2b27f6c2007-01-28 04:59:05 -08004179 if ($self->{path_strip}) {
4180 $path =~ s!$self->{path_strip}!! or
4181 die "Failed to strip path '$path' ($self->{path_strip})\n";
4182 }
Eric Wong706587f2007-01-18 17:50:01 -08004183 $path;
4184}
4185
Eric Wong27a1a802006-11-27 21:44:48 -08004186sub delete_entry {
4187 my ($self, $path, $rev, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004188 return undef if $self->is_path_ignored($path);
Eric Wong4a87db02007-01-04 01:38:18 -08004189
Eric Wong706587f2007-01-18 17:50:01 -08004190 my $gpath = $self->git_path($path);
Eric Wong8a603772007-01-31 02:45:50 -08004191 return undef if ($gpath eq '');
4192
Eric Wong4a87db02007-01-04 01:38:18 -08004193 # remove entire directories.
Eric Wong4f821012009-03-28 22:10:08 -07004194 my ($tree) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
4195 =~ /\A040000 tree ([a-f\d]{40})\t\Q$gpath\E\0/);
4196 if ($tree) {
Eric Wong4a87db02007-01-04 01:38:18 -08004197 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
4198 -r --name-only -z/,
Eric Wong4f821012009-03-28 22:10:08 -07004199 $tree);
Eric Wong4a87db02007-01-04 01:38:18 -08004200 local $/ = "\0";
4201 while (<$ls>) {
Eric Wongef3cfaa2007-01-24 03:30:57 -08004202 chomp;
Eric Wong4f821012009-03-28 22:10:08 -07004203 my $rmpath = "$gpath/$_";
4204 $self->{gii}->remove($rmpath);
4205 print "\tD\t$rmpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08004206 }
Eric Wong9e3cdbd2007-02-09 12:23:47 -08004207 print "\tD\t$gpath/\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08004208 command_close_pipe($ls, $ctx);
Eric Wong4a87db02007-01-04 01:38:18 -08004209 } else {
Eric Wongef3cfaa2007-01-24 03:30:57 -08004210 $self->{gii}->remove($gpath);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08004211 print "\tD\t$gpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08004212 }
Eric Wongf9ad77a2009-12-07 20:49:38 -08004213 $self->{empty}->{$path} = 0;
Eric Wong27a1a802006-11-27 21:44:48 -08004214 undef;
4215}
4216
4217sub open_file {
4218 my ($self, $path, $pb, $rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08004219 my ($mode, $blob);
4220
Ben Jackson0d8bee72009-04-11 10:46:17 -07004221 goto out if $self->is_path_ignored($path);
Eric Wongb03a71a2009-01-11 18:23:38 -08004222
Eric Wong706587f2007-01-18 17:50:01 -08004223 my $gpath = $self->git_path($path);
Eric Wong4f821012009-03-28 22:10:08 -07004224 ($mode, $blob) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
4225 =~ /\A(\d{6}) blob ([a-f\d]{40})\t\Q$gpath\E\0/);
Eric Wong006ede52006-12-08 01:55:19 -08004226 unless (defined $mode && defined $blob) {
4227 die "$path was not found in commit $self->{c} (r$rev)\n";
4228 }
Eric Wongdbc6c742009-01-11 16:51:10 -08004229 if ($mode eq '100644' && $self->{empty_symlinks}->{$path}) {
4230 $mode = '120000';
4231 }
Eric Wongb03a71a2009-01-11 18:23:38 -08004232out:
Eric Wong27a1a802006-11-27 21:44:48 -08004233 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
Eric Wong0864e3b2006-11-28 02:50:17 -08004234 pool => SVN::Pool->new, action => 'M' };
Eric Wong27a1a802006-11-27 21:44:48 -08004235}
4236
4237sub add_file {
4238 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08004239 my $mode;
4240
Ben Jackson0d8bee72009-04-11 10:46:17 -07004241 if (!$self->is_path_ignored($path)) {
Eric Wongb03a71a2009-01-11 18:23:38 -08004242 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
4243 delete $self->{empty}->{$dir};
4244 $mode = '100644';
4245 }
4246 { path => $path, mode_a => $mode, mode_b => $mode,
Eric Wong0864e3b2006-11-28 02:50:17 -08004247 pool => SVN::Pool->new, action => 'A' };
Eric Wong27a1a802006-11-27 21:44:48 -08004248}
4249
Eric Wongd2a9a872006-12-12 14:47:00 -08004250sub add_directory {
4251 my ($self, $path, $cp_path, $cp_rev) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004252 goto out if $self->is_path_ignored($path);
Eric Wong12a6d752007-12-14 08:39:09 -08004253 my $gpath = $self->git_path($path);
4254 if ($gpath eq '') {
4255 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
4256 -r --name-only -z/,
4257 $self->{c});
4258 local $/ = "\0";
4259 while (<$ls>) {
4260 chomp;
4261 $self->{gii}->remove($_);
4262 print "\tD\t$_\n" unless $::_q;
4263 }
4264 command_close_pipe($ls, $ctx);
4265 $self->{empty}->{$path} = 0;
4266 }
Eric Wongd2a9a872006-12-12 14:47:00 -08004267 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
4268 delete $self->{empty}->{$dir};
4269 $self->{empty}->{$path} = 1;
Eric Wongb03a71a2009-01-11 18:23:38 -08004270out:
Eric Wongd2a9a872006-12-12 14:47:00 -08004271 { path => $path };
4272}
4273
4274sub change_dir_prop {
4275 my ($self, $db, $prop, $value) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004276 return undef if $self->is_path_ignored($db->{path});
Eric Wongd2a9a872006-12-12 14:47:00 -08004277 $self->{dir_prop}->{$db->{path}} ||= {};
4278 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
4279 undef;
4280}
4281
4282sub absent_directory {
4283 my ($self, $path, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004284 return undef if $self->is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08004285 $self->{absent_dir}->{$pb->{path}} ||= [];
4286 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
4287 undef;
4288}
4289
4290sub absent_file {
4291 my ($self, $path, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004292 return undef if $self->is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08004293 $self->{absent_file}->{$pb->{path}} ||= [];
4294 push @{$self->{absent_file}->{$pb->{path}}}, $path;
4295 undef;
4296}
4297
Eric Wong27a1a802006-11-27 21:44:48 -08004298sub change_file_prop {
4299 my ($self, $fb, $prop, $value) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004300 return undef if $self->is_path_ignored($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08004301 if ($prop eq 'svn:executable') {
4302 if ($fb->{mode_b} != 120000) {
4303 $fb->{mode_b} = defined $value ? 100755 : 100644;
4304 }
4305 } elsif ($prop eq 'svn:special') {
4306 $fb->{mode_b} = defined $value ? 120000 : 100644;
Eric Wongd2a9a872006-12-12 14:47:00 -08004307 } else {
4308 $self->{file_prop}->{$fb->{path}} ||= {};
4309 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
Eric Wong27a1a802006-11-27 21:44:48 -08004310 }
4311 undef;
4312}
4313
4314sub apply_textdelta {
4315 my ($self, $fb, $exp) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004316 return undef if $self->is_path_ignored($fb->{path});
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004317 my $fh = $::_repository->temp_acquire('svn_delta');
Eric Wong27a1a802006-11-27 21:44:48 -08004318 # $fh gets auto-closed() by SVN::TxDelta::apply(),
4319 # (but $base does not,) so dup() it for reading in close_file
4320 open my $dup, '<&', $fh or croak $!;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004321 my $base = $::_repository->temp_acquire('git_blob');
Eric Wongb03a71a2009-01-11 18:23:38 -08004322
Eric Wong27a1a802006-11-27 21:44:48 -08004323 if ($fb->{blob}) {
Eric Wongbaf5fa82009-01-11 16:51:11 -08004324 my ($base_is_link, $size);
4325
Eric Wongdbc6c742009-01-11 16:51:10 -08004326 if ($fb->{mode_a} eq '120000' &&
4327 ! $self->{empty_symlinks}->{$fb->{path}}) {
4328 print $base 'link ' or die "print $!\n";
Eric Wongbaf5fa82009-01-11 16:51:11 -08004329 $base_is_link = 1;
Eric Wongdbc6c742009-01-11 16:51:10 -08004330 }
Eric Wongbaf5fa82009-01-11 16:51:11 -08004331 retry:
4332 $size = $::_repository->cat_blob($fb->{blob}, $base);
Junio C Hamanod683a0e2008-05-27 23:33:22 -07004333 die "Failed to read object $fb->{blob}" if ($size < 0);
Eric Wong27a1a802006-11-27 21:44:48 -08004334
4335 if (defined $exp) {
4336 seek $base, 0, 0 or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08004337 my $got = ::md5sum($base);
Eric Wongbaf5fa82009-01-11 16:51:11 -08004338 if ($got ne $exp) {
4339 my $err = "Checksum mismatch: ".
4340 "$fb->{path} $fb->{blob}\n" .
4341 "expected: $exp\n" .
4342 " got: $got\n";
4343 if ($base_is_link) {
4344 warn $err,
4345 "Retrying... (possibly ",
4346 "a bad symlink from SVN)\n";
4347 $::_repository->temp_reset($base);
4348 $base_is_link = 0;
4349 goto retry;
4350 }
4351 die $err;
4352 }
Eric Wong27a1a802006-11-27 21:44:48 -08004353 }
4354 }
4355 seek $base, 0, 0 or croak $!;
Marcus Griep0b191382008-08-12 12:00:53 -04004356 $fb->{fh} = $fh;
Eric Wong27a1a802006-11-27 21:44:48 -08004357 $fb->{base} = $base;
Marcus Griep0b191382008-08-12 12:00:53 -04004358 [ SVN::TxDelta::apply($base, $dup, undef, $fb->{path}, $fb->{pool}) ];
Eric Wong27a1a802006-11-27 21:44:48 -08004359}
4360
4361sub close_file {
4362 my ($self, $fb, $exp) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07004363 return undef if $self->is_path_ignored($fb->{path});
Eric Wongb03a71a2009-01-11 18:23:38 -08004364
Eric Wong27a1a802006-11-27 21:44:48 -08004365 my $hash;
Eric Wong706587f2007-01-18 17:50:01 -08004366 my $path = $self->git_path($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08004367 if (my $fh = $fb->{fh}) {
Eric Wong7faf0682007-05-27 15:59:01 -07004368 if (defined $exp) {
4369 seek($fh, 0, 0) or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08004370 my $got = ::md5sum($fh);
Eric Wong7faf0682007-05-27 15:59:01 -07004371 if ($got ne $exp) {
4372 die "Checksum mismatch: $path\n",
4373 "expected: $exp\n got: $got\n";
4374 }
4375 }
Eric Wong27a1a802006-11-27 21:44:48 -08004376 if ($fb->{mode_b} == 120000) {
Marcus Griep510b0942008-08-12 12:45:39 -04004377 sysseek($fh, 0, 0) or croak $!;
Eric Wongdbc6c742009-01-11 16:51:10 -08004378 my $rd = sysread($fh, my $buf, 5);
Marcus Griep510b0942008-08-12 12:45:39 -04004379
Eric Wongdbc6c742009-01-11 16:51:10 -08004380 if (!defined $rd) {
4381 croak "sysread: $!\n";
4382 } elsif ($rd == 0) {
Marcus Griep510b0942008-08-12 12:45:39 -04004383 warn "$path has mode 120000",
Eric Wongdbc6c742009-01-11 16:51:10 -08004384 " but it points to nothing\n",
4385 "converting to an empty file with mode",
4386 " 100644\n";
4387 $fb->{mode_b} = '100644';
4388 } elsif ($buf ne 'link ') {
4389 warn "$path has mode 120000",
4390 " but is not a link\n";
Marcus Griep510b0942008-08-12 12:45:39 -04004391 } else {
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004392 my $tmp_fh = $::_repository->temp_acquire(
4393 'svn_hash');
Marcus Griep510b0942008-08-12 12:45:39 -04004394 my $res;
4395 while ($res = sysread($fh, my $str, 1024)) {
4396 my $out = syswrite($tmp_fh, $str, $res);
4397 defined($out) && $out == $res
4398 or croak("write ",
Marcus Griep836ff952008-09-08 12:53:01 -04004399 Git::temp_path($tmp_fh),
Marcus Griep510b0942008-08-12 12:45:39 -04004400 ": $!\n");
4401 }
4402 defined $res or croak $!;
4403
4404 ($fh, $tmp_fh) = ($tmp_fh, $fh);
4405 Git::temp_release($tmp_fh, 1);
Eric Wong7fc35e02007-12-19 00:06:45 -08004406 }
Eric Wong27a1a802006-11-27 21:44:48 -08004407 }
Adam Robenffe256f2008-05-23 16:19:41 +02004408
Marcus Griep0b191382008-08-12 12:00:53 -04004409 $hash = $::_repository->hash_and_insert_object(
Marcus Griep836ff952008-09-08 12:53:01 -04004410 Git::temp_path($fh));
Eric Wong27a1a802006-11-27 21:44:48 -08004411 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
Marcus Griep0b191382008-08-12 12:00:53 -04004412
4413 Git::temp_release($fb->{base}, 1);
Marcus Griep510b0942008-08-12 12:45:39 -04004414 Git::temp_release($fh, 1);
Eric Wong27a1a802006-11-27 21:44:48 -08004415 } else {
4416 $hash = $fb->{blob} or die "no blob information\n";
4417 }
4418 $fb->{pool}->clear;
Eric Wongef3cfaa2007-01-24 03:30:57 -08004419 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
Eric Wong9e3cdbd2007-02-09 12:23:47 -08004420 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
Eric Wong27a1a802006-11-27 21:44:48 -08004421 undef;
4422}
4423
4424sub abort_edit {
4425 my $self = shift;
Eric Wongef3cfaa2007-01-24 03:30:57 -08004426 $self->{nr} = $self->{gii}->{nr};
4427 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08004428 $self->SUPER::abort_edit(@_);
4429}
4430
4431sub close_edit {
4432 my $self = shift;
Eric Wongdad73c02006-11-28 14:06:05 -08004433 $self->{git_commit_ok} = 1;
Eric Wongef3cfaa2007-01-24 03:30:57 -08004434 $self->{nr} = $self->{gii}->{nr};
4435 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08004436 $self->SUPER::close_edit(@_);
4437}
Eric Wong1a82e792006-06-16 02:55:13 -07004438
Eric Wonga5e0ced2006-06-12 15:23:48 -07004439package SVN::Git::Editor;
Eric Wong24e22aa2007-01-29 00:07:49 -08004440use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004441use strict;
4442use warnings;
4443use Carp qw/croak/;
4444use IO::File;
4445
4446sub new {
Eric Wong61395352007-01-27 14:33:08 -08004447 my ($class, $opts) = @_;
4448 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
4449 die "$_ required!\n" unless (defined $opts->{$_});
Eric Wonga5e0ced2006-06-12 15:23:48 -07004450 }
Eric Wong61395352007-01-27 14:33:08 -08004451
4452 my $pool = SVN::Pool->new;
4453 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
4454 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
4455 $opts->{r}, $mods);
4456
4457 # $opts->{ra} functions should not be used after this:
4458 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
4459 $opts->{editor_cb}, $pool);
4460 my $self = SVN::Delta::Editor->new(@ce, $pool);
4461 bless $self, $class;
4462 foreach (qw/svn_path r tree_a tree_b/) {
4463 $self->{$_} = $opts->{$_};
4464 }
4465 $self->{url} = $opts->{ra}->{url};
4466 $self->{mods} = $mods;
4467 $self->{types} = $types;
4468 $self->{pool} = $pool;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004469 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
4470 $self->{rm} = { };
Eric Wongd3a840d2007-01-26 01:32:45 -08004471 $self->{path_prefix} = length $self->{svn_path} ?
4472 "$self->{svn_path}/" : '';
Brad King128de652008-07-25 11:32:37 -04004473 $self->{config} = $opts->{config};
Steven Walter6abd9332010-09-24 23:51:50 -04004474 $self->{mergeinfo} = $opts->{mergeinfo};
Eric Wonga5e0ced2006-06-12 15:23:48 -07004475 return $self;
4476}
4477
Eric Wong61395352007-01-27 14:33:08 -08004478sub generate_diff {
4479 my ($tree_a, $tree_b) = @_;
4480 my @diff_tree = qw(diff-tree -z -r);
Eric Wong24e22aa2007-01-29 00:07:49 -08004481 if ($_cp_similarity) {
4482 push @diff_tree, "-C$_cp_similarity";
Eric Wong61395352007-01-27 14:33:08 -08004483 } else {
4484 push @diff_tree, '-C';
4485 }
Eric Wong24e22aa2007-01-29 00:07:49 -08004486 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
4487 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
Eric Wong61395352007-01-27 14:33:08 -08004488 push @diff_tree, $tree_a, $tree_b;
4489 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
4490 local $/ = "\0";
4491 my $state = 'meta';
4492 my @mods;
4493 while (<$diff_fh>) {
4494 chomp $_; # this gets rid of the trailing "\0"
4495 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02004496 ($::sha1)\s($::sha1)\s
Eric Wong61395352007-01-27 14:33:08 -08004497 ([MTCRAD])\d*$/xo) {
4498 push @mods, { mode_a => $1, mode_b => $2,
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02004499 sha1_a => $3, sha1_b => $4,
4500 chg => $5 };
4501 if ($5 =~ /^(?:C|R)$/) {
Eric Wong61395352007-01-27 14:33:08 -08004502 $state = 'file_a';
4503 } else {
4504 $state = 'file_b';
4505 }
4506 } elsif ($state eq 'file_a') {
4507 my $x = $mods[$#mods] or croak "Empty array\n";
4508 if ($x->{chg} !~ /^(?:C|R)$/) {
4509 croak "Error parsing $_, $x->{chg}\n";
4510 }
4511 $x->{file_a} = $_;
4512 $state = 'file_b';
4513 } elsif ($state eq 'file_b') {
4514 my $x = $mods[$#mods] or croak "Empty array\n";
4515 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
4516 croak "Error parsing $_, $x->{chg}\n";
4517 }
4518 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
4519 croak "Error parsing $_, $x->{chg}\n";
4520 }
4521 $x->{file_b} = $_;
4522 $state = 'meta';
4523 } else {
4524 croak "Error parsing $_\n";
4525 }
4526 }
4527 command_close_pipe($diff_fh, $ctx);
4528 \@mods;
4529}
4530
4531sub check_diff_paths {
4532 my ($ra, $pfx, $rev, $mods) = @_;
4533 my %types;
4534 $pfx .= '/' if length $pfx;
4535
4536 sub type_diff_paths {
4537 my ($ra, $types, $path, $rev) = @_;
4538 my @p = split m#/+#, $path;
4539 my $c = shift @p;
4540 unless (defined $types->{$c}) {
4541 $types->{$c} = $ra->check_path($c, $rev);
4542 }
4543 while (@p) {
4544 $c .= '/' . shift @p;
4545 next if defined $types->{$c};
4546 $types->{$c} = $ra->check_path($c, $rev);
4547 }
4548 }
4549
4550 foreach my $m (@$mods) {
4551 foreach my $f (qw/file_a file_b/) {
4552 next unless defined $m->{$f};
4553 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
4554 if (length $pfx.$dir && ! defined $types{$dir}) {
4555 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
4556 }
4557 }
4558 }
4559 \%types;
4560}
4561
Eric Wonga5e0ced2006-06-12 15:23:48 -07004562sub split_path {
4563 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
4564}
4565
4566sub repo_path {
Eric Wongd3a840d2007-01-26 01:32:45 -08004567 my ($self, $path) = @_;
Dmitry Statyvka3713e222010-07-30 04:30:13 +02004568 if (my $enc = $self->{pathnameencoding}) {
4569 require Encode;
4570 Encode::from_to($path, $enc, 'UTF-8');
4571 }
Eric Wongd3a840d2007-01-26 01:32:45 -08004572 $self->{path_prefix}.(defined $path ? $path : '');
Eric Wonga5e0ced2006-06-12 15:23:48 -07004573}
4574
4575sub url_path {
4576 my ($self, $path) = @_;
Eric Wong29633bb2007-07-15 21:53:50 -07004577 if ($self->{url} =~ m#^https?://#) {
Eric Wong884cce52009-07-25 02:29:28 -07004578 $path =~ s!([^~a-zA-Z0-9_./-])!uc sprintf("%%%02x",ord($1))!eg;
Eric Wong29633bb2007-07-15 21:53:50 -07004579 }
Eric Wong6e8548c2007-01-27 01:32:00 -08004580 $self->{url} . '/' . $self->repo_path($path);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004581}
4582
4583sub rmdirs {
Eric Wong61395352007-01-27 14:33:08 -08004584 my ($self) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004585 my $rm = $self->{rm};
4586 delete $rm->{''}; # we never delete the url we're tracking
4587 return unless %$rm;
4588
4589 foreach (keys %$rm) {
4590 my @d = split m#/#, $_;
4591 my $c = shift @d;
4592 $rm->{$c} = 1;
4593 while (@d) {
4594 $c .= '/' . shift @d;
4595 $rm->{$c} = 1;
4596 }
4597 }
4598 delete $rm->{$self->{svn_path}};
4599 delete $rm->{''}; # we never delete the url we're tracking
4600 return unless %$rm;
4601
Eric Wong61395352007-01-27 14:33:08 -08004602 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
4603 $self->{tree_b});
Eric Wonga5e0ced2006-06-12 15:23:48 -07004604 local $/ = "\0";
4605 while (<$fh>) {
4606 chomp;
Eric Wong747fa122006-11-24 22:38:17 -08004607 my @dn = split m#/#, $_;
Eric Wongc07eee12006-06-19 17:59:35 -07004608 while (pop @dn) {
4609 delete $rm->{join '/', @dn};
4610 }
4611 unless (%$rm) {
Eric Wong22600a22007-02-01 13:12:26 -08004612 close $fh;
Eric Wongc07eee12006-06-19 17:59:35 -07004613 return;
4614 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07004615 }
Eric Wongaef4e922006-12-15 10:59:54 -08004616 command_close_pipe($fh, $ctx);
Eric Wongc07eee12006-06-19 17:59:35 -07004617
Eric Wonga5e0ced2006-06-12 15:23:48 -07004618 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
4619 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
4620 $self->close_directory($bat->{$d}, $p);
4621 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
Eric Wong44320b92007-01-13 22:35:53 -08004622 print "\tD+\t$d/\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004623 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
4624 delete $bat->{$d};
4625 }
4626}
4627
4628sub open_or_add_dir {
4629 my ($self, $full_path, $baton) = @_;
Eric Wong6e8548c2007-01-27 01:32:00 -08004630 my $t = $self->{types}->{$full_path};
4631 if (!defined $t) {
4632 die "$full_path not known in r$self->{r} or we have a bug!\n";
4633 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004634 {
4635 no warnings 'once';
4636 # SVN::Node::none and SVN::Node::file are used only once,
4637 # so we're shutting up Perl's warnings about them.
4638 if ($t == $SVN::Node::none) {
4639 return $self->add_directory($full_path, $baton,
4640 undef, -1, $self->{pool});
4641 } elsif ($t == $SVN::Node::dir) {
4642 return $self->open_directory($full_path, $baton,
4643 $self->{r}, $self->{pool});
4644 } # no warnings 'once'
4645 print STDERR "$full_path already exists in repository at ",
4646 "r$self->{r} and it is not a directory (",
4647 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
4648 } # no warnings 'once'
Eric Wonga5e0ced2006-06-12 15:23:48 -07004649 exit 1;
4650}
4651
4652sub ensure_path {
4653 my ($self, $path) = @_;
4654 my $bat = $self->{bat};
Eric Wong6e8548c2007-01-27 01:32:00 -08004655 my $repo_path = $self->repo_path($path);
4656 return $bat->{''} unless (length $repo_path);
4657 my @p = split m#/+#, $repo_path;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004658 my $c = shift @p;
4659 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
4660 while (@p) {
4661 my $c0 = $c;
4662 $c .= '/' . shift @p;
4663 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
4664 }
4665 return $bat->{$c};
4666}
4667
Brad King128de652008-07-25 11:32:37 -04004668# Subroutine to convert a globbing pattern to a regular expression.
4669# From perl cookbook.
4670sub glob2pat {
4671 my $globstr = shift;
4672 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
4673 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
4674 return '^' . $globstr . '$';
4675}
4676
4677sub check_autoprop {
4678 my ($self, $pattern, $properties, $file, $fbat) = @_;
4679 # Convert the globbing pattern to a regular expression.
4680 my $regex = glob2pat($pattern);
4681 # Check if the pattern matches the file name.
4682 if($file =~ m/($regex)/) {
4683 # Parse the list of properties to set.
4684 my @props = split(/;/, $properties);
4685 foreach my $prop (@props) {
4686 # Parse 'name=value' syntax and set the property.
4687 if ($prop =~ /([^=]+)=(.*)/) {
4688 my ($n,$v) = ($1,$2);
4689 for ($n, $v) {
4690 s/^\s+//; s/\s+$//;
4691 }
4692 $self->change_file_prop($fbat, $n, $v);
4693 }
4694 }
4695 }
4696}
4697
4698sub apply_autoprops {
4699 my ($self, $file, $fbat) = @_;
4700 my $conf_t = ${$self->{config}}{'config'};
4701 no warnings 'once';
4702 # Check [miscellany]/enable-auto-props in svn configuration.
4703 if (SVN::_Core::svn_config_get_bool(
4704 $conf_t,
4705 $SVN::_Core::SVN_CONFIG_SECTION_MISCELLANY,
4706 $SVN::_Core::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS,
4707 0)) {
4708 # Auto-props are enabled. Enumerate them to look for matches.
4709 my $callback = sub {
4710 $self->check_autoprop($_[0], $_[1], $file, $fbat);
4711 };
4712 SVN::_Core::svn_config_enumerate(
4713 $conf_t,
4714 $SVN::_Core::SVN_CONFIG_SECTION_AUTO_PROPS,
4715 $callback);
4716 }
4717}
4718
Eric Wonga5e0ced2006-06-12 15:23:48 -07004719sub A {
Eric Wong44320b92007-01-13 22:35:53 -08004720 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004721 my ($dir, $file) = split_path($m->{file_b});
4722 my $pbat = $self->ensure_path($dir);
4723 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4724 undef, -1);
Eric Wong44320b92007-01-13 22:35:53 -08004725 print "\tA\t$m->{file_b}\n" unless $::_q;
Brad King128de652008-07-25 11:32:37 -04004726 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004727 $self->chg_file($fbat, $m);
4728 $self->close_file($fbat,undef,$self->{pool});
4729}
4730
4731sub C {
Eric Wong44320b92007-01-13 22:35:53 -08004732 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004733 my ($dir, $file) = split_path($m->{file_b});
4734 my $pbat = $self->ensure_path($dir);
4735 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4736 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08004737 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004738 $self->chg_file($fbat, $m);
4739 $self->close_file($fbat,undef,$self->{pool});
4740}
4741
4742sub delete_entry {
4743 my ($self, $path, $pbat) = @_;
4744 my $rpath = $self->repo_path($path);
4745 my ($dir, $file) = split_path($rpath);
4746 $self->{rm}->{$dir} = 1;
4747 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
4748}
4749
4750sub R {
Eric Wong44320b92007-01-13 22:35:53 -08004751 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004752 my ($dir, $file) = split_path($m->{file_b});
4753 my $pbat = $self->ensure_path($dir);
4754 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4755 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08004756 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Paul Talacko7c4d0212008-09-06 18:50:38 -07004757 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004758 $self->chg_file($fbat, $m);
4759 $self->close_file($fbat,undef,$self->{pool});
4760
4761 ($dir, $file) = split_path($m->{file_a});
4762 $pbat = $self->ensure_path($dir);
4763 $self->delete_entry($m->{file_a}, $pbat);
4764}
4765
4766sub M {
Eric Wong44320b92007-01-13 22:35:53 -08004767 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004768 my ($dir, $file) = split_path($m->{file_b});
4769 my $pbat = $self->ensure_path($dir);
4770 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
4771 $pbat,$self->{r},$self->{pool});
Eric Wong44320b92007-01-13 22:35:53 -08004772 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004773 $self->chg_file($fbat, $m);
4774 $self->close_file($fbat,undef,$self->{pool});
4775}
4776
4777sub T { shift->M(@_) }
4778
4779sub change_file_prop {
4780 my ($self, $fbat, $pname, $pval) = @_;
4781 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
4782}
4783
Steven Walter6abd9332010-09-24 23:51:50 -04004784sub change_dir_prop {
4785 my ($self, $pbat, $pname, $pval) = @_;
4786 $self->SUPER::change_dir_prop($pbat, $pname, $pval, $self->{pool});
4787}
4788
Florian Weimer214a34d2008-08-31 17:45:04 +02004789sub _chg_file_get_blob ($$$$) {
4790 my ($self, $fbat, $m, $which) = @_;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004791 my $fh = $::_repository->temp_acquire("git_blob_$which");
Florian Weimer214a34d2008-08-31 17:45:04 +02004792 if ($m->{"mode_$which"} =~ /^120/) {
4793 print $fh 'link ' or croak $!;
4794 $self->change_file_prop($fbat,'svn:special','*');
4795 } elsif ($m->{mode_a} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
4796 $self->change_file_prop($fbat,'svn:special',undef);
4797 }
4798 my $blob = $m->{"sha1_$which"};
4799 return ($fh,) if ($blob =~ /^0{40}$/);
4800 my $size = $::_repository->cat_blob($blob, $fh);
4801 croak "Failed to read object $blob" if ($size < 0);
4802 $fh->flush == 0 or croak $!;
4803 seek $fh, 0, 0 or croak $!;
4804
4805 my $exp = ::md5sum($fh);
4806 seek $fh, 0, 0 or croak $!;
4807 return ($fh, $exp);
4808}
4809
Eric Wonga5e0ced2006-06-12 15:23:48 -07004810sub chg_file {
4811 my ($self, $fbat, $m) = @_;
4812 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
4813 $self->change_file_prop($fbat,'svn:executable','*');
4814 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
4815 $self->change_file_prop($fbat,'svn:executable',undef);
4816 }
Florian Weimer8598db932008-08-31 17:47:09 +02004817 my ($fh_a, $exp_a) = _chg_file_get_blob $self, $fbat, $m, 'a';
4818 my ($fh_b, $exp_b) = _chg_file_get_blob $self, $fbat, $m, 'b';
Eric Wongf7197df2006-10-14 15:48:35 -07004819 my $pool = SVN::Pool->new;
Florian Weimer8598db932008-08-31 17:47:09 +02004820 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
4821 if (-s $fh_a) {
4822 my $txstream = SVN::TxDelta::new ($fh_a, $fh_b, $pool);
Eric Wong991255c2008-08-31 19:45:07 -07004823 my $res = SVN::TxDelta::send_txstream($txstream, @$atd, $pool);
4824 if (defined $res) {
4825 die "Unexpected result from send_txstream: $res\n",
4826 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
4827 }
Florian Weimer8598db932008-08-31 17:47:09 +02004828 } else {
4829 my $got = SVN::TxDelta::send_stream($fh_b, @$atd, $pool);
4830 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
4831 if ($got ne $exp_b);
4832 }
4833 Git::temp_release($fh_b, 1);
4834 Git::temp_release($fh_a, 1);
Eric Wongf7197df2006-10-14 15:48:35 -07004835 $pool->clear;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004836}
4837
4838sub D {
Eric Wong44320b92007-01-13 22:35:53 -08004839 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004840 my ($dir, $file) = split_path($m->{file_b});
4841 my $pbat = $self->ensure_path($dir);
Eric Wong44320b92007-01-13 22:35:53 -08004842 print "\tD\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004843 $self->delete_entry($m->{file_b}, $pbat);
4844}
4845
4846sub close_edit {
4847 my ($self) = @_;
4848 my ($p,$bat) = ($self->{pool}, $self->{bat});
4849 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
Eric Wong64427542007-05-19 02:58:37 -07004850 next if $_ eq '';
Eric Wonga5e0ced2006-06-12 15:23:48 -07004851 $self->close_directory($bat->{$_}, $p);
4852 }
Eric Wong64427542007-05-19 02:58:37 -07004853 $self->close_directory($bat->{''}, $p);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004854 $self->SUPER::close_edit($p);
4855 $p->clear;
4856}
4857
4858sub abort_edit {
4859 my ($self) = @_;
4860 $self->SUPER::abort_edit($self->{pool});
Eric Wong61395352007-01-27 14:33:08 -08004861}
4862
4863sub DESTROY {
4864 my $self = shift;
4865 $self->SUPER::DESTROY(@_);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004866 $self->{pool}->clear;
4867}
4868
Eric Wong44320b92007-01-13 22:35:53 -08004869# this drives the editor
4870sub apply_diff {
Eric Wong61395352007-01-27 14:33:08 -08004871 my ($self) = @_;
4872 my $mods = $self->{mods};
Eric Wong44320b92007-01-13 22:35:53 -08004873 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
Eric Wong6e8548c2007-01-27 01:32:00 -08004874 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
Eric Wong44320b92007-01-13 22:35:53 -08004875 my $f = $m->{chg};
4876 if (defined $o{$f}) {
4877 $self->$f($m);
4878 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004879 fatal("Invalid change type: $f");
Eric Wong44320b92007-01-13 22:35:53 -08004880 }
4881 }
Steven Walter6abd9332010-09-24 23:51:50 -04004882
4883 if (defined($self->{mergeinfo})) {
4884 $self->change_dir_prop($self->{bat}{''}, "svn:mergeinfo",
4885 $self->{mergeinfo});
4886 }
Eric Wong24e22aa2007-01-29 00:07:49 -08004887 $self->rmdirs if $_rmdir;
Eric Wong6e8548c2007-01-27 01:32:00 -08004888 if (@$mods == 0) {
Eric Wong44320b92007-01-13 22:35:53 -08004889 $self->abort_edit;
4890 } else {
4891 $self->close_edit;
4892 }
Eric Wong6e8548c2007-01-27 01:32:00 -08004893 return scalar @$mods;
Eric Wong44320b92007-01-13 22:35:53 -08004894}
4895
Eric Wongd81bf822007-01-10 01:22:38 -08004896package Git::SVN::Ra;
Eric Wong6af1db42007-02-14 16:04:10 -08004897use vars qw/@ISA $config_dir $_log_window_size/;
Eric Wongd81bf822007-01-10 01:22:38 -08004898use strict;
4899use warnings;
Eric Wonga51cdb02007-09-07 04:00:40 -07004900my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
Eric Wongd81bf822007-01-10 01:22:38 -08004901
4902BEGIN {
4903 # enforce temporary pool usage for some simple functions
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004904 no strict 'refs';
Eric Wongbf8a40b2009-01-25 15:35:52 -08004905 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root
4906 get_file/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004907 my $SUPER = "SUPER::$f";
4908 *$f = sub {
4909 my $self = shift;
4910 my $pool = SVN::Pool->new;
4911 my @ret = $self->$SUPER(@_,$pool);
4912 $pool->clear;
4913 wantarray ? @ret : $ret[0];
4914 };
Eric Wongd81bf822007-01-10 01:22:38 -08004915 }
Eric Wongd81bf822007-01-10 01:22:38 -08004916}
4917
Steven Walter9ff74e92007-09-28 13:24:19 -04004918sub _auth_providers () {
4919 [
4920 SVN::Client::get_simple_provider(),
4921 SVN::Client::get_ssl_server_trust_file_provider(),
4922 SVN::Client::get_simple_prompt_provider(
4923 \&Git::SVN::Prompt::simple, 2),
4924 SVN::Client::get_ssl_client_cert_file_provider(),
4925 SVN::Client::get_ssl_client_cert_prompt_provider(
4926 \&Git::SVN::Prompt::ssl_client_cert, 2),
Sebastian Noack77266e92008-02-25 15:56:28 +01004927 SVN::Client::get_ssl_client_cert_pw_file_provider(),
Steven Walter9ff74e92007-09-28 13:24:19 -04004928 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
4929 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
4930 SVN::Client::get_username_provider(),
4931 SVN::Client::get_ssl_server_trust_prompt_provider(
4932 \&Git::SVN::Prompt::ssl_server_trust),
4933 SVN::Client::get_username_prompt_provider(
4934 \&Git::SVN::Prompt::username, 2)
4935 ]
4936}
4937
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004938sub escape_uri_only {
4939 my ($uri) = @_;
4940 my @tmp;
4941 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -07004942 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004943 push @tmp, $_;
4944 }
4945 join('/', @tmp);
4946}
4947
4948sub escape_url {
4949 my ($url) = @_;
4950 if ($url =~ m#^(https?)://([^/]+)(.*)$#) {
4951 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
4952 $url = "$scheme://$domain$uri";
4953 }
4954 $url;
4955}
4956
Eric Wongd81bf822007-01-10 01:22:38 -08004957sub new {
4958 my ($class, $url) = @_;
Eric Wongf6f09872007-01-18 18:22:18 -08004959 $url =~ s!/+$!!;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004960 return $RA if ($RA && $RA->{url} eq $url);
Eric Wongf6f09872007-01-18 18:22:18 -08004961
josh robbd32fad22010-02-24 16:13:50 +13004962 ::_req_svn();
4963
Eric Wongd81bf822007-01-10 01:22:38 -08004964 SVN::_Core::svn_config_ensure($config_dir, undef);
Steven Walter9ff74e92007-09-28 13:24:19 -04004965 my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
Eric Wongd81bf822007-01-10 01:22:38 -08004966 my $config = SVN::Core::config_get_config($config_dir);
Eric Wong7730fbe2007-07-04 14:07:42 -07004967 $RA = undef;
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004968 my $dont_store_passwords = 1;
4969 my $conf_t = ${$config}{'config'};
4970 {
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004971 no warnings 'once';
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004972 # The usage of $SVN::_Core::SVN_CONFIG_* variables
4973 # produces warnings that variables are used only once.
4974 # I had not found the better way to shut them up, so
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004975 # the warnings of type 'once' are disabled in this block.
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004976 if (SVN::_Core::svn_config_get_bool($conf_t,
4977 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4978 $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
4979 1) == 0) {
4980 SVN::_Core::svn_auth_set_parameter($baton,
4981 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
4982 bless (\$dont_store_passwords, "_p_void"));
4983 }
4984 if (SVN::_Core::svn_config_get_bool($conf_t,
4985 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4986 $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
4987 1) == 0) {
4988 $Git::SVN::Prompt::_no_auth_cache = 1;
4989 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004990 } # no warnings 'once'
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004991 my $self = SVN::Ra->new(url => escape_url($url), auth => $baton,
Eric Wongd81bf822007-01-10 01:22:38 -08004992 config => $config,
4993 pool => SVN::Pool->new,
4994 auth_provider_callbacks => $callbacks);
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004995 $self->{url} = $url;
Eric Wongd81bf822007-01-10 01:22:38 -08004996 $self->{svn_path} = $url;
4997 $self->{repos_root} = $self->get_repos_root;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08004998 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
Eric Wong0dc03d62007-05-13 01:04:43 -07004999 $self->{cache} = { check_path => { r => 0, data => {} },
5000 get_dir => { r => 0, data => {} } };
Eric Wong5d3b7cd2007-01-29 19:16:01 -08005001 $RA = bless $self, $class;
Eric Wongd81bf822007-01-10 01:22:38 -08005002}
5003
Eric Wong0dc03d62007-05-13 01:04:43 -07005004sub check_path {
5005 my ($self, $path, $r) = @_;
5006 my $cache = $self->{cache}->{check_path};
5007 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
5008 return $cache->{data}->{$path};
5009 }
5010 my $pool = SVN::Pool->new;
5011 my $t = $self->SUPER::check_path($path, $r, $pool);
5012 $pool->clear;
5013 if ($r != $cache->{r}) {
5014 %{$cache->{data}} = ();
5015 $cache->{r} = $r;
5016 }
5017 $cache->{data}->{$path} = $t;
5018}
5019
5020sub get_dir {
5021 my ($self, $dir, $r) = @_;
5022 my $cache = $self->{cache}->{get_dir};
5023 if ($r == $cache->{r}) {
5024 if (my $x = $cache->{data}->{$dir}) {
5025 return wantarray ? @$x : $x->[0];
5026 }
5027 }
5028 my $pool = SVN::Pool->new;
5029 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
5030 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
5031 $pool->clear;
5032 if ($r != $cache->{r}) {
5033 %{$cache->{data}} = ();
5034 $cache->{r} = $r;
5035 }
5036 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
5037 wantarray ? (\%dirents, $r, $props) : \%dirents;
5038}
5039
Eric Wongd81bf822007-01-10 01:22:38 -08005040sub DESTROY {
Eric Wong5d3b7cd2007-01-29 19:16:01 -08005041 # do not call the real DESTROY since we store ourselves in $RA
Eric Wongd81bf822007-01-10 01:22:38 -08005042}
5043
Eric Wong1ef626b2009-01-17 22:11:44 -08005044# get_log(paths, start, end, limit,
5045# discover_changed_paths, strict_node_history, receiver)
Eric Wongd81bf822007-01-10 01:22:38 -08005046sub get_log {
5047 my ($self, @args) = @_;
5048 my $pool = SVN::Pool->new;
Eric Wong1ef626b2009-01-17 22:11:44 -08005049
Mattias Nissler3c49a032009-07-07 01:39:52 +02005050 # svn_log_changed_path_t objects passed to get_log are likely to be
5051 # overwritten even if only the refs are copied to an external variable,
5052 # so we should dup the structures in their entirety. Using an
5053 # externally passed pool (instead of our temporary and quickly cleared
5054 # pool in Git::SVN::Ra) does not help matters at all...
5055 my $receiver = pop @args;
Mattias Nissler0b2af452009-07-07 01:40:02 +02005056 my $prefix = "/".$self->{svn_path};
5057 $prefix =~ s#/+($)##;
5058 my $prefix_regex = qr#^\Q$prefix\E#;
Mattias Nissler3c49a032009-07-07 01:39:52 +02005059 push(@args, sub {
5060 my ($paths) = $_[0];
5061 return &$receiver(@_) unless $paths;
5062 $_[0] = ();
5063 foreach my $p (keys %$paths) {
5064 my $i = $paths->{$p};
Mattias Nissler0b2af452009-07-07 01:40:02 +02005065 # Make path relative to our url, not repos_root
5066 $p =~ s/$prefix_regex//;
5067 my %s = map { $_ => $i->$_; }
5068 qw/copyfrom_path copyfrom_rev action/;
5069 if ($s{'copyfrom_path'}) {
5070 $s{'copyfrom_path'} =~ s/$prefix_regex//;
5071 }
Mattias Nissler3c49a032009-07-07 01:39:52 +02005072 $_[0]{$p} = \%s;
5073 }
5074 &$receiver(@_);
5075 });
5076
5077
Eric Wong1ef626b2009-01-17 22:11:44 -08005078 # the limit parameter was not supported in SVN 1.1.x, so we
5079 # drop it. Therefore, the receiver callback passed to it
5080 # is made aware of this limitation by being wrapped if
5081 # the limit passed to is being wrapped.
5082 if ($SVN::Core::VERSION le '1.2.0') {
5083 my $limit = splice(@args, 3, 1);
5084 if ($limit > 0) {
5085 my $receiver = pop @args;
5086 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
5087 }
5088 }
Eric Wongd81bf822007-01-10 01:22:38 -08005089 my $ret = $self->SUPER::get_log(@args, $pool);
5090 $pool->clear;
5091 $ret;
5092}
5093
Steven Walter9ff74e92007-09-28 13:24:19 -04005094sub trees_match {
5095 my ($self, $url1, $rev1, $url2, $rev2) = @_;
5096 my $ctx = SVN::Client->new(auth => _auth_providers);
5097 my $out = IO::File->new_tmpfile;
5098
5099 # older SVN (1.1.x) doesn't take $pool as the last parameter for
5100 # $ctx->diff(), so we'll create a default one
5101 my $pool = SVN::Pool->new_default_sub;
5102
5103 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
5104 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
5105 $out->flush;
5106 my $ret = (($out->stat)[7] == 0);
5107 close $out or croak $!;
5108
5109 $ret;
5110}
5111
Eric Wongd81bf822007-01-10 01:22:38 -08005112sub get_commit_editor {
Eric Wong44320b92007-01-13 22:35:53 -08005113 my ($self, $log, $cb, $pool) = @_;
Eric Wongd81bf822007-01-10 01:22:38 -08005114 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
Eric Wong44320b92007-01-13 22:35:53 -08005115 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08005116}
5117
Eric Wongd81bf822007-01-10 01:22:38 -08005118sub gs_do_update {
Eric Wong8a603772007-01-31 02:45:50 -08005119 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
5120 my $new = ($rev_a == $rev_b);
5121 my $path = $gs->{path};
5122
Eric Wong2e5e2482007-02-23 02:21:59 -08005123 if ($new && -e $gs->{index}) {
5124 unlink $gs->{index} or die
5125 "Couldn't unlink index: $gs->{index}: $!\n";
5126 }
Eric Wongd81bf822007-01-10 01:22:38 -08005127 my $pool = SVN::Pool->new;
Eric Wong8b8fc062007-01-22 11:44:57 -08005128 $editor->set_path_strip($path);
Eric Wong2b27f6c2007-01-28 04:59:05 -08005129 my (@pc) = split m#/#, $path;
5130 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
Eric Wong8a603772007-01-31 02:45:50 -08005131 1, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08005132 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong2b27f6c2007-01-28 04:59:05 -08005133
5134 # Since we can't rely on svn_ra_reparent being available, we'll
5135 # just have to do some magic with set_path to make it so
5136 # we only want a partial path.
5137 my $sp = '';
5138 my $final = join('/', @pc);
5139 while (@pc) {
5140 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
5141 $sp .= '/' if length $sp;
5142 $sp .= shift @pc;
5143 }
5144 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
5145
Eric Wong2b27f6c2007-01-28 04:59:05 -08005146 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
5147
Eric Wongd81bf822007-01-10 01:22:38 -08005148 $reporter->finish_report($pool);
5149 $pool->clear;
5150 $editor->{git_commit_ok};
5151}
5152
Eric Wong2b27f6c2007-01-28 04:59:05 -08005153# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
5154# svn_ra_reparent didn't work before 1.4)
Eric Wongd81bf822007-01-10 01:22:38 -08005155sub gs_do_switch {
Eric Wong8a603772007-01-31 02:45:50 -08005156 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
5157 my $path = $gs->{path};
Eric Wongd81bf822007-01-10 01:22:38 -08005158 my $pool = SVN::Pool->new;
Eric Wong2b27f6c2007-01-28 04:59:05 -08005159
5160 my $full_url = $self->{url};
5161 my $old_url = $full_url;
Eric Wong2a679c72009-07-19 22:08:45 -07005162 $full_url .= '/' . $path if length $path;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08005163 my ($ra, $reparented);
Alec Berrymanad0a82b2008-09-14 17:14:16 -04005164
Eric Wong2a679c72009-07-19 22:08:45 -07005165 if ($old_url =~ m#^svn(\+ssh)?://# ||
5166 ($full_url =~ m#^https?://# &&
5167 escape_url($full_url) ne $full_url)) {
Alec Berrymanad0a82b2008-09-14 17:14:16 -04005168 $_[0] = undef;
5169 $self = undef;
5170 $RA = undef;
5171 $ra = Git::SVN::Ra->new($full_url);
5172 $ra_invalid = 1;
5173 } elsif ($old_url ne $full_url) {
5174 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, $pool);
5175 $self->{url} = $full_url;
5176 $reparented = 1;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08005177 }
Alec Berrymanad0a82b2008-09-14 17:14:16 -04005178
Eric Wong5d3b7cd2007-01-29 19:16:01 -08005179 $ra ||= $self;
Eric Wongf4392df2008-09-06 20:18:18 -07005180 $url_b = escape_url($url_b);
Eric Wong8a603772007-01-31 02:45:50 -08005181 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08005182 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong8b8fc062007-01-22 11:44:57 -08005183 $reporter->set_path('', $rev_a, 0, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08005184 $reporter->finish_report($pool);
Eric Wong2b27f6c2007-01-28 04:59:05 -08005185
Eric Wong5d3b7cd2007-01-29 19:16:01 -08005186 if ($reparented) {
5187 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
5188 $self->{url} = $old_url;
5189 }
Eric Wong2b27f6c2007-01-28 04:59:05 -08005190
Eric Wongd81bf822007-01-10 01:22:38 -08005191 $pool->clear;
5192 $editor->{git_commit_ok};
5193}
5194
Eric Wongb54a9012007-06-13 02:37:03 -07005195sub longest_common_path {
5196 my ($gsv, $globs) = @_;
Eric Wongd2ae1432007-02-07 11:50:16 -08005197 my %common;
Eric Wonge5181922007-02-08 12:53:57 -08005198 my $common_max = scalar @$gsv;
5199
5200 foreach my $gs (@$gsv) {
Eric Wongd2ae1432007-02-07 11:50:16 -08005201 my @tmp = split m#/#, $gs->{path};
5202 my $p = '';
5203 foreach (@tmp) {
5204 $p .= length($p) ? "/$_" : $_;
5205 $common{$p} ||= 0;
5206 $common{$p}++;
5207 }
5208 }
Eric Wonge5181922007-02-08 12:53:57 -08005209 $globs ||= [];
5210 $common_max += scalar @$globs;
5211 foreach my $glob (@$globs) {
5212 my @tmp = split m#/#, $glob->{path}->{left};
5213 my $p = '';
5214 foreach (@tmp) {
5215 $p .= length($p) ? "/$_" : $_;
5216 $common{$p} ||= 0;
5217 $common{$p}++;
5218 }
5219 }
5220
Eric Wongd2ae1432007-02-07 11:50:16 -08005221 my $longest_path = '';
5222 foreach (sort {length $b <=> length $a} keys %common) {
Eric Wonge5181922007-02-08 12:53:57 -08005223 if ($common{$_} == $common_max) {
Eric Wongd2ae1432007-02-07 11:50:16 -08005224 $longest_path = $_;
5225 last;
5226 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08005227 }
Eric Wongb54a9012007-06-13 02:37:03 -07005228 $longest_path;
5229}
5230
5231sub gs_fetch_loop_common {
5232 my ($self, $base, $head, $gsv, $globs) = @_;
5233 return if ($base > $head);
5234 my $inc = $_log_window_size;
5235 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
5236 my $longest_path = longest_common_path($gsv, $globs);
Eric Wonga51cdb02007-09-07 04:00:40 -07005237 my $ra_url = $self->{url};
Alex Vandiverc69700f2009-05-06 16:18:52 -04005238 my $find_trailing_edge;
Eric Wong0af9c9f2007-01-27 22:28:56 -08005239 while (1) {
Eric Wongd4eff2b2007-01-30 14:04:22 -08005240 my %revs;
Eric Wongd2ae1432007-02-07 11:50:16 -08005241 my $err;
Eric Wongf7c3fc42007-01-29 18:34:55 -08005242 my $err_handler = $SVN::Error::handler;
Eric Wongd2ae1432007-02-07 11:50:16 -08005243 $SVN::Error::handler = sub {
5244 ($err) = @_;
5245 skip_unknown_revs($err);
5246 };
5247 sub _cb {
5248 my ($paths, $r, $author, $date, $log) = @_;
Mattias Nissler3c49a032009-07-07 01:39:52 +02005249 [ $paths,
Eric Wongd2ae1432007-02-07 11:50:16 -08005250 { author => $author, date => $date, log => $log } ];
5251 }
5252 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
5253 sub { $revs{$_[1]} = _cb(@_) });
Deskin Miller99366562009-02-08 19:33:18 -05005254 if ($err) {
5255 print "Checked through r$max\r";
Alex Vandiverc69700f2009-05-06 16:18:52 -04005256 } else {
5257 $find_trailing_edge = 1;
Deskin Miller99366562009-02-08 19:33:18 -05005258 }
Alex Vandiverc69700f2009-05-06 16:18:52 -04005259 if ($err and $find_trailing_edge) {
Eric Wongd2ae1432007-02-07 11:50:16 -08005260 print STDERR "Path '$longest_path' ",
5261 "was probably deleted:\n",
5262 $err->expanded_message,
5263 "\nWill attempt to follow ",
5264 "revisions r$min .. r$max ",
5265 "committed before the deletion\n";
5266 my $hi = $max;
5267 while (--$hi >= $min) {
5268 my $ok;
5269 $self->get_log([$longest_path], $min, $hi,
5270 0, 1, 1, sub {
Alex Vandiverb6c61772009-05-06 16:18:53 -04005271 $ok = $_[1];
Eric Wongd2ae1432007-02-07 11:50:16 -08005272 $revs{$_[1]} = _cb(@_) });
5273 if ($ok) {
5274 print STDERR "r$min .. r$ok OK\n";
5275 last;
5276 }
5277 }
Alex Vandiverc69700f2009-05-06 16:18:52 -04005278 $find_trailing_edge = 0;
Eric Wongd2ae1432007-02-07 11:50:16 -08005279 }
Eric Wongd4eff2b2007-01-30 14:04:22 -08005280 $SVN::Error::handler = $err_handler;
Eric Wongfbcc1732007-02-06 18:35:30 -08005281
Eric Wonge5181922007-02-08 12:53:57 -08005282 my %exists = map { $_->{path} => $_ } @$gsv;
Eric Wongd4eff2b2007-01-30 14:04:22 -08005283 foreach my $r (sort {$a <=> $b} keys %revs) {
Eric Wongfbcc1732007-02-06 18:35:30 -08005284 my ($paths, $logged) = @{$revs{$r}};
Eric Wonge5181922007-02-08 12:53:57 -08005285
5286 foreach my $gs ($self->match_globs(\%exists, $paths,
5287 $globs, $r)) {
Eric Wong060610c2007-12-08 23:27:41 -08005288 if ($gs->rev_map_max >= $r) {
Eric Wongfbcc1732007-02-06 18:35:30 -08005289 next;
5290 }
5291 next unless $gs->match_paths($paths, $r);
5292 $gs->{logged_rev_props} = $logged;
Eric Wonge8d120b2007-02-14 16:29:52 -08005293 if (my $last_commit = $gs->last_commit) {
5294 $gs->assert_index_clean($last_commit);
5295 }
Eric Wongfbcc1732007-02-06 18:35:30 -08005296 my $log_entry = $gs->do_fetch($paths, $r);
5297 if ($log_entry) {
Eric Wong0af9c9f2007-01-27 22:28:56 -08005298 $gs->do_git_commit($log_entry);
5299 }
Eric Wong321b1842008-01-02 10:10:03 -08005300 $INDEX_FILES{$gs->{index}} = 1;
Eric Wong0af9c9f2007-01-27 22:28:56 -08005301 }
Eric Wonge5181922007-02-08 12:53:57 -08005302 foreach my $g (@$globs) {
Eric Wong93f26892007-02-11 01:20:26 -08005303 my $k = "svn-remote.$g->{remote}." .
5304 "$g->{t}-maxRev";
5305 Git::SVN::tmp_config($k, $r);
Eric Wonge5181922007-02-08 12:53:57 -08005306 }
Eric Wonga51cdb02007-09-07 04:00:40 -07005307 if ($ra_invalid) {
5308 $_[0] = undef;
5309 $self = undef;
5310 $RA = undef;
5311 $self = Git::SVN::Ra->new($ra_url);
5312 $ra_invalid = undef;
5313 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08005314 }
Eric Wong9c93fee2007-01-31 17:22:31 -08005315 # pre-fill the .rev_db since it'll eventually get filled in
5316 # with '0' x40 if something new gets committed
Eric Wonge5181922007-02-08 12:53:57 -08005317 foreach my $gs (@$gsv) {
Eric Wong66ab84b2007-12-08 23:27:42 -08005318 next if $gs->rev_map_max >= $max;
5319 next if defined $gs->rev_map_get($max);
5320 $gs->rev_map_set($max, 0 x40);
Eric Wong9c93fee2007-01-31 17:22:31 -08005321 }
Eric Wongc3560e52007-02-12 16:03:32 -08005322 foreach my $g (@$globs) {
5323 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
5324 Git::SVN::tmp_config($k, $max);
5325 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08005326 last if $max >= $head;
5327 $min = $max + 1;
5328 $max += $inc;
5329 $max = $head if ($max > $head);
5330 }
Karl Hasselström94bc9142008-02-03 17:56:18 +01005331 Git::SVN::gc();
Eric Wong0af9c9f2007-01-27 22:28:56 -08005332}
5333
Marcus Griep570d35c2008-08-08 01:41:57 -07005334sub get_dir_globbed {
5335 my ($self, $left, $depth, $r) = @_;
5336
5337 my @x = eval { $self->get_dir($left, $r) };
5338 return unless scalar @x == 3;
5339 my $dirents = $x[0];
5340 my @finalents;
5341 foreach my $de (keys %$dirents) {
5342 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
5343 if ($depth > 1) {
5344 my @args = ("$left/$de", $depth - 1, $r);
5345 foreach my $dir ($self->get_dir_globbed(@args)) {
5346 push @finalents, "$de/$dir";
5347 }
5348 } else {
5349 push @finalents, $de;
5350 }
5351 }
5352 @finalents;
5353}
5354
Eric Wonge5181922007-02-08 12:53:57 -08005355sub match_globs {
5356 my ($self, $exists, $paths, $globs, $r) = @_;
Eric Wong74a81222007-02-10 13:28:50 -08005357
5358 sub get_dir_check {
5359 my ($self, $exists, $g, $r) = @_;
Marcus Griep570d35c2008-08-08 01:41:57 -07005360
5361 my @dirs = $self->get_dir_globbed($g->{path}->{left},
5362 $g->{path}->{depth},
5363 $r);
5364
5365 foreach my $de (@dirs) {
Eric Wong74a81222007-02-10 13:28:50 -08005366 my $p = $g->{path}->full_path($de);
5367 next if $exists->{$p};
5368 next if (length $g->{path}->{right} &&
5369 ($self->check_path($p, $r) !=
5370 $SVN::Node::dir));
Jay Soffian07576202010-01-23 03:30:01 -05005371 next unless $p =~ /$g->{path}->{regex}/;
Eric Wong74a81222007-02-10 13:28:50 -08005372 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
5373 $g->{ref}->full_path($de), 1);
5374 }
5375 }
Eric Wonge5181922007-02-08 12:53:57 -08005376 foreach my $g (@$globs) {
Eric Wong74a81222007-02-10 13:28:50 -08005377 if (my $path = $paths->{"/$g->{path}->{left}"}) {
5378 if ($path->{action} =~ /^[AR]$/) {
5379 get_dir_check($self, $exists, $g, $r);
5380 }
5381 }
Eric Wonge5181922007-02-08 12:53:57 -08005382 foreach (keys %$paths) {
Eric Wong28710f72007-02-14 13:32:21 -08005383 if (/$g->{path}->{left_regex}/ &&
5384 !/$g->{path}->{regex}/) {
Eric Wong74a81222007-02-10 13:28:50 -08005385 next if $paths->{$_}->{action} !~ /^[AR]$/;
5386 get_dir_check($self, $exists, $g, $r);
5387 }
Eric Wonge5181922007-02-08 12:53:57 -08005388 next unless /$g->{path}->{regex}/;
5389 my $p = $1;
5390 my $pathname = $g->{path}->full_path($p);
5391 next if $exists->{$pathname};
Eric Wong0c1ec5a2007-04-18 00:17:33 -07005392 next if ($self->check_path($pathname, $r) !=
5393 $SVN::Node::dir);
Eric Wonge5181922007-02-08 12:53:57 -08005394 $exists->{$pathname} = Git::SVN->init(
5395 $self->{url}, $pathname, undef,
5396 $g->{ref}->full_path($p), 1);
5397 }
5398 my $c = '';
5399 foreach (split m#/#, $g->{path}->{left}) {
5400 $c .= "/$_";
5401 next unless ($paths->{$c} &&
Eric Wong74a81222007-02-10 13:28:50 -08005402 ($paths->{$c}->{action} =~ /^[AR]$/));
5403 get_dir_check($self, $exists, $g, $r);
Eric Wonge5181922007-02-08 12:53:57 -08005404 }
5405 }
5406 values %$exists;
5407}
5408
Eric Wonge6434f82007-01-23 16:29:23 -08005409sub minimize_url {
5410 my ($self) = @_;
5411 return $self->{url} if ($self->{url} eq $self->{repos_root});
5412 my $url = $self->{repos_root};
5413 my @components = split(m!/!, $self->{svn_path});
5414 my $c = '';
5415 do {
5416 $url .= "/$c" if length $c;
Eric Wong5f8b2cb2009-07-25 13:14:16 -07005417 eval {
5418 my $ra = (ref $self)->new($url);
5419 my $latest = $ra->get_latest_revnum;
5420 $ra->get_log("", $latest, 0, 1, 0, 1, sub {});
5421 };
Eric Wonge6434f82007-01-23 16:29:23 -08005422 } while ($@ && ($c = shift @components));
5423 $url;
5424}
5425
Eric Wongd81bf822007-01-10 01:22:38 -08005426sub can_do_switch {
5427 my $self = shift;
5428 unless (defined $can_do_switch) {
5429 my $pool = SVN::Pool->new;
5430 my $rep = eval {
5431 $self->do_switch(1, '', 0, $self->{url},
5432 SVN::Delta::Editor->new, $pool);
5433 };
5434 if ($@) {
5435 $can_do_switch = 0;
5436 } else {
5437 $rep->abort_report($pool);
5438 $can_do_switch = 1;
5439 }
5440 $pool->clear;
5441 }
5442 $can_do_switch;
5443}
5444
Eric Wong0af9c9f2007-01-27 22:28:56 -08005445sub skip_unknown_revs {
5446 my ($err) = @_;
5447 my $errno = $err->apr_err();
5448 # Maybe the branch we're tracking didn't
5449 # exist when the repo started, so it's
5450 # not an error if it doesn't, just continue
5451 #
5452 # Wonderfully consistent library, eh?
5453 # 160013 - svn:// and file://
5454 # 175002 - http(s)://
5455 # 175007 - http(s):// (this repo required authorization, too...)
5456 # More codes may be discovered later...
5457 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
Eric Wonga6a15a92007-03-30 17:54:48 -07005458 my $err_key = $err->expanded_message;
5459 # revision numbers change every time, filter them out
5460 $err_key =~ s/\d+/\0/g;
5461 $err_key = "$errno\0$err_key";
5462 unless ($ignored_err{$err_key}) {
5463 warn "W: Ignoring error from SVN, path probably ",
5464 "does not exist: ($errno): ",
5465 $err->expanded_message,"\n";
Eric Wongeee8a172008-01-07 02:40:40 -08005466 warn "W: Do not be alarmed at the above message ",
5467 "git-svn is just searching aggressively for ",
5468 "old history.\n",
5469 "This may take a while on large repositories\n";
Eric Wonga6a15a92007-03-30 17:54:48 -07005470 $ignored_err{$err_key} = 1;
5471 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08005472 return;
5473 }
5474 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
5475}
5476
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005477package Git::SVN::Log;
5478use strict;
5479use warnings;
5480use POSIX qw/strftime/;
Ben Waltone8717842009-02-24 14:44:49 -05005481use Time::Local;
David D Kilzer111947e2007-11-11 22:56:52 -08005482use constant commit_log_separator => ('-' x 72) . "\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005483use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
5484 %rusers $show_commit $incremental/;
5485my $l_fmt;
5486
5487sub cmt_showable {
5488 my ($c) = @_;
5489 return 1 if defined $c->{r};
Eric Wongc16d0872007-04-08 00:59:22 -07005490
5491 # big commit message got truncated by the 16k pretty buffer in rev-list
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005492 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
5493 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
Eric Wongc16d0872007-04-08 00:59:22 -07005494 @{$c->{l}} = ();
Eric Wong44320b92007-01-13 22:35:53 -08005495 my @log = command(qw/cat-file commit/, $c->{c});
Eric Wongc16d0872007-04-08 00:59:22 -07005496
5497 # shift off the headers
5498 shift @log while ($log[0] ne '');
Eric Wong44320b92007-01-13 22:35:53 -08005499 shift @log;
Eric Wongc16d0872007-04-08 00:59:22 -07005500
5501 # TODO: make $c->{l} not have a trailing newline in the future
5502 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005503
5504 (undef, $c->{r}, undef) = ::extract_metadata(
Eric Wong44320b92007-01-13 22:35:53 -08005505 (grep(/^git-svn-id: /, @log))[-1]);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005506 }
5507 return defined $c->{r};
5508}
5509
5510sub log_use_color {
Jeff Kingcd459e32007-12-11 01:28:42 -05005511 return $color || Git->repository->get_colorbool('color.diff');
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005512}
5513
5514sub git_svn_log_cmd {
Eric Wong3bc718b2007-02-13 17:09:40 -08005515 my ($r_min, $r_max, @args) = @_;
5516 my $head = 'HEAD';
Eric Wong6ed77262007-08-15 09:55:18 -07005517 my (@files, @log_opts);
Eric Wong3bc718b2007-02-13 17:09:40 -08005518 foreach my $x (@args) {
Eric Wong6ed77262007-08-15 09:55:18 -07005519 if ($x eq '--' || @files) {
5520 push @files, $x;
5521 } else {
5522 if (::verify_ref("$x^0")) {
5523 $head = $x;
5524 } else {
5525 push @log_opts, $x;
5526 }
5527 }
Eric Wong3bc718b2007-02-13 17:09:40 -08005528 }
5529
Eric Wong13c823f2007-04-08 00:59:19 -07005530 my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
5531 $gs ||= Git::SVN->_new;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005532 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
5533 $gs->refname);
5534 push @cmd, '-r' unless $non_recursive;
5535 push @cmd, qw/--raw --name-status/ if $verbose;
5536 push @cmd, '--color' if log_use_color();
Eric Wong6ed77262007-08-15 09:55:18 -07005537 push @cmd, @log_opts;
5538 if (defined $r_max && $r_max == $r_min) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005539 push @cmd, '--max-count=1';
Eric Wong060610c2007-12-08 23:27:41 -08005540 if (my $c = $gs->rev_map_get($r_max)) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005541 push @cmd, $c;
5542 }
Eric Wong6ed77262007-08-15 09:55:18 -07005543 } elsif (defined $r_max) {
David D Kilzer111947e2007-11-11 22:56:52 -08005544 if ($r_max < $r_min) {
5545 ($r_min, $r_max) = ($r_max, $r_min);
5546 }
5547 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
5548 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
5549 # If there are no commits in the range, both $c_max and $c_min
5550 # will be undefined. If there is at least 1 commit in the
5551 # range, both will be defined.
5552 return () if !defined $c_min || !defined $c_max;
5553 if ($c_min eq $c_max) {
5554 push @cmd, '--max-count=1', $c_min;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005555 } else {
David D Kilzer111947e2007-11-11 22:56:52 -08005556 push @cmd, '--boundary', "$c_min..$c_max";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005557 }
5558 }
Eric Wong6ed77262007-08-15 09:55:18 -07005559 return (@cmd, @files);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005560}
5561
5562# adapted from pager.c
5563sub config_pager {
Jonathan Nieder190c1cd2010-02-14 06:06:10 -06005564 if (! -t *STDOUT) {
5565 $ENV{GIT_PAGER_IN_USE} = 'false';
5566 $pager = undef;
5567 return;
5568 }
5569 chomp($pager = command_oneline(qw(var GIT_PAGER)));
Jonathan Niederdec543e2009-10-30 20:43:19 -05005570 if ($pager eq 'cat') {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005571 $pager = undef;
5572 }
Jeff Kingcd459e32007-12-11 01:28:42 -05005573 $ENV{GIT_PAGER_IN_USE} = defined($pager);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005574}
5575
5576sub run_pager {
Jonathan Nieder190c1cd2010-02-14 06:06:10 -06005577 return unless defined $pager;
Marcus Griep971e6282008-09-10 11:09:46 -04005578 pipe my ($rfd, $wfd) or return;
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005579 defined(my $pid = fork) or ::fatal "Can't fork: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005580 if (!$pid) {
5581 open STDOUT, '>&', $wfd or
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005582 ::fatal "Can't redirect to stdout: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005583 return;
5584 }
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005585 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005586 $ENV{LESS} ||= 'FRSX';
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005587 exec $pager or ::fatal "Can't run pager: $! ($pager)";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005588}
5589
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005590sub format_svn_date {
Ben Waltone8717842009-02-24 14:44:49 -05005591 # some systmes don't handle or mishandle %z, so be creative.
Ben Walton736e6192009-02-27 22:11:45 -05005592 my $t = shift || time;
Ben Waltone8717842009-02-24 14:44:49 -05005593 my $gm = timelocal(gmtime($t));
5594 my $sign = qw( + + - )[ $t <=> $gm ];
5595 my $gmoff = sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
5596 return strftime("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005597}
5598
5599sub parse_git_date {
5600 my ($t, $tz) = @_;
5601 # Date::Parse isn't in the standard Perl distro :(
5602 if ($tz =~ s/^\+//) {
5603 $t += tz_to_s_offset($tz);
5604 } elsif ($tz =~ s/^\-//) {
5605 $t -= tz_to_s_offset($tz);
5606 }
5607 return $t;
5608}
5609
5610sub set_local_timezone {
5611 if (defined $TZ) {
5612 $ENV{TZ} = $TZ;
5613 } else {
5614 delete $ENV{TZ};
5615 }
5616}
5617
Eric Wong21819a32007-01-27 14:38:10 -08005618sub tz_to_s_offset {
5619 my ($tz) = @_;
5620 $tz =~ s/(\d\d)$//;
5621 return ($1 * 60) + ($tz * 3600);
5622}
5623
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005624sub get_author_info {
5625 my ($dest, $author, $t, $tz) = @_;
5626 $author =~ s/(?:^\s*|\s*$)//g;
5627 $dest->{a_raw} = $author;
5628 my $au;
Eric Wong1c8443b2007-01-14 02:17:00 -08005629 if ($::_authors) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005630 $au = $rusers{$author} || undef;
5631 }
5632 if (!$au) {
5633 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
5634 }
5635 $dest->{t} = $t;
5636 $dest->{tz} = $tz;
5637 $dest->{a} = $au;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005638 $dest->{t_utc} = parse_git_date($t, $tz);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005639}
5640
5641sub process_commit {
5642 my ($c, $r_min, $r_max, $defer) = @_;
5643 if (defined $r_min && defined $r_max) {
5644 if ($r_min == $c->{r} && $r_min == $r_max) {
5645 show_commit($c);
5646 return 0;
5647 }
5648 return 1 if $r_min == $r_max;
5649 if ($r_min < $r_max) {
5650 # we need to reverse the print order
5651 return 0 if (defined $limit && --$limit < 0);
5652 push @$defer, $c;
5653 return 1;
5654 }
5655 if ($r_min != $r_max) {
5656 return 1 if ($r_min < $c->{r});
5657 return 1 if ($r_max > $c->{r});
5658 }
5659 }
5660 return 0 if (defined $limit && --$limit < 0);
5661 show_commit($c);
5662 return 1;
5663}
5664
5665sub show_commit {
5666 my $c = shift;
5667 if ($oneline) {
5668 my $x = "\n";
5669 if (my $l = $c->{l}) {
5670 while ($l->[0] =~ /^\s*$/) { shift @$l }
5671 $x = $l->[0];
5672 }
5673 $l_fmt ||= 'A' . length($c->{r});
5674 print 'r',pack($l_fmt, $c->{r}),' | ';
5675 print "$c->{c} | " if $show_commit;
5676 print $x;
5677 } else {
5678 show_commit_normal($c);
5679 }
5680}
5681
5682sub show_commit_changed_paths {
5683 my ($c) = @_;
5684 return unless $c->{changed};
5685 print "Changed paths:\n", @{$c->{changed}};
5686}
5687
5688sub show_commit_normal {
5689 my ($c) = @_;
David D Kilzer111947e2007-11-11 22:56:52 -08005690 print commit_log_separator, "r$c->{r} | ";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005691 print "$c->{c} | " if $show_commit;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005692 print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005693 my $nr_line = 0;
5694
5695 if (my $l = $c->{l}) {
5696 while ($l->[$#$l] eq "\n" && $#$l > 0
5697 && $l->[($#$l - 1)] eq "\n") {
5698 pop @$l;
5699 }
5700 $nr_line = scalar @$l;
5701 if (!$nr_line) {
5702 print "1 line\n\n\n";
5703 } else {
5704 if ($nr_line == 1) {
5705 $nr_line = '1 line';
5706 } else {
5707 $nr_line .= ' lines';
5708 }
5709 print $nr_line, "\n";
5710 show_commit_changed_paths($c);
5711 print "\n";
5712 print $_ foreach @$l;
5713 }
5714 } else {
5715 print "1 line\n";
5716 show_commit_changed_paths($c);
5717 print "\n";
5718
5719 }
Eric Wong488a63e2007-02-15 00:40:42 -08005720 foreach my $x (qw/raw stat diff/) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005721 if ($c->{$x}) {
5722 print "\n";
5723 print $_ foreach @{$c->{$x}}
5724 }
5725 }
5726}
5727
5728sub cmd_show_log {
5729 my (@args) = @_;
5730 my ($r_min, $r_max);
5731 my $r_last = -1; # prevent dupes
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08005732 set_local_timezone();
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005733 if (defined $::_revision) {
5734 if ($::_revision =~ /^(\d+):(\d+)$/) {
5735 ($r_min, $r_max) = ($1, $2);
5736 } elsif ($::_revision =~ /^\d+$/) {
5737 $r_min = $r_max = $::_revision;
5738 } else {
5739 ::fatal "-r$::_revision is not supported, use ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02005740 "standard 'git log' arguments instead";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005741 }
5742 }
5743
5744 config_pager();
Eric Wong6ed77262007-08-15 09:55:18 -07005745 @args = git_svn_log_cmd($r_min, $r_max, @args);
David D Kilzer111947e2007-11-11 22:56:52 -08005746 if (!@args) {
5747 print commit_log_separator unless $incremental || $oneline;
5748 return;
5749 }
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005750 my $log = command_output_pipe(@args);
5751 run_pager();
Eric Wong488a63e2007-02-15 00:40:42 -08005752 my (@k, $c, $d, $stat);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005753 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
5754 while (<$log>) {
Michael J Gruber9963e022011-05-20 13:16:34 +02005755 if (/^${esc_color}commit (?:- )?($::sha1_short)/o) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005756 my $cmt = $1;
5757 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
5758 $r_last = $c->{r};
5759 process_commit($c, $r_min, $r_max, \@k) or
5760 goto out;
5761 }
5762 $d = undef;
5763 $c = { c => $cmt };
5764 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
5765 get_author_info($c, $1, $2, $3);
5766 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
5767 # ignore
5768 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
5769 push @{$c->{raw}}, $_;
5770 } elsif (/^${esc_color}[ACRMDT]\t/) {
5771 # we could add $SVN->{svn_path} here, but that requires
5772 # remote access at the moment (repo_path_split)...
5773 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
5774 push @{$c->{changed}}, $_;
5775 } elsif (/^${esc_color}diff /o) {
5776 $d = 1;
5777 push @{$c->{diff}}, $_;
5778 } elsif ($d) {
5779 push @{$c->{diff}}, $_;
Eric Wong488a63e2007-02-15 00:40:42 -08005780 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
5781 $esc_color*[\+\-]*$esc_color$/x) {
5782 $stat = 1;
5783 push @{$c->{stat}}, $_;
5784 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
5785 push @{$c->{stat}}, $_;
5786 $stat = undef;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005787 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
5788 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
5789 } elsif (s/^${esc_color} //o) {
5790 push @{$c->{l}}, $_;
5791 }
5792 }
5793 if ($c && defined $c->{r} && $c->{r} != $r_last) {
5794 $r_last = $c->{r};
5795 process_commit($c, $r_min, $r_max, \@k);
5796 }
5797 if (@k) {
David D Kilzer111947e2007-11-11 22:56:52 -08005798 ($r_min, $r_max) = ($r_max, $r_min);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005799 process_commit($_, $r_min, $r_max) foreach reverse @k;
5800 }
5801out:
Eric Wongc843c462007-01-12 03:07:31 -08005802 close $log;
David D Kilzer111947e2007-11-11 22:56:52 -08005803 print commit_log_separator unless $incremental || $oneline;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005804}
5805
Tim Stoakes6fb53752008-02-10 15:21:08 +10305806sub cmd_blame {
Steven Grimm4be40382008-05-10 22:11:18 -07005807 my $path = pop;
Tim Stoakes6fb53752008-02-10 15:21:08 +10305808
5809 config_pager();
5810 run_pager();
5811
Steven Grimm4be40382008-05-10 22:11:18 -07005812 my ($fh, $ctx, $rev);
5813
5814 if ($_git_format) {
5815 ($fh, $ctx) = command_output_pipe('blame', @_, $path);
5816 while (my $line = <$fh>) {
5817 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
5818 # Uncommitted edits show up as a rev ID of
5819 # all zeros, which we can't look up with
5820 # cmt_metadata
5821 if ($1 !~ /^0+$/) {
5822 (undef, $rev, undef) =
5823 ::cmt_metadata($1);
5824 $rev = '0' if (!$rev);
5825 } else {
5826 $rev = '0';
5827 }
5828 $rev = sprintf('%-10s', $rev);
5829 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
5830 }
5831 print $line;
Tim Stoakes6fb53752008-02-10 15:21:08 +10305832 }
Steven Grimm4be40382008-05-10 22:11:18 -07005833 } else {
5834 ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD',
5835 '--', $path);
5836 my ($sha1);
5837 my %authors;
Boris Byk6ea42032009-04-11 00:32:41 +04005838 my @buffer;
5839 my %dsha; #distinct sha keys
5840
Steven Grimm4be40382008-05-10 22:11:18 -07005841 while (my $line = <$fh>) {
Boris Byk6ea42032009-04-11 00:32:41 +04005842 push @buffer, $line;
Steven Grimm4be40382008-05-10 22:11:18 -07005843 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
Boris Byk6ea42032009-04-11 00:32:41 +04005844 $dsha{$1} = 1;
5845 }
5846 }
5847
5848 my $s2r = ::cmt_sha2rev_batch([keys %dsha]);
5849
5850 foreach my $line (@buffer) {
5851 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
5852 $rev = $s2r->{$1};
5853 $rev = '0' if (!$rev)
Steven Grimm4be40382008-05-10 22:11:18 -07005854 }
5855 elsif ($line =~ /^author (.*)/) {
5856 $authors{$rev} = $1;
5857 $authors{$rev} =~ s/\s/_/g;
5858 }
5859 elsif ($line =~ /^\t(.*)$/) {
5860 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
5861 }
5862 }
Tim Stoakes6fb53752008-02-10 15:21:08 +10305863 }
5864 command_close_pipe($fh, $ctx);
5865}
5866
Eric Wong706587f2007-01-18 17:50:01 -08005867package Git::SVN::Migration;
5868# these version numbers do NOT correspond to actual version numbers
5869# of git nor git-svn. They are just relative.
5870#
5871# v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
5872#
5873# v1 layout: .git/$id/info/url, refs/remotes/$id
5874#
5875# v2 layout: .git/svn/$id/info/url, refs/remotes/$id
5876#
5877# v3 layout: .git/svn/$id, refs/remotes/$id
5878# - info/url may remain for backwards compatibility
5879# - this is what we migrate up to this layout automatically,
5880# - this will be used by git svn init on single branches
Eric Wong26a62d52007-02-12 13:25:25 -08005881# v3.1 layout (auto migrated):
5882# - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
5883# for backwards compatibility
Eric Wong706587f2007-01-18 17:50:01 -08005884#
5885# v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
5886# - this is only created for newly multi-init-ed
5887# repositories. Similar in spirit to the
5888# --use-separate-remotes option in git-clone (now default)
5889# - we do not automatically migrate to this (following
5890# the example set by core git)
Eric Wong060610c2007-12-08 23:27:41 -08005891#
5892# v5 layout: .rev_db.$UUID => .rev_map.$UUID
5893# - newer, more-efficient format that uses 24-bytes per record
5894# with no filler space.
5895# - use xxd -c24 < .rev_map.$UUID to view and debug
5896# - This is a one-way migration, repositories updated to the
5897# new format will not be able to use old git-svn without
5898# rebuilding the .rev_db. Rebuilding the rev_db is not
5899# possible if noMetadata or useSvmProps are set; but should
5900# be no problem for users that use the (sensible) defaults.
Eric Wong706587f2007-01-18 17:50:01 -08005901use strict;
5902use warnings;
5903use Carp qw/croak/;
5904use File::Path qw/mkpath/;
Eric Wong47e39c52007-01-21 04:27:09 -08005905use File::Basename qw/dirname basename/;
5906use vars qw/$_minimize/;
Eric Wong706587f2007-01-18 17:50:01 -08005907
5908sub migrate_from_v0 {
5909 my $git_dir = $ENV{GIT_DIR};
5910 return undef unless -d $git_dir;
5911 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5912 my $migrated = 0;
5913 while (<$fh>) {
5914 chomp;
5915 my ($id, $orig_ref) = ($_, $_);
5916 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
5917 next unless -f "$git_dir/$id/info/url";
5918 my $new_ref = "refs/remotes/$id";
5919 if (::verify_ref("$new_ref^0")) {
5920 print STDERR "W: $orig_ref is probably an old ",
5921 "branch used by an ancient version of ",
5922 "git-svn.\n",
5923 "However, $new_ref also exists.\n",
5924 "We will not be able ",
5925 "to use this branch until this ",
5926 "ambiguity is resolved.\n";
5927 next;
5928 }
5929 print STDERR "Migrating from v0 layout...\n" if !$migrated;
5930 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
5931 command_noisy('update-ref', $new_ref, $orig_ref);
5932 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
5933 $migrated++;
5934 }
5935 command_close_pipe($fh, $ctx);
5936 print STDERR "Done migrating from v0 layout...\n" if $migrated;
5937 $migrated;
5938}
5939
5940sub migrate_from_v1 {
5941 my $git_dir = $ENV{GIT_DIR};
5942 my $migrated = 0;
5943 return $migrated unless -d $git_dir;
5944 my $svn_dir = "$git_dir/svn";
5945
5946 # just in case somebody used 'svn' as their $id at some point...
5947 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
5948
5949 print STDERR "Migrating from a git-svn v1 layout...\n";
5950 mkpath([$svn_dir]);
5951 print STDERR "Data from a previous version of git-svn exists, but\n\t",
5952 "$svn_dir\n\t(required for this version ",
Frederik Schwarzer8f510be2008-07-14 18:30:24 +02005953 "($::VERSION) of git-svn) does not exist.\n";
Eric Wong706587f2007-01-18 17:50:01 -08005954 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5955 while (<$fh>) {
5956 my $x = $_;
5957 next unless $x =~ s#^refs/remotes/##;
5958 chomp $x;
5959 next unless -f "$git_dir/$x/info/url";
5960 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
5961 next unless $u;
5962 my $dn = dirname("$git_dir/svn/$x");
5963 mkpath([$dn]) unless -d $dn;
5964 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
5965 mkpath(["$git_dir/svn/svn"]);
5966 print STDERR " - $git_dir/$x/info => ",
5967 "$git_dir/svn/$x/info\n";
5968 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
5969 croak "$!: $x";
5970 # don't worry too much about these, they probably
5971 # don't exist with repos this old (save for index,
5972 # and we can easily regenerate that)
5973 foreach my $f (qw/unhandled.log index .rev_db/) {
5974 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
5975 }
5976 } else {
5977 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
5978 rename "$git_dir/$x", "$git_dir/svn/$x" or
5979 croak "$!: $x";
5980 }
5981 $migrated++;
5982 }
5983 command_close_pipe($fh, $ctx);
5984 print STDERR "Done migrating from a git-svn v1 layout\n";
5985 $migrated;
5986}
5987
5988sub read_old_urls {
5989 my ($l_map, $pfx, $path) = @_;
5990 my @dir;
5991 foreach (<$path/*>) {
5992 if (-r "$_/info/url") {
5993 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
5994 my $ref_id = $pfx . basename $_;
5995 my $url = ::file_to_s("$_/info/url");
5996 $l_map->{$ref_id} = $url;
5997 } elsif (-d $_) {
5998 push @dir, $_;
5999 }
6000 }
6001 foreach (@dir) {
6002 my $x = $_;
6003 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
6004 read_old_urls($l_map, $x, $_);
6005 }
6006}
6007
6008sub migrate_from_v2 {
6009 my @cfg = command(qw/config -l/);
6010 return if grep /^svn-remote\..+\.url=/, @cfg;
6011 my %l_map;
6012 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
6013 my $migrated = 0;
6014
6015 foreach my $ref_id (sort keys %l_map) {
Eric Wong471bc002007-02-01 04:06:27 -08006016 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
6017 if ($@) {
6018 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
6019 }
Eric Wong706587f2007-01-18 17:50:01 -08006020 $migrated++;
6021 }
6022 $migrated;
6023}
6024
Eric Wong47e39c52007-01-21 04:27:09 -08006025sub minimize_connections {
6026 my $r = Git::SVN::read_all_remotes();
6027 my $new_urls = {};
6028 my $root_repos = {};
6029 foreach my $repo_id (keys %$r) {
6030 my $url = $r->{$repo_id}->{url} or next;
6031 my $fetch = $r->{$repo_id}->{fetch} or next;
6032 my $ra = Git::SVN::Ra->new($url);
6033
6034 # skip existing cases where we already connect to the root
6035 if (($ra->{url} eq $ra->{repos_root}) ||
Eric Wong7829f202008-06-28 20:40:32 -07006036 ($ra->{repos_root} eq $repo_id)) {
Eric Wong47e39c52007-01-21 04:27:09 -08006037 $root_repos->{$ra->{url}} = $repo_id;
6038 next;
6039 }
6040
6041 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
6042 my $root_path = $ra->{url};
Eric Wong4e9f6cc2007-02-09 12:17:57 -08006043 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
Eric Wong47e39c52007-01-21 04:27:09 -08006044 foreach my $path (keys %$fetch) {
6045 my $ref_id = $fetch->{$path};
6046 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
6047
6048 # make sure we can read when connecting to
6049 # a higher level of a repository
6050 my ($last_rev, undef) = $gs->last_rev_commit;
6051 if (!defined $last_rev) {
6052 $last_rev = eval {
6053 $root_ra->get_latest_revnum;
6054 };
6055 next if $@;
6056 }
6057 my $new = $root_path;
6058 $new .= length $path ? "/$path" : '';
6059 eval {
6060 $root_ra->get_log([$new], $last_rev, $last_rev,
6061 0, 0, 1, sub { });
6062 };
6063 next if $@;
6064 $new_urls->{$ra->{repos_root}}->{$new} =
6065 { ref_id => $ref_id,
6066 old_repo_id => $repo_id,
6067 old_path => $path };
6068 }
6069 }
6070
6071 my @emptied;
6072 foreach my $url (keys %$new_urls) {
6073 # see if we can re-use an existing [svn-remote "repo_id"]
6074 # instead of creating a(n ugly) new section:
Eric Wong7829f202008-06-28 20:40:32 -07006075 my $repo_id = $root_repos->{$url} || $url;
Eric Wong47e39c52007-01-21 04:27:09 -08006076
6077 my $fetch = $new_urls->{$url};
6078 foreach my $path (keys %$fetch) {
6079 my $x = $fetch->{$path};
6080 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
6081 my $pfx = "svn-remote.$x->{old_repo_id}";
6082
6083 my $old_fetch = quotemeta("$x->{old_path}:".
Adam Brewster6f5748e2009-08-11 23:14:27 -04006084 "$x->{ref_id}");
Eric Wong8b8fc062007-01-22 11:44:57 -08006085 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08006086 "$pfx.fetch", '^'. $old_fetch . '$');
6087 delete $r->{$x->{old_repo_id}}->
6088 {fetch}->{$x->{old_path}};
6089 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
Eric Wong8b8fc062007-01-22 11:44:57 -08006090 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08006091 "$pfx.url");
6092 push @emptied, $x->{old_repo_id}
6093 }
6094 }
6095 }
6096 if (@emptied) {
Johannes Schindelin8befc502008-12-14 23:10:52 +01006097 my $file = $ENV{GIT_CONFIG} || "$ENV{GIT_DIR}/config";
Eric Wong47e39c52007-01-21 04:27:09 -08006098 print STDERR <<EOF;
6099The following [svn-remote] sections in your config file ($file) are empty
6100and can be safely removed:
6101EOF
6102 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
6103 }
6104}
6105
Eric Wong706587f2007-01-18 17:50:01 -08006106sub migration_check {
6107 migrate_from_v0();
6108 migrate_from_v1();
6109 migrate_from_v2();
Eric Wong47e39c52007-01-21 04:27:09 -08006110 minimize_connections() if $_minimize;
Eric Wong706587f2007-01-18 17:50:01 -08006111}
6112
Eric Wongef3cfaa2007-01-24 03:30:57 -08006113package Git::IndexInfo;
6114use strict;
6115use warnings;
6116use Git qw/command_input_pipe command_close_pipe/;
6117
6118sub new {
6119 my ($class) = @_;
6120 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
6121 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
6122}
6123
6124sub remove {
6125 my ($self, $path) = @_;
6126 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
6127 return ++$self->{nr};
6128 }
6129 undef;
6130}
6131
6132sub update {
6133 my ($self, $mode, $hash, $path) = @_;
6134 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
6135 return ++$self->{nr};
6136 }
6137 undef;
6138}
6139
6140sub DESTROY {
6141 my ($self) = @_;
6142 command_close_pipe($self->{gui}, $self->{ctx});
6143}
6144
Eric Wong4bb9ed02007-02-03 13:29:17 -08006145package Git::SVN::GlobSpec;
6146use strict;
6147use warnings;
6148
6149sub new {
Jay Soffian07576202010-01-23 03:30:01 -05006150 my ($class, $glob, $pattern_ok) = @_;
Eric Wong4bb9ed02007-02-03 13:29:17 -08006151 my $re = $glob;
6152 $re =~ s!/+$!!g; # no need for trailing slashes
Jay Soffian07576202010-01-23 03:30:01 -05006153 my (@left, @right, @patterns);
6154 my $state = "left";
6155 my $die_msg = "Only one set of wildcard directories " .
6156 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
6157 for my $part (split(m|/|, $glob)) {
6158 if ($part =~ /\*/ && $part ne "*") {
6159 die "Invalid pattern in '$glob': $part\n";
6160 } elsif ($pattern_ok && $part =~ /[{}]/ &&
6161 $part !~ /^\{[^{}]+\}/) {
6162 die "Invalid pattern in '$glob': $part\n";
6163 }
6164 if ($part eq "*") {
6165 die $die_msg if $state eq "right";
6166 $state = "pattern";
6167 push(@patterns, "[^/]*");
6168 } elsif ($pattern_ok && $part =~ /^\{(.*)\}$/) {
6169 die $die_msg if $state eq "right";
6170 $state = "pattern";
6171 my $p = quotemeta($1);
6172 $p =~ s/\\,/|/g;
6173 push(@patterns, "(?:$p)");
6174 } else {
6175 if ($state eq "left") {
6176 push(@left, $part);
6177 } else {
6178 push(@right, $part);
6179 $state = "right";
6180 }
6181 }
Marcus Griep570d35c2008-08-08 01:41:57 -07006182 }
Jay Soffian07576202010-01-23 03:30:01 -05006183 my $depth = @patterns;
Marcus Griep570d35c2008-08-08 01:41:57 -07006184 if ($depth == 0) {
Jay Soffian07576202010-01-23 03:30:01 -05006185 die "One '*' is needed in glob: '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08006186 }
Jay Soffian07576202010-01-23 03:30:01 -05006187 my $left = join('/', @left);
6188 my $right = join('/', @right);
6189 $re = join('/', @patterns);
6190 $re = join('\/',
6191 grep(length, quotemeta($left), "($re)", quotemeta($right)));
Eric Wong74a81222007-02-10 13:28:50 -08006192 my $left_re = qr/^\/\Q$left\E(\/|$)/;
6193 bless { left => $left, right => $right, left_regex => $left_re,
Marcus Griep570d35c2008-08-08 01:41:57 -07006194 regex => qr/$re/, glob => $glob, depth => $depth }, $class;
Eric Wong4bb9ed02007-02-03 13:29:17 -08006195}
6196
6197sub full_path {
6198 my ($self, $path) = @_;
6199 return (length $self->{left} ? "$self->{left}/" : '') .
6200 $path . (length $self->{right} ? "/$self->{right}" : '');
6201}
6202
Eric Wong3397f9d2006-02-16 01:24:16 -08006203__END__
6204
6205Data structures:
6206
Eric Wong4bb9ed02007-02-03 13:29:17 -08006207
6208$remotes = { # returned by read_all_remotes()
6209 'svn' => {
6210 # svn-remote.svn.url=https://svn.musicpd.org
6211 url => 'https://svn.musicpd.org',
6212 # svn-remote.svn.fetch=mpd/trunk:trunk
6213 fetch => {
6214 'mpd/trunk' => 'trunk',
6215 },
6216 # svn-remote.svn.tags=mpd/tags/*:tags/*
6217 tags => {
6218 path => {
6219 left => 'mpd/tags',
6220 right => '',
6221 regex => qr!mpd/tags/([^/]+)$!,
6222 glob => 'tags/*',
6223 },
6224 ref => {
6225 left => 'tags',
6226 right => '',
6227 regex => qr!tags/([^/]+)$!,
6228 glob => 'tags/*',
6229 },
6230 }
6231 }
6232};
6233
Eric Wong44320b92007-01-13 22:35:53 -08006234$log_entry hashref as returned by libsvn_log_entry()
Eric Wong3397f9d2006-02-16 01:24:16 -08006235{
Eric Wong44320b92007-01-13 22:35:53 -08006236 log => 'whitespace-formatted log entry
Eric Wong3397f9d2006-02-16 01:24:16 -08006237', # trailing newline is preserved
6238 revision => '8', # integer
6239 date => '2004-02-24T17:01:44.108345Z', # commit date
6240 author => 'committer name'
6241};
6242
Eric Wong6e8548c2007-01-27 01:32:00 -08006243
6244# this is generated by generate_diff();
Eric Wong3397f9d2006-02-16 01:24:16 -08006245@mods = array of diff-index line hashes, each element represents one line
6246 of diff-index output
6247
6248diff-index line ($m hash)
6249{
6250 mode_a => first column of diff-index output, no leading ':',
6251 mode_b => second column of diff-index output,
6252 sha1_b => sha1sum of the final blob,
Eric Wongac8e0b92006-03-03 01:20:07 -08006253 chg => change type [MCRADT],
Eric Wong3397f9d2006-02-16 01:24:16 -08006254 file_a => original file name of a file (iff chg is 'C' or 'R')
6255 file_b => new/current file name of a file (any chg)
6256}
6257;
Eric Wonga5e0ced2006-06-12 15:23:48 -07006258
Eric Wonga00439a2006-06-27 19:39:13 -07006259# retval of read_url_paths{,_all}();
6260$l_map = {
6261 # repository root url
6262 'https://svn.musicpd.org' => {
6263 # repository path # GIT_SVN_ID
6264 'mpd/trunk' => 'trunk',
6265 'mpd/tags/0.11.5' => 'tags/0.11.5',
6266 },
6267}
6268
Eric Wonga5e0ced2006-06-12 15:23:48 -07006269Notes:
6270 I don't trust the each() function on unless I created %hash myself
6271 because the internal iterator may not have started at base.