#!/bin/sh
case "$#" in
  2) creator_source=$1; creator_target=$2 ;;
  *) exit 64 ;;
esac
shift 2
/usr/bin/env -i PATH=/usr/bin:/bin LC_ALL=C TMPDIR=/tmp \
  GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null \
  GIT_ATTR_NOSYSTEM=1 GIT_PAGER=cat GIT_OPTIONAL_LOCKS=0 \
  /bin/bash --noprofile --norc -s -- "$creator_source" "$creator_target" <<'DEVLAB_GIT_CREATOR'
set -euo pipefail
test "$#" -eq 2 || exit 64
source_input=$1
target_input=$2

export PATH=/usr/bin:/bin
export LC_ALL=C
export TMPDIR=/tmp
export GIT_CONFIG_NOSYSTEM=1
export GIT_CONFIG_GLOBAL=/dev/null
export GIT_CONFIG_SYSTEM=/dev/null
export GIT_ATTR_NOSYSTEM=1
export GIT_PAGER=cat
export GIT_OPTIONAL_LOCKS=0
export GIT_TERMINAL_PROMPT=0
export GIT_MERGE_AUTOEDIT=no
export GIT_AUTHOR_NAME='Dev Lab'
export GIT_AUTHOR_EMAIL='dev-lab@example.invalid'
export GIT_COMMITTER_NAME='Dev Lab'
export GIT_COMMITTER_EMAIL='dev-lab@example.invalid'

# DEVLAB_HISTORY_CONTRACT_BEGIN
DEVLAB_HISTORY_CONTRACT='SCHEMA|1
IDENTITY|Dev Lab|dev-lab@example.invalid
BRANCH|main
SOURCE|README.md|f35c009dec20278b5df50693da43c54ae4b8e228a173fcb9f59e1b3aea5a2dc5|0
SOURCE|config/bashrc.dev-lab|882ef7d9502cc5d8145c5f8b0321dbd17b0c7d431db9421cbe390f1f86e94e12|0
SOURCE|config/env.dev-lab|00aec1c6d894bb334bef59a03fad5d6392f0a2c1e5517cb89b788c4ad72b390a|0
SOURCE|data/access.log|1df6e34d28da0483cc1e62523acc777597ce3178a77d089bf7ffcabb4b0ab254|0
SOURCE|data/health-events.log|fe0119bfe33a541d816c33740f38b32280896ffce54bd7841985c7b1e6bd06e5|0
SOURCE|notes/linux-notes.md|e54276c7c8a2c2818aa48891030e31f7712fde6f5f42b70257c1a3e9582086dc|0
SOURCE|notes/shell-notes.md|d937ab49e62af7d2f5bbb92f457476afa2a9632a9d1c68135a30a98ffe9aed7d|0
SOURCE|output/health-report.txt|32c0404e72a9692ecac0a2877a98b8ae09fa90090c2af9dbdbc3c9da18a1cf1e|0
SOURCE|output/summary.txt|fd45eca2af0f073a58b8a6291f64804b6e5eade93aa773c6d5e49832edd65591|0
SOURCE|scripts/health-check.sh|5fa78e594202c38e9004b65fe69c0281c509b00ad7c10b0c48cfd2b383720e4f|1
SOURCE|scripts/permission-demo.sh|3f54de46ae71a2b3af65bd3aeaf9c28bbf91409abc67c87f718c16c4961445e8|1
SOURCE|scripts/report.sh|eb8bf1b36e7bd043f64665680b71d086a0a9aeb3c5b5a045fe2fb3e40bb52e37|1
COMMIT|C1|chore: establish dev-lab baseline||source|2000-01-01T00:00:01Z
CHANGE|C1|.gitignore|04950b968fe37d63b990bf4d5cb3a5b5684fd59320205c10282df38a8120fdbe|0
COMMIT|C2|docs: add git practice journal|C1|C1|2000-01-01T00:00:02Z
CHANGE|C2|notes/git-notes.md|5250834cba284fc1d9017836f1ac056432d341020531b6c484fa6da4ae1834be|0
COMMIT|C3|docs: record branch workflow|C2|C2|2000-01-01T00:00:03Z
CHANGE|C3|notes/git-notes.md|9796b2da5468c67a7b4274d063d7a9fb10b417c1a8caabc6ae10ac095047f342|0
COMMIT|C4|merge: integrate git workflow|C2,C3|C3|2000-01-01T00:00:04Z
COMMIT|C5|docs: record main troubleshooting step|C4|C4|2000-01-01T00:00:05Z
CHANGE|C5|notes/git-notes.md|91407f0b23c8f27c9baee81d07d736673c2ff49d661379e63caa0c1479cd5860|0
COMMIT|C6|docs: record branch troubleshooting step|C4|C4|2000-01-01T00:00:06Z
CHANGE|C6|notes/git-notes.md|f0aa82ddc8dad5b9f1072fc48f2b376385680a214659cb83cc596bfe03583022|0
COMMIT|C7|merge: resolve troubleshooting guide|C5,C6|C5|2000-01-01T00:00:07Z
CHANGE|C7|notes/git-notes.md|42fa2adba9c1aa31981c95e6389c452d30754840095f9dbe45317994291a2677|0
COMMIT|C8|docs: complete local git notes|C7|C7|2000-01-01T00:00:08Z
CHANGE|C8|README.md|f7afcc991b7f17de1d3b705733e94d0ceb1ede0d614e9fba7435f89390ea85ec|0
CHANGE|C8|notes/git-notes.md|ce1ca9eabafe913d81160cb7de9871f4dc9647c1568262ffe2ed353630a6a79c|0
FINAL|C8|main||'
# DEVLAB_HISTORY_CONTRACT_END

