org.eclipse.jgit.revwalk.RevTag Java Examples

The following examples show how to use org.eclipse.jgit.revwalk.RevTag. 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: GitRepo.java    From git-changelog-lib with Apache License 2.0 6 votes vote down vote up
private Map<String, RevTag> getAnnotatedTagPerTagName(
    final Optional<String> ignoreTagsIfNameMatches, final List<Ref> tagList) {
  final Map<String, RevTag> tagPerCommit = newHashMap();
  for (final Ref tag : tagList) {
    if (ignoreTagsIfNameMatches.isPresent()) {
      if (compile(ignoreTagsIfNameMatches.get()).matcher(tag.getName()).matches()) {
        continue;
      }
    }
    final Ref peeledTag = this.repository.peel(tag);
    if (peeledTag.getPeeledObjectId() != null) {
      try {
        final RevTag revTag = RevTag.parse(this.repository.open(tag.getObjectId()).getBytes());
        tagPerCommit.put(tag.getName(), revTag);
      } catch (final IOException e) {
        LOG.error(e.getMessage(), e);
      }
    }
  }
  return tagPerCommit;
}
 
Example #2
Source File: GitRepo.java    From git-changelog-lib with Apache License 2.0 6 votes vote down vote up
private void addToTags(
    final Map<String, Set<GitCommit>> commitsPerTag,
    final String tagName,
    final Date tagTime,
    final List<GitTag> addTo,
    final Map<String, RevTag> annotatedTagPerTagName) {
  if (commitsPerTag.containsKey(tagName)) {
    final Set<GitCommit> gitCommits = commitsPerTag.get(tagName);
    final boolean isAnnotated = annotatedTagPerTagName.containsKey(tagName);
    String annotation = null;
    if (isAnnotated) {
      annotation = annotatedTagPerTagName.get(tagName).getFullMessage();
    }

    final GitTag gitTag = new GitTag(tagName, annotation, newArrayList(gitCommits), tagTime);
    addTo.add(gitTag);
  }
}
 
Example #3
Source File: AbstractGitRepositoryTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected List<String> listTags(Repository repository) throws IOException, GitAPIException {
    List<String> tags = new ArrayList<>();
    try (Git git = new Git(repository)) {
        for (Ref tag : git.tagList().call()) {
            RevWalk revWalk = new RevWalk(repository);
            try {
                RevTag annotatedTag = revWalk.parseTag(tag.getObjectId());
                tags.add(annotatedTag.getTagName());
            } catch (IncorrectObjectTypeException ex) {
                tags.add(tag.getName().substring("refs/tags/".length()));
            }
        }
    }
    Collections.sort(tags);
    return tags;
}
 
Example #4
Source File: Tag.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
private RevCommit parseCommit() {
	if (this.commit == null) {
		RevWalk rw = new RevWalk(db);
		RevObject any;
		try {
			any = rw.parseAny(this.ref.getObjectId());
			if (any instanceof RevTag) {
				this.tag = (RevTag) any;
				RevObject o = rw.peel(any);
				if (o instanceof RevCommit) {
					this.commit = (RevCommit) rw.peel(any);
				}
			} else if (any instanceof RevCommit) {
				this.commit = (RevCommit) any;
			}
		} catch (IOException e) {
		} finally {
			rw.dispose();
		}
	}
	return commit;
}
 
Example #5
Source File: GitManagerTest.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testForCreateAnotationTag() throws GitAPIException, IOException {
    Git git = Git.wrap(repository);

    gitMgr.createTag(ws, "test", null, "test message", false);

    List<Ref> refs = git.tagList().call();
    assertEquals(1, refs.size());
    assertEquals("refs/tags/test", refs.get(0).getName());

    assertNotEquals(repository.resolve("HEAD"), refs.get(0).getObjectId());

    RevWalk walk = new RevWalk(repository);
    RevTag tag = walk.parseTag(refs.get(0).getObjectId());

    assertEquals("test message", tag.getFullMessage());
}
 
Example #6
Source File: ChangeApplier.java    From smart-testing with Apache License 2.0 5 votes vote down vote up
private Collection<TestResult> cherryPickTags(Collection<RevTag> tags) throws GitAPIException {
    final Set<TestResult> combinedTestResults = new HashSet<>();
    for (final RevTag tag : tags) {
        final ObjectId tagId = tag.getObject().getId();
        git.cherryPick().include(tagId).call();
        combinedTestResults.addAll(testResultsExtractor.expectedTestResults(tagId));
    }
    return combinedTestResults;
}
 
