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

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

Introduction

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

Prototype

@NonNull
Ref getTarget();

Source Link

Document

Get the reference this reference points to, or this .

Usage

From source file:com.benhumphreys.jgitcassandra.store.RefStore.java

License:Apache License

/**
 * Inserts a single ref into the database
 *
 * @throws IllegalStateException if the reference concrete type is not
 *                               one of the four handled classes
 *                               (@see RefType).
 *//*from   w w w  .ja va  2s.  c om*/
private void putRef(String name, Ref r) throws IOException {
    if (r instanceof SymbolicRef) {
        putRow(name, RefType.SYMBOLIC, r.getTarget().getName(), "");
    } else if (r instanceof ObjectIdRef.PeeledNonTag) {
        putRow(name, RefType.PEELED_NONTAG, r.getObjectId().name(), "");
    } else if (r instanceof ObjectIdRef.PeeledTag) {
        putRow(name, RefType.PEELED_TAG, r.getObjectId().name(), r.getPeeledObjectId().toString());
    } else if (r instanceof ObjectIdRef.Unpeeled) {
        putRow(name, RefType.UNPEELED, r.getObjectId().name(), "");
    } else {
        throw new IllegalStateException("Unhandled ref type: " + r);
    }
}

From source file:com.github.kaitoy.goslings.server.dao.jgit.ReferenceDaoImpl.java

License:Open Source License

@Override
public SymbolicReference[] getSymbolicReferences(String token) {
    Repository repo = resolver.getRepository(token);
    return Arrays.stream(SYMBOLIC_REFS).map(refName -> {
        try {/*from ww w .  jav  a  2s .c  o  m*/
            Ref ref = repo.findRef(refName);
            if (ref == null) {
                return null;
            }

            if (ref.isSymbolic()) {
                // attached to a branch
                return new SymbolicReference(ref.getName(), ref.getTarget().getName());
            } else {
                // detached
                return new SymbolicReference(ref.getName(), ref.getObjectId().getName());
            }
        } catch (IOException e) {
            String message = new StringBuilder().append("Failed to find HEAD in the repository ").append(token)
                    .append(" due to an I/O error.").toString();
            LOG.error(message, e);
            throw new DaoException(message, e);
        }
    }).filter(ref -> ref != null).toArray(SymbolicReference[]::new);
}

From source file:com.google.gerrit.httpd.rpc.project.ListBranches.java

License:Apache License

@Override
public ListBranchesResult call() throws NoSuchProjectException {
    final ProjectControl pctl = projectControlFactory.validateFor( //
            projectName, //
            ProjectControl.OWNER | ProjectControl.VISIBLE);

    final List<Branch> branches = new ArrayList<Branch>();
    Branch headBranch = null;/* ww w.  j a  va2  s  . co  m*/
    final Set<String> targets = new HashSet<String>();

    final Repository db;
    try {
        db = repoManager.openRepository(projectName);
    } catch (RepositoryNotFoundException noGitRepository) {
        return new ListBranchesResult(branches, false, true);
    }
    try {
        final Map<String, Ref> all = db.getAllRefs();

        if (!all.containsKey(Constants.HEAD)) {
            // The branch pointed to by HEAD doesn't exist yet, so getAllRefs
            // filtered it out. If we ask for it individually we can find the
            // underlying target and put it into the map anyway.
            //
            try {
                Ref head = db.getRef(Constants.HEAD);
                if (head != null) {
                    all.put(Constants.HEAD, head);
                }
            } catch (IOException e) {
                // Ignore the failure reading HEAD.
            }
        }

        for (final Ref ref : all.values()) {
            if (ref.isSymbolic()) {
                targets.add(ref.getTarget().getName());
            }
        }

        for (final Ref ref : all.values()) {
            if (ref.isSymbolic()) {
                // A symbolic reference to another branch, instead of
                // showing the resolved value, show the name it references.
                //
                String target = ref.getTarget().getName();
                RefControl targetRefControl = pctl.controlForRef(target);
                if (!targetRefControl.isVisible()) {
                    continue;
                }
                if (target.startsWith(Constants.R_HEADS)) {
                    target = target.substring(Constants.R_HEADS.length());
                }

                Branch b = createBranch(ref.getName());
                b.setRevision(new RevId(target));

                if (Constants.HEAD.equals(ref.getName())) {
                    b.setCanDelete(false);
                    headBranch = b;
                } else {
                    b.setCanDelete(targetRefControl.canDelete());
                    branches.add(b);
                }
                continue;
            }

            RefControl refControl = pctl.controlForRef(ref.getName());

            if (ref.getName().startsWith(Constants.R_HEADS) && refControl.isVisible()) {
                final Branch b = createBranch(ref.getName());
                if (ref.getObjectId() != null) {
                    b.setRevision(new RevId(ref.getObjectId().name()));
                }

                b.setCanDelete(!targets.contains(ref.getName()) && refControl.canDelete());

                branches.add(b);
            }
        }
    } finally {
        db.close();
    }
    Collections.sort(branches, new Comparator<Branch>() {
        @Override
        public int compare(final Branch a, final Branch b) {
            return a.getName().compareTo(b.getName());
        }
    });
    if (headBranch != null) {
        branches.add(0, headBranch);
    }
    return new ListBranchesResult(branches, pctl.canAddRefs(), false);
}

