コード例 #1
0
ファイル: PostOrderTreeWalkTest.java プロジェクト: henon/jgit
  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);
  }
コード例 #2
0
ファイル: GitHandler.java プロジェクト: pluto-build/build-git
 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;
 }
コード例 #3
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());
  }
コード例 #4
0
ファイル: TestRepository.java プロジェクト: pdinc-oss/jgit
 /**
  * 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.
 }
コード例 #5
0
ファイル: ResolveMerger.java プロジェクト: dgreensp/jgit
 /**
  * Process the given TreeWalk's entries.
  *
  * @param treeWalk The walk to iterate over.
  * @param ignoreConflicts see {@link ResolveMerger#mergeTrees(AbstractTreeIterator, RevTree,
  *     RevTree, boolean)}
  * @return Whether the trees merged cleanly.
  * @throws IOException
  * @since 3.5
  */
 protected boolean mergeTreeWalk(TreeWalk treeWalk, boolean ignoreConflicts) throws IOException {
   boolean hasWorkingTreeIterator = tw.getTreeCount() > T_FILE;
   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)) {
       cleanUp();
       return false;
     }
     if (treeWalk.isSubtree() && enterSubtree) treeWalk.enterSubtree();
   }
   return true;
 }
コード例 #6
0
 /**
  * Returns the list of files in the specified folder at the specified commit. If the repository
  * does not exist or is empty, an empty list is returned.
  *
  * @param repository
  * @param path if unspecified, root folder is assumed.
  * @param commit if null, HEAD is assumed.
  * @return list of files in specified path
  */
 public static List<PathModel> getFilesInPath(
     Repository repository, String path, RevCommit commit) {
   List<PathModel> list = new ArrayList<PathModel>();
   if (!hasCommits(repository)) {
     return list;
   }
   if (commit == null) {
     commit = getCommit(repository, null);
   }
   final TreeWalk tw = new TreeWalk(repository);
   try {
     tw.addTree(commit.getTree());
     if (!(path == null)) {
       PathFilter f = PathFilter.create(path);
       tw.setFilter(f);
       tw.setRecursive(false);
       boolean foundFolder = false;
       while (tw.next()) {
         if (!foundFolder && tw.isSubtree()) {
           tw.enterSubtree();
         }
         if (tw.getPathString().equals(path)) {
           foundFolder = true;
           continue;
         }
         if (foundFolder) {
           list.add(getPathModel(tw, path, commit));
         }
       }
     } else {
       tw.setRecursive(false);
       while (tw.next()) {
         list.add(getPathModel(tw, null, commit));
       }
     }
   } catch (IOException e) {
     // error(e, repository, "{0} failed to get files for commit {1}", commit.getName());
     Logger.error(e, e.getMessage());
   } finally {
     tw.release();
   }
   Collections.sort(list);
   return list;
 }
コード例 #7
0
 /**
  * Returns a path model of the current file in the treewalk.
  *
  * @param tw
  * @param basePath
  * @param commit
  * @return a path model of the current file in the treewalk
  */
 private static PathModel getPathModel(TreeWalk tw, String basePath, RevCommit commit) {
   String name;
   long size = 0;
   if (basePath == null) {
     name = tw.getPathString();
   } else {
     name = tw.getPathString().substring(basePath.length() + 1);
   }
   try {
     if (!tw.isSubtree()) {
       size = tw.getObjectReader().getObjectSize(tw.getObjectId(0), Constants.OBJ_BLOB);
     }
   } catch (Throwable t) {
     // error(t, null, "failed to retrieve blob size for " + tw.getPathString());
     Logger.error(t, t.getMessage());
   }
   return new PathModel(
       name, tw.getPathString(), size, tw.getFileMode(0).getBits(), commit.getName());
 }
