Java Code Examples for org.eclipse.jgit.revwalk.RevWalk#markStart()

The following examples show how to use org.eclipse.jgit.revwalk.RevWalk#markStart() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GitServiceImpl.java    From apidiff with MIT License 6 votes vote down vote up
@Override
public RevWalk fetchAndCreateNewRevsWalk(Repository repository, String branch) throws Exception {
	List<ObjectId> currentRemoteRefs = new ArrayList<ObjectId>(); 
	for (Ref ref : repository.getAllRefs().values()) {
		String refName = ref.getName();
		if (refName.startsWith(REMOTE_REFS_PREFIX)) {
			currentRemoteRefs.add(ref.getObjectId());
		}
	}
	
	List<TrackingRefUpdate> newRemoteRefs = this.fetch(repository);
	
	RevWalk walk = new RevWalk(repository);
	for (TrackingRefUpdate newRef : newRemoteRefs) {
		if (branch == null || newRef.getLocalName().endsWith("/" + branch)) {
			walk.markStart(walk.parseCommit(newRef.getNewObjectId()));
		}
	}
	for (ObjectId oldRef : currentRemoteRefs) {
		walk.markUninteresting(walk.parseCommit(oldRef));
	}
	walk.setRevFilter(commitsFilter);
	return walk;
}
 
Example 2
Source File: GitServiceImpl.java    From apidiff with MIT License 6 votes vote down vote up
public RevWalk createAllRevsWalk(Repository repository, String branch) throws Exception {
	List<ObjectId> currentRemoteRefs = new ArrayList<ObjectId>(); 
	for (Ref ref : repository.getAllRefs().values()) {
		String refName = ref.getName();
		if (refName.startsWith(REMOTE_REFS_PREFIX)) {
			if (branch == null || refName.endsWith("/" + branch)) {
				currentRemoteRefs.add(ref.getObjectId());
			}
		}
	}
	
	RevWalk walk = new RevWalk(repository);
	for (ObjectId newRef : currentRemoteRefs) {
		walk.markStart(walk.parseCommit(newRef));
	}
	walk.setRevFilter(commitsFilter);
	return walk;
}
 
Example 3
Source File: GetCommonAncestorCommand.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private GitRevisionInfo getSingleBaseCommit (RevWalk walk, List<RevCommit> commits) throws IOException {
    while (commits.size() > 1) {
        walk.reset();
        for (RevCommit c : commits) {
            walk.markStart(walk.parseCommit(c));
        }
        walk.setRevFilter(RevFilter.MERGE_BASE);
        commits.clear();
        for (RevCommit commit = walk.next(); commit != null; commit = walk.next()) {
            commits.add(commit);
        }
    }
    if (commits.isEmpty()) {
        return null;
    } else {
        return getClassFactory().createRevisionInfo(commits.get(0), getRepository());
    }
}
 
Example 4
Source File: GitFXGsonUtil.java    From GitFx with Apache License 2.0 6 votes vote down vote up
public static GitRepoMetaData getGitRepositoryMetaData(String repoPath) {
        try {
            if(!repoPath.endsWith(".git"))
                repoPath = repoPath+File.separator+".git";
            System.out.println("repopath:  "+repoPath);
            GitRepoMetaData gitMetaData = new GitRepoMetaData();
            FileRepositoryBuilder builder = new FileRepositoryBuilder();
            Repository repository = builder.setGitDir(new File(repoPath))
                    .readEnvironment()
                    .setMustExist(true)
                    .findGitDir()
                    .build();
            RevWalk walk = new RevWalk(repository);
            AnyObjectId id = repository.resolve("HEAD");
            if(id == null)
                return null;//Empty repository
            RevCommit rCommit =walk.parseCommit(id);
            walk.markStart(rCommit);
            gitMetaData.setRepository(repository);
            gitMetaData.setRevWalk(walk);
            return gitMetaData;
        } catch (IOException exception) {
//[LOG]            logger.debug("IOException getGitRepositoryMetaData", exception);
        };
        return null;
    }
 
Example 5
Source File: GitServiceImpl.java    From RefactoringMiner with MIT License 6 votes vote down vote up
public RevWalk fetchAndCreateNewRevsWalk(Repository repository, String branch) throws Exception {
	List<ObjectId> currentRemoteRefs = new ArrayList<ObjectId>(); 
	for (Ref ref : repository.getRefDatabase().getRefs()) {
		String refName = ref.getName();
		if (refName.startsWith(REMOTE_REFS_PREFIX)) {
			currentRemoteRefs.add(ref.getObjectId());
		}
	}
	
	List<TrackingRefUpdate> newRemoteRefs = this.fetch(repository);
	
	RevWalk walk = new RevWalk(repository);
	for (TrackingRefUpdate newRef : newRemoteRefs) {
		if (branch == null || newRef.getLocalName().endsWith("/" + branch)) {
			walk.markStart(walk.parseCommit(newRef.getNewObjectId()));
		}
	}
	for (ObjectId oldRef : currentRemoteRefs) {
		walk.markUninteresting(walk.parseCommit(oldRef));
	}
	walk.setRevFilter(commitsFilter);
	return walk;
}
 
Example 6
Source File: GitServiceImpl.java    From RefactoringMiner with MIT License 6 votes vote down vote up
public RevWalk createAllRevsWalk(Repository repository, String branch) throws Exception {
	List<ObjectId> currentRemoteRefs = new ArrayList<ObjectId>(); 
	for (Ref ref : repository.getRefDatabase().getRefs()) {
		String refName = ref.getName();
		if (refName.startsWith(REMOTE_REFS_PREFIX)) {
			if (branch == null || refName.endsWith("/" + branch)) {
				currentRemoteRefs.add(ref.getObjectId());
			}
		}
	}
	
	RevWalk walk = new RevWalk(repository);
	for (ObjectId newRef : currentRemoteRefs) {
		walk.markStart(walk.parseCommit(newRef));
	}
	walk.setRevFilter(commitsFilter);
	return walk;
}
 
Example 7
Source File: JGitOperator.java    From verigreen with Apache License 2.0 6 votes vote down vote up
boolean isRefBehind( Ref behind, Ref tracking ) throws IOException {
  RevWalk walk = new RevWalk( _git.getRepository() );
  try {
    RevCommit behindCommit = walk.parseCommit( behind.getObjectId() );
    RevCommit trackingCommit = walk.parseCommit( tracking.getObjectId() );
    walk.setRevFilter( RevFilter.MERGE_BASE );
    walk.markStart( behindCommit );
    walk.markStart( trackingCommit );
    RevCommit mergeBase = walk.next();
    walk.reset();
    walk.setRevFilter( RevFilter.ALL );
    int aheadCount = RevWalkUtils.count( walk, behindCommit, mergeBase );
    int behindCount = RevWalkUtils.count( walk, trackingCommit, mergeBase );
    
    return behindCount > aheadCount ? true:false;
  } catch (Throwable e) {
         throw new RuntimeException(String.format(
                 "Failed to check if [%s] behind [%s]",
                 behind,
                 tracking), e);
     } 
  finally {
    walk.dispose();
  }
}
 
Example 8
Source File: Tags.java    From gradle-gitsemver with Apache License 2.0 6 votes vote down vote up
private static TagAndVersion findLatestTag(Repository repo, Map<ObjectId, Set<String>> allTags, String prefix)
        throws MissingObjectException, IncorrectObjectTypeException, IOException {

    try {
        RevWalk walk = new RevWalk(repo);
        walk.markStart(walk.parseCommit(GitRepos.getHeadObjectId(repo)));
        return getLatestTagFromWalk(walk, allTags, prefix);
    } catch (NullPointerException e) {
        return new TagAndVersion("0.0.0", new DefaultSemanticVersion(
                "0.0.0",
                0,
                0,
                0,
                null,
                null));
    }
}
 
Example 9
Source File: Git.java    From OpenSZZ-Cloud-Native with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Get Commit that changed the file before the parameter commit
  * @param sha
  * @param file
  * @return
  */
 public String getPreviousCommit (String sha, String file, PrintWriter l){
  if (sha.equals("a8da84c614ba6e6e87c6c91e0c426ddfec2766a2"))
	  System.out.println();
  File  localRepo1 = new File(workingDirectory+"");
  Iterable<RevCommit> iterable;
  String finalSha = "";
  RevCommit latestCommit = null;
  String path = file;
  try {
	org.eclipse.jgit.api.Git git = org.eclipse.jgit.api.Git.open(localRepo1);
	RevWalk revWalk = new RevWalk( git.getRepository() );
    RevCommit revCommit = getCommit(sha, null);
    revWalk.markStart( revCommit );
    revWalk.sort( RevSort.COMMIT_TIME_DESC );
    revWalk.setTreeFilter( AndTreeFilter.create( PathFilter.create( path ), TreeFilter.ANY_DIFF ) );
    latestCommit = revWalk.next();
    while (!latestCommit.getName().equals(sha))
    	latestCommit = revWalk.next();
    latestCommit = revWalk.next();
    if (latestCommit == null)
    	return null;
    finalSha =  latestCommit.getName();

  } catch (Exception e) {
	 l.println("No Predecessor-Commits found for "+sha +"for file " + file);
	return null;
}
  return finalSha;
 }
 
Example 10
Source File: LogCommand.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void markBranchFlags (Map<String, GitBranch> allBranches, RevWalk walk, Map<RevFlag, List<GitBranch>> branchFlags) {
    int i = 1;
    Set<String> usedFlags = new HashSet<>();
    Repository repository = getRepository();
    for (Map.Entry<String, GitBranch> e : allBranches.entrySet()) {
        if (! GitBranch.NO_BRANCH.equals(e.getKey())) {
            String flagId = e.getValue().getId();
            if (usedFlags.contains(flagId)) {
                for (Map.Entry<RevFlag, List<GitBranch>> e2 : branchFlags.entrySet()) {
                    if (e2.getKey().toString().equals(flagId)) {
                        e2.getValue().add(e.getValue());
                    }
                }
            } else {
                usedFlags.add(flagId);
                if (i < 25) {
                    i = i + 1;
                    RevFlag flag = walk.newFlag(flagId);
                    List<GitBranch> branches = new ArrayList<>(allBranches.size());
                    branches.add(e.getValue());
                    branchFlags.put(flag, branches);
                    try {
                        RevCommit branchHeadCommit = walk.parseCommit(repository.resolve(e.getValue().getId()));
                        branchHeadCommit.add(flag);
                        branchHeadCommit.carry(flag);
                        walk.markStart(branchHeadCommit);
                    } catch (IOException ex) {
                        LOG.log(Level.INFO, null, ex);
                    }
                } else {
                    LOG.log(Level.WARNING, "Out of available flags for branches: {0}", allBranches.size()); //NOI18N
                    break;
                }
            }
        }
    }
    walk.carry(branchFlags.keySet());
}
 
Example 11
Source File: DifferentFiles.java    From gitflow-incremental-builder with MIT License 5 votes vote down vote up
private RevCommit getMergeBase(RevCommit baseCommit, RevCommit referenceHeadCommit) throws IOException {
    RevWalk walk = new RevWalk(git.getRepository());
    walk.setRevFilter(RevFilter.MERGE_BASE);
    walk.markStart(walk.lookupCommit(baseCommit));
    walk.markStart(walk.lookupCommit(referenceHeadCommit));
    RevCommit commit = walk.next();
    walk.close();
    logger.info("Using merge base of id: " + commit.getId());
    return commit;
}
 
