summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorPiotr Janik2011-07-20 06:19:04 (EDT)
committer Tomasz Zarna2011-07-20 06:19:04 (EDT)
commit85a59817b9f379e4ce823f3d17a44b10cd9b698d (patch)
tree18e3e9b1686ea6fae22b6a7c86a59ff6ecc324ea
parent730841c9fe461b279da8d036bbb4444bc54ed3e3 (diff)
downloadorg.eclipse.orion.server-85a59817b9f379e4ce823f3d17a44b10cd9b698d.zip
org.eclipse.orion.server-85a59817b9f379e4ce823f3d17a44b10cd9b698d.tar.gz
org.eclipse.orion.server-85a59817b9f379e4ce823f3d17a44b10cd9b698d.tar.bz2
bug 351440 - '+' in refspecs seems to not be respected during push/fetch
operations (server side) https://bugs.eclipse.org/bugs/show_bug.cgi?id=351440
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/GitConstants.java2
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/FetchJob.java38
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitRemoteHandlerV1.java13
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/PushJob.java5
-rw-r--r--tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitFetchTest.java127
-rw-r--r--tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitMergeTest.java2
-rw-r--r--tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitPushTest.java111
-rw-r--r--tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitTest.java62
8 files changed, 335 insertions, 25 deletions
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 930f6c0..b73057b 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
@@ -159,6 +159,8 @@ public class GitConstants {
public static final String KEY_PUSH_TAGS = "PushTags"; //$NON-NLS-1$
+ public static final String KEY_FORCE = "Force"; //$NON-NLS-1$
+
public static final String KEY_RESULT = "Result"; //$NON-NLS-1$
public static final String KEY_TAG_COMMIT = "Commit"; //$NON-NLS-1$
diff --git a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/FetchJob.java b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/FetchJob.java
index 861f0ea..96cf46c 100644
--- a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/FetchJob.java
+++ b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/FetchJob.java
@@ -17,8 +17,8 @@ import org.eclipse.jgit.api.FetchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.JGitInternalException;
-import org.eclipse.jgit.lib.Constants;
-import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.lib.*;
+import org.eclipse.jgit.lib.RefUpdate.Result;
import org.eclipse.jgit.storage.file.FileRepository;
import org.eclipse.jgit.transport.*;
import org.eclipse.orion.server.core.tasks.ITaskService;
@@ -39,14 +39,16 @@ public class FetchJob extends GitJob {
private ServiceReference<ITaskService> taskServiceRef;
private IPath path;
private String remote;
+ private boolean force;
- public FetchJob(CredentialsProvider credentials, Path path) {
+ public FetchJob(CredentialsProvider credentials, Path path, boolean force) {
super("Fetching", (GitCredentialsProvider) credentials); //$NON-NLS-1$
// path: {remote}[/{branch}]/file/{...}
this.path = path;
this.remote = path.segment(0);
this.task = createTask();
+ this.force = force;
}
private TaskInfo createTask() {
@@ -56,7 +58,7 @@ public class FetchJob extends GitJob {
return info;
}
- private void doFetch() throws IOException, CoreException, JGitInternalException, InvalidRemoteException, URISyntaxException {
+ private IStatus doFetch() throws IOException, CoreException, JGitInternalException, InvalidRemoteException, URISyntaxException {
Repository db = getRepository();
String branch = getRemoteBranch();
@@ -71,9 +73,33 @@ public class FetchJob extends GitJob {
if (branch != null) {
// refs/heads/{branch}:refs/remotes/{remote}/{branch}
RefSpec spec = new RefSpec(Constants.R_HEADS + branch + ":" + Constants.R_REMOTES + remote + "/" + branch); //$NON-NLS-1$ //$NON-NLS-2$
+ spec = spec.setForceUpdate(force);
fc.setRefSpecs(spec);
}
- fc.call();
+ FetchResult fetchResult = fc.call();
+
+ // handle result
+ for (TrackingRefUpdate updateRes : fetchResult.getTrackingRefUpdates()) {
+ Result res = updateRes.getResult();
+ // handle status for given ref
+ switch (res) {
+ case NOT_ATTEMPTED :
+ case NO_CHANGE :
+ case NEW :
+ case FORCED :
+ case FAST_FORWARD :
+ case RENAMED :
+ // do nothing, as these statuses are OK
+ break;
+ case REJECTED :
+ case REJECTED_CURRENT_BRANCH :
+ // show warning, as only force fetch can finish successfully
+ return new Status(IStatus.WARNING, GitActivator.PI_GIT, res.name());
+ default :
+ return new Status(IStatus.ERROR, GitActivator.PI_GIT, res.name());
+ }
+ }
+ return Status.OK_STATUS;
}
private Repository getRepository() throws IOException, CoreException {
@@ -111,7 +137,7 @@ public class FetchJob extends GitJob {
protected IStatus run(IProgressMonitor monitor) {
IStatus result = Status.OK_STATUS;
try {
- doFetch();
+ result = doFetch();
} catch (IOException e) {
result = new Status(IStatus.ERROR, GitActivator.PI_GIT, "Error fetching git remote", e);
} catch (CoreException e) {
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 28f5c2f..54ce031 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
@@ -198,6 +198,7 @@ public class GitRemoteHandlerV1 extends ServletResourceHandler<String> {
boolean fetch = Boolean.parseBoolean(requestObject.optString(GitConstants.KEY_FETCH, null));
String srcRef = requestObject.optString(GitConstants.KEY_PUSH_SRC_REF, null);
boolean tags = requestObject.optBoolean(GitConstants.KEY_PUSH_TAGS, false);
+ boolean force = requestObject.optBoolean(GitConstants.KEY_FORCE, false);
// prepare creds
String username = requestObject.optString(GitConstants.KEY_USERNAME, null);
@@ -214,12 +215,12 @@ public class GitRemoteHandlerV1 extends ServletResourceHandler<String> {
// if all went well, continue with fetch or push
if (fetch) {
- return fetch(request, response, cp, path);
+ return fetch(request, response, cp, path, force);
} else if (srcRef != null) {
if (srcRef.equals("")) { //$NON-NLS-1$
return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Pushing with an empty source ref is not allowed. Did you mean DELETE?", null));
}
- return push(request, response, path, cp, srcRef, tags);
+ return push(request, response, path, cp, srcRef, tags, force);
} else {
return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Only Fetch:true is currently supported.", null));
}
@@ -275,10 +276,10 @@ public class GitRemoteHandlerV1 extends ServletResourceHandler<String> {
return true;
}
- private boolean fetch(HttpServletRequest request, HttpServletResponse response, GitCredentialsProvider cp, String path) throws URISyntaxException, JSONException, IOException {
+ private boolean fetch(HttpServletRequest request, HttpServletResponse response, GitCredentialsProvider cp, String path, boolean force) throws URISyntaxException, JSONException, IOException {
// {remote}/{branch}/{file}/{path}
Path p = new Path(path);
- FetchJob job = new FetchJob(cp, p);
+ FetchJob job = new FetchJob(cp, p, force);
job.schedule();
TaskInfo task = job.getTask();
@@ -291,12 +292,12 @@ public class GitRemoteHandlerV1 extends ServletResourceHandler<String> {
return true;
}
- private boolean push(HttpServletRequest request, HttpServletResponse response, String path, GitCredentialsProvider cp, String srcRef, boolean tags) throws ServletException, CoreException, IOException, JSONException, URISyntaxException {
+ private boolean push(HttpServletRequest request, HttpServletResponse response, String path, GitCredentialsProvider cp, String srcRef, boolean tags, boolean force) throws ServletException, CoreException, IOException, JSONException, URISyntaxException {
Path p = new Path(path);
// FIXME: what if a remote or branch is named "file"?
if (p.segment(2).equals("file")) { //$NON-NLS-1$
// /git/remote/{remote}/{branch}/file/{path}
- PushJob job = new PushJob(cp, p, srcRef, tags);
+ PushJob job = new PushJob(cp, p, srcRef, tags, force);
job.schedule();
TaskInfo task = job.getTask();
diff --git a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/PushJob.java b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/PushJob.java
index 82c6f9b..077ddba 100644
--- a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/PushJob.java
+++ b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/PushJob.java
@@ -44,14 +44,16 @@ public class PushJob extends GitJob {
private Path p;
private String srcRef;
private boolean tags;
+ private boolean force;
- public PushJob(CredentialsProvider credentials, Path path, String srcRef, boolean tags) {
+ public PushJob(CredentialsProvider credentials, Path path, String srcRef, boolean tags, boolean force) {
super("Pushing", (GitCredentialsProvider) credentials); //$NON-NLS-1$
this.p = path;
this.srcRef = srcRef;
this.tags = tags;
this.task = createTask();
+ this.force = force;
}
private TaskInfo createTask() {
@@ -78,6 +80,7 @@ public class PushJob extends GitJob {
pushCommand.setRemote(p.segment(0)).setRefSpecs(spec);
if (tags)
pushCommand.setPushTags();
+ pushCommand.setForce(force);
Iterable<PushResult> resultIterable = pushCommand.call();
PushResult pushResult = resultIterable.iterator().next();
// this set will contain only OK status or UP_TO_DATE status
diff --git a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitFetchTest.java b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitFetchTest.java
index 03f5933..09f4a0b 100644
--- a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitFetchTest.java
+++ b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitFetchTest.java
@@ -20,12 +20,14 @@ import java.net.HttpURLConnection;
import java.net.URI;
import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.transport.RemoteRefUpdate.Status;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.orion.internal.server.servlets.ProtocolConstants;
import org.eclipse.orion.server.core.ServerStatus;
@@ -530,7 +532,126 @@ public class GitFetchTest extends GitTest {
}
- static WebRequest getPostGitRemoteRequest(String location, boolean fetch) throws JSONException, UnsupportedEncodingException {
+ @Test
+ public void testForcedFetch() throws Exception {
+ URI workspaceLocation = createWorkspace(getMethodName());
+ JSONObject projectTop1 = createProjectOrLink(workspaceLocation, getMethodName() + "-top1", null);
+ IPath clonePathTop1 = new Path("file").append(projectTop1.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+
+ JSONObject projectTop2 = createProjectOrLink(workspaceLocation, getMethodName() + "-top2", null);
+ IPath clonePathTop2 = new Path("file").append(projectTop2.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+
+ JSONObject projectFolder1 = createProjectOrLink(workspaceLocation, getMethodName() + "-folder1", null);
+ IPath clonePathFolder1 = new Path("file").append(projectFolder1.getString(ProtocolConstants.KEY_ID)).append("folder1").makeAbsolute();
+
+ JSONObject projectFolder2 = createProjectOrLink(workspaceLocation, getMethodName() + "-folder2", null);
+ IPath clonePathFolder2 = new Path("file").append(projectFolder2.getString(ProtocolConstants.KEY_ID)).append("folder2").makeAbsolute();
+
+ JSONObject projectTop3 = createProjectOrLink(workspaceLocation, getMethodName() + "-top3", null);
+ IPath clonePathTop3 = new Path("file").append(projectTop3.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+
+ JSONObject projectFolder3 = createProjectOrLink(workspaceLocation, getMethodName() + "-folder3", null);
+ IPath clonePathFolder3 = new Path("file").append(projectFolder3.getString(ProtocolConstants.KEY_ID)).append("folder1").makeAbsolute();
+
+ IPath[] clonePathsTop = new IPath[] {clonePathTop1, clonePathTop2};
+ IPath[] clonePathsFolder = new IPath[] {clonePathFolder1, clonePathFolder2};
+ IPath[] clonePathsMixed = new IPath[] {clonePathTop3, clonePathFolder3};
+ IPath[][] clonePaths = new IPath[][] {clonePathsTop, clonePathsFolder, clonePathsMixed};
+
+ for (IPath[] clonePath : clonePaths) {
+ // clone1
+ String contentLocation1 = clone(clonePath[0]).getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ // get project1 metadata
+ WebRequest request = getGetFilesRequest(contentLocation1);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject project1 = new JSONObject(response.getText());
+ String project1Location = project1.getString(ProtocolConstants.KEY_LOCATION);
+ JSONObject gitSection1 = project1.getJSONObject(GitConstants.KEY_GIT);
+ String gitRemoteUri1 = gitSection1.getString(GitConstants.KEY_REMOTE);
+ String gitIndexUri1 = gitSection1.getString(GitConstants.KEY_INDEX);
+ String gitHeadUri1 = gitSection1.getString(GitConstants.KEY_HEAD);
+
+ // clone2
+ String contentLocation2 = clone(clonePath[1]).getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ // get project2 metadata
+ request = getGetFilesRequest(contentLocation2);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject project2 = new JSONObject(response.getText());
+ String project2Location = project2.getString(ProtocolConstants.KEY_LOCATION);
+ JSONObject gitSection2 = project2.getJSONObject(GitConstants.KEY_GIT);
+ String gitRemoteUri2 = gitSection2.getString(GitConstants.KEY_REMOTE);
+ String gitIndexUri2 = gitSection2.getString(GitConstants.KEY_INDEX);
+ String gitHeadUri2 = gitSection2.getString(GitConstants.KEY_HEAD);
+
+ // clone1: change
+ request = getPutFileRequest(project1Location + "/test.txt", "clone1 change");
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // clone1: add
+ request = GitAddTest.getPutGitIndexRequest(gitIndexUri1);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // clone1: commit
+ request = GitCommitTest.getPostGitCommitRequest(gitHeadUri1, "clone1 change commit", false);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // clone1: push
+ ServerStatus pushStatus = push(gitRemoteUri1, 1, 0, Constants.MASTER, Constants.HEAD, false);
+ assertEquals(true, pushStatus.isOK());
+
+ // clone2: change
+ request = getPutFileRequest(project2Location + "/test.txt", "clone2 change");
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // clone2: add
+ request = GitAddTest.getPutGitIndexRequest(gitIndexUri2);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // clone2: commit
+ request = GitCommitTest.getPostGitCommitRequest(gitHeadUri2, "clone2 change commit", false);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // clone2: push
+ pushStatus = push(gitRemoteUri2, 1, 0, Constants.MASTER, Constants.HEAD, false);
+ assertEquals(IStatus.WARNING, pushStatus.getSeverity());
+ Status pushResult = Status.valueOf(pushStatus.getMessage());
+ assertEquals(Status.REJECTED_NONFASTFORWARD, pushResult);
+
+ // clone2: forced push
+ pushStatus = push(gitRemoteUri2, 1, 0, Constants.MASTER, Constants.HEAD, false, true);
+ assertEquals(true, pushStatus.isOK());
+
+ // clone1: fetch master
+ JSONObject details = getRemoteBranch(gitRemoteUri1, 1, 0, Constants.MASTER);
+ String remoteBranchLocation = details.getString(ProtocolConstants.KEY_LOCATION);
+ String oldRefId = details.getString(ProtocolConstants.KEY_ID);
+
+ JSONObject newDetails = fetch(remoteBranchLocation);
+
+ // assert nothing new on 'master'
+ String newRefId = newDetails.getString(ProtocolConstants.KEY_ID);
+ assertEquals(oldRefId, newRefId);
+
+ // clone1: forced fetch master
+ newDetails = fetch(remoteBranchLocation, true);
+
+ // assert that fetch succeed and something new on 'master'
+ newRefId = newDetails.getString(ProtocolConstants.KEY_ID);
+ assertFalse(oldRefId.equals(newRefId));
+ }
+ }
+
+ static WebRequest getPostGitRemoteRequest(String location, boolean fetch, boolean force) throws JSONException, UnsupportedEncodingException {
String requestURI;
if (location.startsWith("http://"))
requestURI = location;
@@ -539,13 +660,14 @@ public class GitFetchTest extends GitTest {
JSONObject body = new JSONObject();
body.put(GitConstants.KEY_FETCH, Boolean.toString(fetch));
+ body.put(GitConstants.KEY_FORCE, force);
WebRequest request = new PostMethodWebRequest(requestURI, getJsonAsStream(body.toString()), "UTF-8");
request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
setAuthentication(request);
return request;
}
- static WebRequest getPostGitRemoteRequest(String location, boolean fetch, String name, String kh, byte[] privk, byte[] pubk, byte[] p) throws JSONException, UnsupportedEncodingException {
+ static WebRequest getPostGitRemoteRequest(String location, boolean fetch, boolean force, String name, String kh, byte[] privk, byte[] pubk, byte[] p) throws JSONException, UnsupportedEncodingException {
String requestURI;
if (location.startsWith("http://"))
requestURI = location;
@@ -564,6 +686,7 @@ public class GitFetchTest extends GitTest {
body.put(GitConstants.KEY_PASSPHRASE, new String(p));
body.put(GitConstants.KEY_FETCH, Boolean.toString(fetch));
+ body.put(GitConstants.KEY_FORCE, force);
WebRequest request = new PostMethodWebRequest(requestURI, getJsonAsStream(body.toString()), "UTF-8");
request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
setAuthentication(request);
diff --git a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitMergeTest.java b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitMergeTest.java
index 10d9c74..17e8ae8 100644
--- a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitMergeTest.java
+++ b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitMergeTest.java
@@ -424,7 +424,7 @@ public class GitMergeTest extends GitTest {
assertEquals(true, pushStatus.isOK());
// clone1: fetch
- request = GitFetchTest.getPostGitRemoteRequest(remoteBranchLocation1, true);
+ request = GitFetchTest.getPostGitRemoteRequest(remoteBranchLocation1, true, false);
response = webConversation.getResponse(request);
assertEquals(HttpURLConnection.HTTP_ACCEPTED, response.getResponseCode());
String taskLocation = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);
diff --git a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitPushTest.java b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitPushTest.java
index 0a0df55..a836b89 100644
--- a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitPushTest.java
+++ b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitPushTest.java
@@ -70,7 +70,7 @@ public class GitPushTest extends GitTest {
String remoteBranchLocation = remoteBranch.getString(ProtocolConstants.KEY_LOCATION);
// push with no body
- request = getPostGitRemoteRequest(remoteBranchLocation, null, false);
+ request = getPostGitRemoteRequest(remoteBranchLocation, null, false, false);
response = webConversation.getResponse(request);
assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode());
}
@@ -427,7 +427,7 @@ public class GitPushTest extends GitTest {
String remoteBranchLocation = logResponse.getString(GitConstants.KEY_REMOTE);
// push
- request = getPostGitRemoteRequest(remoteBranchLocation, Constants.HEAD, false);
+ request = getPostGitRemoteRequest(remoteBranchLocation, Constants.HEAD, false, false);
response = webConversation.getResponse(request);
assertEquals(HttpURLConnection.HTTP_ACCEPTED, response.getResponseCode());
}
@@ -512,6 +512,107 @@ public class GitPushTest extends GitTest {
}
@Test
+ public void testForcedPush() throws Exception {
+ URI workspaceLocation = createWorkspace(getMethodName());
+ JSONObject projectTop1 = createProjectOrLink(workspaceLocation, getMethodName() + "-top1", null);
+ IPath clonePathTop1 = new Path("file").append(projectTop1.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+
+ JSONObject projectTop2 = createProjectOrLink(workspaceLocation, getMethodName() + "-top2", null);
+ IPath clonePathTop2 = new Path("file").append(projectTop2.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+
+ JSONObject projectFolder1 = createProjectOrLink(workspaceLocation, getMethodName() + "-folder1", null);
+ IPath clonePathFolder1 = new Path("file").append(projectFolder1.getString(ProtocolConstants.KEY_ID)).append("folder1").makeAbsolute();
+
+ JSONObject projectFolder2 = createProjectOrLink(workspaceLocation, getMethodName() + "-folder2", null);
+ IPath clonePathFolder2 = new Path("file").append(projectFolder2.getString(ProtocolConstants.KEY_ID)).append("folder2").makeAbsolute();
+
+ JSONObject projectTop3 = createProjectOrLink(workspaceLocation, getMethodName() + "-top3", null);
+ IPath clonePathTop3 = new Path("file").append(projectTop3.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+
+ JSONObject projectFolder3 = createProjectOrLink(workspaceLocation, getMethodName() + "-folder3", null);
+ IPath clonePathFolder3 = new Path("file").append(projectFolder3.getString(ProtocolConstants.KEY_ID)).append("folder1").makeAbsolute();
+
+ IPath[] clonePathsTop = new IPath[] {clonePathTop1, clonePathTop2};
+ IPath[] clonePathsFolder = new IPath[] {clonePathFolder1, clonePathFolder2};
+ IPath[] clonePathsMixed = new IPath[] {clonePathTop3, clonePathFolder3};
+ IPath[][] clonePaths = new IPath[][] {clonePathsTop, clonePathsFolder, clonePathsMixed};
+
+ for (IPath[] clonePath : clonePaths) {
+ // clone1
+ String contentLocation1 = clone(clonePath[0]).getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ // get project1 metadata
+ WebRequest request = getGetFilesRequest(contentLocation1);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject project1 = new JSONObject(response.getText());
+ String project1Location = project1.getString(ProtocolConstants.KEY_LOCATION);
+ JSONObject gitSection1 = project1.getJSONObject(GitConstants.KEY_GIT);
+ String gitRemoteUri1 = gitSection1.getString(GitConstants.KEY_REMOTE);
+ String gitIndexUri1 = gitSection1.getString(GitConstants.KEY_INDEX);
+ String gitHeadUri1 = gitSection1.getString(GitConstants.KEY_HEAD);
+
+ // clone2
+ String contentLocation2 = clone(clonePath[1]).getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ // get project2 metadata
+ request = getGetFilesRequest(contentLocation2);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject project2 = new JSONObject(response.getText());
+ String project2Location = project2.getString(ProtocolConstants.KEY_LOCATION);
+ JSONObject gitSection2 = project2.getJSONObject(GitConstants.KEY_GIT);
+ String gitRemoteUri2 = gitSection2.getString(GitConstants.KEY_REMOTE);
+ String gitIndexUri2 = gitSection2.getString(GitConstants.KEY_INDEX);
+ String gitHeadUri2 = gitSection2.getString(GitConstants.KEY_HEAD);
+
+ // clone1: change
+ request = getPutFileRequest(project1Location + "/test.txt", "clone1 change");
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // clone1: add
+ request = GitAddTest.getPutGitIndexRequest(gitIndexUri1);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // clone1: commit
+ request = GitCommitTest.getPostGitCommitRequest(gitHeadUri1, "clone1 change commit", false);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // clone1: push
+ ServerStatus pushStatus = push(gitRemoteUri1, 1, 0, Constants.MASTER, Constants.HEAD, false);
+ assertEquals(true, pushStatus.isOK());
+
+ // clone2: change
+ request = getPutFileRequest(project2Location + "/test.txt", "clone2 change");
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // clone2: add
+ request = GitAddTest.getPutGitIndexRequest(gitIndexUri2);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // clone2: commit
+ request = GitCommitTest.getPostGitCommitRequest(gitHeadUri2, "clone2 change commit", false);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // clone2: push
+ pushStatus = push(gitRemoteUri2, 1, 0, Constants.MASTER, Constants.HEAD, false);
+ assertEquals(IStatus.WARNING, pushStatus.getSeverity());
+ Status pushResult = Status.valueOf(pushStatus.getMessage());
+ assertEquals(Status.REJECTED_NONFASTFORWARD, pushResult);
+
+ // clone2: forced push
+ pushStatus = push(gitRemoteUri2, 1, 0, Constants.MASTER, Constants.HEAD, false, true);
+ assertEquals(true, pushStatus.isOK());
+ }
+ }
+
+ @Test
public void testPushTags() throws Exception {
URI workspaceLocation = createWorkspace(getMethodName());
@@ -570,7 +671,7 @@ public class GitPushTest extends GitTest {
assertEquals(1, tags.length());
}
- static WebRequest getPostGitRemoteRequest(String location, String srcRef, boolean tags) throws JSONException, UnsupportedEncodingException {
+ static WebRequest getPostGitRemoteRequest(String location, String srcRef, boolean tags, boolean force) throws JSONException, UnsupportedEncodingException {
String requestURI;
if (location.startsWith("http://"))
requestURI = location;
@@ -581,13 +682,14 @@ public class GitPushTest extends GitTest {
if (srcRef != null)
body.put(GitConstants.KEY_PUSH_SRC_REF, srcRef);
body.put(GitConstants.KEY_PUSH_TAGS, tags);
+ body.put(GitConstants.KEY_FORCE, force);
WebRequest request = new PostMethodWebRequest(requestURI, getJsonAsStream(body.toString()), "UTF-8");
request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
setAuthentication(request);
return request;
}
- static WebRequest getPostGitRemoteRequest(String location, String srcRef, boolean tags, String name, String kh, byte[] privk, byte[] pubk, byte[] p) throws JSONException, UnsupportedEncodingException {
+ static WebRequest getPostGitRemoteRequest(String location, String srcRef, boolean tags, boolean force, String name, String kh, byte[] privk, byte[] pubk, byte[] p) throws JSONException, UnsupportedEncodingException {
String requestURI;
if (location.startsWith("http://"))
requestURI = location;
@@ -609,6 +711,7 @@ public class GitPushTest extends GitTest {
if (srcRef != null)
body.put(GitConstants.KEY_PUSH_SRC_REF, srcRef);
body.put(GitConstants.KEY_PUSH_TAGS, tags);
+ body.put(GitConstants.KEY_FORCE, force);
WebRequest request = new PostMethodWebRequest(requestURI, getJsonAsStream(body.toString()), "UTF-8");
request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
setAuthentication(request);
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 19ad99e..0aa7150 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
@@ -452,9 +452,13 @@ public abstract class GitTest extends FileSystemTest {
// push
protected ServerStatus push(String gitRemoteUri, int size, int i, String name, String srcRef, boolean tags) throws IOException, SAXException, JSONException {
+ return push(gitRemoteUri, size, i, name, srcRef, tags, false);
+ }
+
+ protected ServerStatus push(String gitRemoteUri, int size, int i, String name, String srcRef, boolean tags, boolean force) throws IOException, SAXException, JSONException {
assertRemoteUri(gitRemoteUri);
String remoteBranchLocation = getRemoteBranch(gitRemoteUri, size, i, name).getString(ProtocolConstants.KEY_LOCATION);
- return push(remoteBranchLocation, srcRef, tags);
+ return push(remoteBranchLocation, srcRef, tags, force);
}
/**
@@ -469,8 +473,24 @@ public abstract class GitTest extends FileSystemTest {
* @throws JSONException
*/
protected ServerStatus push(String gitRemoteBranchUri, String srcRef, boolean tags) throws IOException, SAXException, JSONException {
+ return push(gitRemoteBranchUri, srcRef, tags, false);
+ }
+
+ /**
+ * Pushes the changes from the the source ref to the given remote branch.
+ *
+ * @param gitRemoteBranchUri remote branch URI
+ * @param srcRef the source ref to push
+ * @param tags <code>true</code> to push tags
+ * @param force <code>true</code> to force push
+ * @return JSON object representing response
+ * @throws IOException
+ * @throws SAXException
+ * @throws JSONException
+ */
+ protected ServerStatus push(String gitRemoteBranchUri, String srcRef, boolean tags, boolean force) throws IOException, SAXException, JSONException {
assertRemoteOrRemoteBranchLocation(gitRemoteBranchUri);
- WebRequest request = GitPushTest.getPostGitRemoteRequest(gitRemoteBranchUri, srcRef, tags);
+ WebRequest request = GitPushTest.getPostGitRemoteRequest(gitRemoteBranchUri, srcRef, tags, force);
WebResponse response = webConversation.getResponse(request);
assertEquals(HttpURLConnection.HTTP_ACCEPTED, response.getResponseCode());
String taskLocation = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);
@@ -488,9 +508,13 @@ public abstract class GitTest extends FileSystemTest {
}
protected ServerStatus push(String gitRemoteUri, int size, int i, String name, String srcRef, boolean tags, String userName, String kh, byte[] privk, byte[] pubk, byte[] p, boolean shouldBeOK) throws IOException, SAXException, JSONException {
+ return push(gitRemoteUri, size, i, name, srcRef, tags, false, userName, kh, privk, pubk, p, shouldBeOK);
+ }
+
+ protected ServerStatus push(String gitRemoteUri, int size, int i, String name, String srcRef, boolean tags, boolean force, String userName, String kh, byte[] privk, byte[] pubk, byte[] p, boolean shouldBeOK) throws IOException, SAXException, JSONException {
assertRemoteUri(gitRemoteUri);
String remoteBranchLocation = getRemoteBranch(gitRemoteUri, size, i, name).getString(ProtocolConstants.KEY_LOCATION);
- WebRequest request = GitPushTest.getPostGitRemoteRequest(remoteBranchLocation, srcRef, tags, userName, kh, privk, pubk, p);
+ WebRequest request = GitPushTest.getPostGitRemoteRequest(remoteBranchLocation, srcRef, tags, force, userName, kh, privk, pubk, p);
WebResponse response = webConversation.getResponse(request);
assertEquals(HttpURLConnection.HTTP_ACCEPTED, response.getResponseCode());
String taskLocation = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);
@@ -526,10 +550,24 @@ public abstract class GitTest extends FileSystemTest {
* @throws SAXException
*/
protected JSONObject fetch(String remoteBranchLocation, String userName, String kh, byte[] privk, byte[] pubk, byte[] p, boolean shouldBeOK) throws JSONException, IOException, SAXException {
+ return fetch(remoteBranchLocation, false, userName, kh, privk, pubk, p, shouldBeOK);
+ }
+
+ /**
+ * Fetch objects and refs from the given remote branch.
+ *
+ * @param remoteBranchLocation remote branch URI
+ * @param force <code>true</code> to force fetch
+ * @return JSONObject representing remote branch after the fetch is done
+ * @throws JSONException
+ * @throws IOException
+ * @throws SAXException
+ */
+ protected JSONObject fetch(String remoteBranchLocation, boolean force, String userName, String kh, byte[] privk, byte[] pubk, byte[] p, boolean shouldBeOK) throws JSONException, IOException, SAXException {
assertRemoteOrRemoteBranchLocation(remoteBranchLocation);
// fetch
- WebRequest request = GitFetchTest.getPostGitRemoteRequest(remoteBranchLocation, true, userName, kh, privk, pubk, p);
+ WebRequest request = GitFetchTest.getPostGitRemoteRequest(remoteBranchLocation, true, force, userName, kh, privk, pubk, p);
WebResponse response = webConversation.getResponse(request);
assertEquals(HttpURLConnection.HTTP_ACCEPTED, response.getResponseCode());
String taskLocation = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);
@@ -562,10 +600,24 @@ public abstract class GitTest extends FileSystemTest {
* @throws SAXException
*/
protected JSONObject fetch(String remoteLocation) throws JSONException, IOException, SAXException {
+ return fetch(remoteLocation, false);
+ }
+
+ /**
+ * Fetch objects and refs from the given remote or remote branch.
+ *
+ * @param remoteLocation remote (branch) URI
+ * @param force <code>true</code> to force fetch
+ * @return JSONObject representing remote (branch) after the fetch is done
+ * @throws JSONException
+ * @throws IOException
+ * @throws SAXException
+ */
+ protected JSONObject fetch(String remoteLocation, boolean force) throws JSONException, IOException, SAXException {
assertRemoteOrRemoteBranchLocation(remoteLocation);
// fetch
- WebRequest request = GitFetchTest.getPostGitRemoteRequest(remoteLocation, true);
+ WebRequest request = GitFetchTest.getPostGitRemoteRequest(remoteLocation, true, force);
WebResponse response = webConversation.getResponse(request);
assertEquals(HttpURLConnection.HTTP_ACCEPTED, response.getResponseCode());
String taskLocation = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);