From 2b05ae3f305220520170f156193781d8f627411d Mon Sep 17 00:00:00 2001 From: binaryDiv Date: Sat, 5 Jan 2019 05:31:56 +0100 Subject: [PATCH] dotfiles script v0.4.0 - add 'linkstatus' command --- bin/dotfiles | 68 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 8 deletions(-) diff --git a/bin/dotfiles b/bin/dotfiles index 08dd4ee..fca5d74 100755 --- a/bin/dotfiles +++ b/bin/dotfiles @@ -2,7 +2,7 @@ ## ## binaryDiv's dotfiles management script -## Version 0.3.1 +## Version 0.4.0 ## usage() { @@ -18,10 +18,12 @@ usage() { (this will NOT create links for all dotfiles, though) Dotfile management: - ls [DIR] list contents of .dotfiles directory (or DIR relative to it) - diff FILE use vimdiff to compare ~/FILE to ~/.dotfiles/FILE - link FILE... create a symlink from ~/FILE to ~/.dotfiles/FILE (multiple files possible) - (-q: be quiet, only print something if an error occurs) + ls [DIR] list contents of .dotfiles directory (or DIR relative to it) + diff FILE use vimdiff to compare ~/FILE to ~/.dotfiles/FILE + link FILE... create a symlink from ~/FILE to ~/.dotfiles/FILE (multiple files possible) + (-q: be quiet, only print something if an error occurs) + linkstatus [DIR] list contents of .dotfiles with information about whether there are links to + them in ~/, whether they differ, etc... Synchronization: git ARGS... wrapper for git (all arguments are passed on to git) @@ -89,7 +91,57 @@ case "$cmd" in ls -lAh --color=auto "$lsdir" ;; - # TODO: Command 'status' or 'linkstatus' to show which files are linked and which are not + linkstatus) + subdir="" + if [[ -n $1 ]]; then + subdir="${1%/}" + [[ -e $subdir ]] || { echo "$dotfilesdir/$subdir does not exist"; exit 1; } + [[ -d $subdir ]] && subdir="$subdir/" + fi + + shopt -s nullglob dotglob + cd $dotfilesdir + dotdirs=() + + for file in $subdir*; do + [[ -e $file ]] || continue + [[ $file = ".git" ]] && continue + + file_home="$homedir/$file" + file_dotfiles="$dotfilesdir/$file" + + if [[ -e $file_home ]]; then + if [[ "$(realpath "$file_home")" -ef "$file_dotfiles" ]]; then + file_status=" LINKED " + elif [[ -d $file_home ]]; then + dotdirs+=($file) + continue + elif [[ -f $file_home ]] && cmp --silent "$file_home" "$file_dotfiles"; then + file_status="== EQUAL " + else + file_status="<> DIFFERS " + fi + else + if [[ -d $file_dotfiles ]]; then + file_status="++ NEW DIR " + else + file_status="++ NEW FILE" + fi + fi + + [[ -d $file_dotfiles ]] && file="$file/" + echo "$file_status $file" + done + + if [[ $dotdirs ]]; then + echo + for dir in ${dotdirs[@]}; do + echo "[$dir]" + dotfiles linkstatus "$dir" | grep '\(LINKED\|EQUAL\|DIFFERS\|NEW\)' + echo + done + fi + ;; diff) [[ -n $1 ]] || { echo "$(basename $0) $cmd: Missing argument."; exit 1; } @@ -148,7 +200,7 @@ case "$cmd" in _dotfiles() { COMPREPLY=() local cur="${COMP_WORDS[COMP_CWORD]}" - local commands="help install ls diff link git pull" + local commands="help install ls diff link linkstatus git pull" local gitcommands="add commit diff fetch log pull push rebase status" if [[ $COMP_CWORD -eq 1 ]]; then @@ -159,7 +211,7 @@ case "$cmd" in [[ $COMP_CWORD -eq 2 ]] && _dotfiles_filenames ;; - link) + link|linkstatus) _dotfiles_filenames ;;