Expose beginning of iterator indication from AbstractTreeIterator
[egit.git] / org.spearce.jgit / src / org / spearce / jgit / dircache / DirCacheIterator.java
blob83847238b8ca9765339e175e134e2cc052f83afd
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
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
21 * written permission.
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;
49 /**
50 * Iterate a {@link DirCache} as part of a <code>TreeWalk</code>.
51 * <p>
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}. */
77 protected int ptr;
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;
88 /**
89 * Create a new iterator for an already loaded DirCache instance.
90 * <p>
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
93 * iterator.
95 * @param dc
96 * the cache to walk. It must be already loaded into memory.
98 public DirCacheIterator(final DirCache dc) {
99 cache = dc;
100 tree = dc.getCacheTree(true);
101 treeStart = 0;
102 treeEnd = tree.getEntrySpan();
103 subtreeId = new byte[Constants.OBJECT_ID_LENGTH];
104 if (!eof())
105 parseEntry();
108 protected DirCacheIterator(final DirCacheIterator p, final DirCacheTree dct) {
109 super(p, p.path, p.pathLen + 1);
110 cache = p.cache;
111 tree = dct;
112 treeStart = p.ptr;
113 treeEnd = treeStart + tree.getEntrySpan();
114 subtreeId = p.subtreeId;
115 ptr = p.ptr;
116 parseEntry();
119 @Override
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);
128 @Override
129 public byte[] idBuffer() {
130 if (currentSubtree != null)
131 return subtreeId;
132 if (currentEntry != null)
133 return currentEntry.idBuffer();
134 return zeroid;
137 @Override
138 public int idOffset() {
139 if (currentSubtree != null)
140 return 0;
141 if (currentEntry != null)
142 return currentEntry.idOffset();
143 return 0;
146 @Override
147 public boolean first() {
148 return ptr == treeStart;
151 @Override
152 public boolean eof() {
153 return ptr == treeEnd;
156 @Override
157 public void next(int delta) {
158 while (--delta >= 0) {
159 if (currentSubtree != null)
160 ptr += currentSubtree.getEntrySpan();
161 else
162 ptr++;
163 if (eof())
164 break;
165 parseEntry();
169 @Override
170 public void back(int delta) {
171 while (--delta >= 0) {
172 if (currentSubtree != null)
173 nextSubtreePos--;
174 ptr--;
175 parseEntry();
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.
191 currentSubtree = s;
192 nextSubtreePos++;
194 if (s.isValid())
195 s.getObjectId().copyRawTo(subtreeId, 0);
196 else
197 Arrays.fill(subtreeId, (byte) 0);
198 mode = FileMode.TREE.getBits();
199 path = cep;
200 pathLen = pathOffset + s.nameLength();
201 return;
205 // The current position is a file/symlink/gitlink so we
206 // do not have a subtree located here.
208 mode = currentEntry.getRawMode();
209 path = cep;
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
218 * non-tree.
220 public DirCacheEntry getDirCacheEntry() {
221 return currentSubtree == null ? currentEntry : null;