Refactor AbstractTreeIterator semantics to start on first entry
[egit.git] / org.spearce.jgit / src / org / spearce / jgit / dircache / DirCacheIterator.java
blob248ae1e2f44cd18c2594ce09d782379b4a25a5aa
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 /** Last position in this tree. */
68 private final int treeEnd;
70 /** Special buffer to hold the ObjectId of {@link #currentSubtree}. */
71 private final byte[] subtreeId;
73 /** Index of entry within {@link #cache}. */
74 protected int ptr;
76 /** Next subtree to consider within {@link #tree}. */
77 private int nextSubtreePos;
79 /** The current file entry from {@link #cache}. */
80 protected DirCacheEntry currentEntry;
82 /** The subtree containing {@link #currentEntry} if this is first entry. */
83 protected DirCacheTree currentSubtree;
85 /**
86 * Create a new iterator for an already loaded DirCache instance.
87 * <p>
88 * The iterator implementation may copy part of the cache's data during
89 * construction, so the cache must be read in prior to creating the
90 * iterator.
92 * @param dc
93 * the cache to walk. It must be already loaded into memory.
95 public DirCacheIterator(final DirCache dc) {
96 cache = dc;
97 tree = dc.getCacheTree(true);
98 treeEnd = tree.getEntrySpan();
99 subtreeId = new byte[Constants.OBJECT_ID_LENGTH];
100 if (!eof())
101 parseEntry();
104 protected DirCacheIterator(final DirCacheIterator p, final DirCacheTree dct) {
105 super(p, p.path, p.pathLen + 1);
106 cache = p.cache;
107 tree = dct;
108 treeEnd = p.ptr + tree.getEntrySpan();
109 subtreeId = p.subtreeId;
110 ptr = p.ptr;
111 parseEntry();
114 @Override
115 public AbstractTreeIterator createSubtreeIterator(final Repository repo)
116 throws IncorrectObjectTypeException, IOException {
117 if (currentSubtree == null)
118 throw new IncorrectObjectTypeException(getEntryObjectId(),
119 Constants.TYPE_TREE);
120 return new DirCacheIterator(this, currentSubtree);
123 @Override
124 public byte[] idBuffer() {
125 if (currentSubtree != null)
126 return subtreeId;
127 if (currentEntry != null)
128 return currentEntry.idBuffer();
129 return zeroid;
132 @Override
133 public int idOffset() {
134 if (currentSubtree != null)
135 return 0;
136 if (currentEntry != null)
137 return currentEntry.idOffset();
138 return 0;
141 @Override
142 public boolean eof() {
143 return ptr == treeEnd;
146 @Override
147 public void next() {
148 if (currentSubtree != null)
149 ptr += currentSubtree.getEntrySpan();
150 else
151 ptr++;
152 if (!eof())
153 parseEntry();
156 private void parseEntry() {
157 currentEntry = cache.getEntry(ptr);
158 final byte[] cep = currentEntry.path;
160 if (nextSubtreePos != tree.getChildCount()) {
161 final DirCacheTree s = tree.getChild(nextSubtreePos);
162 if (s.contains(cep, pathOffset, cep.length)) {
163 // The current position is the first file of this subtree.
164 // Use the subtree instead as the current position.
166 currentSubtree = s;
167 nextSubtreePos++;
169 if (s.isValid())
170 s.getObjectId().copyRawTo(subtreeId, 0);
171 else
172 Arrays.fill(subtreeId, (byte) 0);
173 mode = FileMode.TREE.getBits();
174 path = cep;
175 pathLen = pathOffset + s.nameLength();
176 return;
180 // The current position is a file/symlink/gitlink so we
181 // do not have a subtree located here.
183 mode = currentEntry.getRawMode();
184 path = cep;
185 pathLen = cep.length;
186 currentSubtree = null;
190 * Get the DirCacheEntry for the current file.
192 * @return the current cache entry, if this iterator is positioned on a
193 * non-tree.
195 public DirCacheEntry getDirCacheEntry() {
196 return currentSubtree == null ? currentEntry : null;