Search in sources :

Example 86 with ObjectReader

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

the class DraftCommentNotes method onLoad.

@Override
protected void onLoad(LoadHandle handle) throws IOException, ConfigInvalidException {
    ObjectId rev = handle.id();
    if (rev == null) {
        loadDefaults();
        return;
    }
    logger.atFine().log("Load draft comment notes for change %s of project %s", getChangeId(), getProjectName());
    RevCommit tipCommit = handle.walk().parseCommit(rev);
    ObjectReader reader = handle.walk().getObjectReader();
    revisionNoteMap = RevisionNoteMap.parse(args.changeNoteJson, reader, NoteMap.read(reader, tipCommit), HumanComment.Status.DRAFT);
    ImmutableListMultimap.Builder<ObjectId, HumanComment> cs = ImmutableListMultimap.builder();
    for (ChangeRevisionNote rn : revisionNoteMap.revisionNotes.values()) {
        for (HumanComment c : rn.getEntities()) {
            cs.put(c.getCommitId(), c);
        }
    }
    comments = cs.build();
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ObjectReader(org.eclipse.jgit.lib.ObjectReader) HumanComment(com.google.gerrit.entities.HumanComment) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 87 with ObjectReader

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

the class RobotCommentNotes method onLoad.

@Override
protected void onLoad(LoadHandle handle) throws IOException, ConfigInvalidException {
    metaId = handle.id();
    if (metaId == null) {
        loadDefaults();
        return;
    }
    metaId = metaId.copy();
    logger.atFine().log("Load robot comment notes for change %s of project %s", getChangeId(), getProjectName());
    RevCommit tipCommit = handle.walk().parseCommit(metaId);
    ObjectReader reader = handle.walk().getObjectReader();
    revisionNoteMap = RevisionNoteMap.parseRobotComments(args.changeNoteJson, reader, NoteMap.read(reader, tipCommit));
    ImmutableListMultimap.Builder<ObjectId, RobotComment> cs = ImmutableListMultimap.builder();
    for (RobotCommentsRevisionNote rn : revisionNoteMap.revisionNotes.values()) {
        for (RobotComment c : rn.getEntities()) {
            cs.put(c.getCommitId(), c);
        }
    }
    comments = cs.build();
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) RobotComment(com.google.gerrit.entities.RobotComment) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 88 with ObjectReader

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

the class AutoMerger method createAutoMergeCommit.

/**
 * Creates an auto-merge commit of the parents of the given merge commit.
 *
 * @return auto-merge commit. Headers of the returned RevCommit are parsed.
 */
private ObjectId createAutoMergeCommit(Config repoConfig, RevWalk rw, ObjectInserter ins, RevCommit merge, ThreeWayMergeStrategy mergeStrategy) throws IOException {
    rw.parseHeaders(merge);
    ResolveMerger m = (ResolveMerger) mergeStrategy.newMerger(ins, repoConfig);
    DirCache dc = DirCache.newInCore();
    m.setDirCache(dc);
    // If we don't plan on saving results, use a fully in-memory inserter.
    // Using just a non-flushing wrapper is not sufficient, since in particular DfsInserter might
    // try to write to storage after exceeding an internal buffer size.
    m.setObjectInserter(ins instanceof InMemoryInserter ? new NonFlushingWrapper(ins) : ins);
    boolean couldMerge = m.merge(merge.getParents());
    ObjectId treeId;
    if (couldMerge) {
        treeId = m.getResultTreeId();
    } else {
        treeId = MergeUtil.mergeWithConflicts(rw, ins, dc, "HEAD", merge.getParent(0), "BRANCH", merge.getParent(1), m.getMergeResults());
    }
    rw.parseHeaders(merge);
    // For maximum stability, choose a single ident using the committer time of
    // the input commit, using the server name and timezone.
    PersonIdent ident = new PersonIdent(gerritIdentProvider.get(), merge.getCommitterIdent().getWhen(), gerritIdentProvider.get().getTimeZone());
    CommitBuilder cb = new CommitBuilder();
    cb.setAuthor(ident);
    cb.setCommitter(ident);
    cb.setTreeId(treeId);
    cb.setMessage(AUTO_MERGE_MSG_PREFIX + merge.name() + '\n');
    for (RevCommit p : merge.getParents()) {
        cb.addParentId(p);
    }
    if (ins instanceof InMemoryInserter) {
        // they are not available.
        try (ObjectReader tmpReader = ins.newReader();
            RevWalk tmpRw = new RevWalk(tmpReader)) {
            return tmpRw.parseCommit(ins.insert(cb));
        }
    }
    return rw.parseCommit(ins.insert(cb));
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) ObjectId(org.eclipse.jgit.lib.ObjectId) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) InMemoryInserter(com.google.gerrit.server.git.InMemoryInserter) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ResolveMerger(org.eclipse.jgit.merge.ResolveMerger) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 89 with ObjectReader

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

