2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.dircache
;
40 import java
.io
.IOException
;
41 import java
.util
.Arrays
;
43 import org
.spearce
.jgit
.errors
.IncorrectObjectTypeException
;
44 import org
.spearce
.jgit
.lib
.Constants
;
45 import org
.spearce
.jgit
.lib
.FileMode
;
46 import org
.spearce
.jgit
.lib
.Repository
;
47 import org
.spearce
.jgit
.treewalk
.AbstractTreeIterator
;
50 * Iterate a {@link DirCache} as part of a <code>TreeWalk</code>.
52 * This is an iterator to adapt a loaded <code>DirCache</code> instance (such as
53 * read from an existing <code>.git/index</code> file) to the tree structure
54 * used by a <code>TreeWalk</code>, making it possible for applications to walk
55 * over any combination of tree objects already in the object database, index
56 * files, or working directories.
58 * @see org.spearce.jgit.treewalk.TreeWalk
60 public class DirCacheIterator
extends AbstractTreeIterator
{
61 /** The cache this iterator was created to walk. */
62 protected final DirCache cache
;
64 /** The tree this iterator is walking. */
65 private final DirCacheTree tree
;
67 /** First position in this tree. */
68 private final int treeStart
;
70 /** Last position in this tree. */
71 private final int treeEnd
;
73 /** Special buffer to hold the ObjectId of {@link #currentSubtree}. */
74 private final byte[] subtreeId
;
76 /** Index of entry within {@link #cache}. */
79 /** Next subtree to consider within {@link #tree}. */
80 private int nextSubtreePos
;
82 /** The current file entry from {@link #cache}. */
83 protected DirCacheEntry currentEntry
;
85 /** The subtree containing {@link #currentEntry} if this is first entry. */
86 protected DirCacheTree currentSubtree
;
89 * Create a new iterator for an already loaded DirCache instance.
91 * The iterator implementation may copy part of the cache's data during
92 * construction, so the cache must be read in prior to creating the
96 * the cache to walk. It must be already loaded into memory.
98 public DirCacheIterator(final DirCache dc
) {
100 tree
= dc
.getCacheTree(true);
102 treeEnd
= tree
.getEntrySpan();
103 subtreeId
= new byte[Constants
.OBJECT_ID_LENGTH
];
108 protected DirCacheIterator(final DirCacheIterator p
, final DirCacheTree dct
) {
109 super(p
, p
.path
, p
.pathLen
+ 1);
113 treeEnd
= treeStart
+ tree
.getEntrySpan();
114 subtreeId
= p
.subtreeId
;
120 public AbstractTreeIterator
createSubtreeIterator(final Repository repo
)
121 throws IncorrectObjectTypeException
, IOException
{
122 if (currentSubtree
== null)
123 throw new IncorrectObjectTypeException(getEntryObjectId(),
124 Constants
.TYPE_TREE
);
125 return new DirCacheIterator(this, currentSubtree
);
129 public byte[] idBuffer() {
130 if (currentSubtree
!= null)
132 if (currentEntry
!= null)
133 return currentEntry
.idBuffer();
138 public int idOffset() {
139 if (currentSubtree
!= null)
141 if (currentEntry
!= null)
142 return currentEntry
.idOffset();
147 public boolean first() {
148 return ptr
== treeStart
;
152 public boolean eof() {
153 return ptr
== treeEnd
;
157 public void next(int delta
) {
158 while (--delta
>= 0) {
159 if (currentSubtree
!= null)
160 ptr
+= currentSubtree
.getEntrySpan();
170 public void back(int delta
) {
171 while (--delta
>= 0) {
172 if (currentSubtree
!= null)
176 if (currentSubtree
!= null)
177 ptr
-= currentSubtree
.getEntrySpan() - 1;
181 private void parseEntry() {
182 currentEntry
= cache
.getEntry(ptr
);
183 final byte[] cep
= currentEntry
.path
;
185 if (nextSubtreePos
!= tree
.getChildCount()) {
186 final DirCacheTree s
= tree
.getChild(nextSubtreePos
);
187 if (s
.contains(cep
, pathOffset
, cep
.length
)) {
188 // The current position is the first file of this subtree.
189 // Use the subtree instead as the current position.
195 s
.getObjectId().copyRawTo(subtreeId
, 0);
197 Arrays
.fill(subtreeId
, (byte) 0);
198 mode
= FileMode
.TREE
.getBits();
200 pathLen
= pathOffset
+ s
.nameLength();
205 // The current position is a file/symlink/gitlink so we
206 // do not have a subtree located here.
208 mode
= currentEntry
.getRawMode();
210 pathLen
= cep
.length
;
211 currentSubtree
= null;
215 * Get the DirCacheEntry for the current file.
217 * @return the current cache entry, if this iterator is positioned on a
220 public DirCacheEntry
getDirCacheEntry() {
221 return currentSubtree
== null ? currentEntry
: null;