Example usage for org.eclipse.jgit.lib Ref getName

List of usage examples for org.eclipse.jgit.lib Ref getName

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Ref getName.

Prototype

@NonNull
String getName();

Source Link

Document

What this ref is called within the repository.

Usage

From source file:RefNode.java

License:Open Source License

public static void createNode(RevWalk walk, Ref ref, boolean recur) throws IOException {
    String key = ref.getName();
    ObjectId id = ref.getObjectId();//from w  w w .j  ava 2 s. c  o m
    Node node;

    if (!nodeMap.containsKey(key)) {
        // create new ref node
        node = new RefNode(key);

        // determine color
        if (key.contains("/heads/")) {
            node.setBackground(new Color(240, 255, 240));
            node.setForeground(new Color(0, 127, 0));
        } else if (key.contains("/tags/")) {
            node.setBackground(new Color(255, 255, 240));
            node.setForeground(new Color(127, 127, 0));
        } else if (key.contains("/remotes/")) {
            node.setBackground(new Color(240, 240, 240));
            node.setForeground(new Color(0, 0, 0));
        } else {
            node.setBackground(new Color(255, 240, 240));
            node.setForeground(new Color(127, 0, 0));
        }

        if (recur) {
            // add parent node
            RevCommit commit = walk.parseCommit(id);
            node.addParent(Node.createNode(walk, commit, recur));
        }
    }
}

From source file:at.bitandart.zoubek.mervin.gerrit.GerritReviewRepositoryService.java

License:Open Source License

@SuppressWarnings("restriction")
@Override//from  www . j  ava  2  s  .c  om
public List<IReviewDescriptor> getReviews(URI uri) throws InvalidReviewRepositoryException {

    List<IReviewDescriptor> changeIds = new LinkedList<>();

    try {
        // connect to the local git repository
        Git git = Git.open(new File(uri));

        try {
            // Assume that origin refers to the remote gerrit repository
            // list all remote refs from origin
            Collection<Ref> remoteRefs = git.lsRemote().setTimeout(60).call();

            Pattern changeRefPattern = Pattern.compile(CHANGE_REF_PATTERN);

            // search for change refs
            for (Ref ref : remoteRefs) {

                Matcher matcher = changeRefPattern.matcher(ref.getName());
                if (matcher.matches()) {
                    String changePk = matcher.group(CHANGE_REF_PATTERN_GROUP_CHANGE_PK);
                    String changeId = "<unknown>";
                    GerritReviewDescriptor reviewDescriptor;
                    try {
                        reviewDescriptor = new GerritReviewDescriptor(Integer.parseInt(changePk), changeId);
                    } catch (NumberFormatException nfe) {
                        // FIXME ignore it or throw an exception?
                        break;
                    }

                    if (!changeIds.contains(reviewDescriptor)) {
                        changeIds.add(reviewDescriptor);

                        /*
                         * the change id is present in all commit messages,
                         * so we extract it from the commit message of the
                         * current ref
                         */
                        FetchResult fetchResult = git.fetch().setRefSpecs(new RefSpec(ref.getName())).call();

                        Ref localRef = fetchResult.getAdvertisedRef(ref.getName());
                        RevWalk revWalk = new RevWalk(git.getRepository());
                        RevCommit commit = revWalk.parseCommit(localRef.getObjectId());
                        String[] paragraphs = commit.getFullMessage().split("\n");
                        String lastParagraph = paragraphs[paragraphs.length - 1];
                        Pattern pattern = Pattern.compile(".*Change-Id: (I[^ \n]*).*");
                        Matcher changeIdMatcher = pattern.matcher(lastParagraph);

                        if (changeIdMatcher.matches()) {
                            changeId = changeIdMatcher.group(1);
                            reviewDescriptor.setChangeId(changeId);
                            ;
                        } else {
                            logger.warn(MessageFormat.format(
                                    "Could not find the change id for Gerrit change with primary key {0}",
                                    changePk));
                        }
                        revWalk.close();
                    }
                }
            }

        } catch (GitAPIException e) {
            throw new RepositoryIOException("Error during loading all remote changes", e);
        }

    } catch (IOException e) {
        throw new InvalidReviewRepositoryException("Could not open local git repository", e);
    }
    return changeIds;
}

From source file:boa.datagen.scm.GitConnector.java

License:Apache License

@Override
public void getTags(final List<String> names, final List<String> commits) {
    try {/*from w w w.java2  s. c  o m*/
        for (final Ref ref : git.tagList().call()) {
            names.add(ref.getName());
            commits.add(ref.getObjectId().getName());
        }
    } catch (final GitAPIException e) {
        if (debug)
            System.err.println("Git Error reading tags: " + e.getMessage());
    }
}

From source file:boa.datagen.scm.GitConnector.java

License:Apache License

@Override
public void getBranches(final List<String> names, final List<String> commits) {
    try {//from  w  w  w.j  av a2 s .  com
        for (final Ref ref : git.branchList().call()) {
            names.add(ref.getName());
            commits.add(ref.getObjectId().getName());
        }
    } catch (final GitAPIException e) {
        if (debug)
            System.err.println("Git Error reading branches: " + e.getMessage());
    }
}

From source file:br.com.metricminer2.scm.GitRepository.java

