blob: cbc5211d58a37f71b5c4566b9375ee398a1612ba [file] [log] [blame]
Eric Wong3397f9d2006-02-16 01:24:16 -08001#!/usr/bin/env perl
Eric Wong551ce282006-02-20 10:57:29 -08002# Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3# License: GPL v2 or later
Eric Wong3397f9d2006-02-16 01:24:16 -08004use warnings;
5use strict;
6use vars qw/ $AUTHOR $VERSION
Adam Robenffe256f2008-05-23 16:19:41 +02007 $sha1 $sha1_short $_revision $_repository
Eric Wong9760adc2007-01-31 03:06:56 -08008 $_q $_authors %users/;
Eric Wong3397f9d2006-02-16 01:24:16 -08009$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
Eric Wong60d02cc2006-07-06 00:14:16 -070010$VERSION = '@@GIT_VERSION@@';
Eric Wong13ccd6d2006-03-29 22:37:18 -080011
Benoit Sigoure15153452007-10-16 16:36:50 +020012# From which subdir have we been invoked?
13my $cmd_dir_prefix = eval {
14 command_oneline([qw/rev-parse --show-prefix/], STDERR => 0)
15} || '';
16
Eric Wong5253dc32007-02-20 01:36:30 -080017my $git_dir_user_set = 1 if defined $ENV{GIT_DIR};
Eric Wong706587f2007-01-18 17:50:01 -080018$ENV{GIT_DIR} ||= '.git';
Eric Wong9fa00b62007-02-03 12:49:48 -080019$Git::SVN::default_repo_id = 'svn';
Eric Wong8b8fc062007-01-22 11:44:57 -080020$Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
Eric Wong6af1db42007-02-14 16:04:10 -080021$Git::SVN::Ra::_log_window_size = 100;
Eric Wong13ccd6d2006-03-29 22:37:18 -080022
Eric Wongf8c9d1d2007-01-12 02:35:20 -080023$Git::SVN::Log::TZ = $ENV{TZ};
Eric Wong3397f9d2006-02-16 01:24:16 -080024$ENV{TZ} = 'UTC';
Eric Wonga00439a2006-06-27 19:39:13 -070025$| = 1; # unbuffer STDOUT
Eric Wong3397f9d2006-02-16 01:24:16 -080026
Benoit Sigoure207f1a72007-10-16 16:36:52 +020027sub fatal (@) { print STDERR "@_\n"; exit 1 }
Eric Wongb9c85182006-12-15 23:58:07 -080028require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
29require SVN::Ra;
30require SVN::Delta;
31if ($SVN::Core::VERSION lt '1.1.0') {
Benoit Sigoure207f1a72007-10-16 16:36:52 +020032 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
Eric Wongb9c85182006-12-15 23:58:07 -080033}
Eric Wongd81bf822007-01-10 01:22:38 -080034push @Git::SVN::Ra::ISA, 'SVN::Ra';
Eric Wongb9c85182006-12-15 23:58:07 -080035push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
36push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
Eric Wong3397f9d2006-02-16 01:24:16 -080037use Carp qw/croak/;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -080038use Digest::MD5;
Eric Wong3397f9d2006-02-16 01:24:16 -080039use IO::File qw//;
40use File::Basename qw/dirname basename/;
41use File::Path qw/mkpath/;
Eric Wong512b6202007-04-03 01:57:08 -070042use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
Eric Wong968bdf12006-06-15 13:36:12 -070043use IPC::Open3;
Eric Wong336f1712007-01-11 02:14:43 -080044use Git;
Eric Wonga5e0ced2006-06-12 15:23:48 -070045
Eric Wong336f1712007-01-11 02:14:43 -080046BEGIN {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120047 # import functions from Git into our packages, en masse
48 no strict 'refs';
Eric Wong336f1712007-01-11 02:14:43 -080049 foreach (qw/command command_oneline command_noisy command_output_pipe
50 command_input_pipe command_close_pipe/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120051 for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -080052 Git::SVN::Migration Git::SVN::Log Git::SVN),
Sam Vilainc5f71ad2007-06-15 15:43:59 +120053 __PACKAGE__) {
54 *{"${package}::$_"} = \&{"Git::$_"};
55 }
Eric Wong336f1712007-01-11 02:14:43 -080056 }
Eric Wong336f1712007-01-11 02:14:43 -080057}
58
Eric Wongb9c85182006-12-15 23:58:07 -080059my ($SVN);
Eric Wong83e99402006-10-11 18:19:55 -070060
Eric Wongf8c9d1d2007-01-12 02:35:20 -080061$sha1 = qr/[a-f\d]{40}/;
62$sha1_short = qr/[a-f\d]{4,40}/;
Eric Wong44320b92007-01-13 22:35:53 -080063my ($_stdin, $_help, $_edit,
Eric Wong9760adc2007-01-31 03:06:56 -080064 $_message, $_file,
Eric Wongd05d72e2007-01-15 22:59:26 -080065 $_template, $_shared,
Karl Hasselström171af112007-05-03 07:51:35 +020066 $_version, $_fetch_all, $_no_rebase,
Eric Wongdee41f32007-03-13 11:40:36 -070067 $_merge, $_strategy, $_dry_run, $_local,
Steven Grimm4be40382008-05-10 22:11:18 -070068 $_prefix, $_no_checkout, $_url, $_verbose,
Florian Ragwitz5de70ef2008-10-04 19:35:17 -070069 $_git_format, $_commit_url, $_tag);
Eric Wong0bed5ea2007-02-09 02:45:03 -080070$Git::SVN::_follow_parent = 1;
Eric Wong706587f2007-01-18 17:50:01 -080071my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
72 'config-dir=s' => \$Git::SVN::Ra::config_dir,
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +020073 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
74 'ignore-paths=s' => \$SVN::Git::Fetcher::_ignore_regex );
Eric Wong0bed5ea2007-02-09 02:45:03 -080075my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
Eric Wongdc5869c2006-05-24 02:07:32 -070076 'authors-file|A=s' => \$_authors,
Eric Wongecc712d2007-01-31 12:28:10 -080077 'repack:i' => \$Git::SVN::_repack,
Eric Wong97ae0912007-02-11 15:21:24 -080078 'noMetadata' => \$Git::SVN::_no_metadata,
79 'useSvmProps' => \$Git::SVN::_use_svm_props,
Eric Wong62e349d2007-02-16 19:57:29 -080080 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props,
Eric Wong6af1db42007-02-14 16:04:10 -080081 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size,
Eric Wong1e889ef2007-02-16 01:45:13 -080082 'no-checkout' => \$_no_checkout,
Eric Wong80f50742006-06-27 19:39:14 -070083 'quiet|q' => \$_q,
Eric Wongecc712d2007-01-31 12:28:10 -080084 'repack-flags|repack-args|repack-opts=s' =>
85 \$Git::SVN::_repack_flags,
Andy Whitcroft70ae04e2007-11-22 13:44:42 +000086 'use-log-author' => \$Git::SVN::_use_log_author,
Avery Pennarun6aa9ba12008-04-15 21:04:17 -040087 'add-author-from' => \$Git::SVN::_add_author_from,
Pete Harlane82f0d72009-01-17 20:10:14 -080088 'localtime' => \$Git::SVN::_localtime,
Eric Wong706587f2007-01-18 17:50:01 -080089 %remote_opts );
Eric Wong36f5b1f2006-05-23 19:23:41 -070090
martin f. krafft8f728fb2007-07-14 11:25:28 +020091my ($_trunk, $_tags, $_branches, $_stdlayout);
Eric Wong0dfaf0a2007-02-18 02:34:09 -080092my %icv;
Eric Wongdadc6d22007-02-14 12:27:41 -080093my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared,
94 'trunk|T=s' => \$_trunk, 'tags|t=s' => \$_tags,
95 'branches|b=s' => \$_branches, 'prefix=s' => \$_prefix,
martin f. krafft8f728fb2007-07-14 11:25:28 +020096 'stdlayout|s' => \$_stdlayout,
Eric Wong4a1bb4c2007-05-13 09:58:14 -070097 'minimize-url|m' => \$Git::SVN::_minimize_url,
Eric Wong0dfaf0a2007-02-18 02:34:09 -080098 'no-metadata' => sub { $icv{noMetadata} = 1 },
99 'use-svm-props' => sub { $icv{useSvmProps} = 1 },
100 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 },
101 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] },
Eric Wongdadc6d22007-02-14 12:27:41 -0800102 %remote_opts );
Eric Wong27e9fb82006-06-27 19:39:12 -0700103my %cmt_opts = ( 'edit|e' => \$_edit,
Eric Wong24e22aa2007-01-29 00:07:49 -0800104 'rmdir' => \$SVN::Git::Editor::_rmdir,
105 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder,
106 'l=i' => \$SVN::Git::Editor::_rename_limit,
107 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity
Eric Wong27e9fb82006-06-27 19:39:12 -0700108);
Eric Wong9d55b412006-06-12 15:53:13 -0700109
Eric Wong3397f9d2006-02-16 01:24:16 -0800110my %cmd = (
Eric Wong2a3240b2007-01-04 18:09:56 -0800111 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
Eric Wonge98671e2007-02-14 02:21:19 -0800112 { 'revision|r=s' => \$_revision,
Eric Wong905f8b72007-02-16 03:22:40 -0800113 'fetch-all|all' => \$_fetch_all,
Eric Wonge98671e2007-02-14 02:21:19 -0800114 %fc_opts } ],
Eric Wong0425ea92007-02-16 18:45:01 -0800115 clone => [ \&cmd_clone, "Initialize and fetch revisions",
116 { 'revision|r=s' => \$_revision,
117 %fc_opts, %init_opts } ],
Eric Wongd2866f92007-01-11 12:26:16 -0800118 init => [ \&cmd_init, "Initialize a repo for tracking" .
Eric Wongf8ab6b72006-05-31 15:49:56 -0700119 " (requires URL argument)",
Eric Wong9d55b412006-06-12 15:53:13 -0700120 \%init_opts ],
Eric Wongdadc6d22007-02-14 12:27:41 -0800121 'multi-init' => [ \&cmd_multi_init,
122 "Deprecated alias for ".
123 "'$0 init -T<trunk> -b<branches> -t<tags>'",
124 \%init_opts ],
Eric Wongd7ad3be2007-01-14 03:14:28 -0800125 dcommit => [ \&cmd_dcommit,
126 'Commit several diffs to merge with upstream',
Eric Wong3289e862006-12-15 23:58:08 -0800127 { 'merge|m|M' => \$_merge,
128 'strategy|s=s' => \$_strategy,
Eric Wong905f8b72007-02-16 03:22:40 -0800129 'verbose|v' => \$_verbose,
Eric Wong3289e862006-12-15 23:58:08 -0800130 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800131 'fetch-all|all' => \$_fetch_all,
Eric Wongba24e742008-08-07 02:06:16 -0700132 'commit-url=s' => \$_commit_url,
133 'revision|r=i' => \$_revision,
Karl Hasselström171af112007-05-03 07:51:35 +0200134 'no-rebase' => \$_no_rebase,
Eric Wong4b155222006-12-22 21:59:24 -0800135 %cmt_opts, %fc_opts } ],
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700136 branch => [ \&cmd_branch,
137 'Create a branch in the SVN repository',
138 { 'message|m=s' => \$_message,
139 'dry-run|n' => \$_dry_run,
140 'tag|t' => \$_tag } ],
141 tag => [ sub { $_tag = 1; cmd_branch(@_) },
142 'Create a tag in the SVN repository',
143 { 'message|m=s' => \$_message,
144 'dry-run|n' => \$_dry_run } ],
Eric Wong1ce255d2007-01-14 23:21:16 -0800145 'set-tree' => [ \&cmd_set_tree,
146 "Set an SVN repository to a git tree-ish",
147 { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200148 'create-ignore' => [ \&cmd_create_ignore,
149 'Create a .gitignore per svn:ignore',
150 { 'revision|r=i' => \$_revision
151 } ],
Benoit Sigoure15153452007-10-16 16:36:50 +0200152 'propget' => [ \&cmd_propget,
153 'Print the value of a property on a file or directory',
154 { 'revision|r=i' => \$_revision } ],
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200155 'proplist' => [ \&cmd_proplist,
156 'List all properties of a file or directory',
157 { 'revision|r=i' => \$_revision } ],
Eric Wong5969cbe2007-01-11 17:58:39 -0800158 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200159 { 'revision|r=i' => \$_revision
Lars Hjemli05b4df32007-09-05 11:35:29 +0200160 } ],
Vineet Kumar2d879792007-11-19 14:56:15 -0800161 'show-externals' => [ \&cmd_show_externals, "Show svn:externals listings",
162 { 'revision|r=i' => \$_revision
163 } ],
Eric Wong1c8443b2007-01-14 02:17:00 -0800164 'multi-fetch' => [ \&cmd_multi_fetch,
Eric Wonge98671e2007-02-14 02:21:19 -0800165 "Deprecated alias for $0 fetch --all",
166 { 'revision|r=s' => \$_revision, %fc_opts } ],
Eric Wong706587f2007-01-18 17:50:01 -0800167 'migrate' => [ sub { },
168 # no-op, we automatically run this anyways,
Eric Wong706587f2007-01-18 17:50:01 -0800169 'Migrate configuration/metadata/layout from
170 previous versions of git-svn',
Eric Wonga836a0e2007-02-14 19:34:56 -0800171 { 'minimize' => \$Git::SVN::Migration::_minimize,
172 %remote_opts } ],
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800173 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
174 { 'limit=i' => \$Git::SVN::Log::limit,
Eric Wong79bb8d82006-06-01 02:35:44 -0700175 'revision|r=s' => \$_revision,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800176 'verbose|v' => \$Git::SVN::Log::verbose,
177 'incremental' => \$Git::SVN::Log::incremental,
178 'oneline' => \$Git::SVN::Log::oneline,
179 'show-commit' => \$Git::SVN::Log::show_commit,
180 'non-recursive' => \$Git::SVN::Log::non_recursive,
Eric Wong79bb8d82006-06-01 02:35:44 -0700181 'authors-file|A=s' => \$_authors,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800182 'color' => \$Git::SVN::Log::color,
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200183 'pager=s' => \$Git::SVN::Log::pager
Eric Wong79bb8d82006-06-01 02:35:44 -0700184 } ],
Eric Wong222566e2008-08-08 01:41:58 -0700185 'find-rev' => [ \&cmd_find_rev,
186 "Translate between SVN revision numbers and tree-ish",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200187 {} ],
Eric Wong905f8b72007-02-16 03:22:40 -0800188 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
189 { 'merge|m|M' => \$_merge,
190 'verbose|v' => \$_verbose,
191 'strategy|s=s' => \$_strategy,
Eric Wongdee41f32007-03-13 11:40:36 -0700192 'local|l' => \$_local,
Eric Wong905f8b72007-02-16 03:22:40 -0800193 'fetch-all|all' => \$_fetch_all,
Seth Falcon7d45e142008-05-19 20:29:17 -0700194 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800195 %fc_opts } ],
Eric Wong44320b92007-01-13 22:35:53 -0800196 'commit-diff' => [ \&cmd_commit_diff,
197 'Commit a diff between two trees',
Eric Wong27e9fb82006-06-27 19:39:12 -0700198 { 'message|m=s' => \$_message,
199 'file|F=s' => \$_file,
Eric Wong45bf4732006-11-09 01:19:37 -0800200 'revision|r=s' => \$_revision,
Eric Wong27e9fb82006-06-27 19:39:12 -0700201 %cmt_opts } ],
David D. Kilzere6fefa92007-11-21 11:57:18 -0800202 'info' => [ \&cmd_info,
203 "Show info about the latest SVN revision
204 on the current branch",
David D. Kilzer8b014d72007-11-21 11:57:19 -0800205 { 'url' => \$_url, } ],
Tim Stoakes6fb53752008-02-10 15:21:08 +1030206 'blame' => [ \&Git::SVN::Log::cmd_blame,
207 "Show what revision and author last modified each line of a file",
Steven Grimm4be40382008-05-10 22:11:18 -0700208 { 'git-format' => \$_git_format } ],
Eric Wong3397f9d2006-02-16 01:24:16 -0800209);
Eric Wong9d55b412006-06-12 15:53:13 -0700210
Eric Wong3397f9d2006-02-16 01:24:16 -0800211my $cmd;
212for (my $i = 0; $i < @ARGV; $i++) {
213 if (defined $cmd{$ARGV[$i]}) {
214 $cmd = $ARGV[$i];
215 splice @ARGV, $i, 1;
216 last;
217 }
218};
219
Eric Wong540424b2007-12-19 00:31:43 -0800220# make sure we're always running at the top-level working directory
221unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
Eric Wong5253dc32007-02-20 01:36:30 -0800222 unless (-d $ENV{GIT_DIR}) {
223 if ($git_dir_user_set) {
224 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
225 "but it is not a directory\n";
226 }
227 my $git_dir = delete $ENV{GIT_DIR};
Deskin Millerfe4003f2008-11-06 00:07:39 -0500228 my $cdup = undef;
229 git_cmd_try {
230 $cdup = command_oneline(qw/rev-parse --show-cdup/);
231 $git_dir = '.' unless ($cdup);
232 chomp $cdup if ($cdup);
233 $cdup = "." unless ($cdup && length $cdup);
234 } "Already at toplevel, but $git_dir not found\n";
Eric Wong5253dc32007-02-20 01:36:30 -0800235 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
236 unless (-d $git_dir) {
237 die "$git_dir still not found after going to ",
238 "'$cdup'\n";
239 }
240 $ENV{GIT_DIR} = $git_dir;
241 }
Adam Robenffe256f2008-05-23 16:19:41 +0200242 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wong5253dc32007-02-20 01:36:30 -0800243}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100244
245my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
246
247read_repo_config(\%opts);
Eric Wong222566e2008-08-08 01:41:58 -0700248if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
249 Getopt::Long::Configure('pass_through');
250}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100251my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
252 'minimize-connections' => \$Git::SVN::Migration::_minimize,
253 'id|i=s' => \$Git::SVN::default_ref_id,
254 'svn-remote|remote|R=s' => sub {
255 $Git::SVN::no_reuse_existing = 1;
256 $Git::SVN::default_repo_id = $_[1] });
257exit 1 if (!$rv && $cmd && $cmd ne 'log');
258
259usage(0) if $_help;
260version() if $_version;
261usage(1) unless defined $cmd;
262load_authors() if $_authors;
263
Eric Wong0425ea92007-02-16 18:45:01 -0800264unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
Eric Wong706587f2007-01-18 17:50:01 -0800265 Git::SVN::Migration::migration_check();
266}
Eric Wongecc712d2007-01-31 12:28:10 -0800267Git::SVN::init_vars();
Eric Wongb805b442007-01-22 13:52:04 -0800268eval {
269 Git::SVN::verify_remotes_sanity();
270 $cmd{$cmd}->[0]->(@ARGV);
271};
272fatal $@ if $@;
Eric Wong1e889ef2007-02-16 01:45:13 -0800273post_fetch_checkout();
Eric Wong3397f9d2006-02-16 01:24:16 -0800274exit 0;
275
276####################### primary functions ######################
277sub usage {
278 my $exit = shift || 0;
279 my $fd = $exit ? \*STDERR : \*STDOUT;
280 print $fd <<"";
281git-svn - bidirectional operations between a single Subversion tree and git
Stephan Beyer1b1dd232008-07-13 15:36:15 +0200282Usage: git svn <command> [options] [arguments]\n
Eric Wong448c81b2006-03-03 01:20:09 -0800283
284 print $fd "Available commands:\n" unless $cmd;
Eric Wong3397f9d2006-02-16 01:24:16 -0800285
286 foreach (sort keys %cmd) {
Eric Wong448c81b2006-03-03 01:20:09 -0800287 next if $cmd && $cmd ne $_;
Eric Wonga836a0e2007-02-14 19:34:56 -0800288 next if /^multi-/; # don't show deprecated commands
Eric Wongb203b762006-10-11 14:53:36 -0700289 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
Benoit Sigoureaa807bc2007-11-03 19:53:34 +0100290 foreach (sort keys %{$cmd{$_}->[2]}) {
Eric Wong512b6202007-04-03 01:57:08 -0700291 # mixed-case options are for .git/config only
292 next if /[A-Z]/ && /^[a-z]+$/i;
Eric Wong448c81b2006-03-03 01:20:09 -0800293 # prints out arguments as they should be passed:
Eric Wongb8c92ca2006-05-24 01:40:37 -0700294 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
Eric Wongb203b762006-10-11 14:53:36 -0700295 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
Eric Wong448c81b2006-03-03 01:20:09 -0800296 "--$_" : "-$_" }
297 split /\|/,$_)," $x\n";
298 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800299 }
300 print $fd <<"";
Eric Wong448c81b2006-03-03 01:20:09 -0800301\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
302arbitrary identifier if you're tracking multiple SVN branches/repositories in
303one git repository and want to keep them separate. See git-svn(1) for more
304information.
Eric Wong3397f9d2006-02-16 01:24:16 -0800305
306 exit $exit;
307}
308
Eric Wong551ce282006-02-20 10:57:29 -0800309sub version {
Eric Wong7d60ab22006-12-28 01:16:20 -0800310 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
Eric Wong551ce282006-02-20 10:57:29 -0800311 exit 0;
312}
313
Eric Wong8164b652007-01-11 15:35:55 -0800314sub do_git_init_db {
315 unless (-d $ENV{GIT_DIR}) {
316 my @init_db = ('init');
317 push @init_db, "--template=$_template" if defined $_template;
Eric Wongdadc6d22007-02-14 12:27:41 -0800318 if (defined $_shared) {
319 if ($_shared =~ /[a-z]/) {
320 push @init_db, "--shared=$_shared";
321 } else {
322 push @init_db, "--shared";
323 }
324 }
Eric Wong8164b652007-01-11 15:35:55 -0800325 command_noisy(@init_db);
Adam Robenffe256f2008-05-23 16:19:41 +0200326 $_repository = Git->repository(Repository => ".git");
Eric Wong8164b652007-01-11 15:35:55 -0800327 }
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800328 my $set;
329 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
330 foreach my $i (keys %icv) {
331 die "'$set' and '$i' cannot both be set\n" if $set;
332 next unless defined $icv{$i};
333 command_noisy('config', "$pfx.$i", $icv{$i});
334 $set = $i;
335 }
Eric Wong8164b652007-01-11 15:35:55 -0800336}
337
Eric Wongdadc6d22007-02-14 12:27:41 -0800338sub init_subdir {
339 my $repo_path = shift or return;
340 mkpath([$repo_path]) unless -d $repo_path;
341 chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
Eric Wongf30603f2007-02-23 01:26:26 -0800342 $ENV{GIT_DIR} = '.git';
Adam Robenffe256f2008-05-23 16:19:41 +0200343 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wongdadc6d22007-02-14 12:27:41 -0800344}
345
Eric Wong0425ea92007-02-16 18:45:01 -0800346sub cmd_clone {
347 my ($url, $path) = @_;
348 if (!defined $path &&
martin f. krafft8f728fb2007-07-14 11:25:28 +0200349 (defined $_trunk || defined $_branches || defined $_tags ||
350 defined $_stdlayout) &&
Eric Wong0425ea92007-02-16 18:45:01 -0800351 $url !~ m#^[a-z\+]+://#) {
352 $path = $url;
353 }
Eric Wong0425ea92007-02-16 18:45:01 -0800354 $path = basename($url) if !defined $path || !length $path;
Eric Wongf30603f2007-02-23 01:26:26 -0800355 cmd_init($url, $path);
Eric Wong0425ea92007-02-16 18:45:01 -0800356 Git::SVN::fetch_all($Git::SVN::default_repo_id);
357}
358
Eric Wongd2866f92007-01-11 12:26:16 -0800359sub cmd_init {
martin f. krafft8f728fb2007-07-14 11:25:28 +0200360 if (defined $_stdlayout) {
361 $_trunk = 'trunk' if (!defined $_trunk);
362 $_tags = 'tags' if (!defined $_tags);
363 $_branches = 'branches' if (!defined $_branches);
364 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800365 if (defined $_trunk || defined $_branches || defined $_tags) {
366 return cmd_multi_init(@_);
Eric Wong03e0ea82006-06-30 21:42:53 -0700367 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800368 my $url = shift or die "SVN repository location required ",
369 "as a command-line argument\n";
370 init_subdir(@_);
Eric Wong8164b652007-01-11 15:35:55 -0800371 do_git_init_db();
Eric Wong03e0ea82006-06-30 21:42:53 -0700372
Eric Wong706587f2007-01-18 17:50:01 -0800373 Git::SVN->init($url);
Eric Wong3397f9d2006-02-16 01:24:16 -0800374}
375
Eric Wong2a3240b2007-01-04 18:09:56 -0800376sub cmd_fetch {
Eric Wonge98671e2007-02-14 02:21:19 -0800377 if (grep /^\d+=./, @_) {
378 die "'<rev>=<commit>' fetch arguments are ",
379 "no longer supported.\n";
Eric Wong07a1c952007-01-22 15:47:41 -0800380 }
Eric Wonge98671e2007-02-14 02:21:19 -0800381 my ($remote) = @_;
382 if (@_ > 1) {
Eric Wong905f8b72007-02-16 03:22:40 -0800383 die "Usage: $0 fetch [--all] [svn-remote]\n";
Eric Wonge98671e2007-02-14 02:21:19 -0800384 }
385 $remote ||= $Git::SVN::default_repo_id;
386 if ($_fetch_all) {
387 cmd_multi_fetch();
388 } else {
389 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
Eric Wong1c8443b2007-01-14 02:17:00 -0800390 }
Eric Wong2a3240b2007-01-04 18:09:56 -0800391}
392
Eric Wong1ce255d2007-01-14 23:21:16 -0800393sub cmd_set_tree {
Eric Wong3397f9d2006-02-16 01:24:16 -0800394 my (@commits) = @_;
395 if ($_stdin || !@commits) {
396 print "Reading from stdin...\n";
397 @commits = ();
398 while (<STDIN>) {
Eric Wong1ca72ae2006-03-03 01:20:09 -0800399 if (/\b($sha1_short)\b/o) {
Eric Wong3397f9d2006-02-16 01:24:16 -0800400 unshift @commits, $1;
401 }
402 }
403 }
404 my @revs;
Eric Wong8de010a2006-02-20 10:57:26 -0800405 foreach my $c (@commits) {
Eric Wongaef4e922006-12-15 10:59:54 -0800406 my @tmp = command('rev-parse',$c);
Eric Wong8de010a2006-02-20 10:57:26 -0800407 if (scalar @tmp == 1) {
408 push @revs, $tmp[0];
409 } elsif (scalar @tmp > 1) {
Eric Wongaef4e922006-12-15 10:59:54 -0800410 push @revs, reverse(command('rev-list',@tmp));
Eric Wong8de010a2006-02-20 10:57:26 -0800411 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200412 fatal "Failed to rev-parse $c";
Eric Wong8de010a2006-02-20 10:57:26 -0800413 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800414 }
Eric Wong1ce255d2007-01-14 23:21:16 -0800415 my $gs = Git::SVN->new;
416 my ($r_last, $cmt_last) = $gs->last_rev_commit;
417 $gs->fetch;
Eric Wong97f69872007-01-25 11:53:13 -0800418 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
Eric Wong1ce255d2007-01-14 23:21:16 -0800419 fatal "There are new revisions that were fetched ",
420 "and need to be merged (or acknowledged) ",
421 "before committing.\nlast rev: $r_last\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200422 " current: $gs->{last_rev}";
Eric Wong1ce255d2007-01-14 23:21:16 -0800423 }
424 $gs->set_tree($_) foreach @revs;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700425 print "Done committing ",scalar @revs," revisions to SVN\n";
Eric Wong3157dd92007-12-13 08:27:34 -0800426 unlink $gs->{index};
Eric Wonga5e0ced2006-06-12 15:23:48 -0700427}
428
Eric Wongd7ad3be2007-01-14 03:14:28 -0800429sub cmd_dcommit {
430 my $head = shift;
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100431 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
David Reiss826a9332007-11-13 13:47:26 -0800432 'Cannot dcommit with a dirty index. Commit your changes first, '
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100433 . "or stash them with `git stash'.\n";
Eric Wongd7ad3be2007-01-14 03:14:28 -0800434 $head ||= 'HEAD';
Eric Wonga8ae2622007-02-13 14:22:11 -0800435 my @refs;
Eric Wong13c823f2007-04-08 00:59:19 -0700436 my ($url, $rev, $uuid, $gs) = working_head_info($head, \@refs);
Thomas Rast2cb61102008-08-31 15:50:59 +0200437 unless ($gs) {
438 die "Unable to determine upstream SVN information from ",
439 "$head history.\nPerhaps the repository is empty.";
440 }
Eric Wong711521e2008-08-20 00:30:06 -0700441 $url = defined $_commit_url ? $_commit_url : $gs->full_url;
Eric Wongba24e742008-08-07 02:06:16 -0700442 my $last_rev = $_revision if defined $_revision;
Matthieu Moy59b0c242008-04-24 20:06:36 +0200443 if ($url) {
444 print "Committing to $url ...\n";
445 }
Eric Wong733a65a2007-06-13 02:23:28 -0700446 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
Eric Wong751eb392007-08-31 18:16:12 -0700447 if ($_no_rebase && scalar(@$linear_refs) > 1) {
448 warn "Attempting to commit more than one change while ",
449 "--no-rebase is enabled.\n",
450 "If these changes depend on each other, re-running ",
Eric Wong7dfa16b2008-01-02 10:09:49 -0800451 "without --no-rebase may be required."
Eric Wong751eb392007-08-31 18:16:12 -0700452 }
Eric Wong711521e2008-08-20 00:30:06 -0700453 my $expect_url = $url;
454 Git::SVN::remove_username($expect_url);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800455 while (1) {
456 my $d = shift @$linear_refs or last;
Eric Wong45bf4732006-11-09 01:19:37 -0800457 unless (defined $last_rev) {
458 (undef, $last_rev, undef) = cmt_metadata("$d~1");
459 unless (defined $last_rev) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800460 fatal "Unable to extract revision information ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200461 "from commit $d~1";
Eric Wong45bf4732006-11-09 01:19:37 -0800462 }
463 }
Eric Wongb22d4492006-08-26 00:01:23 -0700464 if ($_dry_run) {
465 print "diff-tree $d~1 $d\n";
466 } else {
Eric Wong751eb392007-08-31 18:16:12 -0700467 my $cmt_rev;
Eric Wongd7ad3be2007-01-14 03:14:28 -0800468 my %ed_opts = ( r => $last_rev,
Eric Wong61395352007-01-27 14:33:08 -0800469 log => get_commit_entry($d)->{log},
Eric Wongba24e742008-08-07 02:06:16 -0700470 ra => Git::SVN::Ra->new($url),
Konstantin V. Arkhipov3caf3202007-11-14 03:52:02 +0300471 config => SVN::Core::config_get_config(
472 $Git::SVN::Ra::config_dir
473 ),
Eric Wong61395352007-01-27 14:33:08 -0800474 tree_a => "$d~1",
475 tree_b => $d,
476 editor_cb => sub {
477 print "Committed r$_[0]\n";
Eric Wong751eb392007-08-31 18:16:12 -0700478 $cmt_rev = $_[0];
479 },
Eric Wonga8ae2622007-02-13 14:22:11 -0800480 svn_path => '');
Eric Wong61395352007-01-27 14:33:08 -0800481 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800482 print "No changes\n$d~1 == $d\n";
Eric Wong733a65a2007-06-13 02:23:28 -0700483 } elsif ($parents->{$d} && @{$parents->{$d}}) {
Eric Wong751eb392007-08-31 18:16:12 -0700484 $gs->{inject_parents_dcommit}->{$cmt_rev} =
Eric Wong733a65a2007-06-13 02:23:28 -0700485 $parents->{$d};
Eric Wongd7ad3be2007-01-14 03:14:28 -0800486 }
Eric Wong751eb392007-08-31 18:16:12 -0700487 $_fetch_all ? $gs->fetch_all : $gs->fetch;
Eric Wong7dfa16b2008-01-02 10:09:49 -0800488 $last_rev = $cmt_rev;
Eric Wong751eb392007-08-31 18:16:12 -0700489 next if $_no_rebase;
490
491 # we always want to rebase against the current HEAD,
492 # not any head that was passed to us
Eric Wongc74d9ac2007-11-05 03:21:47 -0800493 my @diff = command('diff-tree', $d,
Eric Wong751eb392007-08-31 18:16:12 -0700494 $gs->refname, '--');
495 my @finish;
496 if (@diff) {
497 @finish = rebase_cmd();
Eric Wongc74d9ac2007-11-05 03:21:47 -0800498 print STDERR "W: $d and ", $gs->refname,
Eric Wong751eb392007-08-31 18:16:12 -0700499 " differ, using @finish:\n",
Eric Wongc74d9ac2007-11-05 03:21:47 -0800500 join("\n", @diff), "\n";
Eric Wong751eb392007-08-31 18:16:12 -0700501 } else {
502 print "No changes between current HEAD and ",
503 $gs->refname,
504 "\nResetting to the latest ",
505 $gs->refname, "\n";
506 @finish = qw/reset --mixed/;
507 }
508 command_noisy(@finish, $gs->refname);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800509 if (@diff) {
510 @refs = ();
511 my ($url_, $rev_, $uuid_, $gs_) =
512 working_head_info($head, \@refs);
513 my ($linear_refs_, $parents_) =
514 linearize_history($gs_, \@refs);
515 if (scalar(@$linear_refs) !=
516 scalar(@$linear_refs_)) {
517 fatal "# of revisions changed ",
518 "\nbefore:\n",
519 join("\n", @$linear_refs),
520 "\n\nafter:\n",
521 join("\n", @$linear_refs_), "\n",
522 'If you are attempting to commit ',
523 "merges, try running:\n\t",
524 'git rebase --interactive',
525 '--preserve-merges ',
526 $gs->refname,
527 "\nBefore dcommitting";
528 }
Eric Wong711521e2008-08-20 00:30:06 -0700529 if ($url_ ne $expect_url) {
Eric Wongc74d9ac2007-11-05 03:21:47 -0800530 fatal "URL mismatch after rebase: ",
Eric Wong711521e2008-08-20 00:30:06 -0700531 "$url_ != $expect_url";
Eric Wongc74d9ac2007-11-05 03:21:47 -0800532 }
533 if ($uuid_ ne $uuid) {
534 fatal "uuid mismatch after rebase: ",
535 "$uuid_ != $uuid";
536 }
537 # remap parents
538 my (%p, @l, $i);
539 for ($i = 0; $i < scalar @$linear_refs; $i++) {
540 my $new = $linear_refs_->[$i] or next;
541 $p{$new} =
542 $parents->{$linear_refs->[$i]};
543 push @l, $new;
544 }
545 $parents = \%p;
546 $linear_refs = \@l;
547 }
Eric Wongb22d4492006-08-26 00:01:23 -0700548 }
549 }
Eric Wong3157dd92007-12-13 08:27:34 -0800550 unlink $gs->{index};
Eric Wongb22d4492006-08-26 00:01:23 -0700551}
552
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700553sub cmd_branch {
554 my ($branch_name, $head) = @_;
555
556 unless (defined $branch_name && length $branch_name) {
557 die(($_tag ? "tag" : "branch") . " name required\n");
558 }
559 $head ||= 'HEAD';
560
561 my ($src, $rev, undef, $gs) = working_head_info($head);
562
Deskin Millera0fbc872008-12-01 21:43:00 -0500563 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700564 my $glob = $remote->{ $_tag ? 'tags' : 'branches' };
565 my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
566 my $dst = join '/', $remote->{url}, $lft, $branch_name, ($rgt || ());
567
568 my $ctx = SVN::Client->new(
569 auth => Git::SVN::Ra::_auth_providers(),
570 log_msg => sub {
571 ${ $_[0] } = defined $_message
572 ? $_message
573 : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
574 . $branch_name;
575 },
576 );
577
578 eval {
579 $ctx->ls($dst, 'HEAD', 0);
580 } and die "branch ${branch_name} already exists\n";
581
582 print "Copying ${src} at r${rev} to ${dst}...\n";
583 $ctx->copy($src, $rev, $dst)
584 unless $_dry_run;
585
586 $gs->fetch_all;
587}
588
Adam Roben26e60162007-04-27 11:57:53 -0700589sub cmd_find_rev {
Marc-Andre Lureauea14e6c2008-03-11 10:00:45 +0200590 my $revision_or_hash = shift or die "SVN or git revision required ",
591 "as a command-line argument\n";
Adam Roben26e60162007-04-27 11:57:53 -0700592 my $result;
593 if ($revision_or_hash =~ /^r\d+$/) {
Adam Robenb3cb7e42007-04-29 01:35:27 -0700594 my $head = shift;
595 $head ||= 'HEAD';
596 my @refs;
João Abecasis63c56022008-07-14 16:28:04 +0100597 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
Adam Robenb3cb7e42007-04-29 01:35:27 -0700598 unless ($gs) {
599 die "Unable to determine upstream SVN information from ",
600 "$head history\n";
Adam Roben26e60162007-04-27 11:57:53 -0700601 }
Adam Robenb3cb7e42007-04-29 01:35:27 -0700602 my $desired_revision = substr($revision_or_hash, 1);
João Abecasis63c56022008-07-14 16:28:04 +0100603 $result = $gs->rev_map_get($desired_revision, $uuid);
Adam Roben26e60162007-04-27 11:57:53 -0700604 } else {
605 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
606 $result = $rev;
607 }
608 print "$result\n" if $result;
609}
610
Eric Wong905f8b72007-02-16 03:22:40 -0800611sub cmd_rebase {
612 command_noisy(qw/update-index --refresh/);
Eric Wong13c823f2007-04-08 00:59:19 -0700613 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
614 unless ($gs) {
Eric Wong905f8b72007-02-16 03:22:40 -0800615 die "Unable to determine upstream SVN information from ",
616 "working tree history\n";
617 }
Seth Falcon7d45e142008-05-19 20:29:17 -0700618 if ($_dry_run) {
619 print "Remote Branch: " . $gs->refname . "\n";
620 print "SVN URL: " . $url . "\n";
621 return;
622 }
Eric Wong905f8b72007-02-16 03:22:40 -0800623 if (command(qw/diff-index HEAD --/)) {
624 print STDERR "Cannot rebase with uncommited changes:\n";
625 command_noisy('status');
626 exit 1;
627 }
Eric Wongdee41f32007-03-13 11:40:36 -0700628 unless ($_local) {
Steven Grimmcec0d5a2007-11-29 11:54:39 -0800629 # rebase will checkout for us, so no need to do it explicitly
630 $_no_checkout = 'true';
Eric Wongdee41f32007-03-13 11:40:36 -0700631 $_fetch_all ? $gs->fetch_all : $gs->fetch;
632 }
Eric Wong905f8b72007-02-16 03:22:40 -0800633 command_noisy(rebase_cmd(), $gs->refname);
634}
635
Eric Wong5969cbe2007-01-11 17:58:39 -0800636sub cmd_show_ignore {
Eric Wong13c823f2007-04-08 00:59:19 -0700637 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
638 $gs ||= Git::SVN->new;
Eric Wong5969cbe2007-01-11 17:58:39 -0800639 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200640 $gs->prop_walk($gs->{path}, $r, sub {
641 my ($gs, $path, $props) = @_;
642 print STDOUT "\n# $path\n";
643 my $s = $props->{'svn:ignore'} or return;
644 $s =~ s/[\r\n]+/\n/g;
645 chomp $s;
646 $s =~ s#^#$path#gm;
647 print STDOUT "$s\n";
648 });
Eric Wonga5e0ced2006-06-12 15:23:48 -0700649}
650
Vineet Kumar2d879792007-11-19 14:56:15 -0800651sub cmd_show_externals {
652 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
653 $gs ||= Git::SVN->new;
654 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
655 $gs->prop_walk($gs->{path}, $r, sub {
656 my ($gs, $path, $props) = @_;
657 print STDOUT "\n# $path\n";
658 my $s = $props->{'svn:externals'} or return;
659 $s =~ s/[\r\n]+/\n/g;
660 chomp $s;
661 $s =~ s#^#$path#gm;
662 print STDOUT "$s\n";
663 });
664}
665
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200666sub cmd_create_ignore {
667 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
668 $gs ||= Git::SVN->new;
669 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
670 $gs->prop_walk($gs->{path}, $r, sub {
671 my ($gs, $path, $props) = @_;
672 # $path is of the form /path/to/dir/
673 my $ignore = '.' . $path . '.gitignore';
674 my $s = $props->{'svn:ignore'} or return;
675 open(GITIGNORE, '>', $ignore)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200676 or fatal("Failed to open `$ignore' for writing: $!");
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200677 $s =~ s/[\r\n]+/\n/g;
678 chomp $s;
679 # Prefix all patterns so that the ignore doesn't apply
680 # to sub-directories.
681 $s =~ s#^#/#gm;
682 print GITIGNORE "$s\n";
683 close(GITIGNORE)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200684 or fatal("Failed to close `$ignore': $!");
Gustaf Hendebyc4c66b22008-05-05 00:33:09 +0200685 command_noisy('add', '-f', $ignore);
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200686 });
687}
688
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800689sub canonicalize_path {
690 my ($path) = @_;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800691 my $dot_slash_added = 0;
692 if (substr($path, 0, 1) ne "/") {
693 $path = "./" . $path;
694 $dot_slash_added = 1;
695 }
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800696 # File::Spec->canonpath doesn't collapse x/../y into y (for a
697 # good reason), so let's do this manually.
698 $path =~ s#/+#/#g;
699 $path =~ s#/\.(?:/|$)#/#g;
700 $path =~ s#/[^/]+/\.\.##g;
701 $path =~ s#/$##g;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800702 $path =~ s#^\./## if $dot_slash_added;
Gerrit Pape2fe403e2008-07-06 19:28:50 +0000703 $path =~ s#^/##;
704 $path =~ s#^\.$##;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800705 return $path;
706}
707
Benoit Sigoure15153452007-10-16 16:36:50 +0200708# get_svnprops(PATH)
709# ------------------
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200710# Helper for cmd_propget and cmd_proplist below.
Benoit Sigoure15153452007-10-16 16:36:50 +0200711sub get_svnprops {
712 my $path = shift;
713 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
714 $gs ||= Git::SVN->new;
715
716 # prefix THE PATH by the sub-directory from which the user
717 # invoked us.
718 $path = $cmd_dir_prefix . $path;
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200719 fatal("No such file or directory: $path") unless -e $path;
Benoit Sigoure15153452007-10-16 16:36:50 +0200720 my $is_dir = -d $path ? 1 : 0;
721 $path = $gs->{path} . '/' . $path;
722
723 # canonicalize the path (otherwise libsvn will abort or fail to
724 # find the file)
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800725 $path = canonicalize_path($path);
Benoit Sigoure15153452007-10-16 16:36:50 +0200726
727 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
728 my $props;
729 if ($is_dir) {
730 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
731 }
732 else {
733 (undef, $props) = $gs->ra->get_file($path, $r, undef);
734 }
735 return $props;
736}
737
738# cmd_propget (PROP, PATH)
739# ------------------------
740# Print the SVN property PROP for PATH.
741sub cmd_propget {
742 my ($prop, $path) = @_;
743 $path = '.' if not defined $path;
744 usage(1) if not defined $prop;
745 my $props = get_svnprops($path);
746 if (not defined $props->{$prop}) {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200747 fatal("`$path' does not have a `$prop' SVN property.");
Benoit Sigoure15153452007-10-16 16:36:50 +0200748 }
749 print $props->{$prop} . "\n";
750}
751
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200752# cmd_proplist (PATH)
753# -------------------
754# Print the list of SVN properties for PATH.
755sub cmd_proplist {
756 my $path = shift;
757 $path = '.' if not defined $path;
758 my $props = get_svnprops($path);
759 print "Properties on '$path':\n";
760 foreach (sort keys %{$props}) {
761 print " $_\n";
762 }
763}
764
Eric Wong8164b652007-01-11 15:35:55 -0800765sub cmd_multi_init {
Eric Wong9d55b412006-06-12 15:53:13 -0700766 my $url = shift;
Eric Wong98327e52007-01-04 18:02:00 -0800767 unless (defined $_trunk || defined $_branches || defined $_tags) {
768 usage(1);
769 }
Eric Wongdc431662007-05-19 03:59:02 -0700770
771 # there are currently some bugs that prevent multi-init/multi-fetch
772 # setups from working well without this.
773 $Git::SVN::_minimize_url = 1;
774
Eric Wong8164b652007-01-11 15:35:55 -0800775 $_prefix = '' unless defined $_prefix;
Eric Wongdadc6d22007-02-14 12:27:41 -0800776 if (defined $url) {
777 $url =~ s#/+$##;
778 init_subdir(@_);
779 }
Eric Wongf30603f2007-02-23 01:26:26 -0800780 do_git_init_db();
Eric Wong98327e52007-01-04 18:02:00 -0800781 if (defined $_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800782 my $trunk_ref = $_prefix . 'trunk';
783 # try both old-style and new-style lookups:
784 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
Eric Wong8164b652007-01-11 15:35:55 -0800785 unless ($gs_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800786 my ($trunk_url, $trunk_path) =
787 complete_svn_url($url, $_trunk);
788 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
789 undef, $trunk_ref);
Eric Wong98327e52007-01-04 18:02:00 -0800790 }
Eric Wongc35b96e2006-10-11 11:53:21 -0700791 }
Eric Wong706587f2007-01-18 17:50:01 -0800792 return unless defined $_branches || defined $_tags;
Eric Wonge7db67e2007-01-11 17:09:26 -0800793 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
794 complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix);
795 complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
Eric Wong9d55b412006-06-12 15:53:13 -0700796}
797
Eric Wong1c8443b2007-01-14 02:17:00 -0800798sub cmd_multi_fetch {
Eric Wong0af9c9f2007-01-27 22:28:56 -0800799 my $remotes = Git::SVN::read_all_remotes();
800 foreach my $repo_id (sort keys %$remotes) {
Eric Wongdb03cd22007-02-13 00:38:02 -0800801 if ($remotes->{$repo_id}->{url}) {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800802 Git::SVN::fetch_all($repo_id, $remotes);
803 }
Eric Wong706587f2007-01-18 17:50:01 -0800804 }
Eric Wong9d55b412006-06-12 15:53:13 -0700805}
806
Eric Wong44320b92007-01-13 22:35:53 -0800807# this command is special because it requires no metadata
808sub cmd_commit_diff {
809 my ($ta, $tb, $url) = @_;
810 my $usage = "Usage: $0 commit-diff -r<revision> ".
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200811 "<tree-ish> <tree-ish> [<URL>]";
Eric Wong44320b92007-01-13 22:35:53 -0800812 fatal($usage) if (!defined $ta || !defined $tb);
Karl Hasselströmd72ab8c2008-05-17 17:07:09 +0200813 my $svn_path = '';
Eric Wong44320b92007-01-13 22:35:53 -0800814 if (!defined $url) {
815 my $gs = eval { Git::SVN->new };
816 if (!$gs) {
817 fatal("Needed URL or usable git-svn --id in ",
818 "the command-line\n", $usage);
819 }
820 $url = $gs->{url};
Eric Wongd3a840d2007-01-26 01:32:45 -0800821 $svn_path = $gs->{path};
Eric Wong44320b92007-01-13 22:35:53 -0800822 }
823 unless (defined $_revision) {
824 fatal("-r|--revision is a required argument\n", $usage);
825 }
826 if (defined $_message && defined $_file) {
827 fatal("Both --message/-m and --file/-F specified ",
828 "for the commit message.\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200829 "I have no idea what you mean");
Eric Wong44320b92007-01-13 22:35:53 -0800830 }
831 if (defined $_file) {
832 $_message = file_to_s($_file);
833 } else {
834 $_message ||= get_commit_entry($tb)->{log};
835 }
836 my $ra ||= Git::SVN::Ra->new($url);
837 my $r = $_revision;
838 if ($r eq 'HEAD') {
839 $r = $ra->get_latest_revnum;
840 } elsif ($r !~ /^\d+$/) {
841 die "revision argument: $r not understood by git-svn\n";
842 }
Eric Wong61395352007-01-27 14:33:08 -0800843 my %ed_opts = ( r => $r,
844 log => $_message,
845 ra => $ra,
846 tree_a => $ta,
847 tree_b => $tb,
848 editor_cb => sub { print "Committed r$_[0]\n" },
849 svn_path => $svn_path );
850 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong44320b92007-01-13 22:35:53 -0800851 print "No changes\n$ta == $tb\n";
852 }
Eric Wong44320b92007-01-13 22:35:53 -0800853}
854
Thomas Rast05427b92008-08-26 21:32:37 +0200855sub escape_uri_only {
856 my ($uri) = @_;
857 my @tmp;
858 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -0700859 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Thomas Rast05427b92008-08-26 21:32:37 +0200860 push @tmp, $_;
861 }
862 join('/', @tmp);
863}
864
865sub escape_url {
866 my ($url) = @_;
867 if ($url =~ m#^([^:]+)://([^/]*)(.*)$#) {
868 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
869 $url = "$scheme://$domain$uri";
870 }
871 $url;
872}
873
David D. Kilzere6fefa92007-11-21 11:57:18 -0800874sub cmd_info {
Eric Wongbd2d4f92008-08-05 00:35:16 -0700875 my $path = canonicalize_path(defined($_[0]) ? $_[0] : ".");
Thomas Rastedde9112008-08-26 21:32:36 +0200876 my $fullpath = canonicalize_path($cmd_dir_prefix . $path);
Eric Wongbd2d4f92008-08-05 00:35:16 -0700877 if (exists $_[1]) {
David D. Kilzere6fefa92007-11-21 11:57:18 -0800878 die "Too many arguments specified\n";
879 }
880
881 my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
882
883 if (!$file_type && !$diff_status) {
Thomas Rast2cf3e3a2008-08-29 15:42:48 +0200884 print STDERR "svn: '$path' is not under version control\n";
885 exit 1;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800886 }
887
888 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
889 unless ($gs) {
890 die "Unable to determine upstream SVN information from ",
891 "working tree history\n";
892 }
Eric Wongbd2d4f92008-08-05 00:35:16 -0700893
894 # canonicalize_path() will return "" to make libsvn 1.5.x happy,
895 $path = "." if $path eq "";
896
Thomas Rastedde9112008-08-26 21:32:36 +0200897 my $full_url = $url . ($fullpath eq "" ? "" : "/$fullpath");
David D. Kilzere6fefa92007-11-21 11:57:18 -0800898
David D. Kilzer8b014d72007-11-21 11:57:19 -0800899 if ($_url) {
Thomas Rast05427b92008-08-26 21:32:37 +0200900 print escape_url($full_url), "\n";
David D. Kilzer8b014d72007-11-21 11:57:19 -0800901 return;
902 }
903
David D. Kilzere6fefa92007-11-21 11:57:18 -0800904 my $result = "Path: $path\n";
905 $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
Thomas Rast05427b92008-08-26 21:32:37 +0200906 $result .= "URL: " . escape_url($full_url) . "\n";
David D. Kilzere6fefa92007-11-21 11:57:18 -0800907
Eric Wonga5460eb2007-11-21 18:20:57 -0800908 eval {
909 my $repos_root = $gs->repos_root;
910 Git::SVN::remove_username($repos_root);
Thomas Rast05427b92008-08-26 21:32:37 +0200911 $result .= "Repository Root: " . escape_url($repos_root) . "\n";
Eric Wonga5460eb2007-11-21 18:20:57 -0800912 };
913 if ($@) {
914 $result .= "Repository Root: (offline)\n";
915 }
Marcel Koeppen22ba47f2009-01-19 03:02:01 +0100916 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" &&
917 ($SVN::Core::VERSION le '1.5.4' || $file_type ne "dir");
David D. Kilzere6fefa92007-11-21 11:57:18 -0800918 $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
919
920 $result .= "Node Kind: " .
921 ($file_type eq "dir" ? "directory" : "file") . "\n";
922
923 my $schedule = $diff_status eq "A"
924 ? "add"
925 : ($diff_status eq "D" ? "delete" : "normal");
926 $result .= "Schedule: $schedule\n";
927
928 if ($diff_status eq "A") {
929 print $result, "\n";
930 return;
931 }
932
933 my ($lc_author, $lc_rev, $lc_date_utc);
Thomas Rastedde9112008-08-26 21:32:36 +0200934 my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath);
David D. Kilzere6fefa92007-11-21 11:57:18 -0800935 my $log = command_output_pipe(@args);
936 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
937 while (<$log>) {
938 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
939 $lc_author = $1;
940 $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
941 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
942 (undef, $lc_rev, undef) = ::extract_metadata($1);
943 }
944 }
945 close $log;
946
947 Git::SVN::Log::set_local_timezone();
948
949 $result .= "Last Changed Author: $lc_author\n";
950 $result .= "Last Changed Rev: $lc_rev\n";
951 $result .= "Last Changed Date: " .
952 Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
953
954 if ($file_type ne "dir") {
955 my $text_last_updated_date =
956 ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
957 $result .=
958 "Text Last Updated: " .
959 Git::SVN::Log::format_svn_date($text_last_updated_date) .
960 "\n";
961 my $checksum;
962 if ($diff_status eq "D") {
963 my ($fh, $ctx) =
964 command_output_pipe(qw(cat-file blob), "HEAD:$path");
965 if ($file_type eq "link") {
966 my $file_name = <$fh>;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -0800967 $checksum = md5sum("link $file_name");
David D. Kilzere6fefa92007-11-21 11:57:18 -0800968 } else {
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -0800969 $checksum = md5sum($fh);
David D. Kilzere6fefa92007-11-21 11:57:18 -0800970 }
971 command_close_pipe($fh, $ctx);
972 } elsif ($file_type eq "link") {
973 my $file_name =
974 command(qw(cat-file blob), "HEAD:$path");
975 $checksum =
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -0800976 md5sum("link " . $file_name);
David D. Kilzere6fefa92007-11-21 11:57:18 -0800977 } else {
978 open FILE, "<", $path or die $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -0800979 $checksum = md5sum(\*FILE);
David D. Kilzere6fefa92007-11-21 11:57:18 -0800980 close FILE or die $!;
981 }
982 $result .= "Checksum: " . $checksum . "\n";
983 }
984
985 print $result, "\n";
986}
987
Eric Wong3397f9d2006-02-16 01:24:16 -0800988########################### utility functions #########################
989
Eric Wong905f8b72007-02-16 03:22:40 -0800990sub rebase_cmd {
991 my @cmd = qw/rebase/;
992 push @cmd, '-v' if $_verbose;
993 push @cmd, qw/--merge/ if $_merge;
994 push @cmd, "--strategy=$_strategy" if $_strategy;
995 @cmd;
996}
997
Eric Wong1e889ef2007-02-16 01:45:13 -0800998sub post_fetch_checkout {
999 return if $_no_checkout;
1000 my $gs = $Git::SVN::_head or return;
1001 return if verify_ref('refs/heads/master^0');
1002
1003 my $valid_head = verify_ref('HEAD^0');
1004 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
1005 return if ($valid_head || !verify_ref('HEAD^0'));
1006
1007 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
1008 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
1009 return if -f $index;
1010
Matthias Lederhofer7ae3df82007-06-03 16:48:16 +02001011 return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
Eric Wong1e889ef2007-02-16 01:45:13 -08001012 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
1013 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
1014 print STDERR "Checked out HEAD:\n ",
1015 $gs->full_url, " r", $gs->last_rev, "\n";
1016}
1017
Eric Wong98327e52007-01-04 18:02:00 -08001018sub complete_svn_url {
1019 my ($url, $path) = @_;
1020 $path =~ s#/+$##;
Eric Wong98327e52007-01-04 18:02:00 -08001021 if ($path !~ m#^[a-z\+]+://#) {
Eric Wong98327e52007-01-04 18:02:00 -08001022 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
1023 fatal("E: '$path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001024 "and a separate URL is not specified");
Eric Wong98327e52007-01-04 18:02:00 -08001025 }
Eric Wong706587f2007-01-18 17:50:01 -08001026 return ($url, $path);
Eric Wong98327e52007-01-04 18:02:00 -08001027 }
Eric Wong706587f2007-01-18 17:50:01 -08001028 return ($path, '');
Eric Wong98327e52007-01-04 18:02:00 -08001029}
1030
Eric Wong9d55b412006-06-12 15:53:13 -07001031sub complete_url_ls_init {
Eric Wong706587f2007-01-18 17:50:01 -08001032 my ($ra, $repo_path, $switch, $pfx) = @_;
1033 unless ($repo_path) {
Eric Wong9d55b412006-06-12 15:53:13 -07001034 print STDERR "W: $switch not specified\n";
1035 return;
1036 }
Eric Wong706587f2007-01-18 17:50:01 -08001037 $repo_path =~ s#/+$##;
1038 if ($repo_path =~ m#^[a-z\+]+://#) {
1039 $ra = Git::SVN::Ra->new($repo_path);
1040 $repo_path = '';
Eric Wonge7db67e2007-01-11 17:09:26 -08001041 } else {
Eric Wong706587f2007-01-18 17:50:01 -08001042 $repo_path =~ s#^/+##;
Eric Wonge7db67e2007-01-11 17:09:26 -08001043 unless ($ra) {
Eric Wong706587f2007-01-18 17:50:01 -08001044 fatal("E: '$repo_path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001045 "and a separate URL is not specified");
Eric Wong9d55b412006-06-12 15:53:13 -07001046 }
Eric Wonge7db67e2007-01-11 17:09:26 -08001047 }
Eric Wong706587f2007-01-18 17:50:01 -08001048 my $url = $ra->{url};
Eric Wongb4d57e52007-02-14 15:10:44 -08001049 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
1050 my $k = "svn-remote.$gs->{repo_id}.url";
1051 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
1052 if ($orig_url && ($orig_url ne $gs->{url})) {
1053 die "$k already set: $orig_url\n",
1054 "wanted to set to: $gs->{url}\n";
Eric Wong88cf4102007-02-01 03:59:07 -08001055 }
Eric Wongb4d57e52007-02-14 15:10:44 -08001056 command_oneline('config', $k, $gs->{url}) unless $orig_url;
Eric Wonged0b9d42008-03-14 11:01:23 -07001057 my $remote_path = "$ra->{svn_path}/$repo_path";
Eric Wongb4d57e52007-02-14 15:10:44 -08001058 $remote_path =~ s#/+#/#g;
1059 $remote_path =~ s#^/##g;
Eric Wonged0b9d42008-03-14 11:01:23 -07001060 $remote_path .= "/*" if $remote_path !~ /\*/;
Eric Wongb4d57e52007-02-14 15:10:44 -08001061 my ($n) = ($switch =~ /^--(\w+)/);
1062 if (length $pfx && $pfx !~ m#/$#) {
1063 die "--prefix='$pfx' must have a trailing slash '/'\n";
Eric Wong9d55b412006-06-12 15:53:13 -07001064 }
Marcus Griep570d35c2008-08-08 01:41:57 -07001065 command_noisy('config',
1066 "svn-remote.$gs->{repo_id}.$n",
1067 "$remote_path:refs/remotes/$pfx*" .
1068 ('/*' x (($remote_path =~ tr/*/*/) - 1)) );
Eric Wong9d55b412006-06-12 15:53:13 -07001069}
1070
Eric Wongaef4e922006-12-15 10:59:54 -08001071sub verify_ref {
1072 my ($ref) = @_;
Eric Wong2c5c1d52006-12-28 01:16:21 -08001073 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
1074 { STDERR => 0 }); };
Eric Wongaef4e922006-12-15 10:59:54 -08001075}
1076
Eric Wonga5e0ced2006-06-12 15:23:48 -07001077sub get_tree_from_treeish {
Eric Wongcf52b8f2006-02-20 10:57:28 -08001078 my ($treeish) = @_;
Eric Wong44320b92007-01-13 22:35:53 -08001079 # $treeish can be a symbolic ref, too:
Eric Wongaef4e922006-12-15 10:59:54 -08001080 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001081 my $expected;
1082 while ($type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001083 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001084 }
1085 if ($type eq 'commit') {
Eric Wongaef4e922006-12-15 10:59:54 -08001086 $expected = (grep /^tree /, command(qw/cat-file commit/,
1087 $treeish))[0];
Eric Wong44320b92007-01-13 22:35:53 -08001088 ($expected) = ($expected =~ /^tree ($sha1)$/o);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001089 die "Unable to get tree from $treeish\n" unless $expected;
1090 } elsif ($type eq 'tree') {
1091 $expected = $treeish;
1092 } else {
1093 die "$treeish is a $type, expected tree, tag or commit\n";
1094 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07001095 return $expected;
1096}
Eric Wongcf52b8f2006-02-20 10:57:28 -08001097
Eric Wong44320b92007-01-13 22:35:53 -08001098sub get_commit_entry {
1099 my ($treeish) = shift;
1100 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
1101 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1102 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1103 open my $log_fh, '>', $commit_editmsg or croak $!;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001104
Eric Wong44320b92007-01-13 22:35:53 -08001105 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wong4ad45152006-07-09 20:20:48 -07001106 if ($type eq 'commit' || $type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001107 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
Eric Wong44320b92007-01-13 22:35:53 -08001108 $type, $treeish);
Eric Wong3397f9d2006-02-16 01:24:16 -08001109 my $in_msg = 0;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001110 my $author;
1111 my $saw_from = 0;
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001112 my $msgbuf = "";
Eric Wong3397f9d2006-02-16 01:24:16 -08001113 while (<$msg_fh>) {
1114 if (!$in_msg) {
1115 $in_msg = 1 if (/^\s*$/);
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001116 $author = $1 if (/^author (.*>)/);
Eric Wongdf746c52006-03-03 01:20:08 -08001117 } elsif (/^git-svn-id: /) {
Eric Wong44320b92007-01-13 22:35:53 -08001118 # skip this for now, we regenerate the
1119 # correct one on re-fetch anyways
1120 # TODO: set *:merge properties or like...
Eric Wong3397f9d2006-02-16 01:24:16 -08001121 } else {
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001122 if (/^From:/ || /^Signed-off-by:/) {
1123 $saw_from = 1;
1124 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001125 $msgbuf .= $_;
Eric Wong3397f9d2006-02-16 01:24:16 -08001126 }
1127 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001128 $msgbuf =~ s/\s+$//s;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001129 if ($Git::SVN::_add_author_from && defined($author)
1130 && !$saw_from) {
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001131 $msgbuf .= "\n\nFrom: $author";
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001132 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001133 print $log_fh $msgbuf or croak $!;
Eric Wongaef4e922006-12-15 10:59:54 -08001134 command_close_pipe($msg_fh, $ctx);
Eric Wong3397f9d2006-02-16 01:24:16 -08001135 }
Eric Wong44320b92007-01-13 22:35:53 -08001136 close $log_fh or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -08001137
1138 if ($_edit || ($type eq 'tree')) {
1139 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
Eric Wong44320b92007-01-13 22:35:53 -08001140 # TODO: strip out spaces, comments, like git-commit.sh
1141 system($editor, $commit_editmsg);
Eric Wong3397f9d2006-02-16 01:24:16 -08001142 }
Eric Wong44320b92007-01-13 22:35:53 -08001143 rename $commit_editmsg, $commit_msg or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07001144 {
1145 # SVN requires messages to be UTF-8 when entering the repo
1146 local $/;
1147 open $log_fh, '<', $commit_msg or croak $!;
1148 binmode $log_fh;
1149 chomp($log_entry{log} = <$log_fh>);
1150
1151 if (my $enc = Git::config('i18n.commitencoding')) {
1152 require Encode;
1153 Encode::from_to($log_entry{log}, $enc, 'UTF-8');
1154 }
1155 close $log_fh or croak $!;
1156 }
Eric Wong44320b92007-01-13 22:35:53 -08001157 unlink $commit_msg;
1158 \%log_entry;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001159}
1160
Eric Wong3397f9d2006-02-16 01:24:16 -08001161sub s_to_file {
1162 my ($str, $file, $mode) = @_;
1163 open my $fd,'>',$file or croak $!;
1164 print $fd $str,"\n" or croak $!;
1165 close $fd or croak $!;
1166 chmod ($mode &~ umask, $file) if (defined $mode);
1167}
1168
1169sub file_to_s {
1170 my $file = shift;
1171 open my $fd,'<',$file or croak "$!: file: $file\n";
1172 local $/;
1173 my $ret = <$fd>;
1174 close $fd or croak $!;
1175 $ret =~ s/\s*$//s;
1176 return $ret;
1177}
1178
Eric Wongeeb0abe2006-03-03 01:20:08 -08001179# '<svn username> = real-name <email address>' mapping based on git-svnimport:
1180sub load_authors {
1181 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001182 my $log = $cmd eq 'log';
Eric Wongeeb0abe2006-03-03 01:20:08 -08001183 while (<$authors>) {
1184 chomp;
Richard MUSIL575d0252007-07-17 19:02:57 +02001185 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
Eric Wongeeb0abe2006-03-03 01:20:08 -08001186 my ($user, $name, $email) = ($1, $2, $3);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001187 if ($log) {
1188 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
1189 } else {
1190 $users{$user} = [$name, $email];
1191 }
Eric Wong79bb8d82006-06-01 02:35:44 -07001192 }
1193 close $authors or croak $!;
1194}
1195
Tom Princee0d10e12007-01-28 16:16:53 -08001196# convert GetOpt::Long specs for use by git-config
Eric Wongb8c92ca2006-05-24 01:40:37 -07001197sub read_repo_config {
Eric Wong706587f2007-01-18 17:50:01 -08001198 return unless -d $ENV{GIT_DIR};
Eric Wongb8c92ca2006-05-24 01:40:37 -07001199 my $opts = shift;
Eric Wong97ae0912007-02-11 15:21:24 -08001200 my @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001201 foreach my $o (keys %$opts) {
Eric Wong97ae0912007-02-11 15:21:24 -08001202 # if we have mixedCase and a long option-only, then
1203 # it's a config-only variable that we don't need for
1204 # the command-line.
1205 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001206 my $v = $opts->{$o};
Eric Wong97ae0912007-02-11 15:21:24 -08001207 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001208 $key =~ s/-//g;
Deskin Miller225f1d02008-10-23 15:21:34 -04001209 my $arg = 'git config';
Eric Wongb8c92ca2006-05-24 01:40:37 -07001210 $arg .= ' --int' if ($o =~ /[:=]i$/);
1211 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1212 if (ref $v eq 'ARRAY') {
1213 chomp(my @tmp = `$arg --get-all svn.$key`);
1214 @$v = @tmp if @tmp;
1215 } else {
1216 chomp(my $tmp = `$arg --get svn.$key`);
Eric Wong77742842007-02-10 21:07:12 -08001217 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
Eric Wongb8c92ca2006-05-24 01:40:37 -07001218 $$v = $tmp;
1219 }
1220 }
1221 }
Eric Wong97ae0912007-02-11 15:21:24 -08001222 delete @$opts{@config_only} if @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001223}
1224
Eric Wong79bb8d82006-06-01 02:35:44 -07001225sub extract_metadata {
Eric Wongc1927a82006-06-27 19:39:11 -07001226 my $id = shift or return (undef, undef, undef);
Sam Vilain3dfab992007-06-30 20:56:13 +12001227 my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
Eric Wong79bb8d82006-06-01 02:35:44 -07001228 \s([a-f\d\-]+)$/x);
Eric Wonge70dc782006-11-23 14:54:04 -08001229 if (!defined $rev || !$uuid || !$url) {
Eric Wong79bb8d82006-06-01 02:35:44 -07001230 # some of the original repositories I made had
Pavel Roskin82e5a822006-07-10 01:50:18 -04001231 # identifiers like this:
Sam Vilain3dfab992007-06-30 20:56:13 +12001232 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
Eric Wong79bb8d82006-06-01 02:35:44 -07001233 }
1234 return ($url, $rev, $uuid);
1235}
1236
Eric Wongc1927a82006-06-27 19:39:11 -07001237sub cmt_metadata {
1238 return extract_metadata((grep(/^git-svn-id: /,
Eric Wongaef4e922006-12-15 10:59:54 -08001239 command(qw/cat-file commit/, shift)))[-1]);
Eric Wongc1927a82006-06-27 19:39:11 -07001240}
1241
Eric Wong905f8b72007-02-16 03:22:40 -08001242sub working_head_info {
1243 my ($head, $refs) = @_;
Pedro Melob6309ac2008-04-10 17:05:21 +01001244 my @args = ('log', '--no-color', '--first-parent', '--pretty=medium');
Lars Hjemli05b4df32007-09-05 11:35:29 +02001245 my ($fh, $ctx) = command_output_pipe(@args, $head);
Sam Vilain3dfab992007-06-30 20:56:13 +12001246 my $hash;
Sam Vilain40cb8f82007-06-30 20:56:14 +12001247 my %max;
Sam Vilain3dfab992007-06-30 20:56:13 +12001248 while (<$fh>) {
1249 if ( m{^commit ($::sha1)$} ) {
1250 unshift @$refs, $hash if $hash and $refs;
1251 $hash = $1;
1252 next;
1253 }
1254 next unless s{^\s*(git-svn-id:)}{$1};
1255 my ($url, $rev, $uuid) = extract_metadata($_);
Eric Wong13c823f2007-04-08 00:59:19 -07001256 if (defined $url && defined $rev) {
Sam Vilain40cb8f82007-06-30 20:56:14 +12001257 next if $max{$url} and $max{$url} < $rev;
Eric Wong13c823f2007-04-08 00:59:19 -07001258 if (my $gs = Git::SVN->find_by_url($url)) {
João Abecasis63c56022008-07-14 16:28:04 +01001259 my $c = $gs->rev_map_get($rev, $uuid);
Adam Robenb03c7a62007-04-25 11:50:32 -07001260 if ($c && $c eq $hash) {
Eric Wong13c823f2007-04-08 00:59:19 -07001261 close $fh; # break the pipe
1262 return ($url, $rev, $uuid, $gs);
Sam Vilain40cb8f82007-06-30 20:56:14 +12001263 } else {
Eric Wong060610c2007-12-08 23:27:41 -08001264 $max{$url} ||= $gs->rev_map_max;
Eric Wong13c823f2007-04-08 00:59:19 -07001265 }
1266 }
1267 }
Eric Wong905f8b72007-02-16 03:22:40 -08001268 }
Eric Wong13c823f2007-04-08 00:59:19 -07001269 command_close_pipe($fh, $ctx);
1270 (undef, undef, undef, undef);
Eric Wong905f8b72007-02-16 03:22:40 -08001271}
1272
Eric Wong733a65a2007-06-13 02:23:28 -07001273sub read_commit_parents {
1274 my ($parents, $c) = @_;
Eric Wong7b02b852007-09-08 16:33:08 -07001275 chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
1276 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1277 @{$parents->{$c}} = split(/ /, $p);
Eric Wong733a65a2007-06-13 02:23:28 -07001278}
1279
1280sub linearize_history {
1281 my ($gs, $refs) = @_;
1282 my %parents;
1283 foreach my $c (@$refs) {
1284 read_commit_parents(\%parents, $c);
1285 }
1286
1287 my @linear_refs;
1288 my %skip = ();
1289 my $last_svn_commit = $gs->last_commit;
1290 foreach my $c (reverse @$refs) {
1291 next if $c eq $last_svn_commit;
1292 last if $skip{$c};
1293
1294 unshift @linear_refs, $c;
1295 $skip{$c} = 1;
1296
1297 # we only want the first parent to diff against for linear
1298 # history, we save the rest to inject when we finalize the
1299 # svn commit
1300 my $fp_a = verify_ref("$c~1");
1301 my $fp_b = shift @{$parents{$c}} if $parents{$c};
1302 if (!$fp_a || !$fp_b) {
1303 die "Commit $c\n",
1304 "has no parent commit, and therefore ",
1305 "nothing to diff against.\n",
1306 "You should be working from a repository ",
1307 "originally created by git-svn\n";
1308 }
1309 if ($fp_a ne $fp_b) {
1310 die "$c~1 = $fp_a, however parsing commit $c ",
1311 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1312 }
1313
1314 foreach my $p (@{$parents{$c}}) {
1315 $skip{$p} = 1;
1316 }
1317 }
1318 (\@linear_refs, \%parents);
1319}
1320
David D. Kilzere6fefa92007-11-21 11:57:18 -08001321sub find_file_type_and_diff_status {
1322 my ($path) = @_;
Dmitry Potapov107cee52008-07-21 00:14:07 +04001323 return ('dir', '') if $path eq '';
David D. Kilzere6fefa92007-11-21 11:57:18 -08001324
1325 my $diff_output =
1326 command_oneline(qw(diff --cached --name-status --), $path) || "";
1327 my $diff_status = (split(' ', $diff_output))[0] || "";
1328
1329 my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || "";
1330
1331 return (undef, undef) if !$diff_status && !$ls_tree;
1332
1333 if ($diff_status eq "A") {
1334 return ("link", $diff_status) if -l $path;
1335 return ("dir", $diff_status) if -d $path;
1336 return ("file", $diff_status);
1337 }
1338
1339 my $mode = (split(' ', $ls_tree))[0] || "";
1340
1341 return ("link", $diff_status) if $mode eq "120000";
1342 return ("dir", $diff_status) if $mode eq "040000";
1343 return ("file", $diff_status);
1344}
1345
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001346sub md5sum {
1347 my $arg = shift;
1348 my $ref = ref $arg;
1349 my $md5 = Digest::MD5->new();
Marcus Griep0b191382008-08-12 12:00:53 -04001350 if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001351 $md5->addfile($arg) or croak $!;
1352 } elsif ($ref eq 'SCALAR') {
1353 $md5->add($$arg) or croak $!;
1354 } elsif (!$ref) {
1355 $md5->add($arg) or croak $!;
1356 } else {
1357 ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1358 }
1359 return $md5->hexdigest();
1360}
1361
Eric Wong9b981fc2007-01-11 12:14:21 -08001362package Git::SVN;
1363use strict;
1364use warnings;
Eric Wong060610c2007-12-08 23:27:41 -08001365use Fcntl qw/:DEFAULT :seek/;
1366use constant rev_map_fmt => 'NH40';
Eric Wongecc712d2007-01-31 12:28:10 -08001367use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
Eric Wong62e349d2007-02-16 19:57:29 -08001368 $_repack $_repack_flags $_use_svm_props $_head
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00001369 $_use_svnsync_props $no_reuse_existing $_minimize_url
Pete Harlane82f0d72009-01-17 20:10:14 -08001370 $_use_log_author $_add_author_from $_localtime/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001371use Carp qw/croak/;
1372use File::Path qw/mkpath/;
Eric Wong373274f2007-01-31 13:54:23 -08001373use File::Copy qw/copy/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001374use IPC::Open3;
1375
Karl Hasselström94bc9142008-02-03 17:56:18 +01001376my ($_gc_nr, $_gc_period);
1377
Eric Wong9b981fc2007-01-11 12:14:21 -08001378# properties that we do not log:
1379my %SKIP_PROP;
1380BEGIN {
1381 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
1382 svn:special svn:executable
1383 svn:entry:committed-rev
1384 svn:entry:last-author
1385 svn:entry:uuid
1386 svn:entry:committed-date/;
Eric Wong91b03282007-02-11 00:51:33 -08001387
1388 # some options are read globally, but can be overridden locally
1389 # per [svn-remote "..."] section. Command-line options will *NOT*
1390 # override options set in an [svn-remote "..."] section
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001391 no strict 'refs';
1392 for my $option (qw/follow_parent no_metadata use_svm_props
1393 use_svnsync_props/) {
1394 my $key = $option;
Eric Wong91b03282007-02-11 00:51:33 -08001395 $key =~ tr/_//d;
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001396 my $prop = "-$option";
1397 *$option = sub {
1398 my ($self) = @_;
1399 return $self->{$prop} if exists $self->{$prop};
1400 my $k = "svn-remote.$self->{repo_id}.$key";
1401 eval { command_oneline(qw/config --get/, $k) };
1402 if ($@) {
1403 $self->{$prop} = ${"Git::SVN::_$option"};
Eric Wong91b03282007-02-11 00:51:33 -08001404 } else {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001405 my $v = command_oneline(qw/config --bool/,$k);
1406 $self->{$prop} = $v eq 'false' ? 0 : 1;
Eric Wong91b03282007-02-11 00:51:33 -08001407 }
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001408 return $self->{$prop};
1409 }
Eric Wong91b03282007-02-11 00:51:33 -08001410 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001411}
1412
Marcus Griep0b191382008-08-12 12:00:53 -04001413
Eric Wong321b1842008-01-02 10:10:03 -08001414my (%LOCKFILES, %INDEX_FILES);
1415END {
1416 unlink keys %LOCKFILES if %LOCKFILES;
1417 unlink keys %INDEX_FILES if %INDEX_FILES;
1418}
Eric Wong373274f2007-01-31 13:54:23 -08001419
Eric Wong4bb9ed02007-02-03 13:29:17 -08001420sub resolve_local_globs {
1421 my ($url, $fetch, $glob_spec) = @_;
1422 return unless defined $glob_spec;
1423 my $ref = $glob_spec->{ref};
1424 my $path = $glob_spec->{path};
1425 foreach (command(qw#for-each-ref --format=%(refname) refs/remotes#)) {
1426 next unless m#^refs/remotes/$ref->{regex}$#;
1427 my $p = $1;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001428 my $pathname = desanitize_refname($path->full_path($p));
1429 my $refname = desanitize_refname($ref->full_path($p));
Eric Wong4bb9ed02007-02-03 13:29:17 -08001430 if (my $existing = $fetch->{$pathname}) {
1431 if ($existing ne $refname) {
1432 die "Refspec conflict:\n",
1433 "existing: refs/remotes/$existing\n",
1434 " globbed: refs/remotes/$refname\n";
1435 }
1436 my $u = (::cmt_metadata("refs/remotes/$refname"))[0];
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001437 $u =~ s!^\Q$url\E(/|$)!! or die
Eric Wong4bb9ed02007-02-03 13:29:17 -08001438 "refs/remotes/$refname: '$url' not found in '$u'\n";
1439 if ($pathname ne $u) {
1440 warn "W: Refspec glob conflict ",
1441 "(ref: refs/remotes/$refname):\n",
1442 "expected path: $pathname\n",
1443 " real path: $u\n",
1444 "Continuing ahead with $u\n";
1445 next;
1446 }
1447 } else {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001448 $fetch->{$pathname} = $refname;
1449 }
1450 }
1451}
1452
Eric Wonge98671e2007-02-14 02:21:19 -08001453sub parse_revision_argument {
1454 my ($base, $head) = @_;
1455 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
1456 return ($base, $head);
1457 }
1458 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
1459 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
1460 return ($head, $head) if ($::_revision eq 'HEAD');
1461 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
1462 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
1463 die "revision argument: $::_revision not understood by git-svn\n";
1464}
1465
Eric Wong0af9c9f2007-01-27 22:28:56 -08001466sub fetch_all {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001467 my ($repo_id, $remotes) = @_;
Eric Wong905f8b72007-02-16 03:22:40 -08001468 if (ref $repo_id) {
1469 my $gs = $repo_id;
1470 $repo_id = undef;
1471 $repo_id = $gs->{repo_id};
1472 }
1473 $remotes ||= read_all_remotes();
Eric Wong7447b4b2007-02-14 18:38:46 -08001474 my $remote = $remotes->{$repo_id} or
1475 die "[svn-remote \"$repo_id\"] unknown\n";
Eric Wonge5181922007-02-08 12:53:57 -08001476 my $fetch = $remote->{fetch};
Eric Wong7447b4b2007-02-14 18:38:46 -08001477 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
Eric Wonge5181922007-02-08 12:53:57 -08001478 my (@gs, @globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001479 my $ra = Git::SVN::Ra->new($url);
Eric Wong26a62d52007-02-12 13:25:25 -08001480 my $uuid = $ra->get_uuid;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001481 my $head = $ra->get_latest_revnum;
Eric Wong28710f72007-02-14 13:32:21 -08001482 my $base = defined $fetch ? $head : 0;
Eric Wonge5181922007-02-08 12:53:57 -08001483
1484 # read the max revs for wildcard expansion (branches/*, tags/*)
1485 foreach my $t (qw/branches tags/) {
1486 defined $remote->{$t} or next;
1487 push @globs, $remote->{$t};
Eric Wong93f26892007-02-11 01:20:26 -08001488 my $max_rev = eval { tmp_config(qw/--int --get/,
1489 "svn-remote.$repo_id.${t}-maxRev") };
1490 if (defined $max_rev && ($max_rev < $base)) {
1491 $base = $max_rev;
Eric Wongd6d33462007-02-16 04:05:33 -08001492 } elsif (!defined $max_rev) {
1493 $base = 0;
Eric Wonge5181922007-02-08 12:53:57 -08001494 }
1495 }
1496
Eric Wongdb03cd22007-02-13 00:38:02 -08001497 if ($fetch) {
1498 foreach my $p (sort keys %$fetch) {
1499 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
Eric Wong060610c2007-12-08 23:27:41 -08001500 my $lr = $gs->rev_map_max;
Eric Wongdb03cd22007-02-13 00:38:02 -08001501 if (defined $lr) {
1502 $base = $lr if ($lr < $base);
1503 }
1504 push @gs, $gs;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001505 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08001506 }
Eric Wonge98671e2007-02-14 02:21:19 -08001507
1508 ($base, $head) = parse_revision_argument($base, $head);
Eric Wonge5181922007-02-08 12:53:57 -08001509 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001510}
1511
Eric Wong47e39c52007-01-21 04:27:09 -08001512sub read_all_remotes {
1513 my $r = {};
João Abecasis63c56022008-07-14 16:28:04 +01001514 my $use_svm_props = eval { command_oneline(qw/config --bool
1515 svn.useSvmProps/) };
1516 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
Eric Wong8b8fc062007-01-22 11:44:57 -08001517 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
Avery Pennarun61192162008-07-30 16:53:55 -04001518 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*(.+)\s*$!) {
1519 my ($remote, $local_ref, $_remote_ref) = ($1, $2, $3);
1520 die("svn-remote.$remote: remote ref '$_remote_ref' "
1521 . "must start with 'refs/remotes/'\n")
1522 unless $_remote_ref =~ m{^refs/remotes/(.+)};
1523 my $remote_ref = $1;
Eric Wong46cf98b2007-07-14 12:40:32 -07001524 $local_ref =~ s{^/}{};
1525 $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
João Abecasis63c56022008-07-14 16:28:04 +01001526 $r->{$remote}->{svm} = {} if $use_svm_props;
1527 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
1528 $r->{$1}->{svm} = {};
Eric Wong47e39c52007-01-21 04:27:09 -08001529 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1530 $r->{$1}->{url} = $2;
Eric Wong4bb9ed02007-02-03 13:29:17 -08001531 } elsif (m!^(.+)\.(branches|tags)=
1532 (.*):refs/remotes/(.+)\s*$/!x) {
1533 my ($p, $g) = ($3, $4);
1534 my $rs = $r->{$1}->{$2} = {
Eric Wonge5181922007-02-08 12:53:57 -08001535 t => $2,
Eric Wong93f26892007-02-11 01:20:26 -08001536 remote => $1,
Eric Wong4bb9ed02007-02-03 13:29:17 -08001537 path => Git::SVN::GlobSpec->new($p),
1538 ref => Git::SVN::GlobSpec->new($g) };
1539 if (length($rs->{ref}->{right}) != 0) {
1540 die "The '*' glob character must be the last ",
1541 "character of '$g'\n";
1542 }
Eric Wong47e39c52007-01-21 04:27:09 -08001543 }
1544 }
João Abecasis63c56022008-07-14 16:28:04 +01001545
1546 map {
1547 if (defined $r->{$_}->{svm}) {
1548 my $svm;
1549 eval {
1550 my $section = "svn-remote.$_";
1551 $svm = {
1552 source => tmp_config('--get',
1553 "$section.svm-source"),
1554 replace => tmp_config('--get',
1555 "$section.svm-replace"),
1556 }
1557 };
1558 $r->{$_}->{svm} = $svm;
1559 }
1560 } keys %$r;
1561
Eric Wong47e39c52007-01-21 04:27:09 -08001562 $r;
1563}
1564
Eric Wongecc712d2007-01-31 12:28:10 -08001565sub init_vars {
Karl Hasselström94bc9142008-02-03 17:56:18 +01001566 $_gc_nr = $_gc_period = 1000;
Karl Hasselströmaf788a62008-02-03 17:56:12 +01001567 if (defined $_repack || defined $_repack_flags) {
1568 warn "Repack options are obsolete; they have no effect.\n";
1569 }
Eric Wongecc712d2007-01-31 12:28:10 -08001570}
1571
Eric Wongb805b442007-01-22 13:52:04 -08001572sub verify_remotes_sanity {
Eric Wong536c4b02007-01-23 11:35:53 -08001573 return unless -d $ENV{GIT_DIR};
Eric Wongb805b442007-01-22 13:52:04 -08001574 my %seen;
1575 foreach (command(qw/config -l/)) {
1576 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1577 if ($seen{$1}) {
1578 die "Remote ref refs/remote/$1 is tracked by",
1579 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1580 "Please resolve this ambiguity in ",
1581 "your git configuration file before ",
1582 "continuing\n";
1583 }
1584 $seen{$1} = $_;
1585 }
1586 }
1587}
1588
Eric Wonge6434f82007-01-23 16:29:23 -08001589sub find_existing_remote {
1590 my ($url, $remotes) = @_;
Eric Wongbefc9ad2007-02-17 02:53:07 -08001591 return undef if $no_reuse_existing;
Eric Wonge6434f82007-01-23 16:29:23 -08001592 my $existing;
1593 foreach my $repo_id (keys %$remotes) {
1594 my $u = $remotes->{$repo_id}->{url} or next;
1595 next if $u ne $url;
1596 $existing = $repo_id;
1597 last;
1598 }
1599 $existing;
1600}
1601
1602sub init_remote_config {
Eric Wongd8115c52007-02-01 03:30:31 -08001603 my ($self, $url, $no_write) = @_;
Eric Wonge6434f82007-01-23 16:29:23 -08001604 $url =~ s!/+$!!; # strip trailing slash
1605 my $r = read_all_remotes();
1606 my $existing = find_existing_remote($url, $r);
1607 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001608 unless ($no_write) {
1609 print STDERR "Using existing ",
1610 "[svn-remote \"$existing\"]\n";
1611 }
Eric Wonge6434f82007-01-23 16:29:23 -08001612 $self->{repo_id} = $existing;
Eric Wong4a1bb4c2007-05-13 09:58:14 -07001613 } elsif ($_minimize_url) {
Eric Wonge6434f82007-01-23 16:29:23 -08001614 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
1615 $existing = find_existing_remote($min_url, $r);
1616 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001617 unless ($no_write) {
1618 print STDERR "Using existing ",
1619 "[svn-remote \"$existing\"]\n";
1620 }
Eric Wonge6434f82007-01-23 16:29:23 -08001621 $self->{repo_id} = $existing;
1622 }
1623 if ($min_url ne $url) {
Eric Wonge5181922007-02-08 12:53:57 -08001624 unless ($no_write) {
1625 print STDERR "Using higher level of URL: ",
1626 "$url => $min_url\n";
1627 }
Eric Wonge6434f82007-01-23 16:29:23 -08001628 my $old_path = $self->{path};
1629 $self->{path} = $url;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001630 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
Eric Wonge6434f82007-01-23 16:29:23 -08001631 if (length $old_path) {
1632 $self->{path} .= "/$old_path";
1633 }
1634 $url = $min_url;
1635 }
1636 }
1637 my $orig_url;
1638 if (!$existing) {
1639 # verify that we aren't overwriting anything:
1640 $orig_url = eval {
1641 command_oneline('config', '--get',
1642 "svn-remote.$self->{repo_id}.url")
1643 };
1644 if ($orig_url && ($orig_url ne $url)) {
1645 die "svn-remote.$self->{repo_id}.url already set: ",
1646 "$orig_url\nwanted to set to: $url\n";
1647 }
1648 }
1649 my ($xrepo_id, $xpath) = find_ref($self->refname);
1650 if (defined $xpath) {
1651 die "svn-remote.$xrepo_id.fetch already set to track ",
1652 "$xpath:refs/remotes/", $self->refname, "\n";
1653 }
Eric Wongd8115c52007-02-01 03:30:31 -08001654 unless ($no_write) {
1655 command_noisy('config',
1656 "svn-remote.$self->{repo_id}.url", $url);
Eric Wong46cf98b2007-07-14 12:40:32 -07001657 $self->{path} =~ s{^/}{};
Eric Wongd8115c52007-02-01 03:30:31 -08001658 command_noisy('config', '--add',
1659 "svn-remote.$self->{repo_id}.fetch",
1660 "$self->{path}:".$self->refname);
1661 }
Eric Wonge6434f82007-01-23 16:29:23 -08001662 $self->{url} = $url;
1663}
1664
Eric Wonga8ae2622007-02-13 14:22:11 -08001665sub find_by_url { # repos_root and, path are optional
1666 my ($class, $full_url, $repos_root, $path) = @_;
Adam Roben56973d22007-04-25 12:42:58 -07001667
Eric Wong1a97a502007-02-20 00:43:19 -08001668 return undef unless defined $full_url;
Adam Roben56973d22007-04-25 12:42:58 -07001669 remove_username($full_url);
1670 remove_username($repos_root) if defined $repos_root;
Eric Wonga8ae2622007-02-13 14:22:11 -08001671 my $remotes = read_all_remotes();
1672 if (defined $full_url && defined $repos_root && !defined $path) {
1673 $path = $full_url;
1674 $path =~ s#^\Q$repos_root\E(?:/|$)##;
1675 }
1676 foreach my $repo_id (keys %$remotes) {
1677 my $u = $remotes->{$repo_id}->{url} or next;
Adam Roben56973d22007-04-25 12:42:58 -07001678 remove_username($u);
Eric Wonga8ae2622007-02-13 14:22:11 -08001679 next if defined $repos_root && $repos_root ne $u;
1680
1681 my $fetch = $remotes->{$repo_id}->{fetch} || {};
1682 foreach (qw/branches tags/) {
1683 resolve_local_globs($u, $fetch,
1684 $remotes->{$repo_id}->{$_});
1685 }
1686 my $p = $path;
John Goerzen0bb91d92008-03-08 16:04:05 -06001687 my $rwr = rewrite_root({repo_id => $repo_id});
João Abecasis63c56022008-07-14 16:28:04 +01001688 my $svm = $remotes->{$repo_id}->{svm}
1689 if defined $remotes->{$repo_id}->{svm};
Eric Wonga8ae2622007-02-13 14:22:11 -08001690 unless (defined $p) {
1691 $p = $full_url;
John Goerzen0bb91d92008-03-08 16:04:05 -06001692 my $z = $u;
João Abecasis63c56022008-07-14 16:28:04 +01001693 my $prefix = '';
John Goerzen0bb91d92008-03-08 16:04:05 -06001694 if ($rwr) {
1695 $z = $rwr;
Dévai Tamás1b7e5432009-02-12 00:14:02 +01001696 remove_username($z);
João Abecasis63c56022008-07-14 16:28:04 +01001697 } elsif (defined $svm) {
1698 $z = $svm->{source};
1699 $prefix = $svm->{replace};
1700 $prefix =~ s#^\Q$u\E(?:/|$)##;
1701 $prefix =~ s#/$##;
John Goerzen0bb91d92008-03-08 16:04:05 -06001702 }
João Abecasis63c56022008-07-14 16:28:04 +01001703 $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next;
Eric Wonga8ae2622007-02-13 14:22:11 -08001704 }
1705 foreach my $f (keys %$fetch) {
1706 next if $f ne $p;
1707 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
1708 }
1709 }
1710 undef;
1711}
1712
Eric Wong9b981fc2007-01-11 12:14:21 -08001713sub init {
Eric Wongd8115c52007-02-01 03:30:31 -08001714 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08001715 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong9b981fc2007-01-11 12:14:21 -08001716 if (defined $url) {
Eric Wongd8115c52007-02-01 03:30:31 -08001717 $self->init_remote_config($url, $no_write);
Eric Wong9b981fc2007-01-11 12:14:21 -08001718 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001719 $self;
1720}
1721
Eric Wong706587f2007-01-18 17:50:01 -08001722sub find_ref {
1723 my ($ref_id) = @_;
1724 foreach (command(qw/config -l/)) {
1725 next unless m!^svn-remote\.(.+)\.fetch=
1726 \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x;
1727 my ($repo_id, $path, $ref) = ($1, $2, $3);
1728 if ($ref eq $ref_id) {
1729 $path = '' if ($path =~ m#^\./?#);
1730 return ($repo_id, $path);
1731 }
1732 }
1733 (undef, undef, undef);
1734}
1735
Eric Wong9b981fc2007-01-11 12:14:21 -08001736sub new {
Eric Wong706587f2007-01-18 17:50:01 -08001737 my ($class, $ref_id, $repo_id, $path) = @_;
1738 if (defined $ref_id && !defined $repo_id && !defined $path) {
1739 ($repo_id, $path) = find_ref($ref_id);
1740 if (!defined $repo_id) {
1741 die "Could not find a \"svn-remote.*.fetch\" key ",
1742 "in the repository configuration matching: ",
1743 "refs/remotes/$ref_id\n";
1744 }
1745 }
1746 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong8b8fc062007-01-22 11:44:57 -08001747 if (!defined $self->{path} || !length $self->{path}) {
1748 my $fetch = command_oneline('config', '--get',
1749 "svn-remote.$repo_id.fetch",
1750 ":refs/remotes/$ref_id\$") or
1751 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1752 "\":refs/remotes/$ref_id\$\" in config\n";
1753 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
1754 }
Eric Wong706587f2007-01-18 17:50:01 -08001755 $self->{url} = command_oneline('config', '--get',
1756 "svn-remote.$repo_id.url") or
1757 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
Eric Wongd6d33462007-02-16 04:05:33 -08001758 $self->rebuild;
Eric Wong9b981fc2007-01-11 12:14:21 -08001759 $self;
1760}
1761
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001762sub refname {
1763 my ($refname) = "refs/remotes/$_[0]->{ref_id}" ;
1764
1765 # It cannot end with a slash /, we'll throw up on this because
1766 # SVN can't have directories with a slash in their name, either:
1767 if ($refname =~ m{/$}) {
1768 die "ref: '$refname' ends with a trailing slash, this is ",
1769 "not permitted by git nor Subversion\n";
1770 }
1771
1772 # It cannot have ASCII control character space, tilde ~, caret ^,
1773 # colon :, question-mark ?, asterisk *, space, or open bracket [
1774 # anywhere.
1775 #
1776 # Additionally, % must be escaped because it is used for escaping
1777 # and we want our escaped refname to be reversible
1778 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
1779
1780 # no slash-separated component can begin with a dot .
1781 # /.* becomes /%2E*
1782 $refname =~ s{/\.}{/%2E}g;
1783
1784 # It cannot have two consecutive dots .. anywhere
1785 # .. becomes %2E%2E
1786 $refname =~ s{\.\.}{%2E%2E}g;
1787
1788 return $refname;
1789}
1790
1791sub desanitize_refname {
1792 my ($refname) = @_;
1793 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
1794 return $refname;
1795}
Eric Wong9b981fc2007-01-11 12:14:21 -08001796
Eric Wong26a62d52007-02-12 13:25:25 -08001797sub svm_uuid {
1798 my ($self) = @_;
1799 return $self->{svm}->{uuid} if $self->svm;
1800 $self->ra;
1801 unless ($self->{svm}) {
1802 die "SVM UUID not cached, and reading remotely failed\n";
1803 }
1804 $self->{svm}->{uuid};
1805}
Eric Wong8a49ee92007-02-10 20:46:50 -08001806
Eric Wong26a62d52007-02-12 13:25:25 -08001807sub svm {
1808 my ($self) = @_;
1809 return $self->{svm} if $self->{svm};
1810 my $svm;
Eric Wong8a49ee92007-02-10 20:46:50 -08001811 # see if we have it in our config, first:
1812 eval {
Eric Wong26a62d52007-02-12 13:25:25 -08001813 my $section = "svn-remote.$self->{repo_id}";
1814 $svm = {
Eric Wong93f26892007-02-11 01:20:26 -08001815 source => tmp_config('--get', "$section.svm-source"),
1816 uuid => tmp_config('--get', "$section.svm-uuid"),
Eric Wongbefc9ad2007-02-17 02:53:07 -08001817 replace => tmp_config('--get', "$section.svm-replace"),
Eric Wong8a49ee92007-02-10 20:46:50 -08001818 }
1819 };
Eric Wongbefc9ad2007-02-17 02:53:07 -08001820 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
1821 $self->{svm} = $svm;
1822 }
Eric Wong26a62d52007-02-12 13:25:25 -08001823 $self->{svm};
1824}
1825
1826sub _set_svm_vars {
1827 my ($self, $ra) = @_;
Eric Wongdb03cd22007-02-13 00:38:02 -08001828 return $ra if $self->svm;
Eric Wong26a62d52007-02-12 13:25:25 -08001829
Eric Wongdb03cd22007-02-13 00:38:02 -08001830 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08001831 "(svm:source, svm:uuid) ",
Eric Wongdb03cd22007-02-13 00:38:02 -08001832 "from the following URLs:\n" );
1833 sub read_svm_props {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001834 my ($self, $ra, $path, $r) = @_;
1835 my $props = ($ra->get_dir($path, $r))[2];
Eric Wongdb03cd22007-02-13 00:38:02 -08001836 my $src = $props->{'svm:source'};
Eric Wong8a49ee92007-02-10 20:46:50 -08001837 my $uuid = $props->{'svm:uuid'};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001838 return undef if (!$src || !$uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08001839
Eric Wongbefc9ad2007-02-17 02:53:07 -08001840 chomp($src, $uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08001841
Eric Wong8a49ee92007-02-10 20:46:50 -08001842 $uuid =~ m{^[0-9a-f\-]{30,}$}
1843 or die "doesn't look right - svm:uuid is '$uuid'\n";
Eric Wongbefc9ad2007-02-17 02:53:07 -08001844
1845 # the '!' is used to mark the repos_root!/relative/path
1846 $src =~ s{/?!/?}{/};
Eric Wongdb03cd22007-02-13 00:38:02 -08001847 $src =~ s{/+$}{}; # no trailing slashes please
Eric Wongbefc9ad2007-02-17 02:53:07 -08001848 # username is of no interest
Eric Wongdb03cd22007-02-13 00:38:02 -08001849 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
Eric Wong8a49ee92007-02-10 20:46:50 -08001850
Eric Wongbefc9ad2007-02-17 02:53:07 -08001851 my $replace = $ra->{url};
1852 $replace .= "/$path" if length $path;
1853
Eric Wongdb03cd22007-02-13 00:38:02 -08001854 my $section = "svn-remote.$self->{repo_id}";
Eric Wongbefc9ad2007-02-17 02:53:07 -08001855 tmp_config("$section.svm-source", $src);
1856 tmp_config("$section.svm-replace", $replace);
1857 tmp_config("$section.svm-uuid", $uuid);
1858 $self->{svm} = {
1859 source => $src,
1860 uuid => $uuid,
1861 replace => $replace
1862 };
Eric Wong8a49ee92007-02-10 20:46:50 -08001863 }
Eric Wongdb03cd22007-02-13 00:38:02 -08001864
1865 my $r = $ra->get_latest_revnum;
1866 my $path = $self->{path};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001867 my %tried;
Eric Wongdb03cd22007-02-13 00:38:02 -08001868 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001869 unless ($tried{"$self->{url}/$path"}) {
1870 return $ra if $self->read_svm_props($ra, $path, $r);
1871 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001872 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001873 $path =~ s#/?[^/]+$##;
Eric Wong8a49ee92007-02-10 20:46:50 -08001874 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001875 die "Path: '$path' should be ''\n" if $path ne '';
1876 return $ra if $self->read_svm_props($ra, $path, $r);
1877 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001878
1879 if ($ra->{repos_root} eq $self->{url}) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001880 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001881 }
1882
1883 # nope, make sure we're connected to the repository root:
1884 my $ok;
1885 my @tried_b;
1886 $path = $ra->{svn_path};
Eric Wongdb03cd22007-02-13 00:38:02 -08001887 $ra = Git::SVN::Ra->new($ra->{repos_root});
1888 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001889 unless ($tried{"$ra->{url}/$path"}) {
1890 $ok = $self->read_svm_props($ra, $path, $r);
1891 last if $ok;
1892 $tried{"$ra->{url}/$path"} = 1;
1893 }
1894 $path =~ s#/?[^/]+$##;
Eric Wongdb03cd22007-02-13 00:38:02 -08001895 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001896 die "Path: '$path' should be ''\n" if $path ne '';
1897 $ok ||= $self->read_svm_props($ra, $path, $r);
1898 $tried{"$ra->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001899 if (!$ok) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001900 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001901 }
1902 Git::SVN::Ra->new($self->{url});
Eric Wong8a49ee92007-02-10 20:46:50 -08001903}
1904
Eric Wong62e349d2007-02-16 19:57:29 -08001905sub svnsync {
1906 my ($self) = @_;
1907 return $self->{svnsync} if $self->{svnsync};
1908
1909 if ($self->no_metadata) {
1910 die "Can't have both 'noMetadata' and ",
1911 "'useSvnsyncProps' options set!\n";
1912 }
1913 if ($self->rewrite_root) {
1914 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1915 "options set!\n";
1916 }
1917
1918 my $svnsync;
1919 # see if we have it in our config, first:
1920 eval {
1921 my $section = "svn-remote.$self->{repo_id}";
Eric Wong98fa5b62008-01-11 23:13:55 -08001922
1923 my $url = tmp_config('--get', "$section.svnsync-url");
1924 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
1925 die "doesn't look right - svn:sync-from-url is '$url'\n";
1926
1927 my $uuid = tmp_config('--get', "$section.svnsync-uuid");
1928 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
1929 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1930
1931 $svnsync = { url => $url, uuid => $uuid }
Eric Wong62e349d2007-02-16 19:57:29 -08001932 };
1933 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
1934 return $self->{svnsync} = $svnsync;
1935 }
1936
1937 my $err = "useSvnsyncProps set, but failed to read " .
1938 "svnsync property: svn:sync-from-";
1939 my $rp = $self->ra->rev_proplist(0);
1940
1941 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
Eric Wong98fa5b62008-01-11 23:13:55 -08001942 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
Eric Wong62e349d2007-02-16 19:57:29 -08001943 die "doesn't look right - svn:sync-from-url is '$url'\n";
1944
1945 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
Eric Wong98fa5b62008-01-11 23:13:55 -08001946 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
Eric Wong62e349d2007-02-16 19:57:29 -08001947 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1948
1949 my $section = "svn-remote.$self->{repo_id}";
1950 tmp_config('--add', "$section.svnsync-uuid", $uuid);
1951 tmp_config('--add', "$section.svnsync-url", $url);
1952 return $self->{svnsync} = { url => $url, uuid => $uuid };
1953}
1954
Eric Wong26a62d52007-02-12 13:25:25 -08001955# this allows us to memoize our SVN::Ra UUID locally and avoid a
1956# remote lookup (useful for 'git svn log').
1957sub ra_uuid {
1958 my ($self) = @_;
1959 unless ($self->{ra_uuid}) {
1960 my $key = "svn-remote.$self->{repo_id}.uuid";
1961 my $uuid = eval { tmp_config('--get', $key) };
1962 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
1963 $self->{ra_uuid} = $uuid;
1964 } else {
1965 die "ra_uuid called without URL\n" unless $self->{url};
1966 $self->{ra_uuid} = $self->ra->get_uuid;
1967 tmp_config('--add', $key, $self->{ra_uuid});
1968 }
1969 }
1970 $self->{ra_uuid};
1971}
1972
Eric Wonga5460eb2007-11-21 18:20:57 -08001973sub _set_repos_root {
1974 my ($self, $repos_root) = @_;
1975 my $k = "svn-remote.$self->{repo_id}.reposRoot";
1976 $repos_root ||= $self->ra->{repos_root};
1977 tmp_config($k, $repos_root);
1978 $repos_root;
1979}
1980
1981sub repos_root {
1982 my ($self) = @_;
1983 my $k = "svn-remote.$self->{repo_id}.reposRoot";
1984 eval { tmp_config('--get', $k) } || $self->_set_repos_root;
1985}
1986
Eric Wong9b981fc2007-01-11 12:14:21 -08001987sub ra {
1988 my ($self) = shift;
Eric Wong8a49ee92007-02-10 20:46:50 -08001989 my $ra = Git::SVN::Ra->new($self->{url});
Eric Wonga5460eb2007-11-21 18:20:57 -08001990 $self->_set_repos_root($ra->{repos_root});
Eric Wong91b03282007-02-11 00:51:33 -08001991 if ($self->use_svm_props && !$self->{svm}) {
1992 if ($self->no_metadata) {
Eric Wong97ae0912007-02-11 15:21:24 -08001993 die "Can't have both 'noMetadata' and ",
1994 "'useSvmProps' options set!\n";
Eric Wong62e349d2007-02-16 19:57:29 -08001995 } elsif ($self->use_svnsync_props) {
1996 die "Can't have both 'useSvnsyncProps' and ",
1997 "'useSvmProps' options set!\n";
Eric Wong91b03282007-02-11 00:51:33 -08001998 }
Eric Wong26a62d52007-02-12 13:25:25 -08001999 $ra = $self->_set_svm_vars($ra);
Eric Wong8a49ee92007-02-10 20:46:50 -08002000 $self->{-want_revprops} = 1;
2001 }
2002 $ra;
Eric Wong9b981fc2007-01-11 12:14:21 -08002003}
2004
Eric Wong15710b62007-01-22 02:20:33 -08002005sub rel_path {
2006 my ($self) = @_;
2007 my $repos_root = $self->ra->{repos_root};
2008 return $self->{path} if ($self->{url} eq $repos_root);
Eric Wong0b594512007-03-25 16:35:31 -07002009 my $url = $self->{url} .
2010 (length $self->{path} ? "/$self->{path}" : $self->{path});
2011 $url =~ s!^\Q$repos_root\E(?:/+|$)!!g;
2012 $url;
Eric Wong15710b62007-01-22 02:20:33 -08002013}
2014
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002015# prop_walk(PATH, REV, SUB)
2016# -------------------------
2017# Recursively traverse PATH at revision REV and invoke SUB for each
2018# directory that contains a SVN property. SUB will be invoked as
2019# follows: &SUB(gs, path, props); where `gs' is this instance of
2020# Git::SVN, `path' the path to the directory where the properties
2021# `props' were found. The `path' will be relative to point of checkout,
2022# that is, if url://repo/trunk is the current Git branch, and that
2023# directory contains a sub-directory `d', SUB will be invoked with `/d/'
2024# as `path' (note the trailing `/').
2025sub prop_walk {
2026 my ($self, $path, $rev, $sub) = @_;
2027
Kevin Ballard35cda062008-01-09 01:37:20 -05002028 $path =~ s#^/##;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002029 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
2030 $path =~ s#^/*#/#g;
Eric Wong9b981fc2007-01-11 12:14:21 -08002031 my $p = $path;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002032 # Strip the irrelevant part of the path.
2033 $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
2034 # Ensure the path is terminated by a `/'.
2035 $p =~ s#/*$#/#;
2036
2037 # The properties contain all the internal SVN stuff nobody
2038 # (usually) cares about.
2039 my $interesting_props = 0;
2040 foreach (keys %{$props}) {
2041 # If it doesn't start with `svn:', it must be a
2042 # user-defined property.
2043 ++$interesting_props and next if $_ !~ /^svn:/;
2044 # FIXME: Fragile, if SVN adds new public properties,
2045 # this needs to be updated.
2046 ++$interesting_props if /^svn:(?:ignore|keywords|executable
2047 |eol-style|mime-type
2048 |externals|needs-lock)$/x;
Eric Wong9b981fc2007-01-11 12:14:21 -08002049 }
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002050 &$sub($self, $p, $props) if $interesting_props;
2051
Eric Wong9b981fc2007-01-11 12:14:21 -08002052 foreach (sort keys %$dirent) {
Eric Wong0dc03d62007-05-13 01:04:43 -07002053 next if $dirent->{$_}->{kind} != $SVN::Node::dir;
Christian Engwerb7166cc2008-05-27 08:46:55 +00002054 $self->prop_walk($self->{path} . $p . $_, $rev, $sub);
Eric Wong9b981fc2007-01-11 12:14:21 -08002055 }
2056}
2057
Eric Wong3ebe8df2007-01-25 17:35:40 -08002058sub last_rev { ($_[0]->last_rev_commit)[0] }
2059sub last_commit { ($_[0]->last_rev_commit)[1] }
2060
Eric Wong9b981fc2007-01-11 12:14:21 -08002061# returns the newest SVN revision number and newest commit SHA1
2062sub last_rev_commit {
2063 my ($self) = @_;
2064 if (defined $self->{last_rev} && defined $self->{last_commit}) {
2065 return ($self->{last_rev}, $self->{last_commit});
2066 }
Eric Wongd2866f92007-01-11 12:26:16 -08002067 my $c = ::verify_ref($self->refname.'^0');
Eric Wong91b03282007-02-11 00:51:33 -08002068 if ($c && !$self->use_svm_props && !$self->no_metadata) {
Eric Wongd2866f92007-01-11 12:26:16 -08002069 my $rev = (::cmt_metadata($c))[1];
Eric Wong9b981fc2007-01-11 12:14:21 -08002070 if (defined $rev) {
2071 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2072 return ($rev, $c);
2073 }
2074 }
Eric Wong060610c2007-12-08 23:27:41 -08002075 my $map_path = $self->map_path;
2076 unless (-e $map_path) {
Eric Wong26a62d52007-02-12 13:25:25 -08002077 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
2078 return (undef, undef);
2079 }
Eric Wong66ab84b2007-12-08 23:27:42 -08002080 my ($rev, $commit) = $self->rev_map_max(1);
Eric Wong060610c2007-12-08 23:27:41 -08002081 ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
2082 return ($rev, $commit);
Eric Wong9b981fc2007-01-11 12:14:21 -08002083}
2084
Eric Wong3ebe8df2007-01-25 17:35:40 -08002085sub get_fetch_range {
2086 my ($self, $min, $max) = @_;
2087 $max ||= $self->ra->get_latest_revnum;
Eric Wong060610c2007-12-08 23:27:41 -08002088 $min ||= $self->rev_map_max;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002089 (++$min, $max);
Eric Wong9b981fc2007-01-11 12:14:21 -08002090}
2091
Eric Wong8a49ee92007-02-10 20:46:50 -08002092sub tmp_config {
Eric Wong93f26892007-02-11 01:20:26 -08002093 my (@args) = @_;
Eric Wongb7e53482007-02-16 04:09:28 -08002094 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
2095 my $config = "$ENV{GIT_DIR}/svn/.metadata";
Eric Wong38570a42007-06-13 02:37:05 -07002096 if (! -f $config && -f $old_def_config) {
Eric Wongb7e53482007-02-16 04:09:28 -08002097 rename $old_def_config, $config or
2098 die "Failed rename $old_def_config => $config: $!\n";
2099 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002100 my $old_config = $ENV{GIT_CONFIG};
Eric Wong93f26892007-02-11 01:20:26 -08002101 $ENV{GIT_CONFIG} = $config;
Eric Wong8a49ee92007-02-10 20:46:50 -08002102 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002103 my @ret = eval {
2104 unless (-f $config) {
2105 mkfile($config);
2106 open my $fh, '>', $config or
2107 die "Can't open $config: $!\n";
2108 print $fh "; This file is used internally by ",
2109 "git-svn\n" or die
2110 "Couldn't write to $config: $!\n";
2111 print $fh "; You should not have to edit it\n" or
2112 die "Couldn't write to $config: $!\n";
2113 close $fh or die "Couldn't close $config: $!\n";
2114 }
2115 command('config', @args);
2116 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002117 my $err = $@;
2118 if (defined $old_config) {
2119 $ENV{GIT_CONFIG} = $old_config;
2120 } else {
2121 delete $ENV{GIT_CONFIG};
2122 }
2123 die $err if $err;
2124 wantarray ? @ret : $ret[0];
2125}
2126
Eric Wong9b981fc2007-01-11 12:14:21 -08002127sub tmp_index_do {
2128 my ($self, $sub) = @_;
2129 my $old_index = $ENV{GIT_INDEX_FILE};
2130 $ENV{GIT_INDEX_FILE} = $self->{index};
Eric Wong8a49ee92007-02-10 20:46:50 -08002131 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002132 my @ret = eval {
2133 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
2134 mkpath([$dir]) unless -d $dir;
2135 &$sub;
2136 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002137 my $err = $@;
2138 if (defined $old_index) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002139 $ENV{GIT_INDEX_FILE} = $old_index;
2140 } else {
2141 delete $ENV{GIT_INDEX_FILE};
2142 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002143 die $err if $err;
Eric Wong9b981fc2007-01-11 12:14:21 -08002144 wantarray ? @ret : $ret[0];
2145}
2146
2147sub assert_index_clean {
2148 my ($self, $treeish) = @_;
2149
2150 $self->tmp_index_do(sub {
2151 command_noisy('read-tree', $treeish) unless -e $self->{index};
2152 my $x = command_oneline('write-tree');
2153 my ($y) = (command(qw/cat-file commit/, $treeish) =~
2154 /^tree ($::sha1)/mo);
Eric Wonge8d120b2007-02-14 16:29:52 -08002155 return if $y eq $x;
2156
2157 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2158 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2159 command_noisy('read-tree', $treeish);
Eric Wong9b981fc2007-01-11 12:14:21 -08002160 $x = command_oneline('write-tree');
2161 if ($y ne $x) {
2162 ::fatal "trees ($treeish) $y != $x\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02002163 "Something is seriously wrong...";
Eric Wong9b981fc2007-01-11 12:14:21 -08002164 }
2165 });
2166}
2167
2168sub get_commit_parents {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002169 my ($self, $log_entry) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002170 my (%seen, @ret, @tmp);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002171 # legacy support for 'set-tree'; this is only used by set_tree_cb:
2172 if (my $ip = $self->{inject_parents}) {
2173 if (my $commit = delete $ip->{$log_entry->{revision}}) {
2174 push @tmp, $commit;
Eric Wong9b981fc2007-01-11 12:14:21 -08002175 }
2176 }
Eric Wongd2866f92007-01-11 12:26:16 -08002177 if (my $cur = ::verify_ref($self->refname.'^0')) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002178 push @tmp, $cur;
2179 }
Eric Wong733a65a2007-06-13 02:23:28 -07002180 if (my $ipd = $self->{inject_parents_dcommit}) {
2181 if (my $commit = delete $ipd->{$log_entry->{revision}}) {
2182 push @tmp, @$commit;
2183 }
2184 }
Eric Wong44320b92007-01-13 22:35:53 -08002185 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
Eric Wong9b981fc2007-01-11 12:14:21 -08002186 while (my $p = shift @tmp) {
2187 next if $seen{$p};
2188 $seen{$p} = 1;
2189 push @ret, $p;
2190 # MAXPARENT is defined to 16 in commit-tree.c:
2191 last if @ret >= 16;
2192 }
2193 if (@tmp) {
Eric Wong44320b92007-01-13 22:35:53 -08002194 die "r$log_entry->{revision}: No room for parents:\n\t",
Eric Wong9b981fc2007-01-11 12:14:21 -08002195 join("\n\t", @tmp), "\n";
2196 }
2197 @ret;
2198}
2199
Eric Wongaea736c2007-02-16 19:15:21 -08002200sub rewrite_root {
2201 my ($self) = @_;
2202 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
2203 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2204 my $rwr = eval { command_oneline(qw/config --get/, $k) };
2205 if ($rwr) {
2206 $rwr =~ s#/+$##;
2207 if ($rwr !~ m#^[a-z\+]+://#) {
2208 die "$rwr is not a valid URL (key: $k)\n";
2209 }
2210 }
2211 $self->{-rewrite_root} = $rwr;
2212}
2213
2214sub metadata_url {
2215 my ($self) = @_;
2216 ($self->rewrite_root || $self->{url}) .
2217 (length $self->{path} ? '/' . $self->{path} : '');
2218}
2219
Eric Wong706587f2007-01-18 17:50:01 -08002220sub full_url {
Eric Wong9b981fc2007-01-11 12:14:21 -08002221 my ($self) = @_;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002222 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
Eric Wong9b981fc2007-01-11 12:14:21 -08002223}
2224
Eric Wongad948022007-12-15 19:08:22 -08002225
2226sub set_commit_header_env {
2227 my ($log_entry) = @_;
2228 my %env;
2229 foreach my $ned (qw/NAME EMAIL DATE/) {
2230 foreach my $ac (qw/AUTHOR COMMITTER/) {
2231 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2232 }
2233 }
2234
2235 $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
2236 $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
2237 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
2238
2239 $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
2240 ? $log_entry->{commit_name}
2241 : $log_entry->{name};
2242 $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
2243 ? $log_entry->{commit_email}
2244 : $log_entry->{email};
2245 \%env;
2246}
2247
2248sub restore_commit_header_env {
2249 my ($env) = @_;
2250 foreach my $ned (qw/NAME EMAIL DATE/) {
2251 foreach my $ac (qw/AUTHOR COMMITTER/) {
2252 my $k = "GIT_${ac}_${ned}";
2253 if (defined $env->{$k}) {
2254 $ENV{$k} = $env->{$k};
2255 } else {
2256 delete $ENV{$k};
2257 }
2258 }
2259 }
2260}
2261
Karl Hasselström94bc9142008-02-03 17:56:18 +01002262sub gc {
2263 command_noisy('gc', '--auto');
2264};
2265
Eric Wong9b981fc2007-01-11 12:14:21 -08002266sub do_git_commit {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002267 my ($self, $log_entry) = @_;
Eric Wong8a603772007-01-31 02:45:50 -08002268 my $lr = $self->last_rev;
2269 if (defined $lr && $lr >= $log_entry->{revision}) {
2270 die "Last fetched revision of ", $self->refname,
2271 " was r$lr, but we are about to fetch: ",
2272 "r$log_entry->{revision}!\n";
2273 }
Eric Wong060610c2007-12-08 23:27:41 -08002274 if (my $c = $self->rev_map_get($log_entry->{revision})) {
Eric Wong44320b92007-01-13 22:35:53 -08002275 croak "$log_entry->{revision} = $c already exists! ",
Eric Wong9b981fc2007-01-11 12:14:21 -08002276 "Why are we refetching it?\n";
2277 }
Eric Wongad948022007-12-15 19:08:22 -08002278 my $old_env = set_commit_header_env($log_entry);
Eric Wong44320b92007-01-13 22:35:53 -08002279 my $tree = $log_entry->{tree};
Eric Wong9b981fc2007-01-11 12:14:21 -08002280 if (!defined $tree) {
2281 $tree = $self->tmp_index_do(sub {
2282 command_oneline('write-tree') });
2283 }
2284 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2285
Deskin Millere855bfc2008-10-31 00:10:25 -04002286 my @exec = ('git', 'commit-tree', $tree);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002287 foreach ($self->get_commit_parents($log_entry)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002288 push @exec, '-p', $_;
2289 }
2290 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2291 or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07002292 binmode $msg_fh;
2293
2294 # we always get UTF-8 from SVN, but we may want our commits in
2295 # a different encoding.
2296 if (my $enc = Git::config('i18n.commitencoding')) {
2297 require Encode;
2298 Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
2299 }
Eric Wong44320b92007-01-13 22:35:53 -08002300 print $msg_fh $log_entry->{log} or croak $!;
Eric Wongad948022007-12-15 19:08:22 -08002301 restore_commit_header_env($old_env);
Eric Wong91b03282007-02-11 00:51:33 -08002302 unless ($self->no_metadata) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002303 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2304 or croak $!;
Eric Wong9760adc2007-01-31 03:06:56 -08002305 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002306 $msg_fh->flush == 0 or croak $!;
2307 close $msg_fh or croak $!;
2308 chomp(my $commit = do { local $/; <$out_fh> });
2309 close $out_fh or croak $!;
2310 waitpid $pid, 0;
2311 croak $? if $?;
2312 if ($commit !~ /^$::sha1$/o) {
2313 die "Failed to commit, invalid sha1: $commit\n";
2314 }
2315
Eric Wong060610c2007-12-08 23:27:41 -08002316 $self->rev_map_set($log_entry->{revision}, $commit, 1);
Eric Wong9b981fc2007-01-11 12:14:21 -08002317
Eric Wong44320b92007-01-13 22:35:53 -08002318 $self->{last_rev} = $log_entry->{revision};
Eric Wong9b981fc2007-01-11 12:14:21 -08002319 $self->{last_commit} = $commit;
Eric Wong8a49ee92007-02-10 20:46:50 -08002320 print "r$log_entry->{revision}";
2321 if (defined $log_entry->{svm_revision}) {
2322 print " (\@$log_entry->{svm_revision})";
Eric Wong060610c2007-12-08 23:27:41 -08002323 $self->rev_map_set($log_entry->{svm_revision}, $commit,
Eric Wong26a62d52007-02-12 13:25:25 -08002324 0, $self->svm_uuid);
Eric Wong8a49ee92007-02-10 20:46:50 -08002325 }
2326 print " = $commit ($self->{ref_id})\n";
Karl Hasselström94bc9142008-02-03 17:56:18 +01002327 if (--$_gc_nr == 0) {
2328 $_gc_nr = $_gc_period;
2329 gc();
2330 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002331 return $commit;
2332}
2333
Eric Wongfbcc1732007-02-06 18:35:30 -08002334sub match_paths {
2335 my ($self, $paths, $r) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002336 return 1 if $self->{path} eq '';
Eric Wongd542aed2007-02-09 02:19:41 -08002337 if (my $path = $paths->{"/$self->{path}"}) {
2338 return ($path->{action} eq 'D') ? 0 : 1;
2339 }
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002340 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
Eric Wongfbcc1732007-02-06 18:35:30 -08002341 if (grep /$self->{path_regex}/, keys %$paths) {
2342 return 1;
2343 }
2344 my $c = '';
2345 foreach (split m#/#, $self->{path}) {
2346 $c .= "/$_";
Eric Wong74a81222007-02-10 13:28:50 -08002347 next unless ($paths->{$c} &&
2348 ($paths->{$c}->{action} =~ /^[AR]$/));
Eric Wonge5181922007-02-08 12:53:57 -08002349 if ($self->ra->check_path($self->{path}, $r) ==
2350 $SVN::Node::dir) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002351 return 1;
2352 }
2353 }
2354 return 0;
2355}
2356
Eric Wong15710b62007-01-22 02:20:33 -08002357sub find_parent_branch {
2358 my ($self, $paths, $rev) = @_;
Eric Wong91b03282007-02-11 00:51:33 -08002359 return undef unless $self->follow_parent;
Eric Wonge5a0b242007-01-25 15:44:54 -08002360 unless (defined $paths) {
Eric Wongc7eba712007-01-31 03:45:28 -08002361 my $err_handler = $SVN::Error::handler;
2362 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
Eric Wongd4eff2b2007-01-30 14:04:22 -08002363 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, sub {
2364 $paths =
2365 Git::SVN::Ra::dup_changed_paths($_[0]) });
Eric Wongc7eba712007-01-31 03:45:28 -08002366 $SVN::Error::handler = $err_handler;
Eric Wonge5a0b242007-01-25 15:44:54 -08002367 }
2368 return undef unless defined $paths;
Eric Wong15710b62007-01-22 02:20:33 -08002369
2370 # look for a parent from another branch:
Eric Wong7f578c52007-01-24 02:16:25 -08002371 my @b_path_components = split m#/#, $self->rel_path;
2372 my @a_path_components;
2373 my $i;
2374 while (@b_path_components) {
2375 $i = $paths->{'/'.join('/', @b_path_components)};
Eric Wong74a81222007-02-10 13:28:50 -08002376 last if $i && defined $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002377 unshift(@a_path_components, pop(@b_path_components));
2378 }
Eric Wong74a81222007-02-10 13:28:50 -08002379 return undef unless defined $i && defined $i->{copyfrom_path};
2380 my $branch_from = $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002381 if (@a_path_components) {
2382 print STDERR "branch_from: $branch_from => ";
2383 $branch_from .= '/'.join('/', @a_path_components);
2384 print STDERR $branch_from, "\n";
2385 }
Eric Wong3ebe8df2007-01-25 17:35:40 -08002386 my $r = $i->{copyfrom_rev};
Eric Wong15710b62007-01-22 02:20:33 -08002387 my $repos_root = $self->ra->{repos_root};
2388 my $url = $self->ra->{url};
2389 my $new_url = $repos_root . $branch_from;
2390 print STDERR "Found possible branch point: ",
2391 "$new_url => ", $self->full_url, ", $r\n";
2392 $branch_from =~ s#^/##;
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002393 my $gs = $self->other_gs($new_url, $url, $repos_root,
2394 $branch_from, $r, $self->{ref_id});
Eric Wong15710b62007-01-22 02:20:33 -08002395 my ($r0, $parent) = $gs->find_rev_before($r, 1);
Deskin Miller553589f2008-12-08 08:31:31 -05002396 {
2397 my ($base, $head);
2398 if (!defined $r0 || !defined $parent) {
2399 ($base, $head) = parse_revision_argument(0, $r);
2400 } else {
2401 if ($r0 < $r) {
2402 $gs->ra->get_log([$gs->{path}], $r0 + 1, $r, 1,
2403 0, 1, sub { $base = $_[1] - 1 });
2404 }
2405 }
2406 if (defined $base && $base <= $r) {
Eric Wongd627de62007-04-15 03:01:29 -07002407 $gs->fetch($base, $r);
2408 }
Deskin Miller553589f2008-12-08 08:31:31 -05002409 ($r0, $parent) = $gs->find_rev_before($r, 1);
Eric Wong15710b62007-01-22 02:20:33 -08002410 }
Eric Wongef70de92007-02-01 04:12:41 -08002411 if (defined $r0 && defined $parent) {
Eric Wong15710b62007-01-22 02:20:33 -08002412 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n";
Eric Wong15710b62007-01-22 02:20:33 -08002413 my $ed;
2414 if ($self->ra->can_do_switch) {
Eric Wong2e5e2482007-02-23 02:21:59 -08002415 $self->assert_index_clean($parent);
Eric Wong8b8fc062007-01-22 11:44:57 -08002416 print STDERR "Following parent with do_switch\n";
Eric Wong15710b62007-01-22 02:20:33 -08002417 # do_switch works with svn/trunk >= r22312, but that
Eric Wong2b27f6c2007-01-28 04:59:05 -08002418 # is not included with SVN 1.4.3 (the latest version
Eric Wong15710b62007-01-22 02:20:33 -08002419 # at the moment), so we can't rely on it
2420 $self->{last_commit} = $parent;
Eric Wong8841b372009-02-11 01:56:58 -08002421 $ed = SVN::Git::Fetcher->new($self, $gs->{path});
Eric Wong8a603772007-01-31 02:45:50 -08002422 $gs->ra->gs_do_switch($r0, $rev, $gs,
Eric Wong15710b62007-01-22 02:20:33 -08002423 $self->full_url, $ed)
2424 or die "SVN connection failed somewhere...\n";
Steven Walter9ff74e92007-09-28 13:24:19 -04002425 } elsif ($self->ra->trees_match($new_url, $r0,
2426 $self->full_url, $rev)) {
2427 print STDERR "Trees match:\n",
2428 " $new_url\@$r0\n",
2429 " ${\$self->full_url}\@$rev\n",
2430 "Following parent with no changes\n";
2431 $self->tmp_index_do(sub {
2432 command_noisy('read-tree', $parent);
2433 });
2434 $self->{last_commit} = $parent;
Eric Wong15710b62007-01-22 02:20:33 -08002435 } else {
Eric Wong8b8fc062007-01-22 11:44:57 -08002436 print STDERR "Following parent with do_update\n";
Eric Wong15710b62007-01-22 02:20:33 -08002437 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08002438 $self->ra->gs_do_update($rev, $rev, $self, $ed)
Eric Wong15710b62007-01-22 02:20:33 -08002439 or die "SVN connection failed somewhere...\n";
2440 }
Eric Wongf7c3fc42007-01-29 18:34:55 -08002441 print STDERR "Successfully followed parent\n";
Eric Wong15710b62007-01-22 02:20:33 -08002442 return $self->make_log_entry($rev, [$parent], $ed);
2443 }
Eric Wong15710b62007-01-22 02:20:33 -08002444 return undef;
2445}
2446
Eric Wong9b981fc2007-01-11 12:14:21 -08002447sub do_fetch {
Eric Wong706587f2007-01-18 17:50:01 -08002448 my ($self, $paths, $rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08002449 my $ed;
Eric Wong9b981fc2007-01-11 12:14:21 -08002450 my ($last_rev, @parents);
Eric Wongb9dffd82007-02-09 01:28:30 -08002451 if (my $lc = $self->last_commit) {
2452 # we can have a branch that was deleted, then re-added
2453 # under the same name but copied from another path, in
2454 # which case we'll have multiple parents (we don't
2455 # want to break the original ref, nor lose copypath info):
2456 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2457 push @{$log_entry->{parents}}, $lc;
2458 return $log_entry;
2459 }
Eric Wong15710b62007-01-22 02:20:33 -08002460 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002461 $last_rev = $self->{last_rev};
Eric Wongb9dffd82007-02-09 01:28:30 -08002462 $ed->{c} = $lc;
2463 @parents = ($lc);
Eric Wong9b981fc2007-01-11 12:14:21 -08002464 } else {
2465 $last_rev = $rev;
Eric Wong15710b62007-01-22 02:20:33 -08002466 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2467 return $log_entry;
2468 }
2469 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002470 }
Eric Wong8a603772007-01-31 02:45:50 -08002471 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002472 die "SVN connection failed somewhere...\n";
2473 }
2474 $self->make_log_entry($rev, \@parents, $ed);
2475}
2476
Eric Wong97f69872007-01-25 11:53:13 -08002477sub get_untracked {
2478 my ($self, $ed) = @_;
2479 my @out;
2480 my $h = $ed->{empty};
Eric Wong9b981fc2007-01-11 12:14:21 -08002481 foreach (sort keys %$h) {
2482 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
Eric Wong97f69872007-01-25 11:53:13 -08002483 push @out, " $act: " . uri_encode($_);
Eric Wong9b981fc2007-01-11 12:14:21 -08002484 warn "W: $act: $_\n";
2485 }
2486 foreach my $t (qw/dir_prop file_prop/) {
Eric Wong97f69872007-01-25 11:53:13 -08002487 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002488 foreach my $path (sort keys %$h) {
2489 my $ppath = $path eq '' ? '.' : $path;
2490 foreach my $prop (sort keys %{$h->{$path}}) {
Eric Wong1ce255d2007-01-14 23:21:16 -08002491 next if $SKIP_PROP{$prop};
Eric Wong9b981fc2007-01-11 12:14:21 -08002492 my $v = $h->{$path}->{$prop};
Eric Wong97f69872007-01-25 11:53:13 -08002493 my $t_ppath_prop = "$t: " .
2494 uri_encode($ppath) . ' ' .
2495 uri_encode($prop);
Eric Wong9b981fc2007-01-11 12:14:21 -08002496 if (defined $v) {
Eric Wong97f69872007-01-25 11:53:13 -08002497 push @out, " +$t_ppath_prop " .
2498 uri_encode($v);
Eric Wong9b981fc2007-01-11 12:14:21 -08002499 } else {
Eric Wong97f69872007-01-25 11:53:13 -08002500 push @out, " -$t_ppath_prop";
Eric Wong9b981fc2007-01-11 12:14:21 -08002501 }
2502 }
2503 }
2504 }
2505 foreach my $t (qw/absent_file absent_directory/) {
Eric Wong97f69872007-01-25 11:53:13 -08002506 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002507 foreach my $parent (sort keys %$h) {
2508 foreach my $path (sort @{$h->{$parent}}) {
Eric Wong97f69872007-01-25 11:53:13 -08002509 push @out, " $t: " .
2510 uri_encode("$parent/$path");
Eric Wong9b981fc2007-01-11 12:14:21 -08002511 warn "W: $t: $parent/$path ",
2512 "Insufficient permissions?\n";
2513 }
2514 }
2515 }
Eric Wong97f69872007-01-25 11:53:13 -08002516 \@out;
Eric Wong9b981fc2007-01-11 12:14:21 -08002517}
2518
Pete Harlane82f0d72009-01-17 20:10:14 -08002519# parse_svn_date(DATE)
2520# --------------------
2521# Given a date (in UTC) from Subversion, return a string in the format
2522# "<TZ Offset> <local date/time>" that Git will use.
2523#
2524# By default the parsed date will be in UTC; if $Git::SVN::_localtime
2525# is true we'll convert it to the local timezone instead.
Eric Wong1c8443b2007-01-14 02:17:00 -08002526sub parse_svn_date {
2527 my $date = shift || return '+0000 1970-01-01 00:00:00';
2528 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
Junio C Hamanob94ead72009-02-18 10:48:01 -08002529 (\d\d)\:(\d\d)\:(\d\d)\.\d*Z$/x) or
Eric Wong1c8443b2007-01-14 02:17:00 -08002530 croak "Unable to parse date: $date\n";
Pete Harlane82f0d72009-01-17 20:10:14 -08002531 my $parsed_date; # Set next.
2532
2533 if ($Git::SVN::_localtime) {
2534 # Translate the Subversion datetime to an epoch time.
2535 # Begin by switching ourselves to $date's timezone, UTC.
2536 my $old_env_TZ = $ENV{TZ};
2537 $ENV{TZ} = 'UTC';
2538
2539 my $epoch_in_UTC =
2540 POSIX::strftime('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
2541
2542 # Determine our local timezone (including DST) at the
2543 # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the
2544 # value of TZ, if any, at the time we were run.
2545 if (defined $Git::SVN::Log::TZ) {
2546 $ENV{TZ} = $Git::SVN::Log::TZ;
2547 } else {
2548 delete $ENV{TZ};
2549 }
2550
2551 my $our_TZ =
2552 POSIX::strftime('%Z', $S, $M, $H, $d, $m - 1, $Y - 1900);
2553
2554 # This converts $epoch_in_UTC into our local timezone.
2555 my ($sec, $min, $hour, $mday, $mon, $year,
2556 $wday, $yday, $isdst) = localtime($epoch_in_UTC);
2557
2558 $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
2559 $our_TZ, $year + 1900, $mon + 1,
2560 $mday, $hour, $min, $sec);
2561
2562 # Reset us to the timezone in effect when we entered
2563 # this routine.
2564 if (defined $old_env_TZ) {
2565 $ENV{TZ} = $old_env_TZ;
2566 } else {
2567 delete $ENV{TZ};
2568 }
2569 } else {
2570 $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
2571 }
2572
2573 return $parsed_date;
Eric Wong1c8443b2007-01-14 02:17:00 -08002574}
2575
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002576sub other_gs {
2577 my ($self, $new_url, $url, $repos_root,
2578 $branch_from, $r, $old_ref_id) = @_;
2579 my $gs = Git::SVN->find_by_url($new_url, $repos_root, $branch_from);
2580 unless ($gs) {
2581 my $ref_id = $old_ref_id;
2582 $ref_id =~ s/\@\d+$//;
2583 $ref_id .= "\@$r";
2584 # just grow a tail if we're not unique enough :x
2585 $ref_id .= '-' while find_ref($ref_id);
2586 print STDERR "Initializing parent: $ref_id\n";
2587 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
2588 if ($u =~ s#^\Q$url\E(/|$)##) {
2589 $p = $u;
2590 $u = $url;
2591 $repo_id = $self->{repo_id};
2592 }
2593 $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1);
2594 }
2595 $gs
2596}
2597
Eric Wong1c8443b2007-01-14 02:17:00 -08002598sub check_author {
2599 my ($author) = @_;
2600 if (!defined $author || length $author == 0) {
2601 $author = '(no author)';
Thomas Guyot-Sionnest9231f502008-04-22 06:07:47 -04002602 } elsif (defined $::_authors && ! defined $::users{$author}) {
Eric Wong1c8443b2007-01-14 02:17:00 -08002603 die "Author: $author not defined in $::_authors file\n";
2604 }
2605 $author;
2606}
2607
Eric Wong9b981fc2007-01-11 12:14:21 -08002608sub make_log_entry {
Eric Wong97f69872007-01-25 11:53:13 -08002609 my ($self, $rev, $parents, $ed) = @_;
2610 my $untracked = $self->get_untracked($ed);
2611
Eric Wong9b981fc2007-01-11 12:14:21 -08002612 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08002613 print $un "r$rev\n" or croak $!;
2614 print $un $_, "\n" foreach @$untracked;
2615 my %log_entry = ( parents => $parents || [], revision => $rev,
2616 log => '');
Eric Wongfbcc1732007-02-06 18:35:30 -08002617
Eric Wong8a49ee92007-02-10 20:46:50 -08002618 my $headrev;
Eric Wongfbcc1732007-02-06 18:35:30 -08002619 my $logged = delete $self->{logged_rev_props};
Eric Wong8a49ee92007-02-10 20:46:50 -08002620 if (!$logged || $self->{-want_revprops}) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002621 my $rp = $self->ra->rev_proplist($rev);
2622 foreach (sort keys %$rp) {
2623 my $v = $rp->{$_};
2624 if (/^svn:(author|date|log)$/) {
2625 $log_entry{$1} = $v;
Eric Wong8a49ee92007-02-10 20:46:50 -08002626 } elsif ($_ eq 'svm:headrev') {
2627 $headrev = $v;
Eric Wongfbcc1732007-02-06 18:35:30 -08002628 } else {
2629 print $un " rev_prop: ", uri_encode($_), ' ',
2630 uri_encode($v), "\n";
2631 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002632 }
Eric Wongfbcc1732007-02-06 18:35:30 -08002633 } else {
2634 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
Eric Wong9b981fc2007-01-11 12:14:21 -08002635 }
2636 close $un or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08002637
Eric Wong9b981fc2007-01-11 12:14:21 -08002638 $log_entry{date} = parse_svn_date($log_entry{date});
Eric Wong9b981fc2007-01-11 12:14:21 -08002639 $log_entry{log} .= "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002640 my $author = $log_entry{author} = check_author($log_entry{author});
2641 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002642 : ($author, undef);
2643
2644 my ($commit_name, $commit_email) = ($name, $email);
2645 if ($_use_log_author) {
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002646 my $name_field;
2647 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
2648 $name_field = $1;
2649 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
2650 $name_field = $1;
2651 }
2652 if (!defined $name_field) {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02002653 if (!defined $email) {
2654 $email = $name;
2655 }
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002656 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002657 ($name, $email) = ($1, $2);
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002658 } elsif ($name_field =~ /(.*)@/) {
2659 ($name, $email) = ($1, $name_field);
2660 } else {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02002661 ($name, $email) = ($name_field, $name_field);
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002662 }
2663 }
Eric Wong91b03282007-02-11 00:51:33 -08002664 if (defined $headrev && $self->use_svm_props) {
Eric Wongaea736c2007-02-16 19:15:21 -08002665 if ($self->rewrite_root) {
2666 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
2667 "options set!\n";
2668 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002669 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002670 # we don't want "SVM: initializing mirror for junk" ...
2671 return undef if $r == 0;
2672 my $svm = $self->svm;
2673 if ($uuid ne $svm->{uuid}) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002674 die "UUID mismatch on SVM path:\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08002675 "expected: $svm->{uuid}\n",
Eric Wong8a49ee92007-02-10 20:46:50 -08002676 " got: $uuid\n";
2677 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002678 my $full_url = $self->full_url;
2679 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
2680 die "Failed to replace '$svm->{replace}' with ",
2681 "'$svm->{source}' in $full_url\n";
Sam Vilain18ea92b2007-02-23 12:32:29 +13002682 # throw away username for storing in records
2683 remove_username($full_url);
Eric Wong8a49ee92007-02-10 20:46:50 -08002684 $log_entry{metadata} = "$full_url\@$r $uuid";
2685 $log_entry{svm_revision} = $r;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002686 $email ||= "$author\@$uuid";
2687 $commit_email ||= "$author\@$uuid";
Eric Wong62e349d2007-02-16 19:57:29 -08002688 } elsif ($self->use_svnsync_props) {
2689 my $full_url = $self->svnsync->{url};
2690 $full_url .= "/$self->{path}" if length $self->{path};
Adam Robence118732007-04-24 18:02:07 -07002691 remove_username($full_url);
Eric Wong62e349d2007-02-16 19:57:29 -08002692 my $uuid = $self->svnsync->{uuid};
2693 $log_entry{metadata} = "$full_url\@$rev $uuid";
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002694 $email ||= "$author\@$uuid";
2695 $commit_email ||= "$author\@$uuid";
Eric Wong8a49ee92007-02-10 20:46:50 -08002696 } else {
Adam Robence118732007-04-24 18:02:07 -07002697 my $url = $self->metadata_url;
2698 remove_username($url);
2699 $log_entry{metadata} = "$url\@$rev " .
Eric Wong26a62d52007-02-12 13:25:25 -08002700 $self->ra->get_uuid;
Eric Wongdb03cd22007-02-13 00:38:02 -08002701 $email ||= "$author\@" . $self->ra->get_uuid;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002702 $commit_email ||= "$author\@" . $self->ra->get_uuid;
Eric Wong8a49ee92007-02-10 20:46:50 -08002703 }
Eric Wongdb03cd22007-02-13 00:38:02 -08002704 $log_entry{name} = $name;
2705 $log_entry{email} = $email;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002706 $log_entry{commit_name} = $commit_name;
2707 $log_entry{commit_email} = $commit_email;
Eric Wong9b981fc2007-01-11 12:14:21 -08002708 \%log_entry;
2709}
2710
2711sub fetch {
Eric Wong3ebe8df2007-01-25 17:35:40 -08002712 my ($self, $min_rev, $max_rev, @parents) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002713 my ($last_rev, $last_commit) = $self->last_rev_commit;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002714 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
Eric Wonge5181922007-02-08 12:53:57 -08002715 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
Eric Wong9b981fc2007-01-11 12:14:21 -08002716}
2717
2718sub set_tree_cb {
2719 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
Eric Wong490f49e2007-02-10 13:58:33 -08002720 $self->{inject_parents} = { $rev => $tree };
2721 $self->fetch(undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08002722}
2723
2724sub set_tree {
2725 my ($self, $tree) = (shift, shift);
Eric Wong1ce255d2007-01-14 23:21:16 -08002726 my $log_entry = ::get_commit_entry($tree);
Eric Wong9b981fc2007-01-11 12:14:21 -08002727 unless ($self->{last_rev}) {
Luc Heinrich0a1a1c82008-09-29 15:58:18 +02002728 ::fatal("Must have an existing revision to commit");
Eric Wong9b981fc2007-01-11 12:14:21 -08002729 }
Eric Wong61395352007-01-27 14:33:08 -08002730 my %ed_opts = ( r => $self->{last_rev},
2731 log => $log_entry->{log},
2732 ra => $self->ra,
2733 tree_a => $self->{last_commit},
2734 tree_b => $tree,
2735 editor_cb => sub {
2736 $self->set_tree_cb($log_entry, $tree, @_) },
2737 svn_path => $self->{path} );
2738 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002739 print "No changes\nr$self->{last_rev} = $tree\n";
2740 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002741}
2742
Eric Wong060610c2007-12-08 23:27:41 -08002743sub rebuild_from_rev_db {
2744 my ($self, $path) = @_;
2745 my $r = -1;
2746 open my $fh, '<', $path or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02002747 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002748 while (<$fh>) {
2749 length($_) == 41 or croak "inconsistent size in ($_) != 41";
2750 chomp($_);
2751 ++$r;
2752 next if $_ eq ('0' x 40);
2753 $self->rev_map_set($r, $_);
2754 print "r$r = $_\n";
2755 }
2756 close $fh or croak "close: $!";
2757 unlink $path or croak "unlink: $!";
2758}
2759
Eric Wongf0ecca12007-01-30 13:11:14 -08002760sub rebuild {
2761 my ($self) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08002762 my $map_path = $self->map_path;
Deskin Miller2beec892008-09-15 21:12:58 -04002763 my $partial = (-e $map_path && ! -z $map_path);
Eric Wongd6d33462007-02-16 04:05:33 -08002764 return unless ::verify_ref($self->refname.'^0');
Deskin Miller2beec892008-09-15 21:12:58 -04002765 if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
Eric Wong060610c2007-12-08 23:27:41 -08002766 my $rev_db = $self->rev_db_path;
2767 $self->rebuild_from_rev_db($rev_db);
2768 if ($self->use_svm_props) {
2769 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
2770 $self->rebuild_from_rev_db($svm_rev_db);
2771 }
2772 $self->unlink_rev_db_symlink;
Eric Wong26a62d52007-02-12 13:25:25 -08002773 return;
2774 }
Deskin Miller2beec892008-09-15 21:12:58 -04002775 print "Rebuilding $map_path ...\n" if (!$partial);
2776 my ($base_rev, $head) = ($partial ? $self->rev_map_max_norebuild(1) :
2777 (undef, undef));
Eric Wong060610c2007-12-08 23:27:41 -08002778 my ($log, $ctx) =
2779 command_output_pipe(qw/rev-list --pretty=raw --no-color --reverse/,
Deskin Miller2beec892008-09-15 21:12:58 -04002780 ($head ? "$head.." : "") . $self->refname,
2781 '--');
Jan Krüger74b1e122008-06-24 02:17:36 +02002782 my $metadata_url = $self->metadata_url;
2783 remove_username($metadata_url);
Eric Wong060610c2007-12-08 23:27:41 -08002784 my $svn_uuid = $self->ra_uuid;
Sam Vilain3dfab992007-06-30 20:56:13 +12002785 my $c;
2786 while (<$log>) {
2787 if ( m{^commit ($::sha1)$} ) {
2788 $c = $1;
2789 next;
2790 }
2791 next unless s{^\s*(git-svn-id:)}{$1};
2792 my ($url, $rev, $uuid) = ::extract_metadata($_);
Sam Vilain18ea92b2007-02-23 12:32:29 +13002793 remove_username($url);
Eric Wongf0ecca12007-01-30 13:11:14 -08002794
2795 # ignore merges (from set-tree)
2796 next if (!defined $rev || !$uuid);
2797
2798 # if we merged or otherwise started elsewhere, this is
2799 # how we break out of it
Eric Wong060610c2007-12-08 23:27:41 -08002800 if (($uuid ne $svn_uuid) ||
Jan Krüger74b1e122008-06-24 02:17:36 +02002801 ($metadata_url && $url && ($url ne $metadata_url))) {
Eric Wongf0ecca12007-01-30 13:11:14 -08002802 next;
2803 }
Deskin Miller2beec892008-09-15 21:12:58 -04002804 if ($partial && $head) {
2805 print "Partial-rebuilding $map_path ...\n";
2806 print "Currently at $base_rev = $head\n";
2807 $head = undef;
2808 }
Eric Wongf0ecca12007-01-30 13:11:14 -08002809
Eric Wong060610c2007-12-08 23:27:41 -08002810 $self->rev_map_set($rev, $c);
Eric Wongf0ecca12007-01-30 13:11:14 -08002811 print "r$rev = $c\n";
2812 }
Sam Vilain3dfab992007-06-30 20:56:13 +12002813 command_close_pipe($log, $ctx);
Deskin Miller2beec892008-09-15 21:12:58 -04002814 print "Done rebuilding $map_path\n" if (!$partial || !$head);
Eric Wong060610c2007-12-08 23:27:41 -08002815 my $rev_db_path = $self->rev_db_path;
2816 if (-f $self->rev_db_path) {
2817 unlink $self->rev_db_path or croak "unlink: $!";
2818 }
2819 $self->unlink_rev_db_symlink;
Eric Wongf0ecca12007-01-30 13:11:14 -08002820}
2821
Eric Wong060610c2007-12-08 23:27:41 -08002822# rev_map:
Eric Wong9b981fc2007-01-11 12:14:21 -08002823# Tie::File seems to be prone to offset errors if revisions get sparse,
2824# it's not that fast, either. Tie::File is also not in Perl 5.6. So
2825# one of my favorite modules is out :< Next up would be one of the DBM
Eric Wong060610c2007-12-08 23:27:41 -08002826# modules, but I'm not sure which is most portable...
2827#
2828# This is the replacement for the rev_db format, which was too big
2829# and inefficient for large repositories with a lot of sparse history
2830# (mainly tags)
2831#
2832# The format is this:
2833# - 24 bytes for every record,
2834# * 4 bytes for the integer representing an SVN revision number
2835# * 20 bytes representing the sha1 of a git commit
2836# - No empty padding records like the old format
Eric Wong66ab84b2007-12-08 23:27:42 -08002837# (except the last record, which can be overwritten)
Eric Wong060610c2007-12-08 23:27:41 -08002838# - new records are written append-only since SVN revision numbers
2839# increase monotonically
2840# - lookups on SVN revision number are done via a binary search
Eric Wong66ab84b2007-12-08 23:27:42 -08002841# - Piping the file to xxd -c24 is a good way of dumping it for
2842# viewing or editing (piped back through xxd -r), should the need
2843# ever arise.
2844# - The last record can be padding revision with an all-zero sha1
2845# This is used to optimize fetch performance when using multiple
2846# "fetch" directives in .git/config
Eric Wong060610c2007-12-08 23:27:41 -08002847#
Eric Wong97ae0912007-02-11 15:21:24 -08002848# These files are disposable unless noMetadata or useSvmProps is set
Eric Wong9b981fc2007-01-11 12:14:21 -08002849
Eric Wong060610c2007-12-08 23:27:41 -08002850sub _rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08002851 my ($fh, $rev, $commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08002852
Michael Weber4f7ec792008-04-18 15:12:04 +02002853 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002854 my $size = (stat($fh))[7];
2855 ($size % 24) == 0 or croak "inconsistent size: $size";
2856
Eric Wong66ab84b2007-12-08 23:27:42 -08002857 my $wr_offset = 0;
Eric Wong060610c2007-12-08 23:27:41 -08002858 if ($size > 0) {
2859 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
2860 my $read = sysread($fh, my $buf, 24) or croak "read: $!";
2861 $read == 24 or croak "read only $read bytes (!= 24)";
2862 my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08002863 if ($last_commit eq ('0' x40)) {
2864 if ($size >= 48) {
2865 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
2866 $read = sysread($fh, $buf, 24) or
2867 croak "read: $!";
2868 $read == 24 or
2869 croak "read only $read bytes (!= 24)";
2870 ($last_rev, $last_commit) =
2871 unpack(rev_map_fmt, $buf);
2872 if ($last_commit eq ('0' x40)) {
2873 croak "inconsistent .rev_map\n";
2874 }
2875 }
2876 if ($last_rev >= $rev) {
2877 croak "last_rev is higher!: $last_rev >= $rev";
2878 }
2879 $wr_offset = -24;
Eric Wong47a0b752007-01-31 05:13:30 -08002880 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002881 }
Eric Wong66ab84b2007-12-08 23:27:42 -08002882 sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002883 syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or
2884 croak "write: $!";
Eric Wong26a62d52007-02-12 13:25:25 -08002885}
2886
2887sub mkfile {
2888 my ($path) = @_;
2889 unless (-e $path) {
2890 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
2891 mkpath([$dir]) unless -d $dir;
2892 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
2893 close $fh or die "Couldn't close (create) $path: $!\n";
2894 }
2895}
2896
Eric Wong060610c2007-12-08 23:27:41 -08002897sub rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08002898 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
2899 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
Eric Wong060610c2007-12-08 23:27:41 -08002900 my $db = $self->map_path($uuid);
Eric Wong26a62d52007-02-12 13:25:25 -08002901 my $db_lock = "$db.lock";
2902 my $sig;
2903 if ($update_ref) {
2904 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
2905 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
2906 }
2907 mkfile($db);
2908
2909 $LOCKFILES{$db_lock} = 1;
2910 my $sync;
2911 # both of these options make our .rev_db file very, very important
2912 # and we can't afford to lose it because rebuild() won't work
2913 if ($self->use_svm_props || $self->no_metadata) {
2914 $sync = 1;
Eric Wong060610c2007-12-08 23:27:41 -08002915 copy($db, $db_lock) or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08002916 "Failed to copy: ",
2917 "$db => $db_lock ($!)\n";
2918 } else {
Eric Wong060610c2007-12-08 23:27:41 -08002919 rename $db, $db_lock or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08002920 "Failed to rename: ",
2921 "$db => $db_lock ($!)\n";
2922 }
Eric Wong060610c2007-12-08 23:27:41 -08002923
Eric Wong66ab84b2007-12-08 23:27:42 -08002924 sysopen(my $fh, $db_lock, O_RDWR | O_CREAT)
Eric Wong060610c2007-12-08 23:27:41 -08002925 or croak "Couldn't open $db_lock: $!\n";
2926 _rev_map_set($fh, $rev, $commit);
Eric Wong97ae0912007-02-11 15:21:24 -08002927 if ($sync) {
2928 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2929 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2930 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002931 close $fh or croak $!;
Eric Wong373274f2007-01-31 13:54:23 -08002932 if ($update_ref) {
Eric Wong1e889ef2007-02-16 01:45:13 -08002933 $_head = $self;
Eric Wong373274f2007-01-31 13:54:23 -08002934 command_noisy('update-ref', '-m', "r$rev",
2935 $self->refname, $commit);
2936 }
Eric Wong060610c2007-12-08 23:27:41 -08002937 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
Eric Wong373274f2007-01-31 13:54:23 -08002938 "$db_lock => $db ($!)\n";
2939 delete $LOCKFILES{$db_lock};
2940 if ($update_ref) {
2941 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
2942 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
2943 kill $sig, $$ if defined $sig;
2944 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002945}
2946
Eric Wong66ab84b2007-12-08 23:27:42 -08002947# If want_commit, this will return an array of (rev, commit) where
2948# commit _must_ be a valid commit in the archive.
2949# Otherwise, it'll return the max revision (whether or not the
2950# commit is valid or just a 0x40 placeholder).
Eric Wong060610c2007-12-08 23:27:41 -08002951sub rev_map_max {
Eric Wong66ab84b2007-12-08 23:27:42 -08002952 my ($self, $want_commit) = @_;
Eric Wongd6d33462007-02-16 04:05:33 -08002953 $self->rebuild;
Deskin Miller2beec892008-09-15 21:12:58 -04002954 my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
2955 $want_commit ? ($r, $c) : $r;
2956}
2957
2958sub rev_map_max_norebuild {
2959 my ($self, $want_commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08002960 my $map_path = $self->map_path;
Eric Wong66ab84b2007-12-08 23:27:42 -08002961 stat $map_path or return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08002962 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02002963 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002964 my $size = (stat($fh))[7];
2965 ($size % 24) == 0 or croak "inconsistent size: $size";
2966
2967 if ($size == 0) {
2968 close $fh or croak "close: $!";
Eric Wong66ab84b2007-12-08 23:27:42 -08002969 return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08002970 }
2971
Eric Wong66ab84b2007-12-08 23:27:42 -08002972 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002973 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002974 my ($r, $c) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08002975 if ($want_commit && $c eq ('0' x40)) {
2976 if ($size < 48) {
2977 return $want_commit ? (0, undef) : 0;
2978 }
2979 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
2980 sysread($fh, $buf, 24) == 24 or croak "read: $!";
2981 ($r, $c) = unpack(rev_map_fmt, $buf);
2982 if ($c eq ('0'x40)) {
2983 croak "Penultimate record is all-zeroes in $map_path";
2984 }
2985 }
2986 close $fh or croak "close: $!";
2987 $want_commit ? ($r, $c) : $r;
Eric Wong9c93fee2007-01-31 17:22:31 -08002988}
2989
Eric Wong060610c2007-12-08 23:27:41 -08002990sub rev_map_get {
Eric Wong26a62d52007-02-12 13:25:25 -08002991 my ($self, $rev, $uuid) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08002992 my $map_path = $self->map_path($uuid);
2993 return undef unless -e $map_path;
2994
2995 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02002996 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002997 my $size = (stat($fh))[7];
2998 ($size % 24) == 0 or croak "inconsistent size: $size";
2999
3000 if ($size == 0) {
3001 close $fh or croak "close: $fh";
3002 return undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003003 }
Eric Wong060610c2007-12-08 23:27:41 -08003004
3005 my ($l, $u) = (0, $size - 24);
3006 my ($r, $c, $buf);
3007
3008 while ($l <= $u) {
3009 my $i = int(($l/24 + $u/24) / 2) * 24;
3010 sysseek($fh, $i, SEEK_SET) or croak "seek: $!";
3011 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
3012 my ($r, $c) = unpack('NH40', $buf);
3013
3014 if ($r < $rev) {
3015 $l = $i + 24;
3016 } elsif ($r > $rev) {
3017 $u = $i - 24;
3018 } else { # $r == $rev
3019 close($fh) or croak "close: $!";
Eric Wong66ab84b2007-12-08 23:27:42 -08003020 return $c eq ('0' x 40) ? undef : $c;
Eric Wong060610c2007-12-08 23:27:41 -08003021 }
3022 }
3023 close($fh) or croak "close: $!";
3024 undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003025}
3026
David D Kilzer111947e2007-11-11 22:56:52 -08003027# Finds the first svn revision that exists on (if $eq_ok is true) or
3028# before $rev for the current branch. It will not search any lower
3029# than $min_rev. Returns the git commit hash and svn revision number
3030# if found, else (undef, undef).
Eric Wong15710b62007-01-22 02:20:33 -08003031sub find_rev_before {
David D Kilzer111947e2007-11-11 22:56:52 -08003032 my ($self, $rev, $eq_ok, $min_rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08003033 --$rev unless $eq_ok;
David D Kilzer111947e2007-11-11 22:56:52 -08003034 $min_rev ||= 1;
3035 while ($rev >= $min_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003036 if (my $c = $self->rev_map_get($rev)) {
Eric Wong15710b62007-01-22 02:20:33 -08003037 return ($rev, $c);
3038 }
3039 --$rev;
3040 }
3041 return (undef, undef);
3042}
3043
David D Kilzer111947e2007-11-11 22:56:52 -08003044# Finds the first svn revision that exists on (if $eq_ok is true) or
3045# after $rev for the current branch. It will not search any higher
3046# than $max_rev. Returns the git commit hash and svn revision number
3047# if found, else (undef, undef).
3048sub find_rev_after {
3049 my ($self, $rev, $eq_ok, $max_rev) = @_;
3050 ++$rev unless $eq_ok;
Eric Wong060610c2007-12-08 23:27:41 -08003051 $max_rev ||= $self->rev_map_max;
David D Kilzer111947e2007-11-11 22:56:52 -08003052 while ($rev <= $max_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003053 if (my $c = $self->rev_map_get($rev)) {
David D Kilzer111947e2007-11-11 22:56:52 -08003054 return ($rev, $c);
3055 }
3056 ++$rev;
3057 }
3058 return (undef, undef);
3059}
3060
Eric Wong9b981fc2007-01-11 12:14:21 -08003061sub _new {
Eric Wong706587f2007-01-18 17:50:01 -08003062 my ($class, $repo_id, $ref_id, $path) = @_;
3063 unless (defined $repo_id && length $repo_id) {
3064 $repo_id = $Git::SVN::default_repo_id;
3065 }
3066 unless (defined $ref_id && length $ref_id) {
Eric Wong8b8fc062007-01-22 11:44:57 -08003067 $_[2] = $ref_id = $Git::SVN::default_ref_id;
Eric Wong706587f2007-01-18 17:50:01 -08003068 }
Eric Wong7829f202008-06-28 20:40:32 -07003069 $_[1] = $repo_id;
Eric Wong706587f2007-01-18 17:50:01 -08003070 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
3071 $_[3] = $path = '' unless (defined $path);
Eric Wongb4d57e52007-02-14 15:10:44 -08003072 mkpath(["$ENV{GIT_DIR}/svn"]);
Eric Wong26a62d52007-02-12 13:25:25 -08003073 bless {
3074 ref_id => $ref_id, dir => $dir, index => "$dir/index",
Eric Wong8a49ee92007-02-10 20:46:50 -08003075 path => $path, config => "$ENV{GIT_DIR}/svn/config",
Eric Wong060610c2007-12-08 23:27:41 -08003076 map_root => "$dir/.rev_map", repo_id => $repo_id }, $class;
Eric Wong26a62d52007-02-12 13:25:25 -08003077}
3078
Eric Wong060610c2007-12-08 23:27:41 -08003079# for read-only access of old .rev_db formats
3080sub unlink_rev_db_symlink {
3081 my ($self) = @_;
3082 my $link = $self->rev_db_path;
3083 $link =~ s/\.[\w-]+$// or croak "missing UUID at the end of $link";
3084 if (-l $link) {
3085 unlink $link or croak "unlink: $link failed!";
3086 }
3087}
3088
3089sub rev_db_path {
3090 my ($self, $uuid) = @_;
3091 my $db_path = $self->map_path($uuid);
3092 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
3093 or croak "map_path: $db_path does not contain '/.rev_map.' !";
3094 $db_path;
3095}
3096
3097# the new replacement for .rev_db
3098sub map_path {
Eric Wong26a62d52007-02-12 13:25:25 -08003099 my ($self, $uuid) = @_;
3100 $uuid ||= $self->ra_uuid;
Eric Wong060610c2007-12-08 23:27:41 -08003101 "$self->{map_root}.$uuid";
Eric Wong9b981fc2007-01-11 12:14:21 -08003102}
3103
Eric Wong1c8443b2007-01-14 02:17:00 -08003104sub uri_encode {
3105 my ($f) = @_;
3106 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
3107 $f
3108}
Eric Wong9b981fc2007-01-11 12:14:21 -08003109
Sam Vilain18ea92b2007-02-23 12:32:29 +13003110sub remove_username {
3111 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
3112}
3113
Eric Wongd976acf2007-01-04 00:45:03 -08003114package Git::SVN::Prompt;
3115use strict;
3116use warnings;
3117require SVN::Core;
3118use vars qw/$_no_auth_cache $_username/;
3119
3120sub simple {
Eric Wong30d055a2006-11-24 01:38:04 -08003121 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
3122 $may_save = undef if $_no_auth_cache;
3123 $default_username = $_username if defined $_username;
3124 if (defined $default_username && length $default_username) {
3125 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003126 print STDERR "Authentication realm: $realm\n";
3127 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003128 }
3129 $cred->username($default_username);
3130 } else {
Eric Wongd976acf2007-01-04 00:45:03 -08003131 username($cred, $realm, $may_save, $pool);
Eric Wong30d055a2006-11-24 01:38:04 -08003132 }
3133 $cred->password(_read_password("Password for '" .
3134 $cred->username . "': ", $realm));
3135 $cred->may_save($may_save);
3136 $SVN::_Core::SVN_NO_ERROR;
3137}
3138
Eric Wongd976acf2007-01-04 00:45:03 -08003139sub ssl_server_trust {
Eric Wong30d055a2006-11-24 01:38:04 -08003140 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
3141 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003142 print STDERR "Error validating server certificate for '$realm':\n";
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003143 {
3144 no warnings 'once';
3145 # All variables SVN::Auth::SSL::* are used only once,
3146 # so we're shutting up Perl warnings about this.
3147 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
3148 print STDERR " - The certificate is not issued ",
3149 "by a trusted authority. Use the\n",
3150 " fingerprint to validate ",
3151 "the certificate manually!\n";
3152 }
3153 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
3154 print STDERR " - The certificate hostname ",
3155 "does not match.\n";
3156 }
3157 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
3158 print STDERR " - The certificate is not yet valid.\n";
3159 }
3160 if ($failures & $SVN::Auth::SSL::EXPIRED) {
3161 print STDERR " - The certificate has expired.\n";
3162 }
3163 if ($failures & $SVN::Auth::SSL::OTHER) {
3164 print STDERR " - The certificate has ",
3165 "an unknown error.\n";
3166 }
3167 } # no warnings 'once'
Eric Wong6f729592007-01-15 20:15:55 -08003168 printf STDERR
3169 "Certificate information:\n".
Eric Wong30d055a2006-11-24 01:38:04 -08003170 " - Hostname: %s\n".
3171 " - Valid: from %s until %s\n".
3172 " - Issuer: %s\n".
3173 " - Fingerprint: %s\n",
3174 map $cert_info->$_, qw(hostname valid_from valid_until
Eric Wong6f729592007-01-15 20:15:55 -08003175 issuer_dname fingerprint);
Eric Wong30d055a2006-11-24 01:38:04 -08003176 my $choice;
3177prompt:
Eric Wong6f729592007-01-15 20:15:55 -08003178 print STDERR $may_save ?
Eric Wong30d055a2006-11-24 01:38:04 -08003179 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
3180 "(R)eject or accept (t)emporarily? ";
Eric Wong6f729592007-01-15 20:15:55 -08003181 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003182 $choice = lc(substr(<STDIN> || 'R', 0, 1));
3183 if ($choice =~ /^t$/i) {
3184 $cred->may_save(undef);
3185 } elsif ($choice =~ /^r$/i) {
3186 return -1;
3187 } elsif ($may_save && $choice =~ /^p$/i) {
3188 $cred->may_save($may_save);
3189 } else {
3190 goto prompt;
3191 }
3192 $cred->accepted_failures($failures);
3193 $SVN::_Core::SVN_NO_ERROR;
3194}
3195
Eric Wongd976acf2007-01-04 00:45:03 -08003196sub ssl_client_cert {
Eric Wong30d055a2006-11-24 01:38:04 -08003197 my ($cred, $realm, $may_save, $pool) = @_;
3198 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003199 print STDERR "Client certificate filename: ";
3200 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003201 chomp(my $filename = <STDIN>);
3202 $cred->cert_file($filename);
3203 $cred->may_save($may_save);
3204 $SVN::_Core::SVN_NO_ERROR;
3205}
3206
Eric Wongd976acf2007-01-04 00:45:03 -08003207sub ssl_client_cert_pw {
Eric Wong30d055a2006-11-24 01:38:04 -08003208 my ($cred, $realm, $may_save, $pool) = @_;
3209 $may_save = undef if $_no_auth_cache;
3210 $cred->password(_read_password("Password: ", $realm));
3211 $cred->may_save($may_save);
3212 $SVN::_Core::SVN_NO_ERROR;
3213}
3214
Eric Wongd976acf2007-01-04 00:45:03 -08003215sub username {
Eric Wong30d055a2006-11-24 01:38:04 -08003216 my ($cred, $realm, $may_save, $pool) = @_;
3217 $may_save = undef if $_no_auth_cache;
3218 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003219 print STDERR "Authentication realm: $realm\n";
Eric Wong30d055a2006-11-24 01:38:04 -08003220 }
3221 my $username;
3222 if (defined $_username) {
3223 $username = $_username;
3224 } else {
Eric Wong6f729592007-01-15 20:15:55 -08003225 print STDERR "Username: ";
3226 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003227 chomp($username = <STDIN>);
3228 }
3229 $cred->username($username);
3230 $cred->may_save($may_save);
3231 $SVN::_Core::SVN_NO_ERROR;
3232}
3233
3234sub _read_password {
3235 my ($prompt, $realm) = @_;
Eric Wong6f729592007-01-15 20:15:55 -08003236 print STDERR $prompt;
3237 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003238 require Term::ReadKey;
3239 Term::ReadKey::ReadMode('noecho');
3240 my $password = '';
3241 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
3242 last if $key =~ /[\012\015]/; # \n\r
3243 $password .= $key;
3244 }
3245 Term::ReadKey::ReadMode('restore');
Eric Wong6f729592007-01-15 20:15:55 -08003246 print STDERR "\n";
3247 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003248 $password;
3249}
3250
Eric Wong27a1a802006-11-27 21:44:48 -08003251package SVN::Git::Fetcher;
3252use vars qw/@ISA/;
3253use strict;
3254use warnings;
3255use Carp qw/croak/;
Adam Robenffe256f2008-05-23 16:19:41 +02003256use File::Temp qw/tempfile/;
Eric Wong27a1a802006-11-27 21:44:48 -08003257use IO::File qw//;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003258use vars qw/$_ignore_regex/;
Eric Wong27a1a802006-11-27 21:44:48 -08003259
3260# file baton members: path, mode_a, mode_b, pool, fh, blob, base
3261sub new {
Eric Wong8841b372009-02-11 01:56:58 -08003262 my ($class, $git_svn, $switch_path) = @_;
Eric Wong27a1a802006-11-27 21:44:48 -08003263 my $self = SVN::Delta::Editor->new;
3264 bless $self, $class;
Eric Wongdbc6c742009-01-11 16:51:10 -08003265 if (exists $git_svn->{last_commit}) {
3266 $self->{c} = $git_svn->{last_commit};
Eric Wong8841b372009-02-11 01:56:58 -08003267 $self->{empty_symlinks} =
3268 _mark_empty_symlinks($git_svn, $switch_path);
Eric Wongdbc6c742009-01-11 16:51:10 -08003269 }
Eric Wongd2a9a872006-12-12 14:47:00 -08003270 $self->{empty} = {};
3271 $self->{dir_prop} = {};
3272 $self->{file_prop} = {};
3273 $self->{absent_dir} = {};
3274 $self->{absent_file} = {};
Eric Wongef3cfaa2007-01-24 03:30:57 -08003275 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
Eric Wong27a1a802006-11-27 21:44:48 -08003276 $self;
3277}
3278
Eric Wongdbc6c742009-01-11 16:51:10 -08003279# this uses the Ra object, so it must be called before do_{switch,update},
3280# not inside them (when the Git::SVN::Fetcher object is passed) to
3281# do_{switch,update}
3282sub _mark_empty_symlinks {
Eric Wong8841b372009-02-11 01:56:58 -08003283 my ($git_svn, $switch_path) = @_;
Eric Wong4c58a712009-01-31 17:31:12 -08003284 my $bool = Git::config_bool('svn.brokenSymlinkWorkaround');
3285 return {} if (defined($bool) && ! $bool);
3286
Eric Wongdbc6c742009-01-11 16:51:10 -08003287 my %ret;
3288 my ($rev, $cmt) = $git_svn->last_rev_commit;
3289 return {} unless ($rev && $cmt);
3290
Eric Wong4c58a712009-01-31 17:31:12 -08003291 # allow the warning to be printed for each revision we fetch to
3292 # ensure the user sees it. The user can also disable the workaround
3293 # on the repository even while git svn is running and the next
3294 # revision fetched will skip this expensive function.
3295 my $printed_warning;
Eric Wongdbc6c742009-01-11 16:51:10 -08003296 chomp(my $empty_blob = `git hash-object -t blob --stdin < /dev/null`);
3297 my ($ls, $ctx) = command_output_pipe(qw/ls-tree -r -z/, $cmt);
3298 local $/ = "\0";
Eric Wong8841b372009-02-11 01:56:58 -08003299 my $pfx = defined($switch_path) ? $switch_path : $git_svn->{path};
Eric Wongdbc6c742009-01-11 16:51:10 -08003300 $pfx .= '/' if length($pfx);
3301 while (<$ls>) {
3302 chomp;
3303 s/\A100644 blob $empty_blob\t//o or next;
Eric Wong4c58a712009-01-31 17:31:12 -08003304 unless ($printed_warning) {
3305 print STDERR "Scanning for empty symlinks, ",
3306 "this may take a while if you have ",
3307 "many empty files\n",
3308 "You may disable this with `",
3309 "git config svn.brokenSymlinkWorkaround ",
3310 "false'.\n",
3311 "This may be done in a different ",
3312 "terminal without restarting ",
3313 "git svn\n";
3314 $printed_warning = 1;
3315 }
Eric Wongdbc6c742009-01-11 16:51:10 -08003316 my $path = $_;
3317 my (undef, $props) =
3318 $git_svn->ra->get_file($pfx.$path, $rev, undef);
3319 if ($props->{'svn:special'}) {
3320 $ret{$path} = 1;
3321 }
3322 }
3323 command_close_pipe($ls, $ctx);
3324 \%ret;
3325}
3326
Eric Wongb03a71a2009-01-11 18:23:38 -08003327# returns true if a given path is inside a ".git" directory
3328sub in_dot_git {
3329 $_[0] =~ m{(?:^|/)\.git(?:/|$)};
3330}
3331
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003332# return value: 0 -- don't ignore, 1 -- ignore
3333sub is_path_ignored {
3334 my ($path) = @_;
3335 return 1 if in_dot_git($path);
3336 return 0 unless defined($_ignore_regex);
3337 return 1 if $path =~ m!$_ignore_regex!o;
3338 return 0;
3339}
3340
Eric Wong8b8fc062007-01-22 11:44:57 -08003341sub set_path_strip {
3342 my ($self, $path) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003343 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
Eric Wong8b8fc062007-01-22 11:44:57 -08003344}
3345
Eric Wongd2a9a872006-12-12 14:47:00 -08003346sub open_root {
3347 { path => '' };
3348}
3349
3350sub open_directory {
3351 my ($self, $path, $pb, $rev) = @_;
3352 { path => $path };
3353}
3354
Eric Wong706587f2007-01-18 17:50:01 -08003355sub git_path {
3356 my ($self, $path) = @_;
Eric Wong2b27f6c2007-01-28 04:59:05 -08003357 if ($self->{path_strip}) {
3358 $path =~ s!$self->{path_strip}!! or
3359 die "Failed to strip path '$path' ($self->{path_strip})\n";
3360 }
Eric Wong706587f2007-01-18 17:50:01 -08003361 $path;
3362}
3363
Eric Wong27a1a802006-11-27 21:44:48 -08003364sub delete_entry {
3365 my ($self, $path, $rev, $pb) = @_;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003366 return undef if is_path_ignored($path);
Eric Wong4a87db02007-01-04 01:38:18 -08003367
Eric Wong706587f2007-01-18 17:50:01 -08003368 my $gpath = $self->git_path($path);
Eric Wong8a603772007-01-31 02:45:50 -08003369 return undef if ($gpath eq '');
3370
Eric Wong4a87db02007-01-04 01:38:18 -08003371 # remove entire directories.
Eric Wong706587f2007-01-18 17:50:01 -08003372 if (command('ls-tree', $self->{c}, '--', $gpath) =~ /^040000 tree/) {
Eric Wong4a87db02007-01-04 01:38:18 -08003373 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3374 -r --name-only -z/,
Eric Wong706587f2007-01-18 17:50:01 -08003375 $self->{c}, '--', $gpath);
Eric Wong4a87db02007-01-04 01:38:18 -08003376 local $/ = "\0";
3377 while (<$ls>) {
Eric Wongef3cfaa2007-01-24 03:30:57 -08003378 chomp;
3379 $self->{gii}->remove($_);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003380 print "\tD\t$_\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003381 }
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003382 print "\tD\t$gpath/\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003383 command_close_pipe($ls, $ctx);
3384 $self->{empty}->{$path} = 0
3385 } else {
Eric Wongef3cfaa2007-01-24 03:30:57 -08003386 $self->{gii}->remove($gpath);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003387 print "\tD\t$gpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003388 }
Eric Wong27a1a802006-11-27 21:44:48 -08003389 undef;
3390}
3391
3392sub open_file {
3393 my ($self, $path, $pb, $rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08003394 my ($mode, $blob);
3395
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003396 goto out if is_path_ignored($path);
Eric Wongb03a71a2009-01-11 18:23:38 -08003397
Eric Wong706587f2007-01-18 17:50:01 -08003398 my $gpath = $self->git_path($path);
Eric Wongb03a71a2009-01-11 18:23:38 -08003399 ($mode, $blob) = (command('ls-tree', $self->{c}, '--', $gpath)
Eric Wong27a1a802006-11-27 21:44:48 -08003400 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
Eric Wong006ede52006-12-08 01:55:19 -08003401 unless (defined $mode && defined $blob) {
3402 die "$path was not found in commit $self->{c} (r$rev)\n";
3403 }
Eric Wongdbc6c742009-01-11 16:51:10 -08003404 if ($mode eq '100644' && $self->{empty_symlinks}->{$path}) {
3405 $mode = '120000';
3406 }
Eric Wongb03a71a2009-01-11 18:23:38 -08003407out:
Eric Wong27a1a802006-11-27 21:44:48 -08003408 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
Eric Wong0864e3b2006-11-28 02:50:17 -08003409 pool => SVN::Pool->new, action => 'M' };
Eric Wong27a1a802006-11-27 21:44:48 -08003410}
3411
3412sub add_file {
3413 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08003414 my $mode;
3415
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003416 if (!is_path_ignored($path)) {
Eric Wongb03a71a2009-01-11 18:23:38 -08003417 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3418 delete $self->{empty}->{$dir};
3419 $mode = '100644';
3420 }
3421 { path => $path, mode_a => $mode, mode_b => $mode,
Eric Wong0864e3b2006-11-28 02:50:17 -08003422 pool => SVN::Pool->new, action => 'A' };
Eric Wong27a1a802006-11-27 21:44:48 -08003423}
3424
Eric Wongd2a9a872006-12-12 14:47:00 -08003425sub add_directory {
3426 my ($self, $path, $cp_path, $cp_rev) = @_;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003427 goto out if is_path_ignored($path);
Eric Wong12a6d752007-12-14 08:39:09 -08003428 my $gpath = $self->git_path($path);
3429 if ($gpath eq '') {
3430 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3431 -r --name-only -z/,
3432 $self->{c});
3433 local $/ = "\0";
3434 while (<$ls>) {
3435 chomp;
3436 $self->{gii}->remove($_);
3437 print "\tD\t$_\n" unless $::_q;
3438 }
3439 command_close_pipe($ls, $ctx);
3440 $self->{empty}->{$path} = 0;
3441 }
Eric Wongd2a9a872006-12-12 14:47:00 -08003442 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3443 delete $self->{empty}->{$dir};
3444 $self->{empty}->{$path} = 1;
Eric Wongb03a71a2009-01-11 18:23:38 -08003445out:
Eric Wongd2a9a872006-12-12 14:47:00 -08003446 { path => $path };
3447}
3448
3449sub change_dir_prop {
3450 my ($self, $db, $prop, $value) = @_;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003451 return undef if is_path_ignored($db->{path});
Eric Wongd2a9a872006-12-12 14:47:00 -08003452 $self->{dir_prop}->{$db->{path}} ||= {};
3453 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
3454 undef;
3455}
3456
3457sub absent_directory {
3458 my ($self, $path, $pb) = @_;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003459 return undef if is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08003460 $self->{absent_dir}->{$pb->{path}} ||= [];
3461 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
3462 undef;
3463}
3464
3465sub absent_file {
3466 my ($self, $path, $pb) = @_;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003467 return undef if is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08003468 $self->{absent_file}->{$pb->{path}} ||= [];
3469 push @{$self->{absent_file}->{$pb->{path}}}, $path;
3470 undef;
3471}
3472
Eric Wong27a1a802006-11-27 21:44:48 -08003473sub change_file_prop {
3474 my ($self, $fb, $prop, $value) = @_;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003475 return undef if is_path_ignored($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08003476 if ($prop eq 'svn:executable') {
3477 if ($fb->{mode_b} != 120000) {
3478 $fb->{mode_b} = defined $value ? 100755 : 100644;
3479 }
3480 } elsif ($prop eq 'svn:special') {
3481 $fb->{mode_b} = defined $value ? 120000 : 100644;
Eric Wongd2a9a872006-12-12 14:47:00 -08003482 } else {
3483 $self->{file_prop}->{$fb->{path}} ||= {};
3484 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
Eric Wong27a1a802006-11-27 21:44:48 -08003485 }
3486 undef;
3487}
3488
3489sub apply_textdelta {
3490 my ($self, $fb, $exp) = @_;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003491 return undef if is_path_ignored($fb->{path});
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003492 my $fh = $::_repository->temp_acquire('svn_delta');
Eric Wong27a1a802006-11-27 21:44:48 -08003493 # $fh gets auto-closed() by SVN::TxDelta::apply(),
3494 # (but $base does not,) so dup() it for reading in close_file
3495 open my $dup, '<&', $fh or croak $!;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003496 my $base = $::_repository->temp_acquire('git_blob');
Eric Wongb03a71a2009-01-11 18:23:38 -08003497
Eric Wong27a1a802006-11-27 21:44:48 -08003498 if ($fb->{blob}) {
Eric Wongbaf5fa82009-01-11 16:51:11 -08003499 my ($base_is_link, $size);
3500
Eric Wongdbc6c742009-01-11 16:51:10 -08003501 if ($fb->{mode_a} eq '120000' &&
3502 ! $self->{empty_symlinks}->{$fb->{path}}) {
3503 print $base 'link ' or die "print $!\n";
Eric Wongbaf5fa82009-01-11 16:51:11 -08003504 $base_is_link = 1;
Eric Wongdbc6c742009-01-11 16:51:10 -08003505 }
Eric Wongbaf5fa82009-01-11 16:51:11 -08003506 retry:
3507 $size = $::_repository->cat_blob($fb->{blob}, $base);
Junio C Hamanod683a0e2008-05-27 23:33:22 -07003508 die "Failed to read object $fb->{blob}" if ($size < 0);
Eric Wong27a1a802006-11-27 21:44:48 -08003509
3510 if (defined $exp) {
3511 seek $base, 0, 0 or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08003512 my $got = ::md5sum($base);
Eric Wongbaf5fa82009-01-11 16:51:11 -08003513 if ($got ne $exp) {
3514 my $err = "Checksum mismatch: ".
3515 "$fb->{path} $fb->{blob}\n" .
3516 "expected: $exp\n" .
3517 " got: $got\n";
3518 if ($base_is_link) {
3519 warn $err,
3520 "Retrying... (possibly ",
3521 "a bad symlink from SVN)\n";
3522 $::_repository->temp_reset($base);
3523 $base_is_link = 0;
3524 goto retry;
3525 }
3526 die $err;
3527 }
Eric Wong27a1a802006-11-27 21:44:48 -08003528 }
3529 }
3530 seek $base, 0, 0 or croak $!;
Marcus Griep0b191382008-08-12 12:00:53 -04003531 $fb->{fh} = $fh;
Eric Wong27a1a802006-11-27 21:44:48 -08003532 $fb->{base} = $base;
Marcus Griep0b191382008-08-12 12:00:53 -04003533 [ SVN::TxDelta::apply($base, $dup, undef, $fb->{path}, $fb->{pool}) ];
Eric Wong27a1a802006-11-27 21:44:48 -08003534}
3535
3536sub close_file {
3537 my ($self, $fb, $exp) = @_;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003538 return undef if is_path_ignored($fb->{path});
Eric Wongb03a71a2009-01-11 18:23:38 -08003539
Eric Wong27a1a802006-11-27 21:44:48 -08003540 my $hash;
Eric Wong706587f2007-01-18 17:50:01 -08003541 my $path = $self->git_path($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08003542 if (my $fh = $fb->{fh}) {
Eric Wong7faf0682007-05-27 15:59:01 -07003543 if (defined $exp) {
3544 seek($fh, 0, 0) or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08003545 my $got = ::md5sum($fh);
Eric Wong7faf0682007-05-27 15:59:01 -07003546 if ($got ne $exp) {
3547 die "Checksum mismatch: $path\n",
3548 "expected: $exp\n got: $got\n";
3549 }
3550 }
Eric Wong27a1a802006-11-27 21:44:48 -08003551 if ($fb->{mode_b} == 120000) {
Marcus Griep510b0942008-08-12 12:45:39 -04003552 sysseek($fh, 0, 0) or croak $!;
Eric Wongdbc6c742009-01-11 16:51:10 -08003553 my $rd = sysread($fh, my $buf, 5);
Marcus Griep510b0942008-08-12 12:45:39 -04003554
Eric Wongdbc6c742009-01-11 16:51:10 -08003555 if (!defined $rd) {
3556 croak "sysread: $!\n";
3557 } elsif ($rd == 0) {
Marcus Griep510b0942008-08-12 12:45:39 -04003558 warn "$path has mode 120000",
Eric Wongdbc6c742009-01-11 16:51:10 -08003559 " but it points to nothing\n",
3560 "converting to an empty file with mode",
3561 " 100644\n";
3562 $fb->{mode_b} = '100644';
3563 } elsif ($buf ne 'link ') {
3564 warn "$path has mode 120000",
3565 " but is not a link\n";
Marcus Griep510b0942008-08-12 12:45:39 -04003566 } else {
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003567 my $tmp_fh = $::_repository->temp_acquire(
3568 'svn_hash');
Marcus Griep510b0942008-08-12 12:45:39 -04003569 my $res;
3570 while ($res = sysread($fh, my $str, 1024)) {
3571 my $out = syswrite($tmp_fh, $str, $res);
3572 defined($out) && $out == $res
3573 or croak("write ",
Marcus Griep836ff952008-09-08 12:53:01 -04003574 Git::temp_path($tmp_fh),
Marcus Griep510b0942008-08-12 12:45:39 -04003575 ": $!\n");
3576 }
3577 defined $res or croak $!;
3578
3579 ($fh, $tmp_fh) = ($tmp_fh, $fh);
3580 Git::temp_release($tmp_fh, 1);
Eric Wong7fc35e02007-12-19 00:06:45 -08003581 }
Eric Wong27a1a802006-11-27 21:44:48 -08003582 }
Adam Robenffe256f2008-05-23 16:19:41 +02003583
Marcus Griep0b191382008-08-12 12:00:53 -04003584 $hash = $::_repository->hash_and_insert_object(
Marcus Griep836ff952008-09-08 12:53:01 -04003585 Git::temp_path($fh));
Eric Wong27a1a802006-11-27 21:44:48 -08003586 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
Marcus Griep0b191382008-08-12 12:00:53 -04003587
3588 Git::temp_release($fb->{base}, 1);
Marcus Griep510b0942008-08-12 12:45:39 -04003589 Git::temp_release($fh, 1);
Eric Wong27a1a802006-11-27 21:44:48 -08003590 } else {
3591 $hash = $fb->{blob} or die "no blob information\n";
3592 }
3593 $fb->{pool}->clear;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003594 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003595 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
Eric Wong27a1a802006-11-27 21:44:48 -08003596 undef;
3597}
3598
3599sub abort_edit {
3600 my $self = shift;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003601 $self->{nr} = $self->{gii}->{nr};
3602 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08003603 $self->SUPER::abort_edit(@_);
3604}
3605
3606sub close_edit {
3607 my $self = shift;
Eric Wongdad73c02006-11-28 14:06:05 -08003608 $self->{git_commit_ok} = 1;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003609 $self->{nr} = $self->{gii}->{nr};
3610 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08003611 $self->SUPER::close_edit(@_);
3612}
Eric Wong1a82e792006-06-16 02:55:13 -07003613
Eric Wonga5e0ced2006-06-12 15:23:48 -07003614package SVN::Git::Editor;
Eric Wong24e22aa2007-01-29 00:07:49 -08003615use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003616use strict;
3617use warnings;
3618use Carp qw/croak/;
3619use IO::File;
3620
3621sub new {
Eric Wong61395352007-01-27 14:33:08 -08003622 my ($class, $opts) = @_;
3623 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
3624 die "$_ required!\n" unless (defined $opts->{$_});
Eric Wonga5e0ced2006-06-12 15:23:48 -07003625 }
Eric Wong61395352007-01-27 14:33:08 -08003626
3627 my $pool = SVN::Pool->new;
3628 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
3629 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
3630 $opts->{r}, $mods);
3631
3632 # $opts->{ra} functions should not be used after this:
3633 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
3634 $opts->{editor_cb}, $pool);
3635 my $self = SVN::Delta::Editor->new(@ce, $pool);
3636 bless $self, $class;
3637 foreach (qw/svn_path r tree_a tree_b/) {
3638 $self->{$_} = $opts->{$_};
3639 }
3640 $self->{url} = $opts->{ra}->{url};
3641 $self->{mods} = $mods;
3642 $self->{types} = $types;
3643 $self->{pool} = $pool;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003644 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
3645 $self->{rm} = { };
Eric Wongd3a840d2007-01-26 01:32:45 -08003646 $self->{path_prefix} = length $self->{svn_path} ?
3647 "$self->{svn_path}/" : '';
Brad King128de652008-07-25 11:32:37 -04003648 $self->{config} = $opts->{config};
Eric Wonga5e0ced2006-06-12 15:23:48 -07003649 return $self;
3650}
3651
Eric Wong61395352007-01-27 14:33:08 -08003652sub generate_diff {
3653 my ($tree_a, $tree_b) = @_;
3654 my @diff_tree = qw(diff-tree -z -r);
Eric Wong24e22aa2007-01-29 00:07:49 -08003655 if ($_cp_similarity) {
3656 push @diff_tree, "-C$_cp_similarity";
Eric Wong61395352007-01-27 14:33:08 -08003657 } else {
3658 push @diff_tree, '-C';
3659 }
Eric Wong24e22aa2007-01-29 00:07:49 -08003660 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
3661 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
Eric Wong61395352007-01-27 14:33:08 -08003662 push @diff_tree, $tree_a, $tree_b;
3663 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
3664 local $/ = "\0";
3665 my $state = 'meta';
3666 my @mods;
3667 while (<$diff_fh>) {
3668 chomp $_; # this gets rid of the trailing "\0"
3669 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02003670 ($::sha1)\s($::sha1)\s
Eric Wong61395352007-01-27 14:33:08 -08003671 ([MTCRAD])\d*$/xo) {
3672 push @mods, { mode_a => $1, mode_b => $2,
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02003673 sha1_a => $3, sha1_b => $4,
3674 chg => $5 };
3675 if ($5 =~ /^(?:C|R)$/) {
Eric Wong61395352007-01-27 14:33:08 -08003676 $state = 'file_a';
3677 } else {
3678 $state = 'file_b';
3679 }
3680 } elsif ($state eq 'file_a') {
3681 my $x = $mods[$#mods] or croak "Empty array\n";
3682 if ($x->{chg} !~ /^(?:C|R)$/) {
3683 croak "Error parsing $_, $x->{chg}\n";
3684 }
3685 $x->{file_a} = $_;
3686 $state = 'file_b';
3687 } elsif ($state eq 'file_b') {
3688 my $x = $mods[$#mods] or croak "Empty array\n";
3689 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
3690 croak "Error parsing $_, $x->{chg}\n";
3691 }
3692 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
3693 croak "Error parsing $_, $x->{chg}\n";
3694 }
3695 $x->{file_b} = $_;
3696 $state = 'meta';
3697 } else {
3698 croak "Error parsing $_\n";
3699 }
3700 }
3701 command_close_pipe($diff_fh, $ctx);
3702 \@mods;
3703}
3704
3705sub check_diff_paths {
3706 my ($ra, $pfx, $rev, $mods) = @_;
3707 my %types;
3708 $pfx .= '/' if length $pfx;
3709
3710 sub type_diff_paths {
3711 my ($ra, $types, $path, $rev) = @_;
3712 my @p = split m#/+#, $path;
3713 my $c = shift @p;
3714 unless (defined $types->{$c}) {
3715 $types->{$c} = $ra->check_path($c, $rev);
3716 }
3717 while (@p) {
3718 $c .= '/' . shift @p;
3719 next if defined $types->{$c};
3720 $types->{$c} = $ra->check_path($c, $rev);
3721 }
3722 }
3723
3724 foreach my $m (@$mods) {
3725 foreach my $f (qw/file_a file_b/) {
3726 next unless defined $m->{$f};
3727 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
3728 if (length $pfx.$dir && ! defined $types{$dir}) {
3729 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
3730 }
3731 }
3732 }
3733 \%types;
3734}
3735
Eric Wonga5e0ced2006-06-12 15:23:48 -07003736sub split_path {
3737 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
3738}
3739
3740sub repo_path {
Eric Wongd3a840d2007-01-26 01:32:45 -08003741 my ($self, $path) = @_;
3742 $self->{path_prefix}.(defined $path ? $path : '');
Eric Wonga5e0ced2006-06-12 15:23:48 -07003743}
3744
3745sub url_path {
3746 my ($self, $path) = @_;
Eric Wong29633bb2007-07-15 21:53:50 -07003747 if ($self->{url} =~ m#^https?://#) {
Eric Wong6a004d32008-10-21 14:12:15 -07003748 $path =~ s/([^~a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
Eric Wong29633bb2007-07-15 21:53:50 -07003749 }
Eric Wong6e8548c2007-01-27 01:32:00 -08003750 $self->{url} . '/' . $self->repo_path($path);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003751}
3752
3753sub rmdirs {
Eric Wong61395352007-01-27 14:33:08 -08003754 my ($self) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003755 my $rm = $self->{rm};
3756 delete $rm->{''}; # we never delete the url we're tracking
3757 return unless %$rm;
3758
3759 foreach (keys %$rm) {
3760 my @d = split m#/#, $_;
3761 my $c = shift @d;
3762 $rm->{$c} = 1;
3763 while (@d) {
3764 $c .= '/' . shift @d;
3765 $rm->{$c} = 1;
3766 }
3767 }
3768 delete $rm->{$self->{svn_path}};
3769 delete $rm->{''}; # we never delete the url we're tracking
3770 return unless %$rm;
3771
Eric Wong61395352007-01-27 14:33:08 -08003772 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
3773 $self->{tree_b});
Eric Wonga5e0ced2006-06-12 15:23:48 -07003774 local $/ = "\0";
3775 while (<$fh>) {
3776 chomp;
Eric Wong747fa122006-11-24 22:38:17 -08003777 my @dn = split m#/#, $_;
Eric Wongc07eee12006-06-19 17:59:35 -07003778 while (pop @dn) {
3779 delete $rm->{join '/', @dn};
3780 }
3781 unless (%$rm) {
Eric Wong22600a22007-02-01 13:12:26 -08003782 close $fh;
Eric Wongc07eee12006-06-19 17:59:35 -07003783 return;
3784 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07003785 }
Eric Wongaef4e922006-12-15 10:59:54 -08003786 command_close_pipe($fh, $ctx);
Eric Wongc07eee12006-06-19 17:59:35 -07003787
Eric Wonga5e0ced2006-06-12 15:23:48 -07003788 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
3789 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3790 $self->close_directory($bat->{$d}, $p);
3791 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
Eric Wong44320b92007-01-13 22:35:53 -08003792 print "\tD+\t$d/\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003793 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
3794 delete $bat->{$d};
3795 }
3796}
3797
3798sub open_or_add_dir {
3799 my ($self, $full_path, $baton) = @_;
Eric Wong6e8548c2007-01-27 01:32:00 -08003800 my $t = $self->{types}->{$full_path};
3801 if (!defined $t) {
3802 die "$full_path not known in r$self->{r} or we have a bug!\n";
3803 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003804 {
3805 no warnings 'once';
3806 # SVN::Node::none and SVN::Node::file are used only once,
3807 # so we're shutting up Perl's warnings about them.
3808 if ($t == $SVN::Node::none) {
3809 return $self->add_directory($full_path, $baton,
3810 undef, -1, $self->{pool});
3811 } elsif ($t == $SVN::Node::dir) {
3812 return $self->open_directory($full_path, $baton,
3813 $self->{r}, $self->{pool});
3814 } # no warnings 'once'
3815 print STDERR "$full_path already exists in repository at ",
3816 "r$self->{r} and it is not a directory (",
3817 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
3818 } # no warnings 'once'
Eric Wonga5e0ced2006-06-12 15:23:48 -07003819 exit 1;
3820}
3821
3822sub ensure_path {
3823 my ($self, $path) = @_;
3824 my $bat = $self->{bat};
Eric Wong6e8548c2007-01-27 01:32:00 -08003825 my $repo_path = $self->repo_path($path);
3826 return $bat->{''} unless (length $repo_path);
3827 my @p = split m#/+#, $repo_path;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003828 my $c = shift @p;
3829 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3830 while (@p) {
3831 my $c0 = $c;
3832 $c .= '/' . shift @p;
3833 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3834 }
3835 return $bat->{$c};
3836}
3837
Brad King128de652008-07-25 11:32:37 -04003838# Subroutine to convert a globbing pattern to a regular expression.
3839# From perl cookbook.
3840sub glob2pat {
3841 my $globstr = shift;
3842 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
3843 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
3844 return '^' . $globstr . '$';
3845}
3846
3847sub check_autoprop {
3848 my ($self, $pattern, $properties, $file, $fbat) = @_;
3849 # Convert the globbing pattern to a regular expression.
3850 my $regex = glob2pat($pattern);
3851 # Check if the pattern matches the file name.
3852 if($file =~ m/($regex)/) {
3853 # Parse the list of properties to set.
3854 my @props = split(/;/, $properties);
3855 foreach my $prop (@props) {
3856 # Parse 'name=value' syntax and set the property.
3857 if ($prop =~ /([^=]+)=(.*)/) {
3858 my ($n,$v) = ($1,$2);
3859 for ($n, $v) {
3860 s/^\s+//; s/\s+$//;
3861 }
3862 $self->change_file_prop($fbat, $n, $v);
3863 }
3864 }
3865 }
3866}
3867
3868sub apply_autoprops {
3869 my ($self, $file, $fbat) = @_;
3870 my $conf_t = ${$self->{config}}{'config'};
3871 no warnings 'once';
3872 # Check [miscellany]/enable-auto-props in svn configuration.
3873 if (SVN::_Core::svn_config_get_bool(
3874 $conf_t,
3875 $SVN::_Core::SVN_CONFIG_SECTION_MISCELLANY,
3876 $SVN::_Core::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS,
3877 0)) {
3878 # Auto-props are enabled. Enumerate them to look for matches.
3879 my $callback = sub {
3880 $self->check_autoprop($_[0], $_[1], $file, $fbat);
3881 };
3882 SVN::_Core::svn_config_enumerate(
3883 $conf_t,
3884 $SVN::_Core::SVN_CONFIG_SECTION_AUTO_PROPS,
3885 $callback);
3886 }
3887}
3888
Eric Wonga5e0ced2006-06-12 15:23:48 -07003889sub A {
Eric Wong44320b92007-01-13 22:35:53 -08003890 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003891 my ($dir, $file) = split_path($m->{file_b});
3892 my $pbat = $self->ensure_path($dir);
3893 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3894 undef, -1);
Eric Wong44320b92007-01-13 22:35:53 -08003895 print "\tA\t$m->{file_b}\n" unless $::_q;
Brad King128de652008-07-25 11:32:37 -04003896 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003897 $self->chg_file($fbat, $m);
3898 $self->close_file($fbat,undef,$self->{pool});
3899}
3900
3901sub C {
Eric Wong44320b92007-01-13 22:35:53 -08003902 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003903 my ($dir, $file) = split_path($m->{file_b});
3904 my $pbat = $self->ensure_path($dir);
3905 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3906 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08003907 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003908 $self->chg_file($fbat, $m);
3909 $self->close_file($fbat,undef,$self->{pool});
3910}
3911
3912sub delete_entry {
3913 my ($self, $path, $pbat) = @_;
3914 my $rpath = $self->repo_path($path);
3915 my ($dir, $file) = split_path($rpath);
3916 $self->{rm}->{$dir} = 1;
3917 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
3918}
3919
3920sub R {
Eric Wong44320b92007-01-13 22:35:53 -08003921 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003922 my ($dir, $file) = split_path($m->{file_b});
3923 my $pbat = $self->ensure_path($dir);
3924 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3925 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08003926 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Paul Talacko7c4d0212008-09-06 18:50:38 -07003927 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003928 $self->chg_file($fbat, $m);
3929 $self->close_file($fbat,undef,$self->{pool});
3930
3931 ($dir, $file) = split_path($m->{file_a});
3932 $pbat = $self->ensure_path($dir);
3933 $self->delete_entry($m->{file_a}, $pbat);
3934}
3935
3936sub M {
Eric Wong44320b92007-01-13 22:35:53 -08003937 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003938 my ($dir, $file) = split_path($m->{file_b});
3939 my $pbat = $self->ensure_path($dir);
3940 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
3941 $pbat,$self->{r},$self->{pool});
Eric Wong44320b92007-01-13 22:35:53 -08003942 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003943 $self->chg_file($fbat, $m);
3944 $self->close_file($fbat,undef,$self->{pool});
3945}
3946
3947sub T { shift->M(@_) }
3948
3949sub change_file_prop {
3950 my ($self, $fbat, $pname, $pval) = @_;
3951 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
3952}
3953
Florian Weimer214a34d2008-08-31 17:45:04 +02003954sub _chg_file_get_blob ($$$$) {
3955 my ($self, $fbat, $m, $which) = @_;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003956 my $fh = $::_repository->temp_acquire("git_blob_$which");
Florian Weimer214a34d2008-08-31 17:45:04 +02003957 if ($m->{"mode_$which"} =~ /^120/) {
3958 print $fh 'link ' or croak $!;
3959 $self->change_file_prop($fbat,'svn:special','*');
3960 } elsif ($m->{mode_a} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
3961 $self->change_file_prop($fbat,'svn:special',undef);
3962 }
3963 my $blob = $m->{"sha1_$which"};
3964 return ($fh,) if ($blob =~ /^0{40}$/);
3965 my $size = $::_repository->cat_blob($blob, $fh);
3966 croak "Failed to read object $blob" if ($size < 0);
3967 $fh->flush == 0 or croak $!;
3968 seek $fh, 0, 0 or croak $!;
3969
3970 my $exp = ::md5sum($fh);
3971 seek $fh, 0, 0 or croak $!;
3972 return ($fh, $exp);
3973}
3974
Eric Wonga5e0ced2006-06-12 15:23:48 -07003975sub chg_file {
3976 my ($self, $fbat, $m) = @_;
3977 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
3978 $self->change_file_prop($fbat,'svn:executable','*');
3979 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
3980 $self->change_file_prop($fbat,'svn:executable',undef);
3981 }
Florian Weimer8598db932008-08-31 17:47:09 +02003982 my ($fh_a, $exp_a) = _chg_file_get_blob $self, $fbat, $m, 'a';
3983 my ($fh_b, $exp_b) = _chg_file_get_blob $self, $fbat, $m, 'b';
Eric Wongf7197df2006-10-14 15:48:35 -07003984 my $pool = SVN::Pool->new;
Florian Weimer8598db932008-08-31 17:47:09 +02003985 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
3986 if (-s $fh_a) {
3987 my $txstream = SVN::TxDelta::new ($fh_a, $fh_b, $pool);
Eric Wong991255c2008-08-31 19:45:07 -07003988 my $res = SVN::TxDelta::send_txstream($txstream, @$atd, $pool);
3989 if (defined $res) {
3990 die "Unexpected result from send_txstream: $res\n",
3991 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
3992 }
Florian Weimer8598db932008-08-31 17:47:09 +02003993 } else {
3994 my $got = SVN::TxDelta::send_stream($fh_b, @$atd, $pool);
3995 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
3996 if ($got ne $exp_b);
3997 }
3998 Git::temp_release($fh_b, 1);
3999 Git::temp_release($fh_a, 1);
Eric Wongf7197df2006-10-14 15:48:35 -07004000 $pool->clear;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004001}
4002
4003sub D {
Eric Wong44320b92007-01-13 22:35:53 -08004004 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004005 my ($dir, $file) = split_path($m->{file_b});
4006 my $pbat = $self->ensure_path($dir);
Eric Wong44320b92007-01-13 22:35:53 -08004007 print "\tD\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004008 $self->delete_entry($m->{file_b}, $pbat);
4009}
4010
4011sub close_edit {
4012 my ($self) = @_;
4013 my ($p,$bat) = ($self->{pool}, $self->{bat});
4014 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
Eric Wong64427542007-05-19 02:58:37 -07004015 next if $_ eq '';
Eric Wonga5e0ced2006-06-12 15:23:48 -07004016 $self->close_directory($bat->{$_}, $p);
4017 }
Eric Wong64427542007-05-19 02:58:37 -07004018 $self->close_directory($bat->{''}, $p);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004019 $self->SUPER::close_edit($p);
4020 $p->clear;
4021}
4022
4023sub abort_edit {
4024 my ($self) = @_;
4025 $self->SUPER::abort_edit($self->{pool});
Eric Wong61395352007-01-27 14:33:08 -08004026}
4027
4028sub DESTROY {
4029 my $self = shift;
4030 $self->SUPER::DESTROY(@_);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004031 $self->{pool}->clear;
4032}
4033
Eric Wong44320b92007-01-13 22:35:53 -08004034# this drives the editor
4035sub apply_diff {
Eric Wong61395352007-01-27 14:33:08 -08004036 my ($self) = @_;
4037 my $mods = $self->{mods};
Eric Wong44320b92007-01-13 22:35:53 -08004038 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
Eric Wong6e8548c2007-01-27 01:32:00 -08004039 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
Eric Wong44320b92007-01-13 22:35:53 -08004040 my $f = $m->{chg};
4041 if (defined $o{$f}) {
4042 $self->$f($m);
4043 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004044 fatal("Invalid change type: $f");
Eric Wong44320b92007-01-13 22:35:53 -08004045 }
4046 }
Eric Wong24e22aa2007-01-29 00:07:49 -08004047 $self->rmdirs if $_rmdir;
Eric Wong6e8548c2007-01-27 01:32:00 -08004048 if (@$mods == 0) {
Eric Wong44320b92007-01-13 22:35:53 -08004049 $self->abort_edit;
4050 } else {
4051 $self->close_edit;
4052 }
Eric Wong6e8548c2007-01-27 01:32:00 -08004053 return scalar @$mods;
Eric Wong44320b92007-01-13 22:35:53 -08004054}
4055
Eric Wongd81bf822007-01-10 01:22:38 -08004056package Git::SVN::Ra;
Eric Wong6af1db42007-02-14 16:04:10 -08004057use vars qw/@ISA $config_dir $_log_window_size/;
Eric Wongd81bf822007-01-10 01:22:38 -08004058use strict;
4059use warnings;
Eric Wonga51cdb02007-09-07 04:00:40 -07004060my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
Eric Wongd81bf822007-01-10 01:22:38 -08004061
4062BEGIN {
4063 # enforce temporary pool usage for some simple functions
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004064 no strict 'refs';
Eric Wongbf8a40b2009-01-25 15:35:52 -08004065 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root
4066 get_file/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004067 my $SUPER = "SUPER::$f";
4068 *$f = sub {
4069 my $self = shift;
4070 my $pool = SVN::Pool->new;
4071 my @ret = $self->$SUPER(@_,$pool);
4072 $pool->clear;
4073 wantarray ? @ret : $ret[0];
4074 };
Eric Wongd81bf822007-01-10 01:22:38 -08004075 }
Eric Wongd81bf822007-01-10 01:22:38 -08004076}
4077
Steven Walter9ff74e92007-09-28 13:24:19 -04004078sub _auth_providers () {
4079 [
4080 SVN::Client::get_simple_provider(),
4081 SVN::Client::get_ssl_server_trust_file_provider(),
4082 SVN::Client::get_simple_prompt_provider(
4083 \&Git::SVN::Prompt::simple, 2),
4084 SVN::Client::get_ssl_client_cert_file_provider(),
4085 SVN::Client::get_ssl_client_cert_prompt_provider(
4086 \&Git::SVN::Prompt::ssl_client_cert, 2),
Sebastian Noack77266e92008-02-25 15:56:28 +01004087 SVN::Client::get_ssl_client_cert_pw_file_provider(),
Steven Walter9ff74e92007-09-28 13:24:19 -04004088 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
4089 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
4090 SVN::Client::get_username_provider(),
4091 SVN::Client::get_ssl_server_trust_prompt_provider(
4092 \&Git::SVN::Prompt::ssl_server_trust),
4093 SVN::Client::get_username_prompt_provider(
4094 \&Git::SVN::Prompt::username, 2)
4095 ]
4096}
4097
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004098sub escape_uri_only {
4099 my ($uri) = @_;
4100 my @tmp;
4101 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -07004102 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004103 push @tmp, $_;
4104 }
4105 join('/', @tmp);
4106}
4107
4108sub escape_url {
4109 my ($url) = @_;
4110 if ($url =~ m#^(https?)://([^/]+)(.*)$#) {
4111 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
4112 $url = "$scheme://$domain$uri";
4113 }
4114 $url;
4115}
4116
Eric Wongd81bf822007-01-10 01:22:38 -08004117sub new {
4118 my ($class, $url) = @_;
Eric Wongf6f09872007-01-18 18:22:18 -08004119 $url =~ s!/+$!!;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004120 return $RA if ($RA && $RA->{url} eq $url);
Eric Wongf6f09872007-01-18 18:22:18 -08004121
Eric Wongd81bf822007-01-10 01:22:38 -08004122 SVN::_Core::svn_config_ensure($config_dir, undef);
Steven Walter9ff74e92007-09-28 13:24:19 -04004123 my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
Eric Wongd81bf822007-01-10 01:22:38 -08004124 my $config = SVN::Core::config_get_config($config_dir);
Eric Wong7730fbe2007-07-04 14:07:42 -07004125 $RA = undef;
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004126 my $dont_store_passwords = 1;
4127 my $conf_t = ${$config}{'config'};
4128 {
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004129 no warnings 'once';
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004130 # The usage of $SVN::_Core::SVN_CONFIG_* variables
4131 # produces warnings that variables are used only once.
4132 # I had not found the better way to shut them up, so
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004133 # the warnings of type 'once' are disabled in this block.
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004134 if (SVN::_Core::svn_config_get_bool($conf_t,
4135 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4136 $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
4137 1) == 0) {
4138 SVN::_Core::svn_auth_set_parameter($baton,
4139 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
4140 bless (\$dont_store_passwords, "_p_void"));
4141 }
4142 if (SVN::_Core::svn_config_get_bool($conf_t,
4143 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4144 $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
4145 1) == 0) {
4146 $Git::SVN::Prompt::_no_auth_cache = 1;
4147 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004148 } # no warnings 'once'
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004149 my $self = SVN::Ra->new(url => escape_url($url), auth => $baton,
Eric Wongd81bf822007-01-10 01:22:38 -08004150 config => $config,
4151 pool => SVN::Pool->new,
4152 auth_provider_callbacks => $callbacks);
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004153 $self->{url} = $url;
Eric Wongd81bf822007-01-10 01:22:38 -08004154 $self->{svn_path} = $url;
4155 $self->{repos_root} = $self->get_repos_root;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08004156 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
Eric Wong0dc03d62007-05-13 01:04:43 -07004157 $self->{cache} = { check_path => { r => 0, data => {} },
4158 get_dir => { r => 0, data => {} } };
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004159 $RA = bless $self, $class;
Eric Wongd81bf822007-01-10 01:22:38 -08004160}
4161
Eric Wong0dc03d62007-05-13 01:04:43 -07004162sub check_path {
4163 my ($self, $path, $r) = @_;
4164 my $cache = $self->{cache}->{check_path};
4165 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
4166 return $cache->{data}->{$path};
4167 }
4168 my $pool = SVN::Pool->new;
4169 my $t = $self->SUPER::check_path($path, $r, $pool);
4170 $pool->clear;
4171 if ($r != $cache->{r}) {
4172 %{$cache->{data}} = ();
4173 $cache->{r} = $r;
4174 }
4175 $cache->{data}->{$path} = $t;
4176}
4177
4178sub get_dir {
4179 my ($self, $dir, $r) = @_;
4180 my $cache = $self->{cache}->{get_dir};
4181 if ($r == $cache->{r}) {
4182 if (my $x = $cache->{data}->{$dir}) {
4183 return wantarray ? @$x : $x->[0];
4184 }
4185 }
4186 my $pool = SVN::Pool->new;
4187 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
4188 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
4189 $pool->clear;
4190 if ($r != $cache->{r}) {
4191 %{$cache->{data}} = ();
4192 $cache->{r} = $r;
4193 }
4194 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
4195 wantarray ? (\%dirents, $r, $props) : \%dirents;
4196}
4197
Eric Wongd81bf822007-01-10 01:22:38 -08004198sub DESTROY {
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004199 # do not call the real DESTROY since we store ourselves in $RA
Eric Wongd81bf822007-01-10 01:22:38 -08004200}
4201
Eric Wong1ef626b2009-01-17 22:11:44 -08004202# get_log(paths, start, end, limit,
4203# discover_changed_paths, strict_node_history, receiver)
Eric Wongd81bf822007-01-10 01:22:38 -08004204sub get_log {
4205 my ($self, @args) = @_;
4206 my $pool = SVN::Pool->new;
Eric Wong1ef626b2009-01-17 22:11:44 -08004207
4208 # the limit parameter was not supported in SVN 1.1.x, so we
4209 # drop it. Therefore, the receiver callback passed to it
4210 # is made aware of this limitation by being wrapped if
4211 # the limit passed to is being wrapped.
4212 if ($SVN::Core::VERSION le '1.2.0') {
4213 my $limit = splice(@args, 3, 1);
4214 if ($limit > 0) {
4215 my $receiver = pop @args;
4216 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
4217 }
4218 }
Eric Wongd81bf822007-01-10 01:22:38 -08004219 my $ret = $self->SUPER::get_log(@args, $pool);
4220 $pool->clear;
4221 $ret;
4222}
4223
Steven Walter9ff74e92007-09-28 13:24:19 -04004224sub trees_match {
4225 my ($self, $url1, $rev1, $url2, $rev2) = @_;
4226 my $ctx = SVN::Client->new(auth => _auth_providers);
4227 my $out = IO::File->new_tmpfile;
4228
4229 # older SVN (1.1.x) doesn't take $pool as the last parameter for
4230 # $ctx->diff(), so we'll create a default one
4231 my $pool = SVN::Pool->new_default_sub;
4232
4233 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
4234 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
4235 $out->flush;
4236 my $ret = (($out->stat)[7] == 0);
4237 close $out or croak $!;
4238
4239 $ret;
4240}
4241
Eric Wongd81bf822007-01-10 01:22:38 -08004242sub get_commit_editor {
Eric Wong44320b92007-01-13 22:35:53 -08004243 my ($self, $log, $cb, $pool) = @_;
Eric Wongd81bf822007-01-10 01:22:38 -08004244 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
Eric Wong44320b92007-01-13 22:35:53 -08004245 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004246}
4247
Eric Wongd81bf822007-01-10 01:22:38 -08004248sub gs_do_update {
Eric Wong8a603772007-01-31 02:45:50 -08004249 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
4250 my $new = ($rev_a == $rev_b);
4251 my $path = $gs->{path};
4252
Eric Wong2e5e2482007-02-23 02:21:59 -08004253 if ($new && -e $gs->{index}) {
4254 unlink $gs->{index} or die
4255 "Couldn't unlink index: $gs->{index}: $!\n";
4256 }
Eric Wongd81bf822007-01-10 01:22:38 -08004257 my $pool = SVN::Pool->new;
Eric Wong8b8fc062007-01-22 11:44:57 -08004258 $editor->set_path_strip($path);
Eric Wong2b27f6c2007-01-28 04:59:05 -08004259 my (@pc) = split m#/#, $path;
4260 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
Eric Wong8a603772007-01-31 02:45:50 -08004261 1, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004262 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong2b27f6c2007-01-28 04:59:05 -08004263
4264 # Since we can't rely on svn_ra_reparent being available, we'll
4265 # just have to do some magic with set_path to make it so
4266 # we only want a partial path.
4267 my $sp = '';
4268 my $final = join('/', @pc);
4269 while (@pc) {
4270 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
4271 $sp .= '/' if length $sp;
4272 $sp .= shift @pc;
4273 }
4274 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
4275
Eric Wong2b27f6c2007-01-28 04:59:05 -08004276 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
4277
Eric Wongd81bf822007-01-10 01:22:38 -08004278 $reporter->finish_report($pool);
4279 $pool->clear;
4280 $editor->{git_commit_ok};
4281}
4282
Eric Wong2b27f6c2007-01-28 04:59:05 -08004283# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
4284# svn_ra_reparent didn't work before 1.4)
Eric Wongd81bf822007-01-10 01:22:38 -08004285sub gs_do_switch {
Eric Wong8a603772007-01-31 02:45:50 -08004286 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
4287 my $path = $gs->{path};
Eric Wongd81bf822007-01-10 01:22:38 -08004288 my $pool = SVN::Pool->new;
Eric Wong2b27f6c2007-01-28 04:59:05 -08004289
4290 my $full_url = $self->{url};
4291 my $old_url = $full_url;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004292 $full_url .= '/' . escape_uri_only($path) if length $path;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004293 my ($ra, $reparented);
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004294
4295 if ($old_url =~ m#^svn(\+ssh)?://#) {
4296 $_[0] = undef;
4297 $self = undef;
4298 $RA = undef;
4299 $ra = Git::SVN::Ra->new($full_url);
4300 $ra_invalid = 1;
4301 } elsif ($old_url ne $full_url) {
4302 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, $pool);
4303 $self->{url} = $full_url;
4304 $reparented = 1;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004305 }
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004306
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004307 $ra ||= $self;
Eric Wongf4392df2008-09-06 20:18:18 -07004308 $url_b = escape_url($url_b);
Eric Wong8a603772007-01-31 02:45:50 -08004309 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004310 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong8b8fc062007-01-22 11:44:57 -08004311 $reporter->set_path('', $rev_a, 0, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004312 $reporter->finish_report($pool);
Eric Wong2b27f6c2007-01-28 04:59:05 -08004313
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004314 if ($reparented) {
4315 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
4316 $self->{url} = $old_url;
4317 }
Eric Wong2b27f6c2007-01-28 04:59:05 -08004318
Eric Wongd81bf822007-01-10 01:22:38 -08004319 $pool->clear;
4320 $editor->{git_commit_ok};
4321}
4322
Eric Wongb54a9012007-06-13 02:37:03 -07004323sub longest_common_path {
4324 my ($gsv, $globs) = @_;
Eric Wongd2ae1432007-02-07 11:50:16 -08004325 my %common;
Eric Wonge5181922007-02-08 12:53:57 -08004326 my $common_max = scalar @$gsv;
4327
4328 foreach my $gs (@$gsv) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004329 my @tmp = split m#/#, $gs->{path};
4330 my $p = '';
4331 foreach (@tmp) {
4332 $p .= length($p) ? "/$_" : $_;
4333 $common{$p} ||= 0;
4334 $common{$p}++;
4335 }
4336 }
Eric Wonge5181922007-02-08 12:53:57 -08004337 $globs ||= [];
4338 $common_max += scalar @$globs;
4339 foreach my $glob (@$globs) {
4340 my @tmp = split m#/#, $glob->{path}->{left};
4341 my $p = '';
4342 foreach (@tmp) {
4343 $p .= length($p) ? "/$_" : $_;
4344 $common{$p} ||= 0;
4345 $common{$p}++;
4346 }
4347 }
4348
Eric Wongd2ae1432007-02-07 11:50:16 -08004349 my $longest_path = '';
4350 foreach (sort {length $b <=> length $a} keys %common) {
Eric Wonge5181922007-02-08 12:53:57 -08004351 if ($common{$_} == $common_max) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004352 $longest_path = $_;
4353 last;
4354 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004355 }
Eric Wongb54a9012007-06-13 02:37:03 -07004356 $longest_path;
4357}
4358
4359sub gs_fetch_loop_common {
4360 my ($self, $base, $head, $gsv, $globs) = @_;
4361 return if ($base > $head);
4362 my $inc = $_log_window_size;
4363 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
4364 my $longest_path = longest_common_path($gsv, $globs);
Eric Wonga51cdb02007-09-07 04:00:40 -07004365 my $ra_url = $self->{url};
Eric Wong0af9c9f2007-01-27 22:28:56 -08004366 while (1) {
Eric Wongd4eff2b2007-01-30 14:04:22 -08004367 my %revs;
Eric Wongd2ae1432007-02-07 11:50:16 -08004368 my $err;
Eric Wongf7c3fc42007-01-29 18:34:55 -08004369 my $err_handler = $SVN::Error::handler;
Eric Wongd2ae1432007-02-07 11:50:16 -08004370 $SVN::Error::handler = sub {
4371 ($err) = @_;
4372 skip_unknown_revs($err);
4373 };
4374 sub _cb {
4375 my ($paths, $r, $author, $date, $log) = @_;
4376 [ dup_changed_paths($paths),
4377 { author => $author, date => $date, log => $log } ];
4378 }
4379 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
4380 sub { $revs{$_[1]} = _cb(@_) });
Deskin Miller99366562009-02-08 19:33:18 -05004381 if ($err) {
4382 print "Checked through r$max\r";
4383 }
Eric Wongd2ae1432007-02-07 11:50:16 -08004384 if ($err && $max >= $head) {
4385 print STDERR "Path '$longest_path' ",
4386 "was probably deleted:\n",
4387 $err->expanded_message,
4388 "\nWill attempt to follow ",
4389 "revisions r$min .. r$max ",
4390 "committed before the deletion\n";
4391 my $hi = $max;
4392 while (--$hi >= $min) {
4393 my $ok;
4394 $self->get_log([$longest_path], $min, $hi,
4395 0, 1, 1, sub {
4396 $ok ||= $_[1];
4397 $revs{$_[1]} = _cb(@_) });
4398 if ($ok) {
4399 print STDERR "r$min .. r$ok OK\n";
4400 last;
4401 }
4402 }
4403 }
Eric Wongd4eff2b2007-01-30 14:04:22 -08004404 $SVN::Error::handler = $err_handler;
Eric Wongfbcc1732007-02-06 18:35:30 -08004405
Eric Wonge5181922007-02-08 12:53:57 -08004406 my %exists = map { $_->{path} => $_ } @$gsv;
Eric Wongd4eff2b2007-01-30 14:04:22 -08004407 foreach my $r (sort {$a <=> $b} keys %revs) {
Eric Wongfbcc1732007-02-06 18:35:30 -08004408 my ($paths, $logged) = @{$revs{$r}};
Eric Wonge5181922007-02-08 12:53:57 -08004409
4410 foreach my $gs ($self->match_globs(\%exists, $paths,
4411 $globs, $r)) {
Eric Wong060610c2007-12-08 23:27:41 -08004412 if ($gs->rev_map_max >= $r) {
Eric Wongfbcc1732007-02-06 18:35:30 -08004413 next;
4414 }
4415 next unless $gs->match_paths($paths, $r);
4416 $gs->{logged_rev_props} = $logged;
Eric Wonge8d120b2007-02-14 16:29:52 -08004417 if (my $last_commit = $gs->last_commit) {
4418 $gs->assert_index_clean($last_commit);
4419 }
Eric Wongfbcc1732007-02-06 18:35:30 -08004420 my $log_entry = $gs->do_fetch($paths, $r);
4421 if ($log_entry) {
Eric Wong0af9c9f2007-01-27 22:28:56 -08004422 $gs->do_git_commit($log_entry);
4423 }
Eric Wong321b1842008-01-02 10:10:03 -08004424 $INDEX_FILES{$gs->{index}} = 1;
Eric Wong0af9c9f2007-01-27 22:28:56 -08004425 }
Eric Wonge5181922007-02-08 12:53:57 -08004426 foreach my $g (@$globs) {
Eric Wong93f26892007-02-11 01:20:26 -08004427 my $k = "svn-remote.$g->{remote}." .
4428 "$g->{t}-maxRev";
4429 Git::SVN::tmp_config($k, $r);
Eric Wonge5181922007-02-08 12:53:57 -08004430 }
Eric Wonga51cdb02007-09-07 04:00:40 -07004431 if ($ra_invalid) {
4432 $_[0] = undef;
4433 $self = undef;
4434 $RA = undef;
4435 $self = Git::SVN::Ra->new($ra_url);
4436 $ra_invalid = undef;
4437 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004438 }
Eric Wong9c93fee2007-01-31 17:22:31 -08004439 # pre-fill the .rev_db since it'll eventually get filled in
4440 # with '0' x40 if something new gets committed
Eric Wonge5181922007-02-08 12:53:57 -08004441 foreach my $gs (@$gsv) {
Eric Wong66ab84b2007-12-08 23:27:42 -08004442 next if $gs->rev_map_max >= $max;
4443 next if defined $gs->rev_map_get($max);
4444 $gs->rev_map_set($max, 0 x40);
Eric Wong9c93fee2007-01-31 17:22:31 -08004445 }
Eric Wongc3560e52007-02-12 16:03:32 -08004446 foreach my $g (@$globs) {
4447 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
4448 Git::SVN::tmp_config($k, $max);
4449 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004450 last if $max >= $head;
4451 $min = $max + 1;
4452 $max += $inc;
4453 $max = $head if ($max > $head);
4454 }
Karl Hasselström94bc9142008-02-03 17:56:18 +01004455 Git::SVN::gc();
Eric Wong0af9c9f2007-01-27 22:28:56 -08004456}
4457
Marcus Griep570d35c2008-08-08 01:41:57 -07004458sub get_dir_globbed {
4459 my ($self, $left, $depth, $r) = @_;
4460
4461 my @x = eval { $self->get_dir($left, $r) };
4462 return unless scalar @x == 3;
4463 my $dirents = $x[0];
4464 my @finalents;
4465 foreach my $de (keys %$dirents) {
4466 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
4467 if ($depth > 1) {
4468 my @args = ("$left/$de", $depth - 1, $r);
4469 foreach my $dir ($self->get_dir_globbed(@args)) {
4470 push @finalents, "$de/$dir";
4471 }
4472 } else {
4473 push @finalents, $de;
4474 }
4475 }
4476 @finalents;
4477}
4478
Eric Wonge5181922007-02-08 12:53:57 -08004479sub match_globs {
4480 my ($self, $exists, $paths, $globs, $r) = @_;
Eric Wong74a81222007-02-10 13:28:50 -08004481
4482 sub get_dir_check {
4483 my ($self, $exists, $g, $r) = @_;
Marcus Griep570d35c2008-08-08 01:41:57 -07004484
4485 my @dirs = $self->get_dir_globbed($g->{path}->{left},
4486 $g->{path}->{depth},
4487 $r);
4488
4489 foreach my $de (@dirs) {
Eric Wong74a81222007-02-10 13:28:50 -08004490 my $p = $g->{path}->full_path($de);
4491 next if $exists->{$p};
4492 next if (length $g->{path}->{right} &&
4493 ($self->check_path($p, $r) !=
4494 $SVN::Node::dir));
4495 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
4496 $g->{ref}->full_path($de), 1);
4497 }
4498 }
Eric Wonge5181922007-02-08 12:53:57 -08004499 foreach my $g (@$globs) {
Eric Wong74a81222007-02-10 13:28:50 -08004500 if (my $path = $paths->{"/$g->{path}->{left}"}) {
4501 if ($path->{action} =~ /^[AR]$/) {
4502 get_dir_check($self, $exists, $g, $r);
4503 }
4504 }
Eric Wonge5181922007-02-08 12:53:57 -08004505 foreach (keys %$paths) {
Eric Wong28710f72007-02-14 13:32:21 -08004506 if (/$g->{path}->{left_regex}/ &&
4507 !/$g->{path}->{regex}/) {
Eric Wong74a81222007-02-10 13:28:50 -08004508 next if $paths->{$_}->{action} !~ /^[AR]$/;
4509 get_dir_check($self, $exists, $g, $r);
4510 }
Eric Wonge5181922007-02-08 12:53:57 -08004511 next unless /$g->{path}->{regex}/;
4512 my $p = $1;
4513 my $pathname = $g->{path}->full_path($p);
4514 next if $exists->{$pathname};
Eric Wong0c1ec5a2007-04-18 00:17:33 -07004515 next if ($self->check_path($pathname, $r) !=
4516 $SVN::Node::dir);
Eric Wonge5181922007-02-08 12:53:57 -08004517 $exists->{$pathname} = Git::SVN->init(
4518 $self->{url}, $pathname, undef,
4519 $g->{ref}->full_path($p), 1);
4520 }
4521 my $c = '';
4522 foreach (split m#/#, $g->{path}->{left}) {
4523 $c .= "/$_";
4524 next unless ($paths->{$c} &&
Eric Wong74a81222007-02-10 13:28:50 -08004525 ($paths->{$c}->{action} =~ /^[AR]$/));
4526 get_dir_check($self, $exists, $g, $r);
Eric Wonge5181922007-02-08 12:53:57 -08004527 }
4528 }
4529 values %$exists;
4530}
4531
Eric Wonge6434f82007-01-23 16:29:23 -08004532sub minimize_url {
4533 my ($self) = @_;
4534 return $self->{url} if ($self->{url} eq $self->{repos_root});
4535 my $url = $self->{repos_root};
4536 my @components = split(m!/!, $self->{svn_path});
4537 my $c = '';
4538 do {
4539 $url .= "/$c" if length $c;
4540 eval { (ref $self)->new($url)->get_latest_revnum };
4541 } while ($@ && ($c = shift @components));
4542 $url;
4543}
4544
Eric Wongd81bf822007-01-10 01:22:38 -08004545sub can_do_switch {
4546 my $self = shift;
4547 unless (defined $can_do_switch) {
4548 my $pool = SVN::Pool->new;
4549 my $rep = eval {
4550 $self->do_switch(1, '', 0, $self->{url},
4551 SVN::Delta::Editor->new, $pool);
4552 };
4553 if ($@) {
4554 $can_do_switch = 0;
4555 } else {
4556 $rep->abort_report($pool);
4557 $can_do_switch = 1;
4558 }
4559 $pool->clear;
4560 }
4561 $can_do_switch;
4562}
4563
Eric Wong0af9c9f2007-01-27 22:28:56 -08004564sub skip_unknown_revs {
4565 my ($err) = @_;
4566 my $errno = $err->apr_err();
4567 # Maybe the branch we're tracking didn't
4568 # exist when the repo started, so it's
4569 # not an error if it doesn't, just continue
4570 #
4571 # Wonderfully consistent library, eh?
4572 # 160013 - svn:// and file://
4573 # 175002 - http(s)://
4574 # 175007 - http(s):// (this repo required authorization, too...)
4575 # More codes may be discovered later...
4576 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
Eric Wonga6a15a92007-03-30 17:54:48 -07004577 my $err_key = $err->expanded_message;
4578 # revision numbers change every time, filter them out
4579 $err_key =~ s/\d+/\0/g;
4580 $err_key = "$errno\0$err_key";
4581 unless ($ignored_err{$err_key}) {
4582 warn "W: Ignoring error from SVN, path probably ",
4583 "does not exist: ($errno): ",
4584 $err->expanded_message,"\n";
Eric Wongeee8a172008-01-07 02:40:40 -08004585 warn "W: Do not be alarmed at the above message ",
4586 "git-svn is just searching aggressively for ",
4587 "old history.\n",
4588 "This may take a while on large repositories\n";
Eric Wonga6a15a92007-03-30 17:54:48 -07004589 $ignored_err{$err_key} = 1;
4590 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004591 return;
4592 }
4593 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
4594}
4595
4596# svn_log_changed_path_t objects passed to get_log are likely to be
4597# overwritten even if only the refs are copied to an external variable,
4598# so we should dup the structures in their entirety. Using an externally
4599# passed pool (instead of our temporary and quickly cleared pool in
4600# Git::SVN::Ra) does not help matters at all...
4601sub dup_changed_paths {
4602 my ($paths) = @_;
4603 return undef unless $paths;
4604 my %ret;
4605 foreach my $p (keys %$paths) {
4606 my $i = $paths->{$p};
4607 my %s = map { $_ => $i->$_ }
4608 qw/copyfrom_path copyfrom_rev action/;
4609 $ret{$p} = \%s;
4610 }
4611 \%ret;
4612}
4613
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004614package Git::SVN::Log;
4615use strict;
4616use warnings;
4617use POSIX qw/strftime/;
David D Kilzer111947e2007-11-11 22:56:52 -08004618use constant commit_log_separator => ('-' x 72) . "\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004619use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
4620 %rusers $show_commit $incremental/;
4621my $l_fmt;
4622
4623sub cmt_showable {
4624 my ($c) = @_;
4625 return 1 if defined $c->{r};
Eric Wongc16d0872007-04-08 00:59:22 -07004626
4627 # big commit message got truncated by the 16k pretty buffer in rev-list
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004628 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
4629 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
Eric Wongc16d0872007-04-08 00:59:22 -07004630 @{$c->{l}} = ();
Eric Wong44320b92007-01-13 22:35:53 -08004631 my @log = command(qw/cat-file commit/, $c->{c});
Eric Wongc16d0872007-04-08 00:59:22 -07004632
4633 # shift off the headers
4634 shift @log while ($log[0] ne '');
Eric Wong44320b92007-01-13 22:35:53 -08004635 shift @log;
Eric Wongc16d0872007-04-08 00:59:22 -07004636
4637 # TODO: make $c->{l} not have a trailing newline in the future
4638 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004639
4640 (undef, $c->{r}, undef) = ::extract_metadata(
Eric Wong44320b92007-01-13 22:35:53 -08004641 (grep(/^git-svn-id: /, @log))[-1]);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004642 }
4643 return defined $c->{r};
4644}
4645
4646sub log_use_color {
Jeff Kingcd459e32007-12-11 01:28:42 -05004647 return $color || Git->repository->get_colorbool('color.diff');
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004648}
4649
4650sub git_svn_log_cmd {
Eric Wong3bc718b2007-02-13 17:09:40 -08004651 my ($r_min, $r_max, @args) = @_;
4652 my $head = 'HEAD';
Eric Wong6ed77262007-08-15 09:55:18 -07004653 my (@files, @log_opts);
Eric Wong3bc718b2007-02-13 17:09:40 -08004654 foreach my $x (@args) {
Eric Wong6ed77262007-08-15 09:55:18 -07004655 if ($x eq '--' || @files) {
4656 push @files, $x;
4657 } else {
4658 if (::verify_ref("$x^0")) {
4659 $head = $x;
4660 } else {
4661 push @log_opts, $x;
4662 }
4663 }
Eric Wong3bc718b2007-02-13 17:09:40 -08004664 }
4665
Eric Wong13c823f2007-04-08 00:59:19 -07004666 my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
4667 $gs ||= Git::SVN->_new;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004668 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
4669 $gs->refname);
4670 push @cmd, '-r' unless $non_recursive;
4671 push @cmd, qw/--raw --name-status/ if $verbose;
4672 push @cmd, '--color' if log_use_color();
Eric Wong6ed77262007-08-15 09:55:18 -07004673 push @cmd, @log_opts;
4674 if (defined $r_max && $r_max == $r_min) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004675 push @cmd, '--max-count=1';
Eric Wong060610c2007-12-08 23:27:41 -08004676 if (my $c = $gs->rev_map_get($r_max)) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004677 push @cmd, $c;
4678 }
Eric Wong6ed77262007-08-15 09:55:18 -07004679 } elsif (defined $r_max) {
David D Kilzer111947e2007-11-11 22:56:52 -08004680 if ($r_max < $r_min) {
4681 ($r_min, $r_max) = ($r_max, $r_min);
4682 }
4683 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
4684 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
4685 # If there are no commits in the range, both $c_max and $c_min
4686 # will be undefined. If there is at least 1 commit in the
4687 # range, both will be defined.
4688 return () if !defined $c_min || !defined $c_max;
4689 if ($c_min eq $c_max) {
4690 push @cmd, '--max-count=1', $c_min;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004691 } else {
David D Kilzer111947e2007-11-11 22:56:52 -08004692 push @cmd, '--boundary', "$c_min..$c_max";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004693 }
4694 }
Eric Wong6ed77262007-08-15 09:55:18 -07004695 return (@cmd, @files);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004696}
4697
4698# adapted from pager.c
4699sub config_pager {
4700 $pager ||= $ENV{GIT_PAGER} || $ENV{PAGER};
4701 if (!defined $pager) {
4702 $pager = 'less';
4703 } elsif (length $pager == 0 || $pager eq 'cat') {
4704 $pager = undef;
4705 }
Jeff Kingcd459e32007-12-11 01:28:42 -05004706 $ENV{GIT_PAGER_IN_USE} = defined($pager);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004707}
4708
4709sub run_pager {
Eric Wongb0193042007-09-21 18:48:45 -07004710 return unless -t *STDOUT && defined $pager;
Marcus Griep971e6282008-09-10 11:09:46 -04004711 pipe my ($rfd, $wfd) or return;
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004712 defined(my $pid = fork) or ::fatal "Can't fork: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004713 if (!$pid) {
4714 open STDOUT, '>&', $wfd or
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004715 ::fatal "Can't redirect to stdout: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004716 return;
4717 }
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004718 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004719 $ENV{LESS} ||= 'FRSX';
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004720 exec $pager or ::fatal "Can't run pager: $! ($pager)";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004721}
4722
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004723sub format_svn_date {
4724 return strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)", localtime(shift));
4725}
4726
4727sub parse_git_date {
4728 my ($t, $tz) = @_;
4729 # Date::Parse isn't in the standard Perl distro :(
4730 if ($tz =~ s/^\+//) {
4731 $t += tz_to_s_offset($tz);
4732 } elsif ($tz =~ s/^\-//) {
4733 $t -= tz_to_s_offset($tz);
4734 }
4735 return $t;
4736}
4737
4738sub set_local_timezone {
4739 if (defined $TZ) {
4740 $ENV{TZ} = $TZ;
4741 } else {
4742 delete $ENV{TZ};
4743 }
4744}
4745
Eric Wong21819a32007-01-27 14:38:10 -08004746sub tz_to_s_offset {
4747 my ($tz) = @_;
4748 $tz =~ s/(\d\d)$//;
4749 return ($1 * 60) + ($tz * 3600);
4750}
4751
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004752sub get_author_info {
4753 my ($dest, $author, $t, $tz) = @_;
4754 $author =~ s/(?:^\s*|\s*$)//g;
4755 $dest->{a_raw} = $author;
4756 my $au;
Eric Wong1c8443b2007-01-14 02:17:00 -08004757 if ($::_authors) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004758 $au = $rusers{$author} || undef;
4759 }
4760 if (!$au) {
4761 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
4762 }
4763 $dest->{t} = $t;
4764 $dest->{tz} = $tz;
4765 $dest->{a} = $au;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004766 $dest->{t_utc} = parse_git_date($t, $tz);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004767}
4768
4769sub process_commit {
4770 my ($c, $r_min, $r_max, $defer) = @_;
4771 if (defined $r_min && defined $r_max) {
4772 if ($r_min == $c->{r} && $r_min == $r_max) {
4773 show_commit($c);
4774 return 0;
4775 }
4776 return 1 if $r_min == $r_max;
4777 if ($r_min < $r_max) {
4778 # we need to reverse the print order
4779 return 0 if (defined $limit && --$limit < 0);
4780 push @$defer, $c;
4781 return 1;
4782 }
4783 if ($r_min != $r_max) {
4784 return 1 if ($r_min < $c->{r});
4785 return 1 if ($r_max > $c->{r});
4786 }
4787 }
4788 return 0 if (defined $limit && --$limit < 0);
4789 show_commit($c);
4790 return 1;
4791}
4792
4793sub show_commit {
4794 my $c = shift;
4795 if ($oneline) {
4796 my $x = "\n";
4797 if (my $l = $c->{l}) {
4798 while ($l->[0] =~ /^\s*$/) { shift @$l }
4799 $x = $l->[0];
4800 }
4801 $l_fmt ||= 'A' . length($c->{r});
4802 print 'r',pack($l_fmt, $c->{r}),' | ';
4803 print "$c->{c} | " if $show_commit;
4804 print $x;
4805 } else {
4806 show_commit_normal($c);
4807 }
4808}
4809
4810sub show_commit_changed_paths {
4811 my ($c) = @_;
4812 return unless $c->{changed};
4813 print "Changed paths:\n", @{$c->{changed}};
4814}
4815
4816sub show_commit_normal {
4817 my ($c) = @_;
David D Kilzer111947e2007-11-11 22:56:52 -08004818 print commit_log_separator, "r$c->{r} | ";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004819 print "$c->{c} | " if $show_commit;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004820 print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004821 my $nr_line = 0;
4822
4823 if (my $l = $c->{l}) {
4824 while ($l->[$#$l] eq "\n" && $#$l > 0
4825 && $l->[($#$l - 1)] eq "\n") {
4826 pop @$l;
4827 }
4828 $nr_line = scalar @$l;
4829 if (!$nr_line) {
4830 print "1 line\n\n\n";
4831 } else {
4832 if ($nr_line == 1) {
4833 $nr_line = '1 line';
4834 } else {
4835 $nr_line .= ' lines';
4836 }
4837 print $nr_line, "\n";
4838 show_commit_changed_paths($c);
4839 print "\n";
4840 print $_ foreach @$l;
4841 }
4842 } else {
4843 print "1 line\n";
4844 show_commit_changed_paths($c);
4845 print "\n";
4846
4847 }
Eric Wong488a63e2007-02-15 00:40:42 -08004848 foreach my $x (qw/raw stat diff/) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004849 if ($c->{$x}) {
4850 print "\n";
4851 print $_ foreach @{$c->{$x}}
4852 }
4853 }
4854}
4855
4856sub cmd_show_log {
4857 my (@args) = @_;
4858 my ($r_min, $r_max);
4859 my $r_last = -1; # prevent dupes
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004860 set_local_timezone();
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004861 if (defined $::_revision) {
4862 if ($::_revision =~ /^(\d+):(\d+)$/) {
4863 ($r_min, $r_max) = ($1, $2);
4864 } elsif ($::_revision =~ /^\d+$/) {
4865 $r_min = $r_max = $::_revision;
4866 } else {
4867 ::fatal "-r$::_revision is not supported, use ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004868 "standard 'git log' arguments instead";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004869 }
4870 }
4871
4872 config_pager();
Eric Wong6ed77262007-08-15 09:55:18 -07004873 @args = git_svn_log_cmd($r_min, $r_max, @args);
David D Kilzer111947e2007-11-11 22:56:52 -08004874 if (!@args) {
4875 print commit_log_separator unless $incremental || $oneline;
4876 return;
4877 }
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004878 my $log = command_output_pipe(@args);
4879 run_pager();
Eric Wong488a63e2007-02-15 00:40:42 -08004880 my (@k, $c, $d, $stat);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004881 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
4882 while (<$log>) {
David D Kilzer60f3ff12007-11-10 22:10:34 -08004883 if (/^${esc_color}commit -?($::sha1_short)/o) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004884 my $cmt = $1;
4885 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
4886 $r_last = $c->{r};
4887 process_commit($c, $r_min, $r_max, \@k) or
4888 goto out;
4889 }
4890 $d = undef;
4891 $c = { c => $cmt };
4892 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
4893 get_author_info($c, $1, $2, $3);
4894 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
4895 # ignore
4896 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
4897 push @{$c->{raw}}, $_;
4898 } elsif (/^${esc_color}[ACRMDT]\t/) {
4899 # we could add $SVN->{svn_path} here, but that requires
4900 # remote access at the moment (repo_path_split)...
4901 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
4902 push @{$c->{changed}}, $_;
4903 } elsif (/^${esc_color}diff /o) {
4904 $d = 1;
4905 push @{$c->{diff}}, $_;
4906 } elsif ($d) {
4907 push @{$c->{diff}}, $_;
Eric Wong488a63e2007-02-15 00:40:42 -08004908 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
4909 $esc_color*[\+\-]*$esc_color$/x) {
4910 $stat = 1;
4911 push @{$c->{stat}}, $_;
4912 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
4913 push @{$c->{stat}}, $_;
4914 $stat = undef;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004915 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
4916 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
4917 } elsif (s/^${esc_color} //o) {
4918 push @{$c->{l}}, $_;
4919 }
4920 }
4921 if ($c && defined $c->{r} && $c->{r} != $r_last) {
4922 $r_last = $c->{r};
4923 process_commit($c, $r_min, $r_max, \@k);
4924 }
4925 if (@k) {
David D Kilzer111947e2007-11-11 22:56:52 -08004926 ($r_min, $r_max) = ($r_max, $r_min);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004927 process_commit($_, $r_min, $r_max) foreach reverse @k;
4928 }
4929out:
Eric Wongc843c462007-01-12 03:07:31 -08004930 close $log;
David D Kilzer111947e2007-11-11 22:56:52 -08004931 print commit_log_separator unless $incremental || $oneline;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004932}
4933
Tim Stoakes6fb53752008-02-10 15:21:08 +10304934sub cmd_blame {
Steven Grimm4be40382008-05-10 22:11:18 -07004935 my $path = pop;
Tim Stoakes6fb53752008-02-10 15:21:08 +10304936
4937 config_pager();
4938 run_pager();
4939
Steven Grimm4be40382008-05-10 22:11:18 -07004940 my ($fh, $ctx, $rev);
4941
4942 if ($_git_format) {
4943 ($fh, $ctx) = command_output_pipe('blame', @_, $path);
4944 while (my $line = <$fh>) {
4945 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
4946 # Uncommitted edits show up as a rev ID of
4947 # all zeros, which we can't look up with
4948 # cmt_metadata
4949 if ($1 !~ /^0+$/) {
4950 (undef, $rev, undef) =
4951 ::cmt_metadata($1);
4952 $rev = '0' if (!$rev);
4953 } else {
4954 $rev = '0';
4955 }
4956 $rev = sprintf('%-10s', $rev);
4957 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
4958 }
4959 print $line;
Tim Stoakes6fb53752008-02-10 15:21:08 +10304960 }
Steven Grimm4be40382008-05-10 22:11:18 -07004961 } else {
4962 ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD',
4963 '--', $path);
4964 my ($sha1);
4965 my %authors;
4966 while (my $line = <$fh>) {
4967 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
4968 $sha1 = $1;
4969 (undef, $rev, undef) = ::cmt_metadata($1);
4970 $rev = '0' if (!$rev);
4971 }
4972 elsif ($line =~ /^author (.*)/) {
4973 $authors{$rev} = $1;
4974 $authors{$rev} =~ s/\s/_/g;
4975 }
4976 elsif ($line =~ /^\t(.*)$/) {
4977 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
4978 }
4979 }
Tim Stoakes6fb53752008-02-10 15:21:08 +10304980 }
4981 command_close_pipe($fh, $ctx);
4982}
4983
Eric Wong706587f2007-01-18 17:50:01 -08004984package Git::SVN::Migration;
4985# these version numbers do NOT correspond to actual version numbers
4986# of git nor git-svn. They are just relative.
4987#
4988# v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
4989#
4990# v1 layout: .git/$id/info/url, refs/remotes/$id
4991#
4992# v2 layout: .git/svn/$id/info/url, refs/remotes/$id
4993#
4994# v3 layout: .git/svn/$id, refs/remotes/$id
4995# - info/url may remain for backwards compatibility
4996# - this is what we migrate up to this layout automatically,
4997# - this will be used by git svn init on single branches
Eric Wong26a62d52007-02-12 13:25:25 -08004998# v3.1 layout (auto migrated):
4999# - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
5000# for backwards compatibility
Eric Wong706587f2007-01-18 17:50:01 -08005001#
5002# v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
5003# - this is only created for newly multi-init-ed
5004# repositories. Similar in spirit to the
5005# --use-separate-remotes option in git-clone (now default)
5006# - we do not automatically migrate to this (following
5007# the example set by core git)
Eric Wong060610c2007-12-08 23:27:41 -08005008#
5009# v5 layout: .rev_db.$UUID => .rev_map.$UUID
5010# - newer, more-efficient format that uses 24-bytes per record
5011# with no filler space.
5012# - use xxd -c24 < .rev_map.$UUID to view and debug
5013# - This is a one-way migration, repositories updated to the
5014# new format will not be able to use old git-svn without
5015# rebuilding the .rev_db. Rebuilding the rev_db is not
5016# possible if noMetadata or useSvmProps are set; but should
5017# be no problem for users that use the (sensible) defaults.
Eric Wong706587f2007-01-18 17:50:01 -08005018use strict;
5019use warnings;
5020use Carp qw/croak/;
5021use File::Path qw/mkpath/;
Eric Wong47e39c52007-01-21 04:27:09 -08005022use File::Basename qw/dirname basename/;
5023use vars qw/$_minimize/;
Eric Wong706587f2007-01-18 17:50:01 -08005024
5025sub migrate_from_v0 {
5026 my $git_dir = $ENV{GIT_DIR};
5027 return undef unless -d $git_dir;
5028 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5029 my $migrated = 0;
5030 while (<$fh>) {
5031 chomp;
5032 my ($id, $orig_ref) = ($_, $_);
5033 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
5034 next unless -f "$git_dir/$id/info/url";
5035 my $new_ref = "refs/remotes/$id";
5036 if (::verify_ref("$new_ref^0")) {
5037 print STDERR "W: $orig_ref is probably an old ",
5038 "branch used by an ancient version of ",
5039 "git-svn.\n",
5040 "However, $new_ref also exists.\n",
5041 "We will not be able ",
5042 "to use this branch until this ",
5043 "ambiguity is resolved.\n";
5044 next;
5045 }
5046 print STDERR "Migrating from v0 layout...\n" if !$migrated;
5047 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
5048 command_noisy('update-ref', $new_ref, $orig_ref);
5049 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
5050 $migrated++;
5051 }
5052 command_close_pipe($fh, $ctx);
5053 print STDERR "Done migrating from v0 layout...\n" if $migrated;
5054 $migrated;
5055}
5056
5057sub migrate_from_v1 {
5058 my $git_dir = $ENV{GIT_DIR};
5059 my $migrated = 0;
5060 return $migrated unless -d $git_dir;
5061 my $svn_dir = "$git_dir/svn";
5062
5063 # just in case somebody used 'svn' as their $id at some point...
5064 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
5065
5066 print STDERR "Migrating from a git-svn v1 layout...\n";
5067 mkpath([$svn_dir]);
5068 print STDERR "Data from a previous version of git-svn exists, but\n\t",
5069 "$svn_dir\n\t(required for this version ",
Frederik Schwarzer8f510be2008-07-14 18:30:24 +02005070 "($::VERSION) of git-svn) does not exist.\n";
Eric Wong706587f2007-01-18 17:50:01 -08005071 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5072 while (<$fh>) {
5073 my $x = $_;
5074 next unless $x =~ s#^refs/remotes/##;
5075 chomp $x;
5076 next unless -f "$git_dir/$x/info/url";
5077 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
5078 next unless $u;
5079 my $dn = dirname("$git_dir/svn/$x");
5080 mkpath([$dn]) unless -d $dn;
5081 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
5082 mkpath(["$git_dir/svn/svn"]);
5083 print STDERR " - $git_dir/$x/info => ",
5084 "$git_dir/svn/$x/info\n";
5085 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
5086 croak "$!: $x";
5087 # don't worry too much about these, they probably
5088 # don't exist with repos this old (save for index,
5089 # and we can easily regenerate that)
5090 foreach my $f (qw/unhandled.log index .rev_db/) {
5091 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
5092 }
5093 } else {
5094 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
5095 rename "$git_dir/$x", "$git_dir/svn/$x" or
5096 croak "$!: $x";
5097 }
5098 $migrated++;
5099 }
5100 command_close_pipe($fh, $ctx);
5101 print STDERR "Done migrating from a git-svn v1 layout\n";
5102 $migrated;
5103}
5104
5105sub read_old_urls {
5106 my ($l_map, $pfx, $path) = @_;
5107 my @dir;
5108 foreach (<$path/*>) {
5109 if (-r "$_/info/url") {
5110 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
5111 my $ref_id = $pfx . basename $_;
5112 my $url = ::file_to_s("$_/info/url");
5113 $l_map->{$ref_id} = $url;
5114 } elsif (-d $_) {
5115 push @dir, $_;
5116 }
5117 }
5118 foreach (@dir) {
5119 my $x = $_;
5120 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
5121 read_old_urls($l_map, $x, $_);
5122 }
5123}
5124
5125sub migrate_from_v2 {
5126 my @cfg = command(qw/config -l/);
5127 return if grep /^svn-remote\..+\.url=/, @cfg;
5128 my %l_map;
5129 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
5130 my $migrated = 0;
5131
5132 foreach my $ref_id (sort keys %l_map) {
Eric Wong471bc002007-02-01 04:06:27 -08005133 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
5134 if ($@) {
5135 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
5136 }
Eric Wong706587f2007-01-18 17:50:01 -08005137 $migrated++;
5138 }
5139 $migrated;
5140}
5141
Eric Wong47e39c52007-01-21 04:27:09 -08005142sub minimize_connections {
5143 my $r = Git::SVN::read_all_remotes();
5144 my $new_urls = {};
5145 my $root_repos = {};
5146 foreach my $repo_id (keys %$r) {
5147 my $url = $r->{$repo_id}->{url} or next;
5148 my $fetch = $r->{$repo_id}->{fetch} or next;
5149 my $ra = Git::SVN::Ra->new($url);
5150
5151 # skip existing cases where we already connect to the root
5152 if (($ra->{url} eq $ra->{repos_root}) ||
Eric Wong7829f202008-06-28 20:40:32 -07005153 ($ra->{repos_root} eq $repo_id)) {
Eric Wong47e39c52007-01-21 04:27:09 -08005154 $root_repos->{$ra->{url}} = $repo_id;
5155 next;
5156 }
5157
5158 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
5159 my $root_path = $ra->{url};
Eric Wong4e9f6cc2007-02-09 12:17:57 -08005160 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
Eric Wong47e39c52007-01-21 04:27:09 -08005161 foreach my $path (keys %$fetch) {
5162 my $ref_id = $fetch->{$path};
5163 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
5164
5165 # make sure we can read when connecting to
5166 # a higher level of a repository
5167 my ($last_rev, undef) = $gs->last_rev_commit;
5168 if (!defined $last_rev) {
5169 $last_rev = eval {
5170 $root_ra->get_latest_revnum;
5171 };
5172 next if $@;
5173 }
5174 my $new = $root_path;
5175 $new .= length $path ? "/$path" : '';
5176 eval {
5177 $root_ra->get_log([$new], $last_rev, $last_rev,
5178 0, 0, 1, sub { });
5179 };
5180 next if $@;
5181 $new_urls->{$ra->{repos_root}}->{$new} =
5182 { ref_id => $ref_id,
5183 old_repo_id => $repo_id,
5184 old_path => $path };
5185 }
5186 }
5187
5188 my @emptied;
5189 foreach my $url (keys %$new_urls) {
5190 # see if we can re-use an existing [svn-remote "repo_id"]
5191 # instead of creating a(n ugly) new section:
Eric Wong7829f202008-06-28 20:40:32 -07005192 my $repo_id = $root_repos->{$url} || $url;
Eric Wong47e39c52007-01-21 04:27:09 -08005193
5194 my $fetch = $new_urls->{$url};
5195 foreach my $path (keys %$fetch) {
5196 my $x = $fetch->{$path};
5197 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
5198 my $pfx = "svn-remote.$x->{old_repo_id}";
5199
5200 my $old_fetch = quotemeta("$x->{old_path}:".
5201 "refs/remotes/$x->{ref_id}");
Eric Wong8b8fc062007-01-22 11:44:57 -08005202 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08005203 "$pfx.fetch", '^'. $old_fetch . '$');
5204 delete $r->{$x->{old_repo_id}}->
5205 {fetch}->{$x->{old_path}};
5206 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
Eric Wong8b8fc062007-01-22 11:44:57 -08005207 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08005208 "$pfx.url");
5209 push @emptied, $x->{old_repo_id}
5210 }
5211 }
5212 }
5213 if (@emptied) {
Johannes Schindelin8befc502008-12-14 23:10:52 +01005214 my $file = $ENV{GIT_CONFIG} || "$ENV{GIT_DIR}/config";
Eric Wong47e39c52007-01-21 04:27:09 -08005215 print STDERR <<EOF;
5216The following [svn-remote] sections in your config file ($file) are empty
5217and can be safely removed:
5218EOF
5219 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
5220 }
5221}
5222
Eric Wong706587f2007-01-18 17:50:01 -08005223sub migration_check {
5224 migrate_from_v0();
5225 migrate_from_v1();
5226 migrate_from_v2();
Eric Wong47e39c52007-01-21 04:27:09 -08005227 minimize_connections() if $_minimize;
Eric Wong706587f2007-01-18 17:50:01 -08005228}
5229
Eric Wongef3cfaa2007-01-24 03:30:57 -08005230package Git::IndexInfo;
5231use strict;
5232use warnings;
5233use Git qw/command_input_pipe command_close_pipe/;
5234
5235sub new {
5236 my ($class) = @_;
5237 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
5238 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
5239}
5240
5241sub remove {
5242 my ($self, $path) = @_;
5243 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
5244 return ++$self->{nr};
5245 }
5246 undef;
5247}
5248
5249sub update {
5250 my ($self, $mode, $hash, $path) = @_;
5251 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
5252 return ++$self->{nr};
5253 }
5254 undef;
5255}
5256
5257sub DESTROY {
5258 my ($self) = @_;
5259 command_close_pipe($self->{gui}, $self->{ctx});
5260}
5261
Eric Wong4bb9ed02007-02-03 13:29:17 -08005262package Git::SVN::GlobSpec;
5263use strict;
5264use warnings;
5265
5266sub new {
5267 my ($class, $glob) = @_;
Eric Wong4bb9ed02007-02-03 13:29:17 -08005268 my $re = $glob;
5269 $re =~ s!/+$!!g; # no need for trailing slashes
Marcus Griep570d35c2008-08-08 01:41:57 -07005270 $re =~ m!^([^*]*)(\*(?:/\*)*)([^*]*)$!;
5271 my $temp = $re;
5272 my ($left, $right) = ($1, $3);
5273 $re = $2;
5274 my $depth = $re =~ tr/*/*/;
5275 if ($depth != $temp =~ tr/*/*/) {
5276 die "Only one set of wildcard directories " .
5277 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
5278 }
5279 if ($depth == 0) {
Eric Wonge5181922007-02-08 12:53:57 -08005280 die "One '*' is needed for glob: '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08005281 }
Marcus Griep570d35c2008-08-08 01:41:57 -07005282 $re =~ s!\*!\[^/\]*!g;
5283 $re = quotemeta($left) . "($re)" . quotemeta($right);
Eric Wong4e9f6cc2007-02-09 12:17:57 -08005284 if (length $left && !($left =~ s!/+$!!g)) {
5285 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
5286 }
5287 if (length $right && !($right =~ s!^/+!!g)) {
5288 die "Missing leading '/' on right side of: '$glob' ($right)\n";
5289 }
Eric Wong74a81222007-02-10 13:28:50 -08005290 my $left_re = qr/^\/\Q$left\E(\/|$)/;
5291 bless { left => $left, right => $right, left_regex => $left_re,
Marcus Griep570d35c2008-08-08 01:41:57 -07005292 regex => qr/$re/, glob => $glob, depth => $depth }, $class;
Eric Wong4bb9ed02007-02-03 13:29:17 -08005293}
5294
5295sub full_path {
5296 my ($self, $path) = @_;
5297 return (length $self->{left} ? "$self->{left}/" : '') .
5298 $path . (length $self->{right} ? "/$self->{right}" : '');
5299}
5300
Eric Wong3397f9d2006-02-16 01:24:16 -08005301__END__
5302
5303Data structures:
5304
Eric Wong4bb9ed02007-02-03 13:29:17 -08005305
5306$remotes = { # returned by read_all_remotes()
5307 'svn' => {
5308 # svn-remote.svn.url=https://svn.musicpd.org
5309 url => 'https://svn.musicpd.org',
5310 # svn-remote.svn.fetch=mpd/trunk:trunk
5311 fetch => {
5312 'mpd/trunk' => 'trunk',
5313 },
5314 # svn-remote.svn.tags=mpd/tags/*:tags/*
5315 tags => {
5316 path => {
5317 left => 'mpd/tags',
5318 right => '',
5319 regex => qr!mpd/tags/([^/]+)$!,
5320 glob => 'tags/*',
5321 },
5322 ref => {
5323 left => 'tags',
5324 right => '',
5325 regex => qr!tags/([^/]+)$!,
5326 glob => 'tags/*',
5327 },
5328 }
5329 }
5330};
5331
Eric Wong44320b92007-01-13 22:35:53 -08005332$log_entry hashref as returned by libsvn_log_entry()
Eric Wong3397f9d2006-02-16 01:24:16 -08005333{
Eric Wong44320b92007-01-13 22:35:53 -08005334 log => 'whitespace-formatted log entry
Eric Wong3397f9d2006-02-16 01:24:16 -08005335', # trailing newline is preserved
5336 revision => '8', # integer
5337 date => '2004-02-24T17:01:44.108345Z', # commit date
5338 author => 'committer name'
5339};
5340
Eric Wong6e8548c2007-01-27 01:32:00 -08005341
5342# this is generated by generate_diff();
Eric Wong3397f9d2006-02-16 01:24:16 -08005343@mods = array of diff-index line hashes, each element represents one line
5344 of diff-index output
5345
5346diff-index line ($m hash)
5347{
5348 mode_a => first column of diff-index output, no leading ':',
5349 mode_b => second column of diff-index output,
5350 sha1_b => sha1sum of the final blob,
Eric Wongac8e0b92006-03-03 01:20:07 -08005351 chg => change type [MCRADT],
Eric Wong3397f9d2006-02-16 01:24:16 -08005352 file_a => original file name of a file (iff chg is 'C' or 'R')
5353 file_b => new/current file name of a file (any chg)
5354}
5355;
Eric Wonga5e0ced2006-06-12 15:23:48 -07005356
Eric Wonga00439a2006-06-27 19:39:13 -07005357# retval of read_url_paths{,_all}();
5358$l_map = {
5359 # repository root url
5360 'https://svn.musicpd.org' => {
5361 # repository path # GIT_SVN_ID
5362 'mpd/trunk' => 'trunk',
5363 'mpd/tags/0.11.5' => 'tags/0.11.5',
5364 },
5365}
5366
Eric Wonga5e0ced2006-06-12 15:23:48 -07005367Notes:
5368 I don't trust the each() function on unless I created %hash myself
5369 because the internal iterator may not have started at base.