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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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 {
  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.release();
  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.release();
  147. throw e;
  148. }
  149. generator.release();
  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.release();
  177. throw e;
  178. }
  179. generator.release();
  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.getRef(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. TreeWalk configWalk = new TreeWalk(repository);
  392. try {
  393. configWalk.addTree(rootTree);
  394. // The root tree may be part of the submodule walk, so we need to revert
  395. // it after this walk.
  396. int idx;
  397. for (idx = 0; !rootTree.first(); idx++) {
  398. rootTree.back(1);
  399. }
  400. try {
  401. configWalk.setRecursive(false);
  402. PathFilter filter = PathFilter.create(Constants.DOT_GIT_MODULES);
  403. configWalk.setFilter(filter);
  404. while (configWalk.next()) {
  405. if (filter.isDone(configWalk)) {
  406. modulesConfig = new BlobBasedConfig(null, repository,
  407. configWalk.getObjectId(0));
  408. return this;
  409. }
  410. }
  411. modulesConfig = new Config();
  412. } finally {
  413. if (idx > 0)
  414. rootTree.next(idx);
  415. }
  416. } finally {
  417. configWalk.release();
  418. }
  419. }
  420. return this;
  421. }
  422. /**
  423. * Checks whether the working tree (or the index in case of a bare repo)
  424. * contains a .gitmodules file. That's a hint that the repo contains
  425. * submodules.
  426. *
  427. * @param repository
  428. * the repository to check
  429. * @return <code>true</code> if the repo contains a .gitmodules file
  430. * @throws IOException
  431. * @throws CorruptObjectException
  432. * @since 3.6
  433. */
  434. public static boolean containsGitModulesFile(Repository repository)
  435. throws IOException {
  436. if (repository.isBare()) {
  437. DirCache dc = repository.readDirCache();
  438. return (dc.findEntry(Constants.DOT_GIT_MODULES) >= 0);
  439. }
  440. File modulesFile = new File(repository.getWorkTree(),
  441. Constants.DOT_GIT_MODULES);
  442. return (modulesFile.exists());
  443. }
  444. private void lazyLoadModulesConfig() throws IOException, ConfigInvalidException {
  445. if (modulesConfig == null)
  446. loadModulesConfig();
  447. }
  448. /**
  449. * Set tree filter
  450. *
  451. * @param filter
  452. * @return this generator
  453. */
  454. public SubmoduleWalk setFilter(TreeFilter filter) {
  455. walk.setFilter(filter);
  456. return this;
  457. }
  458. /**
  459. * Set the tree iterator used for finding submodule entries
  460. *
  461. * @param iterator
  462. * @return this generator
  463. * @throws CorruptObjectException
  464. */
  465. public SubmoduleWalk setTree(final AbstractTreeIterator iterator)
  466. throws CorruptObjectException {
  467. walk.addTree(iterator);
  468. return this;
  469. }
  470. /**
  471. * Set the tree used for finding submodule entries
  472. *
  473. * @param treeId
  474. * @return this generator
  475. * @throws IOException
  476. * @throws IncorrectObjectTypeException
  477. * @throws MissingObjectException
  478. */
  479. public SubmoduleWalk setTree(final AnyObjectId treeId) throws IOException {
  480. walk.addTree(treeId);
  481. return this;
  482. }
  483. /**
  484. * Reset generator and start new submodule walk
  485. *
  486. * @return this generator
  487. */
  488. public SubmoduleWalk reset() {
  489. repoConfig = repository.getConfig();
  490. modulesConfig = null;
  491. walk.reset();
  492. return this;
  493. }
  494. /**
  495. * Get directory that will be the root of the submodule's local repository
  496. *
  497. * @return submodule repository directory
  498. */
  499. public File getDirectory() {
  500. return getSubmoduleDirectory(repository, path);
  501. }
  502. /**
  503. * Advance to next submodule in the index tree.
  504. *
  505. * The object id and path of the next entry can be obtained by calling
  506. * {@link #getObjectId()} and {@link #getPath()}.
  507. *
  508. * @return true if entry found, false otherwise
  509. * @throws IOException
  510. */
  511. public boolean next() throws IOException {
  512. while (walk.next()) {
  513. if (FileMode.GITLINK != walk.getFileMode(0))
  514. continue;
  515. path = walk.getPathString();
  516. return true;
  517. }
  518. path = null;
  519. return false;
  520. }
  521. /**
  522. * Get path of current submodule entry
  523. *
  524. * @return path
  525. */
  526. public String getPath() {
  527. return path;
  528. }
  529. /**
  530. * Get object id of current submodule entry
  531. *
  532. * @return object id
  533. */
  534. public ObjectId getObjectId() {
  535. return walk.getObjectId(0);
  536. }
  537. /**
  538. * Get the configured path for current entry. This will be the value from
  539. * the .gitmodules file in the current repository's working tree.
  540. *
  541. * @return configured path
  542. * @throws ConfigInvalidException
  543. * @throws IOException
  544. */
  545. public String getModulesPath() throws IOException, ConfigInvalidException {
  546. lazyLoadModulesConfig();
  547. return modulesConfig.getString(
  548. ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  549. ConfigConstants.CONFIG_KEY_PATH);
  550. }
  551. /**
  552. * Get the configured remote URL for current entry. This will be the value
  553. * from the repository's config.
  554. *
  555. * @return configured URL
  556. * @throws ConfigInvalidException
  557. * @throws IOException
  558. */
  559. public String getConfigUrl() throws IOException, ConfigInvalidException {
  560. return repoConfig.getString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  561. path, ConfigConstants.CONFIG_KEY_URL);
  562. }
  563. /**
  564. * Get the configured remote URL for current entry. This will be the value
  565. * from the .gitmodules file in the current repository's working tree.
  566. *
  567. * @return configured URL
  568. * @throws ConfigInvalidException
  569. * @throws IOException
  570. */
  571. public String getModulesUrl() throws IOException, ConfigInvalidException {
  572. lazyLoadModulesConfig();
  573. return modulesConfig.getString(
  574. ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  575. ConfigConstants.CONFIG_KEY_URL);
  576. }
  577. /**
  578. * Get the configured update field for current entry. This will be the value
  579. * from the repository's config.
  580. *
  581. * @return update value
  582. * @throws ConfigInvalidException
  583. * @throws IOException
  584. */
  585. public String getConfigUpdate() throws IOException, ConfigInvalidException {
  586. return repoConfig.getString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  587. path, ConfigConstants.CONFIG_KEY_UPDATE);
  588. }
  589. /**
  590. * Get the configured update field for current entry. This will be the value
  591. * from the .gitmodules file in the current repository's working tree.
  592. *
  593. * @return update value
  594. * @throws ConfigInvalidException
  595. * @throws IOException
  596. */
  597. public String getModulesUpdate() throws IOException, ConfigInvalidException {
  598. lazyLoadModulesConfig();
  599. return modulesConfig.getString(
  600. ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  601. ConfigConstants.CONFIG_KEY_UPDATE);
  602. }
  603. /**
  604. * Get the configured ignore field for the current entry. This will be the
  605. * value from the .gitmodules file in the current repository's working tree.
  606. *
  607. * @return ignore value
  608. * @throws ConfigInvalidException
  609. * @throws IOException
  610. * @since 3.6
  611. */
  612. public IgnoreSubmoduleMode getModulesIgnore() throws IOException,
  613. ConfigInvalidException {
  614. lazyLoadModulesConfig();
  615. String name = modulesConfig.getString(
  616. ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  617. ConfigConstants.CONFIG_KEY_IGNORE);
  618. if (name == null)
  619. return null;
  620. return IgnoreSubmoduleMode.valueOf(name.trim().toUpperCase());
  621. }
  622. /**
  623. * Get repository for current submodule entry
  624. *
  625. * @return repository or null if non-existent
  626. * @throws IOException
  627. */
  628. public Repository getRepository() throws IOException {
  629. return getSubmoduleRepository(repository, path);
  630. }
  631. /**
  632. * Get commit id that HEAD points to in the current submodule's repository
  633. *
  634. * @return object id of HEAD reference
  635. * @throws IOException
  636. */
  637. public ObjectId getHead() throws IOException {
  638. Repository subRepo = getRepository();
  639. if (subRepo == null)
  640. return null;
  641. try {
  642. return subRepo.resolve(Constants.HEAD);
  643. } finally {
  644. subRepo.close();
  645. }
  646. }
  647. /**
  648. * Get ref that HEAD points to in the current submodule's repository
  649. *
  650. * @return ref name, null on failures
  651. * @throws IOException
  652. */
  653. public String getHeadRef() throws IOException {
  654. Repository subRepo = getRepository();
  655. if (subRepo == null)
  656. return null;
  657. try {
  658. Ref head = subRepo.getRef(Constants.HEAD);
  659. return head != null ? head.getLeaf().getName() : null;
  660. } finally {
  661. subRepo.close();
  662. }
  663. }
  664. /**
  665. * Get the resolved remote URL for the current submodule.
  666. * <p>
  667. * This method resolves the value of {@link #getModulesUrl()} to an absolute
  668. * URL
  669. *
  670. * @return resolved remote URL
  671. * @throws IOException
  672. * @throws ConfigInvalidException
  673. */
  674. public String getRemoteUrl() throws IOException, ConfigInvalidException {
  675. String url = getModulesUrl();
  676. return url != null ? getSubmoduleRemoteUrl(repository, url) : null;
  677. }
  678. /** Release any resources used by this walker's reader. */
  679. public void release() {
  680. walk.release();
  681. }
  682. }