Example usage for org.eclipse.jgit.api Git close

List of usage examples for org.eclipse.jgit.api Git close

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git close.

Prototype

@Override
public void close() 

Source Link

Document

Free resources associated with this instance.

Usage

From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java

License:Apache License

private void addTestCommits() throws IOException, GitAPIException {
    final Git git = Git.open(gitCloneDirectory);

    try (FileWriter writer = new FileWriter(file1, true)) {
        writer.write("hello world");
    }/*www. j a  va2  s  .  c  o  m*/
    git.add().addFilepattern(".").call();
    git.commit().setMessage("updated").call();

    try (FileWriter writer = new FileWriter(file1, true)) {
        writer.append("\nhello world");
    }
    git.add().addFilepattern(".").call();
    git.commit().setMessage("updated").call();

    try (FileWriter writer = new FileWriter(file2, true)) {
        writer.write("hi world");
    }
    git.add().addFilepattern(".").call();
    git.commit().setMessage("updated").call();
    git.push().setRemote("origin").add("master").call();

    git.close();
}

From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java

License:Apache License

private void createNextCommit() throws IOException, GitAPIException {
    final Git git = Git.open(gitCloneDirectory);
    git.pull().setRemote("origin").setRemoteBranchName("master").call();

    try (FileWriter writer = new FileWriter(file1, true)) {
        writer.write("hi world!\nhello hi");
    }//from w  ww. j  av  a 2s  . co  m
    git.add().addFilepattern(".").call();
    git.commit().setMessage("updated file1").call();

    try (FileWriter writer = new FileWriter(file2, true)) {
        writer.write("world");
    }
    git.add().addFilepattern(".").call();
    git.commit().setMessage("updated file2").call();

    git.push().setRemote("origin").add("master").call();

    git.close();
}

From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java

License:Apache License

private void renameFile() throws IOException, GitAPIException {
    final Git git = Git.open(gitCloneDirectory);
    git.pull().setRemote("origin").setRemoteBranchName("master").call();

    if (!file2.renameTo(file2.toPath().resolveSibling("file3.adoc").toFile()))
        throw new IOException("Could not rename file2 to file3");

    git.add().setUpdate(true).addFilepattern(".").call();
    git.add().addFilepattern(".").call();
    git.commit().setMessage("renamed file2 to file3").call();
    git.push().setRemote("origin").add("master").call();
    git.close();
}

From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java

License:Apache License

private void deleteFile() throws IOException, GitAPIException {
    final Git git = Git.open(gitCloneDirectory);
    git.pull().setRemote("origin").setRemoteBranchName("master").call();

    if (!file2.delete())
        throw new IOException("Could not delete file2");

    git.add().setUpdate(true).addFilepattern(".").call();
    git.commit().setMessage("deleted file2").call();
    git.push().setRemote("origin").add("master").call();
    git.close();
}

From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java

License:Apache License

private void addNotRelevantFile(final String fileName) throws GitAPIException, IOException {
    final Git git = Git.open(gitCloneDirectory);
    git.pull().setRemote("origin").setRemoteBranchName("master").call();

    try (FileWriter writer = new FileWriter(file1.toPath().resolveSibling(fileName + ".mustache").toFile(),
            true)) {//ww w  .j a v  a  2 s .  c om
        writer.write("hello {{world}}");
    }

    git.add().addFilepattern(".").call();
    git.commit().setMessage("added template").call();
    git.push().setRemote("origin").add("master").call();
    git.close();
}

From source file:com.uber.stream.kafka.mirrormaker.controller.core.GitBackUpHandler.java

License:Apache License

