Esempio n. 1
0
  /**
   * Parses an array of Git commits and returns an array of commits that affected the specified
   * object.
   *
   * @param repository the Git repository
   * @param commitIds the Git commit IDs to parse
   * @param path the object affected
   * @return an array of {@link GitCommit} objects representing the commits
   */
  public NSArray<GitCommit> commitsWithIds(NSArray<ObjectId> commitIds, String path) {
    try {
      RevWalk rw = new RevWalk(repository);

      try {
        rw.sort(RevSort.COMMIT_TIME_DESC);

        if (path != null) {
          rw.setTreeFilter(
              AndTreeFilter.create(PathSuffixFilter.create(path), TreeFilter.ANY_DIFF));
        } else {
          rw.setTreeFilter(TreeFilter.ALL);
        }

        for (ObjectId commitId : commitIds) {
          rw.markStart(rw.parseCommit(commitId));
        }

        NSMutableArray<GitCommit> commits = new NSMutableArray<GitCommit>();

        for (RevCommit commit : rw) {
          commits.add(new GitCommit(commit));
        }

        return commits;
      } finally {
        rw.release();
      }
    } catch (Exception e) {
      log.error("An exception occurred while parsing the commit: ", e);
      return null;
    }
  }
Esempio n. 2
0
  /**
   * Compute the current projects that will be missing after the given branch is checked out
   *
   * @param branch
   * @param currentProjects
   * @return non-null but possibly empty array of missing projects
   */
  private IProject[] getMissingProjects(String branch, IProject[] currentProjects) {
    if (delete || currentProjects.length == 0) return new IProject[0];

    ObjectId targetTreeId;
    ObjectId currentTreeId;
    try {
      targetTreeId = repository.resolve(branch + "^{tree}"); // $NON-NLS-1$
      currentTreeId = repository.resolve(Constants.HEAD + "^{tree}"); // $NON-NLS-1$
    } catch (IOException e) {
      return new IProject[0];
    }
    if (targetTreeId == null || currentTreeId == null) return new IProject[0];

    Map<File, IProject> locations = new HashMap<File, IProject>();
    for (IProject project : currentProjects) {
      IPath location = project.getLocation();
      if (location == null) continue;
      location = location.append(IProjectDescription.DESCRIPTION_FILE_NAME);
      locations.put(location.toFile(), project);
    }

    List<IProject> toBeClosed = new ArrayList<IProject>();
    File root = repository.getWorkTree();
    TreeWalk walk = new TreeWalk(repository);
    try {
      walk.addTree(targetTreeId);
      walk.addTree(currentTreeId);
      walk.addTree(new FileTreeIterator(repository));
      walk.setRecursive(true);
      walk.setFilter(
          AndTreeFilter.create(
              PathSuffixFilter.create(IProjectDescription.DESCRIPTION_FILE_NAME),
              TreeFilter.ANY_DIFF));
      while (walk.next()) {
        AbstractTreeIterator targetIter = walk.getTree(0, AbstractTreeIterator.class);
        if (targetIter != null) continue;

        AbstractTreeIterator currentIter = walk.getTree(1, AbstractTreeIterator.class);
        AbstractTreeIterator workingIter = walk.getTree(2, AbstractTreeIterator.class);
        if (currentIter == null || workingIter == null) continue;

        IProject project = locations.get(new File(root, walk.getPathString()));
        if (project != null) toBeClosed.add(project);
      }
    } catch (IOException e) {
      return new IProject[0];
    } finally {
      walk.release();
    }
    return toBeClosed.toArray(new IProject[toBeClosed.size()]);
  }