Search in sources :

Example 1 with REPO_SYNC_DB_COMMIT_MESSAGE_NO_PROCESSING

use of org.craftercms.studio.api.v2.utils.StudioConfiguration.REPO_SYNC_DB_COMMIT_MESSAGE_NO_PROCESSING 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)

Example 2 with REPO_SYNC_DB_COMMIT_MESSAGE_NO_PROCESSING

use of org.craftercms.studio.api.v2.utils.StudioConfiguration.REPO_SYNC_DB_COMMIT_MESSAGE_NO_PROCESSING in project studio by craftercms.

the class GitContentRepository method getOperations.

@Override
public List<RepoOperation> getOperations(String site, String commitIdFrom, String commitIdTo) {
    List<RepoOperation> operations = new ArrayList<>();
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Repository repository = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
        if (repository != null) {
            try {
                // Get the sandbox repo, and then get a reference to the commitId we received and another for head
                boolean fromEmptyRepo = StringUtils.isEmpty(commitIdFrom);
                String firstCommitId = getRepoFirstCommitId(site);
                if (fromEmptyRepo) {
                    commitIdFrom = firstCommitId;
                }
                Repository repo = helper.getRepository(site, SANDBOX);
                ObjectId objCommitIdFrom = repo.resolve(commitIdFrom);
                ObjectId objCommitIdTo = repo.resolve(commitIdTo);
                ObjectId objFirstCommitId = repo.resolve(firstCommitId);
                try (Git git = new Git(repo)) {
                    if (fromEmptyRepo) {
                        try (RevWalk walk = new RevWalk(repo)) {
                            RevCommit firstCommit = walk.parseCommit(objFirstCommitId);
                            try (ObjectReader reader = repo.newObjectReader()) {
                                CanonicalTreeParser firstCommitTreeParser = new CanonicalTreeParser();
                                // reset(reader, firstCommitTree.getId());
                                firstCommitTreeParser.reset();
                                // Diff the two commit Ids
                                long startDiffMark1 = logger.isDebugEnabled() ? System.currentTimeMillis() : 0;
                                List<DiffEntry> diffEntries = git.diff().setOldTree(firstCommitTreeParser).setNewTree(null).call();
                                if (logger.isDebugEnabled()) {
                                    logger.debug("Diff from " + objFirstCommitId.getName() + " to null " + " finished in " + ((System.currentTimeMillis() - startDiffMark1) / 1000) + " seconds");
                                    logger.debug("Number of diff entries " + diffEntries.size());
                                }
                                // Now that we have a diff, let's itemize the file changes, pack them into a TO
                                // and add them to the list of RepoOperations to return to the caller
                                // also include date/time of commit by taking number of seconds and multiply by 1000 and
                                // convert to java date before sending over
                                operations.addAll(processDiffEntry(git, diffEntries, firstCommit.getId()));
                            }
                        }
                    }
                    // let's do it
                    if (!objCommitIdFrom.equals(objCommitIdTo)) {
                        // Compare HEAD with commitId we're given
                        // Get list of commits between commitId and HEAD in chronological order
                        // Get the log of all the commits between commitId and head
                        Iterable<RevCommit> commits = git.log().addRange(objCommitIdFrom, objCommitIdTo).call();
                        // Loop through through the commits and diff one from the next util head
                        ObjectId prevCommitId = objCommitIdFrom;
                        ObjectId nextCommitId = objCommitIdFrom;
                        String author = EMPTY;
                        // Reverse orders of commits
                        // TODO: DB: try to find better algorithm
                        Iterator<RevCommit> iterator = commits.iterator();
                        List<RevCommit> revCommits = new ArrayList<RevCommit>();
                        while (iterator.hasNext()) {
                            RevCommit commit = iterator.next();
                            revCommits.add(commit);
                        }
                        ReverseListIterator<RevCommit> reverseIterator = new ReverseListIterator<RevCommit>(revCommits);
                        while (reverseIterator.hasNext()) {
                            RevCommit commit = reverseIterator.next();
                            if (StringUtils.contains(commit.getFullMessage(), studioConfiguration.getProperty(REPO_SYNC_DB_COMMIT_MESSAGE_NO_PROCESSING))) {
                                prevCommitId = commit.getId();
                                logger.debug("Skipping commitId: " + prevCommitId.getName() + " for site " + site + " because it is marked not to be processed.");
                                GitLog gitLog = getGitLog(site, prevCommitId.getName());
                                if (gitLog != null) {
                                    markGitLogVerifiedProcessed(site, prevCommitId.getName());
                                } else {
                                    insertGitLog(site, prevCommitId.getName(), 1);
                                }
                                updateLastVerifiedGitlogCommitId(site, prevCommitId.getName());
                            } else {
                                nextCommitId = commit.getId();
                                if (commit.getAuthorIdent() != null) {
                                    author = commit.getAuthorIdent().getName();
                                }
                                if (StringUtils.isEmpty(author)) {
                                    author = commit.getCommitterIdent().getName();
                                }
                                RevTree prevTree = helper.getTreeForCommit(repo, prevCommitId.getName());
                                RevTree nextTree = helper.getTreeForCommit(repo, nextCommitId.getName());
                                if (prevTree != null && nextTree != null) {
                                    try (ObjectReader reader = repo.newObjectReader()) {
                                        CanonicalTreeParser prevCommitTreeParser = new CanonicalTreeParser();
                                        CanonicalTreeParser nextCommitTreeParser = new CanonicalTreeParser();
                                        prevCommitTreeParser.reset(reader, prevTree.getId());
                                        nextCommitTreeParser.reset(reader, nextTree.getId());
                                        // Diff the two commit Ids
                                        long startDiffMark2 = logger.isDebugEnabled() ? System.currentTimeMillis() : 0;
                                        List<DiffEntry> diffEntries = git.diff().setOldTree(prevCommitTreeParser).setNewTree(nextCommitTreeParser).call();
                                        if (logger.isDebugEnabled()) {
                                            logger.debug("Diff from " + objCommitIdFrom.getName() + " to " + objCommitIdTo.getName() + " finished in " + ((System.currentTimeMillis() - startDiffMark2) / 1000) + " seconds");
                                            logger.debug("Number of diff entries " + diffEntries.size());
                                        }
                                        // Now that we have a diff, let's itemize the file changes, pack them into a TO
                                        // and add them to the list of RepoOperations to return to the caller
                                        // also include date/time of commit by taking number of seconds and multiply by 1000 and
                                        // convert to java date before sending over
                                        operations.addAll(processDiffEntry(git, diffEntries, nextCommitId));
                                        prevCommitId = nextCommitId;
                                    }
                                }
                            }
                        }
                    }
                } catch (GitAPIException e) {
                    logger.error("Error getting operations for site " + site + " from commit ID: " + commitIdFrom + " to commit ID: " + commitIdTo, e);
                }
            } catch (IOException e) {
                logger.error("Error getting operations for site " + site + " from commit ID: " + commitIdFrom + " to commit ID: " + commitIdTo, e);
            }
        }
    } catch (CryptoException e) {
        logger.error("Error getting operations for site " + site + " from commit ID: " + commitIdFrom + " to commit ID: " + commitIdTo, e);
    }
    return operations;
}
Also used : ArrayList(java.util.ArrayList) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitLog(org.craftercms.studio.api.v2.dal.GitLog) ObjectReader(org.eclipse.jgit.lib.ObjectReader) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ReverseListIterator(org.apache.commons.collections4.iterators.ReverseListIterator) 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) RepoOperation(org.craftercms.studio.api.v2.dal.RepoOperation) CryptoException(org.craftercms.commons.crypto.CryptoException) RevTree(org.eclipse.jgit.revwalk.RevTree)

Aggregations

IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CryptoException (org.craftercms.commons.crypto.CryptoException)2 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)2 ContentRepository (org.craftercms.studio.api.v2.repository.ContentRepository)2 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)2 Git (org.eclipse.jgit.api.Git)2 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 Repository (org.eclipse.jgit.lib.Repository)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 ReverseListIterator (org.apache.commons.collections4.iterators.ReverseListIterator)1 GitLog (org.craftercms.studio.api.v2.dal.GitLog)1 RepoOperation (org.craftercms.studio.api.v2.dal.RepoOperation)1 RingBuffer (org.craftercms.studio.impl.v2.utils.RingBuffer)1 DiffEntry (org.eclipse.jgit.diff.DiffEntry)1 ObjectReader (org.eclipse.jgit.lib.ObjectReader)1 RevTree (org.eclipse.jgit.revwalk.RevTree)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)1