123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- /*
- * Copyright (C) 2014, Google Inc.
- * and other copyright owners as documented in the project's IP log.
- *
- * This program and the accompanying materials are made available
- * under the terms of the Eclipse Distribution License v1.0 which
- * accompanies this distribution, is reproduced below, and is
- * available at http://www.eclipse.org/org/documents/edl-v10.php
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or
- * without modification, are permitted provided that the following
- * conditions are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided
- * with the distribution.
- *
- * - Neither the name of the Eclipse Foundation, Inc. nor the
- * names of its contributors may be used to endorse or promote
- * products derived from this software without specific prior
- * written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- package org.eclipse.jgit.gitrepo;
-
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.nio.channels.FileChannel;
- import java.text.MessageFormat;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
-
- import org.eclipse.jgit.api.AddCommand;
- import org.eclipse.jgit.api.GitCommand;
- import org.eclipse.jgit.api.SubmoduleAddCommand;
- import org.eclipse.jgit.api.errors.GitAPIException;
- import org.eclipse.jgit.gitrepo.internal.RepoText;
- import org.eclipse.jgit.internal.JGitText;
- import org.eclipse.jgit.lib.ProgressMonitor;
- import org.eclipse.jgit.lib.Repository;
-
- import org.xml.sax.Attributes;
- import org.xml.sax.InputSource;
- import org.xml.sax.SAXException;
- import org.xml.sax.XMLReader;
- import org.xml.sax.helpers.DefaultHandler;
- import org.xml.sax.helpers.XMLReaderFactory;
-
- /**
- * A class used to execute a repo command.
- *
- * This will parse a repo XML manifest, convert it into .gitmodules file and the
- * repository config file.
- *
- * @see <a href="https://code.google.com/p/git-repo/">git-repo project page</a>
- * @since 3.4
- */
- public class RepoCommand extends GitCommand<Void> {
-
- private String path;
- private String uri;
- private String groups;
-
- private ProgressMonitor monitor;
-
- private static class CopyFile {
- final String src;
- final String dest;
- final String relativeDest;
-
- CopyFile(Repository repo, String path, String src, String dest) {
- this.src = repo.getWorkTree() + "/" + path + "/" + src; //$NON-NLS-1$ //$NON-NLS-2$
- this.relativeDest = dest;
- this.dest = repo.getWorkTree() + "/" + dest; //$NON-NLS-1$
- }
-
- void copy() throws IOException {
- FileInputStream input = new FileInputStream(src);
- try {
- FileOutputStream output = new FileOutputStream(dest);
- try {
- FileChannel channel = input.getChannel();
- output.getChannel().transferFrom(channel, 0, channel.size());
- } finally {
- output.close();
- }
- } finally {
- input.close();
- }
- }
- }
-
- private static class Project {
- final String name;
- final String path;
- final Set<String> groups;
- final List<CopyFile> copyfiles;
-
- Project(String name, String path, String groups) {
- this.name = name;
- this.path = path;
- this.groups = new HashSet<String>();
- if (groups != null && groups.length() > 0)
- this.groups.addAll(Arrays.asList(groups.split(","))); //$NON-NLS-1$
- copyfiles = new ArrayList<CopyFile>();
- }
-
- void addCopyFile(CopyFile copyfile) {
- copyfiles.add(copyfile);
- }
- }
-
- private static class XmlManifest extends DefaultHandler {
- private final RepoCommand command;
- private final String filename;
- private final String baseUrl;
- private final Map<String, String> remotes;
- private final List<Project> projects;
- private final Set<String> plusGroups;
- private final Set<String> minusGroups;
- private String defaultRemote;
- private Project currentProject;
-
- XmlManifest(RepoCommand command, String filename, String baseUrl, String groups) {
- this.command = command;
- this.filename = filename;
- this.baseUrl = baseUrl;
- remotes = new HashMap<String, String>();
- projects = new ArrayList<Project>();
- plusGroups = new HashSet<String>();
- minusGroups = new HashSet<String>();
- if (groups == null || groups.length() == 0 || groups.equals("default")) { //$NON-NLS-1$
- // default means "all,-notdefault"
- minusGroups.add("notdefault"); //$NON-NLS-1$
- } else {
- for (String group : groups.split(",")) { //$NON-NLS-1$
- if (group.startsWith("-")) //$NON-NLS-1$
- minusGroups.add(group.substring(1));
- else
- plusGroups.add(group);
- }
- }
- }
-
- void read() throws IOException {
- final XMLReader xr;
- try {
- xr = XMLReaderFactory.createXMLReader();
- } catch (SAXException e) {
- throw new IOException(JGitText.get().noXMLParserAvailable);
- }
- xr.setContentHandler(this);
- final FileInputStream in = new FileInputStream(filename);
- try {
- xr.parse(new InputSource(in));
- } catch (SAXException e) {
- IOException error = new IOException(MessageFormat.format(
- RepoText.get().errorParsingManifestFile, filename));
- error.initCause(e);
- throw error;
- } finally {
- in.close();
- }
- }
-
- @Override
- public void startElement(
- String uri,
- String localName,
- String qName,
- Attributes attributes) throws SAXException {
- if ("project".equals(qName)) { //$NON-NLS-1$
- currentProject = new Project( //$NON-NLS-1$
- attributes.getValue("name"), //$NON-NLS-1$
- attributes.getValue("path"), //$NON-NLS-1$
- attributes.getValue("groups")); //$NON-NLS-1$
- } else if ("remote".equals(qName)) { //$NON-NLS-1$
- remotes.put(attributes.getValue("name"), //$NON-NLS-1$
- attributes.getValue("fetch")); //$NON-NLS-1$
- } else if ("default".equals(qName)) { //$NON-NLS-1$
- defaultRemote = attributes.getValue("remote"); //$NON-NLS-1$
- } else if ("copyfile".equals(qName)) { //$NON-NLS-1$
- if (currentProject == null)
- throw new SAXException(RepoText.get().invalidManifest);
- currentProject.addCopyFile(new CopyFile(
- command.repo,
- currentProject.path,
- attributes.getValue("src"), //$NON-NLS-1$
- attributes.getValue("dest"))); //$NON-NLS-1$
- }
- }
-
- @Override
- public void endElement(
- String uri,
- String localName,
- String qName) throws SAXException {
- if ("project".equals(qName)) { //$NON-NLS-1$
- projects.add(currentProject);
- currentProject = null;
- }
- }
-
- @Override
- public void endDocument() throws SAXException {
- if (defaultRemote == null) {
- throw new SAXException(MessageFormat.format(
- RepoText.get().errorNoDefault, filename));
- }
- final String remoteUrl;
- try {
- URI uri = new URI(String.format("%s/%s/", baseUrl, remotes.get(defaultRemote))); //$NON-NLS-1$
- remoteUrl = uri.normalize().toString();
- } catch (URISyntaxException e) {
- throw new SAXException(e);
- }
- for (Project proj : projects) {
- if (inGroups(proj)) {
- String url = remoteUrl + proj.name;
- command.addSubmodule(url, proj.path);
- for (CopyFile copyfile : proj.copyfiles) {
- try {
- copyfile.copy();
- } catch (IOException e) {
- throw new SAXException(
- RepoText.get().copyFileFailed, e);
- }
- AddCommand add = new AddCommand(command.repo)
- .addFilepattern(copyfile.relativeDest);
- try {
- add.call();
- } catch (GitAPIException e) {
- throw new SAXException(e);
- }
- }
- }
- }
- }
-
- boolean inGroups(Project proj) {
- for (String group : minusGroups) {
- if (proj.groups.contains(group)) {
- // minus groups have highest priority.
- return false;
- }
- }
- if (plusGroups.isEmpty() || plusGroups.contains("all")) { //$NON-NLS-1$
- // empty plus groups means "all"
- return true;
- }
- for (String group : plusGroups) {
- if (proj.groups.contains(group))
- return true;
- }
- return false;
- }
- }
-
- private static class ManifestErrorException extends GitAPIException {
- ManifestErrorException(Throwable cause) {
- super(RepoText.get().invalidManifest, cause);
- }
- }
-
- /**
- * @param repo
- */
- public RepoCommand(final Repository repo) {
- super(repo);
- }
-
- /**
- * Set path to the manifest XML file
- *
- * @param path
- * (with <code>/</code> as separator)
- * @return this command
- */
- public RepoCommand setPath(final String path) {
- this.path = path;
- return this;
- }
-
- /**
- * Set base URI of the pathes inside the XML
- *
- * @param uri
- * @return this command
- */
- public RepoCommand setURI(final String uri) {
- this.uri = uri;
- return this;
- }
-
- /**
- * Set groups to sync
- *
- * @param groups groups separated by comma, examples: default|all|G1,-G2,-G3
- * @return this command
- */
- public RepoCommand setGroups(final String groups) {
- this.groups = groups;
- return this;
- }
-
- /**
- * The progress monitor associated with the clone operation. By default,
- * this is set to <code>NullProgressMonitor</code>
- *
- * @see org.eclipse.jgit.lib.NullProgressMonitor
- * @param monitor
- * @return this command
- */
- public RepoCommand setProgressMonitor(final ProgressMonitor monitor) {
- this.monitor = monitor;
- return this;
- }
-
- @Override
- public Void call() throws GitAPIException {
- checkCallable();
- if (path == null || path.length() == 0)
- throw new IllegalArgumentException(JGitText.get().pathNotConfigured);
- if (uri == null || uri.length() == 0)
- throw new IllegalArgumentException(JGitText.get().uriNotConfigured);
-
- XmlManifest manifest = new XmlManifest(this, path, uri, groups);
- try {
- manifest.read();
- } catch (IOException e) {
- throw new ManifestErrorException(e);
- }
-
- return null;
- }
-
- private void addSubmodule(String url, String name) throws SAXException {
- SubmoduleAddCommand add = new SubmoduleAddCommand(repo)
- .setPath(name)
- .setURI(url);
- if (monitor != null)
- add.setProgressMonitor(monitor);
- try {
- add.call();
- } catch (GitAPIException e) {
- throw new SAXException(e);
- }
- }
- }
|