summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorPiotr Janik2011-06-01 08:23:17 (EDT)
committer Szymon Brandys2011-06-01 08:23:17 (EDT)
commit1652ad0af262a8b383222b9195666b363673663f (patch)
treeb2189e1a309701ec83a7598994f95902f8dc5913
parentd753008fefde5f9f8150785784a352116bf856d1 (diff)
downloadorg.eclipse.orion.server-1652ad0af262a8b383222b9195666b363673663f.zip
org.eclipse.orion.server-1652ad0af262a8b383222b9195666b363673663f.tar.gz
org.eclipse.orion.server-1652ad0af262a8b383222b9195666b363673663f.tar.bz2
bug 347906 - [server][git]Deleting Remote does not delete it
https://bugs.eclipse.org/bugs/show_bug.cgi?id=347906
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitRemoteHandlerV1.java13
-rw-r--r--tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitRemoteTest.java130
-rw-r--r--tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitTest.java12
3 files changed, 152 insertions, 3 deletions
diff --git a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitRemoteHandlerV1.java b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitRemoteHandlerV1.java
index c8040ae..aead2ac 100644
--- a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitRemoteHandlerV1.java
+++ b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitRemoteHandlerV1.java
@@ -174,7 +174,7 @@ public class GitRemoteHandlerV1 extends ServletResourceHandler<String> {
File gitDir = GitUtils.getGitDir(p.removeFirstSegments(1));
Repository db = new FileRepository(gitDir);
StoredConfig config = db.getConfig();
- config.unsetSection(Constants.R_REMOTES, remoteName);
+ config.unsetSection(ConfigConstants.CONFIG_REMOTE_SECTION, remoteName);
config.save();
//TODO: handle result
return true;
@@ -263,9 +263,10 @@ public class GitRemoteHandlerV1 extends ServletResourceHandler<String> {
config.save();
URI baseLocation = getURI(request);
- response.setHeader(ProtocolConstants.HEADER_LOCATION, BaseToRemoteConverter.REMOVE_FIRST_3.baseToRemoteLocation(baseLocation, p.segment(0), Repository.shortenRefName(remoteName)).toString());
+ JSONObject result = toJSON(remoteName, baseLocation);
+ OrionServlet.writeJSONResponse(request, response, result);
+ response.setHeader(ProtocolConstants.HEADER_LOCATION, result.getString(ProtocolConstants.KEY_LOCATION));
response.setStatus(HttpServletResponse.SC_CREATED);
- //TODO: handle result
return true;
}
@@ -308,4 +309,10 @@ public class GitRemoteHandlerV1 extends ServletResourceHandler<String> {
private URI createTaskLocation(URI baseLocation, String taskId) throws URISyntaxException {
return new URI(baseLocation.getScheme(), baseLocation.getAuthority(), "/task/id/" + taskId, null, null); //$NON-NLS-1$
}
+
+ private JSONObject toJSON(String remoteName, URI baseLocation) throws JSONException, URISyntaxException {
+ JSONObject result = new JSONObject();
+ result.put(ProtocolConstants.KEY_LOCATION, BaseToRemoteConverter.REMOVE_FIRST_2.baseToRemoteLocation(baseLocation, Repository.shortenRefName(remoteName), ""));
+ return result;
+ }
}
diff --git a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitRemoteTest.java b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitRemoteTest.java
index e5556a3..4c24377 100644
--- a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitRemoteTest.java
+++ b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitRemoteTest.java
@@ -14,23 +14,36 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
+import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URI;
+import java.net.URISyntaxException;
+import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.lib.StoredConfig;
+import org.eclipse.jgit.storage.file.FileRepository;
+import org.eclipse.jgit.transport.RefSpec;
+import org.eclipse.jgit.transport.RemoteConfig;
+import org.eclipse.jgit.transport.URIish;
import org.eclipse.orion.internal.server.servlets.ProtocolConstants;
import org.eclipse.orion.server.core.ServerStatus;
import org.eclipse.orion.server.git.GitConstants;
+import org.eclipse.orion.server.git.servlets.GitUtils;
+import org.eclipse.orion.server.tests.servlets.internal.DeleteMethodWebRequest;
import org.json.JSONArray;
+import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
+import org.xml.sax.SAXException;
import com.meterware.httpunit.GetMethodWebRequest;
+import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
@@ -247,6 +260,90 @@ public class GitRemoteTest extends GitTest {
assertNotNull(remoteBranch);
}
+ @Test
+ public void testAddRemoveRemote() throws Exception {
+ URI workspaceLocation = createWorkspace(getMethodName());
+
+ JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null);
+ JSONObject clone = clone(new Path("file").append(project.getString(ProtocolConstants.KEY_ID)).makeAbsolute());
+ String remotesLocation = clone.getString(GitConstants.KEY_REMOTE);
+
+ // expect only origin
+ getRemote(remotesLocation, 1, 0, Constants.DEFAULT_REMOTE_NAME);
+
+ // create remote
+ WebResponse response = addRemote(remotesLocation, "a", "https://a.b");
+ String remoteLocation = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);
+ String remoteLocationFromBody = new JSONObject(response.getText()).getString(ProtocolConstants.KEY_LOCATION);
+ assertEquals(remoteLocation, remoteLocationFromBody);
+
+ // check details
+ WebRequest request = getGetRequest(remoteLocation);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // list remotes
+ request = getGetRequest(remotesLocation);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject remotes = new JSONObject(response.getText());
+ JSONArray remotesArray = remotes.getJSONArray(ProtocolConstants.KEY_CHILDREN);
+ // expect origin and new remote
+ assertEquals(2, remotesArray.length());
+
+ // remove remote
+ request = getDeleteGitRemoteRequest(remoteLocation);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // list remotes again, make sure it's gone
+ request = getGetRequest(remotesLocation);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ remotes = new JSONObject(response.getText());
+ remotesArray = remotes.getJSONArray(ProtocolConstants.KEY_CHILDREN);
+ // expect origin only
+ assertEquals(1, remotesArray.length());
+ getRemote(remotesLocation, 1, 0, Constants.DEFAULT_REMOTE_NAME);
+ }
+
+ @Test
+ public void testRemoteProperties() throws IOException, SAXException, JSONException, CoreException, URISyntaxException {
+ URI workspaceLocation = createWorkspace(getMethodName());
+
+ JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null);
+ IPath path = new Path("file").append(project.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+ JSONObject clone = clone(path);
+ String remotesLocation = clone.getString(GitConstants.KEY_REMOTE);
+ String contentLocation = project.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ // create remote
+ final String remoteName = "remote1";
+ final String remoteUri = "remote1.com";
+ final String fetchRefSpec = "+refs/heads/*:refs/remotes/%s/*";
+ final String pushUri = "remote2.com";
+ final String pushRefSpec = "refs/heads/*:refs/heads/*";
+ WebResponse response = addRemote(remotesLocation, remoteName, remoteUri, fetchRefSpec, pushUri, pushRefSpec);
+
+ Repository db = new FileRepository(GitUtils.getGitDir(path));
+ StoredConfig config = db.getConfig();
+ RemoteConfig rc = new RemoteConfig(config, remoteName);
+
+ assertNotNull(rc);
+ // main uri
+ assertEquals(1, rc.getURIs().size());
+ assertEquals(new URIish(remoteUri), rc.getURIs().get(0));
+ // fetchRefSpec
+ assertEquals(1, rc.getFetchRefSpecs().size());
+ assertEquals(new RefSpec(fetchRefSpec), rc.getFetchRefSpecs().get(0));
+ // pushUri
+ assertEquals(1, rc.getPushURIs().size());
+ assertEquals(new URIish(pushUri), rc.getPushURIs().get(0));
+ // pushRefSpec
+ assertEquals(1, rc.getPushRefSpecs().size());
+ assertEquals(new RefSpec(pushRefSpec), rc.getPushRefSpecs().get(0));
+ }
+
static WebRequest getGetGitRemoteRequest(String location) {
String requestURI;
if (location.startsWith("http://"))
@@ -261,6 +358,39 @@ public class GitRemoteTest extends GitTest {
return request;
}
+ static WebRequest getPostGitRemoteRequest(String location, String name, String uri, String fetchRefSpec, String pushUri, String pushRefSpec) throws JSONException, UnsupportedEncodingException {
+ String requestURI;
+ if (location.startsWith("http://"))
+ requestURI = location;
+ else if (location.startsWith("/"))
+ requestURI = SERVER_LOCATION + location;
+ else
+ requestURI = SERVER_LOCATION + GIT_SERVLET_LOCATION + GitConstants.REMOTE_RESOURCE + location;
+ JSONObject body = new JSONObject();
+ body.put(GitConstants.KEY_REMOTE_NAME, name);
+ body.put(GitConstants.KEY_REMOTE_URI, uri);
+ body.put(GitConstants.KEY_REMOTE_FETCH_REF, fetchRefSpec);
+ body.put(GitConstants.KEY_REMOTE_PUSH_URI, pushUri);
+ body.put(GitConstants.KEY_REMOTE_PUSH_REF, pushRefSpec);
+ WebRequest request = new PostMethodWebRequest(requestURI, getJsonAsStream(body.toString()), "UTF-8");
+ request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
+ setAuthentication(request);
+ return request;
+ }
+
+ static WebRequest getDeleteGitRemoteRequest(String location) {
+ String requestURI;
+ if (location.startsWith("http://")) {
+ requestURI = location;
+ } else {
+ requestURI = SERVER_LOCATION + GIT_SERVLET_LOCATION + GitConstants.REMOTE_RESOURCE + location;
+ }
+ WebRequest request = new DeleteMethodWebRequest(requestURI);
+ request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
+ setAuthentication(request);
+ return request;
+ }
+
static void assertOnBranch(Git git, String branch) throws IOException {
assertNotNull(git.getRepository().getRef(branch));
}
diff --git a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitTest.java b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitTest.java
index 1d58286..9797d05 100644
--- a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitTest.java
+++ b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitTest.java
@@ -394,6 +394,18 @@ public abstract class GitTest extends FileSystemTest {
return remote;
}
+ protected WebResponse addRemote(String remotesLocation, String name, String uri) throws JSONException, IOException, SAXException {
+ return addRemote(remotesLocation, name, uri, null, null, null);
+ }
+
+ protected WebResponse addRemote(String remotesLocation, String name, String uri, String fetchRefSpec, String pushUri, String pushRefSpec) throws JSONException, IOException, SAXException {
+ assertRemoteUri(remotesLocation);
+ WebRequest request = GitRemoteTest.getPostGitRemoteRequest(remotesLocation, name, uri, fetchRefSpec, pushUri, pushRefSpec);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
+ return response;
+ }
+
protected JSONObject getRemoteBranch(String gitRemoteUri, int size, int i, String name) throws IOException, SAXException, JSONException {
assertRemoteUri(gitRemoteUri);
JSONObject remote = getRemote(gitRemoteUri, 1, 0, Constants.DEFAULT_REMOTE_NAME);