Пример #1
0
  private TreeFormatter createTreeFormatter(
      Map<SubtreeConfig, RevCommit> parentCommits, String commitMessage)
      throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException,
          IOException {
    TreeWalk treeWalk = new TreeWalk(repository);
    try {
      treeWalk.setRecursive(false);
      addTrees(parentCommits, treeWalk);

      TreeFormatter treeFormatter = new TreeFormatter();
      while (treeWalk.next()) {
        AbstractTreeIterator iterator = getSingleTreeIterator(treeWalk, commitMessage);
        if (iterator == null) {
          throw new IllegalStateException(
              "Tree walker did not return a single tree (should not happen): "
                  + treeWalk.getPathString());
        }
        treeFormatter.append(
            iterator.getEntryPathBuffer(),
            0,
            iterator.getEntryPathLength(),
            iterator.getEntryFileMode(),
            iterator.getEntryObjectId());
      }
      return treeFormatter;
    } finally {
      treeWalk.release();
    }
  }
Пример #2
0
 private void assertPaths(TreeWalk treeWalk, String... paths) throws Exception {
   for (int i = 0; i < paths.length; i++) {
     assertTrue(treeWalk.next());
     assertPath(treeWalk.getPathString(), paths);
   }
   assertFalse(treeWalk.next());
 }
Пример #3
0
 public void testEmptyFolderCommittedDeletedCommittedIgnoredComparedWithInitialCommit()
     throws Exception {
   RevCommit commit = createEmptyFolderAndCommit();
   deleteFolderAndCommit();
   recreateEmptyFolderIgnored();
   TreeWalk treeWalk = createTreeWalk(commit);
   assertFalse(treeWalk.next());
 }
Пример #4
0
 private boolean find(RevCommit commit, PathFilter path) throws IOException {
   treeWalk.setFilter(path);
   treeWalk.reset(commit.getTree());
   if (treeWalk.next() && isFile(treeWalk.getRawMode(0))) {
     treeWalk.getObjectId(idBuf, 0);
     return true;
   }
   return false;
 }
Пример #5
0
 private static boolean findIndexFile(TreeWalk tw) throws IOException {
   tw.enterSubtree();
   while (tw.next()) {
     if ((tw.getRawMode(0) & TYPE_MASK) == TYPE_FILE && INDEX_MD.equals(tw.getNameString())) {
       return true;
     }
   }
   return false;
 }
Пример #6
0
 private static byte[] read(Repository db, AnyObjectId treeish, String path)
     throws MissingObjectException, IncorrectObjectTypeException, IOException {
   try (ObjectReader or = db.newObjectReader()) {
     TreeWalk tree = TreeWalk.forPath(or, path, asTree(or, treeish));
     if (tree == null)
       throw new FileNotFoundException(
           MessageFormat.format(JGitText.get().entryNotFoundByPath, path));
     return read(or, tree.getObjectId(0));
   }
 }
Пример #7
0
  private String getRepositoryContent(Repository repository, RevCommit revCommit, String fileName)
      throws IOException {
    TreeWalk treewalk = TreeWalk.forPath(repository, fileName, revCommit.getTree());

    if (treewalk != null) {
      return new String(repository.open(treewalk.getObjectId(0)).getBytes());
    } else {
      return null;
    }
  }
Пример #8
0
 public List<PathInfo> getPathInfos(boolean recursive) throws IOException {
   TreeWalk tw = new TreeWalk(reader);
   tw.addTree(revision.getTree());
   tw.setRecursive(recursive);
   List<PathInfo> paths = Lists.newArrayList();
   while (tw.next()) {
     paths.add(new PathInfo(tw));
   }
   return paths;
 }
Пример #9
0
  public void testWithPostOrder_EnterSubtree() throws Exception {
    final DirCache tree = db.readDirCache();
    {
      final DirCacheBuilder b = tree.builder();

      b.add(makeFile("a"));
      b.add(makeFile("b/c"));
      b.add(makeFile("b/d"));
      b.add(makeFile("q"));

      b.finish();
      assertEquals(4, tree.getEntryCount());
    }

    final TreeWalk tw = new TreeWalk(db);
    tw.reset();
    tw.setPostOrderTraversal(true);
    tw.addTree(new DirCacheIterator(tree));

    assertModes("a", REGULAR_FILE, tw);

    assertModes("b", TREE, tw);
    assertTrue(tw.isSubtree());
    assertFalse(tw.isPostChildren());
    tw.enterSubtree();
    assertModes("b/c", REGULAR_FILE, tw);
    assertModes("b/d", REGULAR_FILE, tw);

    assertModes("b", TREE, tw);
    assertTrue(tw.isSubtree());
    assertTrue(tw.isPostChildren());

    assertModes("q", REGULAR_FILE, tw);
  }
