-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.sh
More file actions
executable file
·362 lines (288 loc) · 7.91 KB
/
Copy pathcore.sh
File metadata and controls
executable file
·362 lines (288 loc) · 7.91 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env bash
set -e
# Colors
BOLD='\033[1m'
CYAN='\033[36m'
DIM='\033[2m'
RED='\033[31m'
RESET='\033[0m'
# Print error and exit
error() {
echo -e "${RED}Error:${RESET} $1" >&2
exit 1
}
# Shared setup (used by both init and clone)
do_setup() {
cd "$HOME"
# Guards
[[ -d ".jj" ]] && error "Already initialized. Remove ~/.jj to re-initialize."
# Check jj is installed
command -v jj >/dev/null 2>&1 || error "jj is not installed. See https://github.com/martinvonz/jj"
# Initialize jj repo
jj git init --no-colocate
# Create .nadm directory
[[ -d ".nadm" ]] || mkdir -p .nadm
# Create dot command implementation used by jj alias
cat > .nadm/dot.sh << 'EOF'
#!/usr/bin/env bash
set -euo pipefail
tracked_file="$HOME/.nadm/tracked"
gitignore_file="$HOME/.gitignore"
ensure_state() {
mkdir -p "$HOME/.nadm"
touch "$tracked_file"
}
canonicalize_path() {
local input="$1"
local raw="$input"
local full_path
local relative_path
[[ -z "$raw" ]] && return 1
if [[ "$raw" == "~/"* ]]; then
raw="$HOME/${raw#~/}"
elif [[ "$raw" != /* ]]; then
raw="$PWD/$raw"
fi
full_path="$(realpath $raw)"
case "$full_path" in
"$HOME")
relative_path="."
;;
"$HOME"/*)
relative_path="${full_path#"$HOME"/}"
;;
*)
printf 'Path must be inside %s: %s\n' "$HOME" "$input" >&2
return 1
;;
esac
if [[ -d "$full_path" ]]; then
printf '%s/\n' "${relative_path%/}"
else
printf '%s\n' "${relative_path%/}"
fi
}
add_tracked_path() {
local path="$1"
if [[ ! -f "$tracked_file" ]] || ! grep -Fqx "$path" "$tracked_file"; then
printf '%s\n' "$path" >> "$tracked_file"
fi
}
forget_tracked_path() {
local path="$1"
local base_path="${path%/}"
local temp_file
temp_file="$(mktemp)"
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" == "$base_path" || "$line" == "$base_path/" || "$line" == "$base_path/"* ]]; then
continue
fi
printf '%s\n' "$line" >> "$temp_file"
done < "$tracked_file"
mv "$temp_file" "$tracked_file"
}
append_unique_line() {
local output_file="$1"
local value="$2"
if [[ ! -f "$output_file" ]] || ! grep -Fqx "$value" "$output_file"; then
printf '%s\n' "$value" >> "$output_file"
fi
}
write_gitignore_header() {
local output_file="$1"
printf '%s\n' '# Do not edit this file directly.' > "$output_file"
printf '%s\n' '# Edit ~/.nadm/tracked and run `jj dot sync`' >> "$output_file"
printf '%s\n' '# Or, run `jj dot edit`' >> "$output_file"
printf '\n' >> "$output_file"
printf '%s\n' '*' >> "$output_file"
printf '%s\n' '!/.gitignore' >> "$output_file"
}
append_unignore_rules_for_path() {
local output_file="$1"
local path="$2"
local normalized_path="${path%/}"
local is_directory="0"
local prefix=""
local index
[[ -z "$normalized_path" ]] && return
[[ "$normalized_path" == "." ]] && return
if [[ "$path" == */ || -d "$HOME/$normalized_path" ]]; then
is_directory="1"
fi
IFS='/' read -r -a path_parts <<< "$normalized_path"
for (( index = 0; index < ${#path_parts[@]} - 1; index += 1 )); do
if [[ -n "$prefix" ]]; then
prefix="$prefix/${path_parts[$index]}"
else
prefix="${path_parts[$index]}"
fi
append_unique_line "$output_file" "!/$prefix/"
append_unique_line "$output_file" "/$prefix/*"
done
if [[ "$is_directory" == "1" ]]; then
append_unique_line "$output_file" "!/$normalized_path/"
append_unique_line "$output_file" "!/$normalized_path/**"
else
append_unique_line "$output_file" "!/$normalized_path"
fi
}
dot_sync() {
local temp_file
ensure_state
temp_file="$(mktemp)"
write_gitignore_header "$temp_file"
while IFS= read -r path || [[ -n "$path" ]]; do
[[ -z "$path" ]] && continue
append_unignore_rules_for_path "$temp_file" "$path"
done < "$tracked_file"
mv "$temp_file" "$gitignore_file"
}
dot_add() {
local target
local canonical_path
ensure_state
if [[ $# -eq 0 ]]; then
printf 'Usage: dot add <file-or-directory>...\n' >&2
return 1
fi
for target in "$@"; do
canonical_path="$(canonicalize_path "$target")"
add_tracked_path "$canonical_path"
done
dot_sync
}
dot_forget() {
local target
local canonical_path
ensure_state
if [[ $# -eq 0 ]]; then
printf 'Usage: dot forget <file-or-directory>...\n' >&2
return 1
fi
for target in "$@"; do
canonical_path="$(canonicalize_path "$target")"
forget_tracked_path "$canonical_path"
# TODO: It's a little inefficient to run a whole sync here, but
# it's okay for now. dot_sync updates .gitignore, which is needed
# or else jj will throw an error that the file is not ignored.
dot_sync
jj file untrack "${canonical_path%/}" || echo -e "\nAre you tracking a parent directory of ${canonical_path%/}?"
done
}
dot_edit() {
if [ -z "$EDITOR" ]; then
echo '$EDITOR environmental variable not set'
exit 1
fi
set -e
$EDITOR ~/.nadm/tracked
dot_sync
}
main() {
local subcommand="${1:-}"
case "$subcommand" in
add)
shift
dot_add "$@"
;;
forget)
shift
dot_forget "$@"
;;
sync)
dot_sync
;;
edit)
dot_edit
;;
*)
printf 'Usage: dot <add|forget|edit|sync> [paths...]\n' >&2
return 1
;;
esac
}
main "$@"
EOF
chmod +x .nadm/dot.sh
# Create config with a single dot alias
cat > .nadm/config.toml << 'EOF'
[aliases]
dot = ["util", "exec", "--", "bash", "-lc", "~/.nadm/dot.sh \"$@\"", ""]
EOF
# Remove auto-generated config if it exists
rm -f .jj/repo/config.toml
# Symlink config
ln -s "$HOME/.nadm/config.toml" .jj/repo/config.toml
touch .nadm/tracked
if ! grep -Fqx ".nadm/" .nadm/tracked; then
printf '%s\n' ".nadm/" >> .nadm/tracked
fi
~/.nadm/dot.sh sync
jj dot add .nadm
jj dot add .gitignore
}
show_usage() {
echo "Usage: nadm <command>"
echo ""
echo "Commands:"
echo " init"
echo " clone <url>"
echo " clone --github <owner/repo>"
}
cmd_init() {
echo -e "${BOLD}Initializing nadm...${RESET}"
do_setup
jj bookmark set main
jj commit -m 'initial nadm setup'
echo
echo -e "${CYAN}Done!${RESET} Your home directory is now a jj repo."
echo "Use 'jj dot add <file>' to start tracking dotfiles."
echo
jj status
}
cmd_clone() {
local url="$1"
[[ -z "$url" ]] && error "Usage: nadm clone <url>"
echo -e "${BOLD}Cloning dotfiles...${RESET}"
do_setup
jj git remote add origin $url
jj git fetch
echo
echo -e "${CYAN}Done!${RESET} Your home directory is now a jj repo."
echo "Use 'jj dot add <file>' to start tracking dotfiles."
echo
jj status
}
main() {
local -a args=()
if [[ $# -gt 0 ]]; then
args=("$@")
elif [[ -n "${NADM_ARGS:-}" ]]; then
read -r -a args <<< "${NADM_ARGS}"
fi
local cmd="${args[0]:-}"
case "$cmd" in
init)
cmd_init
;;
clone)
if [[ "${args[1]:-}" == "--github" ]]; then
local repo="${args[2]:-}"
[[ -z "$repo" ]] && error "Usage: nadm clone --github <owner/repo>"
cmd_clone "https://github.com/${repo}.git"
else
cmd_clone "${args[1]:-}"
fi
;;
""|help|--help|-h)
show_usage
;;
*)
error "Unknown command: $cmd"
;;
esac
}
# Run if executed directly (not sourced)
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi