diff --git a/src/ahriman/core/build_tools/sources.py b/src/ahriman/core/build_tools/sources.py index 24f7fc14..93ba7e4e 100644 --- a/src/ahriman/core/build_tools/sources.py +++ b/src/ahriman/core/build_tools/sources.py @@ -416,7 +416,7 @@ class Sources(LazyLogging): else: patch.write(sources_dir / "PKGBUILD") - def read(self, sources_dir: Path, commit_sha: str, path: Path) -> str: + def read(self, sources_dir: Path, commit_sha: str, path: Path) -> str | None: """ read file content from the specified commit @@ -426,6 +426,10 @@ class Sources(LazyLogging): path(Path): path to file inside the repository Returns: - str: file content at specified commit + str | None: file content at specified commit if available """ - return check_output(*self.git(), "show", f"{commit_sha}:{path}", cwd=sources_dir, logger=self.logger) + try: + return check_output(*self.git(), "show", f"{commit_sha}:{path}", cwd=sources_dir, logger=self.logger) + except CalledProcessError: + self.logger.exception("failed to read file %s at %s", path, commit_sha) + return None diff --git a/src/ahriman/core/database/operations/changes_operations.py b/src/ahriman/core/database/operations/changes_operations.py index 46f8c909..8ce16333 100644 --- a/src/ahriman/core/database/operations/changes_operations.py +++ b/src/ahriman/core/database/operations/changes_operations.py @@ -81,7 +81,7 @@ class ChangesOperations(Operations): values (:package_base, :last_commit_sha, :changes, :pkgbuild, :repository) on conflict (package_base, repository) do update set - last_commit_sha = :last_commit_sha, changes = :changes, pkgbuild = :pkgbuild + last_commit_sha = :last_commit_sha, changes = :changes, pkgbuild = coalesce(:pkgbuild, pkgbuild) """, { "package_base": package_base, diff --git a/tests/ahriman/core/build_tools/test_sources.py b/tests/ahriman/core/build_tools/test_sources.py index 51deb6e7..ab48105e 100644 --- a/tests/ahriman/core/build_tools/test_sources.py +++ b/tests/ahriman/core/build_tools/test_sources.py @@ -605,3 +605,12 @@ def test_read(sources: Sources, mocker: MockerFixture) -> None: check_output_mock = mocker.patch("ahriman.core.build_tools.sources.check_output", return_value="content") assert sources.read(Path("local"), "sha", Path("PKGBUILD")) == "content" check_output_mock.assert_called_once() + + +def test_read_failed(sources: Sources, mocker: MockerFixture) -> None: + """ + must return None in case if file cannot be read from commit + """ + mocker.patch("ahriman.core.build_tools.sources.check_output", + side_effect=CalledProcessError(1, ["command"], "error")) + assert sources.read(Path("local"), "sha", Path("PKGBUILD")) is None diff --git a/tests/ahriman/core/database/operations/test_changes_operations.py b/tests/ahriman/core/database/operations/test_changes_operations.py index cf130006..a089a7ab 100644 --- a/tests/ahriman/core/database/operations/test_changes_operations.py +++ b/tests/ahriman/core/database/operations/test_changes_operations.py @@ -53,3 +53,12 @@ def test_changes_insert_remove_full(database: SQLite, package_ahriman: Package, assert database.changes_get(package_ahriman.base).changes is None assert database.changes_get(package_python_schedule.base).changes is None assert database.changes_get(package_ahriman.base, RepositoryId("i686", database._repository_id.name)) == changes2 + + +def test_changes_insert_pkgbuild_preserve(database: SQLite, package_ahriman: Package) -> None: + """ + must preserve existing pkgbuild when inserting changes without pkgbuild + """ + database.changes_insert(package_ahriman.base, Changes("sha1", "change1", "pkgbuild1")) + database.changes_insert(package_ahriman.base, Changes("sha2", "change2", None)) + assert database.changes_get(package_ahriman.base) == Changes("sha2", "change2", "pkgbuild1")