Search in sources :

Example 26 with Ref

use of org.eclipse.jgit.lib.Ref in project gerrit by GerritCodeReview.

the class InternalChangeQuery method byCommitsOnBranchNotMergedFromDatabase.

private Iterable<ChangeData> byCommitsOnBranchNotMergedFromDatabase(Repository repo, final ReviewDb db, final Branch.NameKey branch, Collection<String> hashes) throws OrmException, IOException {
    Set<Change.Id> changeIds = Sets.newHashSetWithExpectedSize(hashes.size());
    String lastPrefix = null;
    for (Ref ref : repo.getRefDatabase().getRefs(RefNames.REFS_CHANGES).values()) {
        String r = ref.getName();
        if ((lastPrefix != null && r.startsWith(lastPrefix)) || !hashes.contains(ref.getObjectId().name())) {
            continue;
        }
        Change.Id id = Change.Id.fromRef(r);
        if (id == null) {
            continue;
        }
        if (changeIds.add(id)) {
            lastPrefix = r.substring(0, r.lastIndexOf('/'));
        }
    }
    List<ChangeNotes> notes = notesFactory.create(db, branch.getParentKey(), changeIds, cn -> {
        Change c = cn.getChange();
        return c.getDest().equals(branch) && c.getStatus() != Change.Status.MERGED;
    });
    return Lists.transform(notes, n -> changeDataFactory.create(db, n));
}
Also used : Ref(org.eclipse.jgit.lib.Ref) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.reviewdb.client.Change) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes)

Example 27 with Ref

use of org.eclipse.jgit.lib.Ref in project gerrit by GerritCodeReview.

the class SetHead method apply.

@Override
public String apply(final ProjectResource rsrc, Input input) throws AuthException, ResourceNotFoundException, BadRequestException, UnprocessableEntityException, IOException {
    if (!rsrc.getControl().isOwner()) {
        throw new AuthException("restricted to project owner");
    }
    if (input == null || Strings.isNullOrEmpty(input.ref)) {
        throw new BadRequestException("ref required");
    }
    String ref = RefNames.fullName(input.ref);
    try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
        Map<String, Ref> cur = repo.getRefDatabase().exactRef(Constants.HEAD, ref);
        if (!cur.containsKey(ref)) {
            throw new UnprocessableEntityException(String.format("Ref Not Found: %s", ref));
        }
        final String oldHead = cur.get(Constants.HEAD).getTarget().getName();
        final String newHead = ref;
        if (!oldHead.equals(newHead)) {
            final RefUpdate u = repo.updateRef(Constants.HEAD, true);
            u.setRefLogIdent(identifiedUser.get().newRefLogIdent());
            RefUpdate.Result res = u.link(newHead);
            switch(res) {
                case NO_CHANGE:
                case RENAMED:
                case FORCED:
                case NEW:
                    break;
                case FAST_FORWARD:
                case IO_FAILURE:
                case LOCK_FAILURE:
                case NOT_ATTEMPTED:
                case REJECTED:
                case REJECTED_CURRENT_BRANCH:
                default:
                    throw new IOException("Setting HEAD failed with " + res);
            }
            fire(rsrc.getNameKey(), oldHead, newHead);
        }
        return ref;
    } catch (RepositoryNotFoundException e) {
        throw new ResourceNotFoundException(rsrc.getName());
    }
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) IOException(java.io.IOException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 28 with Ref

use of org.eclipse.jgit.lib.Ref in project gerrit by GerritCodeReview.

the class ChangeData method draftRefs.

public Map<Account.Id, Ref> draftRefs() throws OrmException {
    if (draftsByUser == null) {
        if (!lazyLoad) {
            return Collections.emptyMap();
        }
        Change c = change();
        if (c == null) {
            return Collections.emptyMap();
        }
        draftsByUser = new HashMap<>();
        if (notesMigration.readChanges()) {
            for (Ref ref : commentsUtil.getDraftRefs(notes.getChangeId())) {
                Account.Id account = Account.Id.fromRefSuffix(ref.getName());
                if (account != null && // this point.
                !notes().getDraftComments(account, ref).isEmpty()) {
                    draftsByUser.put(account, ref);
                }
            }
        } else {
            for (Comment sc : commentsUtil.draftByChange(db, notes())) {
                draftsByUser.put(sc.author.getId(), null);
            }
        }
    }
    return draftsByUser;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) Comment(com.google.gerrit.reviewdb.client.Comment) RobotComment(com.google.gerrit.reviewdb.client.RobotComment) StarRef(com.google.gerrit.server.StarredChangesUtil.StarRef) Ref(org.eclipse.jgit.lib.Ref) Change(com.google.gerrit.reviewdb.client.Change)

Example 29 with Ref

use of org.eclipse.jgit.lib.Ref in project gerrit by GerritCodeReview.

the class ChangeData method isMergeable.

public Boolean isMergeable() throws OrmException {
    if (mergeable == null) {
        Change c = change();
        if (c == null) {
            return null;
        }
        if (c.getStatus() == Change.Status.MERGED) {
            mergeable = true;
        } else if (c.getStatus() == Change.Status.ABANDONED) {
            return null;
        } else if (c.isWorkInProgress()) {
            return null;
        } else {
            if (!lazyLoad) {
                return null;
            }
            PatchSet ps = currentPatchSet();
            try {
                if (ps == null || !changeControl().isPatchVisible(ps, db)) {
                    return null;
                }
            } catch (OrmException e) {
                if (e.getCause() instanceof NoSuchChangeException) {
                    return null;
                }
                throw e;
            }
            try (Repository repo = repoManager.openRepository(project())) {
                Ref ref = repo.getRefDatabase().exactRef(c.getDest().get());
                SubmitTypeRecord str = submitTypeRecord();
                if (!str.isOk()) {
                    // No need to log, as SubmitRuleEvaluator already did it for us.
                    return false;
                }
                String mergeStrategy = mergeUtilFactory.create(projectCache.get(project())).mergeStrategyName();
                mergeable = mergeabilityCache.get(ObjectId.fromString(ps.getRevision().get()), ref, str.type, mergeStrategy, c.getDest(), repo);
            } catch (IOException e) {
                throw new OrmException(e);
            }
        }
    }
    return mergeable;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) StarRef(com.google.gerrit.server.StarredChangesUtil.StarRef) Ref(org.eclipse.jgit.lib.Ref) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) SubmitTypeRecord(com.google.gerrit.common.data.SubmitTypeRecord) OrmException(com.google.gwtorm.server.OrmException) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) IOException(java.io.IOException)