Пример #10
0
 public static List<File> getNotIgnoredFilesOfRepo(File directory) throws GitException {
   Git git = openRepository(directory);
   Repository repo = null;
   repo = git.getRepository();
   List<File> foundFiles = new ArrayList<>();
   TreeWalk treeWalk = null;
   try {
     treeWalk = new TreeWalk(repo);
     FileTreeIterator tree = new FileTreeIterator(repo);
     treeWalk.addTree(tree);
     treeWalk.setRecursive(false);
     while (treeWalk.next()) {
       WorkingTreeIterator iterator = treeWalk.getTree(0, WorkingTreeIterator.class);
       if (!iterator.isEntryIgnored())
         if (treeWalk.isSubtree()) {
           treeWalk.enterSubtree();
         } else {
           File file = new File(directory, treeWalk.getPathString());
           foundFiles.add(file);
         }
     }
   } catch (IOException e) {
     throw new GitException("Listing of non-ignored files in " + directory + " failed", e);
   } finally {
     if (treeWalk != null) treeWalk.close();
   }
   return foundFiles;
 }
Пример #11
0
  private void assertEntry(FileMode type, boolean entryIgnored, String pathName)
      throws IOException {
    assertTrue("walk has entry", walk.next());
    assertEquals(pathName, walk.getPathString());
    assertEquals(type, walk.getFileMode(0));

    WorkingTreeIterator itr = walk.getTree(0, WorkingTreeIterator.class);
    assertNotNull("has tree", itr);
    assertEquals("is ignored", entryIgnored, itr.isEntryIgnored());
    if (D.equals(type)) walk.enterSubtree();
  }
Пример #12
0
  protected ObjectId getObjectId(String fileName) throws IOException {
    if (revision == null) {
      return null;
    }

    TreeWalk tw = TreeWalk.forPath(reader, fileName, revision.getTree());
    if (tw != null) {
      return tw.getObjectId(0);
    }

    return null;
  }
Пример #13
0
 /**
  * Checks if a file with the given path exists in the HEAD tree
  *
  * @param path
  * @return true if the file exists
  * @throws IOException
  */
 private boolean inHead(String path) throws IOException {
   ObjectId headId = db.resolve(Constants.HEAD);
   RevWalk rw = new RevWalk(db);
   TreeWalk tw = null;
   try {
     tw = TreeWalk.forPath(db, path, rw.parseTree(headId));
     return tw != null;
   } finally {
     rw.release();
     rw.dispose();
     if (tw != null) tw.release();
   }
 }
Пример #14
0
  private DiffEntry findRename(RevCommit parent, RevCommit commit, PathFilter path)
      throws IOException {
    if (renameDetector == null) return null;

    treeWalk.setFilter(TreeFilter.ANY_DIFF);
    treeWalk.reset(parent.getTree(), commit.getTree());
    renameDetector.reset();
    renameDetector.addAll(DiffEntry.scan(treeWalk));
    for (DiffEntry ent : renameDetector.compute()) {
      if (isRename(ent) && ent.getNewPath().equals(path.getPath())) return ent;
    }
    return null;
  }
  private void assertIteration(FileMode type, String pathName, List<Attribute> nodeAttrs)
      throws IOException {
    assertTrue("walk has entry", walk.next());
    assertEquals(pathName, walk.getPathString());
    assertEquals(type, walk.getFileMode(0));
    DirCacheIterator itr = walk.getTree(0, DirCacheIterator.class);
    assertNotNull("has tree", itr);

    AttributesNode attributesNode = itr.getEntryAttributesNode(db.newObjectReader());
    assertAttributesNode(pathName, attributesNode, nodeAttrs);

    if (D.equals(type)) walk.enterSubtree();
  }
Пример #16
0
  /**
   * Update any smudged entries with information from the working tree.
   *
   * @throws IOException
   */
  private void updateSmudgedEntries() throws IOException {
    TreeWalk walk = new TreeWalk(repository);
    List<String> paths = new ArrayList<String>(128);
    try {
      for (int i = 0; i < entryCnt; i++)
        if (sortedEntries[i].isSmudged()) paths.add(sortedEntries[i].getPathString());
      if (paths.isEmpty()) return;
      walk.setFilter(PathFilterGroup.createFromStrings(paths));

      DirCacheIterator iIter = new DirCacheIterator(this);
      FileTreeIterator fIter = new FileTreeIterator(repository);
      walk.addTree(iIter);
      walk.addTree(fIter);
      walk.setRecursive(true);
      while (walk.next()) {
        iIter = walk.getTree(0, DirCacheIterator.class);
        if (iIter == null) continue;
        fIter = walk.getTree(1, FileTreeIterator.class);
        if (fIter == null) continue;
        DirCacheEntry entry = iIter.getDirCacheEntry();
        if (entry.isSmudged() && iIter.idEqual(fIter)) {
          entry.setLength(fIter.getEntryLength());
          entry.setLastModified(fIter.getEntryLastModified());
        }
      }
    } finally {
      walk.release();
    }
  }
