~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Treewalk/FileTreeIterator.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit's IP log.
 
4
 
 
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
 
9
 
 
10
All rights reserved.
 
11
 
 
12
Redistribution and use in source and binary forms, with or
 
13
without modification, are permitted provided that the following
 
14
conditions are met:
 
15
 
 
16
- Redistributions of source code must retain the above copyright
 
17
  notice, this list of conditions and the following disclaimer.
 
18
 
 
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.
 
23
 
 
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
 
27
  written permission.
 
28
 
 
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.
 
42
*/
 
43
 
 
44
using NGit;
 
45
using NGit.Treewalk;
 
46
using NGit.Util;
 
47
using Sharpen;
 
48
 
 
49
namespace NGit.Treewalk
 
50
{
 
51
        /// <summary>Working directory iterator for standard Java IO.</summary>
 
52
        /// <remarks>
 
53
        /// Working directory iterator for standard Java IO.
 
54
        /// <p>
 
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>
 
58
        /// .
 
59
        /// </remarks>
 
60
        public class FileTreeIterator : WorkingTreeIterator
 
61
        {
 
62
                /// <summary>the starting directory.</summary>
 
63
                /// <remarks>
 
64
                /// the starting directory. This directory should correspond to the root of
 
65
                /// the repository.
 
66
                /// </remarks>
 
67
                protected internal readonly FilePath directory;
 
68
 
 
69
                /// <summary>
 
70
                /// the file system abstraction which will be necessary to perform certain
 
71
                /// file system operations.
 
72
                /// </summary>
 
73
                /// <remarks>
 
74
                /// the file system abstraction which will be necessary to perform certain
 
75
                /// file system operations.
 
76
                /// </remarks>
 
77
                protected internal readonly FS fs;
 
78
 
 
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))
 
84
                {
 
85
                        InitRootIterator(repo);
 
86
                }
 
87
 
 
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.
 
93
                /// </param>
 
94
                /// <param name="fs">
 
95
                /// the file system abstraction which will be necessary to perform
 
96
                /// certain file system operations.
 
97
                /// </param>
 
98
                /// <param name="options">working tree options to be used</param>
 
99
                public FileTreeIterator(FilePath root, FS fs, WorkingTreeOptions options) : base(
 
100
                        options)
 
101
                {
 
102
                        directory = root;
 
103
                        this.fs = fs;
 
104
                        Init(Entries());
 
105
                }
 
106
 
 
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.
 
113
                /// </param>
 
114
                /// <param name="root">
 
115
                /// the subdirectory. This should be a directory contained within
 
116
                /// the parent directory.
 
117
                /// </param>
 
118
                protected internal FileTreeIterator(NGit.Treewalk.FileTreeIterator p, FilePath root
 
119
                        , FS fs) : base(p)
 
120
                {
 
121
                        directory = root;
 
122
                        this.fs = fs;
 
123
                        Init(Entries());
 
124
                }
 
125
 
 
126
                /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception>
 
127
                /// <exception cref="System.IO.IOException"></exception>
 
128
                public override AbstractTreeIterator CreateSubtreeIterator(ObjectReader reader)
 
129
                {
 
130
                        return new NGit.Treewalk.FileTreeIterator(this, ((FileTreeIterator.FileEntry)Current
 
131
                                ()).file, fs);
 
132
                }
 
133
 
 
134
                private WorkingTreeIterator.Entry[] Entries()
 
135
                {
 
136
                        FilePath[] all = directory.ListFiles();
 
137
                        if (all == null)
 
138
                        {
 
139
                                return EOF;
 
140
                        }
 
141
                        WorkingTreeIterator.Entry[] r = new WorkingTreeIterator.Entry[all.Length];
 
142
                        for (int i = 0; i < r.Length; i++)
 
143
                        {
 
144
                                r[i] = new FileTreeIterator.FileEntry(all[i], fs);
 
145
                        }
 
146
                        return r;
 
147
                }
 
148
 
 
149
                /// <summary>Wrapper for a standard Java IO file</summary>
 
150
                internal class FileEntry : WorkingTreeIterator.Entry
 
151
                {
 
152
                        internal readonly FilePath file;
 
153
 
 
154
                        private readonly FileMode mode;
 
155
 
 
156
                        private long length = -1;
 
157
 
 
158
                        private long lastModified;
 
159
 
 
160
                        internal FileEntry(FilePath f, FS fs)
 
161
                        {
 
162
                                file = f;
 
163
                                if (f.IsDirectory())
 
164
                                {
 
165
                                        if (new FilePath(f, Constants.DOT_GIT).IsDirectory())
 
166
                                        {
 
167
                                                mode = FileMode.GITLINK;
 
168
                                        }
 
169
                                        else
 
170
                                        {
 
171
                                                mode = FileMode.TREE;
 
172
                                        }
 
173
                                }
 
174
                                else
 
175
                                {
 
176
                                        if (fs.CanExecute(file))
 
177
                                        {
 
178
                                                mode = FileMode.EXECUTABLE_FILE;
 
179
                                        }
 
180
                                        else
 
181
                                        {
 
182
                                                mode = FileMode.REGULAR_FILE;
 
183
                                        }
 
184
                                }
 
185
                        }
 
186
 
 
187
                        public override FileMode GetMode()
 
188
                        {
 
189
                                return mode;
 
190
                        }
 
191
 
 
192
                        public override string GetName()
 
193
                        {
 
194
                                return file.GetName();
 
195
                        }
 
196
 
 
197
                        public override long GetLength()
 
198
                        {
 
199
                                if (length < 0)
 
200
                                {
 
201
                                        length = file.Length();
 
202
                                }
 
203
                                return length;
 
204
                        }
 
205
 
 
206
                        public override long GetLastModified()
 
207
                        {
 
208
                                if (lastModified == 0)
 
209
                                {
 
210
                                        lastModified = file.LastModified();
 
211
                                }
 
212
                                return lastModified;
 
213
                        }
 
214
 
 
215
                        /// <exception cref="System.IO.IOException"></exception>
 
216
                        public override InputStream OpenInputStream()
 
217
                        {
 
218
                                return new FileInputStream(file);
 
219
                        }
 
220
 
 
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()
 
225
                        {
 
226
                                return file;
 
227
                        }
 
228
                }
 
229
 
 
230
                /// <returns>The root directory of this iterator</returns>
 
231
                public virtual FilePath GetDirectory()
 
232
                {
 
233
                        return directory;
 
234
                }
 
235
 
 
236
                /// <returns>
 
237
                /// The location of the working file. This is the same as
 
238
                /// <code>
 
239
                /// new
 
240
                /// File(getDirectory(), getEntryPath())
 
241
                /// </code>
 
242
                /// but may be faster by
 
243
                /// reusing an internal File instance.
 
244
                /// </returns>
 
245
                public virtual FilePath GetEntryFile()
 
246
                {
 
247
                        return ((FileTreeIterator.FileEntry)Current()).GetFile();
 
248
                }
 
249
        }
 
250
}