View Javadoc
1   /*
2    * Copyright (C) 2008, 2010, Google Inc.
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4    * Copyright (C) 2011, 2020, Matthias Sohn <matthias.sohn@sap.com> 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  
13  package org.eclipse.jgit.dircache;
14  
15  import static java.nio.charset.StandardCharsets.ISO_8859_1;
16  
17  import java.io.BufferedInputStream;
18  import java.io.BufferedOutputStream;
19  import java.io.EOFException;
20  import java.io.File;
21  import java.io.FileNotFoundException;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.security.DigestOutputStream;
26  import java.security.MessageDigest;
27  import java.text.MessageFormat;
28  import java.time.Instant;
29  import java.util.ArrayList;
30  import java.util.Arrays;
31  import java.util.Comparator;
32  import java.util.List;
33  
34  import org.eclipse.jgit.errors.CorruptObjectException;
35  import org.eclipse.jgit.errors.IndexReadException;
36  import org.eclipse.jgit.errors.LockFailedException;
37  import org.eclipse.jgit.errors.UnmergedPathException;
38  import org.eclipse.jgit.events.IndexChangedEvent;
39  import org.eclipse.jgit.events.IndexChangedListener;
40  import org.eclipse.jgit.internal.JGitText;
41  import org.eclipse.jgit.internal.storage.file.FileSnapshot;
42  import org.eclipse.jgit.internal.storage.file.LockFile;
43  import org.eclipse.jgit.lib.AnyObjectId;
44  import org.eclipse.jgit.lib.Config;
45  import org.eclipse.jgit.lib.Config.ConfigEnum;
46  import org.eclipse.jgit.lib.ConfigConstants;
47  import org.eclipse.jgit.lib.Constants;
48  import org.eclipse.jgit.lib.ObjectId;
49  import org.eclipse.jgit.lib.ObjectInserter;
50  import org.eclipse.jgit.lib.ObjectReader;
51  import org.eclipse.jgit.lib.Repository;
52  import org.eclipse.jgit.treewalk.FileTreeIterator;
53  import org.eclipse.jgit.treewalk.TreeWalk;
54  import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
55  import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
56  import org.eclipse.jgit.util.FS;
57  import org.eclipse.jgit.util.IO;
58  import org.eclipse.jgit.util.MutableInteger;
59  import org.eclipse.jgit.util.NB;
60  import org.eclipse.jgit.util.TemporaryBuffer;
61  import org.eclipse.jgit.util.io.SilentFileInputStream;
62  
63  /**
64   * Support for the Git dircache (aka index file).
65   * <p>
66   * The index file keeps track of which objects are currently checked out in the
67   * working directory, and the last modified time of those working files. Changes
68   * in the working directory can be detected by comparing the modification times
69   * to the cached modification time within the index file.
70   * <p>
71   * Index files are also used during merges, where the merge happens within the
72   * index file first, and the working directory is updated as a post-merge step.
73   * Conflicts are stored in the index file to allow tool (and human) based
74   * resolutions to be easily performed.
75   */
76  public class DirCache {
77  	private static final byte[] SIG_DIRC = { 'D', 'I', 'R', 'C' };
78  
79  	private static final int EXT_TREE = 0x54524545 /* 'TREE' */;
80  
81  	private static final DirCacheEntry[] NO_ENTRIES = {};
82  
83  	private static final byte[] NO_CHECKSUM = {};
84  
85  	static final Comparator<DirCacheEntry> ENT_CMP = (DirCacheEntry o1,
86  			DirCacheEntry o2) -> {
87  		final int cr = cmp(o1, o2);
88  		if (cr != 0)
89  			return cr;
90  		return o1.getStage() - o2.getStage();
91  	};
92  
93  	static int cmp(DirCacheEntry../../../../org/eclipse/jgit/dircache/DirCacheEntry.html#DirCacheEntry">DirCacheEntry a, DirCacheEntry b) {
94  		return cmp(a.path, a.path.length, b);
95  	}
96  
97  	static int cmp(byte[] aPath, int aLen, DirCacheEntry b) {
98  		return cmp(aPath, aLen, b.path, b.path.length);
99  	}
100 
101 	static int cmp(final byte[] aPath, final int aLen, final byte[] bPath,
102 			final int bLen) {
103 		for (int cPos = 0; cPos < aLen && cPos < bLen; cPos++) {
104 			final int cmp = (aPath[cPos] & 0xff) - (bPath[cPos] & 0xff);
105 			if (cmp != 0)
106 				return cmp;
107 		}
108 		return aLen - bLen;
109 	}
110 
111 	/**
112 	 * Create a new empty index which is never stored on disk.
113 	 *
114 	 * @return an empty cache which has no backing store file. The cache may not
115 	 *         be read or written, but it may be queried and updated (in
116 	 *         memory).
117 	 */
118 	public static DirCache newInCore() {
119 		return new DirCache(null, null);
120 	}
121 
122 	/**
123 	 * Create a new in memory index read from the contents of a tree.
124 	 *
125 	 * @param reader
126 	 *            reader to access the tree objects from a repository.
127 	 * @param treeId
128 	 *            tree to read. Must identify a tree, not a tree-ish.
129 	 * @return a new cache which has no backing store file, but contains the
130 	 *         contents of {@code treeId}.
131 	 * @throws java.io.IOException
132 	 *             one or more trees not available from the ObjectReader.
133 	 * @since 4.2
134 	 */
135 	public static DirCache read(ObjectReader reader, AnyObjectId treeId)
136 			throws IOException {
137 		DirCache d = newInCore();
138 		DirCacheBuilder b = d.builder();
139 		b.addTree(null, DirCacheEntry.STAGE_0, reader, treeId);
140 		b.finish();
141 		return d;
142 	}
143 
144 	/**
145 	 * Create a new in-core index representation and read an index from disk.
146 	 * <p>
147 	 * The new index will be read before it is returned to the caller. Read
148 	 * failures are reported as exceptions and therefore prevent the method from
149 	 * returning a partially populated index.
150 	 *
151 	 * @param repository
152 	 *            repository containing the index to read
153 	 * @return a cache representing the contents of the specified index file (if
154 	 *         it exists) or an empty cache if the file does not exist.
155 	 * @throws java.io.IOException
156 	 *             the index file is present but could not be read.
157 	 * @throws org.eclipse.jgit.errors.CorruptObjectException
158 	 *             the index file is using a format or extension that this
159 	 *             library does not support.
160 	 */
161 	public static DirCache read(Repository repository)
162 			throws CorruptObjectException, IOException {
163 		final DirCache c = read(repository.getIndexFile(), repository.getFS());
164 		c.repository = repository;
165 		return c;
166 	}
167 
168 	/**
169 	 * Create a new in-core index representation and read an index from disk.
170 	 * <p>
171 	 * The new index will be read before it is returned to the caller. Read
172 	 * failures are reported as exceptions and therefore prevent the method from
173 	 * returning a partially populated index.
174 	 *
175 	 * @param indexLocation
176 	 *            location of the index file on disk.
177 	 * @param fs
178 	 *            the file system abstraction which will be necessary to perform
179 	 *            certain file system operations.
180 	 * @return a cache representing the contents of the specified index file (if
181 	 *         it exists) or an empty cache if the file does not exist.
182 	 * @throws java.io.IOException
183 	 *             the index file is present but could not be read.
184 	 * @throws org.eclipse.jgit.errors.CorruptObjectException
185 	 *             the index file is using a format or extension that this
186 	 *             library does not support.
187 	 */
188 	public static DirCache read(File indexLocation, FS fs)
189 			throws CorruptObjectException, IOException {
190 		final DirCache/DirCache.html#DirCache">DirCache c = new DirCache(indexLocation, fs);
191 		c.read();
192 		return c;
193 	}
194 
195 	/**
196 	 * Create a new in-core index representation, lock it, and read from disk.
197 	 * <p>
198 	 * The new index will be locked and then read before it is returned to the
199 	 * caller. Read failures are reported as exceptions and therefore prevent
200 	 * the method from returning a partially populated index. On read failure,
201 	 * the lock is released.
202 	 *
203 	 * @param indexLocation
204 	 *            location of the index file on disk.
205 	 * @param fs
206 	 *            the file system abstraction which will be necessary to perform
207 	 *            certain file system operations.
208 	 * @return a cache representing the contents of the specified index file (if
209 	 *         it exists) or an empty cache if the file does not exist.
210 	 * @throws java.io.IOException
211 	 *             the index file is present but could not be read, or the lock
212 	 *             could not be obtained.
213 	 * @throws org.eclipse.jgit.errors.CorruptObjectException
214 	 *             the index file is using a format or extension that this
215 	 *             library does not support.
216 	 */
217 	public static DirCache lock(File indexLocation, FS fs)
218 			throws CorruptObjectException, IOException {
219 		final DirCache/DirCache.html#DirCache">DirCache c = new DirCache(indexLocation, fs);
220 		if (!c.lock())
221 			throw new LockFailedException(indexLocation);
222 
223 		try {
224 			c.read();
225 		} catch (IOException | RuntimeException | Error e) {
226 			c.unlock();
227 			throw e;
228 		}
229 
230 		return c;
231 	}
232 
233 	/**
234 	 * Create a new in-core index representation, lock it, and read from disk.
235 	 * <p>
236 	 * The new index will be locked and then read before it is returned to the
237 	 * caller. Read failures are reported as exceptions and therefore prevent
238 	 * the method from returning a partially populated index. On read failure,
239 	 * the lock is released.
240 	 *
241 	 * @param repository
242 	 *            repository containing the index to lock and read
243 	 * @param indexChangedListener
244 	 *            listener to be informed when DirCache is committed
245 	 * @return a cache representing the contents of the specified index file (if
246 	 *         it exists) or an empty cache if the file does not exist.
247 	 * @throws java.io.IOException
248 	 *             the index file is present but could not be read, or the lock
249 	 *             could not be obtained.
250 	 * @throws org.eclipse.jgit.errors.CorruptObjectException
251 	 *             the index file is using a format or extension that this
252 	 *             library does not support.
253 	 * @since 2.0
254 	 */
255 	public static DirCache lock(final Repository repository,
256 			final IndexChangedListener indexChangedListener)
257 			throws CorruptObjectException, IOException {
258 		DirCache c = lock(repository.getIndexFile(), repository.getFS(),
259 				indexChangedListener);
260 		c.repository = repository;
261 		return c;
262 	}
263 
264 	/**
265 	 * Create a new in-core index representation, lock it, and read from disk.
266 	 * <p>
267 	 * The new index will be locked and then read before it is returned to the
268 	 * caller. Read failures are reported as exceptions and therefore prevent
269 	 * the method from returning a partially populated index. On read failure,
270 	 * the lock is released.
271 	 *
272 	 * @param indexLocation
273 	 *            location of the index file on disk.
274 	 * @param fs
275 	 *            the file system abstraction which will be necessary to perform
276 	 *            certain file system operations.
277 	 * @param indexChangedListener
278 	 *            listener to be informed when DirCache is committed
279 	 * @return a cache representing the contents of the specified index file (if
280 	 *         it exists) or an empty cache if the file does not exist.
281 	 * @throws java.io.IOException
282 	 *             the index file is present but could not be read, or the lock
283 	 *             could not be obtained.
284 	 * @throws org.eclipse.jgit.errors.CorruptObjectException
285 	 *             the index file is using a format or extension that this
286 	 *             library does not support.
287 	 */
288 	public static DirCache lock(final File indexLocation, final FS fs,
289 			IndexChangedListener indexChangedListener)
290 			throws CorruptObjectException,
291 			IOException {
292 		DirCache c = lock(indexLocation, fs);
293 		c.registerIndexChangedListener(indexChangedListener);
294 		return c;
295 	}
296 
297 	/** Location of the current version of the index file. */
298 	private final File liveFile;
299 
300 	/** Individual file index entries, sorted by path name. */
301 	private DirCacheEntry[] sortedEntries;
302 
303 	/** Number of positions within {@link #sortedEntries} that are valid. */
304 	private int entryCnt;
305 
306 	/** Cache tree for this index; null if the cache tree is not available. */
307 	private DirCacheTree tree;
308 
309 	/** Our active lock (if we hold it); null if we don't have it locked. */
310 	private LockFile myLock;
311 
312 	/** Keep track of whether the index has changed or not */
313 	private FileSnapshot snapshot;
314 
315 	/** index checksum when index was read from disk */
316 	private byte[] readIndexChecksum;
317 
318 	/** index checksum when index was written to disk */
319 	private byte[] writeIndexChecksum;
320 
321 	/** listener to be informed on commit */
322 	private IndexChangedListener indexChangedListener;
323 
324 	/** Repository containing this index */
325 	private Repository repository;
326 
327 	/** If we read this index from disk, the original format. */
328 	private DirCacheVersion version;
329 
330 	/**
331 	 * Create a new in-core index representation.
332 	 * <p>
333 	 * The new index will be empty. Callers may wish to read from the on disk
334 	 * file first with {@link #read()}.
335 	 *
336 	 * @param indexLocation
337 	 *            location of the index file on disk.
338 	 * @param fs
339 	 *            the file system abstraction which will be necessary to perform
340 	 *            certain file system operations.
341 	 */
342 	public DirCache(File indexLocation, FS fs) {
343 		liveFile = indexLocation;
344 		clear();
345 	}
346 
347 	/**
348 	 * Create a new builder to update this cache.
349 	 * <p>
350 	 * Callers should add all entries to the builder, then use
351 	 * {@link org.eclipse.jgit.dircache.DirCacheBuilder#finish()} to update this
352 	 * instance.
353 	 *
354 	 * @return a new builder instance for this cache.
355 	 */
356 	public DirCacheBuilder builder() {
357 		return new DirCacheBuilder(this, entryCnt + 16);
358 	}
359 
360 	/**
361 	 * Create a new editor to recreate this cache.
362 	 * <p>
363 	 * Callers should add commands to the editor, then use
364 	 * {@link org.eclipse.jgit.dircache.DirCacheEditor#finish()} to update this
365 	 * instance.
366 	 *
367 	 * @return a new builder instance for this cache.
368 	 */
369 	public DirCacheEditor editor() {
370 		return new DirCacheEditor(this, entryCnt + 16);
371 	}
372 
373 	DirCacheVersion getVersion() {
374 		return version;
375 	}
376 
377 	void replace(DirCacheEntry[] e, int cnt) {
378 		sortedEntries = e;
379 		entryCnt = cnt;
380 		tree = null;
381 	}
382 
383 	/**
384 	 * Read the index from disk, if it has changed on disk.
385 	 * <p>
386 	 * This method tries to avoid loading the index if it has not changed since
387 	 * the last time we consulted it. A missing index file will be treated as
388 	 * though it were present but had no file entries in it.
389 	 *
390 	 * @throws java.io.IOException
391 	 *             the index file is present but could not be read. This
392 	 *             DirCache instance may not be populated correctly.
393 	 * @throws org.eclipse.jgit.errors.CorruptObjectException
394 	 *             the index file is using a format or extension that this
395 	 *             library does not support.
396 	 */
397 	public void read() throws IOException, CorruptObjectException {
398 		if (liveFile == null)
399 			throw new IOException(JGitText.get().dirCacheDoesNotHaveABackingFile);
400 		if (!liveFile.exists())
401 			clear();
402 		else if (snapshot == null || snapshot.isModified(liveFile)) {
403 			try (SilentFileInputStreamm.html#SilentFileInputStream">SilentFileInputStream inStream = new SilentFileInputStream(
404 					liveFile)) {
405 				clear();
406 				readFrom(inStream);
407 			} catch (FileNotFoundException fnfe) {
408 				if (liveFile.exists()) {
409 					// Panic: the index file exists but we can't read it
410 					throw new IndexReadException(
411 							MessageFormat.format(JGitText.get().cannotReadIndex,
412 									liveFile.getAbsolutePath(), fnfe));
413 				}
414 				// Someone must have deleted it between our exists test
415 				// and actually opening the path. That's fine, its empty.
416 				//
417 				clear();
418 			}
419 			snapshot = FileSnapshot.save(liveFile);
420 		}
421 	}
422 
423 	/**
424 	 * Whether the memory state differs from the index file
425 	 *
426 	 * @return {@code true} if the memory state differs from the index file
427 	 * @throws java.io.IOException
428 	 */
429 	public boolean isOutdated() throws IOException {
430 		if (liveFile == null || !liveFile.exists())
431 			return false;
432 		return snapshot == null || snapshot.isModified(liveFile);
433 	}
434 
435 	/**
436 	 * Empty this index, removing all entries.
437 	 */
438 	public void clear() {
439 		snapshot = null;
440 		sortedEntries = NO_ENTRIES;
441 		entryCnt = 0;
442 		tree = null;
443 		readIndexChecksum = NO_CHECKSUM;
444 	}
445 
446 	private void readFrom(InputStream inStream) throws IOException,
447 			CorruptObjectException {
448 		final BufferedInputStream in = new BufferedInputStream(inStream);
449 		final MessageDigest md = Constants.newMessageDigest();
450 
451 		// Read the index header and verify we understand it.
452 		//
453 		final byte[] hdr = new byte[20];
454 		IO.readFully(in, hdr, 0, 12);
455 		md.update(hdr, 0, 12);
456 		if (!is_DIRC(hdr))
457 			throw new CorruptObjectException(JGitText.get().notADIRCFile);
458 		int versionCode = NB.decodeInt32(hdr, 4);
459 		DirCacheVersion ver = DirCacheVersion.fromInt(versionCode);
460 		if (ver == null) {
461 			throw new CorruptObjectException(
462 					MessageFormat.format(JGitText.get().unknownDIRCVersion,
463 							Integer.valueOf(versionCode)));
464 		}
465 		boolean extended = false;
466 		switch (ver) {
467 		case DIRC_VERSION_MINIMUM:
468 			break;
469 		case DIRC_VERSION_EXTENDED:
470 		case DIRC_VERSION_PATHCOMPRESS:
471 			extended = true;
472 			break;
473 		default:
474 			throw new CorruptObjectException(MessageFormat
475 					.format(JGitText.get().unknownDIRCVersion, ver));
476 		}
477 		version = ver;
478 		entryCnt = NB.decodeInt32(hdr, 8);
479 		if (entryCnt < 0)
480 			throw new CorruptObjectException(JGitText.get().DIRCHasTooManyEntries);
481 
482 		snapshot = FileSnapshot.save(liveFile);
483 		Instant smudge = snapshot.lastModifiedInstant();
484 
485 		// Load the individual file entries.
486 		//
487 		final int infoLength = DirCacheEntry.getMaximumInfoLength(extended);
488 		final byte[] infos = new byte[infoLength * entryCnt];
489 		sortedEntries = new DirCacheEntry[entryCnt];
490 
491 		final MutableInteger.html#MutableInteger">MutableInteger infoAt = new MutableInteger();
492 		for (int i = 0; i < entryCnt; i++) {
493 			sortedEntries[i] = new DirCacheEntry(infos, infoAt, in, md, smudge,
494 					version, i == 0 ? null : sortedEntries[i - 1]);
495 		}
496 
497 		// After the file entries are index extensions, and then a footer.
498 		//
499 		for (;;) {
500 			in.mark(21);
501 			IO.readFully(in, hdr, 0, 20);
502 			if (in.read() < 0) {
503 				// No extensions present; the file ended where we expected.
504 				//
505 				break;
506 			}
507 
508 			in.reset();
509 			md.update(hdr, 0, 8);
510 			IO.skipFully(in, 8);
511 
512 			long sz = NB.decodeUInt32(hdr, 4);
513 			switch (NB.decodeInt32(hdr, 0)) {
514 			case EXT_TREE: {
515 				if (Integer.MAX_VALUE < sz) {
516 					throw new CorruptObjectException(MessageFormat.format(
517 							JGitText.get().DIRCExtensionIsTooLargeAt,
518 							formatExtensionName(hdr), Long.valueOf(sz)));
519 				}
520 				final byte[] raw = new byte[(int) sz];
521 				IO.readFully(in, raw, 0, raw.length);
522 				md.update(raw, 0, raw.length);
523 				tree = new DirCacheTree(raw, new MutableInteger(), null);
524 				break;
525 			}
526 			default:
527 				if (hdr[0] >= 'A' && hdr[0] <= 'Z') {
528 					// The extension is optional and is here only as
529 					// a performance optimization. Since we do not
530 					// understand it, we can safely skip past it, after
531 					// we include its data in our checksum.
532 					//
533 					skipOptionalExtension(in, md, hdr, sz);
534 				} else {
535 					// The extension is not an optimization and is
536 					// _required_ to understand this index format.
537 					// Since we did not trap it above we must abort.
538 					//
539 					throw new CorruptObjectException(MessageFormat.format(JGitText.get().DIRCExtensionNotSupportedByThisVersion
540 							, formatExtensionName(hdr)));
541 				}
542 			}
543 		}
544 
545 		readIndexChecksum = md.digest();
546 		if (!Arrays.equals(readIndexChecksum, hdr)) {
547 			throw new CorruptObjectException(JGitText.get().DIRCChecksumMismatch);
548 		}
549 	}
550 
551 	private void skipOptionalExtension(final InputStream in,
552 			final MessageDigest md, final byte[] hdr, long sz)
553 			throws IOException {
554 		final byte[] b = new byte[4096];
555 		while (0 < sz) {
556 			int n = in.read(b, 0, (int) Math.min(b.length, sz));
557 			if (n < 0) {
558 				throw new EOFException(
559 						MessageFormat.format(
560 								JGitText.get().shortReadOfOptionalDIRCExtensionExpectedAnotherBytes,
561 								formatExtensionName(hdr), Long.valueOf(sz)));
562 			}
563 			md.update(b, 0, n);
564 			sz -= n;
565 		}
566 	}
567 
568 	private static String formatExtensionName(byte[] hdr) {
569 		return "'" + new String(hdr, 0, 4, ISO_8859_1) + "'"; //$NON-NLS-1$ //$NON-NLS-2$
570 	}
571 
572 	private static boolean is_DIRC(byte[] hdr) {
573 		if (hdr.length < SIG_DIRC.length)
574 			return false;
575 		for (int i = 0; i < SIG_DIRC.length; i++)
576 			if (hdr[i] != SIG_DIRC[i])
577 				return false;
578 		return true;
579 	}
580 
581 	/**
582 	 * Try to establish an update lock on the cache file.
583 	 *
584 	 * @return true if the lock is now held by the caller; false if it is held
585 	 *         by someone else.
586 	 * @throws java.io.IOException
587 	 *             the output file could not be created. The caller does not
588 	 *             hold the lock.
589 	 */
590 	public boolean lock() throws IOException {
591 		if (liveFile == null)
592 			throw new IOException(JGitText.get().dirCacheDoesNotHaveABackingFile);
593 		final LockFiletorage/file/LockFile.html#LockFile">LockFile tmp = new LockFile(liveFile);
594 		if (tmp.lock()) {
595 			tmp.setNeedStatInformation(true);
596 			myLock = tmp;
597 			return true;
598 		}
599 		return false;
600 	}
601 
602 	/**
603 	 * Write the entry records from memory to disk.
604 	 * <p>
605 	 * The cache must be locked first by calling {@link #lock()} and receiving
606 	 * true as the return value. Applications are encouraged to lock the index,
607 	 * then invoke {@link #read()} to ensure the in-memory data is current,
608 	 * prior to updating the in-memory entries.
609 	 * <p>
610 	 * Once written the lock is closed and must be either committed with
611 	 * {@link #commit()} or rolled back with {@link #unlock()}.
612 	 *
613 	 * @throws java.io.IOException
614 	 *             the output file could not be created. The caller no longer
615 	 *             holds the lock.
616 	 */
617 	public void write() throws IOException {
618 		final LockFile tmp = myLock;
619 		requireLocked(tmp);
620 		try (OutputStream o = tmp.getOutputStream();
621 				OutputStream bo = new BufferedOutputStream(o)) {
622 			writeTo(liveFile.getParentFile(), bo);
623 		} catch (IOException | RuntimeException | Error err) {
624 			tmp.unlock();
625 			throw err;
626 		}
627 	}
628 
629 	void writeTo(File dir, OutputStream os) throws IOException {
630 		final MessageDigest foot = Constants.newMessageDigest();
631 		final DigestOutputStream dos = new DigestOutputStream(os, foot);
632 
633 		if (version == null && this.repository != null) {
634 			// A new DirCache is being written.
635 			DirCacheConfig config = repository.getConfig()
636 					.get(DirCacheConfig::new);
637 			version = config.getIndexVersion();
638 		}
639 		if (version == null
640 				|| version == DirCacheVersion.DIRC_VERSION_MINIMUM) {
641 			version = DirCacheVersion.DIRC_VERSION_MINIMUM;
642 			for (int i = 0; i < entryCnt; i++) {
643 				if (sortedEntries[i].isExtended()) {
644 					version = DirCacheVersion.DIRC_VERSION_EXTENDED;
645 					break;
646 				}
647 			}
648 		}
649 
650 		// Write the header.
651 		//
652 		final byte[] tmp = new byte[128];
653 		System.arraycopy(SIG_DIRC, 0, tmp, 0, SIG_DIRC.length);
654 		NB.encodeInt32(tmp, 4, version.getVersionCode());
655 		NB.encodeInt32(tmp, 8, entryCnt);
656 		dos.write(tmp, 0, 12);
657 
658 		// Write the individual file entries.
659 
660 		Instant smudge;
661 		if (myLock != null) {
662 			// For new files we need to smudge the index entry
663 			// if they have been modified "now". Ideally we'd
664 			// want the timestamp when we're done writing the index,
665 			// so we use the current timestamp as a approximation.
666 			myLock.createCommitSnapshot();
667 			snapshot = myLock.getCommitSnapshot();
668 			smudge = snapshot.lastModifiedInstant();
669 		} else {
670 			// Used in unit tests only
671 			smudge = Instant.EPOCH;
672 		}
673 
674 		// Check if tree is non-null here since calling updateSmudgedEntries
675 		// will automatically build it via creating a DirCacheIterator
676 		final boolean writeTree = tree != null;
677 
678 		if (repository != null && entryCnt > 0)
679 			updateSmudgedEntries();
680 
681 		for (int i = 0; i < entryCnt; i++) {
682 			final DirCacheEntry e = sortedEntries[i];
683 			if (e.mightBeRacilyClean(smudge)) {
684 				e.smudgeRacilyClean();
685 			}
686 			e.write(dos, version, i == 0 ? null : sortedEntries[i - 1]);
687 		}
688 
689 		if (writeTree) {
690 			@SuppressWarnings("resource") // Explicitly closed in try block, and
691 											// destroyed in finally
692 			TemporaryBuffer bb = new TemporaryBuffer.LocalFile(dir, 5 << 20);
693 			try {
694 				tree.write(tmp, bb);
695 				bb.close();
696 
697 				NB.encodeInt32(tmp, 0, EXT_TREE);
698 				NB.encodeInt32(tmp, 4, (int) bb.length());
699 				dos.write(tmp, 0, 8);
700 				bb.writeTo(dos, null);
701 			} finally {
702 				bb.destroy();
703 			}
704 		}
705 		writeIndexChecksum = foot.digest();
706 		os.write(writeIndexChecksum);
707 		os.close();
708 	}
709 
710 	/**
711 	 * Commit this change and release the lock.
712 	 * <p>
713 	 * If this method fails (returns false) the lock is still released.
714 	 *
715 	 * @return true if the commit was successful and the file contains the new
716 	 *         data; false if the commit failed and the file remains with the
717 	 *         old data.
718 	 * @throws java.lang.IllegalStateException
719 	 *             the lock is not held.
720 	 */
721 	public boolean commit() {
722 		final LockFile tmp = myLock;
723 		requireLocked(tmp);
724 		myLock = null;
725 		if (!tmp.commit()) {
726 			return false;
727 		}
728 		snapshot = tmp.getCommitSnapshot();
729 		if (indexChangedListener != null
730 				&& !Arrays.equals(readIndexChecksum, writeIndexChecksum)) {
731 			indexChangedListener.onIndexChanged(new IndexChangedEvent(true));
732 		}
733 		return true;
734 	}
735 
736 	private void requireLocked(LockFile tmp) {
737 		if (liveFile == null)
738 			throw new IllegalStateException(JGitText.get().dirCacheIsNotLocked);
739 		if (tmp == null)
740 			throw new IllegalStateException(MessageFormat.format(JGitText.get().dirCacheFileIsNotLocked
741 					, liveFile.getAbsolutePath()));
742 	}
743 
744 	/**
745 	 * Unlock this file and abort this change.
746 	 * <p>
747 	 * The temporary file (if created) is deleted before returning.
748 	 */
749 	public void unlock() {
750 		final LockFile tmp = myLock;
751 		if (tmp != null) {
752 			myLock = null;
753 			tmp.unlock();
754 		}
755 	}
756 
757 	/**
758 	 * Locate the position a path's entry is at in the index. For details refer
759 	 * to #findEntry(byte[], int).
760 	 *
761 	 * @param path
762 	 *            the path to search for.
763 	 * @return if &gt;= 0 then the return value is the position of the entry in
764 	 *         the index; pass to {@link #getEntry(int)} to obtain the entry
765 	 *         information. If &lt; 0 the entry does not exist in the index.
766 	 */
767 	public int findEntry(String path) {
768 		final byte[] p = Constants.encode(path);
769 		return findEntry(p, p.length);
770 	}
771 
772 	/**
773 	 * Locate the position a path's entry is at in the index.
774 	 * <p>
775 	 * If there is at least one entry in the index for this path the position of
776 	 * the lowest stage is returned. Subsequent stages can be identified by
777 	 * testing consecutive entries until the path differs.
778 	 * <p>
779 	 * If no path matches the entry -(position+1) is returned, where position is
780 	 * the location it would have gone within the index.
781 	 *
782 	 * @param p
783 	 *            the byte array starting with the path to search for.
784 	 * @param pLen
785 	 *            the length of the path in bytes
786 	 * @return if &gt;= 0 then the return value is the position of the entry in
787 	 *         the index; pass to {@link #getEntry(int)} to obtain the entry
788 	 *         information. If &lt; 0 the entry does not exist in the index.
789 	 * @since 3.4
790 	 */
791 	public int findEntry(byte[] p, int pLen) {
792 		return findEntry(0, p, pLen);
793 	}
794 
795 	int findEntry(int low, byte[] p, int pLen) {
796 		int high = entryCnt;
797 		while (low < high) {
798 			int mid = (low + high) >>> 1;
799 			final int cmp = cmp(p, pLen, sortedEntries[mid]);
800 			if (cmp < 0)
801 				high = mid;
802 			else if (cmp == 0) {
803 				while (mid > 0 && cmp(p, pLen, sortedEntries[mid - 1]) == 0)
804 					mid--;
805 				return mid;
806 			} else
807 				low = mid + 1;
808 		}
809 		return -(low + 1);
810 	}
811 
812 	/**
813 	 * Determine the next index position past all entries with the same name.
814 	 * <p>
815 	 * As index entries are sorted by path name, then stage number, this method
816 	 * advances the supplied position to the first position in the index whose
817 	 * path name does not match the path name of the supplied position's entry.
818 	 *
819 	 * @param position
820 	 *            entry position of the path that should be skipped.
821 	 * @return position of the next entry whose path is after the input.
822 	 */
823 	public int nextEntry(int position) {
824 		DirCacheEntry last = sortedEntries[position];
825 		int nextIdx = position + 1;
826 		while (nextIdx < entryCnt) {
827 			final DirCacheEntry next = sortedEntries[nextIdx];
828 			if (cmp(last, next) != 0)
829 				break;
830 			last = next;
831 			nextIdx++;
832 		}
833 		return nextIdx;
834 	}
835 
836 	int nextEntry(byte[] p, int pLen, int nextIdx) {
837 		while (nextIdx < entryCnt) {
838 			final DirCacheEntry next = sortedEntries[nextIdx];
839 			if (!DirCacheTree.peq(p, next.path, pLen))
840 				break;
841 			nextIdx++;
842 		}
843 		return nextIdx;
844 	}
845 
846 	/**
847 	 * Total number of file entries stored in the index.
848 	 * <p>
849 	 * This count includes unmerged stages for a file entry if the file is
850 	 * currently conflicted in a merge. This means the total number of entries
851 	 * in the index may be up to 3 times larger than the number of files in the
852 	 * working directory.
853 	 * <p>
854 	 * Note that this value counts only <i>files</i>.
855 	 *
856 	 * @return number of entries available.
857 	 * @see #getEntry(int)
858 	 */
859 	public int getEntryCount() {
860 		return entryCnt;
861 	}
862 
863 	/**
864 	 * Get a specific entry.
865 	 *
866 	 * @param i
867 	 *            position of the entry to get.
868 	 * @return the entry at position <code>i</code>.
869 	 */
870 	public DirCacheEntry getEntry(int i) {
871 		return sortedEntries[i];
872 	}
873 
874 	/**
875 	 * Get a specific entry.
876 	 *
877 	 * @param path
878 	 *            the path to search for.
879 	 * @return the entry for the given <code>path</code>.
880 	 */
881 	public DirCacheEntry getEntry(String path) {
882 		final int i = findEntry(path);
883 		return i < 0 ? null : sortedEntries[i];
884 	}
885 
886 	/**
887 	 * Recursively get all entries within a subtree.
888 	 *
889 	 * @param path
890 	 *            the subtree path to get all entries within.
891 	 * @return all entries recursively contained within the subtree.
892 	 */
893 	public DirCacheEntry[] getEntriesWithin(String path) {
894 		if (path.length() == 0) {
895 			DirCacheEntry[] r = new DirCacheEntry[entryCnt];
896 			System.arraycopy(sortedEntries, 0, r, 0, entryCnt);
897 			return r;
898 		}
899 		if (!path.endsWith("/")) //$NON-NLS-1$
900 			path += "/"; //$NON-NLS-1$
901 		final byte[] p = Constants.encode(path);
902 		final int pLen = p.length;
903 
904 		int eIdx = findEntry(p, pLen);
905 		if (eIdx < 0)
906 			eIdx = -(eIdx + 1);
907 		final int lastIdx = nextEntry(p, pLen, eIdx);
908 		final DirCacheEntryheEntry.html#DirCacheEntry">DirCacheEntry[] r = new DirCacheEntry[lastIdx - eIdx];
909 		System.arraycopy(sortedEntries, eIdx, r, 0, r.length);
910 		return r;
911 	}
912 
913 	void toArray(final int i, final DirCacheEntry[] dst, final int off,
914 			final int cnt) {
915 		System.arraycopy(sortedEntries, i, dst, off, cnt);
916 	}
917 
918 	/**
919 	 * Obtain (or build) the current cache tree structure.
920 	 * <p>
921 	 * This method can optionally recreate the cache tree, without flushing the
922 	 * tree objects themselves to disk.
923 	 *
924 	 * @param build
925 	 *            if true and the cache tree is not present in the index it will
926 	 *            be generated and returned to the caller.
927 	 * @return the cache tree; null if there is no current cache tree available
928 	 *         and <code>build</code> was false.
929 	 */
930 	public DirCacheTree getCacheTree(boolean build) {
931 		if (build) {
932 			if (tree == null)
933 				tree = new DirCacheTree();
934 			tree.validate(sortedEntries, entryCnt, 0, 0);
935 		}
936 		return tree;
937 	}
938 
939 	/**
940 	 * Write all index trees to the object store, returning the root tree.
941 	 *
942 	 * @param ow
943 	 *            the writer to use when serializing to the store. The caller is
944 	 *            responsible for flushing the inserter before trying to use the
945 	 *            returned tree identity.
946 	 * @return identity for the root tree.
947 	 * @throws org.eclipse.jgit.errors.UnmergedPathException
948 	 *             one or more paths contain higher-order stages (stage &gt; 0),
949 	 *             which cannot be stored in a tree object.
950 	 * @throws java.lang.IllegalStateException
951 	 *             one or more paths contain an invalid mode which should never
952 	 *             appear in a tree object.
953 	 * @throws java.io.IOException
954 	 *             an unexpected error occurred writing to the object store.
955 	 */
956 	public ObjectId writeTree(ObjectInserter ow)
957 			throws UnmergedPathException, IOException {
958 		return getCacheTree(true).writeTree(sortedEntries, 0, 0, ow);
959 	}
960 
961 	/**
962 	 * Tells whether this index contains unmerged paths.
963 	 *
964 	 * @return {@code true} if this index contains unmerged paths. Means: at
965 	 *         least one entry is of a stage different from 0. {@code false}
966 	 *         will be returned if all entries are of stage 0.
967 	 */
968 	public boolean hasUnmergedPaths() {
969 		for (int i = 0; i < entryCnt; i++) {
970 			if (sortedEntries[i].getStage() > 0) {
971 				return true;
972 			}
973 		}
974 		return false;
975 	}
976 
977 	private void registerIndexChangedListener(IndexChangedListener listener) {
978 		this.indexChangedListener = listener;
979 	}
980 
981 	/**
982 	 * Update any smudged entries with information from the working tree.
983 	 *
984 	 * @throws IOException
985 	 */
986 	private void updateSmudgedEntries() throws IOException {
987 		List<String> paths = new ArrayList<>(128);
988 		try (TreeWalkeeWalk.html#TreeWalk">TreeWalk walk = new TreeWalk(repository)) {
989 			walk.setOperationType(OperationType.CHECKIN_OP);
990 			for (int i = 0; i < entryCnt; i++)
991 				if (sortedEntries[i].isSmudged())
992 					paths.add(sortedEntries[i].getPathString());
993 			if (paths.isEmpty())
994 				return;
995 			walk.setFilter(PathFilterGroup.createFromStrings(paths));
996 
997 			DirCacheIterator iIter = new DirCacheIterator(this);
998 			FileTreeIterator fIter = new FileTreeIterator(repository);
999 			walk.addTree(iIter);
1000 			walk.addTree(fIter);
1001 			fIter.setDirCacheIterator(walk, 0);
1002 			walk.setRecursive(true);
1003 			while (walk.next()) {
1004 				iIter = walk.getTree(0, DirCacheIterator.class);
1005 				if (iIter == null)
1006 					continue;
1007 				fIter = walk.getTree(1, FileTreeIterator.class);
1008 				if (fIter == null)
1009 					continue;
1010 				DirCacheEntry entry = iIter.getDirCacheEntry();
1011 				if (entry.isSmudged() && iIter.idEqual(fIter)) {
1012 					entry.setLength(fIter.getEntryLength());
1013 					entry.setLastModified(fIter.getEntryLastModifiedInstant());
1014 				}
1015 			}
1016 		}
1017 	}
1018 
1019 	enum DirCacheVersion implements ConfigEnum {
1020 
1021 		/** Minimum index version on-disk format that we support. */
1022 		DIRC_VERSION_MINIMUM(2),
1023 		/** Version 3 supports extended flags. */
1024 		DIRC_VERSION_EXTENDED(3),
1025 		/**
1026 		 * Version 4 adds very simple "path compression": it strips out the
1027 		 * common prefix between the last entry written and the current entry.
1028 		 * Instead of writing two entries with paths "foo/bar/baz/a.txt" and
1029 		 * "foo/bar/baz/b.txt" it only writes "b.txt" for the second entry.
1030 		 * <p>
1031 		 * It is also <em>not</em> padded.
1032 		 * </p>
1033 		 */
1034 		DIRC_VERSION_PATHCOMPRESS(4);
1035 
1036 		private final int version;
1037 
1038 		private DirCacheVersion(int versionCode) {
1039 			this.version = versionCode;
1040 		}
1041 
1042 		public int getVersionCode() {
1043 			return version;
1044 		}
1045 
1046 		@Override
1047 		public String toConfigValue() {
1048 			return Integer.toString(version);
1049 		}
1050 
1051 		@Override
1052 		public boolean matchConfigValue(String in) {
1053 			try {
1054 				return version == Integer.parseInt(in);
1055 			} catch (NumberFormatException e) {
1056 				return false;
1057 			}
1058 		}
1059 
1060 		public static DirCacheVersion fromInt(int val) {
1061 			for (DirCacheVersion v : DirCacheVersion.values()) {
1062 				if (val == v.getVersionCode()) {
1063 					return v;
1064 				}
1065 			}
1066 			return null;
1067 		}
1068 	}
1069 
1070 	private static class DirCacheConfig {
1071 
1072 		private final DirCacheVersion indexVersion;
1073 
1074 		public DirCacheConfig(Config cfg) {
1075 			boolean manyFiles = cfg.getBoolean(
1076 					ConfigConstants.CONFIG_FEATURE_SECTION,
1077 					ConfigConstants.CONFIG_KEY_MANYFILES, false);
1078 			indexVersion = cfg.getEnum(DirCacheVersion.values(),
1079 					ConfigConstants.CONFIG_INDEX_SECTION, null,
1080 					ConfigConstants.CONFIG_KEY_VERSION,
1081 					manyFiles ? DirCacheVersion.DIRC_VERSION_PATHCOMPRESS
1082 							: DirCacheVersion.DIRC_VERSION_EXTENDED);
1083 		}
1084 
1085 		public DirCacheVersion getIndexVersion() {
1086 			return indexVersion;
1087 		}
1088 
1089 	}
1090 }