From: Eric Wong Date: Tue, 22 Dec 2015 21:56:36 +0000 (+0000) Subject: repobrowse: add git helper for unquoting X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3e79993321bd0e5a12b104730638eb87947ec86b;p=thirdparty%2Fpublic-inbox.git repobrowse: add git helper for unquoting We need to parse diffs with all manner of funky file names :< --- diff --git a/lib/PublicInbox/RepoBrowseGit.pm b/lib/PublicInbox/RepoBrowseGit.pm new file mode 100644 index 000000000..73a236d37 --- /dev/null +++ b/lib/PublicInbox/RepoBrowseGit.pm @@ -0,0 +1,29 @@ +# Copyright (C) 2015 all contributors +# 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 index 000000000..eeda90a35 --- /dev/null +++ b/t/repobrowse_git.t @@ -0,0 +1,11 @@ +# Copyright (C) 2015 all contributors +# 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();