host_platform=$(/usr/bin/uname -s)
publication_platform=$host_platform
source_path=
source_identity=
pinned_parent=
pinned_parent_identity=
target_basename=
target_path=
staging_path=
staging_basename=
staging_identity=
staging_binding_active=0
pending_signal=
c1_oid=
c2_oid=
c3_oid=
c4_oid=
c5_oid=
c6_oid=
c7_oid=
c8_oid=

die() {
  /bin/echo "git-local creator: $*" >&2
  exit 1
}

path_identity() {
  case "$host_platform" in
    Darwin) /usr/bin/stat -f '%d:%i' "$1" ;;
    Linux) /usr/bin/stat -c '%d:%i' "$1" ;;
    *) die "unsupported platform: $host_platform" ;;
  esac
}

sha256_file() {
  local output digest remainder
  if [[ -x /usr/bin/sha256sum ]]; then
    output=$(/usr/bin/sha256sum "$1")
  elif [[ -x /usr/bin/shasum ]]; then
    output=$(/usr/bin/shasum -a 256 "$1")
  else
    die 'no SHA-256 utility is available'
  fi
  read -r digest remainder <<< "$output"
  /usr/bin/printf '%s\n' "$digest"
}

run_git() {
  local repository=$1
  shift
  /usr/bin/git \
    -c core.hooksPath=/dev/null \
    -c core.fsmonitor=false \
    -c core.attributesFile=/dev/null \
    -c core.excludesFile=/dev/null \
    -c diff.external= \
    -c commit.gpgSign=false \
    -C "$repository" "$@"
}

run_git_at() {
  local reference_date=$1
  local repository=$2
  shift 2
  GIT_AUTHOR_DATE="$reference_date" GIT_COMMITTER_DATE="$reference_date" run_git "$repository" "$@"
}

sha256_git_blob() {
  local repository=$1
  local object_spec=$2
  local output digest remainder
  if [[ -x /usr/bin/sha256sum ]]; then
    output=$(run_git "$repository" cat-file blob "$object_spec" | /usr/bin/sha256sum)
  elif [[ -x /usr/bin/shasum ]]; then
    output=$(run_git "$repository" cat-file blob "$object_spec" | /usr/bin/shasum -a 256)
  else
    die 'no SHA-256 utility is available'
  fi
  read -r digest remainder <<< "$output"
  /usr/bin/printf '%s\n' "$digest"
}

verify_disk_file() {
  local root=$1
  local relative_path=$2
  local expected_hash=$3
  local expected_executable=$4
  local absolute_path="$root/$relative_path"
  [[ -f "$absolute_path" && ! -L "$absolute_path" ]] || die "invalid file: $relative_path"
  [[ "$(sha256_file "$absolute_path")" == "$expected_hash" ]] || die "wrong bytes: $relative_path"
  if [[ "$expected_executable" == 1 ]]; then
    [[ -x "$absolute_path" ]] || die "missing executable mode: $relative_path"
  else
    [[ ! -x "$absolute_path" ]] || die "unexpected executable mode: $relative_path"
  fi
}

require_linux_source() {
  local source_physical=$1
  local entry_count kind file_path expected_hash expected_executable unused1 unused2
  [[ -d "$source_physical" && ! -L "$source_physical" ]] || die 'Linux checkpoint source must be a real directory'
  for directory in config data notes output scripts; do
    [[ -d "$source_physical/$directory" && ! -L "$source_physical/$directory" ]] || die "invalid source directory: $directory"
  done
  entry_count=$(/usr/bin/find "$source_physical" -mindepth 1 -print | /usr/bin/wc -l)
  [[ "$entry_count" -eq 17 ]] || die 'Linux checkpoint source has missing or extra paths'
  while IFS='|' read -r kind file_path expected_hash expected_executable unused1 unused2; do
    [[ "$kind" == SOURCE ]] || continue
    verify_disk_file "$source_physical" "$file_path" "$expected_hash" "$expected_executable"
  done <<< "$DEVLAB_HISTORY_CONTRACT"
}

assert_parent_identity() {
  [[ -d "$pinned_parent" && ! -L "$pinned_parent" ]] || die 'target parent was replaced'
  [[ "$(path_identity "$pinned_parent")" == "$pinned_parent_identity" ]] || die 'target parent identity changed'
}

