Java Code Examples for org.eclipse.jgit.treewalk.TreeWalk#forPath()
The following examples show how to use
org.eclipse.jgit.treewalk.TreeWalk#forPath() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GitContentRepository.java From studio with GNU General Public License v3.0 | 6 votes |
@Override public long getContentSize(final String site, final String path) { Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX); try { RevTree tree = helper.getTreeForLastCommit(repo); try (TreeWalk tw = TreeWalk.forPath(repo, helper.getGitPath(path), tree)) { if (tw != null && tw.getObjectId(0) != null) { ObjectId id = tw.getObjectId(0); ObjectLoader objectLoader = repo.open(id); return objectLoader.getSize(); } } } catch (IOException e) { logger.error("Error while getting content for file at site: " + site + " path: " + path, e); } return -1L; }
Example 2
Source File: GitUtils.java From onedev with MIT License | 6 votes |
@Nullable public static List<String> readLines(Repository repository, RevCommit commit, String path, WhitespaceOption whitespaceOption) { try { TreeWalk treeWalk = TreeWalk.forPath(repository, path, commit.getTree()); if (treeWalk != null) { ObjectId blobId = treeWalk.getObjectId(0); ObjectReader objectReader = treeWalk.getObjectReader(); BlobIdent blobIdent = new BlobIdent(commit.name(), path, FileMode.REGULAR_FILE.getBits()); Blob blob = new Blob(blobIdent, blobId, objectReader); List<String> normalizedLines = new ArrayList<>(); for (String line: Preconditions.checkNotNull(blob.getText()).getLines()) { normalizedLines.add(whitespaceOption.process(line)); } return normalizedLines; } else { return null; } } catch (IOException e) { throw new RuntimeException(e); } }
Example 3
Source File: Project.java From onedev with MIT License | 6 votes |
public int getMode(String revision, @Nullable String path) { if (path != null) { RevCommit commit = getRevCommit(revision, true); try { TreeWalk treeWalk = TreeWalk.forPath(getRepository(), path, commit.getTree()); if (treeWalk != null) { return treeWalk.getRawMode(0); } else { throw new ObjectNotFoundException("Unable to find blob path '" + path + "' in revision '" + revision + "'"); } } catch (IOException e) { throw new RuntimeException(e); } } else { return FileMode.TREE.getBits(); } }
Example 4
Source File: GitUtilsTest.java From onedev with MIT License | 6 votes |
@Test public void testMergeWithContentConflict() throws Exception { addFileAndCommit("initial", "", "initial"); git.checkout().setCreateBranch(true).setName("dev").call(); addFileAndCommit("dev1", "", "dev1"); addFileAndCommit("conflict", "1", "dev2"); git.checkout().setName("master").call(); addFileAndCommit("master1", "", "master1"); addFileAndCommit("conflict", "2", "master2"); assertNull(GitUtils.merge(git.getRepository(), git.getRepository().resolve("master"), git.getRepository().resolve("dev"), false, user, user, "merge commit", false)); ObjectId mergeCommitId = GitUtils.merge(git.getRepository(), git.getRepository().resolve("master"), git.getRepository().resolve("dev"), false, user, user, "merge commit", true); assertNotNull(mergeCommitId); try ( RevWalk revWalk = new RevWalk(git.getRepository())) { RevCommit mergeCommit = revWalk.parseCommit(mergeCommitId); TreeWalk treeWalk = TreeWalk.forPath(git.getRepository(), "conflict", mergeCommit.getTree()); BlobIdent blobIdent = new BlobIdent(mergeCommit.name(), "conflict", FileMode.REGULAR_FILE.getBits()); Blob blob = new Blob(blobIdent, treeWalk.getObjectId(0), treeWalk.getObjectReader()); assertEquals("2", blob.getText().getContent()); } }
Example 5
Source File: GitUtilsTest.java From onedev with MIT License | 6 votes |
@Test public void testMergeWithDeletionAndModificationConflict() throws Exception { addFileAndCommit("file", "", "initial commit"); git.checkout().setCreateBranch(true).setName("dev").call(); removeFileAndCommit("file", "remove file"); git.checkout().setName("master").call(); addFileAndCommit("file", "1", "master"); assertNull(GitUtils.merge(git.getRepository(), git.getRepository().resolve("master"), git.getRepository().resolve("dev"), false, user, user, "merge commit", false)); ObjectId mergeCommitId = GitUtils.merge(git.getRepository(), git.getRepository().resolve("master"), git.getRepository().resolve("dev"), false, user, user, "merge commit", true); assertNotNull(mergeCommitId); try ( RevWalk revWalk = new RevWalk(git.getRepository())) { RevCommit mergeCommit = revWalk.parseCommit(mergeCommitId); TreeWalk treeWalk = TreeWalk.forPath(git.getRepository(), "file", mergeCommit.getTree()); BlobIdent blobIdent = new BlobIdent(mergeCommit.name(), "file", FileMode.REGULAR_FILE.getBits()); Blob blob = new Blob(blobIdent, treeWalk.getObjectId(0), treeWalk.getObjectReader()); assertEquals("1", blob.getText().getContent()); } }
Example 6
Source File: GitUtils.java From blueocean-plugin with MIT License | 6 votes |
@SuppressFBWarnings(value={"RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE"}, justification="JDK11 produces different bytecode - https://github.com/spotbugs/spotbugs/issues/756") static byte[] readFile(Repository repository, String ref, String filePath) { try (ObjectReader reader = repository.newObjectReader()) { ObjectId branchRef = repository.resolve(ref); // repository.exactRef(ref); if (branchRef != null) { // for empty repositories, branchRef may be null RevWalk revWalk = new RevWalk(repository); RevCommit commit = revWalk.parseCommit(branchRef); // and using commit's tree find the path RevTree tree = commit.getTree(); TreeWalk treewalk = TreeWalk.forPath(reader, filePath, tree); if (treewalk != null) { // use the blob id to read the file's data return reader.open(treewalk.getObjectId(0)).getBytes(); } } } catch (IOException ex) { throw new RuntimeException(ex); } return null; }
Example 7
Source File: GitUtilsTest.java From onedev with MIT License | 5 votes |
@Test public void testMergeWithLinkAndLinkConflict() throws Exception { File tempDir; tempDir = FileUtils.createTempDir(); try (InputStream is = Resources.getResource(GitUtilsTest.class, "git-conflict-link-link.zip").openStream()) { ZipUtils.unzip(is, tempDir); try (Git git = Git.open(tempDir)) { ObjectId mergeCommitId; mergeCommitId = GitUtils.merge(git.getRepository(), git.getRepository().resolve("master"), git.getRepository().resolve("dev"), false, user, user, "merge commit", false); assertNull(mergeCommitId); mergeCommitId = GitUtils.merge(git.getRepository(), git.getRepository().resolve("master"), git.getRepository().resolve("dev"), false, user, user, "merge commit", true); assertNotNull(mergeCommitId); try ( RevWalk revWalk = new RevWalk(git.getRepository())) { RevCommit mergeCommit = revWalk.parseCommit(mergeCommitId); TreeWalk treeWalk = TreeWalk.forPath(git.getRepository(), "lib", mergeCommit.getTree()); assertTrue(treeWalk != null && treeWalk.getFileMode(0) == FileMode.GITLINK); treeWalk = TreeWalk.forPath(git.getRepository(), ".gitmodules", mergeCommit.getTree()); BlobIdent blobIdent = new BlobIdent(mergeCommit.name(), ".gitmodules", FileMode.GITLINK.getBits()); Blob blob = new Blob(blobIdent, treeWalk.getObjectId(0), treeWalk.getObjectReader()); assertTrue(blob.getText().getContent().trim().endsWith("/home/robin/temp/lib")); } } } finally { deleteDir(tempDir, 3); } }
Example 8
Source File: LayoutHelper.java From git-as-svn with GNU General Public License v2.0 | 5 votes |
@NotNull static String loadRepositoryId(@NotNull ObjectReader objectReader, ObjectId commit) throws IOException { RevWalk revWalk = new RevWalk(objectReader); TreeWalk treeWalk = TreeWalk.forPath(objectReader, ENTRY_UUID, revWalk.parseCommit(commit).getTree()); if (treeWalk != null) { return GitRepository.loadContent(objectReader, treeWalk.getObjectId(0)); } throw new FileNotFoundException(ENTRY_UUID); }
Example 9
Source File: SMAGit.java From salesforce-migration-assistant with MIT License | 5 votes |
/** * Returns the blob information for the file at the specified path and commit * * @param repoItem * @param commit * @return * @throws Exception */ public byte[] getBlob(String repoItem, String commit) throws Exception { byte[] data; String parentPath = repository.getDirectory().getParent(); ObjectId commitId = repository.resolve(commit); ObjectReader reader = repository.newObjectReader(); RevWalk revWalk = new RevWalk(reader); RevCommit revCommit = revWalk.parseCommit(commitId); RevTree tree = revCommit.getTree(); TreeWalk treeWalk = TreeWalk.forPath(reader, repoItem, tree); if (treeWalk != null) { data = reader.open(treeWalk.getObjectId(0)).getBytes(); } else { throw new IllegalStateException("Did not find expected file '" + repoItem + "'"); } reader.release(); return data; }
Example 10
Source File: VersionControlGit.java From mdw with Apache License 2.0 | 5 votes |
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 { return null; } } }
Example 11
Source File: Commit.java From orion.server with Eclipse Public License 1.0 | 5 votes |
/** * Return body of the commit * * @return body of the commit as an Object Stream * @throws IOException * when reading the object failed */ public ObjectStream toObjectStream() throws IOException { final TreeWalk w = TreeWalk.forPath(db, pattern, revCommit.getTree()); if (w == null) { return null; } ObjectId blobId = w.getObjectId(0); return db.open(blobId, Constants.OBJ_BLOB).openStream(); }
Example 12
Source File: GitClient.java From cf-butler with Apache License 2.0 | 5 votes |
public String readFile(Repository repo, String commitId, String filePath) throws IOException { ObjectId oid = repo.resolve(commitId); RevCommit commit = repo.parseCommit(oid); try (TreeWalk walk = TreeWalk.forPath(repo, filePath, commit.getTree())) { if (walk != null) { byte[] bytes = repo.open(walk.getObjectId(0)).getBytes(); return new String(bytes, StandardCharsets.UTF_8); } else { throw new IllegalArgumentException(String.format("No file found for commitId=%s and filePath=%s", commitId, filePath)); } } }
Example 13
Source File: Project.java From onedev with MIT License | 5 votes |
public InputStream getInputStream(BlobIdent ident) { try (RevWalk revWalk = new RevWalk(getRepository())) { ObjectId commitId = getObjectId(ident.revision, true); RevTree revTree = revWalk.parseCommit(commitId).getTree(); TreeWalk treeWalk = TreeWalk.forPath(getRepository(), ident.path, revTree); if (treeWalk != null) { ObjectLoader objectLoader = treeWalk.getObjectReader().open(treeWalk.getObjectId(0)); return objectLoader.openStream(); } else { throw new ObjectNotFoundException("Unable to find blob path '" + ident.path + "' in revision '" + ident.revision + "'"); } } catch (IOException e) { throw new RuntimeException(e); } }
Example 14
Source File: TreeUtils.java From ParallelGit with Apache License 2.0 | 4 votes |
@Nullable public static TreeWalk forPath(String path, AnyObjectId tree, ObjectReader reader) throws IOException { return TreeWalk.forPath(reader, normalizeNodePath(path), tree); }
Example 15
Source File: TreeUtils.java From ParallelGit with Apache License 2.0 | 4 votes |
@Nullable public static TreeWalk forPath(String path, AnyObjectId tree, ObjectReader reader) throws IOException { return TreeWalk.forPath(reader, normalizeNodePath(path), tree); }
Example 16
Source File: Project.java From onedev with MIT License | 4 votes |
/** * Read blob content and cache result in repository in case the same blob * content is requested again. * * We made this method thread-safe as we are using ForkJoinPool to calculate * diffs of multiple blob changes concurrently, and this method will be * accessed concurrently in that special case. * * @param blobIdent * ident of the blob * @return * blob of specified blob ident * @throws * ObjectNotFoundException if blob of specified ident can not be found in repository * */ @Nullable public Blob getBlob(BlobIdent blobIdent, boolean mustExist) { Preconditions.checkArgument(blobIdent.revision!=null && blobIdent.path!=null && blobIdent.mode!=null, "Revision, path and mode of ident param should be specified"); Optional<Blob> blob = getBlobCache().get(blobIdent); if (blob == null) { try (RevWalk revWalk = new RevWalk(getRepository())) { ObjectId revId = getObjectId(blobIdent.revision, mustExist); if (revId != null) { RevCommit commit = GitUtils.parseCommit(revWalk, revId); if (commit != null) { RevTree revTree = commit.getTree(); TreeWalk treeWalk = TreeWalk.forPath(getRepository(), blobIdent.path, revTree); if (treeWalk != null) { ObjectId blobId = treeWalk.getObjectId(0); if (blobIdent.isGitLink()) { String url = getSubmodules(blobIdent.revision).get(blobIdent.path); if (url == null) { if (mustExist) throw new ObjectNotFoundException("Unable to find submodule '" + blobIdent.path + "' in .gitmodules"); else blob = Optional.absent(); } else { String hash = blobId.name(); blob = Optional.of(new Blob(blobIdent, blobId, new Submodule(url, hash).toString().getBytes())); } } else if (blobIdent.isTree()) { throw new NotFileException("Path '" + blobIdent.path + "' is a tree"); } else { blob = Optional.of(new Blob(blobIdent, blobId, treeWalk.getObjectReader())); } } } } if (blob == null) { if (mustExist) throw new ObjectNotFoundException("Unable to find blob ident: " + blobIdent); else blob = Optional.absent(); } getBlobCache().put(blobIdent, blob); } catch (IOException e) { throw new RuntimeException(e); } } return blob.orNull(); }
Example 17
Source File: SymbolLinkPanel.java From onedev with MIT License | 4 votes |
@Override protected void onInitialize() { super.onInitialize(); Blob blob = context.getProject().getBlob(context.getBlobIdent(), true); String targetPath = PathUtils.normalizeDots( PathUtils.resolveSibling(context.getBlobIdent().path, blob.getText().getContent())); if (targetPath != null && (targetPath.startsWith("/") || new File(targetPath).isAbsolute())) targetPath = null; BlobIdent targetBlobIdent; if (targetPath != null) { Repository repository = context.getProject().getRepository(); try (RevWalk revWalk = new RevWalk(repository)) { ObjectId commitId = context.getProject().getObjectId(context.getBlobIdent().revision, true); RevTree revTree = revWalk.parseCommit(commitId).getTree(); TreeWalk treeWalk = TreeWalk.forPath(repository, targetPath, revTree); if (treeWalk != null) { targetBlobIdent = new BlobIdent(context.getBlobIdent().revision, targetPath, treeWalk.getRawMode(0)); } else { targetBlobIdent = null; } } catch (IOException e) { throw new RuntimeException(e); } } else { targetBlobIdent = null; } WebMarkupContainer link; if (targetBlobIdent == null) { link = new Link<Void>("link") { @Override public void onClick() { } }; link.setEnabled(false); } else { ProjectBlobPage.State state = new ProjectBlobPage.State(targetBlobIdent); link = new ViewStateAwarePageLink<Void>("link", ProjectBlobPage.class, ProjectBlobPage.paramsOf(context.getProject(), state)); } link.add(new Label("label", blob.getText().getContent())); add(link); }