Example usage for org.eclipse.jgit.transport BundleWriter writeBundle

List of usage examples for org.eclipse.jgit.transport BundleWriter writeBundle

Introduction

In this page you can find the example usage for org.eclipse.jgit.transport BundleWriter writeBundle.

Prototype

public void writeBundle(ProgressMonitor monitor, OutputStream os) throws IOException 

Source Link

Document

Generate and write the bundle to the output stream.

Usage

From source file:com.surevine.gateway.scm.git.jgit.JGitGitFacade.java

License:Open Source License

@Override
public Path bundle(final LocalRepoBean repoBean) throws GitException {
    OutputStream outputStream = null;
    try {//from ww w  . j  av a 2  s.  co  m
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        Repository repository = builder.setGitDir(repoBean.getGitConfigDirectory().toFile()).findGitDir()
                .build();
        BundleWriter bundleWriter = new BundleWriter(repository);
        String fileName = StringUtil.cleanStringForFilePath(repoBean.getProjectKey() + "_" + repoBean.getSlug())
                + ".bundle";
        Path outputPath = Paths.get(PropertyUtil.getTempDir(), fileName);
        outputStream = Files.newOutputStream(outputPath);

        Map<String, Ref> refMap = repository.getAllRefs();
        for (Ref ref : refMap.values()) {
            bundleWriter.include(ref);
        }

        bundleWriter.writeBundle(NullProgressMonitor.INSTANCE, outputStream);
        outputStream.close();
        repository.close();
        return outputPath;
    } catch (IOException e) {
        throw new GitException(e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException ioe) {
                LOGGER.error(ioe);
            }
        }
    }
}

From source file:org.jdamico.jgitbkp.components.ManagerComponent.java

License:Open Source License

public List<BundleStatus> generateBundles(List<RepoImpl> reposLst, Config config) throws JGitBackupException {

    List<BundleStatus> bundleStatusLst = new ArrayList<BundleStatus>();

    boolean backupStatus = false;
    StringBuffer backupMessage = new StringBuffer();

    for (int i = 0; i < reposLst.size(); i++) {
        String bundlePath = config.getBundlePath() + "/" + reposLst.get(i).getName() + ".bundle";

        File gitWorkDir = new File(reposLst.get(i).getClonedPath());
        Git git = null;/*from ww  w .ja  v  a2 s .c o  m*/
        Repository localRepoGit = null;
        BundleWriter bundleWriter = null;
        ProgressMonitor pMonitor = null;
        OutputStream oStream = null;
        try {
            git = Git.open(gitWorkDir);
            git.pull();
            if (!Starter.silent)
                System.out.println("Updated data for [" + reposLst.get(i).getName() + "]");
            localRepoGit = git.getRepository();
            bundleWriter = new BundleWriter(localRepoGit);
            pMonitor = new TextProgressMonitor();
            oStream = new FileOutputStream(bundlePath);
            Map<String, Ref> allRefs = localRepoGit.getAllRefs();
            Collection<Ref> values = allRefs.values();
            Iterator<Ref> iter = values.iterator();
            while (iter.hasNext()) {
                try {
                    Ref element = iter.next();
                    bundleWriter.include(element);
                } catch (IllegalArgumentException e) {

                    if (!e.getMessage().equalsIgnoreCase("Invalid ref name: HEAD")) {
                        String err = reposLst.get(i).getName() + ": " + e.getMessage();
                        backupMessage.append(err);
                        LoggerManager.getInstance().logAtExceptionTime(this.getClass().getName(), err);
                    }
                }

            }
            if (!Starter.silent)
                bundleWriter.writeBundle(pMonitor, oStream);
            else
                bundleWriter.writeBundle(null, oStream);

        } catch (IOException e) {
            String err = reposLst.get(i).getName() + ": " + e.getMessage();
            backupMessage.append(err);
            LoggerManager.getInstance().logAtExceptionTime(this.getClass().getName(), err);
            throw new JGitBackupException(e);
        }

        File f = new File(bundlePath);
        if (f.exists() && f.length() > 100) {
            backupStatus = f.exists();
        } else
            backupStatus = false;

        bundleStatusLst.add(new BundleStatus(reposLst.get(i).getName(), backupStatus, backupMessage.toString(),
                Utils.getInstance().getCurrentDateTimeFormated(Constants.DATE_FORMAT), bundlePath));
        if (!Starter.silent)
            System.out.println("Bundle created: " + bundlePath + " - " + backupStatus);
    }

    return bundleStatusLst;
}

From source file:org.jdamico.jgitbkp.tests.JGitTester.java

License:Open Source License

@Test
public void test() throws IOException, GitAPIException {

    String user = "";
    String passwd = "";
    String hostPath = "/git";
    String backupRoot = "/tmp/test";
    String gitRoot = "/mnt";
    String bundlePath = "/tmp";
    boolean keepOld = false;

    List<org.jdamico.jgitbkp.entities.RepoImpl> repos = new ArrayList<org.jdamico.jgitbkp.entities.RepoImpl>();

    File fGitRoot = new File(gitRoot);
    String[] fRepos = fGitRoot.list();
    for (String itemRepo : fRepos) {
        File fItemRepo = new File(gitRoot + "/" + itemRepo);
        if (fItemRepo.isDirectory() && itemRepo.contains(".git") && !itemRepo.equals(".git")) {
            repos.add(new org.jdamico.jgitbkp.entities.RepoImpl(itemRepo, null, backupRoot + "/" + itemRepo));
        }//  w ww .  j ava2  s.co m
    }

    for (int i = 0; i < repos.size(); i++) {

        File f = new File(repos.get(i).getClonedPath());

        if (f.mkdir()) {

            Git.cloneRepository().setCloneAllBranches(true)
                    .setURI("http://" + hostPath + "/" + repos.get(i).getName())
                    .setCredentialsProvider(new UsernamePasswordCredentialsProvider(user, passwd))
                    .setDirectory(new File(repos.get(i).getClonedPath()))
                    .setProgressMonitor(new TextProgressMonitor()).call();

        }

        File gitWorkDir = new File(repos.get(i).getClonedPath());
        Git git = Git.open(gitWorkDir);
        git.pull();
        Repository localRepoGit = git.getRepository();
        BundleWriter bundleWriter = new BundleWriter(localRepoGit);
        ProgressMonitor pMonitor = new TextProgressMonitor();
        OutputStream oStream = new FileOutputStream(bundlePath + "/" + repos.get(i).getName() + ".bundle");

        Map<String, Ref> allRefs = localRepoGit.getAllRefs();
        Collection<Ref> values = allRefs.values();
        Iterator<Ref> iter = values.iterator();
        while (iter.hasNext()) {
            Ref element = iter.next();
            try {

                bundleWriter.include(element);
                System.out.println("Added: " + element.getName());
            } catch (Exception e) {
                // TODO: handle exception
            }

        }

        bundleWriter.writeBundle(pMonitor, oStream);

    }

}