Support iterating and building a DirCache at the same time
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / dircache / DirCacheBuildIterator.java
blob227b64c930d5479d4a77c85708a3525fb95fe593
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;
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;
48 /**
49 * Iterate and update a {@link DirCache} as part of a <code>TreeWalk</code>.
50 * <p>
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.
56 * <p>
57 * This iterator is suitable for update processes, or even a simple delete
58 * algorithm. For example deleting a path:
60 * <pre>
61 * final DirCache dirc = DirCache.lock(db);
62 * final DirCacheBuilder edit = dirc.builder();
64 * final TreeWalk walk = new TreeWalk(db);
65 * walk.reset();
66 * walk.setRecursive(true);
67 * walk.setFilter(PathFilter.create(&quot;name/to/remove&quot;));
68 * walk.addTree(new DirCacheBuildIterator(edit));
70 * while (walk.next())
71 * ; // do nothing on a match as we want to remove matches
72 * edit.commit();
73 * </pre>
75 public class DirCacheBuildIterator extends DirCacheIterator {
76 private final DirCacheBuilder builder;
78 /**
79 * Create a new iterator for an already loaded DirCache instance.
80 * <p>
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
83 * iterator.
85 * @param dcb
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());
91 builder = dcb;
94 protected DirCacheBuildIterator(final DirCacheBuildIterator p,
95 final DirCacheTree dct) {
96 super(p, dct);
97 builder = p.builder;
100 @Override
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);
109 @Override
110 public void skip() throws CorruptObjectException {
111 if (currentSubtree != null)
112 builder.keep(cachePos, currentSubtree.getEntrySpan());
113 else
114 builder.add(currentEntry);
115 next();
118 @Override
119 public void stopWalk() {
120 final int cnt = cache.getEntryCount();
121 if (cachePos < cnt)
122 builder.keep(cachePos, cnt - cachePos);