From source file:com.google.gerrit.httpd.rpc.project.ListBranchesTest.java

License:Apache License

private void assumeVisible(Ref ref, boolean visible, Set<String> targets) {
    RefControl rc = createStrictMock(RefControl.class);
    refMocks.add(rc);/*ww w. ja v  a  2 s  .  co  m*/
    expect(rc.isVisible()).andReturn(visible);
    if (visible && !ref.isSymbolic() && !targets.contains(ref.getName())) {
        expect(rc.canDelete()).andReturn(true);
    }

    if (ref.isSymbolic()) {
        expect(pc.controlForRef(ref.getTarget().getName())).andReturn(rc);
    } else {
        expect(pc.controlForRef(ref.getName())).andReturn(rc);
    }
}

From source file:com.google.gerrit.server.project.GetHead.java

License:Apache License

@Override
public String apply(ProjectResource rsrc) throws AuthException, ResourceNotFoundException, IOException {
    try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
        Ref head = repo.getRefDatabase().exactRef(Constants.HEAD);
        if (head == null) {
            throw new ResourceNotFoundException(Constants.HEAD);
        } else if (head.isSymbolic()) {
            String n = head.getTarget().getName();
            if (rsrc.getControl().controlForRef(n).isVisible()) {
                return n;
            }//from  w w  w  .j a va 2s  . c  o  m
            throw new AuthException("not allowed to see HEAD");
        } else if (head.getObjectId() != null) {
            try (RevWalk rw = new RevWalk(repo)) {
                RevCommit commit = rw.parseCommit(head.getObjectId());
                if (rsrc.getControl().canReadCommit(db.get(), rw, commit)) {
                    return head.getObjectId().name();
                }
                throw new AuthException("not allowed to see HEAD");
            } catch (MissingObjectException | IncorrectObjectTypeException e) {
                if (rsrc.getControl().isOwner()) {
                    return head.getObjectId().name();
                }
                throw new AuthException("not allowed to see HEAD");
            }
        }
        throw new ResourceNotFoundException(Constants.HEAD);
    } catch (RepositoryNotFoundException e) {
        throw new ResourceNotFoundException(rsrc.getName());
    }
}

From source file:com.google.gerrit.server.project.ListBranches.java

License:Apache License

private FluentIterable<BranchInfo> allBranches(ProjectResource rsrc)
        throws IOException, ResourceNotFoundException {
    List<Ref> refs;/*  w w w.  ja  va2  s  . com*/
    try (Repository db = repoManager.openRepository(rsrc.getNameKey())) {
        Collection<Ref> heads = db.getRefDatabase().getRefs(Constants.R_HEADS).values();
        refs = new ArrayList<>(heads.size() + 3);
        refs.addAll(heads);
        refs.addAll(db.getRefDatabase()
                .exactRef(Constants.HEAD, RefNames.REFS_CONFIG, RefNames.REFS_USERS_DEFAULT).values());
    } catch (RepositoryNotFoundException noGitRepository) {
        throw new ResourceNotFoundException();
    }

    Set<String> targets = Sets.newHashSetWithExpectedSize(1);
    for (Ref ref : refs) {
        if (ref.isSymbolic()) {
            targets.add(ref.getTarget().getName());
        }
    }

    List<BranchInfo> branches = new ArrayList<>(refs.size());
    for (Ref ref : refs) {
        if (ref.isSymbolic()) {
            // A symbolic reference to another branch, instead of
            // showing the resolved value, show the name it references.
            //
            String target = ref.getTarget().getName();
            RefControl targetRefControl = rsrc.getControl().controlForRef(target);
            if (!targetRefControl.isVisible()) {
                continue;
            }
            if (target.startsWith(Constants.R_HEADS)) {
                target = target.substring(Constants.R_HEADS.length());
            }

            BranchInfo b = new BranchInfo();
            b.ref = ref.getName();
            b.revision = target;
            branches.add(b);

            if (!Constants.HEAD.equals(ref.getName())) {
                b.canDelete = targetRefControl.canDelete() ? true : null;
            }
            continue;
        }

        RefControl refControl = rsrc.getControl().controlForRef(ref.getName());
        if (refControl.isVisible()) {
            branches.add(createBranchInfo(ref, refControl, targets));
        }
    }
    Collections.sort(branches, new BranchComparator());
    return FluentIterable.from(branches);
}

From source file:com.microsoft.gittf.core.util.StashUtil.java

License:Open Source License

