Example usage for org.eclipse.jgit.lib Constants R_HEADS

List of usage examples for org.eclipse.jgit.lib Constants R_HEADS

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Constants R_HEADS.

Prototype

String R_HEADS

To view the source code for org.eclipse.jgit.lib Constants R_HEADS.

Click Source Link

Document

Prefix for branch refs

Usage

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

License:Apache License

private void markChangeMergedByPush(ReviewDb db, final ReplaceRequest result, ChangeControl control)
        throws OrmException, IOException {
    Change.Id id = result.change.getId();
    db.changes().beginTransaction(id);//from w w  w  .j  a  v  a  2s .co m
    Change change;

    ChangeUpdate update;
    try {
        change = db.changes().atomicUpdate(id, new AtomicUpdate<Change>() {
            @Override
            public Change update(Change change) {
                if (change.getStatus().isOpen()) {
                    change.setCurrentPatchSet(result.info);
                    change.setStatus(Change.Status.MERGED);
                    ChangeUtil.updated(change);
                }
                return change;
            }
        });
        String mergedIntoRef = result.mergedIntoRef;

        StringBuilder msgBuf = new StringBuilder();
        msgBuf.append("Change has been successfully pushed");
        if (!mergedIntoRef.equals(change.getDest().get())) {
            msgBuf.append(" into ");
            if (mergedIntoRef.startsWith(Constants.R_HEADS)) {
                msgBuf.append("branch ");
                msgBuf.append(Repository.shortenRefName(mergedIntoRef));
            } else {
                msgBuf.append(mergedIntoRef);
            }
        }
        msgBuf.append(".");
        ChangeMessage msg = new ChangeMessage(new ChangeMessage.Key(id, ChangeUtil.messageUUID(db)),
                currentUser.getAccountId(), change.getLastUpdatedOn(), result.info.getKey());
        msg.setMessage(msgBuf.toString());

        update = updateFactory.create(control, change.getLastUpdatedOn());

        cmUtil.addChangeMessage(db, update, msg);
        db.commit();
    } finally {
        db.rollback();
    }
    indexer.index(db, change);
    update.commit();
}

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

License:Apache License

private static boolean isHead(final Ref ref) {
    return ref.getName().startsWith(Constants.R_HEADS);
}

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

License:Apache License

private static boolean isHead(final ReceiveCommand cmd) {
    return cmd.getRefName().startsWith(Constants.R_HEADS);
}

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

License:Apache License

private Ref findMergedInto(ChangeContext ctx, String first, RevCommit commit) {
    try {/*from  w w w.  j av a  2  s.  c o  m*/
        RefDatabase refDatabase = ctx.getRepository().getRefDatabase();

        Ref firstRef = refDatabase.exactRef(first);
        if (firstRef != null && isMergedInto(ctx.getRevWalk(), commit, firstRef)) {
            return firstRef;
        }

        for (Ref ref : refDatabase.getRefs(Constants.R_HEADS).values()) {
            if (isMergedInto(ctx.getRevWalk(), commit, ref)) {
                return ref;
            }
        }
        return null;
    } catch (IOException e) {
        log.warn("Can't check for already submitted change", e);
        return null;
    }
}

From source file:com.google.gerrit.server.git.strategy.SubmitDryRun.java

License:Apache License

public static Iterable<ObjectId> getAlreadyAccepted(Repository repo) throws IOException {
    return FluentIterable.from(repo.getRefDatabase().getRefs(Constants.R_HEADS).values())
            .append(repo.getRefDatabase().getRefs(Constants.R_TAGS).values())
            .transform(new Function<Ref, ObjectId>() {
                @Override/*from  w ww. java2 s .com*/
                public ObjectId apply(Ref r) {
                    return r.getObjectId();
                }
            });
}

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

License:Apache License

