Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions bin/worktree.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,12 @@ _worktree_add_one() {
# Creates $PYAUTO_WT_ROOT/<task>/ and inside it:
# - a real git worktree for each listed repo, on branch feature/<task>
# (or checked out if the branch already exists)
# - a symlink back to $PYAUTO_MAIN/<name> for every other top-level entry
# - activate.sh with the per-task PYTHONPATH / cache overrides
# - a symlink back to $PYAUTO_MAIN/<name> for every other top-level entry,
# EXCEPT the names worktree_create writes itself (activate.sh and the
# root marker) — a linked activate.sh would make the write below follow
# the link and clobber the unversioned workspace-root file (#469)
# - activate.sh with the per-task PYTHONPATH / cache overrides, always a
# regular file of its own (any pre-existing link is removed first)
# The branch is created from the repo's default branch if it doesn't exist.
# Accepts both library repos (PyAutoFit, etc.) and workspace repos
# (autolens_workspace, etc.).
Expand Down Expand Up @@ -171,6 +175,9 @@ EOF
# even when the main workspace stores it inside a family directory.
local entry name family
local -A families=()
# Names this function writes into the bundle root itself: never symlink
# them, or the write would land in $PYAUTO_MAIN (#469).
local -A own=([activate.sh]=1 ["$PYAUTO_ROOT_MARKER"]=1)
while IFS=$'\t' read -r name entry; do
[[ -n "$entry" ]] || continue
if [[ "$(dirname "$entry")" != "$PYAUTO_MAIN" ]]; then
Expand All @@ -181,24 +188,64 @@ EOF
for entry in "$PYAUTO_MAIN"/*; do
name="$(basename "$entry")"
[[ -n "${families[$name]:-}" ]] && continue
[[ -n "${own[$name]:-}" ]] && continue
[[ -d "$entry/.git" || -f "$entry/.git" ]] && continue
[[ -e "$root/$name" ]] && continue # already a real worktree
ln -s "$entry" "$root/$name"
done
while IFS=$'\t' read -r name entry; do
[[ -n "$name" && -n "$entry" ]] || continue
[[ -n "${own[$name]:-}" ]] && continue
[[ -d "$entry" ]] || continue
[[ -e "$root/$name" ]] && continue
ln -s "$entry" "$root/$name"
done <<< "$repo_listing"

# Belt-and-braces: remove whatever is there (a symlink included) so the
# redirect below creates a fresh regular file and can never follow a link
# into the workspace root.
rm -f "$root/activate.sh"
worktree_activate_script "$task" > "$root/activate.sh"
chmod +x "$root/activate.sh"

echo "worktree_create: ready at $root"
echo " activate with: source $root/activate.sh"
}

# worktree_repair_activate [task-name ...]
# Opt-in, never called automatically. Converts a bundle activate.sh that is a
# SYMLINK (pre-#469 damage: it pointed at the workspace-root activate.sh, so
# every bundle followed whichever task wrote the root last) into a regenerated
# regular file. With no arguments, scans every bundle under $PYAUTO_WT_ROOT.
# Acts on bundle paths only: the link is removed, its target is never resolved
# or written, and the workspace root is never touched. Regular-file
# activate.sh entries are left as they are.
worktree_repair_activate() {
local -a tasks=("$@")
local b task
if [[ ${#tasks[@]} -eq 0 ]]; then
for b in "$PYAUTO_WT_ROOT"/*/; do
[[ -d "$b" ]] || continue
tasks+=("$(basename "$b")")
done
fi
local repaired=0
for task in "${tasks[@]}"; do
b="$(worktree_root_path "$task")"
if [[ ! -d "$b" ]]; then
echo "worktree_repair_activate: $b does not exist — skipping" >&2
continue
fi
[[ -L "$b/activate.sh" ]] || continue
rm -f "$b/activate.sh" || return 1
worktree_activate_script "$task" > "$b/activate.sh" || return 1
chmod +x "$b/activate.sh"
echo "worktree_repair_activate: regenerated $b/activate.sh"
repaired=$((repaired + 1))
done
echo "worktree_repair_activate: $repaired bundle activate.sh file(s) repaired"
}

# worktree_add_repo <task-name> <repo>
# Attaches a new repo worktree to an existing task root. Used to bring a
# workspace repo into a task that previously only claimed library repos
Expand Down
123 changes: 123 additions & 0 deletions tests/test_worktree_activate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
"""worktree_create never writes through a symlinked activate.sh (#469).

The sibling-symlink loop used to link the workspace root's activate.sh into
the bundle, and the bundle's activate.sh was then written THROUGH that link,
clobbering the unversioned root file and retargeting every earlier bundle.
Hermetic: everything lives under tmp_path; the real workspace is never read
or written.
"""