assert_owned_staging() {
  assert_parent_identity
  [[ -n "$staging_path" && "$staging_path" == "$pinned_parent/$staging_basename" ]] || die 'staging path escaped its parent'
  [[ -d "$staging_path" && ! -L "$staging_path" ]] || die 'owned staging was replaced'
  [[ "$(path_identity "$staging_path")" == "$staging_identity" ]] || die 'owned staging identity changed'
}

cleanup_owned_staging() {
  if [[ -z "${staging_path:-}" || -z "${staging_identity:-}" ]]; then
    return 0
  fi
  if [[ ! -e "$staging_path" && ! -L "$staging_path" ]]; then
    return 0
  fi
  if [[ -d "$staging_path" && ! -L "$staging_path" ]] &&
     [[ "$(path_identity "$staging_path")" == "$staging_identity" ]]; then
    /bin/rm -rf "$staging_path"
    return 0
  fi
  /bin/echo 'git-local creator: foreign staging replacement preserved' >&2
  return 0
}

handle_termination_signal() {
  local signal_name=$1
  if [[ "$staging_binding_active" == 1 ]]; then
    pending_signal=$signal_name
    return 0
  fi
  exit 70
}

finish_staging_identity_binding() {
  staging_binding_active=0
  if [[ -n "$pending_signal" ]]; then
    exit 70
  fi
}

create_owned_staging() {
  local parent=$1
  local basename=$2
  [[ "$parent" == "$pinned_parent" && "$basename" == "$target_basename" ]] || die 'staging request does not match pinned target'
  assert_parent_identity
  pending_signal=
  staging_binding_active=1
  if staging_path=$(/usr/bin/mktemp -d "$parent/.${basename}.devlab-staging.XXXXXX"); then :; else
    staging_binding_active=0
    [[ -z "$pending_signal" ]] || exit 70
    die 'could not create staging directory'
  fi
  staging_basename=${staging_path##*/}
  [[ "$staging_path" == "$parent/$staging_basename" ]] || die 'staging path is outside pinned parent'
  [[ -d "$staging_path" && ! -L "$staging_path" ]] || die 'staging acquisition failed'
  staging_identity=$(path_identity "$staging_path")
  [[ -n "$staging_identity" ]] || die 'staging identity is empty'
  finish_staging_identity_binding
  assert_owned_staging
}

copy_linux_source() {
  local kind file_path expected_hash expected_executable unused1 unused2
  assert_owned_staging
  /bin/mkdir "$staging_path/config" "$staging_path/data" "$staging_path/notes" "$staging_path/output" "$staging_path/scripts"
  while IFS='|' read -r kind file_path expected_hash expected_executable unused1 unused2; do
    [[ "$kind" == SOURCE ]] || continue
    /bin/cp -p "$source_path/$file_path" "$staging_path/$file_path"
  done <<< "$DEVLAB_HISTORY_CONTRACT"
  require_linux_source "$source_path"
  [[ "$(path_identity "$source_path")" == "$source_identity" ]] || die 'source identity changed during copy'
  require_linux_source "$staging_path"
  assert_owned_staging
  # Boundary: verified source copy
}

write_gitignore() {
  [[ ! -e "$1/.gitignore" && ! -L "$1/.gitignore" ]] || die 'unexpected .gitignore before C1'
  /usr/bin/printf '%s\n' '.DS_Store' '.tmp-*' > "$1/.gitignore"
  verify_disk_file "$1" '.gitignore' '04950b968fe37d63b990bf4d5cb3a5b5684fd59320205c10282df38a8120fdbe' 0
}

write_git_notes() {
  local repository=$1
  local state=$2
  local notes_path="$repository/notes/git-notes.md"
  [[ ! -L "$notes_path" ]] || die 'Git notes path was replaced by a symlink'
  case "$state" in
    C2)
      /bin/cat > "$notes_path" <<'DEVLAB_C2_NOTES'
# Git 练习记录

## 工作区模型

- 工作区保存当前文件。
- 暂存区保存下一次提交的候选快照。
- HEAD 指向当前分支的提交。

## 分支工作流

课程将在分支练习中补充这里。

## 排错顺序

课程将在冲突练习中补充这里。

## 安全恢复

课程将在恢复练习中补充这里。
DEVLAB_C2_NOTES
      verify_disk_file "$repository" 'notes/git-notes.md' '5250834cba284fc1d9017836f1ac056432d341020531b6c484fa6da4ae1834be' 0
      ;;
    C3)
      /bin/cat > "$notes_path" <<'DEVLAB_C3_NOTES'
# Git 练习记录

## 工作区模型

- 工作区保存当前文件。
- 暂存区保存下一次提交的候选快照。
- HEAD 指向当前分支的提交。

## 分支工作流

- 在功能分支完成独立修改。
- 合并前先确认当前分支和工作区状态。

