Support recreating a .git/index through DirCache
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / dircache / DirCacheBuilder.java
blobf484ccd69c7fc20ffc3da3c1dbc9dea8390b88cf
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.util.Arrays;
42 /**
43 * Updates a {@link DirCache} by adding individual {@link DirCacheEntry}s.
44 * <p>
45 * A builder always starts from a clean slate and appends in every single
46 * <code>DirCacheEntry</code> which the final updated index must have to reflect
47 * its new content.
48 * <p>
49 * For maximum performance applications should add entries in path name order.
50 * Adding entries out of order is permitted, however a final sorting pass will
51 * be implicitly performed during {@link #finish()} to correct any out-of-order
52 * entries. Duplicate detection is also delayed until the sorting is complete.
54 public class DirCacheBuilder extends BaseDirCacheEditor {
55 private boolean sorted;
57 /**
58 * Construct a new builder.
60 * @param dc
61 * the cache this builder will eventually update.
62 * @param ecnt
63 * estimated number of entries the builder will have upon
64 * completion. This sizes the initial entry table.
66 protected DirCacheBuilder(final DirCache dc, final int ecnt) {
67 super(dc, ecnt);
70 /**
71 * Append one entry into the resulting entry list.
72 * <p>
73 * The entry is placed at the end of the entry list. If the entry causes the
74 * list to now be incorrectly sorted a final sorting phase will be
75 * automatically enabled within {@link #finish()}.
76 * <p>
77 * The internal entry table is automatically expanded if there is
78 * insufficient space for the new addition.
80 * @param newEntry
81 * the new entry to add.
83 public void add(final DirCacheEntry newEntry) {
84 beforeAdd(newEntry);
85 fastAdd(newEntry);
88 /**
89 * Add a range of existing entries from the destination cache.
90 * <p>
91 * The entries are placed at the end of the entry list. If any of the
92 * entries causes the list to now be incorrectly sorted a final sorting
93 * phase will be automatically enabled within {@link #finish()}.
94 * <p>
95 * This method copies from the destination cache, which has not yet been
96 * updated with this editor's new table. So all offsets into the destination
97 * cache are not affected by any updates that may be currently taking place
98 * in this editor.
99 * <p>
100 * The internal entry table is automatically expanded if there is
101 * insufficient space for the new additions.
103 * @param pos
104 * first entry to copy from the destination cache.
105 * @param cnt
106 * number of entries to copy.
108 public void keep(final int pos, int cnt) {
109 beforeAdd(cache.getEntry(pos));
110 fastKeep(pos, cnt);
113 public void finish() {
114 if (!sorted)
115 resort();
116 replace();
119 private void beforeAdd(final DirCacheEntry newEntry) {
120 if (sorted && entryCnt > 0) {
121 final DirCacheEntry lastEntry = entries[entryCnt - 1];
122 final int cr = DirCache.cmp(lastEntry, newEntry);
123 if (cr > 0) {
124 // The new entry sorts before the old entry; we are
125 // no longer sorted correctly. We'll need to redo
126 // the sorting before we can close out the build.
128 sorted = false;
129 } else if (cr == 0) {
130 // Same file path; we can only insert this if the
131 // stages won't be violated.
133 final int peStage = lastEntry.getStage();
134 final int dceStage = newEntry.getStage();
135 if (peStage == dceStage)
136 throw bad(newEntry, "Duplicate stages not allowed");
137 if (peStage == 0 || dceStage == 0)
138 throw bad(newEntry, "Mixed stages not allowed");
139 if (peStage > dceStage)
140 sorted = false;
145 private void resort() {
146 Arrays.sort(entries, 0, entryCnt, DirCache.ENT_CMP);
148 for (int entryIdx = 1; entryIdx < entryCnt; entryIdx++) {
149 final DirCacheEntry pe = entries[entryIdx - 1];
150 final DirCacheEntry ce = entries[entryIdx];
151 final int cr = DirCache.cmp(pe, ce);
152 if (cr == 0) {
153 // Same file path; we can only allow this if the stages
154 // are 1-3 and no 0 exists.
156 final int peStage = pe.getStage();
157 final int ceStage = ce.getStage();
158 if (peStage == ceStage)
159 throw bad(ce, "Duplicate stages not allowed");
160 if (peStage == 0 || ceStage == 0)
161 throw bad(ce, "Mixed stages not allowed");
165 sorted = true;
168 private static IllegalStateException bad(final DirCacheEntry a,
169 final String msg) {
170 return new IllegalStateException(msg + ": " + a.getStage() + " "
171 + a.getPathString());