Skip to content

Commit c6767f4

Browse files
committed
Merge branch 'pw/unquote-path-in-git-pm' into maint
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
2 parents 133578a + 3f9c637 commit c6767f4

File tree

3 files changed

+61
-43
lines changed

3 files changed

+61
-43
lines changed

git-add--interactive.perl

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use 5.008;
44
use strict;
55
use warnings;
6-
use Git;
6+
use Git qw(unquote_path);
77
use Git::I18N;
88

99
binmode(STDOUT, ":raw");
@@ -175,47 +175,6 @@ sub run_cmd_pipe {
175175
}
176176
chomp($GIT_DIR);
177177

178-
my %cquote_map = (
179-
"b" => chr(8),
180-
"t" => chr(9),
181-
"n" => chr(10),
182-
"v" => chr(11),
183-
"f" => chr(12),
184-
"r" => chr(13),
185-
"\\" => "\\",
186-
"\042" => "\042",
187-
);
188-
189-
sub unquote_path {
190-
local ($_) = @_;
191-
my ($retval, $remainder);
192-
if (!/^\042(.*)\042$/) {
193-
return $_;
194-
}
195-
($_, $retval) = ($1, "");
196-
while (/^([^\\]*)\\(.*)$/) {
197-
$remainder = $2;
198-
$retval .= $1;
199-
for ($remainder) {
200-
if (/^([0-3][0-7][0-7])(.*)$/) {
201-
$retval .= chr(oct($1));
202-
$_ = $2;
203-
last;
204-
}
205-
if (/^([\\\042btnvfr])(.*)$/) {
206-
$retval .= $cquote_map{$1};
207-
$_ = $2;
208-
last;
209-
}
210-
# This is malformed -- just return it as-is for now.
211-
return $_[0];
212-
}
213-
$_ = $remainder;
214-
}
215-
$retval .= $_;
216-
return $retval;
217-
}
218-
219178
sub refresh {
220179
my $fh;
221180
open $fh, 'git update-index --refresh |'

perl/Git.pm

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ require Exporter;
6161
remote_refs prompt
6262
get_tz_offset get_record
6363
credential credential_read credential_write
64-
temp_acquire temp_is_locked temp_release temp_reset temp_path);
64+
temp_acquire temp_is_locked temp_release temp_reset temp_path
65+
unquote_path);
6566

6667

6768
=head1 DESCRIPTION
@@ -1451,6 +1452,57 @@ sub prefix_lines {
14511452
return $string;
14521453
}
14531454

1455+
=item unquote_path ( PATH )
1456+
1457+
Unquote a quoted path containing c-escapes as returned by ls-files etc.
1458+
when not using -z or when parsing the output of diff -u.
1459+
1460+
=cut
1461+
1462+
{
1463+
my %cquote_map = (
1464+
"a" => chr(7),
1465+
"b" => chr(8),
1466+
"t" => chr(9),
1467+
"n" => chr(10),
1468+
"v" => chr(11),
1469+
"f" => chr(12),
1470+
"r" => chr(13),
1471+
"\\" => "\\",
1472+
"\042" => "\042",
1473+
);
1474+
1475+
sub unquote_path {
1476+
local ($_) = @_;
1477+
my ($retval, $remainder);
1478+
if (!/^\042(.*)\042$/) {
1479+
return $_;
1480+
}
1481+
($_, $retval) = ($1, "");
1482+
while (/^([^\\]*)\\(.*)$/) {
1483+
$remainder = $2;
1484+
$retval .= $1;
1485+
for ($remainder) {
1486+
if (/^([0-3][0-7][0-7])(.*)$/) {
1487+
$retval .= chr(oct($1));
1488+
$_ = $2;
1489+
last;
1490+
}
1491+
if (/^([\\\042abtnvfr])(.*)$/) {
1492+
$retval .= $cquote_map{$1};
1493+
$_ = $2;
1494+
last;
1495+
}
1496+
# This is malformed
1497+
throw Error::Simple("invalid quoted path $_[0]");
1498+
}
1499+
$_ = $remainder;
1500+
}
1501+
$retval .= $_;
1502+
return $retval;
1503+
}
1504+
}
1505+
14541506
=item get_comment_line_char ( )
14551507
14561508
Gets the core.commentchar configuration value.

t/t9700/test.pl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ sub adjust_dirsep {
133133
unlink $tmpfile3;
134134
chdir($abs_repo_dir);
135135

136+
# unquoting paths
137+
is(Git::unquote_path('abc'), 'abc', 'unquote unquoted path');
138+
is(Git::unquote_path('"abc def"'), 'abc def', 'unquote simple quoted path');
139+
is(Git::unquote_path('"abc\"\\\\ \a\b\t\n\v\f\r\001\040"'),
140+
"abc\"\\ \x07\x08\x09\x0a\x0b\x0c\x0d\x01 ",
141+
'unquote escape sequences');
142+
136143
printf "1..%d\n", Test::More->builder->current_test;
137144

138145
my $is_passing = eval { Test::More->is_passing };

0 commit comments

Comments
 (0)