Search in sources :

Example 16 with RevCommit

use of org.eclipse.jgit.revwalk.RevCommit in project gerrit by GerritCodeReview.

the class SubmoduleOp method composeGitlinksCommit.

/** Create a separate gitlink commit */
public CodeReviewCommit composeGitlinksCommit(final Branch.NameKey subscriber) throws IOException, SubmoduleException {
    OpenRepo or;
    try {
        or = orm.getRepo(subscriber.getParentKey());
    } catch (NoSuchProjectException | IOException e) {
        throw new SubmoduleException("Cannot access superproject", e);
    }
    CodeReviewCommit currentCommit;
    if (branchTips.containsKey(subscriber)) {
        currentCommit = branchTips.get(subscriber);
    } else {
        Ref r = or.repo.exactRef(subscriber.get());
        if (r == null) {
            throw new SubmoduleException("The branch was probably deleted from the subscriber repository");
        }
        currentCommit = or.rw.parseCommit(r.getObjectId());
        addBranchTip(subscriber, currentCommit);
    }
    StringBuilder msgbuf = new StringBuilder("");
    PersonIdent author = null;
    DirCache dc = readTree(or.rw, currentCommit);
    DirCacheEditor ed = dc.editor();
    for (SubmoduleSubscription s : targets.get(subscriber)) {
        RevCommit newCommit = updateSubmodule(dc, ed, msgbuf, s);
        if (newCommit != null) {
            if (author == null) {
                author = newCommit.getAuthorIdent();
            } else if (!author.equals(newCommit.getAuthorIdent())) {
                author = myIdent;
            }
        }
    }
    ed.finish();
    ObjectId newTreeId = dc.writeTree(or.ins);
    // Gitlinks are already in the branch, return null
    if (newTreeId.equals(currentCommit.getTree())) {
        return null;
    }
    CommitBuilder commit = new CommitBuilder();
    commit.setTreeId(newTreeId);
    commit.setParentId(currentCommit);
    StringBuilder commitMsg = new StringBuilder("Update git submodules\n\n");
    if (verboseSuperProject != VerboseSuperprojectUpdate.FALSE) {
        commitMsg.append(msgbuf);
    }
    commit.setMessage(commitMsg.toString());
    commit.setAuthor(author);
    commit.setCommitter(myIdent);
    ObjectId id = or.ins.insert(commit);
    return or.rw.parseCommit(id);
}
Also used : NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) IOException(java.io.IOException) DirCacheEditor(org.eclipse.jgit.dircache.DirCacheEditor) DirCache(org.eclipse.jgit.dircache.DirCache) Ref(org.eclipse.jgit.lib.Ref) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) OpenRepo(com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo) SubmoduleSubscription(com.google.gerrit.reviewdb.client.SubmoduleSubscription) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 17 with RevCommit

use of org.eclipse.jgit.revwalk.RevCommit in project gerrit by GerritCodeReview.

the class SubmoduleOp method createSubmoduleCommitMsg.

private void createSubmoduleCommitMsg(StringBuilder msgbuf, SubmoduleSubscription s, OpenRepo subOr, RevCommit newCommit, RevCommit oldCommit) throws SubmoduleException {
    msgbuf.append("* Update " + s.getPath());
    msgbuf.append(" from branch '" + s.getSubmodule().getShortName() + "'");
    // newly created submodule gitlink, do not append whole history
    if (oldCommit == null) {
        return;
    }
    try {
        subOr.rw.resetRetain(subOr.canMergeFlag);
        subOr.rw.markStart(newCommit);
        subOr.rw.markUninteresting(oldCommit);
        for (RevCommit c : subOr.rw) {
            subOr.rw.parseBody(c);
            if (verboseSuperProject == VerboseSuperprojectUpdate.SUBJECT_ONLY) {
                msgbuf.append("\n  - " + c.getShortMessage());
            } else if (verboseSuperProject == VerboseSuperprojectUpdate.TRUE) {
                msgbuf.append("\n  - " + c.getFullMessage().replace("\n", "\n    "));
            }
        }
    } catch (IOException e) {
        throw new SubmoduleException("Could not perform a revwalk to create superproject commit message", e);
    }
}
Also used : IOException(java.io.IOException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 18 with RevCommit

use of org.eclipse.jgit.revwalk.RevCommit in project gerrit by GerritCodeReview.

the class CheckMergeability method apply.

@Override
public MergeableInfo apply(BranchResource resource) throws IOException, BadRequestException, ResourceNotFoundException {
    if (!(submitType.equals(SubmitType.MERGE_ALWAYS) || submitType.equals(SubmitType.MERGE_IF_NECESSARY))) {
        throw new BadRequestException("Submit type: " + submitType + " is not supported");
    }
    MergeableInfo result = new MergeableInfo();
    result.submitType = submitType;
    result.strategy = strategy;
    try (Repository git = gitManager.openRepository(resource.getNameKey());
        RevWalk rw = new RevWalk(git);
        ObjectInserter inserter = new InMemoryInserter(git)) {
        Merger m = MergeUtil.newMerger(inserter, git.getConfig(), strategy);
        Ref destRef = git.getRefDatabase().exactRef(resource.getRef());
        if (destRef == null) {
            throw new ResourceNotFoundException(resource.getRef());
        }
        RevCommit targetCommit = rw.parseCommit(destRef.getObjectId());
        RevCommit sourceCommit = MergeUtil.resolveCommit(git, rw, source);
        if (!resource.getControl().canReadCommit(db.get(), git, sourceCommit)) {
            throw new BadRequestException("do not have read permission for: " + source);
        }
        if (rw.isMergedInto(sourceCommit, targetCommit)) {
            result.mergeable = true;
            result.commitMerged = true;
            result.contentMerged = true;
            return result;
        }
        if (m.merge(false, targetCommit, sourceCommit)) {
            result.mergeable = true;
            result.commitMerged = false;
            result.contentMerged = m.getResultTreeId().equals(targetCommit.getTree());
        } else {
            result.mergeable = false;
            if (m instanceof ResolveMerger) {
                result.conflicts = ((ResolveMerger) m).getUnmergedPaths();
            }
        }
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage());
    }
    return result;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) Merger(org.eclipse.jgit.merge.Merger) ResolveMerger(org.eclipse.jgit.merge.ResolveMerger) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) MergeableInfo(com.google.gerrit.extensions.common.MergeableInfo) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) InMemoryInserter(com.google.gerrit.server.git.InMemoryInserter) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) ResolveMerger(org.eclipse.jgit.merge.ResolveMerger) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 19 with RevCommit

