Skip to content

Instantly share code, notes, and snippets.

@oraclebox
Forked from Crydust/Git.java
Created December 2, 2016 07:04
Show Gist options
  • Save oraclebox/b9ed9fa65c23db8d2f37e18b9cbf5628 to your computer and use it in GitHub Desktop.
Save oraclebox/b9ed9fa65c23db8d2f37e18b9cbf5628 to your computer and use it in GitHub Desktop.
run git commands from java
public class Git {
private static List<Path> allFilesInDirectory(Path directory) throws IOException {
final List<Path> files = new ArrayList<>();
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
if (dir.endsWith(".git")) {
return FileVisitResult.SKIP_SUBTREE;
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (attrs.isRegularFile()) {
files.add(file);
}
return FileVisitResult.CONTINUE;
}
});
return files;
}
public static void gitInit(Path directory) throws IOException, InterruptedException {
runCommand(directory, "git", "init");
}
public static void gitStage(Path directory) throws IOException, InterruptedException {
runCommand(directory, "git", "add", "-A");
}
public static void gitCommit(Path directory, String message) throws IOException, InterruptedException {
runCommand(directory, "git", "commit", "-m", message);
}
public static void gitGc(Path directory) throws IOException, InterruptedException {
runCommand(directory, "git", "gc");
}
public static void runCommand(Path directory, String... command) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder()
.command(command)
.directory(directory.toFile());
Process p = pb.start();
StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT");
outputGobbler.start();
errorGobbler.start();
int exit = p.waitFor();
errorGobbler.join();
outputGobbler.join();
if (exit != 0) {
throw new AssertionError(String.format("runCommand returned %d", exit));
}
}
private static class StreamGobbler extends Thread {
InputStream is;
String type;
private StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
@Override
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(type + "> " + line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment