From 94181f12a9ef80f78d125f87bf9847c0fd09908b Mon Sep 17 00:00:00 2001 From: Kaloyan Naumov Date: Sat, 28 Nov 2026 00:15:08 +0000 Subject: [PATCH 1/5] A configured entry may not share a clone with another repository A derived clone dir names the repository alone, so a fork and its upstream resolve to one bare clone and each fetch moves the other's branch refs. When two LOOM_CODE_REPOS_JSON entries resolve to the same git_dir, the parser compares their git_remote values after the defaults and skips the later entry if they differ, with a git_dir configuration error that names the holder and says to use one spelling of the remote or set a separate git_dir. Signed-off-by: Kaloyan Naumov --- docs/operations/code-index-operations.md | 12 ++- server/code/domain/index/repos.py | 26 ++++++ tests/unit/test_repos_clone_dir_collision.py | 96 ++++++++++++++++++++ 3 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 tests/unit/test_repos_clone_dir_collision.py diff --git a/docs/operations/code-index-operations.md b/docs/operations/code-index-operations.md index 11c30551f5d..d87642b831f 100644 --- a/docs/operations/code-index-operations.md +++ b/docs/operations/code-index-operations.md @@ -117,7 +117,7 @@ LOOM_CODE_REPOS_JSON='[ | ------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------- | | `namespace`, `org` | where the index lives | | `repo` | `owner/name` on GitHub | -| `git_dir`, `git_remote` | the bare clone and its remote; branches of one repository share one clone | +| `git_dir`, `git_remote` | the bare clone and its remote; entries that share a clone name the same remote | | `git_branch` | the branch this namespace tracks | | `path_prefixes` | comma-separated prefixes to index; an entry without it indexes the whole tree minus the built-in denylist | | `exclude` | globs never indexed (list or comma-separated) | @@ -219,8 +219,14 @@ and a `.loomignore` file at the indexed commit are honoured as excludes. A depen vendored as a git submodule is not in the bare mirror; give it its own entry and name it in `depends_on`. The gauge `loom.code.repos_config_errors` reports entries the server could not use (invalid JSON, a nameless entry, a namespace with `.` or `/`, a duplicate -namespace); 0 means the value parsed clean. Validate the JSON before a restart -with `docker compose --env-file .env -f dev/docker-compose.cloud.yml config`. +namespace, a clone an earlier entry holds with another remote); 0 means the value parsed +clean. While it is not 0 the mirror sweep removes nothing on any host, so the mirrors, +checkouts and lock files of removed repositories stay until the error is fixed. Entries +that share a `git_dir` share its clone only when their `git_remote` values, after the +defaults, are the same string: branches of one repository use one spelling of the +remote, and a fork and its upstream each set their own `git_dir`. Validate the JSON +before a restart with +`docker compose --env-file .env -f dev/docker-compose.cloud.yml config`. Who can read an index is governed by the namespace's join policy and grants ([tokens and grants](../guides/tokens-and-grants.md)). `LOOM_CODE_ORG_READABLE=true` diff --git a/server/code/domain/index/repos.py b/server/code/domain/index/repos.py index 76ccac0cdda..f50fbfc76b9 100644 --- a/server/code/domain/index/repos.py +++ b/server/code/domain/index/repos.py @@ -536,6 +536,9 @@ def _configured_from_env(config: Settings, errors: dict[str, str]) -> list[RepoC base = _scalar_repo(config) repos: list[RepoConfig] = [] seen: set[str] = set() + # A fork and its upstream derive the same clone dir; in one clone each fetch would + # move the other's branch refs. + clone_holders: dict[str, tuple[str, str]] = {} for i, e in enumerate(entries): if not isinstance(e, dict) or not e.get("namespace"): errors[f"entry:{i}"] = f"code repo entry missing 'namespace', skipped: {e!r}" @@ -579,6 +582,8 @@ def _configured_from_env(config: Settings, errors: dict[str, str]) -> list[RepoC proxy_identity = _entry_git_proxy_identity(e, ns, errors) if proxy_identity is None: continue + if _clone_taken(clone_holders, git_dir, git_remote, ns, errors): + continue repos.append( RepoConfig( org=org, @@ -629,6 +634,27 @@ def _configured_from_env(config: Settings, errors: dict[str, str]) -> list[RepoC return repos +def _clone_taken( + holders: dict[str, tuple[str, str]], + git_dir: str, + git_remote: str, + ns: str, + errors: dict[str, str], +) -> bool: + """Whether an earlier entry holds `git_dir` with another `git_remote` (both after + `_entry_clone_fields` applies the defaults); records the entry's config error when + one does. The first entry claims a clone; branches share it by spelling the remote + the same way.""" + held_remote, held_ns = holders.setdefault(git_dir, (git_remote, ns)) + if held_remote == git_remote: + return False + errors[f"git_dir:{ns}"] = ( + f"code repo {ns} skipped: its clone {git_dir} is held by {held_ns} with another " + "git_remote; use one spelling of the remote, or set a separate git_dir" + ) + return True + + def _entry_clone_fields( e: dict[str, Any], ns: str, diff --git a/tests/unit/test_repos_clone_dir_collision.py b/tests/unit/test_repos_clone_dir_collision.py new file mode 100644 index 00000000000..e9399ccb7ff --- /dev/null +++ b/tests/unit/test_repos_clone_dir_collision.py @@ -0,0 +1,96 @@ +"""Two entries on one clone dir share the bare clone only when their `git_remote` +values, after the defaults, are the same: a fork and its upstream derive the same +clone dir, and one clone would let each fetch move the other's refs.""" + +from __future__ import annotations + +import json + +import pytest + +import server.code.domain.index.repos as repos + + +@pytest.fixture +def cfg(monkeypatch: pytest.MonkeyPatch, scalar_code_repo): + from server.settings import settings + + def _set(entries): + monkeypatch.setattr(settings, "code_repos_json", json.dumps(entries)) + repos._config_errors.clear() + repos._config_errors_logged.clear() + return {r.namespace: r for r in repos.configured_repos()} + + return _set + + +def test_a_fork_and_its_upstream_do_not_share_a_clone(cfg) -> None: + got = cfg( + [ + { + "namespace": "code-widgets-fork", + "repo": "git.example.com/acme/widgets", + "git_remote": "https://git.example.com/acme/widgets.git", + }, + {"namespace": "code-widgets-upstream", "repo": "widgets-org/widgets"}, + ] + ) + assert "code-widgets-fork" in got + assert "code-widgets-upstream" not in got + msg = repos._config_errors["git_dir:code-widgets-upstream"] + assert "code-widgets-fork" in msg + assert "use one spelling of the remote, or set a separate git_dir" in msg + + +def test_branches_with_one_spelling_of_the_remote_share_their_clone(cfg) -> None: + got = cfg( + [ + {"namespace": "code-widgets", "repo": "acme/widgets", "git_branch": "main"}, + { + "namespace": "code-widgets-2-4", + "repo": "acme/widgets", + "git_branch": "release/2.4", + "git_remote": "https://github.com/acme/widgets.git", + }, + ] + ) + assert got["code-widgets"].git_dir == got["code-widgets-2-4"].git_dir + assert repos.config_error_count() == 0 + + +def test_two_spellings_of_one_remote_are_refused(cfg) -> None: + got = cfg( + [ + {"namespace": "code-widgets", "repo": "acme/widgets", "git_branch": "main"}, + { + "namespace": "code-widgets-2-4", + "repo": "acme/widgets", + "git_branch": "release/2.4", + "git_remote": "git@github.com:acme/widgets.git", + }, + ] + ) + assert "code-widgets" in got + assert "code-widgets-2-4" not in got + msg = repos._config_errors["git_dir:code-widgets-2-4"] + assert "code-widgets" in msg + assert "use one spelling of the remote, or set a separate git_dir" in msg + + +def test_a_separate_git_dir_lets_both_in(cfg) -> None: + got = cfg( + [ + { + "namespace": "code-widgets-fork", + "repo": "git.example.com/acme/widgets", + "git_remote": "https://git.example.com/acme/widgets.git", + }, + { + "namespace": "code-widgets-upstream", + "repo": "widgets-org/widgets", + "git_dir": "var/code_repos/widgets-upstream.git", + }, + ] + ) + assert got["code-widgets-fork"].git_dir != got["code-widgets-upstream"].git_dir + assert repos.config_error_count() == 0 From cd1401e1ef3aee17e4270a3eff485a85dec9cf3b Mon Sep 17 00:00:00 2001 From: Kaloyan Naumov Date: Sat, 28 Nov 2026 00:34:13 +0000 Subject: [PATCH 2/5] Compare a held clone dir by its canonical path A relative git_dir and its absolute path, or a symlink to a held clone, are one bare clone; compared as written, two remotes could still fetch into it. Signed-off-by: Kaloyan Naumov --- docs/operations/code-index-operations.md | 5 +-- server/code/domain/index/repos.py | 6 ++-- tests/unit/test_repos_clone_dir_collision.py | 32 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/docs/operations/code-index-operations.md b/docs/operations/code-index-operations.md index d87642b831f..77696007483 100644 --- a/docs/operations/code-index-operations.md +++ b/docs/operations/code-index-operations.md @@ -224,8 +224,9 @@ clean. While it is not 0 the mirror sweep removes nothing on any host, so the mi checkouts and lock files of removed repositories stay until the error is fixed. Entries that share a `git_dir` share its clone only when their `git_remote` values, after the defaults, are the same string: branches of one repository use one spelling of the -remote, and a fork and its upstream each set their own `git_dir`. Validate the JSON -before a restart with +remote, and a fork beside its upstream sets its own `git_dir`. Name one clone with one +spelling of its `git_dir` in every entry that shares it. Validate the JSON before a +restart with `docker compose --env-file .env -f dev/docker-compose.cloud.yml config`. Who can read an index is governed by the namespace's join policy and grants diff --git a/server/code/domain/index/repos.py b/server/code/domain/index/repos.py index f50fbfc76b9..324e915067d 100644 --- a/server/code/domain/index/repos.py +++ b/server/code/domain/index/repos.py @@ -644,8 +644,10 @@ def _clone_taken( """Whether an earlier entry holds `git_dir` with another `git_remote` (both after `_entry_clone_fields` applies the defaults); records the entry's config error when one does. The first entry claims a clone; branches share it by spelling the remote - the same way.""" - held_remote, held_ns = holders.setdefault(git_dir, (git_remote, ns)) + the same way. Only this check treats two spellings of a directory as one (symlinks + and `..` resolved); the mirror sweep, its lock files and `trunk_entry` compare + `git_dir` as written.""" + held_remote, held_ns = holders.setdefault(os.path.realpath(git_dir), (git_remote, ns)) if held_remote == git_remote: return False errors[f"git_dir:{ns}"] = ( diff --git a/tests/unit/test_repos_clone_dir_collision.py b/tests/unit/test_repos_clone_dir_collision.py index e9399ccb7ff..b4289734947 100644 --- a/tests/unit/test_repos_clone_dir_collision.py +++ b/tests/unit/test_repos_clone_dir_collision.py @@ -94,3 +94,35 @@ def test_a_separate_git_dir_lets_both_in(cfg) -> None: ) assert got["code-widgets-fork"].git_dir != got["code-widgets-upstream"].git_dir assert repos.config_error_count() == 0 + + +def _two_repositories(first_dir: str, second_dir: str) -> list[dict[str, str]]: + return [ + {"namespace": "code-widgets-fork", "repo": "acme/widgets", "git_dir": first_dir}, + { + "namespace": "code-widgets-upstream", + "repo": "widgets-org/widgets", + "git_dir": second_dir, + }, + ] + + +def test_two_spellings_of_one_clone_are_one_clone( + cfg, monkeypatch: pytest.MonkeyPatch, tmp_path +) -> None: + monkeypatch.chdir(tmp_path) + got = cfg(_two_repositories("var/widgets.git", str(tmp_path / "var" / "widgets.git"))) + assert "code-widgets-fork" in got + assert "code-widgets-upstream" not in got + assert "code-widgets-fork" in repos._config_errors["git_dir:code-widgets-upstream"] + + +def test_a_symlink_to_a_held_clone_is_the_same_clone( + cfg, monkeypatch: pytest.MonkeyPatch, tmp_path +) -> None: + monkeypatch.chdir(tmp_path) + (tmp_path / "widgets.git").mkdir() + (tmp_path / "alias.git").symlink_to(tmp_path / "widgets.git") + got = cfg(_two_repositories("widgets.git", "alias.git")) + assert "code-widgets-upstream" not in got + assert "git_dir:code-widgets-upstream" in repos._config_errors From 004b81f73efe57c8b324f63ade0591d8e88fdaed Mon Sep 17 00:00:00 2001 From: Kaloyan Naumov Date: Sat, 28 Nov 2026 11:10:08 +0000 Subject: [PATCH 3/5] Key held clone dirs by path and by directory identity `_clone_taken` keyed its holders by `realpath(git_dir)`, which keeps the spelling it is given. On a case-insensitive volume `Widget.git` and `widget.git` are one directory under two keys, so two entries with different remotes both loaded and their fetches moved the same refs. Each entry is now held under its canonical path and, once `git_dir` exists, also under its `(st_dev, st_ino)`; it collides with a holder when either key is held. Keying by identity alone would let a directory that does not exist yet (`missing/../widgets.git`) and an existing `widgets.git` slip past each other, as a path key and an identity key never compare. The cost is one `realpath` per entry plus one `os.stat` while the directory exists, no walk over the held clones. A stat that raises OSError (directory missing, or removed while the config is parsed) leaves the path key alone. An entry with the held remote still shares the clone; one with another remote is skipped with the `git_dir` error. Two entries naming a directory that is not created yet, with spellings that differ in case, are still told apart until the first clone creates it. `known_repo_count` no longer says it does no I/O. Signed-off-by: Kaloyan Naumov --- server/code/domain/index/repos.py | 34 ++++++++++++++----- tests/unit/test_repos_clone_dir_collision.py | 35 ++++++++++++++++++++ 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/server/code/domain/index/repos.py b/server/code/domain/index/repos.py index 324e915067d..226a5ebc798 100644 --- a/server/code/domain/index/repos.py +++ b/server/code/domain/index/repos.py @@ -380,8 +380,9 @@ def configured_repos() -> list[RepoConfig]: def known_repo_count() -> int: - """How many entries this process knows are configured (no I/O): the operator's, and - the self-service registry's once it is read. Zero means no repository job is owed + """How many entries this process knows are configured (no store read; a `realpath` + and a stat of each entry's clone dir): the operator's, and the self-service + registry's once it is read. Zero means no repository job is owed (`sandbox.job_owed`).""" return len(_operator_entries() if registry_unread() else configured_repos()) @@ -538,7 +539,7 @@ def _configured_from_env(config: Settings, errors: dict[str, str]) -> list[RepoC seen: set[str] = set() # A fork and its upstream derive the same clone dir; in one clone each fetch would # move the other's branch refs. - clone_holders: dict[str, tuple[str, str]] = {} + clone_holders: dict[str | tuple[int, int], tuple[str, str]] = {} for i, e in enumerate(entries): if not isinstance(e, dict) or not e.get("namespace"): errors[f"entry:{i}"] = f"code repo entry missing 'namespace', skipped: {e!r}" @@ -634,8 +635,22 @@ def _configured_from_env(config: Settings, errors: dict[str, str]) -> list[RepoC return repos +def _clone_keys(git_dir: str) -> list[str | tuple[int, int]]: + """The keys one clone dir is held under: its canonical path, and its + `(st_dev, st_ino)` once it exists, so two spellings of one directory (a case alias + on a case-insensitive volume, a symlink, a bind mount) share a key. A directory + that cannot be read keeps the path key alone.""" + keys: list[str | tuple[int, int]] = [os.path.realpath(git_dir)] + try: + st = os.stat(git_dir) + except OSError: + return keys + keys.append((st.st_dev, st.st_ino)) + return keys + + def _clone_taken( - holders: dict[str, tuple[str, str]], + holders: dict[str | tuple[int, int], tuple[str, str]], git_dir: str, git_remote: str, ns: str, @@ -644,11 +659,14 @@ def _clone_taken( """Whether an earlier entry holds `git_dir` with another `git_remote` (both after `_entry_clone_fields` applies the defaults); records the entry's config error when one does. The first entry claims a clone; branches share it by spelling the remote - the same way. Only this check treats two spellings of a directory as one (symlinks - and `..` resolved); the mirror sweep, its lock files and `trunk_entry` compare - `git_dir` as written.""" - held_remote, held_ns = holders.setdefault(os.path.realpath(git_dir), (git_remote, ns)) + the same way. Only this check treats two spellings of a directory as one (the same + canonical path, or the same directory by identity once it exists); the mirror + sweep, its lock files and `trunk_entry` compare `git_dir` as written.""" + keys = _clone_keys(git_dir) + held_remote, held_ns = next((holders[k] for k in keys if k in holders), (git_remote, ns)) if held_remote == git_remote: + for k in keys: + holders.setdefault(k, (held_remote, held_ns)) return False errors[f"git_dir:{ns}"] = ( f"code repo {ns} skipped: its clone {git_dir} is held by {held_ns} with another " diff --git a/tests/unit/test_repos_clone_dir_collision.py b/tests/unit/test_repos_clone_dir_collision.py index b4289734947..9638f0597a2 100644 --- a/tests/unit/test_repos_clone_dir_collision.py +++ b/tests/unit/test_repos_clone_dir_collision.py @@ -5,6 +5,7 @@ from __future__ import annotations import json +import os import pytest @@ -126,3 +127,37 @@ def test_a_symlink_to_a_held_clone_is_the_same_clone( got = cfg(_two_repositories("widgets.git", "alias.git")) assert "code-widgets-upstream" not in got assert "git_dir:code-widgets-upstream" in repos._config_errors + + +def test_a_case_alias_of_a_held_clone_is_the_same_clone( + cfg, monkeypatch: pytest.MonkeyPatch, tmp_path +) -> None: + # One directory, as on a case-insensitive volume: the lookup of either spelling + # finds `Widget.git`, so `os.stat` reports the same identity for both. + monkeypatch.chdir(tmp_path) + (tmp_path / "Widget.git").mkdir() + real_stat = os.stat + + def stat(path, *args, **kwargs): + if os.path.basename(path).lower() == "widget.git": + path = tmp_path / "Widget.git" + return real_stat(path, *args, **kwargs) + + monkeypatch.setattr(os, "stat", stat) + got = cfg(_two_repositories("Widget.git", "widget.git")) + assert "code-widgets-fork" in got + assert "code-widgets-upstream" not in got + assert "code-widgets-fork" in repos._config_errors["git_dir:code-widgets-upstream"] + + +def test_a_missing_spelling_of_a_held_clone_is_the_same_clone( + cfg, monkeypatch: pytest.MonkeyPatch, tmp_path +) -> None: + # `os.stat` fails on the missing component while the canonical path collapses `..`: + # the entry has no identity yet but still names the held directory. + monkeypatch.chdir(tmp_path) + (tmp_path / "widgets.git").mkdir() + got = cfg(_two_repositories("widgets.git", "missing/../widgets.git")) + assert "code-widgets-fork" in got + assert "code-widgets-upstream" not in got + assert "code-widgets-fork" in repos._config_errors["git_dir:code-widgets-upstream"] From c97ad5e62c1b81fbbeae2c63db82ab9e2f6fe96b Mon Sep 17 00:00:00 2001 From: Kaloyan Naumov Date: Sat, 28 Nov 2026 20:37:26 +0000 Subject: [PATCH 4/5] Refuse git_dir spellings that differ only in case before either exists Two entries whose git_dir differ only in the case of the last component passed validation on a fresh volume, because no directory existed yet to compare by identity. Hold each clone dir under its canonical path with the last component lowercased as well, so they collide with the existing error. On a case-sensitive host this also refuses two distinct directories spelled Widget.git and widget.git with different remotes; the error says to set a separate git_dir. Co-Authored-By: Claude Sonnet 5.5 Signed-off-by: Kaloyan Naumov --- server/code/domain/index/repos.py | 13 ++++++++----- tests/unit/test_repos_clone_dir_collision.py | 11 +++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/server/code/domain/index/repos.py b/server/code/domain/index/repos.py index 226a5ebc798..d2451c87b6f 100644 --- a/server/code/domain/index/repos.py +++ b/server/code/domain/index/repos.py @@ -636,11 +636,14 @@ def _configured_from_env(config: Settings, errors: dict[str, str]) -> list[RepoC def _clone_keys(git_dir: str) -> list[str | tuple[int, int]]: - """The keys one clone dir is held under: its canonical path, and its - `(st_dev, st_ino)` once it exists, so two spellings of one directory (a case alias - on a case-insensitive volume, a symlink, a bind mount) share a key. A directory - that cannot be read keeps the path key alone.""" - keys: list[str | tuple[int, int]] = [os.path.realpath(git_dir)] + """The keys one clone dir is held under: its canonical path, the same path with + the last component lowercased (so two spellings that differ only in case collide + before either directory exists), and its `(st_dev, st_ino)` once it exists, so two + spellings of one directory (a case alias on a case-insensitive volume, a symlink, + a bind mount) share a key. A directory that cannot be read keeps the path keys.""" + path = os.path.realpath(git_dir) + head, tail = os.path.split(path) + keys: list[str | tuple[int, int]] = [path, os.path.join(head, tail.lower())] try: st = os.stat(git_dir) except OSError: diff --git a/tests/unit/test_repos_clone_dir_collision.py b/tests/unit/test_repos_clone_dir_collision.py index 9638f0597a2..1c26bd52904 100644 --- a/tests/unit/test_repos_clone_dir_collision.py +++ b/tests/unit/test_repos_clone_dir_collision.py @@ -161,3 +161,14 @@ def test_a_missing_spelling_of_a_held_clone_is_the_same_clone( assert "code-widgets-fork" in got assert "code-widgets-upstream" not in got assert "code-widgets-fork" in repos._config_errors["git_dir:code-widgets-upstream"] + + +def test_two_spellings_that_differ_only_in_case_are_one_clone_before_either_exists( + cfg, monkeypatch: pytest.MonkeyPatch, tmp_path +) -> None: + # A fresh volume: neither directory exists, so no identity can tell them apart. + monkeypatch.chdir(tmp_path) + got = cfg(_two_repositories("Widget.git", "widget.git")) + assert "code-widgets-fork" in got + assert "code-widgets-upstream" not in got + assert "code-widgets-fork" in repos._config_errors["git_dir:code-widgets-upstream"] From 553cbe20a470e5315973067919bc246ca79810ad Mon Sep 17 00:00:00 2001 From: Kaloyan Naumov Date: Mon, 30 Nov 2026 11:13:21 +0000 Subject: [PATCH 5/5] Hold clone dirs by path and identity only, and document the first-run limit The lowercased last component made two clone paths that differ only in case collide, which also refused two distinct directories on a case-sensitive volume. A clone dir is held under its canonical path and, once it exists, under its (st_dev, st_ino), so case-sensitive volumes accept both spellings and a case-insensitive volume is caught as soon as the directory exists. Before the first clone, two spellings of one path on a case-insensitive volume still pass. The operations guide names this and asks for clone paths that differ by more than capitalization. The test creates both directories and asserts both entries load; it is skipped on a case-insensitive volume. Signed-off-by: Kaloyan Naumov --- docs/operations/code-index-operations.md | 6 ++++-- server/code/domain/index/repos.py | 13 +++++-------- tests/unit/test_repos_clone_dir_collision.py | 11 +++++++---- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/docs/operations/code-index-operations.md b/docs/operations/code-index-operations.md index 77696007483..251f6029ba9 100644 --- a/docs/operations/code-index-operations.md +++ b/docs/operations/code-index-operations.md @@ -225,8 +225,10 @@ checkouts and lock files of removed repositories stay until the error is fixed. that share a `git_dir` share its clone only when their `git_remote` values, after the defaults, are the same string: branches of one repository use one spelling of the remote, and a fork beside its upstream sets its own `git_dir`. Name one clone with one -spelling of its `git_dir` in every entry that shares it. Validate the JSON before a -restart with +spelling of its `git_dir` in every entry that shares it. On a case-insensitive volume, +two `git_dir` values that differ only in case are one directory; the check sees that +once the directory exists, and before the first clone both spellings pass. Use clone +paths that differ by more than capitalization. Validate the JSON before a restart with `docker compose --env-file .env -f dev/docker-compose.cloud.yml config`. Who can read an index is governed by the namespace's join policy and grants diff --git a/server/code/domain/index/repos.py b/server/code/domain/index/repos.py index d2451c87b6f..226a5ebc798 100644 --- a/server/code/domain/index/repos.py +++ b/server/code/domain/index/repos.py @@ -636,14 +636,11 @@ def _configured_from_env(config: Settings, errors: dict[str, str]) -> list[RepoC def _clone_keys(git_dir: str) -> list[str | tuple[int, int]]: - """The keys one clone dir is held under: its canonical path, the same path with - the last component lowercased (so two spellings that differ only in case collide - before either directory exists), and its `(st_dev, st_ino)` once it exists, so two - spellings of one directory (a case alias on a case-insensitive volume, a symlink, - a bind mount) share a key. A directory that cannot be read keeps the path keys.""" - path = os.path.realpath(git_dir) - head, tail = os.path.split(path) - keys: list[str | tuple[int, int]] = [path, os.path.join(head, tail.lower())] + """The keys one clone dir is held under: its canonical path, and its + `(st_dev, st_ino)` once it exists, so two spellings of one directory (a case alias + on a case-insensitive volume, a symlink, a bind mount) share a key. A directory + that cannot be read keeps the path key alone.""" + keys: list[str | tuple[int, int]] = [os.path.realpath(git_dir)] try: st = os.stat(git_dir) except OSError: diff --git a/tests/unit/test_repos_clone_dir_collision.py b/tests/unit/test_repos_clone_dir_collision.py index 1c26bd52904..9906363ef55 100644 --- a/tests/unit/test_repos_clone_dir_collision.py +++ b/tests/unit/test_repos_clone_dir_collision.py @@ -163,12 +163,15 @@ def test_a_missing_spelling_of_a_held_clone_is_the_same_clone( assert "code-widgets-fork" in repos._config_errors["git_dir:code-widgets-upstream"] -def test_two_spellings_that_differ_only_in_case_are_one_clone_before_either_exists( +def test_clone_dirs_that_differ_only_in_case_stay_distinct_on_a_case_sensitive_volume( cfg, monkeypatch: pytest.MonkeyPatch, tmp_path ) -> None: - # A fresh volume: neither directory exists, so no identity can tell them apart. monkeypatch.chdir(tmp_path) + (tmp_path / "Widget.git").mkdir() + (tmp_path / "widget.git").mkdir(exist_ok=True) + if len(list(tmp_path.iterdir())) != 2: + pytest.skip("the volume is case-insensitive: both spellings are one directory") got = cfg(_two_repositories("Widget.git", "widget.git")) assert "code-widgets-fork" in got - assert "code-widgets-upstream" not in got - assert "code-widgets-fork" in repos._config_errors["git_dir:code-widgets-upstream"] + assert "code-widgets-upstream" in got + assert repos.config_error_count() == 0