2 * Copyright (C) 2008, Google Inc.
3 * Copyright (C) 2007-2010, Robin Rosenberg <robin.rosenberg@dewire.com>
4 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * Copyright (C) 2009, Tor Arne Vestbø <torarnv@gmail.com>
6 * and other copyright owners as documented in the project's IP log.
8 * This program and the accompanying materials are made available
9 * under the terms of the Eclipse Distribution License v1.0 which
10 * accompanies this distribution, is reproduced below, and is
11 * available at http://www.eclipse.org/org/documents/edl-v10.php
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or
16 * without modification, are permitted provided that the following
19 * - Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
22 * - Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials provided
25 * with the distribution.
27 * - Neither the name of the Eclipse Foundation, Inc. nor the
28 * names of its contributors may be used to endorse or promote
29 * products derived from this software without specific prior
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
33 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
44 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 package org
.eclipse
.jgit
.treewalk
;
50 import java
.io
.FileInputStream
;
51 import java
.io
.IOException
;
52 import java
.io
.InputStream
;
54 import org
.eclipse
.jgit
.errors
.IncorrectObjectTypeException
;
55 import org
.eclipse
.jgit
.lib
.Constants
;
56 import org
.eclipse
.jgit
.lib
.FileMode
;
57 import org
.eclipse
.jgit
.lib
.ObjectReader
;
58 import org
.eclipse
.jgit
.lib
.Repository
;
59 import org
.eclipse
.jgit
.util
.FS
;
62 * Working directory iterator for standard Java IO.
64 * This iterator uses the standard <code>java.io</code> package to read the
65 * specified working directory as part of a {@link TreeWalk}.
67 public class FileTreeIterator
extends WorkingTreeIterator
{
69 * the starting directory. This directory should correspond to
70 * the root of the repository.
72 protected final File directory
;
75 * the file system abstraction which will be necessary to
76 * perform certain file system operations.
78 protected final FS fs
;
81 * Create a new iterator to traverse the work tree and its children.
84 * the repository whose working tree will be scanned.
86 public FileTreeIterator(Repository repo
) {
87 this(repo
.getWorkTree(), repo
.getFS());
88 initRootIterator(repo
);
92 * Create a new iterator to traverse the given directory and its children.
95 * the starting directory. This directory should correspond to
96 * the root of the repository.
98 * the file system abstraction which will be necessary to
99 * perform certain file system operations.
101 public FileTreeIterator(final File root
, FS fs
) {
108 * Create a new iterator to traverse a subdirectory.
111 * the parent iterator we were created from.
113 * the file system abstraction which will be necessary to
114 * perform certain file system operations.
116 * the subdirectory. This should be a directory contained within
117 * the parent directory.
119 protected FileTreeIterator(final FileTreeIterator p
, final File root
, FS fs
) {
127 public AbstractTreeIterator
createSubtreeIterator(final ObjectReader reader
)
128 throws IncorrectObjectTypeException
, IOException
{
129 return new FileTreeIterator(this, ((FileEntry
) current()).file
, fs
);
132 private Entry
[] entries() {
133 final File
[] all
= directory
.listFiles();
136 final Entry
[] r
= new Entry
[all
.length
];
137 for (int i
= 0; i
< r
.length
; i
++)
138 r
[i
] = new FileEntry(all
[i
], fs
);
143 * Wrapper for a standard Java IO file
145 static public class FileEntry
extends Entry
{
148 private final FileMode mode
;
150 private long length
= -1;
152 private long lastModified
;
154 FileEntry(final File f
, FS fs
) {
157 if (f
.isDirectory()) {
158 if (new File(f
, Constants
.DOT_GIT
).isDirectory())
159 mode
= FileMode
.GITLINK
;
161 mode
= FileMode
.TREE
;
162 } else if (fs
.canExecute(file
))
163 mode
= FileMode
.EXECUTABLE_FILE
;
165 mode
= FileMode
.REGULAR_FILE
;
169 public FileMode
getMode() {
174 public String
getName() {
175 return file
.getName();
179 public long getLength() {
181 length
= file
.length();
186 public long getLastModified() {
187 if (lastModified
== 0)
188 lastModified
= file
.lastModified();
193 public InputStream
openInputStream() throws IOException
{
194 return new FileInputStream(file
);
198 * Get the underlying file of this entry.
200 * @return the underlying file of this entry
202 public File
getFile() {
209 * The root directory of this iterator
211 public File
getDirectory() {