Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BaseRepositoryBuilder.java 21KB

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