## 排错顺序

课程将在冲突练习中补充这里。

## 安全恢复

课程将在恢复练习中补充这里。
DEVLAB_C3_NOTES
      verify_disk_file "$repository" 'notes/git-notes.md' '9796b2da5468c67a7b4274d063d7a9fb10b417c1a8caabc6ae10ac095047f342' 0
      ;;
    C5)
      /bin/cat > "$notes_path" <<'DEVLAB_C5_NOTES'
# Git 练习记录

## 工作区模型

- 工作区保存当前文件。
- 暂存区保存下一次提交的候选快照。
- HEAD 指向当前分支的提交。

## 分支工作流

- 在功能分支完成独立修改。
- 合并前先确认当前分支和工作区状态。

## 排错顺序

- 先查看状态与差异。

## 安全恢复

课程将在恢复练习中补充这里。
DEVLAB_C5_NOTES
      verify_disk_file "$repository" 'notes/git-notes.md' '91407f0b23c8f27c9baee81d07d736673c2ff49d661379e63caa0c1479cd5860' 0
      ;;
    C6)
      /bin/cat > "$notes_path" <<'DEVLAB_C6_NOTES'
# Git 练习记录

## 工作区模型

- 工作区保存当前文件。
- 暂存区保存下一次提交的候选快照。
- HEAD 指向当前分支的提交。

## 分支工作流

- 在功能分支完成独立修改。
- 合并前先确认当前分支和工作区状态。

## 排错顺序

- 先确认当前分支和目标。

## 安全恢复

课程将在恢复练习中补充这里。
DEVLAB_C6_NOTES
      verify_disk_file "$repository" 'notes/git-notes.md' 'f0aa82ddc8dad5b9f1072fc48f2b376385680a214659cb83cc596bfe03583022' 0
      ;;
    C7)
      /bin/cat > "$notes_path" <<'DEVLAB_C7_NOTES'
# Git 练习记录

## 工作区模型

- 工作区保存当前文件。
- 暂存区保存下一次提交的候选快照。
- HEAD 指向当前分支的提交。

## 分支工作流

- 在功能分支完成独立修改。
- 合并前先确认当前分支和工作区状态。

## 排错顺序

- 先查看状态与差异。
- 再确认当前分支和目标。

## 安全恢复

课程将在恢复练习中补充这里。
DEVLAB_C7_NOTES
      verify_disk_file "$repository" 'notes/git-notes.md' '42fa2adba9c1aa31981c95e6389c452d30754840095f9dbe45317994291a2677' 0
      ;;
    C8)
      /bin/cat > "$notes_path" <<'DEVLAB_C8_NOTES'
# Git 练习记录

## 工作区模型

- 工作区保存当前文件。
- 暂存区保存下一次提交的候选快照。
- HEAD 指向当前分支的提交。

## 分支工作流

- 在功能分支完成独立修改。
- 合并前先确认当前分支和工作区状态。

## 排错顺序

- 先查看状态与差异。
- 再确认当前分支和目标。

## 安全恢复

- 先判断修改是否暂存、是否提交、是否共享。
- 只更正尚未共享的本地提交。
DEVLAB_C8_NOTES
      verify_disk_file "$repository" 'notes/git-notes.md' 'ce1ca9eabafe913d81160cb7de9871f4dc9647c1568262ffe2ed353630a6a79c' 0
      ;;
    *) die "unknown Git notes state: $state" ;;
  esac
}

write_final_readme() {
  [[ -f "$1/README.md" && ! -L "$1/README.md" ]] || die 'README was replaced'
  /bin/cat > "$1/README.md" <<'DEVLAB_FINAL_README'
# dev-lab Git local checkpoint

这是 Git 本地能力完成后的权威工作树。它保留 Linux checkpoint 的固定文件，并新增本地 Git 学习记录；`.git/` 历史不存入站点源码。

请使用 `create-git-local.sh` 从规范 Linux checkpoint 重建参考仓库，或在练习目录父级运行 `check-git-local.sh ./dev-lab` 检查自己的连续历史。两个脚本都不读取全局 Git 身份，不添加远程仓库，也不访问网络。
DEVLAB_FINAL_README
  verify_disk_file "$1" 'README.md' 'f7afcc991b7f17de1d3b705733e94d0ceb1ede0d614e9fba7435f89390ea85ec' 0
}

expected_paths_for_commit() {
  if [[ "$1" == C1 ]]; then
    /bin/cat <<'DEVLAB_C1_PATHS'
.gitignore
README.md
config/bashrc.dev-lab
config/env.dev-lab
data/access.log
data/health-events.log
notes/linux-notes.md
notes/shell-notes.md
output/health-report.txt
output/summary.txt
scripts/health-check.sh
scripts/permission-demo.sh
scripts/report.sh
DEVLAB_C1_PATHS
  else
    /bin/cat <<'DEVLAB_LATER_PATHS'
.gitignore
README.md
config/bashrc.dev-lab
config/env.dev-lab
data/access.log
data/health-events.log
notes/git-notes.md
notes/linux-notes.md
notes/shell-notes.md
output/health-report.txt
output/summary.txt
scripts/health-check.sh
scripts/permission-demo.sh
scripts/report.sh
DEVLAB_LATER_PATHS
  fi
}

