start 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. #!/bin/bash
  2. # Dotfiles sync script
  3. set -euo pipefail
  4. # Configuration
  5. hostname_id=$(uname -n)
  6. checkout_dir=$HOME/.dofiles
  7. script_dir=$checkout_dir/dotfiles
  8. dots_dir=$checkout_dir/dotfiles/dots
  9. dots_host_dir=$checkout_dir/dotfiles/dots.$hostname_id
  10. repo="https://git.capella.pro/capella/dotfiles.git"
  11. ProgName=$(basename "$0")
  12. # Colors for output
  13. RED='\033[0;31m'
  14. GREEN='\033[0;32m'
  15. YELLOW='\033[1;33m'
  16. NC='\033[0m' # No Color
  17. # Logging functions
  18. log_info() { echo -e "${GREEN}[INFO]${NC} $*"; }
  19. log_warn() { echo -e "${YELLOW}[WARN]${NC} $*" >&2; }
  20. log_error() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
  21. # Get group names for the current host (sorted, one per line)
  22. get_groups() {
  23. local groups_file="$script_dir/groups.$hostname_id"
  24. if [[ -f "$groups_file" ]]; then
  25. grep -v '^\s*#' "$groups_file" | grep -v '^\s*$' | sort
  26. fi
  27. }
  28. # Get combined sync list (common + groups + host-specific, deduplicated)
  29. get_sync_list() {
  30. {
  31. cat "$script_dir/to_sync" 2>/dev/null || true
  32. while IFS= read -r group; do
  33. cat "$script_dir/to_sync.@$group" 2>/dev/null || true
  34. done < <(get_groups)
  35. cat "$script_dir/to_sync.$hostname_id" 2>/dev/null || true
  36. } | grep -v '^\s*$' | sort -u || true
  37. }
  38. # Get combined package list (common + groups + host-specific, deduplicated).
  39. # Unlike dotfiles, package lists are ADDITIVE: every applicable list is unioned
  40. # rather than overridden, so a host list adds to the common one, never replaces it.
  41. get_pkg_list() {
  42. {
  43. cat "$script_dir/pkglist" 2>/dev/null || true
  44. while IFS= read -r group; do
  45. cat "$script_dir/pkglist.@$group" 2>/dev/null || true
  46. done < <(get_groups)
  47. cat "$script_dir/pkglist.$hostname_id" 2>/dev/null || true
  48. } | grep -v '^\s*#' | grep -v '^\s*$' | sort -u || true
  49. }
  50. # Explicitly-installed packages on this host.
  51. # -Qqe (explicit) keeps AUR *dependencies* out of the list; they get pulled in
  52. # automatically by their parent. The -debug filter drops artifacts makepkg
  53. # generates when building locally -- they only exist on the machine that built
  54. # them and mostly aren't installable elsewhere.
  55. installed_packages() {
  56. yay -Qqe | grep -v -- '-debug$' | sort -u || true
  57. }
  58. # Resolve source path: common -> groups (alpha) -> host-specific (last wins)
  59. resolve_source() {
  60. local p="$1"
  61. local result="$dots_dir/$p"
  62. while IFS= read -r group; do
  63. if [[ -e "$script_dir/dots.@$group/$p" ]]; then
  64. result="$script_dir/dots.@$group/$p"
  65. fi
  66. done < <(get_groups)
  67. if [[ -e "$dots_host_dir/$p" ]]; then
  68. result="$dots_host_dir/$p"
  69. fi
  70. echo "$result"
  71. }
  72. # Check if a file contains @host or @group markers
  73. file_has_markers() {
  74. grep -qE '@(host|group) ' "$1" 2>/dev/null
  75. }
  76. # Check if any file in a directory tree contains markers
  77. dir_has_markers() {
  78. grep -rqlE '@(host|group) ' "$1" 2>/dev/null
  79. }
  80. # Detect the comment style from the first marker line in a file
  81. # Returns "block" for /* */ style, or the line-comment prefix (# // ; --)
  82. detect_comment_style() {
  83. local src="$1"
  84. local marker_line
  85. marker_line=$(grep -m1 -E '@(host|group) ' "$src")
  86. # Extract everything before @host or @group
  87. local prefix="${marker_line%%@*}"
  88. # Trim trailing whitespace
  89. prefix="${prefix%"${prefix##*[![:space:]]}"}"
  90. # Check if it's a block comment opener
  91. if [[ "$prefix" == "/*" ]]; then
  92. echo "block"
  93. else
  94. echo "$prefix"
  95. fi
  96. }
  97. # Process a file with @host/@group markers, outputting only matching sections
  98. process_markers() {
  99. local src="$1"
  100. local in_block=0
  101. local include_block=0
  102. # Pre-load groups into an associative array for fast lookup
  103. declare -A host_groups
  104. while IFS= read -r g; do
  105. host_groups["$g"]=1
  106. done < <(get_groups)
  107. while IFS= read -r line || [[ -n "$line" ]]; do
  108. if [[ $in_block -eq 0 ]]; then
  109. if [[ "$line" =~ @host[[:space:]]+([^[:space:]]+) ]]; then
  110. in_block=1
  111. if [[ "${BASH_REMATCH[1]}" == "$hostname_id" ]]; then
  112. include_block=1
  113. else
  114. include_block=0
  115. fi
  116. elif [[ "$line" =~ @group[[:space:]]+([^[:space:]]+) ]]; then
  117. in_block=1
  118. if [[ -v "host_groups[${BASH_REMATCH[1]}]" ]]; then
  119. include_block=1
  120. else
  121. include_block=0
  122. fi
  123. elif [[ "$line" =~ @end ]]; then
  124. # Stray @end outside a block, skip it
  125. continue
  126. else
  127. printf '%s\n' "$line"
  128. fi
  129. else
  130. if [[ "$line" =~ @end ]]; then
  131. in_block=0
  132. include_block=0
  133. elif [[ $include_block -eq 1 ]]; then
  134. printf '%s\n' "$line"
  135. fi
  136. fi
  137. done < "$src"
  138. if [[ $in_block -eq 1 ]]; then
  139. log_error "Unclosed @host/@group block in $src"
  140. return 1
  141. fi
  142. }
  143. # Ensure we cleanup on exit
  144. trap 'cd "$HOME"' EXIT
  145. # Create checkout directory if needed
  146. mkdir -p "$checkout_dir"
  147. sub_help(){
  148. local groups
  149. groups=$(get_groups | tr '\n' ', ' | sed 's/,$//')
  150. cat << EOF
  151. Usage: $ProgName <subcommand> [options]
  152. Subcommands:
  153. sync [--machine|--group <name>] [dot_file]
  154. Sync dotfiles (optionally add a new dotfile)
  155. --machine: add to host-specific config ($hostname_id)
  156. --group <name>: add to group-specific config
  157. list List currently synced dotfiles
  158. status Show git status of dotfiles repo
  159. packages [install|diff|save] Sync packages (additive: pkglist + pkglist.@<group>
  160. + pkglist.$hostname_id). Not run by 'sync'.
  161. Host: $hostname_id
  162. Groups: ${groups:-none}
  163. Resolution order: common -> groups (alpha) -> host-specific
  164. Common dotfiles: dots/ + to_sync
  165. Group dotfiles: dots.@<group>/ + to_sync.@<group>
  166. Host overrides: dots.$hostname_id/ + to_sync.$hostname_id
  167. In-file markers:
  168. # @host <name> ... # @end (host-specific block)
  169. # @group <name> ... # @end (group-specific block)
  170. Files with markers are processed and written (not symlinked).
  171. For help with each subcommand run:
  172. $ProgName <subcommand> -h|--help
  173. EOF
  174. }
  175. sub_sync(){
  176. # Clone repo if it doesn't exist
  177. if [[ ! -d "$checkout_dir/dotfiles/.git" ]]; then
  178. log_info "Cloning dotfiles repository..."
  179. cd "$checkout_dir"
  180. if ! git clone "$repo"; then
  181. log_error "Failed to clone repository"
  182. exit 1
  183. fi
  184. cd "$script_dir"
  185. git submodule init
  186. git submodule update
  187. fi
  188. # Navigate to script directory
  189. cd "$script_dir"
  190. # Get current branch
  191. current_branch=$(git branch --show-current)
  192. log_info "Pulling latest changes from $current_branch..."
  193. git pull origin "$current_branch"
  194. git submodule update --init --recursive
  195. # Parse flags for adding new dotfiles
  196. local machine_specific=0
  197. local group_target=""
  198. if [[ "${1:-}" == "--machine" ]]; then
  199. machine_specific=1
  200. shift
  201. elif [[ "${1:-}" == "--group" ]]; then
  202. group_target="${2:-}"
  203. if [[ -z "$group_target" ]]; then
  204. log_error "--group requires a group name"
  205. exit 1
  206. fi
  207. shift 2
  208. fi
  209. # Add new dotfile if provided
  210. if [ -n "${1:-}" ]; then
  211. local file_to_add="$1"
  212. # Validate that file exists
  213. if [[ ! -e "$HOME/$file_to_add" ]]; then
  214. log_error "File $HOME/$file_to_add does not exist"
  215. exit 1
  216. fi
  217. log_info "Adding new dotfile: $file_to_add"
  218. real_path=$(realpath "$HOME/$file_to_add")
  219. to_copy=${real_path#"$HOME/"}
  220. if [[ $machine_specific -eq 1 ]]; then
  221. target_dots_dir="$dots_host_dir"
  222. sync_file="$script_dir/to_sync.$hostname_id"
  223. log_info "Adding as host-specific ($hostname_id)"
  224. elif [[ -n "$group_target" ]]; then
  225. target_dots_dir="$script_dir/dots.@$group_target"
  226. sync_file="$script_dir/to_sync.@$group_target"
  227. log_info "Adding as group-specific (@$group_target)"
  228. else
  229. target_dots_dir="$dots_dir"
  230. sync_file="$script_dir/to_sync"
  231. fi
  232. # Create parent directory structure
  233. mkdir -p "$(dirname "$target_dots_dir/$to_copy")"
  234. # Copy to dots directory
  235. if cp -R "$real_path" "$target_dots_dir/$to_copy"; then
  236. log_info "Copied $file_to_add to $target_dots_dir/$to_copy"
  237. else
  238. log_error "Failed to copy $file_to_add"
  239. exit 1
  240. fi
  241. # Add to sync file if not already there
  242. if ! grep -qxF "$to_copy" "$sync_file" 2>/dev/null; then
  243. echo "$to_copy" >> "$sync_file"
  244. log_info "Added $to_copy to $(basename "$sync_file")"
  245. else
  246. log_info "$to_copy already in $(basename "$sync_file")"
  247. fi
  248. # Remove original and create symlink
  249. rm -rf "$real_path"
  250. ln -s "$target_dots_dir/$to_copy" "$real_path"
  251. log_info "Created symlink: $real_path -> $target_dots_dir/$to_copy"
  252. fi
  253. # Stage tracked files from all source directories
  254. log_info "Staging changes..."
  255. get_sync_list | while IFS= read -r p; do
  256. if [[ -e "$dots_dir/$p" ]]; then
  257. git add "$dots_dir/$p"
  258. fi
  259. while IFS= read -r group; do
  260. if [[ -e "$script_dir/dots.@$group/$p" ]]; then
  261. git add "$script_dir/dots.@$group/$p"
  262. fi
  263. done < <(get_groups)
  264. if [[ -e "$dots_host_dir/$p" ]]; then
  265. git add "$dots_host_dir/$p"
  266. fi
  267. done
  268. git add start to_sync
  269. # Stage host-specific, group-specific, and groups files
  270. for f in to_sync.* groups.*; do
  271. [[ -f "$f" ]] && git add "$f"
  272. done
  273. for d in dots.*/; do
  274. [[ -d "$d" ]] && git add "$d"
  275. done
  276. # Commit and push if there are changes
  277. if git diff --cached --quiet; then
  278. log_info "No changes to commit"
  279. else
  280. log_info "Committing changes..."
  281. if git commit -m "Sync: $(date '+%Y-%m-%d %H:%M:%S')"; then
  282. log_info "Pushing to remote..."
  283. git remote add origin "$repo" 2>/dev/null || true
  284. if git push -u origin "$current_branch"; then
  285. log_info "Successfully pushed changes"
  286. else
  287. log_error "Failed to push changes"
  288. exit 1
  289. fi
  290. else
  291. log_error "Failed to commit changes"
  292. exit 1
  293. fi
  294. fi
  295. # Deploy a single file: process markers or symlink
  296. deploy_file() {
  297. local src="$1"
  298. local dest="$2"
  299. mkdir -p "$(dirname "$dest")"
  300. if file_has_markers "$src"; then
  301. # File has markers — process and write (not symlink)
  302. if [[ -L "$dest" ]]; then
  303. rm "$dest"
  304. fi
  305. local comment_style
  306. comment_style=$(detect_comment_style "$src")
  307. {
  308. if [[ "$comment_style" == "block" ]]; then
  309. echo "/* DO NOT EDIT - Generated by dotsync from: $src"
  310. echo " Regenerate with: dotsync */"
  311. else
  312. echo "$comment_style DO NOT EDIT - Generated by dotsync from:"
  313. echo "$comment_style $src"
  314. echo "$comment_style Regenerate with: dotsync"
  315. fi
  316. echo ""
  317. process_markers "$src"
  318. } > "$dest.dotsync_tmp"
  319. # Only update if content changed
  320. if [[ -f "$dest" ]] && cmp -s "$dest" "$dest.dotsync_tmp"; then
  321. rm "$dest.dotsync_tmp"
  322. else
  323. [[ -e "$dest" ]] && rm "$dest"
  324. mv "$dest.dotsync_tmp" "$dest"
  325. chmod --reference="$src" "$dest"
  326. log_info "Processed: $dest (from $src)"
  327. fi
  328. else
  329. # No markers — symlink
  330. if [[ -L "$dest" ]] && [[ "$(readlink "$dest")" == "$src" ]]; then
  331. return # Already correctly linked
  332. fi
  333. if [[ -e "$dest" ]] || [[ -L "$dest" ]]; then
  334. log_warn "Removing existing $dest"
  335. rm -rf "$dest"
  336. fi
  337. if ln -s "$src" "$dest"; then
  338. log_info "Linked: $dest -> $src"
  339. else
  340. log_error "Failed to create symlink for $(basename "$dest")"
  341. return 1
  342. fi
  343. fi
  344. }
  345. # Deploy dotfiles (symlink or process markers)
  346. log_info "Deploying dotfiles for $hostname_id..."
  347. get_sync_list | while IFS= read -r p; do
  348. local source
  349. source=$(resolve_source "$p")
  350. local destination="$HOME/$p"
  351. if [[ ! -e "$source" ]]; then
  352. log_warn "Source $source does not exist, skipping"
  353. continue
  354. fi
  355. if [[ -d "$source" ]]; then
  356. if dir_has_markers "$source"; then
  357. # Directory contains markers — expand into per-file operations
  358. # Remove directory-level symlink if present
  359. if [[ -L "$destination" ]]; then
  360. log_warn "Replacing directory symlink $destination with expanded files"
  361. rm "$destination"
  362. fi
  363. mkdir -p "$destination"
  364. # Walk all files in the source directory
  365. while IFS= read -r src_file; do
  366. local rel="${src_file#"$source/"}"
  367. deploy_file "$src_file" "$destination/$rel"
  368. done < <(find "$source" -type f | sort)
  369. else
  370. # No markers in directory — symlink the whole directory
  371. if [[ -L "$destination" ]] && [[ "$(readlink "$destination")" == "$source" ]]; then
  372. continue # Already correctly linked
  373. fi
  374. if [[ -e "$destination" ]] || [[ -L "$destination" ]]; then
  375. log_warn "Removing existing $destination"
  376. rm -rf "$destination"
  377. fi
  378. mkdir -p "$(dirname "$destination")"
  379. if ln -s "$source" "$destination"; then
  380. log_info "Linked: $destination -> $source"
  381. else
  382. log_error "Failed to create symlink for $p"
  383. exit 1
  384. fi
  385. fi
  386. else
  387. # Single file entry
  388. deploy_file "$source" "$destination"
  389. fi
  390. done
  391. log_info "Sync complete!"
  392. }
  393. sub_list(){
  394. echo "Host: $hostname_id"
  395. local groups
  396. groups=$(get_groups | tr '\n' ', ' | sed 's/,$//')
  397. if [[ -n "$groups" ]]; then
  398. echo "Groups: $groups"
  399. fi
  400. echo ""
  401. if [[ -f "$script_dir/to_sync" ]]; then
  402. echo "Common dotfiles (to_sync):"
  403. while IFS= read -r p; do
  404. [[ -z "$p" ]] && continue
  405. local source
  406. source=$(resolve_source "$p")
  407. local tag=""
  408. if [[ "$source" == "$dots_host_dir/"* ]]; then
  409. tag=" [override: $hostname_id]"
  410. elif [[ "$source" == *"/dots.@"* ]]; then
  411. local gname
  412. gname=$(echo "$source" | sed 's|.*dots\.@\([^/]*\)/.*|\1|')
  413. tag=" [override: @$gname]"
  414. fi
  415. local marker_tag=""
  416. if [[ -f "$source" ]] && file_has_markers "$source"; then
  417. marker_tag=" [processed]"
  418. fi
  419. if [[ -L "$HOME/$p" ]] || [[ -f "$HOME/$p" ]] || [[ -d "$HOME/$p" ]]; then
  420. echo " + $p$tag$marker_tag"
  421. else
  422. echo " - $p (not deployed)$tag$marker_tag"
  423. fi
  424. done < "$script_dir/to_sync"
  425. fi
  426. # Group-specific dotfiles
  427. while IFS= read -r group; do
  428. local gsync="$script_dir/to_sync.@$group"
  429. if [[ -f "$gsync" ]] && [[ -s "$gsync" ]]; then
  430. echo ""
  431. echo "Group dotfiles (to_sync.@$group):"
  432. while IFS= read -r p; do
  433. [[ -z "$p" ]] && continue
  434. if [[ -L "$HOME/$p" ]] || [[ -f "$HOME/$p" ]] || [[ -d "$HOME/$p" ]]; then
  435. echo " + $p"
  436. else
  437. echo " - $p (not deployed)"
  438. fi
  439. done < "$gsync"
  440. fi
  441. done < <(get_groups)
  442. # Host-specific dotfiles
  443. if [[ -f "$script_dir/to_sync.$hostname_id" ]] && [[ -s "$script_dir/to_sync.$hostname_id" ]]; then
  444. echo ""
  445. echo "Host-specific dotfiles (to_sync.$hostname_id):"
  446. while IFS= read -r p; do
  447. [[ -z "$p" ]] && continue
  448. if [[ -L "$HOME/$p" ]] || [[ -f "$HOME/$p" ]] || [[ -d "$HOME/$p" ]]; then
  449. echo " + $p"
  450. else
  451. echo " - $p (not deployed)"
  452. fi
  453. done < "$script_dir/to_sync.$hostname_id"
  454. fi
  455. }
  456. sub_status(){
  457. if [[ ! -d "$script_dir/.git" ]]; then
  458. log_error "Dotfiles repository not found"
  459. exit 1
  460. fi
  461. cd "$script_dir"
  462. log_info "Host: $hostname_id"
  463. log_info "Git status:"
  464. git status
  465. }
  466. sub_packages(){
  467. local action=${1:-install}
  468. if [[ "$action" == "-h" || "$action" == "--help" ]]; then
  469. cat << EOF
  470. Usage: $ProgName packages [install|diff|save]
  471. install Install tracked packages missing on this host (default)
  472. diff Show drift in both directions, change nothing
  473. save Write this host's explicit packages to pkglist.$hostname_id
  474. Lists are additive and resolved as:
  475. pkglist + pkglist.@<group> + pkglist.$hostname_id
  476. Not run by '$ProgName sync': installing needs sudo and takes time, whereas
  477. sync is unprivileged and fast. Keep them separate.
  478. EOF
  479. return 0
  480. fi
  481. local tracked installed missing extra
  482. tracked=$(get_pkg_list)
  483. if [[ -z "$tracked" ]]; then
  484. log_error "No package lists found (pkglist, pkglist.@<group>, pkglist.$hostname_id)"
  485. log_error "Seed one with: $ProgName packages save"
  486. return 1
  487. fi
  488. installed=$(installed_packages)
  489. missing=$(comm -23 <(echo "$tracked") <(echo "$installed"))
  490. extra=$(comm -13 <(echo "$tracked") <(echo "$installed"))
  491. case $action in
  492. install)
  493. if [[ -z "$missing" ]]; then
  494. log_info "All $(echo "$tracked" | wc -l) tracked packages already installed"
  495. return 0
  496. fi
  497. log_info "Installing $(echo "$missing" | wc -l) missing package(s):"
  498. echo "$missing" | sed 's/^/ /'
  499. # Pass targets as arguments rather than piping to stdin: yay opens
  500. # /dev/tty for its prompts, and feeding it stdin breaks that.
  501. echo "$missing" | xargs -r yay -S --needed
  502. ;;
  503. diff)
  504. if [[ -n "$missing" ]]; then
  505. log_warn "Tracked but NOT installed ($(echo "$missing" | wc -l)):"
  506. echo "$missing" | sed 's/^/ /'
  507. fi
  508. if [[ -n "$extra" ]]; then
  509. log_warn "Installed but NOT tracked ($(echo "$extra" | wc -l)):"
  510. echo "$extra" | sed 's/^/ /'
  511. fi
  512. if [[ -z "$missing" && -z "$extra" ]]; then
  513. log_info "In sync: $(echo "$tracked" | wc -l) packages"
  514. fi
  515. ;;
  516. save)
  517. echo "$installed" > "$script_dir/pkglist.$hostname_id"
  518. log_info "Wrote $(echo "$installed" | wc -l) packages to pkglist.$hostname_id"
  519. log_warn "Move entries shared with other hosts into pkglist so they are not duplicated"
  520. ;;
  521. *)
  522. log_error "Unknown packages action: '$action'"
  523. log_error "Run '$ProgName packages --help' for usage."
  524. return 1
  525. ;;
  526. esac
  527. }
  528. subcommand=${1:-}
  529. case $subcommand in
  530. "" | "-h" | "--help")
  531. sub_help
  532. ;;
  533. sync|list|status|packages)
  534. shift
  535. "sub_${subcommand}" "$@"
  536. ;;
  537. *)
  538. log_error "'$subcommand' is not a known subcommand."
  539. echo " Run '$ProgName --help' for a list of known subcommands." >&2
  540. exit 1
  541. ;;
  542. esac