Repository: bitcoin/bitcoin
File: ci/lint.py (Python)

Code before:
import time
from pathlib import Path

CONTAINER_NAME = "bitcoin-linter"


def run(cmd, **kwargs):
    print("+ " + shlex.join(cmd), flush=True)
    kwargs.setdefault("check", True)
    try:
        return subprocess.run(cmd, **kwargs)
    except Exception as e:
        sys.exit(str(e))


def get_worktree_mounts(repo_root):
    git_path = repo_root / ".git"
    if not git_path.is_file():
        return []
    content = git_path.read_text().strip()
    if not content.startswith("gitdir: "):
        return []
    gitdir = (repo_root / content.removeprefix("gitdir: ")).resolve()
    main_gitdir = gitdir.parent.parent
    return [
        f"--volume={gitdir}:{gitdir}:ro",
        f"--volume={main_gitdir}:{main_gitdir}:ro",
    ]


def main():
    repo_root = Path(__file__).resolve().parent.parent
    is_ci = os.environ.get("GITHUB_ACTIONS") == "true"
    container = os.environ.get("CONTAINER_NAME", CONTAINER_NAME)

    build_cmd = [
        "docker",
        "buildx",
        "build",
        f"--tag={container}",
        *shlex.split(os.environ.get("DOCKER_BUILD_CACHE_ARG", "")),
        f"--file={repo_root}/ci/lint_imagefile",
        str(repo_root),
    ]
    if run(build_cmd, check=False).returncode != 0:
        if is_ci:
            print("Retry building image after failure")
            time.sleep(3)
        run(build_cmd)

    extra_env = []

Review comment (suggestion):
```suggestion
        f"--volume={gitdir}:{gitdir}",
```

otherwise:

```
fatal: Unable to create '/btc/bitcoin/.git/worktrees/34391/index.lock': Read-only file system 
```

alternatively, could get rid `gitdir` and just mount `main_gitdir` (without `ro` ):

```diff
diff --git a/ci/lint.py b/ci/lint.py
index ed522c6e90..eff624ce8e 100755
--- a/ci/lint.py
+++ b/ci/lint.py
@@ -25,16 +25,12 @@ def run(cmd, **kwargs):
 def get_worktree_mounts(repo_root):
     git_path = repo_root / ".git"
     if not git_path.is_file():
-        return []
+        return ""
     content = git_path.read_text().strip()
     if not content.startswith("gitdir: "):
-        return []
-    gitdir = (repo_root / content.removeprefix("gitdir: ")).resolve()
-    main_gitdir = gitdir.parent.parent
-    return [
-        f"--volume={gitdir}:{gitdir}:ro",
-        f"--volume={main_gitdir}:{main_gitdir}:ro",
-    ]
+        return ""
+    gitdir = (repo_root / content.removeprefix("gitdir: ")).resolve().parent.parent
+    return f"--volume={gitdir}:{gitdir}"


 def main():
@@ -71,7 +67,7 @@ def main():
             "--rm",
             *extra_env,
             f"--volume={repo_root}:/bitcoin",
-            *get_worktree_mounts(repo_root),
+            get_worktree_mounts(repo_root),
             *([] if is_ci else ["-it"]),
             container,
             *sys.argv[1:],
```

 but this is slightly less belt and suspenders I guess, no strong feelings about that.

Code after:
import time
from pathlib import Path


def run(cmd, **kwargs):
    print("+ " + shlex.join(cmd), flush=True)
    kwargs.setdefault("check", True)
    try:
        return subprocess.run(cmd, **kwargs)
    except Exception as e:
        sys.exit(str(e))


def get_worktree_mounts(repo_root):
    git_path = repo_root / ".git"
    if not git_path.is_file():
        return []
    content = git_path.read_text().strip()
    if not content.startswith("gitdir: "):
        return []
    gitdir = (repo_root / content.removeprefix("gitdir: ")).resolve()
    main_gitdir = gitdir.parent.parent
    return [
        f"--volume={gitdir}:{gitdir}",
        f"--volume={main_gitdir}:{main_gitdir}:ro",
    ]


def main():
    repo_root = Path(__file__).resolve().parent.parent
    is_ci = os.environ.get("GITHUB_ACTIONS") == "true"
    container = "bitcoin-linter"

    build_cmd = [
        "docker",
        "buildx",
        "build",
        f"--tag={container}",
        *shlex.split(os.environ.get("DOCKER_BUILD_CACHE_ARG", "")),
        f"--file={repo_root}/ci/lint_imagefile",
        str(repo_root),
    ]
    if run(build_cmd, check=False).returncode != 0:
        if is_ci:
            print("Retry building image after failure")
            time.sleep(3)
        run(build_cmd)

    extra_env = []
    if is_ci:
        if os.environ.get("GITHUB_EVENT_NAME") == "pull_request":