0

I'm trying to retrieve a single revision of a file from Git. I did some research and the best way I found was git show HEAD[x]:[filename] where [x] is the revisions from last revision to go backwards and [filename] is the name of the file. My code looks like this (in Java):

public static String runProcess2(String executable, String parameter) {
    try {
        Runtime rt = Runtime.getRuntime();
        String path = String.format("%s %s", executable, parameter);
        Process pr = rt.exec(path);

        BufferedReader input = new BufferedReader(new InputStreamReader(
                pr.getInputStream()));

        String line = null;

        StringBuilder sb = new StringBuilder();

        while ((line = input.readLine()) != null) {
            sb.append(line + NL);
        }

        pr.waitFor();
        return sb.toString();

    } catch (Exception e) {
        return null;
    }
}

Which I run with this line of code:

return Utilities.runProcess("git", String.format(
                "--git-dir=%s\\.git --work-tree=%s show HEAD%s:%s", path, path,
                rev, filePath));

This works well for text files. However there are binary files in the repository. If I run this code, somehow it will break the file. (again, this works fine for textual files). How can I retrieve the file content from repository for both binary and text files (with any content encoding)?

I think the method I use for reading the output is not general enough.

Alireza Noori
  • 14,961
  • 30
  • 95
  • 179

0 Answers0