verify_tree_file() {
  local repository=$1
  local oid=$2
  local relative_path=$3
  local expected_hash=$4
  local expected_executable=$5
  local entry prefix actual_path mode object_type object_oid expected_mode actual_hash
  entry=$(run_git "$repository" ls-tree "$oid" -- "$relative_path")
  [[ -n "$entry" ]] || die "missing $relative_path in commit tree"
  prefix=${entry%%$'\t'*}
  actual_path=${entry#*$'\t'}
  read -r mode object_type object_oid <<< "$prefix"
  [[ "$actual_path" == "$relative_path" && "$object_type" == blob ]] || die "wrong tree entry: $relative_path"
  if [[ "$expected_executable" == 1 ]]; then expected_mode=100755; else expected_mode=100644; fi
  [[ "$mode" == "$expected_mode" ]] || die "wrong tree mode: $relative_path"
  actual_hash=$(sha256_git_blob "$repository" "$oid:$relative_path")
  [[ "$actual_hash" == "$expected_hash" ]] || die "wrong tree bytes: $relative_path"
}

verify_commit() {
  local repository=$1
  local logical_id=$2
  local oid=$3
  local expected_subject=$4
  shift 4
  local expected_parents="$*"
  local row actual_parents actual_subject metadata logical_number expected_epoch
  local actual_paths expected_paths kind file_path expected_hash expected_executable unused1 unused2 notes_hash
  assert_owned_staging
  [[ "$(run_git "$repository" cat-file -t "$oid")" == commit ]] || die "$logical_id is not a commit"
  actual_subject=$(run_git "$repository" show -s --format=%s "$oid")
  [[ "$actual_subject" == "$expected_subject" ]] || die "wrong subject for $logical_id"
  row=$(run_git "$repository" rev-list --parents -n 1 "$oid")
  actual_parents=${row#"$oid"}
  actual_parents=${actual_parents# }
  [[ "$actual_parents" == "$expected_parents" ]] || die "wrong parents for $logical_id"
  logical_number=${logical_id#C}
  expected_epoch=$((946684800 + logical_number))
  metadata=$(run_git "$repository" show -s --format='%an|%ae|%cn|%ce|%at|%ct' "$oid")
  [[ "$metadata" == "Dev Lab|dev-lab@example.invalid|Dev Lab|dev-lab@example.invalid|$expected_epoch|$expected_epoch" ]] || die "wrong identity or date for $logical_id"
  actual_paths=$(run_git "$repository" ls-tree -r --name-only "$oid")
  expected_paths=$(expected_paths_for_commit "$logical_id")
  [[ "$actual_paths" == "$expected_paths" ]] || die "wrong path set for $logical_id"
  while IFS='|' read -r kind file_path expected_hash expected_executable unused1 unused2; do
    [[ "$kind" == SOURCE ]] || continue
    if [[ "$logical_id" == C8 && "$file_path" == README.md ]]; then
      expected_hash=f7afcc991b7f17de1d3b705733e94d0ceb1ede0d614e9fba7435f89390ea85ec
    fi
    verify_tree_file "$repository" "$oid" "$file_path" "$expected_hash" "$expected_executable"
  done <<< "$DEVLAB_HISTORY_CONTRACT"
  verify_tree_file "$repository" "$oid" '.gitignore' '04950b968fe37d63b990bf4d5cb3a5b5684fd59320205c10282df38a8120fdbe' 0
  if [[ "$logical_id" != C1 ]]; then
    case "$logical_id" in
      C2) notes_hash=5250834cba284fc1d9017836f1ac056432d341020531b6c484fa6da4ae1834be ;;
      C3|C4) notes_hash=9796b2da5468c67a7b4274d063d7a9fb10b417c1a8caabc6ae10ac095047f342 ;;
      C5) notes_hash=91407f0b23c8f27c9baee81d07d736673c2ff49d661379e63caa0c1479cd5860 ;;
      C6) notes_hash=f0aa82ddc8dad5b9f1072fc48f2b376385680a214659cb83cc596bfe03583022 ;;
      C7) notes_hash=42fa2adba9c1aa31981c95e6389c452d30754840095f9dbe45317994291a2677 ;;
      C8) notes_hash=ce1ca9eabafe913d81160cb7de9871f4dc9647c1568262ffe2ed353630a6a79c ;;
      *) die "unknown logical commit: $logical_id" ;;
    esac
    verify_tree_file "$repository" "$oid" 'notes/git-notes.md' "$notes_hash" 0
  fi
}

