Search in sources :

Example 1 with RingBuffer

use of org.craftercms.studio.impl.v2.utils.RingBuffer in project studio by craftercms.

the class GitContentRepository method updateGitlog.

@Override
public void updateGitlog(String siteId, String lastProcessedCommitId, int batchSize) throws SiteNotFoundException {
    RingBuffer<RevCommit> commitIds = new RingBuffer<RevCommit>(batchSize);
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Repository repository = helper.getRepository(siteId, StringUtils.isEmpty(siteId) ? GLOBAL : SANDBOX);
        if (repository != null) {
            try {
                ObjectId objCommitIdFrom = repository.resolve(lastProcessedCommitId);
                ObjectId objCommitIdTo = repository.resolve(HEAD);
                logger.debug("Update git log from " + objCommitIdFrom.getName() + " to " + objCommitIdTo.getName());
                try (Git git = new Git(repository)) {
                    // let's do it
                    if (!objCommitIdFrom.equals(objCommitIdTo)) {
                        // Get the log of all the commits between commitId and head
                        Iterable<RevCommit> commits = git.log().call();
                        ObjectId nextCommitId;
                        String commitId = EMPTY;
                        Iterator<RevCommit> iterator = commits.iterator();
                        while (iterator.hasNext()) {
                            RevCommit commit = iterator.next();
                            if (StringUtils.equals(commit.getId().getName(), lastProcessedCommitId)) {
                                break;
                            }
                            commitIds.write(commit);
                        }
                        List<String> batch = new ArrayList<String>();
                        RevCommit current = commitIds.read();
                        while (current != null) {
                            nextCommitId = current.getId();
                            commitId = nextCommitId.getName();
                            if (StringUtils.contains(current.getFullMessage(), studioConfiguration.getProperty(REPO_SYNC_DB_COMMIT_MESSAGE_NO_PROCESSING))) {
                                logger.debug("Skipping commitId: " + commitId + " for site " + siteId + " because it is marked not to be processed.");
                            } else {
                                batch.add(0, commitId);
                            }
                            current = commitIds.read();
                        }
                        if (batch.size() > 0) {
                            gitLogDao.insertIgnoreGitLogList(siteId, batch);
                            siteService.updateLastSyncedGitlogCommitId(siteId, batch.get(batch.size() - 1));
                            logger.debug("Inserted " + batch.size() + " git log commits for site " + siteId);
                        } else {
                            siteService.updateLastSyncedGitlogCommitId(siteId, objCommitIdTo.getName());
                        }
                    }
                } catch (GitAPIException e) {
                    logger.error("Error getting commit ids for site " + siteId + " from commit ID: " + lastProcessedCommitId + " to HEAD", e);
                }
            } catch (IOException e) {
                logger.error("Error getting commit ids for site " + siteId + " from commit ID: " + lastProcessedCommitId + " to HEAD", e);
            }
        }
    } catch (CryptoException e) {
        logger.error("Error getting commit ids for site " + siteId + " from commit ID: " + lastProcessedCommitId + " to HEAD", e);
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) RingBuffer(org.craftercms.studio.impl.v2.utils.RingBuffer) IOException(java.io.IOException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v2.repository.ContentRepository) Git(org.eclipse.jgit.api.Git) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CryptoException (org.craftercms.commons.crypto.CryptoException)1 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)1 ContentRepository (org.craftercms.studio.api.v2.repository.ContentRepository)1 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)1 RingBuffer (org.craftercms.studio.impl.v2.utils.RingBuffer)1 Git (org.eclipse.jgit.api.Git)1 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 Repository (org.eclipse.jgit.lib.Repository)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1