You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DirCacheBuildIterator.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  5. *
  6. * This program and the accompanying materials are made available under the
  7. * terms of the Eclipse Distribution License v. 1.0 which is available at
  8. * https://www.eclipse.org/org/documents/edl-v10.php.
  9. *
  10. * SPDX-License-Identifier: BSD-3-Clause
  11. */
  12. package org.eclipse.jgit.dircache;
  13. import java.io.IOException;
  14. import org.eclipse.jgit.errors.CorruptObjectException;
  15. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  16. import org.eclipse.jgit.lib.Constants;
  17. import org.eclipse.jgit.lib.ObjectReader;
  18. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  19. /**
  20. * Iterate and update a {@link org.eclipse.jgit.dircache.DirCache} as part of a
  21. * <code>TreeWalk</code>.
  22. * <p>
  23. * Like {@link org.eclipse.jgit.dircache.DirCacheIterator} this iterator allows
  24. * a DirCache to be used in parallel with other sorts of iterators in a
  25. * TreeWalk. However any entry which appears in the source DirCache and which is
  26. * skipped by the TreeFilter is automatically copied into
  27. * {@link org.eclipse.jgit.dircache.DirCacheBuilder}, thus retaining it in the
  28. * newly updated index.
  29. * <p>
  30. * This iterator is suitable for update processes, or even a simple delete
  31. * algorithm. For example deleting a path:
  32. *
  33. * <pre>
  34. * final DirCache dirc = db.lockDirCache();
  35. * final DirCacheBuilder edit = dirc.builder();
  36. *
  37. * final TreeWalk walk = new TreeWalk(db);
  38. * walk.reset();
  39. * walk.setRecursive(true);
  40. * walk.setFilter(PathFilter.create(&quot;name/to/remove&quot;));
  41. * walk.addTree(new DirCacheBuildIterator(edit));
  42. *
  43. * while (walk.next())
  44. * ; // do nothing on a match as we want to remove matches
  45. * edit.commit();
  46. * </pre>
  47. */
  48. public class DirCacheBuildIterator extends DirCacheIterator {
  49. private final DirCacheBuilder builder;
  50. /**
  51. * Create a new iterator for an already loaded DirCache instance.
  52. * <p>
  53. * The iterator implementation may copy part of the cache's data during
  54. * construction, so the cache must be read in prior to creating the
  55. * iterator.
  56. *
  57. * @param dcb
  58. * the cache builder for the cache to walk. The cache must be
  59. * already loaded into memory.
  60. */
  61. public DirCacheBuildIterator(DirCacheBuilder dcb) {
  62. super(dcb.getDirCache());
  63. builder = dcb;
  64. }
  65. DirCacheBuildIterator(final DirCacheBuildIterator p,
  66. final DirCacheTree dct) {
  67. super(p, dct);
  68. builder = p.builder;
  69. }
  70. /** {@inheritDoc} */
  71. @Override
  72. public AbstractTreeIterator createSubtreeIterator(ObjectReader reader)
  73. throws IncorrectObjectTypeException, IOException {
  74. if (currentSubtree == null)
  75. throw new IncorrectObjectTypeException(getEntryObjectId(),
  76. Constants.TYPE_TREE);
  77. return new DirCacheBuildIterator(this, currentSubtree);
  78. }
  79. /** {@inheritDoc} */
  80. @Override
  81. public void skip() throws CorruptObjectException {
  82. if (currentSubtree != null)
  83. builder.keep(ptr, currentSubtree.getEntrySpan());
  84. else
  85. builder.keep(ptr, 1);
  86. next(1);
  87. }
  88. /** {@inheritDoc} */
  89. @Override
  90. public void stopWalk() {
  91. final int cur = ptr;
  92. final int cnt = cache.getEntryCount();
  93. if (cur < cnt)
  94. builder.keep(cur, cnt - cur);
  95. }
  96. /** {@inheritDoc} */
  97. @Override
  98. protected boolean needsStopWalk() {
  99. return ptr < cache.getEntryCount();
  100. }
  101. }