@Test(expected = IOException.class) public void testInvalidRenamedFiles() throws Exception { String[][] tests = new String[][]{ {"", "67dfbe26"}, {"moved/renamed2.c", ""}, {"", ""}, {null, "67dfbe26"}, {"moved/renamed2.c", null} }; File root = new File(repository.getSourceRoot(), "git"); GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root); for (String[] test : tests) { String file = test[0]; String changeset = test[1]; gitrepo.findOriginalName(file, changeset); } }
String findOriginalName(String fullpath, String changeset) throws IOException { if (fullpath == null || fullpath.isEmpty()) { throw new IOException(String.format("Invalid file path string: %s", fullpath)); } if (changeset == null || changeset.isEmpty()) { throw new IOException(String.format("Invalid changeset string for path %s: %s", fullpath, changeset)); } String fileInRepo = getPathRelativeToCanonicalRepositoryRoot(fullpath); String[] argv = { ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK), "log", "--follow", "--summary", ABBREV_LOG, "--abbrev-commit", "--name-status", "--pretty=format:commit %h%n%d", "--", fileInRepo }; Executor executor = new Executor(Arrays.asList(argv), new File(getDirectoryName()), RuntimeEnvironment.getInstance().getInteractiveCommandTimeout()); int status = executor.exec(); String originalFile = null; try (BufferedReader in = new BufferedReader(newLogReader(executor.getOutputStream()))) { String line; String rev = null; Matcher m; Pattern pattern = Pattern.compile("^R\\d+\\s(.*)\\s(.*)"); while ((line = in.readLine()) != null) { if (line.startsWith("commit ")) { rev = line.substring(7); continue; } if (changeset.equals(rev)) { if (originalFile == null) { originalFile = fileInRepo; } break; } if ((m = pattern.matcher(line)).find()) { originalFile = Paths.get(m.group(1)).toString(); } } } if (status != 0 || originalFile == null) { LOGGER.log(Level.WARNING, "Failed to get original name in revision {2} for: \"{0}\" Exit code: {1}", new Object[]{fullpath, String.valueOf(status), changeset}); return null; } return originalFile; }
GitRepository extends Repository { String findOriginalName(String fullpath, String changeset) throws IOException { if (fullpath == null || fullpath.isEmpty()) { throw new IOException(String.format("Invalid file path string: %s", fullpath)); } if (changeset == null || changeset.isEmpty()) { throw new IOException(String.format("Invalid changeset string for path %s: %s", fullpath, changeset)); } String fileInRepo = getPathRelativeToCanonicalRepositoryRoot(fullpath); String[] argv = { ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK), "log", "--follow", "--summary", ABBREV_LOG, "--abbrev-commit", "--name-status", "--pretty=format:commit %h%n%d", "--", fileInRepo }; Executor executor = new Executor(Arrays.asList(argv), new File(getDirectoryName()), RuntimeEnvironment.getInstance().getInteractiveCommandTimeout()); int status = executor.exec(); String originalFile = null; try (BufferedReader in = new BufferedReader(newLogReader(executor.getOutputStream()))) { String line; String rev = null; Matcher m; Pattern pattern = Pattern.compile("^R\\d+\\s(.*)\\s(.*)"); while ((line = in.readLine()) != null) { if (line.startsWith("commit ")) { rev = line.substring(7); continue; } if (changeset.equals(rev)) { if (originalFile == null) { originalFile = fileInRepo; } break; } if ((m = pattern.matcher(line)).find()) { originalFile = Paths.get(m.group(1)).toString(); } } } if (status != 0 || originalFile == null) { LOGGER.log(Level.WARNING, "Failed to get original name in revision {2} for: \"{0}\" Exit code: {1}", new Object[]{fullpath, String.valueOf(status), changeset}); return null; } return originalFile; } }
GitRepository extends Repository { String findOriginalName(String fullpath, String changeset) throws IOException { if (fullpath == null || fullpath.isEmpty()) { throw new IOException(String.format("Invalid file path string: %s", fullpath)); } if (changeset == null || changeset.isEmpty()) { throw new IOException(String.format("Invalid changeset string for path %s: %s", fullpath, changeset)); } String fileInRepo = getPathRelativeToCanonicalRepositoryRoot(fullpath); String[] argv = { ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK), "log", "--follow", "--summary", ABBREV_LOG, "--abbrev-commit", "--name-status", "--pretty=format:commit %h%n%d", "--", fileInRepo }; Executor executor = new Executor(Arrays.asList(argv), new File(getDirectoryName()), RuntimeEnvironment.getInstance().getInteractiveCommandTimeout()); int status = executor.exec(); String originalFile = null; try (BufferedReader in = new BufferedReader(newLogReader(executor.getOutputStream()))) { String line; String rev = null; Matcher m; Pattern pattern = Pattern.compile("^R\\d+\\s(.*)\\s(.*)"); while ((line = in.readLine()) != null) { if (line.startsWith("commit ")) { rev = line.substring(7); continue; } if (changeset.equals(rev)) { if (originalFile == null) { originalFile = fileInRepo; } break; } if ((m = pattern.matcher(line)).find()) { originalFile = Paths.get(m.group(1)).toString(); } } } if (status != 0 || originalFile == null) { LOGGER.log(Level.WARNING, "Failed to get original name in revision {2} for: \"{0}\" Exit code: {1}", new Object[]{fullpath, String.valueOf(status), changeset}); return null; } return originalFile; } GitRepository(); }
GitRepository extends Repository { String findOriginalName(String fullpath, String changeset) throws IOException { if (fullpath == null || fullpath.isEmpty()) { throw new IOException(String.format("Invalid file path string: %s", fullpath)); } if (changeset == null || changeset.isEmpty()) { throw new IOException(String.format("Invalid changeset string for path %s: %s", fullpath, changeset)); } String fileInRepo = getPathRelativeToCanonicalRepositoryRoot(fullpath); String[] argv = { ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK), "log", "--follow", "--summary", ABBREV_LOG, "--abbrev-commit", "--name-status", "--pretty=format:commit %h%n%d", "--", fileInRepo }; Executor executor = new Executor(Arrays.asList(argv), new File(getDirectoryName()), RuntimeEnvironment.getInstance().getInteractiveCommandTimeout()); int status = executor.exec(); String originalFile = null; try (BufferedReader in = new BufferedReader(newLogReader(executor.getOutputStream()))) { String line; String rev = null; Matcher m; Pattern pattern = Pattern.compile("^R\\d+\\s(.*)\\s(.*)"); while ((line = in.readLine()) != null) { if (line.startsWith("commit ")) { rev = line.substring(7); continue; } if (changeset.equals(rev)) { if (originalFile == null) { originalFile = fileInRepo; } break; } if ((m = pattern.matcher(line)).find()) { originalFile = Paths.get(m.group(1)).toString(); } } } if (status != 0 || originalFile == null) { LOGGER.log(Level.WARNING, "Failed to get original name in revision {2} for: \"{0}\" Exit code: {1}", new Object[]{fullpath, String.valueOf(status), changeset}); return null; } return originalFile; } GitRepository(); @Override Annotation annotate(File file, String revision); @Override boolean fileHasAnnotation(File file); @Override boolean fileHasHistory(File file); @Override boolean isWorking(); @Override String determineCurrentVersion(CommandTimeoutType cmdType); }
GitRepository extends Repository { String findOriginalName(String fullpath, String changeset) throws IOException { if (fullpath == null || fullpath.isEmpty()) { throw new IOException(String.format("Invalid file path string: %s", fullpath)); } if (changeset == null || changeset.isEmpty()) { throw new IOException(String.format("Invalid changeset string for path %s: %s", fullpath, changeset)); } String fileInRepo = getPathRelativeToCanonicalRepositoryRoot(fullpath); String[] argv = { ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK), "log", "--follow", "--summary", ABBREV_LOG, "--abbrev-commit", "--name-status", "--pretty=format:commit %h%n%d", "--", fileInRepo }; Executor executor = new Executor(Arrays.asList(argv), new File(getDirectoryName()), RuntimeEnvironment.getInstance().getInteractiveCommandTimeout()); int status = executor.exec(); String originalFile = null; try (BufferedReader in = new BufferedReader(newLogReader(executor.getOutputStream()))) { String line; String rev = null; Matcher m; Pattern pattern = Pattern.compile("^R\\d+\\s(.*)\\s(.*)"); while ((line = in.readLine()) != null) { if (line.startsWith("commit ")) { rev = line.substring(7); continue; } if (changeset.equals(rev)) { if (originalFile == null) { originalFile = fileInRepo; } break; } if ((m = pattern.matcher(line)).find()) { originalFile = Paths.get(m.group(1)).toString(); } } } if (status != 0 || originalFile == null) { LOGGER.log(Level.WARNING, "Failed to get original name in revision {2} for: \"{0}\" Exit code: {1}", new Object[]{fullpath, String.valueOf(status), changeset}); return null; } return originalFile; } GitRepository(); @Override Annotation annotate(File file, String revision); @Override boolean fileHasAnnotation(File file); @Override boolean fileHasHistory(File file); @Override boolean isWorking(); @Override String determineCurrentVersion(CommandTimeoutType cmdType); static final String CMD_PROPERTY_KEY; static final String CMD_FALLBACK; }