Example #7
Source File: RefInfo.java    From onedev with MIT License 5 votes vote down vote up
@Override
public int compareTo(RefInfo other) {
	Date date;
	if (obj instanceof RevTag && ((RevTag)obj).getTaggerIdent() != null) {
		date =  ((RevTag)obj).getTaggerIdent().getWhen();
	} else if (peeledObj instanceof RevCommit) {
		date = ((RevCommit)peeledObj).getCommitterIdent().getWhen();
	} else {
		date = null;
	}
	Date otherDate;
	if (other.obj instanceof RevTag && ((RevTag)other.obj).getTaggerIdent() != null) {
		otherDate =  ((RevTag)other.obj).getTaggerIdent().getWhen();
	} else if (other.peeledObj instanceof RevCommit) {
		otherDate = ((RevCommit)other.peeledObj).getCommitterIdent().getWhen();
	} else {
		otherDate = null;
	}
	
	if (date != null) {
		if (otherDate != null)
			return date.compareTo(otherDate);
		else
			return 1;
	} else {
		if (otherDate != null)
			return -1;
		else
			return getRef().getName().compareTo(other.getRef().getName());
	}
}
 
Example #8
Source File: ChangeApplier.java    From smart-testing with Apache License 2.0 5 votes vote down vote up
private Collection<RevTag> findMatchingTags(String[] changeDescriptions) throws GitAPIException {
    final Set<String> changes = new HashSet<>(asList(changeDescriptions));
    final RevWalk revWalk = new RevWalk(repository);
    final List<RevTag> matchingTags = git.tagList().call().stream().map(ref -> {
        try {
            return revWalk.parseTag(ref.getObjectId());
        } catch (IOException e) {
            throw new RuntimeException("Unable to find tag for " + ref.getObjectId(), e);
        }
    }).filter(revTag -> changes.contains(revTag.getFullMessage().trim()))
        .collect(Collectors.toList());
    revWalk.dispose();
    return matchingTags;
}
 
Example #9
Source File: ChangeApplier.java    From smart-testing with Apache License 2.0 5 votes vote down vote up
private Collection<TestResult> applyLocallyFromTags(Collection<RevTag> tags) throws GitAPIException {
    final Set<TestResult> combinedTestResults = new HashSet<>();
    final List<RevCommit> stashesToApply = new ArrayList<>();
    stashesToApply.add(git.stashCreate().setIncludeUntracked(true).call());
    for (final RevTag tag : tags) {
        final ObjectId tagId = tag.getObject().getId();
        git.cherryPick().setNoCommit(true).include(tagId).call();
        stashesToApply.add(git.stashCreate().setIncludeUntracked(true).call());
        combinedTestResults.addAll(testResultsExtractor.expectedTestResults(tagId));
    }
    applyStashChangesLocally(stashesToApply);
    return combinedTestResults;
}
 
Example #10
Source File: ChangeApplier.java    From smart-testing with Apache License 2.0 5 votes vote down vote up
Collection<TestResult> apply(ChangeApplicator applicator, String ... changeDescriptions) {
    try {
        final Collection<RevTag> matchingTags = findMatchingTags(changeDescriptions);
        if (changeDescriptions.length != matchingTags.size()) {
            throw new IllegalStateException("Unable to find all required changes " + Arrays.toString(changeDescriptions)
                + ", found only " + matchingTags.stream().map(tag -> tag.getTagName() + ": " + tag.getFullMessage()).collect(Collectors.toList()));
        }
        return applicator.apply(matchingTags);
    } catch (GitAPIException e) {
        throw new RuntimeException("Failed applying changes '" + Arrays.toString(changeDescriptions) + "'", e);
    }
}
 
Example #11
Source File: GitMigrator.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
private Map<String, RevTag> loadTagsForRevisions(List<RevCommit> revisions) throws Exception {
    Map<String, Ref> tags = git.getRepository().getTags();
    Map<String, RevTag> ret = new HashMap<>();
    try (RevWalk walk = new RevWalk(git.getRepository())) {
        for (RevCommit rev : revisions) {
            String s = Git.wrap(git.getRepository()).describe().setTarget(rev).call();
            Ref tt = tags.get(s);
            if (tt != null) {
                RevTag t = walk.parseTag(tt.getObjectId());
                ret.put(rev.getName(), t);
            }
        }
    }
    return ret;
}
 
Example #12
Source File: AbstractGitPersistenceResourceTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private List<String> listTags(Git git) throws IOException, GitAPIException {
    List<String> tags = new ArrayList<>();
    for (Ref tag : git.tagList().call()) {
        RevWalk revWalk = new RevWalk(git.getRepository());
        revWalk.sort(RevSort.COMMIT_TIME_DESC, true);
        try {
            RevTag annotatedTag = revWalk.parseTag(tag.getObjectId());
            tags.add(annotatedTag.getTagName() + " : " + annotatedTag.getFullMessage());
        } catch (IncorrectObjectTypeException ex) {
            tags.add(tag.getName().substring("refs/tags/".length()));
        }
    }
    return tags;
}
 
Example #13
Source File: LayoutHelper.java    From git-as-svn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unwrap commit from reference.
 *
 * @param revWalk  Git parser.
 * @param objectId Reference object.
 * @return Wrapped commit or null (ex: tag on tree).
 * @throws IOException .
 */
@Nullable
private static RevCommit unwrapCommit(@NotNull RevWalk revWalk, @NotNull ObjectId objectId) throws IOException {
  RevObject revObject = revWalk.parseAny(objectId);
  while (true) {
    if (revObject instanceof RevCommit) {
      return (RevCommit) revObject;
    }
    if (revObject instanceof RevTag) {
      revObject = ((RevTag) revObject).getObject();
      continue;
    }
    return null;
  }
}
 
Example #14
Source File: TagUtils.java    From ParallelGit with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static RevTag getTag(AnyObjectId tagObjectId, ObjectReader reader) throws IOException {
  try(RevWalk revWalk = new RevWalk(reader)) {
    return revWalk.parseTag(tagObjectId);
  }
}
 
Example #15
Source File: TagUtils.java    From ParallelGit with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static RevTag getTag(Ref tagRef, Repository repo) throws IOException {
  try(ObjectReader reader = repo.newObjectReader()) {
    return getTag(tagRef, reader);
  }
}
 
Example #16
Source File: TagUtils.java    From ParallelGit with Apache License 2.0 4 votes vote down vote up
@Nullable
public static RevTag getTag(String tagName, Repository repo) throws IOException {
  Ref tagRef = repo.exactRef(RefUtils.fullTagRef(tagName));
  return tagRef != null ? getTag(tagRef, repo) : null;
}
 
Example #17
Source File: TagUtils.java    From ParallelGit with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static RevTag getTag(Ref tagRef, ObjectReader reader) throws IOException {
  return getTag(tagRef.getObjectId(), reader);
}
 
Example #18
Source File: TagUtils.java    From ParallelGit with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static RevTag getTag(AnyObjectId tagObjectId, ObjectReader reader) throws IOException {
  try(RevWalk revWalk = new RevWalk(reader)) {
    return revWalk.parseTag(tagObjectId);
  }
}
 
Example #19
Source File: GitRepo.java    From git-changelog-lib with Apache License 2.0 4 votes vote down vote up
private List<GitTag> gitTags(
    final ObjectId fromObjectId,
    final ObjectId toObjectId,
    final String untaggedName,
    final Optional<String> ignoreTagsIfNameMatches)
    throws Exception {
  final RevCommit from = this.revWalk.lookupCommit(fromObjectId);
  final RevCommit to = this.revWalk.lookupCommit(toObjectId);

  this.commitsToInclude = getDiffingCommits(from, to);

  final List<Ref> tagList = tagsBetweenFromAndTo(from, to);
  /**
   * What: Contains only the commits that are directly referred to by tags.<br>
   * Why: To know if a new tag was found when walking up through the parents.
   */
  final Map<String, Ref> tagPerCommitHash = getTagPerCommitHash(ignoreTagsIfNameMatches, tagList);

  /**
   * What: Contains only the tags that are annotated.<br>
   * Why: To populate tag message, for annotated tags.
   */
  final Map<String, RevTag> annotatedTagPerTagName =
      getAnnotatedTagPerTagName(ignoreTagsIfNameMatches, tagList);

  /**
   * What: Populated with all included commits, referring to there tags.<br>
   * Why: To know if a commit is already mapped to a tag, or not.
   */
  final Map<String, String> tagPerCommitsHash = newHashMap();
  /**
   * What: Commits per tag.<br>
   * Why: Its what we are here for! =)
   */
  final Map<String, Set<GitCommit>> commitsPerTag = newHashMap();
  final Map<String, Date> datePerTag = newHashMap();

  populateComitPerTag(
      from, to, tagPerCommitHash, tagPerCommitsHash, commitsPerTag, datePerTag, null);
  populateComitPerTag(
      from, to, tagPerCommitHash, tagPerCommitsHash, commitsPerTag, datePerTag, untaggedName);

  final List<GitTag> tags = newArrayList();
  addToTags(commitsPerTag, untaggedName, null, tags, annotatedTagPerTagName);
  final List<Ref> tagCommitHashSortedByCommitTime =
      getTagCommitHashSortedByCommitTime(tagPerCommitHash.values());
  for (final Ref tag : tagCommitHashSortedByCommitTime) {
    addToTags(
        commitsPerTag,
        tag.getName(),
        datePerTag.get(tag.getName()),
        tags,
        annotatedTagPerTagName);
  }
  return tags;
}
 
Example #20
Source File: TagUtils.java    From ParallelGit with Apache License 2.0 4 votes vote down vote up
@Nullable
public static RevTag getTag(String tagName, Repository repo) throws IOException {
  Ref tagRef = repo.exactRef(RefUtils.fullTagRef(tagName));
  return tagRef != null ? getTag(tagRef, repo) : null;
}
 
Example #21
Source File: TagUtils.java    From ParallelGit with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static RevTag getTag(Ref tagRef, Repository repo) throws IOException {
  try(ObjectReader reader = repo.newObjectReader()) {
    return getTag(tagRef, reader);
  }
}
 
Example #22
Source File: TagUtils.java    From ParallelGit with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static RevTag getTag(Ref tagRef, ObjectReader reader) throws IOException {
  return getTag(tagRef.getObjectId(), reader);
}
 
Example #23
Source File: GitMigrator.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
private void createSnapshots(Node saveSetNode, String relativeSnpFilePath){
    try {
        List<RevCommit> commits = findCommitsFor(relativeSnpFilePath);
        Map<String, RevTag> tags = loadTagsForRevisions(commits);
        for(RevCommit commit : commits){
            try (ObjectReader objectReader = git.getRepository().newObjectReader(); TreeWalk treeWalk = new TreeWalk(objectReader)) {
                CanonicalTreeParser treeParser = new CanonicalTreeParser();
                treeParser.reset(objectReader, commit.getTree());
                int treeIndex = treeWalk.addTree(treeParser);
                treeWalk.setFilter(PathFilter.create(relativeSnpFilePath));
                treeWalk.setRecursive(true);
                if (treeWalk.next()) {
                    AbstractTreeIterator iterator = treeWalk.getTree(treeIndex, AbstractTreeIterator.class);
                    ObjectId objectId = iterator.getEntryObjectId();
                    ObjectLoader objectLoader = objectReader.open(objectId);
                    RevTag tag = tags.get(commit.getName());
                    try (InputStream stream = objectLoader.openStream()) {
                        List<SnapshotItem> snapshotItems = FileReaderHelper.readSnapshot(stream);
                        if(tag != null){
                            System.out.println();
                        }
                        if(!isSnapshotCompatibleWithSaveSet(saveSetNode, snapshotItems)){
                            continue;
                        }
                        snapshotItems = setConfigPvIds(saveSetNode, snapshotItems);
                        Date commitTime = new Date(commit.getCommitTime() * 1000L);
                        Node snapshotNode = saveAndRestoreService.saveSnapshot(saveSetNode,
                                snapshotItems,
                                commitTime.toString(),
                                commit.getFullMessage());

                        snapshotNode = saveAndRestoreService.getNode(snapshotNode.getUniqueId());
                        Map<String, String> properties = snapshotNode.getProperties();
                        if(properties == null){
                            properties = new HashMap<>();
                        }
                        if(tag != null){
                            properties.put("golden", "true");
                            snapshotNode.setProperties(properties);
                        }
                        snapshotNode.setUserName(commit.getCommitterIdent().getName());
                        saveAndRestoreService.updateNode(snapshotNode);
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #24
Source File: Tag.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
private RevTag parseTag() {
	parseCommit();
	return this.tag;
}
 
Example #25
Source File: GitClassFactoryImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public GitTag createTag (RevTag revTag) {
    return new GitTag(revTag);
}
 
Example #26
Source File: ChangeApplier.java    From smart-testing with Apache License 2.0 votes vote down vote up
Collection<TestResult> apply(Collection<RevTag> tags) throws GitAPIException; 
Example #27
Source File: GitClassFactory.java    From netbeans with Apache License 2.0 votes vote down vote up
public abstract GitTag createTag (RevTag revTag);