Search in sources :

Example 1 with DeletePath

use of org.eclipse.jgit.dircache.DirCacheEditor.DeletePath in project gerrit by GerritCodeReview.

the class SubmoduleOp method updateSubmodule.

private RevCommit updateSubmodule(DirCache dc, DirCacheEditor ed, StringBuilder msgbuf, final SubmoduleSubscription s) throws SubmoduleException, IOException {
    OpenRepo subOr;
    try {
        subOr = orm.getRepo(s.getSubmodule().getParentKey());
    } catch (NoSuchProjectException | IOException e) {
        throw new SubmoduleException("Cannot access submodule", e);
    }
    DirCacheEntry dce = dc.getEntry(s.getPath());
    RevCommit oldCommit = null;
    if (dce != null) {
        if (!dce.getFileMode().equals(FileMode.GITLINK)) {
            String errMsg = "Requested to update gitlink " + s.getPath() + " in " + s.getSubmodule().getParentKey().get() + " but entry " + "doesn't have gitlink file mode.";
            throw new SubmoduleException(errMsg);
        }
        oldCommit = subOr.rw.parseCommit(dce.getObjectId());
    }
    final CodeReviewCommit newCommit;
    if (branchTips.containsKey(s.getSubmodule())) {
        newCommit = branchTips.get(s.getSubmodule());
    } else {
        Ref ref = subOr.repo.getRefDatabase().exactRef(s.getSubmodule().get());
        if (ref == null) {
            ed.add(new DeletePath(s.getPath()));
            return null;
        }
        newCommit = subOr.rw.parseCommit(ref.getObjectId());
        addBranchTip(s.getSubmodule(), newCommit);
    }
    if (Objects.equals(newCommit, oldCommit)) {
        // gitlink have already been updated for this submodule
        return null;
    }
    ed.add(new PathEdit(s.getPath()) {

        @Override
        public void apply(DirCacheEntry ent) {
            ent.setFileMode(FileMode.GITLINK);
            ent.setObjectId(newCommit.getId());
        }
    });
    if (verboseSuperProject != VerboseSuperprojectUpdate.FALSE) {
        createSubmoduleCommitMsg(msgbuf, s, subOr, newCommit, oldCommit);
    }
    subOr.rw.parseBody(newCommit);
    return newCommit;
}
Also used : DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) PathEdit(org.eclipse.jgit.dircache.DirCacheEditor.PathEdit) IOException(java.io.IOException) DeletePath(org.eclipse.jgit.dircache.DirCacheEditor.DeletePath) Ref(org.eclipse.jgit.lib.Ref) OpenRepo(com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with DeletePath

use of org.eclipse.jgit.dircache.DirCacheEditor.DeletePath in project gerrit by GerritCodeReview.

the class VersionedMetaData method saveFile.

protected void saveFile(String fileName, byte[] raw) throws IOException {
    DirCacheEditor editor = newTree.editor();
    if (raw != null && 0 < raw.length) {
        final ObjectId blobId = inserter.insert(Constants.OBJ_BLOB, raw);
        editor.add(new PathEdit(fileName) {

            @Override
            public void apply(DirCacheEntry ent) {
                ent.setFileMode(FileMode.REGULAR_FILE);
                ent.setObjectId(blobId);
            }
        });
    } else {
        editor.add(new DeletePath(fileName));
    }
    editor.finish();
}
Also used : DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) PathEdit(org.eclipse.jgit.dircache.DirCacheEditor.PathEdit) DirCacheEditor(org.eclipse.jgit.dircache.DirCacheEditor) DeletePath(org.eclipse.jgit.dircache.DirCacheEditor.DeletePath)

Example 3 with DeletePath

use of org.eclipse.jgit.dircache.DirCacheEditor.DeletePath in project gerrit by GerritCodeReview.

the class SubmoduleCommits method updateSubmodule.