Пример #17
0
  protected byte[] readFile(String fileName) throws IOException {
    if (revision == null) {
      return new byte[] {};
    }

    TreeWalk tw = TreeWalk.forPath(reader, fileName, revision.getTree());
    if (tw != null) {
      ObjectLoader obj = reader.open(tw.getObjectId(0), Constants.OBJ_BLOB);
      return obj.getCachedBytes(Integer.MAX_VALUE);

    } else {
      return new byte[] {};
    }
  }
Пример #18
0
 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);
     }
   }
 }
Пример #19
0
  public void testResetDoesNotAffectPostOrder() throws Exception {
    final TreeWalk tw = new TreeWalk(db);
    tw.setPostOrderTraversal(true);
    assertTrue(tw.isPostOrderTraversal());
    tw.reset();
    assertTrue(tw.isPostOrderTraversal());

    tw.setPostOrderTraversal(false);
    assertFalse(tw.isPostOrderTraversal());
    tw.reset();
    assertFalse(tw.isPostOrderTraversal());
  }
Пример #20
0
  @Override
  public byte[] getData(String commitName, String filePath) {

    try {

      RevCommit revCommit = revWalk.parseCommit(ObjectId.fromString(commitName));
      RevTree tree = revCommit.getTree();
      TreeWalk treeWalk = TreeWalk.forPath(reader, filePath, tree);

      if (treeWalk == null) return null;
      else return reader.open(treeWalk.getObjectId(0)).getBytes();

    } catch (IOException e) {
      throw new VisMinerAPIException(e.getMessage(), e);
    }
  }
Пример #21
0
  public void testNonRecursiveTreeWalk() throws Exception {
    RevCommit commit = writeFileInFolderAndCommit();
    deleteAll();
    writeFileWithFolderName();
    TreeWalk treeWalk = createNonRecursiveTreeWalk(commit);

    assertTrue(treeWalk.next());
    assertEquals("folder", treeWalk.getPathString());
    assertTrue(treeWalk.next());
    assertEquals("folder", treeWalk.getPathString());
    assertTrue(treeWalk.isSubtree());
    treeWalk.enterSubtree();
    assertTrue(treeWalk.next());
    assertEquals("folder/file", treeWalk.getPathString());
    assertFalse(treeWalk.next());
  }
Пример #22
0
  @Test
  public void testAddUnstagedChanges()
      throws IOException, NoHeadException, NoMessageException, ConcurrentRefUpdateException,
          JGitInternalException, WrongRepositoryStateException, NoFilepatternException {
    File file = new File(db.getWorkTree(), "a.txt");
    FileUtils.createNewFile(file);
    PrintWriter writer = new PrintWriter(file);
    writer.print("content");
    writer.close();

    Git git = new Git(db);
    git.add().addFilepattern("a.txt").call();
    RevCommit commit = git.commit().setMessage("initial commit").call();
    TreeWalk tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
    assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea", tw.getObjectId(0).getName());

    writer = new PrintWriter(file);
    writer.print("content2");
    writer.close();
    commit = git.commit().setMessage("second commit").call();
    tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
    assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea", tw.getObjectId(0).getName());

    commit = git.commit().setAll(true).setMessage("third commit").setAll(true).call();
    tw = TreeWalk.forPath(db, "a.txt", commit.getTree());
    assertEquals("db00fd65b218578127ea51f3dffac701f12f486a", tw.getObjectId(0).getName());
  }
Пример #23
0
  @Override
  public void close() {

    repository.close();
    git.close();
    treeWalk.close();
    revWalk.close();
    reader.close();
  }
Пример #24
0
 /**
  * Lookup an entry stored in a tree, failing if not present.
  *
  * @param tree the tree to search.
  * @param path the path to find the entry of.
  * @return the parsed object entry at this path, never null.
  * @throws Exception
  */
 public RevObject get(final RevTree tree, final String path) throws Exception {
   final TreeWalk tw = new TreeWalk(pool.getObjectReader());
   tw.setFilter(PathFilterGroup.createFromStrings(Collections.singleton(path)));
   tw.reset(tree);
   while (tw.next()) {
     if (tw.isSubtree() && !path.equals(tw.getPathString())) {
       tw.enterSubtree();
       continue;
     }
     final ObjectId entid = tw.getObjectId(0);
     final FileMode entmode = tw.getFileMode(0);
     return pool.lookupAny(entid, entmode.getObjectType());
   }
   fail("Can't find " + path + " in tree " + tree.name());
   return null; // never reached.
 }
