Example usage for org.eclipse.jgit.api Git wrap

List of usage examples for org.eclipse.jgit.api Git wrap

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git wrap.

Prototype

public static Git wrap(Repository repo) 

Source Link

Document

Wrap repository

Usage

From source file:pl.project13.jgit.DescribeCommandIntegrationTest.java

License:Open Source License

@Test
public void shouldReturnJustTheNearestTagWhenAbbrevIsZero() throws Exception {
    // given// ww  w.java2 s.c  o  m
    int zeroAbbrev = 0;
    mavenSandbox.withParentProject(PROJECT_NAME, "jar").withNoChildProject()
            .withGitRepoInParent(AvailableGitTestRepo.WITH_LIGHTWEIGHT_TAG_BEFORE_ANNOTATED_TAG)
            .create(FileSystemMavenSandbox.CleanUp.CLEANUP_FIRST);

    Repository repo = git().getRepository();
    Git.wrap(repo).reset().setMode(ResetCommand.ResetType.HARD).call();

    // when
    DescribeResult res = DescribeCommand.on(repo).abbrev(zeroAbbrev).setVerbose(true).call();

    // then
    assertThat(res.toString()).isEqualTo("annotated-tag");

    ObjectId objectId = res.commitObjectId();
    assert objectId != null;
    assertThat(objectId.getName()).isNotEmpty();
}

From source file:pl.project13.jgit.DescribeCommandTagsIntegrationTest.java

License:Open Source License

@Test
public void shouldUseTheNewestTagOnACommitIfItHasMoreThanOneTags() throws Exception {
    // given//from  w  w w  . j  a v a2 s  .c o  m
    mavenSandbox.withParentProject(PROJECT_NAME, "jar").withNoChildProject()
            .withGitRepoInParent(AvailableGitTestRepo.WITH_LIGHTWEIGHT_TAG_BEFORE_ANNOTATED_TAG)
            .create(FileSystemMavenSandbox.CleanUp.CLEANUP_FIRST);

    String snapshotTag = "0.0.1-SNAPSHOT";
    String latestTag = "OName-0.0.1";

    Repository repo = git().getRepository();
    Git jgit = Git.wrap(repo);
    jgit.reset().setMode(ResetCommand.ResetType.HARD).call();

    // when
    jgit.tag().setName(snapshotTag).call();
    Thread.sleep(2000);
    jgit.tag().setName(latestTag).call();

    DescribeResult res = DescribeCommand.on(repo).tags().setVerbose(true).call();

    // then
    assertThat(res.toString()).isEqualTo(latestTag);
}

From source file:pl.project13.jgit.DescribeCommandTagsIntegrationTest.java

License:Open Source License

@Test
public void shouldUseTheNewestTagOnACommitIfItHasMoreThanOneTagsReversedCase() throws Exception {
    // given/*w ww .j  av  a  2s  .  c om*/
    mavenSandbox.withParentProject(PROJECT_NAME, "jar").withNoChildProject()
            .withGitRepoInParent(AvailableGitTestRepo.WITH_LIGHTWEIGHT_TAG_BEFORE_ANNOTATED_TAG)
            .create(FileSystemMavenSandbox.CleanUp.CLEANUP_FIRST);

    String beforeTag = "OName-0.0.1";
    String latestTag = "0.0.1-SNAPSHOT";

    Repository repo = git().getRepository();
    Git jgit = Git.wrap(repo);
    jgit.reset().setMode(ResetCommand.ResetType.HARD).call();

    // when
    jgit.tag().setName(beforeTag).call();
    jgit.tag().setName(latestTag).call();

    DescribeResult res = DescribeCommand.on(repo).tags().setVerbose(true).call();

    // then
    assertThat(res.toString()).isEqualTo(latestTag);
}

From source file:pl.project13.jgit.JGitCommon.java

License:Open Source License

