Browse Source

Added setInputStream to RepoCommand.

Sometimes an input stream is more useful than the filename of the xml manifest.

Change-Id: Icb09ac751b3d8d7eb14427ad1aac8cee0c371c5f
Signed-off-by: Yuxuan 'fishy' Wang <fishywang@google.com>
tags/v3.5.0.201409071800-rc1
Yuxuan 'fishy' Wang 10 years ago
parent
commit
acd7ed5269

+ 3
- 2
org.eclipse.jgit/resources/org/eclipse/jgit/gitrepo/internal/RepoText.properties View File

copyFileFailed=Error occurred during execution of copyfile rule. copyFileFailed=Error occurred during execution of copyfile rule.
errorNoDefault=Error: no default remote in file {0}.
errorParsingManifestFile=Error occurred during parsing manifest file {0}.
errorNoDefault=Error: no default remote in manifest file.
errorNoDefaultFilename=Error: no default remote in manifest file {0}.
errorParsingManifestFile=Error occurred during parsing manifest file.
errorRemoteUnavailable=Error remote {0} is unavailable. errorRemoteUnavailable=Error remote {0} is unavailable.
invalidManifest=Invalid manifest. invalidManifest=Invalid manifest.
repoCommitMessage=Added repo manifest. repoCommitMessage=Added repo manifest.

+ 70
- 29
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java View File

import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
private String branch; private String branch;
private PersonIdent author; private PersonIdent author;
private RemoteReader callback; private RemoteReader callback;
private InputStream inputStream;


private List<Project> bareProjects; private List<Project> bareProjects;
private Git git; private Git git;


private static class XmlManifest extends DefaultHandler { private static class XmlManifest extends DefaultHandler {
private final RepoCommand command; private final RepoCommand command;
private final InputStream inputStream;
private final String filename; private final String filename;
private final String baseUrl; private final String baseUrl;
private final Map<String, String> remotes; private final Map<String, String> remotes;
private String defaultRevision; private String defaultRevision;
private Project currentProject; private Project currentProject;


XmlManifest(RepoCommand command, String filename, String baseUrl, String groups) {
XmlManifest(RepoCommand command, InputStream inputStream,
String filename, String baseUrl, String groups) {
this.command = command; this.command = command;
this.inputStream = inputStream;
this.filename = filename; this.filename = filename;
this.baseUrl = baseUrl; this.baseUrl = baseUrl;
remotes = new HashMap<String, String>(); remotes = new HashMap<String, String>();
throw new IOException(JGitText.get().noXMLParserAvailable); throw new IOException(JGitText.get().noXMLParserAvailable);
} }
xr.setContentHandler(this); xr.setContentHandler(this);
final FileInputStream in = new FileInputStream(filename);
try { try {
xr.parse(new InputSource(in));
xr.parse(new InputSource(inputStream));
} catch (SAXException e) { } catch (SAXException e) {
IOException error = new IOException(MessageFormat.format(
RepoText.get().errorParsingManifestFile, filename));
IOException error = new IOException(
RepoText.get().errorParsingManifestFile);
error.initCause(e); error.initCause(e);
throw error; throw error;
} finally {
in.close();
} }
} }


@Override @Override
public void endDocument() throws SAXException { public void endDocument() throws SAXException {
if (defaultRemote == null) { if (defaultRemote == null) {
throw new SAXException(MessageFormat.format(
RepoText.get().errorNoDefault, filename));
if (filename != null)
throw new SAXException(MessageFormat.format(
RepoText.get().errorNoDefaultFilename, filename));
else
throw new SAXException(RepoText.get().errorNoDefault);
} }
final String remoteUrl; final String remoteUrl;
try { try {
} }


/** /**
* Set path to the manifest XML file
* Set path to the manifest XML file.
*
* Calling {@link #setInputStream} will ignore the path set here.
* *
* @param path * @param path
* (with <code>/</code> as separator) * (with <code>/</code> as separator)
return this; return this;
} }


/**
* Set the input stream to the manifest XML.
*
* Setting inputStream will ignore the path set.
* It will be closed in {@link #call}.
*
* @param inputStream
* @return this command
*/
public RepoCommand setInputStream(final InputStream inputStream) {
this.inputStream = inputStream;
return this;
}

/** /**
* Set base URI of the pathes inside the XML * Set base URI of the pathes inside the XML
* *


@Override @Override
public RevCommit call() throws GitAPIException { public RevCommit 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);

if (repo.isBare()) {
bareProjects = new ArrayList<Project>();
if (author == null)
author = new PersonIdent(repo);
if (callback == null)
callback = new DefaultRemoteReader();
} else
git = new Git(repo);

XmlManifest manifest = new XmlManifest(this, path, uri, groups);
try { try {
manifest.read();
} catch (IOException e) {
throw new ManifestErrorException(e);
checkCallable();
if (uri == null || uri.length() == 0)
throw new IllegalArgumentException(
JGitText.get().uriNotConfigured);
if (inputStream == null) {
if (path == null || path.length() == 0)
throw new IllegalArgumentException(
JGitText.get().pathNotConfigured);
try {
inputStream = new FileInputStream(path);
} catch (IOException e) {
throw new IllegalArgumentException(
JGitText.get().pathNotConfigured);
}
}

if (repo.isBare()) {
bareProjects = new ArrayList<Project>();
if (author == null)
author = new PersonIdent(repo);
if (callback == null)
callback = new DefaultRemoteReader();
} else
git = new Git(repo);

XmlManifest manifest = new XmlManifest(
this, inputStream, path, uri, groups);
try {
manifest.read();
} catch (IOException e) {
throw new ManifestErrorException(e);
}
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
// Just ignore it, it's not important.
}
} }


if (repo.isBare()) { if (repo.isBare()) {

+ 1
- 0
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/internal/RepoText.java View File

// @formatter:off // @formatter:off
/***/ public String copyFileFailed; /***/ public String copyFileFailed;
/***/ public String errorNoDefault; /***/ public String errorNoDefault;
/***/ public String errorNoDefaultFilename;
/***/ public String errorParsingManifestFile; /***/ public String errorParsingManifestFile;
/***/ public String errorRemoteUnavailable; /***/ public String errorRemoteUnavailable;
/***/ public String invalidManifest; /***/ public String invalidManifest;

Loading…
Cancel
Save