Пример #25
0
  @Nullable
  private static MarkdownFile findFile(RevWalk rw, RevTree root, String path) throws IOException {
    if (Strings.isNullOrEmpty(path)) {
      path = INDEX_MD;
    }

    ObjectReader reader = rw.getObjectReader();
    try (TreeWalk tw = TreeWalk.forPath(reader, path, root)) {
      if (tw == null) {
        return null;
      }
      if ((tw.getRawMode(0) & TYPE_MASK) == TYPE_TREE) {
        if (findIndexFile(tw)) {
          path = tw.getPathString();
        } else {
          return null;
        }
      }
      if ((tw.getRawMode(0) & TYPE_MASK) == TYPE_FILE) {
        if (!path.endsWith(".md")) {
          return null;
        }
        return new MarkdownFile(path, tw.getObjectId(0));
      }
      return null;
    }
  }
Пример #26
0
  protected boolean hasIgnoreFile(Repository repository, RevCommit revCommit, String relativePath)
      throws Exception {

    if (_ignoreFileName == null) {
      return false;
    }

    try (TreeWalk treeWalk = new TreeWalk(repository)) {
      treeWalk.addTree(revCommit.getTree());

      if (revCommit.getParentCount() > 0) {
        RevCommit parentRevCommit = revCommit.getParent(0);

        treeWalk.addTree(parentRevCommit.getTree());
      }

      treeWalk.setRecursive(true);

      treeWalk.setFilter(
          AndTreeFilter.create(
              PathFilter.create(relativePath + "/" + _ignoreFileName), TreeFilter.ANY_DIFF));

      return treeWalk.next();
    }
  }
  /**
   * Returns all tree entries that do not match the ignore paths.
   *
   * @param db
   * @param ignorePaths
   * @param dcBuilder
   * @throws IOException
   */
  private List<DirCacheEntry> getTreeEntries(Repository db, Collection<String> ignorePaths)
      throws IOException {
    List<DirCacheEntry> list = new ArrayList<DirCacheEntry>();
    ObjectId treeId = db.resolve(BRANCH + "^{tree}");
    if (treeId == null) {
      // branch does not exist yet, could be migrating tickets
      return list;
    }
    try (TreeWalk tw = new TreeWalk(db)) {
      int hIdx = tw.addTree(treeId);
      tw.setRecursive(true);

      while (tw.next()) {
        String path = tw.getPathString();
        CanonicalTreeParser hTree = null;
        if (hIdx != -1) {
          hTree = tw.getTree(hIdx, CanonicalTreeParser.class);
        }
        if (!ignorePaths.contains(path)) {
          // add all other tree entries
          if (hTree != null) {
            final DirCacheEntry entry = new DirCacheEntry(path);
            entry.setObjectId(hTree.getEntryObjectId());
            entry.setFileMode(hTree.getEntryFileMode());
            list.add(entry);
          }
        }
      }
    }
    return list;
  }
Пример #28
0
 private TreeWalk createTreeWalk(RevCommit commit, boolean isRecursive, boolean honorIgnores)
     throws Exception {
   TreeWalk treeWalk = new TreeWalk(db);
   treeWalk.setRecursive(isRecursive);
   treeWalk.addTree(commit.getTree());
   treeWalk.addTree(new DirCacheIterator(db.readDirCache()));
   treeWalk.addTree(new FileTreeIterator(db));
   if (!honorIgnores) treeWalk.setFilter(new IndexDiffFilter(1, 2, honorIgnores));
   else treeWalk.setFilter(new IndexDiffFilter(1, 2));
   return treeWalk;
 }
Пример #29
0
 public void testInitialize_TogglePostOrder() throws Exception {
   final TreeWalk tw = new TreeWalk(db);
   assertFalse(tw.isPostOrderTraversal());
   tw.setPostOrderTraversal(true);
   assertTrue(tw.isPostOrderTraversal());
   tw.setPostOrderTraversal(false);
   assertFalse(tw.isPostOrderTraversal());
 }
Пример #30
0
  @Override
  public List<String> getSnapshotFiles(String commitUid) {

    try {

      RevCommit lastCommit = revWalk.parseCommit(repository.resolve(commitUid));

      treeWalk.reset();
      treeWalk.addTree(lastCommit.getTree());
      treeWalk.setRecursive(true);

      List<String> files = new ArrayList<String>();
      while (treeWalk.next()) {
        String path = repositoryPath + "/" + treeWalk.getPathString();
        files.add(StringUtils.sha1(path));
      }

      return files;

    } catch (IOException e) {
      throw new VisMinerAPIException(e.getMessage(), e);
    }
  }