2
This code is derived from jgit (http://eclipse.org/jgit).
3
Copyright owners are documented in jgit's IP log.
5
This program and the accompanying materials are made available
6
under the terms of the Eclipse Distribution License v1.0 which
7
accompanies this distribution, is reproduced below, and is
8
available at http://www.eclipse.org/org/documents/edl-v10.php
12
Redistribution and use in source and binary forms, with or
13
without modification, are permitted provided that the following
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
19
- Redistributions in binary form must reproduce the above
20
copyright notice, this list of conditions and the following
21
disclaimer in the documentation and/or other materials provided
22
with the distribution.
24
- Neither the name of the Eclipse Foundation, Inc. nor the
25
names of its contributors may be used to endorse or promote
26
products derived from this software without specific prior
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49
namespace NGit.Treewalk
51
/// <summary>Working directory iterator for standard Java IO.</summary>
53
/// Working directory iterator for standard Java IO.
55
/// This iterator uses the standard <code>java.io</code> package to read the
56
/// specified working directory as part of a
57
/// <see cref="TreeWalk">TreeWalk</see>
60
public class FileTreeIterator : WorkingTreeIterator
62
/// <summary>the starting directory.</summary>
64
/// the starting directory. This directory should correspond to the root of
67
protected internal readonly FilePath directory;
70
/// the file system abstraction which will be necessary to perform certain
71
/// file system operations.
74
/// the file system abstraction which will be necessary to perform certain
75
/// file system operations.
77
protected internal readonly FS fs;
79
/// <summary>Create a new iterator to traverse the work tree and its children.</summary>
80
/// <remarks>Create a new iterator to traverse the work tree and its children.</remarks>
81
/// <param name="repo">the repository whose working tree will be scanned.</param>
82
public FileTreeIterator(Repository repo) : this(repo.WorkTree, repo.FileSystem, repo
83
.GetConfig().Get(WorkingTreeOptions.KEY))
85
InitRootIterator(repo);
88
/// <summary>Create a new iterator to traverse the given directory and its children.</summary>
89
/// <remarks>Create a new iterator to traverse the given directory and its children.</remarks>
90
/// <param name="root">
91
/// the starting directory. This directory should correspond to
92
/// the root of the repository.
95
/// the file system abstraction which will be necessary to perform
96
/// certain file system operations.
98
/// <param name="options">working tree options to be used</param>
99
public FileTreeIterator(FilePath root, FS fs, WorkingTreeOptions options) : base(
107
/// <summary>Create a new iterator to traverse a subdirectory.</summary>
108
/// <remarks>Create a new iterator to traverse a subdirectory.</remarks>
109
/// <param name="p">the parent iterator we were created from.</param>
110
/// <param name="fs">
111
/// the file system abstraction which will be necessary to perform
112
/// certain file system operations.
114
/// <param name="root">
115
/// the subdirectory. This should be a directory contained within
116
/// the parent directory.
118
protected internal FileTreeIterator(NGit.Treewalk.FileTreeIterator p, FilePath root
126
/// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
127
/// <exception cref="System.IO.IOException"></exception>
128
public override AbstractTreeIterator CreateSubtreeIterator(ObjectReader reader)
130
return new NGit.Treewalk.FileTreeIterator(this, ((FileTreeIterator.FileEntry)Current
134
private WorkingTreeIterator.Entry[] Entries()
136
FilePath[] all = directory.ListFiles();
141
WorkingTreeIterator.Entry[] r = new WorkingTreeIterator.Entry[all.Length];
142
for (int i = 0; i < r.Length; i++)
144
r[i] = new FileTreeIterator.FileEntry(all[i], fs);
149
/// <summary>Wrapper for a standard Java IO file</summary>
150
internal class FileEntry : WorkingTreeIterator.Entry
152
internal readonly FilePath file;
154
private readonly FileMode mode;
156
private long length = -1;
158
private long lastModified;
160
internal FileEntry(FilePath f, FS fs)
165
if (new FilePath(f, Constants.DOT_GIT).IsDirectory())
167
mode = FileMode.GITLINK;
171
mode = FileMode.TREE;
176
if (fs.CanExecute(file))
178
mode = FileMode.EXECUTABLE_FILE;
182
mode = FileMode.REGULAR_FILE;
187
public override FileMode GetMode()
192
public override string GetName()
194
return file.GetName();
197
public override long GetLength()
201
length = file.Length();
206
public override long GetLastModified()
208
if (lastModified == 0)
210
lastModified = file.LastModified();
215
/// <exception cref="System.IO.IOException"></exception>
216
public override InputStream OpenInputStream()
218
return new FileInputStream(file);
221
/// <summary>Get the underlying file of this entry.</summary>
222
/// <remarks>Get the underlying file of this entry.</remarks>
223
/// <returns>the underlying file of this entry</returns>
224
public virtual FilePath GetFile()
230
/// <returns>The root directory of this iterator</returns>
231
public virtual FilePath GetDirectory()
237
/// The location of the working file. This is the same as
240
/// File(getDirectory(), getEntryPath())
242
/// but may be faster by
243
/// reusing an internal File instance.
245
public virtual FilePath GetEntryFile()
247
return ((FileTreeIterator.FileEntry)Current()).GetFile();