]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
repobrowse: add git helper for unquoting
authorEric Wong <e@80x24.org>
Tue, 22 Dec 2015 21:56:36 +0000 (21:56 +0000)
committerEric Wong <e@80x24.org>
Tue, 5 Apr 2016 18:58:27 +0000 (18:58 +0000)
We need to parse diffs with all manner of funky file names :<

lib/PublicInbox/RepoBrowseGit.pm [new file with mode: 0644]
t/repobrowse_git.t [new file with mode: 0644]

diff --git a/lib/PublicInbox/RepoBrowseGit.pm b/lib/PublicInbox/RepoBrowseGit.pm
new file mode 100644 (file)
index 0000000..73a236d
--- /dev/null
@@ -0,0 +1,29 @@
+# Copyright (C) 2015 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ (https://www.gnu.org/licenses/agpl-3.0.txt)
+
+package PublicInbox::RepoBrowseGit;
+use strict;
+use warnings;
+use base qw(Exporter);
+our @EXPORT_OK = qw(git_unquote);
+
+my %GIT_ESC = (
+       a => "\a",
+       b => "\b",
+       f => "\f",
+       n => "\n",
+       r => "\r",
+       t => "\t",
+       v => "\013",
+);
+
+sub git_unquote {
+       my ($s) = @_;
+       return $s unless ($s =~ /\A"(.*)"\z/);
+       $s = $1;
+       $s =~ s/\\([abfnrtv])/$GIT_ESC{$1}/g;
+       $s =~ s/\\([0-7]{1,3})/chr(oct($1))/ge;
+       $s;
+}
+
+1;
diff --git a/t/repobrowse_git.t b/t/repobrowse_git.t
new file mode 100644 (file)
index 0000000..eeda90a
--- /dev/null
@@ -0,0 +1,11 @@
+# Copyright (C) 2015 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ (https://www.gnu.org/licenses/agpl-3.0.txt)
+use strict;
+use warnings;
+use Test::More;
+use PublicInbox::RepoBrowseGit qw(git_unquote);
+
+is("foo\nbar", git_unquote('"foo\\nbar"'), 'unquoted newline');
+is("ElĂ©anor", git_unquote('"El\\303\\251anor"'), 'unquoted octal');
+
+done_testing();