Search in sources :

Example 21 with Ref

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

the class CommentsUtil method deleteAllDraftsFromAllUsers.

public void deleteAllDraftsFromAllUsers(Change.Id changeId) throws IOException {
    try (Repository repo = repoManager.openRepository(allUsers);
        RevWalk rw = new RevWalk(repo)) {
        BatchRefUpdate bru = repo.getRefDatabase().newBatchUpdate();
        for (Ref ref : getDraftRefs(repo, changeId)) {
            bru.addCommand(new ReceiveCommand(ref.getObjectId(), ObjectId.zeroId(), ref.getName()));
        }
        bru.setRefLogMessage("Delete drafts from NoteDb", false);
        bru.execute(rw, NullProgressMonitor.INSTANCE);
        for (ReceiveCommand cmd : bru.getCommands()) {
            if (cmd.getResult() != ReceiveCommand.Result.OK) {
                throw new IOException(String.format("Failed to delete draft comment ref %s at %s: %s (%s)", cmd.getRefName(), cmd.getOldId(), cmd.getResult(), cmd.getMessage()));
            }
        }
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate)

Example 22 with Ref

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

the class AccountsUpdate method deleteUserBranch.

public static void deleteUserBranch(Repository repo, PersonIdent refLogIdent, Account.Id accountId) throws IOException {
    String refName = RefNames.refsUsers(accountId);
    Ref ref = repo.exactRef(refName);
    if (ref == null) {
        return;
    }
    RefUpdate ru = repo.updateRef(refName);
    ru.setExpectedOldObjectId(ref.getObjectId());
    ru.setNewObjectId(ObjectId.zeroId());
    ru.setForceUpdate(true);
    ru.setRefLogIdent(refLogIdent);
    ru.setRefLogMessage("Delete Account", true);
    Result result = ru.delete();
    if (result != Result.FORCED) {
        throw new IOException(String.format("Failed to delete ref %s: %s", refName, result.name()));
    }
}
Also used : Ref(org.eclipse.jgit.lib.Ref) IOException(java.io.IOException) RefUpdate(org.eclipse.jgit.lib.RefUpdate) Result(org.eclipse.jgit.lib.RefUpdate.Result)

Example 23 with Ref

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

the class Mergeable method apply.

@Override
public MergeableInfo apply(RevisionResource resource) throws AuthException, ResourceConflictException, BadRequestException, OrmException, IOException {
    Change change = resource.getChange();
    PatchSet ps = resource.getPatchSet();
    MergeableInfo result = new MergeableInfo();
    if (!change.getStatus().isOpen()) {
        throw new ResourceConflictException("change is " + ChangeUtil.status(change));
    } else if (!ps.getId().equals(change.currentPatchSetId())) {
        // Only the current revision is mergeable. Others always fail.
        return result;
    }
    ChangeData cd = changeDataFactory.create(db.get(), resource.getControl());
    result.submitType = getSubmitType(cd, ps);
    try (Repository git = gitManager.openRepository(change.getProject())) {
        ObjectId commit = toId(ps);
        Ref ref = git.getRefDatabase().exactRef(change.getDest().get());
        ProjectState projectState = projectCache.get(change.getProject());
        String strategy = mergeUtilFactory.create(projectState).mergeStrategyName();
        result.strategy = strategy;
        result.mergeable = isMergable(git, change, commit, ref, result.submitType, strategy);
        if (otherBranches) {
            result.mergeableInto = new ArrayList<>();
            BranchOrderSection branchOrder = projectState.getBranchOrderSection();
            if (branchOrder != null) {
                int prefixLen = Constants.R_HEADS.length();
                String[] names = branchOrder.getMoreStable(ref.getName());
                Map<String, Ref> refs = git.getRefDatabase().exactRef(names);
                for (String n : names) {
                    Ref other = refs.get(n);
                    if (other == null) {
                        continue;
                    }
                    if (cache.get(commit, other, SubmitType.CHERRY_PICK, strategy, change.getDest(), git)) {
                        result.mergeableInto.add(other.getName().substring(prefixLen));
                    }
                }
            }
        }
    }
    return result;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) BranchOrderSection(com.google.gerrit.server.git.BranchOrderSection) ChangeData(com.google.gerrit.server.query.change.ChangeData) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) MergeableInfo(com.google.gerrit.extensions.common.MergeableInfo) ProjectState(com.google.gerrit.server.project.ProjectState)

Example 24 with Ref

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

the class IncludedInResolver method parseCommits.

/** Parse commit of ref and store the relation between ref and commit. */
private void parseCommits(final Collection<Ref> refs) throws IOException {
    if (commitToRef != null) {
        return;
    }
    commitToRef = LinkedListMultimap.create();
    for (Ref ref : refs) {
        final RevCommit commit;
        try {
            commit = rw.parseCommit(ref.getObjectId());
        } catch (IncorrectObjectTypeException notCommit) {
            //
            continue;
        } catch (MissingObjectException notHere) {
            // Log the problem with this branch, but keep processing.
            //
            log.warn("Reference " + ref.getName() + " in " + repo.getDirectory() + " points to dangling object " + ref.getObjectId());
            continue;
        }
        commitToRef.put(commit, ref.getName());
    }
    tipsByCommitTime = Lists.newArrayList(commitToRef.keySet());
    sortOlderFirst(tipsByCommitTime);
}
Also used : Ref(org.eclipse.jgit.lib.Ref) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 25 with Ref

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

the class ConflictsPredicate method listFiles.

public static List<String> listFiles(Change c, Arguments args, ChangeDataCache changeDataCache) throws OrmException {
    try (Repository repo = args.repoManager.openRepository(c.getProject());
        RevWalk rw = new RevWalk(repo)) {
        RevCommit ps = rw.parseCommit(changeDataCache.getTestAgainst());
        if (ps.getParentCount() > 1) {
            String dest = c.getDest().get();
            Ref destBranch = repo.getRefDatabase().getRef(dest);
            destBranch.getObjectId();
            rw.setRevFilter(RevFilter.MERGE_BASE);
            rw.markStart(rw.parseCommit(destBranch.getObjectId()));
            rw.markStart(ps);
            RevCommit base = rw.next();
            // TODO(zivkov): handle the case with multiple merge bases
            List<String> files = new ArrayList<>();
            try (TreeWalk tw = new TreeWalk(repo)) {
                if (base != null) {
                    tw.setFilter(TreeFilter.ANY_DIFF);
                    tw.addTree(base.getTree());
                }
                tw.addTree(ps.getTree());
                tw.setRecursive(true);
                while (tw.next()) {
                    files.add(tw.getPathString());
                }
            }
            return files;
        }
        return args.changeDataFactory.create(args.db.get(), c).currentFilePaths();
    } catch (IOException e) {
        throw new OrmException(e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) OrmException(com.google.gwtorm.server.OrmException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CodeReviewRevWalk(com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

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