Skip to content

Commit

Permalink
Skips indexing of commits already indexed
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-dimov committed Apr 14, 2024
1 parent a660064 commit f3f6747
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
29 changes: 17 additions & 12 deletions icds/data.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
from loguru import logger
from sqlmodel import Session, select
from sqlmodel import select

from icds.models import engine, DbRepo, RepoCommit
from icds.models import DbRepo, RepoCommit


def get_repo_by_name(name: str):
with Session(engine) as session:
statement = select(DbRepo).where(DbRepo.name == name)
repo = session.exec(statement).first()
return repo
def get_repo_by_name(db, name: str) -> DbRepo | None:
statement = select(DbRepo).where(DbRepo.name == name)
repo = db.exec(statement).first()
return repo


def get_or_create_db_repo(db, repo, repo_path):
def get_or_create_db_repo(db, repo, repo_path) -> DbRepo:
remote_url = repo.remotes.origin.url
repo_name = remote_url.split("/")[-1].split(".")[0]
db_repo = get_repo_by_name(repo_name)
db_repo = get_repo_by_name(db, repo_name)
if not db_repo:
logger.info(f"Creating a new repository record for repo '{repo_name}'")
db_repo = DbRepo(
Expand All @@ -27,14 +26,16 @@ def get_or_create_db_repo(db, repo, repo_path):
return db_repo


def get_repo_name_by_id(db, repo_id):
def get_repo_name_by_id(db, repo_id) -> str:
return db.exec(select(DbRepo).where(DbRepo.id == repo_id)).first().name


def search_commits(db, query, repo_name, file, author, start_date, end_date):
def search_commits(
db, query, repo_name, file, author, start_date, end_date
) -> list[RepoCommit]:
statement = select(RepoCommit)
if repo_name:
db_repo = get_repo_by_name(repo_name)
db_repo = get_repo_by_name(db, repo_name)
if not db_repo:
raise ValueError(f"Repository '{repo_name}' not found.")
statement = statement.where(RepoCommit.repo_id == db_repo.id)
Expand All @@ -51,3 +52,7 @@ def search_commits(db, query, repo_name, file, author, start_date, end_date):
)
commits = db.exec(statement).all()
return commits


def get_db_commit_by_hash(db, commit_hash) -> RepoCommit | None:
return db.exec(select(RepoCommit).where(RepoCommit.hash == commit_hash)).first()
10 changes: 9 additions & 1 deletion icodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
RepoCommit,
Session,
)
from icds.data import get_or_create_db_repo, get_repo_name_by_id, search_commits
from icds.data import (
get_or_create_db_repo,
get_repo_name_by_id,
search_commits,
get_db_commit_by_hash,
)
from icds.settings import settings

app = Typer()
Expand Down Expand Up @@ -61,6 +66,9 @@ def build_index(repo_path: Path, branch_name: str = "", n_commits: int = 10):
with Session(engine) as db:
db_repo = get_or_create_db_repo(db, repo, repo_path)
for commit in repo.iter_commits(branch_name, max_count=n_commits, reverse=True):
if get_db_commit_by_hash(db, commit.hexsha):
echo(f"Commit {commit.hexsha} already indexed. Skipping ...")
continue
commit_info = extract_commit_info(commit)
echo(
f"Indexing commit {commit.hexsha} by {commit.author} from {commit.authored_datetime} ... \n"
Expand Down

0 comments on commit f3f6747

Please sign in to comment.