Skip to content

The agent that grows with you

install.sh: refuse to clone over an unrelated destination

ethernet04bfa58d47e3
diff --git a/scripts/install.sh b/scripts/install.sh
index 6af17465e73..69721233dee 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -311,6 +311,16 @@ stage_repository() {
log "not fast-forwardable; reset to origin/$BRANCH"
fi
else
+ # `mv <clone> <existing dir>` nests the checkout INSIDE it as
+ # <dir>/tree, so a pre-existing destination must be empty (we take
+ # the empty dir over) or we refuse: whatever lives there is not ours.
+ if [ -e "$INSTALL_DIR" ] || [ -L "$INSTALL_DIR" ]; then
+ if [ -d "$INSTALL_DIR" ] && [ ! -L "$INSTALL_DIR" ] && [ -z "$(ls -A "$INSTALL_DIR")" ]; then
+ rmdir "$INSTALL_DIR" || fail "cannot replace empty $INSTALL_DIR"
+ else
+ fail "$INSTALL_DIR exists and is not a Hermes git checkout. Move it aside, or install elsewhere with --dir <path>."
+ fi
+ fi
log "cloning $REPO_URL ($BRANCH) into $INSTALL_DIR"
mkdir -p "$(dirname "$INSTALL_DIR")"
local staged attempt cloned=false
diff --git a/tests/scripts/install/test_install_sh_existing_destination.py b/tests/scripts/install/test_install_sh_existing_destination.py
new file mode 100644
index 00000000000..6958f00fbb4
--- /dev/null
+++ b/tests/scripts/install/test_install_sh_existing_destination.py
@@ -0,0 +1,56 @@
+"""A fresh clone never lands inside, or on top of, an unrelated destination."""
+import os
+from pathlib import Path
+import shlex
+import subprocess
+
+import pytest
+
+ROOT = Path(__file__).resolve().parent.parent.parent.parent
+
+
+def _origin(tmp_path: Path) -> Path:
+ origin = tmp_path / "origin"
+ origin.mkdir()
+ git = ["git", "-C", str(origin)]
+ for args in (["init", "-b", "main"], ["config", "user.email", "[email protected]"],
+ ["config", "user.name", "Fixture"]):
+ subprocess.run([*git, *args], check=True, capture_output=True)
+ (origin / "README").write_text("complete checkout\n")
+ subprocess.run([*git, "add", "README"], check=True, capture_output=True)
+ subprocess.run([*git, "commit", "-m", "fixture"], check=True, capture_output=True)
+ return origin
+
+
+def _stage_repository(tmp_path: Path, dest: Path) -> subprocess.CompletedProcess:
+ env = dict(os.environ, HOME=tmp_path.as_posix(), HERMES_HOME=(tmp_path / "home").as_posix(),
+ HERMES_INSTALL_DIR=dest.as_posix(), HERMES_REPO_URL=_origin(tmp_path).as_posix())
+ script = f"source {shlex.quote((ROOT / 'scripts/install.sh').as_posix())} --manifest\nstage_repository\n"
+ return subprocess.run(["bash", "-c", script], env=env, capture_output=True, text=True, timeout=30)
+
+
[email protected]("shape", ["non-empty-dir", "file"])
+def test_unrelated_destination_is_refused_untouched(tmp_path, shape):
+ dest = tmp_path / "install"
+ if shape == "file":
+ dest.write_text("not a checkout\n")
+ else:
+ (dest / "notes").mkdir(parents=True)
+ (dest / "notes" / "keep.txt").write_text("user data\n")
+ before = sorted(str(p.relative_to(dest)) for p in dest.rglob("*")) if dest.is_dir() else dest.read_text()
+ result = _stage_repository(tmp_path, dest)
+ assert result.returncode != 0
+ assert "not a Hermes git checkout" in result.stderr
+ after = sorted(str(p.relative_to(dest)) for p in dest.rglob("*")) if dest.is_dir() else dest.read_text()
+ assert after == before, "refusal must not nest a tree/ inside or alter the destination"
+ assert not (dest / "tree").exists()
+ assert not list(tmp_path.glob(".hermes-clone-*"))
+
+
+def test_empty_destination_directory_becomes_the_checkout(tmp_path):
+ dest = tmp_path / "install"
+ dest.mkdir()
+ result = _stage_repository(tmp_path, dest)
+ assert result.returncode == 0, result.stdout + result.stderr
+ assert (dest / "README").read_text() == "complete checkout\n", "checkout is AT the destination, not under tree/"
+ assert not (dest / "tree").exists()