Example 12
Source File: TreeWalkingDiffDetector.java    From multi-module-maven-release-plugin with MIT License 5 votes vote down vote up
public boolean hasChangedSince(String modulePath, java.util.List<String> childModules, Collection<AnnotatedTag> tags) throws IOException {
    RevWalk walk = new RevWalk(repo);
    try {
        walk.setRetainBody(false);
        walk.markStart(walk.parseCommit(repo.getRefDatabase().findRef("HEAD").getObjectId()));
        filterOutOtherModulesChanges(modulePath, childModules, walk);
        stopWalkingWhenTheTagsAreHit(tags, walk);
        return walk.iterator().hasNext();
    } finally {
        walk.dispose();
    }
}
 
Example 13
Source File: AppraiseGitReviewClient.java    From git-appraise-eclipse with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets the merge base for the two given commits.
 * Danger -- the commits need to be from the given RevWalk or this will
 * fail in a not-completely-obvious way.
 */
private RevCommit getMergeBase(RevWalk walk, RevCommit commit1, RevCommit commit2)
    throws GitClientException {
  try {
    walk.setRevFilter(RevFilter.MERGE_BASE);
    walk.markStart(commit1);
    walk.markStart(commit2);
    return walk.next();
  } catch (Exception e) {
    throw new GitClientException(
        "Failed to get merge base commit for " + commit1 + " and " + commit2, e);
  }
}
 
Example 14
Source File: JGitAPIImpl.java    From git-client-plugin with MIT License 5 votes vote down vote up
/**
 * Adds all matching refs as start commits.
 */
private void markRefs(RevWalk walk, Predicate<Ref> filter) throws IOException {
    try (Repository repo = getRepository()) {
        for (Ref r : repo.getAllRefs().values()) {
            if (filter.apply(r)) {
                RevCommit c = walk.parseCommit(r.getObjectId());
                walk.markStart(c);
            }
        }
    }
}
 
Example 15
Source File: Tags.java    From gradle-gitsemver with Apache License 2.0 5 votes vote down vote up
private static int getCountBetweenCommits(Repository repo,
                                          ObjectId headObjectId,
                                          ObjectId lastTagObjectId)
        throws MissingObjectException, IncorrectObjectTypeException, IOException {
    RevWalk walk = new RevWalk(repo);
    RevCommit startingPoint = walk.parseCommit(headObjectId);
    walk.markStart(startingPoint);
    RevCommit end = walk.lookupCommit(lastTagObjectId);
    walk.sort(RevSort.TOPO);
    int commitCount = 0;
    for (RevCommit c = walk.next(); nonNullOrEnd(end, c); c = walk.next()) {
        commitCount++;
    }
    return commitCount;
}
 
Example 16
Source File: Tags.java    From gradle-gitsemver with Apache License 2.0 5 votes vote down vote up
private static TagAndVersion findLatestTopoTag(Repository repo, Map<ObjectId, Set<String>> allTags, String prefix)
        throws MissingObjectException, IncorrectObjectTypeException, IOException {

    try {
        RevWalk walk = new RevWalk(repo);
        walk.markStart(walk.parseCommit(GitRepos.getHeadObjectId(repo)));
        for (RevCommit commit : walk) {
            ObjectId commitId = commit.getId();
            // Find the very first tag in history
            if (allTags.containsKey(commitId)) {
                List<TagAndVersion> foundTags = new LinkedList<TagAndVersion>();
                // If there are more than one tag for this commit, choose the lexographically superior one
                for (String tagName : allTags.get(commitId)) {
                    String tagVersion = GitRepos.stripVFromVersionString(tagName);
                    if (prefix == null) {
                        foundTags.add(new TagAndVersion(tagName, SemanticVersions.parse(tagVersion)));
                    } else {
                        foundTags.add(new TagAndVersion(tagName, SemanticVersions.parse(prefix, tagVersion)));
                    }
                }
                Collections.sort(foundTags);
                return foundTags.get(foundTags.size() - 1);
            }
        }
        // No tags found - return null
        return null;
    } catch (NullPointerException e) {
        return new TagAndVersion("0.0.0", new DefaultSemanticVersion(
                "0.0.0",
                0,
                0,
                0,
                null,
                null));
    }
}