コード例 #8
0
  /**
   * Refreshes all resources that changed in the index since the last call to this method. This is
   * suitable for incremental updates on index changed events
   *
   * <p>For bare repositories this does nothing.
   */
  private void refreshIndexDelta() {
    if (repository.isBare()) return;

    try {
      DirCache currentIndex = repository.readDirCache();
      DirCache oldIndex = lastIndex;

      lastIndex = currentIndex;

      if (oldIndex == null) {
        refresh(); // full refresh in case we have no data to compare.
        return;
      }

      Set<String> paths = new TreeSet<String>();
      TreeWalk walk = new TreeWalk(repository);

      try {
        walk.addTree(new DirCacheIterator(oldIndex));
        walk.addTree(new DirCacheIterator(currentIndex));
        walk.setFilter(new InterIndexDiffFilter());

        while (walk.next()) {
          if (walk.isSubtree()) walk.enterSubtree();
          else paths.add(walk.getPathString());
        }
      } finally {
        walk.release();
      }

      if (!paths.isEmpty()) refreshFiles(paths);

    } catch (IOException ex) {
      Activator.error(
          MessageFormat.format(CoreText.IndexDiffCacheEntry_errorCalculatingIndexDelta, repository),
          ex);
      scheduleReloadJob(
          "Exception while calculating index delta, doing full reload instead"); //$NON-NLS-1$
    }
  }
コード例 #9
0
 public static JSONArray getListEntries(
     TreeWalk treeWalk, Repository repo, Git git, Ref head, String filePath, String projectName)
     throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException,
         IOException {
   JSONArray contents = new JSONArray();
   do {
     if (treeWalk.isSubtree()) {
       String test = new String(treeWalk.getRawPath());
       if (test.length() /*treeWalk.getPathLength()*/ > filePath.length()) {
         listEntry(
             treeWalk.getNameString(),
             "dir",
             "0",
             treeWalk.getPathString(),
             projectName,
             head.getName(),
             git,
             contents);
       }
       if (test.length() /*treeWalk.getPathLength()*/ <= filePath.length()) {
         treeWalk.enterSubtree();
       }
     } else {
       ObjectId objId = treeWalk.getObjectId(0);
       ObjectLoader loader = repo.open(objId);
       long size = loader.getSize();
       listEntry(
           treeWalk.getNameString(),
           "file",
           Long.toString(size),
           treeWalk.getPathString(),
           projectName,
           head.getName(),
           git,
           contents);
     }
   } while (treeWalk.next());
   return contents;
 }
