Merge branch 'pw/unquote-path-in-git-pm'

Code refactoring.

* pw/unquote-path-in-git-pm:
  t9700: add tests for Git::unquote_path()
  Git::unquote_path(): throw an exception on bad path
  Git::unquote_path(): handle '\a'
  add -i: move unquote_path() to Git.pm
This commit is contained in:
Junio C Hamano 2017-07-10 13:42:50 -07:00
commit 9bf8e0c73d
3 changed files with 61 additions and 43 deletions

View File

@ -3,7 +3,7 @@
use 5.008;
use strict;
use warnings;
use Git;
use Git qw(unquote_path);
use Git::I18N;
binmode(STDOUT, ":raw");
@ -174,47 +174,6 @@ if (!defined $GIT_DIR) {
}
chomp($GIT_DIR);
my %cquote_map = (
"b" => chr(8),
"t" => chr(9),
"n" => chr(10),
"v" => chr(11),
"f" => chr(12),
"r" => chr(13),
"\\" => "\\",
"\042" => "\042",
);
sub unquote_path {
local ($_) = @_;
my ($retval, $remainder);
if (!/^\042(.*)\042$/) {
return $_;
}
($_, $retval) = ($1, "");
while (/^([^\\]*)\\(.*)$/) {
$remainder = $2;
$retval .= $1;
for ($remainder) {
if (/^([0-3][0-7][0-7])(.*)$/) {
$retval .= chr(oct($1));
$_ = $2;
last;
}
if (/^([\\\042btnvfr])(.*)$/) {
$retval .= $cquote_map{$1};
$_ = $2;
last;
}
# This is malformed -- just return it as-is for now.
return $_[0];
}
$_ = $remainder;
}
$retval .= $_;
return $retval;
}
sub refresh {
my $fh;
open $fh, 'git update-index --refresh |'

View File

@ -61,7 +61,8 @@ require Exporter;
remote_refs prompt
get_tz_offset get_record
credential credential_read credential_write
temp_acquire temp_is_locked temp_release temp_reset temp_path);
temp_acquire temp_is_locked temp_release temp_reset temp_path
unquote_path);
=head1 DESCRIPTION
@ -1451,6 +1452,57 @@ sub prefix_lines {
return $string;
}
=item unquote_path ( PATH )
Unquote a quoted path containing c-escapes as returned by ls-files etc.
when not using -z or when parsing the output of diff -u.
=cut
{
my %cquote_map = (
"a" => chr(7),
"b" => chr(8),
"t" => chr(9),
"n" => chr(10),
"v" => chr(11),
"f" => chr(12),
"r" => chr(13),
"\\" => "\\",
"\042" => "\042",
);
sub unquote_path {
local ($_) = @_;
my ($retval, $remainder);
if (!/^\042(.*)\042$/) {
return $_;
}
($_, $retval) = ($1, "");
while (/^([^\\]*)\\(.*)$/) {
$remainder = $2;
$retval .= $1;
for ($remainder) {
if (/^([0-3][0-7][0-7])(.*)$/) {
$retval .= chr(oct($1));
$_ = $2;
last;
}
if (/^([\\\042abtnvfr])(.*)$/) {
$retval .= $cquote_map{$1};
$_ = $2;
last;
}
# This is malformed
throw Error::Simple("invalid quoted path $_[0]");
}
$_ = $remainder;
}
$retval .= $_;
return $retval;
}
}
=item get_comment_line_char ( )
Gets the core.commentchar configuration value.

View File

@ -133,6 +133,13 @@ close TEMPFILE3;
unlink $tmpfile3;
chdir($abs_repo_dir);
# unquoting paths
is(Git::unquote_path('abc'), 'abc', 'unquote unquoted path');
is(Git::unquote_path('"abc def"'), 'abc def', 'unquote simple quoted path');
is(Git::unquote_path('"abc\"\\\\ \a\b\t\n\v\f\r\001\040"'),
"abc\"\\ \x07\x08\x09\x0a\x0b\x0c\x0d\x01 ",
'unquote escape sequences');
printf "1..%d\n", Test::More->builder->current_test;
my $is_passing = eval { Test::More->is_passing };