]> source.dussan.org Git - jgit.git/commitdiff
Added setInputStream to RepoCommand. 80/28080/3
authorYuxuan 'fishy' Wang <fishywang@google.com>
Thu, 5 Jun 2014 20:53:28 +0000 (13:53 -0700)
committerDave Borowitz <dborowitz@google.com>
Mon, 9 Jun 2014 17:26:52 +0000 (13:26 -0400)
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>
org.eclipse.jgit/resources/org/eclipse/jgit/gitrepo/internal/RepoText.properties
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/internal/RepoText.java

index 33fdc2e15fa44bbe1037f64c28790da8192e9bce..256dd7f085e0fdb8a211c098cc4033f299d6dc00 100644 (file)
@@ -1,6 +1,7 @@
 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.
 invalidManifest=Invalid manifest.
 repoCommitMessage=Added repo manifest.
index 22ff066fc3aec8dbf8fb40113bcaf1020a406a3c..b127697fb5fad5a135e03f9f08d2aa067ae62200 100644 (file)
@@ -46,6 +46,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.channels.FileChannel;
@@ -114,6 +115,7 @@ public class RepoCommand extends GitCommand<RevCommit> {
        private String branch;
        private PersonIdent author;
        private RemoteReader callback;
+       private InputStream inputStream;
 
        private List<Project> bareProjects;
        private Git git;
@@ -249,6 +251,7 @@ public class RepoCommand extends GitCommand<RevCommit> {
 
        private static class XmlManifest extends DefaultHandler {
                private final RepoCommand command;
+               private final InputStream inputStream;
                private final String filename;
                private final String baseUrl;
                private final Map<String, String> remotes;
@@ -259,8 +262,10 @@ public class RepoCommand extends GitCommand<RevCommit> {
                private String defaultRevision;
                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.inputStream = inputStream;
                        this.filename = filename;
                        this.baseUrl = baseUrl;
                        remotes = new HashMap<String, String>();
@@ -288,16 +293,13 @@ public class RepoCommand extends GitCommand<RevCommit> {
                                throw new IOException(JGitText.get().noXMLParserAvailable);
                        }
                        xr.setContentHandler(this);
-                       final FileInputStream in = new FileInputStream(filename);
                        try {
-                               xr.parse(new InputSource(in));
+                               xr.parse(new InputSource(inputStream));
                        } catch (SAXException e) {
-                               IOException error = new IOException(MessageFormat.format(
-                                                       RepoText.get().errorParsingManifestFile, filename));
+                               IOException error = new IOException(
+                                                       RepoText.get().errorParsingManifestFile);
                                error.initCause(e);
                                throw error;
-                       } finally {
-                               in.close();
                        }
                }
 
@@ -346,8 +348,11 @@ public class RepoCommand extends GitCommand<RevCommit> {
                @Override
                public void endDocument() throws SAXException {
                        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;
                        try {
@@ -406,7 +411,9 @@ public class RepoCommand extends GitCommand<RevCommit> {
        }
 
        /**
-        * 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
         *            (with <code>/</code> as separator)
@@ -417,6 +424,20 @@ public class RepoCommand extends GitCommand<RevCommit> {
                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
         *
@@ -496,26 +517,46 @@ public class RepoCommand extends GitCommand<RevCommit> {
 
        @Override
        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 {
-                       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()) {
index 34db723b63f608e845d06a0ffab28edf97ae14dd..1f9d5d863addc889a6d606406e04dea239c9c5bd 100644 (file)
@@ -61,6 +61,7 @@ public class RepoText extends TranslationBundle {
        // @formatter:off
        /***/ public String copyFileFailed;
        /***/ public String errorNoDefault;
+       /***/ public String errorNoDefaultFilename;
        /***/ public String errorParsingManifestFile;
        /***/ public String errorRemoteUnavailable;
        /***/ public String invalidManifest;