replay_git_history() {
  local repository=$1
  assert_owned_staging
  if run_git "$repository" init --template= -b main >/dev/null 2>&1; then :; else
    run_git "$repository" init --template= >/dev/null 2>&1
    run_git "$repository" branch -M main >/dev/null 2>&1
  fi
  run_git "$repository" config --local user.name 'Dev Lab'
  run_git "$repository" config --local user.email 'dev-lab@example.invalid'
  run_git "$repository" config --local core.hooksPath /dev/null
  run_git "$repository" config --local core.fsmonitor false
  [[ -d "$repository/.git" && ! -L "$repository/.git" ]] || die 'Git init did not create an owned metadata directory'
  assert_owned_staging
  # Boundary: initialized owned repository

  write_gitignore "$repository"
  run_git "$repository" add -- .gitignore README.md config data notes output scripts
  run_git_at '2000-01-01T00:00:01Z' "$repository" commit -m 'chore: establish dev-lab baseline' >/dev/null 2>&1
  c1_oid=$(run_git "$repository" rev-parse HEAD)
  verify_commit "$repository" C1 "$c1_oid" 'chore: establish dev-lab baseline'
  # Boundary: verified C1

  write_git_notes "$repository" C2
  run_git "$repository" add -- notes/git-notes.md
  run_git_at '2000-01-01T00:00:02Z' "$repository" commit -m 'docs: add git practice journal' >/dev/null 2>&1
  c2_oid=$(run_git "$repository" rev-parse HEAD)
  verify_commit "$repository" C2 "$c2_oid" 'docs: add git practice journal' "$c1_oid"

  run_git "$repository" checkout -q -b feature/git-workflow
  write_git_notes "$repository" C3
  run_git "$repository" add -- notes/git-notes.md
  run_git_at '2000-01-01T00:00:03Z' "$repository" commit -m 'docs: record branch workflow' >/dev/null 2>&1
  c3_oid=$(run_git "$repository" rev-parse HEAD)
  verify_commit "$repository" C3 "$c3_oid" 'docs: record branch workflow' "$c2_oid"
  run_git "$repository" checkout -q main
  run_git_at '2000-01-01T00:00:04Z' "$repository" merge --no-ff -m 'merge: integrate git workflow' feature/git-workflow >/dev/null 2>&1
  c4_oid=$(run_git "$repository" rev-parse HEAD)
  verify_commit "$repository" C4 "$c4_oid" 'merge: integrate git workflow' "$c2_oid" "$c3_oid"
  [[ "$(run_git "$repository" rev-parse refs/heads/feature/git-workflow)" == "$c3_oid" ]] || die 'feature/git-workflow changed before deletion'
  run_git "$repository" branch -d feature/git-workflow >/dev/null 2>&1

  run_git "$repository" branch feature/troubleshooting-guide "$c4_oid"
  write_git_notes "$repository" C5
  run_git "$repository" add -- notes/git-notes.md
  run_git_at '2000-01-01T00:00:05Z' "$repository" commit -m 'docs: record main troubleshooting step' >/dev/null 2>&1
  c5_oid=$(run_git "$repository" rev-parse HEAD)
  verify_commit "$repository" C5 "$c5_oid" 'docs: record main troubleshooting step' "$c4_oid"

  run_git "$repository" checkout -q feature/troubleshooting-guide
  write_git_notes "$repository" C6
  run_git "$repository" add -- notes/git-notes.md
  run_git_at '2000-01-01T00:00:06Z' "$repository" commit -m 'docs: record branch troubleshooting step' >/dev/null 2>&1
  c6_oid=$(run_git "$repository" rev-parse HEAD)
  verify_commit "$repository" C6 "$c6_oid" 'docs: record branch troubleshooting step' "$c4_oid"
  run_git "$repository" checkout -q main
  if run_git_at '2000-01-01T00:00:07Z' "$repository" merge --no-ff feature/troubleshooting-guide >/dev/null 2>&1; then
    die 'expected teaching conflict did not occur'
  fi
  [[ -f "$repository/.git/MERGE_HEAD" ]] || die 'expected merge state is missing'
  [[ "$(run_git "$repository" rev-parse MERGE_HEAD)" == "$c6_oid" ]] || die 'merge candidate changed'
  write_git_notes "$repository" C7
  run_git "$repository" add -- notes/git-notes.md
  run_git_at '2000-01-01T00:00:07Z' "$repository" commit -m 'merge: resolve troubleshooting guide' >/dev/null 2>&1
  c7_oid=$(run_git "$repository" rev-parse HEAD)
  verify_commit "$repository" C7 "$c7_oid" 'merge: resolve troubleshooting guide' "$c5_oid" "$c6_oid"
  [[ "$(run_git "$repository" rev-parse refs/heads/feature/troubleshooting-guide)" == "$c6_oid" ]] || die 'feature/troubleshooting-guide changed before deletion'
  run_git "$repository" branch -d feature/troubleshooting-guide >/dev/null 2>&1

  write_final_readme "$repository"
  write_git_notes "$repository" C8
  run_git "$repository" add -- README.md notes/git-notes.md
  run_git_at '2000-01-01T00:00:08Z' "$repository" commit -m 'docs: complete local git notes' >/dev/null 2>&1
  c8_oid=$(run_git "$repository" rev-parse HEAD)
  verify_commit "$repository" C8 "$c8_oid" 'docs: complete local git notes' "$c7_oid"
}

