start 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. [[ -f bootstrap ]] && git add bootstrap
  270. # Stage host-specific, group-specific, and groups files, plus package lists
  271. for f in to_sync.* groups.* pkglist pkglist.*; do
  272. [[ -f "$f" ]] && git add "$f" || true
  273. done
  274. for d in dots.*/; do
  275. [[ -d "$d" ]] && git add "$d"
  276. done
  277. # Commit and push if there are changes
  278. if git diff --cached --quiet; then
  279. log_info "No changes to commit"
  280. else
  281. log_info "Committing changes..."
  282. if git commit -m "Sync: $(date '+%Y-%m-%d %H:%M:%S')"; then
  283. log_info "Pushing to remote..."
  284. git remote add origin "$repo" 2>/dev/null || true
  285. if git push -u origin "$current_branch"; then
  286. log_info "Successfully pushed changes"
  287. else
  288. log_error "Failed to push changes"
  289. exit 1
  290. fi
  291. else
  292. log_error "Failed to commit changes"
  293. exit 1
  294. fi
  295. fi
  296. # Deploy a single file: process markers or symlink
  297. deploy_file() {
  298. local src="$1"
  299. local dest="$2"
  300. mkdir -p "$(dirname "$dest")"
  301. if file_has_markers "$src"; then
  302. # File has markers — process and write (not symlink)
  303. if [[ -L "$dest" ]]; then
  304. rm "$dest"
  305. fi
  306. local comment_style
  307. comment_style=$(detect_comment_style "$src")
  308. {
  309. if [[ "$comment_style" == "block" ]]; then
  310. echo "/* DO NOT EDIT - Generated by dotsync from: $src"
  311. echo " Regenerate with: dotsync */"
  312. else
  313. echo "$comment_style DO NOT EDIT - Generated by dotsync from:"
  314. echo "$comment_style $src"
  315. echo "$comment_style Regenerate with: dotsync"
  316. fi
  317. echo ""
  318. process_markers "$src"
  319. } > "$dest.dotsync_tmp"
  320. # Only update if content changed
  321. if [[ -f "$dest" ]] && cmp -s "$dest" "$dest.dotsync_tmp"; then
  322. rm "$dest.dotsync_tmp"
  323. else
  324. [[ -e "$dest" ]] && rm "$dest"
  325. mv "$dest.dotsync_tmp" "$dest"
  326. chmod --reference="$src" "$dest"
  327. log_info "Processed: $dest (from $src)"
  328. fi
  329. else
  330. # No markers — symlink
  331. if [[ -L "$dest" ]] && [[ "$(readlink "$dest")" == "$src" ]]; then
  332. return # Already correctly linked
  333. fi
  334. if [[ -e "$dest" ]] || [[ -L "$dest" ]]; then
  335. log_warn "Removing existing $dest"
  336. rm -rf "$dest"
  337. fi
  338. if ln -s "$src" "$dest"; then
  339. log_info "Linked: $dest -> $src"
  340. else
  341. log_error "Failed to create symlink for $(basename "$dest")"
  342. return 1
  343. fi
  344. fi
  345. }
  346. # Deploy dotfiles (symlink or process markers)
  347. log_info "Deploying dotfiles for $hostname_id..."
  348. get_sync_list | while IFS= read -r p; do
  349. local source
  350. source=$(resolve_source "$p")
  351. local destination="$HOME/$p"
  352. if [[ ! -e "$source" ]]; then
  353. log_warn "Source $source does not exist, skipping"
  354. continue
  355. fi
  356. if [[ -d "$source" ]]; then
  357. if dir_has_markers "$source"; then
  358. # Directory contains markers — expand into per-file operations
  359. # Remove directory-level symlink if present
  360. if [[ -L "$destination" ]]; then
  361. log_warn "Replacing directory symlink $destination with expanded files"
  362. rm "$destination"
  363. fi
  364. mkdir -p "$destination"
  365. # Walk all files in the source directory
  366. while IFS= read -r src_file; do
  367. local rel="${src_file#"$source/"}"
  368. deploy_file "$src_file" "$destination/$rel"
  369. done < <(find "$source" -type f | sort)
  370. else
  371. # No markers in directory — symlink the whole directory
  372. if [[ -L "$destination" ]] && [[ "$(readlink "$destination")" == "$source" ]]; then
  373. continue # Already correctly linked
  374. fi
  375. if [[ -e "$destination" ]] || [[ -L "$destination" ]]; then
  376. log_warn "Removing existing $destination"
  377. rm -rf "$destination"
  378. fi
  379. mkdir -p "$(dirname "$destination")"
  380. if ln -s "$source" "$destination"; then
  381. log_info "Linked: $destination -> $source"
  382. else
  383. log_error "Failed to create symlink for $p"
  384. exit 1
  385. fi
  386. fi
  387. else
  388. # Single file entry
  389. deploy_file "$source" "$destination"
  390. fi
  391. done
  392. log_info "Sync complete!"
  393. }
  394. sub_list(){
  395. echo "Host: $hostname_id"
  396. local groups
  397. groups=$(get_groups | tr '\n' ', ' | sed 's/,$//')
  398. if [[ -n "$groups" ]]; then
  399. echo "Groups: $groups"
  400. fi
  401. echo ""
  402. if [[ -f "$script_dir/to_sync" ]]; then
  403. echo "Common dotfiles (to_sync):"
  404. while IFS= read -r p; do
  405. [[ -z "$p" ]] && continue
  406. local source
  407. source=$(resolve_source "$p")
  408. local tag=""
  409. if [[ "$source" == "$dots_host_dir/"* ]]; then
  410. tag=" [override: $hostname_id]"
  411. elif [[ "$source" == *"/dots.@"* ]]; then
  412. local gname
  413. gname=$(echo "$source" | sed 's|.*dots\.@\([^/]*\)/.*|\1|')
  414. tag=" [override: @$gname]"
  415. fi
  416. local marker_tag=""
  417. if [[ -f "$source" ]] && file_has_markers "$source"; then
  418. marker_tag=" [processed]"
  419. fi
  420. if [[ -L "$HOME/$p" ]] || [[ -f "$HOME/$p" ]] || [[ -d "$HOME/$p" ]]; then
  421. echo " + $p$tag$marker_tag"
  422. else
  423. echo " - $p (not deployed)$tag$marker_tag"
  424. fi
  425. done < "$script_dir/to_sync"
  426. fi
  427. # Group-specific dotfiles
  428. while IFS= read -r group; do
  429. local gsync="$script_dir/to_sync.@$group"
  430. if [[ -f "$gsync" ]] && [[ -s "$gsync" ]]; then
  431. echo ""
  432. echo "Group dotfiles (to_sync.@$group):"
  433. while IFS= read -r p; do
  434. [[ -z "$p" ]] && continue
  435. if [[ -L "$HOME/$p" ]] || [[ -f "$HOME/$p" ]] || [[ -d "$HOME/$p" ]]; then
  436. echo " + $p"
  437. else
  438. echo " - $p (not deployed)"
  439. fi
  440. done < "$gsync"
  441. fi
  442. done < <(get_groups)
  443. # Host-specific dotfiles
  444. if [[ -f "$script_dir/to_sync.$hostname_id" ]] && [[ -s "$script_dir/to_sync.$hostname_id" ]]; then
  445. echo ""
  446. echo "Host-specific dotfiles (to_sync.$hostname_id):"
  447. while IFS= read -r p; do
  448. [[ -z "$p" ]] && continue
  449. if [[ -L "$HOME/$p" ]] || [[ -f "$HOME/$p" ]] || [[ -d "$HOME/$p" ]]; then
  450. echo " + $p"
  451. else
  452. echo " - $p (not deployed)"
  453. fi
  454. done < "$script_dir/to_sync.$hostname_id"
  455. fi
  456. }
  457. sub_status(){
  458. if [[ ! -d "$script_dir/.git" ]]; then
  459. log_error "Dotfiles repository not found"
  460. exit 1
  461. fi
  462. cd "$script_dir"
  463. log_info "Host: $hostname_id"
  464. log_info "Git status:"
  465. git status
  466. }
  467. sub_packages(){
  468. local action=${1:-install}
  469. if [[ "$action" == "-h" || "$action" == "--help" ]]; then
  470. cat << EOF
  471. Usage: $ProgName packages [install|diff|save]
  472. install Install tracked packages missing on this host (default)
  473. diff Show drift in both directions, change nothing
  474. save Write this host's explicit packages to pkglist.$hostname_id
  475. Lists are additive and resolved as:
  476. pkglist + pkglist.@<group> + pkglist.$hostname_id
  477. Not run by '$ProgName sync': installing needs sudo and takes time, whereas
  478. sync is unprivileged and fast. Keep them separate.
  479. EOF
  480. return 0
  481. fi
  482. local tracked installed missing extra
  483. tracked=$(get_pkg_list)
  484. if [[ -z "$tracked" ]]; then
  485. log_error "No package lists found (pkglist, pkglist.@<group>, pkglist.$hostname_id)"
  486. log_error "Seed one with: $ProgName packages save"
  487. return 1
  488. fi
  489. installed=$(installed_packages)
  490. missing=$(comm -23 <(echo "$tracked") <(echo "$installed"))
  491. extra=$(comm -13 <(echo "$tracked") <(echo "$installed"))
  492. case $action in
  493. install)
  494. if [[ -z "$missing" ]]; then
  495. log_info "All $(echo "$tracked" | wc -l) tracked packages already installed"
  496. return 0
  497. fi
  498. log_info "Installing $(echo "$missing" | wc -l) missing package(s):"
  499. echo "$missing" | sed 's/^/ /'
  500. # Build an argv array and invoke yay directly -- no pipe, no xargs.
  501. # yay is interactive (cleanBuild, conflict and diff prompts), so it
  502. # needs to inherit the terminal's stdin. Piping to it steals stdin;
  503. # xargs is worse, because it points the child's stdin at /dev/null
  504. # and every prompt immediately reads EOF.
  505. local -a pkgs
  506. mapfile -t pkgs <<< "$missing"
  507. yay -S --needed "${pkgs[@]}"
  508. ;;
  509. diff)
  510. if [[ -n "$missing" ]]; then
  511. log_warn "Tracked but NOT installed ($(echo "$missing" | wc -l)):"
  512. echo "$missing" | sed 's/^/ /'
  513. fi
  514. if [[ -n "$extra" ]]; then
  515. log_warn "Installed but NOT tracked ($(echo "$extra" | wc -l)):"
  516. echo "$extra" | sed 's/^/ /'
  517. fi
  518. if [[ -z "$missing" && -z "$extra" ]]; then
  519. log_info "In sync: $(echo "$tracked" | wc -l) packages"
  520. fi
  521. ;;
  522. save)
  523. echo "$installed" > "$script_dir/pkglist.$hostname_id"
  524. log_info "Wrote $(echo "$installed" | wc -l) packages to pkglist.$hostname_id"
  525. log_warn "Move entries shared with other hosts into pkglist so they are not duplicated"
  526. ;;
  527. *)
  528. log_error "Unknown packages action: '$action'"
  529. log_error "Run '$ProgName packages --help' for usage."
  530. return 1
  531. ;;
  532. esac
  533. }
  534. subcommand=${1:-}
  535. case $subcommand in
  536. "" | "-h" | "--help")
  537. sub_help
  538. ;;
  539. sync|list|status|packages)
  540. shift
  541. "sub_${subcommand}" "$@"
  542. ;;
  543. *)
  544. log_error "'$subcommand' is not a known subcommand."
  545. echo " Run '$ProgName --help' for a list of known subcommands." >&2
  546. exit 1
  547. ;;
  548. esac