use of org.eclipse.jgit.revwalk.RevCommit in project gerrit by GerritCodeReview.

the class MergeSorter method sort.

Collection<CodeReviewCommit> sort(final Collection<CodeReviewCommit> toMerge) throws IOException {
    final Set<CodeReviewCommit> heads = new HashSet<>();
    final Set<CodeReviewCommit> sort = new HashSet<>(toMerge);
    while (!sort.isEmpty()) {
        final CodeReviewCommit n = removeOne(sort);
        rw.resetRetain(canMergeFlag);
        rw.markStart(n);
        for (RevCommit c : accepted) {
            rw.markUninteresting(c);
        }
        CodeReviewCommit c;
        RevCommitList<RevCommit> contents = new RevCommitList<>();
        while ((c = rw.next()) != null) {
            if (!c.has(canMergeFlag) || !incoming.contains(c)) {
                // We cannot merge n as it would bring something we
                // aren't permitted to merge at this time. Drop n.
                //
                n.setStatusCode(CommitMergeStatus.MISSING_DEPENDENCY);
                break;
            }
            contents.add(c);
        }
        if (n.getStatusCode() == CommitMergeStatus.MISSING_DEPENDENCY) {
            continue;
        }
        // Anything reachable through us is better merged by just
        // merging us directly. So prune our ancestors out and let
        // us merge instead.
        //
        sort.removeAll(contents);
        heads.removeAll(contents);
        heads.add(n);
    }
    return heads;
}
Also used : RevCommitList(org.eclipse.jgit.revwalk.RevCommitList) HashSet(java.util.HashSet) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 20 with RevCommit

use of org.eclipse.jgit.revwalk.RevCommit in project gerrit by GerritCodeReview.

the class ChangeRebuilderImpl method getHashtagsEvents.

private List<HashtagsEvent> getHashtagsEvents(Change change, NoteDbUpdateManager manager) throws IOException {
    String refName = changeMetaRef(change.getId());
    Optional<ObjectId> old = manager.getChangeRepo().getObjectId(refName);
    if (!old.isPresent()) {
        return Collections.emptyList();
    }
    RevWalk rw = manager.getChangeRepo().rw;
    List<HashtagsEvent> events = new ArrayList<>();
    rw.reset();
    rw.markStart(rw.parseCommit(old.get()));
    for (RevCommit commit : rw) {
        Account.Id authorId;
        try {
            authorId = changeNoteUtil.parseIdent(commit.getAuthorIdent(), change.getId());
        } catch (ConfigInvalidException e) {
            // Corrupt data, no valid hashtags in this commit.
            continue;
        }
        PatchSet.Id psId = parsePatchSetId(change, commit);
        Set<String> hashtags = parseHashtags(commit);
        if (authorId == null || psId == null || hashtags == null) {
            continue;
        }
        Timestamp commitTime = new Timestamp(commit.getCommitterIdent().getWhen().getTime());
        events.add(new HashtagsEvent(psId, authorId, commitTime, hashtags, change.getCreatedOn()));
    }
    return events;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Timestamp(java.sql.Timestamp) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

RevCommit (org.eclipse.jgit.revwalk.RevCommit)1300 Test (org.junit.Test)650 RevWalk (org.eclipse.jgit.revwalk.RevWalk)332 ObjectId (org.eclipse.jgit.lib.ObjectId)292 Repository (org.eclipse.jgit.lib.Repository)272 IOException (java.io.IOException)221 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)190 Ref (org.eclipse.jgit.lib.Ref)174 ArrayList (java.util.ArrayList)134 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)133 File (java.io.File)133 Git (org.eclipse.jgit.api.Git)133 PersonIdent (org.eclipse.jgit.lib.PersonIdent)105 Change (com.google.gerrit.entities.Change)87 TestRepository (org.eclipse.jgit.junit.TestRepository)72 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)69 ObjectReader (org.eclipse.jgit.lib.ObjectReader)64 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)61 List (java.util.List)61 HashMap (java.util.HashMap)57