verify_final_worktree() {
  local repository=$1
  local entry_count kind file_path expected_hash expected_executable unused1 unused2
  [[ -d "$repository" && ! -L "$repository" ]] || die 'final repository root was replaced'
  [[ -d "$repository/.git" && ! -L "$repository/.git" ]] || die 'final .git is not an owned directory'
  for directory in config data notes output scripts; do
    [[ -d "$repository/$directory" && ! -L "$repository/$directory" ]] || die "invalid final directory: $directory"
  done
  entry_count=$(/usr/bin/find "$repository" -mindepth 1 -path "$repository/.git" -prune -o -print | /usr/bin/wc -l)
  [[ "$entry_count" -eq 19 ]] || die 'final worktree has missing or extra paths'
  while IFS='|' read -r kind file_path expected_hash expected_executable unused1 unused2; do
    [[ "$kind" == SOURCE ]] || continue
    if [[ "$file_path" == README.md ]]; then
      expected_hash=f7afcc991b7f17de1d3b705733e94d0ceb1ede0d614e9fba7435f89390ea85ec
    fi
    verify_disk_file "$repository" "$file_path" "$expected_hash" "$expected_executable"
  done <<< "$DEVLAB_HISTORY_CONTRACT"
  verify_disk_file "$repository" '.gitignore' '04950b968fe37d63b990bf4d5cb3a5b5684fd59320205c10282df38a8120fdbe' 0
  verify_disk_file "$repository" 'notes/git-notes.md' 'ce1ca9eabafe913d81160cb7de9871f4dc9647c1568262ffe2ed353630a6a79c' 0
}

verify_generated_history() {
  local repository=$1
  local refs
  assert_owned_staging
  verify_commit "$repository" C1 "$c1_oid" 'chore: establish dev-lab baseline'
  verify_commit "$repository" C2 "$c2_oid" 'docs: add git practice journal' "$c1_oid"
  verify_commit "$repository" C3 "$c3_oid" 'docs: record branch workflow' "$c2_oid"
  verify_commit "$repository" C4 "$c4_oid" 'merge: integrate git workflow' "$c2_oid" "$c3_oid"
  verify_commit "$repository" C5 "$c5_oid" 'docs: record main troubleshooting step' "$c4_oid"
  verify_commit "$repository" C6 "$c6_oid" 'docs: record branch troubleshooting step' "$c4_oid"
  verify_commit "$repository" C7 "$c7_oid" 'merge: resolve troubleshooting guide' "$c5_oid" "$c6_oid"
  verify_commit "$repository" C8 "$c8_oid" 'docs: complete local git notes' "$c7_oid"
  [[ "$(run_git "$repository" symbolic-ref -q HEAD)" == refs/heads/main ]] || die 'final branch is not main'
  [[ "$(run_git "$repository" rev-parse HEAD)" == "$c8_oid" ]] || die 'final HEAD is not C8'
  [[ "$(run_git "$repository" rev-list --all --count)" == 8 ]] || die 'final history does not contain eight reachable commits'
  refs=$(run_git "$repository" for-each-ref --format='%(refname)' refs)
  [[ "$refs" == refs/heads/main ]] || die 'final refs are not main-only'
  [[ -z "$(run_git "$repository" remote)" ]] || die 'final repository has a remote'
  [[ "$(run_git "$repository" config --local --get user.name)" == 'Dev Lab' ]] || die 'wrong local user.name'
  [[ "$(run_git "$repository" config --local --get user.email)" == 'dev-lab@example.invalid' ]] || die 'wrong local user.email'
  [[ "$(run_git "$repository" config --local --get core.hooksPath)" == /dev/null ]] || die 'hooks are not disabled'
  [[ "$(run_git "$repository" config --local --get core.fsmonitor)" == false ]] || die 'fsmonitor is not disabled'
  if run_git "$repository" config --local --get-regexp '^(alias|filter|include|includeIf|remote)\.' >/dev/null 2>&1; then
    die 'unexpected executable or remote local configuration'
  fi
  run_git "$repository" diff-index --quiet --cached HEAD --
  run_git "$repository" diff-files --quiet --
  [[ -z "$(run_git "$repository" ls-files --others --exclude-standard)" ]] || die 'final repository has untracked files'
  for state_path in MERGE_HEAD REBASE_HEAD rebase-merge rebase-apply CHERRY_PICK_HEAD REVERT_HEAD BISECT_LOG sequencer; do
    [[ ! -e "$repository/.git/$state_path" && ! -L "$repository/.git/$state_path" ]] || die "unfinished Git state: $state_path"
  done
  verify_final_worktree "$repository"
  assert_owned_staging
}

