Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env bash
# Keep this script in a directory with the master copy of your dotfiles.
# For example, keep this script in a dotfiles folder in your Dropbox.
# Run this script to diff your dotfiles between the master copy (in the pwd)
# and the dotfiles installed at ~.
show_help() {
cat <<EOF
Usage: diff-dotfiles.sh OPTION [filename]
If <filename> is present, diff that dotfile. Otherwise, diff all of them,
showing the diff between the repository version and the version currently
installed.
The default output is a unified diff, so you should be able to do things like
$ ./diff-dotfiles.sh .foo | patch ~/.foo
OPTIONS:
-h Show help and exit.
-m Use meld to diff and edit file(s).
-l List files that are different.
Ex:
./diff-dotfiles.sh -l
./diff-dotfiles.sh -m .gitignore
EOF
exit 0
}
# Parse command line options.
difftool="diff -u --color"
list_files=0
while getopts hml opt; do
case $opt in
h)
show_help
;;
m)
difftool="meld"
shift
;;
l)
list_files=1
shift
;;
*)
show_help
;;
esac
done
# If listing files, do that.
if [[ $list_files -eq 1 ]]; then
echo "The following files have differences:"
for filename in .[!.]*; do
if [[ "$filename" == ".git" ]]; then
continue
fi
diff $filename ~/$filename > /dev/null
if [[ $? -ne 0 ]]; then
echo " $filename"
fi
done
exit 0
fi
# Meld with no args should do a directory diff.
if [[ "$difftool" == "meld" && -z "$1" ]]; then
$difftool ~ ./
exit 0
fi
# Diff either the whole dir or just our files, with the correct difftool.
if [[ ! -z "$1" ]]; then
$difftool ~/$1 $1
else
for filename in .[!.]*; do
if [[ "$filename" == ".git" ]]; then
continue
fi
$difftool ~/$filename $filename
echo -e "\n"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment