Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DirCache.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.dircache;
  45. import java.io.BufferedInputStream;
  46. import java.io.BufferedOutputStream;
  47. import java.io.EOFException;
  48. import java.io.File;
  49. import java.io.FileInputStream;
  50. import java.io.FileNotFoundException;
  51. import java.io.IOException;
  52. import java.io.InputStream;
  53. import java.io.OutputStream;
  54. import java.io.UnsupportedEncodingException;
  55. import java.security.DigestOutputStream;
  56. import java.security.MessageDigest;
  57. import java.text.MessageFormat;
  58. import java.util.Arrays;
  59. import java.util.Comparator;
  60. import org.eclipse.jgit.JGitText;
  61. import org.eclipse.jgit.errors.CorruptObjectException;
  62. import org.eclipse.jgit.errors.UnmergedPathException;
  63. import org.eclipse.jgit.lib.Constants;
  64. import org.eclipse.jgit.lib.ObjectId;
  65. import org.eclipse.jgit.lib.ObjectInserter;
  66. import org.eclipse.jgit.lib.Repository;
  67. import org.eclipse.jgit.storage.file.LockFile;
  68. import org.eclipse.jgit.util.IO;
  69. import org.eclipse.jgit.util.MutableInteger;
  70. import org.eclipse.jgit.util.NB;
  71. import org.eclipse.jgit.util.TemporaryBuffer;
  72. /**
  73. * Support for the Git dircache (aka index file).
  74. * <p>
  75. * The index file keeps track of which objects are currently checked out in the
  76. * working directory, and the last modified time of those working files. Changes
  77. * in the working directory can be detected by comparing the modification times
  78. * to the cached modification time within the index file.
  79. * <p>
  80. * Index files are also used during merges, where the merge happens within the
  81. * index file first, and the working directory is updated as a post-merge step.
  82. * Conflicts are stored in the index file to allow tool (and human) based
  83. * resolutions to be easily performed.
  84. */
  85. public class DirCache {
  86. private static final byte[] SIG_DIRC = { 'D', 'I', 'R', 'C' };
  87. private static final int EXT_TREE = 0x54524545 /* 'TREE' */;
  88. private static final int INFO_LEN = DirCacheEntry.INFO_LEN;
  89. private static final DirCacheEntry[] NO_ENTRIES = {};
  90. static final Comparator<DirCacheEntry> ENT_CMP = new Comparator<DirCacheEntry>() {
  91. public int compare(final DirCacheEntry o1, final DirCacheEntry o2) {
  92. final int cr = cmp(o1, o2);
  93. if (cr != 0)
  94. return cr;
  95. return o1.getStage() - o2.getStage();
  96. }
  97. };
  98. static int cmp(final DirCacheEntry a, final DirCacheEntry b) {
  99. return cmp(a.path, a.path.length, b);
  100. }
  101. static int cmp(final byte[] aPath, final int aLen, final DirCacheEntry b) {
  102. return cmp(aPath, aLen, b.path, b.path.length);
  103. }
  104. static int cmp(final byte[] aPath, final int aLen, final byte[] bPath,
  105. final int bLen) {
  106. for (int cPos = 0; cPos < aLen && cPos < bLen; cPos++) {
  107. final int cmp = (aPath[cPos] & 0xff) - (bPath[cPos] & 0xff);
  108. if (cmp != 0)
  109. return cmp;
  110. }
  111. return aLen - bLen;
  112. }
  113. /**
  114. * Create a new empty index which is never stored on disk.
  115. *
  116. * @return an empty cache which has no backing store file. The cache may not
  117. * be read or written, but it may be queried and updated (in
  118. * memory).
  119. */
  120. public static DirCache newInCore() {
  121. return new DirCache(null);
  122. }
  123. /**
  124. * Create a new in-core index representation and read an index from disk.
  125. * <p>
  126. * The new index will be read before it is returned to the caller. Read
  127. * failures are reported as exceptions and therefore prevent the method from
  128. * returning a partially populated index.
  129. *
  130. * @param indexLocation
  131. * location of the index file on disk.
  132. * @return a cache representing the contents of the specified index file (if
  133. * it exists) or an empty cache if the file does not exist.
  134. * @throws IOException
  135. * the index file is present but could not be read.
  136. * @throws CorruptObjectException
  137. * the index file is using a format or extension that this
  138. * library does not support.
  139. */
  140. public static DirCache read(final File indexLocation)
  141. throws CorruptObjectException, IOException {
  142. final DirCache c = new DirCache(indexLocation);
  143. c.read();
  144. return c;
  145. }
  146. /**
  147. * Create a new in-core index representation and read an index from disk.
  148. * <p>
  149. * The new index will be read before it is returned to the caller. Read
  150. * failures are reported as exceptions and therefore prevent the method from
  151. * returning a partially populated index.
  152. *
  153. * @param db
  154. * repository the caller wants to read the default index of.
  155. * @return a cache representing the contents of the specified index file (if
  156. * it exists) or an empty cache if the file does not exist.
  157. * @throws IllegalStateException
  158. * if the repository is bare (lacks a working directory).
  159. * @throws IOException
  160. * the index file is present but could not be read.
  161. * @throws CorruptObjectException
  162. * the index file is using a format or extension that this
  163. * library does not support.
  164. */
  165. public static DirCache read(final Repository db)
  166. throws CorruptObjectException, IOException {
  167. return read(db.getIndexFile());
  168. }
  169. /**
  170. * Create a new in-core index representation, lock it, and read from disk.
  171. * <p>
  172. * The new index will be locked and then read before it is returned to the
  173. * caller. Read failures are reported as exceptions and therefore prevent
  174. * the method from returning a partially populated index. On read failure,
  175. * the lock is released.
  176. *
  177. * @param indexLocation
  178. * location of the index file on disk.
  179. * @return a cache representing the contents of the specified index file (if
  180. * it exists) or an empty cache if the file does not exist.
  181. * @throws IOException
  182. * the index file is present but could not be read, or the lock
  183. * could not be obtained.
  184. * @throws CorruptObjectException
  185. * the index file is using a format or extension that this
  186. * library does not support.
  187. */
  188. public static DirCache lock(final File indexLocation)
  189. throws CorruptObjectException, IOException {
  190. final DirCache c = new DirCache(indexLocation);
  191. if (!c.lock())
  192. throw new IOException(MessageFormat.format(JGitText.get().cannotLock, indexLocation));
  193. try {
  194. c.read();
  195. } catch (IOException e) {
  196. c.unlock();
  197. throw e;
  198. } catch (RuntimeException e) {
  199. c.unlock();
  200. throw e;
  201. } catch (Error e) {
  202. c.unlock();
  203. throw e;
  204. }
  205. return c;
  206. }
  207. /**
  208. * Create a new in-core index representation, lock it, and read from disk.
  209. * <p>
  210. * The new index will be locked and then read before it is returned to the
  211. * caller. Read failures are reported as exceptions and therefore prevent
  212. * the method from returning a partially populated index.
  213. *
  214. * @param db
  215. * repository the caller wants to read the default index of.
  216. * @return a cache representing the contents of the specified index file (if
  217. * it exists) or an empty cache if the file does not exist.
  218. * @throws IllegalStateException
  219. * if the repository is bare (lacks a working directory).
  220. * @throws IOException
  221. * the index file is present but could not be read, or the lock
  222. * could not be obtained.
  223. * @throws CorruptObjectException
  224. * the index file is using a format or extension that this
  225. * library does not support.
  226. */
  227. public static DirCache lock(final Repository db)
  228. throws CorruptObjectException, IOException {
  229. return lock(db.getIndexFile());
  230. }
  231. /** Location of the current version of the index file. */
  232. private final File liveFile;
  233. /** Modification time of the file at the last read/write we did. */
  234. private long lastModified;
  235. /** Individual file index entries, sorted by path name. */
  236. private DirCacheEntry[] sortedEntries;
  237. /** Number of positions within {@link #sortedEntries} that are valid. */
  238. private int entryCnt;
  239. /** Cache tree for this index; null if the cache tree is not available. */
  240. private DirCacheTree tree;
  241. /** Our active lock (if we hold it); null if we don't have it locked. */
  242. private LockFile myLock;
  243. /**
  244. * Create a new in-core index representation.
  245. * <p>
  246. * The new index will be empty. Callers may wish to read from the on disk
  247. * file first with {@link #read()}.
  248. *
  249. * @param indexLocation
  250. * location of the index file on disk.
  251. */
  252. public DirCache(final File indexLocation) {
  253. liveFile = indexLocation;
  254. clear();
  255. }
  256. /**
  257. * Create a new builder to update this cache.
  258. * <p>
  259. * Callers should add all entries to the builder, then use
  260. * {@link DirCacheBuilder#finish()} to update this instance.
  261. *
  262. * @return a new builder instance for this cache.
  263. */
  264. public DirCacheBuilder builder() {
  265. return new DirCacheBuilder(this, entryCnt + 16);
  266. }
  267. /**
  268. * Create a new editor to recreate this cache.
  269. * <p>
  270. * Callers should add commands to the editor, then use
  271. * {@link DirCacheEditor#finish()} to update this instance.
  272. *
  273. * @return a new builder instance for this cache.
  274. */
  275. public DirCacheEditor editor() {
  276. return new DirCacheEditor(this, entryCnt + 16);
  277. }
  278. void replace(final DirCacheEntry[] e, final int cnt) {
  279. sortedEntries = e;
  280. entryCnt = cnt;
  281. tree = null;
  282. }
  283. /**
  284. * Read the index from disk, if it has changed on disk.
  285. * <p>
  286. * This method tries to avoid loading the index if it has not changed since
  287. * the last time we consulted it. A missing index file will be treated as
  288. * though it were present but had no file entries in it.
  289. *
  290. * @throws IOException
  291. * the index file is present but could not be read. This
  292. * DirCache instance may not be populated correctly.
  293. * @throws CorruptObjectException
  294. * the index file is using a format or extension that this
  295. * library does not support.
  296. */
  297. public void read() throws IOException, CorruptObjectException {
  298. if (liveFile == null)
  299. throw new IOException(JGitText.get().dirCacheDoesNotHaveABackingFile);
  300. if (!liveFile.exists())
  301. clear();
  302. else if (liveFile.lastModified() != lastModified) {
  303. try {
  304. final FileInputStream inStream = new FileInputStream(liveFile);
  305. try {
  306. clear();
  307. readFrom(inStream);
  308. } finally {
  309. try {
  310. inStream.close();
  311. } catch (IOException err2) {
  312. // Ignore any close failures.
  313. }
  314. }
  315. } catch (FileNotFoundException fnfe) {
  316. // Someone must have deleted it between our exists test
  317. // and actually opening the path. That's fine, its empty.
  318. //
  319. clear();
  320. }
  321. }
  322. }
  323. /** Empty this index, removing all entries. */
  324. public void clear() {
  325. lastModified = 0;
  326. sortedEntries = NO_ENTRIES;
  327. entryCnt = 0;
  328. tree = null;
  329. }
  330. private void readFrom(final FileInputStream inStream) throws IOException,
  331. CorruptObjectException {
  332. final BufferedInputStream in = new BufferedInputStream(inStream);
  333. final MessageDigest md = Constants.newMessageDigest();
  334. // Read the index header and verify we understand it.
  335. //
  336. final byte[] hdr = new byte[20];
  337. IO.readFully(in, hdr, 0, 12);
  338. md.update(hdr, 0, 12);
  339. if (!is_DIRC(hdr))
  340. throw new CorruptObjectException(JGitText.get().notADIRCFile);
  341. final int ver = NB.decodeInt32(hdr, 4);
  342. if (ver != 2)
  343. throw new CorruptObjectException(MessageFormat.format(JGitText.get().unknownDIRCVersion, ver));
  344. entryCnt = NB.decodeInt32(hdr, 8);
  345. if (entryCnt < 0)
  346. throw new CorruptObjectException(JGitText.get().DIRCHasTooManyEntries);
  347. // Load the individual file entries.
  348. //
  349. final byte[] infos = new byte[INFO_LEN * entryCnt];
  350. sortedEntries = new DirCacheEntry[entryCnt];
  351. for (int i = 0; i < entryCnt; i++)
  352. sortedEntries[i] = new DirCacheEntry(infos, i * INFO_LEN, in, md);
  353. lastModified = liveFile.lastModified();
  354. // After the file entries are index extensions, and then a footer.
  355. //
  356. for (;;) {
  357. in.mark(21);
  358. IO.readFully(in, hdr, 0, 20);
  359. if (in.read() < 0) {
  360. // No extensions present; the file ended where we expected.
  361. //
  362. break;
  363. }
  364. in.reset();
  365. md.update(hdr, 0, 8);
  366. IO.skipFully(in, 8);
  367. long sz = NB.decodeUInt32(hdr, 4);
  368. switch (NB.decodeInt32(hdr, 0)) {
  369. case EXT_TREE: {
  370. if (Integer.MAX_VALUE < sz) {
  371. throw new CorruptObjectException(MessageFormat.format(JGitText.get().DIRCExtensionIsTooLargeAt
  372. , formatExtensionName(hdr), sz));
  373. }
  374. final byte[] raw = new byte[(int) sz];
  375. IO.readFully(in, raw, 0, raw.length);
  376. md.update(raw, 0, raw.length);
  377. tree = new DirCacheTree(raw, new MutableInteger(), null);
  378. break;
  379. }
  380. default:
  381. if (hdr[0] >= 'A' && hdr[0] <= 'Z') {
  382. // The extension is optional and is here only as
  383. // a performance optimization. Since we do not
  384. // understand it, we can safely skip past it, after
  385. // we include its data in our checksum.
  386. //
  387. skipOptionalExtension(in, md, hdr, sz);
  388. } else {
  389. // The extension is not an optimization and is
  390. // _required_ to understand this index format.
  391. // Since we did not trap it above we must abort.
  392. //
  393. throw new CorruptObjectException(MessageFormat.format(JGitText.get().DIRCExtensionNotSupportedByThisVersion
  394. , formatExtensionName(hdr)));
  395. }
  396. }
  397. }
  398. final byte[] exp = md.digest();
  399. if (!Arrays.equals(exp, hdr)) {
  400. throw new CorruptObjectException(JGitText.get().DIRCChecksumMismatch);
  401. }
  402. }
  403. private void skipOptionalExtension(final InputStream in,
  404. final MessageDigest md, final byte[] hdr, long sz)
  405. throws IOException {
  406. final byte[] b = new byte[4096];
  407. while (0 < sz) {
  408. int n = in.read(b, 0, (int) Math.min(b.length, sz));
  409. if (n < 0) {
  410. throw new EOFException(MessageFormat.format(JGitText.get().shortReadOfOptionalDIRCExtensionExpectedAnotherBytes
  411. , formatExtensionName(hdr), sz));
  412. }
  413. md.update(b, 0, n);
  414. sz -= n;
  415. }
  416. }
  417. private static String formatExtensionName(final byte[] hdr)
  418. throws UnsupportedEncodingException {
  419. return "'" + new String(hdr, 0, 4, "ISO-8859-1") + "'";
  420. }
  421. private static boolean is_DIRC(final byte[] hdr) {
  422. if (hdr.length < SIG_DIRC.length)
  423. return false;
  424. for (int i = 0; i < SIG_DIRC.length; i++)
  425. if (hdr[i] != SIG_DIRC[i])
  426. return false;
  427. return true;
  428. }
  429. /**
  430. * Try to establish an update lock on the cache file.
  431. *
  432. * @return true if the lock is now held by the caller; false if it is held
  433. * by someone else.
  434. * @throws IOException
  435. * the output file could not be created. The caller does not
  436. * hold the lock.
  437. */
  438. public boolean lock() throws IOException {
  439. if (liveFile == null)
  440. throw new IOException(JGitText.get().dirCacheDoesNotHaveABackingFile);
  441. final LockFile tmp = new LockFile(liveFile);
  442. if (tmp.lock()) {
  443. tmp.setNeedStatInformation(true);
  444. myLock = tmp;
  445. return true;
  446. }
  447. return false;
  448. }
  449. /**
  450. * Write the entry records from memory to disk.
  451. * <p>
  452. * The cache must be locked first by calling {@link #lock()} and receiving
  453. * true as the return value. Applications are encouraged to lock the index,
  454. * then invoke {@link #read()} to ensure the in-memory data is current,
  455. * prior to updating the in-memory entries.
  456. * <p>
  457. * Once written the lock is closed and must be either committed with
  458. * {@link #commit()} or rolled back with {@link #unlock()}.
  459. *
  460. * @throws IOException
  461. * the output file could not be created. The caller no longer
  462. * holds the lock.
  463. */
  464. public void write() throws IOException {
  465. final LockFile tmp = myLock;
  466. requireLocked(tmp);
  467. try {
  468. writeTo(new BufferedOutputStream(tmp.getOutputStream()));
  469. } catch (IOException err) {
  470. tmp.unlock();
  471. throw err;
  472. } catch (RuntimeException err) {
  473. tmp.unlock();
  474. throw err;
  475. } catch (Error err) {
  476. tmp.unlock();
  477. throw err;
  478. }
  479. }
  480. private void writeTo(final OutputStream os) throws IOException {
  481. final MessageDigest foot = Constants.newMessageDigest();
  482. final DigestOutputStream dos = new DigestOutputStream(os, foot);
  483. // Write the header.
  484. //
  485. final byte[] tmp = new byte[128];
  486. System.arraycopy(SIG_DIRC, 0, tmp, 0, SIG_DIRC.length);
  487. NB.encodeInt32(tmp, 4, /* version */2);
  488. NB.encodeInt32(tmp, 8, entryCnt);
  489. dos.write(tmp, 0, 12);
  490. // Write the individual file entries.
  491. //
  492. if (lastModified <= 0) {
  493. // Write a new index, as no entries require smudging.
  494. //
  495. for (int i = 0; i < entryCnt; i++)
  496. sortedEntries[i].write(dos);
  497. } else {
  498. final int smudge_s = (int) (lastModified / 1000);
  499. final int smudge_ns = ((int) (lastModified % 1000)) * 1000000;
  500. for (int i = 0; i < entryCnt; i++) {
  501. final DirCacheEntry e = sortedEntries[i];
  502. if (e.mightBeRacilyClean(smudge_s, smudge_ns))
  503. e.smudgeRacilyClean();
  504. e.write(dos);
  505. }
  506. }
  507. if (tree != null) {
  508. final TemporaryBuffer bb = new TemporaryBuffer.LocalFile();
  509. tree.write(tmp, bb);
  510. bb.close();
  511. NB.encodeInt32(tmp, 0, EXT_TREE);
  512. NB.encodeInt32(tmp, 4, (int) bb.length());
  513. dos.write(tmp, 0, 8);
  514. bb.writeTo(dos, null);
  515. }
  516. os.write(foot.digest());
  517. os.close();
  518. }
  519. /**
  520. * Commit this change and release the lock.
  521. * <p>
  522. * If this method fails (returns false) the lock is still released.
  523. *
  524. * @return true if the commit was successful and the file contains the new
  525. * data; false if the commit failed and the file remains with the
  526. * old data.
  527. * @throws IllegalStateException
  528. * the lock is not held.
  529. */
  530. public boolean commit() {
  531. final LockFile tmp = myLock;
  532. requireLocked(tmp);
  533. myLock = null;
  534. if (!tmp.commit())
  535. return false;
  536. lastModified = tmp.getCommitLastModified();
  537. return true;
  538. }
  539. private void requireLocked(final LockFile tmp) {
  540. if (liveFile == null)
  541. throw new IllegalStateException(JGitText.get().dirCacheIsNotLocked);
  542. if (tmp == null)
  543. throw new IllegalStateException(MessageFormat.format(JGitText.get().dirCacheFileIsNotLocked
  544. , liveFile.getAbsolutePath()));
  545. }
  546. /**
  547. * Unlock this file and abort this change.
  548. * <p>
  549. * The temporary file (if created) is deleted before returning.
  550. */
  551. public void unlock() {
  552. final LockFile tmp = myLock;
  553. if (tmp != null) {
  554. myLock = null;
  555. tmp.unlock();
  556. }
  557. }
  558. /**
  559. * Locate the position a path's entry is at in the index.
  560. * <p>
  561. * If there is at least one entry in the index for this path the position of
  562. * the lowest stage is returned. Subsequent stages can be identified by
  563. * testing consecutive entries until the path differs.
  564. * <p>
  565. * If no path matches the entry -(position+1) is returned, where position is
  566. * the location it would have gone within the index.
  567. *
  568. * @param path
  569. * the path to search for.
  570. * @return if >= 0 then the return value is the position of the entry in the
  571. * index; pass to {@link #getEntry(int)} to obtain the entry
  572. * information. If < 0 the entry does not exist in the index.
  573. */
  574. public int findEntry(final String path) {
  575. final byte[] p = Constants.encode(path);
  576. return findEntry(p, p.length);
  577. }
  578. int findEntry(final byte[] p, final int pLen) {
  579. int low = 0;
  580. int high = entryCnt;
  581. while (low < high) {
  582. int mid = (low + high) >>> 1;
  583. final int cmp = cmp(p, pLen, sortedEntries[mid]);
  584. if (cmp < 0)
  585. high = mid;
  586. else if (cmp == 0) {
  587. while (mid > 0 && cmp(p, pLen, sortedEntries[mid - 1]) == 0)
  588. mid--;
  589. return mid;
  590. } else
  591. low = mid + 1;
  592. }
  593. return -(low + 1);
  594. }
  595. /**
  596. * Determine the next index position past all entries with the same name.
  597. * <p>
  598. * As index entries are sorted by path name, then stage number, this method
  599. * advances the supplied position to the first position in the index whose
  600. * path name does not match the path name of the supplied position's entry.
  601. *
  602. * @param position
  603. * entry position of the path that should be skipped.
  604. * @return position of the next entry whose path is after the input.
  605. */
  606. public int nextEntry(final int position) {
  607. DirCacheEntry last = sortedEntries[position];
  608. int nextIdx = position + 1;
  609. while (nextIdx < entryCnt) {
  610. final DirCacheEntry next = sortedEntries[nextIdx];
  611. if (cmp(last, next) != 0)
  612. break;
  613. last = next;
  614. nextIdx++;
  615. }
  616. return nextIdx;
  617. }
  618. int nextEntry(final byte[] p, final int pLen, int nextIdx) {
  619. while (nextIdx < entryCnt) {
  620. final DirCacheEntry next = sortedEntries[nextIdx];
  621. if (!DirCacheTree.peq(p, next.path, pLen))
  622. break;
  623. nextIdx++;
  624. }
  625. return nextIdx;
  626. }
  627. /**
  628. * Total number of file entries stored in the index.
  629. * <p>
  630. * This count includes unmerged stages for a file entry if the file is
  631. * currently conflicted in a merge. This means the total number of entries
  632. * in the index may be up to 3 times larger than the number of files in the
  633. * working directory.
  634. * <p>
  635. * Note that this value counts only <i>files</i>.
  636. *
  637. * @return number of entries available.
  638. * @see #getEntry(int)
  639. */
  640. public int getEntryCount() {
  641. return entryCnt;
  642. }
  643. /**
  644. * Get a specific entry.
  645. *
  646. * @param i
  647. * position of the entry to get.
  648. * @return the entry at position <code>i</code>.
  649. */
  650. public DirCacheEntry getEntry(final int i) {
  651. return sortedEntries[i];
  652. }
  653. /**
  654. * Get a specific entry.
  655. *
  656. * @param path
  657. * the path to search for.
  658. * @return the entry at position <code>i</code>.
  659. */
  660. public DirCacheEntry getEntry(final String path) {
  661. final int i = findEntry(path);
  662. return i < 0 ? null : sortedEntries[i];
  663. }
  664. /**
  665. * Recursively get all entries within a subtree.
  666. *
  667. * @param path
  668. * the subtree path to get all entries within.
  669. * @return all entries recursively contained within the subtree.
  670. */
  671. public DirCacheEntry[] getEntriesWithin(String path) {
  672. if (!path.endsWith("/"))
  673. path += "/";
  674. final byte[] p = Constants.encode(path);
  675. final int pLen = p.length;
  676. int eIdx = findEntry(p, pLen);
  677. if (eIdx < 0)
  678. eIdx = -(eIdx + 1);
  679. final int lastIdx = nextEntry(p, pLen, eIdx);
  680. final DirCacheEntry[] r = new DirCacheEntry[lastIdx - eIdx];
  681. System.arraycopy(sortedEntries, eIdx, r, 0, r.length);
  682. return r;
  683. }
  684. void toArray(final int i, final DirCacheEntry[] dst, final int off,
  685. final int cnt) {
  686. System.arraycopy(sortedEntries, i, dst, off, cnt);
  687. }
  688. /**
  689. * Obtain (or build) the current cache tree structure.
  690. * <p>
  691. * This method can optionally recreate the cache tree, without flushing the
  692. * tree objects themselves to disk.
  693. *
  694. * @param build
  695. * if true and the cache tree is not present in the index it will
  696. * be generated and returned to the caller.
  697. * @return the cache tree; null if there is no current cache tree available
  698. * and <code>build</code> was false.
  699. */
  700. public DirCacheTree getCacheTree(final boolean build) {
  701. if (build) {
  702. if (tree == null)
  703. tree = new DirCacheTree();
  704. tree.validate(sortedEntries, entryCnt, 0, 0);
  705. }
  706. return tree;
  707. }
  708. /**
  709. * Write all index trees to the object store, returning the root tree.
  710. *
  711. * @param ow
  712. * the writer to use when serializing to the store. The caller is
  713. * responsible for flushing the inserter before trying to use the
  714. * returned tree identity.
  715. * @return identity for the root tree.
  716. * @throws UnmergedPathException
  717. * one or more paths contain higher-order stages (stage > 0),
  718. * which cannot be stored in a tree object.
  719. * @throws IllegalStateException
  720. * one or more paths contain an invalid mode which should never
  721. * appear in a tree object.
  722. * @throws IOException
  723. * an unexpected error occurred writing to the object store.
  724. */
  725. public ObjectId writeTree(final ObjectInserter ow)
  726. throws UnmergedPathException, IOException {
  727. return getCacheTree(true).writeTree(sortedEntries, 0, 0, ow);
  728. }
  729. /**
  730. * Tells whether this index contains unmerged paths.
  731. *
  732. * @return {@code true} if this index contains unmerged paths. Means: at
  733. * least one entry is of a stage different from 0. {@code false}
  734. * will be returned if all entries are of stage 0.
  735. */
  736. public boolean hasUnmergedPaths() {
  737. for (int i = 0; i < entryCnt; i++) {
  738. if (sortedEntries[i].getStage() > 0) {
  739. return true;
  740. }
  741. }
  742. return false;
  743. }
  744. }