コード例 #10
0
ファイル: GitTree.java プロジェクト: jstansel/modeshape
  protected void addInformationForPath(
      Repository repository,
      Git git,
      DocumentWriter writer,
      RevCommit commit,
      String path,
      CallSpecification spec,
      Values values)
      throws GitAPIException, IOException {
    // Make sure the path is in the canonical form we need ...
    if (path.startsWith("/")) {
      if (path.length() == 1) path = "";
      else path = path.substring(1);
    }

    // Now see if we're actually referring to the "jcr:content" node ...
    boolean isContentNode = false;
    if (path.endsWith(JCR_CONTENT_SUFFIX)) {
      isContentNode = true;
      path = path.substring(0, path.length() - JCR_CONTENT_SUFFIX.length());
    }

    // Create the TreeWalk that we'll use to navigate the files/directories ...
    final TreeWalk tw = new TreeWalk(repository);
    tw.addTree(commit.getTree());
    if ("".equals(path)) {
      // This is the top-level directory, so we don't need to pre-walk to find anything ...
      tw.setRecursive(false);
      while (tw.next()) {
        String childName = tw.getNameString();
        String childId = spec.childId(childName);
        writer.addChild(childId, childName);
      }
    } else {
      // We need to first find our path *before* we can walk the children ...
      PathFilter filter = PathFilter.create(path);
      tw.setFilter(filter);
      while (tw.next()) {
        if (filter.isDone(tw)) {
          break;
        } else if (tw.isSubtree()) {
          tw.enterSubtree();
        }
      }
      // Now that the TreeWalk is the in right location given by the 'path', we can get the
      if (tw.isSubtree()) {
        // The object at the 'path' is a directory, so go into it ...
        tw.enterSubtree();

        // Find the commit in which this folder was last modified ...
        // This may not be terribly efficient, but it seems to work faster on subsequent runs ...
        RevCommit folderCommit = git.log().addPath(path).call().iterator().next();

        // Add folder-related properties ...
        String committer = folderCommit.getCommitterIdent().getName();
        String author = folderCommit.getAuthorIdent().getName();
        DateTime committed = values.dateFrom(folderCommit.getCommitTime());
        writer.setPrimaryType(GitLexicon.FOLDER);
        writer.addProperty(JcrLexicon.CREATED, committed);
        writer.addProperty(JcrLexicon.CREATED_BY, committer);
        writer.addProperty(GitLexicon.OBJECT_ID, folderCommit.getId().name());
        writer.addProperty(GitLexicon.AUTHOR, author);
        writer.addProperty(GitLexicon.COMMITTER, committer);
        writer.addProperty(GitLexicon.COMMITTED, committed);
        writer.addProperty(GitLexicon.TITLE, folderCommit.getShortMessage());

        // And now walk the contents of the directory ...
        while (tw.next()) {
          String childName = tw.getNameString();
          String childId = spec.childId(childName);
          writer.addChild(childId, childName);
        }
      } else {
        // The path specifies a file (or a content node) ...

        // Find the commit in which this folder was last modified ...
        // This may not be terribly efficient, but it seems to work faster on subsequent runs ...
        RevCommit fileCommit = git.log().addPath(path).call().iterator().next();

        // Add file-related properties ...
        String committer = fileCommit.getCommitterIdent().getName();
        String author = fileCommit.getAuthorIdent().getName();
        DateTime committed = values.dateFrom(fileCommit.getCommitTime());
        if (isContentNode) {
          writer.setPrimaryType(GitLexicon.RESOURCE);
          writer.addProperty(JcrLexicon.LAST_MODIFIED, committed);
          writer.addProperty(JcrLexicon.LAST_MODIFIED_BY, committer);
          writer.addProperty(GitLexicon.OBJECT_ID, fileCommit.getId().name());
          writer.addProperty(GitLexicon.AUTHOR, author);
          writer.addProperty(GitLexicon.COMMITTER, committer);
          writer.addProperty(GitLexicon.COMMITTED, committed);
          writer.addProperty(GitLexicon.TITLE, fileCommit.getShortMessage());
          // Create the BinaryValue ...
          ObjectId fileObjectId = tw.getObjectId(0);
          ObjectLoader fileLoader = repository.open(fileObjectId);
          BinaryKey key = new BinaryKey(fileObjectId.getName());
          BinaryValue value = values.binaryFor(key, fileLoader.getSize());
          if (value == null) {
            // It wasn't found in the binary store ...
            if (fileLoader.isLarge()) {
              // Too large to hold in memory, so use the binary store (which reads the file
              // immediately) ...
              value = values.binaryFrom(fileLoader.openStream());
            } else {
              // This is small enough to fit into a byte[], but it still may be pretty big ...
              value =
                  new GitBinaryValue(
                      fileObjectId,
                      fileLoader,
                      connector.getSourceName(),
                      name,
                      connector.getMimeTypeDetector());
            }
          }
          writer.addProperty(JcrLexicon.DATA, value);
          if (connector.includeMimeType()) {
            try {
              String filename =
                  spec.parameter(spec.parameterCount() - 1); // the last is 'jcr:content'
              String mimeType = value.getMimeType(filename);
              if (mimeType != null) writer.addProperty(JcrLexicon.MIMETYPE, mimeType);
            } catch (RepositoryException e) {
              // do nothing
            } catch (IOException e) {
              // do nothing
            }
          }
        } else {
          writer.setPrimaryType(GitLexicon.FILE);
          writer.addProperty(JcrLexicon.CREATED, committed);
          writer.addProperty(JcrLexicon.CREATED_BY, committer);
          writer.addProperty(GitLexicon.OBJECT_ID, fileCommit.getId().name());
          writer.addProperty(GitLexicon.AUTHOR, author);
          writer.addProperty(GitLexicon.COMMITTER, committer);
          writer.addProperty(GitLexicon.COMMITTED, committed);
          writer.addProperty(GitLexicon.TITLE, fileCommit.getShortMessage());

          // Add the "jcr:content" child node ...
          String childId = spec.childId(JCR_CONTENT);
          writer.addChild(childId, JCR_CONTENT);
        }
      }
    }
  }