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.

ManifestParser.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Copyright (C) 2015, 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.io.InputStream;
  47. import java.net.URI;
  48. import java.net.URISyntaxException;
  49. import java.text.MessageFormat;
  50. import java.util.ArrayList;
  51. import java.util.Collections;
  52. import java.util.HashMap;
  53. import java.util.HashSet;
  54. import java.util.Iterator;
  55. import java.util.List;
  56. import java.util.Map;
  57. import java.util.Set;
  58. import org.eclipse.jgit.api.errors.GitAPIException;
  59. import org.eclipse.jgit.gitrepo.RepoProject.CopyFile;
  60. import org.eclipse.jgit.gitrepo.internal.RepoText;
  61. import org.eclipse.jgit.internal.JGitText;
  62. import org.eclipse.jgit.lib.Repository;
  63. import org.xml.sax.Attributes;
  64. import org.xml.sax.InputSource;
  65. import org.xml.sax.SAXException;
  66. import org.xml.sax.XMLReader;
  67. import org.xml.sax.helpers.DefaultHandler;
  68. import org.xml.sax.helpers.XMLReaderFactory;
  69. /**
  70. * Repo XML manifest parser.
  71. *
  72. * @see <a href="https://code.google.com/p/git-repo/">git-repo project page</a>
  73. * @since 4.0
  74. */
  75. public class ManifestParser extends DefaultHandler {
  76. private final String filename;
  77. private final String baseUrl;
  78. private final String defaultBranch;
  79. private final Repository rootRepo;
  80. private final Map<String, Remote> remotes;
  81. private final Set<String> plusGroups;
  82. private final Set<String> minusGroups;
  83. private final List<RepoProject> projects;
  84. private final List<RepoProject> filteredProjects;
  85. private final IncludedFileReader includedReader;
  86. private String defaultRemote;
  87. private String defaultRevision;
  88. private int xmlInRead;
  89. private RepoProject currentProject;
  90. /**
  91. * A callback to read included xml files.
  92. */
  93. public interface IncludedFileReader {
  94. /**
  95. * Read a file from the same base dir of the manifest xml file.
  96. *
  97. * @param path
  98. * The relative path to the file to read
  99. * @return the {@code InputStream} of the file.
  100. * @throws GitAPIException
  101. * @throws IOException
  102. */
  103. public InputStream readIncludeFile(String path)
  104. throws GitAPIException, IOException;
  105. }
  106. /**
  107. * @param includedReader
  108. * @param filename
  109. * @param defaultBranch
  110. * @param baseUrl
  111. * @param groups
  112. * @param rootRepo
  113. */
  114. public ManifestParser(IncludedFileReader includedReader, String filename,
  115. String defaultBranch, String baseUrl, String groups,
  116. Repository rootRepo) {
  117. this.includedReader = includedReader;
  118. this.filename = filename;
  119. this.defaultBranch = defaultBranch;
  120. this.rootRepo = rootRepo;
  121. // Strip trailing /s to match repo behavior.
  122. int lastIndex = baseUrl.length() - 1;
  123. while (lastIndex >= 0 && baseUrl.charAt(lastIndex) == '/')
  124. lastIndex--;
  125. this.baseUrl = baseUrl.substring(0, lastIndex + 1);
  126. plusGroups = new HashSet<String>();
  127. minusGroups = new HashSet<String>();
  128. if (groups == null || groups.length() == 0
  129. || groups.equals("default")) { //$NON-NLS-1$
  130. // default means "all,-notdefault"
  131. minusGroups.add("notdefault"); //$NON-NLS-1$
  132. } else {
  133. for (String group : groups.split(",")) { //$NON-NLS-1$
  134. if (group.startsWith("-")) //$NON-NLS-1$
  135. minusGroups.add(group.substring(1));
  136. else
  137. plusGroups.add(group);
  138. }
  139. }
  140. remotes = new HashMap<String, Remote>();
  141. projects = new ArrayList<RepoProject>();
  142. filteredProjects = new ArrayList<RepoProject>();
  143. }
  144. /**
  145. * Read the xml file.
  146. *
  147. * @param inputStream
  148. * @throws IOException
  149. */
  150. public void read(InputStream inputStream) throws IOException {
  151. xmlInRead++;
  152. final XMLReader xr;
  153. try {
  154. xr = XMLReaderFactory.createXMLReader();
  155. } catch (SAXException e) {
  156. throw new IOException(JGitText.get().noXMLParserAvailable);
  157. }
  158. xr.setContentHandler(this);
  159. try {
  160. xr.parse(new InputSource(inputStream));
  161. } catch (SAXException e) {
  162. IOException error = new IOException(
  163. RepoText.get().errorParsingManifestFile);
  164. error.initCause(e);
  165. throw error;
  166. }
  167. }
  168. @Override
  169. public void startElement(
  170. String uri,
  171. String localName,
  172. String qName,
  173. Attributes attributes) throws SAXException {
  174. if ("project".equals(qName)) { //$NON-NLS-1$
  175. if (attributes.getValue("name") == null) { //$NON-NLS-1$
  176. throw new SAXException(RepoText.get().invalidManifest);
  177. }
  178. currentProject = new RepoProject(
  179. attributes.getValue("name"), //$NON-NLS-1$
  180. attributes.getValue("path"), //$NON-NLS-1$
  181. attributes.getValue("revision"), //$NON-NLS-1$
  182. attributes.getValue("remote"), //$NON-NLS-1$
  183. attributes.getValue("groups")); //$NON-NLS-1$
  184. currentProject.setRecommendShallow(
  185. attributes.getValue("clone-depth")); //$NON-NLS-1$
  186. } else if ("remote".equals(qName)) { //$NON-NLS-1$
  187. String alias = attributes.getValue("alias"); //$NON-NLS-1$
  188. String fetch = attributes.getValue("fetch"); //$NON-NLS-1$
  189. String revision = attributes.getValue("revision"); //$NON-NLS-1$
  190. Remote remote = new Remote(fetch, revision);
  191. remotes.put(attributes.getValue("name"), remote); //$NON-NLS-1$
  192. if (alias != null)
  193. remotes.put(alias, remote);
  194. } else if ("default".equals(qName)) { //$NON-NLS-1$
  195. defaultRemote = attributes.getValue("remote"); //$NON-NLS-1$
  196. defaultRevision = attributes.getValue("revision"); //$NON-NLS-1$
  197. } else if ("copyfile".equals(qName)) { //$NON-NLS-1$
  198. if (currentProject == null)
  199. throw new SAXException(RepoText.get().invalidManifest);
  200. currentProject.addCopyFile(new CopyFile(
  201. rootRepo,
  202. currentProject.getPath(),
  203. attributes.getValue("src"), //$NON-NLS-1$
  204. attributes.getValue("dest"))); //$NON-NLS-1$
  205. } else if ("include".equals(qName)) { //$NON-NLS-1$
  206. String name = attributes.getValue("name"); //$NON-NLS-1$
  207. InputStream is = null;
  208. if (includedReader != null) {
  209. try {
  210. is = includedReader.readIncludeFile(name);
  211. } catch (Exception e) {
  212. throw new SAXException(MessageFormat.format(
  213. RepoText.get().errorIncludeFile, name), e);
  214. }
  215. } else if (filename != null) {
  216. int index = filename.lastIndexOf('/');
  217. String path = filename.substring(0, index + 1) + name;
  218. try {
  219. is = new FileInputStream(path);
  220. } catch (IOException e) {
  221. throw new SAXException(MessageFormat.format(
  222. RepoText.get().errorIncludeFile, path), e);
  223. }
  224. }
  225. if (is == null) {
  226. throw new SAXException(
  227. RepoText.get().errorIncludeNotImplemented);
  228. }
  229. try {
  230. read(is);
  231. } catch (IOException e) {
  232. throw new SAXException(e);
  233. }
  234. }
  235. }
  236. @Override
  237. public void endElement(
  238. String uri,
  239. String localName,
  240. String qName) throws SAXException {
  241. if ("project".equals(qName)) { //$NON-NLS-1$
  242. projects.add(currentProject);
  243. currentProject = null;
  244. }
  245. }
  246. @Override
  247. public void endDocument() throws SAXException {
  248. xmlInRead--;
  249. if (xmlInRead != 0)
  250. return;
  251. // Only do the following after we finished reading everything.
  252. Map<String, String> remoteUrls = new HashMap<String, String>();
  253. URI baseUri;
  254. try {
  255. baseUri = new URI(baseUrl);
  256. } catch (URISyntaxException e) {
  257. throw new SAXException(e);
  258. }
  259. if (defaultRevision == null && defaultRemote != null) {
  260. Remote remote = remotes.get(defaultRemote);
  261. if (remote != null) {
  262. defaultRevision = remote.revision;
  263. }
  264. if (defaultRevision == null) {
  265. defaultRevision = defaultBranch;
  266. }
  267. }
  268. for (RepoProject proj : projects) {
  269. String remote = proj.getRemote();
  270. String revision = defaultRevision;
  271. if (remote == null) {
  272. if (defaultRemote == null) {
  273. if (filename != null)
  274. throw new SAXException(MessageFormat.format(
  275. RepoText.get().errorNoDefaultFilename,
  276. filename));
  277. else
  278. throw new SAXException(
  279. RepoText.get().errorNoDefault);
  280. }
  281. remote = defaultRemote;
  282. } else {
  283. Remote r = remotes.get(remote);
  284. if (r != null && r.revision != null) {
  285. revision = r.revision;
  286. }
  287. }
  288. String remoteUrl = remoteUrls.get(remote);
  289. if (remoteUrl == null) {
  290. remoteUrl =
  291. baseUri.resolve(remotes.get(remote).fetch).toString();
  292. if (!remoteUrl.endsWith("/")) //$NON-NLS-1$
  293. remoteUrl = remoteUrl + "/"; //$NON-NLS-1$
  294. remoteUrls.put(remote, remoteUrl);
  295. }
  296. proj.setUrl(remoteUrl + proj.getName())
  297. .setDefaultRevision(revision);
  298. }
  299. filteredProjects.addAll(projects);
  300. removeNotInGroup();
  301. removeOverlaps();
  302. }
  303. /**
  304. * Getter for projects.
  305. *
  306. * @return projects list reference, never null
  307. */
  308. public List<RepoProject> getProjects() {
  309. return projects;
  310. }
  311. /**
  312. * Getter for filterdProjects.
  313. *
  314. * @return filtered projects list reference, never null
  315. */
  316. public List<RepoProject> getFilteredProjects() {
  317. return filteredProjects;
  318. }
  319. /** Remove projects that are not in our desired groups. */
  320. void removeNotInGroup() {
  321. Iterator<RepoProject> iter = filteredProjects.iterator();
  322. while (iter.hasNext())
  323. if (!inGroups(iter.next()))
  324. iter.remove();
  325. }
  326. /** Remove projects that sits in a subdirectory of any other project. */
  327. void removeOverlaps() {
  328. Collections.sort(filteredProjects);
  329. Iterator<RepoProject> iter = filteredProjects.iterator();
  330. if (!iter.hasNext())
  331. return;
  332. RepoProject last = iter.next();
  333. while (iter.hasNext()) {
  334. RepoProject p = iter.next();
  335. if (last.isAncestorOf(p))
  336. iter.remove();
  337. else
  338. last = p;
  339. }
  340. removeNestedCopyfiles();
  341. }
  342. /** Remove copyfiles that sit in a subdirectory of any other project. */
  343. void removeNestedCopyfiles() {
  344. for (RepoProject proj : filteredProjects) {
  345. List<CopyFile> copyfiles = new ArrayList<>(proj.getCopyFiles());
  346. proj.clearCopyFiles();
  347. for (CopyFile copyfile : copyfiles) {
  348. if (!isNestedCopyfile(copyfile)) {
  349. proj.addCopyFile(copyfile);
  350. }
  351. }
  352. }
  353. }
  354. boolean inGroups(RepoProject proj) {
  355. for (String group : minusGroups) {
  356. if (proj.inGroup(group)) {
  357. // minus groups have highest priority.
  358. return false;
  359. }
  360. }
  361. if (plusGroups.isEmpty() || plusGroups.contains("all")) { //$NON-NLS-1$
  362. // empty plus groups means "all"
  363. return true;
  364. }
  365. for (String group : plusGroups) {
  366. if (proj.inGroup(group))
  367. return true;
  368. }
  369. return false;
  370. }
  371. private boolean isNestedCopyfile(CopyFile copyfile) {
  372. if (copyfile.dest.indexOf('/') == -1) {
  373. // If the copyfile is at root level then it won't be nested.
  374. return false;
  375. }
  376. for (RepoProject proj : filteredProjects) {
  377. if (proj.getPath().compareTo(copyfile.dest) > 0) {
  378. // Early return as remaining projects can't be ancestor of this
  379. // copyfile config (filteredProjects is sorted).
  380. return false;
  381. }
  382. if (proj.isAncestorOf(copyfile.dest)) {
  383. return true;
  384. }
  385. }
  386. return false;
  387. }
  388. private static class Remote {
  389. final String fetch;
  390. final String revision;
  391. Remote(String fetch, String revision) {
  392. this.fetch = fetch;
  393. this.revision = revision;
  394. }
  395. }
  396. }