blob: 2bd70a15779c3288d52bcb401bb0c91f46fbbfde [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 Wong79bb8d82006-06-01 02:35:44 -070036use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
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 Wongf8c9d1d2007-01-12 02:35:20 -080059 $_merge, $_strategy, $_dry_run,
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,
148 'fetch-all|all' => \$_fetch_all,
149 %fc_opts } ],
Eric Wong44320b92007-01-13 22:35:53 -0800150 'commit-diff' => [ \&cmd_commit_diff,
151 'Commit a diff between two trees',
Eric Wong27e9fb82006-06-27 19:39:12 -0700152 { 'message|m=s' => \$_message,
153 'file|F=s' => \$_file,
Eric Wong45bf4732006-11-09 01:19:37 -0800154 'revision|r=s' => \$_revision,
Eric Wong27e9fb82006-06-27 19:39:12 -0700155 %cmt_opts } ],
Eric Wong3397f9d2006-02-16 01:24:16 -0800156);
Eric Wong9d55b412006-06-12 15:53:13 -0700157
Eric Wong3397f9d2006-02-16 01:24:16 -0800158my $cmd;
159for (my $i = 0; $i < @ARGV; $i++) {
160 if (defined $cmd{$ARGV[$i]}) {
161 $cmd = $ARGV[$i];
162 splice @ARGV, $i, 1;
163 last;
164 }
165};
166
Eric Wong448c81b2006-03-03 01:20:09 -0800167my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
Eric Wonga9612be2006-03-03 01:20:08 -0800168
Eric Wongb8c92ca2006-05-24 01:40:37 -0700169read_repo_config(\%opts);
Eric Wong9760adc2007-01-31 03:06:56 -0800170my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
171 'minimize-connections' => \$Git::SVN::Migration::_minimize,
172 'id|i=s' => \$Git::SVN::default_ref_id,
Eric Wongbefc9ad2007-02-17 02:53:07 -0800173 'svn-remote|remote|R=s' => sub {
174 $Git::SVN::no_reuse_existing = 1;
175 $Git::SVN::default_repo_id = $_[1] });
Eric Wong79bb8d82006-06-01 02:35:44 -0700176exit 1 if (!$rv && $cmd ne 'log');
Eric Wong6f0783c2006-03-03 01:20:09 -0800177
Eric Wong3397f9d2006-02-16 01:24:16 -0800178usage(0) if $_help;
Eric Wong551ce282006-02-20 10:57:29 -0800179version() if $_version;
Eric Wongeeb0abe2006-03-03 01:20:08 -0800180usage(1) unless defined $cmd;
181load_authors() if $_authors;
Eric Wong5253dc32007-02-20 01:36:30 -0800182
183# make sure we're always running
184unless ($cmd =~ /(?:clone|init|multi-init)$/) {
185 unless (-d $ENV{GIT_DIR}) {
186 if ($git_dir_user_set) {
187 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
188 "but it is not a directory\n";
189 }
190 my $git_dir = delete $ENV{GIT_DIR};
191 chomp(my $cdup = command_oneline(qw/rev-parse --show-cdup/));
192 unless (length $cdup) {
193 die "Already at toplevel, but $git_dir ",
194 "not found '$cdup'\n";
195 }
196 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
197 unless (-d $git_dir) {
198 die "$git_dir still not found after going to ",
199 "'$cdup'\n";
200 }
201 $ENV{GIT_DIR} = $git_dir;
202 }
203}
Eric Wong0425ea92007-02-16 18:45:01 -0800204unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
Eric Wong706587f2007-01-18 17:50:01 -0800205 Git::SVN::Migration::migration_check();
206}
Eric Wongecc712d2007-01-31 12:28:10 -0800207Git::SVN::init_vars();
Eric Wongb805b442007-01-22 13:52:04 -0800208eval {
209 Git::SVN::verify_remotes_sanity();
210 $cmd{$cmd}->[0]->(@ARGV);
211};
212fatal $@ if $@;
Eric Wong1e889ef2007-02-16 01:45:13 -0800213post_fetch_checkout();
Eric Wong3397f9d2006-02-16 01:24:16 -0800214exit 0;
215
216####################### primary functions ######################
217sub usage {
218 my $exit = shift || 0;
219 my $fd = $exit ? \*STDERR : \*STDOUT;
220 print $fd <<"";
221git-svn - bidirectional operations between a single Subversion tree and git
222Usage: $0 <command> [options] [arguments]\n
Eric Wong448c81b2006-03-03 01:20:09 -0800223
224 print $fd "Available commands:\n" unless $cmd;
Eric Wong3397f9d2006-02-16 01:24:16 -0800225
226 foreach (sort keys %cmd) {
Eric Wong448c81b2006-03-03 01:20:09 -0800227 next if $cmd && $cmd ne $_;
Eric Wonga836a0e2007-02-14 19:34:56 -0800228 next if /^multi-/; # don't show deprecated commands
Eric Wongb203b762006-10-11 14:53:36 -0700229 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
Eric Wong448c81b2006-03-03 01:20:09 -0800230 foreach (keys %{$cmd{$_}->[2]}) {
231 # prints out arguments as they should be passed:
Eric Wongb8c92ca2006-05-24 01:40:37 -0700232 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
Eric Wongb203b762006-10-11 14:53:36 -0700233 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
Eric Wong448c81b2006-03-03 01:20:09 -0800234 "--$_" : "-$_" }
235 split /\|/,$_)," $x\n";
236 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800237 }
238 print $fd <<"";
Eric Wong448c81b2006-03-03 01:20:09 -0800239\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
240arbitrary identifier if you're tracking multiple SVN branches/repositories in
241one git repository and want to keep them separate. See git-svn(1) for more
242information.
Eric Wong3397f9d2006-02-16 01:24:16 -0800243
244 exit $exit;
245}
246
Eric Wong551ce282006-02-20 10:57:29 -0800247sub version {
Eric Wong7d60ab22006-12-28 01:16:20 -0800248 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
Eric Wong551ce282006-02-20 10:57:29 -0800249 exit 0;
250}
251
Eric Wong8164b652007-01-11 15:35:55 -0800252sub do_git_init_db {
253 unless (-d $ENV{GIT_DIR}) {
254 my @init_db = ('init');
255 push @init_db, "--template=$_template" if defined $_template;
Eric Wongdadc6d22007-02-14 12:27:41 -0800256 if (defined $_shared) {
257 if ($_shared =~ /[a-z]/) {
258 push @init_db, "--shared=$_shared";
259 } else {
260 push @init_db, "--shared";
261 }
262 }
Eric Wong8164b652007-01-11 15:35:55 -0800263 command_noisy(@init_db);
264 }
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800265 my $set;
266 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
267 foreach my $i (keys %icv) {
268 die "'$set' and '$i' cannot both be set\n" if $set;
269 next unless defined $icv{$i};
270 command_noisy('config', "$pfx.$i", $icv{$i});
271 $set = $i;
272 }
Eric Wong8164b652007-01-11 15:35:55 -0800273}
274
Eric Wongdadc6d22007-02-14 12:27:41 -0800275sub init_subdir {
276 my $repo_path = shift or return;
277 mkpath([$repo_path]) unless -d $repo_path;
278 chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
Eric Wongf30603f2007-02-23 01:26:26 -0800279 $ENV{GIT_DIR} = '.git';
Eric Wongdadc6d22007-02-14 12:27:41 -0800280}
281
Eric Wong0425ea92007-02-16 18:45:01 -0800282sub cmd_clone {
283 my ($url, $path) = @_;
284 if (!defined $path &&
285 (defined $_trunk || defined $_branches || defined $_tags) &&
286 $url !~ m#^[a-z\+]+://#) {
287 $path = $url;
288 }
Eric Wong0425ea92007-02-16 18:45:01 -0800289 $path = basename($url) if !defined $path || !length $path;
Eric Wongf30603f2007-02-23 01:26:26 -0800290 cmd_init($url, $path);
Eric Wong0425ea92007-02-16 18:45:01 -0800291 Git::SVN::fetch_all($Git::SVN::default_repo_id);
292}
293
Eric Wongd2866f92007-01-11 12:26:16 -0800294sub cmd_init {
Eric Wongdadc6d22007-02-14 12:27:41 -0800295 if (defined $_trunk || defined $_branches || defined $_tags) {
296 return cmd_multi_init(@_);
Eric Wong03e0ea82006-06-30 21:42:53 -0700297 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800298 my $url = shift or die "SVN repository location required ",
299 "as a command-line argument\n";
300 init_subdir(@_);
Eric Wong8164b652007-01-11 15:35:55 -0800301 do_git_init_db();
Eric Wong03e0ea82006-06-30 21:42:53 -0700302
Eric Wong706587f2007-01-18 17:50:01 -0800303 Git::SVN->init($url);
Eric Wong3397f9d2006-02-16 01:24:16 -0800304}
305
Eric Wong2a3240b2007-01-04 18:09:56 -0800306sub cmd_fetch {
Eric Wonge98671e2007-02-14 02:21:19 -0800307 if (grep /^\d+=./, @_) {
308 die "'<rev>=<commit>' fetch arguments are ",
309 "no longer supported.\n";
Eric Wong07a1c952007-01-22 15:47:41 -0800310 }
Eric Wonge98671e2007-02-14 02:21:19 -0800311 my ($remote) = @_;
312 if (@_ > 1) {
Eric Wong905f8b72007-02-16 03:22:40 -0800313 die "Usage: $0 fetch [--all] [svn-remote]\n";
Eric Wonge98671e2007-02-14 02:21:19 -0800314 }
315 $remote ||= $Git::SVN::default_repo_id;
316 if ($_fetch_all) {
317 cmd_multi_fetch();
318 } else {
319 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
Eric Wong1c8443b2007-01-14 02:17:00 -0800320 }
Eric Wong2a3240b2007-01-04 18:09:56 -0800321}
322
Eric Wong1ce255d2007-01-14 23:21:16 -0800323sub cmd_set_tree {
Eric Wong3397f9d2006-02-16 01:24:16 -0800324 my (@commits) = @_;
325 if ($_stdin || !@commits) {
326 print "Reading from stdin...\n";
327 @commits = ();
328 while (<STDIN>) {
Eric Wong1ca72ae2006-03-03 01:20:09 -0800329 if (/\b($sha1_short)\b/o) {
Eric Wong3397f9d2006-02-16 01:24:16 -0800330 unshift @commits, $1;
331 }
332 }
333 }
334 my @revs;
Eric Wong8de010a2006-02-20 10:57:26 -0800335 foreach my $c (@commits) {
Eric Wongaef4e922006-12-15 10:59:54 -0800336 my @tmp = command('rev-parse',$c);
Eric Wong8de010a2006-02-20 10:57:26 -0800337 if (scalar @tmp == 1) {
338 push @revs, $tmp[0];
339 } elsif (scalar @tmp > 1) {
Eric Wongaef4e922006-12-15 10:59:54 -0800340 push @revs, reverse(command('rev-list',@tmp));
Eric Wong8de010a2006-02-20 10:57:26 -0800341 } else {
Eric Wong1ce255d2007-01-14 23:21:16 -0800342 fatal "Failed to rev-parse $c\n";
Eric Wong8de010a2006-02-20 10:57:26 -0800343 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800344 }
Eric Wong1ce255d2007-01-14 23:21:16 -0800345 my $gs = Git::SVN->new;
346 my ($r_last, $cmt_last) = $gs->last_rev_commit;
347 $gs->fetch;
Eric Wong97f69872007-01-25 11:53:13 -0800348 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
Eric Wong1ce255d2007-01-14 23:21:16 -0800349 fatal "There are new revisions that were fetched ",
350 "and need to be merged (or acknowledged) ",
351 "before committing.\nlast rev: $r_last\n",
352 " current: $gs->{last_rev}\n";
353 }
354 $gs->set_tree($_) foreach @revs;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700355 print "Done committing ",scalar @revs," revisions to SVN\n";
356}
357
Eric Wongd7ad3be2007-01-14 03:14:28 -0800358sub cmd_dcommit {
359 my $head = shift;
Eric Wongd7ad3be2007-01-14 03:14:28 -0800360 $head ||= 'HEAD';
Eric Wonga8ae2622007-02-13 14:22:11 -0800361 my @refs;
Eric Wong905f8b72007-02-16 03:22:40 -0800362 my ($url, $rev, $uuid) = working_head_info($head, \@refs);
363 my $c = $refs[-1];
Eric Wonga8ae2622007-02-13 14:22:11 -0800364 unless (defined $url && defined $rev && defined $uuid) {
365 die "Unable to determine upstream SVN information from ",
Eric Wong905f8b72007-02-16 03:22:40 -0800366 "$head history\n";
Eric Wonga8ae2622007-02-13 14:22:11 -0800367 }
Eric Wong60d9c972007-02-14 18:47:16 -0800368 my $gs = Git::SVN->find_by_url($url);
Eric Wong45bf4732006-11-09 01:19:37 -0800369 my $last_rev;
Eric Wonga8ae2622007-02-13 14:22:11 -0800370 foreach my $d (@refs) {
Eric Wongaef4e922006-12-15 10:59:54 -0800371 if (!verify_ref("$d~1")) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800372 fatal "Commit $d\n",
373 "has no parent commit, and therefore ",
374 "nothing to diff against.\n",
375 "You should be working from a repository ",
376 "originally created by git-svn\n";
Eric Wong48d044b2006-11-23 14:54:03 -0800377 }
Eric Wong45bf4732006-11-09 01:19:37 -0800378 unless (defined $last_rev) {
379 (undef, $last_rev, undef) = cmt_metadata("$d~1");
380 unless (defined $last_rev) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800381 fatal "Unable to extract revision information ",
382 "from commit $d~1\n";
Eric Wong45bf4732006-11-09 01:19:37 -0800383 }
384 }
Eric Wongb22d4492006-08-26 00:01:23 -0700385 if ($_dry_run) {
386 print "diff-tree $d~1 $d\n";
387 } else {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800388 my %ed_opts = ( r => $last_rev,
Eric Wong61395352007-01-27 14:33:08 -0800389 log => get_commit_entry($d)->{log},
Eric Wonga8ae2622007-02-13 14:22:11 -0800390 ra => Git::SVN::Ra->new($url),
Eric Wong61395352007-01-27 14:33:08 -0800391 tree_a => "$d~1",
392 tree_b => $d,
393 editor_cb => sub {
394 print "Committed r$_[0]\n";
395 $last_rev = $_[0]; },
Eric Wonga8ae2622007-02-13 14:22:11 -0800396 svn_path => '');
Eric Wong61395352007-01-27 14:33:08 -0800397 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800398 print "No changes\n$d~1 == $d\n";
399 }
Eric Wongb22d4492006-08-26 00:01:23 -0700400 }
401 }
402 return if $_dry_run;
Eric Wong60d9c972007-02-14 18:47:16 -0800403 unless ($gs) {
404 warn "Could not determine fetch information for $url\n",
405 "Will not attempt to fetch and rebase commits.\n",
406 "This probably means you have useSvmProps and should\n",
407 "now resync your SVN::Mirror repository.\n";
408 return;
409 }
Eric Wong905f8b72007-02-16 03:22:40 -0800410 $_fetch_all ? $gs->fetch_all : $gs->fetch;
Eric Wongd7ad3be2007-01-14 03:14:28 -0800411 # we always want to rebase against the current HEAD, not any
412 # head that was passed to us
413 my @diff = command('diff-tree', 'HEAD', $gs->refname, '--');
Eric Wongb22d4492006-08-26 00:01:23 -0700414 my @finish;
415 if (@diff) {
Eric Wong905f8b72007-02-16 03:22:40 -0800416 @finish = rebase_cmd();
Eric Wongd7ad3be2007-01-14 03:14:28 -0800417 print STDERR "W: HEAD and ", $gs->refname, " differ, ",
418 "using @finish:\n", "@diff";
Eric Wongb22d4492006-08-26 00:01:23 -0700419 } else {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800420 print "No changes between current HEAD and ",
421 $gs->refname, "\nResetting to the latest ",
422 $gs->refname, "\n";
Eric Wong47694892006-11-23 14:54:05 -0800423 @finish = qw/reset --mixed/;
Eric Wongb22d4492006-08-26 00:01:23 -0700424 }
Eric Wongd7ad3be2007-01-14 03:14:28 -0800425 command_noisy(@finish, $gs->refname);
Eric Wongb22d4492006-08-26 00:01:23 -0700426}
427
Eric Wong905f8b72007-02-16 03:22:40 -0800428sub cmd_rebase {
429 command_noisy(qw/update-index --refresh/);
430 my $url = (working_head_info('HEAD'))[0];
431 if (!defined $url) {
432 die "Unable to determine upstream SVN information from ",
433 "working tree history\n";
434 }
435
436 my $gs = Git::SVN->find_by_url($url);
437 if (command(qw/diff-index HEAD --/)) {
438 print STDERR "Cannot rebase with uncommited changes:\n";
439 command_noisy('status');
440 exit 1;
441 }
442 $_fetch_all ? $gs->fetch_all : $gs->fetch;
443 command_noisy(rebase_cmd(), $gs->refname);
444}
445
Eric Wong5969cbe2007-01-11 17:58:39 -0800446sub cmd_show_ignore {
Eric Wong1a97a502007-02-20 00:43:19 -0800447 my $url = (::working_head_info('HEAD'))[0];
448 my $gs = Git::SVN->find_by_url($url) || Git::SVN->new;
Eric Wong5969cbe2007-01-11 17:58:39 -0800449 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
450 $gs->traverse_ignore(\*STDOUT, '', $r);
Eric Wonga5e0ced2006-06-12 15:23:48 -0700451}
452
Eric Wong8164b652007-01-11 15:35:55 -0800453sub cmd_multi_init {
Eric Wong9d55b412006-06-12 15:53:13 -0700454 my $url = shift;
Eric Wong98327e52007-01-04 18:02:00 -0800455 unless (defined $_trunk || defined $_branches || defined $_tags) {
456 usage(1);
457 }
Eric Wong8164b652007-01-11 15:35:55 -0800458 $_prefix = '' unless defined $_prefix;
Eric Wongdadc6d22007-02-14 12:27:41 -0800459 if (defined $url) {
460 $url =~ s#/+$##;
461 init_subdir(@_);
462 }
Eric Wongf30603f2007-02-23 01:26:26 -0800463 do_git_init_db();
Eric Wong98327e52007-01-04 18:02:00 -0800464 if (defined $_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800465 my $trunk_ref = $_prefix . 'trunk';
466 # try both old-style and new-style lookups:
467 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
Eric Wong8164b652007-01-11 15:35:55 -0800468 unless ($gs_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800469 my ($trunk_url, $trunk_path) =
470 complete_svn_url($url, $_trunk);
471 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
472 undef, $trunk_ref);
Eric Wong98327e52007-01-04 18:02:00 -0800473 }
Eric Wongc35b96e2006-10-11 11:53:21 -0700474 }
Eric Wong706587f2007-01-18 17:50:01 -0800475 return unless defined $_branches || defined $_tags;
Eric Wonge7db67e2007-01-11 17:09:26 -0800476 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
477 complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix);
478 complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
Eric Wong9d55b412006-06-12 15:53:13 -0700479}
480
Eric Wong1c8443b2007-01-14 02:17:00 -0800481sub cmd_multi_fetch {
Eric Wong0af9c9f2007-01-27 22:28:56 -0800482 my $remotes = Git::SVN::read_all_remotes();
483 foreach my $repo_id (sort keys %$remotes) {
Eric Wongdb03cd22007-02-13 00:38:02 -0800484 if ($remotes->{$repo_id}->{url}) {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800485 Git::SVN::fetch_all($repo_id, $remotes);
486 }
Eric Wong706587f2007-01-18 17:50:01 -0800487 }
Eric Wong9d55b412006-06-12 15:53:13 -0700488}
489
Eric Wong44320b92007-01-13 22:35:53 -0800490# this command is special because it requires no metadata
491sub cmd_commit_diff {
492 my ($ta, $tb, $url) = @_;
493 my $usage = "Usage: $0 commit-diff -r<revision> ".
494 "<tree-ish> <tree-ish> [<URL>]\n";
495 fatal($usage) if (!defined $ta || !defined $tb);
Eric Wongd3a840d2007-01-26 01:32:45 -0800496 my $svn_path;
Eric Wong44320b92007-01-13 22:35:53 -0800497 if (!defined $url) {
498 my $gs = eval { Git::SVN->new };
499 if (!$gs) {
500 fatal("Needed URL or usable git-svn --id in ",
501 "the command-line\n", $usage);
502 }
503 $url = $gs->{url};
Eric Wongd3a840d2007-01-26 01:32:45 -0800504 $svn_path = $gs->{path};
Eric Wong44320b92007-01-13 22:35:53 -0800505 }
506 unless (defined $_revision) {
507 fatal("-r|--revision is a required argument\n", $usage);
508 }
509 if (defined $_message && defined $_file) {
510 fatal("Both --message/-m and --file/-F specified ",
511 "for the commit message.\n",
512 "I have no idea what you mean\n");
513 }
514 if (defined $_file) {
515 $_message = file_to_s($_file);
516 } else {
517 $_message ||= get_commit_entry($tb)->{log};
518 }
519 my $ra ||= Git::SVN::Ra->new($url);
Eric Wongd3a840d2007-01-26 01:32:45 -0800520 $svn_path ||= $ra->{svn_path};
Eric Wong44320b92007-01-13 22:35:53 -0800521 my $r = $_revision;
522 if ($r eq 'HEAD') {
523 $r = $ra->get_latest_revnum;
524 } elsif ($r !~ /^\d+$/) {
525 die "revision argument: $r not understood by git-svn\n";
526 }
Eric Wong61395352007-01-27 14:33:08 -0800527 my %ed_opts = ( r => $r,
528 log => $_message,
529 ra => $ra,
530 tree_a => $ta,
531 tree_b => $tb,
532 editor_cb => sub { print "Committed r$_[0]\n" },
533 svn_path => $svn_path );
534 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong44320b92007-01-13 22:35:53 -0800535 print "No changes\n$ta == $tb\n";
536 }
Eric Wong44320b92007-01-13 22:35:53 -0800537}
538
Eric Wong3397f9d2006-02-16 01:24:16 -0800539########################### utility functions #########################
540
Eric Wong905f8b72007-02-16 03:22:40 -0800541sub rebase_cmd {
542 my @cmd = qw/rebase/;
543 push @cmd, '-v' if $_verbose;
544 push @cmd, qw/--merge/ if $_merge;
545 push @cmd, "--strategy=$_strategy" if $_strategy;
546 @cmd;
547}
548
Eric Wong1e889ef2007-02-16 01:45:13 -0800549sub post_fetch_checkout {
550 return if $_no_checkout;
551 my $gs = $Git::SVN::_head or return;
552 return if verify_ref('refs/heads/master^0');
553
554 my $valid_head = verify_ref('HEAD^0');
555 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
556 return if ($valid_head || !verify_ref('HEAD^0'));
557
558 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
559 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
560 return if -f $index;
561
562 chomp(my $bare = `git config --bool --get core.bare`);
563 return if $bare eq 'true';
564 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
565 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
566 print STDERR "Checked out HEAD:\n ",
567 $gs->full_url, " r", $gs->last_rev, "\n";
568}
569
Eric Wong98327e52007-01-04 18:02:00 -0800570sub complete_svn_url {
571 my ($url, $path) = @_;
572 $path =~ s#/+$##;
Eric Wong98327e52007-01-04 18:02:00 -0800573 if ($path !~ m#^[a-z\+]+://#) {
Eric Wong98327e52007-01-04 18:02:00 -0800574 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
575 fatal("E: '$path' is not a complete URL ",
576 "and a separate URL is not specified\n");
577 }
Eric Wong706587f2007-01-18 17:50:01 -0800578 return ($url, $path);
Eric Wong98327e52007-01-04 18:02:00 -0800579 }
Eric Wong706587f2007-01-18 17:50:01 -0800580 return ($path, '');
Eric Wong98327e52007-01-04 18:02:00 -0800581}
582
Eric Wong9d55b412006-06-12 15:53:13 -0700583sub complete_url_ls_init {
Eric Wong706587f2007-01-18 17:50:01 -0800584 my ($ra, $repo_path, $switch, $pfx) = @_;
585 unless ($repo_path) {
Eric Wong9d55b412006-06-12 15:53:13 -0700586 print STDERR "W: $switch not specified\n";
587 return;
588 }
Eric Wong706587f2007-01-18 17:50:01 -0800589 $repo_path =~ s#/+$##;
590 if ($repo_path =~ m#^[a-z\+]+://#) {
591 $ra = Git::SVN::Ra->new($repo_path);
592 $repo_path = '';
Eric Wonge7db67e2007-01-11 17:09:26 -0800593 } else {
Eric Wong706587f2007-01-18 17:50:01 -0800594 $repo_path =~ s#^/+##;
Eric Wonge7db67e2007-01-11 17:09:26 -0800595 unless ($ra) {
Eric Wong706587f2007-01-18 17:50:01 -0800596 fatal("E: '$repo_path' is not a complete URL ",
Eric Wonge7db67e2007-01-11 17:09:26 -0800597 "and a separate URL is not specified\n");
Eric Wong9d55b412006-06-12 15:53:13 -0700598 }
Eric Wonge7db67e2007-01-11 17:09:26 -0800599 }
Eric Wong706587f2007-01-18 17:50:01 -0800600 my $url = $ra->{url};
Eric Wongb4d57e52007-02-14 15:10:44 -0800601 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
602 my $k = "svn-remote.$gs->{repo_id}.url";
603 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
604 if ($orig_url && ($orig_url ne $gs->{url})) {
605 die "$k already set: $orig_url\n",
606 "wanted to set to: $gs->{url}\n";
Eric Wong88cf4102007-02-01 03:59:07 -0800607 }
Eric Wongb4d57e52007-02-14 15:10:44 -0800608 command_oneline('config', $k, $gs->{url}) unless $orig_url;
609 my $remote_path = "$ra->{svn_path}/$repo_path/*";
610 $remote_path =~ s#/+#/#g;
611 $remote_path =~ s#^/##g;
612 my ($n) = ($switch =~ /^--(\w+)/);
613 if (length $pfx && $pfx !~ m#/$#) {
614 die "--prefix='$pfx' must have a trailing slash '/'\n";
Eric Wong9d55b412006-06-12 15:53:13 -0700615 }
Eric Wongb4d57e52007-02-14 15:10:44 -0800616 command_noisy('config', "svn-remote.$gs->{repo_id}.$n",
617 "$remote_path:refs/remotes/$pfx*");
Eric Wong9d55b412006-06-12 15:53:13 -0700618}
619
Eric Wongaef4e922006-12-15 10:59:54 -0800620sub verify_ref {
621 my ($ref) = @_;
Eric Wong2c5c1d52006-12-28 01:16:21 -0800622 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
623 { STDERR => 0 }); };
Eric Wongaef4e922006-12-15 10:59:54 -0800624}
625
Eric Wonga5e0ced2006-06-12 15:23:48 -0700626sub get_tree_from_treeish {
Eric Wongcf52b8f2006-02-20 10:57:28 -0800627 my ($treeish) = @_;
Eric Wong44320b92007-01-13 22:35:53 -0800628 # $treeish can be a symbolic ref, too:
Eric Wongaef4e922006-12-15 10:59:54 -0800629 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -0800630 my $expected;
631 while ($type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -0800632 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -0800633 }
634 if ($type eq 'commit') {
Eric Wongaef4e922006-12-15 10:59:54 -0800635 $expected = (grep /^tree /, command(qw/cat-file commit/,
636 $treeish))[0];
Eric Wong44320b92007-01-13 22:35:53 -0800637 ($expected) = ($expected =~ /^tree ($sha1)$/o);
Eric Wongcf52b8f2006-02-20 10:57:28 -0800638 die "Unable to get tree from $treeish\n" unless $expected;
639 } elsif ($type eq 'tree') {
640 $expected = $treeish;
641 } else {
642 die "$treeish is a $type, expected tree, tag or commit\n";
643 }
Eric Wonga5e0ced2006-06-12 15:23:48 -0700644 return $expected;
645}
Eric Wongcf52b8f2006-02-20 10:57:28 -0800646
Eric Wong44320b92007-01-13 22:35:53 -0800647sub get_commit_entry {
648 my ($treeish) = shift;
649 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
650 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
651 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
652 open my $log_fh, '>', $commit_editmsg or croak $!;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700653
Eric Wong44320b92007-01-13 22:35:53 -0800654 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wong4ad45152006-07-09 20:20:48 -0700655 if ($type eq 'commit' || $type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -0800656 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
Eric Wong44320b92007-01-13 22:35:53 -0800657 $type, $treeish);
Eric Wong3397f9d2006-02-16 01:24:16 -0800658 my $in_msg = 0;
659 while (<$msg_fh>) {
660 if (!$in_msg) {
661 $in_msg = 1 if (/^\s*$/);
Eric Wongdf746c52006-03-03 01:20:08 -0800662 } elsif (/^git-svn-id: /) {
Eric Wong44320b92007-01-13 22:35:53 -0800663 # skip this for now, we regenerate the
664 # correct one on re-fetch anyways
665 # TODO: set *:merge properties or like...
Eric Wong3397f9d2006-02-16 01:24:16 -0800666 } else {
Eric Wong44320b92007-01-13 22:35:53 -0800667 print $log_fh $_ or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -0800668 }
669 }
Eric Wongaef4e922006-12-15 10:59:54 -0800670 command_close_pipe($msg_fh, $ctx);
Eric Wong3397f9d2006-02-16 01:24:16 -0800671 }
Eric Wong44320b92007-01-13 22:35:53 -0800672 close $log_fh or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -0800673
674 if ($_edit || ($type eq 'tree')) {
675 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
Eric Wong44320b92007-01-13 22:35:53 -0800676 # TODO: strip out spaces, comments, like git-commit.sh
677 system($editor, $commit_editmsg);
Eric Wong3397f9d2006-02-16 01:24:16 -0800678 }
Eric Wong44320b92007-01-13 22:35:53 -0800679 rename $commit_editmsg, $commit_msg or croak $!;
680 open $log_fh, '<', $commit_msg or croak $!;
681 { local $/; chomp($log_entry{log} = <$log_fh>); }
682 close $log_fh or croak $!;
683 unlink $commit_msg;
684 \%log_entry;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700685}
686
Eric Wong3397f9d2006-02-16 01:24:16 -0800687sub s_to_file {
688 my ($str, $file, $mode) = @_;
689 open my $fd,'>',$file or croak $!;
690 print $fd $str,"\n" or croak $!;
691 close $fd or croak $!;
692 chmod ($mode &~ umask, $file) if (defined $mode);
693}
694
695sub file_to_s {
696 my $file = shift;
697 open my $fd,'<',$file or croak "$!: file: $file\n";
698 local $/;
699 my $ret = <$fd>;
700 close $fd or croak $!;
701 $ret =~ s/\s*$//s;
702 return $ret;
703}
704
Eric Wongeeb0abe2006-03-03 01:20:08 -0800705# '<svn username> = real-name <email address>' mapping based on git-svnimport:
706sub load_authors {
707 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800708 my $log = $cmd eq 'log';
Eric Wongeeb0abe2006-03-03 01:20:08 -0800709 while (<$authors>) {
710 chomp;
Shawn Pearce88157882006-09-24 23:04:55 -0400711 next unless /^(\S+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
Eric Wongeeb0abe2006-03-03 01:20:08 -0800712 my ($user, $name, $email) = ($1, $2, $3);
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800713 if ($log) {
714 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
715 } else {
716 $users{$user} = [$name, $email];
717 }
Eric Wong79bb8d82006-06-01 02:35:44 -0700718 }
719 close $authors or croak $!;
720}
721
Tom Princee0d10e12007-01-28 16:16:53 -0800722# convert GetOpt::Long specs for use by git-config
Eric Wongb8c92ca2006-05-24 01:40:37 -0700723sub read_repo_config {
Eric Wong706587f2007-01-18 17:50:01 -0800724 return unless -d $ENV{GIT_DIR};
Eric Wongb8c92ca2006-05-24 01:40:37 -0700725 my $opts = shift;
Eric Wong97ae0912007-02-11 15:21:24 -0800726 my @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -0700727 foreach my $o (keys %$opts) {
Eric Wong97ae0912007-02-11 15:21:24 -0800728 # if we have mixedCase and a long option-only, then
729 # it's a config-only variable that we don't need for
730 # the command-line.
731 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
Eric Wongb8c92ca2006-05-24 01:40:37 -0700732 my $v = $opts->{$o};
Eric Wong97ae0912007-02-11 15:21:24 -0800733 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
Eric Wongb8c92ca2006-05-24 01:40:37 -0700734 $key =~ s/-//g;
Tom Princee0d10e12007-01-28 16:16:53 -0800735 my $arg = 'git-config';
Eric Wongb8c92ca2006-05-24 01:40:37 -0700736 $arg .= ' --int' if ($o =~ /[:=]i$/);
737 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
738 if (ref $v eq 'ARRAY') {
739 chomp(my @tmp = `$arg --get-all svn.$key`);
740 @$v = @tmp if @tmp;
741 } else {
742 chomp(my $tmp = `$arg --get svn.$key`);
Eric Wong77742842007-02-10 21:07:12 -0800743 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
Eric Wongb8c92ca2006-05-24 01:40:37 -0700744 $$v = $tmp;
745 }
746 }
747 }
Eric Wong97ae0912007-02-11 15:21:24 -0800748 delete @$opts{@config_only} if @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -0700749}
750
Eric Wong79bb8d82006-06-01 02:35:44 -0700751sub extract_metadata {
Eric Wongc1927a82006-06-27 19:39:11 -0700752 my $id = shift or return (undef, undef, undef);
Eric Wong79bb8d82006-06-01 02:35:44 -0700753 my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
754 \s([a-f\d\-]+)$/x);
Eric Wonge70dc782006-11-23 14:54:04 -0800755 if (!defined $rev || !$uuid || !$url) {
Eric Wong79bb8d82006-06-01 02:35:44 -0700756 # some of the original repositories I made had
Pavel Roskin82e5a822006-07-10 01:50:18 -0400757 # identifiers like this:
Eric Wong79bb8d82006-06-01 02:35:44 -0700758 ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
759 }
760 return ($url, $rev, $uuid);
761}
762
Eric Wongc1927a82006-06-27 19:39:11 -0700763sub cmt_metadata {
764 return extract_metadata((grep(/^git-svn-id: /,
Eric Wongaef4e922006-12-15 10:59:54 -0800765 command(qw/cat-file commit/, shift)))[-1]);
Eric Wongc1927a82006-06-27 19:39:11 -0700766}
767
Eric Wong905f8b72007-02-16 03:22:40 -0800768sub working_head_info {
769 my ($head, $refs) = @_;
770 my ($url, $rev, $uuid);
771 my ($fh, $ctx) = command_output_pipe('rev-list', $head);
772 while (<$fh>) {
773 chomp;
774 ($url, $rev, $uuid) = cmt_metadata($_);
775 last if (defined $url && defined $rev && defined $uuid);
776 unshift @$refs, $_ if $refs;
777 }
778 close $fh; # break the pipe
779 ($url, $rev, $uuid);
780}
781
Eric Wong9b981fc2007-01-11 12:14:21 -0800782package Git::SVN;
783use strict;
784use warnings;
Eric Wongecc712d2007-01-31 12:28:10 -0800785use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
Eric Wong62e349d2007-02-16 19:57:29 -0800786 $_repack $_repack_flags $_use_svm_props $_head
Eric Wongbefc9ad2007-02-17 02:53:07 -0800787 $_use_svnsync_props $no_reuse_existing/;
Eric Wong9b981fc2007-01-11 12:14:21 -0800788use Carp qw/croak/;
789use File::Path qw/mkpath/;
Eric Wong373274f2007-01-31 13:54:23 -0800790use File::Copy qw/copy/;
Eric Wong9b981fc2007-01-11 12:14:21 -0800791use IPC::Open3;
792
Eric Wongecc712d2007-01-31 12:28:10 -0800793my $_repack_nr;
Eric Wong9b981fc2007-01-11 12:14:21 -0800794# properties that we do not log:
795my %SKIP_PROP;
796BEGIN {
797 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
798 svn:special svn:executable
799 svn:entry:committed-rev
800 svn:entry:last-author
801 svn:entry:uuid
802 svn:entry:committed-date/;
Eric Wong91b03282007-02-11 00:51:33 -0800803
804 # some options are read globally, but can be overridden locally
805 # per [svn-remote "..."] section. Command-line options will *NOT*
806 # override options set in an [svn-remote "..."] section
807 my $e;
Eric Wong62e349d2007-02-16 19:57:29 -0800808 foreach (qw/follow_parent no_metadata use_svm_props
809 use_svnsync_props/) {
Eric Wong91b03282007-02-11 00:51:33 -0800810 my $key = $_;
811 $key =~ tr/_//d;
812 $e .= "sub $_ {
813 my (\$self) = \@_;
814 return \$self->{-$_} if exists \$self->{-$_};
815 my \$k = \"svn-remote.\$self->{repo_id}\.$key\";
816 eval { command_oneline(qw/config --get/, \$k) };
817 if (\$@) {
818 \$self->{-$_} = \$Git::SVN::_$_;
819 } else {
820 my \$v = command_oneline(qw/config --bool/,\$k);
821 \$self->{-$_} = \$v eq 'false' ? 0 : 1;
822 }
823 return \$self->{-$_} }\n";
824 }
825 $e .= "1;\n";
826 eval $e or die $@;
Eric Wong9b981fc2007-01-11 12:14:21 -0800827}
828
Eric Wong373274f2007-01-31 13:54:23 -0800829my %LOCKFILES;
830END { unlink keys %LOCKFILES if %LOCKFILES }
831
Eric Wong4bb9ed02007-02-03 13:29:17 -0800832sub resolve_local_globs {
833 my ($url, $fetch, $glob_spec) = @_;
834 return unless defined $glob_spec;
835 my $ref = $glob_spec->{ref};
836 my $path = $glob_spec->{path};
837 foreach (command(qw#for-each-ref --format=%(refname) refs/remotes#)) {
838 next unless m#^refs/remotes/$ref->{regex}$#;
839 my $p = $1;
840 my $pathname = $path->full_path($p);
841 my $refname = $ref->full_path($p);
842 if (my $existing = $fetch->{$pathname}) {
843 if ($existing ne $refname) {
844 die "Refspec conflict:\n",
845 "existing: refs/remotes/$existing\n",
846 " globbed: refs/remotes/$refname\n";
847 }
848 my $u = (::cmt_metadata("refs/remotes/$refname"))[0];
Eric Wong4e9f6cc2007-02-09 12:17:57 -0800849 $u =~ s!^\Q$url\E(/|$)!! or die
Eric Wong4bb9ed02007-02-03 13:29:17 -0800850 "refs/remotes/$refname: '$url' not found in '$u'\n";
851 if ($pathname ne $u) {
852 warn "W: Refspec glob conflict ",
853 "(ref: refs/remotes/$refname):\n",
854 "expected path: $pathname\n",
855 " real path: $u\n",
856 "Continuing ahead with $u\n";
857 next;
858 }
859 } else {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800860 $fetch->{$pathname} = $refname;
861 }
862 }
863}
864
Eric Wonge98671e2007-02-14 02:21:19 -0800865sub parse_revision_argument {
866 my ($base, $head) = @_;
867 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
868 return ($base, $head);
869 }
870 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
871 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
872 return ($head, $head) if ($::_revision eq 'HEAD');
873 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
874 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
875 die "revision argument: $::_revision not understood by git-svn\n";
876}
877
Eric Wong0af9c9f2007-01-27 22:28:56 -0800878sub fetch_all {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800879 my ($repo_id, $remotes) = @_;
Eric Wong905f8b72007-02-16 03:22:40 -0800880 if (ref $repo_id) {
881 my $gs = $repo_id;
882 $repo_id = undef;
883 $repo_id = $gs->{repo_id};
884 }
885 $remotes ||= read_all_remotes();
Eric Wong7447b4b2007-02-14 18:38:46 -0800886 my $remote = $remotes->{$repo_id} or
887 die "[svn-remote \"$repo_id\"] unknown\n";
Eric Wonge5181922007-02-08 12:53:57 -0800888 my $fetch = $remote->{fetch};
Eric Wong7447b4b2007-02-14 18:38:46 -0800889 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
Eric Wonge5181922007-02-08 12:53:57 -0800890 my (@gs, @globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -0800891 my $ra = Git::SVN::Ra->new($url);
Eric Wong26a62d52007-02-12 13:25:25 -0800892 my $uuid = $ra->get_uuid;
Eric Wong0af9c9f2007-01-27 22:28:56 -0800893 my $head = $ra->get_latest_revnum;
Eric Wong28710f72007-02-14 13:32:21 -0800894 my $base = defined $fetch ? $head : 0;
Eric Wonge5181922007-02-08 12:53:57 -0800895
896 # read the max revs for wildcard expansion (branches/*, tags/*)
897 foreach my $t (qw/branches tags/) {
898 defined $remote->{$t} or next;
899 push @globs, $remote->{$t};
Eric Wong93f26892007-02-11 01:20:26 -0800900 my $max_rev = eval { tmp_config(qw/--int --get/,
901 "svn-remote.$repo_id.${t}-maxRev") };
902 if (defined $max_rev && ($max_rev < $base)) {
903 $base = $max_rev;
Eric Wongd6d33462007-02-16 04:05:33 -0800904 } elsif (!defined $max_rev) {
905 $base = 0;
Eric Wonge5181922007-02-08 12:53:57 -0800906 }
907 }
908
Eric Wongdb03cd22007-02-13 00:38:02 -0800909 if ($fetch) {
910 foreach my $p (sort keys %$fetch) {
911 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
912 my $lr = $gs->rev_db_max;
913 if (defined $lr) {
914 $base = $lr if ($lr < $base);
915 }
916 push @gs, $gs;
Eric Wong0af9c9f2007-01-27 22:28:56 -0800917 }
Eric Wong0af9c9f2007-01-27 22:28:56 -0800918 }
Eric Wonge98671e2007-02-14 02:21:19 -0800919
920 ($base, $head) = parse_revision_argument($base, $head);
Eric Wonge5181922007-02-08 12:53:57 -0800921 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -0800922}
923
Eric Wong47e39c52007-01-21 04:27:09 -0800924sub read_all_remotes {
925 my $r = {};
Eric Wong8b8fc062007-01-22 11:44:57 -0800926 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
Eric Wong47e39c52007-01-21 04:27:09 -0800927 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*refs/remotes/(.+)\s*$!) {
928 $r->{$1}->{fetch}->{$2} = $3;
929 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
930 $r->{$1}->{url} = $2;
Eric Wong4bb9ed02007-02-03 13:29:17 -0800931 } elsif (m!^(.+)\.(branches|tags)=
932 (.*):refs/remotes/(.+)\s*$/!x) {
933 my ($p, $g) = ($3, $4);
934 my $rs = $r->{$1}->{$2} = {
Eric Wonge5181922007-02-08 12:53:57 -0800935 t => $2,
Eric Wong93f26892007-02-11 01:20:26 -0800936 remote => $1,
Eric Wong4bb9ed02007-02-03 13:29:17 -0800937 path => Git::SVN::GlobSpec->new($p),
938 ref => Git::SVN::GlobSpec->new($g) };
939 if (length($rs->{ref}->{right}) != 0) {
940 die "The '*' glob character must be the last ",
941 "character of '$g'\n";
942 }
Eric Wong47e39c52007-01-21 04:27:09 -0800943 }
944 }
945 $r;
946}
947
Eric Wongecc712d2007-01-31 12:28:10 -0800948sub init_vars {
949 if (defined $_repack) {
950 $_repack = 1000 if ($_repack <= 0);
951 $_repack_nr = $_repack;
952 $_repack_flags ||= '-d';
953 }
954}
955
Eric Wongb805b442007-01-22 13:52:04 -0800956sub verify_remotes_sanity {
Eric Wong536c4b02007-01-23 11:35:53 -0800957 return unless -d $ENV{GIT_DIR};
Eric Wongb805b442007-01-22 13:52:04 -0800958 my %seen;
959 foreach (command(qw/config -l/)) {
960 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
961 if ($seen{$1}) {
962 die "Remote ref refs/remote/$1 is tracked by",
963 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
964 "Please resolve this ambiguity in ",
965 "your git configuration file before ",
966 "continuing\n";
967 }
968 $seen{$1} = $_;
969 }
970 }
971}
972
Eric Wong47e39c52007-01-21 04:27:09 -0800973# we allow more chars than remotes2config.sh...
Eric Wong706587f2007-01-18 17:50:01 -0800974sub sanitize_remote_name {
975 my ($name) = @_;
Eric Wong47e39c52007-01-21 04:27:09 -0800976 $name =~ tr{A-Za-z0-9:,/+-}{.}c;
Eric Wong706587f2007-01-18 17:50:01 -0800977 $name;
978}
979
Eric Wonge6434f82007-01-23 16:29:23 -0800980sub find_existing_remote {
981 my ($url, $remotes) = @_;
Eric Wongbefc9ad2007-02-17 02:53:07 -0800982 return undef if $no_reuse_existing;
Eric Wonge6434f82007-01-23 16:29:23 -0800983 my $existing;
984 foreach my $repo_id (keys %$remotes) {
985 my $u = $remotes->{$repo_id}->{url} or next;
986 next if $u ne $url;
987 $existing = $repo_id;
988 last;
989 }
990 $existing;
991}
992
993sub init_remote_config {
Eric Wongd8115c52007-02-01 03:30:31 -0800994 my ($self, $url, $no_write) = @_;
Eric Wonge6434f82007-01-23 16:29:23 -0800995 $url =~ s!/+$!!; # strip trailing slash
996 my $r = read_all_remotes();
997 my $existing = find_existing_remote($url, $r);
998 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -0800999 unless ($no_write) {
1000 print STDERR "Using existing ",
1001 "[svn-remote \"$existing\"]\n";
1002 }
Eric Wonge6434f82007-01-23 16:29:23 -08001003 $self->{repo_id} = $existing;
1004 } else {
1005 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
1006 $existing = find_existing_remote($min_url, $r);
1007 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001008 unless ($no_write) {
1009 print STDERR "Using existing ",
1010 "[svn-remote \"$existing\"]\n";
1011 }
Eric Wonge6434f82007-01-23 16:29:23 -08001012 $self->{repo_id} = $existing;
1013 }
1014 if ($min_url ne $url) {
Eric Wonge5181922007-02-08 12:53:57 -08001015 unless ($no_write) {
1016 print STDERR "Using higher level of URL: ",
1017 "$url => $min_url\n";
1018 }
Eric Wonge6434f82007-01-23 16:29:23 -08001019 my $old_path = $self->{path};
1020 $self->{path} = $url;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001021 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
Eric Wonge6434f82007-01-23 16:29:23 -08001022 if (length $old_path) {
1023 $self->{path} .= "/$old_path";
1024 }
1025 $url = $min_url;
1026 }
1027 }
1028 my $orig_url;
1029 if (!$existing) {
1030 # verify that we aren't overwriting anything:
1031 $orig_url = eval {
1032 command_oneline('config', '--get',
1033 "svn-remote.$self->{repo_id}.url")
1034 };
1035 if ($orig_url && ($orig_url ne $url)) {
1036 die "svn-remote.$self->{repo_id}.url already set: ",
1037 "$orig_url\nwanted to set to: $url\n";
1038 }
1039 }
1040 my ($xrepo_id, $xpath) = find_ref($self->refname);
1041 if (defined $xpath) {
1042 die "svn-remote.$xrepo_id.fetch already set to track ",
1043 "$xpath:refs/remotes/", $self->refname, "\n";
1044 }
Eric Wongd8115c52007-02-01 03:30:31 -08001045 unless ($no_write) {
1046 command_noisy('config',
1047 "svn-remote.$self->{repo_id}.url", $url);
1048 command_noisy('config', '--add',
1049 "svn-remote.$self->{repo_id}.fetch",
1050 "$self->{path}:".$self->refname);
1051 }
Eric Wonge6434f82007-01-23 16:29:23 -08001052 $self->{url} = $url;
1053}
1054
Eric Wonga8ae2622007-02-13 14:22:11 -08001055sub find_by_url { # repos_root and, path are optional
1056 my ($class, $full_url, $repos_root, $path) = @_;
Eric Wong1a97a502007-02-20 00:43:19 -08001057 return undef unless defined $full_url;
Eric Wonga8ae2622007-02-13 14:22:11 -08001058 my $remotes = read_all_remotes();
1059 if (defined $full_url && defined $repos_root && !defined $path) {
1060 $path = $full_url;
1061 $path =~ s#^\Q$repos_root\E(?:/|$)##;
1062 }
1063 foreach my $repo_id (keys %$remotes) {
1064 my $u = $remotes->{$repo_id}->{url} or next;
1065 next if defined $repos_root && $repos_root ne $u;
1066
1067 my $fetch = $remotes->{$repo_id}->{fetch} || {};
1068 foreach (qw/branches tags/) {
1069 resolve_local_globs($u, $fetch,
1070 $remotes->{$repo_id}->{$_});
1071 }
1072 my $p = $path;
1073 unless (defined $p) {
1074 $p = $full_url;
1075 $p =~ s#^\Q$u\E(?:/|$)## or next;
1076 }
1077 foreach my $f (keys %$fetch) {
1078 next if $f ne $p;
1079 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
1080 }
1081 }
1082 undef;
1083}
1084
Eric Wong9b981fc2007-01-11 12:14:21 -08001085sub init {
Eric Wongd8115c52007-02-01 03:30:31 -08001086 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08001087 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong9b981fc2007-01-11 12:14:21 -08001088 if (defined $url) {
Eric Wongd8115c52007-02-01 03:30:31 -08001089 $self->init_remote_config($url, $no_write);
Eric Wong9b981fc2007-01-11 12:14:21 -08001090 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001091 $self;
1092}
1093
Eric Wong706587f2007-01-18 17:50:01 -08001094sub find_ref {
1095 my ($ref_id) = @_;
1096 foreach (command(qw/config -l/)) {
1097 next unless m!^svn-remote\.(.+)\.fetch=
1098 \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x;
1099 my ($repo_id, $path, $ref) = ($1, $2, $3);
1100 if ($ref eq $ref_id) {
1101 $path = '' if ($path =~ m#^\./?#);
1102 return ($repo_id, $path);
1103 }
1104 }
1105 (undef, undef, undef);
1106}
1107
Eric Wong9b981fc2007-01-11 12:14:21 -08001108sub new {
Eric Wong706587f2007-01-18 17:50:01 -08001109 my ($class, $ref_id, $repo_id, $path) = @_;
1110 if (defined $ref_id && !defined $repo_id && !defined $path) {
1111 ($repo_id, $path) = find_ref($ref_id);
1112 if (!defined $repo_id) {
1113 die "Could not find a \"svn-remote.*.fetch\" key ",
1114 "in the repository configuration matching: ",
1115 "refs/remotes/$ref_id\n";
1116 }
1117 }
1118 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong8b8fc062007-01-22 11:44:57 -08001119 if (!defined $self->{path} || !length $self->{path}) {
1120 my $fetch = command_oneline('config', '--get',
1121 "svn-remote.$repo_id.fetch",
1122 ":refs/remotes/$ref_id\$") or
1123 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1124 "\":refs/remotes/$ref_id\$\" in config\n";
1125 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
1126 }
Eric Wong706587f2007-01-18 17:50:01 -08001127 $self->{url} = command_oneline('config', '--get',
1128 "svn-remote.$repo_id.url") or
1129 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
Eric Wongd6d33462007-02-16 04:05:33 -08001130 $self->rebuild;
Eric Wong9b981fc2007-01-11 12:14:21 -08001131 $self;
1132}
1133
Eric Wong706587f2007-01-18 17:50:01 -08001134sub refname { "refs/remotes/$_[0]->{ref_id}" }
Eric Wong9b981fc2007-01-11 12:14:21 -08001135
Eric Wong26a62d52007-02-12 13:25:25 -08001136sub svm_uuid {
1137 my ($self) = @_;
1138 return $self->{svm}->{uuid} if $self->svm;
1139 $self->ra;
1140 unless ($self->{svm}) {
1141 die "SVM UUID not cached, and reading remotely failed\n";
1142 }
1143 $self->{svm}->{uuid};
1144}
Eric Wong8a49ee92007-02-10 20:46:50 -08001145
Eric Wong26a62d52007-02-12 13:25:25 -08001146sub svm {
1147 my ($self) = @_;
1148 return $self->{svm} if $self->{svm};
1149 my $svm;
Eric Wong8a49ee92007-02-10 20:46:50 -08001150 # see if we have it in our config, first:
1151 eval {
Eric Wong26a62d52007-02-12 13:25:25 -08001152 my $section = "svn-remote.$self->{repo_id}";
1153 $svm = {
Eric Wong93f26892007-02-11 01:20:26 -08001154 source => tmp_config('--get', "$section.svm-source"),
1155 uuid => tmp_config('--get', "$section.svm-uuid"),
Eric Wongbefc9ad2007-02-17 02:53:07 -08001156 replace => tmp_config('--get', "$section.svm-replace"),
Eric Wong8a49ee92007-02-10 20:46:50 -08001157 }
1158 };
Eric Wongbefc9ad2007-02-17 02:53:07 -08001159 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
1160 $self->{svm} = $svm;
1161 }
Eric Wong26a62d52007-02-12 13:25:25 -08001162 $self->{svm};
1163}
1164
1165sub _set_svm_vars {
1166 my ($self, $ra) = @_;
Eric Wongdb03cd22007-02-13 00:38:02 -08001167 return $ra if $self->svm;
Eric Wong26a62d52007-02-12 13:25:25 -08001168
Eric Wongdb03cd22007-02-13 00:38:02 -08001169 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08001170 "(svm:source, svm:uuid) ",
Eric Wongdb03cd22007-02-13 00:38:02 -08001171 "from the following URLs:\n" );
1172 sub read_svm_props {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001173 my ($self, $ra, $path, $r) = @_;
1174 my $props = ($ra->get_dir($path, $r))[2];
Eric Wongdb03cd22007-02-13 00:38:02 -08001175 my $src = $props->{'svm:source'};
Eric Wong8a49ee92007-02-10 20:46:50 -08001176 my $uuid = $props->{'svm:uuid'};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001177 return undef if (!$src || !$uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08001178
Eric Wongbefc9ad2007-02-17 02:53:07 -08001179 chomp($src, $uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08001180
Eric Wong8a49ee92007-02-10 20:46:50 -08001181 $uuid =~ m{^[0-9a-f\-]{30,}$}
1182 or die "doesn't look right - svm:uuid is '$uuid'\n";
Eric Wongbefc9ad2007-02-17 02:53:07 -08001183
1184 # the '!' is used to mark the repos_root!/relative/path
1185 $src =~ s{/?!/?}{/};
Eric Wongdb03cd22007-02-13 00:38:02 -08001186 $src =~ s{/+$}{}; # no trailing slashes please
Eric Wongbefc9ad2007-02-17 02:53:07 -08001187 # username is of no interest
Eric Wongdb03cd22007-02-13 00:38:02 -08001188 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
Eric Wong8a49ee92007-02-10 20:46:50 -08001189
Eric Wongbefc9ad2007-02-17 02:53:07 -08001190 my $replace = $ra->{url};
1191 $replace .= "/$path" if length $path;
1192
Eric Wongdb03cd22007-02-13 00:38:02 -08001193 my $section = "svn-remote.$self->{repo_id}";
Eric Wongbefc9ad2007-02-17 02:53:07 -08001194 tmp_config("$section.svm-source", $src);
1195 tmp_config("$section.svm-replace", $replace);
1196 tmp_config("$section.svm-uuid", $uuid);
1197 $self->{svm} = {
1198 source => $src,
1199 uuid => $uuid,
1200 replace => $replace
1201 };
Eric Wong8a49ee92007-02-10 20:46:50 -08001202 }
Eric Wongdb03cd22007-02-13 00:38:02 -08001203
1204 my $r = $ra->get_latest_revnum;
1205 my $path = $self->{path};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001206 my %tried;
Eric Wongdb03cd22007-02-13 00:38:02 -08001207 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001208 unless ($tried{"$self->{url}/$path"}) {
1209 return $ra if $self->read_svm_props($ra, $path, $r);
1210 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001211 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001212 $path =~ s#/?[^/]+$##;
Eric Wong8a49ee92007-02-10 20:46:50 -08001213 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001214 die "Path: '$path' should be ''\n" if $path ne '';
1215 return $ra if $self->read_svm_props($ra, $path, $r);
1216 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001217
1218 if ($ra->{repos_root} eq $self->{url}) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001219 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001220 }
1221
1222 # nope, make sure we're connected to the repository root:
1223 my $ok;
1224 my @tried_b;
1225 $path = $ra->{svn_path};
Eric Wongdb03cd22007-02-13 00:38:02 -08001226 $ra = Git::SVN::Ra->new($ra->{repos_root});
1227 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001228 unless ($tried{"$ra->{url}/$path"}) {
1229 $ok = $self->read_svm_props($ra, $path, $r);
1230 last if $ok;
1231 $tried{"$ra->{url}/$path"} = 1;
1232 }
1233 $path =~ s#/?[^/]+$##;
Eric Wongdb03cd22007-02-13 00:38:02 -08001234 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001235 die "Path: '$path' should be ''\n" if $path ne '';
1236 $ok ||= $self->read_svm_props($ra, $path, $r);
1237 $tried{"$ra->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001238 if (!$ok) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001239 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001240 }
1241 Git::SVN::Ra->new($self->{url});
Eric Wong8a49ee92007-02-10 20:46:50 -08001242}
1243
Eric Wong62e349d2007-02-16 19:57:29 -08001244sub svnsync {
1245 my ($self) = @_;
1246 return $self->{svnsync} if $self->{svnsync};
1247
1248 if ($self->no_metadata) {
1249 die "Can't have both 'noMetadata' and ",
1250 "'useSvnsyncProps' options set!\n";
1251 }
1252 if ($self->rewrite_root) {
1253 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1254 "options set!\n";
1255 }
1256
1257 my $svnsync;
1258 # see if we have it in our config, first:
1259 eval {
1260 my $section = "svn-remote.$self->{repo_id}";
1261 $svnsync = {
1262 url => tmp_config('--get', "$section.svnsync-url"),
1263 uuid => tmp_config('--get', "$section.svnsync-uuid"),
1264 }
1265 };
1266 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
1267 return $self->{svnsync} = $svnsync;
1268 }
1269
1270 my $err = "useSvnsyncProps set, but failed to read " .
1271 "svnsync property: svn:sync-from-";
1272 my $rp = $self->ra->rev_proplist(0);
1273
1274 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
1275 $url =~ m{^[a-z\+]+://} or
1276 die "doesn't look right - svn:sync-from-url is '$url'\n";
1277
1278 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
1279 $uuid =~ m{^[0-9a-f\-]{30,}$} or
1280 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1281
1282 my $section = "svn-remote.$self->{repo_id}";
1283 tmp_config('--add', "$section.svnsync-uuid", $uuid);
1284 tmp_config('--add', "$section.svnsync-url", $url);
1285 return $self->{svnsync} = { url => $url, uuid => $uuid };
1286}
1287
Eric Wong26a62d52007-02-12 13:25:25 -08001288# this allows us to memoize our SVN::Ra UUID locally and avoid a
1289# remote lookup (useful for 'git svn log').
1290sub ra_uuid {
1291 my ($self) = @_;
1292 unless ($self->{ra_uuid}) {
1293 my $key = "svn-remote.$self->{repo_id}.uuid";
1294 my $uuid = eval { tmp_config('--get', $key) };
1295 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
1296 $self->{ra_uuid} = $uuid;
1297 } else {
1298 die "ra_uuid called without URL\n" unless $self->{url};
1299 $self->{ra_uuid} = $self->ra->get_uuid;
1300 tmp_config('--add', $key, $self->{ra_uuid});
1301 }
1302 }
1303 $self->{ra_uuid};
1304}
1305
Eric Wong9b981fc2007-01-11 12:14:21 -08001306sub ra {
1307 my ($self) = shift;
Eric Wong8a49ee92007-02-10 20:46:50 -08001308 my $ra = Git::SVN::Ra->new($self->{url});
Eric Wong91b03282007-02-11 00:51:33 -08001309 if ($self->use_svm_props && !$self->{svm}) {
1310 if ($self->no_metadata) {
Eric Wong97ae0912007-02-11 15:21:24 -08001311 die "Can't have both 'noMetadata' and ",
1312 "'useSvmProps' options set!\n";
Eric Wong62e349d2007-02-16 19:57:29 -08001313 } elsif ($self->use_svnsync_props) {
1314 die "Can't have both 'useSvnsyncProps' and ",
1315 "'useSvmProps' options set!\n";
Eric Wong91b03282007-02-11 00:51:33 -08001316 }
Eric Wong26a62d52007-02-12 13:25:25 -08001317 $ra = $self->_set_svm_vars($ra);
Eric Wong8a49ee92007-02-10 20:46:50 -08001318 $self->{-want_revprops} = 1;
1319 }
1320 $ra;
Eric Wong9b981fc2007-01-11 12:14:21 -08001321}
1322
Eric Wong15710b62007-01-22 02:20:33 -08001323sub rel_path {
1324 my ($self) = @_;
1325 my $repos_root = $self->ra->{repos_root};
1326 return $self->{path} if ($self->{url} eq $repos_root);
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001327 die "BUG: rel_path failed! repos_root: $repos_root, Ra URL: ",
1328 $self->ra->{url}, " path: $self->{path}, URL: $self->{url}\n";
Eric Wong15710b62007-01-22 02:20:33 -08001329}
1330
Eric Wong9b981fc2007-01-11 12:14:21 -08001331sub traverse_ignore {
1332 my ($self, $fh, $path, $r) = @_;
1333 $path =~ s#^/+##g;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08001334 my $ra = $self->ra;
1335 my ($dirent, undef, $props) = $ra->get_dir($path, $r);
Eric Wong9b981fc2007-01-11 12:14:21 -08001336 my $p = $path;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08001337 $p =~ s#^\Q$ra->{svn_path}\E/##;
Eric Wong9b981fc2007-01-11 12:14:21 -08001338 print $fh length $p ? "\n# $p\n" : "\n# /\n";
1339 if (my $s = $props->{'svn:ignore'}) {
1340 $s =~ s/[\r\n]+/\n/g;
1341 chomp $s;
1342 if (length $p == 0) {
1343 $s =~ s#\n#\n/$p#g;
1344 print $fh "/$s\n";
1345 } else {
1346 $s =~ s#\n#\n/$p/#g;
1347 print $fh "/$p/$s\n";
1348 }
1349 }
1350 foreach (sort keys %$dirent) {
1351 next if $dirent->{$_}->kind != $SVN::Node::dir;
1352 $self->traverse_ignore($fh, "$path/$_", $r);
1353 }
1354}
1355
Eric Wong3ebe8df2007-01-25 17:35:40 -08001356sub last_rev { ($_[0]->last_rev_commit)[0] }
1357sub last_commit { ($_[0]->last_rev_commit)[1] }
1358
Eric Wong9b981fc2007-01-11 12:14:21 -08001359# returns the newest SVN revision number and newest commit SHA1
1360sub last_rev_commit {
1361 my ($self) = @_;
1362 if (defined $self->{last_rev} && defined $self->{last_commit}) {
1363 return ($self->{last_rev}, $self->{last_commit});
1364 }
Eric Wongd2866f92007-01-11 12:26:16 -08001365 my $c = ::verify_ref($self->refname.'^0');
Eric Wong91b03282007-02-11 00:51:33 -08001366 if ($c && !$self->use_svm_props && !$self->no_metadata) {
Eric Wongd2866f92007-01-11 12:26:16 -08001367 my $rev = (::cmt_metadata($c))[1];
Eric Wong9b981fc2007-01-11 12:14:21 -08001368 if (defined $rev) {
1369 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
1370 return ($rev, $c);
1371 }
1372 }
Eric Wong26a62d52007-02-12 13:25:25 -08001373 my $db_path = $self->db_path;
1374 unless (-e $db_path) {
1375 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
1376 return (undef, undef);
1377 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001378 my $offset = -41; # from tail
1379 my $rl;
Eric Wong26a62d52007-02-12 13:25:25 -08001380 open my $fh, '<', $db_path or croak "$db_path not readable: $!\n";
Eric Wongce4b4af2007-01-31 17:57:36 -08001381 sysseek($fh, $offset, 2); # don't care for errors
1382 sysread($fh, $rl, 41) == 41 or return (undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08001383 chomp $rl;
Eric Wongce4b4af2007-01-31 17:57:36 -08001384 while (('0' x40) eq $rl && sysseek($fh, 0, 1) != 0) {
Eric Wong9b981fc2007-01-11 12:14:21 -08001385 $offset -= 41;
Eric Wongce4b4af2007-01-31 17:57:36 -08001386 sysseek($fh, $offset, 2); # don't care for errors
1387 sysread($fh, $rl, 41) == 41 or return (undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08001388 chomp $rl;
1389 }
Eric Wong91b03282007-02-11 00:51:33 -08001390 if ($c && $c ne $rl) {
Eric Wong26a62d52007-02-12 13:25:25 -08001391 die "$db_path and ", $self->refname,
Eric Wong9c93fee2007-01-31 17:22:31 -08001392 " inconsistent!:\n$c != $rl\n";
1393 }
Eric Wongce4b4af2007-01-31 17:57:36 -08001394 my $rev = sysseek($fh, 0, 1) or croak $!;
Eric Wong9b981fc2007-01-11 12:14:21 -08001395 $rev = ($rev - 41) / 41;
1396 close $fh or croak $!;
1397 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
1398 return ($rev, $c);
1399}
1400
Eric Wong3ebe8df2007-01-25 17:35:40 -08001401sub get_fetch_range {
1402 my ($self, $min, $max) = @_;
1403 $max ||= $self->ra->get_latest_revnum;
Eric Wong9c93fee2007-01-31 17:22:31 -08001404 $min ||= $self->rev_db_max;
Eric Wong3ebe8df2007-01-25 17:35:40 -08001405 (++$min, $max);
Eric Wong9b981fc2007-01-11 12:14:21 -08001406}
1407
Eric Wong8a49ee92007-02-10 20:46:50 -08001408sub tmp_config {
Eric Wong93f26892007-02-11 01:20:26 -08001409 my (@args) = @_;
Eric Wongb7e53482007-02-16 04:09:28 -08001410 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
1411 my $config = "$ENV{GIT_DIR}/svn/.metadata";
1412 if (-e $old_def_config && ! -e $config) {
1413 rename $old_def_config, $config or
1414 die "Failed rename $old_def_config => $config: $!\n";
1415 }
Eric Wong8a49ee92007-02-10 20:46:50 -08001416 my $old_config = $ENV{GIT_CONFIG};
Eric Wong93f26892007-02-11 01:20:26 -08001417 $ENV{GIT_CONFIG} = $config;
Eric Wong8a49ee92007-02-10 20:46:50 -08001418 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08001419 my @ret = eval {
1420 unless (-f $config) {
1421 mkfile($config);
1422 open my $fh, '>', $config or
1423 die "Can't open $config: $!\n";
1424 print $fh "; This file is used internally by ",
1425 "git-svn\n" or die
1426 "Couldn't write to $config: $!\n";
1427 print $fh "; You should not have to edit it\n" or
1428 die "Couldn't write to $config: $!\n";
1429 close $fh or die "Couldn't close $config: $!\n";
1430 }
1431 command('config', @args);
1432 };
Eric Wong8a49ee92007-02-10 20:46:50 -08001433 my $err = $@;
1434 if (defined $old_config) {
1435 $ENV{GIT_CONFIG} = $old_config;
1436 } else {
1437 delete $ENV{GIT_CONFIG};
1438 }
1439 die $err if $err;
1440 wantarray ? @ret : $ret[0];
1441}
1442
Eric Wong9b981fc2007-01-11 12:14:21 -08001443sub tmp_index_do {
1444 my ($self, $sub) = @_;
1445 my $old_index = $ENV{GIT_INDEX_FILE};
1446 $ENV{GIT_INDEX_FILE} = $self->{index};
Eric Wong8a49ee92007-02-10 20:46:50 -08001447 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08001448 my @ret = eval {
1449 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
1450 mkpath([$dir]) unless -d $dir;
1451 &$sub;
1452 };
Eric Wong8a49ee92007-02-10 20:46:50 -08001453 my $err = $@;
1454 if (defined $old_index) {
Eric Wong9b981fc2007-01-11 12:14:21 -08001455 $ENV{GIT_INDEX_FILE} = $old_index;
1456 } else {
1457 delete $ENV{GIT_INDEX_FILE};
1458 }
Eric Wong8a49ee92007-02-10 20:46:50 -08001459 die $err if $err;
Eric Wong9b981fc2007-01-11 12:14:21 -08001460 wantarray ? @ret : $ret[0];
1461}
1462
1463sub assert_index_clean {
1464 my ($self, $treeish) = @_;
1465
1466 $self->tmp_index_do(sub {
1467 command_noisy('read-tree', $treeish) unless -e $self->{index};
1468 my $x = command_oneline('write-tree');
1469 my ($y) = (command(qw/cat-file commit/, $treeish) =~
1470 /^tree ($::sha1)/mo);
Eric Wonge8d120b2007-02-14 16:29:52 -08001471 return if $y eq $x;
1472
1473 warn "Index mismatch: $y != $x\nrereading $treeish\n";
1474 unlink $self->{index} or die "unlink $self->{index}: $!\n";
1475 command_noisy('read-tree', $treeish);
Eric Wong9b981fc2007-01-11 12:14:21 -08001476 $x = command_oneline('write-tree');
1477 if ($y ne $x) {
1478 ::fatal "trees ($treeish) $y != $x\n",
1479 "Something is seriously wrong...\n";
1480 }
1481 });
1482}
1483
1484sub get_commit_parents {
Eric Wong0af9c9f2007-01-27 22:28:56 -08001485 my ($self, $log_entry) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08001486 my (%seen, @ret, @tmp);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001487 # legacy support for 'set-tree'; this is only used by set_tree_cb:
1488 if (my $ip = $self->{inject_parents}) {
1489 if (my $commit = delete $ip->{$log_entry->{revision}}) {
1490 push @tmp, $commit;
Eric Wong9b981fc2007-01-11 12:14:21 -08001491 }
1492 }
Eric Wongd2866f92007-01-11 12:26:16 -08001493 if (my $cur = ::verify_ref($self->refname.'^0')) {
Eric Wong9b981fc2007-01-11 12:14:21 -08001494 push @tmp, $cur;
1495 }
Eric Wong44320b92007-01-13 22:35:53 -08001496 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
Eric Wong9b981fc2007-01-11 12:14:21 -08001497 while (my $p = shift @tmp) {
1498 next if $seen{$p};
1499 $seen{$p} = 1;
1500 push @ret, $p;
1501 # MAXPARENT is defined to 16 in commit-tree.c:
1502 last if @ret >= 16;
1503 }
1504 if (@tmp) {
Eric Wong44320b92007-01-13 22:35:53 -08001505 die "r$log_entry->{revision}: No room for parents:\n\t",
Eric Wong9b981fc2007-01-11 12:14:21 -08001506 join("\n\t", @tmp), "\n";
1507 }
1508 @ret;
1509}
1510
Eric Wongaea736c2007-02-16 19:15:21 -08001511sub rewrite_root {
1512 my ($self) = @_;
1513 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
1514 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
1515 my $rwr = eval { command_oneline(qw/config --get/, $k) };
1516 if ($rwr) {
1517 $rwr =~ s#/+$##;
1518 if ($rwr !~ m#^[a-z\+]+://#) {
1519 die "$rwr is not a valid URL (key: $k)\n";
1520 }
1521 }
1522 $self->{-rewrite_root} = $rwr;
1523}
1524
1525sub metadata_url {
1526 my ($self) = @_;
1527 ($self->rewrite_root || $self->{url}) .
1528 (length $self->{path} ? '/' . $self->{path} : '');
1529}
1530
Eric Wong706587f2007-01-18 17:50:01 -08001531sub full_url {
Eric Wong9b981fc2007-01-11 12:14:21 -08001532 my ($self) = @_;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08001533 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
Eric Wong9b981fc2007-01-11 12:14:21 -08001534}
1535
1536sub do_git_commit {
Eric Wong0af9c9f2007-01-27 22:28:56 -08001537 my ($self, $log_entry) = @_;
Eric Wong8a603772007-01-31 02:45:50 -08001538 my $lr = $self->last_rev;
1539 if (defined $lr && $lr >= $log_entry->{revision}) {
1540 die "Last fetched revision of ", $self->refname,
1541 " was r$lr, but we are about to fetch: ",
1542 "r$log_entry->{revision}!\n";
1543 }
Eric Wong44320b92007-01-13 22:35:53 -08001544 if (my $c = $self->rev_db_get($log_entry->{revision})) {
1545 croak "$log_entry->{revision} = $c already exists! ",
Eric Wong9b981fc2007-01-11 12:14:21 -08001546 "Why are we refetching it?\n";
1547 }
Eric Wongdb03cd22007-02-13 00:38:02 -08001548 $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $log_entry->{name};
1549 $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} =
1550 $log_entry->{email};
Eric Wong44320b92007-01-13 22:35:53 -08001551 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
Eric Wong9b981fc2007-01-11 12:14:21 -08001552
Eric Wong44320b92007-01-13 22:35:53 -08001553 my $tree = $log_entry->{tree};
Eric Wong9b981fc2007-01-11 12:14:21 -08001554 if (!defined $tree) {
1555 $tree = $self->tmp_index_do(sub {
1556 command_oneline('write-tree') });
1557 }
1558 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
1559
1560 my @exec = ('git-commit-tree', $tree);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001561 foreach ($self->get_commit_parents($log_entry)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08001562 push @exec, '-p', $_;
1563 }
1564 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
1565 or croak $!;
Eric Wong44320b92007-01-13 22:35:53 -08001566 print $msg_fh $log_entry->{log} or croak $!;
Eric Wong91b03282007-02-11 00:51:33 -08001567 unless ($self->no_metadata) {
Eric Wong8a49ee92007-02-10 20:46:50 -08001568 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
1569 or croak $!;
Eric Wong9760adc2007-01-31 03:06:56 -08001570 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001571 $msg_fh->flush == 0 or croak $!;
1572 close $msg_fh or croak $!;
1573 chomp(my $commit = do { local $/; <$out_fh> });
1574 close $out_fh or croak $!;
1575 waitpid $pid, 0;
1576 croak $? if $?;
1577 if ($commit !~ /^$::sha1$/o) {
1578 die "Failed to commit, invalid sha1: $commit\n";
1579 }
1580
Eric Wong373274f2007-01-31 13:54:23 -08001581 $self->rev_db_set($log_entry->{revision}, $commit, 1);
Eric Wong9b981fc2007-01-11 12:14:21 -08001582
Eric Wong44320b92007-01-13 22:35:53 -08001583 $self->{last_rev} = $log_entry->{revision};
Eric Wong9b981fc2007-01-11 12:14:21 -08001584 $self->{last_commit} = $commit;
Eric Wong8a49ee92007-02-10 20:46:50 -08001585 print "r$log_entry->{revision}";
1586 if (defined $log_entry->{svm_revision}) {
1587 print " (\@$log_entry->{svm_revision})";
Eric Wong26a62d52007-02-12 13:25:25 -08001588 $self->rev_db_set($log_entry->{svm_revision}, $commit,
1589 0, $self->svm_uuid);
Eric Wong8a49ee92007-02-10 20:46:50 -08001590 }
1591 print " = $commit ($self->{ref_id})\n";
Eric Wongecc712d2007-01-31 12:28:10 -08001592 if (defined $_repack && (--$_repack_nr == 0)) {
1593 $_repack_nr = $_repack;
1594 # repack doesn't use any arguments with spaces in them, does it?
1595 print "Running git repack $_repack_flags ...\n";
1596 command_noisy('repack', split(/\s+/, $_repack_flags));
1597 print "Done repacking\n";
1598 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001599 return $commit;
1600}
1601
Eric Wongfbcc1732007-02-06 18:35:30 -08001602sub match_paths {
1603 my ($self, $paths, $r) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001604 return 1 if $self->{path} eq '';
Eric Wongd542aed2007-02-09 02:19:41 -08001605 if (my $path = $paths->{"/$self->{path}"}) {
1606 return ($path->{action} eq 'D') ? 0 : 1;
1607 }
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001608 $self->{path_regex} ||= qr/^\/\Q$self->{path}\E\//;
Eric Wongfbcc1732007-02-06 18:35:30 -08001609 if (grep /$self->{path_regex}/, keys %$paths) {
1610 return 1;
1611 }
1612 my $c = '';
1613 foreach (split m#/#, $self->{path}) {
1614 $c .= "/$_";
Eric Wong74a81222007-02-10 13:28:50 -08001615 next unless ($paths->{$c} &&
1616 ($paths->{$c}->{action} =~ /^[AR]$/));
Eric Wonge5181922007-02-08 12:53:57 -08001617 if ($self->ra->check_path($self->{path}, $r) ==
1618 $SVN::Node::dir) {
Eric Wongfbcc1732007-02-06 18:35:30 -08001619 return 1;
1620 }
1621 }
1622 return 0;
1623}
1624
Eric Wong15710b62007-01-22 02:20:33 -08001625sub find_parent_branch {
1626 my ($self, $paths, $rev) = @_;
Eric Wong91b03282007-02-11 00:51:33 -08001627 return undef unless $self->follow_parent;
Eric Wonge5a0b242007-01-25 15:44:54 -08001628 unless (defined $paths) {
Eric Wongc7eba712007-01-31 03:45:28 -08001629 my $err_handler = $SVN::Error::handler;
1630 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
Eric Wongd4eff2b2007-01-30 14:04:22 -08001631 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, sub {
1632 $paths =
1633 Git::SVN::Ra::dup_changed_paths($_[0]) });
Eric Wongc7eba712007-01-31 03:45:28 -08001634 $SVN::Error::handler = $err_handler;
Eric Wonge5a0b242007-01-25 15:44:54 -08001635 }
1636 return undef unless defined $paths;
Eric Wong15710b62007-01-22 02:20:33 -08001637
1638 # look for a parent from another branch:
Eric Wong7f578c52007-01-24 02:16:25 -08001639 my @b_path_components = split m#/#, $self->rel_path;
1640 my @a_path_components;
1641 my $i;
1642 while (@b_path_components) {
1643 $i = $paths->{'/'.join('/', @b_path_components)};
Eric Wong74a81222007-02-10 13:28:50 -08001644 last if $i && defined $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08001645 unshift(@a_path_components, pop(@b_path_components));
1646 }
Eric Wong74a81222007-02-10 13:28:50 -08001647 return undef unless defined $i && defined $i->{copyfrom_path};
1648 my $branch_from = $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08001649 if (@a_path_components) {
1650 print STDERR "branch_from: $branch_from => ";
1651 $branch_from .= '/'.join('/', @a_path_components);
1652 print STDERR $branch_from, "\n";
1653 }
Eric Wong3ebe8df2007-01-25 17:35:40 -08001654 my $r = $i->{copyfrom_rev};
Eric Wong15710b62007-01-22 02:20:33 -08001655 my $repos_root = $self->ra->{repos_root};
1656 my $url = $self->ra->{url};
1657 my $new_url = $repos_root . $branch_from;
1658 print STDERR "Found possible branch point: ",
1659 "$new_url => ", $self->full_url, ", $r\n";
1660 $branch_from =~ s#^/##;
Eric Wonga8ae2622007-02-13 14:22:11 -08001661 my $gs = Git::SVN->find_by_url($new_url, $repos_root, $branch_from);
Eric Wong15710b62007-01-22 02:20:33 -08001662 unless ($gs) {
Eric Wongce2a0f22007-01-28 21:34:23 -08001663 my $ref_id = $self->{ref_id};
1664 $ref_id =~ s/\@\d+$//;
1665 $ref_id .= "\@$r";
Eric Wong15710b62007-01-22 02:20:33 -08001666 # just grow a tail if we're not unique enough :x
1667 $ref_id .= '-' while find_ref($ref_id);
Eric Wongce2a0f22007-01-28 21:34:23 -08001668 print STDERR "Initializing parent: $ref_id\n";
Eric Wongd8115c52007-02-01 03:30:31 -08001669 $gs = Git::SVN->init($new_url, '', $ref_id, $ref_id, 1);
Eric Wong15710b62007-01-22 02:20:33 -08001670 }
1671 my ($r0, $parent) = $gs->find_rev_before($r, 1);
Eric Wong91b03282007-02-11 00:51:33 -08001672 if (!defined $r0 || !defined $parent) {
Eric Wong3ebe8df2007-01-25 17:35:40 -08001673 $gs->fetch(0, $r);
Eric Wong15710b62007-01-22 02:20:33 -08001674 ($r0, $parent) = $gs->last_rev_commit;
1675 }
Eric Wongef70de92007-02-01 04:12:41 -08001676 if (defined $r0 && defined $parent) {
Eric Wong15710b62007-01-22 02:20:33 -08001677 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n";
Eric Wonga2003ab2007-01-22 15:22:50 -08001678 $self->assert_index_clean($parent);
Eric Wong15710b62007-01-22 02:20:33 -08001679 my $ed;
1680 if ($self->ra->can_do_switch) {
Eric Wong8b8fc062007-01-22 11:44:57 -08001681 print STDERR "Following parent with do_switch\n";
Eric Wong15710b62007-01-22 02:20:33 -08001682 # do_switch works with svn/trunk >= r22312, but that
Eric Wong2b27f6c2007-01-28 04:59:05 -08001683 # is not included with SVN 1.4.3 (the latest version
Eric Wong15710b62007-01-22 02:20:33 -08001684 # at the moment), so we can't rely on it
1685 $self->{last_commit} = $parent;
1686 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08001687 $gs->ra->gs_do_switch($r0, $rev, $gs,
Eric Wong15710b62007-01-22 02:20:33 -08001688 $self->full_url, $ed)
1689 or die "SVN connection failed somewhere...\n";
1690 } else {
Eric Wong8b8fc062007-01-22 11:44:57 -08001691 print STDERR "Following parent with do_update\n";
Eric Wong15710b62007-01-22 02:20:33 -08001692 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08001693 $self->ra->gs_do_update($rev, $rev, $self, $ed)
Eric Wong15710b62007-01-22 02:20:33 -08001694 or die "SVN connection failed somewhere...\n";
1695 }
Eric Wongf7c3fc42007-01-29 18:34:55 -08001696 print STDERR "Successfully followed parent\n";
Eric Wong15710b62007-01-22 02:20:33 -08001697 return $self->make_log_entry($rev, [$parent], $ed);
1698 }
Eric Wong15710b62007-01-22 02:20:33 -08001699 return undef;
1700}
1701
Eric Wong9b981fc2007-01-11 12:14:21 -08001702sub do_fetch {
Eric Wong706587f2007-01-18 17:50:01 -08001703 my ($self, $paths, $rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08001704 my $ed;
Eric Wong9b981fc2007-01-11 12:14:21 -08001705 my ($last_rev, @parents);
Eric Wongb9dffd82007-02-09 01:28:30 -08001706 if (my $lc = $self->last_commit) {
1707 # we can have a branch that was deleted, then re-added
1708 # under the same name but copied from another path, in
1709 # which case we'll have multiple parents (we don't
1710 # want to break the original ref, nor lose copypath info):
1711 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1712 push @{$log_entry->{parents}}, $lc;
1713 return $log_entry;
1714 }
Eric Wong15710b62007-01-22 02:20:33 -08001715 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08001716 $last_rev = $self->{last_rev};
Eric Wongb9dffd82007-02-09 01:28:30 -08001717 $ed->{c} = $lc;
1718 @parents = ($lc);
Eric Wong9b981fc2007-01-11 12:14:21 -08001719 } else {
1720 $last_rev = $rev;
Eric Wong15710b62007-01-22 02:20:33 -08001721 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
1722 return $log_entry;
1723 }
1724 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08001725 }
Eric Wong8a603772007-01-31 02:45:50 -08001726 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08001727 die "SVN connection failed somewhere...\n";
1728 }
1729 $self->make_log_entry($rev, \@parents, $ed);
1730}
1731
Eric Wong97f69872007-01-25 11:53:13 -08001732sub get_untracked {
1733 my ($self, $ed) = @_;
1734 my @out;
1735 my $h = $ed->{empty};
Eric Wong9b981fc2007-01-11 12:14:21 -08001736 foreach (sort keys %$h) {
1737 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
Eric Wong97f69872007-01-25 11:53:13 -08001738 push @out, " $act: " . uri_encode($_);
Eric Wong9b981fc2007-01-11 12:14:21 -08001739 warn "W: $act: $_\n";
1740 }
1741 foreach my $t (qw/dir_prop file_prop/) {
Eric Wong97f69872007-01-25 11:53:13 -08001742 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08001743 foreach my $path (sort keys %$h) {
1744 my $ppath = $path eq '' ? '.' : $path;
1745 foreach my $prop (sort keys %{$h->{$path}}) {
Eric Wong1ce255d2007-01-14 23:21:16 -08001746 next if $SKIP_PROP{$prop};
Eric Wong9b981fc2007-01-11 12:14:21 -08001747 my $v = $h->{$path}->{$prop};
Eric Wong97f69872007-01-25 11:53:13 -08001748 my $t_ppath_prop = "$t: " .
1749 uri_encode($ppath) . ' ' .
1750 uri_encode($prop);
Eric Wong9b981fc2007-01-11 12:14:21 -08001751 if (defined $v) {
Eric Wong97f69872007-01-25 11:53:13 -08001752 push @out, " +$t_ppath_prop " .
1753 uri_encode($v);
Eric Wong9b981fc2007-01-11 12:14:21 -08001754 } else {
Eric Wong97f69872007-01-25 11:53:13 -08001755 push @out, " -$t_ppath_prop";
Eric Wong9b981fc2007-01-11 12:14:21 -08001756 }
1757 }
1758 }
1759 }
1760 foreach my $t (qw/absent_file absent_directory/) {
Eric Wong97f69872007-01-25 11:53:13 -08001761 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08001762 foreach my $parent (sort keys %$h) {
1763 foreach my $path (sort @{$h->{$parent}}) {
Eric Wong97f69872007-01-25 11:53:13 -08001764 push @out, " $t: " .
1765 uri_encode("$parent/$path");
Eric Wong9b981fc2007-01-11 12:14:21 -08001766 warn "W: $t: $parent/$path ",
1767 "Insufficient permissions?\n";
1768 }
1769 }
1770 }
Eric Wong97f69872007-01-25 11:53:13 -08001771 \@out;
Eric Wong9b981fc2007-01-11 12:14:21 -08001772}
1773
Eric Wong1c8443b2007-01-14 02:17:00 -08001774sub parse_svn_date {
1775 my $date = shift || return '+0000 1970-01-01 00:00:00';
1776 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
1777 (\d\d)\:(\d\d)\:(\d\d).\d+Z$/x) or
1778 croak "Unable to parse date: $date\n";
1779 "+0000 $Y-$m-$d $H:$M:$S";
1780}
1781
1782sub check_author {
1783 my ($author) = @_;
1784 if (!defined $author || length $author == 0) {
1785 $author = '(no author)';
1786 }
1787 if (defined $::_authors && ! defined $::users{$author}) {
1788 die "Author: $author not defined in $::_authors file\n";
1789 }
1790 $author;
1791}
1792
Eric Wong9b981fc2007-01-11 12:14:21 -08001793sub make_log_entry {
Eric Wong97f69872007-01-25 11:53:13 -08001794 my ($self, $rev, $parents, $ed) = @_;
1795 my $untracked = $self->get_untracked($ed);
1796
Eric Wong9b981fc2007-01-11 12:14:21 -08001797 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08001798 print $un "r$rev\n" or croak $!;
1799 print $un $_, "\n" foreach @$untracked;
1800 my %log_entry = ( parents => $parents || [], revision => $rev,
1801 log => '');
Eric Wongfbcc1732007-02-06 18:35:30 -08001802
Eric Wong8a49ee92007-02-10 20:46:50 -08001803 my $headrev;
Eric Wongfbcc1732007-02-06 18:35:30 -08001804 my $logged = delete $self->{logged_rev_props};
Eric Wong8a49ee92007-02-10 20:46:50 -08001805 if (!$logged || $self->{-want_revprops}) {
Eric Wongfbcc1732007-02-06 18:35:30 -08001806 my $rp = $self->ra->rev_proplist($rev);
1807 foreach (sort keys %$rp) {
1808 my $v = $rp->{$_};
1809 if (/^svn:(author|date|log)$/) {
1810 $log_entry{$1} = $v;
Eric Wong8a49ee92007-02-10 20:46:50 -08001811 } elsif ($_ eq 'svm:headrev') {
1812 $headrev = $v;
Eric Wongfbcc1732007-02-06 18:35:30 -08001813 } else {
1814 print $un " rev_prop: ", uri_encode($_), ' ',
1815 uri_encode($v), "\n";
1816 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001817 }
Eric Wongfbcc1732007-02-06 18:35:30 -08001818 } else {
1819 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
Eric Wong9b981fc2007-01-11 12:14:21 -08001820 }
1821 close $un or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08001822
Eric Wong9b981fc2007-01-11 12:14:21 -08001823 $log_entry{date} = parse_svn_date($log_entry{date});
Eric Wong9b981fc2007-01-11 12:14:21 -08001824 $log_entry{log} .= "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001825 my $author = $log_entry{author} = check_author($log_entry{author});
1826 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
1827 : ($author, undef);
Eric Wong91b03282007-02-11 00:51:33 -08001828 if (defined $headrev && $self->use_svm_props) {
Eric Wongaea736c2007-02-16 19:15:21 -08001829 if ($self->rewrite_root) {
1830 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
1831 "options set!\n";
1832 }
Eric Wong8a49ee92007-02-10 20:46:50 -08001833 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001834 # we don't want "SVM: initializing mirror for junk" ...
1835 return undef if $r == 0;
1836 my $svm = $self->svm;
1837 if ($uuid ne $svm->{uuid}) {
Eric Wong8a49ee92007-02-10 20:46:50 -08001838 die "UUID mismatch on SVM path:\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08001839 "expected: $svm->{uuid}\n",
Eric Wong8a49ee92007-02-10 20:46:50 -08001840 " got: $uuid\n";
1841 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001842 my $full_url = $self->full_url;
1843 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
1844 die "Failed to replace '$svm->{replace}' with ",
1845 "'$svm->{source}' in $full_url\n";
Sam Vilain18ea92b2007-02-23 12:32:29 +13001846 # throw away username for storing in records
1847 remove_username($full_url);
Eric Wong8a49ee92007-02-10 20:46:50 -08001848 $log_entry{metadata} = "$full_url\@$r $uuid";
1849 $log_entry{svm_revision} = $r;
Eric Wongdb03cd22007-02-13 00:38:02 -08001850 $email ||= "$author\@$uuid"
Eric Wong62e349d2007-02-16 19:57:29 -08001851 } elsif ($self->use_svnsync_props) {
1852 my $full_url = $self->svnsync->{url};
1853 $full_url .= "/$self->{path}" if length $self->{path};
1854 my $uuid = $self->svnsync->{uuid};
1855 $log_entry{metadata} = "$full_url\@$rev $uuid";
1856 $email ||= "$author\@$uuid"
Eric Wong8a49ee92007-02-10 20:46:50 -08001857 } else {
Eric Wongaea736c2007-02-16 19:15:21 -08001858 $log_entry{metadata} = $self->metadata_url. "\@$rev " .
Eric Wong26a62d52007-02-12 13:25:25 -08001859 $self->ra->get_uuid;
Eric Wongdb03cd22007-02-13 00:38:02 -08001860 $email ||= "$author\@" . $self->ra->get_uuid;
Eric Wong8a49ee92007-02-10 20:46:50 -08001861 }
Eric Wongdb03cd22007-02-13 00:38:02 -08001862 $log_entry{name} = $name;
1863 $log_entry{email} = $email;
Eric Wong9b981fc2007-01-11 12:14:21 -08001864 \%log_entry;
1865}
1866
1867sub fetch {
Eric Wong3ebe8df2007-01-25 17:35:40 -08001868 my ($self, $min_rev, $max_rev, @parents) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08001869 my ($last_rev, $last_commit) = $self->last_rev_commit;
Eric Wong3ebe8df2007-01-25 17:35:40 -08001870 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
Eric Wonge5181922007-02-08 12:53:57 -08001871 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
Eric Wong9b981fc2007-01-11 12:14:21 -08001872}
1873
1874sub set_tree_cb {
1875 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
Eric Wong490f49e2007-02-10 13:58:33 -08001876 $self->{inject_parents} = { $rev => $tree };
1877 $self->fetch(undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08001878}
1879
1880sub set_tree {
1881 my ($self, $tree) = (shift, shift);
Eric Wong1ce255d2007-01-14 23:21:16 -08001882 my $log_entry = ::get_commit_entry($tree);
Eric Wong9b981fc2007-01-11 12:14:21 -08001883 unless ($self->{last_rev}) {
1884 fatal("Must have an existing revision to commit\n");
1885 }
Eric Wong61395352007-01-27 14:33:08 -08001886 my %ed_opts = ( r => $self->{last_rev},
1887 log => $log_entry->{log},
1888 ra => $self->ra,
1889 tree_a => $self->{last_commit},
1890 tree_b => $tree,
1891 editor_cb => sub {
1892 $self->set_tree_cb($log_entry, $tree, @_) },
1893 svn_path => $self->{path} );
1894 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong9b981fc2007-01-11 12:14:21 -08001895 print "No changes\nr$self->{last_rev} = $tree\n";
1896 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001897}
1898
Eric Wongf0ecca12007-01-30 13:11:14 -08001899sub rebuild {
1900 my ($self) = @_;
Eric Wong26a62d52007-02-12 13:25:25 -08001901 my $db_path = $self->db_path;
Eric Wongd6d33462007-02-16 04:05:33 -08001902 return if (-e $db_path && ! -z $db_path);
1903 return unless ::verify_ref($self->refname.'^0');
Eric Wong26a62d52007-02-12 13:25:25 -08001904 if (-f $self->{db_root}) {
1905 rename $self->{db_root}, $db_path or die
1906 "rename $self->{db_root} => $db_path failed: $!\n";
1907 my ($dir, $base) = ($db_path =~ m#^(.*?)/?([^/]+)$#);
1908 symlink $base, $self->{db_root} or die
1909 "symlink $base => $self->{db_root} failed: $!\n";
1910 return;
1911 }
1912 print "Rebuilding $db_path ...\n";
Eric Wongf0ecca12007-01-30 13:11:14 -08001913 my ($rev_list, $ctx) = command_output_pipe("rev-list", $self->refname);
1914 my $latest;
1915 my $full_url = $self->full_url;
Sam Vilain18ea92b2007-02-23 12:32:29 +13001916 remove_username($full_url);
Eric Wongf0ecca12007-01-30 13:11:14 -08001917 my $svn_uuid;
1918 while (<$rev_list>) {
1919 chomp;
1920 my $c = $_;
1921 die "Non-SHA1: $c\n" unless $c =~ /^$::sha1$/o;
1922 my ($url, $rev, $uuid) = ::cmt_metadata($c);
Sam Vilain18ea92b2007-02-23 12:32:29 +13001923 remove_username($url);
Eric Wongf0ecca12007-01-30 13:11:14 -08001924
1925 # ignore merges (from set-tree)
1926 next if (!defined $rev || !$uuid);
1927
1928 # if we merged or otherwise started elsewhere, this is
1929 # how we break out of it
1930 if ((defined $svn_uuid && ($uuid ne $svn_uuid)) ||
1931 ($full_url && $url && ($url ne $full_url))) {
1932 next;
1933 }
1934 $latest ||= $rev;
1935 $svn_uuid ||= $uuid;
1936
1937 $self->rev_db_set($rev, $c);
1938 print "r$rev = $c\n";
1939 }
1940 command_close_pipe($rev_list, $ctx);
Eric Wong26a62d52007-02-12 13:25:25 -08001941 print "Done rebuilding $db_path\n";
Eric Wongf0ecca12007-01-30 13:11:14 -08001942}
1943
Eric Wong9b981fc2007-01-11 12:14:21 -08001944# rev_db:
1945# Tie::File seems to be prone to offset errors if revisions get sparse,
1946# it's not that fast, either. Tie::File is also not in Perl 5.6. So
1947# one of my favorite modules is out :< Next up would be one of the DBM
1948# modules, but I'm not sure which is most portable... So I'll just
1949# go with something that's plain-text, but still capable of
1950# being randomly accessed. So here's my ultra-simple fixed-width
1951# database. All records are 40 characters + "\n", so it's easy to seek
1952# to a revision: (41 * rev) is the byte offset.
1953# A record of 40 0s denotes an empty revision.
1954# And yes, it's still pretty fast (faster than Tie::File).
Eric Wong97ae0912007-02-11 15:21:24 -08001955# These files are disposable unless noMetadata or useSvmProps is set
Eric Wong9b981fc2007-01-11 12:14:21 -08001956
Eric Wong26a62d52007-02-12 13:25:25 -08001957sub _rev_db_set {
1958 my ($fh, $rev, $commit) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08001959 my $offset = $rev * 41;
1960 # assume that append is the common case:
1961 seek $fh, 0, 2 or croak $!;
1962 my $pos = tell $fh;
1963 if ($pos < $offset) {
Eric Wong47a0b752007-01-31 05:13:30 -08001964 for (1 .. (($offset - $pos) / 41)) {
1965 print $fh (('0' x 40),"\n") or croak $!;
1966 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001967 }
1968 seek $fh, $offset, 0 or croak $!;
1969 print $fh $commit,"\n" or croak $!;
Eric Wong26a62d52007-02-12 13:25:25 -08001970}
1971
1972sub mkfile {
1973 my ($path) = @_;
1974 unless (-e $path) {
1975 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
1976 mkpath([$dir]) unless -d $dir;
1977 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
1978 close $fh or die "Couldn't close (create) $path: $!\n";
1979 }
1980}
1981
1982sub rev_db_set {
1983 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
1984 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
1985 my $db = $self->db_path($uuid);
1986 my $db_lock = "$db.lock";
1987 my $sig;
1988 if ($update_ref) {
1989 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
1990 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
1991 }
1992 mkfile($db);
1993
1994 $LOCKFILES{$db_lock} = 1;
1995 my $sync;
1996 # both of these options make our .rev_db file very, very important
1997 # and we can't afford to lose it because rebuild() won't work
1998 if ($self->use_svm_props || $self->no_metadata) {
1999 $sync = 1;
2000 copy($db, $db_lock) or die "rev_db_set(@_): ",
2001 "Failed to copy: ",
2002 "$db => $db_lock ($!)\n";
2003 } else {
2004 rename $db, $db_lock or die "rev_db_set(@_): ",
2005 "Failed to rename: ",
2006 "$db => $db_lock ($!)\n";
2007 }
2008 open my $fh, '+<', $db_lock or die "Couldn't open $db_lock: $!\n";
2009 _rev_db_set($fh, $rev, $commit);
Eric Wong97ae0912007-02-11 15:21:24 -08002010 if ($sync) {
2011 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2012 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2013 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002014 close $fh or croak $!;
Eric Wong373274f2007-01-31 13:54:23 -08002015 if ($update_ref) {
Eric Wong1e889ef2007-02-16 01:45:13 -08002016 $_head = $self;
Eric Wong373274f2007-01-31 13:54:23 -08002017 command_noisy('update-ref', '-m', "r$rev",
2018 $self->refname, $commit);
2019 }
2020 rename $db_lock, $db or die "rev_db_set(@_): ", "Failed to rename: ",
2021 "$db_lock => $db ($!)\n";
2022 delete $LOCKFILES{$db_lock};
2023 if ($update_ref) {
2024 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
2025 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
2026 kill $sig, $$ if defined $sig;
2027 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002028}
2029
Eric Wong9c93fee2007-01-31 17:22:31 -08002030sub rev_db_max {
2031 my ($self) = @_;
Eric Wongd6d33462007-02-16 04:05:33 -08002032 $self->rebuild;
Eric Wong26a62d52007-02-12 13:25:25 -08002033 my $db_path = $self->db_path;
2034 my @stat = stat $db_path or return 0;
2035 ($stat[7] % 41) == 0 or die "$db_path inconsistent size: $stat[7]\n";
Eric Wong9c93fee2007-01-31 17:22:31 -08002036 my $max = $stat[7] / 41;
2037 (($max > 0) ? $max - 1 : 0);
2038}
2039
Eric Wong9b981fc2007-01-11 12:14:21 -08002040sub rev_db_get {
Eric Wong26a62d52007-02-12 13:25:25 -08002041 my ($self, $rev, $uuid) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002042 my $ret;
2043 my $offset = $rev * 41;
Eric Wong26a62d52007-02-12 13:25:25 -08002044 my $db_path = $self->db_path($uuid);
2045 return undef unless -e $db_path;
2046 open my $fh, '<', $db_path or croak $!;
Eric Wongce4b4af2007-01-31 17:57:36 -08002047 if (sysseek($fh, $offset, 0) == $offset) {
2048 my $read = sysread($fh, $ret, 40);
2049 $ret = undef if ($read != 40 || $ret eq ('0'x40));
Eric Wong9b981fc2007-01-11 12:14:21 -08002050 }
2051 close $fh or croak $!;
2052 $ret;
2053}
2054
Eric Wong15710b62007-01-22 02:20:33 -08002055sub find_rev_before {
2056 my ($self, $rev, $eq_ok) = @_;
2057 --$rev unless $eq_ok;
2058 while ($rev > 0) {
2059 if (my $c = $self->rev_db_get($rev)) {
2060 return ($rev, $c);
2061 }
2062 --$rev;
2063 }
2064 return (undef, undef);
2065}
2066
Eric Wong9b981fc2007-01-11 12:14:21 -08002067sub _new {
Eric Wong706587f2007-01-18 17:50:01 -08002068 my ($class, $repo_id, $ref_id, $path) = @_;
2069 unless (defined $repo_id && length $repo_id) {
2070 $repo_id = $Git::SVN::default_repo_id;
2071 }
2072 unless (defined $ref_id && length $ref_id) {
Eric Wong8b8fc062007-01-22 11:44:57 -08002073 $_[2] = $ref_id = $Git::SVN::default_ref_id;
Eric Wong706587f2007-01-18 17:50:01 -08002074 }
2075 $_[1] = $repo_id = sanitize_remote_name($repo_id);
2076 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
2077 $_[3] = $path = '' unless (defined $path);
Eric Wongb4d57e52007-02-14 15:10:44 -08002078 mkpath(["$ENV{GIT_DIR}/svn"]);
Eric Wong26a62d52007-02-12 13:25:25 -08002079 bless {
2080 ref_id => $ref_id, dir => $dir, index => "$dir/index",
Eric Wong8a49ee92007-02-10 20:46:50 -08002081 path => $path, config => "$ENV{GIT_DIR}/svn/config",
Eric Wong26a62d52007-02-12 13:25:25 -08002082 db_root => "$dir/.rev_db", repo_id => $repo_id }, $class;
2083}
2084
2085sub db_path {
2086 my ($self, $uuid) = @_;
2087 $uuid ||= $self->ra_uuid;
2088 "$self->{db_root}.$uuid";
Eric Wong9b981fc2007-01-11 12:14:21 -08002089}
2090
Eric Wong1c8443b2007-01-14 02:17:00 -08002091sub uri_encode {
2092 my ($f) = @_;
2093 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
2094 $f
2095}
Eric Wong9b981fc2007-01-11 12:14:21 -08002096
Sam Vilain18ea92b2007-02-23 12:32:29 +13002097sub remove_username {
2098 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
2099}
2100
Eric Wongd976acf2007-01-04 00:45:03 -08002101package Git::SVN::Prompt;
2102use strict;
2103use warnings;
2104require SVN::Core;
2105use vars qw/$_no_auth_cache $_username/;
2106
2107sub simple {
Eric Wong30d055a2006-11-24 01:38:04 -08002108 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
2109 $may_save = undef if $_no_auth_cache;
2110 $default_username = $_username if defined $_username;
2111 if (defined $default_username && length $default_username) {
2112 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08002113 print STDERR "Authentication realm: $realm\n";
2114 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002115 }
2116 $cred->username($default_username);
2117 } else {
Eric Wongd976acf2007-01-04 00:45:03 -08002118 username($cred, $realm, $may_save, $pool);
Eric Wong30d055a2006-11-24 01:38:04 -08002119 }
2120 $cred->password(_read_password("Password for '" .
2121 $cred->username . "': ", $realm));
2122 $cred->may_save($may_save);
2123 $SVN::_Core::SVN_NO_ERROR;
2124}
2125
Eric Wongd976acf2007-01-04 00:45:03 -08002126sub ssl_server_trust {
Eric Wong30d055a2006-11-24 01:38:04 -08002127 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
2128 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08002129 print STDERR "Error validating server certificate for '$realm':\n";
Eric Wong30d055a2006-11-24 01:38:04 -08002130 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
Eric Wong6f729592007-01-15 20:15:55 -08002131 print STDERR " - The certificate is not issued by a trusted ",
Eric Wong30d055a2006-11-24 01:38:04 -08002132 "authority. Use the\n",
2133 " fingerprint to validate the certificate manually!\n";
2134 }
2135 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
Eric Wong6f729592007-01-15 20:15:55 -08002136 print STDERR " - The certificate hostname does not match.\n";
Eric Wong30d055a2006-11-24 01:38:04 -08002137 }
2138 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
Eric Wong6f729592007-01-15 20:15:55 -08002139 print STDERR " - The certificate is not yet valid.\n";
Eric Wong30d055a2006-11-24 01:38:04 -08002140 }
2141 if ($failures & $SVN::Auth::SSL::EXPIRED) {
Eric Wong6f729592007-01-15 20:15:55 -08002142 print STDERR " - The certificate has expired.\n";
Eric Wong30d055a2006-11-24 01:38:04 -08002143 }
2144 if ($failures & $SVN::Auth::SSL::OTHER) {
Eric Wong6f729592007-01-15 20:15:55 -08002145 print STDERR " - The certificate has an unknown error.\n";
Eric Wong30d055a2006-11-24 01:38:04 -08002146 }
Eric Wong6f729592007-01-15 20:15:55 -08002147 printf STDERR
2148 "Certificate information:\n".
Eric Wong30d055a2006-11-24 01:38:04 -08002149 " - Hostname: %s\n".
2150 " - Valid: from %s until %s\n".
2151 " - Issuer: %s\n".
2152 " - Fingerprint: %s\n",
2153 map $cert_info->$_, qw(hostname valid_from valid_until
Eric Wong6f729592007-01-15 20:15:55 -08002154 issuer_dname fingerprint);
Eric Wong30d055a2006-11-24 01:38:04 -08002155 my $choice;
2156prompt:
Eric Wong6f729592007-01-15 20:15:55 -08002157 print STDERR $may_save ?
Eric Wong30d055a2006-11-24 01:38:04 -08002158 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
2159 "(R)eject or accept (t)emporarily? ";
Eric Wong6f729592007-01-15 20:15:55 -08002160 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002161 $choice = lc(substr(<STDIN> || 'R', 0, 1));
2162 if ($choice =~ /^t$/i) {
2163 $cred->may_save(undef);
2164 } elsif ($choice =~ /^r$/i) {
2165 return -1;
2166 } elsif ($may_save && $choice =~ /^p$/i) {
2167 $cred->may_save($may_save);
2168 } else {
2169 goto prompt;
2170 }
2171 $cred->accepted_failures($failures);
2172 $SVN::_Core::SVN_NO_ERROR;
2173}
2174
Eric Wongd976acf2007-01-04 00:45:03 -08002175sub ssl_client_cert {
Eric Wong30d055a2006-11-24 01:38:04 -08002176 my ($cred, $realm, $may_save, $pool) = @_;
2177 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08002178 print STDERR "Client certificate filename: ";
2179 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002180 chomp(my $filename = <STDIN>);
2181 $cred->cert_file($filename);
2182 $cred->may_save($may_save);
2183 $SVN::_Core::SVN_NO_ERROR;
2184}
2185
Eric Wongd976acf2007-01-04 00:45:03 -08002186sub ssl_client_cert_pw {
Eric Wong30d055a2006-11-24 01:38:04 -08002187 my ($cred, $realm, $may_save, $pool) = @_;
2188 $may_save = undef if $_no_auth_cache;
2189 $cred->password(_read_password("Password: ", $realm));
2190 $cred->may_save($may_save);
2191 $SVN::_Core::SVN_NO_ERROR;
2192}
2193
Eric Wongd976acf2007-01-04 00:45:03 -08002194sub username {
Eric Wong30d055a2006-11-24 01:38:04 -08002195 my ($cred, $realm, $may_save, $pool) = @_;
2196 $may_save = undef if $_no_auth_cache;
2197 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08002198 print STDERR "Authentication realm: $realm\n";
Eric Wong30d055a2006-11-24 01:38:04 -08002199 }
2200 my $username;
2201 if (defined $_username) {
2202 $username = $_username;
2203 } else {
Eric Wong6f729592007-01-15 20:15:55 -08002204 print STDERR "Username: ";
2205 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002206 chomp($username = <STDIN>);
2207 }
2208 $cred->username($username);
2209 $cred->may_save($may_save);
2210 $SVN::_Core::SVN_NO_ERROR;
2211}
2212
2213sub _read_password {
2214 my ($prompt, $realm) = @_;
Eric Wong6f729592007-01-15 20:15:55 -08002215 print STDERR $prompt;
2216 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002217 require Term::ReadKey;
2218 Term::ReadKey::ReadMode('noecho');
2219 my $password = '';
2220 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
2221 last if $key =~ /[\012\015]/; # \n\r
2222 $password .= $key;
2223 }
2224 Term::ReadKey::ReadMode('restore');
Eric Wong6f729592007-01-15 20:15:55 -08002225 print STDERR "\n";
2226 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08002227 $password;
2228}
2229
Eric Wongd976acf2007-01-04 00:45:03 -08002230package main;
2231
Eric Wongb9c85182006-12-15 23:58:07 -08002232{
2233 my $kill_stupid_warnings = $SVN::Node::none.$SVN::Node::file.
2234 $SVN::Node::dir.$SVN::Node::unknown.
2235 $SVN::Node::none.$SVN::Node::file.
2236 $SVN::Node::dir.$SVN::Node::unknown.
2237 $SVN::Auth::SSL::CNMISMATCH.
2238 $SVN::Auth::SSL::NOTYETVALID.
2239 $SVN::Auth::SSL::EXPIRED.
2240 $SVN::Auth::SSL::UNKNOWNCA.
2241 $SVN::Auth::SSL::OTHER;
2242}
2243
Eric Wong27a1a802006-11-27 21:44:48 -08002244package SVN::Git::Fetcher;
2245use vars qw/@ISA/;
2246use strict;
2247use warnings;
2248use Carp qw/croak/;
2249use IO::File qw//;
Eric Wong90c1b152007-01-29 16:27:08 -08002250use Digest::MD5;
Eric Wong27a1a802006-11-27 21:44:48 -08002251
2252# file baton members: path, mode_a, mode_b, pool, fh, blob, base
2253sub new {
2254 my ($class, $git_svn) = @_;
2255 my $self = SVN::Delta::Editor->new;
2256 bless $self, $class;
Eric Wong1c8443b2007-01-14 02:17:00 -08002257 $self->{c} = $git_svn->{last_commit} if exists $git_svn->{last_commit};
Eric Wongd2a9a872006-12-12 14:47:00 -08002258 $self->{empty} = {};
2259 $self->{dir_prop} = {};
2260 $self->{file_prop} = {};
2261 $self->{absent_dir} = {};
2262 $self->{absent_file} = {};
Eric Wongef3cfaa2007-01-24 03:30:57 -08002263 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
Eric Wong27a1a802006-11-27 21:44:48 -08002264 $self;
2265}
2266
Eric Wong8b8fc062007-01-22 11:44:57 -08002267sub set_path_strip {
2268 my ($self, $path) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002269 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
Eric Wong8b8fc062007-01-22 11:44:57 -08002270}
2271
Eric Wongd2a9a872006-12-12 14:47:00 -08002272sub open_root {
2273 { path => '' };
2274}
2275
2276sub open_directory {
2277 my ($self, $path, $pb, $rev) = @_;
2278 { path => $path };
2279}
2280
Eric Wong706587f2007-01-18 17:50:01 -08002281sub git_path {
2282 my ($self, $path) = @_;
Eric Wong2b27f6c2007-01-28 04:59:05 -08002283 if ($self->{path_strip}) {
2284 $path =~ s!$self->{path_strip}!! or
2285 die "Failed to strip path '$path' ($self->{path_strip})\n";
2286 }
Eric Wong706587f2007-01-18 17:50:01 -08002287 $path;
2288}
2289
Eric Wong27a1a802006-11-27 21:44:48 -08002290sub delete_entry {
2291 my ($self, $path, $rev, $pb) = @_;
Eric Wong4a87db02007-01-04 01:38:18 -08002292
Eric Wong706587f2007-01-18 17:50:01 -08002293 my $gpath = $self->git_path($path);
Eric Wong8a603772007-01-31 02:45:50 -08002294 return undef if ($gpath eq '');
2295
Eric Wong4a87db02007-01-04 01:38:18 -08002296 # remove entire directories.
Eric Wong706587f2007-01-18 17:50:01 -08002297 if (command('ls-tree', $self->{c}, '--', $gpath) =~ /^040000 tree/) {
Eric Wong4a87db02007-01-04 01:38:18 -08002298 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
2299 -r --name-only -z/,
Eric Wong706587f2007-01-18 17:50:01 -08002300 $self->{c}, '--', $gpath);
Eric Wong4a87db02007-01-04 01:38:18 -08002301 local $/ = "\0";
2302 while (<$ls>) {
Eric Wongef3cfaa2007-01-24 03:30:57 -08002303 chomp;
2304 $self->{gii}->remove($_);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08002305 print "\tD\t$_\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08002306 }
Eric Wong9e3cdbd2007-02-09 12:23:47 -08002307 print "\tD\t$gpath/\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08002308 command_close_pipe($ls, $ctx);
2309 $self->{empty}->{$path} = 0
2310 } else {
Eric Wongef3cfaa2007-01-24 03:30:57 -08002311 $self->{gii}->remove($gpath);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08002312 print "\tD\t$gpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08002313 }
Eric Wong27a1a802006-11-27 21:44:48 -08002314 undef;
2315}
2316
2317sub open_file {
2318 my ($self, $path, $pb, $rev) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08002319 my $gpath = $self->git_path($path);
2320 my ($mode, $blob) = (command('ls-tree', $self->{c}, '--', $gpath)
Eric Wong27a1a802006-11-27 21:44:48 -08002321 =~ /^(\d{6}) blob ([a-f\d]{40})\t/);
Eric Wong006ede52006-12-08 01:55:19 -08002322 unless (defined $mode && defined $blob) {
2323 die "$path was not found in commit $self->{c} (r$rev)\n";
2324 }
Eric Wong27a1a802006-11-27 21:44:48 -08002325 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
Eric Wong0864e3b2006-11-28 02:50:17 -08002326 pool => SVN::Pool->new, action => 'M' };
Eric Wong27a1a802006-11-27 21:44:48 -08002327}
2328
2329sub add_file {
2330 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
Eric Wongd2a9a872006-12-12 14:47:00 -08002331 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
2332 delete $self->{empty}->{$dir};
Eric Wong27a1a802006-11-27 21:44:48 -08002333 { path => $path, mode_a => 100644, mode_b => 100644,
Eric Wong0864e3b2006-11-28 02:50:17 -08002334 pool => SVN::Pool->new, action => 'A' };
Eric Wong27a1a802006-11-27 21:44:48 -08002335}
2336
Eric Wongd2a9a872006-12-12 14:47:00 -08002337sub add_directory {
2338 my ($self, $path, $cp_path, $cp_rev) = @_;
2339 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
2340 delete $self->{empty}->{$dir};
2341 $self->{empty}->{$path} = 1;
2342 { path => $path };
2343}
2344
2345sub change_dir_prop {
2346 my ($self, $db, $prop, $value) = @_;
2347 $self->{dir_prop}->{$db->{path}} ||= {};
2348 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
2349 undef;
2350}
2351
2352sub absent_directory {
2353 my ($self, $path, $pb) = @_;
2354 $self->{absent_dir}->{$pb->{path}} ||= [];
2355 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
2356 undef;
2357}
2358
2359sub absent_file {
2360 my ($self, $path, $pb) = @_;
2361 $self->{absent_file}->{$pb->{path}} ||= [];
2362 push @{$self->{absent_file}->{$pb->{path}}}, $path;
2363 undef;
2364}
2365
Eric Wong27a1a802006-11-27 21:44:48 -08002366sub change_file_prop {
2367 my ($self, $fb, $prop, $value) = @_;
2368 if ($prop eq 'svn:executable') {
2369 if ($fb->{mode_b} != 120000) {
2370 $fb->{mode_b} = defined $value ? 100755 : 100644;
2371 }
2372 } elsif ($prop eq 'svn:special') {
2373 $fb->{mode_b} = defined $value ? 120000 : 100644;
Eric Wongd2a9a872006-12-12 14:47:00 -08002374 } else {
2375 $self->{file_prop}->{$fb->{path}} ||= {};
2376 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
Eric Wong27a1a802006-11-27 21:44:48 -08002377 }
2378 undef;
2379}
2380
2381sub apply_textdelta {
2382 my ($self, $fb, $exp) = @_;
2383 my $fh = IO::File->new_tmpfile;
2384 $fh->autoflush(1);
2385 # $fh gets auto-closed() by SVN::TxDelta::apply(),
2386 # (but $base does not,) so dup() it for reading in close_file
2387 open my $dup, '<&', $fh or croak $!;
2388 my $base = IO::File->new_tmpfile;
2389 $base->autoflush(1);
2390 if ($fb->{blob}) {
2391 defined (my $pid = fork) or croak $!;
2392 if (!$pid) {
2393 open STDOUT, '>&', $base or croak $!;
2394 print STDOUT 'link ' if ($fb->{mode_a} == 120000);
2395 exec qw/git-cat-file blob/, $fb->{blob} or croak $!;
2396 }
2397 waitpid $pid, 0;
2398 croak $? if $?;
2399
2400 if (defined $exp) {
2401 seek $base, 0, 0 or croak $!;
2402 my $md5 = Digest::MD5->new;
2403 $md5->addfile($base);
2404 my $got = $md5->hexdigest;
2405 die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
2406 "expected: $exp\n",
2407 " got: $got\n" if ($got ne $exp);
2408 }
2409 }
2410 seek $base, 0, 0 or croak $!;
2411 $fb->{fh} = $dup;
2412 $fb->{base} = $base;
2413 [ SVN::TxDelta::apply($base, $fh, undef, $fb->{path}, $fb->{pool}) ];
2414}
2415
2416sub close_file {
2417 my ($self, $fb, $exp) = @_;
2418 my $hash;
Eric Wong706587f2007-01-18 17:50:01 -08002419 my $path = $self->git_path($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08002420 if (my $fh = $fb->{fh}) {
2421 seek($fh, 0, 0) or croak $!;
2422 my $md5 = Digest::MD5->new;
2423 $md5->addfile($fh);
2424 my $got = $md5->hexdigest;
2425 die "Checksum mismatch: $path\n",
2426 "expected: $exp\n got: $got\n" if ($got ne $exp);
2427 seek($fh, 0, 0) or croak $!;
2428 if ($fb->{mode_b} == 120000) {
2429 read($fh, my $buf, 5) == 5 or croak $!;
2430 $buf eq 'link ' or die "$path has mode 120000",
2431 "but is not a link\n";
2432 }
2433 defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
2434 if (!$pid) {
2435 open STDIN, '<&', $fh or croak $!;
2436 exec qw/git-hash-object -w --stdin/ or croak $!;
2437 }
2438 chomp($hash = do { local $/; <$out> });
2439 close $out or croak $!;
2440 close $fh or croak $!;
2441 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
2442 close $fb->{base} or croak $!;
2443 } else {
2444 $hash = $fb->{blob} or die "no blob information\n";
2445 }
2446 $fb->{pool}->clear;
Eric Wongef3cfaa2007-01-24 03:30:57 -08002447 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
Eric Wong9e3cdbd2007-02-09 12:23:47 -08002448 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
Eric Wong27a1a802006-11-27 21:44:48 -08002449 undef;
2450}
2451
2452sub abort_edit {
2453 my $self = shift;
Eric Wongef3cfaa2007-01-24 03:30:57 -08002454 $self->{nr} = $self->{gii}->{nr};
2455 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08002456 $self->SUPER::abort_edit(@_);
2457}
2458
2459sub close_edit {
2460 my $self = shift;
Eric Wongdad73c02006-11-28 14:06:05 -08002461 $self->{git_commit_ok} = 1;
Eric Wongef3cfaa2007-01-24 03:30:57 -08002462 $self->{nr} = $self->{gii}->{nr};
2463 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08002464 $self->SUPER::close_edit(@_);
2465}
Eric Wong1a82e792006-06-16 02:55:13 -07002466
Eric Wonga5e0ced2006-06-12 15:23:48 -07002467package SVN::Git::Editor;
Eric Wong24e22aa2007-01-29 00:07:49 -08002468use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002469use strict;
2470use warnings;
2471use Carp qw/croak/;
2472use IO::File;
Eric Wong90c1b152007-01-29 16:27:08 -08002473use Digest::MD5;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002474
2475sub new {
Eric Wong61395352007-01-27 14:33:08 -08002476 my ($class, $opts) = @_;
2477 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
2478 die "$_ required!\n" unless (defined $opts->{$_});
Eric Wonga5e0ced2006-06-12 15:23:48 -07002479 }
Eric Wong61395352007-01-27 14:33:08 -08002480
2481 my $pool = SVN::Pool->new;
2482 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
2483 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
2484 $opts->{r}, $mods);
2485
2486 # $opts->{ra} functions should not be used after this:
2487 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
2488 $opts->{editor_cb}, $pool);
2489 my $self = SVN::Delta::Editor->new(@ce, $pool);
2490 bless $self, $class;
2491 foreach (qw/svn_path r tree_a tree_b/) {
2492 $self->{$_} = $opts->{$_};
2493 }
2494 $self->{url} = $opts->{ra}->{url};
2495 $self->{mods} = $mods;
2496 $self->{types} = $types;
2497 $self->{pool} = $pool;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002498 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
2499 $self->{rm} = { };
Eric Wongd3a840d2007-01-26 01:32:45 -08002500 $self->{path_prefix} = length $self->{svn_path} ?
2501 "$self->{svn_path}/" : '';
Eric Wonga5e0ced2006-06-12 15:23:48 -07002502 return $self;
2503}
2504
Eric Wong61395352007-01-27 14:33:08 -08002505sub generate_diff {
2506 my ($tree_a, $tree_b) = @_;
2507 my @diff_tree = qw(diff-tree -z -r);
Eric Wong24e22aa2007-01-29 00:07:49 -08002508 if ($_cp_similarity) {
2509 push @diff_tree, "-C$_cp_similarity";
Eric Wong61395352007-01-27 14:33:08 -08002510 } else {
2511 push @diff_tree, '-C';
2512 }
Eric Wong24e22aa2007-01-29 00:07:49 -08002513 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
2514 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
Eric Wong61395352007-01-27 14:33:08 -08002515 push @diff_tree, $tree_a, $tree_b;
2516 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
2517 local $/ = "\0";
2518 my $state = 'meta';
2519 my @mods;
2520 while (<$diff_fh>) {
2521 chomp $_; # this gets rid of the trailing "\0"
2522 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
2523 $::sha1\s($::sha1)\s
2524 ([MTCRAD])\d*$/xo) {
2525 push @mods, { mode_a => $1, mode_b => $2,
2526 sha1_b => $3, chg => $4 };
2527 if ($4 =~ /^(?:C|R)$/) {
2528 $state = 'file_a';
2529 } else {
2530 $state = 'file_b';
2531 }
2532 } elsif ($state eq 'file_a') {
2533 my $x = $mods[$#mods] or croak "Empty array\n";
2534 if ($x->{chg} !~ /^(?:C|R)$/) {
2535 croak "Error parsing $_, $x->{chg}\n";
2536 }
2537 $x->{file_a} = $_;
2538 $state = 'file_b';
2539 } elsif ($state eq 'file_b') {
2540 my $x = $mods[$#mods] or croak "Empty array\n";
2541 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
2542 croak "Error parsing $_, $x->{chg}\n";
2543 }
2544 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
2545 croak "Error parsing $_, $x->{chg}\n";
2546 }
2547 $x->{file_b} = $_;
2548 $state = 'meta';
2549 } else {
2550 croak "Error parsing $_\n";
2551 }
2552 }
2553 command_close_pipe($diff_fh, $ctx);
2554 \@mods;
2555}
2556
2557sub check_diff_paths {
2558 my ($ra, $pfx, $rev, $mods) = @_;
2559 my %types;
2560 $pfx .= '/' if length $pfx;
2561
2562 sub type_diff_paths {
2563 my ($ra, $types, $path, $rev) = @_;
2564 my @p = split m#/+#, $path;
2565 my $c = shift @p;
2566 unless (defined $types->{$c}) {
2567 $types->{$c} = $ra->check_path($c, $rev);
2568 }
2569 while (@p) {
2570 $c .= '/' . shift @p;
2571 next if defined $types->{$c};
2572 $types->{$c} = $ra->check_path($c, $rev);
2573 }
2574 }
2575
2576 foreach my $m (@$mods) {
2577 foreach my $f (qw/file_a file_b/) {
2578 next unless defined $m->{$f};
2579 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
2580 if (length $pfx.$dir && ! defined $types{$dir}) {
2581 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
2582 }
2583 }
2584 }
2585 \%types;
2586}
2587
Eric Wonga5e0ced2006-06-12 15:23:48 -07002588sub split_path {
2589 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
2590}
2591
2592sub repo_path {
Eric Wongd3a840d2007-01-26 01:32:45 -08002593 my ($self, $path) = @_;
2594 $self->{path_prefix}.(defined $path ? $path : '');
Eric Wonga5e0ced2006-06-12 15:23:48 -07002595}
2596
2597sub url_path {
2598 my ($self, $path) = @_;
Eric Wong6e8548c2007-01-27 01:32:00 -08002599 $self->{url} . '/' . $self->repo_path($path);
Eric Wonga5e0ced2006-06-12 15:23:48 -07002600}
2601
2602sub rmdirs {
Eric Wong61395352007-01-27 14:33:08 -08002603 my ($self) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002604 my $rm = $self->{rm};
2605 delete $rm->{''}; # we never delete the url we're tracking
2606 return unless %$rm;
2607
2608 foreach (keys %$rm) {
2609 my @d = split m#/#, $_;
2610 my $c = shift @d;
2611 $rm->{$c} = 1;
2612 while (@d) {
2613 $c .= '/' . shift @d;
2614 $rm->{$c} = 1;
2615 }
2616 }
2617 delete $rm->{$self->{svn_path}};
2618 delete $rm->{''}; # we never delete the url we're tracking
2619 return unless %$rm;
2620
Eric Wong61395352007-01-27 14:33:08 -08002621 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
2622 $self->{tree_b});
Eric Wonga5e0ced2006-06-12 15:23:48 -07002623 local $/ = "\0";
2624 while (<$fh>) {
2625 chomp;
Eric Wong747fa122006-11-24 22:38:17 -08002626 my @dn = split m#/#, $_;
Eric Wongc07eee12006-06-19 17:59:35 -07002627 while (pop @dn) {
2628 delete $rm->{join '/', @dn};
2629 }
2630 unless (%$rm) {
Eric Wong22600a22007-02-01 13:12:26 -08002631 close $fh;
Eric Wongc07eee12006-06-19 17:59:35 -07002632 return;
2633 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07002634 }
Eric Wongaef4e922006-12-15 10:59:54 -08002635 command_close_pipe($fh, $ctx);
Eric Wongc07eee12006-06-19 17:59:35 -07002636
Eric Wonga5e0ced2006-06-12 15:23:48 -07002637 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
2638 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
2639 $self->close_directory($bat->{$d}, $p);
2640 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
Eric Wong44320b92007-01-13 22:35:53 -08002641 print "\tD+\t$d/\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002642 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
2643 delete $bat->{$d};
2644 }
2645}
2646
2647sub open_or_add_dir {
2648 my ($self, $full_path, $baton) = @_;
Eric Wong6e8548c2007-01-27 01:32:00 -08002649 my $t = $self->{types}->{$full_path};
2650 if (!defined $t) {
2651 die "$full_path not known in r$self->{r} or we have a bug!\n";
2652 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07002653 if ($t == $SVN::Node::none) {
2654 return $self->add_directory($full_path, $baton,
2655 undef, -1, $self->{pool});
2656 } elsif ($t == $SVN::Node::dir) {
2657 return $self->open_directory($full_path, $baton,
2658 $self->{r}, $self->{pool});
2659 }
2660 print STDERR "$full_path already exists in repository at ",
2661 "r$self->{r} and it is not a directory (",
2662 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
2663 exit 1;
2664}
2665
2666sub ensure_path {
2667 my ($self, $path) = @_;
2668 my $bat = $self->{bat};
Eric Wong6e8548c2007-01-27 01:32:00 -08002669 my $repo_path = $self->repo_path($path);
2670 return $bat->{''} unless (length $repo_path);
2671 my @p = split m#/+#, $repo_path;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002672 my $c = shift @p;
2673 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
2674 while (@p) {
2675 my $c0 = $c;
2676 $c .= '/' . shift @p;
2677 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
2678 }
2679 return $bat->{$c};
2680}
2681
2682sub A {
Eric Wong44320b92007-01-13 22:35:53 -08002683 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002684 my ($dir, $file) = split_path($m->{file_b});
2685 my $pbat = $self->ensure_path($dir);
2686 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2687 undef, -1);
Eric Wong44320b92007-01-13 22:35:53 -08002688 print "\tA\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002689 $self->chg_file($fbat, $m);
2690 $self->close_file($fbat,undef,$self->{pool});
2691}
2692
2693sub C {
Eric Wong44320b92007-01-13 22:35:53 -08002694 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002695 my ($dir, $file) = split_path($m->{file_b});
2696 my $pbat = $self->ensure_path($dir);
2697 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2698 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08002699 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002700 $self->chg_file($fbat, $m);
2701 $self->close_file($fbat,undef,$self->{pool});
2702}
2703
2704sub delete_entry {
2705 my ($self, $path, $pbat) = @_;
2706 my $rpath = $self->repo_path($path);
2707 my ($dir, $file) = split_path($rpath);
2708 $self->{rm}->{$dir} = 1;
2709 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
2710}
2711
2712sub R {
Eric Wong44320b92007-01-13 22:35:53 -08002713 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002714 my ($dir, $file) = split_path($m->{file_b});
2715 my $pbat = $self->ensure_path($dir);
2716 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
2717 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08002718 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002719 $self->chg_file($fbat, $m);
2720 $self->close_file($fbat,undef,$self->{pool});
2721
2722 ($dir, $file) = split_path($m->{file_a});
2723 $pbat = $self->ensure_path($dir);
2724 $self->delete_entry($m->{file_a}, $pbat);
2725}
2726
2727sub M {
Eric Wong44320b92007-01-13 22:35:53 -08002728 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002729 my ($dir, $file) = split_path($m->{file_b});
2730 my $pbat = $self->ensure_path($dir);
2731 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
2732 $pbat,$self->{r},$self->{pool});
Eric Wong44320b92007-01-13 22:35:53 -08002733 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002734 $self->chg_file($fbat, $m);
2735 $self->close_file($fbat,undef,$self->{pool});
2736}
2737
2738sub T { shift->M(@_) }
2739
2740sub change_file_prop {
2741 my ($self, $fbat, $pname, $pval) = @_;
2742 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
2743}
2744
2745sub chg_file {
2746 my ($self, $fbat, $m) = @_;
2747 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
2748 $self->change_file_prop($fbat,'svn:executable','*');
2749 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
2750 $self->change_file_prop($fbat,'svn:executable',undef);
2751 }
2752 my $fh = IO::File->new_tmpfile or croak $!;
2753 if ($m->{mode_b} =~ /^120/) {
2754 print $fh 'link ' or croak $!;
2755 $self->change_file_prop($fbat,'svn:special','*');
2756 } elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
2757 $self->change_file_prop($fbat,'svn:special',undef);
2758 }
2759 defined(my $pid = fork) or croak $!;
2760 if (!$pid) {
2761 open STDOUT, '>&', $fh or croak $!;
2762 exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
2763 }
2764 waitpid $pid, 0;
2765 croak $? if $?;
2766 $fh->flush == 0 or croak $!;
2767 seek $fh, 0, 0 or croak $!;
2768
2769 my $md5 = Digest::MD5->new;
2770 $md5->addfile($fh) or croak $!;
2771 seek $fh, 0, 0 or croak $!;
2772
2773 my $exp = $md5->hexdigest;
Eric Wongf7197df2006-10-14 15:48:35 -07002774 my $pool = SVN::Pool->new;
2775 my $atd = $self->apply_textdelta($fbat, undef, $pool);
2776 my $got = SVN::TxDelta::send_stream($fh, @$atd, $pool);
Eric Wonga5e0ced2006-06-12 15:23:48 -07002777 die "Checksum mismatch\nexpected: $exp\ngot: $got\n" if ($got ne $exp);
Eric Wongf7197df2006-10-14 15:48:35 -07002778 $pool->clear;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002779
2780 close $fh or croak $!;
2781}
2782
2783sub D {
Eric Wong44320b92007-01-13 22:35:53 -08002784 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002785 my ($dir, $file) = split_path($m->{file_b});
2786 my $pbat = $self->ensure_path($dir);
Eric Wong44320b92007-01-13 22:35:53 -08002787 print "\tD\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07002788 $self->delete_entry($m->{file_b}, $pbat);
2789}
2790
2791sub close_edit {
2792 my ($self) = @_;
2793 my ($p,$bat) = ($self->{pool}, $self->{bat});
2794 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
2795 $self->close_directory($bat->{$_}, $p);
2796 }
2797 $self->SUPER::close_edit($p);
2798 $p->clear;
2799}
2800
2801sub abort_edit {
2802 my ($self) = @_;
2803 $self->SUPER::abort_edit($self->{pool});
Eric Wong61395352007-01-27 14:33:08 -08002804}
2805
2806sub DESTROY {
2807 my $self = shift;
2808 $self->SUPER::DESTROY(@_);
Eric Wonga5e0ced2006-06-12 15:23:48 -07002809 $self->{pool}->clear;
2810}
2811
Eric Wong44320b92007-01-13 22:35:53 -08002812# this drives the editor
2813sub apply_diff {
Eric Wong61395352007-01-27 14:33:08 -08002814 my ($self) = @_;
2815 my $mods = $self->{mods};
Eric Wong44320b92007-01-13 22:35:53 -08002816 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
Eric Wong6e8548c2007-01-27 01:32:00 -08002817 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
Eric Wong44320b92007-01-13 22:35:53 -08002818 my $f = $m->{chg};
2819 if (defined $o{$f}) {
2820 $self->$f($m);
2821 } else {
2822 fatal("Invalid change type: $f\n");
2823 }
2824 }
Eric Wong24e22aa2007-01-29 00:07:49 -08002825 $self->rmdirs if $_rmdir;
Eric Wong6e8548c2007-01-27 01:32:00 -08002826 if (@$mods == 0) {
Eric Wong44320b92007-01-13 22:35:53 -08002827 $self->abort_edit;
2828 } else {
2829 $self->close_edit;
2830 }
Eric Wong6e8548c2007-01-27 01:32:00 -08002831 return scalar @$mods;
Eric Wong44320b92007-01-13 22:35:53 -08002832}
2833
Eric Wongd81bf822007-01-10 01:22:38 -08002834package Git::SVN::Ra;
Eric Wong6af1db42007-02-14 16:04:10 -08002835use vars qw/@ISA $config_dir $_log_window_size/;
Eric Wongd81bf822007-01-10 01:22:38 -08002836use strict;
2837use warnings;
2838my ($can_do_switch);
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002839my $RA;
Eric Wongd81bf822007-01-10 01:22:38 -08002840
2841BEGIN {
2842 # enforce temporary pool usage for some simple functions
2843 my $e;
Eric Wong74a81222007-02-10 13:28:50 -08002844 foreach (qw/get_latest_revnum get_uuid get_repos_root/) {
Eric Wongd81bf822007-01-10 01:22:38 -08002845 $e .= "sub $_ {
2846 my \$self = shift;
2847 my \$pool = SVN::Pool->new;
2848 my \@ret = \$self->SUPER::$_(\@_,\$pool);
2849 \$pool->clear;
2850 wantarray ? \@ret : \$ret[0]; }\n";
2851 }
Eric Wong74a81222007-02-10 13:28:50 -08002852
2853 # get_dir needs $pool held in cache for dirents to work,
2854 # check_path is cacheable and rev_proplist is close enough
2855 # for our purposes.
2856 foreach (qw/check_path get_dir rev_proplist/) {
2857 $e .= "my \%${_}_cache; my \$${_}_rev = 0; sub $_ {
2858 my \$self = shift;
2859 my \$r = pop;
2860 my \$k = join(\"\\0\", \@_);
2861 if (my \$x = \$${_}_cache{\$r}->{\$k}) {
2862 return wantarray ? \@\$x : \$x->[0];
2863 }
2864 my \$pool = SVN::Pool->new;
2865 my \@ret = \$self->SUPER::$_(\@_, \$r, \$pool);
2866 if (\$r != \$${_}_rev) {
2867 \%${_}_cache = ( pool => [] );
2868 \$${_}_rev = \$r;
2869 }
2870 \$${_}_cache{\$r}->{\$k} = \\\@ret;
2871 push \@{\$${_}_cache{pool}}, \$pool;
2872 wantarray ? \@ret : \$ret[0]; }\n";
2873 }
2874 $e .= "\n1;";
2875 eval $e or die $@;
Eric Wongd81bf822007-01-10 01:22:38 -08002876}
2877
2878sub new {
2879 my ($class, $url) = @_;
Eric Wongf6f09872007-01-18 18:22:18 -08002880 $url =~ s!/+$!!;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002881 return $RA if ($RA && $RA->{url} eq $url);
Eric Wonge2c475d2007-02-23 01:57:40 -08002882 $RA->{pool}->clear if $RA;
Eric Wongf6f09872007-01-18 18:22:18 -08002883
Eric Wongd81bf822007-01-10 01:22:38 -08002884 SVN::_Core::svn_config_ensure($config_dir, undef);
2885 my ($baton, $callbacks) = SVN::Core::auth_open_helper([
2886 SVN::Client::get_simple_provider(),
2887 SVN::Client::get_ssl_server_trust_file_provider(),
2888 SVN::Client::get_simple_prompt_provider(
2889 \&Git::SVN::Prompt::simple, 2),
2890 SVN::Client::get_ssl_client_cert_prompt_provider(
2891 \&Git::SVN::Prompt::ssl_client_cert, 2),
2892 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
2893 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
2894 SVN::Client::get_username_provider(),
2895 SVN::Client::get_ssl_server_trust_prompt_provider(
2896 \&Git::SVN::Prompt::ssl_server_trust),
2897 SVN::Client::get_username_prompt_provider(
2898 \&Git::SVN::Prompt::username, 2),
2899 ]);
2900 my $config = SVN::Core::config_get_config($config_dir);
2901 my $self = SVN::Ra->new(url => $url, auth => $baton,
2902 config => $config,
2903 pool => SVN::Pool->new,
2904 auth_provider_callbacks => $callbacks);
2905 $self->{svn_path} = $url;
2906 $self->{repos_root} = $self->get_repos_root;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002907 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002908 $RA = bless $self, $class;
Eric Wongd81bf822007-01-10 01:22:38 -08002909}
2910
2911sub DESTROY {
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002912 # do not call the real DESTROY since we store ourselves in $RA
Eric Wongd81bf822007-01-10 01:22:38 -08002913}
2914
Eric Wongd81bf822007-01-10 01:22:38 -08002915sub get_log {
2916 my ($self, @args) = @_;
2917 my $pool = SVN::Pool->new;
Eric Wongd81bf822007-01-10 01:22:38 -08002918 splice(@args, 3, 1) if ($SVN::Core::VERSION le '1.2.0');
2919 my $ret = $self->SUPER::get_log(@args, $pool);
2920 $pool->clear;
2921 $ret;
2922}
2923
2924sub get_commit_editor {
Eric Wong44320b92007-01-13 22:35:53 -08002925 my ($self, $log, $cb, $pool) = @_;
Eric Wongd81bf822007-01-10 01:22:38 -08002926 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
Eric Wong44320b92007-01-13 22:35:53 -08002927 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08002928}
2929
Eric Wongd81bf822007-01-10 01:22:38 -08002930sub gs_do_update {
Eric Wong8a603772007-01-31 02:45:50 -08002931 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
2932 my $new = ($rev_a == $rev_b);
2933 my $path = $gs->{path};
2934
Eric Wongd81bf822007-01-10 01:22:38 -08002935 my $pool = SVN::Pool->new;
Eric Wong8b8fc062007-01-22 11:44:57 -08002936 $editor->set_path_strip($path);
Eric Wong2b27f6c2007-01-28 04:59:05 -08002937 my (@pc) = split m#/#, $path;
2938 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
Eric Wong8a603772007-01-31 02:45:50 -08002939 1, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08002940 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong2b27f6c2007-01-28 04:59:05 -08002941
2942 # Since we can't rely on svn_ra_reparent being available, we'll
2943 # just have to do some magic with set_path to make it so
2944 # we only want a partial path.
2945 my $sp = '';
2946 my $final = join('/', @pc);
2947 while (@pc) {
2948 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
2949 $sp .= '/' if length $sp;
2950 $sp .= shift @pc;
2951 }
2952 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
2953
Eric Wong2b27f6c2007-01-28 04:59:05 -08002954 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
2955
Eric Wongd81bf822007-01-10 01:22:38 -08002956 $reporter->finish_report($pool);
2957 $pool->clear;
2958 $editor->{git_commit_ok};
2959}
2960
Eric Wong2b27f6c2007-01-28 04:59:05 -08002961# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
2962# svn_ra_reparent didn't work before 1.4)
Eric Wongd81bf822007-01-10 01:22:38 -08002963sub gs_do_switch {
Eric Wong8a603772007-01-31 02:45:50 -08002964 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
2965 my $path = $gs->{path};
Eric Wongd81bf822007-01-10 01:22:38 -08002966 my $pool = SVN::Pool->new;
Eric Wong2b27f6c2007-01-28 04:59:05 -08002967
2968 my $full_url = $self->{url};
2969 my $old_url = $full_url;
2970 $full_url .= "/$path" if length $path;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002971 my ($ra, $reparented);
2972 if ($old_url ne $full_url) {
2973 if ($old_url !~ m#^svn(\+ssh)?://#) {
2974 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url,
2975 $pool);
2976 $self->{url} = $full_url;
2977 $reparented = 1;
2978 } else {
2979 $ra = Git::SVN::Ra->new($full_url);
2980 }
2981 }
2982 $ra ||= $self;
Eric Wong8a603772007-01-31 02:45:50 -08002983 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08002984 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong8b8fc062007-01-22 11:44:57 -08002985 $reporter->set_path('', $rev_a, 0, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08002986 $reporter->finish_report($pool);
Eric Wong2b27f6c2007-01-28 04:59:05 -08002987
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002988 if ($reparented) {
2989 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
2990 $self->{url} = $old_url;
2991 }
Eric Wong2b27f6c2007-01-28 04:59:05 -08002992
Eric Wongd81bf822007-01-10 01:22:38 -08002993 $pool->clear;
2994 $editor->{git_commit_ok};
2995}
2996
Eric Wong0af9c9f2007-01-27 22:28:56 -08002997sub gs_fetch_loop_common {
Eric Wonge5181922007-02-08 12:53:57 -08002998 my ($self, $base, $head, $gsv, $globs) = @_;
2999 return if ($base > $head);
Eric Wong6af1db42007-02-14 16:04:10 -08003000 my $inc = $_log_window_size;
Eric Wong0af9c9f2007-01-27 22:28:56 -08003001 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
Eric Wongd2ae1432007-02-07 11:50:16 -08003002 my %common;
Eric Wonge5181922007-02-08 12:53:57 -08003003 my $common_max = scalar @$gsv;
3004
3005 foreach my $gs (@$gsv) {
Eric Wongd2ae1432007-02-07 11:50:16 -08003006 my @tmp = split m#/#, $gs->{path};
3007 my $p = '';
3008 foreach (@tmp) {
3009 $p .= length($p) ? "/$_" : $_;
3010 $common{$p} ||= 0;
3011 $common{$p}++;
3012 }
3013 }
Eric Wonge5181922007-02-08 12:53:57 -08003014 $globs ||= [];
3015 $common_max += scalar @$globs;
3016 foreach my $glob (@$globs) {
3017 my @tmp = split m#/#, $glob->{path}->{left};
3018 my $p = '';
3019 foreach (@tmp) {
3020 $p .= length($p) ? "/$_" : $_;
3021 $common{$p} ||= 0;
3022 $common{$p}++;
3023 }
3024 }
3025
Eric Wongd2ae1432007-02-07 11:50:16 -08003026 my $longest_path = '';
3027 foreach (sort {length $b <=> length $a} keys %common) {
Eric Wonge5181922007-02-08 12:53:57 -08003028 if ($common{$_} == $common_max) {
Eric Wongd2ae1432007-02-07 11:50:16 -08003029 $longest_path = $_;
3030 last;
3031 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08003032 }
3033 while (1) {
Eric Wongd4eff2b2007-01-30 14:04:22 -08003034 my %revs;
Eric Wongd2ae1432007-02-07 11:50:16 -08003035 my $err;
Eric Wongf7c3fc42007-01-29 18:34:55 -08003036 my $err_handler = $SVN::Error::handler;
Eric Wongd2ae1432007-02-07 11:50:16 -08003037 $SVN::Error::handler = sub {
3038 ($err) = @_;
3039 skip_unknown_revs($err);
3040 };
3041 sub _cb {
3042 my ($paths, $r, $author, $date, $log) = @_;
3043 [ dup_changed_paths($paths),
3044 { author => $author, date => $date, log => $log } ];
3045 }
3046 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
3047 sub { $revs{$_[1]} = _cb(@_) });
3048 if ($err && $max >= $head) {
3049 print STDERR "Path '$longest_path' ",
3050 "was probably deleted:\n",
3051 $err->expanded_message,
3052 "\nWill attempt to follow ",
3053 "revisions r$min .. r$max ",
3054 "committed before the deletion\n";
3055 my $hi = $max;
3056 while (--$hi >= $min) {
3057 my $ok;
3058 $self->get_log([$longest_path], $min, $hi,
3059 0, 1, 1, sub {
3060 $ok ||= $_[1];
3061 $revs{$_[1]} = _cb(@_) });
3062 if ($ok) {
3063 print STDERR "r$min .. r$ok OK\n";
3064 last;
3065 }
3066 }
3067 }
Eric Wongd4eff2b2007-01-30 14:04:22 -08003068 $SVN::Error::handler = $err_handler;
Eric Wongfbcc1732007-02-06 18:35:30 -08003069
Eric Wonge5181922007-02-08 12:53:57 -08003070 my %exists = map { $_->{path} => $_ } @$gsv;
Eric Wongd4eff2b2007-01-30 14:04:22 -08003071 foreach my $r (sort {$a <=> $b} keys %revs) {
Eric Wongfbcc1732007-02-06 18:35:30 -08003072 my ($paths, $logged) = @{$revs{$r}};
Eric Wonge5181922007-02-08 12:53:57 -08003073
3074 foreach my $gs ($self->match_globs(\%exists, $paths,
3075 $globs, $r)) {
Eric Wongfbcc1732007-02-06 18:35:30 -08003076 if ($gs->rev_db_max >= $r) {
3077 next;
3078 }
3079 next unless $gs->match_paths($paths, $r);
3080 $gs->{logged_rev_props} = $logged;
Eric Wonge8d120b2007-02-14 16:29:52 -08003081 if (my $last_commit = $gs->last_commit) {
3082 $gs->assert_index_clean($last_commit);
3083 }
Eric Wongfbcc1732007-02-06 18:35:30 -08003084 my $log_entry = $gs->do_fetch($paths, $r);
3085 if ($log_entry) {
Eric Wong0af9c9f2007-01-27 22:28:56 -08003086 $gs->do_git_commit($log_entry);
3087 }
3088 }
Eric Wonge5181922007-02-08 12:53:57 -08003089 foreach my $g (@$globs) {
Eric Wong93f26892007-02-11 01:20:26 -08003090 my $k = "svn-remote.$g->{remote}." .
3091 "$g->{t}-maxRev";
3092 Git::SVN::tmp_config($k, $r);
Eric Wonge5181922007-02-08 12:53:57 -08003093 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08003094 }
Eric Wong9c93fee2007-01-31 17:22:31 -08003095 # pre-fill the .rev_db since it'll eventually get filled in
3096 # with '0' x40 if something new gets committed
Eric Wonge5181922007-02-08 12:53:57 -08003097 foreach my $gs (@$gsv) {
Eric Wong9c93fee2007-01-31 17:22:31 -08003098 next if defined $gs->rev_db_get($max);
3099 $gs->rev_db_set($max, 0 x40);
3100 }
Eric Wongc3560e52007-02-12 16:03:32 -08003101 foreach my $g (@$globs) {
3102 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
3103 Git::SVN::tmp_config($k, $max);
3104 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08003105 last if $max >= $head;
3106 $min = $max + 1;
3107 $max += $inc;
3108 $max = $head if ($max > $head);
3109 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08003110}
3111
Eric Wonge5181922007-02-08 12:53:57 -08003112sub match_globs {
3113 my ($self, $exists, $paths, $globs, $r) = @_;
Eric Wong74a81222007-02-10 13:28:50 -08003114
3115 sub get_dir_check {
3116 my ($self, $exists, $g, $r) = @_;
3117 my @x = eval { $self->get_dir($g->{path}->{left}, $r) };
3118 return unless scalar @x == 3;
3119 my $dirents = $x[0];
3120 foreach my $de (keys %$dirents) {
3121 next if $dirents->{$de}->kind != $SVN::Node::dir;
3122 my $p = $g->{path}->full_path($de);
3123 next if $exists->{$p};
3124 next if (length $g->{path}->{right} &&
3125 ($self->check_path($p, $r) !=
3126 $SVN::Node::dir));
3127 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
3128 $g->{ref}->full_path($de), 1);
3129 }
3130 }
Eric Wonge5181922007-02-08 12:53:57 -08003131 foreach my $g (@$globs) {
Eric Wong74a81222007-02-10 13:28:50 -08003132 if (my $path = $paths->{"/$g->{path}->{left}"}) {
3133 if ($path->{action} =~ /^[AR]$/) {
3134 get_dir_check($self, $exists, $g, $r);
3135 }
3136 }
Eric Wonge5181922007-02-08 12:53:57 -08003137 foreach (keys %$paths) {
Eric Wong28710f72007-02-14 13:32:21 -08003138 if (/$g->{path}->{left_regex}/ &&
3139 !/$g->{path}->{regex}/) {
Eric Wong74a81222007-02-10 13:28:50 -08003140 next if $paths->{$_}->{action} !~ /^[AR]$/;
3141 get_dir_check($self, $exists, $g, $r);
3142 }
Eric Wonge5181922007-02-08 12:53:57 -08003143 next unless /$g->{path}->{regex}/;
3144 my $p = $1;
3145 my $pathname = $g->{path}->full_path($p);
3146 next if $exists->{$pathname};
3147 $exists->{$pathname} = Git::SVN->init(
3148 $self->{url}, $pathname, undef,
3149 $g->{ref}->full_path($p), 1);
3150 }
3151 my $c = '';
3152 foreach (split m#/#, $g->{path}->{left}) {
3153 $c .= "/$_";
3154 next unless ($paths->{$c} &&
Eric Wong74a81222007-02-10 13:28:50 -08003155 ($paths->{$c}->{action} =~ /^[AR]$/));
3156 get_dir_check($self, $exists, $g, $r);
Eric Wonge5181922007-02-08 12:53:57 -08003157 }
3158 }
3159 values %$exists;
3160}
3161
Eric Wonge6434f82007-01-23 16:29:23 -08003162sub minimize_url {
3163 my ($self) = @_;
3164 return $self->{url} if ($self->{url} eq $self->{repos_root});
3165 my $url = $self->{repos_root};
3166 my @components = split(m!/!, $self->{svn_path});
3167 my $c = '';
3168 do {
3169 $url .= "/$c" if length $c;
3170 eval { (ref $self)->new($url)->get_latest_revnum };
3171 } while ($@ && ($c = shift @components));
3172 $url;
3173}
3174
Eric Wongd81bf822007-01-10 01:22:38 -08003175sub can_do_switch {
3176 my $self = shift;
3177 unless (defined $can_do_switch) {
3178 my $pool = SVN::Pool->new;
3179 my $rep = eval {
3180 $self->do_switch(1, '', 0, $self->{url},
3181 SVN::Delta::Editor->new, $pool);
3182 };
3183 if ($@) {
3184 $can_do_switch = 0;
3185 } else {
3186 $rep->abort_report($pool);
3187 $can_do_switch = 1;
3188 }
3189 $pool->clear;
3190 }
3191 $can_do_switch;
3192}
3193
Eric Wong0af9c9f2007-01-27 22:28:56 -08003194sub skip_unknown_revs {
3195 my ($err) = @_;
3196 my $errno = $err->apr_err();
3197 # Maybe the branch we're tracking didn't
3198 # exist when the repo started, so it's
3199 # not an error if it doesn't, just continue
3200 #
3201 # Wonderfully consistent library, eh?
3202 # 160013 - svn:// and file://
3203 # 175002 - http(s)://
3204 # 175007 - http(s):// (this repo required authorization, too...)
3205 # More codes may be discovered later...
3206 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
Eric Wong6af1db42007-02-14 16:04:10 -08003207 warn "W: Ignoring error from SVN, path probably ",
3208 "does not exist: ($errno): ",
3209 $err->expanded_message,"\n";
Eric Wong0af9c9f2007-01-27 22:28:56 -08003210 return;
3211 }
3212 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
3213}
3214
3215# svn_log_changed_path_t objects passed to get_log are likely to be
3216# overwritten even if only the refs are copied to an external variable,
3217# so we should dup the structures in their entirety. Using an externally
3218# passed pool (instead of our temporary and quickly cleared pool in
3219# Git::SVN::Ra) does not help matters at all...
3220sub dup_changed_paths {
3221 my ($paths) = @_;
3222 return undef unless $paths;
3223 my %ret;
3224 foreach my $p (keys %$paths) {
3225 my $i = $paths->{$p};
3226 my %s = map { $_ => $i->$_ }
3227 qw/copyfrom_path copyfrom_rev action/;
3228 $ret{$p} = \%s;
3229 }
3230 \%ret;
3231}
3232
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003233package Git::SVN::Log;
3234use strict;
3235use warnings;
3236use POSIX qw/strftime/;
3237use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
3238 %rusers $show_commit $incremental/;
3239my $l_fmt;
3240
3241sub cmt_showable {
3242 my ($c) = @_;
3243 return 1 if defined $c->{r};
3244 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
3245 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
Eric Wong44320b92007-01-13 22:35:53 -08003246 my @log = command(qw/cat-file commit/, $c->{c});
3247 shift @log while ($log[0] ne "\n");
3248 shift @log;
3249 @{$c->{l}} = grep !/^git-svn-id: /, @log;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003250
3251 (undef, $c->{r}, undef) = ::extract_metadata(
Eric Wong44320b92007-01-13 22:35:53 -08003252 (grep(/^git-svn-id: /, @log))[-1]);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003253 }
3254 return defined $c->{r};
3255}
3256
3257sub log_use_color {
3258 return 1 if $color;
3259 my ($dc, $dcvar);
3260 $dcvar = 'color.diff';
3261 $dc = `git-config --get $dcvar`;
3262 if ($dc eq '') {
3263 # nothing at all; fallback to "diff.color"
3264 $dcvar = 'diff.color';
3265 $dc = `git-config --get $dcvar`;
3266 }
3267 chomp($dc);
3268 if ($dc eq 'auto') {
3269 my $pc;
3270 $pc = `git-config --get color.pager`;
3271 if ($pc eq '') {
3272 # does not have it -- fallback to pager.color
3273 $pc = `git-config --bool --get pager.color`;
3274 }
3275 else {
3276 $pc = `git-config --bool --get color.pager`;
3277 if ($?) {
3278 $pc = 'false';
3279 }
3280 }
3281 chomp($pc);
3282 if (-t *STDOUT || (defined $pager && $pc eq 'true')) {
3283 return ($ENV{TERM} && $ENV{TERM} ne 'dumb');
3284 }
3285 return 0;
3286 }
3287 return 0 if $dc eq 'never';
3288 return 1 if $dc eq 'always';
3289 chomp($dc = `git-config --bool --get $dcvar`);
3290 return ($dc eq 'true');
3291}
3292
3293sub git_svn_log_cmd {
Eric Wong3bc718b2007-02-13 17:09:40 -08003294 my ($r_min, $r_max, @args) = @_;
3295 my $head = 'HEAD';
3296 foreach my $x (@args) {
3297 last if $x eq '--';
3298 next unless ::verify_ref("$x^0");
3299 $head = $x;
3300 last;
3301 }
3302
Eric Wong905f8b72007-02-16 03:22:40 -08003303 my $url = (::working_head_info($head))[0];
Eric Wong3bc718b2007-02-13 17:09:40 -08003304 my $gs = Git::SVN->find_by_url($url) || Git::SVN->_new;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003305 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
3306 $gs->refname);
3307 push @cmd, '-r' unless $non_recursive;
3308 push @cmd, qw/--raw --name-status/ if $verbose;
3309 push @cmd, '--color' if log_use_color();
3310 return @cmd unless defined $r_max;
3311 if ($r_max == $r_min) {
3312 push @cmd, '--max-count=1';
3313 if (my $c = $gs->rev_db_get($r_max)) {
3314 push @cmd, $c;
3315 }
3316 } else {
3317 my ($c_min, $c_max);
3318 $c_max = $gs->rev_db_get($r_max);
3319 $c_min = $gs->rev_db_get($r_min);
3320 if (defined $c_min && defined $c_max) {
3321 if ($r_max > $r_max) {
3322 push @cmd, "$c_min..$c_max";
3323 } else {
3324 push @cmd, "$c_max..$c_min";
3325 }
3326 } elsif ($r_max > $r_min) {
3327 push @cmd, $c_max;
3328 } else {
3329 push @cmd, $c_min;
3330 }
3331 }
3332 return @cmd;
3333}
3334
3335# adapted from pager.c
3336sub config_pager {
3337 $pager ||= $ENV{GIT_PAGER} || $ENV{PAGER};
3338 if (!defined $pager) {
3339 $pager = 'less';
3340 } elsif (length $pager == 0 || $pager eq 'cat') {
3341 $pager = undef;
3342 }
3343}
3344
3345sub run_pager {
3346 return unless -t *STDOUT;
3347 pipe my $rfd, my $wfd or return;
3348 defined(my $pid = fork) or ::fatal "Can't fork: $!\n";
3349 if (!$pid) {
3350 open STDOUT, '>&', $wfd or
3351 ::fatal "Can't redirect to stdout: $!\n";
3352 return;
3353 }
3354 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!\n";
3355 $ENV{LESS} ||= 'FRSX';
3356 exec $pager or ::fatal "Can't run pager: $! ($pager)\n";
3357}
3358
Eric Wong21819a32007-01-27 14:38:10 -08003359sub tz_to_s_offset {
3360 my ($tz) = @_;
3361 $tz =~ s/(\d\d)$//;
3362 return ($1 * 60) + ($tz * 3600);
3363}
3364
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003365sub get_author_info {
3366 my ($dest, $author, $t, $tz) = @_;
3367 $author =~ s/(?:^\s*|\s*$)//g;
3368 $dest->{a_raw} = $author;
3369 my $au;
Eric Wong1c8443b2007-01-14 02:17:00 -08003370 if ($::_authors) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003371 $au = $rusers{$author} || undef;
3372 }
3373 if (!$au) {
3374 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
3375 }
3376 $dest->{t} = $t;
3377 $dest->{tz} = $tz;
3378 $dest->{a} = $au;
3379 # Date::Parse isn't in the standard Perl distro :(
3380 if ($tz =~ s/^\+//) {
Eric Wong21819a32007-01-27 14:38:10 -08003381 $t += tz_to_s_offset($tz);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003382 } elsif ($tz =~ s/^\-//) {
Eric Wong21819a32007-01-27 14:38:10 -08003383 $t -= tz_to_s_offset($tz);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003384 }
3385 $dest->{t_utc} = $t;
3386}
3387
3388sub process_commit {
3389 my ($c, $r_min, $r_max, $defer) = @_;
3390 if (defined $r_min && defined $r_max) {
3391 if ($r_min == $c->{r} && $r_min == $r_max) {
3392 show_commit($c);
3393 return 0;
3394 }
3395 return 1 if $r_min == $r_max;
3396 if ($r_min < $r_max) {
3397 # we need to reverse the print order
3398 return 0 if (defined $limit && --$limit < 0);
3399 push @$defer, $c;
3400 return 1;
3401 }
3402 if ($r_min != $r_max) {
3403 return 1 if ($r_min < $c->{r});
3404 return 1 if ($r_max > $c->{r});
3405 }
3406 }
3407 return 0 if (defined $limit && --$limit < 0);
3408 show_commit($c);
3409 return 1;
3410}
3411
3412sub show_commit {
3413 my $c = shift;
3414 if ($oneline) {
3415 my $x = "\n";
3416 if (my $l = $c->{l}) {
3417 while ($l->[0] =~ /^\s*$/) { shift @$l }
3418 $x = $l->[0];
3419 }
3420 $l_fmt ||= 'A' . length($c->{r});
3421 print 'r',pack($l_fmt, $c->{r}),' | ';
3422 print "$c->{c} | " if $show_commit;
3423 print $x;
3424 } else {
3425 show_commit_normal($c);
3426 }
3427}
3428
3429sub show_commit_changed_paths {
3430 my ($c) = @_;
3431 return unless $c->{changed};
3432 print "Changed paths:\n", @{$c->{changed}};
3433}
3434
3435sub show_commit_normal {
3436 my ($c) = @_;
3437 print '-' x72, "\nr$c->{r} | ";
3438 print "$c->{c} | " if $show_commit;
3439 print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
3440 localtime($c->{t_utc})), ' | ';
3441 my $nr_line = 0;
3442
3443 if (my $l = $c->{l}) {
3444 while ($l->[$#$l] eq "\n" && $#$l > 0
3445 && $l->[($#$l - 1)] eq "\n") {
3446 pop @$l;
3447 }
3448 $nr_line = scalar @$l;
3449 if (!$nr_line) {
3450 print "1 line\n\n\n";
3451 } else {
3452 if ($nr_line == 1) {
3453 $nr_line = '1 line';
3454 } else {
3455 $nr_line .= ' lines';
3456 }
3457 print $nr_line, "\n";
3458 show_commit_changed_paths($c);
3459 print "\n";
3460 print $_ foreach @$l;
3461 }
3462 } else {
3463 print "1 line\n";
3464 show_commit_changed_paths($c);
3465 print "\n";
3466
3467 }
Eric Wong488a63e2007-02-15 00:40:42 -08003468 foreach my $x (qw/raw stat diff/) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003469 if ($c->{$x}) {
3470 print "\n";
3471 print $_ foreach @{$c->{$x}}
3472 }
3473 }
3474}
3475
3476sub cmd_show_log {
3477 my (@args) = @_;
3478 my ($r_min, $r_max);
3479 my $r_last = -1; # prevent dupes
3480 if (defined $TZ) {
3481 $ENV{TZ} = $TZ;
3482 } else {
3483 delete $ENV{TZ};
3484 }
3485 if (defined $::_revision) {
3486 if ($::_revision =~ /^(\d+):(\d+)$/) {
3487 ($r_min, $r_max) = ($1, $2);
3488 } elsif ($::_revision =~ /^\d+$/) {
3489 $r_min = $r_max = $::_revision;
3490 } else {
3491 ::fatal "-r$::_revision is not supported, use ",
3492 "standard \'git log\' arguments instead\n";
3493 }
3494 }
3495
3496 config_pager();
Eric Wong3bc718b2007-02-13 17:09:40 -08003497 @args = (git_svn_log_cmd($r_min, $r_max, @args), @args);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003498 my $log = command_output_pipe(@args);
3499 run_pager();
Eric Wong488a63e2007-02-15 00:40:42 -08003500 my (@k, $c, $d, $stat);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003501 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
3502 while (<$log>) {
3503 if (/^${esc_color}commit ($::sha1_short)/o) {
3504 my $cmt = $1;
3505 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
3506 $r_last = $c->{r};
3507 process_commit($c, $r_min, $r_max, \@k) or
3508 goto out;
3509 }
3510 $d = undef;
3511 $c = { c => $cmt };
3512 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
3513 get_author_info($c, $1, $2, $3);
3514 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
3515 # ignore
3516 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
3517 push @{$c->{raw}}, $_;
3518 } elsif (/^${esc_color}[ACRMDT]\t/) {
3519 # we could add $SVN->{svn_path} here, but that requires
3520 # remote access at the moment (repo_path_split)...
3521 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
3522 push @{$c->{changed}}, $_;
3523 } elsif (/^${esc_color}diff /o) {
3524 $d = 1;
3525 push @{$c->{diff}}, $_;
3526 } elsif ($d) {
3527 push @{$c->{diff}}, $_;
Eric Wong488a63e2007-02-15 00:40:42 -08003528 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
3529 $esc_color*[\+\-]*$esc_color$/x) {
3530 $stat = 1;
3531 push @{$c->{stat}}, $_;
3532 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
3533 push @{$c->{stat}}, $_;
3534 $stat = undef;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003535 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
3536 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
3537 } elsif (s/^${esc_color} //o) {
3538 push @{$c->{l}}, $_;
3539 }
3540 }
3541 if ($c && defined $c->{r} && $c->{r} != $r_last) {
3542 $r_last = $c->{r};
3543 process_commit($c, $r_min, $r_max, \@k);
3544 }
3545 if (@k) {
3546 my $swap = $r_max;
3547 $r_max = $r_min;
3548 $r_min = $swap;
3549 process_commit($_, $r_min, $r_max) foreach reverse @k;
3550 }
3551out:
Eric Wongc843c462007-01-12 03:07:31 -08003552 close $log;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08003553 print '-' x72,"\n" unless $incremental || $oneline;
3554}
3555
Eric Wong706587f2007-01-18 17:50:01 -08003556package Git::SVN::Migration;
3557# these version numbers do NOT correspond to actual version numbers
3558# of git nor git-svn. They are just relative.
3559#
3560# v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
3561#
3562# v1 layout: .git/$id/info/url, refs/remotes/$id
3563#
3564# v2 layout: .git/svn/$id/info/url, refs/remotes/$id
3565#
3566# v3 layout: .git/svn/$id, refs/remotes/$id
3567# - info/url may remain for backwards compatibility
3568# - this is what we migrate up to this layout automatically,
3569# - this will be used by git svn init on single branches
Eric Wong26a62d52007-02-12 13:25:25 -08003570# v3.1 layout (auto migrated):
3571# - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
3572# for backwards compatibility
Eric Wong706587f2007-01-18 17:50:01 -08003573#
3574# v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
3575# - this is only created for newly multi-init-ed
3576# repositories. Similar in spirit to the
3577# --use-separate-remotes option in git-clone (now default)
3578# - we do not automatically migrate to this (following
3579# the example set by core git)
3580use strict;
3581use warnings;
3582use Carp qw/croak/;
3583use File::Path qw/mkpath/;
Eric Wong47e39c52007-01-21 04:27:09 -08003584use File::Basename qw/dirname basename/;
3585use vars qw/$_minimize/;
Eric Wong706587f2007-01-18 17:50:01 -08003586
3587sub migrate_from_v0 {
3588 my $git_dir = $ENV{GIT_DIR};
3589 return undef unless -d $git_dir;
3590 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
3591 my $migrated = 0;
3592 while (<$fh>) {
3593 chomp;
3594 my ($id, $orig_ref) = ($_, $_);
3595 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
3596 next unless -f "$git_dir/$id/info/url";
3597 my $new_ref = "refs/remotes/$id";
3598 if (::verify_ref("$new_ref^0")) {
3599 print STDERR "W: $orig_ref is probably an old ",
3600 "branch used by an ancient version of ",
3601 "git-svn.\n",
3602 "However, $new_ref also exists.\n",
3603 "We will not be able ",
3604 "to use this branch until this ",
3605 "ambiguity is resolved.\n";
3606 next;
3607 }
3608 print STDERR "Migrating from v0 layout...\n" if !$migrated;
3609 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
3610 command_noisy('update-ref', $new_ref, $orig_ref);
3611 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
3612 $migrated++;
3613 }
3614 command_close_pipe($fh, $ctx);
3615 print STDERR "Done migrating from v0 layout...\n" if $migrated;
3616 $migrated;
3617}
3618
3619sub migrate_from_v1 {
3620 my $git_dir = $ENV{GIT_DIR};
3621 my $migrated = 0;
3622 return $migrated unless -d $git_dir;
3623 my $svn_dir = "$git_dir/svn";
3624
3625 # just in case somebody used 'svn' as their $id at some point...
3626 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
3627
3628 print STDERR "Migrating from a git-svn v1 layout...\n";
3629 mkpath([$svn_dir]);
3630 print STDERR "Data from a previous version of git-svn exists, but\n\t",
3631 "$svn_dir\n\t(required for this version ",
3632 "($::VERSION) of git-svn) does not. exist\n";
3633 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
3634 while (<$fh>) {
3635 my $x = $_;
3636 next unless $x =~ s#^refs/remotes/##;
3637 chomp $x;
3638 next unless -f "$git_dir/$x/info/url";
3639 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
3640 next unless $u;
3641 my $dn = dirname("$git_dir/svn/$x");
3642 mkpath([$dn]) unless -d $dn;
3643 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
3644 mkpath(["$git_dir/svn/svn"]);
3645 print STDERR " - $git_dir/$x/info => ",
3646 "$git_dir/svn/$x/info\n";
3647 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
3648 croak "$!: $x";
3649 # don't worry too much about these, they probably
3650 # don't exist with repos this old (save for index,
3651 # and we can easily regenerate that)
3652 foreach my $f (qw/unhandled.log index .rev_db/) {
3653 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
3654 }
3655 } else {
3656 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
3657 rename "$git_dir/$x", "$git_dir/svn/$x" or
3658 croak "$!: $x";
3659 }
3660 $migrated++;
3661 }
3662 command_close_pipe($fh, $ctx);
3663 print STDERR "Done migrating from a git-svn v1 layout\n";
3664 $migrated;
3665}
3666
3667sub read_old_urls {
3668 my ($l_map, $pfx, $path) = @_;
3669 my @dir;
3670 foreach (<$path/*>) {
3671 if (-r "$_/info/url") {
3672 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
3673 my $ref_id = $pfx . basename $_;
3674 my $url = ::file_to_s("$_/info/url");
3675 $l_map->{$ref_id} = $url;
3676 } elsif (-d $_) {
3677 push @dir, $_;
3678 }
3679 }
3680 foreach (@dir) {
3681 my $x = $_;
3682 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
3683 read_old_urls($l_map, $x, $_);
3684 }
3685}
3686
3687sub migrate_from_v2 {
3688 my @cfg = command(qw/config -l/);
3689 return if grep /^svn-remote\..+\.url=/, @cfg;
3690 my %l_map;
3691 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
3692 my $migrated = 0;
3693
3694 foreach my $ref_id (sort keys %l_map) {
Eric Wong471bc002007-02-01 04:06:27 -08003695 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
3696 if ($@) {
3697 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
3698 }
Eric Wong706587f2007-01-18 17:50:01 -08003699 $migrated++;
3700 }
3701 $migrated;
3702}
3703
Eric Wong47e39c52007-01-21 04:27:09 -08003704sub minimize_connections {
3705 my $r = Git::SVN::read_all_remotes();
3706 my $new_urls = {};
3707 my $root_repos = {};
3708 foreach my $repo_id (keys %$r) {
3709 my $url = $r->{$repo_id}->{url} or next;
3710 my $fetch = $r->{$repo_id}->{fetch} or next;
3711 my $ra = Git::SVN::Ra->new($url);
3712
3713 # skip existing cases where we already connect to the root
3714 if (($ra->{url} eq $ra->{repos_root}) ||
3715 (Git::SVN::sanitize_remote_name($ra->{repos_root}) eq
3716 $repo_id)) {
3717 $root_repos->{$ra->{url}} = $repo_id;
3718 next;
3719 }
3720
3721 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
3722 my $root_path = $ra->{url};
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003723 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
Eric Wong47e39c52007-01-21 04:27:09 -08003724 foreach my $path (keys %$fetch) {
3725 my $ref_id = $fetch->{$path};
3726 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
3727
3728 # make sure we can read when connecting to
3729 # a higher level of a repository
3730 my ($last_rev, undef) = $gs->last_rev_commit;
3731 if (!defined $last_rev) {
3732 $last_rev = eval {
3733 $root_ra->get_latest_revnum;
3734 };
3735 next if $@;
3736 }
3737 my $new = $root_path;
3738 $new .= length $path ? "/$path" : '';
3739 eval {
3740 $root_ra->get_log([$new], $last_rev, $last_rev,
3741 0, 0, 1, sub { });
3742 };
3743 next if $@;
3744 $new_urls->{$ra->{repos_root}}->{$new} =
3745 { ref_id => $ref_id,
3746 old_repo_id => $repo_id,
3747 old_path => $path };
3748 }
3749 }
3750
3751 my @emptied;
3752 foreach my $url (keys %$new_urls) {
3753 # see if we can re-use an existing [svn-remote "repo_id"]
3754 # instead of creating a(n ugly) new section:
3755 my $repo_id = $root_repos->{$url} ||
3756 Git::SVN::sanitize_remote_name($url);
3757
3758 my $fetch = $new_urls->{$url};
3759 foreach my $path (keys %$fetch) {
3760 my $x = $fetch->{$path};
3761 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
3762 my $pfx = "svn-remote.$x->{old_repo_id}";
3763
3764 my $old_fetch = quotemeta("$x->{old_path}:".
3765 "refs/remotes/$x->{ref_id}");
Eric Wong8b8fc062007-01-22 11:44:57 -08003766 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08003767 "$pfx.fetch", '^'. $old_fetch . '$');
3768 delete $r->{$x->{old_repo_id}}->
3769 {fetch}->{$x->{old_path}};
3770 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
Eric Wong8b8fc062007-01-22 11:44:57 -08003771 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08003772 "$pfx.url");
3773 push @emptied, $x->{old_repo_id}
3774 }
3775 }
3776 }
3777 if (@emptied) {
3778 my $file = $ENV{GIT_CONFIG} || $ENV{GIT_CONFIG_LOCAL} ||
3779 "$ENV{GIT_DIR}/config";
3780 print STDERR <<EOF;
3781The following [svn-remote] sections in your config file ($file) are empty
3782and can be safely removed:
3783EOF
3784 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
3785 }
3786}
3787
Eric Wong706587f2007-01-18 17:50:01 -08003788sub migration_check {
3789 migrate_from_v0();
3790 migrate_from_v1();
3791 migrate_from_v2();
Eric Wong47e39c52007-01-21 04:27:09 -08003792 minimize_connections() if $_minimize;
Eric Wong706587f2007-01-18 17:50:01 -08003793}
3794
Eric Wongef3cfaa2007-01-24 03:30:57 -08003795package Git::IndexInfo;
3796use strict;
3797use warnings;
3798use Git qw/command_input_pipe command_close_pipe/;
3799
3800sub new {
3801 my ($class) = @_;
3802 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
3803 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
3804}
3805
3806sub remove {
3807 my ($self, $path) = @_;
3808 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
3809 return ++$self->{nr};
3810 }
3811 undef;
3812}
3813
3814sub update {
3815 my ($self, $mode, $hash, $path) = @_;
3816 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
3817 return ++$self->{nr};
3818 }
3819 undef;
3820}
3821
3822sub DESTROY {
3823 my ($self) = @_;
3824 command_close_pipe($self->{gui}, $self->{ctx});
3825}
3826
Eric Wong4bb9ed02007-02-03 13:29:17 -08003827package Git::SVN::GlobSpec;
3828use strict;
3829use warnings;
3830
3831sub new {
3832 my ($class, $glob) = @_;
Eric Wong4bb9ed02007-02-03 13:29:17 -08003833 my $re = $glob;
3834 $re =~ s!/+$!!g; # no need for trailing slashes
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003835 my $nr = ($re =~ s!^(.*)\*(.*)$!\(\[^/\]+\)!g);
Eric Wong4bb9ed02007-02-03 13:29:17 -08003836 my ($left, $right) = ($1, $2);
3837 if ($nr > 1) {
Eric Wonge5181922007-02-08 12:53:57 -08003838 die "Only one '*' wildcard expansion ",
3839 "is supported (got $nr): '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08003840 } elsif ($nr == 0) {
Eric Wonge5181922007-02-08 12:53:57 -08003841 die "One '*' is needed for glob: '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08003842 }
3843 $re = quotemeta($left) . $re . quotemeta($right);
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003844 if (length $left && !($left =~ s!/+$!!g)) {
3845 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
3846 }
3847 if (length $right && !($right =~ s!^/+!!g)) {
3848 die "Missing leading '/' on right side of: '$glob' ($right)\n";
3849 }
Eric Wong74a81222007-02-10 13:28:50 -08003850 my $left_re = qr/^\/\Q$left\E(\/|$)/;
3851 bless { left => $left, right => $right, left_regex => $left_re,
Eric Wong4bb9ed02007-02-03 13:29:17 -08003852 regex => qr/$re/, glob => $glob }, $class;
3853}
3854
3855sub full_path {
3856 my ($self, $path) = @_;
3857 return (length $self->{left} ? "$self->{left}/" : '') .
3858 $path . (length $self->{right} ? "/$self->{right}" : '');
3859}
3860
Eric Wong3397f9d2006-02-16 01:24:16 -08003861__END__
3862
3863Data structures:
3864
Eric Wong4bb9ed02007-02-03 13:29:17 -08003865
3866$remotes = { # returned by read_all_remotes()
3867 'svn' => {
3868 # svn-remote.svn.url=https://svn.musicpd.org
3869 url => 'https://svn.musicpd.org',
3870 # svn-remote.svn.fetch=mpd/trunk:trunk
3871 fetch => {
3872 'mpd/trunk' => 'trunk',
3873 },
3874 # svn-remote.svn.tags=mpd/tags/*:tags/*
3875 tags => {
3876 path => {
3877 left => 'mpd/tags',
3878 right => '',
3879 regex => qr!mpd/tags/([^/]+)$!,
3880 glob => 'tags/*',
3881 },
3882 ref => {
3883 left => 'tags',
3884 right => '',
3885 regex => qr!tags/([^/]+)$!,
3886 glob => 'tags/*',
3887 },
3888 }
3889 }
3890};
3891
Eric Wong44320b92007-01-13 22:35:53 -08003892$log_entry hashref as returned by libsvn_log_entry()
Eric Wong3397f9d2006-02-16 01:24:16 -08003893{
Eric Wong44320b92007-01-13 22:35:53 -08003894 log => 'whitespace-formatted log entry
Eric Wong3397f9d2006-02-16 01:24:16 -08003895', # trailing newline is preserved
3896 revision => '8', # integer
3897 date => '2004-02-24T17:01:44.108345Z', # commit date
3898 author => 'committer name'
3899};
3900
Eric Wong6e8548c2007-01-27 01:32:00 -08003901
3902# this is generated by generate_diff();
Eric Wong3397f9d2006-02-16 01:24:16 -08003903@mods = array of diff-index line hashes, each element represents one line
3904 of diff-index output
3905
3906diff-index line ($m hash)
3907{
3908 mode_a => first column of diff-index output, no leading ':',
3909 mode_b => second column of diff-index output,
3910 sha1_b => sha1sum of the final blob,
Eric Wongac8e0b92006-03-03 01:20:07 -08003911 chg => change type [MCRADT],
Eric Wong3397f9d2006-02-16 01:24:16 -08003912 file_a => original file name of a file (iff chg is 'C' or 'R')
3913 file_b => new/current file name of a file (any chg)
3914}
3915;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003916
Eric Wonga00439a2006-06-27 19:39:13 -07003917# retval of read_url_paths{,_all}();
3918$l_map = {
3919 # repository root url
3920 'https://svn.musicpd.org' => {
3921 # repository path # GIT_SVN_ID
3922 'mpd/trunk' => 'trunk',
3923 'mpd/tags/0.11.5' => 'tags/0.11.5',
3924 },
3925}
3926
Eric Wonga5e0ced2006-06-12 15:23:48 -07003927Notes:
3928 I don't trust the each() function on unless I created %hash myself
3929 because the internal iterator may not have started at base.