Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to limit maximum commits used for frecency #756

Merged
merged 3 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ This is an example of a configuration file:

server:
port: 31134 # A port number to run the server on
# Increase number of commits used for computing frecency score
# Default is `1000`, set to `null` to read all history
readMaxCommits: 5000

# globs to ignore in addition to .gitignore
ignorePatterns:
Expand Down
9 changes: 9 additions & 0 deletions seagoat/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def analyze_files(self):
"--name-only",
"--pretty=format:###%h:::%ai:::%an <%ae>:::%s",
"--no-merges",
*self._git_log_extra_options(),
]

self.file_changes.clear()
Expand Down Expand Up @@ -119,6 +120,14 @@ def analyze_files(self):

self._compute_frecency()

def _git_log_extra_options(self):
cmd = []

if (max_commits := self.config["server"]["readMaxCommits"]) is not None:
cmd.append(f"--max-count={max_commits}")

return cmd

def _compute_frecency(self):
self.frecency_scores = {}
for file, commits in self.file_changes.items():
Expand Down
6 changes: 6 additions & 0 deletions seagoat/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"server": {
"port": None,
"ignorePatterns": [],
"readMaxCommits": 5_000,
"chroma": {
"embeddingFunction": {
"name": "DefaultEmbeddingFunction",
Expand All @@ -35,6 +36,11 @@
"additionalProperties": False,
"properties": {
"port": {"type": "integer", "minimum": 1, "maximum": 65535},
"readMaxCommits": {
"type": ["integer", "null"],
"minimum": 1,
"maximum": 65535,
},
"ignorePatterns": {
"type": "array",
"items": {"type": "string"},
Expand Down
28 changes: 27 additions & 1 deletion tests/test_repository.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from pathlib import Path
from typing import Any

from freezegun import freeze_time

from pytest_mock import MockerFixture
from seagoat.engine import Engine
from tests.conftest import MockRepo
from tests.test_server import pytest
Expand Down Expand Up @@ -312,3 +313,28 @@ async def test_allows_limiting_how_many_files_are_automatically_analized(
seagoat.analyze_codebase(minimum_chunks_to_analyze=chunks_to_analyze)

assert len(seagoat.cache.data["chunks_already_analyzed"]) == chunks_to_analyze


@pytest.mark.parametrize(
"config,expected_extra_args",
[
pytest.param({}, ["--max-count=5000"], id="default"),
pytest.param(
{"readMaxCommits": 100}, ["--max-count=100"], id="specific-number"
),
pytest.param({"readMaxCommits": None}, [], id="unlimited"),
],
)
def test_max_count_is_added_for_read_max_commits_setting(
repo: MockRepo,
mocker: MockerFixture,
config: dict[str, Any],
expected_extra_args: list[str],
):
repository = Engine(repo.working_dir).repository
repository.config["server"].update(config)
mock = mocker.spy(repository, "_git_log_extra_options")

repository.analyze_files()

assert mock.spy_return == expected_extra_args