Search in sources :

Example 71 with ObjectInserter

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

the class ReceiveCommits method autoCloseChanges.

private void autoCloseChanges(final ReceiveCommand cmd) {
    logDebug("Starting auto-closing of changes");
    String refName = cmd.getRefName();
    checkState(!MagicBranch.isMagicBranch(refName), "shouldn't be auto-closing changes on magic branch %s", refName);
    // insertChangesAndPatchSets.
    try (BatchUpdate bu = batchUpdateFactory.create(db, projectControl.getProject().getNameKey(), user, TimeUtil.nowTs());
        ObjectInserter ins = repo.newObjectInserter();
        ObjectReader reader = ins.newReader();
        RevWalk rw = new RevWalk(reader)) {
        bu.setRepository(repo, rw, ins).updateChangesInParallel();
        bu.setRequestId(receiveId);
        // TODO(dborowitz): Teach BatchUpdate to ignore missing changes.
        RevCommit newTip = rw.parseCommit(cmd.getNewId());
        Branch.NameKey branch = new Branch.NameKey(project.getNameKey(), refName);
        rw.reset();
        rw.markStart(newTip);
        if (!ObjectId.zeroId().equals(cmd.getOldId())) {
            rw.markUninteresting(rw.parseCommit(cmd.getOldId()));
        }
        ListMultimap<ObjectId, Ref> byCommit = changeRefsById();
        Map<Change.Key, ChangeNotes> byKey = null;
        List<ReplaceRequest> replaceAndClose = new ArrayList<>();
        int existingPatchSets = 0;
        int newPatchSets = 0;
        COMMIT: for (RevCommit c; (c = rw.next()) != null; ) {
            rw.parseBody(c);
            for (Ref ref : byCommit.get(c.copy())) {
                existingPatchSets++;
                PatchSet.Id psId = PatchSet.Id.fromRef(ref.getName());
                bu.addOp(psId.getParentKey(), mergedByPushOpFactory.create(requestScopePropagator, psId, refName));
                continue COMMIT;
            }
            for (String changeId : c.getFooterLines(CHANGE_ID)) {
                if (byKey == null) {
                    byKey = openChangesByBranch(branch);
                }
                ChangeNotes onto = byKey.get(new Change.Key(changeId.trim()));
                if (onto != null) {
                    newPatchSets++;
                    // Hold onto this until we're done with the walk, as the call to
                    // req.validate below calls isMergedInto which resets the walk.
                    ReplaceRequest req = new ReplaceRequest(onto.getChangeId(), c, cmd, false);
                    req.notes = onto;
                    replaceAndClose.add(req);
                    continue COMMIT;
                }
            }
        }
        for (final ReplaceRequest req : replaceAndClose) {
            Change.Id id = req.notes.getChangeId();
            if (!req.validate(true)) {
                logDebug("Not closing {} because validation failed", id);
                continue;
            }
            req.addOps(bu, null);
            bu.addOp(id, mergedByPushOpFactory.create(requestScopePropagator, req.psId, refName).setPatchSetProvider(new Provider<PatchSet>() {

                @Override
                public PatchSet get() {
                    return req.replaceOp.getPatchSet();
                }
            }));
            bu.addOp(id, new ChangeProgressOp(closeProgress));
        }
        logDebug("Auto-closing {} changes with existing patch sets and {} with new patch sets", existingPatchSets, newPatchSets);
        bu.execute();
    } catch (RestApiException e) {
        logError("Can't insert patchset", e);
    } catch (IOException | OrmException | UpdateException | PermissionBackendException e) {
        logError("Can't scan for changes to close", e);
    }
}
Also used : ArrayList(java.util.ArrayList) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) OrmException(com.google.gwtorm.server.OrmException) MagicBranch(com.google.gerrit.server.util.MagicBranch) Branch(com.google.gerrit.reviewdb.client.Branch) ObjectReader(org.eclipse.jgit.lib.ObjectReader) UpdateException(com.google.gerrit.server.update.UpdateException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ObjectId(org.eclipse.jgit.lib.ObjectId) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) Change(com.google.gerrit.reviewdb.client.Change) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Provider(com.google.inject.Provider) Ref(org.eclipse.jgit.lib.Ref) RequestId(com.google.gerrit.server.util.RequestId) ObjectId(org.eclipse.jgit.lib.ObjectId) RevId(com.google.gerrit.reviewdb.client.RevId) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Example 72 with ObjectInserter

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

the class MergeUtil method newMerger.

public static Merger newMerger(ObjectInserter inserter, Config repoConfig, String strategyName) {
    MergeStrategy strategy = MergeStrategy.get(strategyName);
    checkArgument(strategy != null, "invalid merge strategy: %s", strategyName);
    return strategy.newMerger(new ObjectInserter.Filter() {

        @Override
        protected ObjectInserter delegate() {
            return inserter;
        }

        @Override
        public void flush() {
        }

        @Override
        public void close() {
        }
    }, repoConfig);
}
Also used : ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ThreeWayMergeStrategy(org.eclipse.jgit.merge.ThreeWayMergeStrategy) MergeStrategy(org.eclipse.jgit.merge.MergeStrategy)

