2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org
.spearce
.jgit
.treewalk
;
42 import java
.io
.FileInputStream
;
43 import java
.io
.IOException
;
44 import java
.io
.InputStream
;
46 import org
.spearce
.jgit
.errors
.IncorrectObjectTypeException
;
47 import org
.spearce
.jgit
.lib
.FileMode
;
48 import org
.spearce
.jgit
.lib
.Repository
;
49 import org
.spearce
.jgit
.util
.FS
;
52 * Working directory iterator for standard Java IO.
54 * This iterator uses the standard <code>java.io</code> package to read the
55 * specified working directory as part of a {@link TreeWalk}.
57 public class FileTreeIterator
extends WorkingTreeIterator
{
58 private final File directory
;
61 * Create a new iterator to traverse the given directory and its children.
64 * the starting directory. This directory should correspond to
65 * the root of the repository.
67 public FileTreeIterator(final File root
) {
73 * Create a new iterator to traverse a subdirectory.
76 * the parent iterator we were created from.
78 * the subdirectory. This should be a directory contained within
79 * the parent directory.
81 protected FileTreeIterator(final FileTreeIterator p
, final File root
) {
88 public AbstractTreeIterator
createSubtreeIterator(final Repository repo
)
89 throws IncorrectObjectTypeException
, IOException
{
90 return new FileTreeIterator(this, ((FileEntry
) current()).file
);
93 private Entry
[] entries() {
94 final File
[] all
= directory
.listFiles();
97 final Entry
[] r
= new Entry
[all
.length
];
98 for (int i
= 0; i
< r
.length
; i
++)
99 r
[i
] = new FileEntry(all
[i
]);
103 static class FileEntry
extends Entry
{
106 private final FileMode mode
;
108 private long length
= -1;
110 private long lastModified
;
112 FileEntry(final File f
) {
115 if (f
.isDirectory()) {
116 if (new File(f
, ".git").isDirectory())
117 mode
= FileMode
.GITLINK
;
119 mode
= FileMode
.TREE
;
120 } else if (FS
.INSTANCE
.canExecute(file
))
121 mode
= FileMode
.EXECUTABLE_FILE
;
123 mode
= FileMode
.REGULAR_FILE
;
127 public FileMode
getMode() {
132 public String
getName() {
133 return file
.getName();
137 public long getLength() {
139 length
= file
.length();
144 public long getLastModified() {
145 if (lastModified
== 0)
146 lastModified
= file
.lastModified();
151 public InputStream
openInputStream() throws IOException
{
152 return new FileInputStream(file
);