Java Code Examples for org.eclipse.jgit.treewalk.TreeWalk#setRecursive()

The following examples show how to use org.eclipse.jgit.treewalk.TreeWalk#setRecursive() . 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: GitConnector.java    From compiler with Apache License 2.0 6 votes vote down vote up
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 2
Source File: RepositoryPGit.java    From coming with MIT License 6 votes vote down vote up
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 3
Source File: PGA.java    From coming with MIT License 6 votes vote down vote up
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: GitCommit.java    From Getaviz with Apache License 2.0 6 votes vote down vote up
@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 5
Source File: GITStatusCrawler.java    From celerio with Apache License 2.0 6 votes vote down vote up
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: GitServiceImpl.java    From RefactoringMiner with MIT License 5 votes vote down vote up
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 7
Source File: JGitHelper.java    From go-plugins with Apache License 2.0 5 votes vote down vote up
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: GfsTreeWalkTest.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@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 9
Source File: GfsTreeWalkTest.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: GitCommit.java    From compiler with Apache License 2.0 5 votes vote down vote up
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 11
Source File: RepositoryObjectTreeWalker.java    From writelatex-git-bridge with MIT License 5 votes vote down vote up
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 12
Source File: WalkCommitTreeAdapter.java    From coderadar with MIT License 5 votes vote down vote up
@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 13
Source File: GitBranch.java    From git-as-svn with GNU General Public License v2.0 5 votes vote down vote up
@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 14
Source File: GfsTreeWalkTest.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@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 15
Source File: GfsTreeWalkTest.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@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 16
Source File: DifferentFiles.java    From gitflow-incremental-builder with MIT License 5 votes vote down vote up
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 17
Source File: ExportCommitCommand.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@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 18
Source File: AddTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
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 vote down vote up
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: TreeRevFilter.java    From onedev with MIT License 3 votes vote down vote up
/**
 * Create a filter for the first phase of a parent-rewriting limited revision
 * walk.
 * <p>
 * This filter is ANDed to evaluate before all other filters and ties the
 * configured {@link TreeFilter} into the revision walking process.
 * <p>
 * If no interesting tree entries are found the commit is colored with
 * {@code rewriteFlag}, allowing a later pass implemented by
 * {@link RewriteGenerator} to remove those colored commits from the DAG.
 *
 * @see RewriteGenerator
 *
 * @param walker
 *            walker used for reading trees.
 * @param t
 *            filter to compare against any changed paths in each commit. If a
 *            {@link FollowFilter}, will be replaced with a new filter
 *            following new paths after a rename.
 * @param rewriteFlag
 *            flag to color commits to be removed from the simplified DAT.
 */
TreeRevFilter(RevWalk walker, TreeFilter t, int rewriteFlag) {
	pathFilter = new TreeWalk(walker.reader);
	pathFilter.setFilter(t);
	pathFilter.setRecursive(t.shouldBeRecursive());
	this.rewriteFlag = rewriteFlag;
}