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

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