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

BaseRepositoryBuilder.java 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.lib;
  44. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_CORE_SECTION;
  45. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_BARE;
  46. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_WORKTREE;
  47. import static org.eclipse.jgit.lib.Constants.DOT_GIT;
  48. import static org.eclipse.jgit.lib.Constants.GIT_ALTERNATE_OBJECT_DIRECTORIES_KEY;
  49. import static org.eclipse.jgit.lib.Constants.GIT_CEILING_DIRECTORIES_KEY;
  50. import static org.eclipse.jgit.lib.Constants.GIT_DIR_KEY;
  51. import static org.eclipse.jgit.lib.Constants.GIT_INDEX_FILE_KEY;
  52. import static org.eclipse.jgit.lib.Constants.GIT_OBJECT_DIRECTORY_KEY;
  53. import static org.eclipse.jgit.lib.Constants.GIT_WORK_TREE_KEY;
  54. import java.io.File;
  55. import java.io.IOException;
  56. import java.text.MessageFormat;
  57. import java.util.Collection;
  58. import java.util.LinkedList;
  59. import java.util.List;
  60. import org.eclipse.jgit.errors.ConfigInvalidException;
  61. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  62. import org.eclipse.jgit.internal.JGitText;
  63. import org.eclipse.jgit.internal.storage.file.FileRepository;
  64. import org.eclipse.jgit.lib.RepositoryCache.FileKey;
  65. import org.eclipse.jgit.storage.file.FileBasedConfig;
  66. import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
  67. import org.eclipse.jgit.util.FS;
  68. import org.eclipse.jgit.util.IO;
  69. import org.eclipse.jgit.util.RawParseUtils;
  70. import org.eclipse.jgit.util.SystemReader;
  71. /**
  72. * Base builder to customize repository construction.
  73. * <p>
  74. * Repository implementations may subclass this builder in order to add custom
  75. * repository detection methods.
  76. *
  77. * @param <B>
  78. * type of the repository builder.
  79. * @param <R>
  80. * type of the repository that is constructed.
  81. * @see RepositoryBuilder
  82. * @see FileRepositoryBuilder
  83. */
  84. public class BaseRepositoryBuilder<B extends BaseRepositoryBuilder, R extends Repository> {
  85. private static boolean isSymRef(byte[] ref) {
  86. if (ref.length < 9)
  87. return false;
  88. return /**/ref[0] == 'g' //
  89. && ref[1] == 'i' //
  90. && ref[2] == 't' //
  91. && ref[3] == 'd' //
  92. && ref[4] == 'i' //
  93. && ref[5] == 'r' //
  94. && ref[6] == ':' //
  95. && ref[7] == ' ';
  96. }
  97. private static File getSymRef(File workTree, File dotGit, FS fs)
  98. throws IOException {
  99. byte[] content = IO.readFully(dotGit);
  100. if (!isSymRef(content))
  101. throw new IOException(MessageFormat.format(
  102. JGitText.get().invalidGitdirRef, dotGit.getAbsolutePath()));
  103. int pathStart = 8;
  104. int lineEnd = RawParseUtils.nextLF(content, pathStart);
  105. if (content[lineEnd - 1] == '\n')
  106. lineEnd--;
  107. if (lineEnd == pathStart)
  108. throw new IOException(MessageFormat.format(
  109. JGitText.get().invalidGitdirRef, dotGit.getAbsolutePath()));
  110. String gitdirPath = RawParseUtils.decode(content, pathStart, lineEnd);
  111. File gitdirFile = fs.resolve(workTree, gitdirPath);
  112. if (gitdirFile.isAbsolute())
  113. return gitdirFile;
  114. else
  115. return new File(workTree, gitdirPath).getCanonicalFile();
  116. }
  117. private FS fs;
  118. private File gitDir;
  119. private File objectDirectory;
  120. private List<File> alternateObjectDirectories;
  121. private File indexFile;
  122. private File workTree;
  123. /** Directories limiting the search for a Git repository. */
  124. private List<File> ceilingDirectories;
  125. /** True only if the caller wants to force bare behavior. */
  126. private boolean bare;
  127. /** True if the caller requires the repository to exist. */
  128. private boolean mustExist;
  129. /** Configuration file of target repository, lazily loaded if required. */
  130. private Config config;
  131. /**
  132. * Set the file system abstraction needed by this repository.
  133. *
  134. * @param fs
  135. * the abstraction.
  136. * @return {@code this} (for chaining calls).
  137. */
  138. public B setFS(FS fs) {
  139. this.fs = fs;
  140. return self();
  141. }
  142. /** @return the file system abstraction, or null if not set. */
  143. public FS getFS() {
  144. return fs;
  145. }
  146. /**
  147. * Set the Git directory storing the repository metadata.
  148. * <p>
  149. * The meta directory stores the objects, references, and meta files like
  150. * {@code MERGE_HEAD}, or the index file. If {@code null} the path is
  151. * assumed to be {@code workTree/.git}.
  152. *
  153. * @param gitDir
  154. * {@code GIT_DIR}, the repository meta directory.
  155. * @return {@code this} (for chaining calls).
  156. */
  157. public B setGitDir(File gitDir) {
  158. this.gitDir = gitDir;
  159. this.config = null;
  160. return self();
  161. }
  162. /** @return the meta data directory; null if not set. */
  163. public File getGitDir() {
  164. return gitDir;
  165. }
  166. /**
  167. * Set the directory storing the repository's objects.
  168. *
  169. * @param objectDirectory
  170. * {@code GIT_OBJECT_DIRECTORY}, the directory where the
  171. * repository's object files are stored.
  172. * @return {@code this} (for chaining calls).
  173. */
  174. public B setObjectDirectory(File objectDirectory) {
  175. this.objectDirectory = objectDirectory;
  176. return self();
  177. }
  178. /** @return the object directory; null if not set. */
  179. public File getObjectDirectory() {
  180. return objectDirectory;
  181. }
  182. /**
  183. * Add an alternate object directory to the search list.
  184. * <p>
  185. * This setting handles one alternate directory at a time, and is provided
  186. * to support {@code GIT_ALTERNATE_OBJECT_DIRECTORIES}.
  187. *
  188. * @param other
  189. * another objects directory to search after the standard one.
  190. * @return {@code this} (for chaining calls).
  191. */
  192. public B addAlternateObjectDirectory(File other) {
  193. if (other != null) {
  194. if (alternateObjectDirectories == null)
  195. alternateObjectDirectories = new LinkedList<File>();
  196. alternateObjectDirectories.add(other);
  197. }
  198. return self();
  199. }
  200. /**
  201. * Add alternate object directories to the search list.
  202. * <p>
  203. * This setting handles several alternate directories at once, and is
  204. * provided to support {@code GIT_ALTERNATE_OBJECT_DIRECTORIES}.
  205. *
  206. * @param inList
  207. * other object directories to search after the standard one. The
  208. * collection's contents is copied to an internal list.
  209. * @return {@code this} (for chaining calls).
  210. */
  211. public B addAlternateObjectDirectories(Collection<File> inList) {
  212. if (inList != null) {
  213. for (File path : inList)
  214. addAlternateObjectDirectory(path);
  215. }
  216. return self();
  217. }
  218. /**
  219. * Add alternate object directories to the search list.
  220. * <p>
  221. * This setting handles several alternate directories at once, and is
  222. * provided to support {@code GIT_ALTERNATE_OBJECT_DIRECTORIES}.
  223. *
  224. * @param inList
  225. * other object directories to search after the standard one. The
  226. * array's contents is copied to an internal list.
  227. * @return {@code this} (for chaining calls).
  228. */
  229. public B addAlternateObjectDirectories(File[] inList) {
  230. if (inList != null) {
  231. for (File path : inList)
  232. addAlternateObjectDirectory(path);
  233. }
  234. return self();
  235. }
  236. /** @return ordered array of alternate directories; null if non were set. */
  237. public File[] getAlternateObjectDirectories() {
  238. final List<File> alts = alternateObjectDirectories;
  239. if (alts == null)
  240. return null;
  241. return alts.toArray(new File[alts.size()]);
  242. }
  243. /**
  244. * Force the repository to be treated as bare (have no working directory).
  245. * <p>
  246. * If bare the working directory aspects of the repository won't be
  247. * configured, and will not be accessible.
  248. *
  249. * @return {@code this} (for chaining calls).
  250. */
  251. public B setBare() {
  252. setIndexFile(null);
  253. setWorkTree(null);
  254. bare = true;
  255. return self();
  256. }
  257. /** @return true if this repository was forced bare by {@link #setBare()}. */
  258. public boolean isBare() {
  259. return bare;
  260. }
  261. /**
  262. * Require the repository to exist before it can be opened.
  263. *
  264. * @param mustExist
  265. * true if it must exist; false if it can be missing and created
  266. * after being built.
  267. * @return {@code this} (for chaining calls).
  268. */
  269. public B setMustExist(boolean mustExist) {
  270. this.mustExist = mustExist;
  271. return self();
  272. }
  273. /** @return true if the repository must exist before being opened. */
  274. public boolean isMustExist() {
  275. return mustExist;
  276. }
  277. /**
  278. * Set the top level directory of the working files.
  279. *
  280. * @param workTree
  281. * {@code GIT_WORK_TREE}, the working directory of the checkout.
  282. * @return {@code this} (for chaining calls).
  283. */
  284. public B setWorkTree(File workTree) {
  285. this.workTree = workTree;
  286. return self();
  287. }
  288. /** @return the work tree directory, or null if not set. */
  289. public File getWorkTree() {
  290. return workTree;
  291. }
  292. /**
  293. * Set the local index file that is caching checked out file status.
  294. * <p>
  295. * The location of the index file tracking the status information for each
  296. * checked out file in {@code workTree}. This may be null to assume the
  297. * default {@code gitDiir/index}.
  298. *
  299. * @param indexFile
  300. * {@code GIT_INDEX_FILE}, the index file location.
  301. * @return {@code this} (for chaining calls).
  302. */
  303. public B setIndexFile(File indexFile) {
  304. this.indexFile = indexFile;
  305. return self();
  306. }
  307. /** @return the index file location, or null if not set. */
  308. public File getIndexFile() {
  309. return indexFile;
  310. }
  311. /**
  312. * Read standard Git environment variables and configure from those.
  313. * <p>
  314. * This method tries to read the standard Git environment variables, such as
  315. * {@code GIT_DIR} and {@code GIT_WORK_TREE} to configure this builder
  316. * instance. If an environment variable is set, it overrides the value
  317. * already set in this builder.
  318. *
  319. * @return {@code this} (for chaining calls).
  320. */
  321. public B readEnvironment() {
  322. return readEnvironment(SystemReader.getInstance());
  323. }
  324. /**
  325. * Read standard Git environment variables and configure from those.
  326. * <p>
  327. * This method tries to read the standard Git environment variables, such as
  328. * {@code GIT_DIR} and {@code GIT_WORK_TREE} to configure this builder
  329. * instance. If a property is already set in the builder, the environment
  330. * variable is not used.
  331. *
  332. * @param sr
  333. * the SystemReader abstraction to access the environment.
  334. * @return {@code this} (for chaining calls).
  335. */
  336. public B readEnvironment(SystemReader sr) {
  337. if (getGitDir() == null) {
  338. String val = sr.getenv(GIT_DIR_KEY);
  339. if (val != null)
  340. setGitDir(new File(val));
  341. }
  342. if (getObjectDirectory() == null) {
  343. String val = sr.getenv(GIT_OBJECT_DIRECTORY_KEY);
  344. if (val != null)
  345. setObjectDirectory(new File(val));
  346. }
  347. if (getAlternateObjectDirectories() == null) {
  348. String val = sr.getenv(GIT_ALTERNATE_OBJECT_DIRECTORIES_KEY);
  349. if (val != null) {
  350. for (String path : val.split(File.pathSeparator))
  351. addAlternateObjectDirectory(new File(path));
  352. }
  353. }
  354. if (getWorkTree() == null) {
  355. String val = sr.getenv(GIT_WORK_TREE_KEY);
  356. if (val != null)
  357. setWorkTree(new File(val));
  358. }
  359. if (getIndexFile() == null) {
  360. String val = sr.getenv(GIT_INDEX_FILE_KEY);
  361. if (val != null)
  362. setIndexFile(new File(val));
  363. }
  364. if (ceilingDirectories == null) {
  365. String val = sr.getenv(GIT_CEILING_DIRECTORIES_KEY);
  366. if (val != null) {
  367. for (String path : val.split(File.pathSeparator))
  368. addCeilingDirectory(new File(path));
  369. }
  370. }
  371. return self();
  372. }
  373. /**
  374. * Add a ceiling directory to the search limit list.
  375. * <p>
  376. * This setting handles one ceiling directory at a time, and is provided to
  377. * support {@code GIT_CEILING_DIRECTORIES}.
  378. *
  379. * @param root
  380. * a path to stop searching at; its parent will not be searched.
  381. * @return {@code this} (for chaining calls).
  382. */
  383. public B addCeilingDirectory(File root) {
  384. if (root != null) {
  385. if (ceilingDirectories == null)
  386. ceilingDirectories = new LinkedList<File>();
  387. ceilingDirectories.add(root);
  388. }
  389. return self();
  390. }
  391. /**
  392. * Add ceiling directories to the search list.
  393. * <p>
  394. * This setting handles several ceiling directories at once, and is provided
  395. * to support {@code GIT_CEILING_DIRECTORIES}.
  396. *
  397. * @param inList
  398. * directory paths to stop searching at. The collection's
  399. * contents is copied to an internal list.
  400. * @return {@code this} (for chaining calls).
  401. */
  402. public B addCeilingDirectories(Collection<File> inList) {
  403. if (inList != null) {
  404. for (File path : inList)
  405. addCeilingDirectory(path);
  406. }
  407. return self();
  408. }
  409. /**
  410. * Add ceiling directories to the search list.
  411. * <p>
  412. * This setting handles several ceiling directories at once, and is provided
  413. * to support {@code GIT_CEILING_DIRECTORIES}.
  414. *
  415. * @param inList
  416. * directory paths to stop searching at. The array's contents is
  417. * copied to an internal list.
  418. * @return {@code this} (for chaining calls).
  419. */
  420. public B addCeilingDirectories(File[] inList) {
  421. if (inList != null) {
  422. for (File path : inList)
  423. addCeilingDirectory(path);
  424. }
  425. return self();
  426. }
  427. /**
  428. * Configure {@code GIT_DIR} by searching up the file system.
  429. * <p>
  430. * Starts from the current working directory of the JVM and scans up through
  431. * the directory tree until a Git repository is found. Success can be
  432. * determined by checking for {@code getGitDir() != null}.
  433. * <p>
  434. * The search can be limited to specific spaces of the local filesystem by
  435. * {@link #addCeilingDirectory(File)}, or inheriting the list through a
  436. * prior call to {@link #readEnvironment()}.
  437. *
  438. * @return {@code this} (for chaining calls).
  439. */
  440. public B findGitDir() {
  441. if (getGitDir() == null)
  442. findGitDir(new File("").getAbsoluteFile()); //$NON-NLS-1$
  443. return self();
  444. }
  445. /**
  446. * Configure {@code GIT_DIR} by searching up the file system.
  447. * <p>
  448. * Starts from the supplied directory path and scans up through the parent
  449. * directory tree until a Git repository is found. Success can be determined
  450. * by checking for {@code getGitDir() != null}.
  451. * <p>
  452. * The search can be limited to specific spaces of the local filesystem by
  453. * {@link #addCeilingDirectory(File)}, or inheriting the list through a
  454. * prior call to {@link #readEnvironment()}.
  455. *
  456. * @param current
  457. * directory to begin searching in.
  458. * @return {@code this} (for chaining calls).
  459. */
  460. public B findGitDir(File current) {
  461. if (getGitDir() == null) {
  462. FS tryFS = safeFS();
  463. while (current != null) {
  464. File dir = new File(current, DOT_GIT);
  465. if (FileKey.isGitRepository(dir, tryFS)) {
  466. setGitDir(dir);
  467. break;
  468. } else if (dir.isFile()) {
  469. try {
  470. setGitDir(getSymRef(current, dir, tryFS));
  471. break;
  472. } catch (IOException ignored) {
  473. // Continue searching if gitdir ref isn't found
  474. }
  475. } else if (FileKey.isGitRepository(current, tryFS)) {
  476. setGitDir(current);
  477. break;
  478. }
  479. current = current.getParentFile();
  480. if (current != null && ceilingDirectories != null
  481. && ceilingDirectories.contains(current))
  482. break;
  483. }
  484. }
  485. return self();
  486. }
  487. /**
  488. * Guess and populate all parameters not already defined.
  489. * <p>
  490. * If an option was not set, the setup method will try to default the option
  491. * based on other options. If insufficient information is available, an
  492. * exception is thrown to the caller.
  493. *
  494. * @return {@code this}
  495. * @throws IllegalArgumentException
  496. * insufficient parameters were set, or some parameters are
  497. * incompatible with one another.
  498. * @throws IOException
  499. * the repository could not be accessed to configure the rest of
  500. * the builder's parameters.
  501. */
  502. public B setup() throws IllegalArgumentException, IOException {
  503. requireGitDirOrWorkTree();
  504. setupGitDir();
  505. setupWorkTree();
  506. setupInternals();
  507. return self();
  508. }
  509. /**
  510. * Create a repository matching the configuration in this builder.
  511. * <p>
  512. * If an option was not set, the build method will try to default the option
  513. * based on other options. If insufficient information is available, an
  514. * exception is thrown to the caller.
  515. *
  516. * @return a repository matching this configuration. The caller is
  517. * responsible to close the repository instance when it is no longer
  518. * needed.
  519. * @throws IllegalArgumentException
  520. * insufficient parameters were set.
  521. * @throws IOException
  522. * the repository could not be accessed to configure the rest of
  523. * the builder's parameters.
  524. */
  525. @SuppressWarnings({ "unchecked", "resource" })
  526. public R build() throws IOException {
  527. R repo = (R) new FileRepository(setup());
  528. if (isMustExist() && !repo.getObjectDatabase().exists())
  529. throw new RepositoryNotFoundException(getGitDir());
  530. return repo;
  531. }
  532. /** Require either {@code gitDir} or {@code workTree} to be set. */
  533. protected void requireGitDirOrWorkTree() {
  534. if (getGitDir() == null && getWorkTree() == null)
  535. throw new IllegalArgumentException(
  536. JGitText.get().eitherGitDirOrWorkTreeRequired);
  537. }
  538. /**
  539. * Perform standard gitDir initialization.
  540. *
  541. * @throws IOException
  542. * the repository could not be accessed
  543. */
  544. protected void setupGitDir() throws IOException {
  545. // No gitDir? Try to assume its under the workTree or a ref to another
  546. // location
  547. if (getGitDir() == null && getWorkTree() != null) {
  548. File dotGit = new File(getWorkTree(), DOT_GIT);
  549. if (!dotGit.isFile())
  550. setGitDir(dotGit);
  551. else
  552. setGitDir(getSymRef(getWorkTree(), dotGit, safeFS()));
  553. }
  554. }
  555. /**
  556. * Perform standard work-tree initialization.
  557. * <p>
  558. * This is a method typically invoked inside of {@link #setup()}, near the
  559. * end after the repository has been identified and its configuration is
  560. * available for inspection.
  561. *
  562. * @throws IOException
  563. * the repository configuration could not be read.
  564. */
  565. protected void setupWorkTree() throws IOException {
  566. if (getFS() == null)
  567. setFS(FS.DETECTED);
  568. // If we aren't bare, we should have a work tree.
  569. //
  570. if (!isBare() && getWorkTree() == null)
  571. setWorkTree(guessWorkTreeOrFail());
  572. if (!isBare()) {
  573. // If after guessing we're still not bare, we must have
  574. // a metadata directory to hold the repository. Assume
  575. // its at the work tree.
  576. //
  577. if (getGitDir() == null)
  578. setGitDir(getWorkTree().getParentFile());
  579. if (getIndexFile() == null)
  580. setIndexFile(new File(getGitDir(), "index")); //$NON-NLS-1$
  581. }
  582. }
  583. /**
  584. * Configure the internal implementation details of the repository.
  585. *
  586. * @throws IOException
  587. * the repository could not be accessed
  588. */
  589. protected void setupInternals() throws IOException {
  590. if (getObjectDirectory() == null && getGitDir() != null)
  591. setObjectDirectory(safeFS().resolve(getGitDir(), "objects")); //$NON-NLS-1$
  592. }
  593. /**
  594. * Get the cached repository configuration, loading if not yet available.
  595. *
  596. * @return the configuration of the repository.
  597. * @throws IOException
  598. * the configuration is not available, or is badly formed.
  599. */
  600. protected Config getConfig() throws IOException {
  601. if (config == null)
  602. config = loadConfig();
  603. return config;
  604. }
  605. /**
  606. * Parse and load the repository specific configuration.
  607. * <p>
  608. * The default implementation reads {@code gitDir/config}, or returns an
  609. * empty configuration if gitDir was not set.
  610. *
  611. * @return the repository's configuration.
  612. * @throws IOException
  613. * the configuration is not available.
  614. */
  615. protected Config loadConfig() throws IOException {
  616. if (getGitDir() != null) {
  617. // We only want the repository's configuration file, and not
  618. // the user file, as these parameters must be unique to this
  619. // repository and not inherited from other files.
  620. //
  621. File path = safeFS().resolve(getGitDir(), Constants.CONFIG);
  622. FileBasedConfig cfg = new FileBasedConfig(path, safeFS());
  623. try {
  624. cfg.load();
  625. } catch (ConfigInvalidException err) {
  626. throw new IllegalArgumentException(MessageFormat.format(
  627. JGitText.get().repositoryConfigFileInvalid, path
  628. .getAbsolutePath(), err.getMessage()));
  629. }
  630. return cfg;
  631. } else {
  632. return new Config();
  633. }
  634. }
  635. private File guessWorkTreeOrFail() throws IOException {
  636. final Config cfg = getConfig();
  637. // If set, core.worktree wins.
  638. //
  639. String path = cfg.getString(CONFIG_CORE_SECTION, null,
  640. CONFIG_KEY_WORKTREE);
  641. if (path != null)
  642. return safeFS().resolve(getGitDir(), path).getCanonicalFile();
  643. // If core.bare is set, honor its value. Assume workTree is
  644. // the parent directory of the repository.
  645. //
  646. if (cfg.getString(CONFIG_CORE_SECTION, null, CONFIG_KEY_BARE) != null) {
  647. if (cfg.getBoolean(CONFIG_CORE_SECTION, CONFIG_KEY_BARE, true)) {
  648. setBare();
  649. return null;
  650. }
  651. return getGitDir().getParentFile();
  652. }
  653. if (getGitDir().getName().equals(DOT_GIT)) {
  654. // No value for the "bare" flag, but gitDir is named ".git",
  655. // use the parent of the directory
  656. //
  657. return getGitDir().getParentFile();
  658. }
  659. // We have to assume we are bare.
  660. //
  661. setBare();
  662. return null;
  663. }
  664. /** @return the configured FS, or {@link FS#DETECTED}. */
  665. protected FS safeFS() {
  666. return getFS() != null ? getFS() : FS.DETECTED;
  667. }
  668. /** @return {@code this} */
  669. @SuppressWarnings("unchecked")
  670. protected final B self() {
  671. return (B) this;
  672. }
  673. }