@Test public void testReadFileMetadata() throws Exception { String directoryPath = "sample/directory/path" + System.currentTimeMillis(); createDirectory(directoryPath); String fileName = "sampleFile" + System.currentTimeMillis() + ".txt"; String fileContent = "Sample File Cotnent " + System.currentTimeMillis(); createFile(directoryPath + "/" + fileName, fileContent); WebRequest request = getGetFilesRequest(directoryPath + "/" + fileName + "?parts=meta"); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject result = new JSONObject(response.getText()); assertEquals(fileName, result.optString(ProtocolConstants.KEY_NAME)); JSONArray parents = result.optJSONArray(ProtocolConstants.KEY_PARENTS); assertNotNull(parents); assertEquals(3, parents.length()); IPath parentPath = new Path(directoryPath); // immediate parent JSONObject parent = parents.getJSONObject(0); assertEquals(parentPath.segment(2), parent.getString(ProtocolConstants.KEY_NAME)); // grandparent parent = parents.getJSONObject(1); assertEquals(parentPath.segment(1), parent.getString(ProtocolConstants.KEY_NAME)); // ensure all parent locations end with trailing slash for (int i = 0; i < parents.length(); i++) { parent = parents.getJSONObject(i); String location = parent.getString(ProtocolConstants.KEY_LOCATION); assertTrue(location.endsWith("/")); location = parent.getString(ProtocolConstants.KEY_CHILDREN_LOCATION); URI childrenLocation = new URI(location); assertTrue(childrenLocation.getPath().endsWith("/")); } }
private boolean handlePut( HttpServletRequest request, HttpServletResponse response, String pathString) throws GitAPIException, CoreException, IOException, JSONException, ServletException { IPath path = pathString == null ? Path.EMPTY : new Path(pathString); if (path.segment(0).equals("file") && path.segmentCount() > 1) { // $NON-NLS-1$ // make sure a clone is addressed WebProject webProject = GitUtils.projectFromPath(path); if (isAccessAllowed(request.getRemoteUser(), webProject)) { Map<IPath, File> gitDirs = GitUtils.getGitDirs(path, Traverse.CURRENT); if (gitDirs.isEmpty()) { String msg = NLS.bind("Request path is not a git repository: {0}", path); return statusHandler.handleRequest( request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null)); } File gitDir = gitDirs.values().iterator().next(); // make sure required fields are set JSONObject toCheckout = OrionServlet.readJSONRequest(request); JSONArray paths = toCheckout.optJSONArray(ProtocolConstants.KEY_PATH); String branch = toCheckout.optString(GitConstants.KEY_BRANCH_NAME, null); String tag = toCheckout.optString(GitConstants.KEY_TAG_NAME, null); boolean removeUntracked = toCheckout.optBoolean(GitConstants.KEY_REMOVE_UNTRACKED, false); if ((paths == null || paths.length() == 0) && branch == null && tag == null) { String msg = NLS.bind( "Either '{0}' or '{1}' or '{2}' should be provided, got: {3}", new Object[] { ProtocolConstants.KEY_PATH, GitConstants.KEY_BRANCH_NAME, GitConstants.KEY_TAG_NAME, toCheckout }); return statusHandler.handleRequest( request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null)); } Git git = new Git(new FileRepository(gitDir)); if (paths != null) { Set<String> toRemove = new HashSet<String>(); CheckoutCommand checkout = git.checkout(); for (int i = 0; i < paths.length(); i++) { String p = paths.getString(i); if (removeUntracked && !isInIndex(git.getRepository(), p)) toRemove.add(p); checkout.addPath(p); } checkout.call(); for (String p : toRemove) { File f = new File(git.getRepository().getWorkTree(), p); f.delete(); } return true; } else if (tag != null && branch != null) { CheckoutCommand co = git.checkout(); try { co.setName(branch).setStartPoint(tag).setCreateBranch(true).call(); return true; } catch (RefNotFoundException e) { String msg = NLS.bind("Tag not found: {0}", tag); return statusHandler.handleRequest( request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, e)); } catch (GitAPIException e) { if (org.eclipse.jgit.api.CheckoutResult.Status.CONFLICTS.equals( co.getResult().getStatus())) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_CONFLICT, "Checkout aborted.", e)); } // TODO: handle other exceptions } } else if (branch != null) { if (!isLocalBranch(git, branch)) { String msg = NLS.bind("{0} is not a branch.", branch); return statusHandler.handleRequest( request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null)); } CheckoutCommand co = git.checkout(); try { co.setName(Constants.R_HEADS + branch).call(); return true; } catch (CheckoutConflictException e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_CONFLICT, "Checkout aborted.", e)); } catch (RefNotFoundException e) { String msg = NLS.bind("Branch name not found: {0}", branch); return statusHandler.handleRequest( request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, e)); } // TODO: handle other exceptions } } else { String msg = NLS.bind("Nothing found for the given ID: {0}", path); return statusHandler.handleRequest( request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null)); } } String msg = NLS.bind("Invalid checkout request {0}", pathString); return statusHandler.handleRequest( request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null)); }