Repository navigation
Expand file tree
/
Copy pathcreate-prototype-repo.sh
More file actions
executable file
·112 lines (101 loc) · 3.51 KB
/
Copy pathcreate-prototype-repo.sh
File metadata and controls
executable file
·112 lines (101 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
set -euo pipefail
if [[ $# -ne 1 ]]; then
printf 'Usage: %s <new-repository-directory>\n' "$0" >&2
exit 2
fi
source_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
starter="$source_root/starter"
parent="$(dirname "$1")"
name="$(basename "$1")"
if [[ ! -d "$parent" || "$name" == "." || "$name" == ".." || "$name" == "/" ]]; then
printf 'Destination must have an existing parent directory.\n' >&2
exit 1
fi
parent="$(cd "$parent" && pwd -P)"
destination="$parent/$name"
if [[ "$parent" == "$source_root" || "$parent" == "$source_root/"* ]]; then
printf 'Create the new repository outside the Replica checkout.\n' >&2
exit 1
fi
if [[ -e "$destination" || -L "$destination" ]]; then
printf 'Destination already exists: %s\n' "$destination" >&2
exit 1
fi
if [[ -L "$starter" ||
! -f "$starter/.agents/skills/setup-skills/SKILL.md" ||
! -f "$starter/.agents/context-templates/SKILL.md" ||
-L "$starter/.agents/skills/setup-skills/SKILL.md" ||
-L "$starter/.agents/context-templates/SKILL.md" ]]; then
printf 'Starter skills or context templates are missing or linked.\n' >&2
exit 1
fi
temporary="$(mktemp -d "$parent/.replica-starter.XXXXXX")"
created=""
cleanup() {
rm -r -- "$temporary"
if [[ -n "$created" ]]; then
rm -r -- "$created"
fi
}
trap cleanup EXIT
mkdir -- "$temporary/files"
count=0
copy_file() {
local source="$1" relative="$2"
if [[ ! -f "$source" || -L "$source" ]]; then
printf 'Starter file is missing or is not a regular file: %s\n' "$source" >&2
exit 1
fi
mkdir -p -- "$temporary/files/$(dirname "$relative")"
cp -p -- "$source" "$temporary/files/$relative"
((count += 1))
}
if [[ "$(git -C "$source_root" rev-parse --show-toplevel 2>/dev/null || true)" == "$source_root" ]]; then
git -C "$source_root" ls-files --cached --stage -z -- starter/ > "$temporary/manifest"
while IFS= read -r -d '' entry; do
metadata="${entry%%$'\t'*}"
path="${entry#*$'\t'}"
mode="${metadata%% *}"
stage="${metadata##* }"
if [[ ( "$mode" != 100644 && "$mode" != 100755 ) || "$stage" != 0 ||
"$path" != starter/* || "$path" == */../* || "$path" == */./* ||
"$path" == */.git/* || "$path" == starter/.git ]]; then
printf 'Unsupported starter entry: %s\n' "$path" >&2
exit 1
fi
directory="${path%/*}"
while [[ "$directory" != starter ]]; do
if [[ -L "$source_root/$directory" ]]; then
printf 'Linked starter directory: %s\n' "$directory" >&2
exit 1
fi
directory="${directory%/*}"
done
copy_file "$source_root/$path" "${path#starter/}"
done < "$temporary/manifest"
else
unsupported="$(find "$starter" ! -type d ! -type f -print -quit)"
if [[ -n "$unsupported" ]]; then
printf 'Starter archive contains an unsupported file: %s\n' "$unsupported" >&2
exit 1
fi
if [[ -e "$starter/.git" || -L "$starter/.git" ]]; then
printf 'Starter archive contains Git metadata.\n' >&2
exit 1
fi
while IFS= read -r -d '' path; do
copy_file "$path" "${path#"$starter/"}"
done < <(find "$starter" -type f -print0)
fi
if [[ $count -eq 0 ]]; then
printf 'No starter files found in %s\n' "$source_root" >&2
exit 1
fi
git init -q --initial-branch=main "$temporary/files"
mkdir -- "$destination"
created="$destination"
cp -Rp -- "$temporary/files/." "$destination/"
created=""
printf 'Created %s with %s starter files and a new, uncommitted Git repository.\n' "$destination" "$count"
printf 'Start a new agent session in that directory and run /setup-skills.\n'