public Collection<String> getTags(Repository repo, final ObjectId headId) throws GitAPIException {
    RevWalk walk = null;/*  w  w w  . j  a v a  2s.co m*/
    try {
        Git git = Git.wrap(repo);
        walk = new RevWalk(repo);
        List<Ref> tagRefs = git.tagList().call();

        final RevWalk finalWalk = walk;
        Collection<Ref> tagsForHeadCommit = Collections2.filter(tagRefs, new Predicate<Ref>() {
            @Override
            public boolean apply(Ref tagRef) {
                boolean lightweightTag = tagRef.getObjectId().equals(headId);

                try {
                    // TODO make this configurable (most users shouldn't really care too much what kind of tag it is though)
                    return lightweightTag
                            || finalWalk.parseTag(tagRef.getObjectId()).getObject().getId().equals(headId); // or normal tag
                } catch (IOException e) {
                    return false;
                }
            }
        });

        Collection<String> tags = Collections2.transform(tagsForHeadCommit, new Function<Ref, String>() {
            @Override
            public String apply(Ref input) {
                return input.getName().replaceAll("refs/tags/", "");
            }
        });

        return tags;
    } finally {
        if (walk != null) {
            walk.dispose();
        }
    }
}

From source file:pl.project13.jgit.JGitCommon.java

License:Open Source License

protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull Repository repo,
        boolean includeLightweightTags, String matchPattern) {
    Map<ObjectId, List<DatedRevTag>> commitIdsToTags = new HashMap<>();

    try (RevWalk walk = new RevWalk(repo)) {
        walk.markStart(walk.parseCommit(repo.resolve("HEAD")));

        List<Ref> tagRefs = Git.wrap(repo).tagList().call();
        Pattern regex = Pattern.compile(matchPattern);
        log.info("Tag refs [{}]", tagRefs);

        for (Ref tagRef : tagRefs) {
            walk.reset();/*  w w w .j  av a2s.  c o m*/
            String name = tagRef.getName();
            if (!regex.matcher(name).matches()) {
                log.info("Skipping tagRef with name [{}] as it doesn't match [{}]", name, matchPattern);
                continue;
            }
            ObjectId resolvedCommitId = repo.resolve(name);

            // TODO that's a bit of a hack...
            try {
                final RevTag revTag = walk.parseTag(resolvedCommitId);
                ObjectId taggedCommitId = revTag.getObject().getId();
                log.info("Resolved tag [{}] [{}], points at [{}] ", revTag.getTagName(),
                        revTag.getTaggerIdent(), taggedCommitId);

                // sometimes a tag, may point to another tag, so we need to unpack it
                while (isTagId(taggedCommitId)) {
                    taggedCommitId = walk.parseTag(taggedCommitId).getObject().getId();
                }

                if (commitIdsToTags.containsKey(taggedCommitId)) {
                    commitIdsToTags.get(taggedCommitId).add(new DatedRevTag(revTag));
                } else {
                    commitIdsToTags.put(taggedCommitId,
                            new ArrayList<>(Collections.singletonList(new DatedRevTag(revTag))));
                }

            } catch (IncorrectObjectTypeException ex) {
                // it's an lightweight tag! (yeah, really)
                if (includeLightweightTags) {
                    // --tags means "include lightweight tags"
                    log.info("Including lightweight tag [{}]", name);

                    DatedRevTag datedRevTag = new DatedRevTag(resolvedCommitId, name);

                    if (commitIdsToTags.containsKey(resolvedCommitId)) {
                        commitIdsToTags.get(resolvedCommitId).add(datedRevTag);
                    } else {
                        commitIdsToTags.put(resolvedCommitId,
                                new ArrayList<>(Collections.singletonList(datedRevTag)));
                    }
                }
            } catch (Exception ignored) {
                log.info("Failed while parsing [{}] -- ", tagRef, ignored);
            }
        }

        for (Map.Entry<ObjectId, List<DatedRevTag>> entry : commitIdsToTags.entrySet()) {
            log.info("key [{}], tags => [{}] ", entry.getKey(), entry.getValue());
        }
        return commitIdsToTags;
    } catch (Exception e) {
        log.info("Unable to locate tags", e);
    }
    return Collections.emptyMap();
}

From source file:pl.project13.jgit.JGitCommon.java

License:Open Source License

public static boolean isRepositoryInDirtyState(Repository repo) throws GitAPIException {
    Git git = Git.wrap(repo);
    Status status = git.status().call();

    // Git describe doesn't mind about untracked files when checking if
    // repo is dirty. JGit does this, so we cannot use the isClean method
    // to get the same behaviour. Instead check dirty state without
    // status.getUntracked().isEmpty()
    boolean isDirty = !(status.getAdded().isEmpty() && status.getChanged().isEmpty()
            && status.getRemoved().isEmpty() && status.getMissing().isEmpty() && status.getModified().isEmpty()
            && status.getConflicting().isEmpty());

    return isDirty;
}