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
;
42 import org
.spearce
.jgit
.errors
.CorruptObjectException
;
43 import org
.spearce
.jgit
.errors
.IncorrectObjectTypeException
;
44 import org
.spearce
.jgit
.lib
.Constants
;
45 import org
.spearce
.jgit
.lib
.Repository
;
46 import org
.spearce
.jgit
.treewalk
.AbstractTreeIterator
;
49 * Iterate and update a {@link DirCache} as part of a <code>TreeWalk</code>.
51 * Like {@link DirCacheIterator} this iterator allows a DirCache to be used in
52 * parallel with other sorts of iterators in a TreeWalk. However any entry which
53 * appears in the source DirCache and which is skipped by the TreeFilter is
54 * automatically copied into {@link DirCacheBuilder}, thus retaining it in the
55 * newly updated index.
57 * This iterator is suitable for update processes, or even a simple delete
58 * algorithm. For example deleting a path:
61 * final DirCache dirc = DirCache.lock(db);
62 * final DirCacheBuilder edit = dirc.builder();
64 * final TreeWalk walk = new TreeWalk(db);
66 * walk.setRecursive(true);
67 * walk.setFilter(PathFilter.create("name/to/remove"));
68 * walk.addTree(new DirCacheBuildIterator(edit));
71 * ; // do nothing on a match as we want to remove matches
75 public class DirCacheBuildIterator
extends DirCacheIterator
{
76 private final DirCacheBuilder builder
;
79 * Create a new iterator for an already loaded DirCache instance.
81 * The iterator implementation may copy part of the cache's data during
82 * construction, so the cache must be read in prior to creating the
86 * the cache builder for the cache to walk. The cache must be
87 * already loaded into memory.
89 public DirCacheBuildIterator(final DirCacheBuilder dcb
) {
90 super(dcb
.getDirCache());
94 protected DirCacheBuildIterator(final DirCacheBuildIterator p
,
95 final DirCacheTree dct
) {
101 public AbstractTreeIterator
createSubtreeIterator(final Repository repo
)
102 throws IncorrectObjectTypeException
, IOException
{
103 if (currentSubtree
== null)
104 throw new IncorrectObjectTypeException(getEntryObjectId(),
105 Constants
.TYPE_TREE
);
106 return new DirCacheBuildIterator(this, currentSubtree
);
110 public void skip() throws CorruptObjectException
{
111 if (currentSubtree
!= null)
112 builder
.keep(cachePos
, currentSubtree
.getEntrySpan());
114 builder
.add(currentEntry
);
119 public void stopWalk() {
120 final int cnt
= cache
.getEntryCount();
122 builder
.keep(cachePos
, cnt
- cachePos
);