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

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

Introduction

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

Prototype

public Repository getRepository() 

Source Link

Document

Get repository

Usage

From source file:com.tenxdev.ovcs.command.InitCommand.java

License:Open Source License

private void initGitRepo(final Path workingDir, final String remoteUri, final String connectionString)
        throws OvcsException {
    try {/*from w ww.j  a  va 2s. c o  m*/
        System.out.println("Cloning git repo");
        Git.cloneRepository().setDirectory(workingDir.toFile()).setCloneAllBranches(true).setRemote("origin")
                .setURI(remoteUri).setProgressMonitor(new TextProgressMonitor()).call();
        final Git git = Git.open(workingDir.toFile());
        final StoredConfig config = git.getRepository().getConfig();
        config.setString("database", null, "connectionString", connectionString);
        config.save();
        git.checkout().setName("master").setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);
    } catch (GitAPIException | IOException e) {
        throw new OvcsException("Unable to initialize Git repo: " + e.getMessage(), e);
    }
}

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 ww  w.j a va 2 s  . c  o 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.blizzy.documentr.page.PageUtilTest.java

License:Open Source License

@Test
public void toPageVersion() throws GitAPIException, IOException {
    File repoDir = tempDir.getRoot();
    Git git = Git.init().setDirectory(repoDir).call();
    PersonIdent ident = new PersonIdent("user", "user@example.com"); //$NON-NLS-1$ //$NON-NLS-2$
    git.commit().setAuthor(ident).setCommitter(ident).setMessage("test").call(); //$NON-NLS-1$
    RevCommit commit = CommitUtils.getHead(git.getRepository());

    PageVersion pageVersion = PageUtil.toPageVersion(commit);
    assertEquals("user", pageVersion.getLastEditedBy()); //$NON-NLS-1$
    assertSecondsAgo(pageVersion.getLastEdited(), 5);
    assertEquals(commit.getName(), pageVersion.getCommitName());
}

From source file:de.br0tbox.gitfx.ui.controllers.SingleProjectController.java

License:Apache License

private void showHistoryForCommit(final GitFxCommit selectedCommit) {
    Preconditions.checkNotNull(selectedCommit, "selectedCommit");
    final Git git = projectModel.getFxProject().getGit();
    final Repository repository = git.getRepository();
    ObjectId resolve;/*w  w  w . ja  v  a 2s.  c  o m*/
    try {
        resolve = repository.resolve(selectedCommit.getHash());
        final RevWalk revWalk = new RevWalk(repository);
        final RevCommit commit = revWalk.parseCommit(resolve);
        RevCommit parent = null;
        if (commit.getParents().length > 0 && commit.getParent(0) != null) {
            parent = revWalk.parseCommit(commit.getParent(0).getId());
        }

        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final DiffFormatter df = new DiffFormatter(out);
        df.setRepository(repository);
        df.setDiffComparator(RawTextComparator.DEFAULT);
        df.setDetectRenames(true);
        List<DiffEntry> diffs = null;
        if (parent != null) {
            diffs = df.scan(parent.getTree(), commit.getTree());
        } else {
            diffs = df.scan(new EmptyTreeIterator(),
                    new CanonicalTreeParser(null, revWalk.getObjectReader(), commit.getTree()));
        }
        final ObservableList items = commitList.getItems();
        items.clear();
        for (final DiffEntry diff : diffs) {
            df.format(diff);
            final String changes = out.toString("UTF-8");
            changesView.setText(changes);
            if (ChangeType.DELETE.equals(diff.getChangeType())) {
                items.add(diff.getChangeType().toString().subSequence(0, 1) + " " + diff.getOldPath());
            } else {
                items.add(diff.getChangeType().toString().subSequence(0, 1) + " " + diff.getNewPath());
            }
        }
        revWalk.release();
        df.release();
    } catch (final IOException e) {
        LOGGER.error("Error while showing changes for commit " + selectedCommit.getHash(), e);
    }
}

From source file:de.br0tbox.gitfx.ui.progress.GitCloneTask.java

License:Apache License

@Override
protected void onAfterCall(Git returnValue) {
    if (returnValue != null && returnValue.getRepository() != null) {
        returnValue.getRepository().close();
    }/*from ww  w  .  j a v  a2s  . com*/
}

From source file:de.ks.blogging.grav.pages.RepositorySupport.java

License:Apache License

public FileChanges getChangedFiles(String from, String to) {
    try {//  ww w  .j  a v a2 s .co m
        Git git = pages.getGit();
        Repository repository = git.getRepository();
        List<DiffEntry> diffs = git.diff().setNewTree(prepareTreeParser(repository, to))
                .setOldTree(prepareTreeParser(repository, from)).call();
        FileChanges fileChanges = new FileChanges(diffs);
        log.info("Found {} added/modified, {} deleted.", fileChanges.getModifiedAdded().size(),
                fileChanges.getDeleted().size());
        return fileChanges;
    } catch (Exception e) {
        log.error("Could not get changed files ", e);
    }

    return new FileChanges(Collections.emptyList());
}

From source file:deployer.publishers.openshift.RhcApplicationGitRepoModificationsTest.java

License:Apache License

private void populateWithInitialFiles(Git git, File testDir) throws IOException, GitAPIException {
    log.debug("Git Repo Dir: " + git.getRepository().getDirectory().toString());
    log.debug("Test dir: " + testDir.toString());
    FileUtils.writeStringToFile(Files.resolve(testDir, "README"), "This is from the RhcApplication unit test.");
    File aDir = Files.resolve(testDir, "a-dir");
    Files.createDirectories(aDir);
    FileUtils.writeStringToFile(Files.resolve(aDir, "source.json"), "{'hello': 'world'}");
    FileUtils.writeStringToFile(Files.resolve(aDir, "AnotherFile.txt"), "A file about nothing");
    FileUtils.writeStringToFile(Files.resolve(testDir, "deployed_version.txt"), "v0.33333333333333333");
    log.info(git.add().addFilepattern(".").call().toString());
    log.info(git.commit().setAuthor("Unit Test", "unit.test@email.com").setMessage("Initial files").setAll(true)
            .call().toString());/*w  w w .  j av  a2  s.co m*/
}

From source file:edu.wustl.lookingglass.community.CommunityRepository.java

License:Open Source License

private Git init() throws URISyntaxException, IOException, IllegalStateException, GitAPIException {
    assert !hasGitRepository(this.repoDir);

    Git git = Git.init().setDirectory(this.repoDir).call();

    StoredConfig config = git.getRepository().getConfig();
    config.setString("core", null, "ignorecase", "false"); // Be case sensitive explicitly to work on Mac
    config.setString("core", null, "filemode", "false"); // Ignore permission changes
    config.setString("core", null, "precomposeunicode", "true"); // Use the same Unicode form on all filesystems
    config.setString("push", null, "default", "simple");
    config.save();/*from  ww w . j  av  a2s  .c o  m*/

    return git;
}

From source file:es.logongas.openshift.ant.impl.OpenShiftUtil.java

License:Apache License

public String getCurrentBranch(String repositoryPath) {
    try {//from  w w w .  ja v a 2s. c o m
        Git git = Git.open(new File(repositoryPath));

        return git.getRepository().getBranch();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:eu.mihosoft.vrl.io.VersionedFile.java

License:Open Source License

/**
 * Closes the git repository.//from w w  w. j a  v  a 2 s.  c  o  m
 *
 * @param git git repository to close
 */
private void closeGit(Git git) {
    if (git != null) {
        git.getRepository().close();
    }
}