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.

SubmoduleWalk.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. * Copyright (C) 2011, GitHub 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.submodule;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.text.MessageFormat;
  47. import org.eclipse.jgit.dircache.DirCache;
  48. import org.eclipse.jgit.dircache.DirCacheIterator;
  49. import org.eclipse.jgit.errors.ConfigInvalidException;
  50. import org.eclipse.jgit.errors.CorruptObjectException;
  51. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  52. import org.eclipse.jgit.errors.MissingObjectException;
  53. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  54. import org.eclipse.jgit.internal.JGitText;
  55. import org.eclipse.jgit.lib.AnyObjectId;
  56. import org.eclipse.jgit.lib.BlobBasedConfig;
  57. import org.eclipse.jgit.lib.Config;
  58. import org.eclipse.jgit.lib.ConfigConstants;
  59. import org.eclipse.jgit.lib.Constants;
  60. import org.eclipse.jgit.lib.FileMode;
  61. import org.eclipse.jgit.lib.ObjectId;
  62. import org.eclipse.jgit.lib.Ref;
  63. import org.eclipse.jgit.lib.Repository;
  64. import org.eclipse.jgit.lib.RepositoryBuilder;
  65. import org.eclipse.jgit.lib.StoredConfig;
  66. import org.eclipse.jgit.storage.file.FileBasedConfig;
  67. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  68. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  69. import org.eclipse.jgit.treewalk.TreeWalk;
  70. import org.eclipse.jgit.treewalk.filter.PathFilter;
  71. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  72. import org.eclipse.jgit.util.FS;
  73. /**
  74. * Walker that visits all submodule entries found in a tree
  75. */
  76. public class SubmoduleWalk implements AutoCloseable {
  77. /**
  78. * The values for the config param submodule.<name>.ignore
  79. *
  80. * @since 3.6
  81. */
  82. public enum IgnoreSubmoduleMode {
  83. /**
  84. * Ignore all modifications to submodules
  85. */
  86. ALL,
  87. /**
  88. * Ignore changes to the working tree of a submodule
  89. */
  90. DIRTY,
  91. /**
  92. * Ignore changes to untracked files in the working tree of a submodule
  93. */
  94. UNTRACKED,
  95. /**
  96. * Ignore nothing. That's the default
  97. */
  98. NONE;
  99. }
  100. /**
  101. * Create a generator to walk over the submodule entries currently in the
  102. * index
  103. *
  104. * The {@code .gitmodules} file is read from the index.
  105. *
  106. * @param repository
  107. * @return generator over submodule index entries
  108. * @throws IOException
  109. */
  110. public static SubmoduleWalk forIndex(Repository repository)
  111. throws IOException {
  112. SubmoduleWalk generator = new SubmoduleWalk(repository);
  113. try {
  114. DirCache index = repository.readDirCache();
  115. generator.setTree(new DirCacheIterator(index));
  116. } catch (IOException e) {
  117. generator.close();
  118. throw e;
  119. }
  120. return generator;
  121. }
  122. /**
  123. * Create a generator and advance it to the submodule entry at the given
  124. * path
  125. *
  126. * @param repository
  127. * @param treeId
  128. * the root of a tree containing both a submodule at the given path
  129. * and .gitmodules at the root.
  130. * @param path
  131. * @return generator at given path, null if no submodule at given path
  132. * @throws IOException
  133. */
  134. public static SubmoduleWalk forPath(Repository repository,
  135. AnyObjectId treeId, String path) throws IOException {
  136. SubmoduleWalk generator = new SubmoduleWalk(repository);
  137. try {
  138. generator.setTree(treeId);
  139. PathFilter filter = PathFilter.create(path);
  140. generator.setFilter(filter);
  141. generator.setRootTree(treeId);
  142. while (generator.next())
  143. if (filter.isDone(generator.walk))
  144. return generator;
  145. } catch (IOException e) {
  146. generator.close();
  147. throw e;
  148. }
  149. generator.close();
  150. return null;
  151. }
  152. /**
  153. * Create a generator and advance it to the submodule entry at the given
  154. * path
  155. *
  156. * @param repository
  157. * @param iterator
  158. * the root of a tree containing both a submodule at the given path
  159. * and .gitmodules at the root.
  160. * @param path
  161. * @return generator at given path, null if no submodule at given path
  162. * @throws IOException
  163. */
  164. public static SubmoduleWalk forPath(Repository repository,
  165. AbstractTreeIterator iterator, String path) throws IOException {
  166. SubmoduleWalk generator = new SubmoduleWalk(repository);
  167. try {
  168. generator.setTree(iterator);
  169. PathFilter filter = PathFilter.create(path);
  170. generator.setFilter(filter);
  171. generator.setRootTree(iterator);
  172. while (generator.next())
  173. if (filter.isDone(generator.walk))
  174. return generator;
  175. } catch (IOException e) {
  176. generator.close();
  177. throw e;
  178. }
  179. generator.close();
  180. return null;
  181. }
  182. /**
  183. * Get submodule directory
  184. *
  185. * @param parent
  186. * @param path
  187. * @return directory
  188. */
  189. public static File getSubmoduleDirectory(final Repository parent,
  190. final String path) {
  191. return new File(parent.getWorkTree(), path);
  192. }
  193. /**
  194. * Get submodule repository
  195. *
  196. * @param parent
  197. * @param path
  198. * @return repository or null if repository doesn't exist
  199. * @throws IOException
  200. */
  201. public static Repository getSubmoduleRepository(final Repository parent,
  202. final String path) throws IOException {
  203. return getSubmoduleRepository(parent.getWorkTree(), path);
  204. }
  205. /**
  206. * Get submodule repository at path
  207. *
  208. * @param parent
  209. * @param path
  210. * @return repository or null if repository doesn't exist
  211. * @throws IOException
  212. */
  213. public static Repository getSubmoduleRepository(final File parent,
  214. final String path) throws IOException {
  215. File subWorkTree = new File(parent, path);
  216. if (!subWorkTree.isDirectory())
  217. return null;
  218. File workTree = new File(parent, path);
  219. try {
  220. return new RepositoryBuilder() //
  221. .setMustExist(true) //
  222. .setFS(FS.DETECTED) //
  223. .setWorkTree(workTree) //
  224. .build();
  225. } catch (RepositoryNotFoundException e) {
  226. return null;
  227. }
  228. }
  229. /**
  230. * Resolve submodule repository URL.
  231. * <p>
  232. * This handles relative URLs that are typically specified in the
  233. * '.gitmodules' file by resolving them against the remote URL of the parent
  234. * repository.
  235. * <p>
  236. * Relative URLs will be resolved against the parent repository's working
  237. * directory if the parent repository has no configured remote URL.
  238. *
  239. * @param parent
  240. * parent repository
  241. * @param url
  242. * absolute or relative URL of the submodule repository
  243. * @return resolved URL
  244. * @throws IOException
  245. */
  246. public static String getSubmoduleRemoteUrl(final Repository parent,
  247. final String url) throws IOException {
  248. if (!url.startsWith("./") && !url.startsWith("../")) //$NON-NLS-1$ //$NON-NLS-2$
  249. return url;
  250. String remoteName = null;
  251. // Look up remote URL associated wit HEAD ref
  252. Ref ref = parent.exactRef(Constants.HEAD);
  253. if (ref != null) {
  254. if (ref.isSymbolic())
  255. ref = ref.getLeaf();
  256. remoteName = parent.getConfig().getString(
  257. ConfigConstants.CONFIG_BRANCH_SECTION,
  258. Repository.shortenRefName(ref.getName()),
  259. ConfigConstants.CONFIG_KEY_REMOTE);
  260. }
  261. // Fall back to 'origin' if current HEAD ref has no remote URL
  262. if (remoteName == null)
  263. remoteName = Constants.DEFAULT_REMOTE_NAME;
  264. String remoteUrl = parent.getConfig().getString(
  265. ConfigConstants.CONFIG_REMOTE_SECTION, remoteName,
  266. ConfigConstants.CONFIG_KEY_URL);
  267. // Fall back to parent repository's working directory if no remote URL
  268. if (remoteUrl == null) {
  269. remoteUrl = parent.getWorkTree().getAbsolutePath();
  270. // Normalize slashes to '/'
  271. if ('\\' == File.separatorChar)
  272. remoteUrl = remoteUrl.replace('\\', '/');
  273. }
  274. // Remove trailing '/'
  275. if (remoteUrl.charAt(remoteUrl.length() - 1) == '/')
  276. remoteUrl = remoteUrl.substring(0, remoteUrl.length() - 1);
  277. char separator = '/';
  278. String submoduleUrl = url;
  279. while (submoduleUrl.length() > 0) {
  280. if (submoduleUrl.startsWith("./")) //$NON-NLS-1$
  281. submoduleUrl = submoduleUrl.substring(2);
  282. else if (submoduleUrl.startsWith("../")) { //$NON-NLS-1$
  283. int lastSeparator = remoteUrl.lastIndexOf('/');
  284. if (lastSeparator < 1) {
  285. lastSeparator = remoteUrl.lastIndexOf(':');
  286. separator = ':';
  287. }
  288. if (lastSeparator < 1)
  289. throw new IOException(MessageFormat.format(
  290. JGitText.get().submoduleParentRemoteUrlInvalid,
  291. remoteUrl));
  292. remoteUrl = remoteUrl.substring(0, lastSeparator);
  293. submoduleUrl = submoduleUrl.substring(3);
  294. } else
  295. break;
  296. }
  297. return remoteUrl + separator + submoduleUrl;
  298. }
  299. private final Repository repository;
  300. private final TreeWalk walk;
  301. private StoredConfig repoConfig;
  302. private AbstractTreeIterator rootTree;
  303. private Config modulesConfig;
  304. private String path;
  305. /**
  306. * Create submodule generator
  307. *
  308. * @param repository
  309. * @throws IOException
  310. */
  311. public SubmoduleWalk(final Repository repository) throws IOException {
  312. this.repository = repository;
  313. repoConfig = repository.getConfig();
  314. walk = new TreeWalk(repository);
  315. walk.setRecursive(true);
  316. }
  317. /**
  318. * Set the config used by this walk.
  319. *
  320. * This method need only be called if constructing a walk manually instead of
  321. * with one of the static factory methods above.
  322. *
  323. * @param config
  324. * .gitmodules config object
  325. * @return this generator
  326. */
  327. public SubmoduleWalk setModulesConfig(final Config config) {
  328. modulesConfig = config;
  329. return this;
  330. }
  331. /**
  332. * Set the tree used by this walk for finding {@code .gitmodules}.
  333. * <p>
  334. * The root tree is not read until the first submodule is encountered by the
  335. * walk.
  336. * <p>
  337. * This method need only be called if constructing a walk manually instead of
  338. * with one of the static factory methods above.
  339. *
  340. * @param tree
  341. * tree containing .gitmodules
  342. * @return this generator
  343. */
  344. public SubmoduleWalk setRootTree(final AbstractTreeIterator tree) {
  345. rootTree = tree;
  346. modulesConfig = null;
  347. return this;
  348. }
  349. /**
  350. * Set the tree used by this walk for finding {@code .gitmodules}.
  351. * <p>
  352. * The root tree is not read until the first submodule is encountered by the
  353. * walk.
  354. * <p>
  355. * This method need only be called if constructing a walk manually instead of
  356. * with one of the static factory methods above.
  357. *
  358. * @param id
  359. * ID of a tree containing .gitmodules
  360. * @return this generator
  361. * @throws IOException
  362. */
  363. public SubmoduleWalk setRootTree(final AnyObjectId id) throws IOException {
  364. final CanonicalTreeParser p = new CanonicalTreeParser();
  365. p.reset(walk.getObjectReader(), id);
  366. rootTree = p;
  367. modulesConfig = null;
  368. return this;
  369. }
  370. /**
  371. * Load the config for this walk from {@code .gitmodules}.
  372. * <p>
  373. * Uses the root tree if {@link #setRootTree(AbstractTreeIterator)} was
  374. * previously called, otherwise uses the working tree.
  375. * <p>
  376. * If no submodule config is found, loads an empty config.
  377. *
  378. * @return this generator
  379. * @throws IOException if an error occurred, or if the repository is bare
  380. * @throws ConfigInvalidException
  381. */
  382. public SubmoduleWalk loadModulesConfig() throws IOException, ConfigInvalidException {
  383. if (rootTree == null) {
  384. File modulesFile = new File(repository.getWorkTree(),
  385. Constants.DOT_GIT_MODULES);
  386. FileBasedConfig config = new FileBasedConfig(modulesFile,
  387. repository.getFS());
  388. config.load();
  389. modulesConfig = config;
  390. } else {
  391. try (TreeWalk configWalk = new TreeWalk(repository)) {
  392. configWalk.addTree(rootTree);
  393. // The root tree may be part of the submodule walk, so we need to revert
  394. // it after this walk.
  395. int idx;
  396. for (idx = 0; !rootTree.first(); idx++) {
  397. rootTree.back(1);
  398. }
  399. try {
  400. configWalk.setRecursive(false);
  401. PathFilter filter = PathFilter.create(Constants.DOT_GIT_MODULES);
  402. configWalk.setFilter(filter);
  403. while (configWalk.next()) {
  404. if (filter.isDone(configWalk)) {
  405. modulesConfig = new BlobBasedConfig(null, repository,
  406. configWalk.getObjectId(0));
  407. return this;
  408. }
  409. }
  410. modulesConfig = new Config();
  411. } finally {
  412. if (idx > 0)
  413. rootTree.next(idx);
  414. }
  415. }
  416. }
  417. return this;
  418. }
  419. /**
  420. * Checks whether the working tree contains a .gitmodules file. That's a
  421. * hint that the repo contains submodules.
  422. *
  423. * @param repository
  424. * the repository to check
  425. * @return <code>true</code> if the working tree contains a .gitmodules file,
  426. * <code>false</code> otherwise. Always returns <code>false</code>
  427. * for bare repositories.
  428. * @throws IOException
  429. * @throws CorruptObjectException
  430. * @since 3.6
  431. */
  432. public static boolean containsGitModulesFile(Repository repository)
  433. throws IOException {
  434. if (repository.isBare()) {
  435. return false;
  436. }
  437. File modulesFile = new File(repository.getWorkTree(),
  438. Constants.DOT_GIT_MODULES);
  439. return (modulesFile.exists());
  440. }
  441. private void lazyLoadModulesConfig() throws IOException, ConfigInvalidException {
  442. if (modulesConfig == null)
  443. loadModulesConfig();
  444. }
  445. /**
  446. * Set tree filter
  447. *
  448. * @param filter
  449. * @return this generator
  450. */
  451. public SubmoduleWalk setFilter(TreeFilter filter) {
  452. walk.setFilter(filter);
  453. return this;
  454. }
  455. /**
  456. * Set the tree iterator used for finding submodule entries
  457. *
  458. * @param iterator
  459. * @return this generator
  460. * @throws CorruptObjectException
  461. */
  462. public SubmoduleWalk setTree(final AbstractTreeIterator iterator)
  463. throws CorruptObjectException {
  464. walk.addTree(iterator);
  465. return this;
  466. }
  467. /**
  468. * Set the tree used for finding submodule entries
  469. *
  470. * @param treeId
  471. * @return this generator
  472. * @throws IOException
  473. * @throws IncorrectObjectTypeException
  474. * @throws MissingObjectException
  475. */
  476. public SubmoduleWalk setTree(final AnyObjectId treeId) throws IOException {
  477. walk.addTree(treeId);
  478. return this;
  479. }
  480. /**
  481. * Reset generator and start new submodule walk
  482. *
  483. * @return this generator
  484. */
  485. public SubmoduleWalk reset() {
  486. repoConfig = repository.getConfig();
  487. modulesConfig = null;
  488. walk.reset();
  489. return this;
  490. }
  491. /**
  492. * Get directory that will be the root of the submodule's local repository
  493. *
  494. * @return submodule repository directory
  495. */
  496. public File getDirectory() {
  497. return getSubmoduleDirectory(repository, path);
  498. }
  499. /**
  500. * Advance to next submodule in the index tree.
  501. *
  502. * The object id and path of the next entry can be obtained by calling
  503. * {@link #getObjectId()} and {@link #getPath()}.
  504. *
  505. * @return true if entry found, false otherwise
  506. * @throws IOException
  507. */
  508. public boolean next() throws IOException {
  509. while (walk.next()) {
  510. if (FileMode.GITLINK != walk.getFileMode(0))
  511. continue;
  512. path = walk.getPathString();
  513. return true;
  514. }
  515. path = null;
  516. return false;
  517. }
  518. /**
  519. * Get path of current submodule entry
  520. *
  521. * @return path
  522. */
  523. public String getPath() {
  524. return path;
  525. }
  526. /**
  527. * Get object id of current submodule entry
  528. *
  529. * @return object id
  530. */
  531. public ObjectId getObjectId() {
  532. return walk.getObjectId(0);
  533. }
  534. /**
  535. * Get the configured path for current entry. This will be the value from
  536. * the .gitmodules file in the current repository's working tree.
  537. *
  538. * @return configured path
  539. * @throws ConfigInvalidException
  540. * @throws IOException
  541. */
  542. public String getModulesPath() throws IOException, ConfigInvalidException {
  543. lazyLoadModulesConfig();
  544. return modulesConfig.getString(
  545. ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  546. ConfigConstants.CONFIG_KEY_PATH);
  547. }
  548. /**
  549. * Get the configured remote URL for current entry. This will be the value
  550. * from the repository's config.
  551. *
  552. * @return configured URL
  553. * @throws ConfigInvalidException
  554. * @throws IOException
  555. */
  556. public String getConfigUrl() throws IOException, ConfigInvalidException {
  557. return repoConfig.getString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  558. path, ConfigConstants.CONFIG_KEY_URL);
  559. }
  560. /**
  561. * Get the configured remote URL for current entry. This will be the value
  562. * from the .gitmodules file in the current repository's working tree.
  563. *
  564. * @return configured URL
  565. * @throws ConfigInvalidException
  566. * @throws IOException
  567. */
  568. public String getModulesUrl() throws IOException, ConfigInvalidException {
  569. lazyLoadModulesConfig();
  570. return modulesConfig.getString(
  571. ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  572. ConfigConstants.CONFIG_KEY_URL);
  573. }
  574. /**
  575. * Get the configured update field for current entry. This will be the value
  576. * from the repository's config.
  577. *
  578. * @return update value
  579. * @throws ConfigInvalidException
  580. * @throws IOException
  581. */
  582. public String getConfigUpdate() throws IOException, ConfigInvalidException {
  583. return repoConfig.getString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  584. path, ConfigConstants.CONFIG_KEY_UPDATE);
  585. }
  586. /**
  587. * Get the configured update field for current entry. This will be the value
  588. * from the .gitmodules file in the current repository's working tree.
  589. *
  590. * @return update value
  591. * @throws ConfigInvalidException
  592. * @throws IOException
  593. */
  594. public String getModulesUpdate() throws IOException, ConfigInvalidException {
  595. lazyLoadModulesConfig();
  596. return modulesConfig.getString(
  597. ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  598. ConfigConstants.CONFIG_KEY_UPDATE);
  599. }
  600. /**
  601. * Get the configured ignore field for the current entry. This will be the
  602. * value from the .gitmodules file in the current repository's working tree.
  603. *
  604. * @return ignore value
  605. * @throws ConfigInvalidException
  606. * @throws IOException
  607. * @since 3.6
  608. */
  609. public IgnoreSubmoduleMode getModulesIgnore() throws IOException,
  610. ConfigInvalidException {
  611. lazyLoadModulesConfig();
  612. String name = modulesConfig.getString(
  613. ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  614. ConfigConstants.CONFIG_KEY_IGNORE);
  615. if (name == null)
  616. return null;
  617. return IgnoreSubmoduleMode.valueOf(name.trim().toUpperCase());
  618. }
  619. /**
  620. * Get repository for current submodule entry
  621. *
  622. * @return repository or null if non-existent
  623. * @throws IOException
  624. */
  625. public Repository getRepository() throws IOException {
  626. return getSubmoduleRepository(repository, path);
  627. }
  628. /**
  629. * Get commit id that HEAD points to in the current submodule's repository
  630. *
  631. * @return object id of HEAD reference
  632. * @throws IOException
  633. */
  634. public ObjectId getHead() throws IOException {
  635. Repository subRepo = getRepository();
  636. if (subRepo == null)
  637. return null;
  638. try {
  639. return subRepo.resolve(Constants.HEAD);
  640. } finally {
  641. subRepo.close();
  642. }
  643. }
  644. /**
  645. * Get ref that HEAD points to in the current submodule's repository
  646. *
  647. * @return ref name, null on failures
  648. * @throws IOException
  649. */
  650. public String getHeadRef() throws IOException {
  651. Repository subRepo = getRepository();
  652. if (subRepo == null)
  653. return null;
  654. try {
  655. Ref head = subRepo.exactRef(Constants.HEAD);
  656. return head != null ? head.getLeaf().getName() : null;
  657. } finally {
  658. subRepo.close();
  659. }
  660. }
  661. /**
  662. * Get the resolved remote URL for the current submodule.
  663. * <p>
  664. * This method resolves the value of {@link #getModulesUrl()} to an absolute
  665. * URL
  666. *
  667. * @return resolved remote URL
  668. * @throws IOException
  669. * @throws ConfigInvalidException
  670. */
  671. public String getRemoteUrl() throws IOException, ConfigInvalidException {
  672. String url = getModulesUrl();
  673. return url != null ? getSubmoduleRemoteUrl(repository, url) : null;
  674. }
  675. /**
  676. * Release any resources used by this walker's reader.
  677. *
  678. * @since 4.0
  679. */
  680. @Override
  681. public void close() {
  682. walk.close();
  683. }
  684. }