License:Apache License

private void deleteMMBranch(Git git)
        throws GitAPIException, NotMergedException, CannotDeleteCurrentBranchException {
    List<Ref> refs = git.branchList().call();
    for (Ref r : refs) {
        if (r.getName().endsWith("mm")) {
            git.branchDelete().setBranchNames("mm").setForce(true).call();
            break;
        }//w ww.j a  v a2 s .  c  o m
    }
}

From source file:br.com.riselabs.cotonet.util.CodefaceHelper.java

License:Open Source License

/**
 * Returns the list of TAGs from the given repository.
 * //from w ww .j av  a2 s.co m
 * @param repository
 * @return
 * @throws IOException
 * @throws GitAPIException
 */
@Deprecated
public List<String> getTags(Repository repository) throws IOException, GitAPIException {
    List<String> tags = new ArrayList<String>();
    try (Git git = new Git(repository)) {
        List<Ref> call = git.tagList().call();
        for (Ref ref : call) {
            tags.add(ref.getName());
        }
    }
    return tags;
}

From source file:br.edu.ifpb.scm.api.loads.LoaderVersions.java

public static String teste3() throws IOException {
    try (org.eclipse.jgit.lib.Repository repository = org.eclipse.jgit.api.Git.open(PATH).getRepository()) {
        // See e.g. GetRevCommitFromObjectId for how to use a SHA-1 directly
        Ref head = repository.findRef("HEAD");
        System.out.println(/*www . j a va  2  s .  co m*/
                "Ref of HEAD: " + head + ": " + head.getName() + " - " + head.getObjectId().getName() + "\n");
        System.out.println("Ref of HEAD getName: " + head.getName() + "\n");
        System.out.println("Ref of HEAD getId().getName: " + head.getObjectId().getName() + "\n");

        // a RevWalk allows to walk over commits based on some filtering that is defined
        try (RevWalk walk = new RevWalk(repository)) {
            RevCommit commit = walk.parseCommit(head.getObjectId());
            System.out.println("Commit: " + commit);

            // a commit points to a tree
            RevTree tree = walk.parseTree(commit.getTree().getId());
            System.out.println("FOUND TREE: " + tree.getName());

            walk.dispose();
            return tree.getName();
        }
    }
}

From source file:br.edu.ifpb.scm.api.loads.LoaderVersions.java

public static String teste4() throws IOException {
    try (org.eclipse.jgit.lib.Repository repository = org.eclipse.jgit.api.Git.open(PATH).getRepository()) {
        // See e.g. GetRevCommitFromObjectId for how to use a SHA-1 directly
        Ref head = repository.findRef("HEAD");
        System.out.println(/*from  w w w  . java 2 s.c o  m*/
                "Ref of HEAD: " + head + ": " + head.getName() + " - " + head.getObjectId().getName() + "\n");
        System.out.println("Ref of HEAD getName: " + head.getName() + "\n");
        System.out.println("Ref of HEAD getId().getName: " + head.getObjectId().getName() + "\n");

        // a RevWalk allows to walk over commits based on some filtering that is defined
        try (RevWalk walk = new RevWalk(repository)) {
            RevCommit commit = walk.parseCommit(head.getObjectId());
            System.out.println("Commit: " + commit);

            // a commit points to a tree
            RevTree tree = walk.parseTree(commit.getTree().getId());
            System.out.println("FOUND TREE: " + tree.getName());

            walk.dispose();
            return tree.getName();
        }
    }
}

From source file:ch.sbb.releasetrain.git.GitRepoImpl.java

License:Apache License

private boolean remoteBranchExists(final Git git) throws GitAPIException {
    List<Ref> branchRefs = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();
    final String refsHeadBranch = "refs/remotes/origin/" + branch;
    for (Ref branchRef : branchRefs) {
        if (refsHeadBranch.equals(branchRef.getName())) {
            return true;
        }/*  ww w  . java  2 s .c  o  m*/
    }
    return false;
}

From source file:ch.sourcepond.maven.release.scm.git.GitProposedTagsBuilder.java

License:Apache License

private void validateTagNoAlreadyInRemoteRepo() throws SCMException {
    final List<String> tagNamesToSearchFor = new ArrayList<>();
    for (final ProposedTag annotatedTag : proposedTags.values()) {
        tagNamesToSearchFor.add(annotatedTag.name());
    }/*  w w  w . j a va2 s  . c om*/

    final List<String> matchingRemoteTags = new ArrayList<>();
    final Collection<Ref> remoteTags = repo.allRemoteTags();
    for (final Ref remoteTag : remoteTags) {
        for (final String proposedTag : tagNamesToSearchFor) {
            if (remoteTag.getName().equals("refs/tags/" + proposedTag)) {
                matchingRemoteTags.add(proposedTag);
            }
        }
    }

    if (!matchingRemoteTags.isEmpty()) {
        final SCMException exception = new SCMException(
                "Cannot release because there is already a tag with the same build number on the remote Git repo.");
        for (final String matchingRemoteTag : matchingRemoteTags) {
            exception.add(" * There is already a tag named %s in the remote repo.", matchingRemoteTag);
        }
        throw exception.add("Please try releasing again with a new build number.");
    }
}