blob: 43e1591cef4e69a1d06463ad996190b89df6cfe6 [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
Eric Wong9760adc2007-01-31 03:06:56 -08007 $sha1 $sha1_short $_revision
8 $_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/;
38use IO::File qw//;
39use File::Basename qw/dirname basename/;
40use File::Path qw/mkpath/;
Eric Wong512b6202007-04-03 01:57:08 -070041use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
Eric Wong968bdf12006-06-15 13:36:12 -070042use IPC::Open3;
Eric Wong336f1712007-01-11 02:14:43 -080043use Git;
Eric Wonga5e0ced2006-06-12 15:23:48 -070044
Eric Wong336f1712007-01-11 02:14:43 -080045BEGIN {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120046 # import functions from Git into our packages, en masse
47 no strict 'refs';
Eric Wong336f1712007-01-11 02:14:43 -080048 foreach (qw/command command_oneline command_noisy command_output_pipe
49 command_input_pipe command_close_pipe/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120050 for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
David D. Kilzerb2b3ada2007-11-20 22:43:17 -080051 Git::SVN::Migration Git::SVN::Log Git::SVN
52 Git::SVN::Util),
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,
David D. Kilzer8b014d72007-11-21 11:57:19 -080068 $_prefix, $_no_checkout, $_url, $_verbose);
Eric Wong0bed5ea2007-02-09 02:45:03 -080069$Git::SVN::_follow_parent = 1;
Eric Wong706587f2007-01-18 17:50:01 -080070my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
71 'config-dir=s' => \$Git::SVN::Ra::config_dir,
72 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache );
Eric Wong0bed5ea2007-02-09 02:45:03 -080073my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
Eric Wongdc5869c2006-05-24 02:07:32 -070074 'authors-file|A=s' => \$_authors,
Eric Wongecc712d2007-01-31 12:28:10 -080075 'repack:i' => \$Git::SVN::_repack,
Eric Wong97ae0912007-02-11 15:21:24 -080076 'noMetadata' => \$Git::SVN::_no_metadata,
77 'useSvmProps' => \$Git::SVN::_use_svm_props,
Eric Wong62e349d2007-02-16 19:57:29 -080078 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props,
Eric Wong6af1db42007-02-14 16:04:10 -080079 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size,
Eric Wong1e889ef2007-02-16 01:45:13 -080080 'no-checkout' => \$_no_checkout,
Eric Wong80f50742006-06-27 19:39:14 -070081 'quiet|q' => \$_q,
Eric Wongecc712d2007-01-31 12:28:10 -080082 'repack-flags|repack-args|repack-opts=s' =>
83 \$Git::SVN::_repack_flags,
Eric Wong706587f2007-01-18 17:50:01 -080084 %remote_opts );
Eric Wong36f5b1f2006-05-23 19:23:41 -070085
martin f. krafft8f728fb2007-07-14 11:25:28 +020086my ($_trunk, $_tags, $_branches, $_stdlayout);
Eric Wong0dfaf0a2007-02-18 02:34:09 -080087my %icv;
Eric Wongdadc6d22007-02-14 12:27:41 -080088my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared,
89 'trunk|T=s' => \$_trunk, 'tags|t=s' => \$_tags,
90 'branches|b=s' => \$_branches, 'prefix=s' => \$_prefix,
martin f. krafft8f728fb2007-07-14 11:25:28 +020091 'stdlayout|s' => \$_stdlayout,
Eric Wong4a1bb4c2007-05-13 09:58:14 -070092 'minimize-url|m' => \$Git::SVN::_minimize_url,
Eric Wong0dfaf0a2007-02-18 02:34:09 -080093 'no-metadata' => sub { $icv{noMetadata} = 1 },
94 'use-svm-props' => sub { $icv{useSvmProps} = 1 },
95 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 },
96 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] },
Eric Wongdadc6d22007-02-14 12:27:41 -080097 %remote_opts );
Eric Wong27e9fb82006-06-27 19:39:12 -070098my %cmt_opts = ( 'edit|e' => \$_edit,
Eric Wong24e22aa2007-01-29 00:07:49 -080099 'rmdir' => \$SVN::Git::Editor::_rmdir,
100 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder,
101 'l=i' => \$SVN::Git::Editor::_rename_limit,
102 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity
Eric Wong27e9fb82006-06-27 19:39:12 -0700103);
Eric Wong9d55b412006-06-12 15:53:13 -0700104
Eric Wong3397f9d2006-02-16 01:24:16 -0800105my %cmd = (
Eric Wong2a3240b2007-01-04 18:09:56 -0800106 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
Eric Wonge98671e2007-02-14 02:21:19 -0800107 { 'revision|r=s' => \$_revision,
Eric Wong905f8b72007-02-16 03:22:40 -0800108 'fetch-all|all' => \$_fetch_all,
Eric Wonge98671e2007-02-14 02:21:19 -0800109 %fc_opts } ],
Eric Wong0425ea92007-02-16 18:45:01 -0800110 clone => [ \&cmd_clone, "Initialize and fetch revisions",
111 { 'revision|r=s' => \$_revision,
112 %fc_opts, %init_opts } ],
Eric Wongd2866f92007-01-11 12:26:16 -0800113 init => [ \&cmd_init, "Initialize a repo for tracking" .
Eric Wongf8ab6b72006-05-31 15:49:56 -0700114 " (requires URL argument)",
Eric Wong9d55b412006-06-12 15:53:13 -0700115 \%init_opts ],
Eric Wongdadc6d22007-02-14 12:27:41 -0800116 'multi-init' => [ \&cmd_multi_init,
117 "Deprecated alias for ".
118 "'$0 init -T<trunk> -b<branches> -t<tags>'",
119 \%init_opts ],
Eric Wongd7ad3be2007-01-14 03:14:28 -0800120 dcommit => [ \&cmd_dcommit,
121 'Commit several diffs to merge with upstream',
Eric Wong3289e862006-12-15 23:58:08 -0800122 { 'merge|m|M' => \$_merge,
123 'strategy|s=s' => \$_strategy,
Eric Wong905f8b72007-02-16 03:22:40 -0800124 'verbose|v' => \$_verbose,
Eric Wong3289e862006-12-15 23:58:08 -0800125 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800126 'fetch-all|all' => \$_fetch_all,
Karl Hasselström171af112007-05-03 07:51:35 +0200127 'no-rebase' => \$_no_rebase,
Eric Wong4b155222006-12-22 21:59:24 -0800128 %cmt_opts, %fc_opts } ],
Eric Wong1ce255d2007-01-14 23:21:16 -0800129 'set-tree' => [ \&cmd_set_tree,
130 "Set an SVN repository to a git tree-ish",
131 { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200132 'create-ignore' => [ \&cmd_create_ignore,
133 'Create a .gitignore per svn:ignore',
134 { 'revision|r=i' => \$_revision
135 } ],
Benoit Sigoure15153452007-10-16 16:36:50 +0200136 'propget' => [ \&cmd_propget,
137 'Print the value of a property on a file or directory',
138 { 'revision|r=i' => \$_revision } ],
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200139 'proplist' => [ \&cmd_proplist,
140 'List all properties of a file or directory',
141 { 'revision|r=i' => \$_revision } ],
Eric Wong5969cbe2007-01-11 17:58:39 -0800142 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200143 { 'revision|r=i' => \$_revision
Lars Hjemli05b4df32007-09-05 11:35:29 +0200144 } ],
Eric Wong1c8443b2007-01-14 02:17:00 -0800145 'multi-fetch' => [ \&cmd_multi_fetch,
Eric Wonge98671e2007-02-14 02:21:19 -0800146 "Deprecated alias for $0 fetch --all",
147 { 'revision|r=s' => \$_revision, %fc_opts } ],
Eric Wong706587f2007-01-18 17:50:01 -0800148 'migrate' => [ sub { },
149 # no-op, we automatically run this anyways,
Eric Wong706587f2007-01-18 17:50:01 -0800150 'Migrate configuration/metadata/layout from
151 previous versions of git-svn',
Eric Wonga836a0e2007-02-14 19:34:56 -0800152 { 'minimize' => \$Git::SVN::Migration::_minimize,
153 %remote_opts } ],
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800154 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
155 { 'limit=i' => \$Git::SVN::Log::limit,
Eric Wong79bb8d82006-06-01 02:35:44 -0700156 'revision|r=s' => \$_revision,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800157 'verbose|v' => \$Git::SVN::Log::verbose,
158 'incremental' => \$Git::SVN::Log::incremental,
159 'oneline' => \$Git::SVN::Log::oneline,
160 'show-commit' => \$Git::SVN::Log::show_commit,
161 'non-recursive' => \$Git::SVN::Log::non_recursive,
Eric Wong79bb8d82006-06-01 02:35:44 -0700162 'authors-file|A=s' => \$_authors,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800163 'color' => \$Git::SVN::Log::color,
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200164 'pager=s' => \$Git::SVN::Log::pager
Eric Wong79bb8d82006-06-01 02:35:44 -0700165 } ],
Adam Roben26e60162007-04-27 11:57:53 -0700166 'find-rev' => [ \&cmd_find_rev, "Translate between SVN revision numbers and tree-ish",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200167 {} ],
Eric Wong905f8b72007-02-16 03:22:40 -0800168 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
169 { 'merge|m|M' => \$_merge,
170 'verbose|v' => \$_verbose,
171 'strategy|s=s' => \$_strategy,
Eric Wongdee41f32007-03-13 11:40:36 -0700172 'local|l' => \$_local,
Eric Wong905f8b72007-02-16 03:22:40 -0800173 'fetch-all|all' => \$_fetch_all,
174 %fc_opts } ],
Eric Wong44320b92007-01-13 22:35:53 -0800175 'commit-diff' => [ \&cmd_commit_diff,
176 'Commit a diff between two trees',
Eric Wong27e9fb82006-06-27 19:39:12 -0700177 { 'message|m=s' => \$_message,
178 'file|F=s' => \$_file,
Eric Wong45bf4732006-11-09 01:19:37 -0800179 'revision|r=s' => \$_revision,
Eric Wong27e9fb82006-06-27 19:39:12 -0700180 %cmt_opts } ],
David D. Kilzere6fefa92007-11-21 11:57:18 -0800181 'info' => [ \&cmd_info,
182 "Show info about the latest SVN revision
183 on the current branch",
David D. Kilzer8b014d72007-11-21 11:57:19 -0800184 { 'url' => \$_url, } ],
Eric Wong3397f9d2006-02-16 01:24:16 -0800185);
Eric Wong9d55b412006-06-12 15:53:13 -0700186
Eric Wong3397f9d2006-02-16 01:24:16 -0800187my $cmd;
188for (my $i = 0; $i < @ARGV; $i++) {
189 if (defined $cmd{$ARGV[$i]}) {
190 $cmd = $ARGV[$i];
191 splice @ARGV, $i, 1;
192 last;
193 }
194};
195
Eric Wong448c81b2006-03-03 01:20:09 -0800196my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
Eric Wonga9612be2006-03-03 01:20:08 -0800197
Eric Wongb8c92ca2006-05-24 01:40:37 -0700198read_repo_config(\%opts);
Eric Wongc2849142007-04-17 02:41:43 -0700199Getopt::Long::Configure('pass_through') if ($cmd && $cmd eq 'log');
Eric Wong9760adc2007-01-31 03:06:56 -0800200my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
201 'minimize-connections' => \$Git::SVN::Migration::_minimize,
202 'id|i=s' => \$Git::SVN::default_ref_id,
Eric Wongbefc9ad2007-02-17 02:53:07 -0800203 'svn-remote|remote|R=s' => sub {
204 $Git::SVN::no_reuse_existing = 1;
205 $Git::SVN::default_repo_id = $_[1] });
Eric Wongc2849142007-04-17 02:41:43 -0700206exit 1 if (!$rv && $cmd && $cmd ne 'log');
Eric Wong6f0783c2006-03-03 01:20:09 -0800207
Eric Wong3397f9d2006-02-16 01:24:16 -0800208usage(0) if $_help;
Eric Wong551ce282006-02-20 10:57:29 -0800209version() if $_version;
Eric Wongeeb0abe2006-03-03 01:20:08 -0800210usage(1) unless defined $cmd;
211load_authors() if $_authors;
Eric Wong5253dc32007-02-20 01:36:30 -0800212
213# make sure we're always running
214unless ($cmd =~ /(?:clone|init|multi-init)$/) {
215 unless (-d $ENV{GIT_DIR}) {
216 if ($git_dir_user_set) {
217 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
218 "but it is not a directory\n";
219 }
220 my $git_dir = delete $ENV{GIT_DIR};
221 chomp(my $cdup = command_oneline(qw/rev-parse --show-cdup/));
222 unless (length $cdup) {
223 die "Already at toplevel, but $git_dir ",
224 "not found '$cdup'\n";
225 }
226 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
227 unless (-d $git_dir) {
228 die "$git_dir still not found after going to ",
229 "'$cdup'\n";
230 }
231 $ENV{GIT_DIR} = $git_dir;
232 }
233}
Eric Wong0425ea92007-02-16 18:45:01 -0800234unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
Eric Wong706587f2007-01-18 17:50:01 -0800235 Git::SVN::Migration::migration_check();
236}
Eric Wongecc712d2007-01-31 12:28:10 -0800237Git::SVN::init_vars();
Eric Wongb805b442007-01-22 13:52:04 -0800238eval {
239 Git::SVN::verify_remotes_sanity();
240 $cmd{$cmd}->[0]->(@ARGV);
241};
242fatal $@ if $@;
Eric Wong1e889ef2007-02-16 01:45:13 -0800243post_fetch_checkout();
Eric Wong3397f9d2006-02-16 01:24:16 -0800244exit 0;
245
246####################### primary functions ######################
247sub usage {
248 my $exit = shift || 0;
249 my $fd = $exit ? \*STDERR : \*STDOUT;
250 print $fd <<"";
251git-svn - bidirectional operations between a single Subversion tree and git
252Usage: $0 <command> [options] [arguments]\n
Eric Wong448c81b2006-03-03 01:20:09 -0800253
254 print $fd "Available commands:\n" unless $cmd;
Eric Wong3397f9d2006-02-16 01:24:16 -0800255
256 foreach (sort keys %cmd) {
Eric Wong448c81b2006-03-03 01:20:09 -0800257 next if $cmd && $cmd ne $_;
Eric Wonga836a0e2007-02-14 19:34:56 -0800258 next if /^multi-/; # don't show deprecated commands
Eric Wongb203b762006-10-11 14:53:36 -0700259 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
Benoit Sigoureaa807bc2007-11-03 19:53:34 +0100260 foreach (sort keys %{$cmd{$_}->[2]}) {
Eric Wong512b6202007-04-03 01:57:08 -0700261 # mixed-case options are for .git/config only
262 next if /[A-Z]/ && /^[a-z]+$/i;
Eric Wong448c81b2006-03-03 01:20:09 -0800263 # prints out arguments as they should be passed:
Eric Wongb8c92ca2006-05-24 01:40:37 -0700264 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
Eric Wongb203b762006-10-11 14:53:36 -0700265 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
Eric Wong448c81b2006-03-03 01:20:09 -0800266 "--$_" : "-$_" }
267 split /\|/,$_)," $x\n";
268 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800269 }
270 print $fd <<"";
Eric Wong448c81b2006-03-03 01:20:09 -0800271\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
272arbitrary identifier if you're tracking multiple SVN branches/repositories in
273one git repository and want to keep them separate. See git-svn(1) for more
274information.
Eric Wong3397f9d2006-02-16 01:24:16 -0800275
276 exit $exit;
277}
278
Eric Wong551ce282006-02-20 10:57:29 -0800279sub version {
Eric Wong7d60ab22006-12-28 01:16:20 -0800280 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
Eric Wong551ce282006-02-20 10:57:29 -0800281 exit 0;
282}
283
Eric Wong8164b652007-01-11 15:35:55 -0800284sub do_git_init_db {
285 unless (-d $ENV{GIT_DIR}) {
286 my @init_db = ('init');
287 push @init_db, "--template=$_template" if defined $_template;
Eric Wongdadc6d22007-02-14 12:27:41 -0800288 if (defined $_shared) {
289 if ($_shared =~ /[a-z]/) {
290 push @init_db, "--shared=$_shared";
291 } else {
292 push @init_db, "--shared";
293 }
294 }
Eric Wong8164b652007-01-11 15:35:55 -0800295 command_noisy(@init_db);
296 }
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800297 my $set;
298 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
299 foreach my $i (keys %icv) {
300 die "'$set' and '$i' cannot both be set\n" if $set;
301 next unless defined $icv{$i};
302 command_noisy('config', "$pfx.$i", $icv{$i});
303 $set = $i;
304 }
Eric Wong8164b652007-01-11 15:35:55 -0800305}
306
Eric Wongdadc6d22007-02-14 12:27:41 -0800307sub init_subdir {
308 my $repo_path = shift or return;
309 mkpath([$repo_path]) unless -d $repo_path;
310 chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
Eric Wongf30603f2007-02-23 01:26:26 -0800311 $ENV{GIT_DIR} = '.git';
Eric Wongdadc6d22007-02-14 12:27:41 -0800312}
313
Eric Wong0425ea92007-02-16 18:45:01 -0800314sub cmd_clone {
315 my ($url, $path) = @_;
316 if (!defined $path &&
martin f. krafft8f728fb2007-07-14 11:25:28 +0200317 (defined $_trunk || defined $_branches || defined $_tags ||
318 defined $_stdlayout) &&
Eric Wong0425ea92007-02-16 18:45:01 -0800319 $url !~ m#^[a-z\+]+://#) {
320 $path = $url;
321 }
Eric Wong0425ea92007-02-16 18:45:01 -0800322 $path = basename($url) if !defined $path || !length $path;
Eric Wongf30603f2007-02-23 01:26:26 -0800323 cmd_init($url, $path);
Eric Wong0425ea92007-02-16 18:45:01 -0800324 Git::SVN::fetch_all($Git::SVN::default_repo_id);
325}
326
Eric Wongd2866f92007-01-11 12:26:16 -0800327sub cmd_init {
martin f. krafft8f728fb2007-07-14 11:25:28 +0200328 if (defined $_stdlayout) {
329 $_trunk = 'trunk' if (!defined $_trunk);
330 $_tags = 'tags' if (!defined $_tags);
331 $_branches = 'branches' if (!defined $_branches);
332 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800333 if (defined $_trunk || defined $_branches || defined $_tags) {
334 return cmd_multi_init(@_);
Eric Wong03e0ea82006-06-30 21:42:53 -0700335 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800336 my $url = shift or die "SVN repository location required ",
337 "as a command-line argument\n";
338 init_subdir(@_);
Eric Wong8164b652007-01-11 15:35:55 -0800339 do_git_init_db();
Eric Wong03e0ea82006-06-30 21:42:53 -0700340
Eric Wong706587f2007-01-18 17:50:01 -0800341 Git::SVN->init($url);
Eric Wong3397f9d2006-02-16 01:24:16 -0800342}
343
Eric Wong2a3240b2007-01-04 18:09:56 -0800344sub cmd_fetch {
Eric Wonge98671e2007-02-14 02:21:19 -0800345 if (grep /^\d+=./, @_) {
346 die "'<rev>=<commit>' fetch arguments are ",
347 "no longer supported.\n";
Eric Wong07a1c952007-01-22 15:47:41 -0800348 }
Eric Wonge98671e2007-02-14 02:21:19 -0800349 my ($remote) = @_;
350 if (@_ > 1) {
Eric Wong905f8b72007-02-16 03:22:40 -0800351 die "Usage: $0 fetch [--all] [svn-remote]\n";
Eric Wonge98671e2007-02-14 02:21:19 -0800352 }
353 $remote ||= $Git::SVN::default_repo_id;
354 if ($_fetch_all) {
355 cmd_multi_fetch();
356 } else {
357 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
Eric Wong1c8443b2007-01-14 02:17:00 -0800358 }
Eric Wong2a3240b2007-01-04 18:09:56 -0800359}
360
Eric Wong1ce255d2007-01-14 23:21:16 -0800361sub cmd_set_tree {
Eric Wong3397f9d2006-02-16 01:24:16 -0800362 my (@commits) = @_;
363 if ($_stdin || !@commits) {
364 print "Reading from stdin...\n";
365 @commits = ();
366 while (<STDIN>) {
Eric Wong1ca72ae2006-03-03 01:20:09 -0800367 if (/\b($sha1_short)\b/o) {
Eric Wong3397f9d2006-02-16 01:24:16 -0800368 unshift @commits, $1;
369 }
370 }
371 }
372 my @revs;
Eric Wong8de010a2006-02-20 10:57:26 -0800373 foreach my $c (@commits) {
Eric Wongaef4e922006-12-15 10:59:54 -0800374 my @tmp = command('rev-parse',$c);
Eric Wong8de010a2006-02-20 10:57:26 -0800375 if (scalar @tmp == 1) {
376 push @revs, $tmp[0];
377 } elsif (scalar @tmp > 1) {
Eric Wongaef4e922006-12-15 10:59:54 -0800378 push @revs, reverse(command('rev-list',@tmp));
Eric Wong8de010a2006-02-20 10:57:26 -0800379 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200380 fatal "Failed to rev-parse $c";
Eric Wong8de010a2006-02-20 10:57:26 -0800381 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800382 }
Eric Wong1ce255d2007-01-14 23:21:16 -0800383 my $gs = Git::SVN->new;
384 my ($r_last, $cmt_last) = $gs->last_rev_commit;
385 $gs->fetch;
Eric Wong97f69872007-01-25 11:53:13 -0800386 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
Eric Wong1ce255d2007-01-14 23:21:16 -0800387 fatal "There are new revisions that were fetched ",
388 "and need to be merged (or acknowledged) ",
389 "before committing.\nlast rev: $r_last\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200390 " current: $gs->{last_rev}";
Eric Wong1ce255d2007-01-14 23:21:16 -0800391 }
392 $gs->set_tree($_) foreach @revs;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700393 print "Done committing ",scalar @revs," revisions to SVN\n";
394}
395
Eric Wongd7ad3be2007-01-14 03:14:28 -0800396sub cmd_dcommit {
397 my $head = shift;
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100398 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
David Reiss826a9332007-11-13 13:47:26 -0800399 'Cannot dcommit with a dirty index. Commit your changes first, '
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100400 . "or stash them with `git stash'.\n";
Eric Wongd7ad3be2007-01-14 03:14:28 -0800401 $head ||= 'HEAD';
Eric Wonga8ae2622007-02-13 14:22:11 -0800402 my @refs;
Eric Wong13c823f2007-04-08 00:59:19 -0700403 my ($url, $rev, $uuid, $gs) = working_head_info($head, \@refs);
Eric Wong15d54752007-08-22 22:14:31 -0700404 print "Committing to $url ...\n";
Eric Wong13c823f2007-04-08 00:59:19 -0700405 unless ($gs) {
Eric Wonga8ae2622007-02-13 14:22:11 -0800406 die "Unable to determine upstream SVN information from ",
Eric Wong905f8b72007-02-16 03:22:40 -0800407 "$head history\n";
Eric Wonga8ae2622007-02-13 14:22:11 -0800408 }
Eric Wong45bf4732006-11-09 01:19:37 -0800409 my $last_rev;
Eric Wong733a65a2007-06-13 02:23:28 -0700410 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
Eric Wong751eb392007-08-31 18:16:12 -0700411 if ($_no_rebase && scalar(@$linear_refs) > 1) {
412 warn "Attempting to commit more than one change while ",
413 "--no-rebase is enabled.\n",
414 "If these changes depend on each other, re-running ",
415 "without --no-rebase will be required."
416 }
Eric Wongc74d9ac2007-11-05 03:21:47 -0800417 while (1) {
418 my $d = shift @$linear_refs or last;
Eric Wong45bf4732006-11-09 01:19:37 -0800419 unless (defined $last_rev) {
420 (undef, $last_rev, undef) = cmt_metadata("$d~1");
421 unless (defined $last_rev) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800422 fatal "Unable to extract revision information ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200423 "from commit $d~1";
Eric Wong45bf4732006-11-09 01:19:37 -0800424 }
425 }
Eric Wongb22d4492006-08-26 00:01:23 -0700426 if ($_dry_run) {
427 print "diff-tree $d~1 $d\n";
428 } else {
Eric Wong751eb392007-08-31 18:16:12 -0700429 my $cmt_rev;
Eric Wongd7ad3be2007-01-14 03:14:28 -0800430 my %ed_opts = ( r => $last_rev,
Eric Wong61395352007-01-27 14:33:08 -0800431 log => get_commit_entry($d)->{log},
Eric Wong645833b2007-05-12 14:36:20 -0700432 ra => Git::SVN::Ra->new($gs->full_url),
Konstantin V. Arkhipov3caf3202007-11-14 03:52:02 +0300433 config => SVN::Core::config_get_config(
434 $Git::SVN::Ra::config_dir
435 ),
Eric Wong61395352007-01-27 14:33:08 -0800436 tree_a => "$d~1",
437 tree_b => $d,
438 editor_cb => sub {
439 print "Committed r$_[0]\n";
Eric Wong751eb392007-08-31 18:16:12 -0700440 $cmt_rev = $_[0];
441 },
Eric Wonga8ae2622007-02-13 14:22:11 -0800442 svn_path => '');
Eric Wong61395352007-01-27 14:33:08 -0800443 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800444 print "No changes\n$d~1 == $d\n";
Eric Wong733a65a2007-06-13 02:23:28 -0700445 } elsif ($parents->{$d} && @{$parents->{$d}}) {
Eric Wong751eb392007-08-31 18:16:12 -0700446 $gs->{inject_parents_dcommit}->{$cmt_rev} =
Eric Wong733a65a2007-06-13 02:23:28 -0700447 $parents->{$d};
Eric Wongd7ad3be2007-01-14 03:14:28 -0800448 }
Eric Wong751eb392007-08-31 18:16:12 -0700449 $_fetch_all ? $gs->fetch_all : $gs->fetch;
450 next if $_no_rebase;
451
452 # we always want to rebase against the current HEAD,
453 # not any head that was passed to us
Eric Wongc74d9ac2007-11-05 03:21:47 -0800454 my @diff = command('diff-tree', $d,
Eric Wong751eb392007-08-31 18:16:12 -0700455 $gs->refname, '--');
456 my @finish;
457 if (@diff) {
458 @finish = rebase_cmd();
Eric Wongc74d9ac2007-11-05 03:21:47 -0800459 print STDERR "W: $d and ", $gs->refname,
Eric Wong751eb392007-08-31 18:16:12 -0700460 " differ, using @finish:\n",
Eric Wongc74d9ac2007-11-05 03:21:47 -0800461 join("\n", @diff), "\n";
Eric Wong751eb392007-08-31 18:16:12 -0700462 } else {
463 print "No changes between current HEAD and ",
464 $gs->refname,
465 "\nResetting to the latest ",
466 $gs->refname, "\n";
467 @finish = qw/reset --mixed/;
468 }
469 command_noisy(@finish, $gs->refname);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800470 if (@diff) {
471 @refs = ();
472 my ($url_, $rev_, $uuid_, $gs_) =
473 working_head_info($head, \@refs);
474 my ($linear_refs_, $parents_) =
475 linearize_history($gs_, \@refs);
476 if (scalar(@$linear_refs) !=
477 scalar(@$linear_refs_)) {
478 fatal "# of revisions changed ",
479 "\nbefore:\n",
480 join("\n", @$linear_refs),
481 "\n\nafter:\n",
482 join("\n", @$linear_refs_), "\n",
483 'If you are attempting to commit ',
484 "merges, try running:\n\t",
485 'git rebase --interactive',
486 '--preserve-merges ',
487 $gs->refname,
488 "\nBefore dcommitting";
489 }
490 if ($url_ ne $url) {
491 fatal "URL mismatch after rebase: ",
492 "$url_ != $url";
493 }
494 if ($uuid_ ne $uuid) {
495 fatal "uuid mismatch after rebase: ",
496 "$uuid_ != $uuid";
497 }
498 # remap parents
499 my (%p, @l, $i);
500 for ($i = 0; $i < scalar @$linear_refs; $i++) {
501 my $new = $linear_refs_->[$i] or next;
502 $p{$new} =
503 $parents->{$linear_refs->[$i]};
504 push @l, $new;
505 }
506 $parents = \%p;
507 $linear_refs = \@l;
508 }
Eric Wong751eb392007-08-31 18:16:12 -0700509 $last_rev = $cmt_rev;
Eric Wongb22d4492006-08-26 00:01:23 -0700510 }
511 }
Eric Wongb22d4492006-08-26 00:01:23 -0700512}
513
Adam Roben26e60162007-04-27 11:57:53 -0700514sub cmd_find_rev {
515 my $revision_or_hash = shift;
516 my $result;
517 if ($revision_or_hash =~ /^r\d+$/) {
Adam Robenb3cb7e42007-04-29 01:35:27 -0700518 my $head = shift;
519 $head ||= 'HEAD';
520 my @refs;
521 my (undef, undef, undef, $gs) = working_head_info($head, \@refs);
522 unless ($gs) {
523 die "Unable to determine upstream SVN information from ",
524 "$head history\n";
Adam Roben26e60162007-04-27 11:57:53 -0700525 }
Adam Robenb3cb7e42007-04-29 01:35:27 -0700526 my $desired_revision = substr($revision_or_hash, 1);
527 $result = $gs->rev_db_get($desired_revision);
Adam Roben26e60162007-04-27 11:57:53 -0700528 } else {
529 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
530 $result = $rev;
531 }
532 print "$result\n" if $result;
533}
534
Eric Wong905f8b72007-02-16 03:22:40 -0800535sub cmd_rebase {
536 command_noisy(qw/update-index --refresh/);
Eric Wong13c823f2007-04-08 00:59:19 -0700537 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
538 unless ($gs) {
Eric Wong905f8b72007-02-16 03:22:40 -0800539 die "Unable to determine upstream SVN information from ",
540 "working tree history\n";
541 }
Eric Wong905f8b72007-02-16 03:22:40 -0800542 if (command(qw/diff-index HEAD --/)) {
543 print STDERR "Cannot rebase with uncommited changes:\n";
544 command_noisy('status');
545 exit 1;
546 }
Eric Wongdee41f32007-03-13 11:40:36 -0700547 unless ($_local) {
548 $_fetch_all ? $gs->fetch_all : $gs->fetch;
549 }
Eric Wong905f8b72007-02-16 03:22:40 -0800550 command_noisy(rebase_cmd(), $gs->refname);
551}
552
Eric Wong5969cbe2007-01-11 17:58:39 -0800553sub cmd_show_ignore {
Eric Wong13c823f2007-04-08 00:59:19 -0700554 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
555 $gs ||= Git::SVN->new;
Eric Wong5969cbe2007-01-11 17:58:39 -0800556 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200557 $gs->prop_walk($gs->{path}, $r, sub {
558 my ($gs, $path, $props) = @_;
559 print STDOUT "\n# $path\n";
560 my $s = $props->{'svn:ignore'} or return;
561 $s =~ s/[\r\n]+/\n/g;
562 chomp $s;
563 $s =~ s#^#$path#gm;
564 print STDOUT "$s\n";
565 });
Eric Wonga5e0ced2006-06-12 15:23:48 -0700566}
567
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200568sub cmd_create_ignore {
569 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
570 $gs ||= Git::SVN->new;
571 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
572 $gs->prop_walk($gs->{path}, $r, sub {
573 my ($gs, $path, $props) = @_;
574 # $path is of the form /path/to/dir/
575 my $ignore = '.' . $path . '.gitignore';
576 my $s = $props->{'svn:ignore'} or return;
577 open(GITIGNORE, '>', $ignore)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200578 or fatal("Failed to open `$ignore' for writing: $!");
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200579 $s =~ s/[\r\n]+/\n/g;
580 chomp $s;
581 # Prefix all patterns so that the ignore doesn't apply
582 # to sub-directories.
583 $s =~ s#^#/#gm;
584 print GITIGNORE "$s\n";
585 close(GITIGNORE)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200586 or fatal("Failed to close `$ignore': $!");
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200587 command_noisy('add', $ignore);
588 });
589}
590
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800591sub canonicalize_path {
592 my ($path) = @_;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800593 my $dot_slash_added = 0;
594 if (substr($path, 0, 1) ne "/") {
595 $path = "./" . $path;
596 $dot_slash_added = 1;
597 }
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800598 # File::Spec->canonpath doesn't collapse x/../y into y (for a
599 # good reason), so let's do this manually.
600 $path =~ s#/+#/#g;
601 $path =~ s#/\.(?:/|$)#/#g;
602 $path =~ s#/[^/]+/\.\.##g;
603 $path =~ s#/$##g;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800604 $path =~ s#^\./## if $dot_slash_added;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800605 return $path;
606}
607
Benoit Sigoure15153452007-10-16 16:36:50 +0200608# get_svnprops(PATH)
609# ------------------
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200610# Helper for cmd_propget and cmd_proplist below.
Benoit Sigoure15153452007-10-16 16:36:50 +0200611sub get_svnprops {
612 my $path = shift;
613 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
614 $gs ||= Git::SVN->new;
615
616 # prefix THE PATH by the sub-directory from which the user
617 # invoked us.
618 $path = $cmd_dir_prefix . $path;
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200619 fatal("No such file or directory: $path") unless -e $path;
Benoit Sigoure15153452007-10-16 16:36:50 +0200620 my $is_dir = -d $path ? 1 : 0;
621 $path = $gs->{path} . '/' . $path;
622
623 # canonicalize the path (otherwise libsvn will abort or fail to
624 # find the file)
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800625 $path = canonicalize_path($path);
Benoit Sigoure15153452007-10-16 16:36:50 +0200626
627 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
628 my $props;
629 if ($is_dir) {
630 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
631 }
632 else {
633 (undef, $props) = $gs->ra->get_file($path, $r, undef);
634 }
635 return $props;
636}
637
638# cmd_propget (PROP, PATH)
639# ------------------------
640# Print the SVN property PROP for PATH.
641sub cmd_propget {
642 my ($prop, $path) = @_;
643 $path = '.' if not defined $path;
644 usage(1) if not defined $prop;
645 my $props = get_svnprops($path);
646 if (not defined $props->{$prop}) {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200647 fatal("`$path' does not have a `$prop' SVN property.");
Benoit Sigoure15153452007-10-16 16:36:50 +0200648 }
649 print $props->{$prop} . "\n";
650}
651
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200652# cmd_proplist (PATH)
653# -------------------
654# Print the list of SVN properties for PATH.
655sub cmd_proplist {
656 my $path = shift;
657 $path = '.' if not defined $path;
658 my $props = get_svnprops($path);
659 print "Properties on '$path':\n";
660 foreach (sort keys %{$props}) {
661 print " $_\n";
662 }
663}
664
Eric Wong8164b652007-01-11 15:35:55 -0800665sub cmd_multi_init {
Eric Wong9d55b412006-06-12 15:53:13 -0700666 my $url = shift;
Eric Wong98327e52007-01-04 18:02:00 -0800667 unless (defined $_trunk || defined $_branches || defined $_tags) {
668 usage(1);
669 }
Eric Wongdc431662007-05-19 03:59:02 -0700670
671 # there are currently some bugs that prevent multi-init/multi-fetch
672 # setups from working well without this.
673 $Git::SVN::_minimize_url = 1;
674
Eric Wong8164b652007-01-11 15:35:55 -0800675 $_prefix = '' unless defined $_prefix;
Eric Wongdadc6d22007-02-14 12:27:41 -0800676 if (defined $url) {
677 $url =~ s#/+$##;
678 init_subdir(@_);
679 }
Eric Wongf30603f2007-02-23 01:26:26 -0800680 do_git_init_db();
Eric Wong98327e52007-01-04 18:02:00 -0800681 if (defined $_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800682 my $trunk_ref = $_prefix . 'trunk';
683 # try both old-style and new-style lookups:
684 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
Eric Wong8164b652007-01-11 15:35:55 -0800685 unless ($gs_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800686 my ($trunk_url, $trunk_path) =
687 complete_svn_url($url, $_trunk);
688 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
689 undef, $trunk_ref);
Eric Wong98327e52007-01-04 18:02:00 -0800690 }
Eric Wongc35b96e2006-10-11 11:53:21 -0700691 }
Eric Wong706587f2007-01-18 17:50:01 -0800692 return unless defined $_branches || defined $_tags;
Eric Wonge7db67e2007-01-11 17:09:26 -0800693 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
694 complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix);
695 complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
Eric Wong9d55b412006-06-12 15:53:13 -0700696}
697
Eric Wong1c8443b2007-01-14 02:17:00 -0800698sub cmd_multi_fetch {
Eric Wong0af9c9f2007-01-27 22:28:56 -0800699 my $remotes = Git::SVN::read_all_remotes();
700 foreach my $repo_id (sort keys %$remotes) {
Eric Wongdb03cd22007-02-13 00:38:02 -0800701 if ($remotes->{$repo_id}->{url}) {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800702 Git::SVN::fetch_all($repo_id, $remotes);
703 }
Eric Wong706587f2007-01-18 17:50:01 -0800704 }
Eric Wong9d55b412006-06-12 15:53:13 -0700705}
706
Eric Wong44320b92007-01-13 22:35:53 -0800707# this command is special because it requires no metadata
708sub cmd_commit_diff {
709 my ($ta, $tb, $url) = @_;
710 my $usage = "Usage: $0 commit-diff -r<revision> ".
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200711 "<tree-ish> <tree-ish> [<URL>]";
Eric Wong44320b92007-01-13 22:35:53 -0800712 fatal($usage) if (!defined $ta || !defined $tb);
Eric Wongd3a840d2007-01-26 01:32:45 -0800713 my $svn_path;
Eric Wong44320b92007-01-13 22:35:53 -0800714 if (!defined $url) {
715 my $gs = eval { Git::SVN->new };
716 if (!$gs) {
717 fatal("Needed URL or usable git-svn --id in ",
718 "the command-line\n", $usage);
719 }
720 $url = $gs->{url};
Eric Wongd3a840d2007-01-26 01:32:45 -0800721 $svn_path = $gs->{path};
Eric Wong44320b92007-01-13 22:35:53 -0800722 }
723 unless (defined $_revision) {
724 fatal("-r|--revision is a required argument\n", $usage);
725 }
726 if (defined $_message && defined $_file) {
727 fatal("Both --message/-m and --file/-F specified ",
728 "for the commit message.\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200729 "I have no idea what you mean");
Eric Wong44320b92007-01-13 22:35:53 -0800730 }
731 if (defined $_file) {
732 $_message = file_to_s($_file);
733 } else {
734 $_message ||= get_commit_entry($tb)->{log};
735 }
736 my $ra ||= Git::SVN::Ra->new($url);
Eric Wongd3a840d2007-01-26 01:32:45 -0800737 $svn_path ||= $ra->{svn_path};
Eric Wong44320b92007-01-13 22:35:53 -0800738 my $r = $_revision;
739 if ($r eq 'HEAD') {
740 $r = $ra->get_latest_revnum;
741 } elsif ($r !~ /^\d+$/) {
742 die "revision argument: $r not understood by git-svn\n";
743 }
Eric Wong61395352007-01-27 14:33:08 -0800744 my %ed_opts = ( r => $r,
745 log => $_message,
746 ra => $ra,
747 tree_a => $ta,
748 tree_b => $tb,
749 editor_cb => sub { print "Committed r$_[0]\n" },
750 svn_path => $svn_path );
751 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong44320b92007-01-13 22:35:53 -0800752 print "No changes\n$ta == $tb\n";
753 }
Eric Wong44320b92007-01-13 22:35:53 -0800754}
755
David D. Kilzere6fefa92007-11-21 11:57:18 -0800756sub cmd_info {
757 my $path = canonicalize_path(shift or ".");
758 unless (scalar(@_) == 0) {
759 die "Too many arguments specified\n";
760 }
761
762 my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
763
764 if (!$file_type && !$diff_status) {
765 print STDERR "$path: (Not a versioned resource)\n\n";
766 return;
767 }
768
769 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
770 unless ($gs) {
771 die "Unable to determine upstream SVN information from ",
772 "working tree history\n";
773 }
774 my $full_url = $url . ($path eq "." ? "" : "/$path");
775
David D. Kilzer8b014d72007-11-21 11:57:19 -0800776 if ($_url) {
777 print $full_url, "\n";
778 return;
779 }
780
David D. Kilzere6fefa92007-11-21 11:57:18 -0800781 my $result = "Path: $path\n";
782 $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
783 $result .= "URL: " . $full_url . "\n";
784
Eric Wonga5460eb2007-11-21 18:20:57 -0800785 eval {
786 my $repos_root = $gs->repos_root;
787 Git::SVN::remove_username($repos_root);
788 $result .= "Repository Root: $repos_root\n";
789 };
790 if ($@) {
791 $result .= "Repository Root: (offline)\n";
792 }
David D. Kilzere6fefa92007-11-21 11:57:18 -0800793 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A";
794 $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
795
796 $result .= "Node Kind: " .
797 ($file_type eq "dir" ? "directory" : "file") . "\n";
798
799 my $schedule = $diff_status eq "A"
800 ? "add"
801 : ($diff_status eq "D" ? "delete" : "normal");
802 $result .= "Schedule: $schedule\n";
803
804 if ($diff_status eq "A") {
805 print $result, "\n";
806 return;
807 }
808
809 my ($lc_author, $lc_rev, $lc_date_utc);
810 my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $path);
811 my $log = command_output_pipe(@args);
812 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
813 while (<$log>) {
814 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
815 $lc_author = $1;
816 $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
817 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
818 (undef, $lc_rev, undef) = ::extract_metadata($1);
819 }
820 }
821 close $log;
822
823 Git::SVN::Log::set_local_timezone();
824
825 $result .= "Last Changed Author: $lc_author\n";
826 $result .= "Last Changed Rev: $lc_rev\n";
827 $result .= "Last Changed Date: " .
828 Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
829
830 if ($file_type ne "dir") {
831 my $text_last_updated_date =
832 ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
833 $result .=
834 "Text Last Updated: " .
835 Git::SVN::Log::format_svn_date($text_last_updated_date) .
836 "\n";
837 my $checksum;
838 if ($diff_status eq "D") {
839 my ($fh, $ctx) =
840 command_output_pipe(qw(cat-file blob), "HEAD:$path");
841 if ($file_type eq "link") {
842 my $file_name = <$fh>;
843 $checksum = Git::SVN::Util::md5sum("link $file_name");
844 } else {
845 $checksum = Git::SVN::Util::md5sum($fh);
846 }
847 command_close_pipe($fh, $ctx);
848 } elsif ($file_type eq "link") {
849 my $file_name =
850 command(qw(cat-file blob), "HEAD:$path");
851 $checksum =
852 Git::SVN::Util::md5sum("link " . $file_name);
853 } else {
854 open FILE, "<", $path or die $!;
855 $checksum = Git::SVN::Util::md5sum(\*FILE);
856 close FILE or die $!;
857 }
858 $result .= "Checksum: " . $checksum . "\n";
859 }
860
861 print $result, "\n";
862}
863
Eric Wong3397f9d2006-02-16 01:24:16 -0800864########################### utility functions #########################
865
Eric Wong905f8b72007-02-16 03:22:40 -0800866sub rebase_cmd {
867 my @cmd = qw/rebase/;
868 push @cmd, '-v' if $_verbose;
869 push @cmd, qw/--merge/ if $_merge;
870 push @cmd, "--strategy=$_strategy" if $_strategy;
871 @cmd;
872}
873
Eric Wong1e889ef2007-02-16 01:45:13 -0800874sub post_fetch_checkout {
875 return if $_no_checkout;
876 my $gs = $Git::SVN::_head or return;
877 return if verify_ref('refs/heads/master^0');
878
879 my $valid_head = verify_ref('HEAD^0');
880 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
881 return if ($valid_head || !verify_ref('HEAD^0'));
882
883 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
884 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
885 return if -f $index;
886
Matthias Lederhofer7ae3df82007-06-03 16:48:16 +0200887 return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
Eric Wong1e889ef2007-02-16 01:45:13 -0800888 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
889 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
890 print STDERR "Checked out HEAD:\n ",
891 $gs->full_url, " r", $gs->last_rev, "\n";
892}
893
Eric Wong98327e52007-01-04 18:02:00 -0800894sub complete_svn_url {
895 my ($url, $path) = @_;
896 $path =~ s#/+$##;
Eric Wong98327e52007-01-04 18:02:00 -0800897 if ($path !~ m#^[a-z\+]+://#) {
Eric Wong98327e52007-01-04 18:02:00 -0800898 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
899 fatal("E: '$path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200900 "and a separate URL is not specified");
Eric Wong98327e52007-01-04 18:02:00 -0800901 }
Eric Wong706587f2007-01-18 17:50:01 -0800902 return ($url, $path);
Eric Wong98327e52007-01-04 18:02:00 -0800903 }
Eric Wong706587f2007-01-18 17:50:01 -0800904 return ($path, '');
Eric Wong98327e52007-01-04 18:02:00 -0800905}
906
Eric Wong9d55b412006-06-12 15:53:13 -0700907sub complete_url_ls_init {
Eric Wong706587f2007-01-18 17:50:01 -0800908 my ($ra, $repo_path, $switch, $pfx) = @_;
909 unless ($repo_path) {
Eric Wong9d55b412006-06-12 15:53:13 -0700910 print STDERR "W: $switch not specified\n";
911 return;
912 }
Eric Wong706587f2007-01-18 17:50:01 -0800913 $repo_path =~ s#/+$##;
914 if ($repo_path =~ m#^[a-z\+]+://#) {
915 $ra = Git::SVN::Ra->new($repo_path);
916 $repo_path = '';
Eric Wonge7db67e2007-01-11 17:09:26 -0800917 } else {
Eric Wong706587f2007-01-18 17:50:01 -0800918 $repo_path =~ s#^/+##;
Eric Wonge7db67e2007-01-11 17:09:26 -0800919 unless ($ra) {
Eric Wong706587f2007-01-18 17:50:01 -0800920 fatal("E: '$repo_path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200921 "and a separate URL is not specified");
Eric Wong9d55b412006-06-12 15:53:13 -0700922 }
Eric Wonge7db67e2007-01-11 17:09:26 -0800923 }
Eric Wong706587f2007-01-18 17:50:01 -0800924 my $url = $ra->{url};
Eric Wongb4d57e52007-02-14 15:10:44 -0800925 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
926 my $k = "svn-remote.$gs->{repo_id}.url";
927 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
928 if ($orig_url && ($orig_url ne $gs->{url})) {
929 die "$k already set: $orig_url\n",
930 "wanted to set to: $gs->{url}\n";
Eric Wong88cf4102007-02-01 03:59:07 -0800931 }
Eric Wongb4d57e52007-02-14 15:10:44 -0800932 command_oneline('config', $k, $gs->{url}) unless $orig_url;
933 my $remote_path = "$ra->{svn_path}/$repo_path/*";
934 $remote_path =~ s#/+#/#g;
935 $remote_path =~ s#^/##g;
936 my ($n) = ($switch =~ /^--(\w+)/);
937 if (length $pfx && $pfx !~ m#/$#) {
938 die "--prefix='$pfx' must have a trailing slash '/'\n";
Eric Wong9d55b412006-06-12 15:53:13 -0700939 }
Eric Wongb4d57e52007-02-14 15:10:44 -0800940 command_noisy('config', "svn-remote.$gs->{repo_id}.$n",
941 "$remote_path:refs/remotes/$pfx*");
Eric Wong9d55b412006-06-12 15:53:13 -0700942}
943
Eric Wongaef4e922006-12-15 10:59:54 -0800944sub verify_ref {
945 my ($ref) = @_;
Eric Wong2c5c1d52006-12-28 01:16:21 -0800946 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
947 { STDERR => 0 }); };
Eric Wongaef4e922006-12-15 10:59:54 -0800948}
949
Eric Wonga5e0ced2006-06-12 15:23:48 -0700950sub get_tree_from_treeish {
Eric Wongcf52b8f2006-02-20 10:57:28 -0800951 my ($treeish) = @_;
Eric Wong44320b92007-01-13 22:35:53 -0800952 # $treeish can be a symbolic ref, too:
Eric Wongaef4e922006-12-15 10:59:54 -0800953 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -0800954 my $expected;
955 while ($type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -0800956 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -0800957 }
958 if ($type eq 'commit') {
Eric Wongaef4e922006-12-15 10:59:54 -0800959 $expected = (grep /^tree /, command(qw/cat-file commit/,
960 $treeish))[0];
Eric Wong44320b92007-01-13 22:35:53 -0800961 ($expected) = ($expected =~ /^tree ($sha1)$/o);
Eric Wongcf52b8f2006-02-20 10:57:28 -0800962 die "Unable to get tree from $treeish\n" unless $expected;
963 } elsif ($type eq 'tree') {
964 $expected = $treeish;
965 } else {
966 die "$treeish is a $type, expected tree, tag or commit\n";
967 }
Eric Wonga5e0ced2006-06-12 15:23:48 -0700968 return $expected;
969}
Eric Wongcf52b8f2006-02-20 10:57:28 -0800970
Eric Wong44320b92007-01-13 22:35:53 -0800971sub get_commit_entry {
972 my ($treeish) = shift;
973 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
974 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
975 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
976 open my $log_fh, '>', $commit_editmsg or croak $!;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700977
Eric Wong44320b92007-01-13 22:35:53 -0800978 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wong4ad45152006-07-09 20:20:48 -0700979 if ($type eq 'commit' || $type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -0800980 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
Eric Wong44320b92007-01-13 22:35:53 -0800981 $type, $treeish);
Eric Wong3397f9d2006-02-16 01:24:16 -0800982 my $in_msg = 0;
983 while (<$msg_fh>) {
984 if (!$in_msg) {
985 $in_msg = 1 if (/^\s*$/);
Eric Wongdf746c52006-03-03 01:20:08 -0800986 } elsif (/^git-svn-id: /) {
Eric Wong44320b92007-01-13 22:35:53 -0800987 # skip this for now, we regenerate the
988 # correct one on re-fetch anyways
989 # TODO: set *:merge properties or like...
Eric Wong3397f9d2006-02-16 01:24:16 -0800990 } else {
Eric Wong44320b92007-01-13 22:35:53 -0800991 print $log_fh $_ or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -0800992 }
993 }
Eric Wongaef4e922006-12-15 10:59:54 -0800994 command_close_pipe($msg_fh, $ctx);
Eric Wong3397f9d2006-02-16 01:24:16 -0800995 }
Eric Wong44320b92007-01-13 22:35:53 -0800996 close $log_fh or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -0800997
998 if ($_edit || ($type eq 'tree')) {
999 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
Eric Wong44320b92007-01-13 22:35:53 -08001000 # TODO: strip out spaces, comments, like git-commit.sh
1001 system($editor, $commit_editmsg);
Eric Wong3397f9d2006-02-16 01:24:16 -08001002 }
Eric Wong44320b92007-01-13 22:35:53 -08001003 rename $commit_editmsg, $commit_msg or croak $!;
1004 open $log_fh, '<', $commit_msg or croak $!;
1005 { local $/; chomp($log_entry{log} = <$log_fh>); }
1006 close $log_fh or croak $!;
1007 unlink $commit_msg;
1008 \%log_entry;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001009}
1010
Eric Wong3397f9d2006-02-16 01:24:16 -08001011sub s_to_file {
1012 my ($str, $file, $mode) = @_;
1013 open my $fd,'>',$file or croak $!;
1014 print $fd $str,"\n" or croak $!;
1015 close $fd or croak $!;
1016 chmod ($mode &~ umask, $file) if (defined $mode);
1017}
1018
1019sub file_to_s {
1020 my $file = shift;
1021 open my $fd,'<',$file or croak "$!: file: $file\n";
1022 local $/;
1023 my $ret = <$fd>;
1024 close $fd or croak $!;
1025 $ret =~ s/\s*$//s;
1026 return $ret;
1027}
1028
Eric Wongeeb0abe2006-03-03 01:20:08 -08001029# '<svn username> = real-name <email address>' mapping based on git-svnimport:
1030sub load_authors {
1031 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001032 my $log = $cmd eq 'log';
Eric Wongeeb0abe2006-03-03 01:20:08 -08001033 while (<$authors>) {
1034 chomp;
Richard MUSIL575d0252007-07-17 19:02:57 +02001035 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
Eric Wongeeb0abe2006-03-03 01:20:08 -08001036 my ($user, $name, $email) = ($1, $2, $3);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001037 if ($log) {
1038 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
1039 } else {
1040 $users{$user} = [$name, $email];
1041 }
Eric Wong79bb8d82006-06-01 02:35:44 -07001042 }
1043 close $authors or croak $!;
1044}
1045
Tom Princee0d10e12007-01-28 16:16:53 -08001046# convert GetOpt::Long specs for use by git-config
Eric Wongb8c92ca2006-05-24 01:40:37 -07001047sub read_repo_config {
Eric Wong706587f2007-01-18 17:50:01 -08001048 return unless -d $ENV{GIT_DIR};
Eric Wongb8c92ca2006-05-24 01:40:37 -07001049 my $opts = shift;
Eric Wong97ae0912007-02-11 15:21:24 -08001050 my @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001051 foreach my $o (keys %$opts) {
Eric Wong97ae0912007-02-11 15:21:24 -08001052 # if we have mixedCase and a long option-only, then
1053 # it's a config-only variable that we don't need for
1054 # the command-line.
1055 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001056 my $v = $opts->{$o};
Eric Wong97ae0912007-02-11 15:21:24 -08001057 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001058 $key =~ s/-//g;
Tom Princee0d10e12007-01-28 16:16:53 -08001059 my $arg = 'git-config';
Eric Wongb8c92ca2006-05-24 01:40:37 -07001060 $arg .= ' --int' if ($o =~ /[:=]i$/);
1061 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1062 if (ref $v eq 'ARRAY') {
1063 chomp(my @tmp = `$arg --get-all svn.$key`);
1064 @$v = @tmp if @tmp;
1065 } else {
1066 chomp(my $tmp = `$arg --get svn.$key`);
Eric Wong77742842007-02-10 21:07:12 -08001067 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
Eric Wongb8c92ca2006-05-24 01:40:37 -07001068 $$v = $tmp;
1069 }
1070 }
1071 }
Eric Wong97ae0912007-02-11 15:21:24 -08001072 delete @$opts{@config_only} if @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001073}
1074
Eric Wong79bb8d82006-06-01 02:35:44 -07001075sub extract_metadata {
Eric Wongc1927a82006-06-27 19:39:11 -07001076 my $id = shift or return (undef, undef, undef);
Sam Vilain3dfab992007-06-30 20:56:13 +12001077 my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
Eric Wong79bb8d82006-06-01 02:35:44 -07001078 \s([a-f\d\-]+)$/x);
Eric Wonge70dc782006-11-23 14:54:04 -08001079 if (!defined $rev || !$uuid || !$url) {
Eric Wong79bb8d82006-06-01 02:35:44 -07001080 # some of the original repositories I made had
Pavel Roskin82e5a822006-07-10 01:50:18 -04001081 # identifiers like this:
Sam Vilain3dfab992007-06-30 20:56:13 +12001082 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
Eric Wong79bb8d82006-06-01 02:35:44 -07001083 }
1084 return ($url, $rev, $uuid);
1085}
1086
Eric Wongc1927a82006-06-27 19:39:11 -07001087sub cmt_metadata {
1088 return extract_metadata((grep(/^git-svn-id: /,
Eric Wongaef4e922006-12-15 10:59:54 -08001089 command(qw/cat-file commit/, shift)))[-1]);
Eric Wongc1927a82006-06-27 19:39:11 -07001090}
1091
Eric Wong905f8b72007-02-16 03:22:40 -08001092sub working_head_info {
1093 my ($head, $refs) = @_;
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +02001094 my @args = ('log', '--no-color', '--first-parent');
Lars Hjemli05b4df32007-09-05 11:35:29 +02001095 my ($fh, $ctx) = command_output_pipe(@args, $head);
Sam Vilain3dfab992007-06-30 20:56:13 +12001096 my $hash;
Sam Vilain40cb8f82007-06-30 20:56:14 +12001097 my %max;
Sam Vilain3dfab992007-06-30 20:56:13 +12001098 while (<$fh>) {
1099 if ( m{^commit ($::sha1)$} ) {
1100 unshift @$refs, $hash if $hash and $refs;
1101 $hash = $1;
1102 next;
1103 }
1104 next unless s{^\s*(git-svn-id:)}{$1};
1105 my ($url, $rev, $uuid) = extract_metadata($_);
Eric Wong13c823f2007-04-08 00:59:19 -07001106 if (defined $url && defined $rev) {
Sam Vilain40cb8f82007-06-30 20:56:14 +12001107 next if $max{$url} and $max{$url} < $rev;
Eric Wong13c823f2007-04-08 00:59:19 -07001108 if (my $gs = Git::SVN->find_by_url($url)) {
1109 my $c = $gs->rev_db_get($rev);
Adam Robenb03c7a62007-04-25 11:50:32 -07001110 if ($c && $c eq $hash) {
Eric Wong13c823f2007-04-08 00:59:19 -07001111 close $fh; # break the pipe
1112 return ($url, $rev, $uuid, $gs);
Sam Vilain40cb8f82007-06-30 20:56:14 +12001113 } else {
1114 $max{$url} ||= $gs->rev_db_max;
Eric Wong13c823f2007-04-08 00:59:19 -07001115 }
1116 }
1117 }
Eric Wong905f8b72007-02-16 03:22:40 -08001118 }
Eric Wong13c823f2007-04-08 00:59:19 -07001119 command_close_pipe($fh, $ctx);
1120 (undef, undef, undef, undef);
Eric Wong905f8b72007-02-16 03:22:40 -08001121}
1122
Eric Wong733a65a2007-06-13 02:23:28 -07001123sub read_commit_parents {
1124 my ($parents, $c) = @_;
Eric Wong7b02b852007-09-08 16:33:08 -07001125 chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
1126 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1127 @{$parents->{$c}} = split(/ /, $p);
Eric Wong733a65a2007-06-13 02:23:28 -07001128}
1129
1130sub linearize_history {
1131 my ($gs, $refs) = @_;
1132 my %parents;
1133 foreach my $c (@$refs) {
1134 read_commit_parents(\%parents, $c);
1135 }
1136
1137 my @linear_refs;
1138 my %skip = ();
1139 my $last_svn_commit = $gs->last_commit;
1140 foreach my $c (reverse @$refs) {
1141 next if $c eq $last_svn_commit;
1142 last if $skip{$c};
1143
1144 unshift @linear_refs, $c;
1145 $skip{$c} = 1;
1146
1147 # we only want the first parent to diff against for linear
1148 # history, we save the rest to inject when we finalize the
1149 # svn commit
1150 my $fp_a = verify_ref("$c~1");
1151 my $fp_b = shift @{$parents{$c}} if $parents{$c};
1152 if (!$fp_a || !$fp_b) {
1153 die "Commit $c\n",
1154 "has no parent commit, and therefore ",
1155 "nothing to diff against.\n",
1156 "You should be working from a repository ",
1157 "originally created by git-svn\n";
1158 }
1159 if ($fp_a ne $fp_b) {
1160 die "$c~1 = $fp_a, however parsing commit $c ",
1161 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1162 }
1163
1164 foreach my $p (@{$parents{$c}}) {
1165 $skip{$p} = 1;
1166 }
1167 }
1168 (\@linear_refs, \%parents);
1169}
1170
David D. Kilzere6fefa92007-11-21 11:57:18 -08001171sub find_file_type_and_diff_status {
1172 my ($path) = @_;
1173 return ('dir', '') if $path eq '.';
1174
1175 my $diff_output =
1176 command_oneline(qw(diff --cached --name-status --), $path) || "";
1177 my $diff_status = (split(' ', $diff_output))[0] || "";
1178
1179 my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || "";
1180
1181 return (undef, undef) if !$diff_status && !$ls_tree;
1182
1183 if ($diff_status eq "A") {
1184 return ("link", $diff_status) if -l $path;
1185 return ("dir", $diff_status) if -d $path;
1186 return ("file", $diff_status);
1187 }
1188
1189 my $mode = (split(' ', $ls_tree))[0] || "";
1190
1191 return ("link", $diff_status) if $mode eq "120000";
1192 return ("dir", $diff_status) if $mode eq "040000";
1193 return ("file", $diff_status);
1194}
1195
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001196package Git::SVN::Util;
1197use strict;
1198use warnings;
1199use Digest::MD5;
1200
1201sub md5sum {
1202 my $arg = shift;
1203 my $ref = ref $arg;
1204 my $md5 = Digest::MD5->new();
1205 if ($ref eq 'GLOB' || $ref eq 'IO::File') {
1206 $md5->addfile($arg) or croak $!;
1207 } elsif ($ref eq 'SCALAR') {
1208 $md5->add($$arg) or croak $!;
1209 } elsif (!$ref) {
1210 $md5->add($arg) or croak $!;
1211 } else {
1212 ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1213 }
1214 return $md5->hexdigest();
1215}
1216
Eric Wong9b981fc2007-01-11 12:14:21 -08001217package Git::SVN;
1218use strict;
1219use warnings;
Eric Wongecc712d2007-01-31 12:28:10 -08001220use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
Eric Wong62e349d2007-02-16 19:57:29 -08001221 $_repack $_repack_flags $_use_svm_props $_head
Eric Wong4a1bb4c2007-05-13 09:58:14 -07001222 $_use_svnsync_props $no_reuse_existing $_minimize_url/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001223use Carp qw/croak/;
1224use File::Path qw/mkpath/;
Eric Wong373274f2007-01-31 13:54:23 -08001225use File::Copy qw/copy/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001226use IPC::Open3;
1227
Eric Wongecc712d2007-01-31 12:28:10 -08001228my $_repack_nr;
Eric Wong9b981fc2007-01-11 12:14:21 -08001229# properties that we do not log:
1230my %SKIP_PROP;
1231BEGIN {
1232 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
1233 svn:special svn:executable
1234 svn:entry:committed-rev
1235 svn:entry:last-author
1236 svn:entry:uuid
1237 svn:entry:committed-date/;
Eric Wong91b03282007-02-11 00:51:33 -08001238
1239 # some options are read globally, but can be overridden locally
1240 # per [svn-remote "..."] section. Command-line options will *NOT*
1241 # override options set in an [svn-remote "..."] section
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001242 no strict 'refs';
1243 for my $option (qw/follow_parent no_metadata use_svm_props
1244 use_svnsync_props/) {
1245 my $key = $option;
Eric Wong91b03282007-02-11 00:51:33 -08001246 $key =~ tr/_//d;
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001247 my $prop = "-$option";
1248 *$option = sub {
1249 my ($self) = @_;
1250 return $self->{$prop} if exists $self->{$prop};
1251 my $k = "svn-remote.$self->{repo_id}.$key";
1252 eval { command_oneline(qw/config --get/, $k) };
1253 if ($@) {
1254 $self->{$prop} = ${"Git::SVN::_$option"};
Eric Wong91b03282007-02-11 00:51:33 -08001255 } else {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001256 my $v = command_oneline(qw/config --bool/,$k);
1257 $self->{$prop} = $v eq 'false' ? 0 : 1;
Eric Wong91b03282007-02-11 00:51:33 -08001258 }
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001259 return $self->{$prop};
1260 }
Eric Wong91b03282007-02-11 00:51:33 -08001261 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001262}
1263
Eric Wong373274f2007-01-31 13:54:23 -08001264my %LOCKFILES;
1265END { unlink keys %LOCKFILES if %LOCKFILES }
1266
Eric Wong4bb9ed02007-02-03 13:29:17 -08001267sub resolve_local_globs {
1268 my ($url, $fetch, $glob_spec) = @_;
1269 return unless defined $glob_spec;
1270 my $ref = $glob_spec->{ref};
1271 my $path = $glob_spec->{path};
1272 foreach (command(qw#for-each-ref --format=%(refname) refs/remotes#)) {
1273 next unless m#^refs/remotes/$ref->{regex}$#;
1274 my $p = $1;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001275 my $pathname = desanitize_refname($path->full_path($p));
1276 my $refname = desanitize_refname($ref->full_path($p));
Eric Wong4bb9ed02007-02-03 13:29:17 -08001277 if (my $existing = $fetch->{$pathname}) {
1278 if ($existing ne $refname) {
1279 die "Refspec conflict:\n",
1280 "existing: refs/remotes/$existing\n",
1281 " globbed: refs/remotes/$refname\n";
1282 }
1283 my $u = (::cmt_metadata("refs/remotes/$refname"))[0];
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001284 $u =~ s!^\Q$url\E(/|$)!! or die
Eric Wong4bb9ed02007-02-03 13:29:17 -08001285 "refs/remotes/$refname: '$url' not found in '$u'\n";
1286 if ($pathname ne $u) {
1287 warn "W: Refspec glob conflict ",
1288 "(ref: refs/remotes/$refname):\n",
1289 "expected path: $pathname\n",
1290 " real path: $u\n",
1291 "Continuing ahead with $u\n";
1292 next;
1293 }
1294 } else {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001295 $fetch->{$pathname} = $refname;
1296 }
1297 }
1298}
1299
Eric Wonge98671e2007-02-14 02:21:19 -08001300sub parse_revision_argument {
1301 my ($base, $head) = @_;
1302 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
1303 return ($base, $head);
1304 }
1305 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
1306 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
1307 return ($head, $head) if ($::_revision eq 'HEAD');
1308 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
1309 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
1310 die "revision argument: $::_revision not understood by git-svn\n";
1311}
1312
Eric Wong0af9c9f2007-01-27 22:28:56 -08001313sub fetch_all {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001314 my ($repo_id, $remotes) = @_;
Eric Wong905f8b72007-02-16 03:22:40 -08001315 if (ref $repo_id) {
1316 my $gs = $repo_id;
1317 $repo_id = undef;
1318 $repo_id = $gs->{repo_id};
1319 }
1320 $remotes ||= read_all_remotes();
Eric Wong7447b4b2007-02-14 18:38:46 -08001321 my $remote = $remotes->{$repo_id} or
1322 die "[svn-remote \"$repo_id\"] unknown\n";
Eric Wonge5181922007-02-08 12:53:57 -08001323 my $fetch = $remote->{fetch};
Eric Wong7447b4b2007-02-14 18:38:46 -08001324 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
Eric Wonge5181922007-02-08 12:53:57 -08001325 my (@gs, @globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001326 my $ra = Git::SVN::Ra->new($url);
Eric Wong26a62d52007-02-12 13:25:25 -08001327 my $uuid = $ra->get_uuid;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001328 my $head = $ra->get_latest_revnum;
Eric Wong28710f72007-02-14 13:32:21 -08001329 my $base = defined $fetch ? $head : 0;
Eric Wonge5181922007-02-08 12:53:57 -08001330
1331 # read the max revs for wildcard expansion (branches/*, tags/*)
1332 foreach my $t (qw/branches tags/) {
1333 defined $remote->{$t} or next;
1334 push @globs, $remote->{$t};
Eric Wong93f26892007-02-11 01:20:26 -08001335 my $max_rev = eval { tmp_config(qw/--int --get/,
1336 "svn-remote.$repo_id.${t}-maxRev") };
1337 if (defined $max_rev && ($max_rev < $base)) {
1338 $base = $max_rev;
Eric Wongd6d33462007-02-16 04:05:33 -08001339 } elsif (!defined $max_rev) {
1340 $base = 0;
Eric Wonge5181922007-02-08 12:53:57 -08001341 }
1342 }
1343
Eric Wongdb03cd22007-02-13 00:38:02 -08001344 if ($fetch) {
1345 foreach my $p (sort keys %$fetch) {
1346 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
1347 my $lr = $gs->rev_db_max;
1348 if (defined $lr) {
1349 $base = $lr if ($lr < $base);
1350 }
1351 push @gs, $gs;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001352 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08001353 }
Eric Wonge98671e2007-02-14 02:21:19 -08001354
1355 ($base, $head) = parse_revision_argument($base, $head);
Eric Wonge5181922007-02-08 12:53:57 -08001356 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001357}
1358
Eric Wong47e39c52007-01-21 04:27:09 -08001359sub read_all_remotes {
1360 my $r = {};
Eric Wong8b8fc062007-01-22 11:44:57 -08001361 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
Eric Wong47e39c52007-01-21 04:27:09 -08001362 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) {
Eric Wong46cf98b2007-07-14 12:40:32 -07001363 my ($remote, $local_ref, $remote_ref) = ($1, $2, $3);
1364 $local_ref =~ s{^/}{};
1365 $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
Eric Wong47e39c52007-01-21 04:27:09 -08001366 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1367 $r->{$1}->{url} = $2;
Eric Wong4bb9ed02007-02-03 13:29:17 -08001368 } elsif (m!^(.+)\.(branches|tags)=
1369 (.*):refs/remotes/(.+)\s*$/!x) {
1370 my ($p, $g) = ($3, $4);
1371 my $rs = $r->{$1}->{$2} = {
Eric Wonge5181922007-02-08 12:53:57 -08001372 t => $2,
Eric Wong93f26892007-02-11 01:20:26 -08001373 remote => $1,
Eric Wong4bb9ed02007-02-03 13:29:17 -08001374 path => Git::SVN::GlobSpec->new($p),
1375 ref => Git::SVN::GlobSpec->new($g) };
1376 if (length($rs->{ref}->{right}) != 0) {
1377 die "The '*' glob character must be the last ",
1378 "character of '$g'\n";
1379 }
Eric Wong47e39c52007-01-21 04:27:09 -08001380 }
1381 }
1382 $r;
1383}
1384
Eric Wongecc712d2007-01-31 12:28:10 -08001385sub init_vars {
1386 if (defined $_repack) {
1387 $_repack = 1000 if ($_repack <= 0);
1388 $_repack_nr = $_repack;
1389 $_repack_flags ||= '-d';
1390 }
1391}
1392
Eric Wongb805b442007-01-22 13:52:04 -08001393sub verify_remotes_sanity {
Eric Wong536c4b02007-01-23 11:35:53 -08001394 return unless -d $ENV{GIT_DIR};
Eric Wongb805b442007-01-22 13:52:04 -08001395 my %seen;
1396 foreach (command(qw/config -l/)) {
1397 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1398 if ($seen{$1}) {
1399 die "Remote ref refs/remote/$1 is tracked by",
1400 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1401 "Please resolve this ambiguity in ",
1402 "your git configuration file before ",
1403 "continuing\n";
1404 }
1405 $seen{$1} = $_;
1406 }
1407 }
1408}
1409
Eric Wong47e39c52007-01-21 04:27:09 -08001410# we allow more chars than remotes2config.sh...
Eric Wong706587f2007-01-18 17:50:01 -08001411sub sanitize_remote_name {
1412 my ($name) = @_;
Eric Wong47e39c52007-01-21 04:27:09 -08001413 $name =~ tr{A-Za-z0-9:,/+-}{.}c;
Eric Wong706587f2007-01-18 17:50:01 -08001414 $name;
1415}
1416
Eric Wonge6434f82007-01-23 16:29:23 -08001417sub find_existing_remote {
1418 my ($url, $remotes) = @_;
Eric Wongbefc9ad2007-02-17 02:53:07 -08001419 return undef if $no_reuse_existing;
Eric Wonge6434f82007-01-23 16:29:23 -08001420 my $existing;
1421 foreach my $repo_id (keys %$remotes) {
1422 my $u = $remotes->{$repo_id}->{url} or next;
1423 next if $u ne $url;
1424 $existing = $repo_id;
1425 last;
1426 }
1427 $existing;
1428}
1429
1430sub init_remote_config {
Eric Wongd8115c52007-02-01 03:30:31 -08001431 my ($self, $url, $no_write) = @_;
Eric Wonge6434f82007-01-23 16:29:23 -08001432 $url =~ s!/+$!!; # strip trailing slash
1433 my $r = read_all_remotes();
1434 my $existing = find_existing_remote($url, $r);
1435 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001436 unless ($no_write) {
1437 print STDERR "Using existing ",
1438 "[svn-remote \"$existing\"]\n";
1439 }
Eric Wonge6434f82007-01-23 16:29:23 -08001440 $self->{repo_id} = $existing;
Eric Wong4a1bb4c2007-05-13 09:58:14 -07001441 } elsif ($_minimize_url) {
Eric Wonge6434f82007-01-23 16:29:23 -08001442 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
1443 $existing = find_existing_remote($min_url, $r);
1444 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001445 unless ($no_write) {
1446 print STDERR "Using existing ",
1447 "[svn-remote \"$existing\"]\n";
1448 }
Eric Wonge6434f82007-01-23 16:29:23 -08001449 $self->{repo_id} = $existing;
1450 }
1451 if ($min_url ne $url) {
Eric Wonge5181922007-02-08 12:53:57 -08001452 unless ($no_write) {
1453 print STDERR "Using higher level of URL: ",
1454 "$url => $min_url\n";
1455 }
Eric Wonge6434f82007-01-23 16:29:23 -08001456 my $old_path = $self->{path};
1457 $self->{path} = $url;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001458 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
Eric Wonge6434f82007-01-23 16:29:23 -08001459 if (length $old_path) {
1460 $self->{path} .= "/$old_path";
1461 }
1462 $url = $min_url;
1463 }
1464 }
1465 my $orig_url;
1466 if (!$existing) {
1467 # verify that we aren't overwriting anything:
1468 $orig_url = eval {
1469 command_oneline('config', '--get',
1470 "svn-remote.$self->{repo_id}.url")
1471 };
1472 if ($orig_url && ($orig_url ne $url)) {
1473 die "svn-remote.$self->{repo_id}.url already set: ",
1474 "$orig_url\nwanted to set to: $url\n";
1475 }
1476 }
1477 my ($xrepo_id, $xpath) = find_ref($self->refname);
1478 if (defined $xpath) {
1479 die "svn-remote.$xrepo_id.fetch already set to track ",
1480 "$xpath:refs/remotes/", $self->refname, "\n";
1481 }
Eric Wongd8115c52007-02-01 03:30:31 -08001482 unless ($no_write) {
1483 command_noisy('config',
1484 "svn-remote.$self->{repo_id}.url", $url);
Eric Wong46cf98b2007-07-14 12:40:32 -07001485 $self->{path} =~ s{^/}{};
Eric Wongd8115c52007-02-01 03:30:31 -08001486 command_noisy('config', '--add',
1487 "svn-remote.$self->{repo_id}.fetch",
1488 "$self->{path}:".$self->refname);
1489 }
Eric Wonge6434f82007-01-23 16:29:23 -08001490 $self->{url} = $url;
1491}
1492
Eric Wonga8ae2622007-02-13 14:22:11 -08001493sub find_by_url { # repos_root and, path are optional
1494 my ($class, $full_url, $repos_root, $path) = @_;
Adam Roben56973d22007-04-25 12:42:58 -07001495
Eric Wong1a97a502007-02-20 00:43:19 -08001496 return undef unless defined $full_url;
Adam Roben56973d22007-04-25 12:42:58 -07001497 remove_username($full_url);
1498 remove_username($repos_root) if defined $repos_root;
Eric Wonga8ae2622007-02-13 14:22:11 -08001499 my $remotes = read_all_remotes();
1500 if (defined $full_url && defined $repos_root && !defined $path) {
1501 $path = $full_url;
1502 $path =~ s#^\Q$repos_root\E(?:/|$)##;
1503 }
1504 foreach my $repo_id (keys %$remotes) {
1505 my $u = $remotes->{$repo_id}->{url} or next;
Adam Roben56973d22007-04-25 12:42:58 -07001506 remove_username($u);
Eric Wonga8ae2622007-02-13 14:22:11 -08001507 next if defined $repos_root && $repos_root ne $u;
1508
1509 my $fetch = $remotes->{$repo_id}->{fetch} || {};
1510 foreach (qw/branches tags/) {
1511 resolve_local_globs($u, $fetch,
1512 $remotes->{$repo_id}->{$_});
1513 }
1514 my $p = $path;
1515 unless (defined $p) {
1516 $p = $full_url;
1517 $p =~ s#^\Q$u\E(?:/|$)## or next;
1518 }
1519 foreach my $f (keys %$fetch) {
1520 next if $f ne $p;
1521 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
1522 }
1523 }
1524 undef;
1525}
1526
Eric Wong9b981fc2007-01-11 12:14:21 -08001527sub init {
Eric Wongd8115c52007-02-01 03:30:31 -08001528 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08001529 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong9b981fc2007-01-11 12:14:21 -08001530 if (defined $url) {
Eric Wongd8115c52007-02-01 03:30:31 -08001531 $self->init_remote_config($url, $no_write);
Eric Wong9b981fc2007-01-11 12:14:21 -08001532 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001533 $self;
1534}
1535
Eric Wong706587f2007-01-18 17:50:01 -08001536sub find_ref {
1537 my ($ref_id) = @_;
1538 foreach (command(qw/config -l/)) {
1539 next unless m!^svn-remote\.(.+)\.fetch=
1540 \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x;
1541 my ($repo_id, $path, $ref) = ($1, $2, $3);
1542 if ($ref eq $ref_id) {
1543 $path = '' if ($path =~ m#^\./?#);
1544 return ($repo_id, $path);
1545 }
1546 }
1547 (undef, undef, undef);
1548}
1549
Eric Wong9b981fc2007-01-11 12:14:21 -08001550sub new {
Eric Wong706587f2007-01-18 17:50:01 -08001551 my ($class, $ref_id, $repo_id, $path) = @_;
1552 if (defined $ref_id && !defined $repo_id && !defined $path) {
1553 ($repo_id, $path) = find_ref($ref_id);
1554 if (!defined $repo_id) {
1555 die "Could not find a \"svn-remote.*.fetch\" key ",
1556 "in the repository configuration matching: ",
1557 "refs/remotes/$ref_id\n";
1558 }
1559 }
1560 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong8b8fc062007-01-22 11:44:57 -08001561 if (!defined $self->{path} || !length $self->{path}) {
1562 my $fetch = command_oneline('config', '--get',
1563 "svn-remote.$repo_id.fetch",
1564 ":refs/remotes/$ref_id\$") or
1565 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1566 "\":refs/remotes/$ref_id\$\" in config\n";
1567 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
1568 }
Eric Wong706587f2007-01-18 17:50:01 -08001569 $self->{url} = command_oneline('config', '--get',
1570 "svn-remote.$repo_id.url") or
1571 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
Eric Wongd6d33462007-02-16 04:05:33 -08001572 $self->rebuild;
Eric Wong9b981fc2007-01-11 12:14:21 -08001573 $self;
1574}
1575
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001576sub refname {
1577 my ($refname) = "refs/remotes/$_[0]->{ref_id}" ;
1578
1579 # It cannot end with a slash /, we'll throw up on this because
1580 # SVN can't have directories with a slash in their name, either:
1581 if ($refname =~ m{/$}) {
1582 die "ref: '$refname' ends with a trailing slash, this is ",
1583 "not permitted by git nor Subversion\n";
1584 }
1585
1586 # It cannot have ASCII control character space, tilde ~, caret ^,
1587 # colon :, question-mark ?, asterisk *, space, or open bracket [
1588 # anywhere.
1589 #
1590 # Additionally, % must be escaped because it is used for escaping
1591 # and we want our escaped refname to be reversible
1592 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
1593
1594 # no slash-separated component can begin with a dot .
1595 # /.* becomes /%2E*
1596 $refname =~ s{/\.}{/%2E}g;
1597
1598 # It cannot have two consecutive dots .. anywhere
1599 # .. becomes %2E%2E
1600 $refname =~ s{\.\.}{%2E%2E}g;
1601
1602 return $refname;
1603}
1604
1605sub desanitize_refname {
1606 my ($refname) = @_;
1607 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
1608 return $refname;
1609}
Eric Wong9b981fc2007-01-11 12:14:21 -08001610
Eric Wong26a62d52007-02-12 13:25:25 -08001611sub svm_uuid {
1612 my ($self) = @_;
1613 return $self->{svm}->{uuid} if $self->svm;
1614 $self->ra;
1615 unless ($self->{svm}) {
1616 die "SVM UUID not cached, and reading remotely failed\n";
1617 }
1618 $self->{svm}->{uuid};
1619}
Eric Wong8a49ee92007-02-10 20:46:50 -08001620
Eric Wong26a62d52007-02-12 13:25:25 -08001621sub svm {
1622 my ($self) = @_;
1623 return $self->{svm} if $self->{svm};
1624 my $svm;
Eric Wong8a49ee92007-02-10 20:46:50 -08001625 # see if we have it in our config, first:
1626 eval {
Eric Wong26a62d52007-02-12 13:25:25 -08001627 my $section = "svn-remote.$self->{repo_id}";
1628 $svm = {
Eric Wong93f26892007-02-11 01:20:26 -08001629 source => tmp_config('--get', "$section.svm-source"),
1630 uuid => tmp_config('--get', "$section.svm-uuid"),
Eric Wongbefc9ad2007-02-17 02:53:07 -08001631 replace => tmp_config('--get', "$section.svm-replace"),
Eric Wong8a49ee92007-02-10 20:46:50 -08001632 }
1633 };
Eric Wongbefc9ad2007-02-17 02:53:07 -08001634 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
1635 $self->{svm} = $svm;
1636 }
Eric Wong26a62d52007-02-12 13:25:25 -08001637 $self->{svm};
1638}
1639
1640sub _set_svm_vars {
1641 my ($self, $ra) = @_;
Eric Wongdb03cd22007-02-13 00:38:02 -08001642 return $ra if $self->svm;
Eric Wong26a62d52007-02-12 13:25:25 -08001643
Eric Wongdb03cd22007-02-13 00:38:02 -08001644 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08001645 "(svm:source, svm:uuid) ",
Eric Wongdb03cd22007-02-13 00:38:02 -08001646 "from the following URLs:\n" );
1647 sub read_svm_props {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001648 my ($self, $ra, $path, $r) = @_;
1649 my $props = ($ra->get_dir($path, $r))[2];
Eric Wongdb03cd22007-02-13 00:38:02 -08001650 my $src = $props->{'svm:source'};
Eric Wong8a49ee92007-02-10 20:46:50 -08001651 my $uuid = $props->{'svm:uuid'};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001652 return undef if (!$src || !$uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08001653
Eric Wongbefc9ad2007-02-17 02:53:07 -08001654 chomp($src, $uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08001655
Eric Wong8a49ee92007-02-10 20:46:50 -08001656 $uuid =~ m{^[0-9a-f\-]{30,}$}
1657 or die "doesn't look right - svm:uuid is '$uuid'\n";
Eric Wongbefc9ad2007-02-17 02:53:07 -08001658
1659 # the '!' is used to mark the repos_root!/relative/path
1660 $src =~ s{/?!/?}{/};
Eric Wongdb03cd22007-02-13 00:38:02 -08001661 $src =~ s{/+$}{}; # no trailing slashes please
Eric Wongbefc9ad2007-02-17 02:53:07 -08001662 # username is of no interest
Eric Wongdb03cd22007-02-13 00:38:02 -08001663 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
Eric Wong8a49ee92007-02-10 20:46:50 -08001664
Eric Wongbefc9ad2007-02-17 02:53:07 -08001665 my $replace = $ra->{url};
1666 $replace .= "/$path" if length $path;
1667
Eric Wongdb03cd22007-02-13 00:38:02 -08001668 my $section = "svn-remote.$self->{repo_id}";
Eric Wongbefc9ad2007-02-17 02:53:07 -08001669 tmp_config("$section.svm-source", $src);
1670 tmp_config("$section.svm-replace", $replace);
1671 tmp_config("$section.svm-uuid", $uuid);
1672 $self->{svm} = {
1673 source => $src,
1674 uuid => $uuid,
1675 replace => $replace
1676 };
Eric Wong8a49ee92007-02-10 20:46:50 -08001677 }
Eric Wongdb03cd22007-02-13 00:38:02 -08001678
1679 my $r = $ra->get_latest_revnum;
1680 my $path = $self->{path};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001681 my %tried;
Eric Wongdb03cd22007-02-13 00:38:02 -08001682 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001683 unless ($tried{"$self->{url}/$path"}) {
1684 return $ra if $self->read_svm_props($ra, $path, $r);
1685 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001686 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001687 $path =~ s#/?[^/]+$##;
Eric Wong8a49ee92007-02-10 20:46:50 -08001688 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001689 die "Path: '$path' should be ''\n" if $path ne '';
1690 return $ra if $self->read_svm_props($ra, $path, $r);
1691 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001692
1693 if ($ra->{repos_root} eq $self->{url}) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001694 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001695 }
1696
1697 # nope, make sure we're connected to the repository root:
1698 my $ok;
1699 my @tried_b;
1700 $path = $ra->{svn_path};
Eric Wongdb03cd22007-02-13 00:38:02 -08001701 $ra = Git::SVN::Ra->new($ra->{repos_root});
1702 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001703 unless ($tried{"$ra->{url}/$path"}) {
1704 $ok = $self->read_svm_props($ra, $path, $r);
1705 last if $ok;
1706 $tried{"$ra->{url}/$path"} = 1;
1707 }
1708 $path =~ s#/?[^/]+$##;
Eric Wongdb03cd22007-02-13 00:38:02 -08001709 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001710 die "Path: '$path' should be ''\n" if $path ne '';
1711 $ok ||= $self->read_svm_props($ra, $path, $r);
1712 $tried{"$ra->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001713 if (!$ok) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001714 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001715 }
1716 Git::SVN::Ra->new($self->{url});
Eric Wong8a49ee92007-02-10 20:46:50 -08001717}
1718
Eric Wong62e349d2007-02-16 19:57:29 -08001719sub svnsync {
1720 my ($self) = @_;
1721 return $self->{svnsync} if $self->{svnsync};
1722
1723 if ($self->no_metadata) {
1724 die "Can't have both 'noMetadata' and ",
1725 "'useSvnsyncProps' options set!\n";
1726 }
1727 if ($self->rewrite_root) {
1728 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1729 "options set!\n";
1730 }
1731
1732 my $svnsync;
1733 # see if we have it in our config, first:
1734 eval {
1735 my $section = "svn-remote.$self->{repo_id}";
1736 $svnsync = {
1737 url => tmp_config('--get', "$section.svnsync-url"),
1738 uuid => tmp_config('--get', "$section.svnsync-uuid"),
1739 }
1740 };
1741 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
1742 return $self->{svnsync} = $svnsync;
1743 }
1744
1745 my $err = "useSvnsyncProps set, but failed to read " .
1746 "svnsync property: svn:sync-from-";
1747 my $rp = $self->ra->rev_proplist(0);
1748
1749 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
1750 $url =~ m{^[a-z\+]+://} or
1751 die "doesn't look right - svn:sync-from-url is '$url'\n";
1752
1753 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
1754 $uuid =~ m{^[0-9a-f\-]{30,}$} or
1755 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1756
1757 my $section = "svn-remote.$self->{repo_id}";
1758 tmp_config('--add', "$section.svnsync-uuid", $uuid);
1759 tmp_config('--add', "$section.svnsync-url", $url);
1760 return $self->{svnsync} = { url => $url, uuid => $uuid };
1761}
1762
Eric Wong26a62d52007-02-12 13:25:25 -08001763# this allows us to memoize our SVN::Ra UUID locally and avoid a
1764# remote lookup (useful for 'git svn log').
1765sub ra_uuid {
1766 my ($self) = @_;
1767 unless ($self->{ra_uuid}) {
1768 my $key = "svn-remote.$self->{repo_id}.uuid";
1769 my $uuid = eval { tmp_config('--get', $key) };
1770 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
1771 $self->{ra_uuid} = $uuid;
1772 } else {
1773 die "ra_uuid called without URL\n" unless $self->{url};
1774 $self->{ra_uuid} = $self->ra->get_uuid;
1775 tmp_config('--add', $key, $self->{ra_uuid});
1776 }
1777 }
1778 $self->{ra_uuid};
1779}
1780
Eric Wonga5460eb2007-11-21 18:20:57 -08001781sub _set_repos_root {
1782 my ($self, $repos_root) = @_;
1783 my $k = "svn-remote.$self->{repo_id}.reposRoot";
1784 $repos_root ||= $self->ra->{repos_root};
1785 tmp_config($k, $repos_root);
1786 $repos_root;
1787}
1788
1789sub repos_root {
1790 my ($self) = @_;
1791 my $k = "svn-remote.$self->{repo_id}.reposRoot";
1792 eval { tmp_config('--get', $k) } || $self->_set_repos_root;
1793}
1794
Eric Wong9b981fc2007-01-11 12:14:21 -08001795sub ra {
1796 my ($self) = shift;
Eric Wong8a49ee92007-02-10 20:46:50 -08001797 my $ra = Git::SVN::Ra->new($self->{url});
Eric Wonga5460eb2007-11-21 18:20:57 -08001798 $self->_set_repos_root($ra->{repos_root});
Eric Wong91b03282007-02-11 00:51:33 -08001799 if ($self->use_svm_props && !$self->{svm}) {
1800 if ($self->no_metadata) {
Eric Wong97ae0912007-02-11 15:21:24 -08001801 die "Can't have both 'noMetadata' and ",
1802 "'useSvmProps' options set!\n";
Eric Wong62e349d2007-02-16 19:57:29 -08001803 } elsif ($self->use_svnsync_props) {
1804 die "Can't have both 'useSvnsyncProps' and ",
1805 "'useSvmProps' options set!\n";
Eric Wong91b03282007-02-11 00:51:33 -08001806 }
Eric Wong26a62d52007-02-12 13:25:25 -08001807 $ra = $self->_set_svm_vars($ra);
Eric Wong8a49ee92007-02-10 20:46:50 -08001808 $self->{-want_revprops} = 1;
1809 }
1810 $ra;
Eric Wong9b981fc2007-01-11 12:14:21 -08001811}
1812
Eric Wong15710b62007-01-22 02:20:33 -08001813sub rel_path {
1814 my ($self) = @_;
1815 my $repos_root = $self->ra->{repos_root};
1816 return $self->{path} if ($self->{url} eq $repos_root);
Eric Wong0b594512007-03-25 16:35:31 -07001817 my $url = $self->{url} .
1818 (length $self->{path} ? "/$self->{path}" : $self->{path});
1819 $url =~ s!^\Q$repos_root\E(?:/+|$)!!g;
1820 $url;
Eric Wong15710b62007-01-22 02:20:33 -08001821}
1822
Benoit Sigoure01bdab82007-10-16 16:36:48 +02001823# prop_walk(PATH, REV, SUB)
1824# -------------------------
1825# Recursively traverse PATH at revision REV and invoke SUB for each
1826# directory that contains a SVN property. SUB will be invoked as
1827# follows: &SUB(gs, path, props); where `gs' is this instance of
1828# Git::SVN, `path' the path to the directory where the properties
1829# `props' were found. The `path' will be relative to point of checkout,
1830# that is, if url://repo/trunk is the current Git branch, and that
1831# directory contains a sub-directory `d', SUB will be invoked with `/d/'
1832# as `path' (note the trailing `/').
1833sub prop_walk {
1834 my ($self, $path, $rev, $sub) = @_;
1835
1836 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
1837 $path =~ s#^/*#/#g;
Eric Wong9b981fc2007-01-11 12:14:21 -08001838 my $p = $path;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02001839 # Strip the irrelevant part of the path.
1840 $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
1841 # Ensure the path is terminated by a `/'.
1842 $p =~ s#/*$#/#;
1843
1844 # The properties contain all the internal SVN stuff nobody
1845 # (usually) cares about.
1846 my $interesting_props = 0;
1847 foreach (keys %{$props}) {
1848 # If it doesn't start with `svn:', it must be a
1849 # user-defined property.
1850 ++$interesting_props and next if $_ !~ /^svn:/;
1851 # FIXME: Fragile, if SVN adds new public properties,
1852 # this needs to be updated.
1853 ++$interesting_props if /^svn:(?:ignore|keywords|executable
1854 |eol-style|mime-type
1855 |externals|needs-lock)$/x;
Eric Wong9b981fc2007-01-11 12:14:21 -08001856 }
Benoit Sigoure01bdab82007-10-16 16:36:48 +02001857 &$sub($self, $p, $props) if $interesting_props;
1858
Eric Wong9b981fc2007-01-11 12:14:21 -08001859 foreach (sort keys %$dirent) {
Eric Wong0dc03d62007-05-13 01:04:43 -07001860 next if $dirent->{$_}->{kind} != $SVN::Node::dir;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02001861 $self->prop_walk($path . '/' . $_, $rev, $sub);
Eric Wong9b981fc2007-01-11 12:14:21 -08001862 }
1863}
1864
Eric Wong3ebe8df2007-01-25 17:35:40 -08001865sub last_rev { ($_[0]->last_rev_commit)[0] }
1866sub last_commit { ($_[0]->last_rev_commit)[1] }
1867
Eric Wong9b981fc2007-01-11 12:14:21 -08001868# returns the newest SVN revision number and newest commit SHA1
1869sub last_rev_commit {
1870 my ($self) = @_;
1871 if (defined $self->{last_rev} && defined $self->{last_commit}) {
1872 return ($self->{last_rev}, $self->{last_commit});
1873 }
Eric Wongd2866f92007-01-11 12:26:16 -08001874 my $c = ::verify_ref($self->refname.'^0');
Eric Wong91b03282007-02-11 00:51:33 -08001875 if ($c && !$self->use_svm_props && !$self->no_metadata) {
Eric Wongd2866f92007-01-11 12:26:16 -08001876 my $rev = (::cmt_metadata($c))[1];
Eric Wong9b981fc2007-01-11 12:14:21 -08001877 if (defined $rev) {
1878 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
1879 return ($rev, $c);
1880 }
1881 }
Eric Wong26a62d52007-02-12 13:25:25 -08001882 my $db_path = $self->db_path;
1883 unless (-e $db_path) {
1884 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
1885 return (undef, undef);
1886 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001887 my $offset = -41; # from tail
1888 my $rl;
Eric Wong26a62d52007-02-12 13:25:25 -08001889 open my $fh, '<', $db_path or croak "$db_path not readable: $!\n";
Eric Wongce4b4af2007-01-31 17:57:36 -08001890 sysseek($fh, $offset, 2); # don't care for errors
1891 sysread($fh, $rl, 41) == 41 or return (undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08001892 chomp $rl;
Eric Wongce4b4af2007-01-31 17:57:36 -08001893 while (('0' x40) eq $rl && sysseek($fh, 0, 1) != 0) {
Eric Wong9b981fc2007-01-11 12:14:21 -08001894 $offset -= 41;
Eric Wongce4b4af2007-01-31 17:57:36 -08001895 sysseek($fh, $offset, 2); # don't care for errors
1896 sysread($fh, $rl, 41) == 41 or return (undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08001897 chomp $rl;
1898 }
Eric Wong91b03282007-02-11 00:51:33 -08001899 if ($c && $c ne $rl) {
Eric Wong26a62d52007-02-12 13:25:25 -08001900 die "$db_path and ", $self->refname,
Eric Wong9c93fee2007-01-31 17:22:31 -08001901 " inconsistent!:\n$c != $rl\n";
1902 }
Eric Wongce4b4af2007-01-31 17:57:36 -08001903 my $rev = sysseek($fh, 0, 1) or croak $!;
Eric Wong9b981fc2007-01-11 12:14:21 -08001904 $rev = ($rev - 41) / 41;
1905 close $fh or croak $!;
1906 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
1907 return ($rev, $c);
1908}
1909
Eric Wong3ebe8df2007-01-25 17:35:40 -08001910sub get_fetch_range {
1911 my ($self, $min, $max) = @_;
1912 $max ||= $self->ra->get_latest_revnum;
Eric Wong9c93fee2007-01-31 17:22:31 -08001913 $min ||= $self->rev_db_max;
Eric Wong3ebe8df2007-01-25 17:35:40 -08001914 (++$min, $max);
Eric Wong9b981fc2007-01-11 12:14:21 -08001915}
1916
Eric Wong8a49ee92007-02-10 20:46:50 -08001917sub tmp_config {
Eric Wong93f26892007-02-11 01:20:26 -08001918 my (@args) = @_;
Eric Wongb7e53482007-02-16 04:09:28 -08001919 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
1920 my $config = "$ENV{GIT_DIR}/svn/.metadata";
Eric Wong38570a42007-06-13 02:37:05 -07001921 if (! -f $config && -f $old_def_config) {
Eric Wongb7e53482007-02-16 04:09:28 -08001922 rename $old_def_config, $config or
1923 die "Failed rename $old_def_config => $config: $!\n";
1924 }
Eric Wong8a49ee92007-02-10 20:46:50 -08001925 my $old_config = $ENV{GIT_CONFIG};
Eric Wong93f26892007-02-11 01:20:26 -08001926 $ENV{GIT_CONFIG} = $config;
Eric Wong8a49ee92007-02-10 20:46:50 -08001927 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08001928 my @ret = eval {
1929 unless (-f $config) {
1930 mkfile($config);
1931 open my $fh, '>', $config or
1932 die "Can't open $config: $!\n";
1933 print $fh "; This file is used internally by ",
1934 "git-svn\n" or die
1935 "Couldn't write to $config: $!\n";
1936 print $fh "; You should not have to edit it\n" or
1937 die "Couldn't write to $config: $!\n";
1938 close $fh or die "Couldn't close $config: $!\n";
1939 }
1940 command('config', @args);
1941 };
Eric Wong8a49ee92007-02-10 20:46:50 -08001942 my $err = $@;
1943 if (defined $old_config) {
1944 $ENV{GIT_CONFIG} = $old_config;
1945 } else {
1946 delete $ENV{GIT_CONFIG};
1947 }
1948 die $err if $err;
1949 wantarray ? @ret : $ret[0];
1950}
1951
Eric Wong9b981fc2007-01-11 12:14:21 -08001952sub tmp_index_do {
1953 my ($self, $sub) = @_;
1954 my $old_index = $ENV{GIT_INDEX_FILE};
1955 $ENV{GIT_INDEX_FILE} = $self->{index};
Eric Wong8a49ee92007-02-10 20:46:50 -08001956 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08001957 my @ret = eval {
1958 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
1959 mkpath([$dir]) unless -d $dir;
1960 &$sub;
1961 };
Eric Wong8a49ee92007-02-10 20:46:50 -08001962 my $err = $@;
1963 if (defined $old_index) {
Eric Wong9b981fc2007-01-11 12:14:21 -08001964 $ENV{GIT_INDEX_FILE} = $old_index;
1965 } else {
1966 delete $ENV{GIT_INDEX_FILE};
1967 }
Eric Wong8a49ee92007-02-10 20:46:50 -08001968 die $err if $err;
Eric Wong9b981fc2007-01-11 12:14:21 -08001969 wantarray ? @ret : $ret[0];
1970}
1971
1972sub assert_index_clean {
1973 my ($self, $treeish) = @_;
1974
1975 $self->tmp_index_do(sub {
1976 command_noisy('read-tree', $treeish) unless -e $self->{index};
1977 my $x = command_oneline('write-tree');
1978 my ($y) = (command(qw/cat-file commit/, $treeish) =~
1979 /^tree ($::sha1)/mo);
Eric Wonge8d120b2007-02-14 16:29:52 -08001980 return if $y eq $x;
1981
1982 warn "Index mismatch: $y != $x\nrereading $treeish\n";
1983 unlink $self->{index} or die "unlink $self->{index}: $!\n";
1984 command_noisy('read-tree', $treeish);
Eric Wong9b981fc2007-01-11 12:14:21 -08001985 $x = command_oneline('write-tree');
1986 if ($y ne $x) {
1987 ::fatal "trees ($treeish) $y != $x\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001988 "Something is seriously wrong...";
Eric Wong9b981fc2007-01-11 12:14:21 -08001989 }
1990 });
1991}
1992
1993sub get_commit_parents {
Eric Wong0af9c9f2007-01-27 22:28:56 -08001994 my ($self, $log_entry) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08001995 my (%seen, @ret, @tmp);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001996 # legacy support for 'set-tree'; this is only used by set_tree_cb:
1997 if (my $ip = $self->{inject_parents}) {
1998 if (my $commit = delete $ip->{$log_entry->{revision}}) {
1999 push @tmp, $commit;
Eric Wong9b981fc2007-01-11 12:14:21 -08002000 }
2001 }
Eric Wongd2866f92007-01-11 12:26:16 -08002002 if (my $cur = ::verify_ref($self->refname.'^0')) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002003 push @tmp, $cur;
2004 }
Eric Wong733a65a2007-06-13 02:23:28 -07002005 if (my $ipd = $self->{inject_parents_dcommit}) {
2006 if (my $commit = delete $ipd->{$log_entry->{revision}}) {
2007 push @tmp, @$commit;
2008 }
2009 }
Eric Wong44320b92007-01-13 22:35:53 -08002010 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
Eric Wong9b981fc2007-01-11 12:14:21 -08002011 while (my $p = shift @tmp) {
2012 next if $seen{$p};
2013 $seen{$p} = 1;
2014 push @ret, $p;
2015 # MAXPARENT is defined to 16 in commit-tree.c:
2016 last if @ret >= 16;
2017 }
2018 if (@tmp) {
Eric Wong44320b92007-01-13 22:35:53 -08002019 die "r$log_entry->{revision}: No room for parents:\n\t",
Eric Wong9b981fc2007-01-11 12:14:21 -08002020 join("\n\t", @tmp), "\n";
2021 }
2022 @ret;
2023}
2024
Eric Wongaea736c2007-02-16 19:15:21 -08002025sub rewrite_root {
2026 my ($self) = @_;
2027 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
2028 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2029 my $rwr = eval { command_oneline(qw/config --get/, $k) };
2030 if ($rwr) {
2031 $rwr =~ s#/+$##;
2032 if ($rwr !~ m#^[a-z\+]+://#) {
2033 die "$rwr is not a valid URL (key: $k)\n";
2034 }
2035 }
2036 $self->{-rewrite_root} = $rwr;
2037}
2038
2039sub metadata_url {
2040 my ($self) = @_;
2041 ($self->rewrite_root || $self->{url}) .
2042 (length $self->{path} ? '/' . $self->{path} : '');
2043}
2044
Eric Wong706587f2007-01-18 17:50:01 -08002045sub full_url {
Eric Wong9b981fc2007-01-11 12:14:21 -08002046 my ($self) = @_;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002047 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
Eric Wong9b981fc2007-01-11 12:14:21 -08002048}
2049
2050sub do_git_commit {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002051 my ($self, $log_entry) = @_;
Eric Wong8a603772007-01-31 02:45:50 -08002052 my $lr = $self->last_rev;
2053 if (defined $lr && $lr >= $log_entry->{revision}) {
2054 die "Last fetched revision of ", $self->refname,
2055 " was r$lr, but we are about to fetch: ",
2056 "r$log_entry->{revision}!\n";
2057 }
Eric Wong44320b92007-01-13 22:35:53 -08002058 if (my $c = $self->rev_db_get($log_entry->{revision})) {
2059 croak "$log_entry->{revision} = $c already exists! ",
Eric Wong9b981fc2007-01-11 12:14:21 -08002060 "Why are we refetching it?\n";
2061 }
Eric Wongdb03cd22007-02-13 00:38:02 -08002062 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $log_entry->{name};
2063 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} =
2064 $log_entry->{email};
Eric Wong44320b92007-01-13 22:35:53 -08002065 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
Eric Wong9b981fc2007-01-11 12:14:21 -08002066
Eric Wong44320b92007-01-13 22:35:53 -08002067 my $tree = $log_entry->{tree};
Eric Wong9b981fc2007-01-11 12:14:21 -08002068 if (!defined $tree) {
2069 $tree = $self->tmp_index_do(sub {
2070 command_oneline('write-tree') });
2071 }
2072 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2073
2074 my @exec = ('git-commit-tree', $tree);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002075 foreach ($self->get_commit_parents($log_entry)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002076 push @exec, '-p', $_;
2077 }
2078 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2079 or croak $!;
Eric Wong44320b92007-01-13 22:35:53 -08002080 print $msg_fh $log_entry->{log} or croak $!;
Eric Wong91b03282007-02-11 00:51:33 -08002081 unless ($self->no_metadata) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002082 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2083 or croak $!;
Eric Wong9760adc2007-01-31 03:06:56 -08002084 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002085 $msg_fh->flush == 0 or croak $!;
2086 close $msg_fh or croak $!;
2087 chomp(my $commit = do { local $/; <$out_fh> });
2088 close $out_fh or croak $!;
2089 waitpid $pid, 0;
2090 croak $? if $?;
2091 if ($commit !~ /^$::sha1$/o) {
2092 die "Failed to commit, invalid sha1: $commit\n";
2093 }
2094
Eric Wong373274f2007-01-31 13:54:23 -08002095 $self->rev_db_set($log_entry->{revision}, $commit, 1);
Eric Wong9b981fc2007-01-11 12:14:21 -08002096
Eric Wong44320b92007-01-13 22:35:53 -08002097 $self->{last_rev} = $log_entry->{revision};
Eric Wong9b981fc2007-01-11 12:14:21 -08002098 $self->{last_commit} = $commit;
Eric Wong8a49ee92007-02-10 20:46:50 -08002099 print "r$log_entry->{revision}";
2100 if (defined $log_entry->{svm_revision}) {
2101 print " (\@$log_entry->{svm_revision})";
Eric Wong26a62d52007-02-12 13:25:25 -08002102 $self->rev_db_set($log_entry->{svm_revision}, $commit,
2103 0, $self->svm_uuid);
Eric Wong8a49ee92007-02-10 20:46:50 -08002104 }
2105 print " = $commit ($self->{ref_id})\n";
Eric Wongecc712d2007-01-31 12:28:10 -08002106 if (defined $_repack && (--$_repack_nr == 0)) {
2107 $_repack_nr = $_repack;
2108 # repack doesn't use any arguments with spaces in them, does it?
2109 print "Running git repack $_repack_flags ...\n";
2110 command_noisy('repack', split(/\s+/, $_repack_flags));
2111 print "Done repacking\n";
2112 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002113 return $commit;
2114}
2115
Eric Wongfbcc1732007-02-06 18:35:30 -08002116sub match_paths {
2117 my ($self, $paths, $r) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002118 return 1 if $self->{path} eq '';
Eric Wongd542aed2007-02-09 02:19:41 -08002119 if (my $path = $paths->{"/$self->{path}"}) {
2120 return ($path->{action} eq 'D') ? 0 : 1;
2121 }
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002122 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
Eric Wongfbcc1732007-02-06 18:35:30 -08002123 if (grep /$self->{path_regex}/, keys %$paths) {
2124 return 1;
2125 }
2126 my $c = '';
2127 foreach (split m#/#, $self->{path}) {
2128 $c .= "/$_";
Eric Wong74a81222007-02-10 13:28:50 -08002129 next unless ($paths->{$c} &&
2130 ($paths->{$c}->{action} =~ /^[AR]$/));
Eric Wonge5181922007-02-08 12:53:57 -08002131 if ($self->ra->check_path($self->{path}, $r) ==
2132 $SVN::Node::dir) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002133 return 1;
2134 }
2135 }
2136 return 0;
2137}
2138
Eric Wong15710b62007-01-22 02:20:33 -08002139sub find_parent_branch {
2140 my ($self, $paths, $rev) = @_;
Eric Wong91b03282007-02-11 00:51:33 -08002141 return undef unless $self->follow_parent;
Eric Wonge5a0b242007-01-25 15:44:54 -08002142 unless (defined $paths) {
Eric Wongc7eba712007-01-31 03:45:28 -08002143 my $err_handler = $SVN::Error::handler;
2144 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
Eric Wongd4eff2b2007-01-30 14:04:22 -08002145 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, sub {
2146 $paths =
2147 Git::SVN::Ra::dup_changed_paths($_[0]) });
Eric Wongc7eba712007-01-31 03:45:28 -08002148 $SVN::Error::handler = $err_handler;
Eric Wonge5a0b242007-01-25 15:44:54 -08002149 }
2150 return undef unless defined $paths;
Eric Wong15710b62007-01-22 02:20:33 -08002151
2152 # look for a parent from another branch:
Eric Wong7f578c52007-01-24 02:16:25 -08002153 my @b_path_components = split m#/#, $self->rel_path;
2154 my @a_path_components;
2155 my $i;
2156 while (@b_path_components) {
2157 $i = $paths->{'/'.join('/', @b_path_components)};
Eric Wong74a81222007-02-10 13:28:50 -08002158 last if $i && defined $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002159 unshift(@a_path_components, pop(@b_path_components));
2160 }
Eric Wong74a81222007-02-10 13:28:50 -08002161 return undef unless defined $i && defined $i->{copyfrom_path};
2162 my $branch_from = $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002163 if (@a_path_components) {
2164 print STDERR "branch_from: $branch_from => ";
2165 $branch_from .= '/'.join('/', @a_path_components);
2166 print STDERR $branch_from, "\n";
2167 }
Eric Wong3ebe8df2007-01-25 17:35:40 -08002168 my $r = $i->{copyfrom_rev};
Eric Wong15710b62007-01-22 02:20:33 -08002169 my $repos_root = $self->ra->{repos_root};
2170 my $url = $self->ra->{url};
2171 my $new_url = $repos_root . $branch_from;
2172 print STDERR "Found possible branch point: ",
2173 "$new_url => ", $self->full_url, ", $r\n";
2174 $branch_from =~ s#^/##;
Eric Wonga8ae2622007-02-13 14:22:11 -08002175 my $gs = Git::SVN->find_by_url($new_url, $repos_root, $branch_from);
Eric Wong15710b62007-01-22 02:20:33 -08002176 unless ($gs) {
Eric Wongce2a0f22007-01-28 21:34:23 -08002177 my $ref_id = $self->{ref_id};
2178 $ref_id =~ s/\@\d+$//;
2179 $ref_id .= "\@$r";
Eric Wong15710b62007-01-22 02:20:33 -08002180 # just grow a tail if we're not unique enough :x
2181 $ref_id .= '-' while find_ref($ref_id);
Eric Wongce2a0f22007-01-28 21:34:23 -08002182 print STDERR "Initializing parent: $ref_id\n";
Eric Wongd8115c52007-02-01 03:30:31 -08002183 $gs = Git::SVN->init($new_url, '', $ref_id, $ref_id, 1);
Eric Wong15710b62007-01-22 02:20:33 -08002184 }
2185 my ($r0, $parent) = $gs->find_rev_before($r, 1);
Eric Wong91b03282007-02-11 00:51:33 -08002186 if (!defined $r0 || !defined $parent) {
Eric Wongd627de62007-04-15 03:01:29 -07002187 my ($base, $head) = parse_revision_argument(0, $r);
2188 if ($base <= $r) {
2189 $gs->fetch($base, $r);
2190 }
Eric Wong15710b62007-01-22 02:20:33 -08002191 ($r0, $parent) = $gs->last_rev_commit;
2192 }
Eric Wongef70de92007-02-01 04:12:41 -08002193 if (defined $r0 && defined $parent) {
Eric Wong15710b62007-01-22 02:20:33 -08002194 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n";
Eric Wong15710b62007-01-22 02:20:33 -08002195 my $ed;
2196 if ($self->ra->can_do_switch) {
Eric Wong2e5e2482007-02-23 02:21:59 -08002197 $self->assert_index_clean($parent);
Eric Wong8b8fc062007-01-22 11:44:57 -08002198 print STDERR "Following parent with do_switch\n";
Eric Wong15710b62007-01-22 02:20:33 -08002199 # do_switch works with svn/trunk >= r22312, but that
Eric Wong2b27f6c2007-01-28 04:59:05 -08002200 # is not included with SVN 1.4.3 (the latest version
Eric Wong15710b62007-01-22 02:20:33 -08002201 # at the moment), so we can't rely on it
2202 $self->{last_commit} = $parent;
2203 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08002204 $gs->ra->gs_do_switch($r0, $rev, $gs,
Eric Wong15710b62007-01-22 02:20:33 -08002205 $self->full_url, $ed)
2206 or die "SVN connection failed somewhere...\n";
Steven Walter9ff74e92007-09-28 13:24:19 -04002207 } elsif ($self->ra->trees_match($new_url, $r0,
2208 $self->full_url, $rev)) {
2209 print STDERR "Trees match:\n",
2210 " $new_url\@$r0\n",
2211 " ${\$self->full_url}\@$rev\n",
2212 "Following parent with no changes\n";
2213 $self->tmp_index_do(sub {
2214 command_noisy('read-tree', $parent);
2215 });
2216 $self->{last_commit} = $parent;
Eric Wong15710b62007-01-22 02:20:33 -08002217 } else {
Eric Wong8b8fc062007-01-22 11:44:57 -08002218 print STDERR "Following parent with do_update\n";
Eric Wong15710b62007-01-22 02:20:33 -08002219 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08002220 $self->ra->gs_do_update($rev, $rev, $self, $ed)
Eric Wong15710b62007-01-22 02:20:33 -08002221 or die "SVN connection failed somewhere...\n";
2222 }
Eric Wongf7c3fc42007-01-29 18:34:55 -08002223 print STDERR "Successfully followed parent\n";
Eric Wong15710b62007-01-22 02:20:33 -08002224 return $self->make_log_entry($rev, [$parent], $ed);
2225 }
Eric Wong15710b62007-01-22 02:20:33 -08002226 return undef;
2227}
2228
Eric Wong9b981fc2007-01-11 12:14:21 -08002229sub do_fetch {
Eric Wong706587f2007-01-18 17:50:01 -08002230 my ($self, $paths, $rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08002231 my $ed;
Eric Wong9b981fc2007-01-11 12:14:21 -08002232 my ($last_rev, @parents);
Eric Wongb9dffd82007-02-09 01:28:30 -08002233 if (my $lc = $self->last_commit) {
2234 # we can have a branch that was deleted, then re-added
2235 # under the same name but copied from another path, in
2236 # which case we'll have multiple parents (we don't
2237 # want to break the original ref, nor lose copypath info):
2238 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2239 push @{$log_entry->{parents}}, $lc;
2240 return $log_entry;
2241 }
Eric Wong15710b62007-01-22 02:20:33 -08002242 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002243 $last_rev = $self->{last_rev};
Eric Wongb9dffd82007-02-09 01:28:30 -08002244 $ed->{c} = $lc;
2245 @parents = ($lc);
Eric Wong9b981fc2007-01-11 12:14:21 -08002246 } else {
2247 $last_rev = $rev;
Eric Wong15710b62007-01-22 02:20:33 -08002248 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2249 return $log_entry;
2250 }
2251 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002252 }
Eric Wong8a603772007-01-31 02:45:50 -08002253 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002254 die "SVN connection failed somewhere...\n";
2255 }
2256 $self->make_log_entry($rev, \@parents, $ed);
2257}
2258
Eric Wong97f69872007-01-25 11:53:13 -08002259sub get_untracked {
2260 my ($self, $ed) = @_;
2261 my @out;
2262 my $h = $ed->{empty};
Eric Wong9b981fc2007-01-11 12:14:21 -08002263 foreach (sort keys %$h) {
2264 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
Eric Wong97f69872007-01-25 11:53:13 -08002265 push @out, " $act: " . uri_encode($_);
Eric Wong9b981fc2007-01-11 12:14:21 -08002266 warn "W: $act: $_\n";
2267 }
2268 foreach my $t (qw/dir_prop file_prop/) {
Eric Wong97f69872007-01-25 11:53:13 -08002269 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002270 foreach my $path (sort keys %$h) {
2271 my $ppath = $path eq '' ? '.' : $path;
2272 foreach my $prop (sort keys %{$h->{$path}}) {
Eric Wong1ce255d2007-01-14 23:21:16 -08002273 next if $SKIP_PROP{$prop};
Eric Wong9b981fc2007-01-11 12:14:21 -08002274 my $v = $h->{$path}->{$prop};
Eric Wong97f69872007-01-25 11:53:13 -08002275 my $t_ppath_prop = "$t: " .
2276 uri_encode($ppath) . ' ' .
2277 uri_encode($prop);
Eric Wong9b981fc2007-01-11 12:14:21 -08002278 if (defined $v) {
Eric Wong97f69872007-01-25 11:53:13 -08002279 push @out, " +$t_ppath_prop " .
2280 uri_encode($v);
Eric Wong9b981fc2007-01-11 12:14:21 -08002281 } else {
Eric Wong97f69872007-01-25 11:53:13 -08002282 push @out, " -$t_ppath_prop";
Eric Wong9b981fc2007-01-11 12:14:21 -08002283 }
2284 }
2285 }
2286 }
2287 foreach my $t (qw/absent_file absent_directory/) {
Eric Wong97f69872007-01-25 11:53:13 -08002288 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002289 foreach my $parent (sort keys %$h) {
2290 foreach my $path (sort @{$h->{$parent}}) {
Eric Wong97f69872007-01-25 11:53:13 -08002291 push @out, " $t: " .
2292 uri_encode("$parent/$path");
Eric Wong9b981fc2007-01-11 12:14:21 -08002293 warn "W: $t: $parent/$path ",
2294 "Insufficient permissions?\n";
2295 }
2296 }
2297 }
Eric Wong97f69872007-01-25 11:53:13 -08002298 \@out;
Eric Wong9b981fc2007-01-11 12:14:21 -08002299}
2300
Eric Wong1c8443b2007-01-14 02:17:00 -08002301sub parse_svn_date {
2302 my $date = shift || return '+0000 1970-01-01 00:00:00';
2303 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
2304 (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x) or
2305 croak "Unable to parse date: $date\n";
2306 "+0000 $Y-$m-$d $H:$M:$S";
2307}
2308
2309sub check_author {
2310 my ($author) = @_;
2311 if (!defined $author || length $author == 0) {
2312 $author = '(no author)';
2313 }
2314 if (defined $::_authors && ! defined $::users{$author}) {
2315 die "Author: $author not defined in $::_authors file\n";
2316 }
2317 $author;
2318}
2319
Eric Wong9b981fc2007-01-11 12:14:21 -08002320sub make_log_entry {
Eric Wong97f69872007-01-25 11:53:13 -08002321 my ($self, $rev, $parents, $ed) = @_;
2322 my $untracked = $self->get_untracked($ed);
2323
Eric Wong9b981fc2007-01-11 12:14:21 -08002324 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08002325 print $un "r$rev\n" or croak $!;
2326 print $un $_, "\n" foreach @$untracked;
2327 my %log_entry = ( parents => $parents || [], revision => $rev,
2328 log => '');
Eric Wongfbcc1732007-02-06 18:35:30 -08002329
Eric Wong8a49ee92007-02-10 20:46:50 -08002330 my $headrev;
Eric Wongfbcc1732007-02-06 18:35:30 -08002331 my $logged = delete $self->{logged_rev_props};
Eric Wong8a49ee92007-02-10 20:46:50 -08002332 if (!$logged || $self->{-want_revprops}) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002333 my $rp = $self->ra->rev_proplist($rev);
2334 foreach (sort keys %$rp) {
2335 my $v = $rp->{$_};
2336 if (/^svn:(author|date|log)$/) {
2337 $log_entry{$1} = $v;
Eric Wong8a49ee92007-02-10 20:46:50 -08002338 } elsif ($_ eq 'svm:headrev') {
2339 $headrev = $v;
Eric Wongfbcc1732007-02-06 18:35:30 -08002340 } else {
2341 print $un " rev_prop: ", uri_encode($_), ' ',
2342 uri_encode($v), "\n";
2343 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002344 }
Eric Wongfbcc1732007-02-06 18:35:30 -08002345 } else {
2346 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
Eric Wong9b981fc2007-01-11 12:14:21 -08002347 }
2348 close $un or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08002349
Eric Wong9b981fc2007-01-11 12:14:21 -08002350 $log_entry{date} = parse_svn_date($log_entry{date});
Eric Wong9b981fc2007-01-11 12:14:21 -08002351 $log_entry{log} .= "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002352 my $author = $log_entry{author} = check_author($log_entry{author});
2353 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
2354 : ($author, undef);
Eric Wong91b03282007-02-11 00:51:33 -08002355 if (defined $headrev && $self->use_svm_props) {
Eric Wongaea736c2007-02-16 19:15:21 -08002356 if ($self->rewrite_root) {
2357 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
2358 "options set!\n";
2359 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002360 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002361 # we don't want "SVM: initializing mirror for junk" ...
2362 return undef if $r == 0;
2363 my $svm = $self->svm;
2364 if ($uuid ne $svm->{uuid}) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002365 die "UUID mismatch on SVM path:\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08002366 "expected: $svm->{uuid}\n",
Eric Wong8a49ee92007-02-10 20:46:50 -08002367 " got: $uuid\n";
2368 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002369 my $full_url = $self->full_url;
2370 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
2371 die "Failed to replace '$svm->{replace}' with ",
2372 "'$svm->{source}' in $full_url\n";
Sam Vilain18ea92b2007-02-23 12:32:29 +13002373 # throw away username for storing in records
2374 remove_username($full_url);
Eric Wong8a49ee92007-02-10 20:46:50 -08002375 $log_entry{metadata} = "$full_url\@$r $uuid";
2376 $log_entry{svm_revision} = $r;
Eric Wongdb03cd22007-02-13 00:38:02 -08002377 $email ||= "$author\@$uuid"
Eric Wong62e349d2007-02-16 19:57:29 -08002378 } elsif ($self->use_svnsync_props) {
2379 my $full_url = $self->svnsync->{url};
2380 $full_url .= "/$self->{path}" if length $self->{path};
Adam Robence118732007-04-24 18:02:07 -07002381 remove_username($full_url);
Eric Wong62e349d2007-02-16 19:57:29 -08002382 my $uuid = $self->svnsync->{uuid};
2383 $log_entry{metadata} = "$full_url\@$rev $uuid";
2384 $email ||= "$author\@$uuid"
Eric Wong8a49ee92007-02-10 20:46:50 -08002385 } else {
Adam Robence118732007-04-24 18:02:07 -07002386 my $url = $self->metadata_url;
2387 remove_username($url);
2388 $log_entry{metadata} = "$url\@$rev " .
Eric Wong26a62d52007-02-12 13:25:25 -08002389 $self->ra->get_uuid;
Eric Wongdb03cd22007-02-13 00:38:02 -08002390 $email ||= "$author\@" . $self->ra->get_uuid;
Eric Wong8a49ee92007-02-10 20:46:50 -08002391 }
Eric Wongdb03cd22007-02-13 00:38:02 -08002392 $log_entry{name} = $name;
2393 $log_entry{email} = $email;
Eric Wong9b981fc2007-01-11 12:14:21 -08002394 \%log_entry;
2395}
2396
2397sub fetch {
Eric Wong3ebe8df2007-01-25 17:35:40 -08002398 my ($self, $min_rev, $max_rev, @parents) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002399 my ($last_rev, $last_commit) = $self->last_rev_commit;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002400 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
Eric Wonge5181922007-02-08 12:53:57 -08002401 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
Eric Wong9b981fc2007-01-11 12:14:21 -08002402}
2403
2404sub set_tree_cb {
2405 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
Eric Wong490f49e2007-02-10 13:58:33 -08002406 $self->{inject_parents} = { $rev => $tree };
2407 $self->fetch(undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08002408}
2409
2410sub set_tree {
2411 my ($self, $tree) = (shift, shift);
Eric Wong1ce255d2007-01-14 23:21:16 -08002412 my $log_entry = ::get_commit_entry($tree);
Eric Wong9b981fc2007-01-11 12:14:21 -08002413 unless ($self->{last_rev}) {
Benoit Sigoure207f1a72007-10-16 16:36:52 +02002414 fatal("Must have an existing revision to commit");
Eric Wong9b981fc2007-01-11 12:14:21 -08002415 }
Eric Wong61395352007-01-27 14:33:08 -08002416 my %ed_opts = ( r => $self->{last_rev},
2417 log => $log_entry->{log},
2418 ra => $self->ra,
2419 tree_a => $self->{last_commit},
2420 tree_b => $tree,
2421 editor_cb => sub {
2422 $self->set_tree_cb($log_entry, $tree, @_) },
2423 svn_path => $self->{path} );
2424 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002425 print "No changes\nr$self->{last_rev} = $tree\n";
2426 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002427}
2428
Eric Wongf0ecca12007-01-30 13:11:14 -08002429sub rebuild {
2430 my ($self) = @_;
Eric Wong26a62d52007-02-12 13:25:25 -08002431 my $db_path = $self->db_path;
Eric Wongd6d33462007-02-16 04:05:33 -08002432 return if (-e $db_path && ! -z $db_path);
2433 return unless ::verify_ref($self->refname.'^0');
Eric Wong26a62d52007-02-12 13:25:25 -08002434 if (-f $self->{db_root}) {
2435 rename $self->{db_root}, $db_path or die
2436 "rename $self->{db_root} => $db_path failed: $!\n";
2437 my ($dir, $base) = ($db_path =~ m#^(.*?)/?([^/]+)$#);
2438 symlink $base, $self->{db_root} or die
2439 "symlink $base => $self->{db_root} failed: $!\n";
2440 return;
2441 }
2442 print "Rebuilding $db_path ...\n";
Junio C Hamanoeeebd8d2007-08-31 14:29:49 -07002443 my ($log, $ctx) = command_output_pipe("log", '--no-color', $self->refname);
Eric Wongf0ecca12007-01-30 13:11:14 -08002444 my $latest;
2445 my $full_url = $self->full_url;
Sam Vilain18ea92b2007-02-23 12:32:29 +13002446 remove_username($full_url);
Eric Wongf0ecca12007-01-30 13:11:14 -08002447 my $svn_uuid;
Sam Vilain3dfab992007-06-30 20:56:13 +12002448 my $c;
2449 while (<$log>) {
2450 if ( m{^commit ($::sha1)$} ) {
2451 $c = $1;
2452 next;
2453 }
2454 next unless s{^\s*(git-svn-id:)}{$1};
2455 my ($url, $rev, $uuid) = ::extract_metadata($_);
Sam Vilain18ea92b2007-02-23 12:32:29 +13002456 remove_username($url);
Eric Wongf0ecca12007-01-30 13:11:14 -08002457
2458 # ignore merges (from set-tree)
2459 next if (!defined $rev || !$uuid);
2460
2461 # if we merged or otherwise started elsewhere, this is
2462 # how we break out of it
2463 if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
2464 ($full_url && $url && ($url ne $full_url))) {
2465 next;
2466 }
2467 $latest ||= $rev;
2468 $svn_uuid ||= $uuid;
2469
2470 $self->rev_db_set($rev, $c);
2471 print "r$rev = $c\n";
2472 }
Sam Vilain3dfab992007-06-30 20:56:13 +12002473 command_close_pipe($log, $ctx);
Eric Wong26a62d52007-02-12 13:25:25 -08002474 print "Done rebuilding $db_path\n";
Eric Wongf0ecca12007-01-30 13:11:14 -08002475}
2476
Eric Wong9b981fc2007-01-11 12:14:21 -08002477# rev_db:
2478# Tie::File seems to be prone to offset errors if revisions get sparse,
2479# it's not that fast, either. Tie::File is also not in Perl 5.6. So
2480# one of my favorite modules is out :< Next up would be one of the DBM
2481# modules, but I'm not sure which is most portable... So I'll just
2482# go with something that's plain-text, but still capable of
2483# being randomly accessed. So here's my ultra-simple fixed-width
2484# database. All records are 40 characters + "\n", so it's easy to seek
2485# to a revision: (41 * rev) is the byte offset.
2486# A record of 40 0s denotes an empty revision.
2487# And yes, it's still pretty fast (faster than Tie::File).
Eric Wong97ae0912007-02-11 15:21:24 -08002488# These files are disposable unless noMetadata or useSvmProps is set
Eric Wong9b981fc2007-01-11 12:14:21 -08002489
Eric Wong26a62d52007-02-12 13:25:25 -08002490sub _rev_db_set {
2491 my ($fh, $rev, $commit) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002492 my $offset = $rev * 41;
2493 # assume that append is the common case:
2494 seek $fh, 0, 2 or croak $!;
2495 my $pos = tell $fh;
2496 if ($pos < $offset) {
Eric Wong47a0b752007-01-31 05:13:30 -08002497 for (1 .. (($offset - $pos) / 41)) {
2498 print $fh (('0' x 40),"\n") or croak $!;
2499 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002500 }
2501 seek $fh, $offset, 0 or croak $!;
2502 print $fh $commit,"\n" or croak $!;
Eric Wong26a62d52007-02-12 13:25:25 -08002503}
2504
2505sub mkfile {
2506 my ($path) = @_;
2507 unless (-e $path) {
2508 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
2509 mkpath([$dir]) unless -d $dir;
2510 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
2511 close $fh or die "Couldn't close (create) $path: $!\n";
2512 }
2513}
2514
2515sub rev_db_set {
2516 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
2517 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
2518 my $db = $self->db_path($uuid);
2519 my $db_lock = "$db.lock";
2520 my $sig;
2521 if ($update_ref) {
2522 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
2523 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
2524 }
2525 mkfile($db);
2526
2527 $LOCKFILES{$db_lock} = 1;
2528 my $sync;
2529 # both of these options make our .rev_db file very, very important
2530 # and we can't afford to lose it because rebuild() won't work
2531 if ($self->use_svm_props || $self->no_metadata) {
2532 $sync = 1;
2533 copy($db, $db_lock) or die "rev_db_set(@_): ",
2534 "Failed to copy: ",
2535 "$db => $db_lock ($!)\n";
2536 } else {
2537 rename $db, $db_lock or die "rev_db_set(@_): ",
2538 "Failed to rename: ",
2539 "$db => $db_lock ($!)\n";
2540 }
2541 open my $fh, '+<', $db_lock or die "Couldn't open $db_lock: $!\n";
2542 _rev_db_set($fh, $rev, $commit);
Eric Wong97ae0912007-02-11 15:21:24 -08002543 if ($sync) {
2544 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2545 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2546 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002547 close $fh or croak $!;
Eric Wong373274f2007-01-31 13:54:23 -08002548 if ($update_ref) {
Eric Wong1e889ef2007-02-16 01:45:13 -08002549 $_head = $self;
Eric Wong373274f2007-01-31 13:54:23 -08002550 command_noisy('update-ref', '-m', "r$rev",
2551 $self->refname, $commit);
2552 }
2553 rename $db_lock, $db or die "rev_db_set(@_): ", "Failed to rename: ",
2554 "$db_lock => $db ($!)\n";
2555 delete $LOCKFILES{$db_lock};
2556 if ($update_ref) {
2557 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
2558 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
2559 kill $sig, $$ if defined $sig;
2560 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002561}
2562
Eric Wong9c93fee2007-01-31 17:22:31 -08002563sub rev_db_max {
2564 my ($self) = @_;
Eric Wongd6d33462007-02-16 04:05:33 -08002565 $self->rebuild;
Eric Wong26a62d52007-02-12 13:25:25 -08002566 my $db_path = $self->db_path;
2567 my @stat = stat $db_path or return 0;
2568 ($stat[7] % 41) == 0 or die "$db_path inconsistent size: $stat[7]\n";
Eric Wong9c93fee2007-01-31 17:22:31 -08002569 my $max = $stat[7] / 41;
2570 (($max > 0) ? $max - 1 : 0);
2571}
2572
Eric Wong9b981fc2007-01-11 12:14:21 -08002573sub rev_db_get {
Eric Wong26a62d52007-02-12 13:25:25 -08002574 my ($self, $rev, $uuid) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002575 my $ret;
2576 my $offset = $rev * 41;
Eric Wong26a62d52007-02-12 13:25:25 -08002577 my $db_path = $self->db_path($uuid);
2578 return undef unless -e $db_path;
2579 open my $fh, '<', $db_path or croak $!;
Eric Wongce4b4af2007-01-31 17:57:36 -08002580 if (sysseek($fh, $offset, 0) == $offset) {
2581 my $read = sysread($fh, $ret, 40);
2582 $ret = undef if ($read != 40 || $ret eq ('0'x40));
Eric Wong9b981fc2007-01-11 12:14:21 -08002583 }
2584 close $fh or croak $!;
2585 $ret;
2586}
2587
David D Kilzer111947e2007-11-11 22:56:52 -08002588# Finds the first svn revision that exists on (if $eq_ok is true) or
2589# before $rev for the current branch. It will not search any lower
2590# than $min_rev. Returns the git commit hash and svn revision number
2591# if found, else (undef, undef).
Eric Wong15710b62007-01-22 02:20:33 -08002592sub find_rev_before {
David D Kilzer111947e2007-11-11 22:56:52 -08002593 my ($self, $rev, $eq_ok, $min_rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08002594 --$rev unless $eq_ok;
David D Kilzer111947e2007-11-11 22:56:52 -08002595 $min_rev ||= 1;
2596 while ($rev >= $min_rev) {
Eric Wong15710b62007-01-22 02:20:33 -08002597 if (my $c = $self->rev_db_get($rev)) {
2598 return ($rev, $c);
2599 }
2600 --$rev;
2601 }
2602 return (undef, undef);
2603}
2604
David D Kilzer111947e2007-11-11 22:56:52 -08002605# Finds the first svn revision that exists on (if $eq_ok is true) or
2606# after $rev for the current branch. It will not search any higher
2607# than $max_rev. Returns the git commit hash and svn revision number
2608# if found, else (undef, undef).
2609sub find_rev_after {
2610 my ($self, $rev, $eq_ok, $max_rev) = @_;
2611 ++$rev unless $eq_ok;
2612 $max_rev ||= $self->rev_db_max();
2613 while ($rev <= $max_rev) {
2614 if (my $c = $self->rev_db_get($rev)) {
2615 return ($rev, $c);
2616 }
2617 ++$rev;
2618 }
2619 return (undef, undef);
2620}
2621
Eric Wong9b981fc2007-01-11 12:14:21 -08002622sub _new {
Eric Wong706587f2007-01-18 17:50:01 -08002623 my ($class, $repo_id, $ref_id, $path) = @_;
2624 unless (defined $repo_id && length $repo_id) {
2625 $repo_id = $Git::SVN::default_repo_id;
2626 }
2627 unless (defined $ref_id && length $ref_id) {
Eric Wong8b8fc062007-01-22 11:44:57 -08002628 $_[2] = $ref_id = $Git::SVN::default_ref_id;
Eric Wong706587f2007-01-18 17:50:01 -08002629 }
2630 $_[1] = $repo_id = sanitize_remote_name($repo_id);
2631 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
2632 $_[3] = $path = '' unless (defined $path);
Eric Wongb4d57e52007-02-14 15:10:44 -08002633 mkpath(["$ENV{GIT_DIR}/svn"]);
Eric Wong26a62d52007-02-12 13:25:25 -08002634 bless {
2635 ref_id => $ref_id, dir => $dir, index => "$dir/index",
Eric Wong8a49ee92007-02-10 20:46:50 -08002636 path => $path, config => "$ENV{GIT_DIR}/svn/config",
Eric Wong26a62d52007-02-12 13:25:25 -08002637 db_root => "$dir/.rev_db", repo_id => $repo_id }, $class;
2638}
2639
2640sub db_path {
2641 my ($self, $uuid) = @_;
2642 $uuid ||= $self->ra_uuid;
2643 "$self->{db_root}.$uuid";
Eric Wong9b981fc2007-01-11 12:14:21 -08002644}
2645
Eric Wong1c8443b2007-01-14 02:17:00 -08002646sub uri_encode {
2647 my ($f) = @_;
2648 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2649 $f
2650}
Eric Wong9b981fc2007-01-11 12:14:21 -08002651
Sam Vilain18ea92b2007-02-23 12:32:29 +13002652sub remove_username {
2653 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
2654}
2655
Eric Wongd976acf2007-01-04 00:45:03 -08002656package Git::SVN::Prompt;
2657use strict;
2658use warnings;
2659require SVN::Core;
2660use vars qw/$_no_auth_cache $_username/;
2661
2662sub simple {
Eric Wong30d055a2006-11-24 01:38:04 -08002663 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
2664 $may_save = undef if $_no_auth_cache;
2665 $default_username = $_username if defined $_username;
2666 if (defined $default_username && length $default_username) {
2667 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08002668 print STDERR "Authentication realm: $realm\n";
2669 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002670 }
2671 $cred->username($default_username);
2672 } else {
Eric Wongd976acf2007-01-04 00:45:03 -08002673 username($cred, $realm, $may_save, $pool);
Eric Wong30d055a2006-11-24 01:38:04 -08002674 }
2675 $cred->password(_read_password("Password for '" .
2676 $cred->username . "': ", $realm));
2677 $cred->may_save($may_save);
2678 $SVN::_Core::SVN_NO_ERROR;
2679}
2680
Eric Wongd976acf2007-01-04 00:45:03 -08002681sub ssl_server_trust {
Eric Wong30d055a2006-11-24 01:38:04 -08002682 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
2683 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08002684 print STDERR "Error validating server certificate for '$realm':\n";
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04002685 {
2686 no warnings 'once';
2687 # All variables SVN::Auth::SSL::* are used only once,
2688 # so we're shutting up Perl warnings about this.
2689 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
2690 print STDERR " - The certificate is not issued ",
2691 "by a trusted authority. Use the\n",
2692 " fingerprint to validate ",
2693 "the certificate manually!\n";
2694 }
2695 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
2696 print STDERR " - The certificate hostname ",
2697 "does not match.\n";
2698 }
2699 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
2700 print STDERR " - The certificate is not yet valid.\n";
2701 }
2702 if ($failures & $SVN::Auth::SSL::EXPIRED) {
2703 print STDERR " - The certificate has expired.\n";
2704 }
2705 if ($failures & $SVN::Auth::SSL::OTHER) {
2706 print STDERR " - The certificate has ",
2707 "an unknown error.\n";
2708 }
2709 } # no warnings 'once'
Eric Wong6f729592007-01-15 20:15:55 -08002710 printf STDERR
2711 "Certificate information:\n".
Eric Wong30d055a2006-11-24 01:38:04 -08002712 " - Hostname: %s\n".
2713 " - Valid: from %s until %s\n".
2714 " - Issuer: %s\n".
2715 " - Fingerprint: %s\n",
2716 map $cert_info->$_, qw(hostname valid_from valid_until
Eric Wong6f729592007-01-15 20:15:55 -08002717 issuer_dname fingerprint);
Eric Wong30d055a2006-11-24 01:38:04 -08002718 my $choice;
2719prompt:
Eric Wong6f729592007-01-15 20:15:55 -08002720 print STDERR $may_save ?
Eric Wong30d055a2006-11-24 01:38:04 -08002721 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
2722 "(R)eject or accept (t)emporarily? ";
Eric Wong6f729592007-01-15 20:15:55 -08002723 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002724 $choice = lc(substr(<STDIN> || 'R', 0, 1));
2725 if ($choice =~ /^t$/i) {
2726 $cred->may_save(undef);
2727 } elsif ($choice =~ /^r$/i) {
2728 return -1;
2729 } elsif ($may_save && $choice =~ /^p$/i) {
2730 $cred->may_save($may_save);
2731 } else {
2732 goto prompt;
2733 }
2734 $cred->accepted_failures($failures);
2735 $SVN::_Core::SVN_NO_ERROR;
2736}
2737
Eric Wongd976acf2007-01-04 00:45:03 -08002738sub ssl_client_cert {
Eric Wong30d055a2006-11-24 01:38:04 -08002739 my ($cred, $realm, $may_save, $pool) = @_;
2740 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08002741 print STDERR "Client certificate filename: ";
2742 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002743 chomp(my $filename = <STDIN>);
2744 $cred->cert_file($filename);
2745 $cred->may_save($may_save);
2746 $SVN::_Core::SVN_NO_ERROR;
2747}
2748
Eric Wongd976acf2007-01-04 00:45:03 -08002749sub ssl_client_cert_pw {
Eric Wong30d055a2006-11-24 01:38:04 -08002750 my ($cred, $realm, $may_save, $pool) = @_;
2751 $may_save = undef if $_no_auth_cache;
2752 $cred->password(_read_password("Password: ", $realm));
2753 $cred->may_save($may_save);
2754 $SVN::_Core::SVN_NO_ERROR;
2755}
2756
Eric Wongd976acf2007-01-04 00:45:03 -08002757sub username {
Eric Wong30d055a2006-11-24 01:38:04 -08002758 my ($cred, $realm, $may_save, $pool) = @_;
2759 $may_save = undef if $_no_auth_cache;
2760 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08002761 print STDERR "Authentication realm: $realm\n";
Eric Wong30d055a2006-11-24 01:38:04 -08002762 }
2763 my $username;
2764 if (defined $_username) {
2765 $username = $_username;
2766 } else {
Eric Wong6f729592007-01-15 20:15:55 -08002767 print STDERR "Username: ";
2768 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002769 chomp($username = <STDIN>);
2770 }
2771 $cred->username($username);
2772 $cred->may_save($may_save);
2773 $SVN::_Core::SVN_NO_ERROR;
2774}
2775
2776sub _read_password {
2777 my ($prompt, $realm) = @_;
Eric Wong6f729592007-01-15 20:15:55 -08002778 print STDERR $prompt;
2779 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002780 require Term::ReadKey;
2781 Term::ReadKey::ReadMode('noecho');
2782 my $password = '';
2783 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
2784 last if $key =~ /[\012\015]/; # \n\r
2785 $password .= $key;
2786 }
2787 Term::ReadKey::ReadMode('restore');
Eric Wong6f729592007-01-15 20:15:55 -08002788 print STDERR "\n";
2789 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002790 $password;
2791}
2792
Eric Wong27a1a802006-11-27 21:44:48 -08002793package SVN::Git::Fetcher;
2794use vars qw/@ISA/;
2795use strict;
2796use warnings;
2797use Carp qw/croak/;
2798use IO::File qw//;
2799
2800# file baton members: path, mode_a, mode_b, pool, fh, blob, base
2801sub new {
2802 my ($class, $git_svn) = @_;
2803 my $self = SVN::Delta::Editor->new;
2804 bless $self, $class;
Eric Wong1c8443b2007-01-14 02:17:00 -08002805 $self->{c} = $git_svn->{last_commit} if exists $git_svn->{last_commit};
Eric Wongd2a9a872006-12-12 14:47:00 -08002806 $self->{empty} = {};
2807 $self->{dir_prop} = {};
2808 $self->{file_prop} = {};
2809 $self->{absent_dir} = {};
2810 $self->{absent_file} = {};
Eric Wongef3cfaa2007-01-24 03:30:57 -08002811 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
Eric Wong27a1a802006-11-27 21:44:48 -08002812 $self;
2813}
2814
Eric Wong8b8fc062007-01-22 11:44:57 -08002815sub set_path_strip {
2816 my ($self, $path) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002817 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
Eric Wong8b8fc062007-01-22 11:44:57 -08002818}
2819
Eric Wongd2a9a872006-12-12 14:47:00 -08002820sub open_root {
2821 { path => '' };
2822}
2823
2824sub open_directory {
2825 my ($self, $path, $pb, $rev) = @_;
2826 { path => $path };
2827}
2828
Eric Wong706587f2007-01-18 17:50:01 -08002829sub git_path {
2830 my ($self, $path) = @_;
Eric Wong2b27f6c2007-01-28 04:59:05 -08002831 if ($self->{path_strip}) {
2832 $path =~ s!$self->{path_strip}!! or
2833 die "Failed to strip path '$path' ($self->{path_strip})\n";
2834 }
Eric Wong706587f2007-01-18 17:50:01 -08002835 $path;
2836}
2837
Eric Wong27a1a802006-11-27 21:44:48 -08002838sub delete_entry {
2839 my ($self, $path, $rev, $pb) = @_;
Eric Wong4a87db02007-01-04 01:38:18 -08002840
Eric Wong706587f2007-01-18 17:50:01 -08002841 my $gpath = $self->git_path($path);
Eric Wong8a603772007-01-31 02:45:50 -08002842 return undef if ($gpath eq '');
2843
Eric Wong4a87db02007-01-04 01:38:18 -08002844 # remove entire directories.
Eric Wong706587f2007-01-18 17:50:01 -08002845 if (command('ls-tree', $self->{c}, '--', $gpath) =~ /^040000 tree/) {
Eric Wong4a87db02007-01-04 01:38:18 -08002846 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
2847 -r --name-only -z/,
Eric Wong706587f2007-01-18 17:50:01 -08002848 $self->{c}, '--', $gpath);
Eric Wong4a87db02007-01-04 01:38:18 -08002849 local $/ = "\0";
2850 while (<$ls>) {
Eric Wongef3cfaa2007-01-24 03:30:57 -08002851 chomp;
2852 $self->{gii}->remove($_);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08002853 print "\tD\t$_\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08002854 }
Eric Wong9e3cdbd2007-02-09 12:23:47 -08002855 print "\tD\t$gpath/\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08002856 command_close_pipe($ls, $ctx);
2857 $self->{empty}->{$path} = 0
2858 } else {
Eric Wongef3cfaa2007-01-24 03:30:57 -08002859 $self->{gii}->remove($gpath);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08002860 print "\tD\t$gpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08002861 }
Eric Wong27a1a802006-11-27 21:44:48 -08002862 undef;
2863}
2864
2865sub open_file {
2866 my ($self, $path, $pb, $rev) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08002867 my $gpath = $self->git_path($path);
2868 my ($mode, $blob) = (command('ls-tree', $self->{c}, '--', $gpath)
Eric Wong27a1a802006-11-27 21:44:48 -08002869 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
Eric Wong006ede52006-12-08 01:55:19 -08002870 unless (defined $mode && defined $blob) {
2871 die "$path was not found in commit $self->{c} (r$rev)\n";
2872 }
Eric Wong27a1a802006-11-27 21:44:48 -08002873 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
Eric Wong0864e3b2006-11-28 02:50:17 -08002874 pool => SVN::Pool->new, action => 'M' };
Eric Wong27a1a802006-11-27 21:44:48 -08002875}
2876
2877sub add_file {
2878 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
Eric Wongd2a9a872006-12-12 14:47:00 -08002879 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
2880 delete $self->{empty}->{$dir};
Eric Wong27a1a802006-11-27 21:44:48 -08002881 { path => $path, mode_a => 100644, mode_b => 100644,
Eric Wong0864e3b2006-11-28 02:50:17 -08002882 pool => SVN::Pool->new, action => 'A' };
Eric Wong27a1a802006-11-27 21:44:48 -08002883}
2884
Eric Wongd2a9a872006-12-12 14:47:00 -08002885sub add_directory {
2886 my ($self, $path, $cp_path, $cp_rev) = @_;
2887 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
2888 delete $self->{empty}->{$dir};
2889 $self->{empty}->{$path} = 1;
2890 { path => $path };
2891}
2892
2893sub change_dir_prop {
2894 my ($self, $db, $prop, $value) = @_;
2895 $self->{dir_prop}->{$db->{path}} ||= {};
2896 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
2897 undef;
2898}
2899
2900sub absent_directory {
2901 my ($self, $path, $pb) = @_;
2902 $self->{absent_dir}->{$pb->{path}} ||= [];
2903 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
2904 undef;
2905}
2906
2907sub absent_file {
2908 my ($self, $path, $pb) = @_;
2909 $self->{absent_file}->{$pb->{path}} ||= [];
2910 push @{$self->{absent_file}->{$pb->{path}}}, $path;
2911 undef;
2912}
2913
Eric Wong27a1a802006-11-27 21:44:48 -08002914sub change_file_prop {
2915 my ($self, $fb, $prop, $value) = @_;
2916 if ($prop eq 'svn:executable') {
2917 if ($fb->{mode_b} != 120000) {
2918 $fb->{mode_b} = defined $value ? 100755 : 100644;
2919 }
2920 } elsif ($prop eq 'svn:special') {
2921 $fb->{mode_b} = defined $value ? 120000 : 100644;
Eric Wongd2a9a872006-12-12 14:47:00 -08002922 } else {
2923 $self->{file_prop}->{$fb->{path}} ||= {};
2924 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
Eric Wong27a1a802006-11-27 21:44:48 -08002925 }
2926 undef;
2927}
2928
2929sub apply_textdelta {
2930 my ($self, $fb, $exp) = @_;
2931 my $fh = IO::File->new_tmpfile;
2932 $fh->autoflush(1);
2933 # $fh gets auto-closed() by SVN::TxDelta::apply(),
2934 # (but $base does not,) so dup() it for reading in close_file
2935 open my $dup, '<&', $fh or croak $!;
2936 my $base = IO::File->new_tmpfile;
2937 $base->autoflush(1);
2938 if ($fb->{blob}) {
2939 defined (my $pid = fork) or croak $!;
2940 if (!$pid) {
2941 open STDOUT, '>&', $base or croak $!;
2942 print STDOUT 'link ' if ($fb->{mode_a} == 120000);
2943 exec qw/git-cat-file blob/, $fb->{blob} or croak $!;
2944 }
2945 waitpid $pid, 0;
2946 croak $? if $?;
2947
2948 if (defined $exp) {
2949 seek $base, 0, 0 or croak $!;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08002950 my $got = Git::SVN::Util::md5sum($base);
Eric Wong27a1a802006-11-27 21:44:48 -08002951 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
2952 "expected: $exp\n",
2953 " got: $got\n" if ($got ne $exp);
2954 }
2955 }
2956 seek $base, 0, 0 or croak $!;
2957 $fb->{fh} = $dup;
2958 $fb->{base} = $base;
2959 [ SVN::TxDelta::apply($base, $fh, undef, $fb->{path}, $fb->{pool}) ];
2960}
2961
2962sub close_file {
2963 my ($self, $fb, $exp) = @_;
2964 my $hash;
Eric Wong706587f2007-01-18 17:50:01 -08002965 my $path = $self->git_path($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08002966 if (my $fh = $fb->{fh}) {
Eric Wong7faf0682007-05-27 15:59:01 -07002967 if (defined $exp) {
2968 seek($fh, 0, 0) or croak $!;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08002969 my $got = Git::SVN::Util::md5sum($fh);
Eric Wong7faf0682007-05-27 15:59:01 -07002970 if ($got ne $exp) {
2971 die "Checksum mismatch: $path\n",
2972 "expected: $exp\n got: $got\n";
2973 }
2974 }
Junio C Hamanobcd8ee52007-04-29 14:05:54 -07002975 sysseek($fh, 0, 0) or croak $!;
Eric Wong27a1a802006-11-27 21:44:48 -08002976 if ($fb->{mode_b} == 120000) {
Junio C Hamanobcd8ee52007-04-29 14:05:54 -07002977 sysread($fh, my $buf, 5) == 5 or croak $!;
Eric Wong27a1a802006-11-27 21:44:48 -08002978 $buf eq 'link ' or die "$path has mode 120000",
2979 "but is not a link\n";
2980 }
2981 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
2982 if (!$pid) {
2983 open STDIN, '<&', $fh or croak $!;
2984 exec qw/git-hash-object -w --stdin/ or croak $!;
2985 }
2986 chomp($hash = do { local $/; <$out> });
2987 close $out or croak $!;
2988 close $fh or croak $!;
2989 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
2990 close $fb->{base} or croak $!;
2991 } else {
2992 $hash = $fb->{blob} or die "no blob information\n";
2993 }
2994 $fb->{pool}->clear;
Eric Wongef3cfaa2007-01-24 03:30:57 -08002995 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
Eric Wong9e3cdbd2007-02-09 12:23:47 -08002996 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
Eric Wong27a1a802006-11-27 21:44:48 -08002997 undef;
2998}
2999
3000sub abort_edit {
3001 my $self = shift;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003002 $self->{nr} = $self->{gii}->{nr};
3003 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08003004 $self->SUPER::abort_edit(@_);
3005}
3006
3007sub close_edit {
3008 my $self = shift;
Eric Wongdad73c02006-11-28 14:06:05 -08003009 $self->{git_commit_ok} = 1;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003010 $self->{nr} = $self->{gii}->{nr};
3011 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08003012 $self->SUPER::close_edit(@_);
3013}
Eric Wong1a82e792006-06-16 02:55:13 -07003014
Eric Wonga5e0ced2006-06-12 15:23:48 -07003015package SVN::Git::Editor;
Eric Wong24e22aa2007-01-29 00:07:49 -08003016use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003017use strict;
3018use warnings;
3019use Carp qw/croak/;
3020use IO::File;
3021
3022sub new {
Eric Wong61395352007-01-27 14:33:08 -08003023 my ($class, $opts) = @_;
3024 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
3025 die "$_ required!\n" unless (defined $opts->{$_});
Eric Wonga5e0ced2006-06-12 15:23:48 -07003026 }
Eric Wong61395352007-01-27 14:33:08 -08003027
3028 my $pool = SVN::Pool->new;
3029 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
3030 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
3031 $opts->{r}, $mods);
3032
3033 # $opts->{ra} functions should not be used after this:
3034 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
3035 $opts->{editor_cb}, $pool);
3036 my $self = SVN::Delta::Editor->new(@ce, $pool);
3037 bless $self, $class;
3038 foreach (qw/svn_path r tree_a tree_b/) {
3039 $self->{$_} = $opts->{$_};
3040 }
3041 $self->{url} = $opts->{ra}->{url};
3042 $self->{mods} = $mods;
3043 $self->{types} = $types;
3044 $self->{pool} = $pool;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003045 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
3046 $self->{rm} = { };
Eric Wongd3a840d2007-01-26 01:32:45 -08003047 $self->{path_prefix} = length $self->{svn_path} ?
3048 "$self->{svn_path}/" : '';
Eric Wonga5e0ced2006-06-12 15:23:48 -07003049 return $self;
3050}
3051
Eric Wong61395352007-01-27 14:33:08 -08003052sub generate_diff {
3053 my ($tree_a, $tree_b) = @_;
3054 my @diff_tree = qw(diff-tree -z -r);
Eric Wong24e22aa2007-01-29 00:07:49 -08003055 if ($_cp_similarity) {
3056 push @diff_tree, "-C$_cp_similarity";
Eric Wong61395352007-01-27 14:33:08 -08003057 } else {
3058 push @diff_tree, '-C';
3059 }
Eric Wong24e22aa2007-01-29 00:07:49 -08003060 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
3061 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
Eric Wong61395352007-01-27 14:33:08 -08003062 push @diff_tree, $tree_a, $tree_b;
3063 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
3064 local $/ = "\0";
3065 my $state = 'meta';
3066 my @mods;
3067 while (<$diff_fh>) {
3068 chomp $_; # this gets rid of the trailing "\0"
3069 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
3070 $::sha1\s($::sha1)\s
3071 ([MTCRAD])\d*$/xo) {
3072 push @mods, { mode_a => $1, mode_b => $2,
3073 sha1_b => $3, chg => $4 };
3074 if ($4 =~ /^(?:C|R)$/) {
3075 $state = 'file_a';
3076 } else {
3077 $state = 'file_b';
3078 }
3079 } elsif ($state eq 'file_a') {
3080 my $x = $mods[$#mods] or croak "Empty array\n";
3081 if ($x->{chg} !~ /^(?:C|R)$/) {
3082 croak "Error parsing $_, $x->{chg}\n";
3083 }
3084 $x->{file_a} = $_;
3085 $state = 'file_b';
3086 } elsif ($state eq 'file_b') {
3087 my $x = $mods[$#mods] or croak "Empty array\n";
3088 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
3089 croak "Error parsing $_, $x->{chg}\n";
3090 }
3091 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
3092 croak "Error parsing $_, $x->{chg}\n";
3093 }
3094 $x->{file_b} = $_;
3095 $state = 'meta';
3096 } else {
3097 croak "Error parsing $_\n";
3098 }
3099 }
3100 command_close_pipe($diff_fh, $ctx);
3101 \@mods;
3102}
3103
3104sub check_diff_paths {
3105 my ($ra, $pfx, $rev, $mods) = @_;
3106 my %types;
3107 $pfx .= '/' if length $pfx;
3108
3109 sub type_diff_paths {
3110 my ($ra, $types, $path, $rev) = @_;
3111 my @p = split m#/+#, $path;
3112 my $c = shift @p;
3113 unless (defined $types->{$c}) {
3114 $types->{$c} = $ra->check_path($c, $rev);
3115 }
3116 while (@p) {
3117 $c .= '/' . shift @p;
3118 next if defined $types->{$c};
3119 $types->{$c} = $ra->check_path($c, $rev);
3120 }
3121 }
3122
3123 foreach my $m (@$mods) {
3124 foreach my $f (qw/file_a file_b/) {
3125 next unless defined $m->{$f};
3126 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
3127 if (length $pfx.$dir && ! defined $types{$dir}) {
3128 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
3129 }
3130 }
3131 }
3132 \%types;
3133}
3134
Eric Wonga5e0ced2006-06-12 15:23:48 -07003135sub split_path {
3136 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
3137}
3138
3139sub repo_path {
Eric Wongd3a840d2007-01-26 01:32:45 -08003140 my ($self, $path) = @_;
3141 $self->{path_prefix}.(defined $path ? $path : '');
Eric Wonga5e0ced2006-06-12 15:23:48 -07003142}
3143
3144sub url_path {
3145 my ($self, $path) = @_;
Eric Wong29633bb2007-07-15 21:53:50 -07003146 if ($self->{url} =~ m#^https?://#) {
3147 $path =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
3148 }
Eric Wong6e8548c2007-01-27 01:32:00 -08003149 $self->{url} . '/' . $self->repo_path($path);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003150}
3151
3152sub rmdirs {
Eric Wong61395352007-01-27 14:33:08 -08003153 my ($self) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003154 my $rm = $self->{rm};
3155 delete $rm->{''}; # we never delete the url we're tracking
3156 return unless %$rm;
3157
3158 foreach (keys %$rm) {
3159 my @d = split m#/#, $_;
3160 my $c = shift @d;
3161 $rm->{$c} = 1;
3162 while (@d) {
3163 $c .= '/' . shift @d;
3164 $rm->{$c} = 1;
3165 }
3166 }
3167 delete $rm->{$self->{svn_path}};
3168 delete $rm->{''}; # we never delete the url we're tracking
3169 return unless %$rm;
3170
Eric Wong61395352007-01-27 14:33:08 -08003171 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
3172 $self->{tree_b});
Eric Wonga5e0ced2006-06-12 15:23:48 -07003173 local $/ = "\0";
3174 while (<$fh>) {
3175 chomp;
Eric Wong747fa122006-11-24 22:38:17 -08003176 my @dn = split m#/#, $_;
Eric Wongc07eee12006-06-19 17:59:35 -07003177 while (pop @dn) {
3178 delete $rm->{join '/', @dn};
3179 }
3180 unless (%$rm) {
Eric Wong22600a22007-02-01 13:12:26 -08003181 close $fh;
Eric Wongc07eee12006-06-19 17:59:35 -07003182 return;
3183 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07003184 }
Eric Wongaef4e922006-12-15 10:59:54 -08003185 command_close_pipe($fh, $ctx);
Eric Wongc07eee12006-06-19 17:59:35 -07003186
Eric Wonga5e0ced2006-06-12 15:23:48 -07003187 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
3188 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3189 $self->close_directory($bat->{$d}, $p);
3190 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
Eric Wong44320b92007-01-13 22:35:53 -08003191 print "\tD+\t$d/\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003192 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
3193 delete $bat->{$d};
3194 }
3195}
3196
3197sub open_or_add_dir {
3198 my ($self, $full_path, $baton) = @_;
Eric Wong6e8548c2007-01-27 01:32:00 -08003199 my $t = $self->{types}->{$full_path};
3200 if (!defined $t) {
3201 die "$full_path not known in r$self->{r} or we have a bug!\n";
3202 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003203 {
3204 no warnings 'once';
3205 # SVN::Node::none and SVN::Node::file are used only once,
3206 # so we're shutting up Perl's warnings about them.
3207 if ($t == $SVN::Node::none) {
3208 return $self->add_directory($full_path, $baton,
3209 undef, -1, $self->{pool});
3210 } elsif ($t == $SVN::Node::dir) {
3211 return $self->open_directory($full_path, $baton,
3212 $self->{r}, $self->{pool});
3213 } # no warnings 'once'
3214 print STDERR "$full_path already exists in repository at ",
3215 "r$self->{r} and it is not a directory (",
3216 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
3217 } # no warnings 'once'
Eric Wonga5e0ced2006-06-12 15:23:48 -07003218 exit 1;
3219}
3220
3221sub ensure_path {
3222 my ($self, $path) = @_;
3223 my $bat = $self->{bat};
Eric Wong6e8548c2007-01-27 01:32:00 -08003224 my $repo_path = $self->repo_path($path);
3225 return $bat->{''} unless (length $repo_path);
3226 my @p = split m#/+#, $repo_path;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003227 my $c = shift @p;
3228 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3229 while (@p) {
3230 my $c0 = $c;
3231 $c .= '/' . shift @p;
3232 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3233 }
3234 return $bat->{$c};
3235}
3236
3237sub A {
Eric Wong44320b92007-01-13 22:35:53 -08003238 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003239 my ($dir, $file) = split_path($m->{file_b});
3240 my $pbat = $self->ensure_path($dir);
3241 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3242 undef, -1);
Eric Wong44320b92007-01-13 22:35:53 -08003243 print "\tA\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003244 $self->chg_file($fbat, $m);
3245 $self->close_file($fbat,undef,$self->{pool});
3246}
3247
3248sub C {
Eric Wong44320b92007-01-13 22:35:53 -08003249 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003250 my ($dir, $file) = split_path($m->{file_b});
3251 my $pbat = $self->ensure_path($dir);
3252 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3253 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08003254 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003255 $self->chg_file($fbat, $m);
3256 $self->close_file($fbat,undef,$self->{pool});
3257}
3258
3259sub delete_entry {
3260 my ($self, $path, $pbat) = @_;
3261 my $rpath = $self->repo_path($path);
3262 my ($dir, $file) = split_path($rpath);
3263 $self->{rm}->{$dir} = 1;
3264 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
3265}
3266
3267sub R {
Eric Wong44320b92007-01-13 22:35:53 -08003268 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003269 my ($dir, $file) = split_path($m->{file_b});
3270 my $pbat = $self->ensure_path($dir);
3271 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3272 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08003273 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003274 $self->chg_file($fbat, $m);
3275 $self->close_file($fbat,undef,$self->{pool});
3276
3277 ($dir, $file) = split_path($m->{file_a});
3278 $pbat = $self->ensure_path($dir);
3279 $self->delete_entry($m->{file_a}, $pbat);
3280}
3281
3282sub M {
Eric Wong44320b92007-01-13 22:35:53 -08003283 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003284 my ($dir, $file) = split_path($m->{file_b});
3285 my $pbat = $self->ensure_path($dir);
3286 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
3287 $pbat,$self->{r},$self->{pool});
Eric Wong44320b92007-01-13 22:35:53 -08003288 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003289 $self->chg_file($fbat, $m);
3290 $self->close_file($fbat,undef,$self->{pool});
3291}
3292
3293sub T { shift->M(@_) }
3294
3295sub change_file_prop {
3296 my ($self, $fbat, $pname, $pval) = @_;
3297 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
3298}
3299
3300sub chg_file {
3301 my ($self, $fbat, $m) = @_;
3302 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
3303 $self->change_file_prop($fbat,'svn:executable','*');
3304 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
3305 $self->change_file_prop($fbat,'svn:executable',undef);
3306 }
3307 my $fh = IO::File->new_tmpfile or croak $!;
3308 if ($m->{mode_b} =~ /^120/) {
3309 print $fh 'link ' or croak $!;
3310 $self->change_file_prop($fbat,'svn:special','*');
3311 } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
3312 $self->change_file_prop($fbat,'svn:special',undef);
3313 }
3314 defined(my $pid = fork) or croak $!;
3315 if (!$pid) {
3316 open STDOUT, '>&', $fh or croak $!;
3317 exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
3318 }
3319 waitpid $pid, 0;
3320 croak $? if $?;
3321 $fh->flush == 0 or croak $!;
3322 seek $fh, 0, 0 or croak $!;
3323
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08003324 my $exp = Git::SVN::Util::md5sum($fh);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003325 seek $fh, 0, 0 or croak $!;
3326
Eric Wongf7197df2006-10-14 15:48:35 -07003327 my $pool = SVN::Pool->new;
3328 my $atd = $self->apply_textdelta($fbat, undef, $pool);
3329 my $got = SVN::TxDelta::send_stream($fh, @$atd, $pool);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003330 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
Eric Wongf7197df2006-10-14 15:48:35 -07003331 $pool->clear;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003332
3333 close $fh or croak $!;
3334}
3335
3336sub D {
Eric Wong44320b92007-01-13 22:35:53 -08003337 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003338 my ($dir, $file) = split_path($m->{file_b});
3339 my $pbat = $self->ensure_path($dir);
Eric Wong44320b92007-01-13 22:35:53 -08003340 print "\tD\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003341 $self->delete_entry($m->{file_b}, $pbat);
3342}
3343
3344sub close_edit {
3345 my ($self) = @_;
3346 my ($p,$bat) = ($self->{pool}, $self->{bat});
3347 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
Eric Wong64427542007-05-19 02:58:37 -07003348 next if $_ eq '';
Eric Wonga5e0ced2006-06-12 15:23:48 -07003349 $self->close_directory($bat->{$_}, $p);
3350 }
Eric Wong64427542007-05-19 02:58:37 -07003351 $self->close_directory($bat->{''}, $p);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003352 $self->SUPER::close_edit($p);
3353 $p->clear;
3354}
3355
3356sub abort_edit {
3357 my ($self) = @_;
3358 $self->SUPER::abort_edit($self->{pool});
Eric Wong61395352007-01-27 14:33:08 -08003359}
3360
3361sub DESTROY {
3362 my $self = shift;
3363 $self->SUPER::DESTROY(@_);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003364 $self->{pool}->clear;
3365}
3366
Eric Wong44320b92007-01-13 22:35:53 -08003367# this drives the editor
3368sub apply_diff {
Eric Wong61395352007-01-27 14:33:08 -08003369 my ($self) = @_;
3370 my $mods = $self->{mods};
Eric Wong44320b92007-01-13 22:35:53 -08003371 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
Eric Wong6e8548c2007-01-27 01:32:00 -08003372 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
Eric Wong44320b92007-01-13 22:35:53 -08003373 my $f = $m->{chg};
3374 if (defined $o{$f}) {
3375 $self->$f($m);
3376 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +02003377 fatal("Invalid change type: $f");
Eric Wong44320b92007-01-13 22:35:53 -08003378 }
3379 }
Eric Wong24e22aa2007-01-29 00:07:49 -08003380 $self->rmdirs if $_rmdir;
Eric Wong6e8548c2007-01-27 01:32:00 -08003381 if (@$mods == 0) {
Eric Wong44320b92007-01-13 22:35:53 -08003382 $self->abort_edit;
3383 } else {
3384 $self->close_edit;
3385 }
Eric Wong6e8548c2007-01-27 01:32:00 -08003386 return scalar @$mods;
Eric Wong44320b92007-01-13 22:35:53 -08003387}
3388
Eric Wongd81bf822007-01-10 01:22:38 -08003389package Git::SVN::Ra;
Eric Wong6af1db42007-02-14 16:04:10 -08003390use vars qw/@ISA $config_dir $_log_window_size/;
Eric Wongd81bf822007-01-10 01:22:38 -08003391use strict;
3392use warnings;
Eric Wonga51cdb02007-09-07 04:00:40 -07003393my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
Eric Wongd81bf822007-01-10 01:22:38 -08003394
3395BEGIN {
3396 # enforce temporary pool usage for some simple functions
Sam Vilainc5f71ad2007-06-15 15:43:59 +12003397 no strict 'refs';
3398 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root/) {
3399 my $SUPER = "SUPER::$f";
3400 *$f = sub {
3401 my $self = shift;
3402 my $pool = SVN::Pool->new;
3403 my @ret = $self->$SUPER(@_,$pool);
3404 $pool->clear;
3405 wantarray ? @ret : $ret[0];
3406 };
Eric Wongd81bf822007-01-10 01:22:38 -08003407 }
Eric Wongd81bf822007-01-10 01:22:38 -08003408}
3409
Steven Walter9ff74e92007-09-28 13:24:19 -04003410sub _auth_providers () {
3411 [
3412 SVN::Client::get_simple_provider(),
3413 SVN::Client::get_ssl_server_trust_file_provider(),
3414 SVN::Client::get_simple_prompt_provider(
3415 \&Git::SVN::Prompt::simple, 2),
3416 SVN::Client::get_ssl_client_cert_file_provider(),
3417 SVN::Client::get_ssl_client_cert_prompt_provider(
3418 \&Git::SVN::Prompt::ssl_client_cert, 2),
3419 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
3420 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
3421 SVN::Client::get_username_provider(),
3422 SVN::Client::get_ssl_server_trust_prompt_provider(
3423 \&Git::SVN::Prompt::ssl_server_trust),
3424 SVN::Client::get_username_prompt_provider(
3425 \&Git::SVN::Prompt::username, 2)
3426 ]
3427}
3428
Eric Wongcfbe7ab2007-11-11 23:37:42 -08003429sub escape_uri_only {
3430 my ($uri) = @_;
3431 my @tmp;
3432 foreach (split m{/}, $uri) {
3433 s/([^\w.-])/sprintf("%%%02X",ord($1))/eg;
3434 push @tmp, $_;
3435 }
3436 join('/', @tmp);
3437}
3438
3439sub escape_url {
3440 my ($url) = @_;
3441 if ($url =~ m#^(https?)://([^/]+)(.*)$#) {
3442 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
3443 $url = "$scheme://$domain$uri";
3444 }
3445 $url;
3446}
3447
Eric Wongd81bf822007-01-10 01:22:38 -08003448sub new {
3449 my ($class, $url) = @_;
Eric Wongf6f09872007-01-18 18:22:18 -08003450 $url =~ s!/+$!!;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08003451 return $RA if ($RA && $RA->{url} eq $url);
Eric Wongf6f09872007-01-18 18:22:18 -08003452
Eric Wongd81bf822007-01-10 01:22:38 -08003453 SVN::_Core::svn_config_ensure($config_dir, undef);
Steven Walter9ff74e92007-09-28 13:24:19 -04003454 my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
Eric Wongd81bf822007-01-10 01:22:38 -08003455 my $config = SVN::Core::config_get_config($config_dir);
Eric Wong7730fbe2007-07-04 14:07:42 -07003456 $RA = undef;
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04003457 my $dont_store_passwords = 1;
3458 my $conf_t = ${$config}{'config'};
3459 {
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003460 no warnings 'once';
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04003461 # The usage of $SVN::_Core::SVN_CONFIG_* variables
3462 # produces warnings that variables are used only once.
3463 # I had not found the better way to shut them up, so
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003464 # the warnings of type 'once' are disabled in this block.
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04003465 if (SVN::_Core::svn_config_get_bool($conf_t,
3466 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
3467 $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
3468 1) == 0) {
3469 SVN::_Core::svn_auth_set_parameter($baton,
3470 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
3471 bless (\$dont_store_passwords, "_p_void"));
3472 }
3473 if (SVN::_Core::svn_config_get_bool($conf_t,
3474 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
3475 $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
3476 1) == 0) {
3477 $Git::SVN::Prompt::_no_auth_cache = 1;
3478 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003479 } # no warnings 'once'
Eric Wongcfbe7ab2007-11-11 23:37:42 -08003480 my $self = SVN::Ra->new(url => escape_url($url), auth => $baton,
Eric Wongd81bf822007-01-10 01:22:38 -08003481 config => $config,
3482 pool => SVN::Pool->new,
3483 auth_provider_callbacks => $callbacks);
Eric Wongcfbe7ab2007-11-11 23:37:42 -08003484 $self->{url} = $url;
Eric Wongd81bf822007-01-10 01:22:38 -08003485 $self->{svn_path} = $url;
3486 $self->{repos_root} = $self->get_repos_root;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003487 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
Eric Wong0dc03d62007-05-13 01:04:43 -07003488 $self->{cache} = { check_path => { r => 0, data => {} },
3489 get_dir => { r => 0, data => {} } };
Eric Wong5d3b7cd2007-01-29 19:16:01 -08003490 $RA = bless $self, $class;
Eric Wongd81bf822007-01-10 01:22:38 -08003491}
3492
Eric Wong0dc03d62007-05-13 01:04:43 -07003493sub check_path {
3494 my ($self, $path, $r) = @_;
3495 my $cache = $self->{cache}->{check_path};
3496 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
3497 return $cache->{data}->{$path};
3498 }
3499 my $pool = SVN::Pool->new;
3500 my $t = $self->SUPER::check_path($path, $r, $pool);
3501 $pool->clear;
3502 if ($r != $cache->{r}) {
3503 %{$cache->{data}} = ();
3504 $cache->{r} = $r;
3505 }
3506 $cache->{data}->{$path} = $t;
3507}
3508
3509sub get_dir {
3510 my ($self, $dir, $r) = @_;
3511 my $cache = $self->{cache}->{get_dir};
3512 if ($r == $cache->{r}) {
3513 if (my $x = $cache->{data}->{$dir}) {
3514 return wantarray ? @$x : $x->[0];
3515 }
3516 }
3517 my $pool = SVN::Pool->new;
3518 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
3519 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
3520 $pool->clear;
3521 if ($r != $cache->{r}) {
3522 %{$cache->{data}} = ();
3523 $cache->{r} = $r;
3524 }
3525 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
3526 wantarray ? (\%dirents, $r, $props) : \%dirents;
3527}
3528
Eric Wongd81bf822007-01-10 01:22:38 -08003529sub DESTROY {
Eric Wong5d3b7cd2007-01-29 19:16:01 -08003530 # do not call the real DESTROY since we store ourselves in $RA
Eric Wongd81bf822007-01-10 01:22:38 -08003531}
3532
Eric Wongd81bf822007-01-10 01:22:38 -08003533sub get_log {
3534 my ($self, @args) = @_;
3535 my $pool = SVN::Pool->new;
Eric Wongd81bf822007-01-10 01:22:38 -08003536 splice(@args, 3, 1) if ($SVN::Core::VERSION le '1.2.0');
3537 my $ret = $self->SUPER::get_log(@args, $pool);
3538 $pool->clear;
3539 $ret;
3540}
3541
Steven Walter9ff74e92007-09-28 13:24:19 -04003542sub trees_match {
3543 my ($self, $url1, $rev1, $url2, $rev2) = @_;
3544 my $ctx = SVN::Client->new(auth => _auth_providers);
3545 my $out = IO::File->new_tmpfile;
3546
3547 # older SVN (1.1.x) doesn't take $pool as the last parameter for
3548 # $ctx->diff(), so we'll create a default one
3549 my $pool = SVN::Pool->new_default_sub;
3550
3551 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
3552 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
3553 $out->flush;
3554 my $ret = (($out->stat)[7] == 0);
3555 close $out or croak $!;
3556
3557 $ret;
3558}
3559
Eric Wongd81bf822007-01-10 01:22:38 -08003560sub get_commit_editor {
Eric Wong44320b92007-01-13 22:35:53 -08003561 my ($self, $log, $cb, $pool) = @_;
Eric Wongd81bf822007-01-10 01:22:38 -08003562 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
Eric Wong44320b92007-01-13 22:35:53 -08003563 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08003564}
3565
Eric Wongd81bf822007-01-10 01:22:38 -08003566sub gs_do_update {
Eric Wong8a603772007-01-31 02:45:50 -08003567 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
3568 my $new = ($rev_a == $rev_b);
3569 my $path = $gs->{path};
3570
Eric Wong2e5e2482007-02-23 02:21:59 -08003571 if ($new && -e $gs->{index}) {
3572 unlink $gs->{index} or die
3573 "Couldn't unlink index: $gs->{index}: $!\n";
3574 }
Eric Wongd81bf822007-01-10 01:22:38 -08003575 my $pool = SVN::Pool->new;
Eric Wong8b8fc062007-01-22 11:44:57 -08003576 $editor->set_path_strip($path);
Eric Wong2b27f6c2007-01-28 04:59:05 -08003577 my (@pc) = split m#/#, $path;
3578 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
Eric Wong8a603772007-01-31 02:45:50 -08003579 1, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08003580 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong2b27f6c2007-01-28 04:59:05 -08003581
3582 # Since we can't rely on svn_ra_reparent being available, we'll
3583 # just have to do some magic with set_path to make it so
3584 # we only want a partial path.
3585 my $sp = '';
3586 my $final = join('/', @pc);
3587 while (@pc) {
3588 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
3589 $sp .= '/' if length $sp;
3590 $sp .= shift @pc;
3591 }
3592 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
3593
Eric Wong2b27f6c2007-01-28 04:59:05 -08003594 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
3595
Eric Wongd81bf822007-01-10 01:22:38 -08003596 $reporter->finish_report($pool);
3597 $pool->clear;
3598 $editor->{git_commit_ok};
3599}
3600
Eric Wong2b27f6c2007-01-28 04:59:05 -08003601# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
3602# svn_ra_reparent didn't work before 1.4)
Eric Wongd81bf822007-01-10 01:22:38 -08003603sub gs_do_switch {
Eric Wong8a603772007-01-31 02:45:50 -08003604 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
3605 my $path = $gs->{path};
Eric Wongd81bf822007-01-10 01:22:38 -08003606 my $pool = SVN::Pool->new;
Eric Wong2b27f6c2007-01-28 04:59:05 -08003607
3608 my $full_url = $self->{url};
3609 my $old_url = $full_url;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08003610 $full_url .= '/' . escape_uri_only($path) if length $path;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08003611 my ($ra, $reparented);
3612 if ($old_url ne $full_url) {
3613 if ($old_url !~ m#^svn(\+ssh)?://#) {
3614 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url,
3615 $pool);
3616 $self->{url} = $full_url;
3617 $reparented = 1;
3618 } else {
Eric Wonga51cdb02007-09-07 04:00:40 -07003619 $_[0] = undef;
3620 $self = undef;
3621 $RA = undef;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08003622 $ra = Git::SVN::Ra->new($full_url);
Eric Wonga51cdb02007-09-07 04:00:40 -07003623 $ra_invalid = 1;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08003624 }
3625 }
3626 $ra ||= $self;
Eric Wong8a603772007-01-31 02:45:50 -08003627 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08003628 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong8b8fc062007-01-22 11:44:57 -08003629 $reporter->set_path('', $rev_a, 0, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08003630 $reporter->finish_report($pool);
Eric Wong2b27f6c2007-01-28 04:59:05 -08003631
Eric Wong5d3b7cd2007-01-29 19:16:01 -08003632 if ($reparented) {
3633 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
3634 $self->{url} = $old_url;
3635 }
Eric Wong2b27f6c2007-01-28 04:59:05 -08003636
Eric Wongd81bf822007-01-10 01:22:38 -08003637 $pool->clear;
3638 $editor->{git_commit_ok};
3639}
3640
Eric Wongb54a9012007-06-13 02:37:03 -07003641sub longest_common_path {
3642 my ($gsv, $globs) = @_;
Eric Wongd2ae1432007-02-07 11:50:16 -08003643 my %common;
Eric Wonge5181922007-02-08 12:53:57 -08003644 my $common_max = scalar @$gsv;
3645
3646 foreach my $gs (@$gsv) {
Eric Wongd2ae1432007-02-07 11:50:16 -08003647 my @tmp = split m#/#, $gs->{path};
3648 my $p = '';
3649 foreach (@tmp) {
3650 $p .= length($p) ? "/$_" : $_;
3651 $common{$p} ||= 0;
3652 $common{$p}++;
3653 }
3654 }
Eric Wonge5181922007-02-08 12:53:57 -08003655 $globs ||= [];
3656 $common_max += scalar @$globs;
3657 foreach my $glob (@$globs) {
3658 my @tmp = split m#/#, $glob->{path}->{left};
3659 my $p = '';
3660 foreach (@tmp) {
3661 $p .= length($p) ? "/$_" : $_;
3662 $common{$p} ||= 0;
3663 $common{$p}++;
3664 }
3665 }
3666
Eric Wongd2ae1432007-02-07 11:50:16 -08003667 my $longest_path = '';
3668 foreach (sort {length $b <=> length $a} keys %common) {
Eric Wonge5181922007-02-08 12:53:57 -08003669 if ($common{$_} == $common_max) {
Eric Wongd2ae1432007-02-07 11:50:16 -08003670 $longest_path = $_;
3671 last;
3672 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08003673 }
Eric Wongb54a9012007-06-13 02:37:03 -07003674 $longest_path;
3675}
3676
3677sub gs_fetch_loop_common {
3678 my ($self, $base, $head, $gsv, $globs) = @_;
3679 return if ($base > $head);
3680 my $inc = $_log_window_size;
3681 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
3682 my $longest_path = longest_common_path($gsv, $globs);
Eric Wonga51cdb02007-09-07 04:00:40 -07003683 my $ra_url = $self->{url};
Eric Wong0af9c9f2007-01-27 22:28:56 -08003684 while (1) {
Eric Wongd4eff2b2007-01-30 14:04:22 -08003685 my %revs;
Eric Wongd2ae1432007-02-07 11:50:16 -08003686 my $err;
Eric Wongf7c3fc42007-01-29 18:34:55 -08003687 my $err_handler = $SVN::Error::handler;
Eric Wongd2ae1432007-02-07 11:50:16 -08003688 $SVN::Error::handler = sub {
3689 ($err) = @_;
3690 skip_unknown_revs($err);
3691 };
3692 sub _cb {
3693 my ($paths, $r, $author, $date, $log) = @_;
3694 [ dup_changed_paths($paths),
3695 { author => $author, date => $date, log => $log } ];
3696 }
3697 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
3698 sub { $revs{$_[1]} = _cb(@_) });
3699 if ($err && $max >= $head) {
3700 print STDERR "Path '$longest_path' ",
3701 "was probably deleted:\n",
3702 $err->expanded_message,
3703 "\nWill attempt to follow ",
3704 "revisions r$min .. r$max ",
3705 "committed before the deletion\n";
3706 my $hi = $max;
3707 while (--$hi >= $min) {
3708 my $ok;
3709 $self->get_log([$longest_path], $min, $hi,
3710 0, 1, 1, sub {
3711 $ok ||= $_[1];
3712 $revs{$_[1]} = _cb(@_) });
3713 if ($ok) {
3714 print STDERR "r$min .. r$ok OK\n";
3715 last;
3716 }
3717 }
3718 }
Eric Wongd4eff2b2007-01-30 14:04:22 -08003719 $SVN::Error::handler = $err_handler;
Eric Wongfbcc1732007-02-06 18:35:30 -08003720
Eric Wonge5181922007-02-08 12:53:57 -08003721 my %exists = map { $_->{path} => $_ } @$gsv;
Eric Wongd4eff2b2007-01-30 14:04:22 -08003722 foreach my $r (sort {$a <=> $b} keys %revs) {
Eric Wongfbcc1732007-02-06 18:35:30 -08003723 my ($paths, $logged) = @{$revs{$r}};
Eric Wonge5181922007-02-08 12:53:57 -08003724
3725 foreach my $gs ($self->match_globs(\%exists, $paths,
3726 $globs, $r)) {
Eric Wongfbcc1732007-02-06 18:35:30 -08003727 if ($gs->rev_db_max >= $r) {
3728 next;
3729 }
3730 next unless $gs->match_paths($paths, $r);
3731 $gs->{logged_rev_props} = $logged;
Eric Wonge8d120b2007-02-14 16:29:52 -08003732 if (my $last_commit = $gs->last_commit) {
3733 $gs->assert_index_clean($last_commit);
3734 }
Eric Wongfbcc1732007-02-06 18:35:30 -08003735 my $log_entry = $gs->do_fetch($paths, $r);
3736 if ($log_entry) {
Eric Wong0af9c9f2007-01-27 22:28:56 -08003737 $gs->do_git_commit($log_entry);
3738 }
3739 }
Eric Wonge5181922007-02-08 12:53:57 -08003740 foreach my $g (@$globs) {
Eric Wong93f26892007-02-11 01:20:26 -08003741 my $k = "svn-remote.$g->{remote}." .
3742 "$g->{t}-maxRev";
3743 Git::SVN::tmp_config($k, $r);
Eric Wonge5181922007-02-08 12:53:57 -08003744 }
Eric Wonga51cdb02007-09-07 04:00:40 -07003745 if ($ra_invalid) {
3746 $_[0] = undef;
3747 $self = undef;
3748 $RA = undef;
3749 $self = Git::SVN::Ra->new($ra_url);
3750 $ra_invalid = undef;
3751 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08003752 }
Eric Wong9c93fee2007-01-31 17:22:31 -08003753 # pre-fill the .rev_db since it'll eventually get filled in
3754 # with '0' x40 if something new gets committed
Eric Wonge5181922007-02-08 12:53:57 -08003755 foreach my $gs (@$gsv) {
Eric Wong9c93fee2007-01-31 17:22:31 -08003756 next if defined $gs->rev_db_get($max);
3757 $gs->rev_db_set($max, 0 x40);
3758 }
Eric Wongc3560e52007-02-12 16:03:32 -08003759 foreach my $g (@$globs) {
3760 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
3761 Git::SVN::tmp_config($k, $max);
3762 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08003763 last if $max >= $head;
3764 $min = $max + 1;
3765 $max += $inc;
3766 $max = $head if ($max > $head);
3767 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08003768}
3769
Eric Wonge5181922007-02-08 12:53:57 -08003770sub match_globs {
3771 my ($self, $exists, $paths, $globs, $r) = @_;
Eric Wong74a81222007-02-10 13:28:50 -08003772
3773 sub get_dir_check {
3774 my ($self, $exists, $g, $r) = @_;
3775 my @x = eval { $self->get_dir($g->{path}->{left}, $r) };
3776 return unless scalar @x == 3;
3777 my $dirents = $x[0];
3778 foreach my $de (keys %$dirents) {
Eric Wong0dc03d62007-05-13 01:04:43 -07003779 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
Eric Wong74a81222007-02-10 13:28:50 -08003780 my $p = $g->{path}->full_path($de);
3781 next if $exists->{$p};
3782 next if (length $g->{path}->{right} &&
3783 ($self->check_path($p, $r) !=
3784 $SVN::Node::dir));
3785 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
3786 $g->{ref}->full_path($de), 1);
3787 }
3788 }
Eric Wonge5181922007-02-08 12:53:57 -08003789 foreach my $g (@$globs) {
Eric Wong74a81222007-02-10 13:28:50 -08003790 if (my $path = $paths->{"/$g->{path}->{left}"}) {
3791 if ($path->{action} =~ /^[AR]$/) {
3792 get_dir_check($self, $exists, $g, $r);
3793 }
3794 }
Eric Wonge5181922007-02-08 12:53:57 -08003795 foreach (keys %$paths) {
Eric Wong28710f72007-02-14 13:32:21 -08003796 if (/$g->{path}->{left_regex}/ &&
3797 !/$g->{path}->{regex}/) {
Eric Wong74a81222007-02-10 13:28:50 -08003798 next if $paths->{$_}->{action} !~ /^[AR]$/;
3799 get_dir_check($self, $exists, $g, $r);
3800 }
Eric Wonge5181922007-02-08 12:53:57 -08003801 next unless /$g->{path}->{regex}/;
3802 my $p = $1;
3803 my $pathname = $g->{path}->full_path($p);
3804 next if $exists->{$pathname};
Eric Wong0c1ec5a2007-04-18 00:17:33 -07003805 next if ($self->check_path($pathname, $r) !=
3806 $SVN::Node::dir);
Eric Wonge5181922007-02-08 12:53:57 -08003807 $exists->{$pathname} = Git::SVN->init(
3808 $self->{url}, $pathname, undef,
3809 $g->{ref}->full_path($p), 1);
3810 }
3811 my $c = '';
3812 foreach (split m#/#, $g->{path}->{left}) {
3813 $c .= "/$_";
3814 next unless ($paths->{$c} &&
Eric Wong74a81222007-02-10 13:28:50 -08003815 ($paths->{$c}->{action} =~ /^[AR]$/));
3816 get_dir_check($self, $exists, $g, $r);
Eric Wonge5181922007-02-08 12:53:57 -08003817 }
3818 }
3819 values %$exists;
3820}
3821
Eric Wonge6434f82007-01-23 16:29:23 -08003822sub minimize_url {
3823 my ($self) = @_;
3824 return $self->{url} if ($self->{url} eq $self->{repos_root});
3825 my $url = $self->{repos_root};
3826 my @components = split(m!/!, $self->{svn_path});
3827 my $c = '';
3828 do {
3829 $url .= "/$c" if length $c;
3830 eval { (ref $self)->new($url)->get_latest_revnum };
3831 } while ($@ && ($c = shift @components));
3832 $url;
3833}
3834
Eric Wongd81bf822007-01-10 01:22:38 -08003835sub can_do_switch {
3836 my $self = shift;
3837 unless (defined $can_do_switch) {
3838 my $pool = SVN::Pool->new;
3839 my $rep = eval {
3840 $self->do_switch(1, '', 0, $self->{url},
3841 SVN::Delta::Editor->new, $pool);
3842 };
3843 if ($@) {
3844 $can_do_switch = 0;
3845 } else {
3846 $rep->abort_report($pool);
3847 $can_do_switch = 1;
3848 }
3849 $pool->clear;
3850 }
3851 $can_do_switch;
3852}
3853
Eric Wong0af9c9f2007-01-27 22:28:56 -08003854sub skip_unknown_revs {
3855 my ($err) = @_;
3856 my $errno = $err->apr_err();
3857 # Maybe the branch we're tracking didn't
3858 # exist when the repo started, so it's
3859 # not an error if it doesn't, just continue
3860 #
3861 # Wonderfully consistent library, eh?
3862 # 160013 - svn:// and file://
3863 # 175002 - http(s)://
3864 # 175007 - http(s):// (this repo required authorization, too...)
3865 # More codes may be discovered later...
3866 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
Eric Wonga6a15a92007-03-30 17:54:48 -07003867 my $err_key = $err->expanded_message;
3868 # revision numbers change every time, filter them out
3869 $err_key =~ s/\d+/\0/g;
3870 $err_key = "$errno\0$err_key";
3871 unless ($ignored_err{$err_key}) {
3872 warn "W: Ignoring error from SVN, path probably ",
3873 "does not exist: ($errno): ",
3874 $err->expanded_message,"\n";
3875 $ignored_err{$err_key} = 1;
3876 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08003877 return;
3878 }
3879 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
3880}
3881
3882# svn_log_changed_path_t objects passed to get_log are likely to be
3883# overwritten even if only the refs are copied to an external variable,
3884# so we should dup the structures in their entirety. Using an externally
3885# passed pool (instead of our temporary and quickly cleared pool in
3886# Git::SVN::Ra) does not help matters at all...
3887sub dup_changed_paths {
3888 my ($paths) = @_;
3889 return undef unless $paths;
3890 my %ret;
3891 foreach my $p (keys %$paths) {
3892 my $i = $paths->{$p};
3893 my %s = map { $_ => $i->$_ }
3894 qw/copyfrom_path copyfrom_rev action/;
3895 $ret{$p} = \%s;
3896 }
3897 \%ret;
3898}
3899
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003900package Git::SVN::Log;
3901use strict;
3902use warnings;
3903use POSIX qw/strftime/;
David D Kilzer111947e2007-11-11 22:56:52 -08003904use constant commit_log_separator => ('-' x 72) . "\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003905use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
3906 %rusers $show_commit $incremental/;
3907my $l_fmt;
3908
3909sub cmt_showable {
3910 my ($c) = @_;
3911 return 1 if defined $c->{r};
Eric Wongc16d0872007-04-08 00:59:22 -07003912
3913 # big commit message got truncated by the 16k pretty buffer in rev-list
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003914 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
3915 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
Eric Wongc16d0872007-04-08 00:59:22 -07003916 @{$c->{l}} = ();
Eric Wong44320b92007-01-13 22:35:53 -08003917 my @log = command(qw/cat-file commit/, $c->{c});
Eric Wongc16d0872007-04-08 00:59:22 -07003918
3919 # shift off the headers
3920 shift @log while ($log[0] ne '');
Eric Wong44320b92007-01-13 22:35:53 -08003921 shift @log;
Eric Wongc16d0872007-04-08 00:59:22 -07003922
3923 # TODO: make $c->{l} not have a trailing newline in the future
3924 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003925
3926 (undef, $c->{r}, undef) = ::extract_metadata(
Eric Wong44320b92007-01-13 22:35:53 -08003927 (grep(/^git-svn-id: /, @log))[-1]);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003928 }
3929 return defined $c->{r};
3930}
3931
3932sub log_use_color {
3933 return 1 if $color;
3934 my ($dc, $dcvar);
3935 $dcvar = 'color.diff';
3936 $dc = `git-config --get $dcvar`;
3937 if ($dc eq '') {
3938 # nothing at all; fallback to "diff.color"
3939 $dcvar = 'diff.color';
3940 $dc = `git-config --get $dcvar`;
3941 }
3942 chomp($dc);
3943 if ($dc eq 'auto') {
3944 my $pc;
3945 $pc = `git-config --get color.pager`;
3946 if ($pc eq '') {
3947 # does not have it -- fallback to pager.color
3948 $pc = `git-config --bool --get pager.color`;
3949 }
3950 else {
3951 $pc = `git-config --bool --get color.pager`;
3952 if ($?) {
3953 $pc = 'false';
3954 }
3955 }
3956 chomp($pc);
3957 if (-t *STDOUT || (defined $pager && $pc eq 'true')) {
3958 return ($ENV{TERM} && $ENV{TERM} ne 'dumb');
3959 }
3960 return 0;
3961 }
3962 return 0 if $dc eq 'never';
3963 return 1 if $dc eq 'always';
3964 chomp($dc = `git-config --bool --get $dcvar`);
3965 return ($dc eq 'true');
3966}
3967
3968sub git_svn_log_cmd {
Eric Wong3bc718b2007-02-13 17:09:40 -08003969 my ($r_min, $r_max, @args) = @_;
3970 my $head = 'HEAD';
Eric Wong6ed77262007-08-15 09:55:18 -07003971 my (@files, @log_opts);
Eric Wong3bc718b2007-02-13 17:09:40 -08003972 foreach my $x (@args) {
Eric Wong6ed77262007-08-15 09:55:18 -07003973 if ($x eq '--' || @files) {
3974 push @files, $x;
3975 } else {
3976 if (::verify_ref("$x^0")) {
3977 $head = $x;
3978 } else {
3979 push @log_opts, $x;
3980 }
3981 }
Eric Wong3bc718b2007-02-13 17:09:40 -08003982 }
3983
Eric Wong13c823f2007-04-08 00:59:19 -07003984 my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
3985 $gs ||= Git::SVN->_new;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003986 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
3987 $gs->refname);
3988 push @cmd, '-r' unless $non_recursive;
3989 push @cmd, qw/--raw --name-status/ if $verbose;
3990 push @cmd, '--color' if log_use_color();
Eric Wong6ed77262007-08-15 09:55:18 -07003991 push @cmd, @log_opts;
3992 if (defined $r_max && $r_max == $r_min) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003993 push @cmd, '--max-count=1';
3994 if (my $c = $gs->rev_db_get($r_max)) {
3995 push @cmd, $c;
3996 }
Eric Wong6ed77262007-08-15 09:55:18 -07003997 } elsif (defined $r_max) {
David D Kilzer111947e2007-11-11 22:56:52 -08003998 if ($r_max < $r_min) {
3999 ($r_min, $r_max) = ($r_max, $r_min);
4000 }
4001 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
4002 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
4003 # If there are no commits in the range, both $c_max and $c_min
4004 # will be undefined. If there is at least 1 commit in the
4005 # range, both will be defined.
4006 return () if !defined $c_min || !defined $c_max;
4007 if ($c_min eq $c_max) {
4008 push @cmd, '--max-count=1', $c_min;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004009 } else {
David D Kilzer111947e2007-11-11 22:56:52 -08004010 push @cmd, '--boundary', "$c_min..$c_max";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004011 }
4012 }
Eric Wong6ed77262007-08-15 09:55:18 -07004013 return (@cmd, @files);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004014}
4015
4016# adapted from pager.c
4017sub config_pager {
4018 $pager ||= $ENV{GIT_PAGER} || $ENV{PAGER};
4019 if (!defined $pager) {
4020 $pager = 'less';
4021 } elsif (length $pager == 0 || $pager eq 'cat') {
4022 $pager = undef;
4023 }
4024}
4025
4026sub run_pager {
Eric Wongb0193042007-09-21 18:48:45 -07004027 return unless -t *STDOUT && defined $pager;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004028 pipe my $rfd, my $wfd or return;
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004029 defined(my $pid = fork) or ::fatal "Can't fork: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004030 if (!$pid) {
4031 open STDOUT, '>&', $wfd or
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004032 ::fatal "Can't redirect to stdout: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004033 return;
4034 }
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004035 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004036 $ENV{LESS} ||= 'FRSX';
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004037 exec $pager or ::fatal "Can't run pager: $! ($pager)";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004038}
4039
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004040sub format_svn_date {
4041 return strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)", localtime(shift));
4042}
4043
4044sub parse_git_date {
4045 my ($t, $tz) = @_;
4046 # Date::Parse isn't in the standard Perl distro :(
4047 if ($tz =~ s/^\+//) {
4048 $t += tz_to_s_offset($tz);
4049 } elsif ($tz =~ s/^\-//) {
4050 $t -= tz_to_s_offset($tz);
4051 }
4052 return $t;
4053}
4054
4055sub set_local_timezone {
4056 if (defined $TZ) {
4057 $ENV{TZ} = $TZ;
4058 } else {
4059 delete $ENV{TZ};
4060 }
4061}
4062
Eric Wong21819a32007-01-27 14:38:10 -08004063sub tz_to_s_offset {
4064 my ($tz) = @_;
4065 $tz =~ s/(\d\d)$//;
4066 return ($1 * 60) + ($tz * 3600);
4067}
4068
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004069sub get_author_info {
4070 my ($dest, $author, $t, $tz) = @_;
4071 $author =~ s/(?:^\s*|\s*$)//g;
4072 $dest->{a_raw} = $author;
4073 my $au;
Eric Wong1c8443b2007-01-14 02:17:00 -08004074 if ($::_authors) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004075 $au = $rusers{$author} || undef;
4076 }
4077 if (!$au) {
4078 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
4079 }
4080 $dest->{t} = $t;
4081 $dest->{tz} = $tz;
4082 $dest->{a} = $au;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004083 $dest->{t_utc} = parse_git_date($t, $tz);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004084}
4085
4086sub process_commit {
4087 my ($c, $r_min, $r_max, $defer) = @_;
4088 if (defined $r_min && defined $r_max) {
4089 if ($r_min == $c->{r} && $r_min == $r_max) {
4090 show_commit($c);
4091 return 0;
4092 }
4093 return 1 if $r_min == $r_max;
4094 if ($r_min < $r_max) {
4095 # we need to reverse the print order
4096 return 0 if (defined $limit && --$limit < 0);
4097 push @$defer, $c;
4098 return 1;
4099 }
4100 if ($r_min != $r_max) {
4101 return 1 if ($r_min < $c->{r});
4102 return 1 if ($r_max > $c->{r});
4103 }
4104 }
4105 return 0 if (defined $limit && --$limit < 0);
4106 show_commit($c);
4107 return 1;
4108}
4109
4110sub show_commit {
4111 my $c = shift;
4112 if ($oneline) {
4113 my $x = "\n";
4114 if (my $l = $c->{l}) {
4115 while ($l->[0] =~ /^\s*$/) { shift @$l }
4116 $x = $l->[0];
4117 }
4118 $l_fmt ||= 'A' . length($c->{r});
4119 print 'r',pack($l_fmt, $c->{r}),' | ';
4120 print "$c->{c} | " if $show_commit;
4121 print $x;
4122 } else {
4123 show_commit_normal($c);
4124 }
4125}
4126
4127sub show_commit_changed_paths {
4128 my ($c) = @_;
4129 return unless $c->{changed};
4130 print "Changed paths:\n", @{$c->{changed}};
4131}
4132
4133sub show_commit_normal {
4134 my ($c) = @_;
David D Kilzer111947e2007-11-11 22:56:52 -08004135 print commit_log_separator, "r$c->{r} | ";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004136 print "$c->{c} | " if $show_commit;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004137 print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004138 my $nr_line = 0;
4139
4140 if (my $l = $c->{l}) {
4141 while ($l->[$#$l] eq "\n" && $#$l > 0
4142 && $l->[($#$l - 1)] eq "\n") {
4143 pop @$l;
4144 }
4145 $nr_line = scalar @$l;
4146 if (!$nr_line) {
4147 print "1 line\n\n\n";
4148 } else {
4149 if ($nr_line == 1) {
4150 $nr_line = '1 line';
4151 } else {
4152 $nr_line .= ' lines';
4153 }
4154 print $nr_line, "\n";
4155 show_commit_changed_paths($c);
4156 print "\n";
4157 print $_ foreach @$l;
4158 }
4159 } else {
4160 print "1 line\n";
4161 show_commit_changed_paths($c);
4162 print "\n";
4163
4164 }
Eric Wong488a63e2007-02-15 00:40:42 -08004165 foreach my $x (qw/raw stat diff/) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004166 if ($c->{$x}) {
4167 print "\n";
4168 print $_ foreach @{$c->{$x}}
4169 }
4170 }
4171}
4172
4173sub cmd_show_log {
4174 my (@args) = @_;
4175 my ($r_min, $r_max);
4176 my $r_last = -1; # prevent dupes
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004177 set_local_timezone();
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004178 if (defined $::_revision) {
4179 if ($::_revision =~ /^(\d+):(\d+)$/) {
4180 ($r_min, $r_max) = ($1, $2);
4181 } elsif ($::_revision =~ /^\d+$/) {
4182 $r_min = $r_max = $::_revision;
4183 } else {
4184 ::fatal "-r$::_revision is not supported, use ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004185 "standard 'git log' arguments instead";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004186 }
4187 }
4188
4189 config_pager();
Eric Wong6ed77262007-08-15 09:55:18 -07004190 @args = git_svn_log_cmd($r_min, $r_max, @args);
David D Kilzer111947e2007-11-11 22:56:52 -08004191 if (!@args) {
4192 print commit_log_separator unless $incremental || $oneline;
4193 return;
4194 }
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004195 my $log = command_output_pipe(@args);
4196 run_pager();
Eric Wong488a63e2007-02-15 00:40:42 -08004197 my (@k, $c, $d, $stat);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004198 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
4199 while (<$log>) {
David D Kilzer60f3ff12007-11-10 22:10:34 -08004200 if (/^${esc_color}commit -?($::sha1_short)/o) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004201 my $cmt = $1;
4202 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
4203 $r_last = $c->{r};
4204 process_commit($c, $r_min, $r_max, \@k) or
4205 goto out;
4206 }
4207 $d = undef;
4208 $c = { c => $cmt };
4209 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
4210 get_author_info($c, $1, $2, $3);
4211 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
4212 # ignore
4213 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
4214 push @{$c->{raw}}, $_;
4215 } elsif (/^${esc_color}[ACRMDT]\t/) {
4216 # we could add $SVN->{svn_path} here, but that requires
4217 # remote access at the moment (repo_path_split)...
4218 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
4219 push @{$c->{changed}}, $_;
4220 } elsif (/^${esc_color}diff /o) {
4221 $d = 1;
4222 push @{$c->{diff}}, $_;
4223 } elsif ($d) {
4224 push @{$c->{diff}}, $_;
Eric Wong488a63e2007-02-15 00:40:42 -08004225 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
4226 $esc_color*[\+\-]*$esc_color$/x) {
4227 $stat = 1;
4228 push @{$c->{stat}}, $_;
4229 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
4230 push @{$c->{stat}}, $_;
4231 $stat = undef;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004232 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
4233 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
4234 } elsif (s/^${esc_color} //o) {
4235 push @{$c->{l}}, $_;
4236 }
4237 }
4238 if ($c && defined $c->{r} && $c->{r} != $r_last) {
4239 $r_last = $c->{r};
4240 process_commit($c, $r_min, $r_max, \@k);
4241 }
4242 if (@k) {
David D Kilzer111947e2007-11-11 22:56:52 -08004243 ($r_min, $r_max) = ($r_max, $r_min);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004244 process_commit($_, $r_min, $r_max) foreach reverse @k;
4245 }
4246out:
Eric Wongc843c462007-01-12 03:07:31 -08004247 close $log;
David D Kilzer111947e2007-11-11 22:56:52 -08004248 print commit_log_separator unless $incremental || $oneline;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004249}
4250
Eric Wong706587f2007-01-18 17:50:01 -08004251package Git::SVN::Migration;
4252# these version numbers do NOT correspond to actual version numbers
4253# of git nor git-svn. They are just relative.
4254#
4255# v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
4256#
4257# v1 layout: .git/$id/info/url, refs/remotes/$id
4258#
4259# v2 layout: .git/svn/$id/info/url, refs/remotes/$id
4260#
4261# v3 layout: .git/svn/$id, refs/remotes/$id
4262# - info/url may remain for backwards compatibility
4263# - this is what we migrate up to this layout automatically,
4264# - this will be used by git svn init on single branches
Eric Wong26a62d52007-02-12 13:25:25 -08004265# v3.1 layout (auto migrated):
4266# - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
4267# for backwards compatibility
Eric Wong706587f2007-01-18 17:50:01 -08004268#
4269# v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
4270# - this is only created for newly multi-init-ed
4271# repositories. Similar in spirit to the
4272# --use-separate-remotes option in git-clone (now default)
4273# - we do not automatically migrate to this (following
4274# the example set by core git)
4275use strict;
4276use warnings;
4277use Carp qw/croak/;
4278use File::Path qw/mkpath/;
Eric Wong47e39c52007-01-21 04:27:09 -08004279use File::Basename qw/dirname basename/;
4280use vars qw/$_minimize/;
Eric Wong706587f2007-01-18 17:50:01 -08004281
4282sub migrate_from_v0 {
4283 my $git_dir = $ENV{GIT_DIR};
4284 return undef unless -d $git_dir;
4285 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
4286 my $migrated = 0;
4287 while (<$fh>) {
4288 chomp;
4289 my ($id, $orig_ref) = ($_, $_);
4290 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
4291 next unless -f "$git_dir/$id/info/url";
4292 my $new_ref = "refs/remotes/$id";
4293 if (::verify_ref("$new_ref^0")) {
4294 print STDERR "W: $orig_ref is probably an old ",
4295 "branch used by an ancient version of ",
4296 "git-svn.\n",
4297 "However, $new_ref also exists.\n",
4298 "We will not be able ",
4299 "to use this branch until this ",
4300 "ambiguity is resolved.\n";
4301 next;
4302 }
4303 print STDERR "Migrating from v0 layout...\n" if !$migrated;
4304 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
4305 command_noisy('update-ref', $new_ref, $orig_ref);
4306 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
4307 $migrated++;
4308 }
4309 command_close_pipe($fh, $ctx);
4310 print STDERR "Done migrating from v0 layout...\n" if $migrated;
4311 $migrated;
4312}
4313
4314sub migrate_from_v1 {
4315 my $git_dir = $ENV{GIT_DIR};
4316 my $migrated = 0;
4317 return $migrated unless -d $git_dir;
4318 my $svn_dir = "$git_dir/svn";
4319
4320 # just in case somebody used 'svn' as their $id at some point...
4321 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
4322
4323 print STDERR "Migrating from a git-svn v1 layout...\n";
4324 mkpath([$svn_dir]);
4325 print STDERR "Data from a previous version of git-svn exists, but\n\t",
4326 "$svn_dir\n\t(required for this version ",
4327 "($::VERSION) of git-svn) does not. exist\n";
4328 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
4329 while (<$fh>) {
4330 my $x = $_;
4331 next unless $x =~ s#^refs/remotes/##;
4332 chomp $x;
4333 next unless -f "$git_dir/$x/info/url";
4334 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
4335 next unless $u;
4336 my $dn = dirname("$git_dir/svn/$x");
4337 mkpath([$dn]) unless -d $dn;
4338 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
4339 mkpath(["$git_dir/svn/svn"]);
4340 print STDERR " - $git_dir/$x/info => ",
4341 "$git_dir/svn/$x/info\n";
4342 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
4343 croak "$!: $x";
4344 # don't worry too much about these, they probably
4345 # don't exist with repos this old (save for index,
4346 # and we can easily regenerate that)
4347 foreach my $f (qw/unhandled.log index .rev_db/) {
4348 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
4349 }
4350 } else {
4351 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
4352 rename "$git_dir/$x", "$git_dir/svn/$x" or
4353 croak "$!: $x";
4354 }
4355 $migrated++;
4356 }
4357 command_close_pipe($fh, $ctx);
4358 print STDERR "Done migrating from a git-svn v1 layout\n";
4359 $migrated;
4360}
4361
4362sub read_old_urls {
4363 my ($l_map, $pfx, $path) = @_;
4364 my @dir;
4365 foreach (<$path/*>) {
4366 if (-r "$_/info/url") {
4367 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
4368 my $ref_id = $pfx . basename $_;
4369 my $url = ::file_to_s("$_/info/url");
4370 $l_map->{$ref_id} = $url;
4371 } elsif (-d $_) {
4372 push @dir, $_;
4373 }
4374 }
4375 foreach (@dir) {
4376 my $x = $_;
4377 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
4378 read_old_urls($l_map, $x, $_);
4379 }
4380}
4381
4382sub migrate_from_v2 {
4383 my @cfg = command(qw/config -l/);
4384 return if grep /^svn-remote\..+\.url=/, @cfg;
4385 my %l_map;
4386 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
4387 my $migrated = 0;
4388
4389 foreach my $ref_id (sort keys %l_map) {
Eric Wong471bc002007-02-01 04:06:27 -08004390 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
4391 if ($@) {
4392 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
4393 }
Eric Wong706587f2007-01-18 17:50:01 -08004394 $migrated++;
4395 }
4396 $migrated;
4397}
4398
Eric Wong47e39c52007-01-21 04:27:09 -08004399sub minimize_connections {
4400 my $r = Git::SVN::read_all_remotes();
4401 my $new_urls = {};
4402 my $root_repos = {};
4403 foreach my $repo_id (keys %$r) {
4404 my $url = $r->{$repo_id}->{url} or next;
4405 my $fetch = $r->{$repo_id}->{fetch} or next;
4406 my $ra = Git::SVN::Ra->new($url);
4407
4408 # skip existing cases where we already connect to the root
4409 if (($ra->{url} eq $ra->{repos_root}) ||
4410 (Git::SVN::sanitize_remote_name($ra->{repos_root}) eq
4411 $repo_id)) {
4412 $root_repos->{$ra->{url}} = $repo_id;
4413 next;
4414 }
4415
4416 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
4417 my $root_path = $ra->{url};
Eric Wong4e9f6cc2007-02-09 12:17:57 -08004418 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
Eric Wong47e39c52007-01-21 04:27:09 -08004419 foreach my $path (keys %$fetch) {
4420 my $ref_id = $fetch->{$path};
4421 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
4422
4423 # make sure we can read when connecting to
4424 # a higher level of a repository
4425 my ($last_rev, undef) = $gs->last_rev_commit;
4426 if (!defined $last_rev) {
4427 $last_rev = eval {
4428 $root_ra->get_latest_revnum;
4429 };
4430 next if $@;
4431 }
4432 my $new = $root_path;
4433 $new .= length $path ? "/$path" : '';
4434 eval {
4435 $root_ra->get_log([$new], $last_rev, $last_rev,
4436 0, 0, 1, sub { });
4437 };
4438 next if $@;
4439 $new_urls->{$ra->{repos_root}}->{$new} =
4440 { ref_id => $ref_id,
4441 old_repo_id => $repo_id,
4442 old_path => $path };
4443 }
4444 }
4445
4446 my @emptied;
4447 foreach my $url (keys %$new_urls) {
4448 # see if we can re-use an existing [svn-remote "repo_id"]
4449 # instead of creating a(n ugly) new section:
4450 my $repo_id = $root_repos->{$url} ||
4451 Git::SVN::sanitize_remote_name($url);
4452
4453 my $fetch = $new_urls->{$url};
4454 foreach my $path (keys %$fetch) {
4455 my $x = $fetch->{$path};
4456 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
4457 my $pfx = "svn-remote.$x->{old_repo_id}";
4458
4459 my $old_fetch = quotemeta("$x->{old_path}:".
4460 "refs/remotes/$x->{ref_id}");
Eric Wong8b8fc062007-01-22 11:44:57 -08004461 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08004462 "$pfx.fetch", '^'. $old_fetch . '$');
4463 delete $r->{$x->{old_repo_id}}->
4464 {fetch}->{$x->{old_path}};
4465 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
Eric Wong8b8fc062007-01-22 11:44:57 -08004466 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08004467 "$pfx.url");
4468 push @emptied, $x->{old_repo_id}
4469 }
4470 }
4471 }
4472 if (@emptied) {
4473 my $file = $ENV{GIT_CONFIG} || $ENV{GIT_CONFIG_LOCAL} ||
4474 "$ENV{GIT_DIR}/config";
4475 print STDERR <<EOF;
4476The following [svn-remote] sections in your config file ($file) are empty
4477and can be safely removed:
4478EOF
4479 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
4480 }
4481}
4482
Eric Wong706587f2007-01-18 17:50:01 -08004483sub migration_check {
4484 migrate_from_v0();
4485 migrate_from_v1();
4486 migrate_from_v2();
Eric Wong47e39c52007-01-21 04:27:09 -08004487 minimize_connections() if $_minimize;
Eric Wong706587f2007-01-18 17:50:01 -08004488}
4489
Eric Wongef3cfaa2007-01-24 03:30:57 -08004490package Git::IndexInfo;
4491use strict;
4492use warnings;
4493use Git qw/command_input_pipe command_close_pipe/;
4494
4495sub new {
4496 my ($class) = @_;
4497 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
4498 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
4499}
4500
4501sub remove {
4502 my ($self, $path) = @_;
4503 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
4504 return ++$self->{nr};
4505 }
4506 undef;
4507}
4508
4509sub update {
4510 my ($self, $mode, $hash, $path) = @_;
4511 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
4512 return ++$self->{nr};
4513 }
4514 undef;
4515}
4516
4517sub DESTROY {
4518 my ($self) = @_;
4519 command_close_pipe($self->{gui}, $self->{ctx});
4520}
4521
Eric Wong4bb9ed02007-02-03 13:29:17 -08004522package Git::SVN::GlobSpec;
4523use strict;
4524use warnings;
4525
4526sub new {
4527 my ($class, $glob) = @_;
Eric Wong4bb9ed02007-02-03 13:29:17 -08004528 my $re = $glob;
4529 $re =~ s!/+$!!g; # no need for trailing slashes
Eric Wong4e9f6cc2007-02-09 12:17:57 -08004530 my $nr = ($re =~ s!^(.*)\*(.*)$!\(\[^/\]+\)!g);
Eric Wong4bb9ed02007-02-03 13:29:17 -08004531 my ($left, $right) = ($1, $2);
4532 if ($nr > 1) {
Eric Wonge5181922007-02-08 12:53:57 -08004533 die "Only one '*' wildcard expansion ",
4534 "is supported (got $nr): '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08004535 } elsif ($nr == 0) {
Eric Wonge5181922007-02-08 12:53:57 -08004536 die "One '*' is needed for glob: '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08004537 }
4538 $re = quotemeta($left) . $re . quotemeta($right);
Eric Wong4e9f6cc2007-02-09 12:17:57 -08004539 if (length $left && !($left =~ s!/+$!!g)) {
4540 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
4541 }
4542 if (length $right && !($right =~ s!^/+!!g)) {
4543 die "Missing leading '/' on right side of: '$glob' ($right)\n";
4544 }
Eric Wong74a81222007-02-10 13:28:50 -08004545 my $left_re = qr/^\/\Q$left\E(\/|$)/;
4546 bless { left => $left, right => $right, left_regex => $left_re,
Eric Wong4bb9ed02007-02-03 13:29:17 -08004547 regex => qr/$re/, glob => $glob }, $class;
4548}
4549
4550sub full_path {
4551 my ($self, $path) = @_;
4552 return (length $self->{left} ? "$self->{left}/" : '') .
4553 $path . (length $self->{right} ? "/$self->{right}" : '');
4554}
4555
Eric Wong3397f9d2006-02-16 01:24:16 -08004556__END__
4557
4558Data structures:
4559
Eric Wong4bb9ed02007-02-03 13:29:17 -08004560
4561$remotes = { # returned by read_all_remotes()
4562 'svn' => {
4563 # svn-remote.svn.url=https://svn.musicpd.org
4564 url => 'https://svn.musicpd.org',
4565 # svn-remote.svn.fetch=mpd/trunk:trunk
4566 fetch => {
4567 'mpd/trunk' => 'trunk',
4568 },
4569 # svn-remote.svn.tags=mpd/tags/*:tags/*
4570 tags => {
4571 path => {
4572 left => 'mpd/tags',
4573 right => '',
4574 regex => qr!mpd/tags/([^/]+)$!,
4575 glob => 'tags/*',
4576 },
4577 ref => {
4578 left => 'tags',
4579 right => '',
4580 regex => qr!tags/([^/]+)$!,
4581 glob => 'tags/*',
4582 },
4583 }
4584 }
4585};
4586
Eric Wong44320b92007-01-13 22:35:53 -08004587$log_entry hashref as returned by libsvn_log_entry()
Eric Wong3397f9d2006-02-16 01:24:16 -08004588{
Eric Wong44320b92007-01-13 22:35:53 -08004589 log => 'whitespace-formatted log entry
Eric Wong3397f9d2006-02-16 01:24:16 -08004590', # trailing newline is preserved
4591 revision => '8', # integer
4592 date => '2004-02-24T17:01:44.108345Z', # commit date
4593 author => 'committer name'
4594};
4595
Eric Wong6e8548c2007-01-27 01:32:00 -08004596
4597# this is generated by generate_diff();
Eric Wong3397f9d2006-02-16 01:24:16 -08004598@mods = array of diff-index line hashes, each element represents one line
4599 of diff-index output
4600
4601diff-index line ($m hash)
4602{
4603 mode_a => first column of diff-index output, no leading ':',
4604 mode_b => second column of diff-index output,
4605 sha1_b => sha1sum of the final blob,
Eric Wongac8e0b92006-03-03 01:20:07 -08004606 chg => change type [MCRADT],
Eric Wong3397f9d2006-02-16 01:24:16 -08004607 file_a => original file name of a file (iff chg is 'C' or 'R')
4608 file_b => new/current file name of a file (any chg)
4609}
4610;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004611
Eric Wonga00439a2006-06-27 19:39:13 -07004612# retval of read_url_paths{,_all}();
4613$l_map = {
4614 # repository root url
4615 'https://svn.musicpd.org' => {
4616 # repository path # GIT_SVN_ID
4617 'mpd/trunk' => 'trunk',
4618 'mpd/tags/0.11.5' => 'tags/0.11.5',
4619 },
4620}
4621
Eric Wonga5e0ced2006-06-12 15:23:48 -07004622Notes:
4623 I don't trust the each() function on unless I created %hash myself
4624 because the internal iterator may not have started at base.