List of usage examples for org.eclipse.jgit.transport BundleWriter BundleWriter
public BundleWriter(ObjectReader or)
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;// w ww.j av a 2 s .com 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)); }/*from ww w .j av a 2 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); } }