예제 #1
0
  // 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;
  }
예제 #2
0
 public static void doConfigureClone(Git git, String user) throws IOException, CoreException {
   StoredConfig config = git.getRepository().getConfig();
   IOrionUserProfileNode userNode =
       UserServiceHelper.getDefault()
           .getUserProfileService()
           .getUserProfileNode(user, true)
           .getUserProfileNode(IOrionUserProfileConstants.GENERAL_PROFILE_PART);
   if (userNode.get(GitConstants.KEY_NAME, null) != null)
     config.setString(
         ConfigConstants.CONFIG_USER_SECTION,
         null,
         ConfigConstants.CONFIG_KEY_NAME,
         userNode.get(GitConstants.KEY_NAME, null));
   if (userNode.get(GitConstants.KEY_MAIL, null) != null)
     config.setString(
         ConfigConstants.CONFIG_USER_SECTION,
         null,
         ConfigConstants.CONFIG_KEY_EMAIL,
         userNode.get(GitConstants.KEY_MAIL, null));
   config.setBoolean(
       ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_FILEMODE, false);
   config.save();
 }
  // 2. Get branch/commit hash for the source repo - the actual source code
  @Test
  public void existsIn2() throws IOException {
    final String repo = "binrepo-devex";
    //  final String repo = "search_raptor_binary";
    // String githubUrl = "https://github.scm.corp.ebay.com/api/v3";
    // String accessToken = "1cf7d9792235b8592eda18bd7dcc2de37f99b3bc";

    final String path = "D:\\dev\\devex\\.binrepo-devex\\.git";

    File gitDir = new File(path);
    org.eclipse.jgit.lib.Repository repository =
        new org.eclipse.jgit.storage.file.FileRepository(gitDir);
    String branch = repository.getBranch();
    System.out.println("Branch=" + branch);
    final Map<String, Ref> allRefs = repository.getAllRefs();
    for (String s : allRefs.keySet()) {
      System.out.println("Here" + s);
    }

    RevWalk revWalk = new RevWalk(repository);
    ObjectId resolve = repository.resolve(Constants.HEAD);
    RevCommit commitRev = revWalk.parseCommit(resolve);
    String commitHash = commitRev.getName();
    System.out.println(commitHash + "\t" + commitRev.getFullMessage());

    Git binaryRepo = Git.open(gitDir);

    final ListBranchCommand listBranchCommand = binaryRepo.branchList();
    System.out.println(listBranchCommand.getRepository().getFullBranch());
    // get "status"
    final StatusCommand statusCommand = binaryRepo.status();
    Collection<String> toadd = GitUtils.getFilesToStage(statusCommand);
    for (String s : toadd) {
      System.out.println("To be added:" + s);
    }

    // add files to "staging"
    if (toadd.size() > 0) {
      AddCommand addCmd = binaryRepo.add();
      for (String file : toadd) {
        addCmd.addFilepattern(file);
      }

      try {
        addCmd.call();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    final StoredConfig config = repository.getConfig();
    String url = config.getString("remote", "origin", "url");
    if (url != null) {
      System.out.println("Origin comes from " + url);
    }

    // commit
    final CommitCommand commit = binaryRepo.commit();
    String msg = "Saving Repo:%s Branch:%s CommitHash:%s Time:%s";
    final String formattedMsg = String.format(msg, repo, branch, commitHash, new Date().toString());
    commit.setMessage(formattedMsg);
    try {
      commit.call();
    } catch (Exception e) {
      e.printStackTrace();
    }

    // push to origin now
    final PushCommand push = binaryRepo.push();
    final String remote = push.getRemote();
    System.out.println("Remote to push to:'" + remote + "'");
    try {
      push.call();
    } catch (Exception e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    }
  }
예제 #4
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;
  }