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.

RepoCommand.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (C) 2014, Google 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.gitrepo;
  44. import java.io.FileInputStream;
  45. import java.io.IOException;
  46. import java.net.URI;
  47. import java.net.URISyntaxException;
  48. import java.text.MessageFormat;
  49. import java.util.ArrayList;
  50. import java.util.HashMap;
  51. import java.util.List;
  52. import java.util.Map;
  53. import org.eclipse.jgit.api.GitCommand;
  54. import org.eclipse.jgit.api.SubmoduleAddCommand;
  55. import org.eclipse.jgit.api.errors.GitAPIException;
  56. import org.eclipse.jgit.gitrepo.internal.RepoText;
  57. import org.eclipse.jgit.internal.JGitText;
  58. import org.eclipse.jgit.lib.ProgressMonitor;
  59. import org.eclipse.jgit.lib.Repository;
  60. import org.xml.sax.Attributes;
  61. import org.xml.sax.InputSource;
  62. import org.xml.sax.SAXException;
  63. import org.xml.sax.XMLReader;
  64. import org.xml.sax.helpers.DefaultHandler;
  65. import org.xml.sax.helpers.XMLReaderFactory;
  66. /**
  67. * A class used to execute a repo command.
  68. *
  69. * This will parse a repo XML manifest, convert it into .gitmodules file and the
  70. * repository config file.
  71. *
  72. * @see <a href="https://code.google.com/p/git-repo/">git-repo project page</a>
  73. * @since 3.4
  74. */
  75. public class RepoCommand extends GitCommand<Void> {
  76. private String path;
  77. private String uri;
  78. private ProgressMonitor monitor;
  79. private static class Project {
  80. final String name;
  81. final String path;
  82. Project(String name, String path) {
  83. this.name = name;
  84. this.path = path;
  85. }
  86. }
  87. private static class XmlManifest extends DefaultHandler {
  88. private final RepoCommand command;
  89. private final String filename;
  90. private final String baseUrl;
  91. private final Map<String, String> remotes;
  92. private final List<Project> projects;
  93. private String defaultRemote;
  94. XmlManifest(RepoCommand command, String filename, String baseUrl) {
  95. this.command = command;
  96. this.filename = filename;
  97. this.baseUrl = baseUrl;
  98. remotes = new HashMap<String, String>();
  99. projects = new ArrayList<Project>();
  100. }
  101. void read() throws IOException {
  102. final XMLReader xr;
  103. try {
  104. xr = XMLReaderFactory.createXMLReader();
  105. } catch (SAXException e) {
  106. throw new IOException(JGitText.get().noXMLParserAvailable);
  107. }
  108. xr.setContentHandler(this);
  109. final FileInputStream in = new FileInputStream(filename);
  110. try {
  111. xr.parse(new InputSource(in));
  112. } catch (SAXException e) {
  113. IOException error = new IOException(MessageFormat.format(
  114. RepoText.get().errorParsingManifestFile, filename));
  115. error.initCause(e);
  116. throw error;
  117. } finally {
  118. in.close();
  119. }
  120. }
  121. @Override
  122. public void startElement(
  123. String uri,
  124. String localName,
  125. String qName,
  126. Attributes attributes) throws SAXException {
  127. if ("project".equals(qName)) //$NON-NLS-1$
  128. projects.add(new Project(attributes.getValue("name"), attributes.getValue("path"))); //$NON-NLS-1$ //$NON-NLS-2$
  129. else if ("remote".equals(qName)) //$NON-NLS-1$
  130. remotes.put(attributes.getValue("name"), attributes.getValue("fetch")); //$NON-NLS-1$ //$NON-NLS-2$
  131. else if ("default".equals(qName)) //$NON-NLS-1$
  132. defaultRemote = attributes.getValue("remote"); //$NON-NLS-1$
  133. else if ("copyfile".equals(qName)) { //$NON-NLS-1$
  134. // TODO(fishywang): Handle copyfile. Do nothing for now.
  135. }
  136. }
  137. @Override
  138. public void endDocument() throws SAXException {
  139. if (defaultRemote == null) {
  140. throw new SAXException(MessageFormat.format(
  141. RepoText.get().errorNoDefault, filename));
  142. }
  143. final String remoteUrl;
  144. try {
  145. URI uri = new URI(String.format("%s/%s/", baseUrl, remotes.get(defaultRemote))); //$NON-NLS-1$
  146. remoteUrl = uri.normalize().toString();
  147. } catch (URISyntaxException e) {
  148. throw new SAXException(e);
  149. }
  150. for (Project proj : projects) {
  151. String url = remoteUrl + proj.name;
  152. command.addSubmodule(url, proj.path);
  153. }
  154. }
  155. }
  156. private static class ManifestErrorException extends GitAPIException {
  157. ManifestErrorException(Throwable cause) {
  158. super(RepoText.get().invalidManifest, cause);
  159. }
  160. }
  161. /**
  162. * @param repo
  163. */
  164. public RepoCommand(final Repository repo) {
  165. super(repo);
  166. }
  167. /**
  168. * Set path to the manifest XML file
  169. *
  170. * @param path
  171. * (with <code>/</code> as separator)
  172. * @return this command
  173. */
  174. public RepoCommand setPath(final String path) {
  175. this.path = path;
  176. return this;
  177. }
  178. /**
  179. * Set base URI of the pathes inside the XML
  180. *
  181. * @param uri
  182. * @return this command
  183. */
  184. public RepoCommand setURI(final String uri) {
  185. this.uri = uri;
  186. return this;
  187. }
  188. /**
  189. * The progress monitor associated with the clone operation. By default,
  190. * this is set to <code>NullProgressMonitor</code>
  191. *
  192. * @see org.eclipse.jgit.lib.NullProgressMonitor
  193. * @param monitor
  194. * @return this command
  195. */
  196. public RepoCommand setProgressMonitor(final ProgressMonitor monitor) {
  197. this.monitor = monitor;
  198. return this;
  199. }
  200. @Override
  201. public Void call() throws GitAPIException {
  202. checkCallable();
  203. if (path == null || path.length() == 0)
  204. throw new IllegalArgumentException(JGitText.get().pathNotConfigured);
  205. if (uri == null || uri.length() == 0)
  206. throw new IllegalArgumentException(JGitText.get().uriNotConfigured);
  207. XmlManifest manifest = new XmlManifest(this, path, uri);
  208. try {
  209. manifest.read();
  210. } catch (IOException e) {
  211. throw new ManifestErrorException(e);
  212. }
  213. return null;
  214. }
  215. private void addSubmodule(String url, String name) throws SAXException {
  216. SubmoduleAddCommand add = new SubmoduleAddCommand(repo);
  217. if (monitor != null)
  218. add.setProgressMonitor(monitor);
  219. try {
  220. add.setPath(name).setURI(url).call();
  221. } catch (GitAPIException e) {
  222. throw new SAXException(e);
  223. }
  224. }
  225. }