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

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

Introduction

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

Prototype

boolean isSymbolic();

Source Link

Document

Test if this reference is a symbolic reference.

Usage

From source file:com.benhumphreys.jgitcassandra.repo.CassandraRefDatabase.java

License:Apache License

/**
 * Read all known references in the repository.
 *
 * @return all current references of the repository.
 * @throws IOException references cannot be accessed.
 *//* w  ww  .  j  ava 2s .co m*/
@Override
protected RefCache scanAllRefs() throws IOException {
    RefList.Builder<Ref> ids = new RefList.Builder<Ref>();
    RefList.Builder<Ref> sym = new RefList.Builder<Ref>();
    for (Ref ref : refs.values()) {
        if (ref.isSymbolic()) {
            sym.add(ref);
        }
        ids.add(ref);
    }
    ids.sort();
    sym.sort();
    return new RefCache(ids.toRefList(), sym.toRefList());
}

From source file:com.genuitec.eclipse.gerrit.tools.internal.fbranches.commands.MergeStableIntoCurrentBranchCommand.java

License:Open Source License

private boolean canMerge(final Repository repository) {
    String message = null;// w w w  . jav  a2 s  . c o m
    Exception ex = null;
    try {
        Ref head = repository.getRef(Constants.HEAD);
        if (head == null || !head.isSymbolic())
            message = UIText.MergeAction_HeadIsNoBranch;
        else if (!repository.getRepositoryState().equals(RepositoryState.SAFE))
            message = NLS.bind(UIText.MergeAction_WrongRepositoryState, repository.getRepositoryState());
        else if (!head.getLeaf().getName().startsWith("refs/heads/features")) { //$NON-NLS-1$
            message = "Current branch is not a feature branch.";
        }
    } catch (IOException e) {
        message = e.getMessage();
        ex = e;
    }

    if (message != null)
        org.eclipse.egit.ui.Activator.handleError(message, ex, true);
    return (message == null);
}

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 {// www  .  j  ava 2  s .  co 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;/*from w w  w  .j a  va 2s  .  c om*/
    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 Set<String> targets(Map<String, Ref> refs) {
    Set<String> targets = new HashSet<String>();
    for (Ref ref : refs.values()) {
        if (ref.isSymbolic()) {
            targets.add(ref.getLeaf().getName());
        }//from  w  ww  . j ava2s  .  com
    }
    return targets;
}

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);//from  www .  j a v a2s .  c  om
    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.httpd.rpc.project.ProjectDetailFactory.java

License:Apache License

@Override
public ProjectDetail call() throws NoSuchProjectException, IOException {
    final ProjectControl pc = projectControlFactory.validateFor(projectName,
            ProjectControl.OWNER | ProjectControl.VISIBLE);
    final ProjectState projectState = pc.getProjectState();
    final ProjectDetail detail = new ProjectDetail();
    detail.setProject(projectState.getProject());

    final boolean userIsOwner = pc.isOwner();
    final boolean userIsOwnerAnyRef = pc.isOwnerAnyRef();

    detail.setCanModifyAccess(userIsOwnerAnyRef);
    detail.setCanModifyAgreements(userIsOwner);
    detail.setCanModifyDescription(userIsOwner);
    detail.setCanModifyMergeType(userIsOwner);
    detail.setCanModifyState(userIsOwner);

    final InheritedBoolean useContributorAgreements = new InheritedBoolean();
    final InheritedBoolean useSignedOffBy = new InheritedBoolean();
    final InheritedBoolean useContentMerge = new InheritedBoolean();
    final InheritedBoolean requireChangeID = new InheritedBoolean();
    useContributorAgreements.setValue(projectState.getProject().getUseContributorAgreements());
    useSignedOffBy.setValue(projectState.getProject().getUseSignedOffBy());
    useContentMerge.setValue(projectState.getProject().getUseContentMerge());
    requireChangeID.setValue(projectState.getProject().getRequireChangeID());
    ProjectState parentState = Iterables.getFirst(projectState.parents(), null);
    if (parentState != null) {
        useContributorAgreements.setInheritedValue(parentState.isUseContributorAgreements());
        useSignedOffBy.setInheritedValue(parentState.isUseSignedOffBy());
        useContentMerge.setInheritedValue(parentState.isUseContentMerge());
        requireChangeID.setInheritedValue(parentState.isRequireChangeID());
    }//from  w  ww  .j  a va  2 s .  c o m
    detail.setUseContributorAgreements(useContributorAgreements);
    detail.setUseSignedOffBy(useSignedOffBy);
    detail.setUseContentMerge(useContentMerge);
    detail.setRequireChangeID(requireChangeID);

    final Project.NameKey projectName = projectState.getProject().getNameKey();
    Repository git;
    try {
        git = gitRepositoryManager.openRepository(projectName);
    } catch (RepositoryNotFoundException err) {
        throw new NoSuchProjectException(projectName);
    }
    try {
        Ref head = git.getRef(Constants.HEAD);
        if (head != null && head.isSymbolic()
                && GitRepositoryManager.REF_CONFIG.equals(head.getLeaf().getName())) {
            detail.setPermissionOnly(true);
        }
    } catch (IOException err) {
        throw new NoSuchProjectException(projectName);
    } finally {
        git.close();
    }

    return detail;
}

