-
Notifications
You must be signed in to change notification settings - Fork 2
/
.setup_dotfiles.sh
executable file
·101 lines (83 loc) · 2.31 KB
/
.setup_dotfiles.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
set -o nounset
set -o errexit
PROG=$(basename "$0")
# cd to the directory containing this script.
cd "$( dirname "${BASH_SOURCE[0]}" )"
THIS_DIR=$(pwd)
usage() {
cat <<EOF
USAGE: $PROG [-f|--force] [-b <branch>|-B <branch>]
Checks out the dotfiles from $THIS_DIR into \$HOME.
Options:
-b <branch> Create new branch <branch> for \$HOME (default "home")
-B <branch> Use existing branch <branch> for \$HOME; use with care.
-f, --force Overwrite locally modified versions of dotfiles in \$HOME.
EOF
}
OPTS=$(getopt -o 'hfb:B:' -l 'help,force' -n "$PROG" -- "$@")
eval set -- "$OPTS"
FORCE=
BRANCH_FLAG="-b home --track"
COMMITISH=master
while (( $# > 0 )); do
case "$1" in
-f|--force)
FORCE=-f
shift
;;
-b)
BRANCH_FLAG="-b $2 --track"
COMMITISH=master
shift 2
;;
-B)
BRANCH_FLAG=
COMMITISH="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
--)
shift
;;
-*)
echo "$PROG: programming error - unrecognized option '$1'" >&2
exit 2
;;
*)
# No positional arguments expected
usage >&2
exit 2
;;
esac
done
echo "Creating fake worktree ~/.dotfiles"
git worktree add $BRANCH_FLAG --no-checkout ~/.dotfiles $COMMITISH
cat >~/.dotfiles/README <<EOF
This is a fake git worktree. The actual worktree is $HOME.
See $THIS_DIR/README.md for more info.
EOF
GD_ALIAS='git --git-dir="$HOME/.dotfiles/.git" --work-tree="$HOME"'
gd() { git --git-dir="$HOME/.dotfiles/.git" --work-tree="$HOME" "$@"; }
# Go to the home directory so all paths are printed nicely, not "../../<file>".
cd
# `git worktree add --no-checkout` leaves the index in a funky state; reset it
# so that we see what files are missing.
gd reset --quiet
echo "Checking out files to $HOME"
RC=0
gd checkout-index -a $FORCE || RC=$?
cat <<EOF
Done! Use the following alias to manage the dotfiles:
alias gd='$GD_ALIAS'
EOF
if (( RC != 0 )); then
cat <<EOF
WARNING: Some files were left unmodified. Use the following to check them out:
gd checkout -p ~
Remove the -p to simply blow away the existing versions.
EOF
fi