git-p4: fix git-p4.pathEncoding for removed files

In a9e38359e3 we taught git-p4 a way to re-encode path names from what was used in Perforce to UTF-8. This path re-encoding worked properly for "added" paths. "Removed" paths were not re-encoded and therefore different from the "added" paths. Consequently, these files were not removed in a git-p4 cloned Git repository because the path names did not match. Fix this by moving the re-encoding to a place that affects "added" and "removed" paths. Add a test to demonstrate the issue. Signed-off-by: Lars Schneider <larsxschneider@gmail.com> Reviewed-by: Luke Diamand <luke@diamand.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lars Schneider committed Feb 9, 2017 at 16:06 UTC a8b05162e894b88aeb7d5064daba07e1a4f58463
2 files changed +30 -10
git-p4.py
+14 -10
@@ -2388,11 +2388,24 @@ class P4Sync(Command, P4UserMap):
2388 self.gitStream.write(d)
2389 self.gitStream.write('\n')
2390
2391 + def encodeWithUTF8(self, path):
2392 + try:
2393 + path.decode('ascii')
2394 + except:
2395 + encoding = 'utf8'
2396 + if gitConfig('git-p4.pathEncoding'):
2397 + encoding = gitConfig('git-p4.pathEncoding')
2398 + path = path.decode(encoding, 'replace').encode('utf8', 'replace')
2399 + if self.verbose:
2400 + print 'Path with non-ASCII characters detected. Used %s to encode: %s ' % (encoding, path)
2401 + return path
2402 +
2403 # output one file from the P4 stream
2404 # - helper for streamP4Files
2405
2406 def streamOneP4File(self, file, contents):
2407 relPath = self.stripRepoPath(file['depotFile'], self.branchPrefixes)
2408 + relPath = self.encodeWithUTF8(relPath)
2409 if verbose:
2410 size = int(self.stream_file['fileSize'])
2411 sys.stdout.write('\r%s --> %s (%i MB)\n' % (file['depotFile'], relPath, size/1024/1024))
@@ -2465,16 +2478,6 @@ class P4Sync(Command, P4UserMap):
2478 text = regexp.sub(r'$\1$', text)
2479 contents = [ text ]
2480
2468 - try:
2469 - relPath.decode('ascii')
2470 - except:
2471 - encoding = 'utf8'
2472 - if gitConfig('git-p4.pathEncoding'):
2473 - encoding = gitConfig('git-p4.pathEncoding')
2474 - relPath = relPath.decode(encoding, 'replace').encode('utf8', 'replace')
2475 - if self.verbose:
2476 - print 'Path with non-ASCII characters detected. Used %s to encode: %s ' % (encoding, relPath)
2477 -
2481 if self.largeFileSystem:
2482 (git_mode, contents) = self.largeFileSystem.processContent(git_mode, relPath, contents)
2483
@@ -2482,6 +2485,7 @@ class P4Sync(Command, P4UserMap):
2485
2486 def streamOneP4Deletion(self, file):
2487 relPath = self.stripRepoPath(file['path'], self.branchPrefixes)
2488 + relPath = self.encodeWithUTF8(relPath)
2489 if verbose:
2490 sys.stdout.write("delete %s\n" % relPath)
2491 sys.stdout.flush()
t/t9822-git-p4-path-encoding.sh
+16
@@ -51,6 +51,22 @@ test_expect_success 'Clone repo containing iso8859-1 encoded paths with git-p4.p
51 )
52 '
53
54 +test_expect_success 'Delete iso8859-1 encoded paths and clone' '
55 + (
56 + cd "$cli" &&
57 + ISO8859="$(printf "$ISO8859_ESCAPED")" &&
58 + p4 delete "$ISO8859" &&
59 + p4 submit -d "remove file"
60 + ) &&
61 + git p4 clone --destination="$git" //depot@all &&
62 + test_when_finished cleanup_git &&
63 + (
64 + cd "$git" &&
65 + git -c core.quotepath=false ls-files >actual &&
66 + test_must_be_empty actual
67 + )
68 +'
69 +
70 test_expect_success 'kill p4d' '
71 kill_p4d
72 '