/**
 * Creates a stash/*from ww w. ja  va  2s .  c  o m*/
 * 
 * @param repository
 *        the git repository
 * @param repositoryInserter
 *        the repository inserter object to use
 * @param rootBaseTree
 *        the tree id for the base commit of the stash
 * @param rootStashTree
 *        the tree id for the commit of the stash
 * @param rootIndexTree
 *        the tree id for the index commit of the stash
 * @param baseParentId
 *        the parent of the base tree commit
 * @param ownerDisplayName
 *        the owner display name of the stash
 * @param ownerName
 *        the owner name of the stash
 * @param stashComment
 *        the comment used to for the stash
 * @param stashName
 *        the stash name
 * @return
 * @throws IOException
 */
public static final ObjectId create(final Repository repository, final ObjectInserter repositoryInserter,
        final ObjectId rootBaseTree, final ObjectId rootStashTree, final ObjectId rootIndexTree,
        final ObjectId baseParentId, final String ownerDisplayName, final String ownerName,
        final String stashComment, final String stashName) throws IOException {
    Check.notNull(repository, "repository"); //$NON-NLS-1$
    Check.notNull(repositoryInserter, "repositoryInserter"); //$NON-NLS-1$
    Check.notNull(rootBaseTree, "rootBaseTree"); //$NON-NLS-1$
    Check.notNull(rootStashTree, "rootStashTree"); //$NON-NLS-1$
    Check.notNull(rootIndexTree, "rootIndexTree"); //$NON-NLS-1$

    /* identifies the head and the branch we are creating the stash for */
    Ref headReference = repository.getRef(Constants.HEAD);
    RevCommit headCommit = new RevWalk(repository).parseCommit(headReference.getObjectId());
    String currentBranchName = Repository.shortenRefName(headReference.getTarget().getName());

    PersonIdent author = new PersonIdent(ownerDisplayName, ownerName);

    /* create the base commit */
    CommitBuilder commitBuilder = new CommitBuilder();
    commitBuilder.setTreeId(rootBaseTree);
    if (baseParentId != null) {
        commitBuilder.setParentId(baseParentId);
    }
    commitBuilder.setMessage(stashComment);
    commitBuilder.setAuthor(author);
    commitBuilder.setCommitter(author);

    ObjectId baseCommit = repositoryInserter.insert(commitBuilder);

    /* create the index commit */
    commitBuilder.setTreeId(rootIndexTree);
    commitBuilder.setParentId(baseCommit);
    commitBuilder.setMessage(MessageFormat.format(STASH_INDEX_COMMENT, currentBranchName,
            headCommit.abbreviate(7).name(), stashName));
    commitBuilder.setAuthor(author);
    commitBuilder.setCommitter(author);

    ObjectId indexCommit = repositoryInserter.insert(commitBuilder);

    /* create the stash commit */
    commitBuilder.setTreeId(rootStashTree);
    commitBuilder.setParentId(baseCommit);
    commitBuilder.addParentId(indexCommit);

    String stashRefLogComment = MessageFormat.format(STASH_COMMENT, currentBranchName,
            headCommit.abbreviate(7).name(), stashName);
    commitBuilder.setMessage(stashRefLogComment);

    ObjectId stashCommit = repositoryInserter.insert(commitBuilder);

    repositoryInserter.flush();

    /* Update the stash reference and ref log */
    RefUpdate stashReferenceUpdate = repository.updateRef(Constants.R_STASH);
    stashReferenceUpdate.setNewObjectId(stashCommit);
    stashReferenceUpdate.setRefLogIdent(author);
    stashReferenceUpdate.setRefLogMessage(stashRefLogComment, false);

    Ref currentStashRef = repository.getRef(Constants.R_STASH);
    if (currentStashRef != null) {
        stashReferenceUpdate.setExpectedOldObjectId(currentStashRef.getObjectId());
    } else {
        stashReferenceUpdate.setExpectedOldObjectId(ObjectId.zeroId());
    }

    stashReferenceUpdate.forceUpdate();

    return stashCommit;
}

From source file:com.mooregreatsoftware.gitprocess.lib.DefaultBranches.java

License:Apache License

@Override
public @Nullable Branch currentBranch() {
    final Ref ref = ref(HEAD);
    if (ref == null)
        return null;
    final String targetName = ref.getTarget().getName();
    return targetName.startsWith(R_REFS) ? branch(targetName) : null;
}

From source file:com.navercorp.svngit.SVNGitUtil.java

License:Apache License

public static long getRevisionFromCommitId(Repository repo, ObjectId lastModified) throws IOException {
    String name = lastModified.getName();
    Ref ref = repo.getRef("refs/svn/id/" + name);
    Ref target = ref.getTarget();
    return getRevisionFromRefName(target.getName());
}

From source file:com.peergreen.configuration.git.GitRepository.java

License:Apache License

@Override
public Version getProductionVersion() throws RepositoryException {
    String name = "current-version";

    Ref ref = null;
    try {/*from www . j  a  v  a 2 s .  c  om*/
        ref = gitManager.repository().getRef(Constants.R_TAGS + name);
    } catch (IOException e) {
        throw new RepositoryException("Unable to get the production version", e);
    }

    if (ref != null && ref.isSymbolic()) {
        name = ref.getTarget().getName().substring(Constants.R_TAGS.length());
    }

    if (ref == null) {
        return null;
    }

    return new GitVersion(name);
}