Skip to content

Instantly share code, notes, and snippets.

@dmlloyd
Last active December 18, 2022 16:06
Show Gist options
  • Save dmlloyd/776e07bc52ef78b4020b154e9bbd1558 to your computer and use it in GitHub Desktop.
Save dmlloyd/776e07bc52ef78b4020b154e9bbd1558 to your computer and use it in GitHub Desktop.
Git shortlog
import java.io.IOException;
import java.nio.charset.StandardCharsets;
class Scratch {
public static void main(String[] args) throws IOException {
ProcessBuilder pb = new ProcessBuilder("/usr/bin/git", "shortlog", "dhaskjfhdasjkdfhaskl");
pb.redirectOutput(ProcessBuilder.Redirect.PIPE);
pb.redirectError(ProcessBuilder.Redirect.PIPE);
pb.redirectInput(ProcessBuilder.Redirect.DISCARD.file());
Process process = pb.start();
try (CloseableThread t1 = new CloseableThread(() -> {
try {
System.out.print(new String(process.getErrorStream().readAllBytes(), StandardCharsets.UTF_8));
} catch (IOException e) {
throw new RuntimeException(e);
}
})) {
try (CloseableThread t2 = new CloseableThread(() -> {
try {
System.out.print(new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8));
} catch (IOException e) {
throw new RuntimeException(e);
}
})) {
t1.start();
t2.start();
boolean intr = Thread.interrupted();
int res;
for (;;) try {
res = process.waitFor();
break;
} catch (InterruptedException ignored) {
intr = true;
}
if (intr) {
Thread.currentThread().interrupt();
}
}
}
System.out.println("It didn't stall");
}
static final class CloseableThread extends Thread implements AutoCloseable {
CloseableThread(final Runnable target) {
super(target);
}
public void close() {
boolean intr = Thread.interrupted();
for (;;) try {
join();
break;
} catch (InterruptedException ignored) {
intr = true;
}
if (intr) {
Thread.currentThread().interrupt();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment