Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 372119 | Differences between
and this patch

Collapse All | Expand All

(-)a/bundles/org.eclipse.orion.server.git/META-INF/MANIFEST.MF (-2 / +4 lines)
Lines 10-23 Require-Bundle: org.eclipse.orion.server.servlets;bundle-version="0.1.0", Link Here
10
 org.eclipse.orion.server.core;bundle-version="0.2.0",
10
 org.eclipse.orion.server.core;bundle-version="0.2.0",
11
 org.json;bundle-version="1.0.0",
11
 org.json;bundle-version="1.0.0",
12
 org.eclipse.core.filesystem;bundle-version="1.3.100",
12
 org.eclipse.core.filesystem;bundle-version="1.3.100",
13
 org.eclipse.jgit;bundle-version="1.2.0",
13
 org.eclipse.jgit;bundle-version="1.3.0",
14
 org.kohsuke.args4j;bundle-version="2.0.12",
14
 org.eclipse.equinox.http.registry,
15
 org.eclipse.equinox.http.registry,
15
 com.jcraft.jsch;bundle-version="0.1.44",
16
 com.jcraft.jsch;bundle-version="0.1.44",
16
 org.eclipse.orion.server.useradmin;bundle-version="0.2.0",
17
 org.eclipse.orion.server.useradmin;bundle-version="0.2.0",
17
 org.eclipse.equinox.common;bundle-version="3.6.0",
18
 org.eclipse.equinox.common;bundle-version="3.6.0",
18
 org.eclipse.equinox.preferences;bundle-version="3.4.0",
19
 org.eclipse.equinox.preferences;bundle-version="3.4.0",
19
 org.eclipse.core.jobs;bundle-version="3.5.100",
20
 org.eclipse.core.jobs;bundle-version="3.5.100",
20
 org.apache.commons.httpclient;bundle-version="3.1.0"
21
 org.apache.commons.httpclient;bundle-version="3.1.0",
22
 org.eclipse.jgit.pgm;bundle-version="1.3.0"