Example 30 with Ref

use of org.eclipse.jgit.lib.Ref in project gitiles by GerritCodeReview.

the class RefServlet method getBranchesSoyData.

static List<Map<String, Object>> getBranchesSoyData(HttpServletRequest req, int limit) throws IOException {
    RefDatabase refdb = ServletUtils.getRepository(req).getRefDatabase();
    Ref head = refdb.exactRef(Constants.HEAD);
    Ref headLeaf = head != null && head.isSymbolic() ? head.getLeaf() : null;
    return getRefsSoyData(refdb, ViewFilter.getView(req), Constants.R_HEADS, branchComparator(headLeaf), headLeaf, limit);
}
Also used : Ref(org.eclipse.jgit.lib.Ref) RefDatabase(org.eclipse.jgit.lib.RefDatabase)

Aggregations

Ref (org.eclipse.jgit.lib.Ref)137 IOException (java.io.IOException)55 RevCommit (org.eclipse.jgit.revwalk.RevCommit)45 ObjectId (org.eclipse.jgit.lib.ObjectId)38 RevWalk (org.eclipse.jgit.revwalk.RevWalk)37 Repository (org.eclipse.jgit.lib.Repository)35 Change (com.google.gerrit.reviewdb.client.Change)22 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)18 Git (org.eclipse.jgit.api.Git)18 File (java.io.File)16 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)12 OrmException (com.google.gwtorm.server.OrmException)12 Map (java.util.Map)12 PersonIdent (org.eclipse.jgit.lib.PersonIdent)12 Status (org.eclipse.jgit.api.Status)11 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)11 HashMap (java.util.HashMap)10 Account (com.google.gerrit.reviewdb.client.Account)9 Branch (com.google.gerrit.reviewdb.client.Branch)9