@@ -2058,8 +2058,46 @@ def test_tag_config_used_as_branch_kwarg_on_clone(self, tmp_path):
20582058 _ , kwargs = mock_clone_from .call_args
20592059 assert kwargs .get ("branch" ) == "v1.2.3"
20602060
2061+ def test_correct_tag_already_checked_out_returns_path (self , tmp_path ):
2062+ """Existing repo already on the correct tag (detached HEAD): returns path, no re-clone, no pull."""
2063+ from unittest .mock import MagicMock
2064+ from unittest .mock import patch
2065+
2066+ indexer = self ._make_indexer (tmp_path )
2067+ repo_config = GitRepository (
2068+ url = AnyHttpUrl ("https://github.com/fake/repo.git" ),
2069+ base_url = AnyHttpUrl ("https://example.com/" ),
2070+ tag = "v1.2.3" ,
2071+ folders = ["doc" ],
2072+ )
2073+
2074+ repo_dir = tmp_path / "repos" / "repo"
2075+ repo_dir .mkdir (parents = True )
2076+
2077+ # Simulate detached HEAD: active_branch raises TypeError (real GitPython behaviour)
2078+ mock_commit = MagicMock ()
2079+ mock_tag = MagicMock ()
2080+ mock_tag .name = "v1.2.3"
2081+ mock_tag .commit = mock_commit
2082+
2083+ mock_repo = MagicMock ()
2084+ mock_repo .tags = [mock_tag ]
2085+ mock_repo .head .commit = mock_commit # same object → already on correct tag
2086+
2087+ import git
2088+
2089+ with patch ("holoviz_mcp.holoviz_mcp.data.git.Repo" , return_value = mock_repo ):
2090+ with patch ("holoviz_mcp.holoviz_mcp.data.shutil.rmtree" ) as mock_rmtree :
2091+ with patch .object (git .Repo , "clone_from" ) as mock_clone_from :
2092+ result = indexer ._clone_or_update_repo_sync ("repo" , repo_config )
2093+
2094+ assert result == repo_dir
2095+ mock_rmtree .assert_not_called ()
2096+ mock_clone_from .assert_not_called ()
2097+ mock_repo .remotes .origin .pull .assert_not_called ()
2098+
20612099 def test_stale_tag_triggers_reclone (self , tmp_path ):
2062- """Existing repo on wrong tag: dir is deleted and re-cloned on the correct tag."""
2100+ """Existing repo on wrong tag (detached HEAD) : dir is deleted and re-cloned on the correct tag."""
20632101 from unittest .mock import MagicMock
20642102 from unittest .mock import patch
20652103
@@ -2076,8 +2114,14 @@ def test_stale_tag_triggers_reclone(self, tmp_path):
20762114 repo_dir = tmp_path / "repos" / "repo"
20772115 repo_dir .mkdir (parents = True )
20782116
2117+ # Simulate detached HEAD at a different tag — tag commit != HEAD commit
2118+ mock_tag = MagicMock ()
2119+ mock_tag .name = "v1.2.3"
2120+ mock_tag .commit = MagicMock () # tag points somewhere
2121+
20792122 mock_repo = MagicMock ()
2080- mock_repo .active_branch .name = "v0.9.0"
2123+ mock_repo .tags = [mock_tag ]
2124+ mock_repo .head .commit = MagicMock () # HEAD is elsewhere → stale
20812125
20822126 with patch ("holoviz_mcp.holoviz_mcp.data.git.Repo" , return_value = mock_repo ):
20832127 with patch ("holoviz_mcp.holoviz_mcp.data.shutil.rmtree" ) as mock_rmtree :
0 commit comments