21
Bundle-ActivationPolicy: lazy
23
Bundle-ActivationPolicy: lazy
22
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
24
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
23
Bundle-Vendor: %Bundle-Vendor
25
Bundle-Vendor: %Bundle-Vendor
(-)a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/GitConstants.java (+4 lines)
Lines 31-36 public class GitConstants { Link Here
31
31
32
	public static final String KEY_REMOTE = "RemoteLocation"; //$NON-NLS-1$
32
	public static final String KEY_REMOTE = "RemoteLocation"; //$NON-NLS-1$
33
33
34
	public static final String KEY_COMMAND = "CommandLocation"; //$NON-NLS-1$
35
34
	public static final String KEY_REPOSITORY_PATH = "RepositoryPath"; //$NON-NLS-1$
36
	public static final String KEY_REPOSITORY_PATH = "RepositoryPath"; //$NON-NLS-1$
35
37
36
	public static final String KEY_REPOSITORY_STATE = "RepositoryState"; //$NON-NLS-1$
38
	public static final String KEY_REPOSITORY_STATE = "RepositoryState"; //$NON-NLS-1$
Lines 172-175 public class GitConstants { Link Here
172
	public static final String KEY_TAG_COMMITS = "commits"; //$NON-NLS-1$
174
	public static final String KEY_TAG_COMMITS = "commits"; //$NON-NLS-1$
173
175
174
	public static final String KEY_TAG_TYPE = "TagType"; //$NON-NLS-1$
176
	public static final String KEY_TAG_TYPE = "TagType"; //$NON-NLS-1$
177
178
	public static final String KEY_COMMAND_ARGS = "Command"; //$NON-NLS-1$
175
}
179
}
(-)a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/GitFileDecorator.java (-1 / +6 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2011 IBM Corporation and others.
2
 * Copyright (c) 2011, 2012 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 159-164 public class GitFileDecorator implements IWebResourceDecorator { Link Here
159
		link = new URI(location.getScheme(), location.getAuthority(), request.getContextPath() + path.toString(), null, null);
159
		link = new URI(location.getScheme(), location.getAuthority(), request.getContextPath() + path.toString(), null, null);
160
		gitSection.put(GitConstants.KEY_TAG, link);
160
		gitSection.put(GitConstants.KEY_TAG, link);
161
161
162
		// add Git Command URI
163
		path = new Path(GitServlet.GIT_URI + '/' + Command.RESOURCE).append(targetPath);
164
		link = new URI(location.getScheme(), location.getAuthority(), request.getContextPath() + path.toString(), null, null);
165
		gitSection.put(GitConstants.KEY_COMMAND, link);
166
162
		// add Git Clone URI
167
		// add Git Clone URI
163
		gitSection.put(GitConstants.KEY_CLONE, cloneLocation);
168
		gitSection.put(GitConstants.KEY_CLONE, cloneLocation);
164
169
(-)a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/cli/CLIGitCommand.java (+60 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2012 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.orion.server.git.cli;
12
13
import java.io.*;
14
import java.util.ArrayList;
15
import java.util.List;
16
import org.eclipse.jgit.lib.Repository;
17
import org.eclipse.jgit.pgm.TextBuiltin;
18
import org.eclipse.jgit.pgm.opt.CmdLineParser;
19
import org.eclipse.jgit.pgm.opt.SubcommandHandler;
20
import org.kohsuke.args4j.Argument;
21
22
public class CLIGitCommand {
23
	@Argument(index = 0, metaVar = "metaVar_command", required = true, handler = SubcommandHandler.class)
24
	private TextBuiltin subcommand;
25
26
	@Argument(index = 1, metaVar = "metaVar_arg")
27
	private List<String> arguments = new ArrayList<String>();
28
29
	public TextBuiltin getSubcommand() {
30
		return subcommand;
31
	}
32
33
	public List<String> getArguments() {
34
		return arguments;
35
	}
36
37
	public static String execute(String[] argv, Repository db) throws Exception {
38
39
		CLIGitCommand bean = new CLIGitCommand();
40
		final CmdLineParser clp = new CmdLineParser(bean);
41
		clp.parseArgument(argv);
42
43
		final TextBuiltin cmd = bean.getSubcommand();
44
		if (cmd.requiresRepository())
45
			cmd.init(db, null);
46
		else
47
			cmd.init(null, null);
48
49
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
50
		cmd.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(baos)));
51
		try {
52
			cmd.execute(bean.getArguments().toArray(new String[bean.getArguments().size()]));
53
		} finally {
54
			if (cmd.out != null)
55
				cmd.out.flush();
56
		}
57
58
		return baos.toString();
59
	}
60
}
(-)a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/objects/Command.java (+37 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2012 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.orion.server.git.objects;
12
13
import java.net.URI;
14
import java.net.URISyntaxException;
15
import org.eclipse.jgit.lib.Repository;
16
17
public class Command extends GitObject {
18
19
	public static final String RESOURCE = "command"; //$NON-NLS-1$
20
	public static final String TYPE = "Command"; //$NON-NLS-1$
21
22
	Command(URI cloneLocation, Repository db) {
23
		super(cloneLocation, db);
24
	}
25
26
	@Override
27
	protected String getType() {
28
		return TYPE;
29
	}
30
31
	@Override
32
	protected URI getLocation() throws URISyntaxException {
33
		// TODO Auto-generated method stub
34
		return null;
35
	}
36
37
}
(-)a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitCommandHandlerV1.java (+87 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2012 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.orion.server.git.servlets;
12
13
import java.io.File;
14
import java.io.IOException;
15
import javax.servlet.ServletException;
16
import javax.servlet.http.HttpServletRequest;
17
import javax.servlet.http.HttpServletResponse;
18
import org.eclipse.core.runtime.*;
19
import org.eclipse.jgit.lib.Repository;
20
import org.eclipse.jgit.storage.file.FileRepository;
21
import org.eclipse.orion.internal.server.servlets.ServletResourceHandler;
22
import org.eclipse.orion.server.core.ServerStatus;
23
import org.eclipse.orion.server.git.GitConstants;
24
import org.eclipse.orion.server.git.cli.CLIGitCommand;
25
import org.eclipse.orion.server.servlets.OrionServlet;
26
import org.eclipse.osgi.util.NLS;
27
import org.json.*;
28
import org.kohsuke.args4j.CmdLineException;
29
30
public class GitCommandHandlerV1 extends ServletResourceHandler<String> {
31
	private ServletResourceHandler<IStatus> statusHandler;
32
33
	public GitCommandHandlerV1(ServletResourceHandler<IStatus> statusHandler) {
34
		this.statusHandler = statusHandler;
35
	}
36
37
	@SuppressWarnings("incomplete-switch")
38
	@Override
39
	public boolean handleRequest(HttpServletRequest request, HttpServletResponse response, String path) throws ServletException {
40
		try {
41
			switch (getMethod(request)) {
42
				case POST :
43
					return handlePost(request, response, path);
44
			}
45
		} catch (Exception e) {
46
			String msg = NLS.bind("Failed to handle 'command' request for {0}", path);
47
			return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e));
48
		}
49
		return false;
50
	}
51
52
	private boolean handlePost(HttpServletRequest request, HttpServletResponse response, String path) throws ServletException {
53
		IPath p = new Path(path);
54
		try {
55
			File gitDir = GitUtils.getGitDir(p);
56
			Repository db = new FileRepository(gitDir);
57
			String args[] = readArgs(request);
58
			String result = CLIGitCommand.execute(args, db);
59
			writeTextResponse(request, response, result);
60
			return true;
61
		} catch (CmdLineException e) {
62
			return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, NLS.bind("The command cannot be executed: {0}", e.getMessage()), e));
63
		} catch (Exception e) {
64
			return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when procesing command.", e));
65
		}
66
	}
