Skip to contentSkip to conversation

(Resend)[PATCH] git-svn: Translate invalid characters in refname

PATCH Robert Ewald 4 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

In git some characters are invalid as documented
in git-check-ref-format. In subversion these characters might be valid, so a translation is required.

This patch does this translation by url escaping characters, that are not allowed.

Credit goes to Eric Wong, martin f. krafft and Jan Hudec

---

Second posting of translating characters. I hope I got it right this time. Thanks to Alex for taking the time to point me to Documentation/SubmittingPatches.

 git-svn.perl |   38 +++++++++++++++++++++++++++++++++++---
 1 files changed, 35 insertions(+), 3 deletions(-)
git-svn.perl +35 -3
index 6c692a7..bc55d05 100755
@@ -938,8 +938,8 @@ sub resolve_local_globs {
        foreach (command(qw#for-each-ref --format=%(refname) refs/remotes#)) {
                next unless m#^refs/remotes/$ref->{regex}$#;
                my $p = $1;
-               my $pathname = $path->full_path($p);
-               my $refname = $ref->full_path($p);
+               my $pathname = desanitize_refname($path->full_path($p));
+               my $refname = desanitize_refname($ref->full_path($p));
                if (my $existing = $fetch->{$pathname}) {
                        if ($existing ne $refname) {
                                die "Refspec conflict:\n",
@@ -1239,7 +1239,39 @@ sub new {
        $self;
 }
 
-sub refname { "refs/remotes/$_[0]->{ref_id}" }
+sub refname {
+        my ($refname) = "refs/remotes/$_[0]->{ref_id}" ;
+
+        # It cannot end with a slash /, we'll throw up on this because
+        # SVN can't have directories with a slash in their name, either:
+        if ($refname =~ m{/$}) {
+                die "ref: '$refname' ends with a trailing slash, this is ",
+                    "not permitted by git nor Subversion\n";
+        }
+
+        # It cannot have ASCII control character space, tilde ~, caret ^,
+        # colon :, question-mark ?, asterisk *, space, or open bracket [
+        # anywhere.
+        #
+        # Additionally, % must be escaped because it is used for escaping
+        # and we want our escaped refname to be reversible
+        $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;
+
+        # no slash-separated component can begin with a dot .
+        # /.* becomes /%2E*
+        $refname =~ s{/\.}{/%2E}g;
+        # It cannot have two consecutive dots .. anywhere
+        # .. becomes %2E%2E
+        $refname =~ s{\.\.}{%2E%2E}g;
+
+        $refname;
+}
+
+sub desanitize_refname {
+    my ($refname) = @_;
+    $refname =~ s{%(?:([0-9A-F]{2}))}{chr hex($1)}eg;
+    $refname;
+}
 
 sub svm_uuid {
        my ($self) = @_;
Signature · 1 line
1.5.3.rc3-dirty
#1 lore raw
Quoted 7 lines · Robert Ewald
git-svn.perl +7 -0
+sub refname {
+        my ($refname) = "refs/remotes/$_[0]->{ref_id}" ;
+
+        # It cannot end with a slash /, we'll throw up on this because
+        # SVN can't have directories with a slash in their name, either:
+        if ($refname =~ m{/$}) {
+                die "ref: '$refname' ends with a trailing slash, this is ",

git-svn.perl sub new { · line 1258

Quoted 10 lines
git-svn.perl +10 -0
+                    "not permitted by git nor Subversion\n";
+        }
+
+        # It cannot have ASCII control character space, tilde ~, caret ^,
+        # colon :, question-mark ?, asterisk *, space, or open bracket [
+        # anywhere.
+        #
+        # Additionally, % must be escaped because it is used for escaping
+        # and we want our escaped refname to be reversible
+        $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;

uc of sprintf()? You meant "%%%02X"?

Other than that, looks sane to me. I presume that SVN branches whose name would be mangled with this patch would not have been successfully imported with older git-svn anyway, so this won't introduce any regressions?

Eric?

Acked-by
#2 lore raw
Quoted 13 lines · Junio C Hamano
Quoted 11 lines · Robert Ewald
git-svn.perl +11 -0
+sub refname {
+        my ($refname) = "refs/remotes/$_[0]->{ref_id}" ;
+
+        # It cannot end with a slash /, we'll throw up on this because
+        # SVN can't have directories with a slash in their name, either:
+        if ($refname =~ m{/$}) {
+                die "ref: '$refname' ends with a trailing slash, this is ",
+                    "not permitted by git nor Subversion\n";
+        }
+
+        # It cannot have ASCII control character space, tilde ~, caret ^,
Quoted 8 lines

git-svn.perl sub new { · line 1258

Quoted 6 lines
git-svn.perl +6 -0
+        # colon :, question-mark ?, asterisk *, space, or open bracket [
+        # anywhere.
+        #
+        # Additionally, % must be escaped because it is used for escaping
+        # and we want our escaped refname to be reversible
+        $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;

uc of sprintf()? You meant "%%%02X"?

Ah, I wasn't sure if X was portable enough, but then again this sprintf is in Perl.

Quoted 4 lines

Other than that, looks sane to me. I presume that SVN branches whose name would be mangled with this patch would not have been successfully imported with older git-svn anyway, so this won't introduce any regressions?

Seems alright to me, I haven't had a chance to look at it too hard. I'm out of town and haven't been following the mailing list lately.

Signature · 1 line
Eric Wong
#3 lore raw
In reply to Junio C Hamano
Quoted 13 lines · Junio C Hamano
Quoted 11 lines · Robert Ewald
git-svn.perl +11 -0
+sub refname {
+        my ($refname) = "refs/remotes/$_[0]->{ref_id}" ;
+
+        # It cannot end with a slash /, we'll throw up on this because
+        # SVN can't have directories with a slash in their name, either:
+        if ($refname =~ m{/$}) {
+                die "ref: '$refname' ends with a trailing slash, this is ",
+                    "not permitted by git nor Subversion\n";
+        }
+
+        # It cannot have ASCII control character space, tilde ~, caret ^,
Quoted 8 lines

git-svn.perl sub new { · line 1258

Quoted 6 lines
git-svn.perl +6 -0
+        # colon :, question-mark ?, asterisk *, space, or open bracket [
+        # anywhere.
+        #
+        # Additionally, % must be escaped because it is used for escaping
+        # and we want our escaped refname to be reversible
+        $refname =~ s{([ \%~\^:\?\*\[\t])}{uc sprintf('%%%02x',ord($1))}eg;

uc of sprintf()? You meant "%%%02X"?

You are right. Being a total Perl noob I just took Eric's suggestion from an earlier post without really understanding everything.

Quoted 4 lines

Other than that, looks sane to me. I presume that SVN branches whose name would be mangled with this patch would not have been successfully imported with older git-svn anyway, so this won't introduce any regressions?

Now that you mention it, I am not entirely sure. We introduce the % which is mangled as well. It wasn't mangled before.

I now could think of a case that someone has a git-svn branch with a % in the name checked out.

When updates are fetched a new branch head would be created. It wouldn't destroy anything, but a script might break. On the other hand, the script needs to be updated anyway. So I am not sure if % should be handled separately.

Quoted 1 line

Eric?

Yes, Eric, please comment.

Signature · 1 line
Robert Ewald
#5 lore raw
In reply to Robert Ewald

also sprach Robert Ewald <[email protected]> [2007.07.30.1108 +0200]:

Quoted 6 lines

In git some characters are invalid as documented
in git-check-ref-format. In subversion these characters might be valid, so a translation is required.

This patch does this translation by url escaping characters, that are not allowed.

Have you worked more on this? I am back now and could test it on various svn imports. Just want to make sure I have the latest version.

Signature · 10 lines
martin;              (greetings from the heart of the sun.)
  \____ echo mailto: !#^."<*>"|tr "<*> mailto:" net@madduck
 
"we should have a volleyballocracy.
 we elect a six-pack of presidents.
 each one serves until they screw up,
 at which point they rotate."
                                                      -- dennis miller
 
spamtraps: [email protected]