cleanup_nested_owned_staging() {
  local foreign_target=$1
  local owned_basename=$2
  local nested_path="$foreign_target/$owned_basename"
  [[ -d "$foreign_target" && ! -L "$foreign_target" ]] || return 1
  [[ -d "$nested_path" && ! -L "$nested_path" ]] || return 1
  [[ "$(path_identity "$nested_path")" == "$staging_identity" ]] || return 1
  /bin/rm -rf "$nested_path"
  [[ ! -e "$nested_path" && ! -L "$nested_path" ]]
}

publish_owned_target() {
  local parent=$1
  local owned_basename=$2
  local destination_basename=$3
  local owned_path="$parent/$owned_basename"
  local destination_path="$parent/$destination_basename"
  # Boundary: begin publication validation
  [[ "$parent" == "$pinned_parent" ]] || die 'publication parent does not match pinned parent'
  [[ "$owned_basename" == "$staging_basename" && "$owned_path" == "$staging_path" ]] || die 'publication staging does not match owned staging'
  [[ "$destination_basename" == "$target_basename" && "$destination_path" == "$target_path" ]] || die 'publication target does not match pinned target'
  assert_owned_staging
  [[ ! -e "$destination_path" && ! -L "$destination_path" ]] || die 'target appeared before publication'
  # Boundary: target absence rechecked
  case "$publication_platform" in
    Linux) /bin/mv -n -T "$owned_path" "$destination_path" ;;
    Darwin)
      assert_parent_identity
      [[ ! -e "$destination_path" && ! -L "$destination_path" ]] || die 'target appeared immediately before Darwin publication'
      /bin/mv -n -h "$owned_path" "$destination_path"
      ;;
    *) die "unsupported publication platform: $publication_platform" ;;
  esac
  # Boundary: publication command returned
  assert_parent_identity
  [[ ! -e "$owned_path" && ! -L "$owned_path" ]] || die 'staging still exists after publication'
  [[ -d "$destination_path" && ! -L "$destination_path" ]] || die 'published target is not a real directory'
  if [[ -e "$destination_path/$owned_basename" || -L "$destination_path/$owned_basename" ]]; then
    if cleanup_nested_owned_staging "$destination_path" "$owned_basename"; then
      die 'Darwin publication nested owned staging inside a foreign target; owned child removed'
    fi
    die 'publication produced an unknown nested target; foreign state preserved'
  fi
  [[ "$(path_identity "$destination_path")" == "$staging_identity" ]] || die 'published target identity does not match owned staging'
}

create_git_local_reference() {
  local requested_source=$1
  local requested_target=$2
  local target_parent_input
  [[ -n "$requested_source" && -n "$requested_target" ]] || die 'source and target paths are required'
  [[ -d "$requested_source" && ! -L "$requested_source" ]] || die 'source must be a real directory'
  source_path=$(cd -- "$requested_source" && pwd -P) || die 'could not resolve source directory'
  [[ "$source_path" != / ]] || die 'source root is not allowed'
  source_identity=$(path_identity "$source_path")
  require_linux_source "$source_path"

  [[ "$requested_target" != / && "$requested_target" != */ ]] || die 'target must name a new directory'
  target_parent_input=$(/usr/bin/dirname -- "$requested_target")
  target_basename=$(/usr/bin/basename -- "$requested_target")
  [[ -n "$target_basename" && "$target_basename" != . && "$target_basename" != .. ]] || die 'invalid target basename'
  [[ -d "$target_parent_input" && ! -L "$target_parent_input" ]] || die 'target parent must be a real directory'
  pinned_parent=$(cd -- "$target_parent_input" && pwd -P) || die 'could not resolve target parent'
  [[ "$pinned_parent" != / || "$target_basename" != / ]] || die 'invalid target path'
  pinned_parent_identity=$(path_identity "$pinned_parent")
  target_path="$pinned_parent/$target_basename"
  assert_parent_identity
  [[ ! -e "$target_path" && ! -L "$target_path" ]] || die 'target already exists'

  create_owned_staging "$pinned_parent" "$target_basename"
  copy_linux_source
  replay_git_history "$staging_path"
  verify_generated_history "$staging_path"
  require_linux_source "$source_path"
  [[ "$(path_identity "$source_path")" == "$source_identity" ]] || die 'source identity changed before publication'
  publish_owned_target "$pinned_parent" "$staging_basename" "$target_basename"
  [[ "$(path_identity "$target_path")" == "$staging_identity" ]] || die 'published target identity changed'
  /usr/bin/printf '%s\n' 'git-local reference: created'
}

trap cleanup_owned_staging EXIT
trap 'handle_termination_signal HUP' HUP
trap 'handle_termination_signal INT' INT
trap 'handle_termination_signal TERM' TERM
create_git_local_reference "$source_input" "$target_input"
DEVLAB_GIT_CREATOR
