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 /** 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}. */
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
;
86 * Create a new iterator for an already loaded DirCache instance.
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
93 * the cache to walk. It must be already loaded into memory.
95 public DirCacheIterator(final DirCache dc
) {
97 tree
= dc
.getCacheTree(true);
98 treeEnd
= tree
.getEntrySpan();
99 subtreeId
= new byte[Constants
.OBJECT_ID_LENGTH
];
104 protected DirCacheIterator(final DirCacheIterator p
, final DirCacheTree dct
) {
105 super(p
, p
.path
, p
.pathLen
+ 1);
108 treeEnd
= p
.ptr
+ tree
.getEntrySpan();
109 subtreeId
= p
.subtreeId
;
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
);
124 public byte[] idBuffer() {
125 if (currentSubtree
!= null)
127 if (currentEntry
!= null)
128 return currentEntry
.idBuffer();
133 public int idOffset() {
134 if (currentSubtree
!= null)
136 if (currentEntry
!= null)
137 return currentEntry
.idOffset();
142 public boolean eof() {
143 return ptr
== treeEnd
;
148 if (currentSubtree
!= null)
149 ptr
+= currentSubtree
.getEntrySpan();
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.
170 s
.getObjectId().copyRawTo(subtreeId
, 0);
172 Arrays
.fill(subtreeId
, (byte) 0);
173 mode
= FileMode
.TREE
.getBits();
175 pathLen
= pathOffset
+ s
.nameLength();
180 // The current position is a file/symlink/gitlink so we
181 // do not have a subtree located here.
183 mode
= currentEntry
.getRawMode();
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
195 public DirCacheEntry
getDirCacheEntry() {
196 return currentSubtree
== null ? currentEntry
: null;