Пример #1
0
  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));
  }
Пример #2
0
  // 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;
  }