List of usage examples for org.eclipse.jgit.lib ObjectLoader copyTo
public void copyTo(OutputStream out) throws MissingObjectException, IOException
From source file:co.bledo.gitmin.servlet.Review.java
License:Apache License
private String getFileContent(Repository repo, RevCommit commit, String path) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException { // and using commit's tree find the path RevTree tree = commit.getTree();/* w w w .jav a2s . co m*/ TreeWalk treeWalk = new TreeWalk(repo); treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create(path)); if (!treeWalk.next()) { return ""; } ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = repo.open(objectId); // and then one can use either //InputStream in = loader.openStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); loader.copyTo(out); return out.toString(); }
From source file:com.buildautomation.jgit.api.AddAndListNoteOfCommit.java
License:Apache License
public static void addAndListNoteOfCommit() throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { Ref head = repository.exactRef("refs/heads/master"); System.out.println("Found head: " + head); try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(head.getObjectId()); System.out.println("Found Commit: " + commit); try (Git git = new Git(repository)) { git.notesAdd().setMessage("some note message").setObjectId(commit).call(); System.out.println("Added Note to commit " + commit); List<Note> call = git.notesList().call(); System.out.println("Listing " + call.size() + " notes"); for (Note note : call) { // check if we found the note for this commit if (!note.getName().equals(head.getObjectId().getName())) { System.out.println("Note " + note + " did not match commit " + head); continue; }//from w w w. j av a2 s . c o m System.out.println("Found note: " + note + " for commit " + head); // displaying the contents of the note is done via a simple blob-read ObjectLoader loader = repository.open(note.getData()); loader.copyTo(System.out); } } walk.dispose(); } } }
From source file:com.buildautomation.jgit.api.ListNotes.java
License:Apache License
public static void listNotes() throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { List<Note> call = git.notesList().call(); System.out.println("Listing " + call.size() + " notes"); for (Note note : call) { System.out.println("Note: " + note + " " + note.getName() + " " + note.getData().getName() + "\nContent: "); // displaying the contents of the note is done via a simple blob-read ObjectLoader loader = repository.open(note.getData()); loader.copyTo(System.out); }/*w w w . j a v a2 s.c o m*/ } } }
From source file:com.buildautomation.jgit.api.ReadBlobContents.java
License:Apache License
public static void readBlobContents() throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { // the Ref holds an ObjectId for any type of object (tree, commit, blob, tree) Ref head = repository.exactRef("refs/heads/master"); System.out.println("Ref of refs/heads/master: " + head); System.out.println("\nPrint contents of head of master branch, i.e. the latest commit information"); ObjectLoader loader = repository.open(head.getObjectId()); loader.copyTo(System.out); System.out.println(/* w w w. j a va 2 s .com*/ "\nPrint contents of tree of head of master branch, i.e. the latest binary tree information"); // a commit points to a tree try (RevWalk walk = new RevWalk(repository)) { RevCommit commit = walk.parseCommit(head.getObjectId()); RevTree tree = walk.parseTree(commit.getTree().getId()); System.out.println("Found Tree: " + tree); loader = repository.open(tree.getId()); loader.copyTo(System.out); walk.dispose(); } } }
From source file:com.buildautomation.jgit.api.ReadFileFromCommit.java
License:Apache License
public static void readFileFromCommit() throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { // find the HEAD ObjectId lastCommitId = repository.resolve(Constants.HEAD); // a RevWalk allows to walk over commits based on some filtering that is defined try (RevWalk revWalk = new RevWalk(repository)) { RevCommit commit = revWalk.parseCommit(lastCommitId); // and using commit's tree find the path RevTree tree = commit.getTree(); System.out.println("Having tree: " + tree); // now try to find a specific file try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree);//from w w w . ja va2 s . c om treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create("README.md")); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'"); } ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = repository.open(objectId); // and then one can the loader to read the file loader.copyTo(System.out); } revWalk.dispose(); } } }
From source file:com.buildautomation.jgit.api.ReadTagFromName.java
License:Apache License
public static void readTagFromName() throws IOException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { // a RevWalk allows to retrieve information from the repository try (RevWalk walk = new RevWalk(repository)) { // a simple tag that is not annotated Ref simpleTag = repository.findRef("initialtag"); RevObject any = walk.parseAny(simpleTag.getObjectId()); System.out.println("Commit: " + any); // an annotated tag Ref annotatedTag = repository.findRef("secondtag"); any = walk.parseAny(annotatedTag.getObjectId()); System.out.println("Tag: " + any); // finally try to print out the tag-content System.out.println("\nTag-Content: \n"); ObjectLoader loader = repository.open(annotatedTag.getObjectId()); loader.copyTo(System.out); walk.dispose();// w w w. j a v a2 s . c o m } } }
From source file:com.buildautomation.jgit.api.ShowBlame.java
License:Apache License
private static int countFiles(Repository repository, ObjectId commitID, String name) throws IOException { try (RevWalk revWalk = new RevWalk(repository)) { RevCommit commit = revWalk.parseCommit(commitID); RevTree tree = commit.getTree(); System.out.println("Having tree: " + tree); // now try to find a specific file try (TreeWalk treeWalk = new TreeWalk(repository)) { treeWalk.addTree(tree);/* ww w . ja v a 2 s.c o m*/ treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create(name)); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file 'README.md'"); } ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = repository.open(objectId); ByteArrayOutputStream stream = new ByteArrayOutputStream(); // and then one can the loader to read the file loader.copyTo(stream); revWalk.dispose(); return IOUtils.readLines(new ByteArrayInputStream(stream.toByteArray())).size(); } } }
From source file:com.centurylink.mdw.dataaccess.file.VersionControlGit.java
License:Apache License
public byte[] readFromCommit(String commitId, String path) throws Exception { try (RevWalk revWalk = new RevWalk(localRepo)) { RevCommit commit = revWalk.parseCommit(ObjectId.fromString(commitId)); // use commit's tree find the path RevTree tree = commit.getTree(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (TreeWalk treeWalk = new TreeWalk(localRepo)) { treeWalk.addTree(tree);// ww w .j a v a2 s. c om treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create(path)); if (!treeWalk.next()) { return null; } ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = localRepo.open(objectId); loader.copyTo(baos); } revWalk.dispose(); return baos.toByteArray(); } }
From source file:com.gitblit.servlet.RawServlet.java
License:Apache License
protected boolean streamFromRepo(HttpServletRequest request, HttpServletResponse response, Repository repository, RevCommit commit, String requestedPath) throws IOException { boolean served = false; RevWalk rw = new RevWalk(repository); TreeWalk tw = new TreeWalk(repository); try {// www. jav a2 s . c o m tw.reset(); tw.addTree(commit.getTree()); PathFilter f = PathFilter.create(requestedPath); tw.setFilter(f); tw.setRecursive(true); MutableObjectId id = new MutableObjectId(); ObjectReader reader = tw.getObjectReader(); while (tw.next()) { FileMode mode = tw.getFileMode(0); if (mode == FileMode.GITLINK || mode == FileMode.TREE) { continue; } tw.getObjectId(id, 0); String filename = StringUtils.getLastPathElement(requestedPath); try { String userAgent = request.getHeader("User-Agent"); if (userAgent != null && userAgent.indexOf("MSIE 5.5") > -1) { response.setHeader("Content-Disposition", "filename=\"" + URLEncoder.encode(filename, Constants.ENCODING) + "\""); } else if (userAgent != null && userAgent.indexOf("MSIE") > -1) { response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(filename, Constants.ENCODING) + "\""); } else { response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(filename.getBytes(Constants.ENCODING), "latin1") + "\""); } } catch (UnsupportedEncodingException e) { response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); } long len = reader.getObjectSize(id, org.eclipse.jgit.lib.Constants.OBJ_BLOB); setContentType(response, "application/octet-stream"); response.setIntHeader("Content-Length", (int) len); ObjectLoader ldr = repository.open(id); ldr.copyTo(response.getOutputStream()); served = true; } } finally { tw.close(); rw.dispose(); } response.flushBuffer(); return served; }
From source file:com.gitblit.utils.CompressionUtils.java
License:Apache License
/** * Zips the contents of the tree at the (optionally) specified revision and * the (optionally) specified basepath to the supplied outputstream. * * @param repository//from w ww. j ava 2s .co m * @param basePath * if unspecified, entire repository is assumed. * @param objectId * if unspecified, HEAD is assumed. * @param os * @return true if repository was successfully zipped to supplied output * stream */ public static boolean zip(Repository repository, IFilestoreManager filestoreManager, String basePath, String objectId, OutputStream os) { RevCommit commit = JGitUtils.getCommit(repository, objectId); if (commit == null) { return false; } boolean success = false; RevWalk rw = new RevWalk(repository); TreeWalk tw = new TreeWalk(repository); try { tw.reset(); tw.addTree(commit.getTree()); ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os); zos.setComment("Generated by Gitblit"); if (!StringUtils.isEmpty(basePath)) { PathFilter f = PathFilter.create(basePath); tw.setFilter(f); } tw.setRecursive(true); MutableObjectId id = new MutableObjectId(); ObjectReader reader = tw.getObjectReader(); long modified = commit.getAuthorIdent().getWhen().getTime(); while (tw.next()) { FileMode mode = tw.getFileMode(0); if (mode == FileMode.GITLINK || mode == FileMode.TREE) { continue; } tw.getObjectId(id, 0); ObjectLoader loader = repository.open(id); ZipArchiveEntry entry = new ZipArchiveEntry(tw.getPathString()); FilestoreModel filestoreItem = null; if (JGitUtils.isPossibleFilestoreItem(loader.getSize())) { filestoreItem = JGitUtils.getFilestoreItem(tw.getObjectReader().open(id)); } final long size = (filestoreItem == null) ? loader.getSize() : filestoreItem.getSize(); entry.setSize(size); entry.setComment(commit.getName()); entry.setUnixMode(mode.getBits()); entry.setTime(modified); zos.putArchiveEntry(entry); if (filestoreItem == null) { //Copy repository stored file loader.copyTo(zos); } else { //Copy filestore file try (FileInputStream streamIn = new FileInputStream( filestoreManager.getStoragePath(filestoreItem.oid))) { IOUtils.copyLarge(streamIn, zos); } catch (Throwable e) { LOGGER.error( MessageFormat.format("Failed to archive filestore item {0}", filestoreItem.oid), e); //Handle as per other errors throw e; } } zos.closeArchiveEntry(); } zos.finish(); success = true; } catch (IOException e) { error(e, repository, "{0} failed to zip files from commit {1}", commit.getName()); } finally { tw.close(); rw.dispose(); } return success; }