Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 212379 Details for
Bug 372119
Provide REST API for git command line
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
Orion/Server patch v01
server_v01.patch (text/plain), 18.75 KB, created by
Tomasz Zarna
on 2012-03-09 09:04:00 EST
(
hide
)
Description:
Orion/Server patch v01
Filename:
MIME Type:
Creator:
Tomasz Zarna
Created:
2012-03-09 09:04:00 EST
Size:
18.75 KB
patch
obsolete
>diff --git a/bundles/org.eclipse.orion.server.git/META-INF/MANIFEST.MF b/bundles/org.eclipse.orion.server.git/META-INF/MANIFEST.MF >index 718ea91..2d37857 100644 >--- a/bundles/org.eclipse.orion.server.git/META-INF/MANIFEST.MF >+++ b/bundles/org.eclipse.orion.server.git/META-INF/MANIFEST.MF >@@ -10,14 +10,16 @@ Require-Bundle: org.eclipse.orion.server.servlets;bundle-version="0.1.0", > org.eclipse.orion.server.core;bundle-version="0.2.0", > org.json;bundle-version="1.0.0", > org.eclipse.core.filesystem;bundle-version="1.3.100", >- org.eclipse.jgit;bundle-version="1.2.0", >+ org.eclipse.jgit;bundle-version="1.3.0", >+ org.kohsuke.args4j;bundle-version="2.0.12", > org.eclipse.equinox.http.registry, > com.jcraft.jsch;bundle-version="0.1.44", > org.eclipse.orion.server.useradmin;bundle-version="0.2.0", > org.eclipse.equinox.common;bundle-version="3.6.0", > org.eclipse.equinox.preferences;bundle-version="3.4.0", > org.eclipse.core.jobs;bundle-version="3.5.100", >- org.apache.commons.httpclient;bundle-version="3.1.0" >+ org.apache.commons.httpclient;bundle-version="3.1.0", >+ org.eclipse.jgit.pgm;bundle-version="1.3.0" > Bundle-ActivationPolicy: lazy > Bundle-RequiredExecutionEnvironment: JavaSE-1.6 > Bundle-Vendor: %Bundle-Vendor >diff --git a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/GitConstants.java b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/GitConstants.java >index d7b55d8..f9c4e26 100644 >--- a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/GitConstants.java >+++ b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/GitConstants.java >@@ -31,6 +31,8 @@ public class GitConstants { > > public static final String KEY_REMOTE = "RemoteLocation"; //$NON-NLS-1$ > >+ public static final String KEY_COMMAND = "CommandLocation"; //$NON-NLS-1$ >+ > public static final String KEY_REPOSITORY_PATH = "RepositoryPath"; //$NON-NLS-1$ > > public static final String KEY_REPOSITORY_STATE = "RepositoryState"; //$NON-NLS-1$ >@@ -172,4 +174,6 @@ public class GitConstants { > public static final String KEY_TAG_COMMITS = "commits"; //$NON-NLS-1$ > > public static final String KEY_TAG_TYPE = "TagType"; //$NON-NLS-1$ >+ >+ public static final String KEY_COMMAND_ARGS = "Command"; //$NON-NLS-1$ > } >diff --git a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/GitFileDecorator.java b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/GitFileDecorator.java >index 8b2c857..534d303 100644 >--- a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/GitFileDecorator.java >+++ b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/GitFileDecorator.java >@@ -1,5 +1,5 @@ > /******************************************************************************* >- * Copyright (c) 2011 IBM Corporation and others. >+ * Copyright (c) 2011, 2012 IBM Corporation and others. > * All rights reserved. This program and the accompanying materials > * are made available under the terms of the Eclipse Public License v1.0 > * which accompanies this distribution, and is available at >@@ -159,6 +159,11 @@ public class GitFileDecorator implements IWebResourceDecorator { > link = new URI(location.getScheme(), location.getAuthority(), request.getContextPath() + path.toString(), null, null); > gitSection.put(GitConstants.KEY_TAG, link); > >+ // add Git Command URI >+ path = new Path(GitServlet.GIT_URI + '/' + Command.RESOURCE).append(targetPath); >+ link = new URI(location.getScheme(), location.getAuthority(), request.getContextPath() + path.toString(), null, null); >+ gitSection.put(GitConstants.KEY_COMMAND, link); >+ > // add Git Clone URI > gitSection.put(GitConstants.KEY_CLONE, cloneLocation); > >diff --git a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/cli/CLIGitCommand.java b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/cli/CLIGitCommand.java >new file mode 100644 >index 0000000..0b14261 >--- /dev/null >+++ b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/cli/CLIGitCommand.java >@@ -0,0 +1,60 @@ >+/******************************************************************************* >+ * Copyright (c) 2012 IBM Corporation and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.orion.server.git.cli; >+ >+import java.io.*; >+import java.util.ArrayList; >+import java.util.List; >+import org.eclipse.jgit.lib.Repository; >+import org.eclipse.jgit.pgm.TextBuiltin; >+import org.eclipse.jgit.pgm.opt.CmdLineParser; >+import org.eclipse.jgit.pgm.opt.SubcommandHandler; >+import org.kohsuke.args4j.Argument; >+ >+public class CLIGitCommand { >+ @Argument(index = 0, metaVar = "metaVar_command", required = true, handler = SubcommandHandler.class) >+ private TextBuiltin subcommand; >+ >+ @Argument(index = 1, metaVar = "metaVar_arg") >+ private List<String> arguments = new ArrayList<String>(); >+ >+ public TextBuiltin getSubcommand() { >+ return subcommand; >+ } >+ >+ public List<String> getArguments() { >+ return arguments; >+ } >+ >+ public static String execute(String[] argv, Repository db) throws Exception { >+ >+ CLIGitCommand bean = new CLIGitCommand(); >+ final CmdLineParser clp = new CmdLineParser(bean); >+ clp.parseArgument(argv); >+ >+ final TextBuiltin cmd = bean.getSubcommand(); >+ if (cmd.requiresRepository()) >+ cmd.init(db, null); >+ else >+ cmd.init(null, null); >+ >+ ByteArrayOutputStream baos = new ByteArrayOutputStream(); >+ cmd.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(baos))); >+ try { >+ cmd.execute(bean.getArguments().toArray(new String[bean.getArguments().size()])); >+ } finally { >+ if (cmd.out != null) >+ cmd.out.flush(); >+ } >+ >+ return baos.toString(); >+ } >+} >diff --git a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/objects/Command.java b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/objects/Command.java >new file mode 100644 >index 0000000..3970707 >--- /dev/null >+++ b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/objects/Command.java >@@ -0,0 +1,37 @@ >+/******************************************************************************* >+ * Copyright (c) 2012 IBM Corporation and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.orion.server.git.objects; >+ >+import java.net.URI; >+import java.net.URISyntaxException; >+import org.eclipse.jgit.lib.Repository; >+ >+public class Command extends GitObject { >+ >+ public static final String RESOURCE = "command"; //$NON-NLS-1$ >+ public static final String TYPE = "Command"; //$NON-NLS-1$ >+ >+ Command(URI cloneLocation, Repository db) { >+ super(cloneLocation, db); >+ } >+ >+ @Override >+ protected String getType() { >+ return TYPE; >+ } >+ >+ @Override >+ protected URI getLocation() throws URISyntaxException { >+ // TODO Auto-generated method stub >+ return null; >+ } >+ >+} >diff --git a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitCommandHandlerV1.java b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitCommandHandlerV1.java >new file mode 100644 >index 0000000..43b4218 >--- /dev/null >+++ b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitCommandHandlerV1.java >@@ -0,0 +1,87 @@ >+/******************************************************************************* >+ * Copyright (c) 2012 IBM Corporation and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.orion.server.git.servlets; >+ >+import java.io.File; >+import java.io.IOException; >+import javax.servlet.ServletException; >+import javax.servlet.http.HttpServletRequest; >+import javax.servlet.http.HttpServletResponse; >+import org.eclipse.core.runtime.*; >+import org.eclipse.jgit.lib.Repository; >+import org.eclipse.jgit.storage.file.FileRepository; >+import org.eclipse.orion.internal.server.servlets.ServletResourceHandler; >+import org.eclipse.orion.server.core.ServerStatus; >+import org.eclipse.orion.server.git.GitConstants; >+import org.eclipse.orion.server.git.cli.CLIGitCommand; >+import org.eclipse.orion.server.servlets.OrionServlet; >+import org.eclipse.osgi.util.NLS; >+import org.json.*; >+import org.kohsuke.args4j.CmdLineException; >+ >+public class GitCommandHandlerV1 extends ServletResourceHandler<String> { >+ private ServletResourceHandler<IStatus> statusHandler; >+ >+ public GitCommandHandlerV1(ServletResourceHandler<IStatus> statusHandler) { >+ this.statusHandler = statusHandler; >+ } >+ >+ @SuppressWarnings("incomplete-switch") >+ @Override >+ public boolean handleRequest(HttpServletRequest request, HttpServletResponse response, String path) throws ServletException { >+ try { >+ switch (getMethod(request)) { >+ case POST : >+ return handlePost(request, response, path); >+ } >+ } catch (Exception e) { >+ String msg = NLS.bind("Failed to handle 'command' request for {0}", path); >+ return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e)); >+ } >+ return false; >+ } >+ >+ private boolean handlePost(HttpServletRequest request, HttpServletResponse response, String path) throws ServletException { >+ IPath p = new Path(path); >+ try { >+ File gitDir = GitUtils.getGitDir(p); >+ Repository db = new FileRepository(gitDir); >+ String args[] = readArgs(request); >+ String result = CLIGitCommand.execute(args, db); >+ writeTextResponse(request, response, result); >+ return true; >+ } catch (CmdLineException e) { >+ return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, NLS.bind("The command cannot be executed: {0}", e.getMessage()), e)); >+ } catch (Exception e) { >+ return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when procesing command.", e)); >+ } >+ } >+ >+ private String[] readArgs(final HttpServletRequest request) throws IOException, JSONException { >+ JSONObject commandArgs = OrionServlet.readJSONRequest(request); >+ JSONArray jsonArray = commandArgs.getJSONArray(GitConstants.KEY_COMMAND_ARGS); >+ String[] args = new String[jsonArray.length()]; >+ for (int i = 0; i < jsonArray.length(); i++) { >+ args[i] = jsonArray.getString(i); >+ } >+ return args; >+ } >+ >+ private static void writeTextResponse(HttpServletRequest req, HttpServletResponse resp, String result) throws IOException { >+ resp.setStatus(HttpServletResponse.SC_OK); >+ resp.setHeader("Cache-Control", "no-cache"); //$NON-NLS-1$ //$NON-NLS-2$ >+ resp.setHeader("Cache-Control", "no-store"); //$NON-NLS-1$ //$NON-NLS-2$ >+ //TODO look at accept header and chose appropriate response representation >+ resp.setContentType("text/html;charset=UTF-8" /* ProtocolConstants.CONTENT_TYPE_TEXT */); //$NON-NLS-1$ >+ resp.getWriter().print(result); >+ } >+ >+} >diff --git a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitHandlerV1.java b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitHandlerV1.java >index 487fd8e..a6a6a8f 100644 >--- a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitHandlerV1.java >+++ b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitHandlerV1.java >@@ -1,5 +1,5 @@ > /******************************************************************************* >- * Copyright (c) 2011 IBM Corporation and others. >+ * Copyright (c) 2011, 2012 IBM Corporation and others. > * All rights reserved. This program and the accompanying materials > * are made available under the terms of the Eclipse Public License v1.0 > * which accompanies this distribution, and is available at >@@ -32,10 +32,12 @@ public class GitHandlerV1 extends ServletResourceHandler<String> { > private ServletResourceHandler<String> remoteHandlerV1; > private ServletResourceHandler<String> statusHandlerV1; > private ServletResourceHandler<String> tagHandlerV1; >+ private ServletResourceHandler<String> commandHandlerV1; > > GitHandlerV1(ServletResourceHandler<IStatus> statusHandler) { > branchHandlerV1 = new GitBranchHandlerV1(statusHandler); > cloneHandlerV1 = new GitCloneHandlerV1(statusHandler); >+ commandHandlerV1 = new GitCommandHandlerV1(statusHandler); > commitHandlerV1 = new GitCommitHandlerV1(statusHandler); > configHandlerV1 = new GitConfigHandlerV1(statusHandler); > diffHandlerV1 = new GitDiffHandlerV1(statusHandler); >@@ -63,6 +65,8 @@ public class GitHandlerV1 extends ServletResourceHandler<String> { > return branchHandlerV1.handleRequest(request, response, pathString); > } else if (infoParts[1].equals(Clone.RESOURCE)) { > return cloneHandlerV1.handleRequest(request, response, pathString); >+ } else if (infoParts[1].equals(Command.RESOURCE)) { >+ return commandHandlerV1.handleRequest(request, response, pathString); > } else if (infoParts[1].equals(Commit.RESOURCE)) { > return commitHandlerV1.handleRequest(request, response, pathString); > } else if (infoParts[1].equals(ConfigOption.RESOURCE)) { >diff --git a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitCommandTest.java b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitCommandTest.java >new file mode 100644 >index 0000000..c3c2307 >--- /dev/null >+++ b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitCommandTest.java >@@ -0,0 +1,94 @@ >+/******************************************************************************* >+ * Copyright (c) 2012 IBM Corporation and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.orion.server.tests.servlets.git; >+ >+import static org.junit.Assert.assertEquals; >+ >+import java.io.UnsupportedEncodingException; >+import java.net.HttpURLConnection; >+import java.net.URI; >+import java.util.Arrays; >+ >+import org.eclipse.orion.internal.server.core.IOUtilities; >+import org.eclipse.orion.internal.server.servlets.ProtocolConstants; >+import org.eclipse.orion.server.git.GitConstants; >+import org.eclipse.orion.server.git.objects.Command; >+import org.json.JSONException; >+import org.json.JSONObject; >+import org.junit.Test; >+ >+import com.meterware.httpunit.PostMethodWebRequest; >+import com.meterware.httpunit.WebRequest; >+import com.meterware.httpunit.WebResponse; >+ >+public class GitCommandTest extends GitTest { >+ >+ @Test >+ public void command() throws Exception { >+ URI workspaceLocation = createWorkspace(getMethodName()); >+ JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), gitDir.toString()); >+ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); >+ String gitCommandUri = gitSection.getString(GitConstants.KEY_COMMAND); >+ >+ WebRequest request = getPosGitCommandRequest(gitCommandUri, new String[] {"branch"}); >+ WebResponse response = webConversation.getResponse(request); >+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); >+ String commandResult = response.getText(); >+ >+ String expectedResult = "* master\r\n"; >+ assertEquals(expectedResult, commandResult); >+ } >+ >+ @Test >+ public void commandWithArgs() throws Exception { >+ URI workspaceLocation = createWorkspace(getMethodName()); >+ JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), gitDir.toString()); >+ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); >+ String gitCommandUri = gitSection.getString(GitConstants.KEY_COMMAND); >+ >+ WebRequest request = getPosGitCommandRequest(gitCommandUri, new String[] {"commit", "--amend", "-m", "'Amended commit'"}); >+ WebResponse response = webConversation.getResponse(request); >+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); >+ String commandResult = response.getText(); >+ >+ String expectedResult = "[master ecf5161e231e3f798ec9ae0910264bd5c85a4c37] 'Amended commit'\r\n"; >+ assertEquals(expectedResult, commandResult); >+ } >+ >+ @Test >+ public void notImplementedCommand() throws Exception { >+ URI workspaceLocation = createWorkspace(getMethodName()); >+ JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), gitDir.toString()); >+ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); >+ String gitCommandUri = gitSection.getString(GitConstants.KEY_COMMAND); >+ >+ WebRequest request = getPosGitCommandRequest(gitCommandUri, new String[] {"status"}); >+ WebResponse response = webConversation.getResponse(request); >+ assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode()); >+ } >+ >+ private static WebRequest getPosGitCommandRequest(String location, String[] args) throws UnsupportedEncodingException, JSONException { >+ String requestURI; >+ if (location.startsWith("http://")) >+ requestURI = location; >+ else if (location.startsWith("/")) >+ requestURI = SERVER_LOCATION + location; >+ else >+ requestURI = SERVER_LOCATION + GIT_SERVLET_LOCATION + Command.RESOURCE + location; >+ JSONObject body = new JSONObject(); >+ body.put(GitConstants.KEY_COMMAND_ARGS, Arrays.asList(args)); >+ >+ WebRequest request = new PostMethodWebRequest(requestURI, IOUtilities.toInputStream(body.toString()), "application/json"); >+ request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1"); >+ setAuthentication(request); >+ return request; >+ } >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 372119
: 212379 |
212380
|
212382