選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

RefDirectory.java 29KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2009-2010, Google Inc.
  4. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2006, Shawn O. Pearce <spearce@spearce.org>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.lib;
  47. import static org.eclipse.jgit.lib.Constants.CHARSET;
  48. import static org.eclipse.jgit.lib.Constants.HEAD;
  49. import static org.eclipse.jgit.lib.Constants.LOGS;
  50. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH;
  51. import static org.eclipse.jgit.lib.Constants.PACKED_REFS;
  52. import static org.eclipse.jgit.lib.Constants.R_HEADS;
  53. import static org.eclipse.jgit.lib.Constants.R_REFS;
  54. import static org.eclipse.jgit.lib.Constants.R_REMOTES;
  55. import static org.eclipse.jgit.lib.Constants.R_TAGS;
  56. import static org.eclipse.jgit.lib.Constants.encode;
  57. import static org.eclipse.jgit.lib.Ref.Storage.LOOSE;
  58. import static org.eclipse.jgit.lib.Ref.Storage.NEW;
  59. import static org.eclipse.jgit.lib.Ref.Storage.PACKED;
  60. import java.io.BufferedReader;
  61. import java.io.File;
  62. import java.io.FileInputStream;
  63. import java.io.FileNotFoundException;
  64. import java.io.FileOutputStream;
  65. import java.io.IOException;
  66. import java.io.InputStreamReader;
  67. import java.text.MessageFormat;
  68. import java.util.Arrays;
  69. import java.util.Map;
  70. import java.util.concurrent.atomic.AtomicInteger;
  71. import java.util.concurrent.atomic.AtomicReference;
  72. import org.eclipse.jgit.JGitText;
  73. import org.eclipse.jgit.errors.ObjectWritingException;
  74. import org.eclipse.jgit.revwalk.RevObject;
  75. import org.eclipse.jgit.revwalk.RevTag;
  76. import org.eclipse.jgit.revwalk.RevWalk;
  77. import org.eclipse.jgit.util.FS;
  78. import org.eclipse.jgit.util.IO;
  79. import org.eclipse.jgit.util.RawParseUtils;
  80. import org.eclipse.jgit.util.RefList;
  81. import org.eclipse.jgit.util.RefMap;
  82. /**
  83. * Traditional file system based {@link RefDatabase}.
  84. * <p>
  85. * This is the classical reference database representation for a Git repository.
  86. * References are stored in two formats: loose, and packed.
  87. * <p>
  88. * Loose references are stored as individual files within the {@code refs/}
  89. * directory. The file name matches the reference name and the file contents is
  90. * the current {@link ObjectId} in string form.
  91. * <p>
  92. * Packed references are stored in a single text file named {@code packed-refs}.
  93. * In the packed format, each reference is stored on its own line. This file
  94. * reduces the number of files needed for large reference spaces, reducing the
  95. * overall size of a Git repository on disk.
  96. */
  97. public class RefDirectory extends RefDatabase {
  98. /** Magic string denoting the start of a symbolic reference file. */
  99. public static final String SYMREF = "ref: "; //$NON-NLS-1$
  100. /** Magic string denoting the header of a packed-refs file. */
  101. public static final String PACKED_REFS_HEADER = "# pack-refs with:"; //$NON-NLS-1$
  102. /** If in the header, denotes the file has peeled data. */
  103. public static final String PACKED_REFS_PEELED = " peeled"; //$NON-NLS-1$
  104. private final Repository parent;
  105. private final File gitDir;
  106. private final File refsDir;
  107. private final File logsDir;
  108. private final File logsRefsDir;
  109. private final File packedRefsFile;
  110. /**
  111. * Immutable sorted list of loose references.
  112. * <p>
  113. * Symbolic references in this collection are stored unresolved, that is
  114. * their target appears to be a new reference with no ObjectId. These are
  115. * converted into resolved references during a get operation, ensuring the
  116. * live value is always returned.
  117. */
  118. private final AtomicReference<RefList<LooseRef>> looseRefs = new AtomicReference<RefList<LooseRef>>();
  119. /** Immutable sorted list of packed references. */
  120. private final AtomicReference<PackedRefList> packedRefs = new AtomicReference<PackedRefList>();
  121. /**
  122. * Number of modifications made to this database.
  123. * <p>
  124. * This counter is incremented when a change is made, or detected from the
  125. * filesystem during a read operation.
  126. */
  127. private final AtomicInteger modCnt = new AtomicInteger();
  128. /**
  129. * Last {@link #modCnt} that we sent to listeners.
  130. * <p>
  131. * This value is compared to {@link #modCnt}, and a notification is sent to
  132. * the listeners only when it differs.
  133. */
  134. private final AtomicInteger lastNotifiedModCnt = new AtomicInteger();
  135. RefDirectory(final Repository db) {
  136. final FS fs = db.getFS();
  137. parent = db;
  138. gitDir = db.getDirectory();
  139. refsDir = fs.resolve(gitDir, R_REFS);
  140. logsDir = fs.resolve(gitDir, LOGS);
  141. logsRefsDir = fs.resolve(gitDir, LOGS + '/' + R_REFS);
  142. packedRefsFile = fs.resolve(gitDir, PACKED_REFS);
  143. looseRefs.set(RefList.<LooseRef> emptyList());
  144. packedRefs.set(PackedRefList.NO_PACKED_REFS);
  145. }
  146. Repository getRepository() {
  147. return parent;
  148. }
  149. public void create() throws IOException {
  150. refsDir.mkdir();
  151. logsDir.mkdir();
  152. logsRefsDir.mkdir();
  153. new File(refsDir, R_HEADS.substring(R_REFS.length())).mkdir();
  154. new File(refsDir, R_TAGS.substring(R_REFS.length())).mkdir();
  155. new File(logsRefsDir, R_HEADS.substring(R_REFS.length())).mkdir();
  156. }
  157. @Override
  158. public void close() {
  159. // We have no resources to close.
  160. }
  161. void rescan() {
  162. looseRefs.set(RefList.<LooseRef> emptyList());
  163. packedRefs.set(PackedRefList.NO_PACKED_REFS);
  164. }
  165. @Override
  166. public boolean isNameConflicting(String name) throws IOException {
  167. RefList<Ref> packed = getPackedRefs();
  168. RefList<LooseRef> loose = getLooseRefs();
  169. // Cannot be nested within an existing reference.
  170. int lastSlash = name.lastIndexOf('/');
  171. while (0 < lastSlash) {
  172. String needle = name.substring(0, lastSlash);
  173. if (loose.contains(needle) || packed.contains(needle))
  174. return true;
  175. lastSlash = name.lastIndexOf('/', lastSlash - 1);
  176. }
  177. // Cannot be the container of an existing reference.
  178. String prefix = name + '/';
  179. int idx;
  180. idx = -(packed.find(prefix) + 1);
  181. if (idx < packed.size() && packed.get(idx).getName().startsWith(prefix))
  182. return true;
  183. idx = -(loose.find(prefix) + 1);
  184. if (idx < loose.size() && loose.get(idx).getName().startsWith(prefix))
  185. return true;
  186. return false;
  187. }
  188. private RefList<LooseRef> getLooseRefs() {
  189. final RefList<LooseRef> oldLoose = looseRefs.get();
  190. LooseScanner scan = new LooseScanner(oldLoose);
  191. scan.scan(ALL);
  192. RefList<LooseRef> loose;
  193. if (scan.newLoose != null) {
  194. loose = scan.newLoose.toRefList();
  195. if (looseRefs.compareAndSet(oldLoose, loose))
  196. modCnt.incrementAndGet();
  197. } else
  198. loose = oldLoose;
  199. return loose;
  200. }
  201. @Override
  202. public Ref getRef(final String needle) throws IOException {
  203. final RefList<Ref> packed = getPackedRefs();
  204. Ref ref = null;
  205. for (String prefix : SEARCH_PATH) {
  206. ref = readRef(prefix + needle, packed);
  207. if (ref != null) {
  208. ref = resolve(ref, 0, null, null, packed);
  209. break;
  210. }
  211. }
  212. fireRefsChanged();
  213. return ref;
  214. }
  215. @Override
  216. public Map<String, Ref> getRefs(String prefix) throws IOException {
  217. final RefList<Ref> packed = getPackedRefs();
  218. final RefList<LooseRef> oldLoose = looseRefs.get();
  219. LooseScanner scan = new LooseScanner(oldLoose);
  220. scan.scan(prefix);
  221. RefList<LooseRef> loose;
  222. if (scan.newLoose != null) {
  223. loose = scan.newLoose.toRefList();
  224. if (looseRefs.compareAndSet(oldLoose, loose))
  225. modCnt.incrementAndGet();
  226. } else
  227. loose = oldLoose;
  228. fireRefsChanged();
  229. RefList.Builder<Ref> symbolic = scan.symbolic;
  230. for (int idx = 0; idx < symbolic.size();) {
  231. Ref ref = symbolic.get(idx);
  232. ref = resolve(ref, 0, prefix, loose, packed);
  233. if (ref != null && ref.getObjectId() != null) {
  234. symbolic.set(idx, ref);
  235. idx++;
  236. } else {
  237. // A broken symbolic reference, we have to drop it from the
  238. // collections the client is about to receive. Should be a
  239. // rare occurrence so pay a copy penalty.
  240. loose = loose.remove(idx);
  241. symbolic.remove(idx);
  242. }
  243. }
  244. return new RefMap(prefix, packed, upcast(loose), symbolic.toRefList());
  245. }
  246. @SuppressWarnings("unchecked")
  247. private RefList<Ref> upcast(RefList<? extends Ref> loose) {
  248. return (RefList<Ref>) loose;
  249. }
  250. private class LooseScanner {
  251. private final RefList<LooseRef> curLoose;
  252. private int curIdx;
  253. final RefList.Builder<Ref> symbolic = new RefList.Builder<Ref>(4);
  254. RefList.Builder<LooseRef> newLoose;
  255. LooseScanner(final RefList<LooseRef> curLoose) {
  256. this.curLoose = curLoose;
  257. }
  258. void scan(String prefix) {
  259. if (ALL.equals(prefix)) {
  260. scanOne(HEAD);
  261. scanTree(R_REFS, refsDir);
  262. // If any entries remain, they are deleted, drop them.
  263. if (newLoose == null && curIdx < curLoose.size())
  264. newLoose = curLoose.copy(curIdx);
  265. } else if (prefix.startsWith(R_REFS) && prefix.endsWith("/")) {
  266. curIdx = -(curLoose.find(prefix) + 1);
  267. File dir = new File(refsDir, prefix.substring(R_REFS.length()));
  268. scanTree(prefix, dir);
  269. // Skip over entries still within the prefix; these have
  270. // been removed from the directory.
  271. while (curIdx < curLoose.size()) {
  272. if (!curLoose.get(curIdx).getName().startsWith(prefix))
  273. break;
  274. if (newLoose == null)
  275. newLoose = curLoose.copy(curIdx);
  276. curIdx++;
  277. }
  278. // Keep any entries outside of the prefix space, we
  279. // do not know anything about their status.
  280. if (newLoose != null) {
  281. while (curIdx < curLoose.size())
  282. newLoose.add(curLoose.get(curIdx++));
  283. }
  284. }
  285. }
  286. private boolean scanTree(String prefix, File dir) {
  287. final String[] entries = dir.list(LockFile.FILTER);
  288. if (entries == null) // not a directory or an I/O error
  289. return false;
  290. if (0 < entries.length) {
  291. Arrays.sort(entries);
  292. for (String name : entries) {
  293. File e = new File(dir, name);
  294. if (!scanTree(prefix + name + '/', e))
  295. scanOne(prefix + name);
  296. }
  297. }
  298. return true;
  299. }
  300. private void scanOne(String name) {
  301. LooseRef cur;
  302. if (curIdx < curLoose.size()) {
  303. do {
  304. cur = curLoose.get(curIdx);
  305. int cmp = RefComparator.compareTo(cur, name);
  306. if (cmp < 0) {
  307. // Reference is not loose anymore, its been deleted.
  308. // Skip the name in the new result list.
  309. if (newLoose == null)
  310. newLoose = curLoose.copy(curIdx);
  311. curIdx++;
  312. cur = null;
  313. continue;
  314. }
  315. if (cmp > 0) // Newly discovered loose reference.
  316. cur = null;
  317. break;
  318. } while (curIdx < curLoose.size());
  319. } else
  320. cur = null; // Newly discovered loose reference.
  321. LooseRef n;
  322. try {
  323. n = scanRef(cur, name);
  324. } catch (IOException notValid) {
  325. n = null;
  326. }
  327. if (n != null) {
  328. if (cur != n && newLoose == null)
  329. newLoose = curLoose.copy(curIdx);
  330. if (newLoose != null)
  331. newLoose.add(n);
  332. if (n.isSymbolic())
  333. symbolic.add(n);
  334. } else if (cur != null) {
  335. // Tragically, this file is no longer a loose reference.
  336. // Kill our cached entry of it.
  337. if (newLoose == null)
  338. newLoose = curLoose.copy(curIdx);
  339. }
  340. if (cur != null)
  341. curIdx++;
  342. }
  343. }
  344. @Override
  345. public Ref peel(final Ref ref) throws IOException {
  346. final Ref leaf = ref.getLeaf();
  347. if (leaf.isPeeled() || leaf.getObjectId() == null)
  348. return ref;
  349. RevWalk rw = new RevWalk(getRepository());
  350. RevObject obj = rw.parseAny(leaf.getObjectId());
  351. ObjectIdRef newLeaf;
  352. if (obj instanceof RevTag) {
  353. do {
  354. obj = rw.parseAny(((RevTag) obj).getObject());
  355. } while (obj instanceof RevTag);
  356. newLeaf = new ObjectIdRef.PeeledTag(leaf.getStorage(), leaf
  357. .getName(), leaf.getObjectId(), obj.copy());
  358. } else {
  359. newLeaf = new ObjectIdRef.PeeledNonTag(leaf.getStorage(), leaf
  360. .getName(), leaf.getObjectId());
  361. }
  362. // Try to remember this peeling in the cache, so we don't have to do
  363. // it again in the future, but only if the reference is unchanged.
  364. if (leaf.getStorage().isLoose()) {
  365. RefList<LooseRef> curList = looseRefs.get();
  366. int idx = curList.find(leaf.getName());
  367. if (0 <= idx && curList.get(idx) == leaf) {
  368. LooseRef asPeeled = ((LooseRef) leaf).peel(newLeaf);
  369. RefList<LooseRef> newList = curList.set(idx, asPeeled);
  370. looseRefs.compareAndSet(curList, newList);
  371. }
  372. }
  373. return recreate(ref, newLeaf);
  374. }
  375. private static Ref recreate(final Ref old, final ObjectIdRef leaf) {
  376. if (old.isSymbolic()) {
  377. Ref dst = recreate(old.getTarget(), leaf);
  378. return new SymbolicRef(old.getName(), dst);
  379. }
  380. return leaf;
  381. }
  382. void storedSymbolicRef(RefDirectoryUpdate u, long modified, String target) {
  383. putLooseRef(newSymbolicRef(modified, u.getRef().getName(), target));
  384. fireRefsChanged();
  385. }
  386. public RefDirectoryUpdate newUpdate(String name, boolean detach)
  387. throws IOException {
  388. final RefList<Ref> packed = getPackedRefs();
  389. Ref ref = readRef(name, packed);
  390. if (ref != null)
  391. ref = resolve(ref, 0, null, null, packed);
  392. if (ref == null)
  393. ref = new ObjectIdRef.Unpeeled(NEW, name, null);
  394. else if (detach && ref.isSymbolic())
  395. ref = new ObjectIdRef.Unpeeled(LOOSE, name, ref.getObjectId());
  396. return new RefDirectoryUpdate(this, ref);
  397. }
  398. @Override
  399. public RefDirectoryRename newRename(String fromName, String toName)
  400. throws IOException {
  401. RefDirectoryUpdate from = newUpdate(fromName, false);
  402. RefDirectoryUpdate to = newUpdate(toName, false);
  403. return new RefDirectoryRename(from, to);
  404. }
  405. void stored(RefDirectoryUpdate update, long modified) {
  406. final ObjectId target = update.getNewObjectId().copy();
  407. final Ref leaf = update.getRef().getLeaf();
  408. putLooseRef(new LooseUnpeeled(modified, leaf.getName(), target));
  409. }
  410. private void putLooseRef(LooseRef ref) {
  411. RefList<LooseRef> cList, nList;
  412. do {
  413. cList = looseRefs.get();
  414. nList = cList.put(ref);
  415. } while (!looseRefs.compareAndSet(cList, nList));
  416. modCnt.incrementAndGet();
  417. fireRefsChanged();
  418. }
  419. void delete(RefDirectoryUpdate update) throws IOException {
  420. Ref dst = update.getRef().getLeaf();
  421. String name = dst.getName();
  422. // Write the packed-refs file using an atomic update. We might
  423. // wind up reading it twice, before and after the lock, to ensure
  424. // we don't miss an edit made externally.
  425. final PackedRefList packed = getPackedRefs();
  426. if (packed.contains(name)) {
  427. LockFile lck = new LockFile(packedRefsFile);
  428. if (!lck.lock())
  429. throw new IOException(MessageFormat.format(
  430. JGitText.get().cannotLockFile, packedRefsFile));
  431. try {
  432. PackedRefList cur = readPackedRefs(0, 0);
  433. int idx = cur.find(name);
  434. if (0 <= idx)
  435. commitPackedRefs(lck, cur.remove(idx), packed);
  436. } finally {
  437. lck.unlock();
  438. }
  439. }
  440. RefList<LooseRef> curLoose, newLoose;
  441. do {
  442. curLoose = looseRefs.get();
  443. int idx = curLoose.find(name);
  444. if (idx < 0)
  445. break;
  446. newLoose = curLoose.remove(idx);
  447. } while (!looseRefs.compareAndSet(curLoose, newLoose));
  448. int levels = levelsIn(name) - 2;
  449. delete(logFor(name), levels);
  450. if (dst.getStorage().isLoose()) {
  451. update.unlock();
  452. delete(fileFor(name), levels);
  453. }
  454. modCnt.incrementAndGet();
  455. fireRefsChanged();
  456. }
  457. void log(final RefUpdate update, final String msg, final boolean deref)
  458. throws IOException {
  459. final ObjectId oldId = update.getOldObjectId();
  460. final ObjectId newId = update.getNewObjectId();
  461. final Ref ref = update.getRef();
  462. PersonIdent ident = update.getRefLogIdent();
  463. if (ident == null)
  464. ident = new PersonIdent(parent);
  465. else
  466. ident = new PersonIdent(ident);
  467. final StringBuilder r = new StringBuilder();
  468. r.append(ObjectId.toString(oldId));
  469. r.append(' ');
  470. r.append(ObjectId.toString(newId));
  471. r.append(' ');
  472. r.append(ident.toExternalString());
  473. r.append('\t');
  474. r.append(msg);
  475. r.append('\n');
  476. final byte[] rec = encode(r.toString());
  477. if (deref && ref.isSymbolic()) {
  478. log(ref.getName(), rec);
  479. log(ref.getLeaf().getName(), rec);
  480. } else {
  481. log(ref.getName(), rec);
  482. }
  483. }
  484. private void log(final String refName, final byte[] rec) throws IOException {
  485. final File log = logFor(refName);
  486. final boolean write;
  487. if (isLogAllRefUpdates() && shouldAutoCreateLog(refName))
  488. write = true;
  489. else if (log.isFile())
  490. write = true;
  491. else
  492. write = false;
  493. if (write) {
  494. FileOutputStream out;
  495. try {
  496. out = new FileOutputStream(log, true);
  497. } catch (FileNotFoundException err) {
  498. final File dir = log.getParentFile();
  499. if (dir.exists())
  500. throw err;
  501. if (!dir.mkdirs() && !dir.isDirectory())
  502. throw new IOException(MessageFormat.format(JGitText.get().cannotCreateDirectory, dir));
  503. out = new FileOutputStream(log, true);
  504. }
  505. try {
  506. out.write(rec);
  507. } finally {
  508. out.close();
  509. }
  510. }
  511. }
  512. private boolean isLogAllRefUpdates() {
  513. return parent.getConfig().getCore().isLogAllRefUpdates();
  514. }
  515. private boolean shouldAutoCreateLog(final String refName) {
  516. return refName.equals(HEAD) //
  517. || refName.startsWith(R_HEADS) //
  518. || refName.startsWith(R_REMOTES);
  519. }
  520. private Ref resolve(final Ref ref, int depth, String prefix,
  521. RefList<LooseRef> loose, RefList<Ref> packed) throws IOException {
  522. if (ref.isSymbolic()) {
  523. Ref dst = ref.getTarget();
  524. if (MAX_SYMBOLIC_REF_DEPTH <= depth)
  525. return null; // claim it doesn't exist
  526. // If the cached value can be assumed to be current due to a
  527. // recent scan of the loose directory, use it.
  528. if (loose != null && dst.getName().startsWith(prefix)) {
  529. int idx;
  530. if (0 <= (idx = loose.find(dst.getName())))
  531. dst = loose.get(idx);
  532. else if (0 <= (idx = packed.find(dst.getName())))
  533. dst = packed.get(idx);
  534. else
  535. return ref;
  536. } else {
  537. dst = readRef(dst.getName(), packed);
  538. if (dst == null)
  539. return ref;
  540. }
  541. dst = resolve(dst, depth + 1, prefix, loose, packed);
  542. if (dst == null)
  543. return null;
  544. return new SymbolicRef(ref.getName(), dst);
  545. }
  546. return ref;
  547. }
  548. private PackedRefList getPackedRefs() throws IOException {
  549. long size = packedRefsFile.length();
  550. long mtime = size != 0 ? packedRefsFile.lastModified() : 0;
  551. final PackedRefList curList = packedRefs.get();
  552. if (size == curList.lastSize && mtime == curList.lastModified)
  553. return curList;
  554. final PackedRefList newList = readPackedRefs(size, mtime);
  555. if (packedRefs.compareAndSet(curList, newList))
  556. modCnt.incrementAndGet();
  557. return newList;
  558. }
  559. private PackedRefList readPackedRefs(long size, long mtime)
  560. throws IOException {
  561. final BufferedReader br;
  562. try {
  563. br = new BufferedReader(new InputStreamReader(new FileInputStream(
  564. packedRefsFile), CHARSET));
  565. } catch (FileNotFoundException noPackedRefs) {
  566. // Ignore it and leave the new list empty.
  567. return PackedRefList.NO_PACKED_REFS;
  568. }
  569. try {
  570. return new PackedRefList(parsePackedRefs(br), size, mtime);
  571. } finally {
  572. br.close();
  573. }
  574. }
  575. private RefList<Ref> parsePackedRefs(final BufferedReader br)
  576. throws IOException {
  577. RefList.Builder<Ref> all = new RefList.Builder<Ref>();
  578. Ref last = null;
  579. boolean peeled = false;
  580. boolean needSort = false;
  581. String p;
  582. while ((p = br.readLine()) != null) {
  583. if (p.charAt(0) == '#') {
  584. if (p.startsWith(PACKED_REFS_HEADER)) {
  585. p = p.substring(PACKED_REFS_HEADER.length());
  586. peeled = p.contains(PACKED_REFS_PEELED);
  587. }
  588. continue;
  589. }
  590. if (p.charAt(0) == '^') {
  591. if (last == null)
  592. throw new IOException(JGitText.get().peeledLineBeforeRef);
  593. ObjectId id = ObjectId.fromString(p.substring(1));
  594. last = new ObjectIdRef.PeeledTag(PACKED, last.getName(), last
  595. .getObjectId(), id);
  596. all.set(all.size() - 1, last);
  597. continue;
  598. }
  599. int sp = p.indexOf(' ');
  600. ObjectId id = ObjectId.fromString(p.substring(0, sp));
  601. String name = copy(p, sp + 1, p.length());
  602. ObjectIdRef cur;
  603. if (peeled)
  604. cur = new ObjectIdRef.PeeledNonTag(PACKED, name, id);
  605. else
  606. cur = new ObjectIdRef.Unpeeled(PACKED, name, id);
  607. if (last != null && RefComparator.compareTo(last, cur) > 0)
  608. needSort = true;
  609. all.add(cur);
  610. last = cur;
  611. }
  612. if (needSort)
  613. all.sort();
  614. return all.toRefList();
  615. }
  616. private static String copy(final String src, final int off, final int end) {
  617. // Don't use substring since it could leave a reference to the much
  618. // larger existing string. Force construction of a full new object.
  619. return new StringBuilder(end - off).append(src, off, end).toString();
  620. }
  621. private void commitPackedRefs(final LockFile lck, final RefList<Ref> refs,
  622. final PackedRefList oldPackedList) throws IOException {
  623. new RefWriter(refs) {
  624. @Override
  625. protected void writeFile(String name, byte[] content)
  626. throws IOException {
  627. lck.setNeedStatInformation(true);
  628. try {
  629. lck.write(content);
  630. } catch (IOException ioe) {
  631. throw new ObjectWritingException(MessageFormat.format(JGitText.get().unableToWrite, name), ioe);
  632. }
  633. try {
  634. lck.waitForStatChange();
  635. } catch (InterruptedException e) {
  636. lck.unlock();
  637. throw new ObjectWritingException(MessageFormat.format(JGitText.get().interruptedWriting, name));
  638. }
  639. if (!lck.commit())
  640. throw new ObjectWritingException(MessageFormat.format(JGitText.get().unableToWrite, name));
  641. packedRefs.compareAndSet(oldPackedList, new PackedRefList(refs,
  642. content.length, lck.getCommitLastModified()));
  643. }
  644. }.writePackedRefs();
  645. }
  646. private Ref readRef(String name, RefList<Ref> packed) throws IOException {
  647. final RefList<LooseRef> curList = looseRefs.get();
  648. final int idx = curList.find(name);
  649. if (0 <= idx) {
  650. final LooseRef o = curList.get(idx);
  651. final LooseRef n = scanRef(o, name);
  652. if (n == null) {
  653. if (looseRefs.compareAndSet(curList, curList.remove(idx)))
  654. modCnt.incrementAndGet();
  655. return packed.get(name);
  656. }
  657. if (o == n)
  658. return n;
  659. if (looseRefs.compareAndSet(curList, curList.set(idx, n)))
  660. modCnt.incrementAndGet();
  661. return n;
  662. }
  663. final LooseRef n = scanRef(null, name);
  664. if (n == null)
  665. return packed.get(name);
  666. if (looseRefs.compareAndSet(curList, curList.add(idx, n)))
  667. modCnt.incrementAndGet();
  668. return n;
  669. }
  670. private LooseRef scanRef(LooseRef ref, String name) throws IOException {
  671. final File path = fileFor(name);
  672. final long modified = path.lastModified();
  673. if (ref != null) {
  674. if (ref.getLastModified() == modified)
  675. return ref;
  676. name = ref.getName();
  677. } else if (modified == 0)
  678. return null;
  679. final byte[] buf;
  680. try {
  681. buf = IO.readFully(path, 4096);
  682. } catch (FileNotFoundException noFile) {
  683. return null; // doesn't exist; not a reference.
  684. }
  685. int n = buf.length;
  686. if (n == 0)
  687. return null; // empty file; not a reference.
  688. if (isSymRef(buf, n)) {
  689. // trim trailing whitespace
  690. while (0 < n && Character.isWhitespace(buf[n - 1]))
  691. n--;
  692. if (n < 6) {
  693. String content = RawParseUtils.decode(buf, 0, n);
  694. throw new IOException(MessageFormat.format(JGitText.get().notARef, name, content));
  695. }
  696. final String target = RawParseUtils.decode(buf, 5, n);
  697. return newSymbolicRef(modified, name, target);
  698. }
  699. if (n < OBJECT_ID_STRING_LENGTH)
  700. return null; // impossibly short object identifier; not a reference.
  701. final ObjectId id;
  702. try {
  703. id = ObjectId.fromString(buf, 0);
  704. } catch (IllegalArgumentException notRef) {
  705. while (0 < n && Character.isWhitespace(buf[n - 1]))
  706. n--;
  707. String content = RawParseUtils.decode(buf, 0, n);
  708. throw new IOException(MessageFormat.format(JGitText.get().notARef, name, content));
  709. }
  710. return new LooseUnpeeled(modified, name, id);
  711. }
  712. private static boolean isSymRef(final byte[] buf, int n) {
  713. if (n < 6)
  714. return false;
  715. return /**/buf[0] == 'r' //
  716. && buf[1] == 'e' //
  717. && buf[2] == 'f' //
  718. && buf[3] == ':' //
  719. && buf[4] == ' ';
  720. }
  721. /** If the parent should fire listeners, fires them. */
  722. private void fireRefsChanged() {
  723. final int last = lastNotifiedModCnt.get();
  724. final int curr = modCnt.get();
  725. if (last != curr && lastNotifiedModCnt.compareAndSet(last, curr))
  726. parent.fireRefsChanged();
  727. }
  728. /**
  729. * Create a reference update to write a temporary reference.
  730. *
  731. * @return an update for a new temporary reference.
  732. * @throws IOException
  733. * a temporary name cannot be allocated.
  734. */
  735. RefDirectoryUpdate newTemporaryUpdate() throws IOException {
  736. File tmp = File.createTempFile("renamed_", "_ref", refsDir);
  737. String name = Constants.R_REFS + tmp.getName();
  738. Ref ref = new ObjectIdRef.Unpeeled(NEW, name, null);
  739. return new RefDirectoryUpdate(this, ref);
  740. }
  741. /**
  742. * Locate the file on disk for a single reference name.
  743. *
  744. * @param name
  745. * name of the ref, relative to the Git repository top level
  746. * directory (so typically starts with refs/).
  747. * @return the loose file location.
  748. */
  749. File fileFor(String name) {
  750. if (name.startsWith(R_REFS)) {
  751. name = name.substring(R_REFS.length());
  752. return new File(refsDir, name);
  753. }
  754. return new File(gitDir, name);
  755. }
  756. /**
  757. * Locate the log file on disk for a single reference name.
  758. *
  759. * @param name
  760. * name of the ref, relative to the Git repository top level
  761. * directory (so typically starts with refs/).
  762. * @return the log file location.
  763. */
  764. File logFor(String name) {
  765. if (name.startsWith(R_REFS)) {
  766. name = name.substring(R_REFS.length());
  767. return new File(logsRefsDir, name);
  768. }
  769. return new File(logsDir, name);
  770. }
  771. static int levelsIn(final String name) {
  772. int count = 0;
  773. for (int p = name.indexOf('/'); p >= 0; p = name.indexOf('/', p + 1))
  774. count++;
  775. return count;
  776. }
  777. static void delete(final File file, final int depth) throws IOException {
  778. if (!file.delete() && file.isFile())
  779. throw new IOException(MessageFormat.format(JGitText.get().fileCannotBeDeleted, file));
  780. File dir = file.getParentFile();
  781. for (int i = 0; i < depth; ++i) {
  782. if (!dir.delete())
  783. break; // ignore problem here
  784. dir = dir.getParentFile();
  785. }
  786. }
  787. private static class PackedRefList extends RefList<Ref> {
  788. static final PackedRefList NO_PACKED_REFS = new PackedRefList(RefList
  789. .emptyList(), 0, 0);
  790. /** Last length of the packed-refs file when we read it. */
  791. final long lastSize;
  792. /** Last modified time of the packed-refs file when we read it. */
  793. final long lastModified;
  794. PackedRefList(RefList<Ref> src, long size, long mtime) {
  795. super(src);
  796. lastSize = size;
  797. lastModified = mtime;
  798. }
  799. }
  800. private static LooseSymbolicRef newSymbolicRef(long lastModified,
  801. String name, String target) {
  802. Ref dst = new ObjectIdRef.Unpeeled(NEW, target, null);
  803. return new LooseSymbolicRef(lastModified, name, dst);
  804. }
  805. private static interface LooseRef extends Ref {
  806. long getLastModified();
  807. LooseRef peel(ObjectIdRef newLeaf);
  808. }
  809. private final static class LoosePeeledTag extends ObjectIdRef.PeeledTag
  810. implements LooseRef {
  811. private final long lastModified;
  812. LoosePeeledTag(long mtime, String refName, ObjectId id, ObjectId p) {
  813. super(LOOSE, refName, id, p);
  814. this.lastModified = mtime;
  815. }
  816. public long getLastModified() {
  817. return lastModified;
  818. }
  819. public LooseRef peel(ObjectIdRef newLeaf) {
  820. return this;
  821. }
  822. }
  823. private final static class LooseNonTag extends ObjectIdRef.PeeledNonTag
  824. implements LooseRef {
  825. private final long lastModified;
  826. LooseNonTag(long mtime, String refName, ObjectId id) {
  827. super(LOOSE, refName, id);
  828. this.lastModified = mtime;
  829. }
  830. public long getLastModified() {
  831. return lastModified;
  832. }
  833. public LooseRef peel(ObjectIdRef newLeaf) {
  834. return this;
  835. }
  836. }
  837. private final static class LooseUnpeeled extends ObjectIdRef.Unpeeled
  838. implements LooseRef {
  839. private final long lastModified;
  840. LooseUnpeeled(long mtime, String refName, ObjectId id) {
  841. super(LOOSE, refName, id);
  842. this.lastModified = mtime;
  843. }
  844. public long getLastModified() {
  845. return lastModified;
  846. }
  847. public LooseRef peel(ObjectIdRef newLeaf) {
  848. if (newLeaf.getPeeledObjectId() != null)
  849. return new LoosePeeledTag(lastModified, getName(),
  850. getObjectId(), newLeaf.getPeeledObjectId());
  851. else
  852. return new LooseNonTag(lastModified, getName(), getObjectId());
  853. }
  854. }
  855. private final static class LooseSymbolicRef extends SymbolicRef implements
  856. LooseRef {
  857. private final long lastModified;
  858. LooseSymbolicRef(long mtime, String refName, Ref target) {
  859. super(refName, target);
  860. this.lastModified = mtime;
  861. }
  862. public long getLastModified() {
  863. return lastModified;
  864. }
  865. public LooseRef peel(ObjectIdRef newLeaf) {
  866. // We should never try to peel the symbolic references.
  867. throw new UnsupportedOperationException();
  868. }
  869. }
  870. }