Java Code Examples for org.eclipse.jgit.treewalk.TreeWalk#addTree()
The following examples show how to use
org.eclipse.jgit.treewalk.TreeWalk#addTree() .
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: RepositoryPGit.java From coming with MIT License | 6 votes |
protected void detectRenames(RevTree revTree) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException { TreeWalk tw = new TreeWalk(repository); tw.setRecursive(true); tw.addTree(revTree); tw.addTree(new FileTreeIterator(repository)); RenameDetector rd = new RenameDetector(repository); rd.addAll(DiffEntry.scan(tw)); List<DiffEntry> lde = rd.compute(/* tw.getObjectReader(), null */); for (DiffEntry de : lde) { if (de.getScore() >= rd.getRenameScore()) { System.out.println("file: " + de.getOldPath() + " copied/moved to: " + de.getNewPath() + " "); } } }
Example 2
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 3
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 4
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 5
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 6
Source File: SubtreeMerger.java From git-merge-repos with Apache License 2.0 | 5 votes |
private void addTrees(Map<SubtreeConfig, RevCommit> parentCommits, TreeWalk treeWalk) throws IOException { for (Map.Entry<SubtreeConfig, RevCommit> entry : parentCommits.entrySet()) { String directory = entry.getKey().getSubtreeDirectory(); RevCommit parentCommit = entry.getValue(); if (".".equals(directory)) { treeWalk.addTree(parentCommit.getTree()); } else { byte[] prefix = directory.getBytes(RawParseUtils.UTF8_CHARSET); CanonicalTreeParser treeParser = new CanonicalTreeParser(prefix, treeWalk.getObjectReader(), parentCommit.getTree()); treeWalk.addTree(treeParser); } } }
Example 7
Source File: RepositoryObjectTreeWalker.java From writelatex-git-bridge with MIT License | 5 votes |
private TreeWalk initTreeWalk( Repository repository, ObjectId objectId ) throws IOException { if (objectId == null) { return null; } RevWalk walk = new RevWalk(repository); TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(walk.parseCommit(objectId).getTree()); treeWalk.setRecursive(true); return treeWalk; }
Example 8
Source File: GitBranch.java From git-as-svn with GNU General Public License v2.0 | 5 votes |
@NotNull private Map<String, String> collectRename(@NotNull GitFile oldTree, @NotNull GitFile newTree) throws IOException { if (!repository.hasRenameDetection()) { return Collections.emptyMap(); } final GitObject<ObjectId> oldTreeId = oldTree.getObjectId(); final GitObject<ObjectId> newTreeId = newTree.getObjectId(); if (oldTreeId == null || newTreeId == null || !Objects.equals(oldTreeId.getRepo(), newTreeId.getRepo())) { return Collections.emptyMap(); } final TreeWalk tw = new TreeWalk(repository.getGit()); tw.setRecursive(true); tw.setFilter(TreeFilter.ANY_DIFF); tw.addTree(oldTreeId.getObject()); tw.addTree(newTreeId.getObject()); final RenameDetector rd = new RenameDetector(repository.getGit()); rd.addAll(DiffEntry.scan(tw)); final Map<String, String> result = new HashMap<>(); for (DiffEntry diff : rd.compute(tw.getObjectReader(), null)) { if (diff.getScore() >= rd.getRenameScore()) { result.put(StringHelper.normalize(diff.getNewPath()), StringHelper.normalize(diff.getOldPath())); } } return result; }
Example 9
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 10
Source File: GfsTreeWalkTest.java From ParallelGit with Apache License 2.0 | 5 votes |
@Nonnull private TreeWalk prepareTreeWalk(boolean recursive) throws IOException { GfsTreeIterator iterator = GfsTreeIterator.iterateRoot(gfs); TreeWalk ret = new TreeWalk(repo); ret.setRecursive(recursive); ret.addTree(iterator); return 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: 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 14
Source File: GitServiceImpl.java From RefactoringMiner with MIT License | 5 votes |
public void fileTreeDiff(Repository repository, RevCommit currentCommit, List<String> javaFilesBefore, List<String> javaFilesCurrent, Map<String, String> renamedFilesHint) throws Exception { if (currentCommit.getParentCount() > 0) { ObjectId oldTree = currentCommit.getParent(0).getTree(); ObjectId newTree = currentCommit.getTree(); final TreeWalk tw = new TreeWalk(repository); tw.setRecursive(true); tw.addTree(oldTree); tw.addTree(newTree); final RenameDetector rd = new RenameDetector(repository); rd.setRenameScore(80); rd.addAll(DiffEntry.scan(tw)); for (DiffEntry diff : rd.compute(tw.getObjectReader(), null)) { ChangeType changeType = diff.getChangeType(); String oldPath = diff.getOldPath(); String newPath = diff.getNewPath(); if (changeType != ChangeType.ADD) { if (isJavafile(oldPath)) { javaFilesBefore.add(oldPath); } } if (changeType != ChangeType.DELETE) { if (isJavafile(newPath)) { javaFilesCurrent.add(newPath); } } if (changeType == ChangeType.RENAME && diff.getScore() >= rd.getRenameScore()) { if (isJavafile(oldPath) && isJavafile(newPath)) { renamedFilesHint.put(oldPath, newPath); } } } } }
Example 15
Source File: DifferentFiles.java From gitflow-incremental-builder with MIT License | 5 votes |
private Set<Path> getBranchDiff() throws IOException { RevCommit base = getBranchCommit(configuration.baseBranch); final TreeWalk treeWalk = new TreeWalk(git.getRepository()); try { treeWalk.addTree(base.getTree()); treeWalk.addTree(resolveReference(base).getTree()); treeWalk.setFilter(TreeFilter.ANY_DIFF); treeWalk.setRecursive(true); return getDiff(treeWalk, workTree); } finally { treeWalk.close(); } }
Example 16
Source File: ExportCommitCommand.java From netbeans with Apache License 2.0 | 5 votes |
@Override protected void run() throws GitException { Repository repository = getRepository(); String workTreePath = repository.getWorkTree().getAbsolutePath(); RevCommit commit = Utils.findCommit(repository, revisionStr); if (commit.getParentCount() > 1) { throw new GitException("Unable to export a merge commit"); } try (DiffFormatter formatter = new DiffFormatter(out)) { out.write(Constants.encode(formatCommitInfo(commit))); formatter.setRepository(repository); List<DiffEntry> diffEntries; if (commit.getParentCount() > 0) { formatter.setDetectRenames(true); diffEntries = formatter.scan(commit.getParent(0), commit); } else { TreeWalk walk = new TreeWalk(repository); walk.reset(); walk.setRecursive(true); walk.addTree(new EmptyTreeIterator()); walk.addTree(commit.getTree()); walk.setFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, PathFilter.ANY_DIFF)); diffEntries = DiffEntry.scan(walk); } for (DiffEntry ent : diffEntries) { if (monitor.isCanceled()) { break; } listener.notifyFile(new File(workTreePath + File.separator + ent.getNewPath()), ent.getNewPath()); formatter.format(ent); } formatter.flush(); } catch (IOException ex) { throw new GitException(ex); } }
Example 17
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 18
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 19
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 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); } }