The Unified and Autonomous Development Platform
Enhance git show command to support input stream consuming.
robin shine22f84bf69feb
diff --git a/commons.git/src/main/java/com/pmease/commons/git/Git.java b/commons.git/src/main/java/com/pmease/commons/git/Git.javaindex 0222190b7f..a634a3e0db 100644--- a/commons.git/src/main/java/com/pmease/commons/git/Git.java+++ b/commons.git/src/main/java/com/pmease/commons/git/Git.java@@ -36,10 +36,12 @@ import com.pmease.commons.git.command.PushCommand;import com.pmease.commons.git.command.RemoveCommand;import com.pmease.commons.git.command.ResetCommand;import com.pmease.commons.git.command.ShowCommand;+import com.pmease.commons.git.command.ShowForConsumeCommand;import com.pmease.commons.git.command.ShowRefCommand;import com.pmease.commons.git.command.UpdateRefCommand;import com.pmease.commons.util.FileUtils;import com.pmease.commons.util.GeneralException;+import com.pmease.commons.util.execution.StreamConsumer;@SuppressWarnings("serial")public class Git implements Serializable {@@ -257,10 +259,26 @@ public class Git implements Serializable {return new ListTagsCommand(repoDir).call();}+ /**+ * Show content of specified path of specified revision.+ *+ * @param revision+ * revision of the path+ * @param path+ * path to show content+ * @param consumer+ * consumer to absorb content of specified path and revision+ *+ */+ public Git show(String revision, String path, StreamConsumer consumer) {+ new ShowForConsumeCommand(repoDir).revision(revision).path(path).consumer(consumer).call();+ return this;+ }+public byte[] show(String revision, String path) {return new ShowCommand(repoDir).revision(revision).path(path).call();}-+public List<Commit> log(@Nullable String fromRev, @Nullable String toRev,@Nullable String path, int maxCommits) {return new LogCommand(repoDir).fromRev(fromRev).toRev(toRev)diff --git a/commons.git/src/main/java/com/pmease/commons/git/TreeNode.java b/commons.git/src/main/java/com/pmease/commons/git/TreeNode.javaindex 0e245c8019..d1b3ebfeaf 100644--- a/commons.git/src/main/java/com/pmease/commons/git/TreeNode.java+++ b/commons.git/src/main/java/com/pmease/commons/git/TreeNode.java@@ -12,7 +12,6 @@ import org.eclipse.jgit.lib.FileMode;import com.google.common.base.Optional;import com.google.common.base.Preconditions;import com.pmease.commons.git.command.ListTreeCommand;-import com.pmease.commons.git.command.ShowCommand;@SuppressWarnings("serial")public class TreeNode implements Serializable {@@ -126,10 +125,11 @@ public class TreeNode implements Serializable {* represents a directory*/public byte[] show() {+ Git git = new Git(gitDir);if (mode == FileMode.GITLINK) {- return new Git(gitDir).listSubModules(revision).get(path).getBytes();+ return git.listSubModules(revision).get(path).getBytes();} else {- return new ShowCommand(gitDir).revision(getRevision()).path(getPath()).call();+ return git.show(getRevision(), getPath());}}diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/IsTreeLinkCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/IsTreeLinkCommand.javaindex 1c9cce121e..d73f0c3151 100644--- a/commons.git/src/main/java/com/pmease/commons/git/command/IsTreeLinkCommand.java+++ b/commons.git/src/main/java/com/pmease/commons/git/command/IsTreeLinkCommand.java@@ -4,6 +4,7 @@ import java.io.File;import java.io.UnsupportedEncodingException;import com.google.common.base.Preconditions;+import com.pmease.commons.git.Git;import com.pmease.commons.util.execution.Commandline;import com.pmease.commons.util.execution.ExecuteResult;import com.pmease.commons.util.execution.LineConsumer;@@ -33,10 +34,9 @@ public class IsTreeLinkCommand extends GitCommand<Boolean> {Preconditions.checkNotNull(revision, "revision has to be specified.");Preconditions.checkNotNull(symlink, "symlink has to be specified.");- ShowCommand show = new ShowCommand(repoDir).path(symlink).revision(revision);String targetPath;try {- targetPath = new String(show.call(), "UTF8");+ targetPath = new String(new Git(repoDir).show(revision, symlink), "UTF8");} catch (UnsupportedEncodingException e) {throw new RuntimeException(e);}diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/ShowCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/ShowCommand.javaindex a215b15ee8..85edf0c9fd 100644--- a/commons.git/src/main/java/com/pmease/commons/git/command/ShowCommand.java+++ b/commons.git/src/main/java/com/pmease/commons/git/command/ShowCommand.java@@ -1,9 +1,8 @@package com.pmease.commons.git.command;+import java.io.ByteArrayOutputStream;import java.io.File;-import org.apache.commons.io.output.ByteArrayOutputStream;-import com.google.common.base.Preconditions;import com.pmease.commons.util.execution.Commandline;diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/ShowForConsumeCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/ShowForConsumeCommand.javanew file mode 100644index 0000000000..69a1d0d5db--- /dev/null+++ b/commons.git/src/main/java/com/pmease/commons/git/command/ShowForConsumeCommand.java@@ -0,0 +1,48 @@+package com.pmease.commons.git.command;++import java.io.File;++import com.google.common.base.Preconditions;+import com.pmease.commons.util.execution.Commandline;+import com.pmease.commons.util.execution.StreamConsumer;++public class ShowForConsumeCommand extends GitCommand<Void> {++ private String revision;++ private String path;++ private StreamConsumer consumer;++ public ShowForConsumeCommand(final File repoDir) {+ super(repoDir);+ }++ public ShowForConsumeCommand revision(String revision) {+ this.revision = revision;+ return this;+ }++ public ShowForConsumeCommand path(String path) {+ this.path = path;+ return this;+ }++ public ShowForConsumeCommand consumer(StreamConsumer consumer) {+ this.consumer = consumer;+ return this;+ }++ @Override+ public Void call() {+ Preconditions.checkNotNull(revision, "revision has to be specified for browse.");+ Preconditions.checkNotNull(path, "path has to be specified.");+ Preconditions.checkNotNull(consumer, "consumer has to be specified.");++ Commandline cmd = cmd().addArgs("show", revision + ":" + path);+ cmd.execute(consumer, errorLogger).checkReturnCode();++ return null;+ }++}diff --git a/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.java b/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.javaindex 4bc36ebf15..0306adf768 100644--- a/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.java+++ b/commons.git/src/test/java/com/pmease/commons/git/command/ListTreeCommandTest.java@@ -2,8 +2,10 @@ package com.pmease.commons.git.command;import static org.junit.Assert.assertEquals;+import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;+import java.io.InputStream;import java.util.List;import org.eclipse.jgit.lib.FileMode;@@ -13,6 +15,7 @@ import org.junit.Test;import com.pmease.commons.git.Git;import com.pmease.commons.git.TreeNode;import com.pmease.commons.util.FileUtils;+import com.pmease.commons.util.execution.StreamConsumer;public class ListTreeCommandTest {@@ -90,4 +93,17 @@ public class ListTreeCommandTest {}}+ @Test+ public void test() {+ Git git = new Git(new File("w:\\temp\\test"));+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();+ git.show("master", "large.rar", new StreamConsumer() {++ @Override+ protected void consume(InputStream input) {+ }++ });+ System.out.println(baos.toByteArray().length);+ }}diff --git a/commons.util/src/main/java/com/pmease/commons/util/execution/Commandline.java b/commons.util/src/main/java/com/pmease/commons/util/execution/Commandline.javaindex d7d0a3b93d..e96d90530f 100644--- a/commons.util/src/main/java/com/pmease/commons/util/execution/Commandline.java+++ b/commons.util/src/main/java/com/pmease/commons/util/execution/Commandline.java@@ -106,10 +106,6 @@ public class Commandline {return this;}- public ExecuteResult execute(OutputStream stdout, LineConsumer stderr) {- return execute(stdout, stderr, null);- }-private ProcessBuilder createProcessBuilder() {File workingDir = this.workingDir;if (workingDir == null)@@ -149,6 +145,18 @@ public class Commandline {return processBuilder;}+ /**+ * Execute the command.+ *+ * @param stdout+ * output stream to write standard output, caller is responsible for closing the stream+ * @param stderr+ * line consumer to handle standard error+ * @param stdin+ * input stream to read standard input from, caller is responsible for closing the stream+ * @return+ * execution result+ */public ExecuteResult execute(OutputStream stdout, final LineConsumer stderr, @Nullable InputStream stdin) {Process process;try {@@ -194,7 +202,28 @@ public class Commandline {return result;}- public void executeWithoutWait(@Nullable byte[] stdinBytes) {++ /**+ * Execute the command.+ *+ * @param stdout+ * output stream to write standard output, caller is responsible for closing the stream+ * @param stderr+ * line consumer to handle standard error+ * @return+ * execution result+ */+ public ExecuteResult execute(OutputStream stdout, LineConsumer stderr) {+ return execute(stdout, stderr, null);+ }++ /**+ * Execute the command without waiting for command completion.+ *+ * @param stdinBytes+ * standard input bytes to feed into command+ */+ public void executeWithoutWait(@Nullable byte[] stdinBytes) {ByteArrayInputStream stdinStream = null;if (stdinBytes != null && stdinBytes.length != 0)stdinStream = new ByteArrayInputStream(stdinBytes);diff --git a/commons.util/src/main/java/com/pmease/commons/util/execution/ProcessStreamPumper.java b/commons.util/src/main/java/com/pmease/commons/util/execution/ProcessStreamPumper.javaindex 187001b58a..71424dec0f 100644--- a/commons.util/src/main/java/com/pmease/commons/util/execution/ProcessStreamPumper.java+++ b/commons.util/src/main/java/com/pmease/commons/util/execution/ProcessStreamPumper.java@@ -31,11 +31,11 @@ public class ProcessStreamPumper {this.stdout = stdout;this.stderr = stderr;- stdoutPumper = createPump(process.getInputStream(), stdout, false);- stderrPumper = createPump(process.getErrorStream(), stderr, false);+ stdoutPumper = createPump(process.getInputStream(), stdout, true, false);+ stderrPumper = createPump(process.getErrorStream(), stderr, true, false);if (stdin != null)- stdinPumper = createPump(stdin, process.getOutputStream(), true);+ stdinPumper = createPump(stdin, process.getOutputStream(), false, true);elsestdinPumper = null;}@@ -69,8 +69,8 @@ public class ProcessStreamPumper {}}- private Future<?> createPump(final InputStream input, final OutputStream output,- final boolean closeWhenExhausted) {+ private Future<?> createPump(final InputStream input, @Nullable final OutputStream output,+ final boolean closeInputWhenExhausted, final boolean closeOutputWhenExhausted) {return Commandline.EXECUTOR_SERVICE.submit(new Runnable() {@@ -82,11 +82,13 @@ public class ProcessStreamPumper {while ((length = input.read(buf)) > 0) {if (output != null) {output.write(buf, 0, length);- //output.flush();}}- if (closeWhenExhausted && output!=null) {+ if (closeInputWhenExhausted) {+ input.close();+ }+ if (closeOutputWhenExhausted && output!=null) {output.close();}diff --git a/commons.util/src/main/java/com/pmease/commons/util/execution/StreamConsumer.java b/commons.util/src/main/java/com/pmease/commons/util/execution/StreamConsumer.javanew file mode 100644index 0000000000..cb7573fe7c--- /dev/null+++ b/commons.util/src/main/java/com/pmease/commons/util/execution/StreamConsumer.java@@ -0,0 +1,52 @@+package com.pmease.commons.util.execution;++import java.io.IOException;+import java.io.InputStream;+import java.io.PipedInputStream;+import java.io.PipedOutputStream;++import org.apache.commons.io.IOUtils;++public abstract class StreamConsumer extends PipedOutputStream {++ private static final int BUFFER_SIZE = 64*1024;++ public StreamConsumer() {+ final PipedInputStream input = new PipedInputStream(BUFFER_SIZE);+ try {+ connect(input);+ } catch (IOException e) {+ throw new RuntimeException(e);+ }++ Commandline.EXECUTOR_SERVICE.submit(new Runnable() {++ @Override+ public void run() {+ try {+ consume(input);++ // In case consumer does not care about the stream, we pump to the end+ // of the input stream so that the other side of the pipe will not+ // block.+ byte[] buffer = new byte[BUFFER_SIZE];+ while (input.read(buffer) != -1);+ } catch (IOException e) {+ throw new RuntimeException(e);+ } finally {+ IOUtils.closeQuietly(input);+ }+ }++ });+ }++ /**+ * Consume specified input stream.+ *+ * @param input+ * input stream supplied by caller. Will be closed by caller+ */+ protected abstract void consume(InputStream input);++}diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/test/TestPage.html b/gitop.web/src/main/java/com/pmease/gitop/web/page/test/TestPage.htmlindex 599f633232..36b99e0e7b 100644--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/test/TestPage.html+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/test/TestPage.html@@ -3,27 +3,9 @@<head></head><body>- <div wicket:id="accordion" class="accordion1">- <div style="border:1px solid blue;"><a wicket:id="link1">link1</a></div>- <div wicket:id="content1" style="border:1px solid red;">- <div>begin content1</div>- <a wicket:id="show">show</a>- <div wicket:id="content"></div>- <div>end content1</div>- </div>- <div style="border:1px solid blue;"><a wicket:id="link2">link2</a></div>- <div wicket:id="content2" style="border:1px solid red;">content2</div>- <div style="border:1px solid blue;"><a wicket:id="link3">link3</a></div>- <div wicket:id="content3" style="border:1px solid red;">content3</div>+ <div wicket:id="container">+ <div wicket:id="control">control</div>+ <div wicket:id="content">content</div></div>- <a wicket:id="test">test</a></body>-<wicket:fragment wicket:id="content">-<div wicket:id="accordion" class="accordion2">- <div><a wicket:id="link11">link11</a></div>- <div wicket:id="content11">content11</div>- <div><a wicket:id="link12">link12</a></div>- <div wicket:id="content12">content12</div>-</div>-</wicket:fragment></html>diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/test/TestPage.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/test/TestPage.javaindex 2bf1ecf643..92f80c5687 100644--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/test/TestPage.java+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/test/TestPage.java@@ -1,63 +1,31 @@package com.pmease.gitop.web.page.test;-import org.apache.wicket.ajax.AjaxRequestTarget;-import org.apache.wicket.ajax.markup.html.AjaxLink;+import org.apache.wicket.markup.head.IHeaderResponse;+import org.apache.wicket.markup.head.OnDomReadyHeaderItem;import org.apache.wicket.markup.html.WebMarkupContainer;import org.apache.wicket.markup.html.WebPage;-import org.apache.wicket.markup.html.panel.Fragment;--import com.pmease.commons.wicket.behavior.collapse.AccordionPanel;-import com.pmease.commons.wicket.behavior.collapse.CollapseBehavior;+import org.apache.wicket.markup.html.basic.EnclosureContainer;@SuppressWarnings("serial")public class TestPage extends WebPage {public TestPage() {- final WebMarkupContainer accordion = new AccordionPanel("accordion");- add(accordion);+ WebMarkupContainer control = new WebMarkupContainer("control");+ control.setVisible(false);- final WebMarkupContainer content1 = new WebMarkupContainer("content1");- accordion.add(content1);- accordion.add(new WebMarkupContainer("link1").add(new CollapseBehavior(content1)));-- WebMarkupContainer content2 = new WebMarkupContainer("content2");- accordion.add(content2);- accordion.add(new WebMarkupContainer("link2").add(new CollapseBehavior(content2)));-- WebMarkupContainer content3 = new WebMarkupContainer("content3");- accordion.add(content3);- accordion.add(new WebMarkupContainer("link3").add(new CollapseBehavior(content3)));-- content1.add(new AjaxLink<Void>("show") {+ EnclosureContainer container = new EnclosureContainer("container", control);+ add(container);+ container.add(control);+ container.add(new WebMarkupContainer("content") {@Override- public void onClick(AjaxRequestTarget target) {- Fragment fragment = new Fragment("content", "content", TestPage.this);- AccordionPanel accordion = new AccordionPanel("accordion");- fragment.add(accordion);- WebMarkupContainer content11 = new WebMarkupContainer("content11");- accordion.add(content11);- accordion.add(new WebMarkupContainer("link11").add(new CollapseBehavior(content11)));-- WebMarkupContainer content12 = new WebMarkupContainer("content12");- accordion.add(content12);- accordion.add(new WebMarkupContainer("link12").add(new CollapseBehavior(content12)));- fragment.setOutputMarkupId(true);- target.add(fragment);- content1.replace(fragment);+ public void renderHead(IHeaderResponse response) {+ super.renderHead(response);+ String script = String.format("document.getElementById('%s').className += ' someCssClass';", getMarkupId());+ response.render(OnDomReadyHeaderItem.forScript(script));}- });- content1.add(new WebMarkupContainer("content").setOutputMarkupId(true));-- add(new AjaxLink<Void>("test") {-- @Override- public void onClick(AjaxRequestTarget target) {- target.add(accordion);- }-- });+ }.setOutputMarkupId(true));}}