private RevCommit updateSubmodule(DirCache dc, DirCacheEditor ed, StringBuilder msgbuf, SubmoduleSubscription s) throws SubmoduleConflictException, IOException {
    logger.atFine().log("Updating gitlink for %s", s);
    OpenRepo subOr;
    try {
        subOr = orm.getRepo(s.getSubmodule().project());
    } catch (NoSuchProjectException | IOException e) {
        throw new StorageException("Cannot access submodule", e);
    }
    DirCacheEntry dce = dc.getEntry(s.getPath());
    RevCommit oldCommit = null;
    if (dce != null) {
        if (!dce.getFileMode().equals(FileMode.GITLINK)) {
            String errMsg = "Requested to update gitlink " + s.getPath() + " in " + s.getSubmodule().project().get() + " but entry " + "doesn't have gitlink file mode.";
            throw new SubmoduleConflictException(errMsg);
        }
        // making things worse by updating the gitlink to something else.
        try {
            oldCommit = subOr.getCodeReviewRevWalk().parseCommit(dce.getObjectId());
        } catch (IOException e) {
            // Broken gitlink; sanity check failed. Warn and continue so the submit operation can
            // proceed, it will just skip this gitlink update.
            logger.atSevere().withCause(e).log("Failed to read commit %s", dce.getObjectId().name());
            return null;
        }
    }
    Optional<CodeReviewCommit> maybeNewCommit = branchTips.getTip(s.getSubmodule(), subOr);
    if (!maybeNewCommit.isPresent()) {
        // For whatever reason, this submodule was not updated as part of this submit batch, but the
        // superproject is still subscribed to this branch. Re-read the ref to see if anything has
        // changed since the last time the gitlink was updated, and roll that update into the same
        // commit as all other submodule updates.
        ed.add(new DeletePath(s.getPath()));
        return null;
    }
    CodeReviewCommit newCommit = maybeNewCommit.get();
    if (Objects.equals(newCommit, oldCommit)) {
        // gitlink have already been updated for this submodule
        return null;
    }
    ed.add(new PathEdit(s.getPath()) {

        @Override
        public void apply(DirCacheEntry ent) {
            ent.setFileMode(FileMode.GITLINK);
            ent.setObjectId(newCommit.getId());
        }
    });
    if (verboseSuperProject != VerboseSuperprojectUpdate.FALSE) {
        createSubmoduleCommitMsg(msgbuf, s, subOr, newCommit, oldCommit);
    }
    subOr.getCodeReviewRevWalk().parseBody(newCommit);
    return newCommit;
}
Also used : DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) PathEdit(org.eclipse.jgit.dircache.DirCacheEditor.PathEdit) IOException(java.io.IOException) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit) DeletePath(org.eclipse.jgit.dircache.DirCacheEditor.DeletePath) OpenRepo(com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo) StorageException(com.google.gerrit.exceptions.StorageException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 4 with DeletePath

use of org.eclipse.jgit.dircache.DirCacheEditor.DeletePath in project gerrit by GerritCodeReview.

the class VersionedMetaData method saveFile.

protected void saveFile(String fileName, byte[] raw) throws IOException {
    try (TraceTimer timer = TraceContext.newTimer("Save file", Metadata.builder().projectName(projectName.get()).noteDbRefName(getRefName()).noteDbFilePath(fileName).build())) {
        DirCacheEditor editor = newTree.editor();
        if (raw != null && 0 < raw.length) {
            final ObjectId blobId = inserter.insert(Constants.OBJ_BLOB, raw);
            editor.add(new PathEdit(fileName) {

                @Override
                public void apply(DirCacheEntry ent) {
                    ent.setFileMode(FileMode.REGULAR_FILE);
                    ent.setObjectId(blobId);
                }
            });
        } else {
            editor.add(new DeletePath(fileName));
        }
        editor.finish();
    }
}
Also used : DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) PathEdit(org.eclipse.jgit.dircache.DirCacheEditor.PathEdit) DirCacheEditor(org.eclipse.jgit.dircache.DirCacheEditor) DeletePath(org.eclipse.jgit.dircache.DirCacheEditor.DeletePath)

Aggregations

DeletePath (org.eclipse.jgit.dircache.DirCacheEditor.DeletePath)4 PathEdit (org.eclipse.jgit.dircache.DirCacheEditor.PathEdit)4 DirCacheEntry (org.eclipse.jgit.dircache.DirCacheEntry)4 NoSuchProjectException (com.google.gerrit.server.project.NoSuchProjectException)2 IOException (java.io.IOException)2 DirCacheEditor (org.eclipse.jgit.dircache.DirCacheEditor)2 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 StorageException (com.google.gerrit.exceptions.StorageException)1 CodeReviewCommit (com.google.gerrit.server.git.CodeReviewCommit)1 OpenRepo (com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo)1 TraceTimer (com.google.gerrit.server.logging.TraceContext.TraceTimer)1 OpenRepo (com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo)1 Ref (org.eclipse.jgit.lib.Ref)1