Skip to contentSkip to conversation

git-svn: Support for git-svn propset

PATCH In review Alfred Perlstein 3 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 git-svn: Support for git-svn propset

This change allows git-svn to support setting subversion properties.

Very useful for manually setting properties when committing to a subversion repo that *requires* properties to be set without requiring moving your changeset to separate subversion checkout in order to set props.

This change is initially from David Fraser
Appearing here:
http://marc.info/?l=git&m=125259772625008&w=2

They are now forward ported to most recent git along with fixes to deal with files in subdirectories.

Style and functional changes from Eric Wong have been taken in their entirety from:
http://mid.gmane.org/[email protected]

There is a nit that I want to point out. The code does not support adding props unless there are also content changes to the files as well. You can see this in the testcase.

---
 git-svn.perl               | 49 ++++++++++++++++++++++-
 perl/Git/SVN/Editor.pm     | 42 ++++++++++++++++++++
 t/t9148-git-svn-propset.sh | 97 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+), 1 deletion(-)
 create mode 100755 t/t9148-git-svn-propset.sh
git-svn.perl +48 -1
index b6e2186..60f8814 100755
@@ -115,7 +115,7 @@ my ($_stdin, $_help, $_edit,
 	$_before, $_after,
 	$_merge, $_strategy, $_preserve_merges, $_dry_run, $_parents, $_local,
 	$_prefix, $_no_checkout, $_url, $_verbose,
-	$_commit_url, $_tag, $_merge_info, $_interactive);
+	$_commit_url, $_tag, $_merge_info, $_interactive, $_set_svn_props);
 
 # This is a refactoring artifact so Git::SVN can get at this git-svn switch.
 sub opt_prefix { return $_prefix || '' }
