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.

FileRepository.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2008-2010, Google Inc.
  4. * Copyright (C) 2006-2010, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.internal.storage.file;
  47. import static org.eclipse.jgit.lib.RefDatabase.ALL;
  48. import java.io.File;
  49. import java.io.FileInputStream;
  50. import java.io.FileNotFoundException;
  51. import java.io.IOException;
  52. import java.text.MessageFormat;
  53. import java.text.ParseException;
  54. import java.util.HashSet;
  55. import java.util.Locale;
  56. import java.util.Objects;
  57. import java.util.Set;
  58. import org.eclipse.jgit.annotations.Nullable;
  59. import org.eclipse.jgit.api.errors.JGitInternalException;
  60. import org.eclipse.jgit.attributes.AttributesNode;
  61. import org.eclipse.jgit.attributes.AttributesNodeProvider;
  62. import org.eclipse.jgit.errors.ConfigInvalidException;
  63. import org.eclipse.jgit.events.ConfigChangedEvent;
  64. import org.eclipse.jgit.events.ConfigChangedListener;
  65. import org.eclipse.jgit.events.IndexChangedEvent;
  66. import org.eclipse.jgit.internal.JGitText;
  67. import org.eclipse.jgit.internal.storage.file.ObjectDirectory.AlternateHandle;
  68. import org.eclipse.jgit.internal.storage.file.ObjectDirectory.AlternateRepository;
  69. import org.eclipse.jgit.internal.storage.reftree.RefTreeDatabase;
  70. import org.eclipse.jgit.lib.BaseRepositoryBuilder;
  71. import org.eclipse.jgit.lib.ConfigConstants;
  72. import org.eclipse.jgit.lib.Constants;
  73. import org.eclipse.jgit.lib.CoreConfig.HideDotFiles;
  74. import org.eclipse.jgit.lib.CoreConfig.SymLinks;
  75. import org.eclipse.jgit.lib.ObjectId;
  76. import org.eclipse.jgit.lib.ProgressMonitor;
  77. import org.eclipse.jgit.lib.Ref;
  78. import org.eclipse.jgit.lib.RefDatabase;
  79. import org.eclipse.jgit.lib.RefUpdate;
  80. import org.eclipse.jgit.lib.ReflogReader;
  81. import org.eclipse.jgit.lib.Repository;
  82. import org.eclipse.jgit.storage.file.FileBasedConfig;
  83. import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
  84. import org.eclipse.jgit.storage.pack.PackConfig;
  85. import org.eclipse.jgit.util.FS;
  86. import org.eclipse.jgit.util.FileUtils;
  87. import org.eclipse.jgit.util.IO;
  88. import org.eclipse.jgit.util.RawParseUtils;
  89. import org.eclipse.jgit.util.StringUtils;
  90. import org.eclipse.jgit.util.SystemReader;
  91. /**
  92. * Represents a Git repository. A repository holds all objects and refs used for
  93. * managing source code (could by any type of file, but source code is what
  94. * SCM's are typically used for).
  95. *
  96. * In Git terms all data is stored in GIT_DIR, typically a directory called
  97. * .git. A work tree is maintained unless the repository is a bare repository.
  98. * Typically the .git directory is located at the root of the work dir.
  99. *
  100. * <ul>
  101. * <li>GIT_DIR
  102. * <ul>
  103. * <li>objects/ - objects</li>
  104. * <li>refs/ - tags and heads</li>
  105. * <li>config - configuration</li>
  106. * <li>info/ - more configurations</li>
  107. * </ul>
  108. * </li>
  109. * </ul>
  110. * <p>
  111. * This class is thread-safe.
  112. * <p>
  113. * This implementation only handles a subtly undocumented subset of git features.
  114. *
  115. */
  116. public class FileRepository extends Repository {
  117. private static final String UNNAMED = "Unnamed repository; edit this file to name it for gitweb."; //$NON-NLS-1$
  118. private final FileBasedConfig systemConfig;
  119. private final FileBasedConfig userConfig;
  120. private final FileBasedConfig repoConfig;
  121. private final RefDatabase refs;
  122. private final ObjectDirectory objectDatabase;
  123. private FileSnapshot snapshot;
  124. /**
  125. * Construct a representation of a Git repository.
  126. * <p>
  127. * The work tree, object directory, alternate object directories and index
  128. * file locations are deduced from the given git directory and the default
  129. * rules by running {@link FileRepositoryBuilder}. This constructor is the
  130. * same as saying:
  131. *
  132. * <pre>
  133. * new FileRepositoryBuilder().setGitDir(gitDir).build()
  134. * </pre>
  135. *
  136. * @param gitDir
  137. * GIT_DIR (the location of the repository metadata).
  138. * @throws IOException
  139. * the repository appears to already exist but cannot be
  140. * accessed.
  141. * @see FileRepositoryBuilder
  142. */
  143. public FileRepository(final File gitDir) throws IOException {
  144. this(new FileRepositoryBuilder().setGitDir(gitDir).setup());
  145. }
  146. /**
  147. * A convenience API for {@link #FileRepository(File)}.
  148. *
  149. * @param gitDir
  150. * GIT_DIR (the location of the repository metadata).
  151. * @throws IOException
  152. * the repository appears to already exist but cannot be
  153. * accessed.
  154. * @see FileRepositoryBuilder
  155. */
  156. public FileRepository(final String gitDir) throws IOException {
  157. this(new File(gitDir));
  158. }
  159. /**
  160. * Create a repository using the local file system.
  161. *
  162. * @param options
  163. * description of the repository's important paths.
  164. * @throws IOException
  165. * the user configuration file or repository configuration file
  166. * cannot be accessed.
  167. */
  168. public FileRepository(final BaseRepositoryBuilder options) throws IOException {
  169. super(options);
  170. if (StringUtils.isEmptyOrNull(SystemReader.getInstance().getenv(
  171. Constants.GIT_CONFIG_NOSYSTEM_KEY)))
  172. systemConfig = SystemReader.getInstance().openSystemConfig(null,
  173. getFS());
  174. else
  175. systemConfig = new FileBasedConfig(null, FS.DETECTED) {
  176. @Override
  177. public void load() {
  178. // empty, do not load
  179. }
  180. @Override
  181. public boolean isOutdated() {
  182. // regular class would bomb here
  183. return false;
  184. }
  185. };
  186. userConfig = SystemReader.getInstance().openUserConfig(systemConfig,
  187. getFS());
  188. repoConfig = new FileBasedConfig(userConfig, getFS().resolve(
  189. getDirectory(), Constants.CONFIG),
  190. getFS());
  191. loadSystemConfig();
  192. loadUserConfig();
  193. loadRepoConfig();
  194. repoConfig.addChangeListener(new ConfigChangedListener() {
  195. @Override
  196. public void onConfigChanged(ConfigChangedEvent event) {
  197. fireEvent(event);
  198. }
  199. });
  200. final long repositoryFormatVersion = getConfig().getLong(
  201. ConfigConstants.CONFIG_CORE_SECTION, null,
  202. ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 0);
  203. String reftype = repoConfig.getString(
  204. "extensions", null, "refsStorage"); //$NON-NLS-1$ //$NON-NLS-2$
  205. if (repositoryFormatVersion >= 1 && reftype != null) {
  206. if (StringUtils.equalsIgnoreCase(reftype, "reftree")) { //$NON-NLS-1$
  207. refs = new RefTreeDatabase(this, new RefDirectory(this));
  208. } else {
  209. throw new IOException(JGitText.get().unknownRepositoryFormat);
  210. }
  211. } else {
  212. refs = new RefDirectory(this);
  213. }
  214. objectDatabase = new ObjectDirectory(repoConfig, //
  215. options.getObjectDirectory(), //
  216. options.getAlternateObjectDirectories(), //
  217. getFS(), //
  218. new File(getDirectory(), Constants.SHALLOW));
  219. if (objectDatabase.exists()) {
  220. if (repositoryFormatVersion > 1)
  221. throw new IOException(MessageFormat.format(
  222. JGitText.get().unknownRepositoryFormat2,
  223. Long.valueOf(repositoryFormatVersion)));
  224. }
  225. if (!isBare())
  226. snapshot = FileSnapshot.save(getIndexFile());
  227. }
  228. private void loadSystemConfig() throws IOException {
  229. try {
  230. systemConfig.load();
  231. } catch (ConfigInvalidException e1) {
  232. IOException e2 = new IOException(MessageFormat.format(JGitText
  233. .get().systemConfigFileInvalid, systemConfig.getFile()
  234. .getAbsolutePath(), e1));
  235. e2.initCause(e1);
  236. throw e2;
  237. }
  238. }
  239. private void loadUserConfig() throws IOException {
  240. try {
  241. userConfig.load();
  242. } catch (ConfigInvalidException e1) {
  243. IOException e2 = new IOException(MessageFormat.format(JGitText
  244. .get().userConfigFileInvalid, userConfig.getFile()
  245. .getAbsolutePath(), e1));
  246. e2.initCause(e1);
  247. throw e2;
  248. }
  249. }
  250. private void loadRepoConfig() throws IOException {
  251. try {
  252. repoConfig.load();
  253. } catch (ConfigInvalidException e1) {
  254. IOException e2 = new IOException(JGitText.get().unknownRepositoryFormat);
  255. e2.initCause(e1);
  256. throw e2;
  257. }
  258. }
  259. /**
  260. * Create a new Git repository initializing the necessary files and
  261. * directories.
  262. *
  263. * @param bare
  264. * if true, a bare repository is created.
  265. *
  266. * @throws IOException
  267. * in case of IO problem
  268. */
  269. @Override
  270. public void create(boolean bare) throws IOException {
  271. final FileBasedConfig cfg = getConfig();
  272. if (cfg.getFile().exists()) {
  273. throw new IllegalStateException(MessageFormat.format(
  274. JGitText.get().repositoryAlreadyExists, getDirectory()));
  275. }
  276. FileUtils.mkdirs(getDirectory(), true);
  277. HideDotFiles hideDotFiles = getConfig().getEnum(
  278. ConfigConstants.CONFIG_CORE_SECTION, null,
  279. ConfigConstants.CONFIG_KEY_HIDEDOTFILES,
  280. HideDotFiles.DOTGITONLY);
  281. if (hideDotFiles != HideDotFiles.FALSE && !isBare()
  282. && getDirectory().getName().startsWith(".")) //$NON-NLS-1$
  283. getFS().setHidden(getDirectory(), true);
  284. refs.create();
  285. objectDatabase.create();
  286. FileUtils.mkdir(new File(getDirectory(), "branches")); //$NON-NLS-1$
  287. FileUtils.mkdir(new File(getDirectory(), "hooks")); //$NON-NLS-1$
  288. RefUpdate head = updateRef(Constants.HEAD);
  289. head.disableRefLog();
  290. head.link(Constants.R_HEADS + Constants.MASTER);
  291. final boolean fileMode;
  292. if (getFS().supportsExecute()) {
  293. File tmp = File.createTempFile("try", "execute", getDirectory()); //$NON-NLS-1$ //$NON-NLS-2$
  294. getFS().setExecute(tmp, true);
  295. final boolean on = getFS().canExecute(tmp);
  296. getFS().setExecute(tmp, false);
  297. final boolean off = getFS().canExecute(tmp);
  298. FileUtils.delete(tmp);
  299. fileMode = on && !off;
  300. } else {
  301. fileMode = false;
  302. }
  303. SymLinks symLinks = SymLinks.FALSE;
  304. if (getFS().supportsSymlinks()) {
  305. File tmp = new File(getDirectory(), "tmplink"); //$NON-NLS-1$
  306. try {
  307. getFS().createSymLink(tmp, "target"); //$NON-NLS-1$
  308. symLinks = null;
  309. FileUtils.delete(tmp);
  310. } catch (IOException e) {
  311. // Normally a java.nio.file.FileSystemException
  312. }
  313. }
  314. if (symLinks != null)
  315. cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
  316. ConfigConstants.CONFIG_KEY_SYMLINKS, symLinks.name()
  317. .toLowerCase(Locale.ROOT));
  318. cfg.setInt(ConfigConstants.CONFIG_CORE_SECTION, null,
  319. ConfigConstants.CONFIG_KEY_REPO_FORMAT_VERSION, 0);
  320. cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  321. ConfigConstants.CONFIG_KEY_FILEMODE, fileMode);
  322. if (bare)
  323. cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  324. ConfigConstants.CONFIG_KEY_BARE, true);
  325. cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  326. ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES, !bare);
  327. if (SystemReader.getInstance().isMacOS())
  328. // Java has no other way
  329. cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  330. ConfigConstants.CONFIG_KEY_PRECOMPOSEUNICODE, true);
  331. if (!bare) {
  332. File workTree = getWorkTree();
  333. if (!getDirectory().getParentFile().equals(workTree)) {
  334. cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
  335. ConfigConstants.CONFIG_KEY_WORKTREE, getWorkTree()
  336. .getAbsolutePath());
  337. LockFile dotGitLockFile = new LockFile(new File(workTree,
  338. Constants.DOT_GIT));
  339. try {
  340. if (dotGitLockFile.lock()) {
  341. dotGitLockFile.write(Constants.encode(Constants.GITDIR
  342. + getDirectory().getAbsolutePath()));
  343. dotGitLockFile.commit();
  344. }
  345. } finally {
  346. dotGitLockFile.unlock();
  347. }
  348. }
  349. }
  350. cfg.save();
  351. }
  352. /**
  353. * @return the directory containing the objects owned by this repository.
  354. */
  355. public File getObjectsDirectory() {
  356. return objectDatabase.getDirectory();
  357. }
  358. /** @return the object database storing this repository's data. */
  359. @Override
  360. public ObjectDirectory getObjectDatabase() {
  361. return objectDatabase;
  362. }
  363. /** @return the reference database which stores the reference namespace. */
  364. @Override
  365. public RefDatabase getRefDatabase() {
  366. return refs;
  367. }
  368. /** @return the configuration of this repository. */
  369. @Override
  370. public FileBasedConfig getConfig() {
  371. if (systemConfig.isOutdated()) {
  372. try {
  373. loadSystemConfig();
  374. } catch (IOException e) {
  375. throw new RuntimeException(e);
  376. }
  377. }
  378. if (userConfig.isOutdated()) {
  379. try {
  380. loadUserConfig();
  381. } catch (IOException e) {
  382. throw new RuntimeException(e);
  383. }
  384. }
  385. if (repoConfig.isOutdated()) {
  386. try {
  387. loadRepoConfig();
  388. } catch (IOException e) {
  389. throw new RuntimeException(e);
  390. }
  391. }
  392. return repoConfig;
  393. }
  394. @Override
  395. @Nullable
  396. public String getGitwebDescription() throws IOException {
  397. String d;
  398. try {
  399. d = RawParseUtils.decode(IO.readFully(descriptionFile()));
  400. } catch (FileNotFoundException err) {
  401. return null;
  402. }
  403. if (d != null) {
  404. d = d.trim();
  405. if (d.isEmpty() || UNNAMED.equals(d)) {
  406. return null;
  407. }
  408. }
  409. return d;
  410. }
  411. @Override
  412. public void setGitwebDescription(@Nullable String description)
  413. throws IOException {
  414. String old = getGitwebDescription();
  415. if (Objects.equals(old, description)) {
  416. return;
  417. }
  418. File path = descriptionFile();
  419. LockFile lock = new LockFile(path);
  420. if (!lock.lock()) {
  421. throw new IOException(MessageFormat.format(JGitText.get().lockError,
  422. path.getAbsolutePath()));
  423. }
  424. try {
  425. String d = description;
  426. if (d != null) {
  427. d = d.trim();
  428. if (!d.isEmpty()) {
  429. d += '\n';
  430. }
  431. } else {
  432. d = ""; //$NON-NLS-1$
  433. }
  434. lock.write(Constants.encode(d));
  435. lock.commit();
  436. } finally {
  437. lock.unlock();
  438. }
  439. }
  440. private File descriptionFile() {
  441. return new File(getDirectory(), "description"); //$NON-NLS-1$
  442. }
  443. /**
  444. * Objects known to exist but not expressed by {@link #getAllRefs()}.
  445. * <p>
  446. * When a repository borrows objects from another repository, it can
  447. * advertise that it safely has that other repository's references, without
  448. * exposing any other details about the other repository. This may help
  449. * a client trying to push changes avoid pushing more than it needs to.
  450. *
  451. * @return unmodifiable collection of other known objects.
  452. */
  453. @Override
  454. public Set<ObjectId> getAdditionalHaves() {
  455. return getAdditionalHaves(null);
  456. }
  457. /**
  458. * Objects known to exist but not expressed by {@link #getAllRefs()}.
  459. * <p>
  460. * When a repository borrows objects from another repository, it can
  461. * advertise that it safely has that other repository's references, without
  462. * exposing any other details about the other repository. This may help
  463. * a client trying to push changes avoid pushing more than it needs to.
  464. *
  465. * @param skips
  466. * Set of AlternateHandle Ids already seen
  467. *
  468. * @return unmodifiable collection of other known objects.
  469. */
  470. private Set<ObjectId> getAdditionalHaves(Set<AlternateHandle.Id> skips) {
  471. HashSet<ObjectId> r = new HashSet<>();
  472. skips = objectDatabase.addMe(skips);
  473. for (AlternateHandle d : objectDatabase.myAlternates()) {
  474. if (d instanceof AlternateRepository && !skips.contains(d.getId())) {
  475. FileRepository repo;
  476. repo = ((AlternateRepository) d).repository;
  477. for (Ref ref : repo.getAllRefs().values()) {
  478. if (ref.getObjectId() != null)
  479. r.add(ref.getObjectId());
  480. if (ref.getPeeledObjectId() != null)
  481. r.add(ref.getPeeledObjectId());
  482. }
  483. r.addAll(repo.getAdditionalHaves(skips));
  484. }
  485. }
  486. return r;
  487. }
  488. /**
  489. * Add a single existing pack to the list of available pack files.
  490. *
  491. * @param pack
  492. * path of the pack file to open.
  493. * @throws IOException
  494. * index file could not be opened, read, or is not recognized as
  495. * a Git pack file index.
  496. */
  497. public void openPack(final File pack) throws IOException {
  498. objectDatabase.openPack(pack);
  499. }
  500. @Override
  501. public void scanForRepoChanges() throws IOException {
  502. getRefDatabase().getRefs(ALL); // This will look for changes to refs
  503. detectIndexChanges();
  504. }
  505. /** Detect index changes. */
  506. private void detectIndexChanges() {
  507. if (isBare())
  508. return;
  509. File indexFile = getIndexFile();
  510. if (snapshot == null)
  511. snapshot = FileSnapshot.save(indexFile);
  512. else if (snapshot.isModified(indexFile))
  513. notifyIndexChanged();
  514. }
  515. @Override
  516. public void notifyIndexChanged() {
  517. snapshot = FileSnapshot.save(getIndexFile());
  518. fireEvent(new IndexChangedEvent());
  519. }
  520. /**
  521. * @param refName
  522. * @return a {@link ReflogReader} for the supplied refname, or null if the
  523. * named ref does not exist.
  524. * @throws IOException the ref could not be accessed.
  525. */
  526. @Override
  527. public ReflogReader getReflogReader(String refName) throws IOException {
  528. Ref ref = findRef(refName);
  529. if (ref != null)
  530. return new ReflogReaderImpl(this, ref.getName());
  531. return null;
  532. }
  533. @Override
  534. public AttributesNodeProvider createAttributesNodeProvider() {
  535. return new AttributesNodeProviderImpl(this);
  536. }
  537. /**
  538. * Implementation a {@link AttributesNodeProvider} for a
  539. * {@link FileRepository}.
  540. *
  541. * @author <a href="mailto:arthur.daussy@obeo.fr">Arthur Daussy</a>
  542. *
  543. */
  544. static class AttributesNodeProviderImpl implements
  545. AttributesNodeProvider {
  546. private AttributesNode infoAttributesNode;
  547. private AttributesNode globalAttributesNode;
  548. /**
  549. * Constructor.
  550. *
  551. * @param repo
  552. * {@link Repository} that will provide the attribute nodes.
  553. */
  554. protected AttributesNodeProviderImpl(Repository repo) {
  555. infoAttributesNode = new InfoAttributesNode(repo);
  556. globalAttributesNode = new GlobalAttributesNode(repo);
  557. }
  558. @Override
  559. public AttributesNode getInfoAttributesNode() throws IOException {
  560. if (infoAttributesNode instanceof InfoAttributesNode)
  561. infoAttributesNode = ((InfoAttributesNode) infoAttributesNode)
  562. .load();
  563. return infoAttributesNode;
  564. }
  565. @Override
  566. public AttributesNode getGlobalAttributesNode() throws IOException {
  567. if (globalAttributesNode instanceof GlobalAttributesNode)
  568. globalAttributesNode = ((GlobalAttributesNode) globalAttributesNode)
  569. .load();
  570. return globalAttributesNode;
  571. }
  572. static void loadRulesFromFile(AttributesNode r, File attrs)
  573. throws FileNotFoundException, IOException {
  574. if (attrs.exists()) {
  575. FileInputStream in = new FileInputStream(attrs);
  576. try {
  577. r.parse(in);
  578. } finally {
  579. in.close();
  580. }
  581. }
  582. }
  583. }
  584. private boolean shouldAutoDetach() {
  585. return getConfig().getBoolean(ConfigConstants.CONFIG_GC_SECTION,
  586. ConfigConstants.CONFIG_KEY_AUTODETACH, true);
  587. }
  588. @Override
  589. public void autoGC(ProgressMonitor monitor) {
  590. GC gc = new GC(this);
  591. gc.setPackConfig(new PackConfig(this));
  592. gc.setProgressMonitor(monitor);
  593. gc.setAuto(true);
  594. gc.setBackground(shouldAutoDetach());
  595. try {
  596. gc.gc();
  597. } catch (ParseException | IOException e) {
  598. throw new JGitInternalException(JGitText.get().gcFailed, e);
  599. }
  600. }
  601. }