git-svn: add git svn propget
This allows one to easily retrieve a single SVN property from within git-svn without requiring svn or remembering the URL of a repository * git-svn.perl (%cmd): Add the new command `propget'. ($cmd_dir_prefix): New global. (&get_svnprops): New helper. (&cmd_propget): New. Use &get_svnprops. * t/t9101-git-svn-props.sh: Add a test case for propget. [ew: make sure the rev-parse --show-prefix call doesn't break the `git-svn clone' command] Signed-off-by: Benoit Sigoure <tsuna@lrde.epita.fr> Acked-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Benoit Sigoure committed
Oct 16, 2007 at 16:36 UTC
151534515682695348a8172f399f4bf025154a5f
2 files changed
+80
git-svn.perl
+57
@@ -9,6 +9,11 @@ use vars qw/ $AUTHOR $VERSION
9
$AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
10
$VERSION = '@@GIT_VERSION@@';
11
12
+# From which subdir have we been invoked?
13
+my $cmd_dir_prefix = eval {
14
+ command_oneline([qw/rev-parse --show-prefix/], STDERR => 0)
15
+} || '';
16
+
17
my $git_dir_user_set = 1 if defined $ENV{GIT_DIR};
18
$ENV{GIT_DIR} ||= '.git';
19
$Git::SVN::default_repo_id = 'svn';
@@ -127,6 +132,9 @@ my %cmd = (
132
'Create a .gitignore per svn:ignore',
133
{ 'revision|r=i' => \$_revision
134
} ],
135
+ 'propget' => [ \&cmd_propget,
136
+ 'Print the value of a property on a file or directory',
137
+ { 'revision|r=i' => \$_revision } ],
138
'show-ignore' => [ \&cmd_show_ignore, "Show svn:ignore listings",
139
{ 'revision|r=i' => \$_revision
140
} ],
@@ -526,6 +534,55 @@ sub cmd_create_ignore {
534
});
535
}
536
537
+# get_svnprops(PATH)
538
+# ------------------
539
+# Helper for cmd_propget below.
540
+sub get_svnprops {
541
+ my $path = shift;
542
+ my ($url, $rev, $uuid, $gs) = working_head_info('HEAD');
543
+ $gs ||= Git::SVN->new;
544
+
545
+ # prefix THE PATH by the sub-directory from which the user
546
+ # invoked us.
547
+ $path = $cmd_dir_prefix . $path;
548
+ fatal("No such file or directory: $path\n") unless -e $path;
549
+ my $is_dir = -d $path ? 1 : 0;
550
+ $path = $gs->{path} . '/' . $path;
551
+
552
+ # canonicalize the path (otherwise libsvn will abort or fail to
553
+ # find the file)
554
+ # File::Spec->canonpath doesn't collapse x/../y into y (for a
555
+ # good reason), so let's do this manually.
556
+ $path =~ s#/+#/#g;
557
+ $path =~ s#/\.(?:/|$)#/#g;
558
+ $path =~ s#/[^/]+/\.\.##g;
559
+ $path =~ s#/$##g;
560
+
561
+ my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
562
+ my $props;
563
+ if ($is_dir) {
564
+ (undef, undef, $props) = $gs->ra->get_dir($path, $r);
565
+ }
566
+ else {
567
+ (undef, $props) = $gs->ra->get_file($path, $r, undef);
568
+ }
569
+ return $props;
570
+}
571
+
572
+# cmd_propget (PROP, PATH)
573
+# ------------------------
574
+# Print the SVN property PROP for PATH.
575
+sub cmd_propget {
576
+ my ($prop, $path) = @_;
577
+ $path = '.' if not defined $path;
578
+ usage(1) if not defined $prop;
579
+ my $props = get_svnprops($path);
580
+ if (not defined $props->{$prop}) {
581
+ fatal("`$path' does not have a `$prop' SVN property.\n");
582
+ }
583
+ print $props->{$prop} . "\n";
584
+}
585
+
586
sub cmd_multi_init {
587
my $url = shift;
588
unless (defined $_trunk || defined $_branches || defined $_tags) {
t/t9101-git-svn-props.sh
+23
@@ -170,4 +170,27 @@ test_expect_success 'test create-ignore' "
170
git ls-files -s | grep gitignore | cmp - create-ignore-index.expect
171
"
172
173
+cat >prop.expect <<\EOF
174
+no-such-file*
175
+
176
+EOF
177
+cat >prop2.expect <<\EOF
178
+8
179
+EOF
180
+
181
+# This test can be improved: since all the svn:ignore contain the same
182
+# pattern, it can pass even though the propget did not execute on the
183
+# right directory.
184
+test_expect_success 'test propget' "
185
+ git-svn propget svn:ignore . | cmp - prop.expect &&
186
+ cd deeply &&
187
+ git-svn propget svn:ignore . | cmp - ../prop.expect &&
188
+ git-svn propget svn:entry:committed-rev nested/directory/.keep \
189
+ | cmp - ../prop2.expect &&
190
+ git-svn propget svn:ignore .. | cmp - ../prop.expect &&
191
+ git-svn propget svn:ignore nested/ | cmp - ../prop.expect &&
192
+ git-svn propget svn:ignore ./nested | cmp - ../prop.expect &&
193
+ git-svn propget svn:ignore .././deeply/nested | cmp - ../prop.expect
194
+ "
195
+
196
test_done