List of usage examples for org.eclipse.jgit.api Git wrap
public static Git wrap(Repository repo)
From source file:de.blizzy.documentr.page.PageStore.java
License:Open Source License
private MergeConflict savePageInternal(String projectName, String branchName, String path, String suffix, Page page, String baseCommit, String rootDir, User user, ILockedRepository repo, boolean push) throws IOException, GitAPIException { Git git = Git.wrap(repo.r()); String headCommit = CommitUtils.getHead(repo.r()).getName(); if ((baseCommit != null) && headCommit.equals(baseCommit)) { baseCommit = null;// w ww . j av a 2 s . c o m } String editBranchName = "_edit_" + String.valueOf((long) (Math.random() * Long.MAX_VALUE)); //$NON-NLS-1$ if (baseCommit != null) { git.branchCreate().setName(editBranchName).setStartPoint(baseCommit).call(); git.checkout().setName(editBranchName).call(); } Map<String, Object> metaMap = new HashMap<String, Object>(); metaMap.put(TITLE, page.getTitle()); metaMap.put(CONTENT_TYPE, page.getContentType()); if (!page.getTags().isEmpty()) { metaMap.put(TAGS, page.getTags()); } metaMap.put(VIEW_RESTRICTION_ROLE, page.getViewRestrictionRole()); Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); String json = gson.toJson(metaMap); File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File pagesDir = new File(workingDir, rootDir); File workingFile = Util.toFile(pagesDir, path + DocumentrConstants.META_SUFFIX); FileUtils.write(workingFile, json, Charsets.UTF_8); PageData pageData = page.getData(); if (pageData != null) { workingFile = Util.toFile(pagesDir, path + suffix); FileUtils.writeByteArrayToFile(workingFile, pageData.getData()); } AddCommand addCommand = git.add().addFilepattern(rootDir + "/" + path + DocumentrConstants.META_SUFFIX); //$NON-NLS-1$ if (pageData != null) { addCommand.addFilepattern(rootDir + "/" + path + suffix); //$NON-NLS-1$ } addCommand.call(); PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail()); git.commit().setAuthor(ident).setCommitter(ident).setMessage(rootDir + "/" + path + suffix).call(); //$NON-NLS-1$ MergeConflict conflict = null; if (baseCommit != null) { git.rebase().setUpstream(branchName).call(); if (repo.r().getRepositoryState() != RepositoryState.SAFE) { String text = FileUtils.readFileToString(workingFile, Charsets.UTF_8); conflict = new MergeConflict(text, headCommit); git.rebase().setOperation(RebaseCommand.Operation.ABORT).call(); } git.checkout().setName(branchName).call(); if (conflict == null) { git.merge().include(repo.r().resolve(editBranchName)).call(); } git.branchDelete().setBranchNames(editBranchName).setForce(true).call(); } if (push && (conflict == null)) { git.push().call(); } page.setParentPagePath(getParentPagePath(path, repo.r())); if (conflict == null) { PageUtil.updateProjectEditTime(projectName); } return conflict; }
From source file:de.blizzy.documentr.page.PageStore.java
License:Open Source License
@Override public void deletePage(String projectName, String branchName, final String path, User user) throws IOException { Assert.hasLength(projectName);//from ww w . java 2 s.com Assert.hasLength(branchName); Assert.hasLength(path); Assert.notNull(user); ILockedRepository repo = null; List<String> oldPagePaths; boolean deleted = false; try { repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName); File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File pagesDir = new File(workingDir, DocumentrConstants.PAGES_DIR_NAME); File oldSubPagesDir = Util.toFile(pagesDir, path); oldPagePaths = listPagePaths(oldSubPagesDir, true); oldPagePaths = Lists.newArrayList(Lists.transform(oldPagePaths, new Function<String, String>() { @Override public String apply(String p) { return path + "/" + p; //$NON-NLS-1$ } })); oldPagePaths.add(path); Git git = Git.wrap(repo.r()); File file = Util.toFile(pagesDir, path + DocumentrConstants.PAGE_SUFFIX); if (file.isFile()) { git.rm().addFilepattern( DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.PAGE_SUFFIX) //$NON-NLS-1$ .call(); deleted = true; } file = Util.toFile(pagesDir, path + DocumentrConstants.META_SUFFIX); if (file.isFile()) { git.rm().addFilepattern( DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.META_SUFFIX) //$NON-NLS-1$ .call(); deleted = true; } file = Util.toFile(pagesDir, path); if (file.isDirectory()) { git.rm().addFilepattern(DocumentrConstants.PAGES_DIR_NAME + "/" + path) //$NON-NLS-1$ .call(); deleted = true; } File attachmentsDir = new File(workingDir, DocumentrConstants.ATTACHMENTS_DIR_NAME); file = Util.toFile(attachmentsDir, path); if (file.isDirectory()) { git.rm().addFilepattern(DocumentrConstants.ATTACHMENTS_DIR_NAME + "/" + path) //$NON-NLS-1$ .call(); deleted = true; } if (deleted) { PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail()); git.commit().setAuthor(ident).setCommitter(ident).setMessage("delete " + path) //$NON-NLS-1$ .call(); git.push().call(); PageUtil.updateProjectEditTime(projectName); } } catch (GitAPIException e) { throw new IOException(e); } finally { Closeables.closeQuietly(repo); } if (deleted) { eventBus.post(new PagesDeletedEvent(projectName, branchName, Sets.newHashSet(oldPagePaths))); } }
From source file:de.blizzy.documentr.page.PageStore.java
License:Open Source License
@Override public void relocatePage(final String projectName, final String branchName, final String path, String newParentPagePath, User user) throws IOException { Assert.hasLength(projectName);//from w w w. ja v a2 s. c o m Assert.hasLength(branchName); Assert.hasLength(path); Assert.hasLength(newParentPagePath); Assert.notNull(user); // check if pages exist by trying to load them getPage(projectName, branchName, path, false); getPage(projectName, branchName, newParentPagePath, false); ILockedRepository repo = null; List<String> oldPagePaths; List<String> deletedPagePaths; List<String> newPagePaths; List<PagePathChangedEvent> pagePathChangedEvents; try { repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName); String pageName = path.contains("/") ? StringUtils.substringAfterLast(path, "/") : path; //$NON-NLS-1$ //$NON-NLS-2$ final String newPagePath = newParentPagePath + "/" + pageName; //$NON-NLS-1$ File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File oldSubPagesDir = Util.toFile(new File(workingDir, DocumentrConstants.PAGES_DIR_NAME), path); oldPagePaths = listPagePaths(oldSubPagesDir, true); oldPagePaths = Lists.newArrayList(Lists.transform(oldPagePaths, new Function<String, String>() { @Override public String apply(String p) { return path + "/" + p; //$NON-NLS-1$ } })); oldPagePaths.add(path); File deletedPagesSubDir = Util.toFile(new File(workingDir, DocumentrConstants.PAGES_DIR_NAME), newPagePath); deletedPagePaths = listPagePaths(deletedPagesSubDir, true); deletedPagePaths = Lists.newArrayList(Lists.transform(deletedPagePaths, new Function<String, String>() { @Override public String apply(String p) { return newPagePath + "/" + p; //$NON-NLS-1$ } })); deletedPagePaths.add(newPagePath); Git git = Git.wrap(repo.r()); AddCommand addCommand = git.add(); for (String dirName : Sets.newHashSet(DocumentrConstants.PAGES_DIR_NAME, DocumentrConstants.ATTACHMENTS_DIR_NAME)) { File dir = new File(workingDir, dirName); File newSubPagesDir = Util.toFile(dir, newPagePath); if (newSubPagesDir.exists()) { git.rm().addFilepattern(dirName + "/" + newPagePath).call(); //$NON-NLS-1$ } File newPageFile = Util.toFile(dir, newPagePath + DocumentrConstants.PAGE_SUFFIX); if (newPageFile.exists()) { git.rm().addFilepattern(dirName + "/" + newPagePath + DocumentrConstants.PAGE_SUFFIX).call(); //$NON-NLS-1$ } File newMetaFile = Util.toFile(dir, newPagePath + DocumentrConstants.META_SUFFIX); if (newMetaFile.exists()) { git.rm().addFilepattern(dirName + "/" + newPagePath + DocumentrConstants.META_SUFFIX).call(); //$NON-NLS-1$ } File newParentPageDir = Util.toFile(dir, newParentPagePath); File subPagesDir = Util.toFile(dir, path); if (subPagesDir.exists()) { FileUtils.copyDirectoryToDirectory(subPagesDir, newParentPageDir); git.rm().addFilepattern(dirName + "/" + path).call(); //$NON-NLS-1$ addCommand.addFilepattern(dirName + "/" + newParentPagePath + "/" + pageName); //$NON-NLS-1$ //$NON-NLS-2$ } File pageFile = Util.toFile(dir, path + DocumentrConstants.PAGE_SUFFIX); if (pageFile.exists()) { FileUtils.copyFileToDirectory(pageFile, newParentPageDir); git.rm().addFilepattern(dirName + "/" + path + DocumentrConstants.PAGE_SUFFIX).call(); //$NON-NLS-1$ addCommand.addFilepattern( dirName + "/" + newParentPagePath + "/" + pageName + DocumentrConstants.PAGE_SUFFIX); //$NON-NLS-1$ //$NON-NLS-2$ } File metaFile = Util.toFile(dir, path + DocumentrConstants.META_SUFFIX); if (metaFile.exists()) { FileUtils.copyFileToDirectory(metaFile, newParentPageDir); git.rm().addFilepattern(dirName + "/" + path + DocumentrConstants.META_SUFFIX).call(); //$NON-NLS-1$ addCommand.addFilepattern( dirName + "/" + newParentPagePath + "/" + pageName + DocumentrConstants.META_SUFFIX); //$NON-NLS-1$ //$NON-NLS-2$ } } addCommand.call(); PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail()); git.commit().setAuthor(ident).setCommitter(ident) .setMessage("move " + path + " to " + newParentPagePath + "/" + pageName) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ .call(); git.push().call(); newPagePaths = Lists.transform(oldPagePaths, new Function<String, String>() { @Override public String apply(String p) { return newPagePath + StringUtils.removeStart(p, path); } }); pagePathChangedEvents = Lists.transform(oldPagePaths, new Function<String, PagePathChangedEvent>() { @Override public PagePathChangedEvent apply(String oldPath) { String newPath = newPagePath + StringUtils.removeStart(oldPath, path); return new PagePathChangedEvent(projectName, branchName, oldPath, newPath); } }); PageUtil.updateProjectEditTime(projectName); } catch (GitAPIException e) { throw new IOException(e); } finally { Closeables.closeQuietly(repo); } Set<String> allDeletedPagePaths = Sets.newHashSet(oldPagePaths); allDeletedPagePaths.addAll(deletedPagePaths); eventBus.post(new PagesDeletedEvent(projectName, branchName, allDeletedPagePaths)); for (String newPath : newPagePaths) { eventBus.post(new PageChangedEvent(projectName, branchName, newPath)); } for (PagePathChangedEvent event : pagePathChangedEvents) { eventBus.post(event); } }
From source file:de.blizzy.documentr.page.PageStore.java
License:Open Source License
@Override public void deleteAttachment(String projectName, String branchName, String pagePath, String name, User user) throws IOException { Assert.hasLength(projectName);//from w w w .j a v a 2s .c o m Assert.hasLength(branchName); Assert.hasLength(pagePath); Assert.hasLength(name); Assert.notNull(user); ILockedRepository repo = null; try { repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName); File workingDir = RepositoryUtil.getWorkingDir(repo.r()); Git git = Git.wrap(repo.r()); File attachmentsDir = new File(workingDir, DocumentrConstants.ATTACHMENTS_DIR_NAME); boolean deleted = false; File file = Util.toFile(attachmentsDir, pagePath + "/" + name + DocumentrConstants.PAGE_SUFFIX); //$NON-NLS-1$ if (file.isFile()) { FileUtils.forceDelete(file); git.rm().addFilepattern(DocumentrConstants.ATTACHMENTS_DIR_NAME + "/" + //$NON-NLS-1$ pagePath + "/" + name + DocumentrConstants.PAGE_SUFFIX) //$NON-NLS-1$ .call(); deleted = true; } file = Util.toFile(attachmentsDir, pagePath + "/" + name + DocumentrConstants.META_SUFFIX); //$NON-NLS-1$ if (file.isFile()) { FileUtils.forceDelete(file); git.rm().addFilepattern(DocumentrConstants.ATTACHMENTS_DIR_NAME + "/" + //$NON-NLS-1$ pagePath + "/" + name + DocumentrConstants.META_SUFFIX) //$NON-NLS-1$ .call(); deleted = true; } if (deleted) { PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail()); git.commit().setAuthor(ident).setCommitter(ident).setMessage("delete " + pagePath + "/" + name) //$NON-NLS-1$ //$NON-NLS-2$ .call(); git.push().call(); PageUtil.updateProjectEditTime(projectName); } } catch (GitAPIException e) { throw new IOException(e); } finally { Closeables.closeQuietly(repo); } }
From source file:de.blizzy.documentr.page.PageStore.java
License:Open Source License
@Override public void restorePageVersion(String projectName, String branchName, String path, String version, User user) throws IOException { Assert.hasLength(projectName);/*from www .ja v a 2 s. c o m*/ Assert.hasLength(branchName); Assert.hasLength(path); Assert.hasLength(version); ILockedRepository repo = null; try { repo = globalRepositoryManager.getProjectBranchRepository(projectName, branchName); String text = BlobUtils.getContent(repo.r(), version, DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.PAGE_SUFFIX); //$NON-NLS-1$ File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File pagesDir = new File(workingDir, DocumentrConstants.PAGES_DIR_NAME); File file = Util.toFile(pagesDir, path + DocumentrConstants.PAGE_SUFFIX); FileUtils.writeStringToFile(file, text, Charsets.UTF_8.name()); Git git = Git.wrap(repo.r()); git.add() .addFilepattern(DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.PAGE_SUFFIX) //$NON-NLS-1$ .call(); PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail()); git.commit().setAuthor(ident).setCommitter(ident) .setMessage(DocumentrConstants.PAGES_DIR_NAME + "/" + path) //$NON-NLS-1$ .call(); git.push().call(); } catch (GitAPIException e) { throw new IOException(e); } finally { Closeables.closeQuietly(repo); } eventBus.post(new PageChangedEvent(projectName, branchName, path)); }
From source file:de.blizzy.documentr.repository.ProjectRepositoryManager.java
License:Open Source License
ILockedRepository createCentralRepository(boolean bare, User user) throws IOException, GitAPIException { if (centralRepoDir.isDirectory()) { throw new IllegalStateException("repository already exists: " + centralRepoDir.getAbsolutePath()); //$NON-NLS-1$ }//from w w w. j a v a 2s .c o m ILock lock = lockManager.lockAll(); try { Repository repo = null; File gitDir = new File(centralRepoDir, ".git"); //$NON-NLS-1$ try { RepositoryBuilder builder = new RepositoryBuilder().setGitDir(gitDir); if (bare) { builder.setBare(); } repo = builder.build(); repo.create(); } finally { RepositoryUtil.closeQuietly(repo); } File tempGitDir = new File(new File(reposDir, CENTRAL_REPO_NAME + "_temp"), ".git"); //$NON-NLS-1$ //$NON-NLS-2$ Repository tempRepo = null; try { tempRepo = Git.cloneRepository().setURI(gitDir.toURI().toString()).setDirectory(tempGitDir).call() .getRepository(); Git git = Git.wrap(tempRepo); PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail()); git.commit().setAuthor(ident).setCommitter(ident).setMessage("init").call(); //$NON-NLS-1$ git.push().call(); } finally { RepositoryUtil.closeQuietly(tempRepo); } FileUtils.forceDelete(tempGitDir.getParentFile()); } finally { lockManager.unlock(lock); } return getCentralRepository(bare); }
From source file:de.blizzy.documentr.repository.ProjectRepositoryManager.java
License:Open Source License
ILockedRepository createBranchRepository(String branchName, String startingBranch) throws IOException, GitAPIException { Assert.hasLength(branchName);//from ww w. java 2 s . c o m if (startingBranch != null) { Assert.hasLength(startingBranch); } File repoDir = new File(reposDir, branchName); if (repoDir.isDirectory()) { throw new IllegalStateException("repository already exists: " + repoDir.getAbsolutePath()); //$NON-NLS-1$ } List<String> branches = listBranches(); if (branches.contains(branchName)) { throw new IllegalArgumentException("branch already exists: " + branchName); //$NON-NLS-1$ } if ((startingBranch == null) && !branches.isEmpty()) { throw new IllegalArgumentException("must specify a starting branch"); //$NON-NLS-1$ } ILock lock = lockManager.lockAll(); try { Repository centralRepo = null; File centralRepoGitDir; try { centralRepo = getCentralRepositoryInternal(true); centralRepoGitDir = centralRepo.getDirectory(); } finally { RepositoryUtil.closeQuietly(centralRepo); centralRepo = null; } Repository repo = null; try { repo = Git.cloneRepository().setURI(centralRepoGitDir.toURI().toString()).setDirectory(repoDir) .call().getRepository(); try { centralRepo = getCentralRepositoryInternal(true); if (!RepositoryUtils.getBranches(centralRepo).contains(branchName)) { CreateBranchCommand createBranchCommand = Git.wrap(centralRepo).branchCreate(); if (startingBranch != null) { createBranchCommand.setStartPoint(startingBranch); } createBranchCommand.setName(branchName).call(); } } finally { RepositoryUtil.closeQuietly(centralRepo); } Git git = Git.wrap(repo); RefSpec refSpec = new RefSpec("refs/heads/" + branchName + ":refs/remotes/origin/" + branchName); //$NON-NLS-1$ //$NON-NLS-2$ git.fetch().setRemote("origin").setRefSpecs(refSpec).call(); //$NON-NLS-1$ git.branchCreate().setName(branchName).setStartPoint("origin/" + branchName).call(); //$NON-NLS-1$ git.checkout().setName(branchName).call(); } finally { RepositoryUtil.closeQuietly(repo); } } finally { lockManager.unlock(lock); } return getBranchRepository(branchName); }
From source file:de.blizzy.documentr.repository.ProjectRepositoryManager.java
License:Open Source License
ILockedRepository getBranchRepository(String branchName) throws IOException, GitAPIException { Assert.hasLength(branchName);//from w ww . j a va 2 s. co m File repoDir = new File(reposDir, branchName); if (!repoDir.isDirectory()) { throw new RepositoryNotFoundException(projectName, branchName); } LockedRepository lockedRepo = LockedRepository.lockProjectBranch(projectName, branchName, lockManager); Repository repo = new RepositoryBuilder().findGitDir(repoDir).build(); Git.wrap(repo).pull().call(); lockedRepo.setRepository(repo); return lockedRepo; }
From source file:de.blizzy.documentr.repository.ProjectRepositoryManager.java
License:Open Source License
public void importSampleContents() throws IOException, GitAPIException { // TODO: synchronization is not quite correct here, but should be okay in this edge case if (listBranches().isEmpty()) { ILock lock = lockManager.lockAll(); List<String> branches; try {/*from ww w.j a v a2s.co m*/ File gitDir = new File(centralRepoDir, ".git"); //$NON-NLS-1$ FileUtils.forceDelete(gitDir); Git.cloneRepository().setURI(DocumentrConstants.SAMPLE_REPO_URL).setDirectory(gitDir).setBare(true) .call(); Repository centralRepo = null; File centralRepoGitDir; try { centralRepo = getCentralRepositoryInternal(true); centralRepoGitDir = centralRepo.getDirectory(); StoredConfig config = centralRepo.getConfig(); config.unsetSection("remote", "origin"); //$NON-NLS-1$ //$NON-NLS-2$ config.unsetSection("branch", "master"); //$NON-NLS-1$ //$NON-NLS-2$ config.save(); } finally { RepositoryUtil.closeQuietly(centralRepo); } branches = listBranches(); for (String branchName : branches) { File repoDir = new File(reposDir, branchName); Repository repo = null; try { repo = Git.cloneRepository().setURI(centralRepoGitDir.toURI().toString()) .setDirectory(repoDir).call().getRepository(); Git git = Git.wrap(repo); RefSpec refSpec = new RefSpec( "refs/heads/" + branchName + ":refs/remotes/origin/" + branchName); //$NON-NLS-1$ //$NON-NLS-2$ git.fetch().setRemote("origin").setRefSpecs(refSpec).call(); //$NON-NLS-1$ git.branchCreate().setName(branchName).setStartPoint("origin/" + branchName).call(); //$NON-NLS-1$ git.checkout().setName(branchName).call(); } finally { RepositoryUtil.closeQuietly(repo); } } } finally { lockManager.unlock(lock); } for (String branch : branches) { eventBus.post(new BranchCreatedEvent(projectName, branch)); } } }
From source file:de.blizzy.documentr.repository.ProjectRepositoryManagerTest.java
License:Open Source License
@Test public void createBranchRepository() throws IOException, GitAPIException { File reposDir = tempDir.getRoot(); ProjectRepositoryManager repoManager = new ProjectRepositoryManager("project", reposDir, lockManager, //$NON-NLS-1$ eventBus);/*from w ww . j a v a2 s. co m*/ ILockedRepository repo = null; try { repo = repoManager.createCentralRepository(USER); Git.wrap(repo.r()).branchCreate().setName("startingBranch").call(); //$NON-NLS-1$ } finally { Closeables.closeQuietly(repo); repo = null; } try { repo = repoManager.createBranchRepository("branch", "startingBranch"); //$NON-NLS-1$ //$NON-NLS-2$ assertEquals(new File(new File(reposDir, "branch"), ".git"), repo.r().getDirectory()); //$NON-NLS-1$ //$NON-NLS-2$ assertTrue(RepositoryUtils.getBranches(repo.r()).contains("refs/heads/branch")); //$NON-NLS-1$ } finally { Closeables.closeQuietly(repo); repo = null; } try { repo = repoManager.getCentralRepository(); assertTrue(RepositoryUtils.getBranches(repo.r()).contains("refs/heads/branch")); //$NON-NLS-1$ } finally { Closeables.closeQuietly(repo); } }