<commit_before>from datetime import datetime, timezone
from flask import current_app

from zeus import auth
from zeus.config import celery, db
from zeus.models import Repository, RepositoryStatus


@celery.task(max_retries=None)
def import_repo(repo_id, parent=None):
    auth.set_current_tenant(auth.Tenant(repository_ids=[repo_id]))

    repo = Repository.query.get(repo_id)
    if not repo:
        current_app.logger.error('Repository %s not found', repo_id)
        return

    vcs = repo.get_vcs()
    if vcs is None:
        current_app.logger.warning('Repository %s has no VCS backend set', repo.id)
        return

    if repo.status == RepositoryStatus.inactive:
        current_app.logger.info('Repository %s is inactive', repo.id)
        return

    Repository.query.filter(
        Repository.id == repo.id,
    ).update({
        'last_update_attempt': datetime.now(timezone.utc),
    })
    db.session.commit()

    if vcs.exists():
        vcs.update()
    else:
        vcs.clone()

    for commit in vcs.log(parent=parent):
        revision, created = commit.save(repo)
        db.session.commit()
        parent = commit.id

    Repository.query.filter(
        Repository.id == repo.id,
    ).update({
        'last_update': datetime.now(timezone.utc),
    })
    db.session.commit()

    if parent:
        import_repo.delay(
            repo_id=repo.id,
            parent=parent,
        )
<commit_msg>fix: Ensure parents dont recurse on imports<commit_after>from datetime import datetime, timezone
from flask import current_app

from zeus import auth
from zeus.config import celery, db
from zeus.models import Repository, RepositoryStatus


@celery.task(max_retries=None)
def import_repo(repo_id, parent=None):
    auth.set_current_tenant(auth.Tenant(repository_ids=[repo_id]))

    repo = Repository.query.get(repo_id)
    if not repo:
        current_app.logger.error('Repository %s not found', repo_id)
        return

    vcs = repo.get_vcs()
    if vcs is None:
        current_app.logger.warning('Repository %s has no VCS backend set', repo.id)
        return

    if repo.status == RepositoryStatus.inactive:
        current_app.logger.info('Repository %s is inactive', repo.id)
        return

    Repository.query.filter(
        Repository.id == repo.id,
    ).update({
        'last_update_attempt': datetime.now(timezone.utc),
    })
    db.session.commit()

    if vcs.exists():
        vcs.update()
    else:
        vcs.clone()

    for commit in vcs.log(parent=parent):
        revision, created = commit.save(repo)
        db.session.commit()
        if parent == commit.id:
            break
        parent = commit.id

    Repository.query.filter(
        Repository.id == repo.id,
    ).update({
        'last_update': datetime.now(timezone.utc),
    })
    db.session.commit()

    if parent:
        import_repo.delay(
            repo_id=repo.id,
            parent=parent,
        )

{'fraction_non_alphanumeric': 0.10879945429740791, 'fraction_numerical': 0.0, 'mean_word_length': 2.555151515151515, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7526262', 'n_tokens_mistral': 927, 'n_tokens_neox': 897, 'n_words': 204}