blob: c5965c9aafe8d5f990e64e88cea1061f84584b58 [file] [log] [blame]
Eric Wong3397f9d2006-02-16 01:24:16 -08001#!/usr/bin/env perl
Eric Wong551ce282006-02-20 10:57:29 -08002# Copyright (C) 2006, Eric Wong <normalperson@yhbt.net>
3# License: GPL v2 or later
Eric Wong3397f9d2006-02-16 01:24:16 -08004use warnings;
5use strict;
6use vars qw/ $AUTHOR $VERSION
Adam Robenffe256f2008-05-23 16:19:41 +02007 $sha1 $sha1_short $_revision $_repository
Eric Wong9760adc2007-01-31 03:06:56 -08008 $_q $_authors %users/;
Eric Wong3397f9d2006-02-16 01:24:16 -08009$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
Eric Wong60d02cc2006-07-06 00:14:16 -070010$VERSION = '@@GIT_VERSION@@';
Eric Wong13ccd6d2006-03-29 22:37:18 -080011
Benoit Sigoure15153452007-10-16 16:36:50 +020012# From which subdir have we been invoked?
13my $cmd_dir_prefix = eval {
14 command_oneline([qw/rev-parse --show-prefix/], STDERR => 0)
15} || '';
16
Eric Wong5253dc32007-02-20 01:36:30 -080017my $git_dir_user_set = 1 if defined $ENV{GIT_DIR};
Eric Wong706587f2007-01-18 17:50:01 -080018$ENV{GIT_DIR} ||= '.git';
Eric Wong9fa00b62007-02-03 12:49:48 -080019$Git::SVN::default_repo_id = 'svn';
Eric Wong8b8fc062007-01-22 11:44:57 -080020$Git::SVN::default_ref_id = $ENV{GIT_SVN_ID} || 'git-svn';
Eric Wong6af1db42007-02-14 16:04:10 -080021$Git::SVN::Ra::_log_window_size = 100;
Eric Wong13ccd6d2006-03-29 22:37:18 -080022
Eric Wongf8c9d1d2007-01-12 02:35:20 -080023$Git::SVN::Log::TZ = $ENV{TZ};
Eric Wong3397f9d2006-02-16 01:24:16 -080024$ENV{TZ} = 'UTC';
Eric Wonga00439a2006-06-27 19:39:13 -070025$| = 1; # unbuffer STDOUT
Eric Wong3397f9d2006-02-16 01:24:16 -080026
Benoit Sigoure207f1a72007-10-16 16:36:52 +020027sub fatal (@) { print STDERR "@_\n"; exit 1 }
Eric Wongb9c85182006-12-15 23:58:07 -080028require SVN::Core; # use()-ing this causes segfaults for me... *shrug*
29require SVN::Ra;
30require SVN::Delta;
31if ($SVN::Core::VERSION lt '1.1.0') {
Benoit Sigoure207f1a72007-10-16 16:36:52 +020032 fatal "Need SVN::Core 1.1.0 or better (got $SVN::Core::VERSION)";
Eric Wongb9c85182006-12-15 23:58:07 -080033}
Eric Wongd81bf822007-01-10 01:22:38 -080034push @Git::SVN::Ra::ISA, 'SVN::Ra';
Eric Wongb9c85182006-12-15 23:58:07 -080035push @SVN::Git::Editor::ISA, 'SVN::Delta::Editor';
36push @SVN::Git::Fetcher::ISA, 'SVN::Delta::Editor';
Eric Wong3397f9d2006-02-16 01:24:16 -080037use Carp qw/croak/;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -080038use Digest::MD5;
Eric Wong3397f9d2006-02-16 01:24:16 -080039use IO::File qw//;
40use File::Basename qw/dirname basename/;
41use File::Path qw/mkpath/;
Eric Wong512b6202007-04-03 01:57:08 -070042use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
Eric Wong968bdf12006-06-15 13:36:12 -070043use IPC::Open3;
Eric Wong336f1712007-01-11 02:14:43 -080044use Git;
Eric Wonga5e0ced2006-06-12 15:23:48 -070045
Eric Wong336f1712007-01-11 02:14:43 -080046BEGIN {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120047 # import functions from Git into our packages, en masse
48 no strict 'refs';
Eric Wong336f1712007-01-11 02:14:43 -080049 foreach (qw/command command_oneline command_noisy command_output_pipe
Boris Byk6ea42032009-04-11 00:32:41 +040050 command_input_pipe command_close_pipe
51 command_bidi_pipe command_close_bidi_pipe/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +120052 for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -080053 Git::SVN::Migration Git::SVN::Log Git::SVN),
Sam Vilainc5f71ad2007-06-15 15:43:59 +120054 __PACKAGE__) {
55 *{"${package}::$_"} = \&{"Git::$_"};
56 }
Eric Wong336f1712007-01-11 02:14:43 -080057 }
Eric Wong336f1712007-01-11 02:14:43 -080058}
59
Eric Wongb9c85182006-12-15 23:58:07 -080060my ($SVN);
Eric Wong83e99402006-10-11 18:19:55 -070061
Eric Wongf8c9d1d2007-01-12 02:35:20 -080062$sha1 = qr/[a-f\d]{40}/;
63$sha1_short = qr/[a-f\d]{4,40}/;
Eric Wong44320b92007-01-13 22:35:53 -080064my ($_stdin, $_help, $_edit,
Eric Wong9760adc2007-01-31 03:06:56 -080065 $_message, $_file,
Eric Wongd05d72e2007-01-15 22:59:26 -080066 $_template, $_shared,
Jason Merrillc2abd832009-04-06 16:37:59 -040067 $_version, $_fetch_all, $_no_rebase, $_fetch_parent,
Eric Wongdee41f32007-03-13 11:40:36 -070068 $_merge, $_strategy, $_dry_run, $_local,
Steven Grimm4be40382008-05-10 22:11:18 -070069 $_prefix, $_no_checkout, $_url, $_verbose,
Florian Ragwitz5de70ef2008-10-04 19:35:17 -070070 $_git_format, $_commit_url, $_tag);
Eric Wong0bed5ea2007-02-09 02:45:03 -080071$Git::SVN::_follow_parent = 1;
Simon Arlott49750f32009-03-30 19:31:41 +010072$_q ||= 0;
Eric Wong706587f2007-01-18 17:50:01 -080073my %remote_opts = ( 'username=s' => \$Git::SVN::Prompt::_username,
74 'config-dir=s' => \$Git::SVN::Ra::config_dir,
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +020075 'no-auth-cache' => \$Git::SVN::Prompt::_no_auth_cache,
76 'ignore-paths=s' => \$SVN::Git::Fetcher::_ignore_regex );
Eric Wong0bed5ea2007-02-09 02:45:03 -080077my %fc_opts = ( 'follow-parent|follow!' => \$Git::SVN::_follow_parent,
Eric Wongdc5869c2006-05-24 02:07:32 -070078 'authors-file|A=s' => \$_authors,
Eric Wongecc712d2007-01-31 12:28:10 -080079 'repack:i' => \$Git::SVN::_repack,
Eric Wong97ae0912007-02-11 15:21:24 -080080 'noMetadata' => \$Git::SVN::_no_metadata,
81 'useSvmProps' => \$Git::SVN::_use_svm_props,
Eric Wong62e349d2007-02-16 19:57:29 -080082 'useSvnsyncProps' => \$Git::SVN::_use_svnsync_props,
Eric Wong6af1db42007-02-14 16:04:10 -080083 'log-window-size=i' => \$Git::SVN::Ra::_log_window_size,
Eric Wong1e889ef2007-02-16 01:45:13 -080084 'no-checkout' => \$_no_checkout,
Simon Arlott49750f32009-03-30 19:31:41 +010085 'quiet|q+' => \$_q,
Eric Wongecc712d2007-01-31 12:28:10 -080086 'repack-flags|repack-args|repack-opts=s' =>
87 \$Git::SVN::_repack_flags,
Andy Whitcroft70ae04e2007-11-22 13:44:42 +000088 'use-log-author' => \$Git::SVN::_use_log_author,
Avery Pennarun6aa9ba12008-04-15 21:04:17 -040089 'add-author-from' => \$Git::SVN::_add_author_from,
Pete Harlane82f0d72009-01-17 20:10:14 -080090 'localtime' => \$Git::SVN::_localtime,
Eric Wong706587f2007-01-18 17:50:01 -080091 %remote_opts );
Eric Wong36f5b1f2006-05-23 19:23:41 -070092
martin f. krafft8f728fb2007-07-14 11:25:28 +020093my ($_trunk, $_tags, $_branches, $_stdlayout);
Eric Wong0dfaf0a2007-02-18 02:34:09 -080094my %icv;
Eric Wongdadc6d22007-02-14 12:27:41 -080095my %init_opts = ( 'template=s' => \$_template, 'shared:s' => \$_shared,
96 'trunk|T=s' => \$_trunk, 'tags|t=s' => \$_tags,
97 'branches|b=s' => \$_branches, 'prefix=s' => \$_prefix,
martin f. krafft8f728fb2007-07-14 11:25:28 +020098 'stdlayout|s' => \$_stdlayout,
Eric Wong4a1bb4c2007-05-13 09:58:14 -070099 'minimize-url|m' => \$Git::SVN::_minimize_url,
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800100 'no-metadata' => sub { $icv{noMetadata} = 1 },
101 'use-svm-props' => sub { $icv{useSvmProps} = 1 },
102 'use-svnsync-props' => sub { $icv{useSvnsyncProps} = 1 },
103 'rewrite-root=s' => sub { $icv{rewriteRoot} = $_[1] },
Eric Wongdadc6d22007-02-14 12:27:41 -0800104 %remote_opts );
Eric Wong27e9fb82006-06-27 19:39:12 -0700105my %cmt_opts = ( 'edit|e' => \$_edit,
Eric Wong24e22aa2007-01-29 00:07:49 -0800106 'rmdir' => \$SVN::Git::Editor::_rmdir,
107 'find-copies-harder' => \$SVN::Git::Editor::_find_copies_harder,
108 'l=i' => \$SVN::Git::Editor::_rename_limit,
109 'copy-similarity|C=i'=> \$SVN::Git::Editor::_cp_similarity
Eric Wong27e9fb82006-06-27 19:39:12 -0700110);
Eric Wong9d55b412006-06-12 15:53:13 -0700111
Eric Wong3397f9d2006-02-16 01:24:16 -0800112my %cmd = (
Eric Wong2a3240b2007-01-04 18:09:56 -0800113 fetch => [ \&cmd_fetch, "Download new revisions from SVN",
Eric Wonge98671e2007-02-14 02:21:19 -0800114 { 'revision|r=s' => \$_revision,
Eric Wong905f8b72007-02-16 03:22:40 -0800115 'fetch-all|all' => \$_fetch_all,
Jason Merrillc2abd832009-04-06 16:37:59 -0400116 'parent|p' => \$_fetch_parent,
Eric Wonge98671e2007-02-14 02:21:19 -0800117 %fc_opts } ],
Eric Wong0425ea92007-02-16 18:45:01 -0800118 clone => [ \&cmd_clone, "Initialize and fetch revisions",
119 { 'revision|r=s' => \$_revision,
120 %fc_opts, %init_opts } ],
Eric Wongd2866f92007-01-11 12:26:16 -0800121 init => [ \&cmd_init, "Initialize a repo for tracking" .
Eric Wongf8ab6b72006-05-31 15:49:56 -0700122 " (requires URL argument)",
Eric Wong9d55b412006-06-12 15:53:13 -0700123 \%init_opts ],
Eric Wongdadc6d22007-02-14 12:27:41 -0800124 'multi-init' => [ \&cmd_multi_init,
125 "Deprecated alias for ".
126 "'$0 init -T<trunk> -b<branches> -t<tags>'",
127 \%init_opts ],
Eric Wongd7ad3be2007-01-14 03:14:28 -0800128 dcommit => [ \&cmd_dcommit,
129 'Commit several diffs to merge with upstream',
Eric Wong3289e862006-12-15 23:58:08 -0800130 { 'merge|m|M' => \$_merge,
131 'strategy|s=s' => \$_strategy,
Eric Wong905f8b72007-02-16 03:22:40 -0800132 'verbose|v' => \$_verbose,
Eric Wong3289e862006-12-15 23:58:08 -0800133 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800134 'fetch-all|all' => \$_fetch_all,
Eric Wongba24e742008-08-07 02:06:16 -0700135 'commit-url=s' => \$_commit_url,
136 'revision|r=i' => \$_revision,
Karl Hasselström171af112007-05-03 07:51:35 +0200137 'no-rebase' => \$_no_rebase,
Eric Wong4b155222006-12-22 21:59:24 -0800138 %cmt_opts, %fc_opts } ],
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700139 branch => [ \&cmd_branch,
140 'Create a branch in the SVN repository',
141 { 'message|m=s' => \$_message,
142 'dry-run|n' => \$_dry_run,
143 'tag|t' => \$_tag } ],
144 tag => [ sub { $_tag = 1; cmd_branch(@_) },
145 'Create a tag in the SVN repository',
146 { 'message|m=s' => \$_message,
147 'dry-run|n' => \$_dry_run } ],
Eric Wong1ce255d2007-01-14 23:21:16 -0800148 'set-tree' => [ \&cmd_set_tree,
149 "Set an SVN repository to a git tree-ish",
150 { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200151 'create-ignore' => [ \&cmd_create_ignore,
152 'Create a .gitignore per svn:ignore',
153 { 'revision|r=i' => \$_revision
154 } ],
Benoit Sigoure15153452007-10-16 16:36:50 +0200155 'propget' => [ \&cmd_propget,
156 'Print the value of a property on a file or directory',
157 { 'revision|r=i' => \$_revision } ],
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200158 'proplist' => [ \&cmd_proplist,
159 'List all properties of a file or directory',
160 { 'revision|r=i' => \$_revision } ],
Eric Wong5969cbe2007-01-11 17:58:39 -0800161 'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200162 { 'revision|r=i' => \$_revision
Lars Hjemli05b4df32007-09-05 11:35:29 +0200163 } ],
Vineet Kumar2d879792007-11-19 14:56:15 -0800164 'show-externals' => [ \&cmd_show_externals, "Show svn:externals listings",
165 { 'revision|r=i' => \$_revision
166 } ],
Eric Wong1c8443b2007-01-14 02:17:00 -0800167 'multi-fetch' => [ \&cmd_multi_fetch,
Eric Wonge98671e2007-02-14 02:21:19 -0800168 "Deprecated alias for $0 fetch --all",
169 { 'revision|r=s' => \$_revision, %fc_opts } ],
Eric Wong706587f2007-01-18 17:50:01 -0800170 'migrate' => [ sub { },
171 # no-op, we automatically run this anyways,
Eric Wong706587f2007-01-18 17:50:01 -0800172 'Migrate configuration/metadata/layout from
173 previous versions of git-svn',
Eric Wonga836a0e2007-02-14 19:34:56 -0800174 { 'minimize' => \$Git::SVN::Migration::_minimize,
175 %remote_opts } ],
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800176 'log' => [ \&Git::SVN::Log::cmd_show_log, 'Show commit logs',
177 { 'limit=i' => \$Git::SVN::Log::limit,
Eric Wong79bb8d82006-06-01 02:35:44 -0700178 'revision|r=s' => \$_revision,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800179 'verbose|v' => \$Git::SVN::Log::verbose,
180 'incremental' => \$Git::SVN::Log::incremental,
181 'oneline' => \$Git::SVN::Log::oneline,
182 'show-commit' => \$Git::SVN::Log::show_commit,
183 'non-recursive' => \$Git::SVN::Log::non_recursive,
Eric Wong79bb8d82006-06-01 02:35:44 -0700184 'authors-file|A=s' => \$_authors,
Eric Wongf8c9d1d2007-01-12 02:35:20 -0800185 'color' => \$Git::SVN::Log::color,
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200186 'pager=s' => \$Git::SVN::Log::pager
Eric Wong79bb8d82006-06-01 02:35:44 -0700187 } ],
Eric Wong222566e2008-08-08 01:41:58 -0700188 'find-rev' => [ \&cmd_find_rev,
189 "Translate between SVN revision numbers and tree-ish",
Lars Hjemli4dbfe2e2007-09-07 02:00:08 +0200190 {} ],
Eric Wong905f8b72007-02-16 03:22:40 -0800191 'rebase' => [ \&cmd_rebase, "Fetch and rebase your working directory",
192 { 'merge|m|M' => \$_merge,
193 'verbose|v' => \$_verbose,
194 'strategy|s=s' => \$_strategy,
Eric Wongdee41f32007-03-13 11:40:36 -0700195 'local|l' => \$_local,
Eric Wong905f8b72007-02-16 03:22:40 -0800196 'fetch-all|all' => \$_fetch_all,
Seth Falcon7d45e142008-05-19 20:29:17 -0700197 'dry-run|n' => \$_dry_run,
Eric Wong905f8b72007-02-16 03:22:40 -0800198 %fc_opts } ],
Eric Wong44320b92007-01-13 22:35:53 -0800199 'commit-diff' => [ \&cmd_commit_diff,
200 'Commit a diff between two trees',
Eric Wong27e9fb82006-06-27 19:39:12 -0700201 { 'message|m=s' => \$_message,
202 'file|F=s' => \$_file,
Eric Wong45bf4732006-11-09 01:19:37 -0800203 'revision|r=s' => \$_revision,
Eric Wong27e9fb82006-06-27 19:39:12 -0700204 %cmt_opts } ],
David D. Kilzere6fefa92007-11-21 11:57:18 -0800205 'info' => [ \&cmd_info,
206 "Show info about the latest SVN revision
207 on the current branch",
David D. Kilzer8b014d72007-11-21 11:57:19 -0800208 { 'url' => \$_url, } ],
Tim Stoakes6fb53752008-02-10 15:21:08 +1030209 'blame' => [ \&Git::SVN::Log::cmd_blame,
210 "Show what revision and author last modified each line of a file",
Steven Grimm4be40382008-05-10 22:11:18 -0700211 { 'git-format' => \$_git_format } ],
Eric Wong3397f9d2006-02-16 01:24:16 -0800212);
Eric Wong9d55b412006-06-12 15:53:13 -0700213
Eric Wong3397f9d2006-02-16 01:24:16 -0800214my $cmd;
215for (my $i = 0; $i < @ARGV; $i++) {
216 if (defined $cmd{$ARGV[$i]}) {
217 $cmd = $ARGV[$i];
218 splice @ARGV, $i, 1;
219 last;
220 }
221};
222
Eric Wong540424b2007-12-19 00:31:43 -0800223# make sure we're always running at the top-level working directory
224unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
Eric Wong5253dc32007-02-20 01:36:30 -0800225 unless (-d $ENV{GIT_DIR}) {
226 if ($git_dir_user_set) {
227 die "GIT_DIR=$ENV{GIT_DIR} explicitly set, ",
228 "but it is not a directory\n";
229 }
230 my $git_dir = delete $ENV{GIT_DIR};
Deskin Millerfe4003f2008-11-06 00:07:39 -0500231 my $cdup = undef;
232 git_cmd_try {
233 $cdup = command_oneline(qw/rev-parse --show-cdup/);
234 $git_dir = '.' unless ($cdup);
235 chomp $cdup if ($cdup);
236 $cdup = "." unless ($cdup && length $cdup);
237 } "Already at toplevel, but $git_dir not found\n";
Eric Wong5253dc32007-02-20 01:36:30 -0800238 chdir $cdup or die "Unable to chdir up to '$cdup'\n";
239 unless (-d $git_dir) {
240 die "$git_dir still not found after going to ",
241 "'$cdup'\n";
242 }
243 $ENV{GIT_DIR} = $git_dir;
244 }
Adam Robenffe256f2008-05-23 16:19:41 +0200245 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wong5253dc32007-02-20 01:36:30 -0800246}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100247
248my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
249
250read_repo_config(\%opts);
Eric Wong222566e2008-08-08 01:41:58 -0700251if ($cmd && ($cmd eq 'log' || $cmd eq 'blame')) {
252 Getopt::Long::Configure('pass_through');
253}
Gustaf Hendebyf4dd3342007-11-24 14:47:56 +0100254my $rv = GetOptions(%opts, 'help|H|h' => \$_help, 'version|V' => \$_version,
255 'minimize-connections' => \$Git::SVN::Migration::_minimize,
256 'id|i=s' => \$Git::SVN::default_ref_id,
257 'svn-remote|remote|R=s' => sub {
258 $Git::SVN::no_reuse_existing = 1;
259 $Git::SVN::default_repo_id = $_[1] });
260exit 1 if (!$rv && $cmd && $cmd ne 'log');
261
262usage(0) if $_help;
263version() if $_version;
264usage(1) unless defined $cmd;
265load_authors() if $_authors;
266
Eric Wong0425ea92007-02-16 18:45:01 -0800267unless ($cmd =~ /^(?:clone|init|multi-init|commit-diff)$/) {
Eric Wong706587f2007-01-18 17:50:01 -0800268 Git::SVN::Migration::migration_check();
269}
Eric Wongecc712d2007-01-31 12:28:10 -0800270Git::SVN::init_vars();
Eric Wongb805b442007-01-22 13:52:04 -0800271eval {
272 Git::SVN::verify_remotes_sanity();
273 $cmd{$cmd}->[0]->(@ARGV);
274};
275fatal $@ if $@;
Eric Wong1e889ef2007-02-16 01:45:13 -0800276post_fetch_checkout();
Eric Wong3397f9d2006-02-16 01:24:16 -0800277exit 0;
278
279####################### primary functions ######################
280sub usage {
281 my $exit = shift || 0;
282 my $fd = $exit ? \*STDERR : \*STDOUT;
283 print $fd <<"";
284git-svn - bidirectional operations between a single Subversion tree and git
Stephan Beyer1b1dd232008-07-13 15:36:15 +0200285Usage: git svn <command> [options] [arguments]\n
Eric Wong448c81b2006-03-03 01:20:09 -0800286
287 print $fd "Available commands:\n" unless $cmd;
Eric Wong3397f9d2006-02-16 01:24:16 -0800288
289 foreach (sort keys %cmd) {
Eric Wong448c81b2006-03-03 01:20:09 -0800290 next if $cmd && $cmd ne $_;
Eric Wonga836a0e2007-02-14 19:34:56 -0800291 next if /^multi-/; # don't show deprecated commands
Eric Wongb203b762006-10-11 14:53:36 -0700292 print $fd ' ',pack('A17',$_),$cmd{$_}->[1],"\n";
Benoit Sigoureaa807bc2007-11-03 19:53:34 +0100293 foreach (sort keys %{$cmd{$_}->[2]}) {
Eric Wong512b6202007-04-03 01:57:08 -0700294 # mixed-case options are for .git/config only
295 next if /[A-Z]/ && /^[a-z]+$/i;
Eric Wong448c81b2006-03-03 01:20:09 -0800296 # prints out arguments as they should be passed:
Eric Wongb8c92ca2006-05-24 01:40:37 -0700297 my $x = s#[:=]s$## ? '<arg>' : s#[:=]i$## ? '<num>' : '';
Eric Wongb203b762006-10-11 14:53:36 -0700298 print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
Eric Wong448c81b2006-03-03 01:20:09 -0800299 "--$_" : "-$_" }
300 split /\|/,$_)," $x\n";
301 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800302 }
303 print $fd <<"";
Eric Wong448c81b2006-03-03 01:20:09 -0800304\nGIT_SVN_ID may be set in the environment or via the --id/-i switch to an
305arbitrary identifier if you're tracking multiple SVN branches/repositories in
306one git repository and want to keep them separate. See git-svn(1) for more
307information.
Eric Wong3397f9d2006-02-16 01:24:16 -0800308
309 exit $exit;
310}
311
Eric Wong551ce282006-02-20 10:57:29 -0800312sub version {
Eric Wong7d60ab22006-12-28 01:16:20 -0800313 print "git-svn version $VERSION (svn $SVN::Core::VERSION)\n";
Eric Wong551ce282006-02-20 10:57:29 -0800314 exit 0;
315}
316
Eric Wong8164b652007-01-11 15:35:55 -0800317sub do_git_init_db {
318 unless (-d $ENV{GIT_DIR}) {
319 my @init_db = ('init');
320 push @init_db, "--template=$_template" if defined $_template;
Eric Wongdadc6d22007-02-14 12:27:41 -0800321 if (defined $_shared) {
322 if ($_shared =~ /[a-z]/) {
323 push @init_db, "--shared=$_shared";
324 } else {
325 push @init_db, "--shared";
326 }
327 }
Eric Wong8164b652007-01-11 15:35:55 -0800328 command_noisy(@init_db);
Adam Robenffe256f2008-05-23 16:19:41 +0200329 $_repository = Git->repository(Repository => ".git");
Eric Wong8164b652007-01-11 15:35:55 -0800330 }
Johannes Schindelind3c96342009-04-09 13:29:57 +0200331 command_noisy('config', 'core.autocrlf', 'false');
Eric Wong0dfaf0a2007-02-18 02:34:09 -0800332 my $set;
333 my $pfx = "svn-remote.$Git::SVN::default_repo_id";
334 foreach my $i (keys %icv) {
335 die "'$set' and '$i' cannot both be set\n" if $set;
336 next unless defined $icv{$i};
337 command_noisy('config', "$pfx.$i", $icv{$i});
338 $set = $i;
339 }
Ben Jackson88ec2052009-04-11 10:46:18 -0700340 my $ignore_regex = \$SVN::Git::Fetcher::_ignore_regex;
341 command_noisy('config', "$pfx.ignore-paths", $$ignore_regex)
342 if defined $$ignore_regex;
Eric Wong8164b652007-01-11 15:35:55 -0800343}
344
Eric Wongdadc6d22007-02-14 12:27:41 -0800345sub init_subdir {
346 my $repo_path = shift or return;
347 mkpath([$repo_path]) unless -d $repo_path;
348 chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
Eric Wongf30603f2007-02-23 01:26:26 -0800349 $ENV{GIT_DIR} = '.git';
Adam Robenffe256f2008-05-23 16:19:41 +0200350 $_repository = Git->repository(Repository => $ENV{GIT_DIR});
Eric Wongdadc6d22007-02-14 12:27:41 -0800351}
352
Eric Wong0425ea92007-02-16 18:45:01 -0800353sub cmd_clone {
354 my ($url, $path) = @_;
355 if (!defined $path &&
martin f. krafft8f728fb2007-07-14 11:25:28 +0200356 (defined $_trunk || defined $_branches || defined $_tags ||
357 defined $_stdlayout) &&
Eric Wong0425ea92007-02-16 18:45:01 -0800358 $url !~ m#^[a-z\+]+://#) {
359 $path = $url;
360 }
Eric Wong0425ea92007-02-16 18:45:01 -0800361 $path = basename($url) if !defined $path || !length $path;
Eric Wongf30603f2007-02-23 01:26:26 -0800362 cmd_init($url, $path);
Eric Wong0425ea92007-02-16 18:45:01 -0800363 Git::SVN::fetch_all($Git::SVN::default_repo_id);
364}
365
Eric Wongd2866f92007-01-11 12:26:16 -0800366sub cmd_init {
martin f. krafft8f728fb2007-07-14 11:25:28 +0200367 if (defined $_stdlayout) {
368 $_trunk = 'trunk' if (!defined $_trunk);
369 $_tags = 'tags' if (!defined $_tags);
370 $_branches = 'branches' if (!defined $_branches);
371 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800372 if (defined $_trunk || defined $_branches || defined $_tags) {
373 return cmd_multi_init(@_);
Eric Wong03e0ea82006-06-30 21:42:53 -0700374 }
Eric Wongdadc6d22007-02-14 12:27:41 -0800375 my $url = shift or die "SVN repository location required ",
376 "as a command-line argument\n";
377 init_subdir(@_);
Eric Wong8164b652007-01-11 15:35:55 -0800378 do_git_init_db();
Eric Wong03e0ea82006-06-30 21:42:53 -0700379
Eric Wong706587f2007-01-18 17:50:01 -0800380 Git::SVN->init($url);
Eric Wong3397f9d2006-02-16 01:24:16 -0800381}
382
Eric Wong2a3240b2007-01-04 18:09:56 -0800383sub cmd_fetch {
Eric Wonge98671e2007-02-14 02:21:19 -0800384 if (grep /^\d+=./, @_) {
385 die "'<rev>=<commit>' fetch arguments are ",
386 "no longer supported.\n";
Eric Wong07a1c952007-01-22 15:47:41 -0800387 }
Eric Wonge98671e2007-02-14 02:21:19 -0800388 my ($remote) = @_;
389 if (@_ > 1) {
Jason Merrillc2abd832009-04-06 16:37:59 -0400390 die "Usage: $0 fetch [--all] [--parent] [svn-remote]\n";
Eric Wonge98671e2007-02-14 02:21:19 -0800391 }
Jason Merrillc2abd832009-04-06 16:37:59 -0400392 if ($_fetch_parent) {
393 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
394 unless ($gs) {
395 die "Unable to determine upstream SVN information from ",
396 "working tree history\n";
397 }
398 # just fetch, don't checkout.
399 $_no_checkout = 'true';
400 $_fetch_all ? $gs->fetch_all : $gs->fetch;
401 } elsif ($_fetch_all) {
Eric Wonge98671e2007-02-14 02:21:19 -0800402 cmd_multi_fetch();
403 } else {
Jason Merrillc2abd832009-04-06 16:37:59 -0400404 $remote ||= $Git::SVN::default_repo_id;
Eric Wonge98671e2007-02-14 02:21:19 -0800405 Git::SVN::fetch_all($remote, Git::SVN::read_all_remotes());
Eric Wong1c8443b2007-01-14 02:17:00 -0800406 }
Eric Wong2a3240b2007-01-04 18:09:56 -0800407}
408
Eric Wong1ce255d2007-01-14 23:21:16 -0800409sub cmd_set_tree {
Eric Wong3397f9d2006-02-16 01:24:16 -0800410 my (@commits) = @_;
411 if ($_stdin || !@commits) {
412 print "Reading from stdin...\n";
413 @commits = ();
414 while (<STDIN>) {
Eric Wong1ca72ae2006-03-03 01:20:09 -0800415 if (/\b($sha1_short)\b/o) {
Eric Wong3397f9d2006-02-16 01:24:16 -0800416 unshift @commits, $1;
417 }
418 }
419 }
420 my @revs;
Eric Wong8de010a2006-02-20 10:57:26 -0800421 foreach my $c (@commits) {
Eric Wongaef4e922006-12-15 10:59:54 -0800422 my @tmp = command('rev-parse',$c);
Eric Wong8de010a2006-02-20 10:57:26 -0800423 if (scalar @tmp == 1) {
424 push @revs, $tmp[0];
425 } elsif (scalar @tmp > 1) {
Eric Wongaef4e922006-12-15 10:59:54 -0800426 push @revs, reverse(command('rev-list',@tmp));
Eric Wong8de010a2006-02-20 10:57:26 -0800427 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200428 fatal "Failed to rev-parse $c";
Eric Wong8de010a2006-02-20 10:57:26 -0800429 }
Eric Wong3397f9d2006-02-16 01:24:16 -0800430 }
Eric Wong1ce255d2007-01-14 23:21:16 -0800431 my $gs = Git::SVN->new;
432 my ($r_last, $cmt_last) = $gs->last_rev_commit;
433 $gs->fetch;
Eric Wong97f69872007-01-25 11:53:13 -0800434 if (defined $gs->{last_rev} && $r_last != $gs->{last_rev}) {
Eric Wong1ce255d2007-01-14 23:21:16 -0800435 fatal "There are new revisions that were fetched ",
436 "and need to be merged (or acknowledged) ",
437 "before committing.\nlast rev: $r_last\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200438 " current: $gs->{last_rev}";
Eric Wong1ce255d2007-01-14 23:21:16 -0800439 }
440 $gs->set_tree($_) foreach @revs;
Eric Wonga5e0ced2006-06-12 15:23:48 -0700441 print "Done committing ",scalar @revs," revisions to SVN\n";
Eric Wong3157dd92007-12-13 08:27:34 -0800442 unlink $gs->{index};
Eric Wonga5e0ced2006-06-12 15:23:48 -0700443}
444
Eric Wongd7ad3be2007-01-14 03:14:28 -0800445sub cmd_dcommit {
446 my $head = shift;
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100447 git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
David Reiss826a9332007-11-13 13:47:26 -0800448 'Cannot dcommit with a dirty index. Commit your changes first, '
Benoit Sigourec8cfa3e2007-11-11 19:41:41 +0100449 . "or stash them with `git stash'.\n";
Eric Wongd7ad3be2007-01-14 03:14:28 -0800450 $head ||= 'HEAD';
Eric Wonga8ae2622007-02-13 14:22:11 -0800451 my @refs;
Eric Wong13c823f2007-04-08 00:59:19 -0700452 my ($url, $rev, $uuid, $gs) = working_head_info($head, \@refs);
Thomas Rast2cb61102008-08-31 15:50:59 +0200453 unless ($gs) {
454 die "Unable to determine upstream SVN information from ",
455 "$head history.\nPerhaps the repository is empty.";
456 }
Peter Oberndorfer0df84052009-02-23 12:02:53 +0100457
458 if (defined $_commit_url) {
459 $url = $_commit_url;
460 } else {
461 $url = eval { command_oneline('config', '--get',
462 "svn-remote.$gs->{repo_id}.commiturl") };
463 if (!$url) {
464 $url = $gs->full_url
465 }
466 }
467
Eric Wongba24e742008-08-07 02:06:16 -0700468 my $last_rev = $_revision if defined $_revision;
Matthieu Moy59b0c242008-04-24 20:06:36 +0200469 if ($url) {
470 print "Committing to $url ...\n";
471 }
Eric Wong733a65a2007-06-13 02:23:28 -0700472 my ($linear_refs, $parents) = linearize_history($gs, \@refs);
Eric Wong751eb392007-08-31 18:16:12 -0700473 if ($_no_rebase && scalar(@$linear_refs) > 1) {
474 warn "Attempting to commit more than one change while ",
475 "--no-rebase is enabled.\n",
476 "If these changes depend on each other, re-running ",
Eric Wong7dfa16b2008-01-02 10:09:49 -0800477 "without --no-rebase may be required."
Eric Wong751eb392007-08-31 18:16:12 -0700478 }
Eric Wong711521e2008-08-20 00:30:06 -0700479 my $expect_url = $url;
480 Git::SVN::remove_username($expect_url);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800481 while (1) {
482 my $d = shift @$linear_refs or last;
Eric Wong45bf4732006-11-09 01:19:37 -0800483 unless (defined $last_rev) {
484 (undef, $last_rev, undef) = cmt_metadata("$d~1");
485 unless (defined $last_rev) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800486 fatal "Unable to extract revision information ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200487 "from commit $d~1";
Eric Wong45bf4732006-11-09 01:19:37 -0800488 }
489 }
Eric Wongb22d4492006-08-26 00:01:23 -0700490 if ($_dry_run) {
491 print "diff-tree $d~1 $d\n";
492 } else {
Eric Wong751eb392007-08-31 18:16:12 -0700493 my $cmt_rev;
Eric Wongd7ad3be2007-01-14 03:14:28 -0800494 my %ed_opts = ( r => $last_rev,
Eric Wong61395352007-01-27 14:33:08 -0800495 log => get_commit_entry($d)->{log},
Eric Wongba24e742008-08-07 02:06:16 -0700496 ra => Git::SVN::Ra->new($url),
Konstantin V. Arkhipov3caf3202007-11-14 03:52:02 +0300497 config => SVN::Core::config_get_config(
498 $Git::SVN::Ra::config_dir
499 ),
Eric Wong61395352007-01-27 14:33:08 -0800500 tree_a => "$d~1",
501 tree_b => $d,
502 editor_cb => sub {
503 print "Committed r$_[0]\n";
Eric Wong751eb392007-08-31 18:16:12 -0700504 $cmt_rev = $_[0];
505 },
Eric Wonga8ae2622007-02-13 14:22:11 -0800506 svn_path => '');
Eric Wong61395352007-01-27 14:33:08 -0800507 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wongd7ad3be2007-01-14 03:14:28 -0800508 print "No changes\n$d~1 == $d\n";
Eric Wong733a65a2007-06-13 02:23:28 -0700509 } elsif ($parents->{$d} && @{$parents->{$d}}) {
Eric Wong751eb392007-08-31 18:16:12 -0700510 $gs->{inject_parents_dcommit}->{$cmt_rev} =
Eric Wong733a65a2007-06-13 02:23:28 -0700511 $parents->{$d};
Eric Wongd7ad3be2007-01-14 03:14:28 -0800512 }
Eric Wong751eb392007-08-31 18:16:12 -0700513 $_fetch_all ? $gs->fetch_all : $gs->fetch;
Eric Wong7dfa16b2008-01-02 10:09:49 -0800514 $last_rev = $cmt_rev;
Eric Wong751eb392007-08-31 18:16:12 -0700515 next if $_no_rebase;
516
517 # we always want to rebase against the current HEAD,
518 # not any head that was passed to us
Eric Wongc74d9ac2007-11-05 03:21:47 -0800519 my @diff = command('diff-tree', $d,
Eric Wong751eb392007-08-31 18:16:12 -0700520 $gs->refname, '--');
521 my @finish;
522 if (@diff) {
523 @finish = rebase_cmd();
Eric Wongc74d9ac2007-11-05 03:21:47 -0800524 print STDERR "W: $d and ", $gs->refname,
Eric Wong751eb392007-08-31 18:16:12 -0700525 " differ, using @finish:\n",
Eric Wongc74d9ac2007-11-05 03:21:47 -0800526 join("\n", @diff), "\n";
Eric Wong751eb392007-08-31 18:16:12 -0700527 } else {
528 print "No changes between current HEAD and ",
529 $gs->refname,
530 "\nResetting to the latest ",
531 $gs->refname, "\n";
532 @finish = qw/reset --mixed/;
533 }
534 command_noisy(@finish, $gs->refname);
Eric Wongc74d9ac2007-11-05 03:21:47 -0800535 if (@diff) {
536 @refs = ();
537 my ($url_, $rev_, $uuid_, $gs_) =
538 working_head_info($head, \@refs);
539 my ($linear_refs_, $parents_) =
540 linearize_history($gs_, \@refs);
541 if (scalar(@$linear_refs) !=
542 scalar(@$linear_refs_)) {
543 fatal "# of revisions changed ",
544 "\nbefore:\n",
545 join("\n", @$linear_refs),
546 "\n\nafter:\n",
547 join("\n", @$linear_refs_), "\n",
548 'If you are attempting to commit ',
549 "merges, try running:\n\t",
550 'git rebase --interactive',
551 '--preserve-merges ',
552 $gs->refname,
553 "\nBefore dcommitting";
554 }
Eric Wong711521e2008-08-20 00:30:06 -0700555 if ($url_ ne $expect_url) {
Eric Wongc74d9ac2007-11-05 03:21:47 -0800556 fatal "URL mismatch after rebase: ",
Eric Wong711521e2008-08-20 00:30:06 -0700557 "$url_ != $expect_url";
Eric Wongc74d9ac2007-11-05 03:21:47 -0800558 }
559 if ($uuid_ ne $uuid) {
560 fatal "uuid mismatch after rebase: ",
561 "$uuid_ != $uuid";
562 }
563 # remap parents
564 my (%p, @l, $i);
565 for ($i = 0; $i < scalar @$linear_refs; $i++) {
566 my $new = $linear_refs_->[$i] or next;
567 $p{$new} =
568 $parents->{$linear_refs->[$i]};
569 push @l, $new;
570 }
571 $parents = \%p;
572 $linear_refs = \@l;
573 }
Eric Wongb22d4492006-08-26 00:01:23 -0700574 }
575 }
Eric Wong3157dd92007-12-13 08:27:34 -0800576 unlink $gs->{index};
Eric Wongb22d4492006-08-26 00:01:23 -0700577}
578
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700579sub cmd_branch {
580 my ($branch_name, $head) = @_;
581
582 unless (defined $branch_name && length $branch_name) {
583 die(($_tag ? "tag" : "branch") . " name required\n");
584 }
585 $head ||= 'HEAD';
586
587 my ($src, $rev, undef, $gs) = working_head_info($head);
588
Deskin Millera0fbc872008-12-01 21:43:00 -0500589 my $remote = Git::SVN::read_all_remotes()->{$gs->{repo_id}};
Florian Ragwitz5de70ef2008-10-04 19:35:17 -0700590 my $glob = $remote->{ $_tag ? 'tags' : 'branches' };
591 my ($lft, $rgt) = @{ $glob->{path} }{qw/left right/};
592 my $dst = join '/', $remote->{url}, $lft, $branch_name, ($rgt || ());
593
594 my $ctx = SVN::Client->new(
595 auth => Git::SVN::Ra::_auth_providers(),
596 log_msg => sub {
597 ${ $_[0] } = defined $_message
598 ? $_message
599 : 'Create ' . ($_tag ? 'tag ' : 'branch ' )
600 . $branch_name;
601 },
602 );
603
604 eval {
605 $ctx->ls($dst, 'HEAD', 0);
606 } and die "branch ${branch_name} already exists\n";
607
608 print "Copying ${src} at r${rev} to ${dst}...\n";
609 $ctx->copy($src, $rev, $dst)
610 unless $_dry_run;
611
612 $gs->fetch_all;
613}
614
Adam Roben26e60162007-04-27 11:57:53 -0700615sub cmd_find_rev {
Marc-Andre Lureauea14e6c2008-03-11 10:00:45 +0200616 my $revision_or_hash = shift or die "SVN or git revision required ",
617 "as a command-line argument\n";
Adam Roben26e60162007-04-27 11:57:53 -0700618 my $result;
619 if ($revision_or_hash =~ /^r\d+$/) {
Adam Robenb3cb7e42007-04-29 01:35:27 -0700620 my $head = shift;
621 $head ||= 'HEAD';
622 my @refs;
João Abecasis63c56022008-07-14 16:28:04 +0100623 my (undef, undef, $uuid, $gs) = working_head_info($head, \@refs);
Adam Robenb3cb7e42007-04-29 01:35:27 -0700624 unless ($gs) {
625 die "Unable to determine upstream SVN information from ",
626 "$head history\n";
Adam Roben26e60162007-04-27 11:57:53 -0700627 }
Adam Robenb3cb7e42007-04-29 01:35:27 -0700628 my $desired_revision = substr($revision_or_hash, 1);
João Abecasis63c56022008-07-14 16:28:04 +0100629 $result = $gs->rev_map_get($desired_revision, $uuid);
Adam Roben26e60162007-04-27 11:57:53 -0700630 } else {
631 my (undef, $rev, undef) = cmt_metadata($revision_or_hash);
632 $result = $rev;
633 }
634 print "$result\n" if $result;
635}
636
Eric Wong905f8b72007-02-16 03:22:40 -0800637sub cmd_rebase {
638 command_noisy(qw/update-index --refresh/);
Eric Wong13c823f2007-04-08 00:59:19 -0700639 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
640 unless ($gs) {
Eric Wong905f8b72007-02-16 03:22:40 -0800641 die "Unable to determine upstream SVN information from ",
642 "working tree history\n";
643 }
Seth Falcon7d45e142008-05-19 20:29:17 -0700644 if ($_dry_run) {
645 print "Remote Branch: " . $gs->refname . "\n";
646 print "SVN URL: " . $url . "\n";
647 return;
648 }
Eric Wong905f8b72007-02-16 03:22:40 -0800649 if (command(qw/diff-index HEAD --/)) {
650 print STDERR "Cannot rebase with uncommited changes:\n";
651 command_noisy('status');
652 exit 1;
653 }
Eric Wongdee41f32007-03-13 11:40:36 -0700654 unless ($_local) {
Steven Grimmcec0d5a2007-11-29 11:54:39 -0800655 # rebase will checkout for us, so no need to do it explicitly
656 $_no_checkout = 'true';
Eric Wongdee41f32007-03-13 11:40:36 -0700657 $_fetch_all ? $gs->fetch_all : $gs->fetch;
658 }
Eric Wong905f8b72007-02-16 03:22:40 -0800659 command_noisy(rebase_cmd(), $gs->refname);
660}
661
Eric Wong5969cbe2007-01-11 17:58:39 -0800662sub cmd_show_ignore {
Eric Wong13c823f2007-04-08 00:59:19 -0700663 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
664 $gs ||= Git::SVN->new;
Eric Wong5969cbe2007-01-11 17:58:39 -0800665 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
Benoit Sigoure01bdab82007-10-16 16:36:48 +0200666 $gs->prop_walk($gs->{path}, $r, sub {
667 my ($gs, $path, $props) = @_;
668 print STDOUT "\n# $path\n";
669 my $s = $props->{'svn:ignore'} or return;
670 $s =~ s/[\r\n]+/\n/g;
671 chomp $s;
672 $s =~ s#^#$path#gm;
673 print STDOUT "$s\n";
674 });
Eric Wonga5e0ced2006-06-12 15:23:48 -0700675}
676
Vineet Kumar2d879792007-11-19 14:56:15 -0800677sub cmd_show_externals {
678 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
679 $gs ||= Git::SVN->new;
680 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
681 $gs->prop_walk($gs->{path}, $r, sub {
682 my ($gs, $path, $props) = @_;
683 print STDOUT "\n# $path\n";
684 my $s = $props->{'svn:externals'} or return;
685 $s =~ s/[\r\n]+/\n/g;
686 chomp $s;
687 $s =~ s#^#$path#gm;
688 print STDOUT "$s\n";
689 });
690}
691
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200692sub cmd_create_ignore {
693 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
694 $gs ||= Git::SVN->new;
695 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
696 $gs->prop_walk($gs->{path}, $r, sub {
697 my ($gs, $path, $props) = @_;
698 # $path is of the form /path/to/dir/
Brian Gernhardt7d9fd452009-02-19 13:08:04 -0500699 $path = '.' . $path;
700 # SVN can have attributes on empty directories,
701 # which git won't track
702 mkpath([$path]) unless -d $path;
703 my $ignore = $path . '.gitignore';
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200704 my $s = $props->{'svn:ignore'} or return;
705 open(GITIGNORE, '>', $ignore)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200706 or fatal("Failed to open `$ignore' for writing: $!");
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200707 $s =~ s/[\r\n]+/\n/g;
708 chomp $s;
709 # Prefix all patterns so that the ignore doesn't apply
710 # to sub-directories.
711 $s =~ s#^#/#gm;
712 print GITIGNORE "$s\n";
713 close(GITIGNORE)
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200714 or fatal("Failed to close `$ignore': $!");
Gustaf Hendebyc4c66b22008-05-05 00:33:09 +0200715 command_noisy('add', '-f', $ignore);
Benoit Sigoured05ddec2007-10-16 16:36:49 +0200716 });
717}
718
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800719sub canonicalize_path {
720 my ($path) = @_;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800721 my $dot_slash_added = 0;
722 if (substr($path, 0, 1) ne "/") {
723 $path = "./" . $path;
724 $dot_slash_added = 1;
725 }
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800726 # File::Spec->canonpath doesn't collapse x/../y into y (for a
727 # good reason), so let's do this manually.
728 $path =~ s#/+#/#g;
729 $path =~ s#/\.(?:/|$)#/#g;
730 $path =~ s#/[^/]+/\.\.##g;
731 $path =~ s#/$##g;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800732 $path =~ s#^\./## if $dot_slash_added;
Gerrit Pape2fe403e2008-07-06 19:28:50 +0000733 $path =~ s#^/##;
734 $path =~ s#^\.$##;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800735 return $path;
736}
737
Benoit Sigoure15153452007-10-16 16:36:50 +0200738# get_svnprops(PATH)
739# ------------------
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200740# Helper for cmd_propget and cmd_proplist below.
Benoit Sigoure15153452007-10-16 16:36:50 +0200741sub get_svnprops {
742 my $path = shift;
743 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
744 $gs ||= Git::SVN->new;
745
746 # prefix THE PATH by the sub-directory from which the user
747 # invoked us.
748 $path = $cmd_dir_prefix . $path;
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200749 fatal("No such file or directory: $path") unless -e $path;
Benoit Sigoure15153452007-10-16 16:36:50 +0200750 my $is_dir = -d $path ? 1 : 0;
751 $path = $gs->{path} . '/' . $path;
752
753 # canonicalize the path (otherwise libsvn will abort or fail to
754 # find the file)
David D. Kilzerb2b3ada2007-11-20 22:43:17 -0800755 $path = canonicalize_path($path);
Benoit Sigoure15153452007-10-16 16:36:50 +0200756
757 my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
758 my $props;
759 if ($is_dir) {
760 (undef, undef, $props) = $gs->ra->get_dir($path, $r);
761 }
762 else {
763 (undef, $props) = $gs->ra->get_file($path, $r, undef);
764 }
765 return $props;
766}
767
768# cmd_propget (PROP, PATH)
769# ------------------------
770# Print the SVN property PROP for PATH.
771sub cmd_propget {
772 my ($prop, $path) = @_;
773 $path = '.' if not defined $path;
774 usage(1) if not defined $prop;
775 my $props = get_svnprops($path);
776 if (not defined $props->{$prop}) {
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200777 fatal("`$path' does not have a `$prop' SVN property.");
Benoit Sigoure15153452007-10-16 16:36:50 +0200778 }
779 print $props->{$prop} . "\n";
780}
781
Benoit Sigoure51e057c2007-10-16 16:36:51 +0200782# cmd_proplist (PATH)
783# -------------------
784# Print the list of SVN properties for PATH.
785sub cmd_proplist {
786 my $path = shift;
787 $path = '.' if not defined $path;
788 my $props = get_svnprops($path);
789 print "Properties on '$path':\n";
790 foreach (sort keys %{$props}) {
791 print " $_\n";
792 }
793}
794
Eric Wong8164b652007-01-11 15:35:55 -0800795sub cmd_multi_init {
Eric Wong9d55b412006-06-12 15:53:13 -0700796 my $url = shift;
Eric Wong98327e52007-01-04 18:02:00 -0800797 unless (defined $_trunk || defined $_branches || defined $_tags) {
798 usage(1);
799 }
Eric Wongdc431662007-05-19 03:59:02 -0700800
801 # there are currently some bugs that prevent multi-init/multi-fetch
802 # setups from working well without this.
803 $Git::SVN::_minimize_url = 1;
804
Eric Wong8164b652007-01-11 15:35:55 -0800805 $_prefix = '' unless defined $_prefix;
Eric Wongdadc6d22007-02-14 12:27:41 -0800806 if (defined $url) {
807 $url =~ s#/+$##;
808 init_subdir(@_);
809 }
Eric Wongf30603f2007-02-23 01:26:26 -0800810 do_git_init_db();
Eric Wong98327e52007-01-04 18:02:00 -0800811 if (defined $_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800812 my $trunk_ref = $_prefix . 'trunk';
813 # try both old-style and new-style lookups:
814 my $gs_trunk = eval { Git::SVN->new($trunk_ref) };
Eric Wong8164b652007-01-11 15:35:55 -0800815 unless ($gs_trunk) {
Eric Wong706587f2007-01-18 17:50:01 -0800816 my ($trunk_url, $trunk_path) =
817 complete_svn_url($url, $_trunk);
818 $gs_trunk = Git::SVN->init($trunk_url, $trunk_path,
819 undef, $trunk_ref);
Eric Wong98327e52007-01-04 18:02:00 -0800820 }
Eric Wongc35b96e2006-10-11 11:53:21 -0700821 }
Eric Wong706587f2007-01-18 17:50:01 -0800822 return unless defined $_branches || defined $_tags;
Eric Wonge7db67e2007-01-11 17:09:26 -0800823 my $ra = $url ? Git::SVN::Ra->new($url) : undef;
824 complete_url_ls_init($ra, $_branches, '--branches/-b', $_prefix);
825 complete_url_ls_init($ra, $_tags, '--tags/-t', $_prefix . 'tags/');
Eric Wong9d55b412006-06-12 15:53:13 -0700826}
827
Eric Wong1c8443b2007-01-14 02:17:00 -0800828sub cmd_multi_fetch {
Eric Wong0af9c9f2007-01-27 22:28:56 -0800829 my $remotes = Git::SVN::read_all_remotes();
830 foreach my $repo_id (sort keys %$remotes) {
Eric Wongdb03cd22007-02-13 00:38:02 -0800831 if ($remotes->{$repo_id}->{url}) {
Eric Wong4bb9ed02007-02-03 13:29:17 -0800832 Git::SVN::fetch_all($repo_id, $remotes);
833 }
Eric Wong706587f2007-01-18 17:50:01 -0800834 }
Eric Wong9d55b412006-06-12 15:53:13 -0700835}
836
Eric Wong44320b92007-01-13 22:35:53 -0800837# this command is special because it requires no metadata
838sub cmd_commit_diff {
839 my ($ta, $tb, $url) = @_;
840 my $usage = "Usage: $0 commit-diff -r<revision> ".
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200841 "<tree-ish> <tree-ish> [<URL>]";
Eric Wong44320b92007-01-13 22:35:53 -0800842 fatal($usage) if (!defined $ta || !defined $tb);
Karl Hasselströmd72ab8c2008-05-17 17:07:09 +0200843 my $svn_path = '';
Eric Wong44320b92007-01-13 22:35:53 -0800844 if (!defined $url) {
845 my $gs = eval { Git::SVN->new };
846 if (!$gs) {
847 fatal("Needed URL or usable git-svn --id in ",
848 "the command-line\n", $usage);
849 }
850 $url = $gs->{url};
Eric Wongd3a840d2007-01-26 01:32:45 -0800851 $svn_path = $gs->{path};
Eric Wong44320b92007-01-13 22:35:53 -0800852 }
853 unless (defined $_revision) {
854 fatal("-r|--revision is a required argument\n", $usage);
855 }
856 if (defined $_message && defined $_file) {
857 fatal("Both --message/-m and --file/-F specified ",
858 "for the commit message.\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +0200859 "I have no idea what you mean");
Eric Wong44320b92007-01-13 22:35:53 -0800860 }
861 if (defined $_file) {
862 $_message = file_to_s($_file);
863 } else {
864 $_message ||= get_commit_entry($tb)->{log};
865 }
866 my $ra ||= Git::SVN::Ra->new($url);
867 my $r = $_revision;
868 if ($r eq 'HEAD') {
869 $r = $ra->get_latest_revnum;
870 } elsif ($r !~ /^\d+$/) {
871 die "revision argument: $r not understood by git-svn\n";
872 }
Eric Wong61395352007-01-27 14:33:08 -0800873 my %ed_opts = ( r => $r,
874 log => $_message,
875 ra => $ra,
876 tree_a => $ta,
877 tree_b => $tb,
878 editor_cb => sub { print "Committed r$_[0]\n" },
879 svn_path => $svn_path );
880 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong44320b92007-01-13 22:35:53 -0800881 print "No changes\n$ta == $tb\n";
882 }
Eric Wong44320b92007-01-13 22:35:53 -0800883}
884
Thomas Rast05427b92008-08-26 21:32:37 +0200885sub escape_uri_only {
886 my ($uri) = @_;
887 my @tmp;
888 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -0700889 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Thomas Rast05427b92008-08-26 21:32:37 +0200890 push @tmp, $_;
891 }
892 join('/', @tmp);
893}
894
895sub escape_url {
896 my ($url) = @_;
897 if ($url =~ m#^([^:]+)://([^/]*)(.*)$#) {
898 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
899 $url = "$scheme://$domain$uri";
900 }
901 $url;
902}
903
David D. Kilzere6fefa92007-11-21 11:57:18 -0800904sub cmd_info {
Eric Wongbd2d4f92008-08-05 00:35:16 -0700905 my $path = canonicalize_path(defined($_[0]) ? $_[0] : ".");
Thomas Rastedde9112008-08-26 21:32:36 +0200906 my $fullpath = canonicalize_path($cmd_dir_prefix . $path);
Eric Wongbd2d4f92008-08-05 00:35:16 -0700907 if (exists $_[1]) {
David D. Kilzere6fefa92007-11-21 11:57:18 -0800908 die "Too many arguments specified\n";
909 }
910
911 my ($file_type, $diff_status) = find_file_type_and_diff_status($path);
912
913 if (!$file_type && !$diff_status) {
Thomas Rast2cf3e3a2008-08-29 15:42:48 +0200914 print STDERR "svn: '$path' is not under version control\n";
915 exit 1;
David D. Kilzere6fefa92007-11-21 11:57:18 -0800916 }
917
918 my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
919 unless ($gs) {
920 die "Unable to determine upstream SVN information from ",
921 "working tree history\n";
922 }
Eric Wongbd2d4f92008-08-05 00:35:16 -0700923
924 # canonicalize_path() will return "" to make libsvn 1.5.x happy,
925 $path = "." if $path eq "";
926
Thomas Rastedde9112008-08-26 21:32:36 +0200927 my $full_url = $url . ($fullpath eq "" ? "" : "/$fullpath");
David D. Kilzere6fefa92007-11-21 11:57:18 -0800928
David D. Kilzer8b014d72007-11-21 11:57:19 -0800929 if ($_url) {
Thomas Rast05427b92008-08-26 21:32:37 +0200930 print escape_url($full_url), "\n";
David D. Kilzer8b014d72007-11-21 11:57:19 -0800931 return;
932 }
933
David D. Kilzere6fefa92007-11-21 11:57:18 -0800934 my $result = "Path: $path\n";
935 $result .= "Name: " . basename($path) . "\n" if $file_type ne "dir";
Thomas Rast05427b92008-08-26 21:32:37 +0200936 $result .= "URL: " . escape_url($full_url) . "\n";
David D. Kilzere6fefa92007-11-21 11:57:18 -0800937
Eric Wonga5460eb2007-11-21 18:20:57 -0800938 eval {
939 my $repos_root = $gs->repos_root;
940 Git::SVN::remove_username($repos_root);
Thomas Rast05427b92008-08-26 21:32:37 +0200941 $result .= "Repository Root: " . escape_url($repos_root) . "\n";
Eric Wonga5460eb2007-11-21 18:20:57 -0800942 };
943 if ($@) {
944 $result .= "Repository Root: (offline)\n";
945 }
Marcel Koeppen22ba47f2009-01-19 03:02:01 +0100946 $result .= "Repository UUID: $uuid\n" unless $diff_status eq "A" &&
947 ($SVN::Core::VERSION le '1.5.4' || $file_type ne "dir");
David D. Kilzere6fefa92007-11-21 11:57:18 -0800948 $result .= "Revision: " . ($diff_status eq "A" ? 0 : $rev) . "\n";
949
950 $result .= "Node Kind: " .
951 ($file_type eq "dir" ? "directory" : "file") . "\n";
952
953 my $schedule = $diff_status eq "A"
954 ? "add"
955 : ($diff_status eq "D" ? "delete" : "normal");
956 $result .= "Schedule: $schedule\n";
957
958 if ($diff_status eq "A") {
959 print $result, "\n";
960 return;
961 }
962
963 my ($lc_author, $lc_rev, $lc_date_utc);
Thomas Rastedde9112008-08-26 21:32:36 +0200964 my @args = Git::SVN::Log::git_svn_log_cmd($rev, $rev, "--", $fullpath);
David D. Kilzere6fefa92007-11-21 11:57:18 -0800965 my $log = command_output_pipe(@args);
966 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
967 while (<$log>) {
968 if (/^${esc_color}author (.+) <[^>]+> (\d+) ([\-\+]?\d+)$/o) {
969 $lc_author = $1;
970 $lc_date_utc = Git::SVN::Log::parse_git_date($2, $3);
971 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
972 (undef, $lc_rev, undef) = ::extract_metadata($1);
973 }
974 }
975 close $log;
976
977 Git::SVN::Log::set_local_timezone();
978
979 $result .= "Last Changed Author: $lc_author\n";
980 $result .= "Last Changed Rev: $lc_rev\n";
981 $result .= "Last Changed Date: " .
982 Git::SVN::Log::format_svn_date($lc_date_utc) . "\n";
983
984 if ($file_type ne "dir") {
985 my $text_last_updated_date =
986 ($diff_status eq "D" ? $lc_date_utc : (stat $path)[9]);
987 $result .=
988 "Text Last Updated: " .
989 Git::SVN::Log::format_svn_date($text_last_updated_date) .
990 "\n";
991 my $checksum;
992 if ($diff_status eq "D") {
993 my ($fh, $ctx) =
994 command_output_pipe(qw(cat-file blob), "HEAD:$path");
995 if ($file_type eq "link") {
996 my $file_name = <$fh>;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -0800997 $checksum = md5sum("link $file_name");
David D. Kilzere6fefa92007-11-21 11:57:18 -0800998 } else {
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -0800999 $checksum = md5sum($fh);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001000 }
1001 command_close_pipe($fh, $ctx);
1002 } elsif ($file_type eq "link") {
1003 my $file_name =
1004 command(qw(cat-file blob), "HEAD:$path");
1005 $checksum =
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001006 md5sum("link " . $file_name);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001007 } else {
1008 open FILE, "<", $path or die $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08001009 $checksum = md5sum(\*FILE);
David D. Kilzere6fefa92007-11-21 11:57:18 -08001010 close FILE or die $!;
1011 }
1012 $result .= "Checksum: " . $checksum . "\n";
1013 }
1014
1015 print $result, "\n";
1016}
1017
Eric Wong3397f9d2006-02-16 01:24:16 -08001018########################### utility functions #########################
1019
Eric Wong905f8b72007-02-16 03:22:40 -08001020sub rebase_cmd {
1021 my @cmd = qw/rebase/;
1022 push @cmd, '-v' if $_verbose;
1023 push @cmd, qw/--merge/ if $_merge;
1024 push @cmd, "--strategy=$_strategy" if $_strategy;
1025 @cmd;
1026}
1027
Eric Wong1e889ef2007-02-16 01:45:13 -08001028sub post_fetch_checkout {
1029 return if $_no_checkout;
1030 my $gs = $Git::SVN::_head or return;
1031 return if verify_ref('refs/heads/master^0');
1032
1033 my $valid_head = verify_ref('HEAD^0');
1034 command_noisy(qw(update-ref refs/heads/master), $gs->refname);
1035 return if ($valid_head || !verify_ref('HEAD^0'));
1036
1037 return if $ENV{GIT_DIR} !~ m#^(?:.*/)?\.git$#;
1038 my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
1039 return if -f $index;
1040
Matthias Lederhofer7ae3df82007-06-03 16:48:16 +02001041 return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
Eric Wong1e889ef2007-02-16 01:45:13 -08001042 return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
1043 command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
1044 print STDERR "Checked out HEAD:\n ",
1045 $gs->full_url, " r", $gs->last_rev, "\n";
1046}
1047
Eric Wong98327e52007-01-04 18:02:00 -08001048sub complete_svn_url {
1049 my ($url, $path) = @_;
1050 $path =~ s#/+$##;
Eric Wong98327e52007-01-04 18:02:00 -08001051 if ($path !~ m#^[a-z\+]+://#) {
Eric Wong98327e52007-01-04 18:02:00 -08001052 if (!defined $url || $url !~ m#^[a-z\+]+://#) {
1053 fatal("E: '$path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001054 "and a separate URL is not specified");
Eric Wong98327e52007-01-04 18:02:00 -08001055 }
Eric Wong706587f2007-01-18 17:50:01 -08001056 return ($url, $path);
Eric Wong98327e52007-01-04 18:02:00 -08001057 }
Eric Wong706587f2007-01-18 17:50:01 -08001058 return ($path, '');
Eric Wong98327e52007-01-04 18:02:00 -08001059}
1060
Eric Wong9d55b412006-06-12 15:53:13 -07001061sub complete_url_ls_init {
Eric Wong706587f2007-01-18 17:50:01 -08001062 my ($ra, $repo_path, $switch, $pfx) = @_;
1063 unless ($repo_path) {
Eric Wong9d55b412006-06-12 15:53:13 -07001064 print STDERR "W: $switch not specified\n";
1065 return;
1066 }
Eric Wong706587f2007-01-18 17:50:01 -08001067 $repo_path =~ s#/+$##;
1068 if ($repo_path =~ m#^[a-z\+]+://#) {
1069 $ra = Git::SVN::Ra->new($repo_path);
1070 $repo_path = '';
Eric Wonge7db67e2007-01-11 17:09:26 -08001071 } else {
Eric Wong706587f2007-01-18 17:50:01 -08001072 $repo_path =~ s#^/+##;
Eric Wonge7db67e2007-01-11 17:09:26 -08001073 unless ($ra) {
Eric Wong706587f2007-01-18 17:50:01 -08001074 fatal("E: '$repo_path' is not a complete URL ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02001075 "and a separate URL is not specified");
Eric Wong9d55b412006-06-12 15:53:13 -07001076 }
Eric Wonge7db67e2007-01-11 17:09:26 -08001077 }
Eric Wong706587f2007-01-18 17:50:01 -08001078 my $url = $ra->{url};
Eric Wongb4d57e52007-02-14 15:10:44 -08001079 my $gs = Git::SVN->init($url, undef, undef, undef, 1);
1080 my $k = "svn-remote.$gs->{repo_id}.url";
1081 my $orig_url = eval { command_oneline(qw/config --get/, $k) };
1082 if ($orig_url && ($orig_url ne $gs->{url})) {
1083 die "$k already set: $orig_url\n",
1084 "wanted to set to: $gs->{url}\n";
Eric Wong88cf4102007-02-01 03:59:07 -08001085 }
Eric Wongb4d57e52007-02-14 15:10:44 -08001086 command_oneline('config', $k, $gs->{url}) unless $orig_url;
Eric Wonged0b9d42008-03-14 11:01:23 -07001087 my $remote_path = "$ra->{svn_path}/$repo_path";
Eric Wongb4d57e52007-02-14 15:10:44 -08001088 $remote_path =~ s#/+#/#g;
1089 $remote_path =~ s#^/##g;
Eric Wonged0b9d42008-03-14 11:01:23 -07001090 $remote_path .= "/*" if $remote_path !~ /\*/;
Eric Wongb4d57e52007-02-14 15:10:44 -08001091 my ($n) = ($switch =~ /^--(\w+)/);
1092 if (length $pfx && $pfx !~ m#/$#) {
1093 die "--prefix='$pfx' must have a trailing slash '/'\n";
Eric Wong9d55b412006-06-12 15:53:13 -07001094 }
Marcus Griep570d35c2008-08-08 01:41:57 -07001095 command_noisy('config',
1096 "svn-remote.$gs->{repo_id}.$n",
1097 "$remote_path:refs/remotes/$pfx*" .
1098 ('/*' x (($remote_path =~ tr/*/*/) - 1)) );
Eric Wong9d55b412006-06-12 15:53:13 -07001099}
1100
Eric Wongaef4e922006-12-15 10:59:54 -08001101sub verify_ref {
1102 my ($ref) = @_;
Eric Wong2c5c1d52006-12-28 01:16:21 -08001103 eval { command_oneline([ 'rev-parse', '--verify', $ref ],
1104 { STDERR => 0 }); };
Eric Wongaef4e922006-12-15 10:59:54 -08001105}
1106
Eric Wonga5e0ced2006-06-12 15:23:48 -07001107sub get_tree_from_treeish {
Eric Wongcf52b8f2006-02-20 10:57:28 -08001108 my ($treeish) = @_;
Eric Wong44320b92007-01-13 22:35:53 -08001109 # $treeish can be a symbolic ref, too:
Eric Wongaef4e922006-12-15 10:59:54 -08001110 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001111 my $expected;
1112 while ($type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001113 ($treeish, $type) = command(qw/cat-file tag/, $treeish);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001114 }
1115 if ($type eq 'commit') {
Eric Wongaef4e922006-12-15 10:59:54 -08001116 $expected = (grep /^tree /, command(qw/cat-file commit/,
1117 $treeish))[0];
Eric Wong44320b92007-01-13 22:35:53 -08001118 ($expected) = ($expected =~ /^tree ($sha1)$/o);
Eric Wongcf52b8f2006-02-20 10:57:28 -08001119 die "Unable to get tree from $treeish\n" unless $expected;
1120 } elsif ($type eq 'tree') {
1121 $expected = $treeish;
1122 } else {
1123 die "$treeish is a $type, expected tree, tag or commit\n";
1124 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07001125 return $expected;
1126}
Eric Wongcf52b8f2006-02-20 10:57:28 -08001127
Eric Wong44320b92007-01-13 22:35:53 -08001128sub get_commit_entry {
1129 my ($treeish) = shift;
1130 my %log_entry = ( log => '', tree => get_tree_from_treeish($treeish) );
1131 my $commit_editmsg = "$ENV{GIT_DIR}/COMMIT_EDITMSG";
1132 my $commit_msg = "$ENV{GIT_DIR}/COMMIT_MSG";
1133 open my $log_fh, '>', $commit_editmsg or croak $!;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001134
Eric Wong44320b92007-01-13 22:35:53 -08001135 my $type = command_oneline(qw/cat-file -t/, $treeish);
Eric Wong4ad45152006-07-09 20:20:48 -07001136 if ($type eq 'commit' || $type eq 'tag') {
Eric Wongaef4e922006-12-15 10:59:54 -08001137 my ($msg_fh, $ctx) = command_output_pipe('cat-file',
Eric Wong44320b92007-01-13 22:35:53 -08001138 $type, $treeish);
Eric Wong3397f9d2006-02-16 01:24:16 -08001139 my $in_msg = 0;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001140 my $author;
1141 my $saw_from = 0;
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001142 my $msgbuf = "";
Eric Wong3397f9d2006-02-16 01:24:16 -08001143 while (<$msg_fh>) {
1144 if (!$in_msg) {
1145 $in_msg = 1 if (/^\s*$/);
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001146 $author = $1 if (/^author (.*>)/);
Eric Wongdf746c52006-03-03 01:20:08 -08001147 } elsif (/^git-svn-id: /) {
Eric Wong44320b92007-01-13 22:35:53 -08001148 # skip this for now, we regenerate the
1149 # correct one on re-fetch anyways
1150 # TODO: set *:merge properties or like...
Eric Wong3397f9d2006-02-16 01:24:16 -08001151 } else {
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001152 if (/^From:/ || /^Signed-off-by:/) {
1153 $saw_from = 1;
1154 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001155 $msgbuf .= $_;
Eric Wong3397f9d2006-02-16 01:24:16 -08001156 }
1157 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001158 $msgbuf =~ s/\s+$//s;
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001159 if ($Git::SVN::_add_author_from && defined($author)
1160 && !$saw_from) {
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001161 $msgbuf .= "\n\nFrom: $author";
Avery Pennarun6aa9ba12008-04-15 21:04:17 -04001162 }
Avery Pennarun328eb9b2008-06-12 19:10:50 -04001163 print $log_fh $msgbuf or croak $!;
Eric Wongaef4e922006-12-15 10:59:54 -08001164 command_close_pipe($msg_fh, $ctx);
Eric Wong3397f9d2006-02-16 01:24:16 -08001165 }
Eric Wong44320b92007-01-13 22:35:53 -08001166 close $log_fh or croak $!;
Eric Wong3397f9d2006-02-16 01:24:16 -08001167
1168 if ($_edit || ($type eq 'tree')) {
1169 my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi';
Eric Wong44320b92007-01-13 22:35:53 -08001170 # TODO: strip out spaces, comments, like git-commit.sh
1171 system($editor, $commit_editmsg);
Eric Wong3397f9d2006-02-16 01:24:16 -08001172 }
Eric Wong44320b92007-01-13 22:35:53 -08001173 rename $commit_editmsg, $commit_msg or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07001174 {
1175 # SVN requires messages to be UTF-8 when entering the repo
1176 local $/;
1177 open $log_fh, '<', $commit_msg or croak $!;
1178 binmode $log_fh;
1179 chomp($log_entry{log} = <$log_fh>);
1180
1181 if (my $enc = Git::config('i18n.commitencoding')) {
1182 require Encode;
1183 Encode::from_to($log_entry{log}, $enc, 'UTF-8');
1184 }
1185 close $log_fh or croak $!;
1186 }
Eric Wong44320b92007-01-13 22:35:53 -08001187 unlink $commit_msg;
1188 \%log_entry;
Eric Wonga5e0ced2006-06-12 15:23:48 -07001189}
1190
Eric Wong3397f9d2006-02-16 01:24:16 -08001191sub s_to_file {
1192 my ($str, $file, $mode) = @_;
1193 open my $fd,'>',$file or croak $!;
1194 print $fd $str,"\n" or croak $!;
1195 close $fd or croak $!;
1196 chmod ($mode &~ umask, $file) if (defined $mode);
1197}
1198
1199sub file_to_s {
1200 my $file = shift;
1201 open my $fd,'<',$file or croak "$!: file: $file\n";
1202 local $/;
1203 my $ret = <$fd>;
1204 close $fd or croak $!;
1205 $ret =~ s/\s*$//s;
1206 return $ret;
1207}
1208
Eric Wongeeb0abe2006-03-03 01:20:08 -08001209# '<svn username> = real-name <email address>' mapping based on git-svnimport:
1210sub load_authors {
1211 open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001212 my $log = $cmd eq 'log';
Eric Wongeeb0abe2006-03-03 01:20:08 -08001213 while (<$authors>) {
1214 chomp;
Richard MUSIL575d0252007-07-17 19:02:57 +02001215 next unless /^(.+?|\(no author\))\s*=\s*(.+?)\s*<(.+)>\s*$/;
Eric Wongeeb0abe2006-03-03 01:20:08 -08001216 my ($user, $name, $email) = ($1, $2, $3);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08001217 if ($log) {
1218 $Git::SVN::Log::rusers{"$name <$email>"} = $user;
1219 } else {
1220 $users{$user} = [$name, $email];
1221 }
Eric Wong79bb8d82006-06-01 02:35:44 -07001222 }
1223 close $authors or croak $!;
1224}
1225
Tom Princee0d10e12007-01-28 16:16:53 -08001226# convert GetOpt::Long specs for use by git-config
Eric Wongb8c92ca2006-05-24 01:40:37 -07001227sub read_repo_config {
Eric Wong706587f2007-01-18 17:50:01 -08001228 return unless -d $ENV{GIT_DIR};
Eric Wongb8c92ca2006-05-24 01:40:37 -07001229 my $opts = shift;
Eric Wong97ae0912007-02-11 15:21:24 -08001230 my @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001231 foreach my $o (keys %$opts) {
Eric Wong97ae0912007-02-11 15:21:24 -08001232 # if we have mixedCase and a long option-only, then
1233 # it's a config-only variable that we don't need for
1234 # the command-line.
1235 push @config_only, $o if ($o =~ /[A-Z]/ && $o =~ /^[a-z]+$/i);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001236 my $v = $opts->{$o};
Eric Wong97ae0912007-02-11 15:21:24 -08001237 my ($key) = ($o =~ /^([a-zA-Z\-]+)/);
Eric Wongb8c92ca2006-05-24 01:40:37 -07001238 $key =~ s/-//g;
Deskin Miller225f1d02008-10-23 15:21:34 -04001239 my $arg = 'git config';
Eric Wongb8c92ca2006-05-24 01:40:37 -07001240 $arg .= ' --int' if ($o =~ /[:=]i$/);
1241 $arg .= ' --bool' if ($o !~ /[:=][sfi]$/);
1242 if (ref $v eq 'ARRAY') {
1243 chomp(my @tmp = `$arg --get-all svn.$key`);
1244 @$v = @tmp if @tmp;
1245 } else {
1246 chomp(my $tmp = `$arg --get svn.$key`);
Eric Wong77742842007-02-10 21:07:12 -08001247 if ($tmp && !($arg =~ / --bool/ && $tmp eq 'false')) {
Eric Wongb8c92ca2006-05-24 01:40:37 -07001248 $$v = $tmp;
1249 }
1250 }
1251 }
Eric Wong97ae0912007-02-11 15:21:24 -08001252 delete @$opts{@config_only} if @config_only;
Eric Wongb8c92ca2006-05-24 01:40:37 -07001253}
1254
Eric Wong79bb8d82006-06-01 02:35:44 -07001255sub extract_metadata {
Eric Wongc1927a82006-06-27 19:39:11 -07001256 my $id = shift or return (undef, undef, undef);
Sam Vilain3dfab992007-06-30 20:56:13 +12001257 my ($url, $rev, $uuid) = ($id =~ /^\s*git-svn-id:\s+(.*)\@(\d+)
Eric Wong79bb8d82006-06-01 02:35:44 -07001258 \s([a-f\d\-]+)$/x);
Eric Wonge70dc782006-11-23 14:54:04 -08001259 if (!defined $rev || !$uuid || !$url) {
Eric Wong79bb8d82006-06-01 02:35:44 -07001260 # some of the original repositories I made had
Pavel Roskin82e5a822006-07-10 01:50:18 -04001261 # identifiers like this:
Sam Vilain3dfab992007-06-30 20:56:13 +12001262 ($rev, $uuid) = ($id =~/^\s*git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
Eric Wong79bb8d82006-06-01 02:35:44 -07001263 }
1264 return ($url, $rev, $uuid);
1265}
1266
Eric Wongc1927a82006-06-27 19:39:11 -07001267sub cmt_metadata {
1268 return extract_metadata((grep(/^git-svn-id: /,
Eric Wongaef4e922006-12-15 10:59:54 -08001269 command(qw/cat-file commit/, shift)))[-1]);
Eric Wongc1927a82006-06-27 19:39:11 -07001270}
1271
Boris Byk6ea42032009-04-11 00:32:41 +04001272sub cmt_sha2rev_batch {
1273 my %s2r;
1274 my ($pid, $in, $out, $ctx) = command_bidi_pipe(qw/cat-file --batch/);
1275 my $list = shift;
1276
1277 foreach my $sha (@{$list}) {
1278 my $first = 1;
1279 my $size = 0;
1280 print $out $sha, "\n";
1281
1282 while (my $line = <$in>) {
1283 if ($first && $line =~ /^[[:xdigit:]]{40}\smissing$/) {
1284 last;
1285 } elsif ($first &&
1286 $line =~ /^[[:xdigit:]]{40}\scommit\s(\d+)$/) {
1287 $first = 0;
1288 $size = $1;
1289 next;
1290 } elsif ($line =~ /^(git-svn-id: )/) {
1291 my (undef, $rev, undef) =
1292 extract_metadata($line);
1293 $s2r{$sha} = $rev;
1294 }
1295
1296 $size -= length($line);
1297 last if ($size == 0);
1298 }
1299 }
1300
1301 command_close_bidi_pipe($pid, $in, $out, $ctx);
1302
1303 return \%s2r;
1304}
1305
Eric Wong905f8b72007-02-16 03:22:40 -08001306sub working_head_info {
1307 my ($head, $refs) = @_;
Pedro Melob6309ac2008-04-10 17:05:21 +01001308 my @args = ('log', '--no-color', '--first-parent', '--pretty=medium');
Lars Hjemli05b4df32007-09-05 11:35:29 +02001309 my ($fh, $ctx) = command_output_pipe(@args, $head);
Sam Vilain3dfab992007-06-30 20:56:13 +12001310 my $hash;
Sam Vilain40cb8f82007-06-30 20:56:14 +12001311 my %max;
Sam Vilain3dfab992007-06-30 20:56:13 +12001312 while (<$fh>) {
1313 if ( m{^commit ($::sha1)$} ) {
1314 unshift @$refs, $hash if $hash and $refs;
1315 $hash = $1;
1316 next;
1317 }
1318 next unless s{^\s*(git-svn-id:)}{$1};
1319 my ($url, $rev, $uuid) = extract_metadata($_);
Eric Wong13c823f2007-04-08 00:59:19 -07001320 if (defined $url && defined $rev) {
Sam Vilain40cb8f82007-06-30 20:56:14 +12001321 next if $max{$url} and $max{$url} < $rev;
Eric Wong13c823f2007-04-08 00:59:19 -07001322 if (my $gs = Git::SVN->find_by_url($url)) {
João Abecasis63c56022008-07-14 16:28:04 +01001323 my $c = $gs->rev_map_get($rev, $uuid);
Adam Robenb03c7a62007-04-25 11:50:32 -07001324 if ($c && $c eq $hash) {
Eric Wong13c823f2007-04-08 00:59:19 -07001325 close $fh; # break the pipe
1326 return ($url, $rev, $uuid, $gs);
Sam Vilain40cb8f82007-06-30 20:56:14 +12001327 } else {
Eric Wong060610c2007-12-08 23:27:41 -08001328 $max{$url} ||= $gs->rev_map_max;
Eric Wong13c823f2007-04-08 00:59:19 -07001329 }
1330 }
1331 }
Eric Wong905f8b72007-02-16 03:22:40 -08001332 }
Eric Wong13c823f2007-04-08 00:59:19 -07001333 command_close_pipe($fh, $ctx);
1334 (undef, undef, undef, undef);
Eric Wong905f8b72007-02-16 03:22:40 -08001335}
1336
Eric Wong733a65a2007-06-13 02:23:28 -07001337sub read_commit_parents {
1338 my ($parents, $c) = @_;
Eric Wong7b02b852007-09-08 16:33:08 -07001339 chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
1340 $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
1341 @{$parents->{$c}} = split(/ /, $p);
Eric Wong733a65a2007-06-13 02:23:28 -07001342}
1343
1344sub linearize_history {
1345 my ($gs, $refs) = @_;
1346 my %parents;
1347 foreach my $c (@$refs) {
1348 read_commit_parents(\%parents, $c);
1349 }
1350
1351 my @linear_refs;
1352 my %skip = ();
1353 my $last_svn_commit = $gs->last_commit;
1354 foreach my $c (reverse @$refs) {
1355 next if $c eq $last_svn_commit;
1356 last if $skip{$c};
1357
1358 unshift @linear_refs, $c;
1359 $skip{$c} = 1;
1360
1361 # we only want the first parent to diff against for linear
1362 # history, we save the rest to inject when we finalize the
1363 # svn commit
1364 my $fp_a = verify_ref("$c~1");
1365 my $fp_b = shift @{$parents{$c}} if $parents{$c};
1366 if (!$fp_a || !$fp_b) {
1367 die "Commit $c\n",
1368 "has no parent commit, and therefore ",
1369 "nothing to diff against.\n",
1370 "You should be working from a repository ",
1371 "originally created by git-svn\n";
1372 }
1373 if ($fp_a ne $fp_b) {
1374 die "$c~1 = $fp_a, however parsing commit $c ",
1375 "revealed that:\n$c~1 = $fp_b\nBUG!\n";
1376 }
1377
1378 foreach my $p (@{$parents{$c}}) {
1379 $skip{$p} = 1;
1380 }
1381 }
1382 (\@linear_refs, \%parents);
1383}
1384
David D. Kilzere6fefa92007-11-21 11:57:18 -08001385sub find_file_type_and_diff_status {
1386 my ($path) = @_;
Dmitry Potapov107cee52008-07-21 00:14:07 +04001387 return ('dir', '') if $path eq '';
David D. Kilzere6fefa92007-11-21 11:57:18 -08001388
1389 my $diff_output =
1390 command_oneline(qw(diff --cached --name-status --), $path) || "";
1391 my $diff_status = (split(' ', $diff_output))[0] || "";
1392
1393 my $ls_tree = command_oneline(qw(ls-tree HEAD), $path) || "";
1394
1395 return (undef, undef) if !$diff_status && !$ls_tree;
1396
1397 if ($diff_status eq "A") {
1398 return ("link", $diff_status) if -l $path;
1399 return ("dir", $diff_status) if -d $path;
1400 return ("file", $diff_status);
1401 }
1402
1403 my $mode = (split(' ', $ls_tree))[0] || "";
1404
1405 return ("link", $diff_status) if $mode eq "120000";
1406 return ("dir", $diff_status) if $mode eq "040000";
1407 return ("file", $diff_status);
1408}
1409
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001410sub md5sum {
1411 my $arg = shift;
1412 my $ref = ref $arg;
1413 my $md5 = Digest::MD5->new();
Marcus Griep0b191382008-08-12 12:00:53 -04001414 if ($ref eq 'GLOB' || $ref eq 'IO::File' || $ref eq 'File::Temp') {
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08001415 $md5->addfile($arg) or croak $!;
1416 } elsif ($ref eq 'SCALAR') {
1417 $md5->add($$arg) or croak $!;
1418 } elsif (!$ref) {
1419 $md5->add($arg) or croak $!;
1420 } else {
1421 ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1422 }
1423 return $md5->hexdigest();
1424}
1425
Eric Wong9b981fc2007-01-11 12:14:21 -08001426package Git::SVN;
1427use strict;
1428use warnings;
Eric Wong060610c2007-12-08 23:27:41 -08001429use Fcntl qw/:DEFAULT :seek/;
1430use constant rev_map_fmt => 'NH40';
Eric Wongecc712d2007-01-31 12:28:10 -08001431use vars qw/$default_repo_id $default_ref_id $_no_metadata $_follow_parent
Eric Wong62e349d2007-02-16 19:57:29 -08001432 $_repack $_repack_flags $_use_svm_props $_head
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00001433 $_use_svnsync_props $no_reuse_existing $_minimize_url
Pete Harlane82f0d72009-01-17 20:10:14 -08001434 $_use_log_author $_add_author_from $_localtime/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001435use Carp qw/croak/;
1436use File::Path qw/mkpath/;
Eric Wong373274f2007-01-31 13:54:23 -08001437use File::Copy qw/copy/;
Eric Wong9b981fc2007-01-11 12:14:21 -08001438use IPC::Open3;
1439
Karl Hasselström94bc9142008-02-03 17:56:18 +01001440my ($_gc_nr, $_gc_period);
1441
Eric Wong9b981fc2007-01-11 12:14:21 -08001442# properties that we do not log:
1443my %SKIP_PROP;
1444BEGIN {
1445 %SKIP_PROP = map { $_ => 1 } qw/svn:wc:ra_dav:version-url
1446 svn:special svn:executable
1447 svn:entry:committed-rev
1448 svn:entry:last-author
1449 svn:entry:uuid
1450 svn:entry:committed-date/;
Eric Wong91b03282007-02-11 00:51:33 -08001451
1452 # some options are read globally, but can be overridden locally
1453 # per [svn-remote "..."] section. Command-line options will *NOT*
1454 # override options set in an [svn-remote "..."] section
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001455 no strict 'refs';
1456 for my $option (qw/follow_parent no_metadata use_svm_props
1457 use_svnsync_props/) {
1458 my $key = $option;
Eric Wong91b03282007-02-11 00:51:33 -08001459 $key =~ tr/_//d;
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001460 my $prop = "-$option";
1461 *$option = sub {
1462 my ($self) = @_;
1463 return $self->{$prop} if exists $self->{$prop};
1464 my $k = "svn-remote.$self->{repo_id}.$key";
1465 eval { command_oneline(qw/config --get/, $k) };
1466 if ($@) {
1467 $self->{$prop} = ${"Git::SVN::_$option"};
Eric Wong91b03282007-02-11 00:51:33 -08001468 } else {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001469 my $v = command_oneline(qw/config --bool/,$k);
1470 $self->{$prop} = $v eq 'false' ? 0 : 1;
Eric Wong91b03282007-02-11 00:51:33 -08001471 }
Sam Vilainc5f71ad2007-06-15 15:43:59 +12001472 return $self->{$prop};
1473 }
Eric Wong91b03282007-02-11 00:51:33 -08001474 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001475}
1476
Marcus Griep0b191382008-08-12 12:00:53 -04001477
Eric Wong321b1842008-01-02 10:10:03 -08001478my (%LOCKFILES, %INDEX_FILES);
1479END {
1480 unlink keys %LOCKFILES if %LOCKFILES;
1481 unlink keys %INDEX_FILES if %INDEX_FILES;
1482}
Eric Wong373274f2007-01-31 13:54:23 -08001483
Eric Wong4bb9ed02007-02-03 13:29:17 -08001484sub resolve_local_globs {
1485 my ($url, $fetch, $glob_spec) = @_;
1486 return unless defined $glob_spec;
1487 my $ref = $glob_spec->{ref};
1488 my $path = $glob_spec->{path};
1489 foreach (command(qw#for-each-ref --format=%(refname) refs/remotes#)) {
1490 next unless m#^refs/remotes/$ref->{regex}$#;
1491 my $p = $1;
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001492 my $pathname = desanitize_refname($path->full_path($p));
1493 my $refname = desanitize_refname($ref->full_path($p));
Eric Wong4bb9ed02007-02-03 13:29:17 -08001494 if (my $existing = $fetch->{$pathname}) {
1495 if ($existing ne $refname) {
1496 die "Refspec conflict:\n",
1497 "existing: refs/remotes/$existing\n",
1498 " globbed: refs/remotes/$refname\n";
1499 }
1500 my $u = (::cmt_metadata("refs/remotes/$refname"))[0];
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001501 $u =~ s!^\Q$url\E(/|$)!! or die
Eric Wong4bb9ed02007-02-03 13:29:17 -08001502 "refs/remotes/$refname: '$url' not found in '$u'\n";
1503 if ($pathname ne $u) {
1504 warn "W: Refspec glob conflict ",
1505 "(ref: refs/remotes/$refname):\n",
1506 "expected path: $pathname\n",
1507 " real path: $u\n",
1508 "Continuing ahead with $u\n";
1509 next;
1510 }
1511 } else {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001512 $fetch->{$pathname} = $refname;
1513 }
1514 }
1515}
1516
Eric Wonge98671e2007-02-14 02:21:19 -08001517sub parse_revision_argument {
1518 my ($base, $head) = @_;
1519 if (!defined $::_revision || $::_revision eq 'BASE:HEAD') {
1520 return ($base, $head);
1521 }
1522 return ($1, $2) if ($::_revision =~ /^(\d+):(\d+)$/);
1523 return ($::_revision, $::_revision) if ($::_revision =~ /^\d+$/);
1524 return ($head, $head) if ($::_revision eq 'HEAD');
1525 return ($base, $1) if ($::_revision =~ /^BASE:(\d+)$/);
1526 return ($1, $head) if ($::_revision =~ /^(\d+):HEAD$/);
1527 die "revision argument: $::_revision not understood by git-svn\n";
1528}
1529
Eric Wong0af9c9f2007-01-27 22:28:56 -08001530sub fetch_all {
Eric Wong4bb9ed02007-02-03 13:29:17 -08001531 my ($repo_id, $remotes) = @_;
Eric Wong905f8b72007-02-16 03:22:40 -08001532 if (ref $repo_id) {
1533 my $gs = $repo_id;
1534 $repo_id = undef;
1535 $repo_id = $gs->{repo_id};
1536 }
1537 $remotes ||= read_all_remotes();
Eric Wong7447b4b2007-02-14 18:38:46 -08001538 my $remote = $remotes->{$repo_id} or
1539 die "[svn-remote \"$repo_id\"] unknown\n";
Eric Wonge5181922007-02-08 12:53:57 -08001540 my $fetch = $remote->{fetch};
Eric Wong7447b4b2007-02-14 18:38:46 -08001541 my $url = $remote->{url} or die "svn-remote.$repo_id.url not defined\n";
Eric Wonge5181922007-02-08 12:53:57 -08001542 my (@gs, @globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001543 my $ra = Git::SVN::Ra->new($url);
Eric Wong26a62d52007-02-12 13:25:25 -08001544 my $uuid = $ra->get_uuid;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001545 my $head = $ra->get_latest_revnum;
Eric Wong28710f72007-02-14 13:32:21 -08001546 my $base = defined $fetch ? $head : 0;
Eric Wonge5181922007-02-08 12:53:57 -08001547
1548 # read the max revs for wildcard expansion (branches/*, tags/*)
1549 foreach my $t (qw/branches tags/) {
1550 defined $remote->{$t} or next;
1551 push @globs, $remote->{$t};
Eric Wong93f26892007-02-11 01:20:26 -08001552 my $max_rev = eval { tmp_config(qw/--int --get/,
1553 "svn-remote.$repo_id.${t}-maxRev") };
1554 if (defined $max_rev && ($max_rev < $base)) {
1555 $base = $max_rev;
Eric Wongd6d33462007-02-16 04:05:33 -08001556 } elsif (!defined $max_rev) {
1557 $base = 0;
Eric Wonge5181922007-02-08 12:53:57 -08001558 }
1559 }
1560
Eric Wongdb03cd22007-02-13 00:38:02 -08001561 if ($fetch) {
1562 foreach my $p (sort keys %$fetch) {
1563 my $gs = Git::SVN->new($fetch->{$p}, $repo_id, $p);
Eric Wong060610c2007-12-08 23:27:41 -08001564 my $lr = $gs->rev_map_max;
Eric Wongdb03cd22007-02-13 00:38:02 -08001565 if (defined $lr) {
1566 $base = $lr if ($lr < $base);
1567 }
1568 push @gs, $gs;
Eric Wong0af9c9f2007-01-27 22:28:56 -08001569 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08001570 }
Eric Wonge98671e2007-02-14 02:21:19 -08001571
1572 ($base, $head) = parse_revision_argument($base, $head);
Eric Wonge5181922007-02-08 12:53:57 -08001573 $ra->gs_fetch_loop_common($base, $head, \@gs, \@globs);
Eric Wong0af9c9f2007-01-27 22:28:56 -08001574}
1575
Eric Wong47e39c52007-01-21 04:27:09 -08001576sub read_all_remotes {
1577 my $r = {};
João Abecasis63c56022008-07-14 16:28:04 +01001578 my $use_svm_props = eval { command_oneline(qw/config --bool
1579 svn.useSvmProps/) };
1580 $use_svm_props = $use_svm_props eq 'true' if $use_svm_props;
Eric Wong8b8fc062007-01-22 11:44:57 -08001581 foreach (grep { s/^svn-remote\.// } command(qw/config -l/)) {
Avery Pennarun61192162008-07-30 16:53:55 -04001582 if (m!^(.+)\.fetch=\s*(.*)\s*:\s*(.+)\s*$!) {
1583 my ($remote, $local_ref, $_remote_ref) = ($1, $2, $3);
1584 die("svn-remote.$remote: remote ref '$_remote_ref' "
1585 . "must start with 'refs/remotes/'\n")
1586 unless $_remote_ref =~ m{^refs/remotes/(.+)};
1587 my $remote_ref = $1;
Eric Wong46cf98b2007-07-14 12:40:32 -07001588 $local_ref =~ s{^/}{};
1589 $r->{$remote}->{fetch}->{$local_ref} = $remote_ref;
João Abecasis63c56022008-07-14 16:28:04 +01001590 $r->{$remote}->{svm} = {} if $use_svm_props;
1591 } elsif (m!^(.+)\.usesvmprops=\s*(.*)\s*$!) {
1592 $r->{$1}->{svm} = {};
Eric Wong47e39c52007-01-21 04:27:09 -08001593 } elsif (m!^(.+)\.url=\s*(.*)\s*$!) {
1594 $r->{$1}->{url} = $2;
Eric Wong4bb9ed02007-02-03 13:29:17 -08001595 } elsif (m!^(.+)\.(branches|tags)=
1596 (.*):refs/remotes/(.+)\s*$/!x) {
1597 my ($p, $g) = ($3, $4);
1598 my $rs = $r->{$1}->{$2} = {
Eric Wonge5181922007-02-08 12:53:57 -08001599 t => $2,
Eric Wong93f26892007-02-11 01:20:26 -08001600 remote => $1,
Eric Wong4bb9ed02007-02-03 13:29:17 -08001601 path => Git::SVN::GlobSpec->new($p),
1602 ref => Git::SVN::GlobSpec->new($g) };
1603 if (length($rs->{ref}->{right}) != 0) {
1604 die "The '*' glob character must be the last ",
1605 "character of '$g'\n";
1606 }
Eric Wong47e39c52007-01-21 04:27:09 -08001607 }
1608 }
João Abecasis63c56022008-07-14 16:28:04 +01001609
1610 map {
1611 if (defined $r->{$_}->{svm}) {
1612 my $svm;
1613 eval {
1614 my $section = "svn-remote.$_";
1615 $svm = {
1616 source => tmp_config('--get',
1617 "$section.svm-source"),
1618 replace => tmp_config('--get',
1619 "$section.svm-replace"),
1620 }
1621 };
1622 $r->{$_}->{svm} = $svm;
1623 }
1624 } keys %$r;
1625
Eric Wong47e39c52007-01-21 04:27:09 -08001626 $r;
1627}
1628
Eric Wongecc712d2007-01-31 12:28:10 -08001629sub init_vars {
Karl Hasselström94bc9142008-02-03 17:56:18 +01001630 $_gc_nr = $_gc_period = 1000;
Karl Hasselströmaf788a62008-02-03 17:56:12 +01001631 if (defined $_repack || defined $_repack_flags) {
1632 warn "Repack options are obsolete; they have no effect.\n";
1633 }
Eric Wongecc712d2007-01-31 12:28:10 -08001634}
1635
Eric Wongb805b442007-01-22 13:52:04 -08001636sub verify_remotes_sanity {
Eric Wong536c4b02007-01-23 11:35:53 -08001637 return unless -d $ENV{GIT_DIR};
Eric Wongb805b442007-01-22 13:52:04 -08001638 my %seen;
1639 foreach (command(qw/config -l/)) {
1640 if (m!^svn-remote\.(?:.+)\.fetch=.*:refs/remotes/(\S+)\s*$!) {
1641 if ($seen{$1}) {
1642 die "Remote ref refs/remote/$1 is tracked by",
1643 "\n \"$_\"\nand\n \"$seen{$1}\"\n",
1644 "Please resolve this ambiguity in ",
1645 "your git configuration file before ",
1646 "continuing\n";
1647 }
1648 $seen{$1} = $_;
1649 }
1650 }
1651}
1652
Eric Wonge6434f82007-01-23 16:29:23 -08001653sub find_existing_remote {
1654 my ($url, $remotes) = @_;
Eric Wongbefc9ad2007-02-17 02:53:07 -08001655 return undef if $no_reuse_existing;
Eric Wonge6434f82007-01-23 16:29:23 -08001656 my $existing;
1657 foreach my $repo_id (keys %$remotes) {
1658 my $u = $remotes->{$repo_id}->{url} or next;
1659 next if $u ne $url;
1660 $existing = $repo_id;
1661 last;
1662 }
1663 $existing;
1664}
1665
1666sub init_remote_config {
Eric Wongd8115c52007-02-01 03:30:31 -08001667 my ($self, $url, $no_write) = @_;
Eric Wonge6434f82007-01-23 16:29:23 -08001668 $url =~ s!/+$!!; # strip trailing slash
1669 my $r = read_all_remotes();
1670 my $existing = find_existing_remote($url, $r);
1671 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001672 unless ($no_write) {
1673 print STDERR "Using existing ",
1674 "[svn-remote \"$existing\"]\n";
1675 }
Eric Wonge6434f82007-01-23 16:29:23 -08001676 $self->{repo_id} = $existing;
Eric Wong4a1bb4c2007-05-13 09:58:14 -07001677 } elsif ($_minimize_url) {
Eric Wonge6434f82007-01-23 16:29:23 -08001678 my $min_url = Git::SVN::Ra->new($url)->minimize_url;
1679 $existing = find_existing_remote($min_url, $r);
1680 if ($existing) {
Eric Wonge5181922007-02-08 12:53:57 -08001681 unless ($no_write) {
1682 print STDERR "Using existing ",
1683 "[svn-remote \"$existing\"]\n";
1684 }
Eric Wonge6434f82007-01-23 16:29:23 -08001685 $self->{repo_id} = $existing;
1686 }
1687 if ($min_url ne $url) {
Eric Wonge5181922007-02-08 12:53:57 -08001688 unless ($no_write) {
1689 print STDERR "Using higher level of URL: ",
1690 "$url => $min_url\n";
1691 }
Eric Wonge6434f82007-01-23 16:29:23 -08001692 my $old_path = $self->{path};
1693 $self->{path} = $url;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08001694 $self->{path} =~ s!^\Q$min_url\E(/|$)!!;
Eric Wonge6434f82007-01-23 16:29:23 -08001695 if (length $old_path) {
1696 $self->{path} .= "/$old_path";
1697 }
1698 $url = $min_url;
1699 }
1700 }
1701 my $orig_url;
1702 if (!$existing) {
1703 # verify that we aren't overwriting anything:
1704 $orig_url = eval {
1705 command_oneline('config', '--get',
1706 "svn-remote.$self->{repo_id}.url")
1707 };
1708 if ($orig_url && ($orig_url ne $url)) {
1709 die "svn-remote.$self->{repo_id}.url already set: ",
1710 "$orig_url\nwanted to set to: $url\n";
1711 }
1712 }
1713 my ($xrepo_id, $xpath) = find_ref($self->refname);
1714 if (defined $xpath) {
1715 die "svn-remote.$xrepo_id.fetch already set to track ",
1716 "$xpath:refs/remotes/", $self->refname, "\n";
1717 }
Eric Wongd8115c52007-02-01 03:30:31 -08001718 unless ($no_write) {
1719 command_noisy('config',
1720 "svn-remote.$self->{repo_id}.url", $url);
Eric Wong46cf98b2007-07-14 12:40:32 -07001721 $self->{path} =~ s{^/}{};
Eric Wongd8115c52007-02-01 03:30:31 -08001722 command_noisy('config', '--add',
1723 "svn-remote.$self->{repo_id}.fetch",
1724 "$self->{path}:".$self->refname);
1725 }
Eric Wonge6434f82007-01-23 16:29:23 -08001726 $self->{url} = $url;
1727}
1728
Eric Wonga8ae2622007-02-13 14:22:11 -08001729sub find_by_url { # repos_root and, path are optional
1730 my ($class, $full_url, $repos_root, $path) = @_;
Adam Roben56973d22007-04-25 12:42:58 -07001731
Eric Wong1a97a502007-02-20 00:43:19 -08001732 return undef unless defined $full_url;
Adam Roben56973d22007-04-25 12:42:58 -07001733 remove_username($full_url);
1734 remove_username($repos_root) if defined $repos_root;
Eric Wonga8ae2622007-02-13 14:22:11 -08001735 my $remotes = read_all_remotes();
1736 if (defined $full_url && defined $repos_root && !defined $path) {
1737 $path = $full_url;
1738 $path =~ s#^\Q$repos_root\E(?:/|$)##;
1739 }
1740 foreach my $repo_id (keys %$remotes) {
1741 my $u = $remotes->{$repo_id}->{url} or next;
Adam Roben56973d22007-04-25 12:42:58 -07001742 remove_username($u);
Eric Wonga8ae2622007-02-13 14:22:11 -08001743 next if defined $repos_root && $repos_root ne $u;
1744
1745 my $fetch = $remotes->{$repo_id}->{fetch} || {};
1746 foreach (qw/branches tags/) {
1747 resolve_local_globs($u, $fetch,
1748 $remotes->{$repo_id}->{$_});
1749 }
1750 my $p = $path;
John Goerzen0bb91d92008-03-08 16:04:05 -06001751 my $rwr = rewrite_root({repo_id => $repo_id});
João Abecasis63c56022008-07-14 16:28:04 +01001752 my $svm = $remotes->{$repo_id}->{svm}
1753 if defined $remotes->{$repo_id}->{svm};
Eric Wonga8ae2622007-02-13 14:22:11 -08001754 unless (defined $p) {
1755 $p = $full_url;
John Goerzen0bb91d92008-03-08 16:04:05 -06001756 my $z = $u;
João Abecasis63c56022008-07-14 16:28:04 +01001757 my $prefix = '';
John Goerzen0bb91d92008-03-08 16:04:05 -06001758 if ($rwr) {
1759 $z = $rwr;
Dévai Tamás1b7e5432009-02-12 00:14:02 +01001760 remove_username($z);
João Abecasis63c56022008-07-14 16:28:04 +01001761 } elsif (defined $svm) {
1762 $z = $svm->{source};
1763 $prefix = $svm->{replace};
1764 $prefix =~ s#^\Q$u\E(?:/|$)##;
1765 $prefix =~ s#/$##;
John Goerzen0bb91d92008-03-08 16:04:05 -06001766 }
João Abecasis63c56022008-07-14 16:28:04 +01001767 $p =~ s#^\Q$z\E(?:/|$)#$prefix# or next;
Eric Wonga8ae2622007-02-13 14:22:11 -08001768 }
1769 foreach my $f (keys %$fetch) {
1770 next if $f ne $p;
1771 return Git::SVN->new($fetch->{$f}, $repo_id, $f);
1772 }
1773 }
1774 undef;
1775}
1776
Eric Wong9b981fc2007-01-11 12:14:21 -08001777sub init {
Eric Wongd8115c52007-02-01 03:30:31 -08001778 my ($class, $url, $path, $repo_id, $ref_id, $no_write) = @_;
Eric Wong706587f2007-01-18 17:50:01 -08001779 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong9b981fc2007-01-11 12:14:21 -08001780 if (defined $url) {
Eric Wongd8115c52007-02-01 03:30:31 -08001781 $self->init_remote_config($url, $no_write);
Eric Wong9b981fc2007-01-11 12:14:21 -08001782 }
Eric Wong9b981fc2007-01-11 12:14:21 -08001783 $self;
1784}
1785
Eric Wong706587f2007-01-18 17:50:01 -08001786sub find_ref {
1787 my ($ref_id) = @_;
1788 foreach (command(qw/config -l/)) {
1789 next unless m!^svn-remote\.(.+)\.fetch=
1790 \s*(.*)\s*:\s*refs/remotes/(.+)\s*$!x;
1791 my ($repo_id, $path, $ref) = ($1, $2, $3);
1792 if ($ref eq $ref_id) {
1793 $path = '' if ($path =~ m#^\./?#);
1794 return ($repo_id, $path);
1795 }
1796 }
1797 (undef, undef, undef);
1798}
1799
Eric Wong9b981fc2007-01-11 12:14:21 -08001800sub new {
Eric Wong706587f2007-01-18 17:50:01 -08001801 my ($class, $ref_id, $repo_id, $path) = @_;
1802 if (defined $ref_id && !defined $repo_id && !defined $path) {
1803 ($repo_id, $path) = find_ref($ref_id);
1804 if (!defined $repo_id) {
1805 die "Could not find a \"svn-remote.*.fetch\" key ",
1806 "in the repository configuration matching: ",
1807 "refs/remotes/$ref_id\n";
1808 }
1809 }
1810 my $self = _new($class, $repo_id, $ref_id, $path);
Eric Wong8b8fc062007-01-22 11:44:57 -08001811 if (!defined $self->{path} || !length $self->{path}) {
1812 my $fetch = command_oneline('config', '--get',
1813 "svn-remote.$repo_id.fetch",
1814 ":refs/remotes/$ref_id\$") or
1815 die "Failed to read \"svn-remote.$repo_id.fetch\" ",
1816 "\":refs/remotes/$ref_id\$\" in config\n";
1817 ($self->{path}, undef) = split(/\s*:\s*/, $fetch);
1818 }
Eric Wong706587f2007-01-18 17:50:01 -08001819 $self->{url} = command_oneline('config', '--get',
1820 "svn-remote.$repo_id.url") or
1821 die "Failed to read \"svn-remote.$repo_id.url\" in config\n";
Eric Wongd6d33462007-02-16 04:05:33 -08001822 $self->rebuild;
Eric Wong9b981fc2007-01-11 12:14:21 -08001823 $self;
1824}
1825
Robert Ewaldbf655fd2007-07-30 11:08:21 +02001826sub refname {
1827 my ($refname) = "refs/remotes/$_[0]->{ref_id}" ;
1828
1829 # It cannot end with a slash /, we'll throw up on this because
1830 # SVN can't have directories with a slash in their name, either:
1831 if ($refname =~ m{/$}) {
1832 die "ref: '$refname' ends with a trailing slash, this is ",
1833 "not permitted by git nor Subversion\n";
1834 }
1835
1836 # It cannot have ASCII control character space, tilde ~, caret ^,
1837 # colon :, question-mark ?, asterisk *, space, or open bracket [
1838 # anywhere.
1839 #
1840 # Additionally, % must be escaped because it is used for escaping
1841 # and we want our escaped refname to be reversible
1842 $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
1843
1844 # no slash-separated component can begin with a dot .
1845 # /.* becomes /%2E*
1846 $refname =~ s{/\.}{/%2E}g;
1847
1848 # It cannot have two consecutive dots .. anywhere
1849 # .. becomes %2E%2E
1850 $refname =~ s{\.\.}{%2E%2E}g;
1851
1852 return $refname;
1853}
1854
1855sub desanitize_refname {
1856 my ($refname) = @_;
1857 $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
1858 return $refname;
1859}
Eric Wong9b981fc2007-01-11 12:14:21 -08001860
Eric Wong26a62d52007-02-12 13:25:25 -08001861sub svm_uuid {
1862 my ($self) = @_;
1863 return $self->{svm}->{uuid} if $self->svm;
1864 $self->ra;
1865 unless ($self->{svm}) {
1866 die "SVM UUID not cached, and reading remotely failed\n";
1867 }
1868 $self->{svm}->{uuid};
1869}
Eric Wong8a49ee92007-02-10 20:46:50 -08001870
Eric Wong26a62d52007-02-12 13:25:25 -08001871sub svm {
1872 my ($self) = @_;
1873 return $self->{svm} if $self->{svm};
1874 my $svm;
Eric Wong8a49ee92007-02-10 20:46:50 -08001875 # see if we have it in our config, first:
1876 eval {
Eric Wong26a62d52007-02-12 13:25:25 -08001877 my $section = "svn-remote.$self->{repo_id}";
1878 $svm = {
Eric Wong93f26892007-02-11 01:20:26 -08001879 source => tmp_config('--get', "$section.svm-source"),
1880 uuid => tmp_config('--get', "$section.svm-uuid"),
Eric Wongbefc9ad2007-02-17 02:53:07 -08001881 replace => tmp_config('--get', "$section.svm-replace"),
Eric Wong8a49ee92007-02-10 20:46:50 -08001882 }
1883 };
Eric Wongbefc9ad2007-02-17 02:53:07 -08001884 if ($svm && $svm->{source} && $svm->{uuid} && $svm->{replace}) {
1885 $self->{svm} = $svm;
1886 }
Eric Wong26a62d52007-02-12 13:25:25 -08001887 $self->{svm};
1888}
1889
1890sub _set_svm_vars {
1891 my ($self, $ra) = @_;
Eric Wongdb03cd22007-02-13 00:38:02 -08001892 return $ra if $self->svm;
Eric Wong26a62d52007-02-12 13:25:25 -08001893
Eric Wongdb03cd22007-02-13 00:38:02 -08001894 my @err = ( "useSvmProps set, but failed to read SVM properties\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08001895 "(svm:source, svm:uuid) ",
Eric Wongdb03cd22007-02-13 00:38:02 -08001896 "from the following URLs:\n" );
1897 sub read_svm_props {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001898 my ($self, $ra, $path, $r) = @_;
1899 my $props = ($ra->get_dir($path, $r))[2];
Eric Wongdb03cd22007-02-13 00:38:02 -08001900 my $src = $props->{'svm:source'};
Eric Wong8a49ee92007-02-10 20:46:50 -08001901 my $uuid = $props->{'svm:uuid'};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001902 return undef if (!$src || !$uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08001903
Eric Wongbefc9ad2007-02-17 02:53:07 -08001904 chomp($src, $uuid);
Eric Wongdb03cd22007-02-13 00:38:02 -08001905
Eric Wong8a49ee92007-02-10 20:46:50 -08001906 $uuid =~ m{^[0-9a-f\-]{30,}$}
1907 or die "doesn't look right - svm:uuid is '$uuid'\n";
Eric Wongbefc9ad2007-02-17 02:53:07 -08001908
1909 # the '!' is used to mark the repos_root!/relative/path
1910 $src =~ s{/?!/?}{/};
Eric Wongdb03cd22007-02-13 00:38:02 -08001911 $src =~ s{/+$}{}; # no trailing slashes please
Eric Wongbefc9ad2007-02-17 02:53:07 -08001912 # username is of no interest
Eric Wongdb03cd22007-02-13 00:38:02 -08001913 $src =~ s{(^[a-z\+]*://)[^/@]*@}{$1};
Eric Wong8a49ee92007-02-10 20:46:50 -08001914
Eric Wongbefc9ad2007-02-17 02:53:07 -08001915 my $replace = $ra->{url};
1916 $replace .= "/$path" if length $path;
1917
Eric Wongdb03cd22007-02-13 00:38:02 -08001918 my $section = "svn-remote.$self->{repo_id}";
Eric Wongbefc9ad2007-02-17 02:53:07 -08001919 tmp_config("$section.svm-source", $src);
1920 tmp_config("$section.svm-replace", $replace);
1921 tmp_config("$section.svm-uuid", $uuid);
1922 $self->{svm} = {
1923 source => $src,
1924 uuid => $uuid,
1925 replace => $replace
1926 };
Eric Wong8a49ee92007-02-10 20:46:50 -08001927 }
Eric Wongdb03cd22007-02-13 00:38:02 -08001928
1929 my $r = $ra->get_latest_revnum;
1930 my $path = $self->{path};
Eric Wongbefc9ad2007-02-17 02:53:07 -08001931 my %tried;
Eric Wongdb03cd22007-02-13 00:38:02 -08001932 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001933 unless ($tried{"$self->{url}/$path"}) {
1934 return $ra if $self->read_svm_props($ra, $path, $r);
1935 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001936 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001937 $path =~ s#/?[^/]+$##;
Eric Wong8a49ee92007-02-10 20:46:50 -08001938 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001939 die "Path: '$path' should be ''\n" if $path ne '';
1940 return $ra if $self->read_svm_props($ra, $path, $r);
1941 $tried{"$self->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001942
1943 if ($ra->{repos_root} eq $self->{url}) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001944 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001945 }
1946
1947 # nope, make sure we're connected to the repository root:
1948 my $ok;
1949 my @tried_b;
1950 $path = $ra->{svn_path};
Eric Wongdb03cd22007-02-13 00:38:02 -08001951 $ra = Git::SVN::Ra->new($ra->{repos_root});
1952 while (length $path) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001953 unless ($tried{"$ra->{url}/$path"}) {
1954 $ok = $self->read_svm_props($ra, $path, $r);
1955 last if $ok;
1956 $tried{"$ra->{url}/$path"} = 1;
1957 }
1958 $path =~ s#/?[^/]+$##;
Eric Wongdb03cd22007-02-13 00:38:02 -08001959 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08001960 die "Path: '$path' should be ''\n" if $path ne '';
1961 $ok ||= $self->read_svm_props($ra, $path, $r);
1962 $tried{"$ra->{url}/$path"} = 1;
Eric Wongdb03cd22007-02-13 00:38:02 -08001963 if (!$ok) {
Eric Wongbefc9ad2007-02-17 02:53:07 -08001964 die @err, (map { " $_\n" } keys %tried), "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08001965 }
1966 Git::SVN::Ra->new($self->{url});
Eric Wong8a49ee92007-02-10 20:46:50 -08001967}
1968
Eric Wong62e349d2007-02-16 19:57:29 -08001969sub svnsync {
1970 my ($self) = @_;
1971 return $self->{svnsync} if $self->{svnsync};
1972
1973 if ($self->no_metadata) {
1974 die "Can't have both 'noMetadata' and ",
1975 "'useSvnsyncProps' options set!\n";
1976 }
1977 if ($self->rewrite_root) {
1978 die "Can't have both 'useSvnsyncProps' and 'rewriteRoot' ",
1979 "options set!\n";
1980 }
1981
1982 my $svnsync;
1983 # see if we have it in our config, first:
1984 eval {
1985 my $section = "svn-remote.$self->{repo_id}";
Eric Wong98fa5b62008-01-11 23:13:55 -08001986
1987 my $url = tmp_config('--get', "$section.svnsync-url");
1988 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
1989 die "doesn't look right - svn:sync-from-url is '$url'\n";
1990
1991 my $uuid = tmp_config('--get', "$section.svnsync-uuid");
1992 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
1993 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
1994
1995 $svnsync = { url => $url, uuid => $uuid }
Eric Wong62e349d2007-02-16 19:57:29 -08001996 };
1997 if ($svnsync && $svnsync->{url} && $svnsync->{uuid}) {
1998 return $self->{svnsync} = $svnsync;
1999 }
2000
2001 my $err = "useSvnsyncProps set, but failed to read " .
2002 "svnsync property: svn:sync-from-";
2003 my $rp = $self->ra->rev_proplist(0);
2004
2005 my $url = $rp->{'svn:sync-from-url'} or die $err . "url\n";
Eric Wong98fa5b62008-01-11 23:13:55 -08002006 ($url) = ($url =~ m{^([a-z\+]+://\S+)$}) or
Eric Wong62e349d2007-02-16 19:57:29 -08002007 die "doesn't look right - svn:sync-from-url is '$url'\n";
2008
2009 my $uuid = $rp->{'svn:sync-from-uuid'} or die $err . "uuid\n";
Eric Wong98fa5b62008-01-11 23:13:55 -08002010 ($uuid) = ($uuid =~ m{^([0-9a-f\-]{30,})$}) or
Eric Wong62e349d2007-02-16 19:57:29 -08002011 die "doesn't look right - svn:sync-from-uuid is '$uuid'\n";
2012
2013 my $section = "svn-remote.$self->{repo_id}";
2014 tmp_config('--add', "$section.svnsync-uuid", $uuid);
2015 tmp_config('--add', "$section.svnsync-url", $url);
2016 return $self->{svnsync} = { url => $url, uuid => $uuid };
2017}
2018
Eric Wong26a62d52007-02-12 13:25:25 -08002019# this allows us to memoize our SVN::Ra UUID locally and avoid a
2020# remote lookup (useful for 'git svn log').
2021sub ra_uuid {
2022 my ($self) = @_;
2023 unless ($self->{ra_uuid}) {
2024 my $key = "svn-remote.$self->{repo_id}.uuid";
2025 my $uuid = eval { tmp_config('--get', $key) };
2026 if (!$@ && $uuid && $uuid =~ /^([a-f\d\-]{30,})$/) {
2027 $self->{ra_uuid} = $uuid;
2028 } else {
2029 die "ra_uuid called without URL\n" unless $self->{url};
2030 $self->{ra_uuid} = $self->ra->get_uuid;
2031 tmp_config('--add', $key, $self->{ra_uuid});
2032 }
2033 }
2034 $self->{ra_uuid};
2035}
2036
Eric Wonga5460eb2007-11-21 18:20:57 -08002037sub _set_repos_root {
2038 my ($self, $repos_root) = @_;
2039 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2040 $repos_root ||= $self->ra->{repos_root};
2041 tmp_config($k, $repos_root);
2042 $repos_root;
2043}
2044
2045sub repos_root {
2046 my ($self) = @_;
2047 my $k = "svn-remote.$self->{repo_id}.reposRoot";
2048 eval { tmp_config('--get', $k) } || $self->_set_repos_root;
2049}
2050
Eric Wong9b981fc2007-01-11 12:14:21 -08002051sub ra {
2052 my ($self) = shift;
Eric Wong8a49ee92007-02-10 20:46:50 -08002053 my $ra = Git::SVN::Ra->new($self->{url});
Eric Wonga5460eb2007-11-21 18:20:57 -08002054 $self->_set_repos_root($ra->{repos_root});
Eric Wong91b03282007-02-11 00:51:33 -08002055 if ($self->use_svm_props && !$self->{svm}) {
2056 if ($self->no_metadata) {
Eric Wong97ae0912007-02-11 15:21:24 -08002057 die "Can't have both 'noMetadata' and ",
2058 "'useSvmProps' options set!\n";
Eric Wong62e349d2007-02-16 19:57:29 -08002059 } elsif ($self->use_svnsync_props) {
2060 die "Can't have both 'useSvnsyncProps' and ",
2061 "'useSvmProps' options set!\n";
Eric Wong91b03282007-02-11 00:51:33 -08002062 }
Eric Wong26a62d52007-02-12 13:25:25 -08002063 $ra = $self->_set_svm_vars($ra);
Eric Wong8a49ee92007-02-10 20:46:50 -08002064 $self->{-want_revprops} = 1;
2065 }
2066 $ra;
Eric Wong9b981fc2007-01-11 12:14:21 -08002067}
2068
Eric Wong15710b62007-01-22 02:20:33 -08002069sub rel_path {
2070 my ($self) = @_;
2071 my $repos_root = $self->ra->{repos_root};
2072 return $self->{path} if ($self->{url} eq $repos_root);
Eric Wong0b594512007-03-25 16:35:31 -07002073 my $url = $self->{url} .
2074 (length $self->{path} ? "/$self->{path}" : $self->{path});
2075 $url =~ s!^\Q$repos_root\E(?:/+|$)!!g;
2076 $url;
Eric Wong15710b62007-01-22 02:20:33 -08002077}
2078
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002079# prop_walk(PATH, REV, SUB)
2080# -------------------------
2081# Recursively traverse PATH at revision REV and invoke SUB for each
2082# directory that contains a SVN property. SUB will be invoked as
2083# follows: &SUB(gs, path, props); where `gs' is this instance of
2084# Git::SVN, `path' the path to the directory where the properties
2085# `props' were found. The `path' will be relative to point of checkout,
2086# that is, if url://repo/trunk is the current Git branch, and that
2087# directory contains a sub-directory `d', SUB will be invoked with `/d/'
2088# as `path' (note the trailing `/').
2089sub prop_walk {
2090 my ($self, $path, $rev, $sub) = @_;
2091
Kevin Ballard35cda062008-01-09 01:37:20 -05002092 $path =~ s#^/##;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002093 my ($dirent, undef, $props) = $self->ra->get_dir($path, $rev);
2094 $path =~ s#^/*#/#g;
Eric Wong9b981fc2007-01-11 12:14:21 -08002095 my $p = $path;
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002096 # Strip the irrelevant part of the path.
2097 $p =~ s#^/+\Q$self->{path}\E(/|$)#/#;
2098 # Ensure the path is terminated by a `/'.
2099 $p =~ s#/*$#/#;
2100
2101 # The properties contain all the internal SVN stuff nobody
2102 # (usually) cares about.
2103 my $interesting_props = 0;
2104 foreach (keys %{$props}) {
2105 # If it doesn't start with `svn:', it must be a
2106 # user-defined property.
2107 ++$interesting_props and next if $_ !~ /^svn:/;
2108 # FIXME: Fragile, if SVN adds new public properties,
2109 # this needs to be updated.
2110 ++$interesting_props if /^svn:(?:ignore|keywords|executable
2111 |eol-style|mime-type
2112 |externals|needs-lock)$/x;
Eric Wong9b981fc2007-01-11 12:14:21 -08002113 }
Benoit Sigoure01bdab82007-10-16 16:36:48 +02002114 &$sub($self, $p, $props) if $interesting_props;
2115
Eric Wong9b981fc2007-01-11 12:14:21 -08002116 foreach (sort keys %$dirent) {
Eric Wong0dc03d62007-05-13 01:04:43 -07002117 next if $dirent->{$_}->{kind} != $SVN::Node::dir;
Christian Engwerb7166cc2008-05-27 08:46:55 +00002118 $self->prop_walk($self->{path} . $p . $_, $rev, $sub);
Eric Wong9b981fc2007-01-11 12:14:21 -08002119 }
2120}
2121
Eric Wong3ebe8df2007-01-25 17:35:40 -08002122sub last_rev { ($_[0]->last_rev_commit)[0] }
2123sub last_commit { ($_[0]->last_rev_commit)[1] }
2124
Eric Wong9b981fc2007-01-11 12:14:21 -08002125# returns the newest SVN revision number and newest commit SHA1
2126sub last_rev_commit {
2127 my ($self) = @_;
2128 if (defined $self->{last_rev} && defined $self->{last_commit}) {
2129 return ($self->{last_rev}, $self->{last_commit});
2130 }
Eric Wongd2866f92007-01-11 12:26:16 -08002131 my $c = ::verify_ref($self->refname.'^0');
Eric Wong91b03282007-02-11 00:51:33 -08002132 if ($c && !$self->use_svm_props && !$self->no_metadata) {
Eric Wongd2866f92007-01-11 12:26:16 -08002133 my $rev = (::cmt_metadata($c))[1];
Eric Wong9b981fc2007-01-11 12:14:21 -08002134 if (defined $rev) {
2135 ($self->{last_rev}, $self->{last_commit}) = ($rev, $c);
2136 return ($rev, $c);
2137 }
2138 }
Eric Wong060610c2007-12-08 23:27:41 -08002139 my $map_path = $self->map_path;
2140 unless (-e $map_path) {
Eric Wong26a62d52007-02-12 13:25:25 -08002141 ($self->{last_rev}, $self->{last_commit}) = (undef, undef);
2142 return (undef, undef);
2143 }
Eric Wong66ab84b2007-12-08 23:27:42 -08002144 my ($rev, $commit) = $self->rev_map_max(1);
Eric Wong060610c2007-12-08 23:27:41 -08002145 ($self->{last_rev}, $self->{last_commit}) = ($rev, $commit);
2146 return ($rev, $commit);
Eric Wong9b981fc2007-01-11 12:14:21 -08002147}
2148
Eric Wong3ebe8df2007-01-25 17:35:40 -08002149sub get_fetch_range {
2150 my ($self, $min, $max) = @_;
2151 $max ||= $self->ra->get_latest_revnum;
Eric Wong060610c2007-12-08 23:27:41 -08002152 $min ||= $self->rev_map_max;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002153 (++$min, $max);
Eric Wong9b981fc2007-01-11 12:14:21 -08002154}
2155
Eric Wong8a49ee92007-02-10 20:46:50 -08002156sub tmp_config {
Eric Wong93f26892007-02-11 01:20:26 -08002157 my (@args) = @_;
Eric Wongb7e53482007-02-16 04:09:28 -08002158 my $old_def_config = "$ENV{GIT_DIR}/svn/config";
2159 my $config = "$ENV{GIT_DIR}/svn/.metadata";
Eric Wong38570a42007-06-13 02:37:05 -07002160 if (! -f $config && -f $old_def_config) {
Eric Wongb7e53482007-02-16 04:09:28 -08002161 rename $old_def_config, $config or
2162 die "Failed rename $old_def_config => $config: $!\n";
2163 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002164 my $old_config = $ENV{GIT_CONFIG};
Eric Wong93f26892007-02-11 01:20:26 -08002165 $ENV{GIT_CONFIG} = $config;
Eric Wong8a49ee92007-02-10 20:46:50 -08002166 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002167 my @ret = eval {
2168 unless (-f $config) {
2169 mkfile($config);
2170 open my $fh, '>', $config or
2171 die "Can't open $config: $!\n";
2172 print $fh "; This file is used internally by ",
2173 "git-svn\n" or die
2174 "Couldn't write to $config: $!\n";
2175 print $fh "; You should not have to edit it\n" or
2176 die "Couldn't write to $config: $!\n";
2177 close $fh or die "Couldn't close $config: $!\n";
2178 }
2179 command('config', @args);
2180 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002181 my $err = $@;
2182 if (defined $old_config) {
2183 $ENV{GIT_CONFIG} = $old_config;
2184 } else {
2185 delete $ENV{GIT_CONFIG};
2186 }
2187 die $err if $err;
2188 wantarray ? @ret : $ret[0];
2189}
2190
Eric Wong9b981fc2007-01-11 12:14:21 -08002191sub tmp_index_do {
2192 my ($self, $sub) = @_;
2193 my $old_index = $ENV{GIT_INDEX_FILE};
2194 $ENV{GIT_INDEX_FILE} = $self->{index};
Eric Wong8a49ee92007-02-10 20:46:50 -08002195 $@ = undef;
Eric Wongb4d57e52007-02-14 15:10:44 -08002196 my @ret = eval {
2197 my ($dir, $base) = ($self->{index} =~ m#^(.*?)/?([^/]+)$#);
2198 mkpath([$dir]) unless -d $dir;
2199 &$sub;
2200 };
Eric Wong8a49ee92007-02-10 20:46:50 -08002201 my $err = $@;
2202 if (defined $old_index) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002203 $ENV{GIT_INDEX_FILE} = $old_index;
2204 } else {
2205 delete $ENV{GIT_INDEX_FILE};
2206 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002207 die $err if $err;
Eric Wong9b981fc2007-01-11 12:14:21 -08002208 wantarray ? @ret : $ret[0];
2209}
2210
2211sub assert_index_clean {
2212 my ($self, $treeish) = @_;
2213
2214 $self->tmp_index_do(sub {
2215 command_noisy('read-tree', $treeish) unless -e $self->{index};
2216 my $x = command_oneline('write-tree');
2217 my ($y) = (command(qw/cat-file commit/, $treeish) =~
2218 /^tree ($::sha1)/mo);
Eric Wonge8d120b2007-02-14 16:29:52 -08002219 return if $y eq $x;
2220
2221 warn "Index mismatch: $y != $x\nrereading $treeish\n";
2222 unlink $self->{index} or die "unlink $self->{index}: $!\n";
2223 command_noisy('read-tree', $treeish);
Eric Wong9b981fc2007-01-11 12:14:21 -08002224 $x = command_oneline('write-tree');
2225 if ($y ne $x) {
2226 ::fatal "trees ($treeish) $y != $x\n",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02002227 "Something is seriously wrong...";
Eric Wong9b981fc2007-01-11 12:14:21 -08002228 }
2229 });
2230}
2231
2232sub get_commit_parents {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002233 my ($self, $log_entry) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002234 my (%seen, @ret, @tmp);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002235 # legacy support for 'set-tree'; this is only used by set_tree_cb:
2236 if (my $ip = $self->{inject_parents}) {
2237 if (my $commit = delete $ip->{$log_entry->{revision}}) {
2238 push @tmp, $commit;
Eric Wong9b981fc2007-01-11 12:14:21 -08002239 }
2240 }
Eric Wongd2866f92007-01-11 12:26:16 -08002241 if (my $cur = ::verify_ref($self->refname.'^0')) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002242 push @tmp, $cur;
2243 }
Eric Wong733a65a2007-06-13 02:23:28 -07002244 if (my $ipd = $self->{inject_parents_dcommit}) {
2245 if (my $commit = delete $ipd->{$log_entry->{revision}}) {
2246 push @tmp, @$commit;
2247 }
2248 }
Eric Wong44320b92007-01-13 22:35:53 -08002249 push @tmp, $_ foreach (@{$log_entry->{parents}}, @tmp);
Eric Wong9b981fc2007-01-11 12:14:21 -08002250 while (my $p = shift @tmp) {
2251 next if $seen{$p};
2252 $seen{$p} = 1;
2253 push @ret, $p;
2254 # MAXPARENT is defined to 16 in commit-tree.c:
2255 last if @ret >= 16;
2256 }
2257 if (@tmp) {
Eric Wong44320b92007-01-13 22:35:53 -08002258 die "r$log_entry->{revision}: No room for parents:\n\t",
Eric Wong9b981fc2007-01-11 12:14:21 -08002259 join("\n\t", @tmp), "\n";
2260 }
2261 @ret;
2262}
2263
Eric Wongaea736c2007-02-16 19:15:21 -08002264sub rewrite_root {
2265 my ($self) = @_;
2266 return $self->{-rewrite_root} if exists $self->{-rewrite_root};
2267 my $k = "svn-remote.$self->{repo_id}.rewriteRoot";
2268 my $rwr = eval { command_oneline(qw/config --get/, $k) };
2269 if ($rwr) {
2270 $rwr =~ s#/+$##;
2271 if ($rwr !~ m#^[a-z\+]+://#) {
2272 die "$rwr is not a valid URL (key: $k)\n";
2273 }
2274 }
2275 $self->{-rewrite_root} = $rwr;
2276}
2277
2278sub metadata_url {
2279 my ($self) = @_;
2280 ($self->rewrite_root || $self->{url}) .
2281 (length $self->{path} ? '/' . $self->{path} : '');
2282}
2283
Eric Wong706587f2007-01-18 17:50:01 -08002284sub full_url {
Eric Wong9b981fc2007-01-11 12:14:21 -08002285 my ($self) = @_;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08002286 $self->{url} . (length $self->{path} ? '/' . $self->{path} : '');
Eric Wong9b981fc2007-01-11 12:14:21 -08002287}
2288
Eric Wongad948022007-12-15 19:08:22 -08002289
2290sub set_commit_header_env {
2291 my ($log_entry) = @_;
2292 my %env;
2293 foreach my $ned (qw/NAME EMAIL DATE/) {
2294 foreach my $ac (qw/AUTHOR COMMITTER/) {
2295 $env{"GIT_${ac}_${ned}"} = $ENV{"GIT_${ac}_${ned}"};
2296 }
2297 }
2298
2299 $ENV{GIT_AUTHOR_NAME} = $log_entry->{name};
2300 $ENV{GIT_AUTHOR_EMAIL} = $log_entry->{email};
2301 $ENV{GIT_AUTHOR_DATE} = $ENV{GIT_COMMITTER_DATE} = $log_entry->{date};
2302
2303 $ENV{GIT_COMMITTER_NAME} = (defined $log_entry->{commit_name})
2304 ? $log_entry->{commit_name}
2305 : $log_entry->{name};
2306 $ENV{GIT_COMMITTER_EMAIL} = (defined $log_entry->{commit_email})
2307 ? $log_entry->{commit_email}
2308 : $log_entry->{email};
2309 \%env;
2310}
2311
2312sub restore_commit_header_env {
2313 my ($env) = @_;
2314 foreach my $ned (qw/NAME EMAIL DATE/) {
2315 foreach my $ac (qw/AUTHOR COMMITTER/) {
2316 my $k = "GIT_${ac}_${ned}";
2317 if (defined $env->{$k}) {
2318 $ENV{$k} = $env->{$k};
2319 } else {
2320 delete $ENV{$k};
2321 }
2322 }
2323 }
2324}
2325
Karl Hasselström94bc9142008-02-03 17:56:18 +01002326sub gc {
2327 command_noisy('gc', '--auto');
2328};
2329
Eric Wong9b981fc2007-01-11 12:14:21 -08002330sub do_git_commit {
Eric Wong0af9c9f2007-01-27 22:28:56 -08002331 my ($self, $log_entry) = @_;
Eric Wong8a603772007-01-31 02:45:50 -08002332 my $lr = $self->last_rev;
2333 if (defined $lr && $lr >= $log_entry->{revision}) {
2334 die "Last fetched revision of ", $self->refname,
2335 " was r$lr, but we are about to fetch: ",
2336 "r$log_entry->{revision}!\n";
2337 }
Eric Wong060610c2007-12-08 23:27:41 -08002338 if (my $c = $self->rev_map_get($log_entry->{revision})) {
Eric Wong44320b92007-01-13 22:35:53 -08002339 croak "$log_entry->{revision} = $c already exists! ",
Eric Wong9b981fc2007-01-11 12:14:21 -08002340 "Why are we refetching it?\n";
2341 }
Eric Wongad948022007-12-15 19:08:22 -08002342 my $old_env = set_commit_header_env($log_entry);
Eric Wong44320b92007-01-13 22:35:53 -08002343 my $tree = $log_entry->{tree};
Eric Wong9b981fc2007-01-11 12:14:21 -08002344 if (!defined $tree) {
2345 $tree = $self->tmp_index_do(sub {
2346 command_oneline('write-tree') });
2347 }
2348 die "Tree is not a valid sha1: $tree\n" if $tree !~ /^$::sha1$/o;
2349
Deskin Millere855bfc2008-10-31 00:10:25 -04002350 my @exec = ('git', 'commit-tree', $tree);
Eric Wong0af9c9f2007-01-27 22:28:56 -08002351 foreach ($self->get_commit_parents($log_entry)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002352 push @exec, '-p', $_;
2353 }
2354 defined(my $pid = open3(my $msg_fh, my $out_fh, '>&STDERR', @exec))
2355 or croak $!;
Eric Wong16fc08e2008-10-29 23:49:26 -07002356 binmode $msg_fh;
2357
2358 # we always get UTF-8 from SVN, but we may want our commits in
2359 # a different encoding.
2360 if (my $enc = Git::config('i18n.commitencoding')) {
2361 require Encode;
2362 Encode::from_to($log_entry->{log}, 'UTF-8', $enc);
2363 }
Eric Wong44320b92007-01-13 22:35:53 -08002364 print $msg_fh $log_entry->{log} or croak $!;
Eric Wongad948022007-12-15 19:08:22 -08002365 restore_commit_header_env($old_env);
Eric Wong91b03282007-02-11 00:51:33 -08002366 unless ($self->no_metadata) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002367 print $msg_fh "\ngit-svn-id: $log_entry->{metadata}\n"
2368 or croak $!;
Eric Wong9760adc2007-01-31 03:06:56 -08002369 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002370 $msg_fh->flush == 0 or croak $!;
2371 close $msg_fh or croak $!;
2372 chomp(my $commit = do { local $/; <$out_fh> });
2373 close $out_fh or croak $!;
2374 waitpid $pid, 0;
2375 croak $? if $?;
2376 if ($commit !~ /^$::sha1$/o) {
2377 die "Failed to commit, invalid sha1: $commit\n";
2378 }
2379
Eric Wong060610c2007-12-08 23:27:41 -08002380 $self->rev_map_set($log_entry->{revision}, $commit, 1);
Eric Wong9b981fc2007-01-11 12:14:21 -08002381
Eric Wong44320b92007-01-13 22:35:53 -08002382 $self->{last_rev} = $log_entry->{revision};
Eric Wong9b981fc2007-01-11 12:14:21 -08002383 $self->{last_commit} = $commit;
Simon Arlott49750f32009-03-30 19:31:41 +01002384 print "r$log_entry->{revision}" unless $::_q > 1;
Eric Wong8a49ee92007-02-10 20:46:50 -08002385 if (defined $log_entry->{svm_revision}) {
Simon Arlott49750f32009-03-30 19:31:41 +01002386 print " (\@$log_entry->{svm_revision})" unless $::_q > 1;
Eric Wong060610c2007-12-08 23:27:41 -08002387 $self->rev_map_set($log_entry->{svm_revision}, $commit,
Eric Wong26a62d52007-02-12 13:25:25 -08002388 0, $self->svm_uuid);
Eric Wong8a49ee92007-02-10 20:46:50 -08002389 }
Simon Arlott49750f32009-03-30 19:31:41 +01002390 print " = $commit ($self->{ref_id})\n" unless $::_q > 1;
Karl Hasselström94bc9142008-02-03 17:56:18 +01002391 if (--$_gc_nr == 0) {
2392 $_gc_nr = $_gc_period;
2393 gc();
2394 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002395 return $commit;
2396}
2397
Eric Wongfbcc1732007-02-06 18:35:30 -08002398sub match_paths {
2399 my ($self, $paths, $r) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08002400 return 1 if $self->{path} eq '';
Eric Wongd542aed2007-02-09 02:19:41 -08002401 if (my $path = $paths->{"/$self->{path}"}) {
2402 return ($path->{action} eq 'D') ? 0 : 1;
2403 }
Michael Lai9162b862009-03-09 11:45:47 -07002404 my $repos_root = $self->ra->{repos_root};
2405 my $extended_path = $self->{url} . '/' . $self->{path};
2406 $extended_path =~ s#^\Q$repos_root\E(/|$)##;
2407 $self->{path_regex} ||= qr/^\/\Q$extended_path\E\//;
Eric Wongfbcc1732007-02-06 18:35:30 -08002408 if (grep /$self->{path_regex}/, keys %$paths) {
2409 return 1;
2410 }
2411 my $c = '';
2412 foreach (split m#/#, $self->{path}) {
2413 $c .= "/$_";
Eric Wong74a81222007-02-10 13:28:50 -08002414 next unless ($paths->{$c} &&
2415 ($paths->{$c}->{action} =~ /^[AR]$/));
Eric Wonge5181922007-02-08 12:53:57 -08002416 if ($self->ra->check_path($self->{path}, $r) ==
2417 $SVN::Node::dir) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002418 return 1;
2419 }
2420 }
2421 return 0;
2422}
2423
Eric Wong15710b62007-01-22 02:20:33 -08002424sub find_parent_branch {
2425 my ($self, $paths, $rev) = @_;
Eric Wong91b03282007-02-11 00:51:33 -08002426 return undef unless $self->follow_parent;
Eric Wonge5a0b242007-01-25 15:44:54 -08002427 unless (defined $paths) {
Eric Wongc7eba712007-01-31 03:45:28 -08002428 my $err_handler = $SVN::Error::handler;
2429 $SVN::Error::handler = \&Git::SVN::Ra::skip_unknown_revs;
Eric Wongd4eff2b2007-01-30 14:04:22 -08002430 $self->ra->get_log([$self->{path}], $rev, $rev, 0, 1, 1, sub {
2431 $paths =
2432 Git::SVN::Ra::dup_changed_paths($_[0]) });
Eric Wongc7eba712007-01-31 03:45:28 -08002433 $SVN::Error::handler = $err_handler;
Eric Wonge5a0b242007-01-25 15:44:54 -08002434 }
2435 return undef unless defined $paths;
Eric Wong15710b62007-01-22 02:20:33 -08002436
2437 # look for a parent from another branch:
Eric Wong7f578c52007-01-24 02:16:25 -08002438 my @b_path_components = split m#/#, $self->rel_path;
2439 my @a_path_components;
2440 my $i;
2441 while (@b_path_components) {
2442 $i = $paths->{'/'.join('/', @b_path_components)};
Eric Wong74a81222007-02-10 13:28:50 -08002443 last if $i && defined $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002444 unshift(@a_path_components, pop(@b_path_components));
2445 }
Eric Wong74a81222007-02-10 13:28:50 -08002446 return undef unless defined $i && defined $i->{copyfrom_path};
2447 my $branch_from = $i->{copyfrom_path};
Eric Wong7f578c52007-01-24 02:16:25 -08002448 if (@a_path_components) {
2449 print STDERR "branch_from: $branch_from => ";
2450 $branch_from .= '/'.join('/', @a_path_components);
2451 print STDERR $branch_from, "\n";
2452 }
Eric Wong3ebe8df2007-01-25 17:35:40 -08002453 my $r = $i->{copyfrom_rev};
Eric Wong15710b62007-01-22 02:20:33 -08002454 my $repos_root = $self->ra->{repos_root};
2455 my $url = $self->ra->{url};
2456 my $new_url = $repos_root . $branch_from;
2457 print STDERR "Found possible branch point: ",
2458 "$new_url => ", $self->full_url, ", $r\n";
2459 $branch_from =~ s#^/##;
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002460 my $gs = $self->other_gs($new_url, $url, $repos_root,
2461 $branch_from, $r, $self->{ref_id});
Eric Wong15710b62007-01-22 02:20:33 -08002462 my ($r0, $parent) = $gs->find_rev_before($r, 1);
Deskin Miller553589f2008-12-08 08:31:31 -05002463 {
2464 my ($base, $head);
2465 if (!defined $r0 || !defined $parent) {
2466 ($base, $head) = parse_revision_argument(0, $r);
2467 } else {
2468 if ($r0 < $r) {
2469 $gs->ra->get_log([$gs->{path}], $r0 + 1, $r, 1,
2470 0, 1, sub { $base = $_[1] - 1 });
2471 }
2472 }
2473 if (defined $base && $base <= $r) {
Eric Wongd627de62007-04-15 03:01:29 -07002474 $gs->fetch($base, $r);
2475 }
Deskin Miller553589f2008-12-08 08:31:31 -05002476 ($r0, $parent) = $gs->find_rev_before($r, 1);
Eric Wong15710b62007-01-22 02:20:33 -08002477 }
Eric Wongef70de92007-02-01 04:12:41 -08002478 if (defined $r0 && defined $parent) {
Eric Wong15710b62007-01-22 02:20:33 -08002479 print STDERR "Found branch parent: ($self->{ref_id}) $parent\n";
Eric Wong15710b62007-01-22 02:20:33 -08002480 my $ed;
2481 if ($self->ra->can_do_switch) {
Eric Wong2e5e2482007-02-23 02:21:59 -08002482 $self->assert_index_clean($parent);
Eric Wong8b8fc062007-01-22 11:44:57 -08002483 print STDERR "Following parent with do_switch\n";
Eric Wong15710b62007-01-22 02:20:33 -08002484 # do_switch works with svn/trunk >= r22312, but that
Eric Wong2b27f6c2007-01-28 04:59:05 -08002485 # is not included with SVN 1.4.3 (the latest version
Eric Wong15710b62007-01-22 02:20:33 -08002486 # at the moment), so we can't rely on it
Eric Wong83c2fcf2009-02-22 20:25:00 -08002487 $self->{last_rev} = $r0;
Eric Wong15710b62007-01-22 02:20:33 -08002488 $self->{last_commit} = $parent;
Eric Wong8841b372009-02-11 01:56:58 -08002489 $ed = SVN::Git::Fetcher->new($self, $gs->{path});
Eric Wong8a603772007-01-31 02:45:50 -08002490 $gs->ra->gs_do_switch($r0, $rev, $gs,
Eric Wong15710b62007-01-22 02:20:33 -08002491 $self->full_url, $ed)
2492 or die "SVN connection failed somewhere...\n";
Steven Walter9ff74e92007-09-28 13:24:19 -04002493 } elsif ($self->ra->trees_match($new_url, $r0,
2494 $self->full_url, $rev)) {
2495 print STDERR "Trees match:\n",
2496 " $new_url\@$r0\n",
2497 " ${\$self->full_url}\@$rev\n",
2498 "Following parent with no changes\n";
2499 $self->tmp_index_do(sub {
2500 command_noisy('read-tree', $parent);
2501 });
2502 $self->{last_commit} = $parent;
Eric Wong15710b62007-01-22 02:20:33 -08002503 } else {
Eric Wong8b8fc062007-01-22 11:44:57 -08002504 print STDERR "Following parent with do_update\n";
Eric Wong15710b62007-01-22 02:20:33 -08002505 $ed = SVN::Git::Fetcher->new($self);
Eric Wong8a603772007-01-31 02:45:50 -08002506 $self->ra->gs_do_update($rev, $rev, $self, $ed)
Eric Wong15710b62007-01-22 02:20:33 -08002507 or die "SVN connection failed somewhere...\n";
2508 }
Eric Wongf7c3fc42007-01-29 18:34:55 -08002509 print STDERR "Successfully followed parent\n";
Eric Wong15710b62007-01-22 02:20:33 -08002510 return $self->make_log_entry($rev, [$parent], $ed);
2511 }
Eric Wong15710b62007-01-22 02:20:33 -08002512 return undef;
2513}
2514
Eric Wong9b981fc2007-01-11 12:14:21 -08002515sub do_fetch {
Eric Wong706587f2007-01-18 17:50:01 -08002516 my ($self, $paths, $rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08002517 my $ed;
Eric Wong9b981fc2007-01-11 12:14:21 -08002518 my ($last_rev, @parents);
Eric Wongb9dffd82007-02-09 01:28:30 -08002519 if (my $lc = $self->last_commit) {
2520 # we can have a branch that was deleted, then re-added
2521 # under the same name but copied from another path, in
2522 # which case we'll have multiple parents (we don't
2523 # want to break the original ref, nor lose copypath info):
2524 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2525 push @{$log_entry->{parents}}, $lc;
2526 return $log_entry;
2527 }
Eric Wong15710b62007-01-22 02:20:33 -08002528 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002529 $last_rev = $self->{last_rev};
Eric Wongb9dffd82007-02-09 01:28:30 -08002530 $ed->{c} = $lc;
2531 @parents = ($lc);
Eric Wong9b981fc2007-01-11 12:14:21 -08002532 } else {
2533 $last_rev = $rev;
Eric Wong15710b62007-01-22 02:20:33 -08002534 if (my $log_entry = $self->find_parent_branch($paths, $rev)) {
2535 return $log_entry;
2536 }
2537 $ed = SVN::Git::Fetcher->new($self);
Eric Wong9b981fc2007-01-11 12:14:21 -08002538 }
Eric Wong8a603772007-01-31 02:45:50 -08002539 unless ($self->ra->gs_do_update($last_rev, $rev, $self, $ed)) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002540 die "SVN connection failed somewhere...\n";
2541 }
2542 $self->make_log_entry($rev, \@parents, $ed);
2543}
2544
Eric Wong97f69872007-01-25 11:53:13 -08002545sub get_untracked {
2546 my ($self, $ed) = @_;
2547 my @out;
2548 my $h = $ed->{empty};
Eric Wong9b981fc2007-01-11 12:14:21 -08002549 foreach (sort keys %$h) {
2550 my $act = $h->{$_} ? '+empty_dir' : '-empty_dir';
Eric Wong97f69872007-01-25 11:53:13 -08002551 push @out, " $act: " . uri_encode($_);
Eric Wong9b981fc2007-01-11 12:14:21 -08002552 warn "W: $act: $_\n";
2553 }
2554 foreach my $t (qw/dir_prop file_prop/) {
Eric Wong97f69872007-01-25 11:53:13 -08002555 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002556 foreach my $path (sort keys %$h) {
2557 my $ppath = $path eq '' ? '.' : $path;
2558 foreach my $prop (sort keys %{$h->{$path}}) {
Eric Wong1ce255d2007-01-14 23:21:16 -08002559 next if $SKIP_PROP{$prop};
Eric Wong9b981fc2007-01-11 12:14:21 -08002560 my $v = $h->{$path}->{$prop};
Eric Wong97f69872007-01-25 11:53:13 -08002561 my $t_ppath_prop = "$t: " .
2562 uri_encode($ppath) . ' ' .
2563 uri_encode($prop);
Eric Wong9b981fc2007-01-11 12:14:21 -08002564 if (defined $v) {
Eric Wong97f69872007-01-25 11:53:13 -08002565 push @out, " +$t_ppath_prop " .
2566 uri_encode($v);
Eric Wong9b981fc2007-01-11 12:14:21 -08002567 } else {
Eric Wong97f69872007-01-25 11:53:13 -08002568 push @out, " -$t_ppath_prop";
Eric Wong9b981fc2007-01-11 12:14:21 -08002569 }
2570 }
2571 }
2572 }
2573 foreach my $t (qw/absent_file absent_directory/) {
Eric Wong97f69872007-01-25 11:53:13 -08002574 $h = $ed->{$t} or next;
Eric Wong9b981fc2007-01-11 12:14:21 -08002575 foreach my $parent (sort keys %$h) {
2576 foreach my $path (sort @{$h->{$parent}}) {
Eric Wong97f69872007-01-25 11:53:13 -08002577 push @out, " $t: " .
2578 uri_encode("$parent/$path");
Eric Wong9b981fc2007-01-11 12:14:21 -08002579 warn "W: $t: $parent/$path ",
2580 "Insufficient permissions?\n";
2581 }
2582 }
2583 }
Eric Wong97f69872007-01-25 11:53:13 -08002584 \@out;
Eric Wong9b981fc2007-01-11 12:14:21 -08002585}
2586
Pete Harlane82f0d72009-01-17 20:10:14 -08002587# parse_svn_date(DATE)
2588# --------------------
2589# Given a date (in UTC) from Subversion, return a string in the format
2590# "<TZ Offset> <local date/time>" that Git will use.
2591#
2592# By default the parsed date will be in UTC; if $Git::SVN::_localtime
2593# is true we'll convert it to the local timezone instead.
Eric Wong1c8443b2007-01-14 02:17:00 -08002594sub parse_svn_date {
2595 my $date = shift || return '+0000 1970-01-01 00:00:00';
2596 my ($Y,$m,$d,$H,$M,$S) = ($date =~ /^(\d{4})\-(\d\d)\-(\d\d)T
Junio C Hamanob94ead72009-02-18 10:48:01 -08002597 (\d\d)\:(\d\d)\:(\d\d)\.\d*Z$/x) or
Eric Wong1c8443b2007-01-14 02:17:00 -08002598 croak "Unable to parse date: $date\n";
Pete Harlane82f0d72009-01-17 20:10:14 -08002599 my $parsed_date; # Set next.
2600
2601 if ($Git::SVN::_localtime) {
2602 # Translate the Subversion datetime to an epoch time.
2603 # Begin by switching ourselves to $date's timezone, UTC.
2604 my $old_env_TZ = $ENV{TZ};
2605 $ENV{TZ} = 'UTC';
2606
2607 my $epoch_in_UTC =
2608 POSIX::strftime('%s', $S, $M, $H, $d, $m - 1, $Y - 1900);
2609
2610 # Determine our local timezone (including DST) at the
2611 # time of $epoch_in_UTC. $Git::SVN::Log::TZ stored the
2612 # value of TZ, if any, at the time we were run.
2613 if (defined $Git::SVN::Log::TZ) {
2614 $ENV{TZ} = $Git::SVN::Log::TZ;
2615 } else {
2616 delete $ENV{TZ};
2617 }
2618
2619 my $our_TZ =
2620 POSIX::strftime('%Z', $S, $M, $H, $d, $m - 1, $Y - 1900);
2621
2622 # This converts $epoch_in_UTC into our local timezone.
2623 my ($sec, $min, $hour, $mday, $mon, $year,
2624 $wday, $yday, $isdst) = localtime($epoch_in_UTC);
2625
2626 $parsed_date = sprintf('%s %04d-%02d-%02d %02d:%02d:%02d',
2627 $our_TZ, $year + 1900, $mon + 1,
2628 $mday, $hour, $min, $sec);
2629
2630 # Reset us to the timezone in effect when we entered
2631 # this routine.
2632 if (defined $old_env_TZ) {
2633 $ENV{TZ} = $old_env_TZ;
2634 } else {
2635 delete $ENV{TZ};
2636 }
2637 } else {
2638 $parsed_date = "+0000 $Y-$m-$d $H:$M:$S";
2639 }
2640
2641 return $parsed_date;
Eric Wong1c8443b2007-01-14 02:17:00 -08002642}
2643
Sam Vilain8e3f9b12007-06-26 19:23:59 +12002644sub other_gs {
2645 my ($self, $new_url, $url, $repos_root,
2646 $branch_from, $r, $old_ref_id) = @_;
2647 my $gs = Git::SVN->find_by_url($new_url, $repos_root, $branch_from);
2648 unless ($gs) {
2649 my $ref_id = $old_ref_id;
2650 $ref_id =~ s/\@\d+$//;
2651 $ref_id .= "\@$r";
2652 # just grow a tail if we're not unique enough :x
2653 $ref_id .= '-' while find_ref($ref_id);
2654 print STDERR "Initializing parent: $ref_id\n";
2655 my ($u, $p, $repo_id) = ($new_url, '', $ref_id);
2656 if ($u =~ s#^\Q$url\E(/|$)##) {
2657 $p = $u;
2658 $u = $url;
2659 $repo_id = $self->{repo_id};
2660 }
2661 $gs = Git::SVN->init($u, $p, $repo_id, $ref_id, 1);
2662 }
2663 $gs
2664}
2665
Eric Wong1c8443b2007-01-14 02:17:00 -08002666sub check_author {
2667 my ($author) = @_;
2668 if (!defined $author || length $author == 0) {
2669 $author = '(no author)';
Thomas Guyot-Sionnest9231f502008-04-22 06:07:47 -04002670 } elsif (defined $::_authors && ! defined $::users{$author}) {
Eric Wong1c8443b2007-01-14 02:17:00 -08002671 die "Author: $author not defined in $::_authors file\n";
2672 }
2673 $author;
2674}
2675
Eric Wong9b981fc2007-01-11 12:14:21 -08002676sub make_log_entry {
Eric Wong97f69872007-01-25 11:53:13 -08002677 my ($self, $rev, $parents, $ed) = @_;
2678 my $untracked = $self->get_untracked($ed);
2679
Eric Wong9b981fc2007-01-11 12:14:21 -08002680 open my $un, '>>', "$self->{dir}/unhandled.log" or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08002681 print $un "r$rev\n" or croak $!;
2682 print $un $_, "\n" foreach @$untracked;
2683 my %log_entry = ( parents => $parents || [], revision => $rev,
2684 log => '');
Eric Wongfbcc1732007-02-06 18:35:30 -08002685
Eric Wong8a49ee92007-02-10 20:46:50 -08002686 my $headrev;
Eric Wongfbcc1732007-02-06 18:35:30 -08002687 my $logged = delete $self->{logged_rev_props};
Eric Wong8a49ee92007-02-10 20:46:50 -08002688 if (!$logged || $self->{-want_revprops}) {
Eric Wongfbcc1732007-02-06 18:35:30 -08002689 my $rp = $self->ra->rev_proplist($rev);
2690 foreach (sort keys %$rp) {
2691 my $v = $rp->{$_};
2692 if (/^svn:(author|date|log)$/) {
2693 $log_entry{$1} = $v;
Eric Wong8a49ee92007-02-10 20:46:50 -08002694 } elsif ($_ eq 'svm:headrev') {
2695 $headrev = $v;
Eric Wongfbcc1732007-02-06 18:35:30 -08002696 } else {
2697 print $un " rev_prop: ", uri_encode($_), ' ',
2698 uri_encode($v), "\n";
2699 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002700 }
Eric Wongfbcc1732007-02-06 18:35:30 -08002701 } else {
2702 map { $log_entry{$_} = $logged->{$_} } keys %$logged;
Eric Wong9b981fc2007-01-11 12:14:21 -08002703 }
2704 close $un or croak $!;
Eric Wong97f69872007-01-25 11:53:13 -08002705
Eric Wong9b981fc2007-01-11 12:14:21 -08002706 $log_entry{date} = parse_svn_date($log_entry{date});
Eric Wong9b981fc2007-01-11 12:14:21 -08002707 $log_entry{log} .= "\n";
Eric Wongdb03cd22007-02-13 00:38:02 -08002708 my $author = $log_entry{author} = check_author($log_entry{author});
2709 my ($name, $email) = defined $::users{$author} ? @{$::users{$author}}
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002710 : ($author, undef);
2711
2712 my ($commit_name, $commit_email) = ($name, $email);
2713 if ($_use_log_author) {
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002714 my $name_field;
2715 if ($log_entry{log} =~ /From:\s+(.*\S)\s*\n/i) {
2716 $name_field = $1;
2717 } elsif ($log_entry{log} =~ /Signed-off-by:\s+(.*\S)\s*\n/i) {
2718 $name_field = $1;
2719 }
2720 if (!defined $name_field) {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02002721 if (!defined $email) {
2722 $email = $name;
2723 }
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002724 } elsif ($name_field =~ /(.*?)\s+<(.*)>/) {
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002725 ($name, $email) = ($1, $2);
Andy Whitcroft5ff6aae2007-12-13 06:58:15 +00002726 } elsif ($name_field =~ /(.*)@/) {
2727 ($name, $email) = ($1, $name_field);
2728 } else {
Stephen R. van den Bergabfa5332008-04-29 23:20:32 +02002729 ($name, $email) = ($name_field, $name_field);
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002730 }
2731 }
Eric Wong91b03282007-02-11 00:51:33 -08002732 if (defined $headrev && $self->use_svm_props) {
Eric Wongaea736c2007-02-16 19:15:21 -08002733 if ($self->rewrite_root) {
2734 die "Can't have both 'useSvmProps' and 'rewriteRoot' ",
2735 "options set!\n";
2736 }
Eric Wong8a49ee92007-02-10 20:46:50 -08002737 my ($uuid, $r) = $headrev =~ m{^([a-f\d\-]{30,}):(\d+)$};
Eric Wongbefc9ad2007-02-17 02:53:07 -08002738 # we don't want "SVM: initializing mirror for junk" ...
2739 return undef if $r == 0;
2740 my $svm = $self->svm;
2741 if ($uuid ne $svm->{uuid}) {
Eric Wong8a49ee92007-02-10 20:46:50 -08002742 die "UUID mismatch on SVM path:\n",
Eric Wongbefc9ad2007-02-17 02:53:07 -08002743 "expected: $svm->{uuid}\n",
Eric Wong8a49ee92007-02-10 20:46:50 -08002744 " got: $uuid\n";
2745 }
Eric Wongbefc9ad2007-02-17 02:53:07 -08002746 my $full_url = $self->full_url;
2747 $full_url =~ s#^\Q$svm->{replace}\E(/|$)#$svm->{source}$1# or
2748 die "Failed to replace '$svm->{replace}' with ",
2749 "'$svm->{source}' in $full_url\n";
Sam Vilain18ea92b2007-02-23 12:32:29 +13002750 # throw away username for storing in records
2751 remove_username($full_url);
Eric Wong8a49ee92007-02-10 20:46:50 -08002752 $log_entry{metadata} = "$full_url\@$r $uuid";
2753 $log_entry{svm_revision} = $r;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002754 $email ||= "$author\@$uuid";
2755 $commit_email ||= "$author\@$uuid";
Eric Wong62e349d2007-02-16 19:57:29 -08002756 } elsif ($self->use_svnsync_props) {
2757 my $full_url = $self->svnsync->{url};
2758 $full_url .= "/$self->{path}" if length $self->{path};
Adam Robence118732007-04-24 18:02:07 -07002759 remove_username($full_url);
Eric Wong62e349d2007-02-16 19:57:29 -08002760 my $uuid = $self->svnsync->{uuid};
2761 $log_entry{metadata} = "$full_url\@$rev $uuid";
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002762 $email ||= "$author\@$uuid";
2763 $commit_email ||= "$author\@$uuid";
Eric Wong8a49ee92007-02-10 20:46:50 -08002764 } else {
Adam Robence118732007-04-24 18:02:07 -07002765 my $url = $self->metadata_url;
2766 remove_username($url);
2767 $log_entry{metadata} = "$url\@$rev " .
Eric Wong26a62d52007-02-12 13:25:25 -08002768 $self->ra->get_uuid;
Eric Wongdb03cd22007-02-13 00:38:02 -08002769 $email ||= "$author\@" . $self->ra->get_uuid;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002770 $commit_email ||= "$author\@" . $self->ra->get_uuid;
Eric Wong8a49ee92007-02-10 20:46:50 -08002771 }
Eric Wongdb03cd22007-02-13 00:38:02 -08002772 $log_entry{name} = $name;
2773 $log_entry{email} = $email;
Andy Whitcroft70ae04e2007-11-22 13:44:42 +00002774 $log_entry{commit_name} = $commit_name;
2775 $log_entry{commit_email} = $commit_email;
Eric Wong9b981fc2007-01-11 12:14:21 -08002776 \%log_entry;
2777}
2778
2779sub fetch {
Eric Wong3ebe8df2007-01-25 17:35:40 -08002780 my ($self, $min_rev, $max_rev, @parents) = @_;
Eric Wong9b981fc2007-01-11 12:14:21 -08002781 my ($last_rev, $last_commit) = $self->last_rev_commit;
Eric Wong3ebe8df2007-01-25 17:35:40 -08002782 my ($base, $head) = $self->get_fetch_range($min_rev, $max_rev);
Eric Wonge5181922007-02-08 12:53:57 -08002783 $self->ra->gs_fetch_loop_common($base, $head, [$self]);
Eric Wong9b981fc2007-01-11 12:14:21 -08002784}
2785
2786sub set_tree_cb {
2787 my ($self, $log_entry, $tree, $rev, $date, $author) = @_;
Eric Wong490f49e2007-02-10 13:58:33 -08002788 $self->{inject_parents} = { $rev => $tree };
2789 $self->fetch(undef, undef);
Eric Wong9b981fc2007-01-11 12:14:21 -08002790}
2791
2792sub set_tree {
2793 my ($self, $tree) = (shift, shift);
Eric Wong1ce255d2007-01-14 23:21:16 -08002794 my $log_entry = ::get_commit_entry($tree);
Eric Wong9b981fc2007-01-11 12:14:21 -08002795 unless ($self->{last_rev}) {
Luc Heinrich0a1a1c82008-09-29 15:58:18 +02002796 ::fatal("Must have an existing revision to commit");
Eric Wong9b981fc2007-01-11 12:14:21 -08002797 }
Eric Wong61395352007-01-27 14:33:08 -08002798 my %ed_opts = ( r => $self->{last_rev},
2799 log => $log_entry->{log},
2800 ra => $self->ra,
2801 tree_a => $self->{last_commit},
2802 tree_b => $tree,
2803 editor_cb => sub {
2804 $self->set_tree_cb($log_entry, $tree, @_) },
2805 svn_path => $self->{path} );
2806 if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
Eric Wong9b981fc2007-01-11 12:14:21 -08002807 print "No changes\nr$self->{last_rev} = $tree\n";
2808 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002809}
2810
Eric Wong060610c2007-12-08 23:27:41 -08002811sub rebuild_from_rev_db {
2812 my ($self, $path) = @_;
2813 my $r = -1;
2814 open my $fh, '<', $path or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02002815 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002816 while (<$fh>) {
2817 length($_) == 41 or croak "inconsistent size in ($_) != 41";
2818 chomp($_);
2819 ++$r;
2820 next if $_ eq ('0' x 40);
2821 $self->rev_map_set($r, $_);
2822 print "r$r = $_\n";
2823 }
2824 close $fh or croak "close: $!";
2825 unlink $path or croak "unlink: $!";
2826}
2827
Eric Wongf0ecca12007-01-30 13:11:14 -08002828sub rebuild {
2829 my ($self) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08002830 my $map_path = $self->map_path;
Deskin Miller2beec892008-09-15 21:12:58 -04002831 my $partial = (-e $map_path && ! -z $map_path);
Eric Wongd6d33462007-02-16 04:05:33 -08002832 return unless ::verify_ref($self->refname.'^0');
Deskin Miller2beec892008-09-15 21:12:58 -04002833 if (!$partial && ($self->use_svm_props || $self->no_metadata)) {
Eric Wong060610c2007-12-08 23:27:41 -08002834 my $rev_db = $self->rev_db_path;
2835 $self->rebuild_from_rev_db($rev_db);
2836 if ($self->use_svm_props) {
2837 my $svm_rev_db = $self->rev_db_path($self->svm_uuid);
2838 $self->rebuild_from_rev_db($svm_rev_db);
2839 }
2840 $self->unlink_rev_db_symlink;
Eric Wong26a62d52007-02-12 13:25:25 -08002841 return;
2842 }
Deskin Miller2beec892008-09-15 21:12:58 -04002843 print "Rebuilding $map_path ...\n" if (!$partial);
2844 my ($base_rev, $head) = ($partial ? $self->rev_map_max_norebuild(1) :
2845 (undef, undef));
Eric Wong060610c2007-12-08 23:27:41 -08002846 my ($log, $ctx) =
2847 command_output_pipe(qw/rev-list --pretty=raw --no-color --reverse/,
Deskin Miller2beec892008-09-15 21:12:58 -04002848 ($head ? "$head.." : "") . $self->refname,
2849 '--');
Jan Krüger74b1e122008-06-24 02:17:36 +02002850 my $metadata_url = $self->metadata_url;
2851 remove_username($metadata_url);
Eric Wong060610c2007-12-08 23:27:41 -08002852 my $svn_uuid = $self->ra_uuid;
Sam Vilain3dfab992007-06-30 20:56:13 +12002853 my $c;
2854 while (<$log>) {
2855 if ( m{^commit ($::sha1)$} ) {
2856 $c = $1;
2857 next;
2858 }
2859 next unless s{^\s*(git-svn-id:)}{$1};
2860 my ($url, $rev, $uuid) = ::extract_metadata($_);
Sam Vilain18ea92b2007-02-23 12:32:29 +13002861 remove_username($url);
Eric Wongf0ecca12007-01-30 13:11:14 -08002862
2863 # ignore merges (from set-tree)
2864 next if (!defined $rev || !$uuid);
2865
2866 # if we merged or otherwise started elsewhere, this is
2867 # how we break out of it
Eric Wong060610c2007-12-08 23:27:41 -08002868 if (($uuid ne $svn_uuid) ||
Jan Krüger74b1e122008-06-24 02:17:36 +02002869 ($metadata_url && $url && ($url ne $metadata_url))) {
Eric Wongf0ecca12007-01-30 13:11:14 -08002870 next;
2871 }
Deskin Miller2beec892008-09-15 21:12:58 -04002872 if ($partial && $head) {
2873 print "Partial-rebuilding $map_path ...\n";
2874 print "Currently at $base_rev = $head\n";
2875 $head = undef;
2876 }
Eric Wongf0ecca12007-01-30 13:11:14 -08002877
Eric Wong060610c2007-12-08 23:27:41 -08002878 $self->rev_map_set($rev, $c);
Eric Wongf0ecca12007-01-30 13:11:14 -08002879 print "r$rev = $c\n";
2880 }
Sam Vilain3dfab992007-06-30 20:56:13 +12002881 command_close_pipe($log, $ctx);
Deskin Miller2beec892008-09-15 21:12:58 -04002882 print "Done rebuilding $map_path\n" if (!$partial || !$head);
Eric Wong060610c2007-12-08 23:27:41 -08002883 my $rev_db_path = $self->rev_db_path;
2884 if (-f $self->rev_db_path) {
2885 unlink $self->rev_db_path or croak "unlink: $!";
2886 }
2887 $self->unlink_rev_db_symlink;
Eric Wongf0ecca12007-01-30 13:11:14 -08002888}
2889
Eric Wong060610c2007-12-08 23:27:41 -08002890# rev_map:
Eric Wong9b981fc2007-01-11 12:14:21 -08002891# Tie::File seems to be prone to offset errors if revisions get sparse,
2892# it's not that fast, either. Tie::File is also not in Perl 5.6. So
2893# one of my favorite modules is out :< Next up would be one of the DBM
Eric Wong060610c2007-12-08 23:27:41 -08002894# modules, but I'm not sure which is most portable...
2895#
2896# This is the replacement for the rev_db format, which was too big
2897# and inefficient for large repositories with a lot of sparse history
2898# (mainly tags)
2899#
2900# The format is this:
2901# - 24 bytes for every record,
2902# * 4 bytes for the integer representing an SVN revision number
2903# * 20 bytes representing the sha1 of a git commit
2904# - No empty padding records like the old format
Eric Wong66ab84b2007-12-08 23:27:42 -08002905# (except the last record, which can be overwritten)
Eric Wong060610c2007-12-08 23:27:41 -08002906# - new records are written append-only since SVN revision numbers
2907# increase monotonically
2908# - lookups on SVN revision number are done via a binary search
Eric Wong66ab84b2007-12-08 23:27:42 -08002909# - Piping the file to xxd -c24 is a good way of dumping it for
2910# viewing or editing (piped back through xxd -r), should the need
2911# ever arise.
2912# - The last record can be padding revision with an all-zero sha1
2913# This is used to optimize fetch performance when using multiple
2914# "fetch" directives in .git/config
Eric Wong060610c2007-12-08 23:27:41 -08002915#
Eric Wong97ae0912007-02-11 15:21:24 -08002916# These files are disposable unless noMetadata or useSvmProps is set
Eric Wong9b981fc2007-01-11 12:14:21 -08002917
Eric Wong060610c2007-12-08 23:27:41 -08002918sub _rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08002919 my ($fh, $rev, $commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08002920
Michael Weber4f7ec792008-04-18 15:12:04 +02002921 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002922 my $size = (stat($fh))[7];
2923 ($size % 24) == 0 or croak "inconsistent size: $size";
2924
Eric Wong66ab84b2007-12-08 23:27:42 -08002925 my $wr_offset = 0;
Eric Wong060610c2007-12-08 23:27:41 -08002926 if ($size > 0) {
2927 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
2928 my $read = sysread($fh, my $buf, 24) or croak "read: $!";
2929 $read == 24 or croak "read only $read bytes (!= 24)";
2930 my ($last_rev, $last_commit) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08002931 if ($last_commit eq ('0' x40)) {
2932 if ($size >= 48) {
2933 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
2934 $read = sysread($fh, $buf, 24) or
2935 croak "read: $!";
2936 $read == 24 or
2937 croak "read only $read bytes (!= 24)";
2938 ($last_rev, $last_commit) =
2939 unpack(rev_map_fmt, $buf);
2940 if ($last_commit eq ('0' x40)) {
2941 croak "inconsistent .rev_map\n";
2942 }
2943 }
2944 if ($last_rev >= $rev) {
2945 croak "last_rev is higher!: $last_rev >= $rev";
2946 }
2947 $wr_offset = -24;
Eric Wong47a0b752007-01-31 05:13:30 -08002948 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002949 }
Eric Wong66ab84b2007-12-08 23:27:42 -08002950 sysseek($fh, $wr_offset, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08002951 syswrite($fh, pack(rev_map_fmt, $rev, $commit), 24) == 24 or
2952 croak "write: $!";
Eric Wong26a62d52007-02-12 13:25:25 -08002953}
2954
2955sub mkfile {
2956 my ($path) = @_;
2957 unless (-e $path) {
2958 my ($dir, $base) = ($path =~ m#^(.*?)/?([^/]+)$#);
2959 mkpath([$dir]) unless -d $dir;
2960 open my $fh, '>>', $path or die "Couldn't create $path: $!\n";
2961 close $fh or die "Couldn't close (create) $path: $!\n";
2962 }
2963}
2964
Eric Wong060610c2007-12-08 23:27:41 -08002965sub rev_map_set {
Eric Wong26a62d52007-02-12 13:25:25 -08002966 my ($self, $rev, $commit, $update_ref, $uuid) = @_;
2967 length $commit == 40 or die "arg3 must be a full SHA1 hexsum\n";
Eric Wong060610c2007-12-08 23:27:41 -08002968 my $db = $self->map_path($uuid);
Eric Wong26a62d52007-02-12 13:25:25 -08002969 my $db_lock = "$db.lock";
2970 my $sig;
2971 if ($update_ref) {
2972 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
2973 $SIG{USR1} = $SIG{USR2} = sub { $sig = $_[0] };
2974 }
2975 mkfile($db);
2976
2977 $LOCKFILES{$db_lock} = 1;
2978 my $sync;
2979 # both of these options make our .rev_db file very, very important
2980 # and we can't afford to lose it because rebuild() won't work
2981 if ($self->use_svm_props || $self->no_metadata) {
2982 $sync = 1;
Eric Wong060610c2007-12-08 23:27:41 -08002983 copy($db, $db_lock) or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08002984 "Failed to copy: ",
2985 "$db => $db_lock ($!)\n";
2986 } else {
Eric Wong060610c2007-12-08 23:27:41 -08002987 rename $db, $db_lock or die "rev_map_set(@_): ",
Eric Wong26a62d52007-02-12 13:25:25 -08002988 "Failed to rename: ",
2989 "$db => $db_lock ($!)\n";
2990 }
Eric Wong060610c2007-12-08 23:27:41 -08002991
Eric Wong66ab84b2007-12-08 23:27:42 -08002992 sysopen(my $fh, $db_lock, O_RDWR | O_CREAT)
Eric Wong060610c2007-12-08 23:27:41 -08002993 or croak "Couldn't open $db_lock: $!\n";
2994 _rev_map_set($fh, $rev, $commit);
Eric Wong97ae0912007-02-11 15:21:24 -08002995 if ($sync) {
2996 $fh->flush or die "Couldn't flush $db_lock: $!\n";
2997 $fh->sync or die "Couldn't sync $db_lock: $!\n";
2998 }
Eric Wong9b981fc2007-01-11 12:14:21 -08002999 close $fh or croak $!;
Eric Wong373274f2007-01-31 13:54:23 -08003000 if ($update_ref) {
Eric Wong1e889ef2007-02-16 01:45:13 -08003001 $_head = $self;
Eric Wong373274f2007-01-31 13:54:23 -08003002 command_noisy('update-ref', '-m', "r$rev",
3003 $self->refname, $commit);
3004 }
Eric Wong060610c2007-12-08 23:27:41 -08003005 rename $db_lock, $db or die "rev_map_set(@_): ", "Failed to rename: ",
Eric Wong373274f2007-01-31 13:54:23 -08003006 "$db_lock => $db ($!)\n";
3007 delete $LOCKFILES{$db_lock};
3008 if ($update_ref) {
3009 $SIG{INT} = $SIG{HUP} = $SIG{TERM} = $SIG{ALRM} = $SIG{PIPE} =
3010 $SIG{USR1} = $SIG{USR2} = 'DEFAULT';
3011 kill $sig, $$ if defined $sig;
3012 }
Eric Wong9b981fc2007-01-11 12:14:21 -08003013}
3014
Eric Wong66ab84b2007-12-08 23:27:42 -08003015# If want_commit, this will return an array of (rev, commit) where
3016# commit _must_ be a valid commit in the archive.
3017# Otherwise, it'll return the max revision (whether or not the
3018# commit is valid or just a 0x40 placeholder).
Eric Wong060610c2007-12-08 23:27:41 -08003019sub rev_map_max {
Eric Wong66ab84b2007-12-08 23:27:42 -08003020 my ($self, $want_commit) = @_;
Eric Wongd6d33462007-02-16 04:05:33 -08003021 $self->rebuild;
Deskin Miller2beec892008-09-15 21:12:58 -04003022 my ($r, $c) = $self->rev_map_max_norebuild($want_commit);
3023 $want_commit ? ($r, $c) : $r;
3024}
3025
3026sub rev_map_max_norebuild {
3027 my ($self, $want_commit) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003028 my $map_path = $self->map_path;
Eric Wong66ab84b2007-12-08 23:27:42 -08003029 stat $map_path or return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08003030 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02003031 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003032 my $size = (stat($fh))[7];
3033 ($size % 24) == 0 or croak "inconsistent size: $size";
3034
3035 if ($size == 0) {
3036 close $fh or croak "close: $!";
Eric Wong66ab84b2007-12-08 23:27:42 -08003037 return $want_commit ? (0, undef) : 0;
Eric Wong060610c2007-12-08 23:27:41 -08003038 }
3039
Eric Wong66ab84b2007-12-08 23:27:42 -08003040 sysseek($fh, -24, SEEK_END) or croak "seek: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003041 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003042 my ($r, $c) = unpack(rev_map_fmt, $buf);
Eric Wong66ab84b2007-12-08 23:27:42 -08003043 if ($want_commit && $c eq ('0' x40)) {
3044 if ($size < 48) {
3045 return $want_commit ? (0, undef) : 0;
3046 }
3047 sysseek($fh, -48, SEEK_END) or croak "seek: $!";
3048 sysread($fh, $buf, 24) == 24 or croak "read: $!";
3049 ($r, $c) = unpack(rev_map_fmt, $buf);
3050 if ($c eq ('0'x40)) {
3051 croak "Penultimate record is all-zeroes in $map_path";
3052 }
3053 }
3054 close $fh or croak "close: $!";
3055 $want_commit ? ($r, $c) : $r;
Eric Wong9c93fee2007-01-31 17:22:31 -08003056}
3057
Eric Wong060610c2007-12-08 23:27:41 -08003058sub rev_map_get {
Eric Wong26a62d52007-02-12 13:25:25 -08003059 my ($self, $rev, $uuid) = @_;
Eric Wong060610c2007-12-08 23:27:41 -08003060 my $map_path = $self->map_path($uuid);
3061 return undef unless -e $map_path;
3062
3063 sysopen(my $fh, $map_path, O_RDONLY) or croak "open: $!";
Michael Weber4f7ec792008-04-18 15:12:04 +02003064 binmode $fh or croak "binmode: $!";
Eric Wong060610c2007-12-08 23:27:41 -08003065 my $size = (stat($fh))[7];
3066 ($size % 24) == 0 or croak "inconsistent size: $size";
3067
3068 if ($size == 0) {
3069 close $fh or croak "close: $fh";
3070 return undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003071 }
Eric Wong060610c2007-12-08 23:27:41 -08003072
3073 my ($l, $u) = (0, $size - 24);
3074 my ($r, $c, $buf);
3075
3076 while ($l <= $u) {
3077 my $i = int(($l/24 + $u/24) / 2) * 24;
3078 sysseek($fh, $i, SEEK_SET) or croak "seek: $!";
3079 sysread($fh, my $buf, 24) == 24 or croak "read: $!";
3080 my ($r, $c) = unpack('NH40', $buf);
3081
3082 if ($r < $rev) {
3083 $l = $i + 24;
3084 } elsif ($r > $rev) {
3085 $u = $i - 24;
3086 } else { # $r == $rev
3087 close($fh) or croak "close: $!";
Eric Wong66ab84b2007-12-08 23:27:42 -08003088 return $c eq ('0' x 40) ? undef : $c;
Eric Wong060610c2007-12-08 23:27:41 -08003089 }
3090 }
3091 close($fh) or croak "close: $!";
3092 undef;
Eric Wong9b981fc2007-01-11 12:14:21 -08003093}
3094
David D Kilzer111947e2007-11-11 22:56:52 -08003095# Finds the first svn revision that exists on (if $eq_ok is true) or
3096# before $rev for the current branch. It will not search any lower
3097# than $min_rev. Returns the git commit hash and svn revision number
3098# if found, else (undef, undef).
Eric Wong15710b62007-01-22 02:20:33 -08003099sub find_rev_before {
David D Kilzer111947e2007-11-11 22:56:52 -08003100 my ($self, $rev, $eq_ok, $min_rev) = @_;
Eric Wong15710b62007-01-22 02:20:33 -08003101 --$rev unless $eq_ok;
David D Kilzer111947e2007-11-11 22:56:52 -08003102 $min_rev ||= 1;
3103 while ($rev >= $min_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003104 if (my $c = $self->rev_map_get($rev)) {
Eric Wong15710b62007-01-22 02:20:33 -08003105 return ($rev, $c);
3106 }
3107 --$rev;
3108 }
3109 return (undef, undef);
3110}
3111
David D Kilzer111947e2007-11-11 22:56:52 -08003112# Finds the first svn revision that exists on (if $eq_ok is true) or
3113# after $rev for the current branch. It will not search any higher
3114# than $max_rev. Returns the git commit hash and svn revision number
3115# if found, else (undef, undef).
3116sub find_rev_after {
3117 my ($self, $rev, $eq_ok, $max_rev) = @_;
3118 ++$rev unless $eq_ok;
Eric Wong060610c2007-12-08 23:27:41 -08003119 $max_rev ||= $self->rev_map_max;
David D Kilzer111947e2007-11-11 22:56:52 -08003120 while ($rev <= $max_rev) {
Eric Wong060610c2007-12-08 23:27:41 -08003121 if (my $c = $self->rev_map_get($rev)) {
David D Kilzer111947e2007-11-11 22:56:52 -08003122 return ($rev, $c);
3123 }
3124 ++$rev;
3125 }
3126 return (undef, undef);
3127}
3128
Eric Wong9b981fc2007-01-11 12:14:21 -08003129sub _new {
Eric Wong706587f2007-01-18 17:50:01 -08003130 my ($class, $repo_id, $ref_id, $path) = @_;
3131 unless (defined $repo_id && length $repo_id) {
3132 $repo_id = $Git::SVN::default_repo_id;
3133 }
3134 unless (defined $ref_id && length $ref_id) {
Eric Wong8b8fc062007-01-22 11:44:57 -08003135 $_[2] = $ref_id = $Git::SVN::default_ref_id;
Eric Wong706587f2007-01-18 17:50:01 -08003136 }
Eric Wong7829f202008-06-28 20:40:32 -07003137 $_[1] = $repo_id;
Eric Wong706587f2007-01-18 17:50:01 -08003138 my $dir = "$ENV{GIT_DIR}/svn/$ref_id";
3139 $_[3] = $path = '' unless (defined $path);
Eric Wongb4d57e52007-02-14 15:10:44 -08003140 mkpath(["$ENV{GIT_DIR}/svn"]);
Eric Wong26a62d52007-02-12 13:25:25 -08003141 bless {
3142 ref_id => $ref_id, dir => $dir, index => "$dir/index",
Eric Wong8a49ee92007-02-10 20:46:50 -08003143 path => $path, config => "$ENV{GIT_DIR}/svn/config",
Eric Wong060610c2007-12-08 23:27:41 -08003144 map_root => "$dir/.rev_map", repo_id => $repo_id }, $class;
Eric Wong26a62d52007-02-12 13:25:25 -08003145}
3146
Eric Wong060610c2007-12-08 23:27:41 -08003147# for read-only access of old .rev_db formats
3148sub unlink_rev_db_symlink {
3149 my ($self) = @_;
3150 my $link = $self->rev_db_path;
3151 $link =~ s/\.[\w-]+$// or croak "missing UUID at the end of $link";
3152 if (-l $link) {
3153 unlink $link or croak "unlink: $link failed!";
3154 }
3155}
3156
3157sub rev_db_path {
3158 my ($self, $uuid) = @_;
3159 my $db_path = $self->map_path($uuid);
3160 $db_path =~ s{/\.rev_map\.}{/\.rev_db\.}
3161 or croak "map_path: $db_path does not contain '/.rev_map.' !";
3162 $db_path;
3163}
3164
3165# the new replacement for .rev_db
3166sub map_path {
Eric Wong26a62d52007-02-12 13:25:25 -08003167 my ($self, $uuid) = @_;
3168 $uuid ||= $self->ra_uuid;
Eric Wong060610c2007-12-08 23:27:41 -08003169 "$self->{map_root}.$uuid";
Eric Wong9b981fc2007-01-11 12:14:21 -08003170}
3171
Eric Wong1c8443b2007-01-14 02:17:00 -08003172sub uri_encode {
3173 my ($f) = @_;
3174 $f =~ s#([^a-zA-Z0-9\*!\:_\./\-])#uc sprintf("%%%02x",ord($1))#eg;
3175 $f
3176}
Eric Wong9b981fc2007-01-11 12:14:21 -08003177
Sam Vilain18ea92b2007-02-23 12:32:29 +13003178sub remove_username {
3179 $_[0] =~ s{^([^:]*://)[^@]+@}{$1};
3180}
3181
Eric Wongd976acf2007-01-04 00:45:03 -08003182package Git::SVN::Prompt;
3183use strict;
3184use warnings;
3185require SVN::Core;
3186use vars qw/$_no_auth_cache $_username/;
3187
3188sub simple {
Eric Wong30d055a2006-11-24 01:38:04 -08003189 my ($cred, $realm, $default_username, $may_save, $pool) = @_;
3190 $may_save = undef if $_no_auth_cache;
3191 $default_username = $_username if defined $_username;
3192 if (defined $default_username && length $default_username) {
3193 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003194 print STDERR "Authentication realm: $realm\n";
3195 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003196 }
3197 $cred->username($default_username);
3198 } else {
Eric Wongd976acf2007-01-04 00:45:03 -08003199 username($cred, $realm, $may_save, $pool);
Eric Wong30d055a2006-11-24 01:38:04 -08003200 }
3201 $cred->password(_read_password("Password for '" .
3202 $cred->username . "': ", $realm));
3203 $cred->may_save($may_save);
3204 $SVN::_Core::SVN_NO_ERROR;
3205}
3206
Eric Wongd976acf2007-01-04 00:45:03 -08003207sub ssl_server_trust {
Eric Wong30d055a2006-11-24 01:38:04 -08003208 my ($cred, $realm, $failures, $cert_info, $may_save, $pool) = @_;
3209 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003210 print STDERR "Error validating server certificate for '$realm':\n";
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003211 {
3212 no warnings 'once';
3213 # All variables SVN::Auth::SSL::* are used only once,
3214 # so we're shutting up Perl warnings about this.
3215 if ($failures & $SVN::Auth::SSL::UNKNOWNCA) {
3216 print STDERR " - The certificate is not issued ",
3217 "by a trusted authority. Use the\n",
3218 " fingerprint to validate ",
3219 "the certificate manually!\n";
3220 }
3221 if ($failures & $SVN::Auth::SSL::CNMISMATCH) {
3222 print STDERR " - The certificate hostname ",
3223 "does not match.\n";
3224 }
3225 if ($failures & $SVN::Auth::SSL::NOTYETVALID) {
3226 print STDERR " - The certificate is not yet valid.\n";
3227 }
3228 if ($failures & $SVN::Auth::SSL::EXPIRED) {
3229 print STDERR " - The certificate has expired.\n";
3230 }
3231 if ($failures & $SVN::Auth::SSL::OTHER) {
3232 print STDERR " - The certificate has ",
3233 "an unknown error.\n";
3234 }
3235 } # no warnings 'once'
Eric Wong6f729592007-01-15 20:15:55 -08003236 printf STDERR
3237 "Certificate information:\n".
Eric Wong30d055a2006-11-24 01:38:04 -08003238 " - Hostname: %s\n".
3239 " - Valid: from %s until %s\n".
3240 " - Issuer: %s\n".
3241 " - Fingerprint: %s\n",
3242 map $cert_info->$_, qw(hostname valid_from valid_until
Eric Wong6f729592007-01-15 20:15:55 -08003243 issuer_dname fingerprint);
Eric Wong30d055a2006-11-24 01:38:04 -08003244 my $choice;
3245prompt:
Eric Wong6f729592007-01-15 20:15:55 -08003246 print STDERR $may_save ?
Eric Wong30d055a2006-11-24 01:38:04 -08003247 "(R)eject, accept (t)emporarily or accept (p)ermanently? " :
3248 "(R)eject or accept (t)emporarily? ";
Eric Wong6f729592007-01-15 20:15:55 -08003249 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003250 $choice = lc(substr(<STDIN> || 'R', 0, 1));
3251 if ($choice =~ /^t$/i) {
3252 $cred->may_save(undef);
3253 } elsif ($choice =~ /^r$/i) {
3254 return -1;
3255 } elsif ($may_save && $choice =~ /^p$/i) {
3256 $cred->may_save($may_save);
3257 } else {
3258 goto prompt;
3259 }
3260 $cred->accepted_failures($failures);
3261 $SVN::_Core::SVN_NO_ERROR;
3262}
3263
Eric Wongd976acf2007-01-04 00:45:03 -08003264sub ssl_client_cert {
Eric Wong30d055a2006-11-24 01:38:04 -08003265 my ($cred, $realm, $may_save, $pool) = @_;
3266 $may_save = undef if $_no_auth_cache;
Eric Wong6f729592007-01-15 20:15:55 -08003267 print STDERR "Client certificate filename: ";
3268 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003269 chomp(my $filename = <STDIN>);
3270 $cred->cert_file($filename);
3271 $cred->may_save($may_save);
3272 $SVN::_Core::SVN_NO_ERROR;
3273}
3274
Eric Wongd976acf2007-01-04 00:45:03 -08003275sub ssl_client_cert_pw {
Eric Wong30d055a2006-11-24 01:38:04 -08003276 my ($cred, $realm, $may_save, $pool) = @_;
3277 $may_save = undef if $_no_auth_cache;
3278 $cred->password(_read_password("Password: ", $realm));
3279 $cred->may_save($may_save);
3280 $SVN::_Core::SVN_NO_ERROR;
3281}
3282
Eric Wongd976acf2007-01-04 00:45:03 -08003283sub username {
Eric Wong30d055a2006-11-24 01:38:04 -08003284 my ($cred, $realm, $may_save, $pool) = @_;
3285 $may_save = undef if $_no_auth_cache;
3286 if (defined $realm && length $realm) {
Eric Wong6f729592007-01-15 20:15:55 -08003287 print STDERR "Authentication realm: $realm\n";
Eric Wong30d055a2006-11-24 01:38:04 -08003288 }
3289 my $username;
3290 if (defined $_username) {
3291 $username = $_username;
3292 } else {
Eric Wong6f729592007-01-15 20:15:55 -08003293 print STDERR "Username: ";
3294 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003295 chomp($username = <STDIN>);
3296 }
3297 $cred->username($username);
3298 $cred->may_save($may_save);
3299 $SVN::_Core::SVN_NO_ERROR;
3300}
3301
3302sub _read_password {
3303 my ($prompt, $realm) = @_;
Eric Wong6f729592007-01-15 20:15:55 -08003304 print STDERR $prompt;
3305 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003306 require Term::ReadKey;
3307 Term::ReadKey::ReadMode('noecho');
3308 my $password = '';
3309 while (defined(my $key = Term::ReadKey::ReadKey(0))) {
3310 last if $key =~ /[\012\015]/; # \n\r
3311 $password .= $key;
3312 }
3313 Term::ReadKey::ReadMode('restore');
Eric Wong6f729592007-01-15 20:15:55 -08003314 print STDERR "\n";
3315 STDERR->flush;
Eric Wong30d055a2006-11-24 01:38:04 -08003316 $password;
3317}
3318
Eric Wong27a1a802006-11-27 21:44:48 -08003319package SVN::Git::Fetcher;
3320use vars qw/@ISA/;
3321use strict;
3322use warnings;
3323use Carp qw/croak/;
Adam Robenffe256f2008-05-23 16:19:41 +02003324use File::Temp qw/tempfile/;
Eric Wong27a1a802006-11-27 21:44:48 -08003325use IO::File qw//;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003326use vars qw/$_ignore_regex/;
Eric Wong27a1a802006-11-27 21:44:48 -08003327
3328# file baton members: path, mode_a, mode_b, pool, fh, blob, base
3329sub new {
Eric Wong8841b372009-02-11 01:56:58 -08003330 my ($class, $git_svn, $switch_path) = @_;
Eric Wong27a1a802006-11-27 21:44:48 -08003331 my $self = SVN::Delta::Editor->new;
3332 bless $self, $class;
Eric Wongdbc6c742009-01-11 16:51:10 -08003333 if (exists $git_svn->{last_commit}) {
3334 $self->{c} = $git_svn->{last_commit};
Eric Wong8841b372009-02-11 01:56:58 -08003335 $self->{empty_symlinks} =
3336 _mark_empty_symlinks($git_svn, $switch_path);
Eric Wongdbc6c742009-01-11 16:51:10 -08003337 }
Ben Jackson0d8bee72009-04-11 10:46:17 -07003338 $self->{ignore_regex} = eval { command_oneline('config', '--get',
3339 "svn-remote.$git_svn->{repo_id}.ignore-paths") };
Eric Wongd2a9a872006-12-12 14:47:00 -08003340 $self->{empty} = {};
3341 $self->{dir_prop} = {};
3342 $self->{file_prop} = {};
3343 $self->{absent_dir} = {};
3344 $self->{absent_file} = {};
Eric Wongef3cfaa2007-01-24 03:30:57 -08003345 $self->{gii} = $git_svn->tmp_index_do(sub { Git::IndexInfo->new });
Eric Wong27a1a802006-11-27 21:44:48 -08003346 $self;
3347}
3348
Eric Wongdbc6c742009-01-11 16:51:10 -08003349# this uses the Ra object, so it must be called before do_{switch,update},
3350# not inside them (when the Git::SVN::Fetcher object is passed) to
3351# do_{switch,update}
3352sub _mark_empty_symlinks {
Eric Wong8841b372009-02-11 01:56:58 -08003353 my ($git_svn, $switch_path) = @_;
Eric Wong4c58a712009-01-31 17:31:12 -08003354 my $bool = Git::config_bool('svn.brokenSymlinkWorkaround');
Eric Wong48679e52009-02-27 19:40:16 -08003355 return {} if (!defined($bool)) || (defined($bool) && ! $bool);
Eric Wong4c58a712009-01-31 17:31:12 -08003356
Eric Wongdbc6c742009-01-11 16:51:10 -08003357 my %ret;
3358 my ($rev, $cmt) = $git_svn->last_rev_commit;
3359 return {} unless ($rev && $cmt);
3360
Eric Wong4c58a712009-01-31 17:31:12 -08003361 # allow the warning to be printed for each revision we fetch to
3362 # ensure the user sees it. The user can also disable the workaround
3363 # on the repository even while git svn is running and the next
3364 # revision fetched will skip this expensive function.
3365 my $printed_warning;
Eric Wongdbc6c742009-01-11 16:51:10 -08003366 chomp(my $empty_blob = `git hash-object -t blob --stdin < /dev/null`);
3367 my ($ls, $ctx) = command_output_pipe(qw/ls-tree -r -z/, $cmt);
3368 local $/ = "\0";
Eric Wong8841b372009-02-11 01:56:58 -08003369 my $pfx = defined($switch_path) ? $switch_path : $git_svn->{path};
Eric Wongdbc6c742009-01-11 16:51:10 -08003370 $pfx .= '/' if length($pfx);
3371 while (<$ls>) {
3372 chomp;
3373 s/\A100644 blob $empty_blob\t//o or next;
Eric Wong4c58a712009-01-31 17:31:12 -08003374 unless ($printed_warning) {
3375 print STDERR "Scanning for empty symlinks, ",
3376 "this may take a while if you have ",
3377 "many empty files\n",
3378 "You may disable this with `",
3379 "git config svn.brokenSymlinkWorkaround ",
3380 "false'.\n",
3381 "This may be done in a different ",
3382 "terminal without restarting ",
3383 "git svn\n";
3384 $printed_warning = 1;
3385 }
Eric Wongdbc6c742009-01-11 16:51:10 -08003386 my $path = $_;
3387 my (undef, $props) =
3388 $git_svn->ra->get_file($pfx.$path, $rev, undef);
3389 if ($props->{'svn:special'}) {
3390 $ret{$path} = 1;
3391 }
3392 }
3393 command_close_pipe($ls, $ctx);
3394 \%ret;
3395}
3396
Eric Wongb03a71a2009-01-11 18:23:38 -08003397# returns true if a given path is inside a ".git" directory
3398sub in_dot_git {
3399 $_[0] =~ m{(?:^|/)\.git(?:/|$)};
3400}
3401
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003402# return value: 0 -- don't ignore, 1 -- ignore
3403sub is_path_ignored {
Ben Jackson0d8bee72009-04-11 10:46:17 -07003404 my ($self, $path) = @_;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003405 return 1 if in_dot_git($path);
Ben Jackson0d8bee72009-04-11 10:46:17 -07003406 return 1 if defined($self->{ignore_regex}) &&
3407 $path =~ m!$self->{ignore_regex}!;
Vitaly \"_Vi\" Shukelaedc662f2009-01-26 00:21:40 +02003408 return 0 unless defined($_ignore_regex);
3409 return 1 if $path =~ m!$_ignore_regex!o;
3410 return 0;
3411}
3412
Eric Wong8b8fc062007-01-22 11:44:57 -08003413sub set_path_strip {
3414 my ($self, $path) = @_;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08003415 $self->{path_strip} = qr/^\Q$path\E(\/|$)/ if length $path;
Eric Wong8b8fc062007-01-22 11:44:57 -08003416}
3417
Eric Wongd2a9a872006-12-12 14:47:00 -08003418sub open_root {
3419 { path => '' };
3420}
3421
3422sub open_directory {
3423 my ($self, $path, $pb, $rev) = @_;
3424 { path => $path };
3425}
3426
Eric Wong706587f2007-01-18 17:50:01 -08003427sub git_path {
3428 my ($self, $path) = @_;
Eric Wong2b27f6c2007-01-28 04:59:05 -08003429 if ($self->{path_strip}) {
3430 $path =~ s!$self->{path_strip}!! or
3431 die "Failed to strip path '$path' ($self->{path_strip})\n";
3432 }
Eric Wong706587f2007-01-18 17:50:01 -08003433 $path;
3434}
3435
Eric Wong27a1a802006-11-27 21:44:48 -08003436sub delete_entry {
3437 my ($self, $path, $rev, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003438 return undef if $self->is_path_ignored($path);
Eric Wong4a87db02007-01-04 01:38:18 -08003439
Eric Wong706587f2007-01-18 17:50:01 -08003440 my $gpath = $self->git_path($path);
Eric Wong8a603772007-01-31 02:45:50 -08003441 return undef if ($gpath eq '');
3442
Eric Wong4a87db02007-01-04 01:38:18 -08003443 # remove entire directories.
Eric Wong4f821012009-03-28 22:10:08 -07003444 my ($tree) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
3445 =~ /\A040000 tree ([a-f\d]{40})\t\Q$gpath\E\0/);
3446 if ($tree) {
Eric Wong4a87db02007-01-04 01:38:18 -08003447 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3448 -r --name-only -z/,
Eric Wong4f821012009-03-28 22:10:08 -07003449 $tree);
Eric Wong4a87db02007-01-04 01:38:18 -08003450 local $/ = "\0";
3451 while (<$ls>) {
Eric Wongef3cfaa2007-01-24 03:30:57 -08003452 chomp;
Eric Wong4f821012009-03-28 22:10:08 -07003453 my $rmpath = "$gpath/$_";
3454 $self->{gii}->remove($rmpath);
3455 print "\tD\t$rmpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003456 }
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003457 print "\tD\t$gpath/\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003458 command_close_pipe($ls, $ctx);
3459 $self->{empty}->{$path} = 0
3460 } else {
Eric Wongef3cfaa2007-01-24 03:30:57 -08003461 $self->{gii}->remove($gpath);
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003462 print "\tD\t$gpath\n" unless $::_q;
Eric Wong4a87db02007-01-04 01:38:18 -08003463 }
Eric Wong27a1a802006-11-27 21:44:48 -08003464 undef;
3465}
3466
3467sub open_file {
3468 my ($self, $path, $pb, $rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08003469 my ($mode, $blob);
3470
Ben Jackson0d8bee72009-04-11 10:46:17 -07003471 goto out if $self->is_path_ignored($path);
Eric Wongb03a71a2009-01-11 18:23:38 -08003472
Eric Wong706587f2007-01-18 17:50:01 -08003473 my $gpath = $self->git_path($path);
Eric Wong4f821012009-03-28 22:10:08 -07003474 ($mode, $blob) = (command('ls-tree', '-z', $self->{c}, "./$gpath")
3475 =~ /\A(\d{6}) blob ([a-f\d]{40})\t\Q$gpath\E\0/);
Eric Wong006ede52006-12-08 01:55:19 -08003476 unless (defined $mode && defined $blob) {
3477 die "$path was not found in commit $self->{c} (r$rev)\n";
3478 }
Eric Wongdbc6c742009-01-11 16:51:10 -08003479 if ($mode eq '100644' && $self->{empty_symlinks}->{$path}) {
3480 $mode = '120000';
3481 }
Eric Wongb03a71a2009-01-11 18:23:38 -08003482out:
Eric Wong27a1a802006-11-27 21:44:48 -08003483 { path => $path, mode_a => $mode, mode_b => $mode, blob => $blob,
Eric Wong0864e3b2006-11-28 02:50:17 -08003484 pool => SVN::Pool->new, action => 'M' };
Eric Wong27a1a802006-11-27 21:44:48 -08003485}
3486
3487sub add_file {
3488 my ($self, $path, $pb, $cp_path, $cp_rev) = @_;
Eric Wongb03a71a2009-01-11 18:23:38 -08003489 my $mode;
3490
Ben Jackson0d8bee72009-04-11 10:46:17 -07003491 if (!$self->is_path_ignored($path)) {
Eric Wongb03a71a2009-01-11 18:23:38 -08003492 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3493 delete $self->{empty}->{$dir};
3494 $mode = '100644';
3495 }
3496 { path => $path, mode_a => $mode, mode_b => $mode,
Eric Wong0864e3b2006-11-28 02:50:17 -08003497 pool => SVN::Pool->new, action => 'A' };
Eric Wong27a1a802006-11-27 21:44:48 -08003498}
3499
Eric Wongd2a9a872006-12-12 14:47:00 -08003500sub add_directory {
3501 my ($self, $path, $cp_path, $cp_rev) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003502 goto out if $self->is_path_ignored($path);
Eric Wong12a6d752007-12-14 08:39:09 -08003503 my $gpath = $self->git_path($path);
3504 if ($gpath eq '') {
3505 my ($ls, $ctx) = command_output_pipe(qw/ls-tree
3506 -r --name-only -z/,
3507 $self->{c});
3508 local $/ = "\0";
3509 while (<$ls>) {
3510 chomp;
3511 $self->{gii}->remove($_);
3512 print "\tD\t$_\n" unless $::_q;
3513 }
3514 command_close_pipe($ls, $ctx);
3515 $self->{empty}->{$path} = 0;
3516 }
Eric Wongd2a9a872006-12-12 14:47:00 -08003517 my ($dir, $file) = ($path =~ m#^(.*?)/?([^/]+)$#);
3518 delete $self->{empty}->{$dir};
3519 $self->{empty}->{$path} = 1;
Eric Wongb03a71a2009-01-11 18:23:38 -08003520out:
Eric Wongd2a9a872006-12-12 14:47:00 -08003521 { path => $path };
3522}
3523
3524sub change_dir_prop {
3525 my ($self, $db, $prop, $value) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003526 return undef if $self->is_path_ignored($db->{path});
Eric Wongd2a9a872006-12-12 14:47:00 -08003527 $self->{dir_prop}->{$db->{path}} ||= {};
3528 $self->{dir_prop}->{$db->{path}}->{$prop} = $value;
3529 undef;
3530}
3531
3532sub absent_directory {
3533 my ($self, $path, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003534 return undef if $self->is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08003535 $self->{absent_dir}->{$pb->{path}} ||= [];
3536 push @{$self->{absent_dir}->{$pb->{path}}}, $path;
3537 undef;
3538}
3539
3540sub absent_file {
3541 my ($self, $path, $pb) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003542 return undef if $self->is_path_ignored($path);
Eric Wongd2a9a872006-12-12 14:47:00 -08003543 $self->{absent_file}->{$pb->{path}} ||= [];
3544 push @{$self->{absent_file}->{$pb->{path}}}, $path;
3545 undef;
3546}
3547
Eric Wong27a1a802006-11-27 21:44:48 -08003548sub change_file_prop {
3549 my ($self, $fb, $prop, $value) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003550 return undef if $self->is_path_ignored($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08003551 if ($prop eq 'svn:executable') {
3552 if ($fb->{mode_b} != 120000) {
3553 $fb->{mode_b} = defined $value ? 100755 : 100644;
3554 }
3555 } elsif ($prop eq 'svn:special') {
3556 $fb->{mode_b} = defined $value ? 120000 : 100644;
Eric Wongd2a9a872006-12-12 14:47:00 -08003557 } else {
3558 $self->{file_prop}->{$fb->{path}} ||= {};
3559 $self->{file_prop}->{$fb->{path}}->{$prop} = $value;
Eric Wong27a1a802006-11-27 21:44:48 -08003560 }
3561 undef;
3562}
3563
3564sub apply_textdelta {
3565 my ($self, $fb, $exp) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003566 return undef if $self->is_path_ignored($fb->{path});
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003567 my $fh = $::_repository->temp_acquire('svn_delta');
Eric Wong27a1a802006-11-27 21:44:48 -08003568 # $fh gets auto-closed() by SVN::TxDelta::apply(),
3569 # (but $base does not,) so dup() it for reading in close_file
3570 open my $dup, '<&', $fh or croak $!;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003571 my $base = $::_repository->temp_acquire('git_blob');
Eric Wongb03a71a2009-01-11 18:23:38 -08003572
Eric Wong27a1a802006-11-27 21:44:48 -08003573 if ($fb->{blob}) {
Eric Wongbaf5fa82009-01-11 16:51:11 -08003574 my ($base_is_link, $size);
3575
Eric Wongdbc6c742009-01-11 16:51:10 -08003576 if ($fb->{mode_a} eq '120000' &&
3577 ! $self->{empty_symlinks}->{$fb->{path}}) {
3578 print $base 'link ' or die "print $!\n";
Eric Wongbaf5fa82009-01-11 16:51:11 -08003579 $base_is_link = 1;
Eric Wongdbc6c742009-01-11 16:51:10 -08003580 }
Eric Wongbaf5fa82009-01-11 16:51:11 -08003581 retry:
3582 $size = $::_repository->cat_blob($fb->{blob}, $base);
Junio C Hamanod683a0e2008-05-27 23:33:22 -07003583 die "Failed to read object $fb->{blob}" if ($size < 0);
Eric Wong27a1a802006-11-27 21:44:48 -08003584
3585 if (defined $exp) {
3586 seek $base, 0, 0 or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08003587 my $got = ::md5sum($base);
Eric Wongbaf5fa82009-01-11 16:51:11 -08003588 if ($got ne $exp) {
3589 my $err = "Checksum mismatch: ".
3590 "$fb->{path} $fb->{blob}\n" .
3591 "expected: $exp\n" .
3592 " got: $got\n";
3593 if ($base_is_link) {
3594 warn $err,
3595 "Retrying... (possibly ",
3596 "a bad symlink from SVN)\n";
3597 $::_repository->temp_reset($base);
3598 $base_is_link = 0;
3599 goto retry;
3600 }
3601 die $err;
3602 }
Eric Wong27a1a802006-11-27 21:44:48 -08003603 }
3604 }
3605 seek $base, 0, 0 or croak $!;
Marcus Griep0b191382008-08-12 12:00:53 -04003606 $fb->{fh} = $fh;
Eric Wong27a1a802006-11-27 21:44:48 -08003607 $fb->{base} = $base;
Marcus Griep0b191382008-08-12 12:00:53 -04003608 [ SVN::TxDelta::apply($base, $dup, undef, $fb->{path}, $fb->{pool}) ];
Eric Wong27a1a802006-11-27 21:44:48 -08003609}
3610
3611sub close_file {
3612 my ($self, $fb, $exp) = @_;
Ben Jackson0d8bee72009-04-11 10:46:17 -07003613 return undef if $self->is_path_ignored($fb->{path});
Eric Wongb03a71a2009-01-11 18:23:38 -08003614
Eric Wong27a1a802006-11-27 21:44:48 -08003615 my $hash;
Eric Wong706587f2007-01-18 17:50:01 -08003616 my $path = $self->git_path($fb->{path});
Eric Wong27a1a802006-11-27 21:44:48 -08003617 if (my $fh = $fb->{fh}) {
Eric Wong7faf0682007-05-27 15:59:01 -07003618 if (defined $exp) {
3619 seek($fh, 0, 0) or croak $!;
David D. Kilzer8d7c4fa2007-11-22 11:18:00 -08003620 my $got = ::md5sum($fh);
Eric Wong7faf0682007-05-27 15:59:01 -07003621 if ($got ne $exp) {
3622 die "Checksum mismatch: $path\n",
3623 "expected: $exp\n got: $got\n";
3624 }
3625 }
Eric Wong27a1a802006-11-27 21:44:48 -08003626 if ($fb->{mode_b} == 120000) {
Marcus Griep510b0942008-08-12 12:45:39 -04003627 sysseek($fh, 0, 0) or croak $!;
Eric Wongdbc6c742009-01-11 16:51:10 -08003628 my $rd = sysread($fh, my $buf, 5);
Marcus Griep510b0942008-08-12 12:45:39 -04003629
Eric Wongdbc6c742009-01-11 16:51:10 -08003630 if (!defined $rd) {
3631 croak "sysread: $!\n";
3632 } elsif ($rd == 0) {
Marcus Griep510b0942008-08-12 12:45:39 -04003633 warn "$path has mode 120000",
Eric Wongdbc6c742009-01-11 16:51:10 -08003634 " but it points to nothing\n",
3635 "converting to an empty file with mode",
3636 " 100644\n";
3637 $fb->{mode_b} = '100644';
3638 } elsif ($buf ne 'link ') {
3639 warn "$path has mode 120000",
3640 " but is not a link\n";
Marcus Griep510b0942008-08-12 12:45:39 -04003641 } else {
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08003642 my $tmp_fh = $::_repository->temp_acquire(
3643 'svn_hash');
Marcus Griep510b0942008-08-12 12:45:39 -04003644 my $res;
3645 while ($res = sysread($fh, my $str, 1024)) {
3646 my $out = syswrite($tmp_fh, $str, $res);
3647 defined($out) && $out == $res
3648 or croak("write ",
Marcus Griep836ff952008-09-08 12:53:01 -04003649 Git::temp_path($tmp_fh),
Marcus Griep510b0942008-08-12 12:45:39 -04003650 ": $!\n");
3651 }
3652 defined $res or croak $!;
3653
3654 ($fh, $tmp_fh) = ($tmp_fh, $fh);
3655 Git::temp_release($tmp_fh, 1);
Eric Wong7fc35e02007-12-19 00:06:45 -08003656 }
Eric Wong27a1a802006-11-27 21:44:48 -08003657 }
Adam Robenffe256f2008-05-23 16:19:41 +02003658
Marcus Griep0b191382008-08-12 12:00:53 -04003659 $hash = $::_repository->hash_and_insert_object(
Marcus Griep836ff952008-09-08 12:53:01 -04003660 Git::temp_path($fh));
Eric Wong27a1a802006-11-27 21:44:48 -08003661 $hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
Marcus Griep0b191382008-08-12 12:00:53 -04003662
3663 Git::temp_release($fb->{base}, 1);
Marcus Griep510b0942008-08-12 12:45:39 -04003664 Git::temp_release($fh, 1);
Eric Wong27a1a802006-11-27 21:44:48 -08003665 } else {
3666 $hash = $fb->{blob} or die "no blob information\n";
3667 }
3668 $fb->{pool}->clear;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003669 $self->{gii}->update($fb->{mode_b}, $hash, $path) or croak $!;
Eric Wong9e3cdbd2007-02-09 12:23:47 -08003670 print "\t$fb->{action}\t$path\n" if $fb->{action} && ! $::_q;
Eric Wong27a1a802006-11-27 21:44:48 -08003671 undef;
3672}
3673
3674sub abort_edit {
3675 my $self = shift;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003676 $self->{nr} = $self->{gii}->{nr};
3677 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08003678 $self->SUPER::abort_edit(@_);
3679}
3680
3681sub close_edit {
3682 my $self = shift;
Eric Wongdad73c02006-11-28 14:06:05 -08003683 $self->{git_commit_ok} = 1;
Eric Wongef3cfaa2007-01-24 03:30:57 -08003684 $self->{nr} = $self->{gii}->{nr};
3685 delete $self->{gii};
Eric Wong27a1a802006-11-27 21:44:48 -08003686 $self->SUPER::close_edit(@_);
3687}
Eric Wong1a82e792006-06-16 02:55:13 -07003688
Eric Wonga5e0ced2006-06-12 15:23:48 -07003689package SVN::Git::Editor;
Eric Wong24e22aa2007-01-29 00:07:49 -08003690use vars qw/@ISA $_rmdir $_cp_similarity $_find_copies_harder $_rename_limit/;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003691use strict;
3692use warnings;
3693use Carp qw/croak/;
3694use IO::File;
3695
3696sub new {
Eric Wong61395352007-01-27 14:33:08 -08003697 my ($class, $opts) = @_;
3698 foreach (qw/svn_path r ra tree_a tree_b log editor_cb/) {
3699 die "$_ required!\n" unless (defined $opts->{$_});
Eric Wonga5e0ced2006-06-12 15:23:48 -07003700 }
Eric Wong61395352007-01-27 14:33:08 -08003701
3702 my $pool = SVN::Pool->new;
3703 my $mods = generate_diff($opts->{tree_a}, $opts->{tree_b});
3704 my $types = check_diff_paths($opts->{ra}, $opts->{svn_path},
3705 $opts->{r}, $mods);
3706
3707 # $opts->{ra} functions should not be used after this:
3708 my @ce = $opts->{ra}->get_commit_editor($opts->{log},
3709 $opts->{editor_cb}, $pool);
3710 my $self = SVN::Delta::Editor->new(@ce, $pool);
3711 bless $self, $class;
3712 foreach (qw/svn_path r tree_a tree_b/) {
3713 $self->{$_} = $opts->{$_};
3714 }
3715 $self->{url} = $opts->{ra}->{url};
3716 $self->{mods} = $mods;
3717 $self->{types} = $types;
3718 $self->{pool} = $pool;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003719 $self->{bat} = { '' => $self->open_root($self->{r}, $self->{pool}) };
3720 $self->{rm} = { };
Eric Wongd3a840d2007-01-26 01:32:45 -08003721 $self->{path_prefix} = length $self->{svn_path} ?
3722 "$self->{svn_path}/" : '';
Brad King128de652008-07-25 11:32:37 -04003723 $self->{config} = $opts->{config};
Eric Wonga5e0ced2006-06-12 15:23:48 -07003724 return $self;
3725}
3726
Eric Wong61395352007-01-27 14:33:08 -08003727sub generate_diff {
3728 my ($tree_a, $tree_b) = @_;
3729 my @diff_tree = qw(diff-tree -z -r);
Eric Wong24e22aa2007-01-29 00:07:49 -08003730 if ($_cp_similarity) {
3731 push @diff_tree, "-C$_cp_similarity";
Eric Wong61395352007-01-27 14:33:08 -08003732 } else {
3733 push @diff_tree, '-C';
3734 }
Eric Wong24e22aa2007-01-29 00:07:49 -08003735 push @diff_tree, '--find-copies-harder' if $_find_copies_harder;
3736 push @diff_tree, "-l$_rename_limit" if defined $_rename_limit;
Eric Wong61395352007-01-27 14:33:08 -08003737 push @diff_tree, $tree_a, $tree_b;
3738 my ($diff_fh, $ctx) = command_output_pipe(@diff_tree);
3739 local $/ = "\0";
3740 my $state = 'meta';
3741 my @mods;
3742 while (<$diff_fh>) {
3743 chomp $_; # this gets rid of the trailing "\0"
3744 if ($state eq 'meta' && /^:(\d{6})\s(\d{6})\s
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02003745 ($::sha1)\s($::sha1)\s
Eric Wong61395352007-01-27 14:33:08 -08003746 ([MTCRAD])\d*$/xo) {
3747 push @mods, { mode_a => $1, mode_b => $2,
Florian Weimer2d0c8ac2008-08-31 17:05:09 +02003748 sha1_a => $3, sha1_b => $4,
3749 chg => $5 };
3750 if ($5 =~ /^(?:C|R)$/) {
Eric Wong61395352007-01-27 14:33:08 -08003751 $state = 'file_a';
3752 } else {
3753 $state = 'file_b';
3754 }
3755 } elsif ($state eq 'file_a') {
3756 my $x = $mods[$#mods] or croak "Empty array\n";
3757 if ($x->{chg} !~ /^(?:C|R)$/) {
3758 croak "Error parsing $_, $x->{chg}\n";
3759 }
3760 $x->{file_a} = $_;
3761 $state = 'file_b';
3762 } elsif ($state eq 'file_b') {
3763 my $x = $mods[$#mods] or croak "Empty array\n";
3764 if (exists $x->{file_a} && $x->{chg} !~ /^(?:C|R)$/) {
3765 croak "Error parsing $_, $x->{chg}\n";
3766 }
3767 if (!exists $x->{file_a} && $x->{chg} =~ /^(?:C|R)$/) {
3768 croak "Error parsing $_, $x->{chg}\n";
3769 }
3770 $x->{file_b} = $_;
3771 $state = 'meta';
3772 } else {
3773 croak "Error parsing $_\n";
3774 }
3775 }
3776 command_close_pipe($diff_fh, $ctx);
3777 \@mods;
3778}
3779
3780sub check_diff_paths {
3781 my ($ra, $pfx, $rev, $mods) = @_;
3782 my %types;
3783 $pfx .= '/' if length $pfx;
3784
3785 sub type_diff_paths {
3786 my ($ra, $types, $path, $rev) = @_;
3787 my @p = split m#/+#, $path;
3788 my $c = shift @p;
3789 unless (defined $types->{$c}) {
3790 $types->{$c} = $ra->check_path($c, $rev);
3791 }
3792 while (@p) {
3793 $c .= '/' . shift @p;
3794 next if defined $types->{$c};
3795 $types->{$c} = $ra->check_path($c, $rev);
3796 }
3797 }
3798
3799 foreach my $m (@$mods) {
3800 foreach my $f (qw/file_a file_b/) {
3801 next unless defined $m->{$f};
3802 my ($dir) = ($m->{$f} =~ m#^(.*?)/?(?:[^/]+)$#);
3803 if (length $pfx.$dir && ! defined $types{$dir}) {
3804 type_diff_paths($ra, \%types, $pfx.$dir, $rev);
3805 }
3806 }
3807 }
3808 \%types;
3809}
3810
Eric Wonga5e0ced2006-06-12 15:23:48 -07003811sub split_path {
3812 return ($_[0] =~ m#^(.*?)/?([^/]+)$#);
3813}
3814
3815sub repo_path {
Eric Wongd3a840d2007-01-26 01:32:45 -08003816 my ($self, $path) = @_;
3817 $self->{path_prefix}.(defined $path ? $path : '');
Eric Wonga5e0ced2006-06-12 15:23:48 -07003818}
3819
3820sub url_path {
3821 my ($self, $path) = @_;
Eric Wong29633bb2007-07-15 21:53:50 -07003822 if ($self->{url} =~ m#^https?://#) {
Eric Wong6a004d32008-10-21 14:12:15 -07003823 $path =~ s/([^~a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
Eric Wong29633bb2007-07-15 21:53:50 -07003824 }
Eric Wong6e8548c2007-01-27 01:32:00 -08003825 $self->{url} . '/' . $self->repo_path($path);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003826}
3827
3828sub rmdirs {
Eric Wong61395352007-01-27 14:33:08 -08003829 my ($self) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003830 my $rm = $self->{rm};
3831 delete $rm->{''}; # we never delete the url we're tracking
3832 return unless %$rm;
3833
3834 foreach (keys %$rm) {
3835 my @d = split m#/#, $_;
3836 my $c = shift @d;
3837 $rm->{$c} = 1;
3838 while (@d) {
3839 $c .= '/' . shift @d;
3840 $rm->{$c} = 1;
3841 }
3842 }
3843 delete $rm->{$self->{svn_path}};
3844 delete $rm->{''}; # we never delete the url we're tracking
3845 return unless %$rm;
3846
Eric Wong61395352007-01-27 14:33:08 -08003847 my ($fh, $ctx) = command_output_pipe(qw/ls-tree --name-only -r -z/,
3848 $self->{tree_b});
Eric Wonga5e0ced2006-06-12 15:23:48 -07003849 local $/ = "\0";
3850 while (<$fh>) {
3851 chomp;
Eric Wong747fa122006-11-24 22:38:17 -08003852 my @dn = split m#/#, $_;
Eric Wongc07eee12006-06-19 17:59:35 -07003853 while (pop @dn) {
3854 delete $rm->{join '/', @dn};
3855 }
3856 unless (%$rm) {
Eric Wong22600a22007-02-01 13:12:26 -08003857 close $fh;
Eric Wongc07eee12006-06-19 17:59:35 -07003858 return;
3859 }
Eric Wonga5e0ced2006-06-12 15:23:48 -07003860 }
Eric Wongaef4e922006-12-15 10:59:54 -08003861 command_close_pipe($fh, $ctx);
Eric Wongc07eee12006-06-19 17:59:35 -07003862
Eric Wonga5e0ced2006-06-12 15:23:48 -07003863 my ($r, $p, $bat) = ($self->{r}, $self->{pool}, $self->{bat});
3864 foreach my $d (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$rm) {
3865 $self->close_directory($bat->{$d}, $p);
3866 my ($dn) = ($d =~ m#^(.*?)/?(?:[^/]+)$#);
Eric Wong44320b92007-01-13 22:35:53 -08003867 print "\tD+\t$d/\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003868 $self->SUPER::delete_entry($d, $r, $bat->{$dn}, $p);
3869 delete $bat->{$d};
3870 }
3871}
3872
3873sub open_or_add_dir {
3874 my ($self, $full_path, $baton) = @_;
Eric Wong6e8548c2007-01-27 01:32:00 -08003875 my $t = $self->{types}->{$full_path};
3876 if (!defined $t) {
3877 die "$full_path not known in r$self->{r} or we have a bug!\n";
3878 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04003879 {
3880 no warnings 'once';
3881 # SVN::Node::none and SVN::Node::file are used only once,
3882 # so we're shutting up Perl's warnings about them.
3883 if ($t == $SVN::Node::none) {
3884 return $self->add_directory($full_path, $baton,
3885 undef, -1, $self->{pool});
3886 } elsif ($t == $SVN::Node::dir) {
3887 return $self->open_directory($full_path, $baton,
3888 $self->{r}, $self->{pool});
3889 } # no warnings 'once'
3890 print STDERR "$full_path already exists in repository at ",
3891 "r$self->{r} and it is not a directory (",
3892 ($t == $SVN::Node::file ? 'file' : 'unknown'),"/$t)\n";
3893 } # no warnings 'once'
Eric Wonga5e0ced2006-06-12 15:23:48 -07003894 exit 1;
3895}
3896
3897sub ensure_path {
3898 my ($self, $path) = @_;
3899 my $bat = $self->{bat};
Eric Wong6e8548c2007-01-27 01:32:00 -08003900 my $repo_path = $self->repo_path($path);
3901 return $bat->{''} unless (length $repo_path);
3902 my @p = split m#/+#, $repo_path;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003903 my $c = shift @p;
3904 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{''});
3905 while (@p) {
3906 my $c0 = $c;
3907 $c .= '/' . shift @p;
3908 $bat->{$c} ||= $self->open_or_add_dir($c, $bat->{$c0});
3909 }
3910 return $bat->{$c};
3911}
3912
Brad King128de652008-07-25 11:32:37 -04003913# Subroutine to convert a globbing pattern to a regular expression.
3914# From perl cookbook.
3915sub glob2pat {
3916 my $globstr = shift;
3917 my %patmap = ('*' => '.*', '?' => '.', '[' => '[', ']' => ']');
3918 $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
3919 return '^' . $globstr . '$';
3920}
3921
3922sub check_autoprop {
3923 my ($self, $pattern, $properties, $file, $fbat) = @_;
3924 # Convert the globbing pattern to a regular expression.
3925 my $regex = glob2pat($pattern);
3926 # Check if the pattern matches the file name.
3927 if($file =~ m/($regex)/) {
3928 # Parse the list of properties to set.
3929 my @props = split(/;/, $properties);
3930 foreach my $prop (@props) {
3931 # Parse 'name=value' syntax and set the property.
3932 if ($prop =~ /([^=]+)=(.*)/) {
3933 my ($n,$v) = ($1,$2);
3934 for ($n, $v) {
3935 s/^\s+//; s/\s+$//;
3936 }
3937 $self->change_file_prop($fbat, $n, $v);
3938 }
3939 }
3940 }
3941}
3942
3943sub apply_autoprops {
3944 my ($self, $file, $fbat) = @_;
3945 my $conf_t = ${$self->{config}}{'config'};
3946 no warnings 'once';
3947 # Check [miscellany]/enable-auto-props in svn configuration.
3948 if (SVN::_Core::svn_config_get_bool(
3949 $conf_t,
3950 $SVN::_Core::SVN_CONFIG_SECTION_MISCELLANY,
3951 $SVN::_Core::SVN_CONFIG_OPTION_ENABLE_AUTO_PROPS,
3952 0)) {
3953 # Auto-props are enabled. Enumerate them to look for matches.
3954 my $callback = sub {
3955 $self->check_autoprop($_[0], $_[1], $file, $fbat);
3956 };
3957 SVN::_Core::svn_config_enumerate(
3958 $conf_t,
3959 $SVN::_Core::SVN_CONFIG_SECTION_AUTO_PROPS,
3960 $callback);
3961 }
3962}
3963
Eric Wonga5e0ced2006-06-12 15:23:48 -07003964sub A {
Eric Wong44320b92007-01-13 22:35:53 -08003965 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003966 my ($dir, $file) = split_path($m->{file_b});
3967 my $pbat = $self->ensure_path($dir);
3968 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3969 undef, -1);
Eric Wong44320b92007-01-13 22:35:53 -08003970 print "\tA\t$m->{file_b}\n" unless $::_q;
Brad King128de652008-07-25 11:32:37 -04003971 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07003972 $self->chg_file($fbat, $m);
3973 $self->close_file($fbat,undef,$self->{pool});
3974}
3975
3976sub C {
Eric Wong44320b92007-01-13 22:35:53 -08003977 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003978 my ($dir, $file) = split_path($m->{file_b});
3979 my $pbat = $self->ensure_path($dir);
3980 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
3981 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08003982 print "\tC\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003983 $self->chg_file($fbat, $m);
3984 $self->close_file($fbat,undef,$self->{pool});
3985}
3986
3987sub delete_entry {
3988 my ($self, $path, $pbat) = @_;
3989 my $rpath = $self->repo_path($path);
3990 my ($dir, $file) = split_path($rpath);
3991 $self->{rm}->{$dir} = 1;
3992 $self->SUPER::delete_entry($rpath, $self->{r}, $pbat, $self->{pool});
3993}
3994
3995sub R {
Eric Wong44320b92007-01-13 22:35:53 -08003996 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07003997 my ($dir, $file) = split_path($m->{file_b});
3998 my $pbat = $self->ensure_path($dir);
3999 my $fbat = $self->add_file($self->repo_path($m->{file_b}), $pbat,
4000 $self->url_path($m->{file_a}), $self->{r});
Eric Wong44320b92007-01-13 22:35:53 -08004001 print "\tR\t$m->{file_a} => $m->{file_b}\n" unless $::_q;
Paul Talacko7c4d0212008-09-06 18:50:38 -07004002 $self->apply_autoprops($file, $fbat);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004003 $self->chg_file($fbat, $m);
4004 $self->close_file($fbat,undef,$self->{pool});
4005
4006 ($dir, $file) = split_path($m->{file_a});
4007 $pbat = $self->ensure_path($dir);
4008 $self->delete_entry($m->{file_a}, $pbat);
4009}
4010
4011sub M {
Eric Wong44320b92007-01-13 22:35:53 -08004012 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004013 my ($dir, $file) = split_path($m->{file_b});
4014 my $pbat = $self->ensure_path($dir);
4015 my $fbat = $self->open_file($self->repo_path($m->{file_b}),
4016 $pbat,$self->{r},$self->{pool});
Eric Wong44320b92007-01-13 22:35:53 -08004017 print "\t$m->{chg}\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004018 $self->chg_file($fbat, $m);
4019 $self->close_file($fbat,undef,$self->{pool});
4020}
4021
4022sub T { shift->M(@_) }
4023
4024sub change_file_prop {
4025 my ($self, $fbat, $pname, $pval) = @_;
4026 $self->SUPER::change_file_prop($fbat, $pname, $pval, $self->{pool});
4027}
4028
Florian Weimer214a34d2008-08-31 17:45:04 +02004029sub _chg_file_get_blob ($$$$) {
4030 my ($self, $fbat, $m, $which) = @_;
Marten Svanfeldt (dev)1b3069a2008-11-13 00:38:06 +08004031 my $fh = $::_repository->temp_acquire("git_blob_$which");
Florian Weimer214a34d2008-08-31 17:45:04 +02004032 if ($m->{"mode_$which"} =~ /^120/) {
4033 print $fh 'link ' or croak $!;
4034 $self->change_file_prop($fbat,'svn:special','*');
4035 } elsif ($m->{mode_a} =~ /^120/ && $m->{"mode_$which"} !~ /^120/) {
4036 $self->change_file_prop($fbat,'svn:special',undef);
4037 }
4038 my $blob = $m->{"sha1_$which"};
4039 return ($fh,) if ($blob =~ /^0{40}$/);
4040 my $size = $::_repository->cat_blob($blob, $fh);
4041 croak "Failed to read object $blob" if ($size < 0);
4042 $fh->flush == 0 or croak $!;
4043 seek $fh, 0, 0 or croak $!;
4044
4045 my $exp = ::md5sum($fh);
4046 seek $fh, 0, 0 or croak $!;
4047 return ($fh, $exp);
4048}
4049
Eric Wonga5e0ced2006-06-12 15:23:48 -07004050sub chg_file {
4051 my ($self, $fbat, $m) = @_;
4052 if ($m->{mode_b} =~ /755$/ && $m->{mode_a} !~ /755$/) {
4053 $self->change_file_prop($fbat,'svn:executable','*');
4054 } elsif ($m->{mode_b} !~ /755$/ && $m->{mode_a} =~ /755$/) {
4055 $self->change_file_prop($fbat,'svn:executable',undef);
4056 }
Florian Weimer8598db932008-08-31 17:47:09 +02004057 my ($fh_a, $exp_a) = _chg_file_get_blob $self, $fbat, $m, 'a';
4058 my ($fh_b, $exp_b) = _chg_file_get_blob $self, $fbat, $m, 'b';
Eric Wongf7197df2006-10-14 15:48:35 -07004059 my $pool = SVN::Pool->new;
Florian Weimer8598db932008-08-31 17:47:09 +02004060 my $atd = $self->apply_textdelta($fbat, $exp_a, $pool);
4061 if (-s $fh_a) {
4062 my $txstream = SVN::TxDelta::new ($fh_a, $fh_b, $pool);
Eric Wong991255c2008-08-31 19:45:07 -07004063 my $res = SVN::TxDelta::send_txstream($txstream, @$atd, $pool);
4064 if (defined $res) {
4065 die "Unexpected result from send_txstream: $res\n",
4066 "(SVN::Core::VERSION: $SVN::Core::VERSION)\n";
4067 }
Florian Weimer8598db932008-08-31 17:47:09 +02004068 } else {
4069 my $got = SVN::TxDelta::send_stream($fh_b, @$atd, $pool);
4070 die "Checksum mismatch\nexpected: $exp_b\ngot: $got\n"
4071 if ($got ne $exp_b);
4072 }
4073 Git::temp_release($fh_b, 1);
4074 Git::temp_release($fh_a, 1);
Eric Wongf7197df2006-10-14 15:48:35 -07004075 $pool->clear;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004076}
4077
4078sub D {
Eric Wong44320b92007-01-13 22:35:53 -08004079 my ($self, $m) = @_;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004080 my ($dir, $file) = split_path($m->{file_b});
4081 my $pbat = $self->ensure_path($dir);
Eric Wong44320b92007-01-13 22:35:53 -08004082 print "\tD\t$m->{file_b}\n" unless $::_q;
Eric Wonga5e0ced2006-06-12 15:23:48 -07004083 $self->delete_entry($m->{file_b}, $pbat);
4084}
4085
4086sub close_edit {
4087 my ($self) = @_;
4088 my ($p,$bat) = ($self->{pool}, $self->{bat});
4089 foreach (sort { $b =~ tr#/#/# <=> $a =~ tr#/#/# } keys %$bat) {
Eric Wong64427542007-05-19 02:58:37 -07004090 next if $_ eq '';
Eric Wonga5e0ced2006-06-12 15:23:48 -07004091 $self->close_directory($bat->{$_}, $p);
4092 }
Eric Wong64427542007-05-19 02:58:37 -07004093 $self->close_directory($bat->{''}, $p);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004094 $self->SUPER::close_edit($p);
4095 $p->clear;
4096}
4097
4098sub abort_edit {
4099 my ($self) = @_;
4100 $self->SUPER::abort_edit($self->{pool});
Eric Wong61395352007-01-27 14:33:08 -08004101}
4102
4103sub DESTROY {
4104 my $self = shift;
4105 $self->SUPER::DESTROY(@_);
Eric Wonga5e0ced2006-06-12 15:23:48 -07004106 $self->{pool}->clear;
4107}
4108
Eric Wong44320b92007-01-13 22:35:53 -08004109# this drives the editor
4110sub apply_diff {
Eric Wong61395352007-01-27 14:33:08 -08004111 my ($self) = @_;
4112 my $mods = $self->{mods};
Eric Wong44320b92007-01-13 22:35:53 -08004113 my %o = ( D => 1, R => 0, C => -1, A => 3, M => 3, T => 3 );
Eric Wong6e8548c2007-01-27 01:32:00 -08004114 foreach my $m (sort { $o{$a->{chg}} <=> $o{$b->{chg}} } @$mods) {
Eric Wong44320b92007-01-13 22:35:53 -08004115 my $f = $m->{chg};
4116 if (defined $o{$f}) {
4117 $self->$f($m);
4118 } else {
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004119 fatal("Invalid change type: $f");
Eric Wong44320b92007-01-13 22:35:53 -08004120 }
4121 }
Eric Wong24e22aa2007-01-29 00:07:49 -08004122 $self->rmdirs if $_rmdir;
Eric Wong6e8548c2007-01-27 01:32:00 -08004123 if (@$mods == 0) {
Eric Wong44320b92007-01-13 22:35:53 -08004124 $self->abort_edit;
4125 } else {
4126 $self->close_edit;
4127 }
Eric Wong6e8548c2007-01-27 01:32:00 -08004128 return scalar @$mods;
Eric Wong44320b92007-01-13 22:35:53 -08004129}
4130
Eric Wongd81bf822007-01-10 01:22:38 -08004131package Git::SVN::Ra;
Eric Wong6af1db42007-02-14 16:04:10 -08004132use vars qw/@ISA $config_dir $_log_window_size/;
Eric Wongd81bf822007-01-10 01:22:38 -08004133use strict;
4134use warnings;
Eric Wonga51cdb02007-09-07 04:00:40 -07004135my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
Eric Wongd81bf822007-01-10 01:22:38 -08004136
4137BEGIN {
4138 # enforce temporary pool usage for some simple functions
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004139 no strict 'refs';
Eric Wongbf8a40b2009-01-25 15:35:52 -08004140 for my $f (qw/rev_proplist get_latest_revnum get_uuid get_repos_root
4141 get_file/) {
Sam Vilainc5f71ad2007-06-15 15:43:59 +12004142 my $SUPER = "SUPER::$f";
4143 *$f = sub {
4144 my $self = shift;
4145 my $pool = SVN::Pool->new;
4146 my @ret = $self->$SUPER(@_,$pool);
4147 $pool->clear;
4148 wantarray ? @ret : $ret[0];
4149 };
Eric Wongd81bf822007-01-10 01:22:38 -08004150 }
Eric Wongd81bf822007-01-10 01:22:38 -08004151}
4152
Steven Walter9ff74e92007-09-28 13:24:19 -04004153sub _auth_providers () {
4154 [
4155 SVN::Client::get_simple_provider(),
4156 SVN::Client::get_ssl_server_trust_file_provider(),
4157 SVN::Client::get_simple_prompt_provider(
4158 \&Git::SVN::Prompt::simple, 2),
4159 SVN::Client::get_ssl_client_cert_file_provider(),
4160 SVN::Client::get_ssl_client_cert_prompt_provider(
4161 \&Git::SVN::Prompt::ssl_client_cert, 2),
Sebastian Noack77266e92008-02-25 15:56:28 +01004162 SVN::Client::get_ssl_client_cert_pw_file_provider(),
Steven Walter9ff74e92007-09-28 13:24:19 -04004163 SVN::Client::get_ssl_client_cert_pw_prompt_provider(
4164 \&Git::SVN::Prompt::ssl_client_cert_pw, 2),
4165 SVN::Client::get_username_provider(),
4166 SVN::Client::get_ssl_server_trust_prompt_provider(
4167 \&Git::SVN::Prompt::ssl_server_trust),
4168 SVN::Client::get_username_prompt_provider(
4169 \&Git::SVN::Prompt::username, 2)
4170 ]
4171}
4172
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004173sub escape_uri_only {
4174 my ($uri) = @_;
4175 my @tmp;
4176 foreach (split m{/}, $uri) {
Eric Wong6a004d32008-10-21 14:12:15 -07004177 s/([^~\w.%+-]|%(?![a-fA-F0-9]{2}))/sprintf("%%%02X",ord($1))/eg;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004178 push @tmp, $_;
4179 }
4180 join('/', @tmp);
4181}
4182
4183sub escape_url {
4184 my ($url) = @_;
4185 if ($url =~ m#^(https?)://([^/]+)(.*)$#) {
4186 my ($scheme, $domain, $uri) = ($1, $2, escape_uri_only($3));
4187 $url = "$scheme://$domain$uri";
4188 }
4189 $url;
4190}
4191
Eric Wongd81bf822007-01-10 01:22:38 -08004192sub new {
4193 my ($class, $url) = @_;
Eric Wongf6f09872007-01-18 18:22:18 -08004194 $url =~ s!/+$!!;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004195 return $RA if ($RA && $RA->{url} eq $url);
Eric Wongf6f09872007-01-18 18:22:18 -08004196
Eric Wongd81bf822007-01-10 01:22:38 -08004197 SVN::_Core::svn_config_ensure($config_dir, undef);
Steven Walter9ff74e92007-09-28 13:24:19 -04004198 my ($baton, $callbacks) = SVN::Core::auth_open_helper(_auth_providers);
Eric Wongd81bf822007-01-10 01:22:38 -08004199 my $config = SVN::Core::config_get_config($config_dir);
Eric Wong7730fbe2007-07-04 14:07:42 -07004200 $RA = undef;
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004201 my $dont_store_passwords = 1;
4202 my $conf_t = ${$config}{'config'};
4203 {
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004204 no warnings 'once';
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004205 # The usage of $SVN::_Core::SVN_CONFIG_* variables
4206 # produces warnings that variables are used only once.
4207 # I had not found the better way to shut them up, so
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004208 # the warnings of type 'once' are disabled in this block.
Eygene Ryabinkin602015e2007-10-06 22:57:19 +04004209 if (SVN::_Core::svn_config_get_bool($conf_t,
4210 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4211 $SVN::_Core::SVN_CONFIG_OPTION_STORE_PASSWORDS,
4212 1) == 0) {
4213 SVN::_Core::svn_auth_set_parameter($baton,
4214 $SVN::_Core::SVN_AUTH_PARAM_DONT_STORE_PASSWORDS,
4215 bless (\$dont_store_passwords, "_p_void"));
4216 }
4217 if (SVN::_Core::svn_config_get_bool($conf_t,
4218 $SVN::_Core::SVN_CONFIG_SECTION_AUTH,
4219 $SVN::_Core::SVN_CONFIG_OPTION_STORE_AUTH_CREDS,
4220 1) == 0) {
4221 $Git::SVN::Prompt::_no_auth_cache = 1;
4222 }
Eygene Ryabinkinfd499bc2007-10-15 11:19:12 +04004223 } # no warnings 'once'
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004224 my $self = SVN::Ra->new(url => escape_url($url), auth => $baton,
Eric Wongd81bf822007-01-10 01:22:38 -08004225 config => $config,
4226 pool => SVN::Pool->new,
4227 auth_provider_callbacks => $callbacks);
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004228 $self->{url} = $url;
Eric Wongd81bf822007-01-10 01:22:38 -08004229 $self->{svn_path} = $url;
4230 $self->{repos_root} = $self->get_repos_root;
Eric Wong4e9f6cc2007-02-09 12:17:57 -08004231 $self->{svn_path} =~ s#^\Q$self->{repos_root}\E(/|$)##;
Eric Wong0dc03d62007-05-13 01:04:43 -07004232 $self->{cache} = { check_path => { r => 0, data => {} },
4233 get_dir => { r => 0, data => {} } };
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004234 $RA = bless $self, $class;
Eric Wongd81bf822007-01-10 01:22:38 -08004235}
4236
Eric Wong0dc03d62007-05-13 01:04:43 -07004237sub check_path {
4238 my ($self, $path, $r) = @_;
4239 my $cache = $self->{cache}->{check_path};
4240 if ($r == $cache->{r} && exists $cache->{data}->{$path}) {
4241 return $cache->{data}->{$path};
4242 }
4243 my $pool = SVN::Pool->new;
4244 my $t = $self->SUPER::check_path($path, $r, $pool);
4245 $pool->clear;
4246 if ($r != $cache->{r}) {
4247 %{$cache->{data}} = ();
4248 $cache->{r} = $r;
4249 }
4250 $cache->{data}->{$path} = $t;
4251}
4252
4253sub get_dir {
4254 my ($self, $dir, $r) = @_;
4255 my $cache = $self->{cache}->{get_dir};
4256 if ($r == $cache->{r}) {
4257 if (my $x = $cache->{data}->{$dir}) {
4258 return wantarray ? @$x : $x->[0];
4259 }
4260 }
4261 my $pool = SVN::Pool->new;
4262 my ($d, undef, $props) = $self->SUPER::get_dir($dir, $r, $pool);
4263 my %dirents = map { $_ => { kind => $d->{$_}->kind } } keys %$d;
4264 $pool->clear;
4265 if ($r != $cache->{r}) {
4266 %{$cache->{data}} = ();
4267 $cache->{r} = $r;
4268 }
4269 $cache->{data}->{$dir} = [ \%dirents, $r, $props ];
4270 wantarray ? (\%dirents, $r, $props) : \%dirents;
4271}
4272
Eric Wongd81bf822007-01-10 01:22:38 -08004273sub DESTROY {
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004274 # do not call the real DESTROY since we store ourselves in $RA
Eric Wongd81bf822007-01-10 01:22:38 -08004275}
4276
Eric Wong1ef626b2009-01-17 22:11:44 -08004277# get_log(paths, start, end, limit,
4278# discover_changed_paths, strict_node_history, receiver)
Eric Wongd81bf822007-01-10 01:22:38 -08004279sub get_log {
4280 my ($self, @args) = @_;
4281 my $pool = SVN::Pool->new;
Eric Wong1ef626b2009-01-17 22:11:44 -08004282
4283 # the limit parameter was not supported in SVN 1.1.x, so we
4284 # drop it. Therefore, the receiver callback passed to it
4285 # is made aware of this limitation by being wrapped if
4286 # the limit passed to is being wrapped.
4287 if ($SVN::Core::VERSION le '1.2.0') {
4288 my $limit = splice(@args, 3, 1);
4289 if ($limit > 0) {
4290 my $receiver = pop @args;
4291 push(@args, sub { &$receiver(@_) if (--$limit >= 0) });
4292 }
4293 }
Eric Wongd81bf822007-01-10 01:22:38 -08004294 my $ret = $self->SUPER::get_log(@args, $pool);
4295 $pool->clear;
4296 $ret;
4297}
4298
Steven Walter9ff74e92007-09-28 13:24:19 -04004299sub trees_match {
4300 my ($self, $url1, $rev1, $url2, $rev2) = @_;
4301 my $ctx = SVN::Client->new(auth => _auth_providers);
4302 my $out = IO::File->new_tmpfile;
4303
4304 # older SVN (1.1.x) doesn't take $pool as the last parameter for
4305 # $ctx->diff(), so we'll create a default one
4306 my $pool = SVN::Pool->new_default_sub;
4307
4308 $ra_invalid = 1; # this will open a new SVN::Ra connection to $url1
4309 $ctx->diff([], $url1, $rev1, $url2, $rev2, 1, 1, 0, $out, $out);
4310 $out->flush;
4311 my $ret = (($out->stat)[7] == 0);
4312 close $out or croak $!;
4313
4314 $ret;
4315}
4316
Eric Wongd81bf822007-01-10 01:22:38 -08004317sub get_commit_editor {
Eric Wong44320b92007-01-13 22:35:53 -08004318 my ($self, $log, $cb, $pool) = @_;
Eric Wongd81bf822007-01-10 01:22:38 -08004319 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
Eric Wong44320b92007-01-13 22:35:53 -08004320 $self->SUPER::get_commit_editor($log, $cb, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004321}
4322
Eric Wongd81bf822007-01-10 01:22:38 -08004323sub gs_do_update {
Eric Wong8a603772007-01-31 02:45:50 -08004324 my ($self, $rev_a, $rev_b, $gs, $editor) = @_;
4325 my $new = ($rev_a == $rev_b);
4326 my $path = $gs->{path};
4327
Eric Wong2e5e2482007-02-23 02:21:59 -08004328 if ($new && -e $gs->{index}) {
4329 unlink $gs->{index} or die
4330 "Couldn't unlink index: $gs->{index}: $!\n";
4331 }
Eric Wongd81bf822007-01-10 01:22:38 -08004332 my $pool = SVN::Pool->new;
Eric Wong8b8fc062007-01-22 11:44:57 -08004333 $editor->set_path_strip($path);
Eric Wong2b27f6c2007-01-28 04:59:05 -08004334 my (@pc) = split m#/#, $path;
4335 my $reporter = $self->do_update($rev_b, (@pc ? shift @pc : ''),
Eric Wong8a603772007-01-31 02:45:50 -08004336 1, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004337 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong2b27f6c2007-01-28 04:59:05 -08004338
4339 # Since we can't rely on svn_ra_reparent being available, we'll
4340 # just have to do some magic with set_path to make it so
4341 # we only want a partial path.
4342 my $sp = '';
4343 my $final = join('/', @pc);
4344 while (@pc) {
4345 $reporter->set_path($sp, $rev_b, 0, @lock, $pool);
4346 $sp .= '/' if length $sp;
4347 $sp .= shift @pc;
4348 }
4349 die "BUG: '$sp' != '$final'\n" if ($sp ne $final);
4350
Eric Wong2b27f6c2007-01-28 04:59:05 -08004351 $reporter->set_path($sp, $rev_a, $new, @lock, $pool);
4352
Eric Wongd81bf822007-01-10 01:22:38 -08004353 $reporter->finish_report($pool);
4354 $pool->clear;
4355 $editor->{git_commit_ok};
4356}
4357
Eric Wong2b27f6c2007-01-28 04:59:05 -08004358# this requires SVN 1.4.3 or later (do_switch didn't work before 1.4.3, and
4359# svn_ra_reparent didn't work before 1.4)
Eric Wongd81bf822007-01-10 01:22:38 -08004360sub gs_do_switch {
Eric Wong8a603772007-01-31 02:45:50 -08004361 my ($self, $rev_a, $rev_b, $gs, $url_b, $editor) = @_;
4362 my $path = $gs->{path};
Eric Wongd81bf822007-01-10 01:22:38 -08004363 my $pool = SVN::Pool->new;
Eric Wong2b27f6c2007-01-28 04:59:05 -08004364
4365 my $full_url = $self->{url};
4366 my $old_url = $full_url;
Eric Wongcfbe7ab2007-11-11 23:37:42 -08004367 $full_url .= '/' . escape_uri_only($path) if length $path;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004368 my ($ra, $reparented);
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004369
4370 if ($old_url =~ m#^svn(\+ssh)?://#) {
4371 $_[0] = undef;
4372 $self = undef;
4373 $RA = undef;
4374 $ra = Git::SVN::Ra->new($full_url);
4375 $ra_invalid = 1;
4376 } elsif ($old_url ne $full_url) {
4377 SVN::_Ra::svn_ra_reparent($self->{session}, $full_url, $pool);
4378 $self->{url} = $full_url;
4379 $reparented = 1;
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004380 }
Alec Berrymanad0a82b2008-09-14 17:14:16 -04004381
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004382 $ra ||= $self;
Eric Wongf4392df2008-09-06 20:18:18 -07004383 $url_b = escape_url($url_b);
Eric Wong8a603772007-01-31 02:45:50 -08004384 my $reporter = $ra->do_switch($rev_b, '', 1, $url_b, $editor, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004385 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef) : ();
Eric Wong8b8fc062007-01-22 11:44:57 -08004386 $reporter->set_path('', $rev_a, 0, @lock, $pool);
Eric Wongd81bf822007-01-10 01:22:38 -08004387 $reporter->finish_report($pool);
Eric Wong2b27f6c2007-01-28 04:59:05 -08004388
Eric Wong5d3b7cd2007-01-29 19:16:01 -08004389 if ($reparented) {
4390 SVN::_Ra::svn_ra_reparent($self->{session}, $old_url, $pool);
4391 $self->{url} = $old_url;
4392 }
Eric Wong2b27f6c2007-01-28 04:59:05 -08004393
Eric Wongd81bf822007-01-10 01:22:38 -08004394 $pool->clear;
4395 $editor->{git_commit_ok};
4396}
4397
Eric Wongb54a9012007-06-13 02:37:03 -07004398sub longest_common_path {
4399 my ($gsv, $globs) = @_;
Eric Wongd2ae1432007-02-07 11:50:16 -08004400 my %common;
Eric Wonge5181922007-02-08 12:53:57 -08004401 my $common_max = scalar @$gsv;
4402
4403 foreach my $gs (@$gsv) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004404 my @tmp = split m#/#, $gs->{path};
4405 my $p = '';
4406 foreach (@tmp) {
4407 $p .= length($p) ? "/$_" : $_;
4408 $common{$p} ||= 0;
4409 $common{$p}++;
4410 }
4411 }
Eric Wonge5181922007-02-08 12:53:57 -08004412 $globs ||= [];
4413 $common_max += scalar @$globs;
4414 foreach my $glob (@$globs) {
4415 my @tmp = split m#/#, $glob->{path}->{left};
4416 my $p = '';
4417 foreach (@tmp) {
4418 $p .= length($p) ? "/$_" : $_;
4419 $common{$p} ||= 0;
4420 $common{$p}++;
4421 }
4422 }
4423
Eric Wongd2ae1432007-02-07 11:50:16 -08004424 my $longest_path = '';
4425 foreach (sort {length $b <=> length $a} keys %common) {
Eric Wonge5181922007-02-08 12:53:57 -08004426 if ($common{$_} == $common_max) {
Eric Wongd2ae1432007-02-07 11:50:16 -08004427 $longest_path = $_;
4428 last;
4429 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004430 }
Eric Wongb54a9012007-06-13 02:37:03 -07004431 $longest_path;
4432}
4433
4434sub gs_fetch_loop_common {
4435 my ($self, $base, $head, $gsv, $globs) = @_;
4436 return if ($base > $head);
4437 my $inc = $_log_window_size;
4438 my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
4439 my $longest_path = longest_common_path($gsv, $globs);
Eric Wonga51cdb02007-09-07 04:00:40 -07004440 my $ra_url = $self->{url};
Eric Wong0af9c9f2007-01-27 22:28:56 -08004441 while (1) {
Eric Wongd4eff2b2007-01-30 14:04:22 -08004442 my %revs;
Eric Wongd2ae1432007-02-07 11:50:16 -08004443 my $err;
Eric Wongf7c3fc42007-01-29 18:34:55 -08004444 my $err_handler = $SVN::Error::handler;
Eric Wongd2ae1432007-02-07 11:50:16 -08004445 $SVN::Error::handler = sub {
4446 ($err) = @_;
4447 skip_unknown_revs($err);
4448 };
4449 sub _cb {
4450 my ($paths, $r, $author, $date, $log) = @_;
4451 [ dup_changed_paths($paths),
4452 { author => $author, date => $date, log => $log } ];
4453 }
4454 $self->get_log([$longest_path], $min, $max, 0, 1, 1,
4455 sub { $revs{$_[1]} = _cb(@_) });
Deskin Miller99366562009-02-08 19:33:18 -05004456 if ($err) {
4457 print "Checked through r$max\r";
4458 }
Eric Wongd2ae1432007-02-07 11:50:16 -08004459 if ($err && $max >= $head) {
4460 print STDERR "Path '$longest_path' ",
4461 "was probably deleted:\n",
4462 $err->expanded_message,
4463 "\nWill attempt to follow ",
4464 "revisions r$min .. r$max ",
4465 "committed before the deletion\n";
4466 my $hi = $max;
4467 while (--$hi >= $min) {
4468 my $ok;
4469 $self->get_log([$longest_path], $min, $hi,
4470 0, 1, 1, sub {
4471 $ok ||= $_[1];
4472 $revs{$_[1]} = _cb(@_) });
4473 if ($ok) {
4474 print STDERR "r$min .. r$ok OK\n";
4475 last;
4476 }
4477 }
4478 }
Eric Wongd4eff2b2007-01-30 14:04:22 -08004479 $SVN::Error::handler = $err_handler;
Eric Wongfbcc1732007-02-06 18:35:30 -08004480
Eric Wonge5181922007-02-08 12:53:57 -08004481 my %exists = map { $_->{path} => $_ } @$gsv;
Eric Wongd4eff2b2007-01-30 14:04:22 -08004482 foreach my $r (sort {$a <=> $b} keys %revs) {
Eric Wongfbcc1732007-02-06 18:35:30 -08004483 my ($paths, $logged) = @{$revs{$r}};
Eric Wonge5181922007-02-08 12:53:57 -08004484
4485 foreach my $gs ($self->match_globs(\%exists, $paths,
4486 $globs, $r)) {
Eric Wong060610c2007-12-08 23:27:41 -08004487 if ($gs->rev_map_max >= $r) {
Eric Wongfbcc1732007-02-06 18:35:30 -08004488 next;
4489 }
4490 next unless $gs->match_paths($paths, $r);
4491 $gs->{logged_rev_props} = $logged;
Eric Wonge8d120b2007-02-14 16:29:52 -08004492 if (my $last_commit = $gs->last_commit) {
4493 $gs->assert_index_clean($last_commit);
4494 }
Eric Wongfbcc1732007-02-06 18:35:30 -08004495 my $log_entry = $gs->do_fetch($paths, $r);
4496 if ($log_entry) {
Eric Wong0af9c9f2007-01-27 22:28:56 -08004497 $gs->do_git_commit($log_entry);
4498 }
Eric Wong321b1842008-01-02 10:10:03 -08004499 $INDEX_FILES{$gs->{index}} = 1;
Eric Wong0af9c9f2007-01-27 22:28:56 -08004500 }
Eric Wonge5181922007-02-08 12:53:57 -08004501 foreach my $g (@$globs) {
Eric Wong93f26892007-02-11 01:20:26 -08004502 my $k = "svn-remote.$g->{remote}." .
4503 "$g->{t}-maxRev";
4504 Git::SVN::tmp_config($k, $r);
Eric Wonge5181922007-02-08 12:53:57 -08004505 }
Eric Wonga51cdb02007-09-07 04:00:40 -07004506 if ($ra_invalid) {
4507 $_[0] = undef;
4508 $self = undef;
4509 $RA = undef;
4510 $self = Git::SVN::Ra->new($ra_url);
4511 $ra_invalid = undef;
4512 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004513 }
Eric Wong9c93fee2007-01-31 17:22:31 -08004514 # pre-fill the .rev_db since it'll eventually get filled in
4515 # with '0' x40 if something new gets committed
Eric Wonge5181922007-02-08 12:53:57 -08004516 foreach my $gs (@$gsv) {
Eric Wong66ab84b2007-12-08 23:27:42 -08004517 next if $gs->rev_map_max >= $max;
4518 next if defined $gs->rev_map_get($max);
4519 $gs->rev_map_set($max, 0 x40);
Eric Wong9c93fee2007-01-31 17:22:31 -08004520 }
Eric Wongc3560e52007-02-12 16:03:32 -08004521 foreach my $g (@$globs) {
4522 my $k = "svn-remote.$g->{remote}.$g->{t}-maxRev";
4523 Git::SVN::tmp_config($k, $max);
4524 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004525 last if $max >= $head;
4526 $min = $max + 1;
4527 $max += $inc;
4528 $max = $head if ($max > $head);
4529 }
Karl Hasselström94bc9142008-02-03 17:56:18 +01004530 Git::SVN::gc();
Eric Wong0af9c9f2007-01-27 22:28:56 -08004531}
4532
Marcus Griep570d35c2008-08-08 01:41:57 -07004533sub get_dir_globbed {
4534 my ($self, $left, $depth, $r) = @_;
4535
4536 my @x = eval { $self->get_dir($left, $r) };
4537 return unless scalar @x == 3;
4538 my $dirents = $x[0];
4539 my @finalents;
4540 foreach my $de (keys %$dirents) {
4541 next if $dirents->{$de}->{kind} != $SVN::Node::dir;
4542 if ($depth > 1) {
4543 my @args = ("$left/$de", $depth - 1, $r);
4544 foreach my $dir ($self->get_dir_globbed(@args)) {
4545 push @finalents, "$de/$dir";
4546 }
4547 } else {
4548 push @finalents, $de;
4549 }
4550 }
4551 @finalents;
4552}
4553
Eric Wonge5181922007-02-08 12:53:57 -08004554sub match_globs {
4555 my ($self, $exists, $paths, $globs, $r) = @_;
Eric Wong74a81222007-02-10 13:28:50 -08004556
4557 sub get_dir_check {
4558 my ($self, $exists, $g, $r) = @_;
Marcus Griep570d35c2008-08-08 01:41:57 -07004559
4560 my @dirs = $self->get_dir_globbed($g->{path}->{left},
4561 $g->{path}->{depth},
4562 $r);
4563
4564 foreach my $de (@dirs) {
Eric Wong74a81222007-02-10 13:28:50 -08004565 my $p = $g->{path}->full_path($de);
4566 next if $exists->{$p};
4567 next if (length $g->{path}->{right} &&
4568 ($self->check_path($p, $r) !=
4569 $SVN::Node::dir));
4570 $exists->{$p} = Git::SVN->init($self->{url}, $p, undef,
4571 $g->{ref}->full_path($de), 1);
4572 }
4573 }
Eric Wonge5181922007-02-08 12:53:57 -08004574 foreach my $g (@$globs) {
Eric Wong74a81222007-02-10 13:28:50 -08004575 if (my $path = $paths->{"/$g->{path}->{left}"}) {
4576 if ($path->{action} =~ /^[AR]$/) {
4577 get_dir_check($self, $exists, $g, $r);
4578 }
4579 }
Eric Wonge5181922007-02-08 12:53:57 -08004580 foreach (keys %$paths) {
Eric Wong28710f72007-02-14 13:32:21 -08004581 if (/$g->{path}->{left_regex}/ &&
4582 !/$g->{path}->{regex}/) {
Eric Wong74a81222007-02-10 13:28:50 -08004583 next if $paths->{$_}->{action} !~ /^[AR]$/;
4584 get_dir_check($self, $exists, $g, $r);
4585 }
Eric Wonge5181922007-02-08 12:53:57 -08004586 next unless /$g->{path}->{regex}/;
4587 my $p = $1;
4588 my $pathname = $g->{path}->full_path($p);
4589 next if $exists->{$pathname};
Eric Wong0c1ec5a2007-04-18 00:17:33 -07004590 next if ($self->check_path($pathname, $r) !=
4591 $SVN::Node::dir);
Eric Wonge5181922007-02-08 12:53:57 -08004592 $exists->{$pathname} = Git::SVN->init(
4593 $self->{url}, $pathname, undef,
4594 $g->{ref}->full_path($p), 1);
4595 }
4596 my $c = '';
4597 foreach (split m#/#, $g->{path}->{left}) {
4598 $c .= "/$_";
4599 next unless ($paths->{$c} &&
Eric Wong74a81222007-02-10 13:28:50 -08004600 ($paths->{$c}->{action} =~ /^[AR]$/));
4601 get_dir_check($self, $exists, $g, $r);
Eric Wonge5181922007-02-08 12:53:57 -08004602 }
4603 }
4604 values %$exists;
4605}
4606
Eric Wonge6434f82007-01-23 16:29:23 -08004607sub minimize_url {
4608 my ($self) = @_;
4609 return $self->{url} if ($self->{url} eq $self->{repos_root});
4610 my $url = $self->{repos_root};
4611 my @components = split(m!/!, $self->{svn_path});
4612 my $c = '';
4613 do {
4614 $url .= "/$c" if length $c;
4615 eval { (ref $self)->new($url)->get_latest_revnum };
4616 } while ($@ && ($c = shift @components));
4617 $url;
4618}
4619
Eric Wongd81bf822007-01-10 01:22:38 -08004620sub can_do_switch {
4621 my $self = shift;
4622 unless (defined $can_do_switch) {
4623 my $pool = SVN::Pool->new;
4624 my $rep = eval {
4625 $self->do_switch(1, '', 0, $self->{url},
4626 SVN::Delta::Editor->new, $pool);
4627 };
4628 if ($@) {
4629 $can_do_switch = 0;
4630 } else {
4631 $rep->abort_report($pool);
4632 $can_do_switch = 1;
4633 }
4634 $pool->clear;
4635 }
4636 $can_do_switch;
4637}
4638
Eric Wong0af9c9f2007-01-27 22:28:56 -08004639sub skip_unknown_revs {
4640 my ($err) = @_;
4641 my $errno = $err->apr_err();
4642 # Maybe the branch we're tracking didn't
4643 # exist when the repo started, so it's
4644 # not an error if it doesn't, just continue
4645 #
4646 # Wonderfully consistent library, eh?
4647 # 160013 - svn:// and file://
4648 # 175002 - http(s)://
4649 # 175007 - http(s):// (this repo required authorization, too...)
4650 # More codes may be discovered later...
4651 if ($errno == 175007 || $errno == 175002 || $errno == 160013) {
Eric Wonga6a15a92007-03-30 17:54:48 -07004652 my $err_key = $err->expanded_message;
4653 # revision numbers change every time, filter them out
4654 $err_key =~ s/\d+/\0/g;
4655 $err_key = "$errno\0$err_key";
4656 unless ($ignored_err{$err_key}) {
4657 warn "W: Ignoring error from SVN, path probably ",
4658 "does not exist: ($errno): ",
4659 $err->expanded_message,"\n";
Eric Wongeee8a172008-01-07 02:40:40 -08004660 warn "W: Do not be alarmed at the above message ",
4661 "git-svn is just searching aggressively for ",
4662 "old history.\n",
4663 "This may take a while on large repositories\n";
Eric Wonga6a15a92007-03-30 17:54:48 -07004664 $ignored_err{$err_key} = 1;
4665 }
Eric Wong0af9c9f2007-01-27 22:28:56 -08004666 return;
4667 }
4668 die "Error from SVN, ($errno): ", $err->expanded_message,"\n";
4669}
4670
4671# svn_log_changed_path_t objects passed to get_log are likely to be
4672# overwritten even if only the refs are copied to an external variable,
4673# so we should dup the structures in their entirety. Using an externally
4674# passed pool (instead of our temporary and quickly cleared pool in
4675# Git::SVN::Ra) does not help matters at all...
4676sub dup_changed_paths {
4677 my ($paths) = @_;
4678 return undef unless $paths;
4679 my %ret;
4680 foreach my $p (keys %$paths) {
4681 my $i = $paths->{$p};
4682 my %s = map { $_ => $i->$_ }
4683 qw/copyfrom_path copyfrom_rev action/;
4684 $ret{$p} = \%s;
4685 }
4686 \%ret;
4687}
4688
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004689package Git::SVN::Log;
4690use strict;
4691use warnings;
4692use POSIX qw/strftime/;
Ben Waltone8717842009-02-24 14:44:49 -05004693use Time::Local;
David D Kilzer111947e2007-11-11 22:56:52 -08004694use constant commit_log_separator => ('-' x 72) . "\n";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004695use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
4696 %rusers $show_commit $incremental/;
4697my $l_fmt;
4698
4699sub cmt_showable {
4700 my ($c) = @_;
4701 return 1 if defined $c->{r};
Eric Wongc16d0872007-04-08 00:59:22 -07004702
4703 # big commit message got truncated by the 16k pretty buffer in rev-list
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004704 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
4705 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
Eric Wongc16d0872007-04-08 00:59:22 -07004706 @{$c->{l}} = ();
Eric Wong44320b92007-01-13 22:35:53 -08004707 my @log = command(qw/cat-file commit/, $c->{c});
Eric Wongc16d0872007-04-08 00:59:22 -07004708
4709 # shift off the headers
4710 shift @log while ($log[0] ne '');
Eric Wong44320b92007-01-13 22:35:53 -08004711 shift @log;
Eric Wongc16d0872007-04-08 00:59:22 -07004712
4713 # TODO: make $c->{l} not have a trailing newline in the future
4714 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004715
4716 (undef, $c->{r}, undef) = ::extract_metadata(
Eric Wong44320b92007-01-13 22:35:53 -08004717 (grep(/^git-svn-id: /, @log))[-1]);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004718 }
4719 return defined $c->{r};
4720}
4721
4722sub log_use_color {
Jeff Kingcd459e32007-12-11 01:28:42 -05004723 return $color || Git->repository->get_colorbool('color.diff');
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004724}
4725
4726sub git_svn_log_cmd {
Eric Wong3bc718b2007-02-13 17:09:40 -08004727 my ($r_min, $r_max, @args) = @_;
4728 my $head = 'HEAD';
Eric Wong6ed77262007-08-15 09:55:18 -07004729 my (@files, @log_opts);
Eric Wong3bc718b2007-02-13 17:09:40 -08004730 foreach my $x (@args) {
Eric Wong6ed77262007-08-15 09:55:18 -07004731 if ($x eq '--' || @files) {
4732 push @files, $x;
4733 } else {
4734 if (::verify_ref("$x^0")) {
4735 $head = $x;
4736 } else {
4737 push @log_opts, $x;
4738 }
4739 }
Eric Wong3bc718b2007-02-13 17:09:40 -08004740 }
4741
Eric Wong13c823f2007-04-08 00:59:19 -07004742 my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
4743 $gs ||= Git::SVN->_new;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004744 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
4745 $gs->refname);
4746 push @cmd, '-r' unless $non_recursive;
4747 push @cmd, qw/--raw --name-status/ if $verbose;
4748 push @cmd, '--color' if log_use_color();
Eric Wong6ed77262007-08-15 09:55:18 -07004749 push @cmd, @log_opts;
4750 if (defined $r_max && $r_max == $r_min) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004751 push @cmd, '--max-count=1';
Eric Wong060610c2007-12-08 23:27:41 -08004752 if (my $c = $gs->rev_map_get($r_max)) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004753 push @cmd, $c;
4754 }
Eric Wong6ed77262007-08-15 09:55:18 -07004755 } elsif (defined $r_max) {
David D Kilzer111947e2007-11-11 22:56:52 -08004756 if ($r_max < $r_min) {
4757 ($r_min, $r_max) = ($r_max, $r_min);
4758 }
4759 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
4760 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
4761 # If there are no commits in the range, both $c_max and $c_min
4762 # will be undefined. If there is at least 1 commit in the
4763 # range, both will be defined.
4764 return () if !defined $c_min || !defined $c_max;
4765 if ($c_min eq $c_max) {
4766 push @cmd, '--max-count=1', $c_min;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004767 } else {
David D Kilzer111947e2007-11-11 22:56:52 -08004768 push @cmd, '--boundary', "$c_min..$c_max";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004769 }
4770 }
Eric Wong6ed77262007-08-15 09:55:18 -07004771 return (@cmd, @files);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004772}
4773
4774# adapted from pager.c
4775sub config_pager {
4776 $pager ||= $ENV{GIT_PAGER} || $ENV{PAGER};
4777 if (!defined $pager) {
4778 $pager = 'less';
4779 } elsif (length $pager == 0 || $pager eq 'cat') {
4780 $pager = undef;
4781 }
Jeff Kingcd459e32007-12-11 01:28:42 -05004782 $ENV{GIT_PAGER_IN_USE} = defined($pager);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004783}
4784
4785sub run_pager {
Eric Wongb0193042007-09-21 18:48:45 -07004786 return unless -t *STDOUT && defined $pager;
Marcus Griep971e6282008-09-10 11:09:46 -04004787 pipe my ($rfd, $wfd) or return;
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004788 defined(my $pid = fork) or ::fatal "Can't fork: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004789 if (!$pid) {
4790 open STDOUT, '>&', $wfd or
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004791 ::fatal "Can't redirect to stdout: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004792 return;
4793 }
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004794 open STDIN, '<&', $rfd or ::fatal "Can't redirect stdin: $!";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004795 $ENV{LESS} ||= 'FRSX';
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004796 exec $pager or ::fatal "Can't run pager: $! ($pager)";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004797}
4798
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004799sub format_svn_date {
Ben Waltone8717842009-02-24 14:44:49 -05004800 # some systmes don't handle or mishandle %z, so be creative.
Ben Walton736e6192009-02-27 22:11:45 -05004801 my $t = shift || time;
Ben Waltone8717842009-02-24 14:44:49 -05004802 my $gm = timelocal(gmtime($t));
4803 my $sign = qw( + + - )[ $t <=> $gm ];
4804 my $gmoff = sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
4805 return strftime("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004806}
4807
4808sub parse_git_date {
4809 my ($t, $tz) = @_;
4810 # Date::Parse isn't in the standard Perl distro :(
4811 if ($tz =~ s/^\+//) {
4812 $t += tz_to_s_offset($tz);
4813 } elsif ($tz =~ s/^\-//) {
4814 $t -= tz_to_s_offset($tz);
4815 }
4816 return $t;
4817}
4818
4819sub set_local_timezone {
4820 if (defined $TZ) {
4821 $ENV{TZ} = $TZ;
4822 } else {
4823 delete $ENV{TZ};
4824 }
4825}
4826
Eric Wong21819a32007-01-27 14:38:10 -08004827sub tz_to_s_offset {
4828 my ($tz) = @_;
4829 $tz =~ s/(\d\d)$//;
4830 return ($1 * 60) + ($tz * 3600);
4831}
4832
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004833sub get_author_info {
4834 my ($dest, $author, $t, $tz) = @_;
4835 $author =~ s/(?:^\s*|\s*$)//g;
4836 $dest->{a_raw} = $author;
4837 my $au;
Eric Wong1c8443b2007-01-14 02:17:00 -08004838 if ($::_authors) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004839 $au = $rusers{$author} || undef;
4840 }
4841 if (!$au) {
4842 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
4843 }
4844 $dest->{t} = $t;
4845 $dest->{tz} = $tz;
4846 $dest->{a} = $au;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004847 $dest->{t_utc} = parse_git_date($t, $tz);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004848}
4849
4850sub process_commit {
4851 my ($c, $r_min, $r_max, $defer) = @_;
4852 if (defined $r_min && defined $r_max) {
4853 if ($r_min == $c->{r} && $r_min == $r_max) {
4854 show_commit($c);
4855 return 0;
4856 }
4857 return 1 if $r_min == $r_max;
4858 if ($r_min < $r_max) {
4859 # we need to reverse the print order
4860 return 0 if (defined $limit && --$limit < 0);
4861 push @$defer, $c;
4862 return 1;
4863 }
4864 if ($r_min != $r_max) {
4865 return 1 if ($r_min < $c->{r});
4866 return 1 if ($r_max > $c->{r});
4867 }
4868 }
4869 return 0 if (defined $limit && --$limit < 0);
4870 show_commit($c);
4871 return 1;
4872}
4873
4874sub show_commit {
4875 my $c = shift;
4876 if ($oneline) {
4877 my $x = "\n";
4878 if (my $l = $c->{l}) {
4879 while ($l->[0] =~ /^\s*$/) { shift @$l }
4880 $x = $l->[0];
4881 }
4882 $l_fmt ||= 'A' . length($c->{r});
4883 print 'r',pack($l_fmt, $c->{r}),' | ';
4884 print "$c->{c} | " if $show_commit;
4885 print $x;
4886 } else {
4887 show_commit_normal($c);
4888 }
4889}
4890
4891sub show_commit_changed_paths {
4892 my ($c) = @_;
4893 return unless $c->{changed};
4894 print "Changed paths:\n", @{$c->{changed}};
4895}
4896
4897sub show_commit_normal {
4898 my ($c) = @_;
David D Kilzer111947e2007-11-11 22:56:52 -08004899 print commit_log_separator, "r$c->{r} | ";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004900 print "$c->{c} | " if $show_commit;
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004901 print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004902 my $nr_line = 0;
4903
4904 if (my $l = $c->{l}) {
4905 while ($l->[$#$l] eq "\n" && $#$l > 0
4906 && $l->[($#$l - 1)] eq "\n") {
4907 pop @$l;
4908 }
4909 $nr_line = scalar @$l;
4910 if (!$nr_line) {
4911 print "1 line\n\n\n";
4912 } else {
4913 if ($nr_line == 1) {
4914 $nr_line = '1 line';
4915 } else {
4916 $nr_line .= ' lines';
4917 }
4918 print $nr_line, "\n";
4919 show_commit_changed_paths($c);
4920 print "\n";
4921 print $_ foreach @$l;
4922 }
4923 } else {
4924 print "1 line\n";
4925 show_commit_changed_paths($c);
4926 print "\n";
4927
4928 }
Eric Wong488a63e2007-02-15 00:40:42 -08004929 foreach my $x (qw/raw stat diff/) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004930 if ($c->{$x}) {
4931 print "\n";
4932 print $_ foreach @{$c->{$x}}
4933 }
4934 }
4935}
4936
4937sub cmd_show_log {
4938 my (@args) = @_;
4939 my ($r_min, $r_max);
4940 my $r_last = -1; # prevent dupes
David D. Kilzerb2b3ada2007-11-20 22:43:17 -08004941 set_local_timezone();
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004942 if (defined $::_revision) {
4943 if ($::_revision =~ /^(\d+):(\d+)$/) {
4944 ($r_min, $r_max) = ($1, $2);
4945 } elsif ($::_revision =~ /^\d+$/) {
4946 $r_min = $r_max = $::_revision;
4947 } else {
4948 ::fatal "-r$::_revision is not supported, use ",
Benoit Sigoure207f1a72007-10-16 16:36:52 +02004949 "standard 'git log' arguments instead";
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004950 }
4951 }
4952
4953 config_pager();
Eric Wong6ed77262007-08-15 09:55:18 -07004954 @args = git_svn_log_cmd($r_min, $r_max, @args);
David D Kilzer111947e2007-11-11 22:56:52 -08004955 if (!@args) {
4956 print commit_log_separator unless $incremental || $oneline;
4957 return;
4958 }
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004959 my $log = command_output_pipe(@args);
4960 run_pager();
Eric Wong488a63e2007-02-15 00:40:42 -08004961 my (@k, $c, $d, $stat);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004962 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
4963 while (<$log>) {
David D Kilzer60f3ff12007-11-10 22:10:34 -08004964 if (/^${esc_color}commit -?($::sha1_short)/o) {
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004965 my $cmt = $1;
4966 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
4967 $r_last = $c->{r};
4968 process_commit($c, $r_min, $r_max, \@k) or
4969 goto out;
4970 }
4971 $d = undef;
4972 $c = { c => $cmt };
4973 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
4974 get_author_info($c, $1, $2, $3);
4975 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
4976 # ignore
4977 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
4978 push @{$c->{raw}}, $_;
4979 } elsif (/^${esc_color}[ACRMDT]\t/) {
4980 # we could add $SVN->{svn_path} here, but that requires
4981 # remote access at the moment (repo_path_split)...
4982 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
4983 push @{$c->{changed}}, $_;
4984 } elsif (/^${esc_color}diff /o) {
4985 $d = 1;
4986 push @{$c->{diff}}, $_;
4987 } elsif ($d) {
4988 push @{$c->{diff}}, $_;
Eric Wong488a63e2007-02-15 00:40:42 -08004989 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
4990 $esc_color*[\+\-]*$esc_color$/x) {
4991 $stat = 1;
4992 push @{$c->{stat}}, $_;
4993 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
4994 push @{$c->{stat}}, $_;
4995 $stat = undef;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08004996 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
4997 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
4998 } elsif (s/^${esc_color} //o) {
4999 push @{$c->{l}}, $_;
5000 }
5001 }
5002 if ($c && defined $c->{r} && $c->{r} != $r_last) {
5003 $r_last = $c->{r};
5004 process_commit($c, $r_min, $r_max, \@k);
5005 }
5006 if (@k) {
David D Kilzer111947e2007-11-11 22:56:52 -08005007 ($r_min, $r_max) = ($r_max, $r_min);
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005008 process_commit($_, $r_min, $r_max) foreach reverse @k;
5009 }
5010out:
Eric Wongc843c462007-01-12 03:07:31 -08005011 close $log;
David D Kilzer111947e2007-11-11 22:56:52 -08005012 print commit_log_separator unless $incremental || $oneline;
Eric Wongf8c9d1d2007-01-12 02:35:20 -08005013}
5014
Tim Stoakes6fb53752008-02-10 15:21:08 +10305015sub cmd_blame {
Steven Grimm4be40382008-05-10 22:11:18 -07005016 my $path = pop;
Tim Stoakes6fb53752008-02-10 15:21:08 +10305017
5018 config_pager();
5019 run_pager();
5020
Steven Grimm4be40382008-05-10 22:11:18 -07005021 my ($fh, $ctx, $rev);
5022
5023 if ($_git_format) {
5024 ($fh, $ctx) = command_output_pipe('blame', @_, $path);
5025 while (my $line = <$fh>) {
5026 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
5027 # Uncommitted edits show up as a rev ID of
5028 # all zeros, which we can't look up with
5029 # cmt_metadata
5030 if ($1 !~ /^0+$/) {
5031 (undef, $rev, undef) =
5032 ::cmt_metadata($1);
5033 $rev = '0' if (!$rev);
5034 } else {
5035 $rev = '0';
5036 }
5037 $rev = sprintf('%-10s', $rev);
5038 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
5039 }
5040 print $line;
Tim Stoakes6fb53752008-02-10 15:21:08 +10305041 }
Steven Grimm4be40382008-05-10 22:11:18 -07005042 } else {
5043 ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD',
5044 '--', $path);
5045 my ($sha1);
5046 my %authors;
Boris Byk6ea42032009-04-11 00:32:41 +04005047 my @buffer;
5048 my %dsha; #distinct sha keys
5049
Steven Grimm4be40382008-05-10 22:11:18 -07005050 while (my $line = <$fh>) {
Boris Byk6ea42032009-04-11 00:32:41 +04005051 push @buffer, $line;
Steven Grimm4be40382008-05-10 22:11:18 -07005052 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
Boris Byk6ea42032009-04-11 00:32:41 +04005053 $dsha{$1} = 1;
5054 }
5055 }
5056
5057 my $s2r = ::cmt_sha2rev_batch([keys %dsha]);
5058
5059 foreach my $line (@buffer) {
5060 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
5061 $rev = $s2r->{$1};
5062 $rev = '0' if (!$rev)
Steven Grimm4be40382008-05-10 22:11:18 -07005063 }
5064 elsif ($line =~ /^author (.*)/) {
5065 $authors{$rev} = $1;
5066 $authors{$rev} =~ s/\s/_/g;
5067 }
5068 elsif ($line =~ /^\t(.*)$/) {
5069 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
5070 }
5071 }
Tim Stoakes6fb53752008-02-10 15:21:08 +10305072 }
5073 command_close_pipe($fh, $ctx);
5074}
5075
Eric Wong706587f2007-01-18 17:50:01 -08005076package Git::SVN::Migration;
5077# these version numbers do NOT correspond to actual version numbers
5078# of git nor git-svn. They are just relative.
5079#
5080# v0 layout: .git/$id/info/url, refs/heads/$id-HEAD
5081#
5082# v1 layout: .git/$id/info/url, refs/remotes/$id
5083#
5084# v2 layout: .git/svn/$id/info/url, refs/remotes/$id
5085#
5086# v3 layout: .git/svn/$id, refs/remotes/$id
5087# - info/url may remain for backwards compatibility
5088# - this is what we migrate up to this layout automatically,
5089# - this will be used by git svn init on single branches
Eric Wong26a62d52007-02-12 13:25:25 -08005090# v3.1 layout (auto migrated):
5091# - .rev_db => .rev_db.$UUID, .rev_db will remain as a symlink
5092# for backwards compatibility
Eric Wong706587f2007-01-18 17:50:01 -08005093#
5094# v4 layout: .git/svn/$repo_id/$id, refs/remotes/$repo_id/$id
5095# - this is only created for newly multi-init-ed
5096# repositories. Similar in spirit to the
5097# --use-separate-remotes option in git-clone (now default)
5098# - we do not automatically migrate to this (following
5099# the example set by core git)
Eric Wong060610c2007-12-08 23:27:41 -08005100#
5101# v5 layout: .rev_db.$UUID => .rev_map.$UUID
5102# - newer, more-efficient format that uses 24-bytes per record
5103# with no filler space.
5104# - use xxd -c24 < .rev_map.$UUID to view and debug
5105# - This is a one-way migration, repositories updated to the
5106# new format will not be able to use old git-svn without
5107# rebuilding the .rev_db. Rebuilding the rev_db is not
5108# possible if noMetadata or useSvmProps are set; but should
5109# be no problem for users that use the (sensible) defaults.
Eric Wong706587f2007-01-18 17:50:01 -08005110use strict;
5111use warnings;
5112use Carp qw/croak/;
5113use File::Path qw/mkpath/;
Eric Wong47e39c52007-01-21 04:27:09 -08005114use File::Basename qw/dirname basename/;
5115use vars qw/$_minimize/;
Eric Wong706587f2007-01-18 17:50:01 -08005116
5117sub migrate_from_v0 {
5118 my $git_dir = $ENV{GIT_DIR};
5119 return undef unless -d $git_dir;
5120 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5121 my $migrated = 0;
5122 while (<$fh>) {
5123 chomp;
5124 my ($id, $orig_ref) = ($_, $_);
5125 next unless $id =~ s#^refs/heads/(.+)-HEAD$#$1#;
5126 next unless -f "$git_dir/$id/info/url";
5127 my $new_ref = "refs/remotes/$id";
5128 if (::verify_ref("$new_ref^0")) {
5129 print STDERR "W: $orig_ref is probably an old ",
5130 "branch used by an ancient version of ",
5131 "git-svn.\n",
5132 "However, $new_ref also exists.\n",
5133 "We will not be able ",
5134 "to use this branch until this ",
5135 "ambiguity is resolved.\n";
5136 next;
5137 }
5138 print STDERR "Migrating from v0 layout...\n" if !$migrated;
5139 print STDERR "Renaming ref: $orig_ref => $new_ref\n";
5140 command_noisy('update-ref', $new_ref, $orig_ref);
5141 command_noisy('update-ref', '-d', $orig_ref, $orig_ref);
5142 $migrated++;
5143 }
5144 command_close_pipe($fh, $ctx);
5145 print STDERR "Done migrating from v0 layout...\n" if $migrated;
5146 $migrated;
5147}
5148
5149sub migrate_from_v1 {
5150 my $git_dir = $ENV{GIT_DIR};
5151 my $migrated = 0;
5152 return $migrated unless -d $git_dir;
5153 my $svn_dir = "$git_dir/svn";
5154
5155 # just in case somebody used 'svn' as their $id at some point...
5156 return $migrated if -d $svn_dir && ! -f "$svn_dir/info/url";
5157
5158 print STDERR "Migrating from a git-svn v1 layout...\n";
5159 mkpath([$svn_dir]);
5160 print STDERR "Data from a previous version of git-svn exists, but\n\t",
5161 "$svn_dir\n\t(required for this version ",
Frederik Schwarzer8f510be2008-07-14 18:30:24 +02005162 "($::VERSION) of git-svn) does not exist.\n";
Eric Wong706587f2007-01-18 17:50:01 -08005163 my ($fh, $ctx) = command_output_pipe(qw/rev-parse --symbolic --all/);
5164 while (<$fh>) {
5165 my $x = $_;
5166 next unless $x =~ s#^refs/remotes/##;
5167 chomp $x;
5168 next unless -f "$git_dir/$x/info/url";
5169 my $u = eval { ::file_to_s("$git_dir/$x/info/url") };
5170 next unless $u;
5171 my $dn = dirname("$git_dir/svn/$x");
5172 mkpath([$dn]) unless -d $dn;
5173 if ($x eq 'svn') { # they used 'svn' as GIT_SVN_ID:
5174 mkpath(["$git_dir/svn/svn"]);
5175 print STDERR " - $git_dir/$x/info => ",
5176 "$git_dir/svn/$x/info\n";
5177 rename "$git_dir/$x/info", "$git_dir/svn/$x/info" or
5178 croak "$!: $x";
5179 # don't worry too much about these, they probably
5180 # don't exist with repos this old (save for index,
5181 # and we can easily regenerate that)
5182 foreach my $f (qw/unhandled.log index .rev_db/) {
5183 rename "$git_dir/$x/$f", "$git_dir/svn/$x/$f";
5184 }
5185 } else {
5186 print STDERR " - $git_dir/$x => $git_dir/svn/$x\n";
5187 rename "$git_dir/$x", "$git_dir/svn/$x" or
5188 croak "$!: $x";
5189 }
5190 $migrated++;
5191 }
5192 command_close_pipe($fh, $ctx);
5193 print STDERR "Done migrating from a git-svn v1 layout\n";
5194 $migrated;
5195}
5196
5197sub read_old_urls {
5198 my ($l_map, $pfx, $path) = @_;
5199 my @dir;
5200 foreach (<$path/*>) {
5201 if (-r "$_/info/url") {
5202 $pfx .= '/' if $pfx && $pfx !~ m!/$!;
5203 my $ref_id = $pfx . basename $_;
5204 my $url = ::file_to_s("$_/info/url");
5205 $l_map->{$ref_id} = $url;
5206 } elsif (-d $_) {
5207 push @dir, $_;
5208 }
5209 }
5210 foreach (@dir) {
5211 my $x = $_;
5212 $x =~ s!^\Q$ENV{GIT_DIR}\E/svn/!!o;
5213 read_old_urls($l_map, $x, $_);
5214 }
5215}
5216
5217sub migrate_from_v2 {
5218 my @cfg = command(qw/config -l/);
5219 return if grep /^svn-remote\..+\.url=/, @cfg;
5220 my %l_map;
5221 read_old_urls(\%l_map, '', "$ENV{GIT_DIR}/svn");
5222 my $migrated = 0;
5223
5224 foreach my $ref_id (sort keys %l_map) {
Eric Wong471bc002007-02-01 04:06:27 -08005225 eval { Git::SVN->init($l_map{$ref_id}, '', undef, $ref_id) };
5226 if ($@) {
5227 Git::SVN->init($l_map{$ref_id}, '', $ref_id, $ref_id);
5228 }
Eric Wong706587f2007-01-18 17:50:01 -08005229 $migrated++;
5230 }
5231 $migrated;
5232}
5233
Eric Wong47e39c52007-01-21 04:27:09 -08005234sub minimize_connections {
5235 my $r = Git::SVN::read_all_remotes();
5236 my $new_urls = {};
5237 my $root_repos = {};
5238 foreach my $repo_id (keys %$r) {
5239 my $url = $r->{$repo_id}->{url} or next;
5240 my $fetch = $r->{$repo_id}->{fetch} or next;
5241 my $ra = Git::SVN::Ra->new($url);
5242
5243 # skip existing cases where we already connect to the root
5244 if (($ra->{url} eq $ra->{repos_root}) ||
Eric Wong7829f202008-06-28 20:40:32 -07005245 ($ra->{repos_root} eq $repo_id)) {
Eric Wong47e39c52007-01-21 04:27:09 -08005246 $root_repos->{$ra->{url}} = $repo_id;
5247 next;
5248 }
5249
5250 my $root_ra = Git::SVN::Ra->new($ra->{repos_root});
5251 my $root_path = $ra->{url};
Eric Wong4e9f6cc2007-02-09 12:17:57 -08005252 $root_path =~ s#^\Q$ra->{repos_root}\E(/|$)##;
Eric Wong47e39c52007-01-21 04:27:09 -08005253 foreach my $path (keys %$fetch) {
5254 my $ref_id = $fetch->{$path};
5255 my $gs = Git::SVN->new($ref_id, $repo_id, $path);
5256
5257 # make sure we can read when connecting to
5258 # a higher level of a repository
5259 my ($last_rev, undef) = $gs->last_rev_commit;
5260 if (!defined $last_rev) {
5261 $last_rev = eval {
5262 $root_ra->get_latest_revnum;
5263 };
5264 next if $@;
5265 }
5266 my $new = $root_path;
5267 $new .= length $path ? "/$path" : '';
5268 eval {
5269 $root_ra->get_log([$new], $last_rev, $last_rev,
5270 0, 0, 1, sub { });
5271 };
5272 next if $@;
5273 $new_urls->{$ra->{repos_root}}->{$new} =
5274 { ref_id => $ref_id,
5275 old_repo_id => $repo_id,
5276 old_path => $path };
5277 }
5278 }
5279
5280 my @emptied;
5281 foreach my $url (keys %$new_urls) {
5282 # see if we can re-use an existing [svn-remote "repo_id"]
5283 # instead of creating a(n ugly) new section:
Eric Wong7829f202008-06-28 20:40:32 -07005284 my $repo_id = $root_repos->{$url} || $url;
Eric Wong47e39c52007-01-21 04:27:09 -08005285
5286 my $fetch = $new_urls->{$url};
5287 foreach my $path (keys %$fetch) {
5288 my $x = $fetch->{$path};
5289 Git::SVN->init($url, $path, $repo_id, $x->{ref_id});
5290 my $pfx = "svn-remote.$x->{old_repo_id}";
5291
5292 my $old_fetch = quotemeta("$x->{old_path}:".
5293 "refs/remotes/$x->{ref_id}");
Eric Wong8b8fc062007-01-22 11:44:57 -08005294 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08005295 "$pfx.fetch", '^'. $old_fetch . '$');
5296 delete $r->{$x->{old_repo_id}}->
5297 {fetch}->{$x->{old_path}};
5298 if (!keys %{$r->{$x->{old_repo_id}}->{fetch}}) {
Eric Wong8b8fc062007-01-22 11:44:57 -08005299 command_noisy(qw/config --unset/,
Eric Wong47e39c52007-01-21 04:27:09 -08005300 "$pfx.url");
5301 push @emptied, $x->{old_repo_id}
5302 }
5303 }
5304 }
5305 if (@emptied) {
Johannes Schindelin8befc502008-12-14 23:10:52 +01005306 my $file = $ENV{GIT_CONFIG} || "$ENV{GIT_DIR}/config";
Eric Wong47e39c52007-01-21 04:27:09 -08005307 print STDERR <<EOF;
5308The following [svn-remote] sections in your config file ($file) are empty
5309and can be safely removed:
5310EOF
5311 print STDERR "[svn-remote \"$_\"]\n" foreach @emptied;
5312 }
5313}
5314
Eric Wong706587f2007-01-18 17:50:01 -08005315sub migration_check {
5316 migrate_from_v0();
5317 migrate_from_v1();
5318 migrate_from_v2();
Eric Wong47e39c52007-01-21 04:27:09 -08005319 minimize_connections() if $_minimize;
Eric Wong706587f2007-01-18 17:50:01 -08005320}
5321
Eric Wongef3cfaa2007-01-24 03:30:57 -08005322package Git::IndexInfo;
5323use strict;
5324use warnings;
5325use Git qw/command_input_pipe command_close_pipe/;
5326
5327sub new {
5328 my ($class) = @_;
5329 my ($gui, $ctx) = command_input_pipe(qw/update-index -z --index-info/);
5330 bless { gui => $gui, ctx => $ctx, nr => 0}, $class;
5331}
5332
5333sub remove {
5334 my ($self, $path) = @_;
5335 if (print { $self->{gui} } '0 ', 0 x 40, "\t", $path, "\0") {
5336 return ++$self->{nr};
5337 }
5338 undef;
5339}
5340
5341sub update {
5342 my ($self, $mode, $hash, $path) = @_;
5343 if (print { $self->{gui} } $mode, ' ', $hash, "\t", $path, "\0") {
5344 return ++$self->{nr};
5345 }
5346 undef;
5347}
5348
5349sub DESTROY {
5350 my ($self) = @_;
5351 command_close_pipe($self->{gui}, $self->{ctx});
5352}
5353
Eric Wong4bb9ed02007-02-03 13:29:17 -08005354package Git::SVN::GlobSpec;
5355use strict;
5356use warnings;
5357
5358sub new {
5359 my ($class, $glob) = @_;
Eric Wong4bb9ed02007-02-03 13:29:17 -08005360 my $re = $glob;
5361 $re =~ s!/+$!!g; # no need for trailing slashes
Marcus Griep570d35c2008-08-08 01:41:57 -07005362 $re =~ m!^([^*]*)(\*(?:/\*)*)([^*]*)$!;
5363 my $temp = $re;
5364 my ($left, $right) = ($1, $3);
5365 $re = $2;
5366 my $depth = $re =~ tr/*/*/;
5367 if ($depth != $temp =~ tr/*/*/) {
5368 die "Only one set of wildcard directories " .
5369 "(e.g. '*' or '*/*/*') is supported: '$glob'\n";
5370 }
5371 if ($depth == 0) {
Eric Wonge5181922007-02-08 12:53:57 -08005372 die "One '*' is needed for glob: '$glob'\n";
Eric Wong4bb9ed02007-02-03 13:29:17 -08005373 }
Marcus Griep570d35c2008-08-08 01:41:57 -07005374 $re =~ s!\*!\[^/\]*!g;
5375 $re = quotemeta($left) . "($re)" . quotemeta($right);
Eric Wong4e9f6cc2007-02-09 12:17:57 -08005376 if (length $left && !($left =~ s!/+$!!g)) {
5377 die "Missing trailing '/' on left side of: '$glob' ($left)\n";
5378 }
5379 if (length $right && !($right =~ s!^/+!!g)) {
5380 die "Missing leading '/' on right side of: '$glob' ($right)\n";
5381 }
Eric Wong74a81222007-02-10 13:28:50 -08005382 my $left_re = qr/^\/\Q$left\E(\/|$)/;
5383 bless { left => $left, right => $right, left_regex => $left_re,
Marcus Griep570d35c2008-08-08 01:41:57 -07005384 regex => qr/$re/, glob => $glob, depth => $depth }, $class;
Eric Wong4bb9ed02007-02-03 13:29:17 -08005385}
5386
5387sub full_path {
5388 my ($self, $path) = @_;
5389 return (length $self->{left} ? "$self->{left}/" : '') .
5390 $path . (length $self->{right} ? "/$self->{right}" : '');
5391}
5392
Eric Wong3397f9d2006-02-16 01:24:16 -08005393__END__
5394
5395Data structures:
5396
Eric Wong4bb9ed02007-02-03 13:29:17 -08005397
5398$remotes = { # returned by read_all_remotes()
5399 'svn' => {
5400 # svn-remote.svn.url=https://svn.musicpd.org
5401 url => 'https://svn.musicpd.org',
5402 # svn-remote.svn.fetch=mpd/trunk:trunk
5403 fetch => {
5404 'mpd/trunk' => 'trunk',
5405 },
5406 # svn-remote.svn.tags=mpd/tags/*:tags/*
5407 tags => {
5408 path => {
5409 left => 'mpd/tags',
5410 right => '',
5411 regex => qr!mpd/tags/([^/]+)$!,
5412 glob => 'tags/*',
5413 },
5414 ref => {
5415 left => 'tags',
5416 right => '',
5417 regex => qr!tags/([^/]+)$!,
5418 glob => 'tags/*',
5419 },
5420 }
5421 }
5422};
5423
Eric Wong44320b92007-01-13 22:35:53 -08005424$log_entry hashref as returned by libsvn_log_entry()
Eric Wong3397f9d2006-02-16 01:24:16 -08005425{
Eric Wong44320b92007-01-13 22:35:53 -08005426 log => 'whitespace-formatted log entry
Eric Wong3397f9d2006-02-16 01:24:16 -08005427', # trailing newline is preserved
5428 revision => '8', # integer
5429 date => '2004-02-24T17:01:44.108345Z', # commit date
5430 author => 'committer name'
5431};
5432
Eric Wong6e8548c2007-01-27 01:32:00 -08005433
5434# this is generated by generate_diff();
Eric Wong3397f9d2006-02-16 01:24:16 -08005435@mods = array of diff-index line hashes, each element represents one line
5436 of diff-index output
5437
5438diff-index line ($m hash)
5439{
5440 mode_a => first column of diff-index output, no leading ':',
5441 mode_b => second column of diff-index output,
5442 sha1_b => sha1sum of the final blob,
Eric Wongac8e0b92006-03-03 01:20:07 -08005443 chg => change type [MCRADT],
Eric Wong3397f9d2006-02-16 01:24:16 -08005444 file_a => original file name of a file (iff chg is 'C' or 'R')
5445 file_b => new/current file name of a file (any chg)
5446}
5447;
Eric Wonga5e0ced2006-06-12 15:23:48 -07005448
Eric Wonga00439a2006-06-27 19:39:13 -07005449# retval of read_url_paths{,_all}();
5450$l_map = {
5451 # repository root url
5452 'https://svn.musicpd.org' => {
5453 # repository path # GIT_SVN_ID
5454 'mpd/trunk' => 'trunk',
5455 'mpd/tags/0.11.5' => 'tags/0.11.5',
5456 },
5457}
5458
Eric Wonga5e0ced2006-06-12 15:23:48 -07005459Notes:
5460 I don't trust the each() function on unless I created %hash myself
5461 because the internal iterator may not have started at base.