From aad4eea4b9ae067a9c629677f93aaa1abf4b981e Mon Sep 17 00:00:00 2001 From: rebuildup Date: Mon, 30 Nov 2026 14:40:28 +0900 Subject: [PATCH] =?UTF-8?q?fix(#282):=20POSIX=20=E3=81=AE=20machine-global?= =?UTF-8?q?=20mise=20config=20/=20lock=20=E3=82=92=20symlink=20=E3=81=A7?= =?UTF-8?q?=E3=81=AF=E3=81=AA=E3=81=8F=20copy=20=E3=81=AB=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ADR-0016 §2 は POSIX で `~/.config/mise/config.toml` と `mise.lock` を配備用 チェックアウト内の tracked file への symlink にしていた。しかし symlink だと machine 側の mise 書き込みが配備用チェックアウトに落ちる。 - `mise use -g ` は global config を書き換える → 配備用チェックアウトの `mise.global.toml` が変更される (実測: `yq = "4.54.3"` の追加)。 - `MISE_LOCKED` を効かせない install は global lock を書き換える → `mise.global.lock` が変更される (実測: 約 700 行の差分)。 - 配備用チェックアウトが dirty になると ADR-0016 §3 の規則で converge が skipped になり、以降 main を取り込まない。macOS / Ubuntu-WSL の両方で 実際にこの状態になっていた。 Windows と同じ copy 方式に統一する。 - config と lock を copy する (一時ファイルへ書いてから置換)。 - lock が参照する sidecar (`aube = { path = ".mise/locks/mise.global/..." }`) を lock と同じ directory へ copy する。 - 既存の symlink は managed marker を持つ場合だけ copy へ移行する。 - user content を持つ config は従来どおり拒否、0 bytes の config は採用。 - backup は直近 5 件に制限し、無制限増加を止める。 - `scripts/converge.sh` の dsh-declaration 判定は readlink ではなく copy の 内容を比較する (symlink 方式では同じファイル同士を比較するため常に false)。 - ADR-0016 に §2a (copy 方式の理由) と §2b (移行手順) を追加。 `scripts/test-apply-global-mise.sh` で実ファイルを検証し、CI に追加。 Co-Authored-By: Claude Code --- components/hoxay/install.sh | 12 ++- docs/adr/ADR-0016.md | 28 +++++- scripts/apply-global-mise.sh | 107 ++++++++++++++++----- scripts/ci.sh | 8 ++ scripts/converge.sh | 32 ++++--- scripts/test-apply-global-mise.sh | 152 ++++++++++++++++++++++++++++++ 6 files changed, 297 insertions(+), 42 deletions(-) create mode 100755 scripts/test-apply-global-mise.sh diff --git a/components/hoxay/install.sh b/components/hoxay/install.sh index 6affda06..81d44e0c 100755 --- a/components/hoxay/install.sh +++ b/components/hoxay/install.sh @@ -71,7 +71,7 @@ log "building $name ($commit)" # `mise install --locked` must see only the checkout's own mise.toml / # mise.lock. On managed machines the user-global config -# (~/.config/mise/config.toml, symlinked to pc-setup's mise.global.toml) +# (~/.config/mise/config.toml, a copy of pc-setup's mise.global.toml) # is also discovered as an ancestor config and pins machine-global tools # that are absent from the repo lockfile, failing --locked installs # (rebuildup/pc-setup#245). Point the global config at an empty file and @@ -80,10 +80,12 @@ log "building $name ($commit)" MISE_EMPTY_GLOBAL="$STATE_DIR/mise-empty-global.toml" : > "$MISE_EMPTY_GLOBAL" # mise compares the CANONICAL path of a discovered config file against the -# ignore list, so a config that is symlinked into the user config dir -# (e.g. ~/.config/mise/config.toml -> pc-setup's mise.global.toml) has to be -# ignored by its real path too — excluding the directory alone would not -# keep the global pins out of the build (rebuildup/pc-setup#245). +# ignore list. The global config is a copy of the repository's config, so the +# directory exclusion is enough for a managed machine; a config that is +# symlinked into the user config dir (a machine that still uses the old +# symlink layout) has to be ignored by its real path too — excluding the +# directory alone would not keep the global pins out of the build +# (rebuildup/pc-setup#245). # ~/.tool-versions is read by mise's ancestor discovery as well, and the # managed state dir lives under the home directory — a tool pinned there # that the repo lockfile does not know would fail `--locked` too. diff --git a/docs/adr/ADR-0016.md b/docs/adr/ADR-0016.md index 907eb9c9..559c8eca 100644 --- a/docs/adr/ADR-0016.md +++ b/docs/adr/ADR-0016.md @@ -36,7 +36,29 @@ converge は dotfiles の端末定義も毎 tick で各端末へ反映する。I - 作業チェックアウト(`~/src/pc-setup` など)は配備に使わない。作業ブランチが何であっても、配備される内容は main に限定される。 - `~/.local/libexec/pc-setup` は実行ファイルや補助バイナリの置き場として使われてきた。チェックアウト(ソースツリー)を置くとバイナリ置き場と混ざるため使わない。 - `~/.local/state/pc-setup` は状態・ログ・ロックの置き場にする。ソースツリーは置かない。 -- `apply-global-mise.sh` は自身の位置から repo root を決めるため、converge は必ず `$DEPLOY/scripts/apply-global-mise.sh` を呼ぶ。これで `~/.config/mise/config.toml` と `mise.lock` は配備用チェックアウトを指す。 +- `apply-global-mise.sh` は自身の位置から repo root を決めるため、converge は必ず `$DEPLOY/scripts/apply-global-mise.sh` を呼ぶ。これで `~/.config/mise/config.toml` と `mise.lock` は配備用チェックアウトの**内容**と一致する。 + +### 2a. machine-global config は symlink ではなく copy + +`~/.config/mise/config.toml` と `~/.config/mise/mise.lock` は、配備用チェックアウト内の tracked file への **symlink ではなく copy** として置く(Windows 版 `apply-global-mise.ps1` と同じ方式)。 + +symlink にしていた consequence は次のとおりで、いずれも「配備用チェックアウトが dirty になる → converge が dirty を拒否して恒久的に skip する → 端末が main を取り込み続ける」という連鎖发生过。 + +- `mise use -g ` は global config に tool を書き込む。symlink だと実体 = 配備用チェックアウトの `mise.global.toml` が変更される(実測: `yq = "4.54.3"` の追加)。 +- `MISE_LOCKED` を効かせない `mise install` は global lock を書き換える。symlink だと実体 = 配備用チェックアウトの `mise.global.lock` が変更される(実測: 約 700 行の差分)。 +- 配備用チェックアウトが dirty になると §3 の規則で converge が skipped になる。 + +copy にすると、端末側の mise の書き込みは machine 自身の copy に留まり、配備用チェックアウトは dirty にならない。write は一時ファイルへ書いてから置換し、途中で中断されても truncated な config は残らない。copy の内容は常に配備用チェックアウトの main と一致し、次の tick で更新される。 + +同じ理由で、`~/.config/mise/.mise/locks/...` の sidecar も lock と同じ directory へ copy する(lock が参照する path が lock file からの相対 path であるため)。 + +### 2b. 移行 + +旧 symlink で運用されている machine では、次の順で copy 方式へ移行する。 + +1. `~/.config/mise/config.toml` と `mise.lock` の symlink 先が配備用チェックアウト(またはその中の `mise.global.toml` / `mise.global.lock`)である場合だけ移行対象とする。別の場所を指す symlink は user の設定なので触らない。 +2. 配備用チェックアウトの dirty な tracked file は、差分を確認したうえで `git restore` する。ADR-0016 §3 の「dirty は拒否してスキップ」は人が判断する差分を自動で消さないため、この修復は明示的な操作として行う。 +3. `apply-global-mise.sh` を実行すると、symlink が copy に置き換わる(symlink の中身が managed marker を持つ場合のみ)。以降の書き込みは copy に留まる。 ### 3. 安全性 @@ -46,7 +68,7 @@ converge は dotfiles の端末定義も毎 tick で各端末へ反映する。I - **冪等**:2 回目の実行は変更なしで exit 0 になる。 - **同時実行ガード**:`~/.local/state/pc-setup/converge.lock/` を mkdir で取る(macOS に flock はない)。pid を書き、pid が死んでいれば stale として退ける。退ける時は rename してから削除し、別プロセスが新しく取った lock を消さないようにする。 - **再実行の境界**:同期後に配備済みの `converge.sh` へ `exec` する。bash はスクリプトを逐次読むため、実行中に自分の本体が差し替わると壊れる。`exec` は pid を保つので、同じ lock を引き継げる。 -- **巻き戻しをしない理由**:`apply-global-mise.sh` が config のリンクを張り替えた後で install が失敗しても、ロールバックはしない。mise の install は追加的で、旧バージョンはディスクに残る。巻き戻すと、この処理が取り除こうとしている dirty な作業チェックアウトへのリンクが戻ってしまう。欠けているツールは status に載るので、次の実行で再試行される。 +- **巻き戻しをしない理由**:`apply-global-mise.sh` が config を書き換えた後で install が失敗しても、ロールバックはしない。mise の install は追加的で、旧バージョンはディスクに残る。欠けているツールは status に載るので、次の実行で再試行される。 - **終了コード**:0 は全ステップ成功、1 は失敗あり、3 は skipped または pending あり、75 は lock 競合。 ### 4. スケジューラ:macOS LaunchAgent(StartInterval 3600、RunAtLoad) @@ -124,7 +146,7 @@ dsh は未マージの PR #154 にしか宣言がない。配備先が main に - macOS では、ターミナルを開かなくても定期的に main へ追従する。 - 失敗は status に残り、次の実行で再試行される。 - 旧バイナリを保持する古いプロセスは検出されるが、止めはしない。止めるかどうかは人が決める。 -- `apply-global-mise.sh` の config 張り替えは `rm` の後に `ln -s` を行うため、ごく短い間は config が存在しない。converge の側ではこの窓を塞げない。別途改善が必要である。 +- `apply-global-mise.sh` の config 書き込みは一時ファイルへ書いてから置換する(§2a)。対象は旧内容か新内容のどちらかで、truncated な config は残らない。 - WSL2 と Windows のスケジューラは実装した。ただし各 OS の実機での発火は未検証(後続の cross-OS 検証で確認する)。ADR の方針(採用は train、配備は converge、シェルは activate のみ)は両 OS に適用する。 ## Re-evaluation triggers diff --git a/scripts/apply-global-mise.sh b/scripts/apply-global-mise.sh index f392cd95..8fdf1e21 100755 --- a/scripts/apply-global-mise.sh +++ b/scripts/apply-global-mise.sh @@ -27,30 +27,94 @@ fi mkdir -p "$(dirname "$target_config")" -if [[ -L "$target_config" ]]; then - rm "$target_config" -elif [[ -e "$target_config" ]]; then - if ! head -n 1 "$target_config" | grep -Fxq "$managed_marker"; then - printf 'refusing to overwrite unmanaged mise global config: %s\n' "$target_config" >&2 - exit 1 +# The machine-global config is a *copy* of the repository's config, not a +# symlink into it. +# +# A symlink made the deployment checkout part of the machine's mutable mise +# state: `mise use -g ` writes into the global config, and an install +# that runs without MISE_LOCKED rewrites the global lock. Through a symlink +# both writes landed in the deployment checkout's tracked files, which made the +# checkout dirty, and a dirty deployment checkout is refused by converge +# (ADR-0016 §3). The result was a machine that stopped adopting `main` and +# stayed on whatever state it had. Copying keeps the checkout pristine and +# gives the machine its own writable copy, which is what Windows already did. +# +# The rules for the target are the same as on Windows +# (scripts/mise-config-target.ps1): a config that holds content is only +# overwritten when this repository wrote it, a zero-length config is adopted, +# and the write replaces the target as a whole. +config_state() { + local path="$1" first_line + if [[ ! -e "$path" ]]; then + printf 'missing' + return 0 + fi + if [[ ! -s "$path" ]]; then + printf 'empty' + return 0 fi - rm "$target_config" + first_line="$(head -n 1 "$path" 2>/dev/null || true)" + if [[ "$first_line" == "$managed_marker" ]]; then + printf 'managed' + return 0 + fi + printf 'unmanaged' +} + +config_state_value="$(config_state "$target_config")" +if [[ "$config_state_value" == "unmanaged" ]]; then + printf 'refusing to overwrite unmanaged mise global config: %s\n' "$target_config" >&2 + exit 1 +fi + +if [[ -e "$target_config" ]] && ! cmp -s "$source_config" "$target_config"; then + config_backup="${target_config}.pc-setup-backup-$(date +%Y%m%d-%H%M%S)" + cp -p "$target_config" "$config_backup" + printf 'backup %s\n' "$config_backup" fi -ln -s "$source_config" "$target_config" -printf 'linked %s -> %s\n' "$target_config" "$source_config" +# Replace the target as a whole: a truncated config would leave every mise shim +# unusable, and the managed marker guard above would refuse to repair it. +config_tmp="${target_config}.pc-setup-tmp.$$" +cp "$source_config" "$config_tmp" +mv -f "$config_tmp" "$target_config" +printf 'applied %s <- %s\n' "$target_config" "$source_config" -if [[ -f "$source_lock" ]]; then - if [[ -L "$target_lock" ]]; then - rm "$target_lock" - elif [[ -e "$target_lock" ]]; then - printf 'refusing to overwrite unmanaged mise global lock: %s\n' "$target_lock" >&2 - exit 1 +# The backup copies above are kept per content change, which made the config +# directory grow without bound. Keep the most recent ones and drop the rest. +config_backup_keep="${PC_SETUP_MISE_CONFIG_BACKUP_KEEP:-5}" +# macOS ships bash 3.2, which has no mapfile, so the list is read with a loop. +config_backup_index=0 +while IFS= read -r stale_backup; do + [[ -n "$stale_backup" ]] || continue + config_backup_index=$((config_backup_index + 1)) + if [[ "$config_backup_index" -gt "$config_backup_keep" ]]; then + rm -f "$stale_backup" + printf 'pruned %s\n' "$stale_backup" fi +done < <(find "$(dirname "$target_config")" -maxdepth 1 -name "$(basename "$target_config").pc-setup-backup-*" -type f 2>/dev/null | sort -r) - ln -s "$source_lock" "$target_lock" - printf 'linked %s -> %s\n' "$target_lock" "$source_lock" -elif [[ -L "$target_lock" && "$(readlink "$target_lock")" == "$source_lock" ]]; then +if [[ -f "$source_lock" ]]; then + lock_tmp="${target_lock}.pc-setup-tmp.$$" + cp "$source_lock" "$lock_tmp" + mv -f "$lock_tmp" "$target_lock" + printf 'applied %s <- %s\n' "$target_lock" "$source_lock" + + # The lock references its dependency sidecars by a path relative to the lock + # file (`aube = { path = ".mise/locks/mise.global//" }`), so + # the sidecar tree has to sit next to the lock. The lock is a copy now, so + # mise resolves those paths against the config directory. + source_sidecars="$repo_root/.mise" + target_sidecars="$(dirname "$target_lock")/.mise" + if [[ -d "$source_sidecars" ]]; then + sidecars_tmp="${target_sidecars}.pc-setup-tmp.$$" + rm -rf "$sidecars_tmp" + cp -R "$source_sidecars" "$sidecars_tmp" + rm -rf "$target_sidecars" + mv -f "$sidecars_tmp" "$target_sidecars" + printf 'applied %s <- %s\n' "$target_sidecars" "$source_sidecars" + fi +elif [[ -L "$target_lock" ]]; then rm "$target_lock" printf 'removed stale managed mise global lock link: %s\n' "$target_lock" fi @@ -62,9 +126,10 @@ export MISE_HTTP_RETRIES="${MISE_HTTP_RETRIES:-5}" # it installs from, so the unlocked `mise install gh` bootstrap below replaces the # committed lock before `--locked` is ever consulted (observed: 38393 -> 75443 # bytes) and the machine installs a different tool set than the repository pins. -# On POSIX the lock is a symlink into the checkout, so that rewrite also dirties -# the work tree. Lock reads for the rest of the run when a committed lock exists; -# a machine with no committed lock must still be able to create one. +# The lock the machine reads is a copy of the repository's lock, so that rewrite +# now lands in the copy and the repository's lock stays untouched. Lock reads for +# the rest of the run when a committed lock exists; a machine with no committed +# lock must still be able to create one. if [[ -f "$source_lock" ]]; then export MISE_LOCKED=1 fi diff --git a/scripts/ci.sh b/scripts/ci.sh index 31f0e080..00d3a26b 100755 --- a/scripts/ci.sh +++ b/scripts/ci.sh @@ -154,6 +154,14 @@ if stale: raise SystemExit(f"ADR index references absent files: {', '.join(stale)}") PY +printf 'Checking the machine-global mise apply rules...\n' +# scripts/apply-global-mise.sh has to write copies of the repository config and +# lock, never symlinks into the checkout. A symlink let `mise use -g` and an +# unlocked install modify the deployment checkout, and a dirty deployment +# checkout is refused by converge, so the machine stopped adopting `main` +# (rebuildup/pc-setup#282). +bash scripts/test-apply-global-mise.sh + printf 'Checking macOS container runtime contract...\n' grep -Fq "bash \"\$repo_root/platforms/macos/containers/install.sh\"" scripts/apply-platform.sh grep -Fq "mise -C \"\$HOME\" bootstrap packages apply" scripts/apply-global-mise.sh diff --git a/scripts/converge.sh b/scripts/converge.sh index 8ab1ce2f..dbf19cf1 100755 --- a/scripts/converge.sh +++ b/scripts/converge.sh @@ -210,26 +210,32 @@ apply_mise() { return 0 fi - local previous_target="" rc=0 lock_before="" lock_after="" - previous_target="$(readlink "$mise_config_path" 2>/dev/null || true)" + local previous_config="" rc=0 lock_before="" lock_after="" + # The machine-global config is a copy of the repository's config, so the + # previous content has to be read before the apply replaces it. It is what + # the dsh-declaration check below compares against the deployed config. + if [[ -f "$mise_config_path" ]]; then + previous_config="$(cat "$mise_config_path" 2>/dev/null || true)" + fi lock_before="$(git -C "$deploy_dir" hash-object mise.global.lock 2>/dev/null || true)" # apply-global-mise.sh runs one unlocked `mise install gh` before its auth check. - # Without MISE_LOCKED=1, mise writes lock-time provenance into the linked - # mise.global.lock (observed: ~650 added lines in the deployment checkout). - # MISE_LOCKED=1 keeps that write from happening. The lock hash is compared - # afterwards as a safety net; the script reports a change and never restores it. + # Without MISE_LOCKED=1, mise writes lock-time provenance into the machine's + # lock (observed: ~650 added lines in the deployment checkout while the lock + # was a symlink into it). The lock is a copy now, so that rewrite lands in the + # copy; MISE_LOCKED=1 keeps it from happening at all. The lock hash is + # compared afterwards as a safety net; the script reports a change and never + # restores it. # - # apply-global-mise.sh links the config into the deployment checkout and then + # apply-global-mise.sh copies the config from the deployment checkout and then # runs `mise install --locked`. A failed install is not rolled back: mise # installs are additive, the previous versions stay on disk, and the status - # report lists what is missing. Rolling the link back would reintroduce the - # dirty work checkout that this step exists to remove. + # report lists what is missing. if MISE_LOCKED=1 "$deploy_dir/scripts/apply-global-mise.sh" >>"$log_file" 2>&1; then if grep -Fq 'using committed mise global lock' "$log_file"; then - record mise-apply ok "config linked to deploy checkout; locked install ok" + record mise-apply ok "config applied from deploy checkout; locked install ok" else - record mise-apply ok "config linked to deploy checkout; install ok without a committed lock" + record mise-apply ok "config applied from deploy checkout; install ok without a committed lock" fi else rc=$? @@ -242,8 +248,8 @@ apply_mise() { fi # Known caveat (ADR-0016): npm:@deepseek-ai/dsh exists only in unmerged PR #154. - if [[ -n "$previous_target" && -f "$previous_target" ]] \ - && grep -Fq 'deepseek-ai/dsh' "$previous_target" \ + if [[ -n "$previous_config" ]] \ + && grep -Fq 'deepseek-ai/dsh' <<<"$previous_config" \ && ! grep -Fq 'deepseek-ai/dsh' "$deploy_dir/mise.global.toml"; then record dsh-declaration pending "npm:@deepseek-ai/dsh is declared only in unmerged PR #154; dsh is no longer managed by main" fi diff --git a/scripts/test-apply-global-mise.sh b/scripts/test-apply-global-mise.sh new file mode 100755 index 00000000..0eaa6577 --- /dev/null +++ b/scripts/test-apply-global-mise.sh @@ -0,0 +1,152 @@ +#!/usr/bin/env bash +# Behavioural check for scripts/apply-global-mise.sh. +# +# The rule that matters: the machine-global mise config and lock are copies of +# the repository's files, never symlinks into the checkout. A symlink made the +# deployment checkout part of the machine's mutable mise state, and a dirty +# deployment checkout is refused by converge, so the machine stopped adopting +# `main` (rebuildup/pc-setup#282). +set -euo pipefail + +repo_root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd -P)" +apply_script="$repo_root/scripts/apply-global-mise.sh" + +root="$(mktemp -d)" +trap 'rm -rf "$root"' EXIT + +fake_bin="$root/bin" +mkdir -p "$fake_bin" "$root/home" + +# A fake mise that records every invocation and succeeds. The apply only needs +# a working `mise` to reach the point where the config and lock are written. +cat > "$fake_bin/mise" <<'SH' +#!/usr/bin/env bash +set -euo pipefail +printf '%s\n' "$*" >> "$FAKE_MISE_LOG" +case "$*" in + *"rustup default"*) printf 'stable\n' ;; +esac +exit 0 +SH +chmod +x "$fake_bin/mise" + +export PATH="$fake_bin:$PATH" +export HOME="$root/home" +export MISE_GLOBAL_CONFIG_FILE="$root/home/.config/mise/config.toml" +export FAKE_MISE_LOG="$root/mise.log" +export MISE_GITHUB_TOKEN="gho_ci_test_token" +export PC_SETUP_MISE_RETRY_DELAY_SECONDS=0 + +config_target="$MISE_GLOBAL_CONFIG_FILE" +lock_target="$(dirname "$config_target")/mise.lock" + +source_config="$repo_root/mise.global.toml" +source_lock="$repo_root/mise.global.lock" + +config_hash_before="$(cksum < "$source_config")" +lock_hash_before="$(cksum < "$source_lock")" + +printf '%s\n' '--- first apply ---' +"$apply_script" > "$root/apply.log" 2>&1 || { + cat "$root/apply.log" >&2 + printf 'apply-global-mise.sh failed\n' >&2 + exit 1 +} + +if [[ -L "$config_target" ]]; then + printf 'the machine-global config must not be a symlink into the checkout\n' >&2 + exit 1 +fi +if [[ -L "$lock_target" ]]; then + printf 'the machine-global lock must not be a symlink into the checkout\n' >&2 + exit 1 +fi +if ! cmp -s "$source_config" "$config_target"; then + printf 'the machine-global config does not match the repository config\n' >&2 + exit 1 +fi +if ! cmp -s "$source_lock" "$lock_target"; then + printf 'the machine-global lock does not match the repository lock\n' >&2 + exit 1 +fi +printf 'ok config and lock are copies of the repository files\n' + +# The lock references its dependency sidecars relative to the lock file, so the +# sidecar tree has to sit next to the copied lock. +if [[ ! -d "$(dirname "$lock_target")/.mise/locks" ]]; then + printf 'the dependency sidecars were not copied next to the lock\n' >&2 + exit 1 +fi +printf 'ok dependency sidecars sit next to the copied lock\n' + +# The repository's tracked files must not be touched by the apply. +if [[ "$config_hash_before" != "$(cksum < "$source_config")" ]]; then + printf 'the apply modified the repository config\n' >&2 + exit 1 +fi +if [[ "$lock_hash_before" != "$(cksum < "$source_lock")" ]]; then + printf 'the apply modified the repository lock\n' >&2 + exit 1 +fi +printf 'ok the repository files are untouched\n' + +# A second apply must not change anything and must not add a backup. +backup_count_before="$(find "$(dirname "$config_target")" -maxdepth 1 -name 'config.toml.pc-setup-backup-*' -type f | wc -l | tr -d ' ')" +"$apply_script" > "$root/apply2.log" 2>&1 || { + cat "$root/apply2.log" >&2 + printf 'the second apply failed\n' >&2 + exit 1 +} +backup_count_after="$(find "$(dirname "$config_target")" -maxdepth 1 -name 'config.toml.pc-setup-backup-*' -type f | wc -l | tr -d ' ')" +if [[ "$backup_count_before" != "$backup_count_after" ]]; then + printf 'the second apply created an extra backup (%s -> %s)\n' "$backup_count_before" "$backup_count_after" >&2 + exit 1 +fi +printf 'ok a repeated apply changes nothing\n' + +# A config the user wrote is never replaced. +printf 'user = true\n' > "$config_target" +if "$apply_script" > "$root/apply3.log" 2>&1; then + printf 'an unmanaged machine-global config must be refused\n' >&2 + exit 1 +fi +if ! grep -Fxq 'user = true' "$config_target"; then + printf 'the refused config was modified\n' >&2 + exit 1 +fi +printf 'ok an unmanaged config is refused and left untouched\n' + +# A zero-length config is the residue of an interrupted write. It has to be +# adopted, otherwise the machine cannot repair itself. +: > "$config_target" +"$apply_script" > "$root/apply4.log" 2>&1 || { + cat "$root/apply4.log" >&2 + printf 'an empty machine-global config must be adoptable\n' >&2 + exit 1 +} +if ! cmp -s "$source_config" "$config_target"; then + printf 'the empty config was not replaced by the repository config\n' >&2 + exit 1 +fi +printf 'ok an empty config is adopted\n' + +# The legacy symlink state is migrated to a copy. A symlink that points at a +# file this repository wrote carries the managed marker, so it is ours. +ln -sfn "$source_config" "$config_target" +ln -sfn "$source_lock" "$lock_target" +"$apply_script" > "$root/apply5.log" 2>&1 || { + cat "$root/apply5.log" >&2 + printf 'the legacy symlink state must be migrated\n' >&2 + exit 1 +} +if [[ -L "$config_target" ]] || [[ -L "$lock_target" ]]; then + printf 'the legacy symlinks were not replaced by copies\n' >&2 + exit 1 +fi +if ! cmp -s "$source_config" "$config_target"; then + printf 'the migrated config does not match the repository config\n' >&2 + exit 1 +fi +printf 'ok the legacy symlink state is migrated to copies\n' + +printf 'ok machine-global mise apply rules verified\n'