Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jgit-dev] Bug in TreeWalk.forPath (?)

On Wed, Jan 26, 2011 at 01:17, Georgios Gousios <gousiosg@xxxxxxx> wrote:
>
> I am using JGit 0.10.1 from Maven. In certain occasions, such as the one
> documented in the following JUnit test case, JGit recognises a file path as
> either a directory or file, depending on whether TreeWalk.forPath() is used
> or not. Is it a bug in forPath() or am I missing something on how to use it?

You are missing something.

>        String repo = "/home/gousiosg/test"; //Clone of
> git://github.com/schacon/ruby-git.git
>        Repository local = new FileRepository(repo);
>        FileMode a = null, b = null;
>        RevWalk rw = new RevWalk(local);
>        ObjectId treeId =
> local.resolve("5df04c8b946ef9c1f31bf8e722a9262b512c1928");
>
>        RevTree tree = rw.parseTree(treeId);
>        final TreeWalk walk = new TreeWalk(local);
>        walk.setRecursive(false);

Here you have disabled recursive walking. This means the TreeWalk will
only scan the top level tree, and will *not* dive into subdirectories.

>        walk.addTree(tree);
>
>        while (walk.next()) {
>            String pathstr = walk.getPathString();
>            if (pathstr.equals("working")) {

Here you have matched an item in the top level tree that is named
"working".  This is *not* test/files/working, this is just plain old
working.  Which happens to be a TREE:

>        assertEquals(a, FileMode.TREE);
>
>        RevCommit c =
> rw.parseCommit(local.resolve("b18bca3b853dee6a7bc86f09921aa3b1ee3f3d7b"));
>        TreeWalk tw = TreeWalk.forPath(local, "tests/files/working",
> c.getTree());

This is a file called "working", in the "files" subdirectory, of the
"tests" subdirectory of the top level.  Because you handed a path with
/ in it, TreeWalk.forPath setRecursive(true) automatically and was
able to dive into the "tests" subdirectory when it was found, and then
the "files" subdirectory, and then found "working" as a file and
returned it.

forPath is basically:

  walk.setRecursive(true);

Which is short for doing this instead:

  walk.setRecursive(fales);
  while (walk.next()) {
    if (walk.isSubtree()) {
      walk.enterSubtree();
      continue;
    }
    ... your other logic ...
 }

That is, when recursive is true you do not see subdirectories, you
only see files.

-- 
Shawn.


Back to the top