package command; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; import java.io.*; import java.util.Objects; import java.util.concurrent.Executors; import java.util.function.Consumer; public class CommandInit implements Command { private String path = "/home/jack/Documents/testrepo"; public void createWorkTree() throws IOException { ProcessBuilder builder = new ProcessBuilder(); builder.command("sh", "git worktree add ../issue-worktree issues"); builder.directory(new File(path)); Process process = builder.start(); StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), System.out::println); Executors.newSingleThreadExecutor().submit(streamGobbler); int exitCode = 0; try { exitCode = process.waitFor(); } catch (InterruptedException e) { e.printStackTrace(); } assert exitCode == 0; } public void execute() { //problem: files not added to git obv cant be stashed, lost when change branch File dir = new File(path); try (Git git = Git.open(dir)) { //Newly initialized repositories have a peculiarity in that no branch has yet been created. // RevCommit stash = git.stashCreate().call(); String branch = git.getRepository().getBranch(); git.checkout() .setCreateBranch(true) .setOrphan(true) .setName("issues") .call(); for (File file : Objects.requireNonNull(dir.listFiles())) { if (!file.getName().equals(".git")) { file.delete(); } } git.add().setUpdate(true).addFilepattern(".").call(); git.commit().setMessage("initialise issue branch").call(); git.checkout().setName(branch).call(); // createWorkTree(); // git.stashApply().setStashRef(stash.getName()).call(); } catch (IOException e) { System.out.println("Not a git repository - aborting operation"); } catch (GitAPIException e) { e.printStackTrace(); } } private static class StreamGobbler implements Runnable { private InputStream inputStream; private Consumer<String> consumer; public StreamGobbler(InputStream inputStream, Consumer<String> consumer) { this.inputStream = inputStream; this.consumer = consumer; } @Override public void run() { new BufferedReader(new InputStreamReader(inputStream)).lines() .forEach(consumer); } } } // Ref headRef = git.getRepository().getRef( Constants.HEAD ); //if( headRef == null || headRef.getObjectId() == null ) { // // no commit yet // } // System.out.println(System.getProperty("user.dir"));