DEV Community

Discussion on: 2 minutes to stow

Collapse
 
grendel profile image
grendel

i just have my ~/.dot repository mirror the directory structure that it's supposed to have from $HOME, and run this script after i git pull (which i just realized i can invoke via post-merge git hook):

#!/bin/bash

cd "$(dirname -- "$0")"

warn="\033[33;1m"   # Bold Yellow
new="\033[32;1m"    # Bold Green
plain="\033[0m"     # Reset text style
cols=$(tput cols)


puts() {
    local -n color=$1; shift
    printf -- '%b%s%b\n' "$color" "$*" "$plain"
}


find . -type f \
        -not -name "$(basename -- "$0")" \
        -and -not -regex '.*/\(stay\|\.git\).*' \
        -printf '%P\n' \
        | while IFS= read -r file
do
    dest="$HOME/$file"

    mkdir -p -- "${dest%/*}" || continue

    if [[ ! -f $dest ]]; then
        puts new 'Creating $dest...'
        ln -- "$file" "$dest"
        continue
    fi

    if diff -- "$file" "$dest" >/dev/null; then
        ln -f -- "$file" "$dest"
        continue
    fi

    msg="- Updates to $file "
    printf -v bar '%*s' $(( cols - ${#msg} ))
    puts warn "$msg" "${bar// /–}"

    diff --color -u3 -- "$dest" "$file"

    printf %b "$warn"
        printf -v bar '%*s' $cols; printf '%s\n' "${bar// /_}"
        ln -i -- "$file" "$dest"
    printf %b "$plain"
done
Enter fullscreen mode Exit fullscreen mode

note: i also have a ~/.dot/stay directory, where i put things that i don't want to hardlink to anywhere