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",
'"' => '"',
'\\' => '\\',
);
+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 ($) {
$_[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;
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();