Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

RepoCommand.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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.FileOutputStream;
  46. import java.io.IOException;
  47. import java.net.URI;
  48. import java.net.URISyntaxException;
  49. import java.nio.channels.FileChannel;
  50. import java.text.MessageFormat;
  51. import java.util.ArrayList;
  52. import java.util.Arrays;
  53. import java.util.HashMap;
  54. import java.util.HashSet;
  55. import java.util.List;
  56. import java.util.Map;
  57. import java.util.Set;
  58. import org.eclipse.jgit.api.AddCommand;
  59. import org.eclipse.jgit.api.GitCommand;
  60. import org.eclipse.jgit.api.SubmoduleAddCommand;
  61. import org.eclipse.jgit.api.errors.GitAPIException;
  62. import org.eclipse.jgit.gitrepo.internal.RepoText;
  63. import org.eclipse.jgit.internal.JGitText;
  64. import org.eclipse.jgit.lib.ProgressMonitor;
  65. import org.eclipse.jgit.lib.Repository;
  66. import org.xml.sax.Attributes;
  67. import org.xml.sax.InputSource;
  68. import org.xml.sax.SAXException;
  69. import org.xml.sax.XMLReader;
  70. import org.xml.sax.helpers.DefaultHandler;
  71. import org.xml.sax.helpers.XMLReaderFactory;
  72. /**
  73. * A class used to execute a repo command.
  74. *
  75. * This will parse a repo XML manifest, convert it into .gitmodules file and the
  76. * repository config file.
  77. *
  78. * @see <a href="https://code.google.com/p/git-repo/">git-repo project page</a>
  79. * @since 3.4
  80. */
  81. public class RepoCommand extends GitCommand<Void> {
  82. private String path;
  83. private String uri;
  84. private String groups;
  85. private ProgressMonitor monitor;
  86. private static class CopyFile {
  87. final String src;
  88. final String dest;
  89. final String relativeDest;
  90. CopyFile(Repository repo, String path, String src, String dest) {
  91. this.src = repo.getWorkTree() + "/" + path + "/" + src; //$NON-NLS-1$ //$NON-NLS-2$
  92. this.relativeDest = dest;
  93. this.dest = repo.getWorkTree() + "/" + dest; //$NON-NLS-1$
  94. }
  95. void copy() throws IOException {
  96. FileInputStream input = new FileInputStream(src);
  97. try {
  98. FileOutputStream output = new FileOutputStream(dest);
  99. try {
  100. FileChannel channel = input.getChannel();
  101. output.getChannel().transferFrom(channel, 0, channel.size());
  102. } finally {
  103. output.close();
  104. }
  105. } finally {
  106. input.close();
  107. }
  108. }
  109. }
  110. private static class Project {
  111. final String name;
  112. final String path;
  113. final Set<String> groups;
  114. final List<CopyFile> copyfiles;
  115. Project(String name, String path, String groups) {
  116. this.name = name;
  117. this.path = path;
  118. this.groups = new HashSet<String>();
  119. if (groups != null && groups.length() > 0)
  120. this.groups.addAll(Arrays.asList(groups.split(","))); //$NON-NLS-1$
  121. copyfiles = new ArrayList<CopyFile>();
  122. }
  123. void addCopyFile(CopyFile copyfile) {
  124. copyfiles.add(copyfile);
  125. }
  126. }
  127. private static class XmlManifest extends DefaultHandler {
  128. private final RepoCommand command;
  129. private final String filename;
  130. private final String baseUrl;
  131. private final Map<String, String> remotes;
  132. private final List<Project> projects;
  133. private final Set<String> plusGroups;
  134. private final Set<String> minusGroups;
  135. private String defaultRemote;
  136. private Project currentProject;
  137. XmlManifest(RepoCommand command, String filename, String baseUrl, String groups) {
  138. this.command = command;
  139. this.filename = filename;
  140. this.baseUrl = baseUrl;
  141. remotes = new HashMap<String, String>();
  142. projects = new ArrayList<Project>();
  143. plusGroups = new HashSet<String>();
  144. minusGroups = new HashSet<String>();
  145. if (groups == null || groups.length() == 0 || groups.equals("default")) { //$NON-NLS-1$
  146. // default means "all,-notdefault"
  147. minusGroups.add("notdefault"); //$NON-NLS-1$
  148. } else {
  149. for (String group : groups.split(",")) { //$NON-NLS-1$
  150. if (group.startsWith("-")) //$NON-NLS-1$
  151. minusGroups.add(group.substring(1));
  152. else
  153. plusGroups.add(group);
  154. }
  155. }
  156. }
  157. void read() throws IOException {
  158. final XMLReader xr;
  159. try {
  160. xr = XMLReaderFactory.createXMLReader();
  161. } catch (SAXException e) {
  162. throw new IOException(JGitText.get().noXMLParserAvailable);
  163. }
  164. xr.setContentHandler(this);
  165. final FileInputStream in = new FileInputStream(filename);
  166. try {
  167. xr.parse(new InputSource(in));
  168. } catch (SAXException e) {
  169. IOException error = new IOException(MessageFormat.format(
  170. RepoText.get().errorParsingManifestFile, filename));
  171. error.initCause(e);
  172. throw error;
  173. } finally {
  174. in.close();
  175. }
  176. }
  177. @Override
  178. public void startElement(
  179. String uri,
  180. String localName,
  181. String qName,
  182. Attributes attributes) throws SAXException {
  183. if ("project".equals(qName)) { //$NON-NLS-1$
  184. currentProject = new Project( //$NON-NLS-1$
  185. attributes.getValue("name"), //$NON-NLS-1$
  186. attributes.getValue("path"), //$NON-NLS-1$
  187. attributes.getValue("groups")); //$NON-NLS-1$
  188. } else if ("remote".equals(qName)) { //$NON-NLS-1$
  189. remotes.put(attributes.getValue("name"), //$NON-NLS-1$
  190. attributes.getValue("fetch")); //$NON-NLS-1$
  191. } else if ("default".equals(qName)) { //$NON-NLS-1$
  192. defaultRemote = attributes.getValue("remote"); //$NON-NLS-1$
  193. } else if ("copyfile".equals(qName)) { //$NON-NLS-1$
  194. if (currentProject == null)
  195. throw new SAXException(RepoText.get().invalidManifest);
  196. currentProject.addCopyFile(new CopyFile(
  197. command.repo,
  198. currentProject.path,
  199. attributes.getValue("src"), //$NON-NLS-1$
  200. attributes.getValue("dest"))); //$NON-NLS-1$
  201. }
  202. }
  203. @Override
  204. public void endElement(
  205. String uri,
  206. String localName,
  207. String qName) throws SAXException {
  208. if ("project".equals(qName)) { //$NON-NLS-1$
  209. projects.add(currentProject);
  210. currentProject = null;
  211. }
  212. }
  213. @Override
  214. public void endDocument() throws SAXException {
  215. if (defaultRemote == null) {
  216. throw new SAXException(MessageFormat.format(
  217. RepoText.get().errorNoDefault, filename));
  218. }
  219. final String remoteUrl;
  220. try {
  221. URI uri = new URI(String.format("%s/%s/", baseUrl, remotes.get(defaultRemote))); //$NON-NLS-1$
  222. remoteUrl = uri.normalize().toString();
  223. } catch (URISyntaxException e) {
  224. throw new SAXException(e);
  225. }
  226. for (Project proj : projects) {
  227. if (inGroups(proj)) {
  228. String url = remoteUrl + proj.name;
  229. command.addSubmodule(url, proj.path);
  230. for (CopyFile copyfile : proj.copyfiles) {
  231. try {
  232. copyfile.copy();
  233. } catch (IOException e) {
  234. throw new SAXException(
  235. RepoText.get().copyFileFailed, e);
  236. }
  237. AddCommand add = new AddCommand(command.repo)
  238. .addFilepattern(copyfile.relativeDest);
  239. try {
  240. add.call();
  241. } catch (GitAPIException e) {
  242. throw new SAXException(e);
  243. }
  244. }
  245. }
  246. }
  247. }
  248. boolean inGroups(Project proj) {
  249. for (String group : minusGroups) {
  250. if (proj.groups.contains(group)) {
  251. // minus groups have highest priority.
  252. return false;
  253. }
  254. }
  255. if (plusGroups.isEmpty() || plusGroups.contains("all")) { //$NON-NLS-1$
  256. // empty plus groups means "all"
  257. return true;
  258. }
  259. for (String group : plusGroups) {
  260. if (proj.groups.contains(group))
  261. return true;
  262. }
  263. return false;
  264. }
  265. }
  266. private static class ManifestErrorException extends GitAPIException {
  267. ManifestErrorException(Throwable cause) {
  268. super(RepoText.get().invalidManifest, cause);
  269. }
  270. }
  271. /**
  272. * @param repo
  273. */
  274. public RepoCommand(final Repository repo) {
  275. super(repo);
  276. }
  277. /**
  278. * Set path to the manifest XML file
  279. *
  280. * @param path
  281. * (with <code>/</code> as separator)
  282. * @return this command
  283. */
  284. public RepoCommand setPath(final String path) {
  285. this.path = path;
  286. return this;
  287. }
  288. /**
  289. * Set base URI of the pathes inside the XML
  290. *
  291. * @param uri
  292. * @return this command
  293. */
  294. public RepoCommand setURI(final String uri) {
  295. this.uri = uri;
  296. return this;
  297. }
  298. /**
  299. * Set groups to sync
  300. *
  301. * @param groups groups separated by comma, examples: default|all|G1,-G2,-G3
  302. * @return this command
  303. */
  304. public RepoCommand setGroups(final String groups) {
  305. this.groups = groups;
  306. return this;
  307. }
  308. /**
  309. * The progress monitor associated with the clone operation. By default,
  310. * this is set to <code>NullProgressMonitor</code>
  311. *
  312. * @see org.eclipse.jgit.lib.NullProgressMonitor
  313. * @param monitor
  314. * @return this command
  315. */
  316. public RepoCommand setProgressMonitor(final ProgressMonitor monitor) {
  317. this.monitor = monitor;
  318. return this;
  319. }
  320. @Override
  321. public Void call() throws GitAPIException {
  322. checkCallable();
  323. if (path == null || path.length() == 0)
  324. throw new IllegalArgumentException(JGitText.get().pathNotConfigured);
  325. if (uri == null || uri.length() == 0)
  326. throw new IllegalArgumentException(JGitText.get().uriNotConfigured);
  327. XmlManifest manifest = new XmlManifest(this, path, uri, groups);
  328. try {
  329. manifest.read();
  330. } catch (IOException e) {
  331. throw new ManifestErrorException(e);
  332. }
  333. return null;
  334. }
  335. private void addSubmodule(String url, String name) throws SAXException {
  336. SubmoduleAddCommand add = new SubmoduleAddCommand(repo)
  337. .setPath(name)
  338. .setURI(url);
  339. if (monitor != null)
  340. add.setProgressMonitor(monitor);
  341. try {
  342. add.call();
  343. } catch (GitAPIException e) {
  344. throw new SAXException(e);
  345. }
  346. }
  347. }