public void writeToFile(String fileName, String data) throws Exception {
    Repository backupRepo = null;//from   w w  w.  j av a 2s  . co m
    BufferedWriter output = null;
    Git git = null;
    Git result = null;
    try {
        try {
            FileUtils.deleteDirectory(new File(localPath));
        } catch (IOException e) {
            LOGGER.error("Error deleting exisiting backup directory");
            throw e;
        }

        try {
            result = Git.cloneRepository().setURI(remotePath).setDirectory(new File(localPath)).call();
        } catch (Exception e) {
            LOGGER.error("Error cloning backup git repo");
            throw e;
        }

        try {
            backupRepo = new FileRepository(localPath + "/.git");
        } catch (IOException e) {
            throw e;
        }

        git = new Git(backupRepo);
        File myfile = new File(localPath + "/" + fileName);

        try {
            output = new BufferedWriter(new FileWriter(myfile));
            output.write(data);
            output.flush();
        } catch (IOException e) {
            LOGGER.error("Error writing backup to the file with name " + fileName);
            throw e;
        }

        try {
            git.add().addFilepattern(".").call();
        } catch (GitAPIException e) {
            LOGGER.error("Error adding files to git");
            throw e;
        }

        try {
            git.commit().setMessage("Taking backup on " + new Date()).call();

        } catch (GitAPIException e) {
            LOGGER.error("Error commiting files to git");
            throw e;
        }

        try {
            git.push().call();
        } catch (GitAPIException e) {
            LOGGER.error("Error pushing files to git");
            throw e;
        }
    } catch (Exception e) {
        throw e;
    } finally {
        output.close();
        git.close();
        if (result != null)
            result.getRepository().close();
        backupRepo.close();
    }
}

From source file:de._692b8c32.cdlauncher.tasks.GITCheckoutTask.java

License:Open Source License

@Override
public void doWork() {
    setProgress(-1);/*from ww w . j  a v  a  2  s.  c  o  m*/

    try {
        Git git = Git.open(cacheDir);
        git.reset().setMode(ResetCommand.ResetType.HARD).call();
        git.checkout().setAllPaths(true).setForce(true).setName(branch).setStartPoint(startPoint.getValue())
                .call();

        if (destinationDir != null) {
            FileUtils.delete(destinationDir, FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
            destinationDir.mkdirs();
            Files.list(cacheDir.toPath()).filter(path -> !(".git".equals(path.getFileName().toString())))
                    .forEach(path -> {
                        try {
                            Files.move(path, destinationDir.toPath().resolve(path.getFileName()),
                                    StandardCopyOption.ATOMIC_MOVE);
                        } catch (IOException ex) {
                            throw new RuntimeException("Failed to move " + path.getFileName(), ex);
                        }
                    });
        }
        git.close();
    } catch (RepositoryNotFoundException | InvalidRemoteException ex) {
        throw new RuntimeException("Could not find repository");
    } catch (GitAPIException | IOException ex) {
        throw new RuntimeException("Could not checkout data", ex);
    }
}

From source file:de._692b8c32.cdlauncher.tasks.GITUpdateTask.java

License:Open Source License

@Override
public void doWork() {
    try {// w w w .ja va  2 s.  c  om
        Git git;
        if (!cacheDir.exists()) {
            cacheDir.mkdirs();
            git = Git.cloneRepository().setURI(repoUri).setDirectory(cacheDir).setNoCheckout(true)
                    .setProgressMonitor(this).call();
        } else {
            git = Git.open(cacheDir);
            git.fetch().setProgressMonitor(this).call();
        }
        git.close();
    } catch (RepositoryNotFoundException | InvalidRemoteException ex) {
        Logger.getLogger(GITUpdateTask.class.getName()).log(Level.SEVERE, "Could not find repository", ex);
        try {
            FileUtils.delete(cacheDir);
            run();
        } catch (IOException | StackOverflowError ex1) {
            throw new RuntimeException("Fix of broken repository failed", ex1);
        }
    } catch (GitAPIException | IOException ex) {
        throw new RuntimeException("Could not download data", ex);
    }
}

From source file:eu.atos.paas.git.Clone.java

License:Open Source License

public Repository call() throws GitAPIException, IOException {

    CloneCommand cmd = Git.cloneRepository();
    logger.debug("Start git clone {} into {}", sourceUrl, destProjectDir.getPath());
    Git gitRepo = cmd.setURI(sourceUrl.toString()).setDirectory(destProjectDir).call();
    gitRepo.close();
    logger.debug("End git clone {}", sourceUrl);

    return new Repository(destProjectDir);
}

From source file:eu.atos.paas.git.Repository.java

License:Open Source License

public static Repository clone(String from, File dest) throws GitAPIException, IOException {

    CloneCommand cmd = Git.cloneRepository();
    logger.debug("Start git clone {} into {}", from, dest.getPath());
    Git gitRepo = cmd.setURI(from).setDirectory(dest).call();
    gitRepo.close();
    logger.debug("End git clone {}", from);

    return new Repository(dest);
}