private boolean handleDelete( HttpServletRequest request, HttpServletResponse response, String pathString) throws GitAPIException, CoreException, IOException, ServletException { IPath path = pathString == null ? Path.EMPTY : new Path(pathString); // expected path format is /file/{workspaceId}/{projectId}[/{directoryPath}] if (path.segment(0).equals("file") && path.segmentCount() > 2) { // $NON-NLS-1$ // make sure a clone is addressed WebProject webProject = GitUtils.projectFromPath(path); if (webProject != null && isAccessAllowed(request.getRemoteUser(), webProject)) { File gitDir = GitUtils.getGitDirs(path, Traverse.CURRENT).values().iterator().next(); Repository repo = new FileRepository(gitDir); repo.close(); FileUtils.delete(repo.getWorkTree(), FileUtils.RECURSIVE | FileUtils.RETRY); if (path.segmentCount() == 3) return statusHandler.handleRequest( request, response, removeProject(request.getRemoteUser(), webProject)); return true; } 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 delete request {0}", pathString); return statusHandler.handleRequest( request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null)); }
@Override protected IStatus performJob() { try { // list all tags File gitDir = GitUtils.getGitDir(path); Repository db = new FileRepository(gitDir); Git git = new Git(db); List<Ref> refs = git.tagList().call(); JSONObject result = new JSONObject(); List<Tag> tags = new ArrayList<Tag>(); for (Ref ref : refs) { Tag tag = new Tag(cloneLocation, db, ref); tags.add(tag); } Collections.sort(tags, Tag.COMPARATOR); JSONArray children = new JSONArray(); int firstTag = pageSize > 0 ? pageSize * (pageNo - 1) : 0; int lastTag = pageSize > 0 ? firstTag + pageSize - 1 : tags.size() - 1; lastTag = lastTag > tags.size() - 1 ? tags.size() - 1 : lastTag; if (pageNo > 1 && baseLocation != null) { String prev = baseLocation + "?page=" + (pageNo - 1) + "&pageSize=" + pageSize; if (commitsSize > 0) { prev += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize; } result.put(ProtocolConstants.KEY_PREVIOUS_LOCATION, prev); } if (lastTag < tags.size() - 1) { String next = baseLocation + "?page=" + (pageNo + 1) + "&pageSize=" + pageSize; if (commitsSize > 0) { next += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize; } result.put(ProtocolConstants.KEY_NEXT_LOCATION, next); } for (int i = firstTag; i <= lastTag; i++) { Tag tag = tags.get(i); if (this.commitsSize == 0) { children.put(tag.toJSON()); } else { // add info about commits if requested LogCommand lc = git.log(); String toCommitName = tag.getRevCommitName(); ObjectId toCommitId = db.resolve(toCommitName); Ref toCommitRef = db.getRef(toCommitName); toCommitId = getCommitObjectId(db, toCommitId); lc.add(toCommitId); lc.setMaxCount(this.commitsSize); Iterable<RevCommit> commits = lc.call(); Log log = new Log(cloneLocation, db, commits, null, null, toCommitRef); children.put(tag.toJSON(log.toJSON(1, commitsSize))); } } result.put(ProtocolConstants.KEY_CHILDREN, children); result.put(ProtocolConstants.KEY_TYPE, Tag.TYPE); return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result); } catch (Exception e) { String msg = NLS.bind("An error occured when listing tags for {0}", path); return new Status(IStatus.ERROR, GitActivator.PI_GIT, msg, e); } }
private boolean cherryPick( HttpServletRequest request, HttpServletResponse response, Repository db, String commitToCherryPick) throws ServletException, JSONException { RevWalk revWalk = new RevWalk(db); try { Ref headRef = db.getRef(Constants.HEAD); if (headRef == null) return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when cherry-picking.", null)); RevCommit head = revWalk.parseCommit(headRef.getObjectId()); ObjectId objectId = db.resolve(commitToCherryPick); Git git = new Git(db); CherryPickResult cherryPickResult = git.cherryPick().include(objectId).call(); RevCommit newHead = cherryPickResult.getNewHead(); JSONObject result = new JSONObject(); result.put(GitConstants.KEY_RESULT, cherryPickResult.getStatus().name()); result.put(GitConstants.KEY_HEAD_UPDATED, !head.equals(newHead)); OrionServlet.writeJSONResponse( request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT); return true; } catch (IOException e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when cherry-picking.", e)); } catch (GitAPIException e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when cherry-picking.", e)); } finally { revWalk.release(); } }
private AbstractTreeIterator getTreeIterator(Repository db, String name) throws IOException { final ObjectId id = db.resolve(name); if (id == null) throw new IllegalArgumentException(name); final CanonicalTreeParser p = new CanonicalTreeParser(); final ObjectReader or = db.newObjectReader(); try { p.reset(or, new RevWalk(db).parseTree(id)); return p; } finally { or.release(); } }
private boolean isLocalBranch(Git git, String branch) throws GitAPIException { List<Ref> branches = git.branchList().call(); for (Ref ref : branches) { if (Repository.shortenRefName(ref.getName()).equals(branch)) return true; } return false; }
// remove remote private boolean handleDelete( HttpServletRequest request, HttpServletResponse response, String path) throws CoreException, IOException, URISyntaxException, JSONException, ServletException { Path p = new Path(path); if (p.segment(1).equals("file")) { // $NON-NLS-1$ // expected path: /gitapi/remote/{remote}/file/{path} String remoteName = p.segment(0); File gitDir = GitUtils.getGitDir(p.removeFirstSegments(1)); Repository db = new FileRepository(gitDir); StoredConfig config = db.getConfig(); config.unsetSection(ConfigConstants.CONFIG_REMOTE_SECTION, remoteName); config.save(); // TODO: handle result return true; } return false; }
private boolean tag( HttpServletRequest request, HttpServletResponse response, Repository db, String commitId, String tagName, boolean isRoot) throws JSONException, URISyntaxException, ServletException { Git git = new Git(db); RevWalk walk = new RevWalk(db); try { ObjectId objectId = db.resolve(commitId); RevCommit revCommit = walk.lookupCommit(objectId); walk.parseBody(revCommit); GitTagHandlerV1.tag(git, revCommit, tagName); URI cloneLocation = BaseToCloneConverter.getCloneLocation( getURI(request), BaseToCloneConverter.COMMIT_REFRANGE); Commit commit = new Commit(cloneLocation, db, revCommit, null); JSONObject result = commit.toJSON(); OrionServlet.writeJSONResponse( request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT); return true; } catch (IOException e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when tagging.", e)); } catch (GitAPIException e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when tagging.", e)); } catch (CoreException e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when tagging.", e)); } finally { walk.dispose(); } }
private boolean merge( HttpServletRequest request, HttpServletResponse response, Repository db, String commitToMerge, boolean squash) throws ServletException, JSONException { try { ObjectId objectId = db.resolve(commitToMerge); Git git = new Git(db); MergeResult mergeResult = git.merge().setSquash(squash).include(objectId).call(); JSONObject result = new JSONObject(); result.put(GitConstants.KEY_RESULT, mergeResult.getMergeStatus().name()); if (mergeResult.getFailingPaths() != null && !mergeResult.getFailingPaths().isEmpty()) result.put(GitConstants.KEY_FAILING_PATHS, mergeResult.getFailingPaths()); OrionServlet.writeJSONResponse( request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT); return true; } catch (CheckoutConflictException e) { return workaroundBug356918(request, response, e); } catch (IOException e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when merging.", e)); } catch (GitAPIException e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when merging.", e)); } }
private boolean handleGetCommitBody( HttpServletRequest request, HttpServletResponse response, Repository db, String ref, String pattern) throws IOException, ServletException, CoreException { ObjectId refId = db.resolve(ref); if (refId == null) { String msg = NLS.bind("Failed to get commit body for ref {0}", ref); return statusHandler.handleRequest( request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null)); } RevWalk walk = new RevWalk(db); walk.setTreeFilter( AndTreeFilter.create( PathFilterGroup.createFromStrings(Collections.singleton(pattern)), TreeFilter.ANY_DIFF)); RevCommit revCommit = walk.parseCommit(refId); walk.dispose(); Commit commit = new Commit(null /* not needed */, db, revCommit, pattern); ObjectStream stream = commit.toObjectStream(); if (stream == null) { String msg = NLS.bind("Commit body for ref {0} not found", ref); return statusHandler.handleRequest( request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null)); } IOUtilities.pipe(stream, response.getOutputStream(), true, false); return true; }
private boolean handleGetDiff( HttpServletRequest request, HttpServletResponse response, Repository db, String scope, String pattern, OutputStream out) throws Exception { Git git = new Git(db); DiffCommand diff = git.diff(); diff.setOutputStream(new BufferedOutputStream(out)); AbstractTreeIterator oldTree; AbstractTreeIterator newTree = new FileTreeIterator(db); if (scope.contains("..")) { // $NON-NLS-1$ String[] commits = scope.split("\\.\\."); // $NON-NLS-1$ if (commits.length != 2) { String msg = NLS.bind("Failed to generate diff for {0}", scope); return statusHandler.handleRequest( request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null)); } oldTree = getTreeIterator(db, commits[0]); newTree = getTreeIterator(db, commits[1]); } else if (scope.equals(GitConstants.KEY_DIFF_CACHED)) { ObjectId head = db.resolve(Constants.HEAD + "^{tree}"); // $NON-NLS-1$ if (head == null) { String msg = NLS.bind("Failed to generate diff for {0}, no HEAD", scope); return statusHandler.handleRequest( request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null)); } CanonicalTreeParser p = new CanonicalTreeParser(); ObjectReader reader = db.newObjectReader(); try { p.reset(reader, head); } finally { reader.release(); } oldTree = p; newTree = new DirCacheIterator(db.readDirCache()); } else if (scope.equals(GitConstants.KEY_DIFF_DEFAULT)) { oldTree = new DirCacheIterator(db.readDirCache()); } else { oldTree = getTreeIterator(db, scope); } String[] paths = request.getParameterValues(ProtocolConstants.KEY_PATH); TreeFilter filter = null; TreeFilter pathFilter = null; if (paths != null) { if (paths.length > 1) { Set<TreeFilter> pathFilters = new HashSet<TreeFilter>(paths.length); for (String path : paths) { pathFilters.add(PathFilter.create(path)); } pathFilter = OrTreeFilter.create(pathFilters); } else if (paths.length == 1) { pathFilter = PathFilter.create(paths[0]); } } if (pattern != null) { PathFilter patternFilter = PathFilter.create(pattern); if (pathFilter != null) filter = AndTreeFilter.create(patternFilter, pathFilter); else filter = patternFilter; } else { filter = pathFilter; } if (filter != null) diff.setPathFilter(filter); diff.setOldTree(oldTree); diff.setNewTree(newTree); diff.call(); return true; }
private boolean revert( HttpServletRequest request, HttpServletResponse response, Repository db, String commitToRevert) throws ServletException, JSONException { RevWalk revWalk = new RevWalk(db); try { Ref headRef = db.getRef(Constants.HEAD); if (headRef == null) return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when reverting.", null)); ObjectId objectId = db.resolve(commitToRevert); Git git = new Git(db); RevertCommand revertCommand = git.revert().include(objectId); RevCommit revertedCommit = revertCommand.call(); if (revertedCommit == null) { JSONObject result = new JSONObject(); result.put(GitConstants.KEY_RESULT, "FAILURE"); // $NON-NLS-1$ OrionServlet.writeJSONResponse( request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT); return true; } JSONObject result = new JSONObject(); result.put(GitConstants.KEY_RESULT, "OK"); // $NON-NLS-1$ OrionServlet.writeJSONResponse( request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT); return true; } catch (IOException e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when reverting.", e)); } catch (GitAPIException e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when reverting.", e)); } finally { revWalk.release(); } }
private boolean handleGet(HttpServletRequest request, HttpServletResponse response, String path) throws IOException, JSONException, ServletException, URISyntaxException, CoreException { Path p = new Path(path); // FIXME: what if a remote or branch is named "file"? if (p.segment(0).equals("file")) { // $NON-NLS-1$ // /git/remote/file/{path} File gitDir = GitUtils.getGitDir(p); Repository db = new FileRepository(gitDir); Set<String> configNames = db.getConfig().getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION); JSONObject result = new JSONObject(); JSONArray children = new JSONArray(); URI cloneLocation = BaseToCloneConverter.getCloneLocation(getURI(request), BaseToCloneConverter.REMOTE_LIST); for (String configName : configNames) { Remote remote = new Remote(cloneLocation, db, configName); children.put(remote.toJSON(false, null)); } result.put(ProtocolConstants.KEY_CHILDREN, children); result.put(ProtocolConstants.KEY_TYPE, Remote.TYPE); OrionServlet.writeJSONResponse(request, response, result); return true; } else if (p.segment(1).equals("file")) { // $NON-NLS-1$ // /git/remote/{remote}/file/{path} RemoteDetailsJob job; String commits = request.getParameter(GitConstants.KEY_TAG_COMMITS); int commitsNumber = commits == null ? 0 : Integer.parseInt(commits); String page = request.getParameter("page"); if (page != null) { int pageNo = Integer.parseInt(page); int pageSize = request.getParameter("pageSize") == null ? PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize")); job = new RemoteDetailsJob( TaskJobHandler.getUserId(request), p.segment(0), p.removeFirstSegments(1), BaseToCloneConverter.getCloneLocation(getURI(request), BaseToCloneConverter.REMOTE), commitsNumber, pageNo, pageSize, request.getRequestURI()); } else { job = new RemoteDetailsJob( TaskJobHandler.getUserId(request), p.segment(0), p.removeFirstSegments(1), BaseToCloneConverter.getCloneLocation(getURI(request), BaseToCloneConverter.REMOTE), commitsNumber); } return TaskJobHandler.handleTaskJob(request, response, job, statusHandler); } else if (p.segment(2).equals("file")) { // $NON-NLS-1$ // /git/remote/{remote}/{branch}/file/{path} File gitDir = GitUtils.getGitDir(p.removeFirstSegments(2)); Repository db = new FileRepository(gitDir); URI cloneLocation = BaseToCloneConverter.getCloneLocation( getURI(request), BaseToCloneConverter.REMOTE_BRANCH); Remote remote = new Remote(cloneLocation, db, p.segment(0)); RemoteBranch remoteBranch = new RemoteBranch(cloneLocation, db, remote, p.segment(1)); JSONObject result = remoteBranch.toJSON(); if (result != null) { OrionServlet.writeJSONResponse(request, response, result); return true; } JSONObject errorData = new JSONObject(); errorData.put(GitConstants.KEY_CLONE, cloneLocation); return statusHandler.handleRequest( request, response, new ServerStatus( new Status( IStatus.ERROR, GitActivator.PI_GIT, "No remote branch found: " + p.uptoSegment(2).removeTrailingSeparator()), HttpServletResponse.SC_NOT_FOUND, errorData)); } return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Bad request, \"/git/remote/{remote}/{branch}/file/{path}\" expected", null)); }
private boolean rebase( HttpServletRequest request, HttpServletResponse response, Repository db, String commitToRebase, String rebaseOperation) throws ServletException, JSONException, AmbiguousObjectException, IOException { JSONObject result = new JSONObject(); try { Git git = new Git(db); RebaseCommand rebase = git.rebase(); Operation operation; if (rebaseOperation != null) { operation = Operation.valueOf(rebaseOperation); } else { operation = Operation.BEGIN; } if (commitToRebase != null && !commitToRebase.isEmpty()) { ObjectId objectId = db.resolve(commitToRebase); rebase.setUpstream(objectId); } else if (operation.equals(Operation.BEGIN)) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Missing commit refId.", null)); } rebase.setOperation(operation); RebaseResult rebaseResult = rebase.call(); result.put(GitConstants.KEY_RESULT, rebaseResult.getStatus().name()); } catch (UnmergedPathsException e) { // this error should be handled by client, so return a proper status result.put(GitConstants.KEY_RESULT, AdditionalRebaseStatus.FAILED_UNMERGED_PATHS.name()); } catch (WrongRepositoryStateException e) { // this error should be handled by client, so return a proper status result.put( GitConstants.KEY_RESULT, AdditionalRebaseStatus.FAILED_WRONG_REPOSITORY_STATE.name()); } catch (IllegalArgumentException e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Invalid rebase operation.", e)); } catch (GitAPIException e) { // get cause and try to handle if (e.getCause() instanceof org.eclipse.jgit.errors.CheckoutConflictException) { // this error should be handled by client, so return a proper status result.put(GitConstants.KEY_RESULT, AdditionalRebaseStatus.FAILED_PENDING_CHANGES.name()); } else { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when rebasing.", e)); } } OrionServlet.writeJSONResponse( request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT); return true; }
@Override protected boolean handlePost(RequestInfo requestInfo) throws ServletException { String gitSegment = requestInfo.gitSegment; HttpServletRequest request = requestInfo.request; HttpServletResponse response = requestInfo.response; Repository db = requestInfo.db; String pattern = requestInfo.relativePath; JSONObject requestObject = requestInfo.getJSONRequest(); try { String commitToMerge = requestObject.optString(GitConstants.KEY_MERGE, null); if (commitToMerge != null) { boolean squash = requestObject.optBoolean(GitConstants.KEY_SQUASH, false); return merge(request, response, db, commitToMerge, squash); } String commitToRebase = requestObject.optString(GitConstants.KEY_REBASE, null); String rebaseOperation = requestObject.optString(GitConstants.KEY_OPERATION, null); if (commitToRebase != null) { return rebase(request, response, db, commitToRebase, rebaseOperation); } String commitToCherryPick = requestObject.optString(GitConstants.KEY_CHERRY_PICK, null); if (commitToCherryPick != null) { return cherryPick(request, response, db, commitToCherryPick); } String commitToRevert = requestObject.optString(GitConstants.KEY_REVERT, null); if (commitToRevert != null) { return revert(request, response, db, commitToRevert); } String newCommit = requestObject.optString(GitConstants.KEY_COMMIT_NEW, null); if (newCommit != null) return identifyNewCommitResource(request, response, db, newCommit); String reviewReqLogin = requestObject.optString(GitConstants.KEY_REVIEW_REQ_NOTIFY_LOGIN); if (reviewReqLogin != null && reviewReqLogin.length() != 0) { String reviewReqUrl = requestObject.optString(GitConstants.KEY_REVIEW_REQ_URL); String ReviewReqCommit = requestObject.optString(GitConstants.KEY_REVIEW_REQ_COMMIT); String ReviewReqAuthorName = requestObject.optString(GitConstants.KEY_REVIEW_REQ_AUTHOR_NAME); String ReviewMessage = requestObject.optString(GitConstants.KEY_REVIEW_REQ_MESSAGE); return sendNotification( request, response, db, reviewReqLogin, ReviewReqCommit, reviewReqUrl, ReviewReqAuthorName, ReviewMessage); } ObjectId refId = db.resolve(gitSegment); if (refId == null || !Constants.HEAD.equals(gitSegment)) { String msg = NLS.bind("Commit failed. Ref must be HEAD and is {0}", gitSegment); return statusHandler.handleRequest( request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null)); } String message = requestObject.optString(GitConstants.KEY_COMMIT_MESSAGE, null); if (message == null || message.isEmpty()) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Missing commit message.", null)); } Git git = new Git(db); CommitCommand cc = git.commit(); Config config = git.getRepository().getConfig(); boolean amend = Boolean.parseBoolean(requestObject.optString(GitConstants.KEY_COMMIT_AMEND, null)); boolean insertChangeId = GitUtils.isGerrit(config) || Boolean.parseBoolean(requestObject.optString(GitConstants.KEY_CHANGE_ID, null)); String committerName = requestObject.optString(GitConstants.KEY_COMMITTER_NAME, null); String committerEmail = requestObject.optString(GitConstants.KEY_COMMITTER_EMAIL, null); String authorName = requestObject.optString(GitConstants.KEY_AUTHOR_NAME, null); String authorEmail = requestObject.optString(GitConstants.KEY_AUTHOR_EMAIL, null); // workaround of a bug in JGit which causes invalid // support of null values of author/committer name/email, see bug 352984 PersonIdent defPersonIdent = new PersonIdent(db); if (committerName == null) committerName = defPersonIdent.getName(); if (committerEmail == null) committerEmail = defPersonIdent.getEmailAddress(); if (authorName == null) authorName = committerName; if (authorEmail == null) authorEmail = committerEmail; cc.setCommitter(committerName, committerEmail); cc.setAuthor(authorName, authorEmail); if (insertChangeId) cc.setInsertChangeId(true); // support for committing by path: "git commit -o path" if (!pattern.isEmpty()) { cc.setOnly(pattern); } try { // "git commit [--amend] -m '{message}' [-a|{path}]" RevCommit lastCommit = cc.setAmend(amend).setMessage(message).call(); URI cloneLocation = BaseToCloneConverter.getCloneLocation( getURI(request), BaseToCloneConverter.COMMIT_REFRANGE); Commit commit = new Commit(cloneLocation, db, lastCommit, pattern); JSONObject result = commit.toJSON(); OrionServlet.writeJSONResponse( request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT); return true; } catch (GitAPIException e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "An error occured when commiting.", e)); } catch (UnmergedPathException e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An internal error occured when commiting.", e)); } } catch (Exception e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when requesting a commit info.", e)); } }
private boolean handleGetCommitLog( HttpServletRequest request, HttpServletResponse response, IPath filePath, Repository db, String refIdsRange, String pattern) throws AmbiguousObjectException, IOException, ServletException, JSONException, URISyntaxException, CoreException { int page = request.getParameter("page") != null ? new Integer(request.getParameter("page")).intValue() : 0; //$NON-NLS-1$ //$NON-NLS-2$ int pageSize = request.getParameter("pageSize") != null ? new Integer(request.getParameter("pageSize")).intValue() : PAGE_SIZE; //$NON-NLS-1$ //$NON-NLS-2$ ObjectId toObjectId = null; ObjectId fromObjectId = null; Ref toRefId = null; Ref fromRefId = 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)); } } 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)); } } toObjectId = getCommitObjectId(db, toObjectId); } URI baseLocation = getURI(request); URI cloneLocation = BaseToCloneConverter.getCloneLocation( baseLocation, refIdsRange == null ? BaseToCloneConverter.COMMIT : BaseToCloneConverter.COMMIT_REFRANGE); LogJob job = new LogJob( TaskJobHandler.getUserId(request), filePath, cloneLocation, page, pageSize, toObjectId, fromObjectId, toRefId, fromRefId, refIdsRange, pattern); return TaskJobHandler.handleTaskJob( request, response, job, statusHandler, JsonURIUnqualificationStrategy.ALL_NO_GIT); }
// add new remote private boolean addRemote(HttpServletRequest request, HttpServletResponse response, String path) throws IOException, JSONException, ServletException, CoreException, URISyntaxException { // expected path: /git/remote/file/{path} Path p = new Path(path); JSONObject toPut = OrionServlet.readJSONRequest(request); String remoteName = toPut.optString(GitConstants.KEY_REMOTE_NAME, null); // remoteName is required if (remoteName == null || remoteName.isEmpty()) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Remote name must be provided", null)); } String remoteURI = toPut.optString(GitConstants.KEY_REMOTE_URI, null); // remoteURI is required if (remoteURI == null || remoteURI.isEmpty()) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Remote URI must be provided", null)); } String fetchRefSpec = toPut.optString(GitConstants.KEY_REMOTE_FETCH_REF, null); String remotePushURI = toPut.optString(GitConstants.KEY_REMOTE_PUSH_URI, null); String pushRefSpec = toPut.optString(GitConstants.KEY_REMOTE_PUSH_REF, null); File gitDir = GitUtils.getGitDir(p); Repository db = new FileRepository(gitDir); StoredConfig config = db.getConfig(); RemoteConfig rc = new RemoteConfig(config, remoteName); rc.addURI(new URIish(remoteURI)); // FetchRefSpec is required, but default version can be generated // if it isn't provided if (fetchRefSpec == null || fetchRefSpec.isEmpty()) { fetchRefSpec = String.format("+refs/heads/*:refs/remotes/%s/*", remoteName); // $NON-NLS-1$ } rc.addFetchRefSpec(new RefSpec(fetchRefSpec)); // pushURI is optional if (remotePushURI != null && !remotePushURI.isEmpty()) rc.addPushURI(new URIish(remotePushURI)); // PushRefSpec is optional if (pushRefSpec != null && !pushRefSpec.isEmpty()) rc.addPushRefSpec(new RefSpec(pushRefSpec)); rc.update(config); config.save(); URI cloneLocation = BaseToCloneConverter.getCloneLocation(getURI(request), BaseToCloneConverter.REMOTE_LIST); Remote remote = new Remote(cloneLocation, db, remoteName); JSONObject result = new JSONObject(); result.put(ProtocolConstants.KEY_LOCATION, remote.getLocation()); OrionServlet.writeJSONResponse(request, response, result); response.setHeader( ProtocolConstants.HEADER_LOCATION, result.getString(ProtocolConstants.KEY_LOCATION)); response.setStatus(HttpServletResponse.SC_CREATED); return true; }
private boolean isInIndex(Repository db, String path) throws IOException { DirCache dc = DirCache.read(db.getIndexFile(), db.getFS()); return dc.getEntry(path) != null; }
private String stripGlobalPaths(Repository db, String message) { return message.replaceAll( "(?i)" + Pattern.quote(db.getDirectory().getParentFile().getAbsolutePath().toLowerCase()), ""); }