josefk31 commented on code in PR #19165: URL: https://github.com/apache/kafka/pull/19165#discussion_r1987726881
########## generator/src/main/java/org/apache/kafka/message/checker/CheckerUtils.java: ########## @@ -121,18 +121,34 @@ static MessageSpec readMessageSpecFromString(String contents) { } /** - * Read a MessageSpec file from remote git repo. + * Read the file from the specified git reference. * - * @param filePath The file to read from remote git repo. - * @param ref The specific git reference to be used for testing. + * @param filePath The fully qualified file path. The git directory will be derived from this. + * @param gitRef The specific git reference to be used for comparison. * @return The file contents. */ - static String getDataFromGit(String filePath, Path gitPath, String ref) throws IOException { - Git git = Git.open(new File(gitPath + "/.git")); + static String readFileFromGitRef(String filePath, String gitRef) throws IOException { + Path fileAbsolutePath = Paths.get(filePath).toAbsolutePath(); + + // traverse up parent directories until .git directory is found + Path projectRoot = fileAbsolutePath.getParent(); + if (projectRoot == null) { + throw new RuntimeException("The file path provided does not have a parent directory"); + } + while (!Files.exists(projectRoot.resolve(".git"))) { + projectRoot = projectRoot.getParent(); + if (projectRoot == null) { + throw new RuntimeException("Invalid path, need to be within a Git repository"); + } + } + + String pathFromProjectRoot = projectRoot.relativize(fileAbsolutePath).toString(); + + Git git = Git.open(new File(projectRoot + "/.git")); Review Comment: Nit; is `Paths.get` preferred here? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org