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 23KB

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