@Override
public BranchInfo apply(ProjectResource rsrc, Input input)
        throws BadRequestException, AuthException, ResourceConflictException, IOException {
    if (input == null) {
        input = new Input();
    }/*w  w  w  .  ja v  a 2  s  .c  o  m*/
    if (input.ref != null && !ref.equals(input.ref)) {
        throw new BadRequestException("ref must match URL");
    }
    if (input.revision == null) {
        input.revision = Constants.HEAD;
    }
    while (ref.startsWith("/")) {
        ref = ref.substring(1);
    }
    ref = RefNames.fullName(ref);
    if (!Repository.isValidRefName(ref)) {
        throw new BadRequestException("invalid branch name \"" + ref + "\"");
    }
    if (MagicBranch.isMagicBranch(ref)) {
        throw new BadRequestException(
                "not allowed to create branches under \"" + MagicBranch.getMagicRefNamePrefix(ref) + "\"");
    }

    final Branch.NameKey name = new Branch.NameKey(rsrc.getNameKey(), ref);
    final RefControl refControl = rsrc.getControl().controlForRef(name);
    try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
        final ObjectId revid = parseBaseRevision(repo, rsrc.getNameKey(), input.revision);
        final RevWalk rw = verifyConnected(repo, revid);
        RevObject object = rw.parseAny(revid);

        if (ref.startsWith(Constants.R_HEADS)) {
            // Ensure that what we start the branch from is a commit. If we
            // were given a tag, deference to the commit instead.
            //
            try {
                object = rw.parseCommit(object);
            } catch (IncorrectObjectTypeException notCommit) {
                throw new BadRequestException("\"" + input.revision + "\" not a commit");
            }
        }

        rw.reset();
        if (!refControl.canCreate(db.get(), rw, object)) {
            throw new AuthException("Cannot create \"" + ref + "\"");
        }

        try {
            final RefUpdate u = repo.updateRef(ref);
            u.setExpectedOldObjectId(ObjectId.zeroId());
            u.setNewObjectId(object.copy());
            u.setRefLogIdent(identifiedUser.get().newRefLogIdent());
            u.setRefLogMessage("created via REST from " + input.revision, false);
            final RefUpdate.Result result = u.update(rw);
            switch (result) {
            case FAST_FORWARD:
            case NEW:
            case NO_CHANGE:
                referenceUpdated.fire(name.getParentKey(), u, ReceiveCommand.Type.CREATE);
                hooks.doRefUpdatedHook(name, u, identifiedUser.get().getAccount());
                break;
            case LOCK_FAILURE:
                if (repo.getRefDatabase().exactRef(ref) != null) {
                    throw new ResourceConflictException("branch \"" + ref + "\" already exists");
                }
                String refPrefix = getRefPrefix(ref);
                while (!Constants.R_HEADS.equals(refPrefix)) {
                    if (repo.getRefDatabase().exactRef(refPrefix) != null) {
                        throw new ResourceConflictException("Cannot create branch \"" + ref
                                + "\" since it conflicts with branch \"" + refPrefix + "\".");
                    }
                    refPrefix = getRefPrefix(refPrefix);
                }
                //$FALL-THROUGH$
            default: {
                throw new IOException(result.name());
            }
            }

            BranchInfo info = new BranchInfo();
            info.ref = ref;
            info.revision = revid.getName();
            info.canDelete = refControl.canDelete() ? true : null;
            return info;
        } catch (IOException err) {
            log.error("Cannot create branch \"" + name + "\"", err);
            throw err;
        }
    } catch (InvalidRevisionException e) {
        throw new BadRequestException("invalid revision \"" + input.revision + "\"");
    }
}

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

License:Apache License

private static String getRefPrefix(final String refName) {
    final int i = refName.lastIndexOf('/');
    if (i > Constants.R_HEADS.length() - 1) {
        return refName.substring(0, i);
    }//w ww. ja va2s  .  com
    return Constants.R_HEADS;
}

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

License:Apache License

private RevWalk verifyConnected(final Repository repo, final ObjectId revid) throws InvalidRevisionException {
    try {//from  w  ww  .j a va 2s.c o  m
        final ObjectWalk rw = new ObjectWalk(repo);
        try {
            rw.markStart(rw.parseCommit(revid));
        } catch (IncorrectObjectTypeException err) {
            throw new InvalidRevisionException();
        }
        RefDatabase refDb = repo.getRefDatabase();
        Iterable<Ref> refs = Iterables.concat(refDb.getRefs(Constants.R_HEADS).values(),
                refDb.getRefs(Constants.R_TAGS).values());
        Ref rc = refDb.exactRef(RefNames.REFS_CONFIG);
        if (rc != null) {
            refs = Iterables.concat(refs, Collections.singleton(rc));
        }
        for (Ref r : refs) {
            try {
                rw.markUninteresting(rw.parseAny(r.getObjectId()));
            } catch (MissingObjectException err) {
                continue;
            }
        }
        rw.checkConnectivity();
        return rw;
    } catch (IncorrectObjectTypeException | MissingObjectException err) {
        throw new InvalidRevisionException();
    } catch (IOException err) {
        log.error("Repository \"" + repo.getDirectory() + "\" may be corrupt; suggest running git fsck", err);
        throw new InvalidRevisionException();
    }
}

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

License:Apache License

private List<String> normalizeBranchNames(List<String> branches) throws BadRequestException {
    if (branches == null || branches.isEmpty()) {
        return Collections.singletonList(Constants.R_HEADS + Constants.MASTER);
    }/* w  w w.j a v a  2 s  .c o  m*/

    List<String> normalizedBranches = new ArrayList<>();
    for (String branch : branches) {
        while (branch.startsWith("/")) {
            branch = branch.substring(1);
        }
        branch = RefNames.fullName(branch);
        if (!Repository.isValidRefName(branch)) {
            throw new BadRequestException(String.format("Branch \"%s\" is not a valid name.", branch));
        }
        if (!normalizedBranches.contains(branch)) {
            normalizedBranches.add(branch);
        }
    }
    return normalizedBranches;
}

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;/*from  w  ww. j a va2 s  .  co  m*/
    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);
}