import os
import subprocess
from pathlib import Path

import pytest


WORKTREE_SH = Path(__file__).resolve().parents[1] / "bin/worktree.sh"
ROOT_ACTIVATE = b"# fabricated root activate.sh - must never change\nexport FAKE_ROOT=1\n"


def _git(*args):
subprocess.run(["git", *map(str, args)], check=True, capture_output=True)


@pytest.fixture
def workspace(tmp_path):
main = tmp_path / "main"
mind = main / "PyAutoMind"
mind.mkdir(parents=True)
(mind / "repos.yaml").write_text("repos:\n Demo:\n path: Demo\n")
(main / "activate.sh").write_bytes(ROOT_ACTIVATE)
demo = main / "Demo"
demo.mkdir()
remote = tmp_path / "remote.git"
_git("init", "--bare", remote)
_git("init", "-b", "main", demo)
_git("-C", demo, "config", "user.email", "test@example.com")
_git("-C", demo, "config", "user.name", "Test")
(demo / "README.md").write_text("demo\n")
_git("-C", demo, "add", "README.md")
_git("-C", demo, "commit", "-m", "initial")
_git("-C", demo, "remote", "add", "origin", remote)
_git("-C", demo, "push", "-u", "origin", "main")

bundles = tmp_path / "bundles"
env = os.environ | {
"PYAUTO_MAIN": str(main),
"PYAUTO_WT_ROOT": str(bundles),
"PYAUTO_WT_FORCE": "1",
}
return main, bundles, env


def _run(env, cmd):
result = subprocess.run(
["bash", "-c", f'source "{WORKTREE_SH}"; {cmd}'],
env=env, capture_output=True, text=True,
)
assert result.returncode == 0, result.stderr
return result


def _assert_own_activate(bundle, task):
activate = bundle / "activate.sh"
assert not activate.is_symlink()
assert activate.is_file()
text = activate.read_text()
assert f'export PYAUTO_TASK="{task}"' in text
# The bundle's own library paths are on its PYTHONPATH. PyAutoNerves (an
# organ, first in PYAUTO_LIBS) stands in for the library list: satellite
# repo names in organ code trip the tenant firewall
# (PyAutoMind/scripts/repos_sync.py).
assert f'export PYAUTO_ROOT="{bundle}"' in text
assert f'export PYTHONPATH="{bundle}/PyAutoNerves:' in text


def test_create_leaves_root_activate_untouched(workspace, tmp_path):
main, bundles, env = workspace
_run(env, "worktree_create task Demo")
bundle = bundles / "task"
assert tmp_path in bundle.parents
assert (main / "activate.sh").read_bytes() == ROOT_ACTIVATE
_assert_own_activate(bundle, "task")
assert (bundle / ".pyauto-root").is_file()
assert not (bundle / ".pyauto-root").is_symlink()


def test_second_bundle_does_not_retarget_first(workspace):
main, bundles, env = workspace
_run(env, "worktree_create first Demo")
_run(env, "worktree_create second Demo")
assert (main / "activate.sh").read_bytes() == ROOT_ACTIVATE
_assert_own_activate(bundles / "first", "first")
_assert_own_activate(bundles / "second", "second")


def test_repair_converts_symlinked_bundle_activate(workspace):
main, bundles, env = workspace
_run(env, "worktree_create task Demo")
bundle = bundles / "task"
# Recreate the pre-fix damage: the bundle's activate.sh is a link to root.
(bundle / "activate.sh").unlink()
(bundle / "activate.sh").symlink_to(main / "activate.sh")

_run(env, "worktree_repair_activate task")

assert (main / "activate.sh").read_bytes() == ROOT_ACTIVATE
_assert_own_activate(bundle, "task")


def test_repair_defaults_to_every_bundle(workspace):
main, bundles, env = workspace
_run(env, "worktree_create one Demo")
_run(env, "worktree_create two Demo")
for task in ("one", "two"):
(bundles / task / "activate.sh").unlink()
(bundles / task / "activate.sh").symlink_to(main / "activate.sh")

_run(env, "worktree_repair_activate")

assert (main / "activate.sh").read_bytes() == ROOT_ACTIVATE
_assert_own_activate(bundles / "one", "one")
_assert_own_activate(bundles / "two", "two")
Loading