blob: 077d6b3a134fcb6c8f74f1473cbb9d36bdb7c6f9 [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
Eric Wong5253dc32007-02-20 01:36:30 -080012my $git_dir_user_set = 1 if defined $ENV{GIT_DIR};
Eric Wong706587f2007-01-18 17:50:01 -080013$ENV{GIT_DIR} ||= '.git';
Eric Wong9fa00b62007-02-03 12:49:48 -080014$Git::SVN::default_repo_id = 'svn';
Eric Wong8b8fc062007-01-22 11:44:57 -080015$Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
Eric Wong6af1db42007-02-14 16:04:10 -080016$Git::SVN::Ra::_log_window_size = 100;
Eric Wong13ccd6d2006-03-29 22:37:18 -080017
Eric Wongf8c9d1d2007-01-12 02:35:20 -080018$Git::SVN::Log::TZ = $ENV{TZ};
Eric Wong3397f9d2006-02-16 01:24:16 -080019$ENV{TZ} = 'UTC';
Eric Wonga00439a2006-06-27 19:39:13 -070020$| = 1; # unbuffer STDOUT
Eric Wong3397f9d2006-02-16 01:24:16 -080021
Eric Wong6fda05a2006-12-12 14:47:02 -080022sub fatal (@) { print STDERR @_; exit 1 }
Eric Wongb9c85182006-12-15 23:58:07 -080023require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
24require SVN::Ra;
25require SVN::Delta;
26if ($SVN::Core::VERSION lt '1.1.0') {
27 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)\n";
28}
Eric Wongd81bf822007-01-10 01:22:38 -080029push @Git::SVN::Ra::ISA, 'SVN::Ra';
Eric Wongb9c85182006-12-15 23:58:07 -080030push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
31push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
Eric Wong3397f9d2006-02-16 01:24:16 -080032use Carp qw/croak/;
33use IO::File qw//;
34use File::Basename qw/dirname basename/;
35use File::Path qw/mkpath/;
Eric Wong512b6202007-04-03 01:57:08 -070036use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
Eric Wong968bdf12006-06-15 13:36:12 -070037use IPC::Open3;
Eric Wong336f1712007-01-11 02:14:43 -080038use Git;
Eric Wonga5e0ced2006-06-12 15:23:48 -070039
Eric Wong336f1712007-01-11 02:14:43 -080040BEGIN {
41 my $s;
42 foreach (qw/command command_oneline command_noisy command_output_pipe
43 command_input_pipe command_close_pipe/) {
44 $s .= "*SVN::Git::Editor::$_ = *SVN::Git::Fetcher::$_ = ".
Eric Wong706587f2007-01-18 17:50:01 -080045 "*Git::SVN::Migration::$_ = ".
Eric Wongf8c9d1d2007-01-12 02:35:20 -080046 "*Git::SVN::Log::$_ = *Git::SVN::$_ = *$_ = *Git::$_; ";
Eric Wong336f1712007-01-11 02:14:43 -080047 }
48 eval $s;
49}
50
Eric Wongb9c85182006-12-15 23:58:07 -080051my ($SVN);
Eric Wong83e99402006-10-11 18:19:55 -070052
Eric Wongf8c9d1d2007-01-12 02:35:20 -080053$sha1 = qr/[a-f\d]{40}/;
54$sha1_short = qr/[a-f\d]{4,40}/;
Eric Wong44320b92007-01-13 22:35:53 -080055my ($_stdin, $_help, $_edit,
Eric Wong9760adc2007-01-31 03:06:56 -080056 $_message, $_file,
Eric Wongd05d72e2007-01-15 22:59:26 -080057 $_template, $_shared,
Eric Wonge98671e2007-02-14 02:21:19 -080058 $_version, $_fetch_all,
Eric Wongdee41f32007-03-13 11:40:36 -070059 $_merge, $_strategy, $_dry_run, $_local,
Eric Wong905f8b72007-02-16 03:22:40 -080060 $_prefix, $_no_checkout, $_verbose);
Eric Wong0bed5ea2007-02-09 02:45:03 -080061$Git::SVN::_follow_parent = 1;
Eric Wong706587f2007-01-18 17:50:01 -080062my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
63 'config-dir=s' => \$Git::SVN::Ra::config_dir,
64 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache );
Eric Wong0bed5ea2007-02-09 02:45:03 -080065my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
Eric Wongdc5869c2006-05-24 02:07:32 -070066 'authors-file|A=s' => \$_authors,
Eric Wongecc712d2007-01-31 12:28:10 -080067 'repack:i' => \$Git::SVN::_repack,
Eric Wong97ae0912007-02-11 15:21:24 -080068 'noMetadata' => \$Git::SVN::_no_metadata,
69 'useSvmProps' => \$Git::SVN::_use_svm_props,
Eric Wong62e349d2007-02-16 19:57:29 -080070 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props,
Eric Wong6af1db42007-02-14 16:04:10 -080071 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size,
Eric Wong1e889ef2007-02-16 01:45:13 -080072 'no-checkout' => \$_no_checkout,
Eric Wong80f50742006-06-27 19:39:14 -070073 'quiet|q' => \$_q,
Eric Wongecc712d2007-01-31 12:28:10 -080074 'repack-flags|repack-args|repack-opts=s' =>
75 \$Git::SVN::_repack_flags,
Eric Wong706587f2007-01-18 17:50:01 -080076 %remote_opts );
Eric Wong36f5b1f2006-05-23 19:23:41 -070077
Eric Wong9d55b412006-06-12 15:53:13 -070078my ($_trunk, $_tags, $_branches);
Eric Wong0dfaf0a2007-02-18 02:34:09 -080079my %icv;
Eric Wongdadc6d22007-02-14 12:27:41 -080080my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared,
81 'trunk|T=s' => \$_trunk, 'tags|t=s' => \$_tags,
82 'branches|b=s' => \$_branches, 'prefix=s' => \$_prefix,
Eric Wong0dfaf0a2007-02-18 02:34:09 -080083 'no-metadata' => sub { $icv{noMetadata} = 1 },
84 'use-svm-props' => sub { $icv{useSvmProps} = 1 },
85 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 },
86 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] },
Eric Wongdadc6d22007-02-14 12:27:41 -080087 %remote_opts );
Eric Wong27e9fb82006-06-27 19:39:12 -070088my %cmt_opts = ( 'edit|e' => \$_edit,
Eric Wong24e22aa2007-01-29 00:07:49 -080089 'rmdir' => \$SVN::Git::Editor::_rmdir,
90 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder,
91 'l=i' => \$SVN::Git::Editor::_rename_limit,
92 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity
Eric Wong27e9fb82006-06-27 19:39:12 -070093);
Eric Wong9d55b412006-06-12 15:53:13 -070094
Eric Wong3397f9d2006-02-16 01:24:16 -080095my %cmd = (
Eric Wong2a3240b2007-01-04 18:09:56 -080096 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
Eric Wonge98671e2007-02-14 02:21:19 -080097 { 'revision|r=s' => \$_revision,
Eric Wong905f8b72007-02-16 03:22:40 -080098 'fetch-all|all' => \$_fetch_all,
Eric Wonge98671e2007-02-14 02:21:19 -080099 %fc_opts } ],
Eric Wong0425ea92007-02-16 18:45:01 -0800100 clone => [ \&cmd_clone, "Initialize and fetch revisions",
101 { 'revision|r=s' => \$_revision,
102 %fc_opts, %init_opts } ],
Eric Wongd2866f92007-01-11 12:26:16 -0800103 init => [ \&cmd_init, "Initialize a repo for tracking" .
Eric Wongf8ab6b72006-05-31 15:49:56 -0700104 " (requires URL argument)",
Eric Wong9d55b412006-06-12 15:53:13 -0700105 \%init_opts ],
Eric Wongdadc6d22007-02-14 12:27:41 -0800106 'multi-init' => [ \&cmd_multi_init,
107 "Deprecated alias for ".
108 "'$0 init -T<trunk> -b<branches> -t<tags>'",
109 \%init_opts ],
Eric Wongd7ad3be2007-01-14 03:14:28 -0800110 dcommit => [ \&cmd_dcommit,
111 'Commit several diffs to merge with upstream',
Eric Wong3289e862006-12-15 23:58:08 -0800112 { 'merge|m|M' => \$_merge,
113 'strategy|s=s' => \$_strategy,
Eric Wong905f8b72007-02-16 03:22:40 -0800114 'verbose|v' => \$_verbose,
Eric Wong3289e862006-12-15 23:58:08 -0800115 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800116 'fetch-all|all' => \$_fetch_all,
Eric Wong4b155222006-12-22 21:59:24 -0800117 %cmt_opts, %fc_opts } ],
Eric Wong1ce255d2007-01-14 23:21:16 -0800118 'set-tree' => [ \&cmd_set_tree,
119 "Set an SVN repository to a git tree-ish",
120 { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
Eric Wong5969cbe2007-01-11 17:58:39 -0800121 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
Eric Wonga5e0ced2006-06-12 15:23:48 -0700122 { 'revision|r=i' => \$_revision } ],
Eric Wong1c8443b2007-01-14 02:17:00 -0800123 'multi-fetch' => [ \&cmd_multi_fetch,
Eric Wonge98671e2007-02-14 02:21:19 -0800124 "Deprecated alias for $0 fetch --all",
125 { 'revision|r=s' => \$_revision, %fc_opts } ],
Eric Wong706587f2007-01-18 17:50:01 -0800126 'migrate' => [ sub { },
127 # no-op, we automatically run this anyways,
Eric Wong706587f2007-01-18 17:50:01 -0800128 'Migrate configuration/metadata/layout from
129 previous versions of git-svn',
Eric Wonga836a0e2007-02-14 19:34:56 -0800130 { 'minimize' => \$Git::SVN::Migration::_minimize,
131 %remote_opts } ],
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800132 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
133 { 'limit=i' => \$Git::SVN::Log::limit,
Eric Wong79bb8d82006-06-01 02:35:44 -0700134 'revision|r=s' => \$_revision,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800135 'verbose|v' => \$Git::SVN::Log::verbose,
136 'incremental' => \$Git::SVN::Log::incremental,
137 'oneline' => \$Git::SVN::Log::oneline,
138 'show-commit' => \$Git::SVN::Log::show_commit,
139 'non-recursive' => \$Git::SVN::Log::non_recursive,
Eric Wong79bb8d82006-06-01 02:35:44 -0700140 'authors-file|A=s' => \$_authors,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800141 'color' => \$Git::SVN::Log::color,
142 'pager=s' => \$Git::SVN::Log::pager,
Eric Wong79bb8d82006-06-01 02:35:44 -0700143 } ],
Eric Wong905f8b72007-02-16 03:22:40 -0800144 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
145 { 'merge|m|M' => \$_merge,
146 'verbose|v' => \$_verbose,
147 'strategy|s=s' => \$_strategy,
Eric Wongdee41f32007-03-13 11:40:36 -0700148 'local|l' => \$_local,
Eric Wong905f8b72007-02-16 03:22:40 -0800149 'fetch-all|all' => \$_fetch_all,
150 %fc_opts } ],
Eric Wong44320b92007-01-13 22:35:53 -0800151 'commit-diff' => [ \&cmd_commit_diff,
152 'Commit a diff between two trees',
Eric Wong27e9fb82006-06-27 19:39:12 -0700153 { 'message|m=s' => \$_message,
154 'file|F=s' => \$_file,
Eric Wong45bf4732006-11-09 01:19:37 -0800155 'revision|r=s' => \$_revision,
Eric Wong27e9fb82006-06-27 19:39:12 -0700156 %cmt_opts } ],
Eric Wong3397f9d2006-02-16 01:24:16 -0800157);
Eric Wong9d55b412006-06-12 15:53:13 -0700158
Eric Wong3397f9d2006-02-16 01:24:16 -0800159my $cmd;
160for (my $i = 0; $i < @ARGV; $i++) {
161 if (defined $cmd{$ARGV[$i]}) {
162 $cmd = $ARGV[$i];
163 splice @ARGV, $i, 1;
164 last;
165 }
166};
167
Eric Wong448c81b2006-03-03 01:20:09 -0800168my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
Eric Wonga9612be2006-03-03 01:20:08 -0800169
Eric Wongb8c92ca2006-05-24 01:40:37 -0700170read_repo_config(\%opts);
Eric Wongc2849142007-04-17 02:41:43 -0700171Getopt::Long::Configure('pass_through') if ($cmd && $cmd eq 'log');
Eric Wong9760adc2007-01-31 03:06:56 -0800172my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
173 'minimize-connections' => \$Git::SVN::Migration::_minimize,
174 'id|i=s' => \$Git::SVN::default_ref_id,
Eric Wongbefc9ad2007-02-17 02:53:07 -0800175 'svn-remote|remote|R=s' => sub {
176 $Git::SVN::no_reuse_existing = 1;
177 $Git::SVN::default_repo_id = $_[1] });
Eric Wongc2849142007-04-17 02:41:43 -0700178exit 1 if (!$rv && $cmd && $cmd ne 'log');
Eric Wong6f0783c2006-03-03 01:20:09 -0800179
Eric Wong3397f9d2006-02-16 01:24:16 -0800180usage(0) if $_help;
Eric Wong551ce282006-02-20 10:57:29 -0800181version() if $_version;
Eric Wongeeb0abe2006-03-03 01:20:08 -0800182usage(1) unless defined $cmd;
183load_authors() if $_authors;
Eric Wong5253dc32007-02-20 01:36:30 -0800184
185# make sure we're always running
186unless ($cmd =~ /(?:clone|init|multi-init)$/) {
187 unless (-d $ENV{GIT_DIR}) {
188 if ($git_dir_user_set) {
189 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
190 "but it is not a directory\n";
191 }
192 my $git_dir = delete $ENV{GIT_DIR};
193 chomp(my $cdup = command_oneline(qw/rev-parse --show-cdup/));
194 unless (length $cdup) {
195 die "Already at toplevel, but $git_dir ",
196 "not found '$cdup'\n";
197 }
198 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
199 unless (-d $git_dir) {
200 die "$git_dir still not found after going to ",
201 "'$cdup'\n";
202 }
203 $ENV{GIT_DIR} = $git_dir;
204 }
205}
Eric Wong0425ea92007-02-16 18:45:01 -0800206unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
Eric Wong706587f2007-01-18 17:50:01 -0800207 Git::SVN::Migration::migration_check();
208}
Eric Wongecc712d2007-01-31 12:28:10 -0800209Git::SVN::init_vars();
Eric Wongb805b442007-01-22 13:52:04 -0800210eval {
211 Git::SVN::verify_remotes_sanity();
212 $cmd{$cmd}->[0]->(@ARGV);
213};
214fatal $@ if $@;
Eric Wong1e889ef2007-02-16 01:45:13 -0800215post_fetch_checkout();
Eric Wong3397f9d2006-02-16 01:24:16 -0800216exit 0;
217
218####################### primary functions ######################
219sub usage {
220 my $exit = shift || 0;
221 my $fd = $exit ? \*STDERR : \*STDOUT;
222 print $fd <<"";
223git-svn - bidirectional operations between a single Subversion tree and git
224Usage: $0 <command> [options] [arguments]\n
Eric Wong448c81b2006-03-03 01:20:09 -0800225
226 print $fd "Available commands:\n" unless $cmd;
Eric Wong3397f9d2006-02-16 01:24:16 -0800227
228 foreach (sort keys %cmd) {
Eric Wong448c81b2006-03-03 01:20:09 -0800229 next if $cmd && $cmd ne $_;
Eric Wonga836a0e2007-02-14 19:34:56 -0800230 next if /^multi-/; # don't show deprecated commands
Eric Wongb203b762006-10-11 14:53:36 -0700231 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
Eric Wong448c81b2006-03-03 01:20:09 -0800232 foreach (keys %{$cmd{$_}->[2]}) {
Eric Wong512b6202007-04-03 01:57:08 -0700233 # mixed-case options are for .git/config only
234 next if /[A-Z]/ && /^[a-z]+$/i;
Eric Wong448c81b2006-03-03 01:20:09 -0800235 # prints out arguments as they should be passed:
Eric Wongb8c92ca2006-05-24 01:40:37 -0700236 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
Eric Wongb203b762006-10-11 14:53:36 -0700237 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
Eric Wong448c81b2006-03-03 01:20:09 -0800238 "--$_" : "-$_" }
239 split /\|/,$_)," $x\n";
240 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800241 }
242 print $fd <<"";
Eric Wong448c81b2006-03-03 01:20:09 -0800243\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
244arbitrary identifier if you're tracking multiple SVN branches/repositories in
245one git repository and want to keep them separate. See git-svn(1) for more
246information.
Eric Wong3397f9d2006-02-16 01:24:16 -0800247
248 exit $exit;
249}
250
Eric Wong551ce282006-02-20 10:57:29 -0800251sub version {
Eric Wong7d60ab22006-12-28 01:16:20 -0800252 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
Eric Wong551ce282006-02-20 10:57:29 -0800253 exit 0;
254}
255
Eric Wong8164b652007-01-11 15:35:55 -0800256sub do_git_init_db {
257 unless (-d $ENV{GIT_DIR}) {
258 my @init_db = ('init');
259 push @init_db, "--template=$_template" if defined $_template;
Eric Wongdadc6d22007-02-14 12:27:41 -0800260 if (defined $_shared) {
261 if ($_shared =~ /[a-z]/) {
262 push @init_db, "--shared=$_shared";
263 } else {
264 push @init_db, "--shared";
265 }
266 }
Eric Wong8164b652007-01-11 15:35:55 -0800267 command_noisy(@init_db);
268 }
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800269 my $set;
270 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
271 foreach my $i (keys %icv) {
272 die "'$set' and '$i' cannot both be set\n" if $set;
273 next unless defined $icv{$i};
274 command_noisy('config', "$pfx.$i", $icv{$i});
275 $set = $i;
276 }
Eric Wong8164b652007-01-11 15:35:55 -0800277}
278
Eric Wongdadc6d22007-02-14 12:27:41 -0800279sub init_subdir {
280 my $repo_path = shift or return;
281 mkpath([$repo_path]) unless -d $repo_path;
282 chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
Eric Wongf30603f2007-02-23 01:26:26 -0800283 $ENV{GIT_DIR} = '.git';
Eric Wongdadc6d22007-02-14 12:27:41 -0800284}
285
Eric Wong0425ea92007-02-16 18:45:01 -0800286sub cmd_clone {
287 my ($url, $path) = @_;
288 if (!defined $path &&
289 (defined $_trunk || defined $_branches || defined $_tags) &&
290 $url !~ m#^[a-z\+]+://#) {
291 $path = $url;
292 }
Eric Wong0425ea92007-02-16 18:45:01 -0800293 $path = basename($url) if !defined $path || !length $path;
Eric Wongf30603f2007-02-23 01:26:26 -0800294 cmd_init($url, $path);
Eric Wong0425ea92007-02-16 18:45:01 -0800295 Git::SVN::fetch_all($Git::SVN::default_repo_id);
296}
297
Eric Wongd2866f92007-01-11 12:26:16 -0800298sub cmd_init {
Eric Wongdadc6d22007-02-14 12:27:41 -0800299 if (defined $_trunk || defined $_branches || defined $_tags) {
300 return cmd_multi_init(@_);
Eric Wong03e0ea82006-06-30 21:42:53 -0700301 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800302 my $url = shift or die "SVN repository location required ",
303 "as a command-line argument\n";
304 init_subdir(@_);
Eric Wong8164b652007-01-11 15:35:55 -0800305 do_git_init_db();
Eric Wong03e0ea82006-06-30 21:42:53 -0700306
Eric Wong706587f2007-01-18 17:50:01 -0800307 Git::SVN->init($url);
Eric Wong3397f9d2006-02-16 01:24:16 -0800308}
309
Eric Wong2a3240b2007-01-04 18:09:56 -0800310sub cmd_fetch {
Eric Wonge98671e2007-02-14 02:21:19 -0800311 if (grep /^\d+=./, @_) {
312 die "'<rev>=<commit>' fetch arguments are ",
313 "no longer supported.\n";
Eric Wong07a1c952007-01-22 15:47:41 -0800314 }
Eric Wonge98671e2007-02-14 02:21:19 -0800315 my ($remote) = @_;
316 if (@_ > 1) {
Eric Wong905f8b72007-02-16 03:22:40 -0800317 die "Usage: $0 fetch [--all] [svn-remote]\n";
Eric Wonge98671e2007-02-14 02:21:19 -0800318 }
319 $remote ||= $Git::SVN::default_repo_id;
320 if ($_fetch_all) {
321 cmd_multi_fetch();
322 } else {
323 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
Eric Wong1c8443b2007-01-14 02:17:00 -0800324 }
Eric Wong2a3240b2007-01-04 18:09:56 -0800325}
326
Eric Wong1ce255d2007-01-14 23:21:16 -0800327sub cmd_set_tree {
Eric Wong3397f9d2006-02-16 01:24:16 -0800328 my (@commits) = @_;
329 if ($_stdin || !@commits) {
330 print "Reading from stdin...\n";
331 @commits = ();
332 while (<STDIN>) {
Eric Wong1ca72ae2006-03-03 01:20:09 -0800333 if (/\b($sha1_short)\b/o) {
Eric Wong3397f9d2006-02-16 01:24:16 -0800334 unshift @commits, $1;
335 }
336 }
337 }
338 my @revs;
Eric Wong8de010a2006-02-20 10:57:26 -0800339 foreach my $c (@commits) {
Eric Wongaef4e922006-12-15 10:59:54 -0800340 my @tmp = command('rev-parse',$c);
Eric Wong8de010a2006-02-20 10:57:26 -0800341 if (scalar @tmp == 1) {
342 push @revs, $tmp[0];
343 } elsif (scalar @tmp > 1) {
Eric Wongaef4e922006-12-15 10:59:54 -0800344 push @revs, reverse(command('rev-list',@tmp));
Eric Wong8de010a2006-02-20 10:57:26 -0800345 } else {
Eric Wong1ce255d2007-01-14 23:21:16 -0800346 fatal "Failed to rev-parse $c\n";
Eric Wong8de010a2006-02-20 10:57:26 -0800347 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800348 }
Eric Wong1ce255d2007-01-14 23:21:16 -0800349 my $gs = Git::SVN->new;
350 my ($r_last, $cmt_last) = $gs->last_rev_commit;
351 $gs->fetch;
Eric Wong97f69872007-01-25 11:53:13 -0800352 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
Eric Wong1ce255d2007-01-14 23:21:16 -0800353 fatal "There are new revisions that were fetched ",
354 "and need to be merged (or acknowledged) ",
355 "before committing.\nlast rev: $r_last\n",
356 " current: $gs->{last_rev}\n";
357 }
358 $gs->set_tree($_) foreach @revs;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700359 print "Done committing ",scalar @revs," revisions to SVN\n";
360}
361
Eric Wongd7ad3be2007-01-14 03:14:28 -0800362sub cmd_dcommit {
363 my $head = shift;
Eric Wongd7ad3be2007-01-14 03:14:28 -0800364 $head ||= 'HEAD';
Eric Wonga8ae2622007-02-13 14:22:11 -0800365 my @refs;
Eric Wong13c823f2007-04-08 00:59:19 -0700366 my ($url, $rev, $uuid, $gs) = working_head_info($head, \@refs);
367 unless ($gs) {
Eric Wonga8ae2622007-02-13 14:22:11 -0800368 die "Unable to determine upstream SVN information from ",
Eric Wong905f8b72007-02-16 03:22:40 -0800369 "$head history\n";
Eric Wonga8ae2622007-02-13 14:22:11 -0800370 }
Eric Wong13c823f2007-04-08 00:59:19 -0700371 my $c = $refs[-1];
Eric Wong45bf4732006-11-09 01:19:37 -0800372 my $last_rev;
Eric Wonga8ae2622007-02-13 14:22:11 -0800373 foreach my $d (@refs) {
Eric Wongaef4e922006-12-15 10:59:54 -0800374 if (!verify_ref("$d~1")) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800375 fatal "Commit $d\n",
376 "has no parent commit, and therefore ",
377 "nothing to diff against.\n",
378 "You should be working from a repository ",
379 "originally created by git-svn\n";
Eric Wong48d044b2006-11-23 14:54:03 -0800380 }
Eric Wong45bf4732006-11-09 01:19:37 -0800381 unless (defined $last_rev) {
382 (undef, $last_rev, undef) = cmt_metadata("$d~1");
383 unless (defined $last_rev) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800384 fatal "Unable to extract revision information ",
385 "from commit $d~1\n";
Eric Wong45bf4732006-11-09 01:19:37 -0800386 }
387 }
Eric Wongb22d4492006-08-26 00:01:23 -0700388 if ($_dry_run) {
389 print "diff-tree $d~1 $d\n";
390 } else {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800391 my %ed_opts = ( r => $last_rev,
Eric Wong61395352007-01-27 14:33:08 -0800392 log => get_commit_entry($d)->{log},
Eric Wonga8ae2622007-02-13 14:22:11 -0800393 ra => Git::SVN::Ra->new($url),
Eric Wong61395352007-01-27 14:33:08 -0800394 tree_a => "$d~1",
395 tree_b => $d,
396 editor_cb => sub {
397 print "Committed r$_[0]\n";
398 $last_rev = $_[0]; },
Eric Wonga8ae2622007-02-13 14:22:11 -0800399 svn_path => '');
Eric Wong61395352007-01-27 14:33:08 -0800400 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800401 print "No changes\n$d~1 == $d\n";
402 }
Eric Wongb22d4492006-08-26 00:01:23 -0700403 }
404 }
405 return if $_dry_run;
Eric Wong60d9c972007-02-14 18:47:16 -0800406 unless ($gs) {
407 warn "Could not determine fetch information for $url\n",
408 "Will not attempt to fetch and rebase commits.\n",
409 "This probably means you have useSvmProps and should\n",
410 "now resync your SVN::Mirror repository.\n";
411 return;
412 }
Eric Wong905f8b72007-02-16 03:22:40 -0800413 $_fetch_all ? $gs->fetch_all : $gs->fetch;
Eric Wongd7ad3be2007-01-14 03:14:28 -0800414 # we always want to rebase against the current HEAD, not any
415 # head that was passed to us
416 my @diff = command('diff-tree', 'HEAD', $gs->refname, '--');
Eric Wongb22d4492006-08-26 00:01:23 -0700417 my @finish;
418 if (@diff) {
Eric Wong905f8b72007-02-16 03:22:40 -0800419 @finish = rebase_cmd();
Eric Wongd7ad3be2007-01-14 03:14:28 -0800420 print STDERR "W: HEAD and ", $gs->refname, " differ, ",
421 "using @finish:\n", "@diff";
Eric Wongb22d4492006-08-26 00:01:23 -0700422 } else {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800423 print "No changes between current HEAD and ",
424 $gs->refname, "\nResetting to the latest ",
425 $gs->refname, "\n";
Eric Wong47694892006-11-23 14:54:05 -0800426 @finish = qw/reset --mixed/;
Eric Wongb22d4492006-08-26 00:01:23 -0700427 }
Eric Wongd7ad3be2007-01-14 03:14:28 -0800428 command_noisy(@finish, $gs->refname);
Eric Wongb22d4492006-08-26 00:01:23 -0700429}
430
Eric Wong905f8b72007-02-16 03:22:40 -0800431sub cmd_rebase {
432 command_noisy(qw/update-index --refresh/);
Eric Wong13c823f2007-04-08 00:59:19 -0700433 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
434 unless ($gs) {
Eric Wong905f8b72007-02-16 03:22:40 -0800435 die "Unable to determine upstream SVN information from ",
436 "working tree history\n";
437 }
Eric Wong905f8b72007-02-16 03:22:40 -0800438 if (command(qw/diff-index HEAD --/)) {
439 print STDERR "Cannot rebase with uncommited changes:\n";
440 command_noisy('status');
441 exit 1;
442 }
Eric Wongdee41f32007-03-13 11:40:36 -0700443 unless ($_local) {
444 $_fetch_all ? $gs->fetch_all : $gs->fetch;
445 }
Eric Wong905f8b72007-02-16 03:22:40 -0800446 command_noisy(rebase_cmd(), $gs->refname);
447}
448
Eric Wong5969cbe2007-01-11 17:58:39 -0800449sub cmd_show_ignore {
Eric Wong13c823f2007-04-08 00:59:19 -0700450 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
451 $gs ||= Git::SVN->new;
Eric Wong5969cbe2007-01-11 17:58:39 -0800452 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
Eric Wong102a0a22007-03-04 00:15:29 -0800453 $gs->traverse_ignore(\*STDOUT, $gs->{path}, $r);
Eric Wonga5e0ced2006-06-12 15:23:48 -0700454}
455
Eric Wong8164b652007-01-11 15:35:55 -0800456sub cmd_multi_init {
Eric Wong9d55b412006-06-12 15:53:13 -0700457 my $url = shift;
Eric Wong98327e52007-01-04 18:02:00 -0800458 unless (defined $_trunk || defined $_branches || defined $_tags) {
459 usage(1);
460 }
Eric Wong8164b652007-01-11 15:35:55 -0800461 $_prefix = '' unless defined $_prefix;
Eric Wongdadc6d22007-02-14 12:27:41 -0800462 if (defined $url) {
463 $url =~ s#/+$##;
464 init_subdir(@_);
465 }
Eric Wongf30603f2007-02-23 01:26:26 -0800466 do_git_init_db();
Eric Wong98327e52007-01-04 18:02:00 -0800467 if (defined $_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800468 my $trunk_ref = $_prefix . 'trunk';
469 # try both old-style and new-style lookups:
470 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
Eric Wong8164b652007-01-11 15:35:55 -0800471 unless ($gs_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800472 my ($trunk_url, $trunk_path) =
473 complete_svn_url($url, $_trunk);
474 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
475 undef, $trunk_ref);
Eric Wong98327e52007-01-04 18:02:00 -0800476 }
Eric Wongc35b96e2006-10-11 11:53:21 -0700477 }
Eric Wong706587f2007-01-18 17:50:01 -0800478 return unless defined $_branches || defined $_tags;
Eric Wonge7db67e2007-01-11 17:09:26 -0800479 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
480 complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix);
481 complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
Eric Wong9d55b412006-06-12 15:53:13 -0700482}
483
Eric Wong1c8443b2007-01-14 02:17:00 -0800484sub cmd_multi_fetch {
Eric Wong0af9c9f2007-01-27 22:28:56 -0800485 my $remotes = Git::SVN::read_all_remotes();
486 foreach my $repo_id (sort keys %$remotes) {
Eric Wongdb03cd22007-02-13 00:38:02 -0800487 if ($remotes->{$repo_id}->{url}) {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800488 Git::SVN::fetch_all($repo_id, $remotes);
489 }
Eric Wong706587f2007-01-18 17:50:01 -0800490 }
Eric Wong9d55b412006-06-12 15:53:13 -0700491}
492
Eric Wong44320b92007-01-13 22:35:53 -0800493# this command is special because it requires no metadata
494sub cmd_commit_diff {
495 my ($ta, $tb, $url) = @_;
496 my $usage = "Usage: $0 commit-diff -r<revision> ".
497 "<tree-ish> <tree-ish> [<URL>]\n";
498 fatal($usage) if (!defined $ta || !defined $tb);
Eric Wongd3a840d2007-01-26 01:32:45 -0800499 my $svn_path;
Eric Wong44320b92007-01-13 22:35:53 -0800500 if (!defined $url) {
501 my $gs = eval { Git::SVN->new };
502 if (!$gs) {
503 fatal("Needed URL or usable git-svn --id in ",
504 "the command-line\n", $usage);
505 }
506 $url = $gs->{url};
Eric Wongd3a840d2007-01-26 01:32:45 -0800507 $svn_path = $gs->{path};
Eric Wong44320b92007-01-13 22:35:53 -0800508 }
509 unless (defined $_revision) {
510 fatal("-r|--revision is a required argument\n", $usage);
511 }
512 if (defined $_message && defined $_file) {
513 fatal("Both --message/-m and --file/-F specified ",
514 "for the commit message.\n",
515 "I have no idea what you mean\n");
516 }
517 if (defined $_file) {
518 $_message = file_to_s($_file);
519 } else {
520 $_message ||= get_commit_entry($tb)->{log};
521 }
522 my $ra ||= Git::SVN::Ra->new($url);
Eric Wongd3a840d2007-01-26 01:32:45 -0800523 $svn_path ||= $ra->{svn_path};
Eric Wong44320b92007-01-13 22:35:53 -0800524 my $r = $_revision;
525 if ($r eq 'HEAD') {
526 $r = $ra->get_latest_revnum;
527 } elsif ($r !~ /^\d+$/) {
528 die "revision argument: $r not understood by git-svn\n";
529 }
Eric Wong61395352007-01-27 14:33:08 -0800530 my %ed_opts = ( r => $r,
531 log => $_message,
532 ra => $ra,
533 tree_a => $ta,
534 tree_b => $tb,
535 editor_cb => sub { print "Committed r$_[0]\n" },
536 svn_path => $svn_path );
537 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong44320b92007-01-13 22:35:53 -0800538 print "No changes\n$ta == $tb\n";
539 }
Eric Wong44320b92007-01-13 22:35:53 -0800540}
541
Eric Wong3397f9d2006-02-16 01:24:16 -0800542########################### utility functions #########################
543
Eric Wong905f8b72007-02-16 03:22:40 -0800544sub rebase_cmd {
545 my @cmd = qw/rebase/;
546 push @cmd, '-v' if $_verbose;
547 push @cmd, qw/--merge/ if $_merge;
548 push @cmd, "--strategy=$_strategy" if $_strategy;
549 @cmd;
550}
551
Eric Wong1e889ef2007-02-16 01:45:13 -0800552sub post_fetch_checkout {
553 return if $_no_checkout;
554 my $gs = $Git::SVN::_head or return;
555 return if verify_ref('refs/heads/master^0');
556
557 my $valid_head = verify_ref('HEAD^0');
558 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
559 return if ($valid_head || !verify_ref('HEAD^0'));
560
561 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
562 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
563 return if -f $index;
564
565 chomp(my $bare = `git config --bool --get core.bare`);
566 return if $bare eq 'true';
567 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
568 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
569 print STDERR "Checked out HEAD:\n ",
570 $gs->full_url, " r", $gs->last_rev, "\n";
571}
572
Eric Wong98327e52007-01-04 18:02:00 -0800573sub complete_svn_url {
574 my ($url, $path) = @_;
575 $path =~ s#/+$##;
Eric Wong98327e52007-01-04 18:02:00 -0800576 if ($path !~ m#^[a-z\+]+://#) {
Eric Wong98327e52007-01-04 18:02:00 -0800577 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
578 fatal("E: '$path' is not a complete URL ",
579 "and a separate URL is not specified\n");
580 }
Eric Wong706587f2007-01-18 17:50:01 -0800581 return ($url, $path);
Eric Wong98327e52007-01-04 18:02:00 -0800582 }
Eric Wong706587f2007-01-18 17:50:01 -0800583 return ($path, '');
Eric Wong98327e52007-01-04 18:02:00 -0800584}
585
Eric Wong9d55b412006-06-12 15:53:13 -0700586sub complete_url_ls_init {
Eric Wong706587f2007-01-18 17:50:01 -0800587 my ($ra, $repo_path, $switch, $pfx) = @_;
588 unless ($repo_path) {
Eric Wong9d55b412006-06-12 15:53:13 -0700589 print STDERR "W: $switch not specified\n";
590 return;
591 }
Eric Wong706587f2007-01-18 17:50:01 -0800592 $repo_path =~ s#/+$##;
593 if ($repo_path =~ m#^[a-z\+]+://#) {
594 $ra = Git::SVN::Ra->new($repo_path);
595 $repo_path = '';
Eric Wonge7db67e2007-01-11 17:09:26 -0800596 } else {
Eric Wong706587f2007-01-18 17:50:01 -0800597 $repo_path =~ s#^/+##;
Eric Wonge7db67e2007-01-11 17:09:26 -0800598 unless ($ra) {
Eric Wong706587f2007-01-18 17:50:01 -0800599 fatal("E: '$repo_path' is not a complete URL ",
Eric Wonge7db67e2007-01-11 17:09:26 -0800600 "and a separate URL is not specified\n");
Eric Wong9d55b412006-06-12 15:53:13 -0700601 }
Eric Wonge7db67e2007-01-11 17:09:26 -0800602 }
Eric Wong706587f2007-01-18 17:50:01 -0800603 my $url = $ra->{url};
Eric Wongb4d57e52007-02-14 15:10:44 -0800604 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
605 my $k = "svn-remote.$gs->{repo_id}.url";
606 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
607 if ($orig_url && ($orig_url ne $gs->{url})) {
608 die "$k already set: $orig_url\n",
609 "wanted to set to: $gs->{url}\n";
Eric Wong88cf4102007-02-01 03:59:07 -0800610 }
Eric Wongb4d57e52007-02-14 15:10:44 -0800611 command_oneline('config', $k, $gs->{url}) unless $orig_url;
612 my $remote_path = "$ra->{svn_path}/$repo_path/*";
613 $remote_path =~ s#/+#/#g;
614 $remote_path =~ s#^/##g;
615 my ($n) = ($switch =~ /^--(\w+)/);
616 if (length $pfx && $pfx !~ m#/$#) {
617 die "--prefix='$pfx' must have a trailing slash '/'\n";
Eric Wong9d55b412006-06-12 15:53:13 -0700618 }
Eric Wongb4d57e52007-02-14 15:10:44 -0800619 command_noisy('config', "svn-remote.$gs->{repo_id}.$n",
620 "$remote_path:refs/remotes/$pfx*");
Eric Wong9d55b412006-06-12 15:53:13 -0700621}
622
Eric Wongaef4e922006-12-15 10:59:54 -0800623sub verify_ref {
624 my ($ref) = @_;
Eric Wong2c5c1d52006-12-28 01:16:21 -0800625 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
626 { STDERR => 0 }); };
Eric Wongaef4e922006-12-15 10:59:54 -0800627}
628
Eric Wonga5e0ced2006-06-12 15:23:48 -0700629sub get_tree_from_treeish {
Eric Wongcf52b8f2006-02-20 10:57:28 -0800630 my ($treeish) = @_;
Eric Wong44320b92007-01-13 22:35:53 -0800631 # $treeish can be a symbolic ref, too:
Eric Wongaef4e922006-12-15 10:59:54 -0800632 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -0800633 my $expected;
634 while ($type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -0800635 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -0800636 }
637 if ($type eq 'commit') {
Eric Wongaef4e922006-12-15 10:59:54 -0800638 $expected = (grep /^tree /, command(qw/cat-file commit/,
639 $treeish))[0];
Eric Wong44320b92007-01-13 22:35:53 -0800640 ($expected) = ($expected =~ /^tree ($sha1)$/o);
Eric Wongcf52b8f2006-02-20 10:57:28 -0800641 die "Unable to get tree from $treeish\n" unless $expected;
642 } elsif ($type eq 'tree') {
643 $expected = $treeish;
644 } else {
645 die "$treeish is a $type, expected tree, tag or commit\n";
646 }
Eric Wonga5e0ced2006-06-12 15:23:48 -0700647 return $expected;
648}
Eric Wongcf52b8f2006-02-20 10:57:28 -0800649
Eric Wong44320b92007-01-13 22:35:53 -0800650sub get_commit_entry {
651 my ($treeish) = shift;
652 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
653 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
654 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
655 open my $log_fh, '>', $commit_editmsg or croak $!;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700656
Eric Wong44320b92007-01-13 22:35:53 -0800657 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wong4ad45152006-07-09 20:20:48 -0700658 if ($type eq 'commit' || $type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -0800659 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
Eric Wong44320b92007-01-13 22:35:53 -0800660 $type, $treeish);
Eric Wong3397f9d2006-02-16 01:24:16 -0800661 my $in_msg = 0;
662 while (<$msg_fh>) {
663 if (!$in_msg) {
664 $in_msg = 1 if (/^\s*$/);
Eric Wongdf746c52006-03-03 01:20:08 -0800665 } elsif (/^git-svn-id: /) {
Eric Wong44320b92007-01-13 22:35:53 -0800666 # skip this for now, we regenerate the
667 # correct one on re-fetch anyways
668 # TODO: set *:merge properties or like...
Eric Wong3397f9d2006-02-16 01:24:16 -0800669 } else {
Eric Wong44320b92007-01-13 22:35:53 -0800670 print $log_fh $_ or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -0800671 }
672 }
Eric Wongaef4e922006-12-15 10:59:54 -0800673 command_close_pipe($msg_fh, $ctx);
Eric Wong3397f9d2006-02-16 01:24:16 -0800674 }
Eric Wong44320b92007-01-13 22:35:53 -0800675 close $log_fh or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -0800676
677 if ($_edit || ($type eq 'tree')) {
678 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
Eric Wong44320b92007-01-13 22:35:53 -0800679 # TODO: strip out spaces, comments, like git-commit.sh
680 system($editor, $commit_editmsg);
Eric Wong3397f9d2006-02-16 01:24:16 -0800681 }
Eric Wong44320b92007-01-13 22:35:53 -0800682 rename $commit_editmsg, $commit_msg or croak $!;
683 open $log_fh, '<', $commit_msg or croak $!;
684 { local $/; chomp($log_entry{log} = <$log_fh>); }
685 close $log_fh or croak $!;
686 unlink $commit_msg;
687 \%log_entry;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700688}
689
Eric Wong3397f9d2006-02-16 01:24:16 -0800690sub s_to_file {
691 my ($str, $file, $mode) = @_;
692 open my $fd,'>',$file or croak $!;
693 print $fd $str,"\n" or croak $!;
694 close $fd or croak $!;
695 chmod ($mode &~ umask, $file) if (defined $mode);
696}
697
698sub file_to_s {
699 my $file = shift;
700 open my $fd,'<',$file or croak "$!: file: $file\n";
701 local $/;
702 my $ret = <$fd>;
703 close $fd or croak $!;
704 $ret =~ s/\s*$//s;
705 return $ret;
706}
707
Eric Wongeeb0abe2006-03-03 01:20:08 -0800708# '<svn username> = real-name <email address>' mapping based on git-svnimport:
709sub load_authors {
710 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800711 my $log = $cmd eq 'log';
Eric Wongeeb0abe2006-03-03 01:20:08 -0800712 while (<$authors>) {
713 chomp;
Shawn Pearce88157882006-09-24 23:04:55 -0400714 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
Eric Wongeeb0abe2006-03-03 01:20:08 -0800715 my ($user, $name, $email) = ($1, $2, $3);
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800716 if ($log) {
717 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
718 } else {
719 $users{$user} = [$name, $email];
720 }
Eric Wong79bb8d82006-06-01 02:35:44 -0700721 }
722 close $authors or croak $!;
723}
724
Tom Princee0d10e12007-01-28 16:16:53 -0800725# convert GetOpt::Long specs for use by git-config
Eric Wongb8c92ca2006-05-24 01:40:37 -0700726sub read_repo_config {
Eric Wong706587f2007-01-18 17:50:01 -0800727 return unless -d $ENV{GIT_DIR};
Eric Wongb8c92ca2006-05-24 01:40:37 -0700728 my $opts = shift;
Eric Wong97ae0912007-02-11 15:21:24 -0800729 my @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -0700730 foreach my $o (keys %$opts) {
Eric Wong97ae0912007-02-11 15:21:24 -0800731 # if we have mixedCase and a long option-only, then
732 # it's a config-only variable that we don't need for
733 # the command-line.
734 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
Eric Wongb8c92ca2006-05-24 01:40:37 -0700735 my $v = $opts->{$o};
Eric Wong97ae0912007-02-11 15:21:24 -0800736 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
Eric Wongb8c92ca2006-05-24 01:40:37 -0700737 $key =~ s/-//g;
Tom Princee0d10e12007-01-28 16:16:53 -0800738 my $arg = 'git-config';
Eric Wongb8c92ca2006-05-24 01:40:37 -0700739 $arg .= ' --int' if ($o =~ /[:=]i$/);
740 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
741 if (ref $v eq 'ARRAY') {
742 chomp(my @tmp = `$arg --get-all svn.$key`);
743 @$v = @tmp if @tmp;
744 } else {
745 chomp(my $tmp = `$arg --get svn.$key`);
Eric Wong77742842007-02-10 21:07:12 -0800746 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
Eric Wongb8c92ca2006-05-24 01:40:37 -0700747 $$v = $tmp;
748 }
749 }
750 }
Eric Wong97ae0912007-02-11 15:21:24 -0800751 delete @$opts{@config_only} if @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -0700752}
753
Eric Wong79bb8d82006-06-01 02:35:44 -0700754sub extract_metadata {
Eric Wongc1927a82006-06-27 19:39:11 -0700755 my $id = shift or return (undef, undef, undef);
Eric Wong79bb8d82006-06-01 02:35:44 -0700756 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
757 \s([a-f\d\-]+)$/x);
Eric Wonge70dc782006-11-23 14:54:04 -0800758 if (!defined $rev || !$uuid || !$url) {
Eric Wong79bb8d82006-06-01 02:35:44 -0700759 # some of the original repositories I made had
Pavel Roskin82e5a822006-07-10 01:50:18 -0400760 # identifiers like this:
Eric Wong79bb8d82006-06-01 02:35:44 -0700761 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
762 }
763 return ($url, $rev, $uuid);
764}
765
Eric Wongc1927a82006-06-27 19:39:11 -0700766sub cmt_metadata {
767 return extract_metadata((grep(/^git-svn-id: /,
Eric Wongaef4e922006-12-15 10:59:54 -0800768 command(qw/cat-file commit/, shift)))[-1]);
Eric Wongc1927a82006-06-27 19:39:11 -0700769}
770
Eric Wong905f8b72007-02-16 03:22:40 -0800771sub working_head_info {
772 my ($head, $refs) = @_;
Eric Wong905f8b72007-02-16 03:22:40 -0800773 my ($fh, $ctx) = command_output_pipe('rev-list', $head);
774 while (<$fh>) {
775 chomp;
Eric Wong13c823f2007-04-08 00:59:19 -0700776 my ($url, $rev, $uuid) = cmt_metadata($_);
777 if (defined $url && defined $rev) {
778 if (my $gs = Git::SVN->find_by_url($url)) {
779 my $c = $gs->rev_db_get($rev);
780 if ($c && $c eq $_) {
781 close $fh; # break the pipe
782 return ($url, $rev, $uuid, $gs);
783 }
784 }
785 }
Eric Wong905f8b72007-02-16 03:22:40 -0800786 unshift @$refs, $_ if $refs;
787 }
Eric Wong13c823f2007-04-08 00:59:19 -0700788 command_close_pipe($fh, $ctx);
789 (undef, undef, undef, undef);
Eric Wong905f8b72007-02-16 03:22:40 -0800790}
791
Eric Wong9b981fc2007-01-11 12:14:21 -0800792package Git::SVN;
793use strict;
794use warnings;
Eric Wongecc712d2007-01-31 12:28:10 -0800795use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
Eric Wong62e349d2007-02-16 19:57:29 -0800796 $_repack $_repack_flags $_use_svm_props $_head
Eric Wongbefc9ad2007-02-17 02:53:07 -0800797 $_use_svnsync_props $no_reuse_existing/;
Eric Wong9b981fc2007-01-11 12:14:21 -0800798use Carp qw/croak/;
799use File::Path qw/mkpath/;
Eric Wong373274f2007-01-31 13:54:23 -0800800use File::Copy qw/copy/;
Eric Wong9b981fc2007-01-11 12:14:21 -0800801use IPC::Open3;
802
Eric Wongecc712d2007-01-31 12:28:10 -0800803my $_repack_nr;
Eric Wong9b981fc2007-01-11 12:14:21 -0800804# properties that we do not log:
805my %SKIP_PROP;
806BEGIN {
807 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
808 svn:special svn:executable
809 svn:entry:committed-rev
810 svn:entry:last-author
811 svn:entry:uuid
812 svn:entry:committed-date/;
Eric Wong91b03282007-02-11 00:51:33 -0800813
814 # some options are read globally, but can be overridden locally
815 # per [svn-remote "..."] section. Command-line options will *NOT*
816 # override options set in an [svn-remote "..."] section
817 my $e;
Eric Wong62e349d2007-02-16 19:57:29 -0800818 foreach (qw/follow_parent no_metadata use_svm_props
819 use_svnsync_props/) {
Eric Wong91b03282007-02-11 00:51:33 -0800820 my $key = $_;
821 $key =~ tr/_//d;
822 $e .= "sub $_ {
823 my (\$self) = \@_;
824 return \$self->{-$_} if exists \$self->{-$_};
825 my \$k = \"svn-remote.\$self->{repo_id}\.$key\";
826 eval { command_oneline(qw/config --get/, \$k) };
827 if (\$@) {
828 \$self->{-$_} = \$Git::SVN::_$_;
829 } else {
830 my \$v = command_oneline(qw/config --bool/,\$k);
831 \$self->{-$_} = \$v eq 'false' ? 0 : 1;
832 }
833 return \$self->{-$_} }\n";
834 }
835 $e .= "1;\n";
836 eval $e or die $@;
Eric Wong9b981fc2007-01-11 12:14:21 -0800837}
838
Eric Wong373274f2007-01-31 13:54:23 -0800839my %LOCKFILES;
840END { unlink keys %LOCKFILES if %LOCKFILES }
841
Eric Wong4bb9ed02007-02-03 13:29:17 -0800842sub resolve_local_globs {
843 my ($url, $fetch, $glob_spec) = @_;
844 return unless defined $glob_spec;
845 my $ref = $glob_spec->{ref};
846 my $path = $glob_spec->{path};
847 foreach (command(qw#for-each-ref --format=%(refname) refs/remotes#)) {
848 next unless m#^refs/remotes/$ref->{regex}$#;
849 my $p = $1;
850 my $pathname = $path->full_path($p);
851 my $refname = $ref->full_path($p);
852 if (my $existing = $fetch->{$pathname}) {
853 if ($existing ne $refname) {
854 die "Refspec conflict:\n",
855 "existing: refs/remotes/$existing\n",
856 " globbed: refs/remotes/$refname\n";
857 }
858 my $u = (::cmt_metadata("refs/remotes/$refname"))[0];
Eric Wong4e9f6cc2007-02-09 12:17:57 -0800859 $u =~ s!^\Q$url\E(/|$)!! or die
Eric Wong4bb9ed02007-02-03 13:29:17 -0800860 "refs/remotes/$refname: '$url' not found in '$u'\n";
861 if ($pathname ne $u) {
862 warn "W: Refspec glob conflict ",
863 "(ref: refs/remotes/$refname):\n",
864 "expected path: $pathname\n",
865 " real path: $u\n",
866 "Continuing ahead with $u\n";
867 next;
868 }
869 } else {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800870 $fetch->{$pathname} = $refname;
871 }
872 }
873}
874
Eric Wonge98671e2007-02-14 02:21:19 -0800875sub parse_revision_argument {
876 my ($base, $head) = @_;
877 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
878 return ($base, $head);
879 }
880 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
881 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
882 return ($head, $head) if ($::_revision eq 'HEAD');
883 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
884 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
885 die "revision argument: $::_revision not understood by git-svn\n";
886}
887
Eric Wong0af9c9f2007-01-27 22:28:56 -0800888sub fetch_all {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800889 my ($repo_id, $remotes) = @_;
Eric Wong905f8b72007-02-16 03:22:40 -0800890 if (ref $repo_id) {
891 my $gs = $repo_id;
892 $repo_id = undef;
893 $repo_id = $gs->{repo_id};
894 }
895 $remotes ||= read_all_remotes();
Eric Wong7447b4b2007-02-14 18:38:46 -0800896 my $remote = $remotes->{$repo_id} or
897 die "[svn-remote \"$repo_id\"] unknown\n";
Eric Wonge5181922007-02-08 12:53:57 -0800898 my $fetch = $remote->{fetch};
Eric Wong7447b4b2007-02-14 18:38:46 -0800899 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
Eric Wonge5181922007-02-08 12:53:57 -0800900 my (@gs, @globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -0800901 my $ra = Git::SVN::Ra->new($url);
Eric Wong26a62d52007-02-12 13:25:25 -0800902 my $uuid = $ra->get_uuid;
Eric Wong0af9c9f2007-01-27 22:28:56 -0800903 my $head = $ra->get_latest_revnum;
Eric Wong28710f72007-02-14 13:32:21 -0800904 my $base = defined $fetch ? $head : 0;
Eric Wonge5181922007-02-08 12:53:57 -0800905
906 # read the max revs for wildcard expansion (branches/*, tags/*)
907 foreach my $t (qw/branches tags/) {
908 defined $remote->{$t} or next;
909 push @globs, $remote->{$t};
Eric Wong93f26892007-02-11 01:20:26 -0800910 my $max_rev = eval { tmp_config(qw/--int --get/,
911 "svn-remote.$repo_id.${t}-maxRev") };
912 if (defined $max_rev && ($max_rev < $base)) {
913 $base = $max_rev;
Eric Wongd6d33462007-02-16 04:05:33 -0800914 } elsif (!defined $max_rev) {
915 $base = 0;
Eric Wonge5181922007-02-08 12:53:57 -0800916 }
917 }
918
Eric Wongdb03cd22007-02-13 00:38:02 -0800919 if ($fetch) {
920 foreach my $p (sort keys %$fetch) {
921 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
922 my $lr = $gs->rev_db_max;
923 if (defined $lr) {
924 $base = $lr if ($lr < $base);
925 }
926 push @gs, $gs;
Eric Wong0af9c9f2007-01-27 22:28:56 -0800927 }
Eric Wong0af9c9f2007-01-27 22:28:56 -0800928 }
Eric Wonge98671e2007-02-14 02:21:19 -0800929
930 ($base, $head) = parse_revision_argument($base, $head);
Eric Wonge5181922007-02-08 12:53:57 -0800931 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -0800932}
933
Eric Wong47e39c52007-01-21 04:27:09 -0800934sub read_all_remotes {
935 my $r = {};
Eric Wong8b8fc062007-01-22 11:44:57 -0800936 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
Eric Wong47e39c52007-01-21 04:27:09 -0800937 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) {
938 $r->{$1}->{fetch}->{$2} = $3;
939 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
940 $r->{$1}->{url} = $2;
Eric Wong4bb9ed02007-02-03 13:29:17 -0800941 } elsif (m!^(.+)\.(branches|tags)=
942 (.*):refs/remotes/(.+)\s*$/!x) {
943 my ($p, $g) = ($3, $4);
944 my $rs = $r->{$1}->{$2} = {
Eric Wonge5181922007-02-08 12:53:57 -0800945 t => $2,
Eric Wong93f26892007-02-11 01:20:26 -0800946 remote => $1,
Eric Wong4bb9ed02007-02-03 13:29:17 -0800947 path => Git::SVN::GlobSpec->new($p),
948 ref => Git::SVN::GlobSpec->new($g) };
949 if (length($rs->{ref}->{right}) != 0) {
950 die "The '*' glob character must be the last ",
951 "character of '$g'\n";
952 }
Eric Wong47e39c52007-01-21 04:27:09 -0800953 }
954 }
955 $r;
956}
957
Eric Wongecc712d2007-01-31 12:28:10 -0800958sub init_vars {
959 if (defined $_repack) {
960 $_repack = 1000 if ($_repack <= 0);
961 $_repack_nr = $_repack;
962 $_repack_flags ||= '-d';
963 }
964}
965
Eric Wongb805b442007-01-22 13:52:04 -0800966sub verify_remotes_sanity {
Eric Wong536c4b02007-01-23 11:35:53 -0800967 return unless -d $ENV{GIT_DIR};
Eric Wongb805b442007-01-22 13:52:04 -0800968 my %seen;
969 foreach (command(qw/config -l/)) {
970 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
971 if ($seen{$1}) {
972 die "Remote ref refs/remote/$1 is tracked by",
973 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
974 "Please resolve this ambiguity in ",
975 "your git configuration file before ",
976 "continuing\n";
977 }
978 $seen{$1} = $_;
979 }
980 }
981}
982
Eric Wong47e39c52007-01-21 04:27:09 -0800983# we allow more chars than remotes2config.sh...
Eric Wong706587f2007-01-18 17:50:01 -0800984sub sanitize_remote_name {
985 my ($name) = @_;
Eric Wong47e39c52007-01-21 04:27:09 -0800986 $name =~ tr{A-Za-z0-9:,/+-}{.}c;
Eric Wong706587f2007-01-18 17:50:01 -0800987 $name;
988}
989
Eric Wonge6434f82007-01-23 16:29:23 -0800990sub find_existing_remote {
991 my ($url, $remotes) = @_;
Eric Wongbefc9ad2007-02-17 02:53:07 -0800992 return undef if $no_reuse_existing;
Eric Wonge6434f82007-01-23 16:29:23 -0800993 my $existing;
994 foreach my $repo_id (keys %$remotes) {
995 my $u = $remotes->{$repo_id}->{url} or next;
996 next if $u ne $url;
997 $existing = $repo_id;
998 last;
999 }
1000 $existing;
1001}
1002
1003sub init_remote_config {
Eric Wongd8115c52007-02-01 03:30:31 -08001004 my ($self, $url, $no_write) = @_;
Eric Wonge6434f82007-01-23 16:29:23 -08001005 $url =~ s!/+$!!; # strip trailing slash
1006 my $r = read_all_remotes();
1007 my $existing = find_existing_remote($url, $r);
1008 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001009 unless ($no_write) {
1010 print STDERR "Using existing ",
1011 "[svn-remote \"$existing\"]\n";
1012 }
Eric Wonge6434f82007-01-23 16:29:23 -08001013 $self->{repo_id} = $existing;
1014 } else {
1015 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
1016 $existing = find_existing_remote($min_url, $r);
1017 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001018 unless ($no_write) {
1019 print STDERR "Using existing ",
1020 "[svn-remote \"$existing\"]\n";
1021 }
Eric Wonge6434f82007-01-23 16:29:23 -08001022 $self->{repo_id} = $existing;
1023 }
1024 if ($min_url ne $url) {
Eric Wonge5181922007-02-08 12:53:57 -08001025 unless ($no_write) {
1026 print STDERR "Using higher level of URL: ",
1027 "$url => $min_url\n";
1028 }
Eric Wonge6434f82007-01-23 16:29:23 -08001029 my $old_path = $self->{path};
1030 $self->{path} = $url;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001031 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
Eric Wonge6434f82007-01-23 16:29:23 -08001032 if (length $old_path) {
1033 $self->{path} .= "/$old_path";
1034 }
1035 $url = $min_url;
1036 }
1037 }
1038 my $orig_url;
1039 if (!$existing) {
1040 # verify that we aren't overwriting anything:
1041 $orig_url = eval {
1042 command_oneline('config', '--get',
1043 "svn-remote.$self->{repo_id}.url")
1044 };
1045 if ($orig_url && ($orig_url ne $url)) {
1046 die "svn-remote.$self->{repo_id}.url already set: ",
1047 "$orig_url\nwanted to set to: $url\n";
1048 }
1049 }
1050 my ($xrepo_id, $xpath) = find_ref($self->refname);
1051 if (defined $xpath) {
1052 die "svn-remote.$xrepo_id.fetch already set to track ",
1053 "$xpath:refs/remotes/", $self->refname, "\n";
1054 }
Eric Wongd8115c52007-02-01 03:30:31 -08001055 unless ($no_write) {
1056 command_noisy('config',
1057 "svn-remote.$self->{repo_id}.url", $url);
1058 command_noisy('config', '--add',
1059 "svn-remote.$self->{repo_id}.fetch",
1060 "$self->{path}:".$self->refname);
1061 }
Eric Wonge6434f82007-01-23 16:29:23 -08001062 $self->{url} = $url;
1063}
1064
Eric Wonga8ae2622007-02-13 14:22:11 -08001065sub find_by_url { # repos_root and, path are optional
1066 my ($class, $full_url, $repos_root, $path) = @_;
Eric Wong1a97a502007-02-20 00:43:19 -08001067 return undef unless defined $full_url;
Eric Wonga8ae2622007-02-13 14:22:11 -08001068 my $remotes = read_all_remotes();
1069 if (defined $full_url && defined $repos_root && !defined $path) {
1070 $path = $full_url;
1071 $path =~ s#^\Q$repos_root\E(?:/|$)##;
1072 }
1073 foreach my $repo_id (keys %$remotes) {
1074 my $u = $remotes->{$repo_id}->{url} or next;
1075 next if defined $repos_root && $repos_root ne $u;
1076
1077 my $fetch = $remotes->{$repo_id}->{fetch} || {};
1078 foreach (qw/branches tags/) {
1079 resolve_local_globs($u, $fetch,
1080 $remotes->{$repo_id}->{$_});
1081 }
1082 my $p = $path;
1083 unless (defined $p) {
1084 $p = $full_url;
1085 $p =~ s#^\Q$u\E(?:/|$)## or next;
1086 }
1087 foreach my $f (keys %$fetch) {
1088 next if $f ne $p;
1089 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
1090 }
1091 }
1092 undef;
1093}
1094
Eric Wong9b981fc2007-01-11 12:14:21 -08001095sub init {
Eric Wongd8115c52007-02-01 03:30:31 -08001096 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08001097 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong9b981fc2007-01-11 12:14:21 -08001098 if (defined $url) {
Eric Wongd8115c52007-02-01 03:30:31 -08001099 $self->init_remote_config($url, $no_write);
Eric Wong9b981fc2007-01-11 12:14:21 -08001100 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001101 $self;
1102}
1103
Eric Wong706587f2007-01-18 17:50:01 -08001104sub find_ref {
1105 my ($ref_id) = @_;
1106 foreach (command(qw/config -l/)) {
1107 next unless m!^svn-remote\.(.+)\.fetch=
1108 \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x;
1109 my ($repo_id, $path, $ref) = ($1, $2, $3);
1110 if ($ref eq $ref_id) {
1111 $path = '' if ($path =~ m#^\./?#);
1112 return ($repo_id, $path);
1113 }
1114 }
1115 (undef, undef, undef);
1116}
1117
Eric Wong9b981fc2007-01-11 12:14:21 -08001118sub new {
Eric Wong706587f2007-01-18 17:50:01 -08001119 my ($class, $ref_id, $repo_id, $path) = @_;
1120 if (defined $ref_id && !defined $repo_id && !defined $path) {
1121 ($repo_id, $path) = find_ref($ref_id);
1122 if (!defined $repo_id) {
1123 die "Could not find a \"svn-remote.*.fetch\" key ",
1124 "in the repository configuration matching: ",
1125 "refs/remotes/$ref_id\n";
1126 }
1127 }
1128 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong8b8fc062007-01-22 11:44:57 -08001129 if (!defined $self->{path} || !length $self->{path}) {
1130 my $fetch = command_oneline('config', '--get',
1131 "svn-remote.$repo_id.fetch",
1132 ":refs/remotes/$ref_id\$") or
1133 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1134 "\":refs/remotes/$ref_id\$\" in config\n";
1135 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
1136 }
Eric Wong706587f2007-01-18 17:50:01 -08001137 $self->{url} = command_oneline('config', '--get',
1138 "svn-remote.$repo_id.url") or
1139 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
Eric Wongd6d33462007-02-16 04:05:33 -08001140 $self->rebuild;
Eric Wong9b981fc2007-01-11 12:14:21 -08001141 $self;
1142}
1143
Eric Wong706587f2007-01-18 17:50:01 -08001144sub refname { "refs/remotes/$_[0]->{ref_id}" }
Eric Wong9b981fc2007-01-11 12:14:21 -08001145
Eric Wong26a62d52007-02-12 13:25:25 -08001146sub svm_uuid {
1147 my ($self) = @_;
1148 return $self->{svm}->{uuid} if $self->svm;
1149 $self->ra;
1150 unless ($self->{svm}) {
1151 die "SVM UUID not cached, and reading remotely failed\n";
1152 }
1153 $self->{svm}->{uuid};
1154}
Eric Wong8a49ee92007-02-10 20:46:50 -08001155
Eric Wong26a62d52007-02-12 13:25:25 -08001156sub svm {
1157 my ($self) = @_;
1158 return $self->{svm} if $self->{svm};
1159 my $svm;
Eric Wong8a49ee92007-02-10 20:46:50 -08001160 # see if we have it in our config, first:
1161 eval {
Eric Wong26a62d52007-02-12 13:25:25 -08001162 my $section = "svn-remote.$self->{repo_id}";
1163 $svm = {
Eric Wong93f26892007-02-11 01:20:26 -08001164 source => tmp_config('--get', "$section.svm-source"),
1165 uuid => tmp_config('--get', "$section.svm-uuid"),
Eric Wongbefc9ad2007-02-17 02:53:07 -08001166 replace => tmp_config('--get', "$section.svm-replace"),
Eric Wong8a49ee92007-02-10 20:46:50 -08001167 }
1168 };
Eric Wongbefc9ad2007-02-17 02:53:07 -08001169 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
1170 $self->{svm} = $svm;
1171 }
Eric Wong26a62d52007-02-12 13:25:25 -08001172 $self->{svm};
1173}
1174
1175sub _set_svm_vars {
1176 my ($self, $ra) = @_;
Eric Wongdb03cd22007-02-13 00:38:02 -08001177 return $ra if $self->svm;
Eric Wong26a62d52007-02-12 13:25:25 -08001178
Eric Wongdb03cd22007-02-13 00:38:02 -08001179 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08001180 "(svm:source, svm:uuid) ",
Eric Wongdb03cd22007-02-13 00:38:02 -08001181 "from the following URLs:\n" );
1182 sub read_svm_props {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001183 my ($self, $ra, $path, $r) = @_;
1184 my $props = ($ra->get_dir($path, $r))[2];
Eric Wongdb03cd22007-02-13 00:38:02 -08001185 my $src = $props->{'svm:source'};
Eric Wong8a49ee92007-02-10 20:46:50 -08001186 my $uuid = $props->{'svm:uuid'};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001187 return undef if (!$src || !$uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08001188
Eric Wongbefc9ad2007-02-17 02:53:07 -08001189 chomp($src, $uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08001190
Eric Wong8a49ee92007-02-10 20:46:50 -08001191 $uuid =~ m{^[0-9a-f\-]{30,}$}
1192 or die "doesn't look right - svm:uuid is '$uuid'\n";
Eric Wongbefc9ad2007-02-17 02:53:07 -08001193
1194 # the '!' is used to mark the repos_root!/relative/path
1195 $src =~ s{/?!/?}{/};
Eric Wongdb03cd22007-02-13 00:38:02 -08001196 $src =~ s{/+$}{}; # no trailing slashes please
Eric Wongbefc9ad2007-02-17 02:53:07 -08001197 # username is of no interest
Eric Wongdb03cd22007-02-13 00:38:02 -08001198 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
Eric Wong8a49ee92007-02-10 20:46:50 -08001199
Eric Wongbefc9ad2007-02-17 02:53:07 -08001200 my $replace = $ra->{url};
1201 $replace .= "/$path" if length $path;
1202
Eric Wongdb03cd22007-02-13 00:38:02 -08001203 my $section = "svn-remote.$self->{repo_id}";
Eric Wongbefc9ad2007-02-17 02:53:07 -08001204 tmp_config("$section.svm-source", $src);
1205 tmp_config("$section.svm-replace", $replace);
1206 tmp_config("$section.svm-uuid", $uuid);
1207 $self->{svm} = {
1208 source => $src,
1209 uuid => $uuid,
1210 replace => $replace
1211 };
Eric Wong8a49ee92007-02-10 20:46:50 -08001212 }
Eric Wongdb03cd22007-02-13 00:38:02 -08001213
1214 my $r = $ra->get_latest_revnum;
1215 my $path = $self->{path};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001216 my %tried;
Eric Wongdb03cd22007-02-13 00:38:02 -08001217 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001218 unless ($tried{"$self->{url}/$path"}) {
1219 return $ra if $self->read_svm_props($ra, $path, $r);
1220 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001221 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001222 $path =~ s#/?[^/]+$##;
Eric Wong8a49ee92007-02-10 20:46:50 -08001223 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001224 die "Path: '$path' should be ''\n" if $path ne '';
1225 return $ra if $self->read_svm_props($ra, $path, $r);
1226 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001227
1228 if ($ra->{repos_root} eq $self->{url}) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001229 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001230 }
1231
1232 # nope, make sure we're connected to the repository root:
1233 my $ok;
1234 my @tried_b;
1235 $path = $ra->{svn_path};
Eric Wongdb03cd22007-02-13 00:38:02 -08001236 $ra = Git::SVN::Ra->new($ra->{repos_root});
1237 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001238 unless ($tried{"$ra->{url}/$path"}) {
1239 $ok = $self->read_svm_props($ra, $path, $r);
1240 last if $ok;
1241 $tried{"$ra->{url}/$path"} = 1;
1242 }
1243 $path =~ s#/?[^/]+$##;
Eric Wongdb03cd22007-02-13 00:38:02 -08001244 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001245 die "Path: '$path' should be ''\n" if $path ne '';
1246 $ok ||= $self->read_svm_props($ra, $path, $r);
1247 $tried{"$ra->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001248 if (!$ok) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001249 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001250 }
1251 Git::SVN::Ra->new($self->{url});
Eric Wong8a49ee92007-02-10 20:46:50 -08001252}
1253
Eric Wong62e349d2007-02-16 19:57:29 -08001254sub svnsync {
1255 my ($self) = @_;
1256 return $self->{svnsync} if $self->{svnsync};
1257
1258 if ($self->no_metadata) {
1259 die "Can't have both 'noMetadata' and ",
1260 "'useSvnsyncProps' options set!\n";
1261 }
1262 if ($self->rewrite_root) {
1263 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1264 "options set!\n";
1265 }
1266
1267 my $svnsync;
1268 # see if we have it in our config, first:
1269 eval {
1270 my $section = "svn-remote.$self->{repo_id}";
1271 $svnsync = {
1272 url => tmp_config('--get', "$section.svnsync-url"),
1273 uuid => tmp_config('--get', "$section.svnsync-uuid"),
1274 }
1275 };
1276 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
1277 return $self->{svnsync} = $svnsync;
1278 }
1279
1280 my $err = "useSvnsyncProps set, but failed to read " .
1281 "svnsync property: svn:sync-from-";
1282 my $rp = $self->ra->rev_proplist(0);
1283
1284 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
1285 $url =~ m{^[a-z\+]+://} or
1286 die "doesn't look right - svn:sync-from-url is '$url'\n";
1287
1288 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
1289 $uuid =~ m{^[0-9a-f\-]{30,}$} or
1290 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1291
1292 my $section = "svn-remote.$self->{repo_id}";
1293 tmp_config('--add', "$section.svnsync-uuid", $uuid);
1294 tmp_config('--add', "$section.svnsync-url", $url);
1295 return $self->{svnsync} = { url => $url, uuid => $uuid };
1296}
1297
Eric Wong26a62d52007-02-12 13:25:25 -08001298# this allows us to memoize our SVN::Ra UUID locally and avoid a
1299# remote lookup (useful for 'git svn log').
1300sub ra_uuid {
1301 my ($self) = @_;
1302 unless ($self->{ra_uuid}) {
1303 my $key = "svn-remote.$self->{repo_id}.uuid";
1304 my $uuid = eval { tmp_config('--get', $key) };
1305 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
1306 $self->{ra_uuid} = $uuid;
1307 } else {
1308 die "ra_uuid called without URL\n" unless $self->{url};
1309 $self->{ra_uuid} = $self->ra->get_uuid;
1310 tmp_config('--add', $key, $self->{ra_uuid});
1311 }
1312 }
1313 $self->{ra_uuid};
1314}
1315
Eric Wong9b981fc2007-01-11 12:14:21 -08001316sub ra {
1317 my ($self) = shift;
Eric Wong8a49ee92007-02-10 20:46:50 -08001318 my $ra = Git::SVN::Ra->new($self->{url});
Eric Wong91b03282007-02-11 00:51:33 -08001319 if ($self->use_svm_props && !$self->{svm}) {
1320 if ($self->no_metadata) {
Eric Wong97ae0912007-02-11 15:21:24 -08001321 die "Can't have both 'noMetadata' and ",
1322 "'useSvmProps' options set!\n";
Eric Wong62e349d2007-02-16 19:57:29 -08001323 } elsif ($self->use_svnsync_props) {
1324 die "Can't have both 'useSvnsyncProps' and ",
1325 "'useSvmProps' options set!\n";
Eric Wong91b03282007-02-11 00:51:33 -08001326 }
Eric Wong26a62d52007-02-12 13:25:25 -08001327 $ra = $self->_set_svm_vars($ra);
Eric Wong8a49ee92007-02-10 20:46:50 -08001328 $self->{-want_revprops} = 1;
1329 }
1330 $ra;
Eric Wong9b981fc2007-01-11 12:14:21 -08001331}
1332
Eric Wong15710b62007-01-22 02:20:33 -08001333sub rel_path {
1334 my ($self) = @_;
1335 my $repos_root = $self->ra->{repos_root};
1336 return $self->{path} if ($self->{url} eq $repos_root);
Eric Wong0b594512007-03-25 16:35:31 -07001337 my $url = $self->{url} .
1338 (length $self->{path} ? "/$self->{path}" : $self->{path});
1339 $url =~ s!^\Q$repos_root\E(?:/+|$)!!g;
1340 $url;
Eric Wong15710b62007-01-22 02:20:33 -08001341}
1342
Eric Wong9b981fc2007-01-11 12:14:21 -08001343sub traverse_ignore {
1344 my ($self, $fh, $path, $r) = @_;
1345 $path =~ s#^/+##g;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08001346 my $ra = $self->ra;
1347 my ($dirent, undef, $props) = $ra->get_dir($path, $r);
Eric Wong9b981fc2007-01-11 12:14:21 -08001348 my $p = $path;
Eric Wong102a0a22007-03-04 00:15:29 -08001349 $p =~ s#^\Q$self->{path}\E(/|$)##;
Eric Wong9b981fc2007-01-11 12:14:21 -08001350 print $fh length $p ? "\n# $p\n" : "\n# /\n";
1351 if (my $s = $props->{'svn:ignore'}) {
1352 $s =~ s/[\r\n]+/\n/g;
1353 chomp $s;
1354 if (length $p == 0) {
1355 $s =~ s#\n#\n/$p#g;
1356 print $fh "/$s\n";
1357 } else {
1358 $s =~ s#\n#\n/$p/#g;
1359 print $fh "/$p/$s\n";
1360 }
1361 }
1362 foreach (sort keys %$dirent) {
1363 next if $dirent->{$_}->kind != $SVN::Node::dir;
1364 $self->traverse_ignore($fh, "$path/$_", $r);
1365 }
1366}
1367
Eric Wong3ebe8df2007-01-25 17:35:40 -08001368sub last_rev { ($_[0]->last_rev_commit)[0] }
1369sub last_commit { ($_[0]->last_rev_commit)[1] }
1370
Eric Wong9b981fc2007-01-11 12:14:21 -08001371# returns the newest SVN revision number and newest commit SHA1
1372sub last_rev_commit {
1373 my ($self) = @_;
1374 if (defined $self->{last_rev} && defined $self->{last_commit}) {
1375 return ($self->{last_rev}, $self->{last_commit});
1376 }
Eric Wongd2866f92007-01-11 12:26:16 -08001377 my $c = ::verify_ref($self->refname.'^0');
Eric Wong91b03282007-02-11 00:51:33 -08001378 if ($c && !$self->use_svm_props && !$self->no_metadata) {
Eric Wongd2866f92007-01-11 12:26:16 -08001379 my $rev = (::cmt_metadata($c))[1];
Eric Wong9b981fc2007-01-11 12:14:21 -08001380 if (defined $rev) {
1381 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
1382 return ($rev, $c);
1383 }
1384 }
Eric Wong26a62d52007-02-12 13:25:25 -08001385 my $db_path = $self->db_path;
1386 unless (-e $db_path) {
1387 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
1388 return (undef, undef);
1389 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001390 my $offset = -41; # from tail
1391 my $rl;
Eric Wong26a62d52007-02-12 13:25:25 -08001392 open my $fh, '<', $db_path or croak "$db_path not readable: $!\n";
Eric Wongce4b4af2007-01-31 17:57:36 -08001393 sysseek($fh, $offset, 2); # don't care for errors
1394 sysread($fh, $rl, 41) == 41 or return (undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08001395 chomp $rl;
Eric Wongce4b4af2007-01-31 17:57:36 -08001396 while (('0' x40) eq $rl && sysseek($fh, 0, 1) != 0) {
Eric Wong9b981fc2007-01-11 12:14:21 -08001397 $offset -= 41;
Eric Wongce4b4af2007-01-31 17:57:36 -08001398 sysseek($fh, $offset, 2); # don't care for errors
1399 sysread($fh, $rl, 41) == 41 or return (undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08001400 chomp $rl;
1401 }
Eric Wong91b03282007-02-11 00:51:33 -08001402 if ($c && $c ne $rl) {
Eric Wong26a62d52007-02-12 13:25:25 -08001403 die "$db_path and ", $self->refname,
Eric Wong9c93fee2007-01-31 17:22:31 -08001404 " inconsistent!:\n$c != $rl\n";
1405 }
Eric Wongce4b4af2007-01-31 17:57:36 -08001406 my $rev = sysseek($fh, 0, 1) or croak $!;
Eric Wong9b981fc2007-01-11 12:14:21 -08001407 $rev = ($rev - 41) / 41;
1408 close $fh or croak $!;
1409 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
1410 return ($rev, $c);
1411}
1412
Eric Wong3ebe8df2007-01-25 17:35:40 -08001413sub get_fetch_range {
1414 my ($self, $min, $max) = @_;
1415 $max ||= $self->ra->get_latest_revnum;
Eric Wong9c93fee2007-01-31 17:22:31 -08001416 $min ||= $self->rev_db_max;
Eric Wong3ebe8df2007-01-25 17:35:40 -08001417 (++$min, $max);
Eric Wong9b981fc2007-01-11 12:14:21 -08001418}
1419
Eric Wong8a49ee92007-02-10 20:46:50 -08001420sub tmp_config {
Eric Wong93f26892007-02-11 01:20:26 -08001421 my (@args) = @_;
Eric Wongb7e53482007-02-16 04:09:28 -08001422 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
1423 my $config = "$ENV{GIT_DIR}/svn/.metadata";
1424 if (-e $old_def_config && ! -e $config) {
1425 rename $old_def_config, $config or
1426 die "Failed rename $old_def_config => $config: $!\n";
1427 }
Eric Wong8a49ee92007-02-10 20:46:50 -08001428 my $old_config = $ENV{GIT_CONFIG};
Eric Wong93f26892007-02-11 01:20:26 -08001429 $ENV{GIT_CONFIG} = $config;
Eric Wong8a49ee92007-02-10 20:46:50 -08001430 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08001431 my @ret = eval {
1432 unless (-f $config) {
1433 mkfile($config);
1434 open my $fh, '>', $config or
1435 die "Can't open $config: $!\n";
1436 print $fh "; This file is used internally by ",
1437 "git-svn\n" or die
1438 "Couldn't write to $config: $!\n";
1439 print $fh "; You should not have to edit it\n" or
1440 die "Couldn't write to $config: $!\n";
1441 close $fh or die "Couldn't close $config: $!\n";
1442 }
1443 command('config', @args);
1444 };
Eric Wong8a49ee92007-02-10 20:46:50 -08001445 my $err = $@;
1446 if (defined $old_config) {
1447 $ENV{GIT_CONFIG} = $old_config;
1448 } else {
1449 delete $ENV{GIT_CONFIG};
1450 }
1451 die $err if $err;
1452 wantarray ? @ret : $ret[0];
1453}
1454
Eric Wong9b981fc2007-01-11 12:14:21 -08001455sub tmp_index_do {
1456 my ($self, $sub) = @_;
1457 my $old_index = $ENV{GIT_INDEX_FILE};
1458 $ENV{GIT_INDEX_FILE} = $self->{index};
Eric Wong8a49ee92007-02-10 20:46:50 -08001459 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08001460 my @ret = eval {
1461 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
1462 mkpath([$dir]) unless -d $dir;
1463 &$sub;
1464 };
Eric Wong8a49ee92007-02-10 20:46:50 -08001465 my $err = $@;
1466 if (defined $old_index) {
Eric Wong9b981fc2007-01-11 12:14:21 -08001467 $ENV{GIT_INDEX_FILE} = $old_index;
1468 } else {
1469 delete $ENV{GIT_INDEX_FILE};
1470 }
Eric Wong8a49ee92007-02-10 20:46:50 -08001471 die $err if $err;
Eric Wong9b981fc2007-01-11 12:14:21 -08001472 wantarray ? @ret : $ret[0];
1473}
1474
1475sub assert_index_clean {
1476 my ($self, $treeish) = @_;
1477
1478 $self->tmp_index_do(sub {
1479 command_noisy('read-tree', $treeish) unless -e $self->{index};
1480 my $x = command_oneline('write-tree');
1481 my ($y) = (command(qw/cat-file commit/, $treeish) =~
1482 /^tree ($::sha1)/mo);
Eric Wonge8d120b2007-02-14 16:29:52 -08001483 return if $y eq $x;
1484
1485 warn "Index mismatch: $y != $x\nrereading $treeish\n";
1486 unlink $self->{index} or die "unlink $self->{index}: $!\n";
1487 command_noisy('read-tree', $treeish);
Eric Wong9b981fc2007-01-11 12:14:21 -08001488 $x = command_oneline('write-tree');
1489 if ($y ne $x) {
1490 ::fatal "trees ($treeish) $y != $x\n",
1491 "Something is seriously wrong...\n";
1492 }
1493 });
1494}
1495
1496sub get_commit_parents {
Eric Wong0af9c9f2007-01-27 22:28:56 -08001497 my ($self, $log_entry) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08001498 my (%seen, @ret, @tmp);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001499 # legacy support for 'set-tree'; this is only used by set_tree_cb:
1500 if (my $ip = $self->{inject_parents}) {
1501 if (my $commit = delete $ip->{$log_entry->{revision}}) {
1502 push @tmp, $commit;
Eric Wong9b981fc2007-01-11 12:14:21 -08001503 }
1504 }
Eric Wongd2866f92007-01-11 12:26:16 -08001505 if (my $cur = ::verify_ref($self->refname.'^0')) {
Eric Wong9b981fc2007-01-11 12:14:21 -08001506 push @tmp, $cur;
1507 }
Eric Wong44320b92007-01-13 22:35:53 -08001508 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
Eric Wong9b981fc2007-01-11 12:14:21 -08001509 while (my $p = shift @tmp) {
1510 next if $seen{$p};
1511 $seen{$p} = 1;
1512 push @ret, $p;
1513 # MAXPARENT is defined to 16 in commit-tree.c:
1514 last if @ret >= 16;
1515 }
1516 if (@tmp) {
Eric Wong44320b92007-01-13 22:35:53 -08001517 die "r$log_entry->{revision}: No room for parents:\n\t",
Eric Wong9b981fc2007-01-11 12:14:21 -08001518 join("\n\t", @tmp), "\n";
1519 }
1520 @ret;
1521}
1522
Eric Wongaea736c2007-02-16 19:15:21 -08001523sub rewrite_root {
1524 my ($self) = @_;
1525 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
1526 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
1527 my $rwr = eval { command_oneline(qw/config --get/, $k) };
1528 if ($rwr) {
1529 $rwr =~ s#/+$##;
1530 if ($rwr !~ m#^[a-z\+]+://#) {
1531 die "$rwr is not a valid URL (key: $k)\n";
1532 }
1533 }
1534 $self->{-rewrite_root} = $rwr;
1535}
1536
1537sub metadata_url {
1538 my ($self) = @_;
1539 ($self->rewrite_root || $self->{url}) .
1540 (length $self->{path} ? '/' . $self->{path} : '');
1541}
1542
Eric Wong706587f2007-01-18 17:50:01 -08001543sub full_url {
Eric Wong9b981fc2007-01-11 12:14:21 -08001544 my ($self) = @_;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08001545 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
Eric Wong9b981fc2007-01-11 12:14:21 -08001546}
1547
1548sub do_git_commit {
Eric Wong0af9c9f2007-01-27 22:28:56 -08001549 my ($self, $log_entry) = @_;
Eric Wong8a603772007-01-31 02:45:50 -08001550 my $lr = $self->last_rev;
1551 if (defined $lr && $lr >= $log_entry->{revision}) {
1552 die "Last fetched revision of ", $self->refname,
1553 " was r$lr, but we are about to fetch: ",
1554 "r$log_entry->{revision}!\n";
1555 }
Eric Wong44320b92007-01-13 22:35:53 -08001556 if (my $c = $self->rev_db_get($log_entry->{revision})) {
1557 croak "$log_entry->{revision} = $c already exists! ",
Eric Wong9b981fc2007-01-11 12:14:21 -08001558 "Why are we refetching it?\n";
1559 }
Eric Wongdb03cd22007-02-13 00:38:02 -08001560 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $log_entry->{name};
1561 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} =
1562 $log_entry->{email};
Eric Wong44320b92007-01-13 22:35:53 -08001563 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
Eric Wong9b981fc2007-01-11 12:14:21 -08001564
Eric Wong44320b92007-01-13 22:35:53 -08001565 my $tree = $log_entry->{tree};
Eric Wong9b981fc2007-01-11 12:14:21 -08001566 if (!defined $tree) {
1567 $tree = $self->tmp_index_do(sub {
1568 command_oneline('write-tree') });
1569 }
1570 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
1571
1572 my @exec = ('git-commit-tree', $tree);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001573 foreach ($self->get_commit_parents($log_entry)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08001574 push @exec, '-p', $_;
1575 }
1576 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1577 or croak $!;
Eric Wong44320b92007-01-13 22:35:53 -08001578 print $msg_fh $log_entry->{log} or croak $!;
Eric Wong91b03282007-02-11 00:51:33 -08001579 unless ($self->no_metadata) {
Eric Wong8a49ee92007-02-10 20:46:50 -08001580 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
1581 or croak $!;
Eric Wong9760adc2007-01-31 03:06:56 -08001582 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001583 $msg_fh->flush == 0 or croak $!;
1584 close $msg_fh or croak $!;
1585 chomp(my $commit = do { local $/; <$out_fh> });
1586 close $out_fh or croak $!;
1587 waitpid $pid, 0;
1588 croak $? if $?;
1589 if ($commit !~ /^$::sha1$/o) {
1590 die "Failed to commit, invalid sha1: $commit\n";
1591 }
1592
Eric Wong373274f2007-01-31 13:54:23 -08001593 $self->rev_db_set($log_entry->{revision}, $commit, 1);
Eric Wong9b981fc2007-01-11 12:14:21 -08001594
Eric Wong44320b92007-01-13 22:35:53 -08001595 $self->{last_rev} = $log_entry->{revision};
Eric Wong9b981fc2007-01-11 12:14:21 -08001596 $self->{last_commit} = $commit;
Eric Wong8a49ee92007-02-10 20:46:50 -08001597 print "r$log_entry->{revision}";
1598 if (defined $log_entry->{svm_revision}) {
1599 print " (\@$log_entry->{svm_revision})";
Eric Wong26a62d52007-02-12 13:25:25 -08001600 $self->rev_db_set($log_entry->{svm_revision}, $commit,
1601 0, $self->svm_uuid);
Eric Wong8a49ee92007-02-10 20:46:50 -08001602 }
1603 print " = $commit ($self->{ref_id})\n";
Eric Wongecc712d2007-01-31 12:28:10 -08001604 if (defined $_repack && (--$_repack_nr == 0)) {
1605 $_repack_nr = $_repack;
1606 # repack doesn't use any arguments with spaces in them, does it?
1607 print "Running git repack $_repack_flags ...\n";
1608 command_noisy('repack', split(/\s+/, $_repack_flags));
1609 print "Done repacking\n";
1610 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001611 return $commit;
1612}
1613
Eric Wongfbcc1732007-02-06 18:35:30 -08001614sub match_paths {
1615 my ($self, $paths, $r) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001616 return 1 if $self->{path} eq '';
Eric Wongd542aed2007-02-09 02:19:41 -08001617 if (my $path = $paths->{"/$self->{path}"}) {
1618 return ($path->{action} eq 'D') ? 0 : 1;
1619 }
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001620 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
Eric Wongfbcc1732007-02-06 18:35:30 -08001621 if (grep /$self->{path_regex}/, keys %$paths) {
1622 return 1;
1623 }
1624 my $c = '';
1625 foreach (split m#/#, $self->{path}) {
1626 $c .= "/$_";
Eric Wong74a81222007-02-10 13:28:50 -08001627 next unless ($paths->{$c} &&
1628 ($paths->{$c}->{action} =~ /^[AR]$/));
Eric Wonge5181922007-02-08 12:53:57 -08001629 if ($self->ra->check_path($self->{path}, $r) ==
1630 $SVN::Node::dir) {
Eric Wongfbcc1732007-02-06 18:35:30 -08001631 return 1;
1632 }
1633 }
1634 return 0;
1635}
1636
Eric Wong15710b62007-01-22 02:20:33 -08001637sub find_parent_branch {
1638 my ($self, $paths, $rev) = @_;
Eric Wong91b03282007-02-11 00:51:33 -08001639 return undef unless $self->follow_parent;
Eric Wonge5a0b242007-01-25 15:44:54 -08001640 unless (defined $paths) {
Eric Wongc7eba712007-01-31 03:45:28 -08001641 my $err_handler = $SVN::Error::handler;
1642 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
Eric Wongd4eff2b2007-01-30 14:04:22 -08001643 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, sub {
1644 $paths =
1645 Git::SVN::Ra::dup_changed_paths($_[0]) });
Eric Wongc7eba712007-01-31 03:45:28 -08001646 $SVN::Error::handler = $err_handler;
Eric Wonge5a0b242007-01-25 15:44:54 -08001647 }
1648 return undef unless defined $paths;
Eric Wong15710b62007-01-22 02:20:33 -08001649
1650 # look for a parent from another branch:
Eric Wong7f578c52007-01-24 02:16:25 -08001651 my @b_path_components = split m#/#, $self->rel_path;
1652 my @a_path_components;
1653 my $i;
1654 while (@b_path_components) {
1655 $i = $paths->{'/'.join('/', @b_path_components)};
Eric Wong74a81222007-02-10 13:28:50 -08001656 last if $i && defined $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08001657 unshift(@a_path_components, pop(@b_path_components));
1658 }
Eric Wong74a81222007-02-10 13:28:50 -08001659 return undef unless defined $i && defined $i->{copyfrom_path};
1660 my $branch_from = $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08001661 if (@a_path_components) {
1662 print STDERR "branch_from: $branch_from => ";
1663 $branch_from .= '/'.join('/', @a_path_components);
1664 print STDERR $branch_from, "\n";
1665 }
Eric Wong3ebe8df2007-01-25 17:35:40 -08001666 my $r = $i->{copyfrom_rev};
Eric Wong15710b62007-01-22 02:20:33 -08001667 my $repos_root = $self->ra->{repos_root};
1668 my $url = $self->ra->{url};
1669 my $new_url = $repos_root . $branch_from;
1670 print STDERR "Found possible branch point: ",
1671 "$new_url => ", $self->full_url, ", $r\n";
1672 $branch_from =~ s#^/##;
Eric Wonga8ae2622007-02-13 14:22:11 -08001673 my $gs = Git::SVN->find_by_url($new_url, $repos_root, $branch_from);
Eric Wong15710b62007-01-22 02:20:33 -08001674 unless ($gs) {
Eric Wongce2a0f22007-01-28 21:34:23 -08001675 my $ref_id = $self->{ref_id};
1676 $ref_id =~ s/\@\d+$//;
1677 $ref_id .= "\@$r";
Eric Wong15710b62007-01-22 02:20:33 -08001678 # just grow a tail if we're not unique enough :x
1679 $ref_id .= '-' while find_ref($ref_id);
Eric Wongce2a0f22007-01-28 21:34:23 -08001680 print STDERR "Initializing parent: $ref_id\n";
Eric Wongd8115c52007-02-01 03:30:31 -08001681 $gs = Git::SVN->init($new_url, '', $ref_id, $ref_id, 1);
Eric Wong15710b62007-01-22 02:20:33 -08001682 }
1683 my ($r0, $parent) = $gs->find_rev_before($r, 1);
Eric Wong91b03282007-02-11 00:51:33 -08001684 if (!defined $r0 || !defined $parent) {
Eric Wongd627de62007-04-15 03:01:29 -07001685 my ($base, $head) = parse_revision_argument(0, $r);
1686 if ($base <= $r) {
1687 $gs->fetch($base, $r);
1688 }
Eric Wong15710b62007-01-22 02:20:33 -08001689 ($r0, $parent) = $gs->last_rev_commit;
1690 }
Eric Wongef70de92007-02-01 04:12:41 -08001691 if (defined $r0 && defined $parent) {
Eric Wong15710b62007-01-22 02:20:33 -08001692 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n";
Eric Wong15710b62007-01-22 02:20:33 -08001693 my $ed;
1694 if ($self->ra->can_do_switch) {
Eric Wong2e5e2482007-02-23 02:21:59 -08001695 $self->assert_index_clean($parent);
Eric Wong8b8fc062007-01-22 11:44:57 -08001696 print STDERR "Following parent with do_switch\n";
Eric Wong15710b62007-01-22 02:20:33 -08001697 # do_switch works with svn/trunk >= r22312, but that
Eric Wong2b27f6c2007-01-28 04:59:05 -08001698 # is not included with SVN 1.4.3 (the latest version
Eric Wong15710b62007-01-22 02:20:33 -08001699 # at the moment), so we can't rely on it
1700 $self->{last_commit} = $parent;
1701 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08001702 $gs->ra->gs_do_switch($r0, $rev, $gs,
Eric Wong15710b62007-01-22 02:20:33 -08001703 $self->full_url, $ed)
1704 or die "SVN connection failed somewhere...\n";
1705 } else {
Eric Wong8b8fc062007-01-22 11:44:57 -08001706 print STDERR "Following parent with do_update\n";
Eric Wong15710b62007-01-22 02:20:33 -08001707 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08001708 $self->ra->gs_do_update($rev, $rev, $self, $ed)
Eric Wong15710b62007-01-22 02:20:33 -08001709 or die "SVN connection failed somewhere...\n";
1710 }
Eric Wongf7c3fc42007-01-29 18:34:55 -08001711 print STDERR "Successfully followed parent\n";
Eric Wong15710b62007-01-22 02:20:33 -08001712 return $self->make_log_entry($rev, [$parent], $ed);
1713 }
Eric Wong15710b62007-01-22 02:20:33 -08001714 return undef;
1715}
1716
Eric Wong9b981fc2007-01-11 12:14:21 -08001717sub do_fetch {
Eric Wong706587f2007-01-18 17:50:01 -08001718 my ($self, $paths, $rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08001719 my $ed;
Eric Wong9b981fc2007-01-11 12:14:21 -08001720 my ($last_rev, @parents);
Eric Wongb9dffd82007-02-09 01:28:30 -08001721 if (my $lc = $self->last_commit) {
1722 # we can have a branch that was deleted, then re-added
1723 # under the same name but copied from another path, in
1724 # which case we'll have multiple parents (we don't
1725 # want to break the original ref, nor lose copypath info):
1726 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1727 push @{$log_entry->{parents}}, $lc;
1728 return $log_entry;
1729 }
Eric Wong15710b62007-01-22 02:20:33 -08001730 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08001731 $last_rev = $self->{last_rev};
Eric Wongb9dffd82007-02-09 01:28:30 -08001732 $ed->{c} = $lc;
1733 @parents = ($lc);
Eric Wong9b981fc2007-01-11 12:14:21 -08001734 } else {
1735 $last_rev = $rev;
Eric Wong15710b62007-01-22 02:20:33 -08001736 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1737 return $log_entry;
1738 }
1739 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08001740 }
Eric Wong8a603772007-01-31 02:45:50 -08001741 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08001742 die "SVN connection failed somewhere...\n";
1743 }
1744 $self->make_log_entry($rev, \@parents, $ed);
1745}
1746
Eric Wong97f69872007-01-25 11:53:13 -08001747sub get_untracked {
1748 my ($self, $ed) = @_;
1749 my @out;
1750 my $h = $ed->{empty};
Eric Wong9b981fc2007-01-11 12:14:21 -08001751 foreach (sort keys %$h) {
1752 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
Eric Wong97f69872007-01-25 11:53:13 -08001753 push @out, " $act: " . uri_encode($_);
Eric Wong9b981fc2007-01-11 12:14:21 -08001754 warn "W: $act: $_\n";
1755 }
1756 foreach my $t (qw/dir_prop file_prop/) {
Eric Wong97f69872007-01-25 11:53:13 -08001757 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08001758 foreach my $path (sort keys %$h) {
1759 my $ppath = $path eq '' ? '.' : $path;
1760 foreach my $prop (sort keys %{$h->{$path}}) {
Eric Wong1ce255d2007-01-14 23:21:16 -08001761 next if $SKIP_PROP{$prop};
Eric Wong9b981fc2007-01-11 12:14:21 -08001762 my $v = $h->{$path}->{$prop};
Eric Wong97f69872007-01-25 11:53:13 -08001763 my $t_ppath_prop = "$t: " .
1764 uri_encode($ppath) . ' ' .
1765 uri_encode($prop);
Eric Wong9b981fc2007-01-11 12:14:21 -08001766 if (defined $v) {
Eric Wong97f69872007-01-25 11:53:13 -08001767 push @out, " +$t_ppath_prop " .
1768 uri_encode($v);
Eric Wong9b981fc2007-01-11 12:14:21 -08001769 } else {
Eric Wong97f69872007-01-25 11:53:13 -08001770 push @out, " -$t_ppath_prop";
Eric Wong9b981fc2007-01-11 12:14:21 -08001771 }
1772 }
1773 }
1774 }
1775 foreach my $t (qw/absent_file absent_directory/) {
Eric Wong97f69872007-01-25 11:53:13 -08001776 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08001777 foreach my $parent (sort keys %$h) {
1778 foreach my $path (sort @{$h->{$parent}}) {
Eric Wong97f69872007-01-25 11:53:13 -08001779 push @out, " $t: " .
1780 uri_encode("$parent/$path");
Eric Wong9b981fc2007-01-11 12:14:21 -08001781 warn "W: $t: $parent/$path ",
1782 "Insufficient permissions?\n";
1783 }
1784 }
1785 }
Eric Wong97f69872007-01-25 11:53:13 -08001786 \@out;
Eric Wong9b981fc2007-01-11 12:14:21 -08001787}
1788
Eric Wong1c8443b2007-01-14 02:17:00 -08001789sub parse_svn_date {
1790 my $date = shift || return '+0000 1970-01-01 00:00:00';
1791 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
1792 (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x) or
1793 croak "Unable to parse date: $date\n";
1794 "+0000 $Y-$m-$d $H:$M:$S";
1795}
1796
1797sub check_author {
1798 my ($author) = @_;
1799 if (!defined $author || length $author == 0) {
1800 $author = '(no author)';
1801 }
1802 if (defined $::_authors && ! defined $::users{$author}) {
1803 die "Author: $author not defined in $::_authors file\n";
1804 }
1805 $author;
1806}
1807
Eric Wong9b981fc2007-01-11 12:14:21 -08001808sub make_log_entry {
Eric Wong97f69872007-01-25 11:53:13 -08001809 my ($self, $rev, $parents, $ed) = @_;
1810 my $untracked = $self->get_untracked($ed);
1811
Eric Wong9b981fc2007-01-11 12:14:21 -08001812 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08001813 print $un "r$rev\n" or croak $!;
1814 print $un $_, "\n" foreach @$untracked;
1815 my %log_entry = ( parents => $parents || [], revision => $rev,
1816 log => '');
Eric Wongfbcc1732007-02-06 18:35:30 -08001817
Eric Wong8a49ee92007-02-10 20:46:50 -08001818 my $headrev;
Eric Wongfbcc1732007-02-06 18:35:30 -08001819 my $logged = delete $self->{logged_rev_props};
Eric Wong8a49ee92007-02-10 20:46:50 -08001820 if (!$logged || $self->{-want_revprops}) {
Eric Wongfbcc1732007-02-06 18:35:30 -08001821 my $rp = $self->ra->rev_proplist($rev);
1822 foreach (sort keys %$rp) {
1823 my $v = $rp->{$_};
1824 if (/^svn:(author|date|log)$/) {
1825 $log_entry{$1} = $v;
Eric Wong8a49ee92007-02-10 20:46:50 -08001826 } elsif ($_ eq 'svm:headrev') {
1827 $headrev = $v;
Eric Wongfbcc1732007-02-06 18:35:30 -08001828 } else {
1829 print $un " rev_prop: ", uri_encode($_), ' ',
1830 uri_encode($v), "\n";
1831 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001832 }
Eric Wongfbcc1732007-02-06 18:35:30 -08001833 } else {
1834 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
Eric Wong9b981fc2007-01-11 12:14:21 -08001835 }
1836 close $un or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08001837
Eric Wong9b981fc2007-01-11 12:14:21 -08001838 $log_entry{date} = parse_svn_date($log_entry{date});
Eric Wong9b981fc2007-01-11 12:14:21 -08001839 $log_entry{log} .= "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001840 my $author = $log_entry{author} = check_author($log_entry{author});
1841 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
1842 : ($author, undef);
Eric Wong91b03282007-02-11 00:51:33 -08001843 if (defined $headrev && $self->use_svm_props) {
Eric Wongaea736c2007-02-16 19:15:21 -08001844 if ($self->rewrite_root) {
1845 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
1846 "options set!\n";
1847 }
Eric Wong8a49ee92007-02-10 20:46:50 -08001848 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001849 # we don't want "SVM: initializing mirror for junk" ...
1850 return undef if $r == 0;
1851 my $svm = $self->svm;
1852 if ($uuid ne $svm->{uuid}) {
Eric Wong8a49ee92007-02-10 20:46:50 -08001853 die "UUID mismatch on SVM path:\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08001854 "expected: $svm->{uuid}\n",
Eric Wong8a49ee92007-02-10 20:46:50 -08001855 " got: $uuid\n";
1856 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001857 my $full_url = $self->full_url;
1858 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
1859 die "Failed to replace '$svm->{replace}' with ",
1860 "'$svm->{source}' in $full_url\n";
Sam Vilain18ea92b2007-02-23 12:32:29 +13001861 # throw away username for storing in records
1862 remove_username($full_url);
Eric Wong8a49ee92007-02-10 20:46:50 -08001863 $log_entry{metadata} = "$full_url\@$r $uuid";
1864 $log_entry{svm_revision} = $r;
Eric Wongdb03cd22007-02-13 00:38:02 -08001865 $email ||= "$author\@$uuid"
Eric Wong62e349d2007-02-16 19:57:29 -08001866 } elsif ($self->use_svnsync_props) {
1867 my $full_url = $self->svnsync->{url};
1868 $full_url .= "/$self->{path}" if length $self->{path};
Adam Robence118732007-04-24 18:02:07 -07001869 remove_username($full_url);
Eric Wong62e349d2007-02-16 19:57:29 -08001870 my $uuid = $self->svnsync->{uuid};
1871 $log_entry{metadata} = "$full_url\@$rev $uuid";
1872 $email ||= "$author\@$uuid"
Eric Wong8a49ee92007-02-10 20:46:50 -08001873 } else {
Adam Robence118732007-04-24 18:02:07 -07001874 my $url = $self->metadata_url;
1875 remove_username($url);
1876 $log_entry{metadata} = "$url\@$rev " .
Eric Wong26a62d52007-02-12 13:25:25 -08001877 $self->ra->get_uuid;
Eric Wongdb03cd22007-02-13 00:38:02 -08001878 $email ||= "$author\@" . $self->ra->get_uuid;
Eric Wong8a49ee92007-02-10 20:46:50 -08001879 }
Eric Wongdb03cd22007-02-13 00:38:02 -08001880 $log_entry{name} = $name;
1881 $log_entry{email} = $email;
Eric Wong9b981fc2007-01-11 12:14:21 -08001882 \%log_entry;
1883}
1884
1885sub fetch {
Eric Wong3ebe8df2007-01-25 17:35:40 -08001886 my ($self, $min_rev, $max_rev, @parents) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08001887 my ($last_rev, $last_commit) = $self->last_rev_commit;
Eric Wong3ebe8df2007-01-25 17:35:40 -08001888 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
Eric Wonge5181922007-02-08 12:53:57 -08001889 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
Eric Wong9b981fc2007-01-11 12:14:21 -08001890}
1891
1892sub set_tree_cb {
1893 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
Eric Wong490f49e2007-02-10 13:58:33 -08001894 $self->{inject_parents} = { $rev => $tree };
1895 $self->fetch(undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08001896}
1897
1898sub set_tree {
1899 my ($self, $tree) = (shift, shift);
Eric Wong1ce255d2007-01-14 23:21:16 -08001900 my $log_entry = ::get_commit_entry($tree);
Eric Wong9b981fc2007-01-11 12:14:21 -08001901 unless ($self->{last_rev}) {
1902 fatal("Must have an existing revision to commit\n");
1903 }
Eric Wong61395352007-01-27 14:33:08 -08001904 my %ed_opts = ( r => $self->{last_rev},
1905 log => $log_entry->{log},
1906 ra => $self->ra,
1907 tree_a => $self->{last_commit},
1908 tree_b => $tree,
1909 editor_cb => sub {
1910 $self->set_tree_cb($log_entry, $tree, @_) },
1911 svn_path => $self->{path} );
1912 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong9b981fc2007-01-11 12:14:21 -08001913 print "No changes\nr$self->{last_rev} = $tree\n";
1914 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001915}
1916
Eric Wongf0ecca12007-01-30 13:11:14 -08001917sub rebuild {
1918 my ($self) = @_;
Eric Wong26a62d52007-02-12 13:25:25 -08001919 my $db_path = $self->db_path;
Eric Wongd6d33462007-02-16 04:05:33 -08001920 return if (-e $db_path && ! -z $db_path);
1921 return unless ::verify_ref($self->refname.'^0');
Eric Wong26a62d52007-02-12 13:25:25 -08001922 if (-f $self->{db_root}) {
1923 rename $self->{db_root}, $db_path or die
1924 "rename $self->{db_root} => $db_path failed: $!\n";
1925 my ($dir, $base) = ($db_path =~ m#^(.*?)/?([^/]+)$#);
1926 symlink $base, $self->{db_root} or die
1927 "symlink $base => $self->{db_root} failed: $!\n";
1928 return;
1929 }
1930 print "Rebuilding $db_path ...\n";
Eric Wongf0ecca12007-01-30 13:11:14 -08001931 my ($rev_list, $ctx) = command_output_pipe("rev-list", $self->refname);
1932 my $latest;
1933 my $full_url = $self->full_url;
Sam Vilain18ea92b2007-02-23 12:32:29 +13001934 remove_username($full_url);
Eric Wongf0ecca12007-01-30 13:11:14 -08001935 my $svn_uuid;
1936 while (<$rev_list>) {
1937 chomp;
1938 my $c = $_;
1939 die "Non-SHA1: $c\n" unless $c =~ /^$::sha1$/o;
1940 my ($url, $rev, $uuid) = ::cmt_metadata($c);
Sam Vilain18ea92b2007-02-23 12:32:29 +13001941 remove_username($url);
Eric Wongf0ecca12007-01-30 13:11:14 -08001942
1943 # ignore merges (from set-tree)
1944 next if (!defined $rev || !$uuid);
1945
1946 # if we merged or otherwise started elsewhere, this is
1947 # how we break out of it
1948 if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
1949 ($full_url && $url && ($url ne $full_url))) {
1950 next;
1951 }
1952 $latest ||= $rev;
1953 $svn_uuid ||= $uuid;
1954
1955 $self->rev_db_set($rev, $c);
1956 print "r$rev = $c\n";
1957 }
1958 command_close_pipe($rev_list, $ctx);
Eric Wong26a62d52007-02-12 13:25:25 -08001959 print "Done rebuilding $db_path\n";
Eric Wongf0ecca12007-01-30 13:11:14 -08001960}
1961
Eric Wong9b981fc2007-01-11 12:14:21 -08001962# rev_db:
1963# Tie::File seems to be prone to offset errors if revisions get sparse,
1964# it's not that fast, either. Tie::File is also not in Perl 5.6. So
1965# one of my favorite modules is out :< Next up would be one of the DBM
1966# modules, but I'm not sure which is most portable... So I'll just
1967# go with something that's plain-text, but still capable of
1968# being randomly accessed. So here's my ultra-simple fixed-width
1969# database. All records are 40 characters + "\n", so it's easy to seek
1970# to a revision: (41 * rev) is the byte offset.
1971# A record of 40 0s denotes an empty revision.
1972# And yes, it's still pretty fast (faster than Tie::File).
Eric Wong97ae0912007-02-11 15:21:24 -08001973# These files are disposable unless noMetadata or useSvmProps is set
Eric Wong9b981fc2007-01-11 12:14:21 -08001974
Eric Wong26a62d52007-02-12 13:25:25 -08001975sub _rev_db_set {
1976 my ($fh, $rev, $commit) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08001977 my $offset = $rev * 41;
1978 # assume that append is the common case:
1979 seek $fh, 0, 2 or croak $!;
1980 my $pos = tell $fh;
1981 if ($pos < $offset) {
Eric Wong47a0b752007-01-31 05:13:30 -08001982 for (1 .. (($offset - $pos) / 41)) {
1983 print $fh (('0' x 40),"\n") or croak $!;
1984 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001985 }
1986 seek $fh, $offset, 0 or croak $!;
1987 print $fh $commit,"\n" or croak $!;
Eric Wong26a62d52007-02-12 13:25:25 -08001988}
1989
1990sub mkfile {
1991 my ($path) = @_;
1992 unless (-e $path) {
1993 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
1994 mkpath([$dir]) unless -d $dir;
1995 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
1996 close $fh or die "Couldn't close (create) $path: $!\n";
1997 }
1998}
1999
2000sub rev_db_set {
2001 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
2002 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
2003 my $db = $self->db_path($uuid);
2004 my $db_lock = "$db.lock";
2005 my $sig;
2006 if ($update_ref) {
2007 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
2008 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
2009 }
2010 mkfile($db);
2011
2012 $LOCKFILES{$db_lock} = 1;
2013 my $sync;
2014 # both of these options make our .rev_db file very, very important
2015 # and we can't afford to lose it because rebuild() won't work
2016 if ($self->use_svm_props || $self->no_metadata) {
2017 $sync = 1;
2018 copy($db, $db_lock) or die "rev_db_set(@_): ",
2019 "Failed to copy: ",
2020 "$db => $db_lock ($!)\n";
2021 } else {
2022 rename $db, $db_lock or die "rev_db_set(@_): ",
2023 "Failed to rename: ",
2024 "$db => $db_lock ($!)\n";
2025 }
2026 open my $fh, '+<', $db_lock or die "Couldn't open $db_lock: $!\n";
2027 _rev_db_set($fh, $rev, $commit);
Eric Wong97ae0912007-02-11 15:21:24 -08002028 if ($sync) {
2029 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2030 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2031 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002032 close $fh or croak $!;
Eric Wong373274f2007-01-31 13:54:23 -08002033 if ($update_ref) {
Eric Wong1e889ef2007-02-16 01:45:13 -08002034 $_head = $self;
Eric Wong373274f2007-01-31 13:54:23 -08002035 command_noisy('update-ref', '-m', "r$rev",
2036 $self->refname, $commit);
2037 }
2038 rename $db_lock, $db or die "rev_db_set(@_): ", "Failed to rename: ",
2039 "$db_lock => $db ($!)\n";
2040 delete $LOCKFILES{$db_lock};
2041 if ($update_ref) {
2042 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
2043 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
2044 kill $sig, $$ if defined $sig;
2045 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002046}
2047
Eric Wong9c93fee2007-01-31 17:22:31 -08002048sub rev_db_max {
2049 my ($self) = @_;
Eric Wongd6d33462007-02-16 04:05:33 -08002050 $self->rebuild;
Eric Wong26a62d52007-02-12 13:25:25 -08002051 my $db_path = $self->db_path;
2052 my @stat = stat $db_path or return 0;
2053 ($stat[7] % 41) == 0 or die "$db_path inconsistent size: $stat[7]\n";
Eric Wong9c93fee2007-01-31 17:22:31 -08002054 my $max = $stat[7] / 41;
2055 (($max > 0) ? $max - 1 : 0);
2056}
2057
Eric Wong9b981fc2007-01-11 12:14:21 -08002058sub rev_db_get {
Eric Wong26a62d52007-02-12 13:25:25 -08002059 my ($self, $rev, $uuid) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002060 my $ret;
2061 my $offset = $rev * 41;
Eric Wong26a62d52007-02-12 13:25:25 -08002062 my $db_path = $self->db_path($uuid);
2063 return undef unless -e $db_path;
2064 open my $fh, '<', $db_path or croak $!;
Eric Wongce4b4af2007-01-31 17:57:36 -08002065 if (sysseek($fh, $offset, 0) == $offset) {
2066 my $read = sysread($fh, $ret, 40);
2067 $ret = undef if ($read != 40 || $ret eq ('0'x40));
Eric Wong9b981fc2007-01-11 12:14:21 -08002068 }
2069 close $fh or croak $!;
2070 $ret;
2071}
2072
Eric Wong15710b62007-01-22 02:20:33 -08002073sub find_rev_before {
2074 my ($self, $rev, $eq_ok) = @_;
2075 --$rev unless $eq_ok;
2076 while ($rev > 0) {
2077 if (my $c = $self->rev_db_get($rev)) {
2078 return ($rev, $c);
2079 }
2080 --$rev;
2081 }
2082 return (undef, undef);
2083}
2084
Eric Wong9b981fc2007-01-11 12:14:21 -08002085sub _new {
Eric Wong706587f2007-01-18 17:50:01 -08002086 my ($class, $repo_id, $ref_id, $path) = @_;
2087 unless (defined $repo_id && length $repo_id) {
2088 $repo_id = $Git::SVN::default_repo_id;
2089 }
2090 unless (defined $ref_id && length $ref_id) {
Eric Wong8b8fc062007-01-22 11:44:57 -08002091 $_[2] = $ref_id = $Git::SVN::default_ref_id;
Eric Wong706587f2007-01-18 17:50:01 -08002092 }
2093 $_[1] = $repo_id = sanitize_remote_name($repo_id);
2094 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
2095 $_[3] = $path = '' unless (defined $path);
Eric Wongb4d57e52007-02-14 15:10:44 -08002096 mkpath(["$ENV{GIT_DIR}/svn"]);
Eric Wong26a62d52007-02-12 13:25:25 -08002097 bless {
2098 ref_id => $ref_id, dir => $dir, index => "$dir/index",
Eric Wong8a49ee92007-02-10 20:46:50 -08002099 path => $path, config => "$ENV{GIT_DIR}/svn/config",
Eric Wong26a62d52007-02-12 13:25:25 -08002100 db_root => "$dir/.rev_db", repo_id => $repo_id }, $class;
2101}
2102
2103sub db_path {
2104 my ($self, $uuid) = @_;
2105 $uuid ||= $self->ra_uuid;
2106 "$self->{db_root}.$uuid";
Eric Wong9b981fc2007-01-11 12:14:21 -08002107}
2108
Eric Wong1c8443b2007-01-14 02:17:00 -08002109sub uri_encode {
2110 my ($f) = @_;
2111 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2112 $f
2113}
Eric Wong9b981fc2007-01-11 12:14:21 -08002114
Sam Vilain18ea92b2007-02-23 12:32:29 +13002115sub remove_username {
2116 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
2117}
2118
Eric Wongd976acf2007-01-04 00:45:03 -08002119package Git::SVN::Prompt;
2120use strict;
2121use warnings;
2122require SVN::Core;
2123use vars qw/$_no_auth_cache $_username/;
2124
2125sub simple {
Eric Wong30d055a2006-11-24 01:38:04 -08002126 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
2127 $may_save = undef if $_no_auth_cache;
2128 $default_username = $_username if defined $_username;
2129 if (defined $default_username && length $default_username) {
2130 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08002131 print STDERR "Authentication realm: $realm\n";
2132 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002133 }
2134 $cred->username($default_username);
2135 } else {
Eric Wongd976acf2007-01-04 00:45:03 -08002136 username($cred, $realm, $may_save, $pool);
Eric Wong30d055a2006-11-24 01:38:04 -08002137 }
2138 $cred->password(_read_password("Password for '" .
2139 $cred->username . "': ", $realm));
2140 $cred->may_save($may_save);
2141 $SVN::_Core::SVN_NO_ERROR;
2142}
2143
Eric Wongd976acf2007-01-04 00:45:03 -08002144sub ssl_server_trust {
Eric Wong30d055a2006-11-24 01:38:04 -08002145 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
2146 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08002147 print STDERR "Error validating server certificate for '$realm':\n";
Eric Wong30d055a2006-11-24 01:38:04 -08002148 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
Eric Wong6f729592007-01-15 20:15:55 -08002149 print STDERR " - The certificate is not issued by a trusted ",
Eric Wong30d055a2006-11-24 01:38:04 -08002150 "authority. Use the\n",
2151 " fingerprint to validate the certificate manually!\n";
2152 }
2153 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
Eric Wong6f729592007-01-15 20:15:55 -08002154 print STDERR " - The certificate hostname does not match.\n";
Eric Wong30d055a2006-11-24 01:38:04 -08002155 }
2156 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
Eric Wong6f729592007-01-15 20:15:55 -08002157 print STDERR " - The certificate is not yet valid.\n";
Eric Wong30d055a2006-11-24 01:38:04 -08002158 }
2159 if ($failures & $SVN::Auth::SSL::EXPIRED) {
Eric Wong6f729592007-01-15 20:15:55 -08002160 print STDERR " - The certificate has expired.\n";
Eric Wong30d055a2006-11-24 01:38:04 -08002161 }
2162 if ($failures & $SVN::Auth::SSL::OTHER) {
Eric Wong6f729592007-01-15 20:15:55 -08002163 print STDERR " - The certificate has an unknown error.\n";
Eric Wong30d055a2006-11-24 01:38:04 -08002164 }
Eric Wong6f729592007-01-15 20:15:55 -08002165 printf STDERR
2166 "Certificate information:\n".
Eric Wong30d055a2006-11-24 01:38:04 -08002167 " - Hostname: %s\n".
2168 " - Valid: from %s until %s\n".
2169 " - Issuer: %s\n".
2170 " - Fingerprint: %s\n",
2171 map $cert_info->$_, qw(hostname valid_from valid_until
Eric Wong6f729592007-01-15 20:15:55 -08002172 issuer_dname fingerprint);
Eric Wong30d055a2006-11-24 01:38:04 -08002173 my $choice;
2174prompt:
Eric Wong6f729592007-01-15 20:15:55 -08002175 print STDERR $may_save ?
Eric Wong30d055a2006-11-24 01:38:04 -08002176 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
2177 "(R)eject or accept (t)emporarily? ";
Eric Wong6f729592007-01-15 20:15:55 -08002178 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002179 $choice = lc(substr(<STDIN> || 'R', 0, 1));
2180 if ($choice =~ /^t$/i) {
2181 $cred->may_save(undef);
2182 } elsif ($choice =~ /^r$/i) {
2183 return -1;
2184 } elsif ($may_save && $choice =~ /^p$/i) {
2185 $cred->may_save($may_save);
2186 } else {
2187 goto prompt;
2188 }
2189 $cred->accepted_failures($failures);
2190 $SVN::_Core::SVN_NO_ERROR;
2191}
2192
Eric Wongd976acf2007-01-04 00:45:03 -08002193sub ssl_client_cert {
Eric Wong30d055a2006-11-24 01:38:04 -08002194 my ($cred, $realm, $may_save, $pool) = @_;
2195 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08002196 print STDERR "Client certificate filename: ";
2197 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002198 chomp(my $filename = <STDIN>);
2199 $cred->cert_file($filename);
2200 $cred->may_save($may_save);
2201 $SVN::_Core::SVN_NO_ERROR;
2202}
2203
Eric Wongd976acf2007-01-04 00:45:03 -08002204sub ssl_client_cert_pw {
Eric Wong30d055a2006-11-24 01:38:04 -08002205 my ($cred, $realm, $may_save, $pool) = @_;
2206 $may_save = undef if $_no_auth_cache;
2207 $cred->password(_read_password("Password: ", $realm));
2208 $cred->may_save($may_save);
2209 $SVN::_Core::SVN_NO_ERROR;
2210}
2211
Eric Wongd976acf2007-01-04 00:45:03 -08002212sub username {
Eric Wong30d055a2006-11-24 01:38:04 -08002213 my ($cred, $realm, $may_save, $pool) = @_;
2214 $may_save = undef if $_no_auth_cache;
2215 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08002216 print STDERR "Authentication realm: $realm\n";
Eric Wong30d055a2006-11-24 01:38:04 -08002217 }
2218 my $username;
2219 if (defined $_username) {
2220 $username = $_username;
2221 } else {
Eric Wong6f729592007-01-15 20:15:55 -08002222 print STDERR "Username: ";
2223 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002224 chomp($username = <STDIN>);
2225 }
2226 $cred->username($username);
2227 $cred->may_save($may_save);
2228 $SVN::_Core::SVN_NO_ERROR;
2229}
2230
2231sub _read_password {
2232 my ($prompt, $realm) = @_;
Eric Wong6f729592007-01-15 20:15:55 -08002233 print STDERR $prompt;
2234 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002235 require Term::ReadKey;
2236 Term::ReadKey::ReadMode('noecho');
2237 my $password = '';
2238 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
2239 last if $key =~ /[\012\015]/; # \n\r
2240 $password .= $key;
2241 }
2242 Term::ReadKey::ReadMode('restore');
Eric Wong6f729592007-01-15 20:15:55 -08002243 print STDERR "\n";
2244 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002245 $password;
2246}
2247
Eric Wongd976acf2007-01-04 00:45:03 -08002248package main;
2249
Eric Wongb9c85182006-12-15 23:58:07 -08002250{
2251 my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file.
2252 $SVN::Node::dir.$SVN::Node::unknown.
2253 $SVN::Node::none.$SVN::Node::file.
2254 $SVN::Node::dir.$SVN::Node::unknown.
2255 $SVN::Auth::SSL::CNMISMATCH.
2256 $SVN::Auth::SSL::NOTYETVALID.
2257 $SVN::Auth::SSL::EXPIRED.
2258 $SVN::Auth::SSL::UNKNOWNCA.
2259 $SVN::Auth::SSL::OTHER;
2260}
2261
Eric Wong27a1a802006-11-27 21:44:48 -08002262package SVN::Git::Fetcher;
2263use vars qw/@ISA/;
2264use strict;
2265use warnings;
2266use Carp qw/croak/;
2267use IO::File qw//;
Eric Wong90c1b152007-01-29 16:27:08 -08002268use Digest::MD5;
Eric Wong27a1a802006-11-27 21:44:48 -08002269
2270# file baton members: path, mode_a, mode_b, pool, fh, blob, base
2271sub new {
2272 my ($class, $git_svn) = @_;
2273 my $self = SVN::Delta::Editor->new;
2274 bless $self, $class;
Eric Wong1c8443b2007-01-14 02:17:00 -08002275 $self->{c} = $git_svn->{last_commit} if exists $git_svn->{last_commit};
Eric Wongd2a9a872006-12-12 14:47:00 -08002276 $self->{empty} = {};
2277 $self->{dir_prop} = {};
2278 $self->{file_prop} = {};
2279 $self->{absent_dir} = {};
2280 $self->{absent_file} = {};
Eric Wongef3cfaa2007-01-24 03:30:57 -08002281 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
Eric Wong27a1a802006-11-27 21:44:48 -08002282 $self;
2283}
2284
Eric Wong8b8fc062007-01-22 11:44:57 -08002285sub set_path_strip {
2286 my ($self, $path) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002287 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
Eric Wong8b8fc062007-01-22 11:44:57 -08002288}
2289
Eric Wongd2a9a872006-12-12 14:47:00 -08002290sub open_root {
2291 { path => '' };
2292}
2293
2294sub open_directory {
2295 my ($self, $path, $pb, $rev) = @_;
2296 { path => $path };
2297}
2298
Eric Wong706587f2007-01-18 17:50:01 -08002299sub git_path {
2300 my ($self, $path) = @_;
Eric Wong2b27f6c2007-01-28 04:59:05 -08002301 if ($self->{path_strip}) {
2302 $path =~ s!$self->{path_strip}!! or
2303 die "Failed to strip path '$path' ($self->{path_strip})\n";
2304 }
Eric Wong706587f2007-01-18 17:50:01 -08002305 $path;
2306}
2307
Eric Wong27a1a802006-11-27 21:44:48 -08002308sub delete_entry {
2309 my ($self, $path, $rev, $pb) = @_;
Eric Wong4a87db02007-01-04 01:38:18 -08002310
Eric Wong706587f2007-01-18 17:50:01 -08002311 my $gpath = $self->git_path($path);
Eric Wong8a603772007-01-31 02:45:50 -08002312 return undef if ($gpath eq '');
2313
Eric Wong4a87db02007-01-04 01:38:18 -08002314 # remove entire directories.
Eric Wong706587f2007-01-18 17:50:01 -08002315 if (command('ls-tree', $self->{c}, '--', $gpath) =~ /^040000 tree/) {
Eric Wong4a87db02007-01-04 01:38:18 -08002316 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
2317 -r --name-only -z/,
Eric Wong706587f2007-01-18 17:50:01 -08002318 $self->{c}, '--', $gpath);
Eric Wong4a87db02007-01-04 01:38:18 -08002319 local $/ = "\0";
2320 while (<$ls>) {
Eric Wongef3cfaa2007-01-24 03:30:57 -08002321 chomp;
2322 $self->{gii}->remove($_);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08002323 print "\tD\t$_\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08002324 }
Eric Wong9e3cdbd2007-02-09 12:23:47 -08002325 print "\tD\t$gpath/\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08002326 command_close_pipe($ls, $ctx);
2327 $self->{empty}->{$path} = 0
2328 } else {
Eric Wongef3cfaa2007-01-24 03:30:57 -08002329 $self->{gii}->remove($gpath);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08002330 print "\tD\t$gpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08002331 }
Eric Wong27a1a802006-11-27 21:44:48 -08002332 undef;
2333}
2334
2335sub open_file {
2336 my ($self, $path, $pb, $rev) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08002337 my $gpath = $self->git_path($path);
2338 my ($mode, $blob) = (command('ls-tree', $self->{c}, '--', $gpath)
Eric Wong27a1a802006-11-27 21:44:48 -08002339 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
Eric Wong006ede52006-12-08 01:55:19 -08002340 unless (defined $mode && defined $blob) {
2341 die "$path was not found in commit $self->{c} (r$rev)\n";
2342 }
Eric Wong27a1a802006-11-27 21:44:48 -08002343 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
Eric Wong0864e3b2006-11-28 02:50:17 -08002344 pool => SVN::Pool->new, action => 'M' };
Eric Wong27a1a802006-11-27 21:44:48 -08002345}
2346
2347sub add_file {
2348 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
Eric Wongd2a9a872006-12-12 14:47:00 -08002349 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
2350 delete $self->{empty}->{$dir};
Eric Wong27a1a802006-11-27 21:44:48 -08002351 { path => $path, mode_a => 100644, mode_b => 100644,
Eric Wong0864e3b2006-11-28 02:50:17 -08002352 pool => SVN::Pool->new, action => 'A' };
Eric Wong27a1a802006-11-27 21:44:48 -08002353}
2354
Eric Wongd2a9a872006-12-12 14:47:00 -08002355sub add_directory {
2356 my ($self, $path, $cp_path, $cp_rev) = @_;
2357 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
2358 delete $self->{empty}->{$dir};
2359 $self->{empty}->{$path} = 1;
2360 { path => $path };
2361}
2362
2363sub change_dir_prop {
2364 my ($self, $db, $prop, $value) = @_;
2365 $self->{dir_prop}->{$db->{path}} ||= {};
2366 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
2367 undef;
2368}
2369
2370sub absent_directory {
2371 my ($self, $path, $pb) = @_;
2372 $self->{absent_dir}->{$pb->{path}} ||= [];
2373 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
2374 undef;
2375}
2376
2377sub absent_file {
2378 my ($self, $path, $pb) = @_;
2379 $self->{absent_file}->{$pb->{path}} ||= [];
2380 push @{$self->{absent_file}->{$pb->{path}}}, $path;
2381 undef;
2382}
2383
Eric Wong27a1a802006-11-27 21:44:48 -08002384sub change_file_prop {
2385 my ($self, $fb, $prop, $value) = @_;
2386 if ($prop eq 'svn:executable') {
2387 if ($fb->{mode_b} != 120000) {
2388 $fb->{mode_b} = defined $value ? 100755 : 100644;
2389 }
2390 } elsif ($prop eq 'svn:special') {
2391 $fb->{mode_b} = defined $value ? 120000 : 100644;
Eric Wongd2a9a872006-12-12 14:47:00 -08002392 } else {
2393 $self->{file_prop}->{$fb->{path}} ||= {};
2394 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
Eric Wong27a1a802006-11-27 21:44:48 -08002395 }
2396 undef;
2397}
2398
2399sub apply_textdelta {
2400 my ($self, $fb, $exp) = @_;
2401 my $fh = IO::File->new_tmpfile;
2402 $fh->autoflush(1);
2403 # $fh gets auto-closed() by SVN::TxDelta::apply(),
2404 # (but $base does not,) so dup() it for reading in close_file
2405 open my $dup, '<&', $fh or croak $!;
2406 my $base = IO::File->new_tmpfile;
2407 $base->autoflush(1);
2408 if ($fb->{blob}) {
2409 defined (my $pid = fork) or croak $!;
2410 if (!$pid) {
2411 open STDOUT, '>&', $base or croak $!;
2412 print STDOUT 'link ' if ($fb->{mode_a} == 120000);
2413 exec qw/git-cat-file blob/, $fb->{blob} or croak $!;
2414 }
2415 waitpid $pid, 0;
2416 croak $? if $?;
2417
2418 if (defined $exp) {
2419 seek $base, 0, 0 or croak $!;
2420 my $md5 = Digest::MD5->new;
2421 $md5->addfile($base);
2422 my $got = $md5->hexdigest;
2423 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
2424 "expected: $exp\n",
2425 " got: $got\n" if ($got ne $exp);
2426 }
2427 }
2428 seek $base, 0, 0 or croak $!;
2429 $fb->{fh} = $dup;
2430 $fb->{base} = $base;
2431 [ SVN::TxDelta::apply($base, $fh, undef, $fb->{path}, $fb->{pool}) ];
2432}
2433
2434sub close_file {
2435 my ($self, $fb, $exp) = @_;
2436 my $hash;
Eric Wong706587f2007-01-18 17:50:01 -08002437 my $path = $self->git_path($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08002438 if (my $fh = $fb->{fh}) {
2439 seek($fh, 0, 0) or croak $!;
2440 my $md5 = Digest::MD5->new;
2441 $md5->addfile($fh);
2442 my $got = $md5->hexdigest;
2443 die "Checksum mismatch: $path\n",
2444 "expected: $exp\n got: $got\n" if ($got ne $exp);
2445 seek($fh, 0, 0) or croak $!;
2446 if ($fb->{mode_b} == 120000) {
2447 read($fh, my $buf, 5) == 5 or croak $!;
2448 $buf eq 'link ' or die "$path has mode 120000",
2449 "but is not a link\n";
2450 }
2451 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
2452 if (!$pid) {
2453 open STDIN, '<&', $fh or croak $!;
2454 exec qw/git-hash-object -w --stdin/ or croak $!;
2455 }
2456 chomp($hash = do { local $/; <$out> });
2457 close $out or croak $!;
2458 close $fh or croak $!;
2459 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
2460 close $fb->{base} or croak $!;
2461 } else {
2462 $hash = $fb->{blob} or die "no blob information\n";
2463 }
2464 $fb->{pool}->clear;
Eric Wongef3cfaa2007-01-24 03:30:57 -08002465 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
Eric Wong9e3cdbd2007-02-09 12:23:47 -08002466 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
Eric Wong27a1a802006-11-27 21:44:48 -08002467 undef;
2468}
2469
2470sub abort_edit {
2471 my $self = shift;
Eric Wongef3cfaa2007-01-24 03:30:57 -08002472 $self->{nr} = $self->{gii}->{nr};
2473 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08002474 $self->SUPER::abort_edit(@_);
2475}
2476
2477sub close_edit {
2478 my $self = shift;
Eric Wongdad73c02006-11-28 14:06:05 -08002479 $self->{git_commit_ok} = 1;
Eric Wongef3cfaa2007-01-24 03:30:57 -08002480 $self->{nr} = $self->{gii}->{nr};
2481 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08002482 $self->SUPER::close_edit(@_);
2483}
Eric Wong1a82e792006-06-16 02:55:13 -07002484
Eric Wonga5e0ced2006-06-12 15:23:48 -07002485package SVN::Git::Editor;
Eric Wong24e22aa2007-01-29 00:07:49 -08002486use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002487use strict;
2488use warnings;
2489use Carp qw/croak/;
2490use IO::File;
Eric Wong90c1b152007-01-29 16:27:08 -08002491use Digest::MD5;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002492
2493sub new {
Eric Wong61395352007-01-27 14:33:08 -08002494 my ($class, $opts) = @_;
2495 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
2496 die "$_ required!\n" unless (defined $opts->{$_});
Eric Wonga5e0ced2006-06-12 15:23:48 -07002497 }
Eric Wong61395352007-01-27 14:33:08 -08002498
2499 my $pool = SVN::Pool->new;
2500 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
2501 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
2502 $opts->{r}, $mods);
2503
2504 # $opts->{ra} functions should not be used after this:
2505 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
2506 $opts->{editor_cb}, $pool);
2507 my $self = SVN::Delta::Editor->new(@ce, $pool);
2508 bless $self, $class;
2509 foreach (qw/svn_path r tree_a tree_b/) {
2510 $self->{$_} = $opts->{$_};
2511 }
2512 $self->{url} = $opts->{ra}->{url};
2513 $self->{mods} = $mods;
2514 $self->{types} = $types;
2515 $self->{pool} = $pool;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002516 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
2517 $self->{rm} = { };
Eric Wongd3a840d2007-01-26 01:32:45 -08002518 $self->{path_prefix} = length $self->{svn_path} ?
2519 "$self->{svn_path}/" : '';
Eric Wonga5e0ced2006-06-12 15:23:48 -07002520 return $self;
2521}
2522
Eric Wong61395352007-01-27 14:33:08 -08002523sub generate_diff {
2524 my ($tree_a, $tree_b) = @_;
2525 my @diff_tree = qw(diff-tree -z -r);
Eric Wong24e22aa2007-01-29 00:07:49 -08002526 if ($_cp_similarity) {
2527 push @diff_tree, "-C$_cp_similarity";
Eric Wong61395352007-01-27 14:33:08 -08002528 } else {
2529 push @diff_tree, '-C';
2530 }
Eric Wong24e22aa2007-01-29 00:07:49 -08002531 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
2532 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
Eric Wong61395352007-01-27 14:33:08 -08002533 push @diff_tree, $tree_a, $tree_b;
2534 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
2535 local $/ = "\0";
2536 my $state = 'meta';
2537 my @mods;
2538 while (<$diff_fh>) {
2539 chomp $_; # this gets rid of the trailing "\0"
2540 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
2541 $::sha1\s($::sha1)\s
2542 ([MTCRAD])\d*$/xo) {
2543 push @mods, { mode_a => $1, mode_b => $2,
2544 sha1_b => $3, chg => $4 };
2545 if ($4 =~ /^(?:C|R)$/) {
2546 $state = 'file_a';
2547 } else {
2548 $state = 'file_b';
2549 }
2550 } elsif ($state eq 'file_a') {
2551 my $x = $mods[$#mods] or croak "Empty array\n";
2552 if ($x->{chg} !~ /^(?:C|R)$/) {
2553 croak "Error parsing $_, $x->{chg}\n";
2554 }
2555 $x->{file_a} = $_;
2556 $state = 'file_b';
2557 } elsif ($state eq 'file_b') {
2558 my $x = $mods[$#mods] or croak "Empty array\n";
2559 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
2560 croak "Error parsing $_, $x->{chg}\n";
2561 }
2562 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
2563 croak "Error parsing $_, $x->{chg}\n";
2564 }
2565 $x->{file_b} = $_;
2566 $state = 'meta';
2567 } else {
2568 croak "Error parsing $_\n";
2569 }
2570 }
2571 command_close_pipe($diff_fh, $ctx);
2572 \@mods;
2573}
2574
2575sub check_diff_paths {
2576 my ($ra, $pfx, $rev, $mods) = @_;
2577 my %types;
2578 $pfx .= '/' if length $pfx;
2579
2580 sub type_diff_paths {
2581 my ($ra, $types, $path, $rev) = @_;
2582 my @p = split m#/+#, $path;
2583 my $c = shift @p;
2584 unless (defined $types->{$c}) {
2585 $types->{$c} = $ra->check_path($c, $rev);
2586 }
2587 while (@p) {
2588 $c .= '/' . shift @p;
2589 next if defined $types->{$c};
2590 $types->{$c} = $ra->check_path($c, $rev);
2591 }
2592 }
2593
2594 foreach my $m (@$mods) {
2595 foreach my $f (qw/file_a file_b/) {
2596 next unless defined $m->{$f};
2597 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
2598 if (length $pfx.$dir && ! defined $types{$dir}) {
2599 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
2600 }
2601 }
2602 }
2603 \%types;
2604}
2605
Eric Wonga5e0ced2006-06-12 15:23:48 -07002606sub split_path {
2607 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
2608}
2609
2610sub repo_path {
Eric Wongd3a840d2007-01-26 01:32:45 -08002611 my ($self, $path) = @_;
2612 $self->{path_prefix}.(defined $path ? $path : '');
Eric Wonga5e0ced2006-06-12 15:23:48 -07002613}
2614
2615sub url_path {
2616 my ($self, $path) = @_;
Eric Wong6e8548c2007-01-27 01:32:00 -08002617 $self->{url} . '/' . $self->repo_path($path);
Eric Wonga5e0ced2006-06-12 15:23:48 -07002618}
2619
2620sub rmdirs {
Eric Wong61395352007-01-27 14:33:08 -08002621 my ($self) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002622 my $rm = $self->{rm};
2623 delete $rm->{''}; # we never delete the url we're tracking
2624 return unless %$rm;
2625
2626 foreach (keys %$rm) {
2627 my @d = split m#/#, $_;
2628 my $c = shift @d;
2629 $rm->{$c} = 1;
2630 while (@d) {
2631 $c .= '/' . shift @d;
2632 $rm->{$c} = 1;
2633 }
2634 }
2635 delete $rm->{$self->{svn_path}};
2636 delete $rm->{''}; # we never delete the url we're tracking
2637 return unless %$rm;
2638
Eric Wong61395352007-01-27 14:33:08 -08002639 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
2640 $self->{tree_b});
Eric Wonga5e0ced2006-06-12 15:23:48 -07002641 local $/ = "\0";
2642 while (<$fh>) {
2643 chomp;
Eric Wong747fa122006-11-24 22:38:17 -08002644 my @dn = split m#/#, $_;
Eric Wongc07eee12006-06-19 17:59:35 -07002645 while (pop @dn) {
2646 delete $rm->{join '/', @dn};
2647 }
2648 unless (%$rm) {
Eric Wong22600a22007-02-01 13:12:26 -08002649 close $fh;
Eric Wongc07eee12006-06-19 17:59:35 -07002650 return;
2651 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07002652 }
Eric Wongaef4e922006-12-15 10:59:54 -08002653 command_close_pipe($fh, $ctx);
Eric Wongc07eee12006-06-19 17:59:35 -07002654
Eric Wonga5e0ced2006-06-12 15:23:48 -07002655 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
2656 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
2657 $self->close_directory($bat->{$d}, $p);
2658 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
Eric Wong44320b92007-01-13 22:35:53 -08002659 print "\tD+\t$d/\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002660 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
2661 delete $bat->{$d};
2662 }
2663}
2664
2665sub open_or_add_dir {
2666 my ($self, $full_path, $baton) = @_;
Eric Wong6e8548c2007-01-27 01:32:00 -08002667 my $t = $self->{types}->{$full_path};
2668 if (!defined $t) {
2669 die "$full_path not known in r$self->{r} or we have a bug!\n";
2670 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07002671 if ($t == $SVN::Node::none) {
2672 return $self->add_directory($full_path, $baton,
2673 undef, -1, $self->{pool});
2674 } elsif ($t == $SVN::Node::dir) {
2675 return $self->open_directory($full_path, $baton,
2676 $self->{r}, $self->{pool});
2677 }
2678 print STDERR "$full_path already exists in repository at ",
2679 "r$self->{r} and it is not a directory (",
2680 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
2681 exit 1;
2682}
2683
2684sub ensure_path {
2685 my ($self, $path) = @_;
2686 my $bat = $self->{bat};
Eric Wong6e8548c2007-01-27 01:32:00 -08002687 my $repo_path = $self->repo_path($path);
2688 return $bat->{''} unless (length $repo_path);
2689 my @p = split m#/+#, $repo_path;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002690 my $c = shift @p;
2691 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
2692 while (@p) {
2693 my $c0 = $c;
2694 $c .= '/' . shift @p;
2695 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
2696 }
2697 return $bat->{$c};
2698}
2699
2700sub A {
Eric Wong44320b92007-01-13 22:35:53 -08002701 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002702 my ($dir, $file) = split_path($m->{file_b});
2703 my $pbat = $self->ensure_path($dir);
2704 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2705 undef, -1);
Eric Wong44320b92007-01-13 22:35:53 -08002706 print "\tA\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002707 $self->chg_file($fbat, $m);
2708 $self->close_file($fbat,undef,$self->{pool});
2709}
2710
2711sub C {
Eric Wong44320b92007-01-13 22:35:53 -08002712 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002713 my ($dir, $file) = split_path($m->{file_b});
2714 my $pbat = $self->ensure_path($dir);
2715 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2716 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08002717 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002718 $self->chg_file($fbat, $m);
2719 $self->close_file($fbat,undef,$self->{pool});
2720}
2721
2722sub delete_entry {
2723 my ($self, $path, $pbat) = @_;
2724 my $rpath = $self->repo_path($path);
2725 my ($dir, $file) = split_path($rpath);
2726 $self->{rm}->{$dir} = 1;
2727 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
2728}
2729
2730sub R {
Eric Wong44320b92007-01-13 22:35:53 -08002731 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002732 my ($dir, $file) = split_path($m->{file_b});
2733 my $pbat = $self->ensure_path($dir);
2734 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2735 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08002736 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002737 $self->chg_file($fbat, $m);
2738 $self->close_file($fbat,undef,$self->{pool});
2739
2740 ($dir, $file) = split_path($m->{file_a});
2741 $pbat = $self->ensure_path($dir);
2742 $self->delete_entry($m->{file_a}, $pbat);
2743}
2744
2745sub M {
Eric Wong44320b92007-01-13 22:35:53 -08002746 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002747 my ($dir, $file) = split_path($m->{file_b});
2748 my $pbat = $self->ensure_path($dir);
2749 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
2750 $pbat,$self->{r},$self->{pool});
Eric Wong44320b92007-01-13 22:35:53 -08002751 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002752 $self->chg_file($fbat, $m);
2753 $self->close_file($fbat,undef,$self->{pool});
2754}
2755
2756sub T { shift->M(@_) }
2757
2758sub change_file_prop {
2759 my ($self, $fbat, $pname, $pval) = @_;
2760 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
2761}
2762
2763sub chg_file {
2764 my ($self, $fbat, $m) = @_;
2765 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
2766 $self->change_file_prop($fbat,'svn:executable','*');
2767 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
2768 $self->change_file_prop($fbat,'svn:executable',undef);
2769 }
2770 my $fh = IO::File->new_tmpfile or croak $!;
2771 if ($m->{mode_b} =~ /^120/) {
2772 print $fh 'link ' or croak $!;
2773 $self->change_file_prop($fbat,'svn:special','*');
2774 } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
2775 $self->change_file_prop($fbat,'svn:special',undef);
2776 }
2777 defined(my $pid = fork) or croak $!;
2778 if (!$pid) {
2779 open STDOUT, '>&', $fh or croak $!;
2780 exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
2781 }
2782 waitpid $pid, 0;
2783 croak $? if $?;
2784 $fh->flush == 0 or croak $!;
2785 seek $fh, 0, 0 or croak $!;
2786
2787 my $md5 = Digest::MD5->new;
2788 $md5->addfile($fh) or croak $!;
2789 seek $fh, 0, 0 or croak $!;
2790
2791 my $exp = $md5->hexdigest;
Eric Wongf7197df2006-10-14 15:48:35 -07002792 my $pool = SVN::Pool->new;
2793 my $atd = $self->apply_textdelta($fbat, undef, $pool);
2794 my $got = SVN::TxDelta::send_stream($fh, @$atd, $pool);
Eric Wonga5e0ced2006-06-12 15:23:48 -07002795 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
Eric Wongf7197df2006-10-14 15:48:35 -07002796 $pool->clear;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002797
2798 close $fh or croak $!;
2799}
2800
2801sub D {
Eric Wong44320b92007-01-13 22:35:53 -08002802 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002803 my ($dir, $file) = split_path($m->{file_b});
2804 my $pbat = $self->ensure_path($dir);
Eric Wong44320b92007-01-13 22:35:53 -08002805 print "\tD\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002806 $self->delete_entry($m->{file_b}, $pbat);
2807}
2808
2809sub close_edit {
2810 my ($self) = @_;
2811 my ($p,$bat) = ($self->{pool}, $self->{bat});
2812 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
2813 $self->close_directory($bat->{$_}, $p);
2814 }
2815 $self->SUPER::close_edit($p);
2816 $p->clear;
2817}
2818
2819sub abort_edit {
2820 my ($self) = @_;
2821 $self->SUPER::abort_edit($self->{pool});
Eric Wong61395352007-01-27 14:33:08 -08002822}
2823
2824sub DESTROY {
2825 my $self = shift;
2826 $self->SUPER::DESTROY(@_);
Eric Wonga5e0ced2006-06-12 15:23:48 -07002827 $self->{pool}->clear;
2828}
2829
Eric Wong44320b92007-01-13 22:35:53 -08002830# this drives the editor
2831sub apply_diff {
Eric Wong61395352007-01-27 14:33:08 -08002832 my ($self) = @_;
2833 my $mods = $self->{mods};
Eric Wong44320b92007-01-13 22:35:53 -08002834 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
Eric Wong6e8548c2007-01-27 01:32:00 -08002835 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
Eric Wong44320b92007-01-13 22:35:53 -08002836 my $f = $m->{chg};
2837 if (defined $o{$f}) {
2838 $self->$f($m);
2839 } else {
2840 fatal("Invalid change type: $f\n");
2841 }
2842 }
Eric Wong24e22aa2007-01-29 00:07:49 -08002843 $self->rmdirs if $_rmdir;
Eric Wong6e8548c2007-01-27 01:32:00 -08002844 if (@$mods == 0) {
Eric Wong44320b92007-01-13 22:35:53 -08002845 $self->abort_edit;
2846 } else {
2847 $self->close_edit;
2848 }
Eric Wong6e8548c2007-01-27 01:32:00 -08002849 return scalar @$mods;
Eric Wong44320b92007-01-13 22:35:53 -08002850}
2851
Eric Wongd81bf822007-01-10 01:22:38 -08002852package Git::SVN::Ra;
Eric Wong6af1db42007-02-14 16:04:10 -08002853use vars qw/@ISA $config_dir $_log_window_size/;
Eric Wongd81bf822007-01-10 01:22:38 -08002854use strict;
2855use warnings;
Eric Wonga6a15a92007-03-30 17:54:48 -07002856my ($can_do_switch, %ignored_err, $RA);
Eric Wongd81bf822007-01-10 01:22:38 -08002857
2858BEGIN {
2859 # enforce temporary pool usage for some simple functions
2860 my $e;
Eric Wong74a81222007-02-10 13:28:50 -08002861 foreach (qw/get_latest_revnum get_uuid get_repos_root/) {
Eric Wongd81bf822007-01-10 01:22:38 -08002862 $e .= "sub $_ {
2863 my \$self = shift;
2864 my \$pool = SVN::Pool->new;
2865 my \@ret = \$self->SUPER::$_(\@_,\$pool);
2866 \$pool->clear;
2867 wantarray ? \@ret : \$ret[0]; }\n";
2868 }
Eric Wong74a81222007-02-10 13:28:50 -08002869
2870 # get_dir needs $pool held in cache for dirents to work,
2871 # check_path is cacheable and rev_proplist is close enough
2872 # for our purposes.
2873 foreach (qw/check_path get_dir rev_proplist/) {
2874 $e .= "my \%${_}_cache; my \$${_}_rev = 0; sub $_ {
2875 my \$self = shift;
2876 my \$r = pop;
2877 my \$k = join(\"\\0\", \@_);
2878 if (my \$x = \$${_}_cache{\$r}->{\$k}) {
2879 return wantarray ? \@\$x : \$x->[0];
2880 }
2881 my \$pool = SVN::Pool->new;
2882 my \@ret = \$self->SUPER::$_(\@_, \$r, \$pool);
2883 if (\$r != \$${_}_rev) {
2884 \%${_}_cache = ( pool => [] );
2885 \$${_}_rev = \$r;
2886 }
2887 \$${_}_cache{\$r}->{\$k} = \\\@ret;
2888 push \@{\$${_}_cache{pool}}, \$pool;
2889 wantarray ? \@ret : \$ret[0]; }\n";
2890 }
2891 $e .= "\n1;";
2892 eval $e or die $@;
Eric Wongd81bf822007-01-10 01:22:38 -08002893}
2894
2895sub new {
2896 my ($class, $url) = @_;
Eric Wongf6f09872007-01-18 18:22:18 -08002897 $url =~ s!/+$!!;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002898 return $RA if ($RA && $RA->{url} eq $url);
Eric Wonge2c475d2007-02-23 01:57:40 -08002899 $RA->{pool}->clear if $RA;
Eric Wongf6f09872007-01-18 18:22:18 -08002900
Eric Wongd81bf822007-01-10 01:22:38 -08002901 SVN::_Core::svn_config_ensure($config_dir, undef);
2902 my ($baton, $callbacks) = SVN::Core::auth_open_helper([
2903 SVN::Client::get_simple_provider(),
2904 SVN::Client::get_ssl_server_trust_file_provider(),
2905 SVN::Client::get_simple_prompt_provider(
2906 \&Git::SVN::Prompt::simple, 2),
2907 SVN::Client::get_ssl_client_cert_prompt_provider(
2908 \&Git::SVN::Prompt::ssl_client_cert, 2),
2909 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
2910 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
2911 SVN::Client::get_username_provider(),
2912 SVN::Client::get_ssl_server_trust_prompt_provider(
2913 \&Git::SVN::Prompt::ssl_server_trust),
2914 SVN::Client::get_username_prompt_provider(
2915 \&Git::SVN::Prompt::username, 2),
2916 ]);
2917 my $config = SVN::Core::config_get_config($config_dir);
2918 my $self = SVN::Ra->new(url => $url, auth => $baton,
2919 config => $config,
2920 pool => SVN::Pool->new,
2921 auth_provider_callbacks => $callbacks);
2922 $self->{svn_path} = $url;
2923 $self->{repos_root} = $self->get_repos_root;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002924 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002925 $RA = bless $self, $class;
Eric Wongd81bf822007-01-10 01:22:38 -08002926}
2927
2928sub DESTROY {
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002929 # do not call the real DESTROY since we store ourselves in $RA
Eric Wongd81bf822007-01-10 01:22:38 -08002930}
2931
Eric Wongd81bf822007-01-10 01:22:38 -08002932sub get_log {
2933 my ($self, @args) = @_;
2934 my $pool = SVN::Pool->new;
Eric Wongd81bf822007-01-10 01:22:38 -08002935 splice(@args, 3, 1) if ($SVN::Core::VERSION le '1.2.0');
2936 my $ret = $self->SUPER::get_log(@args, $pool);
2937 $pool->clear;
2938 $ret;
2939}
2940
2941sub get_commit_editor {
Eric Wong44320b92007-01-13 22:35:53 -08002942 my ($self, $log, $cb, $pool) = @_;
Eric Wongd81bf822007-01-10 01:22:38 -08002943 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
Eric Wong44320b92007-01-13 22:35:53 -08002944 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08002945}
2946
Eric Wongd81bf822007-01-10 01:22:38 -08002947sub gs_do_update {
Eric Wong8a603772007-01-31 02:45:50 -08002948 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
2949 my $new = ($rev_a == $rev_b);
2950 my $path = $gs->{path};
2951
Eric Wong2e5e2482007-02-23 02:21:59 -08002952 if ($new && -e $gs->{index}) {
2953 unlink $gs->{index} or die
2954 "Couldn't unlink index: $gs->{index}: $!\n";
2955 }
Eric Wongd81bf822007-01-10 01:22:38 -08002956 my $pool = SVN::Pool->new;
Eric Wong8b8fc062007-01-22 11:44:57 -08002957 $editor->set_path_strip($path);
Eric Wong2b27f6c2007-01-28 04:59:05 -08002958 my (@pc) = split m#/#, $path;
2959 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
Eric Wong8a603772007-01-31 02:45:50 -08002960 1, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08002961 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong2b27f6c2007-01-28 04:59:05 -08002962
2963 # Since we can't rely on svn_ra_reparent being available, we'll
2964 # just have to do some magic with set_path to make it so
2965 # we only want a partial path.
2966 my $sp = '';
2967 my $final = join('/', @pc);
2968 while (@pc) {
2969 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
2970 $sp .= '/' if length $sp;
2971 $sp .= shift @pc;
2972 }
2973 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
2974
Eric Wong2b27f6c2007-01-28 04:59:05 -08002975 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
2976
Eric Wongd81bf822007-01-10 01:22:38 -08002977 $reporter->finish_report($pool);
2978 $pool->clear;
2979 $editor->{git_commit_ok};
2980}
2981
Eric Wong2b27f6c2007-01-28 04:59:05 -08002982# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
2983# svn_ra_reparent didn't work before 1.4)
Eric Wongd81bf822007-01-10 01:22:38 -08002984sub gs_do_switch {
Eric Wong8a603772007-01-31 02:45:50 -08002985 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
2986 my $path = $gs->{path};
Eric Wongd81bf822007-01-10 01:22:38 -08002987 my $pool = SVN::Pool->new;
Eric Wong2b27f6c2007-01-28 04:59:05 -08002988
2989 my $full_url = $self->{url};
2990 my $old_url = $full_url;
2991 $full_url .= "/$path" if length $path;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002992 my ($ra, $reparented);
2993 if ($old_url ne $full_url) {
2994 if ($old_url !~ m#^svn(\+ssh)?://#) {
2995 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url,
2996 $pool);
2997 $self->{url} = $full_url;
2998 $reparented = 1;
2999 } else {
3000 $ra = Git::SVN::Ra->new($full_url);
3001 }
3002 }
3003 $ra ||= $self;
Eric Wong8a603772007-01-31 02:45:50 -08003004 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08003005 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong8b8fc062007-01-22 11:44:57 -08003006 $reporter->set_path('', $rev_a, 0, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08003007 $reporter->finish_report($pool);
Eric Wong2b27f6c2007-01-28 04:59:05 -08003008
Eric Wong5d3b7cd2007-01-29 19:16:01 -08003009 if ($reparented) {
3010 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
3011 $self->{url} = $old_url;
3012 }
Eric Wong2b27f6c2007-01-28 04:59:05 -08003013
Eric Wongd81bf822007-01-10 01:22:38 -08003014 $pool->clear;
3015 $editor->{git_commit_ok};
3016}
3017
Eric Wong0af9c9f2007-01-27 22:28:56 -08003018sub gs_fetch_loop_common {
Eric Wonge5181922007-02-08 12:53:57 -08003019 my ($self, $base, $head, $gsv, $globs) = @_;
3020 return if ($base > $head);
Eric Wong6af1db42007-02-14 16:04:10 -08003021 my $inc = $_log_window_size;
Eric Wong0af9c9f2007-01-27 22:28:56 -08003022 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
Eric Wongd2ae1432007-02-07 11:50:16 -08003023 my %common;
Eric Wonge5181922007-02-08 12:53:57 -08003024 my $common_max = scalar @$gsv;
3025
3026 foreach my $gs (@$gsv) {
Eric Wongd2ae1432007-02-07 11:50:16 -08003027 my @tmp = split m#/#, $gs->{path};
3028 my $p = '';
3029 foreach (@tmp) {
3030 $p .= length($p) ? "/$_" : $_;
3031 $common{$p} ||= 0;
3032 $common{$p}++;
3033 }
3034 }
Eric Wonge5181922007-02-08 12:53:57 -08003035 $globs ||= [];
3036 $common_max += scalar @$globs;
3037 foreach my $glob (@$globs) {
3038 my @tmp = split m#/#, $glob->{path}->{left};
3039 my $p = '';
3040 foreach (@tmp) {
3041 $p .= length($p) ? "/$_" : $_;
3042 $common{$p} ||= 0;
3043 $common{$p}++;
3044 }
3045 }
3046
Eric Wongd2ae1432007-02-07 11:50:16 -08003047 my $longest_path = '';
3048 foreach (sort {length $b <=> length $a} keys %common) {
Eric Wonge5181922007-02-08 12:53:57 -08003049 if ($common{$_} == $common_max) {
Eric Wongd2ae1432007-02-07 11:50:16 -08003050 $longest_path = $_;
3051 last;
3052 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08003053 }
3054 while (1) {
Eric Wongd4eff2b2007-01-30 14:04:22 -08003055 my %revs;
Eric Wongd2ae1432007-02-07 11:50:16 -08003056 my $err;
Eric Wongf7c3fc42007-01-29 18:34:55 -08003057 my $err_handler = $SVN::Error::handler;
Eric Wongd2ae1432007-02-07 11:50:16 -08003058 $SVN::Error::handler = sub {
3059 ($err) = @_;
3060 skip_unknown_revs($err);
3061 };
3062 sub _cb {
3063 my ($paths, $r, $author, $date, $log) = @_;
3064 [ dup_changed_paths($paths),
3065 { author => $author, date => $date, log => $log } ];
3066 }
3067 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
3068 sub { $revs{$_[1]} = _cb(@_) });
3069 if ($err && $max >= $head) {
3070 print STDERR "Path '$longest_path' ",
3071 "was probably deleted:\n",
3072 $err->expanded_message,
3073 "\nWill attempt to follow ",
3074 "revisions r$min .. r$max ",
3075 "committed before the deletion\n";
3076 my $hi = $max;
3077 while (--$hi >= $min) {
3078 my $ok;
3079 $self->get_log([$longest_path], $min, $hi,
3080 0, 1, 1, sub {
3081 $ok ||= $_[1];
3082 $revs{$_[1]} = _cb(@_) });
3083 if ($ok) {
3084 print STDERR "r$min .. r$ok OK\n";
3085 last;
3086 }
3087 }
3088 }
Eric Wongd4eff2b2007-01-30 14:04:22 -08003089 $SVN::Error::handler = $err_handler;
Eric Wongfbcc1732007-02-06 18:35:30 -08003090
Eric Wonge5181922007-02-08 12:53:57 -08003091 my %exists = map { $_->{path} => $_ } @$gsv;
Eric Wongd4eff2b2007-01-30 14:04:22 -08003092 foreach my $r (sort {$a <=> $b} keys %revs) {
Eric Wongfbcc1732007-02-06 18:35:30 -08003093 my ($paths, $logged) = @{$revs{$r}};
Eric Wonge5181922007-02-08 12:53:57 -08003094
3095 foreach my $gs ($self->match_globs(\%exists, $paths,
3096 $globs, $r)) {
Eric Wongfbcc1732007-02-06 18:35:30 -08003097 if ($gs->rev_db_max >= $r) {
3098 next;
3099 }
3100 next unless $gs->match_paths($paths, $r);
3101 $gs->{logged_rev_props} = $logged;
Eric Wonge8d120b2007-02-14 16:29:52 -08003102 if (my $last_commit = $gs->last_commit) {
3103 $gs->assert_index_clean($last_commit);
3104 }
Eric Wongfbcc1732007-02-06 18:35:30 -08003105 my $log_entry = $gs->do_fetch($paths, $r);
3106 if ($log_entry) {
Eric Wong0af9c9f2007-01-27 22:28:56 -08003107 $gs->do_git_commit($log_entry);
3108 }
3109 }
Eric Wonge5181922007-02-08 12:53:57 -08003110 foreach my $g (@$globs) {
Eric Wong93f26892007-02-11 01:20:26 -08003111 my $k = "svn-remote.$g->{remote}." .
3112 "$g->{t}-maxRev";
3113 Git::SVN::tmp_config($k, $r);
Eric Wonge5181922007-02-08 12:53:57 -08003114 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08003115 }
Eric Wong9c93fee2007-01-31 17:22:31 -08003116 # pre-fill the .rev_db since it'll eventually get filled in
3117 # with '0' x40 if something new gets committed
Eric Wonge5181922007-02-08 12:53:57 -08003118 foreach my $gs (@$gsv) {
Eric Wong9c93fee2007-01-31 17:22:31 -08003119 next if defined $gs->rev_db_get($max);
3120 $gs->rev_db_set($max, 0 x40);
3121 }
Eric Wongc3560e52007-02-12 16:03:32 -08003122 foreach my $g (@$globs) {
3123 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
3124 Git::SVN::tmp_config($k, $max);
3125 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08003126 last if $max >= $head;
3127 $min = $max + 1;
3128 $max += $inc;
3129 $max = $head if ($max > $head);
3130 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08003131}
3132
Eric Wonge5181922007-02-08 12:53:57 -08003133sub match_globs {
3134 my ($self, $exists, $paths, $globs, $r) = @_;
Eric Wong74a81222007-02-10 13:28:50 -08003135
3136 sub get_dir_check {
3137 my ($self, $exists, $g, $r) = @_;
3138 my @x = eval { $self->get_dir($g->{path}->{left}, $r) };
3139 return unless scalar @x == 3;
3140 my $dirents = $x[0];
3141 foreach my $de (keys %$dirents) {
3142 next if $dirents->{$de}->kind != $SVN::Node::dir;
3143 my $p = $g->{path}->full_path($de);
3144 next if $exists->{$p};
3145 next if (length $g->{path}->{right} &&
3146 ($self->check_path($p, $r) !=
3147 $SVN::Node::dir));
3148 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
3149 $g->{ref}->full_path($de), 1);
3150 }
3151 }
Eric Wonge5181922007-02-08 12:53:57 -08003152 foreach my $g (@$globs) {
Eric Wong74a81222007-02-10 13:28:50 -08003153 if (my $path = $paths->{"/$g->{path}->{left}"}) {
3154 if ($path->{action} =~ /^[AR]$/) {
3155 get_dir_check($self, $exists, $g, $r);
3156 }
3157 }
Eric Wonge5181922007-02-08 12:53:57 -08003158 foreach (keys %$paths) {
Eric Wong28710f72007-02-14 13:32:21 -08003159 if (/$g->{path}->{left_regex}/ &&
3160 !/$g->{path}->{regex}/) {
Eric Wong74a81222007-02-10 13:28:50 -08003161 next if $paths->{$_}->{action} !~ /^[AR]$/;
3162 get_dir_check($self, $exists, $g, $r);
3163 }
Eric Wonge5181922007-02-08 12:53:57 -08003164 next unless /$g->{path}->{regex}/;
3165 my $p = $1;
3166 my $pathname = $g->{path}->full_path($p);
3167 next if $exists->{$pathname};
Eric Wong0c1ec5a2007-04-18 00:17:33 -07003168 next if ($self->check_path($pathname, $r) !=
3169 $SVN::Node::dir);
Eric Wonge5181922007-02-08 12:53:57 -08003170 $exists->{$pathname} = Git::SVN->init(
3171 $self->{url}, $pathname, undef,
3172 $g->{ref}->full_path($p), 1);
3173 }
3174 my $c = '';
3175 foreach (split m#/#, $g->{path}->{left}) {
3176 $c .= "/$_";
3177 next unless ($paths->{$c} &&
Eric Wong74a81222007-02-10 13:28:50 -08003178 ($paths->{$c}->{action} =~ /^[AR]$/));
3179 get_dir_check($self, $exists, $g, $r);
Eric Wonge5181922007-02-08 12:53:57 -08003180 }
3181 }
3182 values %$exists;
3183}
3184
Eric Wonge6434f82007-01-23 16:29:23 -08003185sub minimize_url {
3186 my ($self) = @_;
3187 return $self->{url} if ($self->{url} eq $self->{repos_root});
3188 my $url = $self->{repos_root};
3189 my @components = split(m!/!, $self->{svn_path});
3190 my $c = '';
3191 do {
3192 $url .= "/$c" if length $c;
3193 eval { (ref $self)->new($url)->get_latest_revnum };
3194 } while ($@ && ($c = shift @components));
3195 $url;
3196}
3197
Eric Wongd81bf822007-01-10 01:22:38 -08003198sub can_do_switch {
3199 my $self = shift;
3200 unless (defined $can_do_switch) {
3201 my $pool = SVN::Pool->new;
3202 my $rep = eval {
3203 $self->do_switch(1, '', 0, $self->{url},
3204 SVN::Delta::Editor->new, $pool);
3205 };
3206 if ($@) {
3207 $can_do_switch = 0;
3208 } else {
3209 $rep->abort_report($pool);
3210 $can_do_switch = 1;
3211 }
3212 $pool->clear;
3213 }
3214 $can_do_switch;
3215}
3216
Eric Wong0af9c9f2007-01-27 22:28:56 -08003217sub skip_unknown_revs {
3218 my ($err) = @_;
3219 my $errno = $err->apr_err();
3220 # Maybe the branch we're tracking didn't
3221 # exist when the repo started, so it's
3222 # not an error if it doesn't, just continue
3223 #
3224 # Wonderfully consistent library, eh?
3225 # 160013 - svn:// and file://
3226 # 175002 - http(s)://
3227 # 175007 - http(s):// (this repo required authorization, too...)
3228 # More codes may be discovered later...
3229 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
Eric Wonga6a15a92007-03-30 17:54:48 -07003230 my $err_key = $err->expanded_message;
3231 # revision numbers change every time, filter them out
3232 $err_key =~ s/\d+/\0/g;
3233 $err_key = "$errno\0$err_key";
3234 unless ($ignored_err{$err_key}) {
3235 warn "W: Ignoring error from SVN, path probably ",
3236 "does not exist: ($errno): ",
3237 $err->expanded_message,"\n";
3238 $ignored_err{$err_key} = 1;
3239 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08003240 return;
3241 }
3242 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
3243}
3244
3245# svn_log_changed_path_t objects passed to get_log are likely to be
3246# overwritten even if only the refs are copied to an external variable,
3247# so we should dup the structures in their entirety. Using an externally
3248# passed pool (instead of our temporary and quickly cleared pool in
3249# Git::SVN::Ra) does not help matters at all...
3250sub dup_changed_paths {
3251 my ($paths) = @_;
3252 return undef unless $paths;
3253 my %ret;
3254 foreach my $p (keys %$paths) {
3255 my $i = $paths->{$p};
3256 my %s = map { $_ => $i->$_ }
3257 qw/copyfrom_path copyfrom_rev action/;
3258 $ret{$p} = \%s;
3259 }
3260 \%ret;
3261}
3262
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003263package Git::SVN::Log;
3264use strict;
3265use warnings;
3266use POSIX qw/strftime/;
3267use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
3268 %rusers $show_commit $incremental/;
3269my $l_fmt;
3270
3271sub cmt_showable {
3272 my ($c) = @_;
3273 return 1 if defined $c->{r};
Eric Wongc16d0872007-04-08 00:59:22 -07003274
3275 # big commit message got truncated by the 16k pretty buffer in rev-list
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003276 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
3277 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
Eric Wongc16d0872007-04-08 00:59:22 -07003278 @{$c->{l}} = ();
Eric Wong44320b92007-01-13 22:35:53 -08003279 my @log = command(qw/cat-file commit/, $c->{c});
Eric Wongc16d0872007-04-08 00:59:22 -07003280
3281 # shift off the headers
3282 shift @log while ($log[0] ne '');
Eric Wong44320b92007-01-13 22:35:53 -08003283 shift @log;
Eric Wongc16d0872007-04-08 00:59:22 -07003284
3285 # TODO: make $c->{l} not have a trailing newline in the future
3286 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003287
3288 (undef, $c->{r}, undef) = ::extract_metadata(
Eric Wong44320b92007-01-13 22:35:53 -08003289 (grep(/^git-svn-id: /, @log))[-1]);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003290 }
3291 return defined $c->{r};
3292}
3293
3294sub log_use_color {
3295 return 1 if $color;
3296 my ($dc, $dcvar);
3297 $dcvar = 'color.diff';
3298 $dc = `git-config --get $dcvar`;
3299 if ($dc eq '') {
3300 # nothing at all; fallback to "diff.color"
3301 $dcvar = 'diff.color';
3302 $dc = `git-config --get $dcvar`;
3303 }
3304 chomp($dc);
3305 if ($dc eq 'auto') {
3306 my $pc;
3307 $pc = `git-config --get color.pager`;
3308 if ($pc eq '') {
3309 # does not have it -- fallback to pager.color
3310 $pc = `git-config --bool --get pager.color`;
3311 }
3312 else {
3313 $pc = `git-config --bool --get color.pager`;
3314 if ($?) {
3315 $pc = 'false';
3316 }
3317 }
3318 chomp($pc);
3319 if (-t *STDOUT || (defined $pager && $pc eq 'true')) {
3320 return ($ENV{TERM} && $ENV{TERM} ne 'dumb');
3321 }
3322 return 0;
3323 }
3324 return 0 if $dc eq 'never';
3325 return 1 if $dc eq 'always';
3326 chomp($dc = `git-config --bool --get $dcvar`);
3327 return ($dc eq 'true');
3328}
3329
3330sub git_svn_log_cmd {
Eric Wong3bc718b2007-02-13 17:09:40 -08003331 my ($r_min, $r_max, @args) = @_;
3332 my $head = 'HEAD';
3333 foreach my $x (@args) {
3334 last if $x eq '--';
3335 next unless ::verify_ref("$x^0");
3336 $head = $x;
3337 last;
3338 }
3339
Eric Wong13c823f2007-04-08 00:59:19 -07003340 my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
3341 $gs ||= Git::SVN->_new;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003342 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
3343 $gs->refname);
3344 push @cmd, '-r' unless $non_recursive;
3345 push @cmd, qw/--raw --name-status/ if $verbose;
3346 push @cmd, '--color' if log_use_color();
3347 return @cmd unless defined $r_max;
3348 if ($r_max == $r_min) {
3349 push @cmd, '--max-count=1';
3350 if (my $c = $gs->rev_db_get($r_max)) {
3351 push @cmd, $c;
3352 }
3353 } else {
3354 my ($c_min, $c_max);
3355 $c_max = $gs->rev_db_get($r_max);
3356 $c_min = $gs->rev_db_get($r_min);
3357 if (defined $c_min && defined $c_max) {
3358 if ($r_max > $r_max) {
3359 push @cmd, "$c_min..$c_max";
3360 } else {
3361 push @cmd, "$c_max..$c_min";
3362 }
3363 } elsif ($r_max > $r_min) {
3364 push @cmd, $c_max;
3365 } else {
3366 push @cmd, $c_min;
3367 }
3368 }
3369 return @cmd;
3370}
3371
3372# adapted from pager.c
3373sub config_pager {
3374 $pager ||= $ENV{GIT_PAGER} || $ENV{PAGER};
3375 if (!defined $pager) {
3376 $pager = 'less';
3377 } elsif (length $pager == 0 || $pager eq 'cat') {
3378 $pager = undef;
3379 }
3380}
3381
3382sub run_pager {
3383 return unless -t *STDOUT;
3384 pipe my $rfd, my $wfd or return;
3385 defined(my $pid = fork) or ::fatal "Can't fork: $!\n";
3386 if (!$pid) {
3387 open STDOUT, '>&', $wfd or
3388 ::fatal "Can't redirect to stdout: $!\n";
3389 return;
3390 }
3391 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!\n";
3392 $ENV{LESS} ||= 'FRSX';
3393 exec $pager or ::fatal "Can't run pager: $! ($pager)\n";
3394}
3395
Eric Wong21819a32007-01-27 14:38:10 -08003396sub tz_to_s_offset {
3397 my ($tz) = @_;
3398 $tz =~ s/(\d\d)$//;
3399 return ($1 * 60) + ($tz * 3600);
3400}
3401
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003402sub get_author_info {
3403 my ($dest, $author, $t, $tz) = @_;
3404 $author =~ s/(?:^\s*|\s*$)//g;
3405 $dest->{a_raw} = $author;
3406 my $au;
Eric Wong1c8443b2007-01-14 02:17:00 -08003407 if ($::_authors) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003408 $au = $rusers{$author} || undef;
3409 }
3410 if (!$au) {
3411 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
3412 }
3413 $dest->{t} = $t;
3414 $dest->{tz} = $tz;
3415 $dest->{a} = $au;
3416 # Date::Parse isn't in the standard Perl distro :(
3417 if ($tz =~ s/^\+//) {
Eric Wong21819a32007-01-27 14:38:10 -08003418 $t += tz_to_s_offset($tz);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003419 } elsif ($tz =~ s/^\-//) {
Eric Wong21819a32007-01-27 14:38:10 -08003420 $t -= tz_to_s_offset($tz);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003421 }
3422 $dest->{t_utc} = $t;
3423}
3424
3425sub process_commit {
3426 my ($c, $r_min, $r_max, $defer) = @_;
3427 if (defined $r_min && defined $r_max) {
3428 if ($r_min == $c->{r} && $r_min == $r_max) {
3429 show_commit($c);
3430 return 0;
3431 }
3432 return 1 if $r_min == $r_max;
3433 if ($r_min < $r_max) {
3434 # we need to reverse the print order
3435 return 0 if (defined $limit && --$limit < 0);
3436 push @$defer, $c;
3437 return 1;
3438 }
3439 if ($r_min != $r_max) {
3440 return 1 if ($r_min < $c->{r});
3441 return 1 if ($r_max > $c->{r});
3442 }
3443 }
3444 return 0 if (defined $limit && --$limit < 0);
3445 show_commit($c);
3446 return 1;
3447}
3448
3449sub show_commit {
3450 my $c = shift;
3451 if ($oneline) {
3452 my $x = "\n";
3453 if (my $l = $c->{l}) {
3454 while ($l->[0] =~ /^\s*$/) { shift @$l }
3455 $x = $l->[0];
3456 }
3457 $l_fmt ||= 'A' . length($c->{r});
3458 print 'r',pack($l_fmt, $c->{r}),' | ';
3459 print "$c->{c} | " if $show_commit;
3460 print $x;
3461 } else {
3462 show_commit_normal($c);
3463 }
3464}
3465
3466sub show_commit_changed_paths {
3467 my ($c) = @_;
3468 return unless $c->{changed};
3469 print "Changed paths:\n", @{$c->{changed}};
3470}
3471
3472sub show_commit_normal {
3473 my ($c) = @_;
3474 print '-' x72, "\nr$c->{r} | ";
3475 print "$c->{c} | " if $show_commit;
3476 print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
3477 localtime($c->{t_utc})), ' | ';
3478 my $nr_line = 0;
3479
3480 if (my $l = $c->{l}) {
3481 while ($l->[$#$l] eq "\n" && $#$l > 0
3482 && $l->[($#$l - 1)] eq "\n") {
3483 pop @$l;
3484 }
3485 $nr_line = scalar @$l;
3486 if (!$nr_line) {
3487 print "1 line\n\n\n";
3488 } else {
3489 if ($nr_line == 1) {
3490 $nr_line = '1 line';
3491 } else {
3492 $nr_line .= ' lines';
3493 }
3494 print $nr_line, "\n";
3495 show_commit_changed_paths($c);
3496 print "\n";
3497 print $_ foreach @$l;
3498 }
3499 } else {
3500 print "1 line\n";
3501 show_commit_changed_paths($c);
3502 print "\n";
3503
3504 }
Eric Wong488a63e2007-02-15 00:40:42 -08003505 foreach my $x (qw/raw stat diff/) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003506 if ($c->{$x}) {
3507 print "\n";
3508 print $_ foreach @{$c->{$x}}
3509 }
3510 }
3511}
3512
3513sub cmd_show_log {
3514 my (@args) = @_;
3515 my ($r_min, $r_max);
3516 my $r_last = -1; # prevent dupes
3517 if (defined $TZ) {
3518 $ENV{TZ} = $TZ;
3519 } else {
3520 delete $ENV{TZ};
3521 }
3522 if (defined $::_revision) {
3523 if ($::_revision =~ /^(\d+):(\d+)$/) {
3524 ($r_min, $r_max) = ($1, $2);
3525 } elsif ($::_revision =~ /^\d+$/) {
3526 $r_min = $r_max = $::_revision;
3527 } else {
3528 ::fatal "-r$::_revision is not supported, use ",
3529 "standard \'git log\' arguments instead\n";
3530 }
3531 }
3532
3533 config_pager();
Eric Wong3bc718b2007-02-13 17:09:40 -08003534 @args = (git_svn_log_cmd($r_min, $r_max, @args), @args);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003535 my $log = command_output_pipe(@args);
3536 run_pager();
Eric Wong488a63e2007-02-15 00:40:42 -08003537 my (@k, $c, $d, $stat);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003538 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
3539 while (<$log>) {
3540 if (/^${esc_color}commit ($::sha1_short)/o) {
3541 my $cmt = $1;
3542 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
3543 $r_last = $c->{r};
3544 process_commit($c, $r_min, $r_max, \@k) or
3545 goto out;
3546 }
3547 $d = undef;
3548 $c = { c => $cmt };
3549 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
3550 get_author_info($c, $1, $2, $3);
3551 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
3552 # ignore
3553 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
3554 push @{$c->{raw}}, $_;
3555 } elsif (/^${esc_color}[ACRMDT]\t/) {
3556 # we could add $SVN->{svn_path} here, but that requires
3557 # remote access at the moment (repo_path_split)...
3558 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
3559 push @{$c->{changed}}, $_;
3560 } elsif (/^${esc_color}diff /o) {
3561 $d = 1;
3562 push @{$c->{diff}}, $_;
3563 } elsif ($d) {
3564 push @{$c->{diff}}, $_;
Eric Wong488a63e2007-02-15 00:40:42 -08003565 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
3566 $esc_color*[\+\-]*$esc_color$/x) {
3567 $stat = 1;
3568 push @{$c->{stat}}, $_;
3569 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
3570 push @{$c->{stat}}, $_;
3571 $stat = undef;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003572 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
3573 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
3574 } elsif (s/^${esc_color} //o) {
3575 push @{$c->{l}}, $_;
3576 }
3577 }
3578 if ($c && defined $c->{r} && $c->{r} != $r_last) {
3579 $r_last = $c->{r};
3580 process_commit($c, $r_min, $r_max, \@k);
3581 }
3582 if (@k) {
3583 my $swap = $r_max;
3584 $r_max = $r_min;
3585 $r_min = $swap;
3586 process_commit($_, $r_min, $r_max) foreach reverse @k;
3587 }
3588out:
Eric Wongc843c462007-01-12 03:07:31 -08003589 close $log;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003590 print '-' x72,"\n" unless $incremental || $oneline;
3591}
3592
Eric Wong706587f2007-01-18 17:50:01 -08003593package Git::SVN::Migration;
3594# these version numbers do NOT correspond to actual version numbers
3595# of git nor git-svn. They are just relative.
3596#
3597# v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
3598#
3599# v1 layout: .git/$id/info/url, refs/remotes/$id
3600#
3601# v2 layout: .git/svn/$id/info/url, refs/remotes/$id
3602#
3603# v3 layout: .git/svn/$id, refs/remotes/$id
3604# - info/url may remain for backwards compatibility
3605# - this is what we migrate up to this layout automatically,
3606# - this will be used by git svn init on single branches
Eric Wong26a62d52007-02-12 13:25:25 -08003607# v3.1 layout (auto migrated):
3608# - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
3609# for backwards compatibility
Eric Wong706587f2007-01-18 17:50:01 -08003610#
3611# v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
3612# - this is only created for newly multi-init-ed
3613# repositories. Similar in spirit to the
3614# --use-separate-remotes option in git-clone (now default)
3615# - we do not automatically migrate to this (following
3616# the example set by core git)
3617use strict;
3618use warnings;
3619use Carp qw/croak/;
3620use File::Path qw/mkpath/;
Eric Wong47e39c52007-01-21 04:27:09 -08003621use File::Basename qw/dirname basename/;
3622use vars qw/$_minimize/;
Eric Wong706587f2007-01-18 17:50:01 -08003623
3624sub migrate_from_v0 {
3625 my $git_dir = $ENV{GIT_DIR};
3626 return undef unless -d $git_dir;
3627 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
3628 my $migrated = 0;
3629 while (<$fh>) {
3630 chomp;
3631 my ($id, $orig_ref) = ($_, $_);
3632 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
3633 next unless -f "$git_dir/$id/info/url";
3634 my $new_ref = "refs/remotes/$id";
3635 if (::verify_ref("$new_ref^0")) {
3636 print STDERR "W: $orig_ref is probably an old ",
3637 "branch used by an ancient version of ",
3638 "git-svn.\n",
3639 "However, $new_ref also exists.\n",
3640 "We will not be able ",
3641 "to use this branch until this ",
3642 "ambiguity is resolved.\n";
3643 next;
3644 }
3645 print STDERR "Migrating from v0 layout...\n" if !$migrated;
3646 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
3647 command_noisy('update-ref', $new_ref, $orig_ref);
3648 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
3649 $migrated++;
3650 }
3651 command_close_pipe($fh, $ctx);
3652 print STDERR "Done migrating from v0 layout...\n" if $migrated;
3653 $migrated;
3654}
3655
3656sub migrate_from_v1 {
3657 my $git_dir = $ENV{GIT_DIR};
3658 my $migrated = 0;
3659 return $migrated unless -d $git_dir;
3660 my $svn_dir = "$git_dir/svn";
3661
3662 # just in case somebody used 'svn' as their $id at some point...
3663 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
3664
3665 print STDERR "Migrating from a git-svn v1 layout...\n";
3666 mkpath([$svn_dir]);
3667 print STDERR "Data from a previous version of git-svn exists, but\n\t",
3668 "$svn_dir\n\t(required for this version ",
3669 "($::VERSION) of git-svn) does not. exist\n";
3670 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
3671 while (<$fh>) {
3672 my $x = $_;
3673 next unless $x =~ s#^refs/remotes/##;
3674 chomp $x;
3675 next unless -f "$git_dir/$x/info/url";
3676 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
3677 next unless $u;
3678 my $dn = dirname("$git_dir/svn/$x");
3679 mkpath([$dn]) unless -d $dn;
3680 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
3681 mkpath(["$git_dir/svn/svn"]);
3682 print STDERR " - $git_dir/$x/info => ",
3683 "$git_dir/svn/$x/info\n";
3684 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
3685 croak "$!: $x";
3686 # don't worry too much about these, they probably
3687 # don't exist with repos this old (save for index,
3688 # and we can easily regenerate that)
3689 foreach my $f (qw/unhandled.log index .rev_db/) {
3690 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
3691 }
3692 } else {
3693 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
3694 rename "$git_dir/$x", "$git_dir/svn/$x" or
3695 croak "$!: $x";
3696 }
3697 $migrated++;
3698 }
3699 command_close_pipe($fh, $ctx);
3700 print STDERR "Done migrating from a git-svn v1 layout\n";
3701 $migrated;
3702}
3703
3704sub read_old_urls {
3705 my ($l_map, $pfx, $path) = @_;
3706 my @dir;
3707 foreach (<$path/*>) {
3708 if (-r "$_/info/url") {
3709 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
3710 my $ref_id = $pfx . basename $_;
3711 my $url = ::file_to_s("$_/info/url");
3712 $l_map->{$ref_id} = $url;
3713 } elsif (-d $_) {
3714 push @dir, $_;
3715 }
3716 }
3717 foreach (@dir) {
3718 my $x = $_;
3719 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
3720 read_old_urls($l_map, $x, $_);
3721 }
3722}
3723
3724sub migrate_from_v2 {
3725 my @cfg = command(qw/config -l/);
3726 return if grep /^svn-remote\..+\.url=/, @cfg;
3727 my %l_map;
3728 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
3729 my $migrated = 0;
3730
3731 foreach my $ref_id (sort keys %l_map) {
Eric Wong471bc002007-02-01 04:06:27 -08003732 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
3733 if ($@) {
3734 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
3735 }
Eric Wong706587f2007-01-18 17:50:01 -08003736 $migrated++;
3737 }
3738 $migrated;
3739}
3740
Eric Wong47e39c52007-01-21 04:27:09 -08003741sub minimize_connections {
3742 my $r = Git::SVN::read_all_remotes();
3743 my $new_urls = {};
3744 my $root_repos = {};
3745 foreach my $repo_id (keys %$r) {
3746 my $url = $r->{$repo_id}->{url} or next;
3747 my $fetch = $r->{$repo_id}->{fetch} or next;
3748 my $ra = Git::SVN::Ra->new($url);
3749
3750 # skip existing cases where we already connect to the root
3751 if (($ra->{url} eq $ra->{repos_root}) ||
3752 (Git::SVN::sanitize_remote_name($ra->{repos_root}) eq
3753 $repo_id)) {
3754 $root_repos->{$ra->{url}} = $repo_id;
3755 next;
3756 }
3757
3758 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
3759 my $root_path = $ra->{url};
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003760 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
Eric Wong47e39c52007-01-21 04:27:09 -08003761 foreach my $path (keys %$fetch) {
3762 my $ref_id = $fetch->{$path};
3763 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
3764
3765 # make sure we can read when connecting to
3766 # a higher level of a repository
3767 my ($last_rev, undef) = $gs->last_rev_commit;
3768 if (!defined $last_rev) {
3769 $last_rev = eval {
3770 $root_ra->get_latest_revnum;
3771 };
3772 next if $@;
3773 }
3774 my $new = $root_path;
3775 $new .= length $path ? "/$path" : '';
3776 eval {
3777 $root_ra->get_log([$new], $last_rev, $last_rev,
3778 0, 0, 1, sub { });
3779 };
3780 next if $@;
3781 $new_urls->{$ra->{repos_root}}->{$new} =
3782 { ref_id => $ref_id,
3783 old_repo_id => $repo_id,
3784 old_path => $path };
3785 }
3786 }
3787
3788 my @emptied;
3789 foreach my $url (keys %$new_urls) {
3790 # see if we can re-use an existing [svn-remote "repo_id"]
3791 # instead of creating a(n ugly) new section:
3792 my $repo_id = $root_repos->{$url} ||
3793 Git::SVN::sanitize_remote_name($url);
3794
3795 my $fetch = $new_urls->{$url};
3796 foreach my $path (keys %$fetch) {
3797 my $x = $fetch->{$path};
3798 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
3799 my $pfx = "svn-remote.$x->{old_repo_id}";
3800
3801 my $old_fetch = quotemeta("$x->{old_path}:".
3802 "refs/remotes/$x->{ref_id}");
Eric Wong8b8fc062007-01-22 11:44:57 -08003803 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08003804 "$pfx.fetch", '^'. $old_fetch . '$');
3805 delete $r->{$x->{old_repo_id}}->
3806 {fetch}->{$x->{old_path}};
3807 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
Eric Wong8b8fc062007-01-22 11:44:57 -08003808 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08003809 "$pfx.url");
3810 push @emptied, $x->{old_repo_id}
3811 }
3812 }
3813 }
3814 if (@emptied) {
3815 my $file = $ENV{GIT_CONFIG} || $ENV{GIT_CONFIG_LOCAL} ||
3816 "$ENV{GIT_DIR}/config";
3817 print STDERR <<EOF;
3818The following [svn-remote] sections in your config file ($file) are empty
3819and can be safely removed:
3820EOF
3821 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
3822 }
3823}
3824
Eric Wong706587f2007-01-18 17:50:01 -08003825sub migration_check {
3826 migrate_from_v0();
3827 migrate_from_v1();
3828 migrate_from_v2();
Eric Wong47e39c52007-01-21 04:27:09 -08003829 minimize_connections() if $_minimize;
Eric Wong706587f2007-01-18 17:50:01 -08003830}
3831
Eric Wongef3cfaa2007-01-24 03:30:57 -08003832package Git::IndexInfo;
3833use strict;
3834use warnings;
3835use Git qw/command_input_pipe command_close_pipe/;
3836
3837sub new {
3838 my ($class) = @_;
3839 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
3840 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
3841}
3842
3843sub remove {
3844 my ($self, $path) = @_;
3845 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
3846 return ++$self->{nr};
3847 }
3848 undef;
3849}
3850
3851sub update {
3852 my ($self, $mode, $hash, $path) = @_;
3853 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
3854 return ++$self->{nr};
3855 }
3856 undef;
3857}
3858
3859sub DESTROY {
3860 my ($self) = @_;
3861 command_close_pipe($self->{gui}, $self->{ctx});
3862}
3863
Eric Wong4bb9ed02007-02-03 13:29:17 -08003864package Git::SVN::GlobSpec;
3865use strict;
3866use warnings;
3867
3868sub new {
3869 my ($class, $glob) = @_;
Eric Wong4bb9ed02007-02-03 13:29:17 -08003870 my $re = $glob;
3871 $re =~ s!/+$!!g; # no need for trailing slashes
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003872 my $nr = ($re =~ s!^(.*)\*(.*)$!\(\[^/\]+\)!g);
Eric Wong4bb9ed02007-02-03 13:29:17 -08003873 my ($left, $right) = ($1, $2);
3874 if ($nr > 1) {
Eric Wonge5181922007-02-08 12:53:57 -08003875 die "Only one '*' wildcard expansion ",
3876 "is supported (got $nr): '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08003877 } elsif ($nr == 0) {
Eric Wonge5181922007-02-08 12:53:57 -08003878 die "One '*' is needed for glob: '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08003879 }
3880 $re = quotemeta($left) . $re . quotemeta($right);
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003881 if (length $left && !($left =~ s!/+$!!g)) {
3882 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
3883 }
3884 if (length $right && !($right =~ s!^/+!!g)) {
3885 die "Missing leading '/' on right side of: '$glob' ($right)\n";
3886 }
Eric Wong74a81222007-02-10 13:28:50 -08003887 my $left_re = qr/^\/\Q$left\E(\/|$)/;
3888 bless { left => $left, right => $right, left_regex => $left_re,
Eric Wong4bb9ed02007-02-03 13:29:17 -08003889 regex => qr/$re/, glob => $glob }, $class;
3890}
3891
3892sub full_path {
3893 my ($self, $path) = @_;
3894 return (length $self->{left} ? "$self->{left}/" : '') .
3895 $path . (length $self->{right} ? "/$self->{right}" : '');
3896}
3897
Eric Wong3397f9d2006-02-16 01:24:16 -08003898__END__
3899
3900Data structures:
3901
Eric Wong4bb9ed02007-02-03 13:29:17 -08003902
3903$remotes = { # returned by read_all_remotes()
3904 'svn' => {
3905 # svn-remote.svn.url=https://svn.musicpd.org
3906 url => 'https://svn.musicpd.org',
3907 # svn-remote.svn.fetch=mpd/trunk:trunk
3908 fetch => {
3909 'mpd/trunk' => 'trunk',
3910 },
3911 # svn-remote.svn.tags=mpd/tags/*:tags/*
3912 tags => {
3913 path => {
3914 left => 'mpd/tags',
3915 right => '',
3916 regex => qr!mpd/tags/([^/]+)$!,
3917 glob => 'tags/*',
3918 },
3919 ref => {
3920 left => 'tags',
3921 right => '',
3922 regex => qr!tags/([^/]+)$!,
3923 glob => 'tags/*',
3924 },
3925 }
3926 }
3927};
3928
Eric Wong44320b92007-01-13 22:35:53 -08003929$log_entry hashref as returned by libsvn_log_entry()
Eric Wong3397f9d2006-02-16 01:24:16 -08003930{
Eric Wong44320b92007-01-13 22:35:53 -08003931 log => 'whitespace-formatted log entry
Eric Wong3397f9d2006-02-16 01:24:16 -08003932', # trailing newline is preserved
3933 revision => '8', # integer
3934 date => '2004-02-24T17:01:44.108345Z', # commit date
3935 author => 'committer name'
3936};
3937
Eric Wong6e8548c2007-01-27 01:32:00 -08003938
3939# this is generated by generate_diff();
Eric Wong3397f9d2006-02-16 01:24:16 -08003940@mods = array of diff-index line hashes, each element represents one line
3941 of diff-index output
3942
3943diff-index line ($m hash)
3944{
3945 mode_a => first column of diff-index output, no leading ':',
3946 mode_b => second column of diff-index output,
3947 sha1_b => sha1sum of the final blob,
Eric Wongac8e0b92006-03-03 01:20:07 -08003948 chg => change type [MCRADT],
Eric Wong3397f9d2006-02-16 01:24:16 -08003949 file_a => original file name of a file (iff chg is 'C' or 'R')
3950 file_b => new/current file name of a file (any chg)
3951}
3952;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003953
Eric Wonga00439a2006-06-27 19:39:13 -07003954# retval of read_url_paths{,_all}();
3955$l_map = {
3956 # repository root url
3957 'https://svn.musicpd.org' => {
3958 # repository path # GIT_SVN_ID
3959 'mpd/trunk' => 'trunk',
3960 'mpd/tags/0.11.5' => 'tags/0.11.5',
3961 },
3962}
3963
Eric Wonga5e0ced2006-06-12 15:23:48 -07003964Notes:
3965 I don't trust the each() function on unless I created %hash myself
3966 because the internal iterator may not have started at base.