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.

SubmoduleInitCommand.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2011, GitHub Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.api;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.Collection;
  14. import java.util.List;
  15. import org.eclipse.jgit.api.errors.GitAPIException;
  16. import org.eclipse.jgit.api.errors.JGitInternalException;
  17. import org.eclipse.jgit.errors.ConfigInvalidException;
  18. import org.eclipse.jgit.lib.ConfigConstants;
  19. import org.eclipse.jgit.lib.Repository;
  20. import org.eclipse.jgit.lib.StoredConfig;
  21. import org.eclipse.jgit.submodule.SubmoduleWalk;
  22. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  23. /**
  24. * A class used to execute a submodule init command.
  25. *
  26. * This will copy the 'url' and 'update' fields from the working tree
  27. * .gitmodules file to a repository's config file for each submodule not
  28. * currently present in the repository's config file.
  29. *
  30. * @see <a href=
  31. * "http://www.kernel.org/pub/software/scm/git/docs/git-submodule.html"
  32. * >Git documentation about submodules</a>
  33. */
  34. public class SubmoduleInitCommand extends GitCommand<Collection<String>> {
  35. private final Collection<String> paths;
  36. /**
  37. * Constructor for SubmoduleInitCommand.
  38. *
  39. * @param repo
  40. * a {@link org.eclipse.jgit.lib.Repository} object.
  41. */
  42. public SubmoduleInitCommand(Repository repo) {
  43. super(repo);
  44. paths = new ArrayList<>();
  45. }
  46. /**
  47. * Add repository-relative submodule path to initialize
  48. *
  49. * @param path
  50. * (with <code>/</code> as separator)
  51. * @return this command
  52. */
  53. public SubmoduleInitCommand addPath(String path) {
  54. paths.add(path);
  55. return this;
  56. }
  57. /** {@inheritDoc} */
  58. @Override
  59. public Collection<String> call() throws GitAPIException {
  60. checkCallable();
  61. try (SubmoduleWalk generator = SubmoduleWalk.forIndex(repo)) {
  62. if (!paths.isEmpty())
  63. generator.setFilter(PathFilterGroup.createFromStrings(paths));
  64. StoredConfig config = repo.getConfig();
  65. List<String> initialized = new ArrayList<>();
  66. while (generator.next()) {
  67. // Ignore entry if URL is already present in config file
  68. if (generator.getConfigUrl() != null)
  69. continue;
  70. String path = generator.getPath();
  71. String name = generator.getModuleName();
  72. // Copy 'url' and 'update' fields from .gitmodules to config
  73. // file
  74. String url = generator.getRemoteUrl();
  75. String update = generator.getModulesUpdate();
  76. if (url != null)
  77. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  78. name, ConfigConstants.CONFIG_KEY_URL, url);
  79. if (update != null)
  80. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  81. name, ConfigConstants.CONFIG_KEY_UPDATE, update);
  82. if (url != null || update != null)
  83. initialized.add(path);
  84. }
  85. // Save repository config if any values were updated
  86. if (!initialized.isEmpty())
  87. config.save();
  88. return initialized;
  89. } catch (IOException | ConfigInvalidException e) {
  90. throw new JGitInternalException(e.getMessage(), e);
  91. }
  92. }
  93. }