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

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

Introduction

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

Prototype

Map include

To view the source code for org.eclipse.jgit.transport BundleWriter include.

Click Source Link

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  w  w  w  .  j av a 2s .  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 w w w .j  av  a  2  s  .c om*/
        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  w  w .  j a v a 2 s .c  o 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);

    }

}