diff --git a/nava/platform/util/git.py b/nava/platform/util/git.py index 5554099..39fa716 100644 --- a/nava/platform/util/git.py +++ b/nava/platform/util/git.py @@ -92,8 +92,16 @@ def tag(self, tag: str) -> None: def rename_branch(self, new_branch_name: str) -> None: self._run_cmd(["git", "branch", "-m", new_branch_name]) - def get_commit_hash_for_head(self) -> str: - return self._run_cmd(["git", "rev-parse", "HEAD"]).stdout.strip() + def get_commit_hash_for_head(self) -> str | None: + result = self._run_cmd(["git", "rev-parse", "HEAD"]) + + # say you run this against an empty repo, you'll get a return code of + # 128 and message "fatal: ambiguous argument 'HEAD': unknown revision or + # path not in the working tree." + if result.returncode != 0: + return None + + return result.stdout.strip() def is_path_ignored(self, path: str) -> bool: result = self._run_cmd(["git", "check-ignore", "-q", path]) @@ -135,6 +143,14 @@ def get_commit_description(self, commit_ish: str = "HEAD") -> str | None: return result.stdout.strip() + def get_commit_count(self, ref: str = "HEAD") -> int | None: + result = self._run_cmd(["git", "rev-list", "--count", ref]) + + if result.returncode != 0: + return None + + return int(result.stdout.strip()) + def is_a_git_worktree(dir: Path) -> bool: result = run_text( diff --git a/tests/projects/test_migrate_from_legacy_template.py b/tests/projects/test_migrate_from_legacy_template.py index f55dd76..5062fd1 100644 --- a/tests/projects/test_migrate_from_legacy_template.py +++ b/tests/projects/test_migrate_from_legacy_template.py @@ -29,14 +29,27 @@ def new_template_dir_with_git(tmp_path: Path) -> GitProject: return new_dir_with_git(tmp_path / "template") +@pytest.fixture +def new_template_dir_with_git_and_content(new_template_dir_with_git: GitProject) -> GitProject: + template_git = new_template_dir_with_git + + (template_git.dir / "foo.txt").write_text("bar") + template_git.commit_all("Initial commit") + + return template_git + + @pytest.fixture def legacy_project_dir_with_git( - new_project_dir_with_git: GitProject, new_template_dir_with_git: GitProject + new_project_dir_with_git: GitProject, new_template_dir_with_git_and_content: GitProject ) -> GitProject: + template_git = new_template_dir_with_git_and_content + (new_project_dir_with_git.dir / ".template-version").write_text( - new_template_dir_with_git.get_commit_hash_for_head() + template_git.get_commit_hash_for_head() or "foobar" ) new_project_dir_with_git.commit_all("Legacy install") + return new_project_dir_with_git @@ -64,7 +77,7 @@ def test_migrate_from_legacy_no_commit( cli_context: CliContext, ): project = Project(legacy_project_dir_with_git.dir) - commit_count_before = len(project.git.log().stdout.splitlines()) + commit_count_before = project.git.get_commit_count() MigrateFromLegacyTemplate( ctx=cli_context, @@ -73,7 +86,7 @@ def test_migrate_from_legacy_no_commit( new_version_answers_file_name="foo.yml", ).migrate_from_legacy(commit=False) - commit_count_after = len(project.git.log().stdout.splitlines()) + commit_count_after = project.git.get_commit_count() # only new file should exist assert (project.dir / ".template" / "foo.yml").exists() @@ -88,7 +101,7 @@ def test_migrate_from_legacy_commit( cli_context: CliContext, ): project = Project(legacy_project_dir_with_git.dir) - commit_count_before = len(project.git.log().stdout.splitlines()) + commit_count_before = project.git.get_commit_count() or 0 MigrateFromLegacyTemplate( ctx=cli_context, @@ -97,7 +110,7 @@ def test_migrate_from_legacy_commit( new_version_answers_file_name="foo.yml", ).migrate_from_legacy(commit=True) - commit_count_after = len(project.git.log().stdout.splitlines()) + commit_count_after = project.git.get_commit_count() # only new file should exist assert (project.dir / ".template" / "foo.yml").exists()