From c8a4111320aaed484deecbbc7d1f63f38f3dc57b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 18 Jan 2019 10:21:40 +0000 Subject: git: add git_quote It'll be helpful for displaying progress in SolverGit output. --- lib/PublicInbox/Git.pm | 12 +++++++++++- t/git.t | 7 ++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/PublicInbox/Git.pm b/lib/PublicInbox/Git.pm index a270180c..d0ac6b6c 100644 --- a/lib/PublicInbox/Git.pm +++ b/lib/PublicInbox/Git.pm @@ -13,7 +13,7 @@ use POSIX qw(dup2); require IO::Handle; use PublicInbox::Spawn qw(spawn popen_rd); use base qw(Exporter); -our @EXPORT_OK = qw(git_unquote); +our @EXPORT_OK = qw(git_unquote git_quote); my %GIT_ESC = ( a => "\a", @@ -26,6 +26,8 @@ my %GIT_ESC = ( '"' => '"', '\\' => '\\', ); +my %ESC_GIT = map { $GIT_ESC{$_} => $_ } keys %GIT_ESC; + # unquote pathnames used by git, see quote.c::unquote_c_style.c in git.git sub git_unquote ($) { @@ -36,6 +38,14 @@ sub git_unquote ($) { $_[0]; } +sub git_quote ($) { + if ($_[0] =~ s/([\\"\a\b\f\n\r\t\013]|[^[:print:]])/ + '\\'.($ESC_GIT{$1}||sprintf("%0o",ord($1)))/egs) { + return qq{"$_[0]"}; + } + $_[0]; +} + sub new { my ($class, $git_dir) = @_; my @st; diff --git a/t/git.t b/t/git.t index 2d58a106..9c80fbb4 100644 --- a/t/git.t +++ b/t/git.t @@ -144,11 +144,16 @@ if ('alternates reloaded') { is($$found, $config, 'alternates reloaded'); } -use_ok 'PublicInbox::Git', qw(git_unquote); +use_ok 'PublicInbox::Git', qw(git_unquote git_quote); my $s; is("foo\nbar", git_unquote($s = '"foo\\nbar"'), 'unquoted newline'); is("Eléanor", git_unquote($s = '"El\\303\\251anor"'), 'unquoted octal'); is(git_unquote($s = '"I\"m"'), 'I"m', 'unquoted dq'); is(git_unquote($s = '"I\\m"'), 'I\\m', 'unquoted backslash'); +is(git_quote($s = "Eléanor"), '"El\\303\\251anor"', 'quoted octal'); +is(git_quote($s = "hello\"world"), '"hello\"world"', 'quoted dq'); +is(git_quote($s = "hello\\world"), '"hello\\\\world"', 'quoted backslash'); +is(git_quote($s = "hello\nworld"), '"hello\\nworld"', 'quoted LF'); + done_testing(); -- cgit v1.2.3-24-ge0c7