-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsync-system-instructions.sh
More file actions
executable file
·260 lines (222 loc) · 7.2 KB
/
Copy pathsync-system-instructions.sh
File metadata and controls
executable file
·260 lines (222 loc) · 7.2 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env bash
# Sync System Instructions to All Repositories
# Usage: ./sync-system-instructions.sh [-d base_dir] [-c] [-p]
#
# Options:
# -d base_dir Base directory containing repos (default: ~/dev/github)
# -c Commit changes
# -p Push changes after committing
set -euo pipefail
# Defaults
BASE_DIR="$HOME/dev/github"
COMMIT=false
PUSH=false
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
# Markdown files to sync to all repos
# Format: "source_path:target_name" - source is relative to dotfiles root, target is the filename in destination repos
# Note: CLAUDE.md is NOT synced — it lives globally at ~/.claude/CLAUDE.md (deployed by deploy.sh)
MARKDOWN_FILES=(
"AGENTS.md:AGENTS.md"
"GEMINI.md:GEMINI.md"
"RULES.md:RULES.md"
)
# Dotfiles directory - source of truth for system instructions
# As per README quick start, dotfiles should be cloned to ~/dev/github/dotfiles
DOTFILES_DIR="$HOME/dev/github/dotfiles"
# Parse arguments
while getopts "d:cp" opt; do
case $opt in
d) BASE_DIR="$OPTARG" ;;
c) COMMIT=true ;;
p) PUSH=true ;;
*)
echo "Usage: $0 [-d base_dir] [-c] [-p]" >&2
exit 1
;;
esac
done
echo -e "${CYAN}========================================${NC}"
echo -e "${CYAN} System Instructions Sync${NC}"
echo -e "${CYAN}========================================${NC}"
echo -e "${BLUE}Base Directory:${NC} $BASE_DIR"
echo -e "${BLUE}Commit:${NC} $COMMIT"
echo -e "${BLUE}Push:${NC} $PUSH"
echo -e "${CYAN}========================================${NC}"
echo
# Validate base directory exists
if [[ ! -d "$BASE_DIR" ]]; then
echo -e "${RED}Error: Directory not found: $BASE_DIR${NC}" >&2
exit 1
fi
# Validate dotfiles directory exists
if [[ ! -d "$DOTFILES_DIR" ]]; then
echo -e "${RED}Error: Dotfiles directory not found: $DOTFILES_DIR${NC}" >&2
exit 1
fi
# Copy system instruction markdown files to a repository
# Usage: copy_markdown_files <repo_path>
copy_markdown_files() {
local repo_path="$1"
# Skip if dotfiles dir (don't copy to self)
if [[ "$(cd "$repo_path" 2>/dev/null && pwd)" == "$(cd "$DOTFILES_DIR" 2>/dev/null && pwd)" ]]; then
return 0
fi
# Check if it's a git repository
if ! cd "$repo_path" 2>/dev/null || ! git rev-parse --git-dir >/dev/null 2>&1; then
return 0
fi
local copied_count=0
# Remove stale CLAUDE.md from repo (now centralized at ~/.claude/CLAUDE.md)
local claude_md="$repo_path/CLAUDE.md"
if [[ -f "$claude_md" ]]; then
rm -f "$claude_md"
echo -e " ${GREEN}removed${NC} CLAUDE.md (now using ~/.claude/CLAUDE.md)"
copied_count=$((copied_count + 1)) || true
fi
for md_mapping in "${MARKDOWN_FILES[@]}"; do
# Parse "source_path:target_name" format
local source_path="${md_mapping%%:*}"
local target_name="${md_mapping##*:}"
local source_file="$DOTFILES_DIR/$source_path"
local target_file="$repo_path/$target_name"
if [[ -f "$source_file" ]]; then
# Check if file is different
if [[ ! -f "$target_file" ]] || ! cmp -s "$source_file" "$target_file"; then
if cp -f "$source_file" "$target_file" 2>/dev/null; then
echo -e " ${GREEN}synced${NC} $target_name"
copied_count=$((copied_count + 1)) || true
fi
fi
fi
done
if [[ $copied_count -gt 0 ]]; then
echo -e " ${BLUE}system instructions updated ($copied_count files)${NC}"
return 0
else
echo -e " ${YELLOW}already up to date${NC}"
return 0 # Return success so commit/push phase still runs
fi
}
# Commit changes using git directly
commit_changes() {
local repo_path="$1"
local repo_name
repo_name=$(basename "$repo_path")
cd "$repo_path" 2>/dev/null || return 0
# Remove stale CLAUDE.md from repo (now centralized at ~/.claude/CLAUDE.md)
if [[ -f "CLAUDE.md" ]]; then
git rm -f CLAUDE.md 2>/dev/null || rm -f CLAUDE.md
echo -e " ${GREEN}removed${NC} CLAUDE.md (now using ~/.claude/CLAUDE.md)"
fi
# Check if there are changes to commit (check both unstaged and staged)
if git diff --quiet AGENTS.md GEMINI.md RULES.md 2>/dev/null &&
git diff --cached --quiet AGENTS.md GEMINI.md RULES.md CLAUDE.md 2>/dev/null; then
echo -e " ${YELLOW}already up to date${NC} (no changes to commit)"
cd - >/dev/null
return 1
fi
# Add files
if ! git add AGENTS.md GEMINI.md RULES.md 2>/dev/null; then
cd - >/dev/null
return 1
fi
# Get git config from dotfiles repo as fallback
local user_name user_email
user_name=$(git -C "$DOTFILES_DIR" config user.name 2>/dev/null)
user_email=$(git -C "$DOTFILES_DIR" config user.email 2>/dev/null)
# Try commit with config override if available
local commit_cmd
if [[ -n "$user_name" && -n "$user_email" ]]; then
commit_cmd="git -c user.name='$user_name' -c user.email='$user_email' commit -m 'chore: sync system instructions (remove CLAUDE.md)'"
else
commit_cmd="git commit -m 'chore: sync system instructions (remove CLAUDE.md)'"
fi
if eval "$commit_cmd" >/dev/null 2>&1; then
echo -e " ${GREEN}committed${NC} $repo_name"
cd - >/dev/null
return 0
fi
# Commit failed - show helpful message
echo -e " ${RED}commit failed${NC} $repo_name"
if [[ -z "$user_name" || -z "$user_email" ]]; then
echo -e " ${YELLOW}→ Set global git config:${NC} git config --global user.name 'Your Name' && git config --global user.email 'you@example.com'"
fi
cd - >/dev/null
return 1
}
# Push changes using git
push_changes() {
local repo_path="$1"
local repo_name
repo_name=$(basename "$repo_path")
cd "$repo_path" 2>/dev/null || return 0
# Get the current branch name
local branch
branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) || branch="main"
# Check if there are commits to push (compare with remote branch)
if git rev-parse --verify "origin/$branch" >/dev/null 2>&1; then
local ahead_count
ahead_count=$(git rev-list --count "origin/$branch..HEAD" 2>/dev/null) || ahead_count=0
if [[ "$ahead_count" -eq 0 ]]; then
echo -e " ${YELLOW}already up to date${NC} (nothing to push)"
cd - >/dev/null
return 1
fi
fi
# Show push errors
if git push origin "$branch" 2>&1; then
echo -e " ${GREEN}pushed${NC} $repo_name"
cd - >/dev/null
return 0
else
echo -e " ${RED}push failed${NC} $repo_name"
cd - >/dev/null
return 1
fi
}
# Main processing
REPO_COUNT=0
SYNCED_COUNT=0
SKIPPED_COUNT=0
echo -e "${CYAN}Scanning for repositories in: $BASE_DIR${NC}"
echo
for dir in "$BASE_DIR"/*; do
if [[ -d "$dir" ]]; then
REPO_NAME=$(basename "$dir")
echo -n "[$REPO_NAME] "
if copy_markdown_files "$dir"; then
((SYNCED_COUNT++)) || true
else
((SKIPPED_COUNT++)) || true
fi
((REPO_COUNT++)) || true
fi
done
# Commit and push if requested
if [[ "$COMMIT" == true ]]; then
echo
echo -e "${CYAN}Committing changes...${NC}"
for dir in "$BASE_DIR"/*; do
if [[ -d "$dir" ]]; then
commit_changes "$dir" || true
if [[ "$PUSH" == true ]]; then
push_changes "$dir" || true
fi
fi
done
fi
# Summary
echo
echo -e "${CYAN}========================================${NC}"
echo -e "${CYAN} Summary${NC}"
echo -e "${CYAN}========================================${NC}"
echo -e " ${GREEN}Synced:${NC} $SYNCED_COUNT"
echo -e " ${YELLOW}Skipped:${NC} $SKIPPED_COUNT"
echo -e " ${CYAN}Total:${NC} $REPO_COUNT repositories"
echo -e "${CYAN}========================================${NC}"