Java Code Examples for org.eclipse.jgit.treewalk.TreeWalk#next()
The following examples show how to use
org.eclipse.jgit.treewalk.TreeWalk#next() .
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: JGitAPIImpl.java From git-client-plugin with MIT License | 6 votes |
/** {@inheritDoc} */ @Deprecated @Override public List<IndexEntry> lsTree(String treeIsh, boolean recursive) throws GitException, InterruptedException { try (Repository repo = getRepository(); ObjectReader or = repo.newObjectReader(); RevWalk w = new RevWalk(or)) { TreeWalk tree = new TreeWalk(or); tree.addTree(w.parseTree(repo.resolve(treeIsh))); tree.setRecursive(recursive); List<IndexEntry> r = new ArrayList<>(); while (tree.next()) { RevObject rev = w.parseAny(tree.getObjectId(0)); r.add(new IndexEntry( String.format("%06o", tree.getRawMode(0)), typeString(rev.getType()), tree.getObjectId(0).name(), tree.getNameString())); } return r; } catch (IOException e) { throw new GitException(e); } }
Example 2
Source File: GitConnector.java From compiler with Apache License 2.0 | 6 votes |
public List<String> getSnapshot(String commit) { ArrayList<String> snapshot = new ArrayList<String>(); TreeWalk tw = new TreeWalk(repository); tw.reset(); try { RevCommit rc = revwalk.parseCommit(repository.resolve(commit)); tw.addTree(rc.getTree()); tw.setRecursive(true); while (tw.next()) { if (!tw.isSubtree()) { String path = tw.getPathString(); snapshot.add(path); } } } catch (IOException e) { System.err.println(e.getMessage()); } tw.close(); return snapshot; }
Example 3
Source File: PGA.java From coming with MIT License | 6 votes |
private void obtainDiff(Repository repository, RevCommit commit, List<String> paths) throws IOException, GitAPIException { // 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 TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(tree); treeWalk.setRecursive(true); for (String path : paths) { String filePath = SIVA_COMMITS_DIR + commit.getName() + "/" + path; File file = new File(filePath); if (!file.exists()) { treeWalk.setFilter(PathFilter.create(path)); if (!treeWalk.next()) { throw new IllegalStateException("Did not find expected file '" + path + "'"); } ObjectId objectId = treeWalk.getObjectId(0); ObjectLoader loader = repository.open(objectId); // and then one can the loader to read the file // loader.copyTo(System.out); loader.copyTo(FileUtils.openOutputStream(file)); } } }
Example 4
Source File: GITStatusCrawler.java From celerio with Apache License 2.0 | 6 votes |
public static SCMStatus doStatus(File baseDir) throws RuntimeException { try { Map<String, Boolean> map = new HashMap<String, Boolean>(); FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(new File(baseDir, ".git")).build(); TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(getTree(repository)); treeWalk.setRecursive(true); while (treeWalk.next()) { map.put(treeWalk.getPathString(), Boolean.TRUE); } log.info("-----------------------------------------------------------------------------------------------"); log.info("PROJECT IS UNDER GIT: Files tracked by git ({}) won't be overwritten/deleted by Celerio", map.size()); log.info("-----------------------------------------------------------------------------------------------"); return new SCMStatus(map); } catch (IOException ioe) { throw new RuntimeException(ioe); } }
Example 5
Source File: GitCommit.java From Getaviz with Apache License 2.0 | 6 votes |
@Override public Map<String, VersionedFile> getFiles(Iterable<String> filters) throws FilesNotAvailableException { Map<String, VersionedFile> returnable = new HashMap<String, VersionedFile>(); IterablePatternMatcher matcher = new IterablePatternMatcher(); Iterable<Pattern> patterns = matcher.transformToPattern(filters); try { RevTree tree = commit.getTree(); TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(tree); treeWalk.setRecursive(true); while (treeWalk.next()) { String currentFilePath = treeWalk.getPathString(); if (matcher.isIncluded(patterns, currentFilePath)) { returnable.put(currentFilePath, readToByteArray(treeWalk)); } } } catch (IOException e) { throw new FilesNotAvailableException(e); } return returnable; }
Example 6
Source File: WalkCommitTreeAdapter.java From coderadar with MIT License | 5 votes |
@Override public void walkCommitTree( String projectRoot, String name, WalkTreeCommandInterface commandInterface) throws UnableToWalkCommitTreeException { try { Git git = Git.open(new File(projectRoot)); ObjectId commitId = git.getRepository().resolve(name); RevWalk walk = new RevWalk(git.getRepository()); RevCommit commit = walk.parseCommit(commitId); RevTree tree = commit.getTree(); TreeWalk treeWalk = new TreeWalk(git.getRepository()); treeWalk.addTree(tree); treeWalk.setRecursive(true); while (treeWalk.next()) { if (!treeWalk.getPathString().endsWith(".java") || treeWalk.getPathString().contains("build") || treeWalk.getPathString().contains("out") || treeWalk.getPathString().contains("classes") || treeWalk.getPathString().contains("node_modules") || treeWalk.getPathString().contains("test")) { continue; } commandInterface.walkMethod(treeWalk.getPathString()); } git.close(); } catch (IOException e) { throw new UnableToWalkCommitTreeException(e.getMessage()); } }
Example 7
Source File: JGitHelper.java From go-plugins with Apache License 2.0 | 5 votes |
private Revision getRevisionObj(Repository repository, RevCommit commit) throws IOException { String commitSHA = commit.getName(); Date commitTime = commit.getAuthorIdent().getWhen(); String comment = commit.getFullMessage().trim(); String user = commit.getAuthorIdent().getName(); String emailId = commit.getAuthorIdent().getEmailAddress(); List<ModifiedFile> modifiedFiles = new ArrayList<ModifiedFile>(); if (commit.getParentCount() == 0) { TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(commit.getTree()); treeWalk.setRecursive(false); while (treeWalk.next()) { modifiedFiles.add(new ModifiedFile(treeWalk.getPathString(), "added")); } } else { RevWalk rw = new RevWalk(repository); RevCommit parent = rw.parseCommit(commit.getParent(0).getId()); DiffFormatter diffFormatter = new DiffFormatter(DisabledOutputStream.INSTANCE); diffFormatter.setRepository(repository); diffFormatter.setDiffComparator(RawTextComparator.DEFAULT); diffFormatter.setDetectRenames(true); List<DiffEntry> diffEntries = diffFormatter.scan(parent.getTree(), commit.getTree()); for (DiffEntry diffEntry : diffEntries) { modifiedFiles.add(new ModifiedFile(diffEntry.getNewPath(), getAction(diffEntry.getChangeType().name()))); } } return new Revision(commitSHA, commitTime, comment, user, emailId, modifiedFiles); }
Example 8
Source File: SMAGit.java From salesforce-migration-assistant with MIT License | 5 votes |
/** * Replicates ls-tree for the current commit. * * @return Map containing the full path and the data for all items in the repository. * @throws IOException */ public Map<String, byte[]> getAllMetadata() throws Exception { Map<String, byte[]> contents = new HashMap<String, byte[]>(); ObjectReader reader = repository.newObjectReader(); ObjectId commitId = repository.resolve(curCommit); RevWalk revWalk = new RevWalk(reader); RevCommit commit = revWalk.parseCommit(commitId); RevTree tree = commit.getTree(); TreeWalk treeWalk = new TreeWalk(reader); treeWalk.addTree(tree); treeWalk.setRecursive(false); while (treeWalk.next()) { if (treeWalk.isSubtree()) { treeWalk.enterSubtree(); } else { String member = treeWalk.getPathString(); if (member.contains(SOURCEDIR)) { byte[] data = getBlob(member, curCommit); contents.put(member, data); } } } reader.release(); return contents; }
Example 9
Source File: GfsTreeWalkTest.java From ParallelGit with Apache License 2.0 | 5 votes |
@Nonnull private TreeWalk forPath(String path) throws IOException { TreeWalk tw = prepareTreeWalk(false); PathFilter filter = PathFilter.create(path.charAt(0) == '/' ? path.substring(1) : path); tw.setFilter(filter); tw.setRecursive(false); while(tw.next()) { if(filter.isDone(tw)) return tw; if(tw.isSubtree()) tw.enterSubtree(); } throw new IllegalStateException(); }
Example 10
Source File: GfsTreeWalkTest.java From ParallelGit with Apache License 2.0 | 5 votes |
@Nonnull private String[] toArrayWithLeadingSlash(TreeWalk tw) throws IOException { List<String> list = new ArrayList<>(); while(tw.next()) list.add("/" + tw.getPathString()); String[] ret = new String[list.size()]; return list.toArray(ret); }
Example 11
Source File: GitCommit.java From compiler with Apache License 2.0 | 5 votes |
void updateChangedFiles(RevCommit rc) { if (rc.getParentCount() == 0) { TreeWalk tw = new TreeWalk(repository); tw.reset(); try { tw.addTree(rc.getTree()); tw.setRecursive(true); while (tw.next()) { if (!tw.isSubtree()) { String path = tw.getPathString(); getChangedFile(path, ChangeKind.ADDED); filePathGitObjectIds.put(path, tw.getObjectId(0)); } } } catch (IOException e) { if (debug) System.err.println(e.getMessage()); } tw.close(); } else { parentIndices = new int[rc.getParentCount()]; for (int i = 0; i < rc.getParentCount(); i++) { int parentIndex = connector.revisionMap.get(rc.getParent(i).getName()); // merged commit in git only store diffs between the first parent and the child if (i == 0) updateChangedFiles(rc.getParent(i), parentIndex, rc); parentIndices[i] = parentIndex; } } }
Example 12
Source File: GitConnector.java From compiler with Apache License 2.0 | 5 votes |
@Override public List<ChangedFile> buildHeadSnapshot() { final List<ChangedFile> snapshot = new ArrayList<ChangedFile>(); TreeWalk tw = new TreeWalk(repository); tw.reset(); try { RevCommit rc = revwalk.parseCommit(repository.resolve(Constants.HEAD)); tw.addTree(rc.getTree()); tw.setRecursive(true); while (tw.next()) { if (!tw.isSubtree()) { String path = tw.getPathString(); ChangedFile.Builder cfb = ChangedFile.newBuilder(); cfb.setChange(ChangeKind.UNKNOWN); cfb.setName(path); cfb.setKind(FileKind.OTHER); cfb.setKey(0); cfb.setAst(false); GitCommit gc = new GitCommit(this, repository, revwalk, projectName); gc.filePathGitObjectIds.put(path, tw.getObjectId(0)); gc.processChangeFile(cfb); snapshot.add(cfb.build()); } } } catch (Exception e) { System.err.println(e.getMessage()); } tw.close(); return snapshot; }
Example 13
Source File: GfsTreeWalkTest.java From ParallelGit with Apache License 2.0 | 5 votes |
@Nonnull private TreeWalk forPath(String path) throws IOException { TreeWalk tw = prepareTreeWalk(false); PathFilter filter = PathFilter.create(path.charAt(0) == '/' ? path.substring(1) : path); tw.setFilter(filter); tw.setRecursive(false); while(tw.next()) { if(filter.isDone(tw)) return tw; if(tw.isSubtree()) tw.enterSubtree(); } throw new IllegalStateException(); }
Example 14
Source File: GfsTreeWalkTest.java From ParallelGit with Apache License 2.0 | 5 votes |
@Nonnull private String[] toArrayWithLeadingSlash(TreeWalk tw) throws IOException { List<String> list = new ArrayList<>(); while(tw.next()) list.add("/" + tw.getPathString()); String[] ret = new String[list.size()]; return list.toArray(ret); }
Example 15
Source File: DifferentFiles.java From gitflow-incremental-builder with MIT License | 5 votes |
private Set<Path> getDiff(TreeWalk treeWalk, Path gitDir) throws IOException { final Set<Path> paths = new HashSet<>(); while (treeWalk.next()) { Path path = Paths.get(treeWalk.getPathString()).normalize(); if (pathIncluded(path)) { paths.add(gitDir.resolve(path)); } } return paths; }
Example 16
Source File: CherryPickCommand.java From netbeans with Apache License 2.0 | 5 votes |
@Override public ThreeWayMerger newMerger (Repository db, boolean inCore) { return new RecursiveMerger(db, inCore) { protected boolean mergeTreeWalk (TreeWalk treeWalk, boolean ignoreConflicts) throws IOException { boolean ok = true; boolean hasWorkingTreeIterator = tw.getTreeCount() > T_FILE; boolean hasAttributeNodeProvider = treeWalk.getAttributesNodeProvider() != null; while (treeWalk.next()) { if (!processEntry( treeWalk.getTree(T_BASE, CanonicalTreeParser.class), treeWalk.getTree(T_OURS, CanonicalTreeParser.class), treeWalk.getTree(T_THEIRS, CanonicalTreeParser.class), treeWalk.getTree(T_INDEX, DirCacheBuildIterator.class), hasWorkingTreeIterator ? treeWalk.getTree(T_FILE, WorkingTreeIterator.class) : null, ignoreConflicts, hasAttributeNodeProvider ? treeWalk.getAttributes() : NO_ATTRIBUTES )) { ok = false; } if (treeWalk.isSubtree() && enterSubtree) { treeWalk.enterSubtree(); } } if (!ok) { cleanUp(); } return ok; } }; }
Example 17
Source File: AddTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testLineEndingsWindows () throws Exception { if (!isWindows()) { return; } // lets turn autocrlf on StoredConfig cfg = repository.getConfig(); cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOCRLF, "true"); cfg.save(); File f = new File(workDir, "f"); write(f, "a\r\nb\r\n"); File[] roots = new File[] { f }; GitClient client = getClient(workDir); runExternally(workDir, Arrays.asList("git.cmd", "add", "f")); DirCacheEntry e1 = repository.readDirCache().getEntry("f"); client.add(roots, NULL_PROGRESS_MONITOR); DirCacheEntry e2 = repository.readDirCache().getEntry("f"); assertStatus(client.getStatus(roots, NULL_PROGRESS_MONITOR), workDir, f, true, Status.STATUS_ADDED, Status.STATUS_NORMAL, Status.STATUS_ADDED, false); List<String> res = runExternally(workDir, Arrays.asList("git.cmd", "status", "-s")); assertEquals(Arrays.asList("A f"), res); assertEquals(e1.getFileMode(), e2.getFileMode()); assertEquals(e1.getPathString(), e2.getPathString()); assertEquals(e1.getRawMode(), e2.getRawMode()); assertEquals(e1.getStage(), e2.getStage()); assertEquals(e1.getLength(), e2.getLength()); assertEquals(e1.getObjectId(), e2.getObjectId()); write(f, "a\nb\n"); res = runExternally(workDir, Arrays.asList("git.cmd", "status", "-s")); assertEquals(Arrays.asList("AM f"), res); assertStatus(client.getStatus(roots, NULL_PROGRESS_MONITOR), workDir, f, true, Status.STATUS_ADDED, Status.STATUS_MODIFIED, Status.STATUS_ADDED, false); res = runExternally(workDir, Arrays.asList("git.cmd", "commit", "-m", "gugu")); res = runExternally(workDir, Arrays.asList("git.cmd", "checkout", "--", "f")); RevCommit commit = Utils.findCommit(repository, "HEAD"); TreeWalk walk = new TreeWalk(repository); walk.reset(); walk.addTree(commit.getTree()); walk.setFilter(PathFilter.create("f")); walk.setRecursive(true); walk.next(); assertEquals("f", walk.getPathString()); ObjectLoader loader = repository.getObjectDatabase().open(walk.getObjectId(0)); assertEquals(4, loader.getSize()); assertEquals("a\nb\n", new String(loader.getBytes())); assertEquals(e1.getObjectId(), walk.getObjectId(0)); res = runExternally(workDir, Arrays.asList("git.cmd", "status", "-s")); assertEquals(0, res.size()); assertStatus(client.getStatus(roots, NULL_PROGRESS_MONITOR), workDir, f, true, Status.STATUS_NORMAL, Status.STATUS_NORMAL, Status.STATUS_NORMAL, false); }
Example 18
Source File: AddTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testAddMixedLineEndings () throws Exception { File f = new File(workDir, "f"); String content = ""; for (int i = 0; i < 10000; ++i) { content += i + "\r\n"; } write(f, content); File[] files = new File[] { f }; GitClient client = getClient(workDir); client.add(files, NULL_PROGRESS_MONITOR); client.commit(files, "commit", null, null, NULL_PROGRESS_MONITOR); Map<File, GitStatus> statuses = client.getStatus(files, NULL_PROGRESS_MONITOR); assertEquals(1, statuses.size()); assertStatus(statuses, workDir, f, true, Status.STATUS_NORMAL, Status.STATUS_NORMAL, Status.STATUS_NORMAL, false); // lets turn autocrlf on StoredConfig cfg = repository.getConfig(); cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOCRLF, "true"); cfg.save(); // when this starts failing, remove the work around ObjectInserter inserter = repository.newObjectInserter(); TreeWalk treeWalk = new TreeWalk(repository); treeWalk.setFilter(PathFilterGroup.createFromStrings("f")); treeWalk.setRecursive(true); treeWalk.reset(); treeWalk.addTree(new FileTreeIterator(repository)); while (treeWalk.next()) { String path = treeWalk.getPathString(); assertEquals("f", path); WorkingTreeIterator fit = treeWalk.getTree(0, WorkingTreeIterator.class); try (InputStream in = fit.openEntryStream()) { inserter.insert(Constants.OBJ_BLOB, fit.getEntryLength(), in); fail("this should fail, remove the work around"); } catch (EOFException ex) { assertEquals("Input did not match supplied length. 10.000 bytes are missing.", ex.getMessage()); } finally { inserter.close(); } break; } // no err should occur write(f, content + "hello"); statuses = client.getStatus(files, NULL_PROGRESS_MONITOR); assertEquals(1, statuses.size()); assertStatus(statuses, workDir, f, true, Status.STATUS_NORMAL, Status.STATUS_MODIFIED, Status.STATUS_MODIFIED, false); client.add(files, NULL_PROGRESS_MONITOR); statuses = client.getStatus(files, NULL_PROGRESS_MONITOR); assertEquals(1, statuses.size()); assertStatus(statuses, workDir, f, true, Status.STATUS_MODIFIED, Status.STATUS_NORMAL, Status.STATUS_MODIFIED, false); client.commit(files, "message", null, null, NULL_PROGRESS_MONITOR); statuses = client.getStatus(files, NULL_PROGRESS_MONITOR); assertEquals(1, statuses.size()); assertStatus(statuses, workDir, f, true, Status.STATUS_NORMAL, Status.STATUS_NORMAL, Status.STATUS_NORMAL, false); }
Example 19
Source File: GitProctorCore.java From proctor with Apache License 2.0 | 4 votes |
@Override @Nullable public <C> C getFileContents( final Class<C> c, final String[] path, @Nullable final C defaultValue, final String revision ) throws StoreException.ReadException, JsonProcessingException { try { if (!ObjectId.isId(revision)) { throw new StoreException.ReadException("Malformed id " + revision); } final ObjectId blobOrCommitId = ObjectId.fromString(revision); final ObjectLoader loader = git.getRepository().open(blobOrCommitId); if (loader.getType() == Constants.OBJ_COMMIT) { // look up the file at this revision final RevCommit commit = RevCommit.parse(loader.getCachedBytes()); final TreeWalk treeWalk2 = new TreeWalk(git.getRepository()); treeWalk2.addTree(commit.getTree()); treeWalk2.setRecursive(true); final String joinedPath = String.join("/", path); treeWalk2.setFilter(PathFilter.create(joinedPath)); if (!treeWalk2.next()) { // it did not find expected file `joinPath` so return default value return defaultValue; } final ObjectId blobId = treeWalk2.getObjectId(0); return getFileContents(c, blobId); } else if (loader.getType() == Constants.OBJ_BLOB) { return getFileContents(c, blobOrCommitId); } else { throw new StoreException.ReadException("Invalid Object Type " + loader.getType() + " for id " + revision); } } catch (final IOException e) { throw new StoreException.ReadException(e); } }
Example 20
Source File: GitProctorCore.java From proctor with Apache License 2.0 | 4 votes |
@Override public TestVersionResult determineVersions(final String fetchRevision) throws StoreException.ReadException { try { final RevWalk walk = new RevWalk(git.getRepository()); final ObjectId commitId = ObjectId.fromString(fetchRevision); final RevCommit headTree = walk.parseCommit(commitId); final RevTree tree = headTree.getTree(); // now use a TreeWalk to iterate over all files in the Tree recursively // you can set Filters to narrow down the results if needed final TreeWalk treeWalk = new TreeWalk(git.getRepository()); treeWalk.addTree(tree); treeWalk.setFilter(AndTreeFilter .create(PathFilter.create(testDefinitionsDirectory), PathSuffixFilter.create("definition.json"))); treeWalk.setRecursive(true); final List<TestVersionResult.Test> tests = Lists.newArrayList(); while (treeWalk.next()) { final ObjectId id = treeWalk.getObjectId(0); // final RevTree revTree = walk.lookupTree(id); final String path = treeWalk.getPathString(); final String[] pieces = path.split("/"); final String testname = pieces[pieces.length - 2]; // tree / parent directory name // testname, blobid pair // note this is the blobid hash - not a commit hash // RevTree.id and RevBlob.id tests.add(new TestVersionResult.Test(testname, id.name())); } walk.dispose(); return new TestVersionResult( tests, new Date(Long.valueOf(headTree.getCommitTime()) * 1000 /* convert seconds to milliseconds */), determineAuthorId(headTree), headTree.toObjectId().getName(), headTree.getFullMessage() ); } catch (final IOException e) { throw new StoreException.ReadException(e); } }