summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorChris Conley2013-07-30 05:20:00 (EDT)
committer Szymon Brandys2013-07-30 12:15:20 (EDT)
commit24323a8f54ebebfd773c5a1d2394d7a6b4810249 (patch)
tree6f5979b3c3c47b4a9cf4cec6ec2a44a467c08abc
parent44b7bf02658388e95103bed809b48cb3ba3276ec (diff)
downloadorg.eclipse.orion.server-24323a8f54ebebfd773c5a1d2394d7a6b4810249.zip
org.eclipse.orion.server-24323a8f54ebebfd773c5a1d2394d7a6b4810249.tar.gz
org.eclipse.orion.server-24323a8f54ebebfd773c5a1d2394d7a6b4810249.tar.bz2
Bug 413969 - Add Git Blame API
Signed-off-by: Chris Conley <chrisconley15@gmail.com>
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/BaseToCloneConverter.java2
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/GitConstants.java17
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/GitFileDecorator.java5
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/objects/Blame.java260
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitBlameHandlerV1.java186
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitHandlerV1.java7
-rw-r--r--tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/AllGitTests.java2
-rw-r--r--tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitBlameTest.java423
-rw-r--r--tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitTest.java32
9 files changed, 930 insertions, 4 deletions
diff --git a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/BaseToCloneConverter.java b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/BaseToCloneConverter.java
index e193654..ef6e1e5 100644
--- a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/BaseToCloneConverter.java
+++ b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/BaseToCloneConverter.java
@@ -60,6 +60,8 @@ public abstract class BaseToCloneConverter {
public static final BaseToCloneConverter TAG = REMOTE;
+ public static final BaseToCloneConverter BLAME = STATUS;
+
public static final BaseToCloneConverter REMOTE_BRANCH = new BaseToCloneConverter() {
@Override
public IPath getFilePath(URI base) throws URISyntaxException {
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 015c9eb..94de51d 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2011, 2012 IBM Corporation and others.
+ * Copyright (c) 2011, 2012, 2013 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
@@ -11,7 +11,7 @@
package org.eclipse.orion.server.git;
/**
- * Constants used by Git.
+ * Constants used by Git.
*/
public class GitConstants {
@@ -184,4 +184,17 @@ public class GitConstants {
public static final String KEY_REVIEW_REQ_AUTHOR_NAME = "ReviewReqAuthorName"; //$NON-NLS-1$
public static final String KEY_REVIEW_REQ_MESSAGE = "ReviewMessage"; //$NON-NLS-1$
+
+ public static final String KEY_BLAME = "Blame";
+
+ public static final String KEY_BLAME_INFO = "BlameInfo";
+
+ public static final String KEY_START_RANGE = "Start";
+
+ public static final String KEY_END_RANGE = "End";
+
+ public static final String KEY_OBJECTID = "ObjectId";
+
+ public static final String KEY_BLAME_LOCATION = "BlameLocation";
+
}
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 f51b6cd..7909b0c 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
@@ -165,6 +165,11 @@ public class GitFileDecorator implements IWebResourceDecorator {
link = new URI(location.getScheme(), location.getAuthority(), path.toString(), null, null);
gitSection.put(GitConstants.KEY_TAG, link);
+ // add Git Blame URI
+ path = new Path(GitServlet.GIT_URI + '/' + Blame.RESOURCE).append(Constants.HEAD).append(targetPath);
+ link = new URI(location.getScheme(), location.getAuthority(), path.toString(), null, null);
+ gitSection.put(GitConstants.KEY_BLAME, 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/objects/Blame.java b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/objects/Blame.java
new file mode 100644
index 0000000..f78f73a
--- /dev/null
+++ b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/objects/Blame.java
@@ -0,0 +1,260 @@
+/*******************************************************************************
+ * Copyright (c) 2013 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.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.orion.server.core.resources.*;
+import org.eclipse.orion.server.core.resources.annotations.PropertyDescription;
+import org.eclipse.orion.server.core.resources.annotations.ResourceDescription;
+import org.eclipse.orion.server.git.BaseToCommitConverter;
+import org.eclipse.orion.server.git.GitConstants;
+import org.json.*;
+
+@ResourceDescription(type = Blame.TYPE)
+public class Blame extends GitObject {
+
+ public static final String RESOURCE = "blame"; //$NON-NLS-1$
+
+ public static final String TYPE = "Blame"; //$NON-NLS-1$
+
+ private Repository db = null;
+
+ private List<String> lines = new ArrayList<String>();
+
+ private String filePath = null;
+
+ private ObjectId startCommit = null;
+
+ protected Serializer<JSONObject> jsonSerializer = new JSONSerializer();
+
+ private URI cloneLocation;
+
+ private String blameLocation = null;
+
+ private static final ResourceShape DEFAULT_RESOURCE_SHAPE = new ResourceShape();
+ {
+ Property[] defaultProperties = new Property[] {new Property(GitConstants.KEY_BLAME_LOCATION), new Property(GitConstants.KEY_BLAME_INFO)};
+ DEFAULT_RESOURCE_SHAPE.setProperties(defaultProperties);
+ }
+
+ public Blame(URI cloneLocation, Repository db) {
+ super(cloneLocation, db);
+ this.setRepository(db);
+ this.setCloneLocation(cloneLocation);
+ }
+
+ /*
+ * Getters and Setters
+ */
+
+ /**
+ * Add Strings of commitIds for each line
+ *
+ * @param line
+ */
+ public void addLine(String line) {
+ this.lines.add(line);
+ }
+
+ /**
+ * Returns a list of commitIds that correspond to each line in a file
+ *
+ * @return lines list
+ */
+ public List<String> getLines() {
+ return this.lines;
+ }
+
+ /**
+ * Get a specific commit Id for a line
+ *
+ * @param index
+ * @return String of list element
+ *
+ */
+ public String getLine(int index) {
+ return this.lines.get(index);
+ }
+
+ /**
+ *
+ * @param repo
+ */
+ public void setRepository(Repository repo) {
+ this.db = repo;
+ }
+
+ /**
+ *
+ * @return repository
+ */
+ public Repository getRepository() {
+ return this.db;
+ }
+
+ /**
+ * Set the file path which will be blamed
+ *
+ * @param path
+ */
+ public void setFilePath(String path) {
+ this.filePath = path;
+ }
+
+ /**
+ * Get the file path for the blame object
+ *
+ * @return file path of blame object
+ */
+ public String getFilePath() {
+ return this.filePath;
+ }
+
+ /**
+ * clear lines array
+ */
+ public void clearLines() {
+ this.lines = new ArrayList<String>();
+ }
+
+ /**
+ * Set the Commit where the blameing will start from
+ */
+
+ public void setStartCommit(ObjectId id) {
+ this.startCommit = id;
+ }
+
+ /**
+ * return the commit the blame function will start from
+ *
+ * @return ObjectId
+ */
+ public ObjectId getStartCommit() {
+ return this.startCommit;
+ }
+
+ /**
+ * Set cloneLocation for BaseToCommitConverter
+ */
+
+ public void setCloneLocation(URI uri) {
+ cloneLocation = uri;
+ }
+
+ /**
+ * Get cloneLocation for BaseToCommitConverter
+ *
+ * @return URI
+ */
+ public URI getCloneLocation() {
+ return cloneLocation;
+ }
+
+ /**
+ * Set the URI for the blame for this file
+ * @param location
+ */
+ public void setBlameLocation(String location) {
+ this.blameLocation = location;
+ }
+
+ /**
+ * Returns the URI of the blame location
+ * @return blameLocation
+ */
+ public String getBlameLocation() {
+ return this.blameLocation;
+ }
+
+ /**
+ * JSON Serializing
+ */
+
+ public JSONObject toJSON() throws URISyntaxException {
+ return jsonSerializer.serialize(this, DEFAULT_RESOURCE_SHAPE);
+ }
+
+ @PropertyDescription(name = GitConstants.KEY_BLAME_LOCATION)
+ private String getLocatJSON() throws JSONException {
+ if (this.getBlameLocation() != null) {
+ return this.getBlameLocation();
+ }
+ return null;
+ }
+
+ @PropertyDescription(name = GitConstants.KEY_BLAME_INFO)
+ private JSONArray getBlameJSON() throws URISyntaxException, JSONException, IOException {
+ JSONArray LinesJSON = new JSONArray();
+ if (lines.size() > 0) {
+
+ JSONObject tempObj = null;
+ String tempPath = null;
+ for (int i = 0; i < lines.size(); i++) {
+ String line = lines.get(i);
+ if (!line.equalsIgnoreCase(tempPath)) {
+ tempPath = line;
+ if (i > 0) {
+ tempObj.put(GitConstants.KEY_END_RANGE, i);
+ LinesJSON.put(tempObj);
+ }
+ tempObj = new JSONObject();
+ tempObj.put(GitConstants.KEY_START_RANGE, i + 1);
+ URI commit = BaseToCommitConverter.getCommitLocation(cloneLocation, line, BaseToCommitConverter.REMOVE_FIRST_2);
+ tempObj.put(GitConstants.KEY_COMMIT, commit);
+ tempObj.put(GitConstants.KEY_OBJECTID, line);
+ }
+
+ }
+ tempObj.put(GitConstants.KEY_END_RANGE, lines.size());
+ LinesJSON.put(tempObj);
+
+ return LinesJSON;
+ }
+
+ return null;
+ }
+
+ @Override
+ protected URI getLocation() throws URISyntaxException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ /**
+ *
+ * JSON STRUCTURE
+ * {
+ * "Location": "file.java",
+ * "Type": "Blame"
+ * "Blame":
+ * {
+ * {
+ * "CommitLocation":"/gitapi/commit/2b3a36c1b2f0064216a871740bd6906b6af7434a/file/C/",
+ * "Start": 0,
+ * "End": 7
+ * },
+ * {
+ * "CommitLocation":"/gitapi/commit/fb774ffe5b1bbfbe80ac9afb5263901afdc12874/file/C/",
+ * "Start": 8,
+ * "End": 13
+ * {
+ * }
+ *
+ */
+
+}
diff --git a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitBlameHandlerV1.java b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitBlameHandlerV1.java
new file mode 100644
index 0000000..172998e
--- /dev/null
+++ b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitBlameHandlerV1.java
@@ -0,0 +1,186 @@
+/*******************************************************************************
+ * Copyright (c) 2013 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.IOException;
+import java.net.URI;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.jgit.api.BlameCommand;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.blame.BlameResult;
+import org.eclipse.jgit.diff.RawTextComparator;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.orion.internal.server.servlets.ServletResourceHandler;
+import org.eclipse.orion.server.git.BaseToCloneConverter;
+import org.eclipse.orion.server.git.GitConstants;
+import org.eclipse.orion.server.git.objects.Blame;
+import org.eclipse.orion.server.servlets.OrionServlet;
+import org.json.JSONObject;
+
+public class GitBlameHandlerV1 extends AbstractGitHandler {
+
+ GitBlameHandlerV1(ServletResourceHandler<IStatus> statusHandler) {
+ super(statusHandler);
+ }
+
+ /*
+ * Handle Get
+ *
+ * Uses the head of the repository as the latest commit to test against
+ *
+ */
+
+ @Override
+ protected boolean handleGet(RequestInfo requestInfo) throws ServletException {
+ try {
+
+ HttpServletRequest request = requestInfo.request;
+ HttpServletResponse response = requestInfo.response;
+ request.removeAttribute(requestInfo.gitSegment);
+
+ //get URI without refID for clone location
+ String blameUri = getURI(request).toString().substring(6);
+ String[] s = blameUri.split("/");
+ StringBuffer sb = new StringBuffer();
+ for (int i = 1; i < s.length; i++) {
+ if (i != 3) {
+ sb.append("/");
+ sb.append(s[i]);
+ }
+ }
+ URI uri = new URI(sb.toString());
+
+ URI cloneLocation = BaseToCloneConverter.getCloneLocation(uri, BaseToCloneConverter.BLAME);
+
+ String path = requestInfo.relativePath;
+ Blame blame = new Blame(cloneLocation, requestInfo.db);
+
+ if (path.length() != 0) {
+ // check that path isnt for a folder
+ String file = path.substring(path.length() - 1);
+ if (!file.endsWith("/") && !file.endsWith("\"")) {
+ blame.setFilePath(path);
+ blame.setBlameLocation(blameUri);
+
+ }
+ }
+
+ // blame.setCloneLocation(cloneLocation);
+ //blame.setCloneLocation(cloneLocation);
+ blame.setRepository(requestInfo.db);
+ doBlame(blame);
+
+ OrionServlet.writeJSONResponse(request, response, blame.toJSON());
+
+ return true;
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ /*
+ * HandlePost
+ *
+ * Use if you want to blame starting from a certain commit
+ *
+ */
+ @Override
+ protected boolean handlePost(RequestInfo requestInfo) throws ServletException {
+ try {
+
+ HttpServletRequest request = requestInfo.request;
+ HttpServletResponse response = requestInfo.response;
+
+ JSONObject obj = requestInfo.getJSONRequest();
+ String commitString = obj.getString(GitConstants.KEY_COMMIT);
+ ObjectId id = ObjectId.fromString(commitString);
+
+ //get URI without refID for clone location
+ String blameUri = getURI(request).toString().substring(6);
+ String[] s = blameUri.split("/");
+ StringBuffer sb = new StringBuffer();
+ for (int i = 1; i < s.length; i++) {
+ if (i != 3) {
+ sb.append("/");
+ sb.append(s[i]);
+ }
+ }
+ URI uri = new URI(sb.toString());
+
+ URI cloneLocation = BaseToCloneConverter.getCloneLocation(uri, BaseToCloneConverter.BLAME);
+
+ String path = requestInfo.relativePath;
+ Blame blame = new Blame(null, requestInfo.db);
+
+ if (path.length() != 0) {
+ // check that path isnt for a folder
+ String file = path.substring(path.length() - 1);
+ if (!file.endsWith("/") && !file.endsWith("\"")) {
+ blame.setFilePath(path);
+ blame.setBlameLocation(blameUri);
+
+ }
+ }
+
+ blame.setCloneLocation(cloneLocation);
+
+ blame.setRepository(requestInfo.db);
+ blame.setStartCommit(id);
+ doBlame(blame);
+
+ OrionServlet.writeJSONResponse(request, response, blame.toJSON());
+ return true;
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ public void doBlame(Blame blame) throws GitAPIException, IOException {
+ Repository db = blame.getRepository();
+ String filePath = blame.getFilePath();
+
+ if (db != null && filePath != null) {
+
+ BlameCommand blameCommand = new BlameCommand(db);
+ blameCommand.setFilePath(filePath);
+ blameCommand.setFollowFileRenames(true);
+ blameCommand.setTextComparator(RawTextComparator.WS_IGNORE_ALL);
+
+ if (blame.getStartCommit() != null) {
+ blameCommand.setStartCommit(blame.getStartCommit());
+ }
+ BlameResult result;
+
+ try {
+ result = blameCommand.call();
+ } catch (Exception e1) {
+ return;
+ }
+ if (result != null) {
+ blame.clearLines();
+ RevCommit commitPath;
+ String path;
+ for (int i = 0; i < result.getResultContents().size(); i++) {
+ commitPath = result.getSourceCommit(i);
+ path = commitPath.getId().getName();
+ blame.addLine(path);
+
+ }
+ }
+ }
+
+ }
+} \ No newline at end of file
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..621b505 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, 2013 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,9 +32,11 @@ public class GitHandlerV1 extends ServletResourceHandler<String> {
private ServletResourceHandler<String> remoteHandlerV1;
private ServletResourceHandler<String> statusHandlerV1;
private ServletResourceHandler<String> tagHandlerV1;
+ private ServletResourceHandler<String> blameHandlerV1;
GitHandlerV1(ServletResourceHandler<IStatus> statusHandler) {
branchHandlerV1 = new GitBranchHandlerV1(statusHandler);
+ blameHandlerV1 = new GitBlameHandlerV1(statusHandler);
cloneHandlerV1 = new GitCloneHandlerV1(statusHandler);
commitHandlerV1 = new GitCommitHandlerV1(statusHandler);
configHandlerV1 = new GitConfigHandlerV1(statusHandler);
@@ -43,6 +45,7 @@ public class GitHandlerV1 extends ServletResourceHandler<String> {
remoteHandlerV1 = new GitRemoteHandlerV1(statusHandler);
statusHandlerV1 = new GitStatusHandlerV1(statusHandler);
tagHandlerV1 = new GitTagHandlerV1(statusHandler);
+
}
@Override
@@ -77,6 +80,8 @@ public class GitHandlerV1 extends ServletResourceHandler<String> {
return statusHandlerV1.handleRequest(request, response, pathString);
} else if (infoParts[1].equals(Tag.RESOURCE)) {
return tagHandlerV1.handleRequest(request, response, pathString);
+ } else if (infoParts[1].equals(Blame.RESOURCE)) {
+ return blameHandlerV1.handleRequest(request, response, pathString);
}
return false;
}
diff --git a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/AllGitTests.java b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/AllGitTests.java
index 284ae3f..d550895 100644
--- a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/AllGitTests.java
+++ b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/AllGitTests.java
@@ -20,7 +20,7 @@ import org.junit.runners.Suite.SuiteClasses;
* Runs all automated server tests for git support.
*/
@RunWith(Suite.class)
-@SuiteClasses({GitUriTest.class, //
+@SuiteClasses({GitUriTest.class, GitBlameTest.class,//
GitCloneTest.class, //
GitInitTest.class, //
GitDiffTest.class, //
diff --git a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitBlameTest.java b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitBlameTest.java
new file mode 100644
index 0000000..ac46473
--- /dev/null
+++ b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitBlameTest.java
@@ -0,0 +1,423 @@
+/*******************************************************************************
+ * 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
+ * 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 static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotSame;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URI;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.orion.internal.server.servlets.ProtocolConstants;
+import org.eclipse.orion.server.git.GitConstants;
+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.WebRequest;
+import com.meterware.httpunit.WebResponse;
+
+public class GitBlameTest extends GitTest {
+
+ @Test
+ public void testBlameNoCommits() throws IOException, SAXException, JSONException, CoreException {
+ URI workspaceLocation = createWorkspace(getMethodName());
+ IPath[] clonePaths = createTestProjects(workspaceLocation);
+
+ for (IPath clonePath : clonePaths) {
+ //clone a repo
+ JSONObject clone = clone(clonePath);
+ String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ //get project/folder metadata
+ WebRequest request = getGetRequest(cloneContentLocation);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject folder = new JSONObject(response.getText());
+
+ // get blameUri
+ JSONObject gitSection = folder.getJSONObject(GitConstants.KEY_GIT);
+ String gitBlameUri = gitSection.getString(GitConstants.KEY_BLAME);
+
+ // blame request
+ request = getGetGitBlameRequest(gitBlameUri);
+ response = webConversation.getResource(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // test
+ JSONObject blameObject = new JSONObject(response.getText());
+ assertEquals(blameObject.length(), 1);
+ assertEquals(blameObject.getString(ProtocolConstants.KEY_TYPE), "Blame");
+
+ }
+ }
+
+ @Test
+ public void testBlameOneCommit() throws IOException, SAXException, JSONException, CoreException {
+
+ URI workspaceLocation = createWorkspace(getMethodName());
+ IPath[] clonePaths = createTestProjects(workspaceLocation);
+
+ for (IPath clonePath : clonePaths) {
+ //clone a repo
+ JSONObject clone = clone(clonePath);
+ String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ //get project/folder metadata
+ WebRequest request = getGetRequest(cloneContentLocation);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject folder = new JSONObject(response.getText());
+ JSONObject gitSection = folder.getJSONObject(GitConstants.KEY_GIT);
+ String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);
+
+ //create and modify file
+ JSONObject testTxt = getChild(folder, "test.txt");
+ modifyFile(testTxt, "line one \n line two \n line 3 \n line 4");
+
+ //commit
+ addFile(testTxt);
+ request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "initial commit", false);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // get blame Uri
+ JSONObject testTxtGitSection = testTxt.getJSONObject(GitConstants.KEY_GIT);
+ String blameUri = testTxtGitSection.getString(GitConstants.KEY_BLAME);
+
+ // blame request
+ request = getGetGitBlameRequest(blameUri);
+ response = webConversation.getResource(request);
+
+ // get BlameInfo
+ JSONObject blameObject = new JSONObject(response.getText());
+
+ //Test
+ assertEquals(blameObject.getString(ProtocolConstants.KEY_TYPE), "Blame");
+
+ JSONArray blame = blameObject.getJSONArray(GitConstants.KEY_BLAME_INFO);
+ assertNotNull(blameObject.get(GitConstants.KEY_BLAME_INFO));
+ assertEquals(blame.length(), 1);
+ blameObject = blame.getJSONObject(0);
+ assertEquals(blameObject.getInt(GitConstants.KEY_START_RANGE), 1);
+ assertEquals(blameObject.getInt(GitConstants.KEY_END_RANGE), 4);
+ assertCommitUri(blameObject.getString(GitConstants.KEY_COMMIT));
+
+ }
+ }
+
+ @Test
+ public void testBlameMultiCommit() throws IOException, SAXException, JSONException, CoreException {
+
+ URI workspaceLocation = createWorkspace(getMethodName());
+ IPath[] clonePaths = createTestProjects(workspaceLocation);
+
+ for (IPath clonePath : clonePaths) {
+ //clone a repo
+ JSONObject clone = clone(clonePath);
+ String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ //get project/folder metadata
+ WebRequest request = getGetRequest(cloneContentLocation);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject folder = new JSONObject(response.getText());
+ JSONObject gitSection = folder.getJSONObject(GitConstants.KEY_GIT);
+ String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);
+
+ //create file test.txtx
+ JSONObject testTxt = getChild(folder, "test.txt");
+ modifyFile(testTxt, "line one \n line two \n line 3 \n line 4");
+
+ //commit the file
+ addFile(testTxt);
+ request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "initial commit", false);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // get the blame uri for this file
+ JSONObject testTxtGitSection = testTxt.getJSONObject(GitConstants.KEY_GIT);
+ String blameUri = testTxtGitSection.getString(GitConstants.KEY_BLAME);
+
+ // blame the file
+ request = getGetGitBlameRequest(blameUri);
+ response = webConversation.getResource(request);
+
+ // testing
+ JSONObject blameObject = new JSONObject(response.getText());
+
+ // non blame info tests
+ assertEquals(blameObject.length(), 3);
+ assertEquals(blameObject.getString(ProtocolConstants.KEY_TYPE), "Blame");
+ assertEquals(blameObject.getString(GitConstants.KEY_BLAME_LOCATION), blameUri);
+
+ // blame info tests
+ JSONArray blame = blameObject.getJSONArray(GitConstants.KEY_BLAME_INFO);
+ assertNotNull(blameObject.get(GitConstants.KEY_BLAME_INFO));
+ assertEquals(blame.length(), 1);
+ blameObject = blame.getJSONObject(0);
+ assertEquals(blameObject.getInt(GitConstants.KEY_START_RANGE), 1);
+ assertEquals(blameObject.getInt(GitConstants.KEY_END_RANGE), 4);
+ // assertCommitUri(blameObject.getString(GitConstants.KEY_COMMIT));
+
+ // modify the file
+ modifyFile(testTxt, "line one \n line two \n line 3 \n LINE FOUR \n LINE FIVE");
+ addFile(testTxt);
+
+ //commit the new changes
+ request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "initial commit", false);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // get blame uri
+ testTxtGitSection = testTxt.getJSONObject(GitConstants.KEY_GIT);
+ blameUri = testTxtGitSection.getString(GitConstants.KEY_BLAME);
+
+ // blame file
+ request = getGetGitBlameRequest(blameUri);
+ response = webConversation.getResource(request);
+
+ // test
+ blameObject = new JSONObject(response.getText());
+
+ // non blame info tests
+ assertEquals(blameObject.length(), 3);
+ assertEquals(blameObject.getString(ProtocolConstants.KEY_TYPE), "Blame");
+ assertEquals(blameObject.getString(GitConstants.KEY_BLAME_LOCATION), blameUri);
+
+ // blame info tests
+ blame = blameObject.getJSONArray(GitConstants.KEY_BLAME_INFO);
+ assertNotNull(blameObject.get(GitConstants.KEY_BLAME_INFO));
+ assertEquals(blame.length(), 2);
+
+ // test object 1 from the first commit
+ blameObject = blame.getJSONObject(0);
+ assertEquals(blameObject.getInt(GitConstants.KEY_START_RANGE), 1);
+ assertEquals(blameObject.getInt(GitConstants.KEY_END_RANGE), 3);
+ assertCommitUri(blameObject.getString(GitConstants.KEY_COMMIT));
+
+ //test object 2 from the second commit
+ blameObject = blame.getJSONObject(1);
+ assertEquals(blameObject.getInt(GitConstants.KEY_START_RANGE), 4);
+ assertEquals(blameObject.getInt(GitConstants.KEY_END_RANGE), 5);
+ assertCommitUri(blameObject.getString(GitConstants.KEY_COMMIT));
+
+ // make sure commits are not the same
+ assertNotSame(blame.getJSONObject(0).get(GitConstants.KEY_COMMIT), blame.getJSONObject(1).get(GitConstants.KEY_COMMIT));
+ assertNotSame(blame.getJSONObject(0).get(GitConstants.KEY_END_RANGE), blame.getJSONObject(1).get(GitConstants.KEY_START_RANGE));
+
+ }
+ }
+
+ @Test
+ public void testBlameMultiFile() throws IOException, SAXException, JSONException, CoreException {
+
+ URI workspaceLocation = createWorkspace(getMethodName());
+ IPath[] clonePaths = createTestProjects(workspaceLocation);
+ for (IPath clonePath : clonePaths) {
+ //clone a repo
+ JSONObject clone = clone(clonePath);
+ String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ //get project/folder metadata
+ WebRequest request = getGetRequest(cloneContentLocation);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject folder = new JSONObject(response.getText());
+
+ JSONObject gitSection = folder.getJSONObject(GitConstants.KEY_GIT);
+ String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);
+
+ /*
+ * Commit 1
+ */
+ // create the original file and modify it
+ JSONObject testTxt = getChild(folder, "test.txt");
+ modifyFile(testTxt, "line one \n line two \n line 3 \n line 4");
+
+ //commit file
+ addFile(testTxt);
+ request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "initial commit", false);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // get blameURI
+ JSONObject testTxtGitSection = testTxt.getJSONObject(GitConstants.KEY_GIT);
+ String blameUri = testTxtGitSection.getString(GitConstants.KEY_BLAME);
+
+ // make blame request
+ request = getGetGitBlameRequest(blameUri);
+ response = webConversation.getResource(request);
+
+ // test
+ JSONObject blameObject = new JSONObject(response.getText());
+ assertEquals(blameObject.getString(ProtocolConstants.KEY_TYPE), "Blame");
+ assertEquals(blameObject.getString(GitConstants.KEY_BLAME_LOCATION), blameUri);
+
+ //test blameInfo
+ JSONArray blame = blameObject.getJSONArray(GitConstants.KEY_BLAME_INFO);
+ assertNotNull(blameObject.get(GitConstants.KEY_BLAME_INFO));
+ assertEquals(blame.length(), 1);
+ blameObject = blame.getJSONObject(0);
+ assertEquals(blameObject.getInt(GitConstants.KEY_START_RANGE), 1);
+ assertEquals(blameObject.getInt(GitConstants.KEY_END_RANGE), 4);
+ assertCommitUri(blameObject.getString(GitConstants.KEY_COMMIT));
+
+ /*
+ * commit 2 - different file
+ */
+
+ // create a second file in a different folder
+ JSONObject newfolder = getChild(folder, "folder");
+ JSONObject folderTxt = getChild(newfolder, "folder.txt");
+ modifyFile(folderTxt, "commit me");
+
+ // commit the new file
+ addFile(folderTxt);
+ request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "initial commit on testFile2.txt", false);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // make blame request
+ request = getGetGitBlameRequest(blameUri);
+ response = webConversation.getResource(request);
+
+ /*
+ * These tests should produce the same results as the above tests
+ */
+ blameObject = new JSONObject(response.getText());
+ assertEquals(blameObject.getString(ProtocolConstants.KEY_TYPE), "Blame");
+ assertEquals(blameObject.getString(GitConstants.KEY_BLAME_LOCATION), blameUri);
+
+ //test blameInfo
+ blame = blameObject.getJSONArray(GitConstants.KEY_BLAME_INFO);
+ assertNotNull(blameObject.get(GitConstants.KEY_BLAME_INFO));
+ assertEquals(blame.length(), 1);
+ blameObject = blame.getJSONObject(0);
+ assertEquals(blameObject.getInt(GitConstants.KEY_START_RANGE), 1);
+ assertEquals(blameObject.getInt(GitConstants.KEY_END_RANGE), 4);
+ assertCommitUri(blameObject.getString(GitConstants.KEY_COMMIT));
+
+ /*
+ * commit 3 - original file
+ */
+
+ // modify original file
+ modifyFile(testTxt, "line one \n line two \n line 3 \n LINE FOUR \n LINE FIVE");
+
+ //commit the changes
+ addFile(testTxt);
+ request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "modified testFile.txt", false);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ blameUri = testTxtGitSection.getString(GitConstants.KEY_BLAME);
+
+ // do blame computation
+ request = getGetGitBlameRequest(blameUri);
+ response = webConversation.getResource(request);
+
+ // test
+ blameObject = new JSONObject(response.getText());
+
+ // non blame info tests
+ //assertEquals(blameObject.length(), 3);
+ assertEquals(blameObject.getString(ProtocolConstants.KEY_TYPE), "Blame");
+ assertEquals(blameObject.getString(GitConstants.KEY_BLAME_LOCATION), blameUri);
+
+ // blame info tests
+ blame = blameObject.getJSONArray(GitConstants.KEY_BLAME_INFO);
+ assertNotNull(blameObject.get(GitConstants.KEY_BLAME_INFO));
+ assertEquals(blame.length(), 2);
+
+ // test object 1 from the first commit
+ blameObject = blame.getJSONObject(0);
+ assertEquals(blameObject.getInt(GitConstants.KEY_START_RANGE), 1);
+ assertEquals(blameObject.getInt(GitConstants.KEY_END_RANGE), 3);
+ assertCommitUri(blameObject.getString(GitConstants.KEY_COMMIT));
+
+ //test object 2 from the second commit
+ blameObject = blame.getJSONObject(1);
+ assertEquals(blameObject.getInt(GitConstants.KEY_START_RANGE), 4);
+ assertEquals(blameObject.getInt(GitConstants.KEY_END_RANGE), 5);
+ assertCommitUri(blameObject.getString(GitConstants.KEY_COMMIT));
+
+ // make sure commits are not the same
+ assertNotSame(blame.getJSONObject(0).get(GitConstants.KEY_COMMIT), blame.getJSONObject(1).get(GitConstants.KEY_COMMIT));
+ assertNotSame(blame.getJSONObject(0).get(GitConstants.KEY_END_RANGE), blame.getJSONObject(1).get(GitConstants.KEY_START_RANGE));
+
+ }
+ }
+
+ @Test
+ public void testFolderBlame() throws IOException, SAXException, JSONException, CoreException {
+
+ URI workspaceLocation = createWorkspace(getMethodName());
+ IPath[] clonePaths = createTestProjects(workspaceLocation);
+
+ for (IPath clonePath : clonePaths) {
+ //clone a repo
+ JSONObject clone = clone(clonePath);
+ String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ //get project/folder metadata
+ WebRequest request = getGetRequest(cloneContentLocation);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject folder = new JSONObject(response.getText());
+ JSONObject gitSection = folder.getJSONObject(GitConstants.KEY_GIT);
+ String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);
+
+ JSONObject newfolder = getChild(folder, "folder");
+
+ //modifyFile(newfolder, "commit me");
+
+ // commit the new file
+ addFile(newfolder);
+ request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "initial commit on testFile2.txt", false);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ JSONObject newfolderGitSection = newfolder.getJSONObject(GitConstants.KEY_GIT);
+ String blameUri = newfolderGitSection.getString(GitConstants.KEY_BLAME);
+
+ // blame request
+ request = getGetGitBlameRequest(blameUri);
+ response = webConversation.getResource(request);
+
+ // get BlameInfo
+ JSONObject blameObject = new JSONObject(response.getText());
+
+ //Test
+
+ assertEquals(blameObject.length(), 1);
+ assertEquals(blameObject.getString(ProtocolConstants.KEY_TYPE), "Blame");
+
+ }
+ }
+
+ protected static WebRequest getGetGitBlameRequest(String location) {
+ String requestURI = toAbsoluteURI(location);
+ WebRequest request = new GetMethodWebRequest(requestURI);
+ request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
+ setAuthentication(request);
+ return request;
+ }
+
+} \ No newline at end of file
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 f2a36cc..586cb72 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
@@ -69,6 +69,7 @@ import org.eclipse.orion.server.core.metastore.ProjectInfo;
import org.eclipse.orion.server.core.metastore.WorkspaceInfo;
import org.eclipse.orion.server.core.tasks.TaskInfo;
import org.eclipse.orion.server.git.GitConstants;
+import org.eclipse.orion.server.git.objects.Blame;
import org.eclipse.orion.server.git.objects.Branch;
import org.eclipse.orion.server.git.objects.Clone;
import org.eclipse.orion.server.git.objects.Commit;
@@ -808,6 +809,26 @@ public abstract class GitTest extends FileSystemTest {
return webConversation.getResponse(request);
}
+ protected WebResponse getPostGitBlame(String blameUri, String commitId) throws JSONException, IOException, SAXException {
+ assertBlameUri(blameUri);
+ String requestURI = toAbsoluteURI(blameUri);
+ JSONObject body = new JSONObject();
+ body.put(GitConstants.KEY_COMMIT, commitId);
+ WebRequest request = new PutMethodWebRequest(requestURI, IOUtilities.toInputStream(body.toString()), "application/json");
+ request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
+ setAuthentication(request);
+ return webConversation.getResponse(request);
+ }
+
+ protected WebResponse getGetGitBlame(String blameUri) throws IOException, SAXException {
+ assertBlameUri(blameUri);
+ String requestURI = toAbsoluteURI(blameUri);
+ WebRequest request = new GetMethodWebRequest(requestURI);
+ request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
+ setAuthentication(request);
+ return webConversation.getResponse(request);
+ }
+
/**
* Return commits for the given URI.
*
@@ -1035,12 +1056,23 @@ public abstract class GitTest extends FileSystemTest {
assertEquals("file", path.segment(0));
}
+ protected static void assertBlameUri(String fileUri) {
+ URI uri = URI.create(toRelativeURI(fileUri));
+ IPath path = new Path(uri.getPath());
+ // /gitapi/blame/file/{id}/workspace/{path}
+ System.out.println(uri.toString());
+ assertTrue(path.segmentCount() > 4);
+ assertEquals(Blame.RESOURCE, path.segment(1));
+ assertTrue("file".equals(path.segment(2)) || "file".equals(path.segment(3)));
+ }
+
protected static void assertBranchUri(String branchUri) {
URI uri = URI.create(toRelativeURI(branchUri));
IPath path = new Path(uri.getPath());
// /git/branch/[{name}/]file/{path}
assertTrue(path.segmentCount() > 3);
assertEquals(GitServlet.GIT_URI.substring(1), path.segment(0));
+ assertEquals(GitServlet.GIT_URI.substring(1), path.segment(0));
assertEquals(Branch.RESOURCE, path.segment(1));
assertTrue("file".equals(path.segment(2)) || "file".equals(path.segment(3)));
}