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
;
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
;
30 * Working directory iterator for standard Java IO.
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
;
39 * Create a new iterator to traverse the given directory and its children.
42 * the starting directory. This directory should correspond to
43 * the root of the repository.
45 public FileTreeIterator(final File root
) {
50 * Create a new iterator to traverse a subdirectory.
53 * the parent iterator we were created from.
55 * the subdirectory. This should be a directory contained within
56 * the parent directory.
58 protected FileTreeIterator(final FileTreeIterator p
, final File root
) {
64 public AbstractTreeIterator
createSubtreeIterator(final Repository repo
)
65 throws IncorrectObjectTypeException
, IOException
{
66 return new FileTreeIterator(this, ((FileEntry
) current()).file
);
70 protected Entry
[] getEntries() {
71 final File
[] all
= directory
.listFiles();
74 final Entry
[] r
= new Entry
[all
.length
];
75 for (int i
= 0; i
< r
.length
; i
++)
76 r
[i
] = new FileEntry(all
[i
]);
80 static class FileEntry
extends Entry
{
83 private final FileMode mode
;
85 private long length
= -1;
87 FileEntry(final File f
) {
90 if (f
.isDirectory()) {
91 if (new File(f
, ".git").isDirectory())
92 mode
= FileMode
.GITLINK
;
95 } else if (FS
.INSTANCE
.canExecute(file
))
96 mode
= FileMode
.EXECUTABLE_FILE
;
98 mode
= FileMode
.REGULAR_FILE
;
102 public FileMode
getMode() {
107 public String
getName() {
108 return file
.getName();
112 public long getLength() {
114 length
= file
.length();
119 public InputStream
openInputStream() throws IOException
{
120 return new FileInputStream(file
);