RemoteFileUtil { static FilePath createRelativePath(@NotNull FilePath localFile, @NotNull P4RemoteFile localDepot, P4RemoteFile remote) { List<String> localDepotPaths = splitDepotPaths(localDepot.getDisplayName()); List<String> remoteDepotPaths = splitDepotPaths(remote.getDisplayName()); int diffPos = 0; while (diffPos < localDepotPaths.size() && diffPos < remoteDepotPaths.size() && localDepotPaths.get(diffPos).equals(remoteDepotPaths.get(diffPos))) { diffPos++; } if (diffPos <= 0) { if (LOG.isDebugEnabled()) { LOG.debug("No similarities between " + localDepot + " and " + remote.getDepotPath()); } return null; } List<String> localFilePaths = splitFilePaths(localFile); int localRelativeSize = localFilePaths.size() - ( localDepotPaths.size() - diffPos ); while (localFilePaths.size() > localRelativeSize) { localFilePaths.remove(localFilePaths.size() - 1); } for (int i = diffPos; i < remoteDepotPaths.size(); i++) { localFilePaths.add(remoteDepotPaths.get(i)); } StringBuilder ret = new StringBuilder(); boolean first = true; for (String part : localFilePaths) { if (first) { first = false; } else { ret.append(File.separator); } ret.append(part); } if (LOG.isDebugEnabled()) { LOG.debug("Transformed local " + localDepot + " + remote " + remote + " -> " + ret); } return VcsUtil.getFilePath(ret.toString()); }  @NotNull static FilePath findRelativeRemotePath(@NotNull P4LocalFile local, @Nullable P4RemoteFile remote);  }
@ExtendWith(TemporaryFolderExtension.class) @Test void testSplitFilePath_simple(TemporaryFolder tmpDir) { FilePath src = VcsUtil.getFilePath(tmpDir.newFile("a/test.txt")); FilePath dest = VcsUtil.getFilePath(tmpDir.newFile("b/test.txt")); P4RemoteFileImpl local = new P4RemoteFileImpl(" P4RemoteFileImpl remote = new P4RemoteFileImpl(" assertEquals( dest, RemoteFileUtil.createRelativePath(src, local, remote) ); }