67
68
	private String[] readArgs(final HttpServletRequest request) throws IOException, JSONException {
69
		JSONObject commandArgs = OrionServlet.readJSONRequest(request);
70
		JSONArray jsonArray = commandArgs.getJSONArray(GitConstants.KEY_COMMAND_ARGS);
71
		String[] args = new String[jsonArray.length()];
72
		for (int i = 0; i < jsonArray.length(); i++) {
73
			args[i] = jsonArray.getString(i);
74
		}
75
		return args;
76
	}
77
78
	private static void writeTextResponse(HttpServletRequest req, HttpServletResponse resp, String result) throws IOException {
79
		resp.setStatus(HttpServletResponse.SC_OK);
80
		resp.setHeader("Cache-Control", "no-cache"); //$NON-NLS-1$ //$NON-NLS-2$
81
		resp.setHeader("Cache-Control", "no-store"); //$NON-NLS-1$ //$NON-NLS-2$
82
		//TODO look at accept header and chose appropriate response representation
83
		resp.setContentType("text/html;charset=UTF-8" /* ProtocolConstants.CONTENT_TYPE_TEXT */); //$NON-NLS-1$
84
		resp.getWriter().print(result);
85
	}
86
87
}
(-)a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitHandlerV1.java (-1 / +5 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2011 IBM Corporation and others.
2
 * Copyright (c) 2011, 2012 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 32-41 public class GitHandlerV1 extends ServletResourceHandler<String> { Link Here
32
	private ServletResourceHandler<String> remoteHandlerV1;
32
	private ServletResourceHandler<String> remoteHandlerV1;
33
	private ServletResourceHandler<String> statusHandlerV1;
33
	private ServletResourceHandler<String> statusHandlerV1;
34
	private ServletResourceHandler<String> tagHandlerV1;
34
	private ServletResourceHandler<String> tagHandlerV1;
35
	private ServletResourceHandler<String> commandHandlerV1;
35
36
36
	GitHandlerV1(ServletResourceHandler<IStatus> statusHandler) {
37
	GitHandlerV1(ServletResourceHandler<IStatus> statusHandler) {
37
		branchHandlerV1 = new GitBranchHandlerV1(statusHandler);
38
		branchHandlerV1 = new GitBranchHandlerV1(statusHandler);
38
		cloneHandlerV1 = new GitCloneHandlerV1(statusHandler);
39
		cloneHandlerV1 = new GitCloneHandlerV1(statusHandler);
40
		commandHandlerV1 = new GitCommandHandlerV1(statusHandler);
39
		commitHandlerV1 = new GitCommitHandlerV1(statusHandler);
41
		commitHandlerV1 = new GitCommitHandlerV1(statusHandler);
40
		configHandlerV1 = new GitConfigHandlerV1(statusHandler);
42
		configHandlerV1 = new GitConfigHandlerV1(statusHandler);
41
		diffHandlerV1 = new GitDiffHandlerV1(statusHandler);
43
		diffHandlerV1 = new GitDiffHandlerV1(statusHandler);
Lines 63-68 public class GitHandlerV1 extends ServletResourceHandler<String> { Link Here
63
			return branchHandlerV1.handleRequest(request, response, pathString);
65
			return branchHandlerV1.handleRequest(request, response, pathString);
64
		} else if (infoParts[1].equals(Clone.RESOURCE)) {
66
		} else if (infoParts[1].equals(Clone.RESOURCE)) {
65
			return cloneHandlerV1.handleRequest(request, response, pathString);
67
			return cloneHandlerV1.handleRequest(request, response, pathString);
68
		} else if (infoParts[1].equals(Command.RESOURCE)) {
69
			return commandHandlerV1.handleRequest(request, response, pathString);
66
		} else if (infoParts[1].equals(Commit.RESOURCE)) {
70
		} else if (infoParts[1].equals(Commit.RESOURCE)) {
67
			return commitHandlerV1.handleRequest(request, response, pathString);
71
			return commitHandlerV1.handleRequest(request, response, pathString);
68
		} else if (infoParts[1].equals(ConfigOption.RESOURCE)) {
72
		} else if (infoParts[1].equals(ConfigOption.RESOURCE)) {
(-)a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitCommandTest.java (+94 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2012 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.orion.server.tests.servlets.git;
12
13
import static org.junit.Assert.assertEquals;
14
15
import java.io.UnsupportedEncodingException;
16
import java.net.HttpURLConnection;
17
import java.net.URI;
18
import java.util.Arrays;
19
20
import org.eclipse.orion.internal.server.core.IOUtilities;
21
import org.eclipse.orion.internal.server.servlets.ProtocolConstants;
22
import org.eclipse.orion.server.git.GitConstants;
23
import org.eclipse.orion.server.git.objects.Command;
24
import org.json.JSONException;
25
import org.json.JSONObject;
26
import org.junit.Test;
27
28
import com.meterware.httpunit.PostMethodWebRequest;
29
import com.meterware.httpunit.WebRequest;
30
import com.meterware.httpunit.WebResponse;
31
32
public class GitCommandTest extends GitTest {
33
34
	@Test
35
	public void command() throws Exception {
36
		URI workspaceLocation = createWorkspace(getMethodName());
37
		JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), gitDir.toString());
38
		JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
39
		String gitCommandUri = gitSection.getString(GitConstants.KEY_COMMAND);
40
41
		WebRequest request = getPosGitCommandRequest(gitCommandUri, new String[] {"branch"});
42
		WebResponse response = webConversation.getResponse(request);
43
		assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
44
		String commandResult = response.getText();
45
46
		String expectedResult = "* master\r\n";
47
		assertEquals(expectedResult, commandResult);
48
	}
49
50
	@Test
51
	public void commandWithArgs() throws Exception {
52
		URI workspaceLocation = createWorkspace(getMethodName());
53
		JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), gitDir.toString());
54
		JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
55
		String gitCommandUri = gitSection.getString(GitConstants.KEY_COMMAND);
56
57
		WebRequest request = getPosGitCommandRequest(gitCommandUri, new String[] {"commit", "--amend", "-m", "'Amended commit'"});
58
		WebResponse response = webConversation.getResponse(request);
59
		assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
60
		String commandResult = response.getText();
61
62
		String expectedResult = "[master ecf5161e231e3f798ec9ae0910264bd5c85a4c37] 'Amended commit'\r\n";
63
		assertEquals(expectedResult, commandResult);
64
	}
65
66
	@Test
67
	public void notImplementedCommand() throws Exception {
68
		URI workspaceLocation = createWorkspace(getMethodName());
69
		JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), gitDir.toString());
70
		JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
71
		String gitCommandUri = gitSection.getString(GitConstants.KEY_COMMAND);
72
73
		WebRequest request = getPosGitCommandRequest(gitCommandUri, new String[] {"status"});
74
		WebResponse response = webConversation.getResponse(request);
75
		assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode());
76
	}
77
78
	private static WebRequest getPosGitCommandRequest(String location, String[] args) throws UnsupportedEncodingException, JSONException {
79
		String requestURI;
80
		if (location.startsWith("http://"))
81
			requestURI = location;
82
		else if (location.startsWith("/"))
83
			requestURI = SERVER_LOCATION + location;
84
		else
85
			requestURI = SERVER_LOCATION + GIT_SERVLET_LOCATION + Command.RESOURCE + location;
86
		JSONObject body = new JSONObject();
87
		body.put(GitConstants.KEY_COMMAND_ARGS, Arrays.asList(args));
88
89
		WebRequest request = new PostMethodWebRequest(requestURI, IOUtilities.toInputStream(body.toString()), "application/json");
90
		request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
91
		setAuthentication(request);
92
		return request;
93
	}
94
}

Return to bug 372119