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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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.lib.RepositoryCache.FileKey;
  64. import org.eclipse.jgit.storage.file.FileBasedConfig;
  65. import org.eclipse.jgit.storage.file.FileRepository;
  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 FS fs;
  98. private File gitDir;
  99. private File objectDirectory;
  100. private List<File> alternateObjectDirectories;
  101. private File indexFile;
  102. private File workTree;
  103. /** Directories limiting the search for a Git repository. */
  104. private List<File> ceilingDirectories;
  105. /** True only if the caller wants to force bare behavior. */
  106. private boolean bare;
  107. /** True if the caller requires the repository to exist. */
  108. private boolean mustExist;
  109. /** Configuration file of target repository, lazily loaded if required. */
  110. private Config config;
  111. /**
  112. * Set the file system abstraction needed by this repository.
  113. *
  114. * @param fs
  115. * the abstraction.
  116. * @return {@code this} (for chaining calls).
  117. */
  118. public B setFS(FS fs) {
  119. this.fs = fs;
  120. return self();
  121. }
  122. /** @return the file system abstraction, or null if not set. */
  123. public FS getFS() {
  124. return fs;
  125. }
  126. /**
  127. * Set the Git directory storing the repository metadata.
  128. * <p>
  129. * The meta directory stores the objects, references, and meta files like
  130. * {@code MERGE_HEAD}, or the index file. If {@code null} the path is
  131. * assumed to be {@code workTree/.git}.
  132. *
  133. * @param gitDir
  134. * {@code GIT_DIR}, the repository meta directory.
  135. * @return {@code this} (for chaining calls).
  136. */
  137. public B setGitDir(File gitDir) {
  138. this.gitDir = gitDir;
  139. this.config = null;
  140. return self();
  141. }
  142. /** @return the meta data directory; null if not set. */
  143. public File getGitDir() {
  144. return gitDir;
  145. }
  146. /**
  147. * Set the directory storing the repository's objects.
  148. *
  149. * @param objectDirectory
  150. * {@code GIT_OBJECT_DIRECTORY}, the directory where the
  151. * repository's object files are stored.
  152. * @return {@code this} (for chaining calls).
  153. */
  154. public B setObjectDirectory(File objectDirectory) {
  155. this.objectDirectory = objectDirectory;
  156. return self();
  157. }
  158. /** @return the object directory; null if not set. */
  159. public File getObjectDirectory() {
  160. return objectDirectory;
  161. }
  162. /**
  163. * Add an alternate object directory to the search list.
  164. * <p>
  165. * This setting handles one alternate directory at a time, and is provided
  166. * to support {@code GIT_ALTERNATE_OBJECT_DIRECTORIES}.
  167. *
  168. * @param other
  169. * another objects directory to search after the standard one.
  170. * @return {@code this} (for chaining calls).
  171. */
  172. public B addAlternateObjectDirectory(File other) {
  173. if (other != null) {
  174. if (alternateObjectDirectories == null)
  175. alternateObjectDirectories = new LinkedList<File>();
  176. alternateObjectDirectories.add(other);
  177. }
  178. return self();
  179. }
  180. /**
  181. * Add alternate object directories to the search list.
  182. * <p>
  183. * This setting handles several alternate directories at once, and is
  184. * provided to support {@code GIT_ALTERNATE_OBJECT_DIRECTORIES}.
  185. *
  186. * @param inList
  187. * other object directories to search after the standard one. The
  188. * collection's contents is copied to an internal list.
  189. * @return {@code this} (for chaining calls).
  190. */
  191. public B addAlternateObjectDirectories(Collection<File> inList) {
  192. if (inList != null) {
  193. for (File path : inList)
  194. addAlternateObjectDirectory(path);
  195. }
  196. return self();
  197. }
  198. /**
  199. * Add alternate object directories to the search list.
  200. * <p>
  201. * This setting handles several alternate directories at once, and is
  202. * provided to support {@code GIT_ALTERNATE_OBJECT_DIRECTORIES}.
  203. *
  204. * @param inList
  205. * other object directories to search after the standard one. The
  206. * array's contents is copied to an internal list.
  207. * @return {@code this} (for chaining calls).
  208. */
  209. public B addAlternateObjectDirectories(File[] inList) {
  210. if (inList != null) {
  211. for (File path : inList)
  212. addAlternateObjectDirectory(path);
  213. }
  214. return self();
  215. }
  216. /** @return ordered array of alternate directories; null if non were set. */
  217. public File[] getAlternateObjectDirectories() {
  218. final List<File> alts = alternateObjectDirectories;
  219. if (alts == null)
  220. return null;
  221. return alts.toArray(new File[alts.size()]);
  222. }
  223. /**
  224. * Force the repository to be treated as bare (have no working directory).
  225. * <p>
  226. * If bare the working directory aspects of the repository won't be
  227. * configured, and will not be accessible.
  228. *
  229. * @return {@code this} (for chaining calls).
  230. */
  231. public B setBare() {
  232. setIndexFile(null);
  233. setWorkTree(null);
  234. bare = true;
  235. return self();
  236. }
  237. /** @return true if this repository was forced bare by {@link #setBare()}. */
  238. public boolean isBare() {
  239. return bare;
  240. }
  241. /**
  242. * Require the repository to exist before it can be opened.
  243. *
  244. * @param mustExist
  245. * true if it must exist; false if it can be missing and created
  246. * after being built.
  247. * @return {@code this} (for chaining calls).
  248. */
  249. public B setMustExist(boolean mustExist) {
  250. this.mustExist = mustExist;
  251. return self();
  252. }
  253. /** @return true if the repository must exist before being opened. */
  254. public boolean isMustExist() {
  255. return mustExist;
  256. }
  257. /**
  258. * Set the top level directory of the working files.
  259. *
  260. * @param workTree
  261. * {@code GIT_WORK_TREE}, the working directory of the checkout.
  262. * @return {@code this} (for chaining calls).
  263. */
  264. public B setWorkTree(File workTree) {
  265. this.workTree = workTree;
  266. return self();
  267. }
  268. /** @return the work tree directory, or null if not set. */
  269. public File getWorkTree() {
  270. return workTree;
  271. }
  272. /**
  273. * Set the local index file that is caching checked out file status.
  274. * <p>
  275. * The location of the index file tracking the status information for each
  276. * checked out file in {@code workTree}. This may be null to assume the
  277. * default {@code gitDiir/index}.
  278. *
  279. * @param indexFile
  280. * {@code GIT_INDEX_FILE}, the index file location.
  281. * @return {@code this} (for chaining calls).
  282. */
  283. public B setIndexFile(File indexFile) {
  284. this.indexFile = indexFile;
  285. return self();
  286. }
  287. /** @return the index file location, or null if not set. */
  288. public File getIndexFile() {
  289. return indexFile;
  290. }
  291. /**
  292. * Read standard Git environment variables and configure from those.
  293. * <p>
  294. * This method tries to read the standard Git environment variables, such as
  295. * {@code GIT_DIR} and {@code GIT_WORK_TREE} to configure this builder
  296. * instance. If an environment variable is set, it overrides the value
  297. * already set in this builder.
  298. *
  299. * @return {@code this} (for chaining calls).
  300. */
  301. public B readEnvironment() {
  302. return readEnvironment(SystemReader.getInstance());
  303. }
  304. /**
  305. * Read standard Git environment variables and configure from those.
  306. * <p>
  307. * This method tries to read the standard Git environment variables, such as
  308. * {@code GIT_DIR} and {@code GIT_WORK_TREE} to configure this builder
  309. * instance. If a property is already set in the builder, the environment
  310. * variable is not used.
  311. *
  312. * @param sr
  313. * the SystemReader abstraction to access the environment.
  314. * @return {@code this} (for chaining calls).
  315. */
  316. public B readEnvironment(SystemReader sr) {
  317. if (getGitDir() == null) {
  318. String val = sr.getenv(GIT_DIR_KEY);
  319. if (val != null)
  320. setGitDir(new File(val));
  321. }
  322. if (getObjectDirectory() == null) {
  323. String val = sr.getenv(GIT_OBJECT_DIRECTORY_KEY);
  324. if (val != null)
  325. setObjectDirectory(new File(val));
  326. }
  327. if (getAlternateObjectDirectories() == null) {
  328. String val = sr.getenv(GIT_ALTERNATE_OBJECT_DIRECTORIES_KEY);
  329. if (val != null) {
  330. for (String path : val.split(File.pathSeparator))
  331. addAlternateObjectDirectory(new File(path));
  332. }
  333. }
  334. if (getWorkTree() == null) {
  335. String val = sr.getenv(GIT_WORK_TREE_KEY);
  336. if (val != null)
  337. setWorkTree(new File(val));
  338. }
  339. if (getIndexFile() == null) {
  340. String val = sr.getenv(GIT_INDEX_FILE_KEY);
  341. if (val != null)
  342. setIndexFile(new File(val));
  343. }
  344. if (ceilingDirectories == null) {
  345. String val = sr.getenv(GIT_CEILING_DIRECTORIES_KEY);
  346. if (val != null) {
  347. for (String path : val.split(File.pathSeparator))
  348. addCeilingDirectory(new File(path));
  349. }
  350. }
  351. return self();
  352. }
  353. /**
  354. * Add a ceiling directory to the search limit list.
  355. * <p>
  356. * This setting handles one ceiling directory at a time, and is provided to
  357. * support {@code GIT_CEILING_DIRECTORIES}.
  358. *
  359. * @param root
  360. * a path to stop searching at; its parent will not be searched.
  361. * @return {@code this} (for chaining calls).
  362. */
  363. public B addCeilingDirectory(File root) {
  364. if (root != null) {
  365. if (ceilingDirectories == null)
  366. ceilingDirectories = new LinkedList<File>();
  367. ceilingDirectories.add(root);
  368. }
  369. return self();
  370. }
  371. /**
  372. * Add ceiling directories to the search list.
  373. * <p>
  374. * This setting handles several ceiling directories at once, and is provided
  375. * to support {@code GIT_CEILING_DIRECTORIES}.
  376. *
  377. * @param inList
  378. * directory paths to stop searching at. The collection's
  379. * contents is copied to an internal list.
  380. * @return {@code this} (for chaining calls).
  381. */
  382. public B addCeilingDirectories(Collection<File> inList) {
  383. if (inList != null) {
  384. for (File path : inList)
  385. addCeilingDirectory(path);
  386. }
  387. return self();
  388. }
  389. /**
  390. * Add ceiling directories to the search list.
  391. * <p>
  392. * This setting handles several ceiling directories at once, and is provided
  393. * to support {@code GIT_CEILING_DIRECTORIES}.
  394. *
  395. * @param inList
  396. * directory paths to stop searching at. The array's contents is
  397. * copied to an internal list.
  398. * @return {@code this} (for chaining calls).
  399. */
  400. public B addCeilingDirectories(File[] inList) {
  401. if (inList != null) {
  402. for (File path : inList)
  403. addCeilingDirectory(path);
  404. }
  405. return self();
  406. }
  407. /**
  408. * Configure {@code GIT_DIR} by searching up the file system.
  409. * <p>
  410. * Starts from the current working directory of the JVM and scans up through
  411. * the directory tree until a Git repository is found. Success can be
  412. * determined by checking for {@code getGitDir() != null}.
  413. * <p>
  414. * The search can be limited to specific spaces of the local filesystem by
  415. * {@link #addCeilingDirectory(File)}, or inheriting the list through a
  416. * prior call to {@link #readEnvironment()}.
  417. *
  418. * @return {@code this} (for chaining calls).
  419. */
  420. public B findGitDir() {
  421. if (getGitDir() == null)
  422. findGitDir(new File("").getAbsoluteFile());
  423. return self();
  424. }
  425. /**
  426. * Configure {@code GIT_DIR} by searching up the file system.
  427. * <p>
  428. * Starts from the supplied directory path and scans up through the parent
  429. * directory tree until a Git repository is found. Success can be determined
  430. * by checking for {@code getGitDir() != null}.
  431. * <p>
  432. * The search can be limited to specific spaces of the local filesystem by
  433. * {@link #addCeilingDirectory(File)}, or inheriting the list through a
  434. * prior call to {@link #readEnvironment()}.
  435. *
  436. * @param current
  437. * directory to begin searching in.
  438. * @return {@code this} (for chaining calls).
  439. */
  440. public B findGitDir(File current) {
  441. if (getGitDir() == null) {
  442. FS tryFS = safeFS();
  443. while (current != null) {
  444. File dir = new File(current, DOT_GIT);
  445. if (FileKey.isGitRepository(dir, tryFS)) {
  446. setGitDir(dir);
  447. break;
  448. }
  449. current = current.getParentFile();
  450. if (current != null && ceilingDirectories != null
  451. && ceilingDirectories.contains(current))
  452. break;
  453. }
  454. }
  455. return self();
  456. }
  457. /**
  458. * Guess and populate all parameters not already defined.
  459. * <p>
  460. * If an option was not set, the setup method will try to default the option
  461. * based on other options. If insufficient information is available, an
  462. * exception is thrown to the caller.
  463. *
  464. * @return {@code this}
  465. * @throws IllegalArgumentException
  466. * insufficient parameters were set, or some parameters are
  467. * incompatible with one another.
  468. * @throws IOException
  469. * the repository could not be accessed to configure the rest of
  470. * the builder's parameters.
  471. */
  472. public B setup() throws IllegalArgumentException, IOException {
  473. requireGitDirOrWorkTree();
  474. setupGitDir();
  475. setupWorkTree();
  476. setupInternals();
  477. return self();
  478. }
  479. /**
  480. * Create a repository matching the configuration in this builder.
  481. * <p>
  482. * If an option was not set, the build method will try to default the option
  483. * based on other options. If insufficient information is available, an
  484. * exception is thrown to the caller.
  485. *
  486. * @return a repository matching this configuration.
  487. * @throws IllegalArgumentException
  488. * insufficient parameters were set.
  489. * @throws IOException
  490. * the repository could not be accessed to configure the rest of
  491. * the builder's parameters.
  492. */
  493. @SuppressWarnings("unchecked")
  494. public R build() throws IOException {
  495. R repo = (R) new FileRepository(setup());
  496. if (isMustExist() && !repo.getObjectDatabase().exists())
  497. throw new RepositoryNotFoundException(getGitDir());
  498. return repo;
  499. }
  500. /** Require either {@code gitDir} or {@code workTree} to be set. */
  501. protected void requireGitDirOrWorkTree() {
  502. if (getGitDir() == null && getWorkTree() == null)
  503. throw new IllegalArgumentException(
  504. JGitText.get().eitherGitDirOrWorkTreeRequired);
  505. }
  506. /**
  507. * Perform standard gitDir initialization.
  508. *
  509. * @throws IOException
  510. * the repository could not be accessed
  511. */
  512. protected void setupGitDir() throws IOException {
  513. // No gitDir? Try to assume its under the workTree or a ref to another
  514. // location
  515. if (getGitDir() == null && getWorkTree() != null) {
  516. File dotGit = new File(getWorkTree(), DOT_GIT);
  517. if (!dotGit.isFile())
  518. setGitDir(dotGit);
  519. else {
  520. byte[] content = IO.readFully(dotGit);
  521. if (!isSymRef(content))
  522. throw new IOException(MessageFormat.format(
  523. JGitText.get().invalidGitdirRef,
  524. dotGit.getAbsolutePath()));
  525. int pathStart = 8;
  526. int lineEnd = RawParseUtils.nextLF(content, pathStart);
  527. if (content[lineEnd - 1] == '\n')
  528. lineEnd--;
  529. if (lineEnd == pathStart)
  530. throw new IOException(MessageFormat.format(
  531. JGitText.get().invalidGitdirRef,
  532. dotGit.getAbsolutePath()));
  533. String gitdirPath = RawParseUtils.decode(content, pathStart,
  534. lineEnd);
  535. File gitdirFile = new File(gitdirPath);
  536. if (gitdirFile.isAbsolute())
  537. setGitDir(gitdirFile);
  538. else
  539. setGitDir(new File(getWorkTree(), gitdirPath)
  540. .getCanonicalFile());
  541. }
  542. }
  543. }
  544. /**
  545. * Perform standard work-tree initialization.
  546. * <p>
  547. * This is a method typically invoked inside of {@link #setup()}, near the
  548. * end after the repository has been identified and its configuration is
  549. * available for inspection.
  550. *
  551. * @throws IOException
  552. * the repository configuration could not be read.
  553. */
  554. protected void setupWorkTree() throws IOException {
  555. if (getFS() == null)
  556. setFS(FS.DETECTED);
  557. // If we aren't bare, we should have a work tree.
  558. //
  559. if (!isBare() && getWorkTree() == null)
  560. setWorkTree(guessWorkTreeOrFail());
  561. if (!isBare()) {
  562. // If after guessing we're still not bare, we must have
  563. // a metadata directory to hold the repository. Assume
  564. // its at the work tree.
  565. //
  566. if (getGitDir() == null)
  567. setGitDir(getWorkTree().getParentFile());
  568. if (getIndexFile() == null)
  569. setIndexFile(new File(getGitDir(), "index"));
  570. }
  571. }
  572. /**
  573. * Configure the internal implementation details of the repository.
  574. *
  575. * @throws IOException
  576. * the repository could not be accessed
  577. */
  578. protected void setupInternals() throws IOException {
  579. if (getObjectDirectory() == null && getGitDir() != null)
  580. setObjectDirectory(safeFS().resolve(getGitDir(), "objects"));
  581. }
  582. /**
  583. * Get the cached repository configuration, loading if not yet available.
  584. *
  585. * @return the configuration of the repository.
  586. * @throws IOException
  587. * the configuration is not available, or is badly formed.
  588. */
  589. protected Config getConfig() throws IOException {
  590. if (config == null)
  591. config = loadConfig();
  592. return config;
  593. }
  594. /**
  595. * Parse and load the repository specific configuration.
  596. * <p>
  597. * The default implementation reads {@code gitDir/config}, or returns an
  598. * empty configuration if gitDir was not set.
  599. *
  600. * @return the repository's configuration.
  601. * @throws IOException
  602. * the configuration is not available.
  603. */
  604. protected Config loadConfig() throws IOException {
  605. if (getGitDir() != null) {
  606. // We only want the repository's configuration file, and not
  607. // the user file, as these parameters must be unique to this
  608. // repository and not inherited from other files.
  609. //
  610. File path = safeFS().resolve(getGitDir(), Constants.CONFIG);
  611. FileBasedConfig cfg = new FileBasedConfig(path, safeFS());
  612. try {
  613. cfg.load();
  614. } catch (ConfigInvalidException err) {
  615. throw new IllegalArgumentException(MessageFormat.format(
  616. JGitText.get().repositoryConfigFileInvalid, path
  617. .getAbsolutePath(), err.getMessage()));
  618. }
  619. return cfg;
  620. } else {
  621. return new Config();
  622. }
  623. }
  624. private File guessWorkTreeOrFail() throws IOException {
  625. final Config cfg = getConfig();
  626. // If set, core.worktree wins.
  627. //
  628. String path = cfg.getString(CONFIG_CORE_SECTION, null,
  629. CONFIG_KEY_WORKTREE);
  630. if (path != null)
  631. return safeFS().resolve(getGitDir(), path);
  632. // If core.bare is set, honor its value. Assume workTree is
  633. // the parent directory of the repository.
  634. //
  635. if (cfg.getString(CONFIG_CORE_SECTION, null, CONFIG_KEY_BARE) != null) {
  636. if (cfg.getBoolean(CONFIG_CORE_SECTION, CONFIG_KEY_BARE, true)) {
  637. setBare();
  638. return null;
  639. }
  640. return getGitDir().getParentFile();
  641. }
  642. if (getGitDir().getName().equals(DOT_GIT)) {
  643. // No value for the "bare" flag, but gitDir is named ".git",
  644. // use the parent of the directory
  645. //
  646. return getGitDir().getParentFile();
  647. }
  648. // We have to assume we are bare.
  649. //
  650. setBare();
  651. return null;
  652. }
  653. /** @return the configured FS, or {@link FS#DETECTED}. */
  654. protected FS safeFS() {
  655. return getFS() != null ? getFS() : FS.DETECTED;
  656. }
  657. /** @return {@code this} */
  658. @SuppressWarnings("unchecked")
  659. protected final B self() {
  660. return (B) this;
  661. }
  662. }