the class ExternalIdCacheLoader method buildAllExternalIds.

/**
 * Build a new {@link AllExternalIds} from an old state by applying additions and removals that
 * were performed since then.
 *
 * <p>Removals are applied before additions.
 *
 * @param repo open repository
 * @param oldExternalIds prior state that is used as base
 * @param additions map of name to blob ID for each external ID that should be added
 * @param removals set of name {@link ObjectId}s that should be removed
 */
private AllExternalIds buildAllExternalIds(Repository repo, AllExternalIds oldExternalIds, Map<ObjectId, ObjectId> additions, Set<ObjectId> removals) throws IOException {
    ImmutableMap.Builder<ExternalId.Key, ExternalId> byKey = ImmutableMap.builder();
    ImmutableSetMultimap.Builder<Account.Id, ExternalId> byAccount = ImmutableSetMultimap.builder();
    ImmutableSetMultimap.Builder<String, ExternalId> byEmail = ImmutableSetMultimap.builder();
    // Copy over old ExternalIds but exclude deleted ones
    for (ExternalId externalId : oldExternalIds.byAccount().values()) {
        if (removals.contains(externalId.blobId())) {
            continue;
        }
        byKey.put(externalId.key(), externalId);
        byAccount.put(externalId.accountId(), externalId);
        if (externalId.email() != null) {
            byEmail.put(externalId.email(), externalId);
        }
    }
    // Add newly discovered ExternalIds
    try (ObjectReader reader = repo.newObjectReader()) {
        for (Map.Entry<ObjectId, ObjectId> nameToBlob : additions.entrySet()) {
            ExternalId parsedExternalId;
            try {
                parsedExternalId = externalIdFactory.parse(nameToBlob.getKey().name(), reader.open(nameToBlob.getValue()).getCachedBytes(), nameToBlob.getValue());
            } catch (ConfigInvalidException | RuntimeException e) {
                logger.atSevere().withCause(e).log("Ignoring invalid external ID note %s", nameToBlob.getKey().name());
                continue;
            }
            byKey.put(parsedExternalId.key(), parsedExternalId);
            byAccount.put(parsedExternalId.accountId(), parsedExternalId);
            if (parsedExternalId.email() != null) {
                byEmail.put(parsedExternalId.email(), parsedExternalId);
            }
        }
    }
    return new AutoValue_AllExternalIds(byKey.build(), byAccount.build(), byEmail.build());
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) ObjectId(org.eclipse.jgit.lib.ObjectId) ImmutableMap(com.google.common.collect.ImmutableMap) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectId(org.eclipse.jgit.lib.ObjectId) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 90 with ObjectReader

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

the class CommitUtil method createRevertChange.

/**
 * Allows creating a revert change.
 *
 * @param notes ChangeNotes of the change being reverted.
 * @param user Current User performing the revert.
 * @param input the RevertInput entity for conducting the revert.
 * @param timestamp timestamp for the created change.
 * @return ObjectId that represents the newly created commit.
 */
public Change.Id createRevertChange(ChangeNotes notes, CurrentUser user, RevertInput input, Instant timestamp) throws RestApiException, UpdateException, ConfigInvalidException, IOException {
    String message = Strings.emptyToNull(input.message);
    try (Repository git = repoManager.openRepository(notes.getProjectName());
        ObjectInserter oi = git.newObjectInserter();
        ObjectReader reader = oi.newReader();
        RevWalk revWalk = new RevWalk(reader)) {
        ObjectId generatedChangeId = CommitMessageUtil.generateChangeId();
        ObjectId revCommit = createRevertCommit(message, notes, user, timestamp, oi, revWalk, generatedChangeId);
        return createRevertChangeFromCommit(revCommit, input, notes, user, generatedChangeId, timestamp, oi, revWalk, git);
    } catch (RepositoryNotFoundException e) {
        throw new ResourceNotFoundException(notes.getChangeId().toString(), e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Aggregations

ObjectReader (org.eclipse.jgit.lib.ObjectReader)115 RevWalk (org.eclipse.jgit.revwalk.RevWalk)69 ObjectId (org.eclipse.jgit.lib.ObjectId)59 RevCommit (org.eclipse.jgit.revwalk.RevCommit)56 Repository (org.eclipse.jgit.lib.Repository)47 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)31 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)26 IOException (java.io.IOException)24 Test (org.junit.Test)23 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)20 RevTree (org.eclipse.jgit.revwalk.RevTree)19 DiffEntry (org.eclipse.jgit.diff.DiffEntry)18 Ref (org.eclipse.jgit.lib.Ref)17 PersonIdent (org.eclipse.jgit.lib.PersonIdent)14 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)13 TestRepository (org.eclipse.jgit.junit.TestRepository)12 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)11 Change (com.google.gerrit.reviewdb.client.Change)10 Map (java.util.Map)10 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)9