summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorPiotr Janik2011-07-27 07:52:16 (EDT)
committer Tomasz Zarna2011-07-27 07:52:16 (EDT)
commitb8682f865737d5804ff838f732b7491567b39c0b (patch)
treeadee36e06597ba615e48526d9e317513957a9111
parente91ebdb8cffb17eb3811d729e8be194b3dbde218 (diff)
downloadorg.eclipse.orion.server-b8682f865737d5804ff838f732b7491567b39c0b.zip
org.eclipse.orion.server-b8682f865737d5804ff838f732b7491567b39c0b.tar.gz
org.eclipse.orion.server-b8682f865737d5804ff838f732b7491567b39c0b.tar.bz2
bug 343644 - [server] Git Log should return branches for commits
https://bugs.eclipse.org/bugs/show_bug.cgi?id=343644
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/GitConstants.java4
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitCommitHandlerV1.java51
-rw-r--r--tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitLogTest.java185
3 files changed, 190 insertions, 50 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 96d8cc0..0862d1f 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
@@ -119,6 +119,10 @@ public class GitConstants {
public static final String KEY_COMMIT_DIFF_CHANGETYPE = "ChangeType"; //$NON-NLS-1$
+ public static final String KEY_TAGS = "Tags"; //$NON-NLS-1$
+
+ public static final String KEY_BRANCHES = "Branches"; //$NON-NLS-1$
+
public static final String KEY_LOG_TO_REF = "toRef"; //$NON-NLS-1$
public static final String KEY_LOG_FROM_REF = "fromRef"; //$NON-NLS-1$
diff --git a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitCommitHandlerV1.java b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitCommitHandlerV1.java
index fec4f54..a297c01 100644
--- a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitCommitHandlerV1.java
+++ b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitCommitHandlerV1.java
@@ -21,6 +21,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.core.runtime.*;
import org.eclipse.jgit.api.*;
+import org.eclipse.jgit.api.ListBranchCommand.ListMode;
import org.eclipse.jgit.api.RebaseCommand.Operation;
import org.eclipse.jgit.api.errors.*;
import org.eclipse.jgit.diff.DiffEntry;
@@ -202,7 +203,8 @@ public class GitCommitHandlerV1 extends ServletResourceHandler<String> {
try {
Iterable<RevCommit> commits = log.call();
- JSONObject result = toJSON(db, OrionServlet.getURI(request), commits, page, pageSize, filter, isRoot);
+ Map<ObjectId, JSONArray> commitToBranchMap = getCommitToBranchMap(db);
+ JSONObject result = toJSON(db, OrionServlet.getURI(request), commits, commitToBranchMap, page, pageSize, filter, isRoot);
result.put(GitConstants.KEY_REPOSITORY_PATH, isRoot ? "" : path); //$NON-NLS-1$
@@ -233,7 +235,7 @@ public class GitCommitHandlerV1 extends ServletResourceHandler<String> {
}
}
- private JSONObject toJSON(Repository db, URI baseLocation, Iterable<RevCommit> commits, int page, int pageSize, TreeFilter filter, boolean isRoot) throws JSONException, URISyntaxException, MissingObjectException, IOException {
+ private JSONObject toJSON(Repository db, URI baseLocation, Iterable<RevCommit> commits, Map<ObjectId, JSONArray> commitToBranchMap, int page, int pageSize, TreeFilter filter, boolean isRoot) throws JSONException, URISyntaxException, MissingObjectException, IOException {
boolean pageable = (page > 0);
int startIndex = (page - 1) * pageSize;
int index = 0;
@@ -251,13 +253,13 @@ public class GitCommitHandlerV1 extends ServletResourceHandler<String> {
index++;
- children.put(toJSON(db, revCommit, baseLocation, filter, isRoot));
+ children.put(toJSON(db, revCommit, commitToBranchMap, baseLocation, filter, isRoot));
}
result.put(ProtocolConstants.KEY_CHILDREN, children);
return result;
}
- private JSONObject toJSON(Repository db, RevCommit revCommit, URI baseLocation, TreeFilter filter, boolean isRoot) throws JSONException, URISyntaxException, IOException {
+ private JSONObject toJSON(Repository db, RevCommit revCommit, Map<ObjectId, JSONArray> commitToBranchMap, URI baseLocation, TreeFilter filter, boolean isRoot) throws JSONException, URISyntaxException, IOException {
JSONObject commit = new JSONObject();
commit.put(ProtocolConstants.KEY_LOCATION, createCommitLocation(baseLocation, revCommit.getName(), null));
commit.put(ProtocolConstants.KEY_CONTENT_LOCATION, createCommitLocation(baseLocation, revCommit.getName(), "parts=body")); //$NON-NLS-1$
@@ -274,8 +276,10 @@ public class GitCommitHandlerV1 extends ServletResourceHandler<String> {
commit.put(GitConstants.KEY_COMMITTER_EMAIL, committer.getEmailAddress());
commit.put(GitConstants.KEY_COMMIT_TIME, ((long) revCommit.getCommitTime()) * 1000 /* time in milliseconds */);
commit.put(GitConstants.KEY_COMMIT_MESSAGE, revCommit.getFullMessage());
- commit.put(ProtocolConstants.KEY_CHILDREN, toJSON(getTagsForCommit(db, revCommit)));
+ commit.put(GitConstants.KEY_TAGS, toJSON(getTagsForCommit(db, revCommit)));
commit.put(ProtocolConstants.KEY_TYPE, GitConstants.COMMIT_TYPE);
+ commit.put(GitConstants.KEY_BRANCHES, commitToBranchMap.get(revCommit.getId()));
+ commit.put(ProtocolConstants.KEY_PARENTS, parentsToJSON(revCommit.getParents()));
if (revCommit.getParentCount() > 0) {
JSONArray diffs = new JSONArray();
@@ -426,8 +430,9 @@ public class GitCommitHandlerV1 extends ServletResourceHandler<String> {
try {
// "git commit [--amend] -m '{message}' [-a|{path}]"
RevCommit lastCommit = commit.setAmend(amend).setMessage(message).call();
+ Map<ObjectId, JSONArray> commitToBranchMap = getCommitToBranchMap(db);
- JSONObject result = toJSON(db, lastCommit, getURI(request), null, isRoot);
+ JSONObject result = toJSON(db, lastCommit, commitToBranchMap, getURI(request), null, isRoot);
OrionServlet.writeJSONResponse(request, response, result);
return true;
} catch (GitAPIException e) {
@@ -542,8 +547,9 @@ public class GitCommitHandlerV1 extends ServletResourceHandler<String> {
walk.parseBody(revCommit);
GitTagHandlerV1.tag(git, revCommit, tagName);
+ Map<ObjectId, JSONArray> commitToBranchMap = getCommitToBranchMap(db);
- JSONObject result = toJSON(db, revCommit, OrionServlet.getURI(request), null, isRoot);
+ JSONObject result = toJSON(db, revCommit, commitToBranchMap, OrionServlet.getURI(request), null, isRoot);
OrionServlet.writeJSONResponse(request, response, result);
walk.dispose();
return true;
@@ -570,4 +576,35 @@ public class GitCommitHandlerV1 extends ServletResourceHandler<String> {
}
return revTags;
}
+
+ private static Map<ObjectId, JSONArray> getCommitToBranchMap(Repository db) throws JSONException {
+ HashMap<ObjectId, JSONArray> commitToBranch = new HashMap<ObjectId, JSONArray>();
+ Git git = new Git(db);
+ List<Ref> branchRefs = git.branchList().setListMode(ListMode.ALL).call();
+ for (Ref branchRef : branchRefs) {
+ ObjectId commitId = branchRef.getLeaf().getObjectId();
+ JSONObject branch = new JSONObject();
+ branch.put(ProtocolConstants.KEY_FULL_NAME, branchRef.getName());
+
+ JSONArray branchesArray = commitToBranch.get(commitId);
+ if (branchesArray != null) {
+ branchesArray.put(branch);
+ } else {
+ branchesArray = new JSONArray();
+ branchesArray.put(branch);
+ commitToBranch.put(commitId, branchesArray);
+ }
+ }
+ return commitToBranch;
+ }
+
+ private JSONArray parentsToJSON(RevCommit[] revCommits) throws JSONException {
+ JSONArray parents = new JSONArray();
+ for (RevCommit revCommit : revCommits) {
+ JSONObject parent = new JSONObject();
+ parent.put(ProtocolConstants.KEY_NAME, revCommit.getName());
+ parents.put(parent);
+ }
+ return parents;
+ }
}
diff --git a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitLogTest.java b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitLogTest.java
index 1edd3d7..2dc2e05 100644
--- a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitLogTest.java
+++ b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitLogTest.java
@@ -26,7 +26,6 @@ import org.eclipse.orion.server.git.GitConstants;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-import org.junit.Ignore;
import org.junit.Test;
import com.meterware.httpunit.PostMethodWebRequest;
@@ -173,66 +172,166 @@ public class GitLogTest extends GitTest {
@Test
public void testLogWithTag() throws Exception {
- // clone a repo
URI workspaceLocation = createWorkspace(getMethodName());
- JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null);
- IPath clonePath = new Path("file").append(project.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
- clone(clonePath);
+ JSONObject projectTop = createProjectOrLink(workspaceLocation, getMethodName() + "-top", null);
+ IPath clonePathTop = new Path("file").append(projectTop.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
- // get project metadata
- WebRequest request = getGetFilesRequest(project.getString(ProtocolConstants.KEY_CONTENT_LOCATION));
- WebResponse response = webConversation.getResponse(request);
- assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
- project = new JSONObject(response.getText());
+ JSONObject projectFolder = createProjectOrLink(workspaceLocation, getMethodName() + "-folder", null);
+ IPath clonePathFolder = new Path("file").append(projectFolder.getString(ProtocolConstants.KEY_ID)).append("folder").makeAbsolute();
- JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
- String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);
+ IPath[] clonePaths = new IPath[] {clonePathTop, clonePathFolder};
- JSONArray commitsArray = log(gitHeadUri, true);
- assertEquals(1, commitsArray.length());
+ for (IPath clonePath : clonePaths) {
+ // clone a repo
+ JSONObject clone = clone(clonePath);
+ String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
- String commitUri = commitsArray.getJSONObject(0).getString(ProtocolConstants.KEY_LOCATION);
- JSONObject updatedCommit = tag(commitUri, "tag");
- JSONArray tagsAndBranchesArray = updatedCommit.getJSONArray(ProtocolConstants.KEY_CHILDREN);
- assertEquals(1, tagsAndBranchesArray.length());
- assertEquals(Constants.R_TAGS + "tag", tagsAndBranchesArray.getJSONObject(0).get(ProtocolConstants.KEY_FULL_NAME));
+ // get project metadata
+ WebRequest request = getGetFilesRequest(cloneContentLocation);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject project = new JSONObject(response.getText());
- commitsArray = log(gitHeadUri, true);
- assertEquals(1, commitsArray.length());
+ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
+ String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);
- tagsAndBranchesArray = commitsArray.getJSONObject(0).getJSONArray(ProtocolConstants.KEY_CHILDREN);
- assertEquals(1, tagsAndBranchesArray.length());
- assertEquals(Constants.R_TAGS + "tag", tagsAndBranchesArray.getJSONObject(0).get(ProtocolConstants.KEY_FULL_NAME));
+ JSONArray commitsArray = log(gitHeadUri, true);
+ assertEquals(1, commitsArray.length());
+
+ String commitUri = commitsArray.getJSONObject(0).getString(ProtocolConstants.KEY_LOCATION);
+ JSONObject updatedCommit = tag(commitUri, "tag");
+ JSONArray tagsArray = updatedCommit.getJSONArray(GitConstants.KEY_TAGS);
+ assertEquals(1, tagsArray.length());
+ assertEquals(Constants.R_TAGS + "tag", tagsArray.getJSONObject(0).get(ProtocolConstants.KEY_FULL_NAME));
+
+ commitsArray = log(gitHeadUri, true);
+ assertEquals(1, commitsArray.length());
+
+ tagsArray = commitsArray.getJSONObject(0).getJSONArray(GitConstants.KEY_TAGS);
+ assertEquals(1, tagsArray.length());
+ assertEquals(Constants.R_TAGS + "tag", tagsArray.getJSONObject(0).get(ProtocolConstants.KEY_FULL_NAME));
+ }
}
@Test
- @Ignore("bug 343644")
public void testLogWithBranch() throws Exception {
URI workspaceLocation = createWorkspace(getMethodName());
+ JSONObject projectTop = createProjectOrLink(workspaceLocation, getMethodName() + "-top", null);
+ IPath clonePathTop = new Path("file").append(projectTop.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
- JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null);
- JSONObject clone = clone(new Path("file").append(project.getString(ProtocolConstants.KEY_ID)).makeAbsolute());
- String branchesLocation = clone.getString(GitConstants.KEY_BRANCH);
+ JSONObject projectFolder = createProjectOrLink(workspaceLocation, getMethodName() + "-folder", null);
+ IPath clonePathFolder = new Path("file").append(projectFolder.getString(ProtocolConstants.KEY_ID)).append("folder").makeAbsolute();
- // get project metadata
- WebRequest request = getGetFilesRequest(project.getString(ProtocolConstants.KEY_CONTENT_LOCATION));
- WebResponse response = webConversation.getResponse(request);
- assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
- project = new JSONObject(response.getText());
- JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
- String gitCommitUri = gitSection.getString(GitConstants.KEY_COMMIT);
+ IPath[] clonePaths = new IPath[] {clonePathTop, clonePathFolder};
- JSONArray commitsArray = log(gitCommitUri, true);
- assertEquals(1, commitsArray.length());
+ for (IPath clonePath : clonePaths) {
+ // clone a repo
+ JSONObject clone = clone(clonePath);
+ String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+ String branchesLocation = clone.getString(GitConstants.KEY_BRANCH);
- branch(branchesLocation, "branch");
+ // get project metadata
+ WebRequest request = getGetFilesRequest(cloneContentLocation);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject project = new JSONObject(response.getText());
- commitsArray = log(gitCommitUri, true);
- assertEquals(1, commitsArray.length());
+ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
+ String gitCommitUri = gitSection.getString(GitConstants.KEY_COMMIT);
- JSONArray tagsAndBranchesArray = commitsArray.getJSONObject(0).getJSONArray(ProtocolConstants.KEY_CHILDREN);
- assertEquals(1, tagsAndBranchesArray.length());
- assertEquals(Constants.R_HEADS + "branch", tagsAndBranchesArray.get(0));
+ JSONArray commitsArray = log(gitCommitUri, true);
+ assertEquals(1, commitsArray.length());
+
+ branch(branchesLocation, "branch");
+
+ commitsArray = log(gitCommitUri, true);
+ assertEquals(1, commitsArray.length());
+
+ JSONArray branchesArray = commitsArray.getJSONObject(0).getJSONArray(GitConstants.KEY_BRANCHES);
+ assertEquals(3, branchesArray.length());
+ assertEquals(Constants.R_HEADS + "branch", branchesArray.getJSONObject(0).get(ProtocolConstants.KEY_FULL_NAME));
+ assertEquals(Constants.R_HEADS + Constants.MASTER, branchesArray.getJSONObject(1).get(ProtocolConstants.KEY_FULL_NAME));
+ assertEquals(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + Constants.MASTER, branchesArray.getJSONObject(2).get(ProtocolConstants.KEY_FULL_NAME));
+ }
+ }
+
+ @Test
+ public void testLogWithParents() throws Exception {
+ URI workspaceLocation = createWorkspace(getMethodName());
+ JSONObject projectTop = createProjectOrLink(workspaceLocation, getMethodName() + "-top", null);
+ IPath clonePathTop = new Path("file").append(projectTop.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+
+ JSONObject projectFolder = createProjectOrLink(workspaceLocation, getMethodName() + "-folder", null);
+ IPath clonePathFolder = new Path("file").append(projectFolder.getString(ProtocolConstants.KEY_ID)).append("folder").makeAbsolute();
+
+ IPath[] clonePaths = new IPath[] {clonePathTop, clonePathFolder};
+
+ for (IPath clonePath : clonePaths) {
+ // clone a repo
+ JSONObject clone = clone(clonePath);
+ String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ // get project metadata
+ WebRequest request = getGetFilesRequest(cloneContentLocation);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject project = new JSONObject(response.getText());
+
+ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
+ String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX);
+ String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD);
+
+ // modify
+ String projectLocation = project.getString(ProtocolConstants.KEY_LOCATION);
+ request = getPutFileRequest(projectLocation + "test.txt", "first change");
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // add
+ request = GitAddTest.getPutGitIndexRequest(gitIndexUri + "test.txt");
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // commit1
+ request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit1", false);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // modify again
+ request = getPutFileRequest(projectLocation + "test.txt", "second change");
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // add
+ request = GitAddTest.getPutGitIndexRequest(gitIndexUri + "test.txt");
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // commit2
+ request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit2", false);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+
+ // get the full log
+ JSONArray commitsArray = log(gitHeadUri, false);
+ assertEquals(3, commitsArray.length());
+
+ JSONObject commit = commitsArray.getJSONObject(0);
+ assertEquals("commit2", commit.get(GitConstants.KEY_COMMIT_MESSAGE));
+ assertEquals(1, commit.getJSONArray(ProtocolConstants.KEY_PARENTS).length());
+ String parent = commit.getJSONArray(ProtocolConstants.KEY_PARENTS).getJSONObject(0).getString(ProtocolConstants.KEY_NAME);
+
+ commit = commitsArray.getJSONObject(1);
+ assertEquals("commit1", commit.get(GitConstants.KEY_COMMIT_MESSAGE));
+ assertEquals(parent, commit.get(ProtocolConstants.KEY_NAME));
+ assertEquals(1, commit.getJSONArray(ProtocolConstants.KEY_PARENTS).length());
+ parent = commit.getJSONArray(ProtocolConstants.KEY_PARENTS).getJSONObject(0).getString(ProtocolConstants.KEY_NAME);
+
+ commit = commitsArray.getJSONObject(2);
+ assertEquals("Initial commit", commit.get(GitConstants.KEY_COMMIT_MESSAGE));
+ assertEquals(parent, commit.get(ProtocolConstants.KEY_NAME));
+ assertEquals(0, commit.getJSONArray(ProtocolConstants.KEY_PARENTS).length());
+ }
}
@Test