From source file:com.google.gerrit.server.git.TagSet.java

License:Apache License

void prepare(TagMatcher m) {
    @SuppressWarnings("resource")
    RevWalk rw = null;/*from www.  j  a  v  a  2  s  .  c o  m*/
    try {
        for (Ref currentRef : m.include) {
            if (currentRef.isSymbolic()) {
                continue;
            }
            if (currentRef.getObjectId() == null) {
                continue;
            }

            CachedRef savedRef = refs.get(currentRef.getName());
            if (savedRef == null) {
                // If the reference isn't known to the set, return null
                // and force the caller to rebuild the set in a new copy.
                m.newRefs.add(currentRef);
                continue;
            }

            // The reference has not been moved. It can be used as-is.
            ObjectId savedObjectId = savedRef.get();
            if (currentRef.getObjectId().equals(savedObjectId)) {
                m.mask.set(savedRef.flag);
                continue;
            }

            // Check on-the-fly to see if the branch still reaches the tag.
            // This is very likely for a branch that fast-forwarded.
            try {
                if (rw == null) {
                    rw = new RevWalk(m.db);
                    rw.setRetainBody(false);
                }

                RevCommit savedCommit = rw.parseCommit(savedObjectId);
                RevCommit currentCommit = rw.parseCommit(currentRef.getObjectId());
                if (rw.isMergedInto(savedCommit, currentCommit)) {
                    // Fast-forward. Safely update the reference in-place.
                    savedRef.compareAndSet(savedObjectId, currentRef.getObjectId());
                    m.mask.set(savedRef.flag);
                    continue;
                }

                // The branch rewound. Walk the list of commits removed from
                // the reference. If any matches to a tag, this has to be removed.
                boolean err = false;
                rw.reset();
                rw.markStart(savedCommit);
                rw.markUninteresting(currentCommit);
                rw.sort(RevSort.TOPO, true);
                RevCommit c;
                while ((c = rw.next()) != null) {
                    Tag tag = tags.get(c);
                    if (tag != null && tag.refFlags.get(savedRef.flag)) {
                        m.lostRefs.add(new TagMatcher.LostRef(tag, savedRef.flag));
                        err = true;
                    }
                }
                if (!err) {
                    // All of the tags are still reachable. Update in-place.
                    savedRef.compareAndSet(savedObjectId, currentRef.getObjectId());
                    m.mask.set(savedRef.flag);
                }

            } catch (IOException err) {
                // Defer a cache update until later. No conclusion can be made
                // based on an exception reading from the repository storage.
                log.warn("Error checking tags of " + projectName, err);
            }
        }
    } finally {
        if (rw != null) {
            rw.close();
        }
    }
}

From source file:com.google.gerrit.server.git.TagSet.java

License:Apache License

static boolean skip(Ref ref) {
    return ref.isSymbolic() || ref.getObjectId() == null || PatchSet.isRef(ref.getName());
}

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;
            }//  w  ww  . j ava  2  s. co 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());
    }
}