Search code examples
javagitprocessbuildergit-log

Java Processbuilder GitLog pretty-format


I create a java method to make a gitlog from a git repo.

Method:

String gitLogFormat = "--pretty=format:\"%H %an %ad %s\"";

List<String> cmds = Arrays.asList("git", "log", gitLogFormat, "--numstat",  "--date=short");
ProcessBuilder builder = new ProcessBuilder(cmds);
builder.directory(new File( GitPath.toAbsolutePath().toString() ));

builder.redirectOutput(ProcessBuilder.Redirect.to( path.resolve("gitlog.txt").toFile() ) );

Process process = builder.start();

If i run this method without gitLogFormat it works and I get a file with the Gitlog. But with the format I always get the exitcode 128.

Is there a Problem with the \" because the other both options --numstat and --date=short works?

Update

I try it with :

String gitLogFormat = "--pretty=format:\"%H_%an_%ad_%s\"";

and it works.

The Whitespaces are the Problems. Have anyone an idea how i can use the code with withespaces because the Log should have spaces between the informations.


Solution

  • Update 2 and Solution:

    The Problem are the Whitespaces so i must add between every whitespace a ,

    It works with this code :

    List<String> cmds = Arrays.asList("git", "log", "--pretty=format:\"%H" , "%an" , "%ad" , "%s\"" , "--numstat" , "--date=short");