@@ -193,6 +193,7 @@ my %cmd = (
 			  'dry-run|n' => \$_dry_run,
 			  'fetch-all|all' => \$_fetch_all,
 			  'commit-url=s' => \$_commit_url,
+			  'set-svn-props=s' => \$_set_svn_props,
 			  'revision|r=i' => \$_revision,
 			  'no-rebase' => \$_no_rebase,
 			  'mergeinfo=s' => \$_merge_info,
@@ -228,6 +229,9 @@ my %cmd = (
         'propget' => [ \&cmd_propget,
 		       'Print the value of a property on a file or directory',
 		       { 'revision|r=i' => \$_revision } ],
+        'propset' => [ \&cmd_propset,
+		       'Set the value of a property on a file or directory - will be set on commit',
+		       {} ],
         'proplist' => [ \&cmd_proplist,
 		       'List all properties of a file or directory',
 		       { 'revision|r=i' => \$_revision } ],
@@ -1376,6 +1380,49 @@ sub cmd_propget {
 	print $props->{$prop} . "\n";
 }
 
+# cmd_propset (PROPNAME, PROPVAL, PATH)
+# ------------------------
+# Adjust the SVN property PROPNAME to PROPVAL for PATH.
+sub cmd_propset {
+	my ($propname, $propval, $path) = @_;
+	$path = '.' if not defined $path;
+	$path = $cmd_dir_prefix . $path;
+	usage(1) if not defined $propname;
+	usage(1) if not defined $propval;
+	my $file = basename($path);
+	my $dn = dirname($path);
+	my $cur_props = Git::SVN::Editor::check_attr( "svn-properties", $path );
+	my @new_props;
+	if (!$cur_props || $cur_props eq "unset" || $cur_props eq "" || $cur_props eq "set") {
+		push @new_props, "$propname=$propval";
+	} else {
+		# TODO: handle combining properties better
+		my @props = split(/;/, $cur_props);
+		my $replaced_prop;
+		foreach my $prop (@props) {
+			# Parse 'name=value' syntax and set the property.
+			if ($prop =~ /([^=]+)=(.*)/) {
+				my ($n,$v) = ($1,$2);
+				if ($n eq $propname) {
+					$v = $propval;
+					$replaced_prop = 1;
+				}
+				push @new_props, "$n=$v";
+			}
+		}
+		if (!$replaced_prop) {
+			push @new_props, "$propname=$propval";
+		}
+	}
+	my $attrfile = "$dn/.gitattributes";
+	open my $attrfh, '>>', $attrfile or die "Can't open $attrfile: $!\n";
+	# TODO: don't simply append here if $file already has svn-properties
+	my $new_props = join(';', @new_props);
+	print $attrfh "$file svn-properties=$new_props\n" or
+		die "write to $attrfile: $!\n";
+	close $attrfh or die "close $attrfile: $!\n";
+}
+
 # cmd_proplist (PATH)
 # -------------------
 # Print the list of SVN properties for PATH.
perl/Git/SVN/Editor.pm +42 -0

72 lines not on this page · raw message

t/t9148-git-svn-propset.sh new file +97 -0

97 lines not on this page · raw message

Signature · 1 line
2.1.2
#1 lore raw
Quoted 2 lines · Alfred Perlstein

Appearing here:
http://marc.info/?l=git&m=125259772625008&w=2

Probably better to use a mid URL here, too

http://mid.gmane.org/[email protected]

such a long URL, though...

Quoted 6 lines
perl/Git/SVN/Editor.pm +0 -0
@@ -288,6 +288,44 @@ sub apply_autoprops {
 	}
 }
 

perl/Git/SVN/Editor.pm sub apply_autoprops { · line 300

Quoted 10 lines
perl/Git/SVN/Editor.pm +10 -0
+sub check_attr {
+	my ($attr,$path) = @_;
+	my $fh = command_output_pipe("check-attr", $attr, "--", $path);
+	return undef if (!$fh);
+
+	my $val = <$fh>;
+	close $fh;
+	if ($val) { $val =~ s/^[^:]*:\s*[^:]*:\s*(.*)\s*$/$1/; }
+	return $val;
+}

I just noticed command_output_pipe didn't use a corresponding command_close_pipe to check for errors, but command_oneline is even better. I'll squash the following:

perl/Git/SVN/Editor.pm +1 -5
@@ -290,11 +290,7 @@ sub apply_autoprops {
 
 sub check_attr {
 	my ($attr,$path) = @_;
-	my $fh = command_output_pipe("check-attr", $attr, "--", $path);
-	return undef if (!$fh);
-
-	my $val = <$fh>;
-	close $fh;
+	my $val = command_oneline("check-attr", $attr, "--", $path);
 	if ($val) { $val =~ s/^[^:]*:\s*[^:]*:\s*(.*)\s*$/$1/; }
 	return $val;
 }

In your test, "local" isn't portable, unfortunately, but tests seem to work fine without local so I've removed them:

t/t9148-git-svn-propset.sh +6 -8
@@ -29,10 +29,9 @@ test_expect_success 'fetch revisions from svn' '
 	git svn fetch
 	'
 
-set_props()
-{
-	local subdir="$1"
-	local file="$2"
+set_props () {
+	subdir="$1"
+	file="$2"
 	shift;shift;
 	(cd "$subdir" &&
 		while [ $# -gt 0 ] ; do
@@ -43,10 +42,9 @@ set_props()
 		git commit -m "testing propset" "$file")
 }
 
-confirm_props()
-{
-	local subdir="$1"
-	local file="$2"
+confirm_props () {
+	subdir="$1"
+	file="$2"
 	shift;shift;
 	(set -e ; cd "svn_project/$subdir" &&
 		while [ $# -gt 0 ] ; do

Unless there's other improvements we missed, I'll push out your v3 with my changes squashed in for Junio to pull in a day or two. Thank you again for working on this!

#2 lore raw
Quoted 85 lines · Eric Wong
Quoted 2 lines · Alfred Perlstein

Appearing here:
http://marc.info/?l=git&m=125259772625008&w=2

Probably better to use a mid URL here, too

http://mid.gmane.org/[email protected]

such a long URL, though...

Quoted 6 lines
perl/Git/SVN/Editor.pm +0 -0
@@ -288,6 +288,44 @@ sub apply_autoprops {
   }
 }
 

perl/Git/SVN/Editor.pm sub apply_autoprops { · line 300

Quoted 10 lines
perl/Git/SVN/Editor.pm +10 -0
+sub check_attr {
+    my ($attr,$path) = @_;
+    my $fh = command_output_pipe("check-attr", $attr, "--", $path);
+    return undef if (!$fh);
+
+    my $val = <$fh>;
+    close $fh;
+    if ($val) { $val =~ s/^[^:]*:\s*[^:]*:\s*(.*)\s*$/$1/; }
+    return $val;
+}

I just noticed command_output_pipe didn't use a corresponding command_close_pipe to check for errors, but command_oneline is even better. I'll squash the following:

perl/Git/SVN/Editor.pm +1 -5
@@ -290,11 +290,7 @@ sub apply_autoprops {
 
 sub check_attr {
   my ($attr,$path) = @_;
-    my $fh = command_output_pipe("check-attr", $attr, "--", $path);
-    return undef if (!$fh);
-
-    my $val = <$fh>;
-    close $fh;
+    my $val = command_oneline("check-attr", $attr, "--", $path);
   if ($val) { $val =~ s/^[^:]*:\s*[^:]*:\s*(.*)\s*$/$1/; }
   return $val;
 }

In your test, "local" isn't portable, unfortunately, but tests seem to work fine without local so I've removed them:

t/t9148-git-svn-propset.sh +6 -8
@@ -29,10 +29,9 @@ test_expect_success 'fetch revisions from svn' '
   git svn fetch
   '
 
-set_props()
-{
-    local subdir="$1"
-    local file="$2"
+set_props () {
+    subdir="$1"
+    file="$2"
   shift;shift;
   (cd "$subdir" &&
       while [ $# -gt 0 ] ; do
@@ -43,10 +42,9 @@ set_props()
       git commit -m "testing propset" "$file")
 }
 
-confirm_props()
-{
-    local subdir="$1"
-    local file="$2"
+confirm_props () {
+    subdir="$1"
+    file="$2"
   shift;shift;
   (set -e ; cd "svn_project/$subdir" &&
       while [ $# -gt 0 ] ; do
Quoted 4 lines

Unless there's other improvements we missed, I'll push out your v3 with my changes squashed in for Junio to pull in a day or two. Thank you again for working on this!

Eric,

All looks good to me.

Thank you all very much for the feedback and help. It's made this a very rewarding endeavor.

-Alfred.