aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPiotr Janik2011-07-28 10:16:51 (EDT)
committerTomasz Zarna2011-07-28 10:16:51 (EDT)
commitad2d1b99ea9caad83ce82a2ee6b9cb1a2f043ecc (patch)
tree53f7ee5ed9b045bb0a11ae34f43a1d3697d4dd51
parente228ebefe879ce526a16da61ada4750574a2b1f2 (diff)
downloadorg.eclipse.orion.server-ad2d1b99ea9caad83ce82a2ee6b9cb1a2f043ecc.zip
org.eclipse.orion.server-ad2d1b99ea9caad83ce82a2ee6b9cb1a2f043ecc.tar.gz
org.eclipse.orion.server-ad2d1b99ea9caad83ce82a2ee6b9cb1a2f043ecc.tar.bz2
bug 353088 - Implement APi and UI for Git log --all
https://bugs.eclipse.org/bugs/show_bug.cgi?id=353088
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/BaseToCloneConverter.java11
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitCloneHandlerV1.java4
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitCommitHandlerV1.java124
-rw-r--r--tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitLogTest.java100
-rw-r--r--tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitTest.java27
5 files changed, 209 insertions, 57 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 9a8c66c..e0d01fe 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
@@ -39,6 +39,8 @@ public abstract class BaseToCloneConverter {
};
};
+ public static final BaseToCloneConverter COMMIT = STATUS;
+
public static final BaseToCloneConverter REMOTE = new BaseToCloneConverter() {
@Override
public IPath getFilePath(URI base) throws URISyntaxException {
@@ -46,12 +48,9 @@ public abstract class BaseToCloneConverter {
};
};
- public static final BaseToCloneConverter BRANCH = new BaseToCloneConverter() {
- @Override
- public IPath getFilePath(URI base) throws URISyntaxException {
- return new Path(base.getPath()).removeFirstSegments(3).makeRelative();
- };
- };
+ public static final BaseToCloneConverter BRANCH = REMOTE;
+
+ public static final BaseToCloneConverter COMMIT_REFRANGE = REMOTE;
public static final BaseToCloneConverter REMOTE_BRANCH = new BaseToCloneConverter() {
@Override
diff --git a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitCloneHandlerV1.java b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitCloneHandlerV1.java
index bd84ead..542f17c 100644
--- a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitCloneHandlerV1.java
+++ b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitCloneHandlerV1.java
@@ -500,6 +500,10 @@ public class GitCloneHandlerV1 extends ServletResourceHandler<String> {
location = new URI(baseLocation.getScheme(), baseLocation.getUserInfo(), baseLocation.getHost(), baseLocation.getPort(), np.toString(), baseLocation.getQuery(), baseLocation.getFragment());
result.put(GitConstants.KEY_HEAD, location);
+ np = new Path(GitServlet.GIT_URI).append(GitConstants.COMMIT_RESOURCE).append("file").append(k); //$NON-NLS-1$
+ location = new URI(baseLocation.getScheme(), baseLocation.getUserInfo(), baseLocation.getHost(), baseLocation.getPort(), np.toString(), baseLocation.getQuery(), baseLocation.getFragment());
+ result.put(GitConstants.KEY_COMMIT, location);
+
np = new Path(GitServlet.GIT_URI).append(GitConstants.BRANCH_RESOURCE).append("file").append(k); //$NON-NLS-1$
location = new URI(baseLocation.getScheme(), baseLocation.getUserInfo(), baseLocation.getHost(), baseLocation.getPort(), np.toString(), baseLocation.getQuery(), baseLocation.getFragment());
result.put(GitConstants.KEY_BRANCH, location);
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 a297c01..28259ac 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
@@ -102,24 +102,39 @@ public class GitCommitHandlerV1 extends ServletResourceHandler<String> {
}
private boolean handleGet(HttpServletRequest request, HttpServletResponse response, Repository db, Path path) throws CoreException, IOException, ServletException, JSONException, URISyntaxException {
- IPath filePath = path.hasTrailingSeparator() ? path.removeFirstSegments(1) : path.removeFirstSegments(1).removeLastSegments(1);
- Set<Entry<IPath, File>> set = GitUtils.getGitDirs(filePath, Traverse.GO_UP).entrySet();
- File gitDir = set.iterator().next().getValue();
- if (gitDir == null)
- return false; // TODO: or an error response code, 405?
-
- db = new FileRepository(gitDir);
-
- // /{ref}/file/{projectId}...}
- String parts = request.getParameter("parts"); //$NON-NLS-1$
- String pattern = GitUtils.getRelativePath(path, set.iterator().next().getKey());
- if (path.segmentCount() > 3 && "body".equals(parts)) { //$NON-NLS-1$
- return handleGetCommitBody(request, response, db, path.segment(0), pattern);
- }
- if (path.segmentCount() > 2 && (parts == null || "log".equals(parts))) { //$NON-NLS-1$
- return handleGetCommitLog(request, response, db, path.segment(0), pattern);
+ if (path.segment(0).equals("file")) { //$NON-NLS-1$
+ // special case for git log --all
+ IPath filePath = path.hasTrailingSeparator() ? path : path.removeLastSegments(1);
+ Set<Entry<IPath, File>> set = GitUtils.getGitDirs(filePath, Traverse.GO_UP).entrySet();
+ File gitDir = set.iterator().next().getValue();
+ if (gitDir == null)
+ return false; // TODO: or an error response code, 405?
+
+ db = new FileRepository(gitDir);
+
+ String pattern = GitUtils.getRelativePath(path, set.iterator().next().getKey());
+ return handleGetCommitLog(request, response, db, null, pattern);
+
+ } else if (path.segment(1).equals("file")) { //$NON-NLS-1$
+ // git log branch_name
+ IPath filePath = path.hasTrailingSeparator() ? path.removeFirstSegments(1) : path.removeFirstSegments(1).removeLastSegments(1);
+ Set<Entry<IPath, File>> set = GitUtils.getGitDirs(filePath, Traverse.GO_UP).entrySet();
+ File gitDir = set.iterator().next().getValue();
+ if (gitDir == null)
+ return false; // TODO: or an error response code, 405?
+
+ db = new FileRepository(gitDir);
+
+ // /{ref}/file/{projectId}...}
+ String parts = request.getParameter("parts"); //$NON-NLS-1$
+ String pattern = GitUtils.getRelativePath(path, set.iterator().next().getKey());
+ if (path.segmentCount() > 3 && "body".equals(parts)) { //$NON-NLS-1$
+ return handleGetCommitBody(request, response, db, path.segment(0), pattern);
+ }
+ if (path.segmentCount() > 2 && (parts == null || "log".equals(parts))) { //$NON-NLS-1$
+ return handleGetCommitLog(request, response, db, path.segment(0), pattern);
+ }
}
-
return false;
}
@@ -155,42 +170,54 @@ public class GitCommitHandlerV1 extends ServletResourceHandler<String> {
Ref toRefId = null;
Ref fromRefId = null;
- if (refIdsRange.contains("..")) { //$NON-NLS-1$
- String[] commits = refIdsRange.split("\\.\\."); //$NON-NLS-1$
- if (commits.length != 2) {
- String msg = NLS.bind("Failed to generate commit log for ref {0}", refIdsRange);
- return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
- }
+ Git git = new Git(db);
+ LogCommand log = git.log();
- fromObjectId = db.resolve(commits[0]);
- fromRefId = db.getRef(commits[0]);
- if (fromObjectId == null) {
- String msg = NLS.bind("Failed to generate commit log for ref {0}", commits[0]);
- return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
- }
+ if (refIdsRange != null) {
+ // git log <since>..<until>
+ if (refIdsRange.contains("..")) { //$NON-NLS-1$
+ String[] commits = refIdsRange.split("\\.\\."); //$NON-NLS-1$
+ if (commits.length != 2) {
+ String msg = NLS.bind("Failed to generate commit log for ref {0}", refIdsRange);
+ return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
+ }
+
+ fromObjectId = db.resolve(commits[0]);
+ fromRefId = db.getRef(commits[0]);
+ if (fromObjectId == null) {
+ String msg = NLS.bind("Failed to generate commit log for ref {0}", commits[0]);
+ return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
+ }
- toObjectId = db.resolve(commits[1]);
- toRefId = db.getRef(commits[1]);
- if (toObjectId == null) {
- String msg = NLS.bind("No ref or commit found: {0}", commits[1]);
- return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null));
+ toObjectId = db.resolve(commits[1]);
+ toRefId = db.getRef(commits[1]);
+ if (toObjectId == null) {
+ String msg = NLS.bind("No ref or commit found: {0}", commits[1]);
+ return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null));
+ }
+ } else {
+ toObjectId = db.resolve(refIdsRange);
+ toRefId = db.getRef(refIdsRange);
+ if (toObjectId == null) {
+ String msg = NLS.bind("No ref or commit found: {0}", refIdsRange);
+ return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null));
+ }
}
+
+ // set the commit range
+ log.add(toObjectId);
+
+ if (fromObjectId != null)
+ log.not(fromObjectId);
} else {
- toObjectId = db.resolve(refIdsRange);
- toRefId = db.getRef(refIdsRange);
- if (toObjectId == null) {
- String msg = NLS.bind("No ref or commit found: {0}", refIdsRange);
- return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null));
+ // git log --all
+ // workaround for git log --all - see bug 353310
+ List<Ref> branches = git.branchList().setListMode(ListMode.ALL).call();
+ for (Ref branch : branches) {
+ log.add(branch.getObjectId());
}
}
- Git git = new Git(db);
- LogCommand log = git.log();
- // set the commit range
- log.add(toObjectId);
- if (fromObjectId != null)
- log.not(fromObjectId);
-
// set the path filter
TreeFilter filter = null;
@@ -207,6 +234,10 @@ public class GitCommitHandlerV1 extends ServletResourceHandler<String> {
JSONObject result = toJSON(db, OrionServlet.getURI(request), commits, commitToBranchMap, page, pageSize, filter, isRoot);
result.put(GitConstants.KEY_REPOSITORY_PATH, isRoot ? "" : path); //$NON-NLS-1$
+ if (refIdsRange == null)
+ result.put(GitConstants.KEY_CLONE, BaseToCloneConverter.getCloneLocation(getURI(request), BaseToCloneConverter.COMMIT));
+ else
+ result.put(GitConstants.KEY_CLONE, BaseToCloneConverter.getCloneLocation(getURI(request), BaseToCloneConverter.COMMIT_REFRANGE));
if (toRefId != null) {
result.put(GitConstants.KEY_REMOTE, BaseToRemoteConverter.getRemoteBranchLocation(getURI(request), Repository.shortenRefName(toRefId.getName()), db, BaseToRemoteConverter.REMOVE_FIRST_3));
@@ -224,6 +255,7 @@ public class GitCommitHandlerV1 extends ServletResourceHandler<String> {
result.put(GitConstants.KEY_LOG_FROM_REF, BranchToJSONConverter.toJSON(fromRefId.getTarget(), db, getURI(request), 3));
}
}
+
OrionServlet.writeJSONResponse(request, response, result);
return true;
} catch (NoHeadException e) {
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 2dc2e05..f1d219e 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
@@ -335,6 +335,106 @@ public class GitLogTest extends GitTest {
}
@Test
+ public void testLogAllBranches() 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 cloneLocation = clone.getString(ProtocolConstants.KEY_LOCATION);
+ String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+ String branchesLocation = clone.getString(GitConstants.KEY_BRANCH);
+ String gitCommitUri = clone.getString(GitConstants.KEY_COMMIT);
+
+ // 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);
+
+ // create branch
+ final String newBranchName = "branch";
+ branch(branchesLocation, newBranchName);
+
+ // 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());
+
+ // checkout "branch"
+ checkoutBranch(cloneLocation, newBranchName);
+
+ // 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 standard log for HEAD - only init and commit2 should be visible
+ JSONArray commitsArray = log(gitHeadUri, false);
+ assertEquals(2, commitsArray.length());
+
+ JSONObject commit = commitsArray.getJSONObject(0);
+ assertEquals("commit2", commit.get(GitConstants.KEY_COMMIT_MESSAGE));
+
+ commit = commitsArray.getJSONObject(1);
+ assertEquals("Initial commit", commit.get(GitConstants.KEY_COMMIT_MESSAGE));
+
+ // get log for all branches - initial commit, commit1 and commit2 should be visible
+ commitsArray = log(gitCommitUri, false);
+ assertEquals(3, commitsArray.length());
+
+ commit = commitsArray.getJSONObject(0);
+ assertEquals("commit2", commit.get(GitConstants.KEY_COMMIT_MESSAGE));
+ JSONArray branchesArray = commit.getJSONArray(GitConstants.KEY_BRANCHES);
+ assertEquals(1, branchesArray.length());
+ assertEquals(Constants.R_HEADS + newBranchName, branchesArray.getJSONObject(0).get(ProtocolConstants.KEY_FULL_NAME));
+
+ commit = commitsArray.getJSONObject(1);
+ assertEquals("commit1", commit.get(GitConstants.KEY_COMMIT_MESSAGE));
+ branchesArray = commit.getJSONArray(GitConstants.KEY_BRANCHES);
+ assertEquals(1, branchesArray.length());
+ assertEquals(Constants.R_HEADS + Constants.MASTER, branchesArray.getJSONObject(0).get(ProtocolConstants.KEY_FULL_NAME));
+
+ commit = commitsArray.getJSONObject(2);
+ assertEquals("Initial commit", commit.get(GitConstants.KEY_COMMIT_MESSAGE));
+ branchesArray = commit.getJSONArray(GitConstants.KEY_BRANCHES);
+ assertEquals(1, branchesArray.length());
+ assertEquals(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + Constants.MASTER, branchesArray.getJSONObject(0).get(ProtocolConstants.KEY_FULL_NAME));
+ }
+ }
+
+ @Test
public void testLogFolder() throws Exception {
URI workspaceLocation = createWorkspace(getMethodName());
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 c32a0a3..02a54b2 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
@@ -807,11 +807,28 @@ public abstract class GitTest extends FileSystemTest {
protected static void assertCommitUri(String commitUri) {
URI uri = URI.create(commitUri);
IPath path = new Path(uri.getPath());
- // /git/commit/{ref}/file/{path}
- assertTrue(path.segmentCount() > 4);
- assertEquals(GitServlet.GIT_URI.substring(1), path.segment(0));
- assertEquals(GitConstants.COMMIT_RESOURCE, path.segment(1));
- assertEquals("file", path.segment(3));
+ AssertionError error = null;
+ // /gitapi/commit/{ref}/file/{path}
+ try {
+ assertTrue(path.segmentCount() > 4);
+ assertEquals(GitServlet.GIT_URI.substring(1), path.segment(0));
+ assertEquals(GitConstants.COMMIT_RESOURCE, path.segment(1));
+ assertEquals("file", path.segment(3));
+ } catch (AssertionError e) {
+ error = e;
+ }
+
+ // /gitapi/commit/file/{path}
+ try {
+ assertTrue(path.segmentCount() > 3);
+ assertEquals(GitServlet.GIT_URI.substring(1), path.segment(0));
+ assertEquals(GitConstants.COMMIT_RESOURCE, path.segment(1));
+ assertEquals("file", path.segment(2));
+ } catch (AssertionError e) {
+ if (error != null) {
+ throw error; // rethrow the first exception
+ } // otherwise it's a commit location, ignore this exception
+ }
}
static void assertStatusUri(String statusUri) {