Example 73 with ObjectInserter

use of org.eclipse.jgit.lib.ObjectInserter in project blueocean-plugin by jenkinsci.

the class GitUtils method commit.

// TODO - remove once https://github.com/spotbugs/spotbugs/issues/756 is resolved
@SuppressFBWarnings(value = { "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE" }, justification = "JDK11 produces different bytecode - https://github.com/spotbugs/spotbugs/issues/756")
public static void commit(final Repository repo, final String refName, final String path, final byte[] contents, final String name, final String email, final String message, final TimeZone timeZone, final Date when) {
    final PersonIdent author = buildPersonIdent(repo, name, email, timeZone, when);
    try (final ObjectInserter odi = repo.newObjectInserter()) {
        // Create the in-memory index of the new/updated issue.
        final ObjectId headId = repo.resolve(refName + "^{commit}");
        final DirCache index = createTemporaryIndex(repo, headId, path, contents);
        final ObjectId indexTreeId = index.writeTree(odi);
        // Create a commit object
        final CommitBuilder commit = new CommitBuilder();
        commit.setAuthor(author);
        commit.setCommitter(author);
        commit.setEncoding(Constants.CHARACTER_ENCODING);
        commit.setMessage(message);
        // headId can be null if the repository has no commit yet
        if (headId != null) {
            commit.setParentId(headId);
        }
        commit.setTreeId(indexTreeId);
        // Insert the commit into the repository
        final ObjectId commitId = odi.insert(commit);
        odi.flush();
        try (RevWalk revWalk = new RevWalk(repo)) {
            final RevCommit revCommit = revWalk.parseCommit(commitId);
            final RefUpdate ru = repo.updateRef(refName);
            if (headId == null) {
                ru.setExpectedOldObjectId(ObjectId.zeroId());
            } else {
                ru.setExpectedOldObjectId(headId);
            }
            ru.setNewObjectId(commitId);
            ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
            final RefUpdate.Result rc = ru.forceUpdate();
            switch(rc) {
                case NEW:
                case FORCED:
                case FAST_FORWARD:
                    break;
                case REJECTED:
                case LOCK_FAILURE:
                    throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
                default:
                    throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed, Constants.HEAD, commitId.toString(), rc));
            }
        }
    } catch (ConcurrentRefUpdateException | IOException | JGitInternalException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) DirCache(org.eclipse.jgit.dircache.DirCache) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) PersonIdent(org.eclipse.jgit.lib.PersonIdent) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) RefUpdate(org.eclipse.jgit.lib.RefUpdate) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 74 with ObjectInserter

use of org.eclipse.jgit.lib.ObjectInserter in project egit by eclipse.

the class IndexFileRevisionTest method entry.

private DirCacheEntry entry(String path, int stage, String data) throws IOException {
    DirCacheEntry entry = new DirCacheEntry(path, stage);
    entry.setFileMode(FileMode.REGULAR_FILE);
    try (ObjectInserter inserter = repository.newObjectInserter()) {
        ObjectId blob = inserter.insert(Constants.OBJ_BLOB, data.getBytes("UTF-8"));
        entry.setObjectId(blob);
        inserter.flush();
    }
    return entry;
}
Also used : DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId)

Example 75 with ObjectInserter

use of org.eclipse.jgit.lib.ObjectInserter in project egit by eclipse.

the class TagOperation method updateTagObject.

private ObjectId updateTagObject() throws TeamException {
    ObjectId startPointRef = tag.getObjectId();
    try {
        ObjectId tagId;
        repo.open(startPointRef);
        try (ObjectInserter inserter = repo.newObjectInserter()) {
            tagId = inserter.insert(tag);
            inserter.flush();
        }
        return tagId;
    } catch (IOException e) {
        throw new TeamException(NLS.bind(CoreText.TagOperation_objectIdNotFound, tag.getTag(), e.getMessage()), e);
    }
}
Also used : TeamException(org.eclipse.team.core.TeamException) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException)

Aggregations

ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)108 ObjectId (org.eclipse.jgit.lib.ObjectId)77 RevWalk (org.eclipse.jgit.revwalk.RevWalk)55 Repository (org.eclipse.jgit.lib.Repository)49 RevCommit (org.eclipse.jgit.revwalk.RevCommit)35 IOException (java.io.IOException)31 ObjectReader (org.eclipse.jgit.lib.ObjectReader)31 PersonIdent (org.eclipse.jgit.lib.PersonIdent)30 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)28 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)24 RefUpdate (org.eclipse.jgit.lib.RefUpdate)24 DirCache (org.eclipse.jgit.dircache.DirCache)15 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)14 Change (com.google.gerrit.entities.Change)13 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)13 Change (com.google.gerrit.reviewdb.client.Change)13 DirCacheEntry (org.eclipse.jgit.dircache.DirCacheEntry)13 Ref (org.eclipse.jgit.lib.Ref)13 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)12 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)10