Make setup.sh only backup changed files, and output what those changes are. Allow for dry-runs too.
David Blume

David Blume commited on 2016-01-16 12:03:26
Showing 2 changed files, with 50 additions and 14 deletions.

... ...
@@ -24,8 +24,13 @@ If you're not cloning the repo, then run the following:
24 24
     dotfiles$ tar -xvf dotfiles.tar
25 25
     dotfiles$ rm dotfiles.tar
26 26
 
27
-Then, when you run `setup.sh`, it'll backup your old files to `backup_of_dotfiles_<date>`
28
-and replace them with the ones here.
27
+Then, when you run `setup.sh`, it'll backup your changed files to `backup_of_dotfiles_<date>`
28
+and replace them with the ones here. You can perform a **dry run** to see which files will
29
+be changed by passing the "-n" parameter.
30
+
31
+    dotfiles$ ./setup.sh -n
32
+
33
+If you approve of the changes, then just run `setup.sh`
29 34
 
30 35
     dotfiles$ ./setup.sh
31 36
 
... ...
@@ -1,39 +1,70 @@
1 1
 #!/usr/bin/env bash
2 2
 set -eu -o pipefail # See: https://sipb.mit.edu/doc/safe-shell/
3 3
 
4
+declare -r SCRIPT_NAME=$(basename "$BASH_SOURCE")
4 5
 declare -r backup_dir=$HOME/backup_of_dotfiles_`date "+%Y-%m-%d"`
5 6
 declare -a dotfiles=(".bashrc" ".bash_profile" ".vimrc" 
6 7
                      ".gitconfig" ".gitignore" ".inputrc")
8
+declare -i DRY_RUN=0
9
+
10
+## exit the shell (with status 2) after printing the message
11
+usage() {
12
+    echo "\
13
+$SCRIPT_NAME -hn
14
+    -h      Print this help text
15
+    -n      Perform a dry run, to see what'll change
16
+"
17
+    exit 2;
18
+}
19
+
20
+## Process the options
21
+while getopts "hn" OPTION
22
+do
23
+  case $OPTION in
24
+    h) usage;;
25
+    n) DRY_RUN=1;;
26
+    \?) usage;;
27
+  esac
28
+done
7 29
 
8 30
 if [ ! -d $backup_dir ]; then
9
-    mkdir -p $backup_dir
31
+    ((DRY_RUN==0)) && mkdir -p $backup_dir
10 32
 fi
11 33
 
12 34
 # Move original dot files to backup
13 35
 for i in "${dotfiles[@]}"
14 36
 do
15 37
     if [ -e $HOME/"$i" ]; then
38
+        if ! cmp --silent $HOME/"$i" "$i" ; then
39
+	    echo "$i" will be changed as follows:
40
+	    diff $HOME/"$i" "$i" || true
41
+            if [ $DRY_RUN -eq 0 ]; then
16 42
                 mv $HOME/"$i" $backup_dir
43
+                # Consider using symbolic links instead
44
+                # so pulling updates automatically apply
45
+                cp "$i" $HOME
46
+            fi
47
+	else
48
+	    echo No change to "$i".
49
+	fi
50
+    else
51
+        echo "$i" will be added to HOME.
52
+	((DRY_RUN==0)) && cp "$i" $HOME
17 53
     fi
18 54
 done
55
+
56
+if [ $DRY_RUN -eq 0 ]; then
19 57
     if [ -d $HOME/.vim ]; then
20 58
         mv $HOME/.vim $backup_dir
21 59
     fi
22
-
23
-echo Note: Your old dotfiles are backed up to $backup_dir
24
-
25
-# Move new dot files in.
26
-# If you cloned the repo, consider making symbolic links instead,
27
-# to more easily keep this home directory current by pulling updates.
28
-for i in "${dotfiles[@]}"
29
-do
30
-    cp "$i" $HOME
31
-done
32 60
     cp -r .vim $HOME
61
+fi
62
+
63
+echo Your old dotfiles are backed up to $backup_dir
33 64
 
34 65
 # Make a directory for vim undo
35 66
 if [ ! -d $HOME/.vim_undo ]; then
36
-    mkdir -p $HOME/.vim_undo
67
+    ((DRY_RUN==0)) && mkdir -p $HOME/.vim_undo
37 68
 fi
38 69
 
39 70
 # Tell David what's left.
40 71