Teach FileTreeIterator how to recognize a submodule when it sees one
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / treewalk / FileTreeIterator.java
blobb209a71e9a48f97d69f803ee15c96daf0dfb0f2f
1 /*
2 * Copyright (C) 2008 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.jgit.treewalk;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
24 import org.spearce.jgit.errors.IncorrectObjectTypeException;
25 import org.spearce.jgit.lib.FileMode;
26 import org.spearce.jgit.lib.Repository;
27 import org.spearce.jgit.util.FS;
29 /**
30 * Working directory iterator for standard Java IO.
31 * <p>
32 * This iterator uses the standard <code>java.io</code> package to read the
33 * specified working directory as part of a {@link TreeWalk}.
35 public class FileTreeIterator extends WorkingTreeIterator {
36 private final File directory;
38 /**
39 * Create a new iterator to traverse the given directory and its children.
41 * @param root
42 * the starting directory. This directory should correspond to
43 * the root of the repository.
45 public FileTreeIterator(final File root) {
46 directory = root;
49 /**
50 * Create a new iterator to traverse a subdirectory.
52 * @param p
53 * the parent iterator we were created from.
54 * @param root
55 * the subdirectory. This should be a directory contained within
56 * the parent directory.
58 protected FileTreeIterator(final FileTreeIterator p, final File root) {
59 super(p);
60 directory = root;
63 @Override
64 public AbstractTreeIterator createSubtreeIterator(final Repository repo)
65 throws IncorrectObjectTypeException, IOException {
66 return new FileTreeIterator(this, ((FileEntry) current()).file);
69 @Override
70 protected Entry[] getEntries() {
71 final File[] all = directory.listFiles();
72 if (all == null)
73 return EOF;
74 final Entry[] r = new Entry[all.length];
75 for (int i = 0; i < r.length; i++)
76 r[i] = new FileEntry(all[i]);
77 return r;
80 static class FileEntry extends Entry {
81 final File file;
83 private final FileMode mode;
85 private long length = -1;
87 FileEntry(final File f) {
88 file = f;
90 if (f.isDirectory()) {
91 if (new File(f, ".git").isDirectory())
92 mode = FileMode.GITLINK;
93 else
94 mode = FileMode.TREE;
95 } else if (FS.INSTANCE.canExecute(file))
96 mode = FileMode.EXECUTABLE_FILE;
97 else
98 mode = FileMode.REGULAR_FILE;
101 @Override
102 public FileMode getMode() {
103 return mode;
106 @Override
107 public String getName() {
108 return file.getName();
111 @Override
112 public long getLength() {
113 if (length < 0)
114 length = file.length();
115 return length;
118 @Override
119 public InputStream openInputStream() throws IOException {
120 return new FileInputStream(file);