Last active
December 18, 2022 16:06
-
-
Save dmlloyd/776e07bc52ef78b4020b154e9bbd1558 to your computer and use it in GitHub Desktop.
Git shortlog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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