List of usage examples for org.eclipse.jgit.treewalk TreeWalk forPath
public static TreeWalk forPath(final Repository db, final String path, final RevTree tree) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException
From source file:at.bitandart.zoubek.mervin.gerrit.GitURIParser.java
License:Open Source License
/** * loads the referenced {@link ObjectId} and the referenced * {@link ObjectLoader}//w w w . j av a 2 s.c om * * @throws IOException * if an error occurs during parsing the URI */ private void loadObject() throws IOException { if (commit == null) loadCommit(); TreeWalk treeWalk = TreeWalk.forPath(repository, filePath, commit.getTree()); if (treeWalk == null) { throw new IOException("Could not find a file at \"" + filePath + "\" in commit " + commitHash); } // finally we got the object in question objectId = treeWalk.getObjectId(0); objectLoader = repository.open(objectId); }
From source file:com.buildautomation.jgit.api.ListFilesOfCommitAndTag.java
License:Apache License
private static TreeWalk buildTreeWalk(Repository repository, RevTree tree, final String path) throws IOException { TreeWalk treeWalk = TreeWalk.forPath(repository, path, tree); if (treeWalk == null) { throw new FileNotFoundException( "Did not find expected file '" + path + "' in tree '" + tree.getName() + "'"); }//from w w w .j av a2s . c o m return treeWalk; }
From source file:com.centurylink.mdw.dataaccess.file.VersionControlGit.java
License:Apache License
public ObjectStream getRemoteContentStream(String branch, String path) throws Exception { ObjectId id = localRepo.resolve("refs/remotes/origin/" + branch); try (ObjectReader reader = localRepo.newObjectReader(); RevWalk walk = new RevWalk(reader)) { RevCommit commit = walk.parseCommit(id); RevTree tree = commit.getTree(); TreeWalk treewalk = TreeWalk.forPath(reader, path, tree); if (treewalk != null) { return reader.open(treewalk.getObjectId(0)).openStream(); } else {/*from w ww . j a va 2 s.c om*/ return null; } } }
From source file:com.gitblit.utils.JGitUtils.java
License:Apache License
/** * Returns a path model by path string/*from w ww . j a v a 2 s. c om*/ * * @param repo * @param path * @param filter * @param commit * @return a path model of the specified object */ private static PathModel getPathModel(Repository repo, String path, String filter, RevCommit commit) throws IOException { long size = 0; FilestoreModel filestoreItem = null; TreeWalk tw = TreeWalk.forPath(repo, path, commit.getTree()); String pathString = path; if (!tw.isSubtree() && (tw.getFileMode(0) != FileMode.GITLINK)) { pathString = PathUtils.getLastPathComponent(pathString); size = tw.getObjectReader().getObjectSize(tw.getObjectId(0), Constants.OBJ_BLOB); if (isPossibleFilestoreItem(size)) { filestoreItem = getFilestoreItem(tw.getObjectReader().open(tw.getObjectId(0))); } } else if (tw.isSubtree()) { // do not display dirs that are behind in the path if (!Strings.isNullOrEmpty(filter)) { pathString = path.replaceFirst(filter + "/", ""); } // remove the last slash from path in displayed link if (pathString != null && pathString.charAt(pathString.length() - 1) == '/') { pathString = pathString.substring(0, pathString.length() - 1); } } return new PathModel(pathString, tw.getPathString(), filestoreItem, size, tw.getFileMode(0).getBits(), tw.getObjectId(0).getName(), commit.getName()); }
From source file:com.google.gerrit.httpd.raw.CatServlet.java
License:Apache License
@Override protected void doGet(final HttpServletRequest req, final HttpServletResponse rsp) throws IOException { String keyStr = req.getPathInfo(); // We shouldn't have to do this extra decode pass, but somehow we // are now receiving our "^1" suffix as "%5E1", which confuses us // downstream. Other times we get our embedded "," as "%2C", which // is equally bad. And yet when these happen a "%2F" is left as-is, // rather than escaped as "%252F", which makes me feel really really // uncomfortable with a blind decode right here. ///*from w ww . j a v a 2 s. c o m*/ keyStr = Url.decode(keyStr); if (!keyStr.startsWith("/")) { rsp.sendError(HttpServletResponse.SC_NOT_FOUND); return; } keyStr = keyStr.substring(1); final Patch.Key patchKey; final int side; { final int c = keyStr.lastIndexOf('^'); if (c == 0) { rsp.sendError(HttpServletResponse.SC_NOT_FOUND); return; } if (c < 0) { side = 0; } else { try { side = Integer.parseInt(keyStr.substring(c + 1)); keyStr = keyStr.substring(0, c); } catch (NumberFormatException e) { rsp.sendError(HttpServletResponse.SC_NOT_FOUND); return; } } try { patchKey = Patch.Key.parse(keyStr); } catch (NumberFormatException e) { rsp.sendError(HttpServletResponse.SC_NOT_FOUND); return; } } final Change.Id changeId = patchKey.getParentKey().getParentKey(); final Project project; final String revision; try { final ReviewDb db = requestDb.get(); final ChangeControl control = changeControl.validateFor(changeId, userProvider.get()); project = control.getProject(); if (patchKey.getParentKey().get() == 0) { // change edit try { Optional<ChangeEdit> edit = changeEditUtil.byChange(control.getChange()); if (edit.isPresent()) { revision = edit.get().getRevision().get(); } else { rsp.sendError(HttpServletResponse.SC_NOT_FOUND); return; } } catch (AuthException e) { rsp.sendError(HttpServletResponse.SC_NOT_FOUND); return; } } else { PatchSet patchSet = db.patchSets().get(patchKey.getParentKey()); if (patchSet == null) { rsp.sendError(HttpServletResponse.SC_NOT_FOUND); return; } revision = patchSet.getRevision().get(); } } catch (NoSuchChangeException e) { rsp.sendError(HttpServletResponse.SC_NOT_FOUND); return; } catch (OrmException e) { getServletContext().log("Cannot query database", e); rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } ObjectLoader blobLoader; RevCommit fromCommit; String suffix; String path = patchKey.getFileName(); try (Repository repo = repoManager.openRepository(project.getNameKey())) { try (ObjectReader reader = repo.newObjectReader(); RevWalk rw = new RevWalk(reader)) { RevCommit c; c = rw.parseCommit(ObjectId.fromString(revision)); if (side == 0) { fromCommit = c; suffix = "new"; } else if (1 <= side && side - 1 < c.getParentCount()) { fromCommit = rw.parseCommit(c.getParent(side - 1)); if (c.getParentCount() == 1) { suffix = "old"; } else { suffix = "old" + side; } } else { rsp.sendError(HttpServletResponse.SC_NOT_FOUND); return; } try (TreeWalk tw = TreeWalk.forPath(reader, path, fromCommit.getTree())) { if (tw == null) { rsp.sendError(HttpServletResponse.SC_NOT_FOUND); return; } if (tw.getFileMode(0).getObjectType() == Constants.OBJ_BLOB) { blobLoader = reader.open(tw.getObjectId(0), Constants.OBJ_BLOB); } else { rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } } } } catch (RepositoryNotFoundException e) { getServletContext().log("Cannot open repository", e); rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } catch (IOException | RuntimeException e) { getServletContext().log("Cannot read repository", e); rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } final byte[] raw = blobLoader.isLarge() ? null : blobLoader.getCachedBytes(); final long when = fromCommit.getCommitTime() * 1000L; rsp.setDateHeader("Last-Modified", when); CacheHeaders.setNotCacheable(rsp); try (OutputStream out = openOutputStream(req, rsp, blobLoader, fromCommit, when, path, suffix, raw)) { if (raw != null) { out.write(raw); } else { blobLoader.copyTo(out); } } }
From source file:com.google.gerrit.server.change.FileContentUtil.java
License:Apache License
public BinaryResult getContent(ProjectState project, ObjectId revstr, String path) throws ResourceNotFoundException, IOException { try (Repository repo = openRepository(project); RevWalk rw = new RevWalk(repo)) { RevCommit commit = rw.parseCommit(revstr); ObjectReader reader = rw.getObjectReader(); TreeWalk tw = TreeWalk.forPath(reader, path, commit.getTree()); if (tw == null) { throw new ResourceNotFoundException(); }/*from w w w. j a v a2 s. co m*/ org.eclipse.jgit.lib.FileMode mode = tw.getFileMode(0); ObjectId id = tw.getObjectId(0); if (mode == org.eclipse.jgit.lib.FileMode.GITLINK) { return BinaryResult.create(id.name()).setContentType(X_GIT_GITLINK).base64(); } ObjectLoader obj = repo.open(id, OBJ_BLOB); byte[] raw; try { raw = obj.getCachedBytes(MAX_SIZE); } catch (LargeObjectException e) { raw = null; } String type; if (mode == org.eclipse.jgit.lib.FileMode.SYMLINK) { type = X_GIT_SYMLINK; } else { type = registry.getMimeType(path, raw).toString(); type = resolveContentType(project, path, FileMode.FILE, type); } return asBinaryResult(raw, obj).setContentType(type).base64(); } }
From source file:com.google.gerrit.server.change.FileContentUtil.java
License:Apache License
public BinaryResult downloadContent(ProjectState project, ObjectId revstr, String path, @Nullable String suffix) throws ResourceNotFoundException, IOException { suffix = Strings.emptyToNull(CharMatcher.inRange('a', 'z').retainFrom(Strings.nullToEmpty(suffix))); try (Repository repo = openRepository(project); RevWalk rw = new RevWalk(repo)) { RevCommit commit = rw.parseCommit(revstr); ObjectReader reader = rw.getObjectReader(); TreeWalk tw = TreeWalk.forPath(reader, path, commit.getTree()); if (tw == null) { throw new ResourceNotFoundException(); }//from www . ja va 2 s . com int mode = tw.getFileMode(0).getObjectType(); if (mode != Constants.OBJ_BLOB) { throw new ResourceNotFoundException(); } ObjectId id = tw.getObjectId(0); ObjectLoader obj = repo.open(id, OBJ_BLOB); byte[] raw; try { raw = obj.getCachedBytes(MAX_SIZE); } catch (LargeObjectException e) { raw = null; } MimeType contentType = registry.getMimeType(path, raw); return registry.isSafeInline(contentType) ? wrapBlob(path, obj, raw, contentType, suffix) : zipBlob(path, obj, commit, suffix); } }
From source file:com.google.gerrit.server.edit.ChangeEditModifier.java
License:Apache License
private static ObjectId writeNewTree(TreeOperation op, RevWalk rw, ObjectInserter ins, RevCommit prevEdit, ObjectReader reader, String fileName, @Nullable String newFile, @Nullable final ObjectId content) throws IOException { DirCache newTree = readTree(reader, prevEdit); DirCacheEditor dce = newTree.editor(); switch (op) { case DELETE_ENTRY: dce.add(new DeletePath(fileName)); break;/* w ww . j av a 2s .c o m*/ case RENAME_ENTRY: rw.parseHeaders(prevEdit); TreeWalk tw = TreeWalk.forPath(rw.getObjectReader(), fileName, prevEdit.getTree()); if (tw != null) { dce.add(new DeletePath(fileName)); addFileToCommit(newFile, dce, tw); } break; case CHANGE_ENTRY: checkNotNull(content, "new content required"); dce.add(new PathEdit(fileName) { @Override public void apply(DirCacheEntry ent) { if (ent.getRawMode() == 0) { ent.setFileMode(FileMode.REGULAR_FILE); } ent.setObjectId(content); } }); break; case RESTORE_ENTRY: if (prevEdit.getParentCount() == 0) { dce.add(new DeletePath(fileName)); break; } RevCommit base = prevEdit.getParent(0); rw.parseHeaders(base); tw = TreeWalk.forPath(rw.getObjectReader(), fileName, base.getTree()); if (tw == null) { dce.add(new DeletePath(fileName)); break; } addFileToCommit(fileName, dce, tw); break; } dce.finish(); return newTree.writeTree(ins); }
From source file:com.google.gerrit.server.edit.tree.RenameFileModification.java
License:Apache License
@Override public List<DirCacheEditor.PathEdit> getPathEdits(Repository repository, RevCommit baseCommit) throws IOException { try (RevWalk revWalk = new RevWalk(repository)) { revWalk.parseHeaders(baseCommit); try (TreeWalk treeWalk = TreeWalk.forPath(revWalk.getObjectReader(), currentFilePath, baseCommit.getTree())) { if (treeWalk == null) { return Collections.emptyList(); }//from ww w .j a va 2 s.c om DirCacheEditor.DeletePath deletePathEdit = new DirCacheEditor.DeletePath(currentFilePath); AddPath addPathEdit = new AddPath(newFilePath, treeWalk.getFileMode(0), treeWalk.getObjectId(0)); return Arrays.asList(deletePathEdit, addPathEdit); } } }
From source file:com.google.gerrit.server.edit.tree.RestoreFileModification.java
License:Apache License
@Override public List<DirCacheEditor.PathEdit> getPathEdits(Repository repository, RevCommit baseCommit) throws IOException { if (baseCommit.getParentCount() == 0) { DirCacheEditor.DeletePath deletePath = new DirCacheEditor.DeletePath(filePath); return Collections.singletonList(deletePath); }//from w ww .j a va 2s . c o m RevCommit base = baseCommit.getParent(0); try (RevWalk revWalk = new RevWalk(repository)) { revWalk.parseHeaders(base); try (TreeWalk treeWalk = TreeWalk.forPath(revWalk.getObjectReader(), filePath, base.getTree())) { if (treeWalk == null) { DirCacheEditor.DeletePath deletePath = new DirCacheEditor.DeletePath(filePath); return Collections.singletonList(deletePath); } AddPath addPath = new AddPath(filePath, treeWalk.getFileMode(0), treeWalk.getObjectId(0)); return Collections.singletonList(addPath); } } }