-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdot
executable file
·134 lines (122 loc) · 2.87 KB
/
mdot
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/zsh
dot_clean_up(){
unset GIT_DIR
unset GIT_WORK_TREE
unset DOTREMOTE
unset DOTDEST
unset DOTGIT
cd $OLDPWD
}
dot_success(){
dot_clean_up
exit 0
}
dot_fail(){
dot_clean_up
echo "dot failed. See traceback for more information."
exit 1
}
# Read config from current dir
source dotrc 2> /dev/null
# Read config from $HOME/.dotrc
source $HOME/.dotrc 2> /dev/null
if [ -z "$DOTGIT" ];then
DOTGIT=${HOME}/dotfiles.git
fi
if [ -z "$DOTDEST" ];then
DOTDEST=$HOME
fi
if [ -z "$DOTREMOTE" ];then
echo "You have to define the remote location of your dotfile repository."
echo "Set DOTREMOTE or edit your ~/.dotrc."
dot_fail
fi
subcmd="$1"
OLDCWD=`pwd`
dot_clear(){
echo "This will delete all files provided by $DOTREMOTE."
echo "Are you sure you want to continue? [yes/No]"
read answer
if [ "$answer" = "yes" ]; then
dotbackup=$DOTDEST/dotfiles_BACKUP`date +%s`.tar
ssh $DOTHOST "cd ${DOTREMOTEDIR} && git ls-tree -r HEAD --name-only" |\
tee | tar -cf $dotbackup --ignore-failed-read --remove-files -T -
ret=$?
if [ $ret -eq 0 ]; then
rm -rf $DOTGIT
fi
echo "Cleared files have been backed up in $dotbackup."
return $ret
else
return 0
fi
}
dot_unclear(){
dotbackup=`\ls -r dotfiles_BACKUP* | head -1`
tar xf $dotbackup
ret=$?
if [ $ret -eq 0 ]; then
rm $dotbackup
echo "Reinstated files from $dotbackup."
fi
return $ret
}
dot_init(){
export GIT_DIR=$DOTGIT
export GIT_WORK_TREE=$DOTDEST
git init
git remote add origin $DOTREMOTE
git checkout -b master
git pull origin master
git branch --set-upstream-to origin/master master
git config status.showuntrackedfiles no
unset GIT_DIR
unset GIT_WORK_TREE
return $?
}
dot_submodule_init(){
export GIT_DIR=$DOTGIT
export GIT_WORK_TREE=$DOTDEST
git submodule init
git submodule update
unset GIT_DIR
unset GIT_WORK_TREE
return $?
}
if [ "$subcmd" = "clear" ]; then
cd $DOTDEST
dot_clear
if [ $? -ne 0 ]; then
dot_fail
fi
dot_success
elif [ "$subcmd" = "unclear" ]; then
cd $DOTDEST
dot_unclear
if [ $? -ne 0 ]; then
dot_fail
fi
dot_success
elif [ "$subcmd" = "initialize" ]; then
cd $DOTDEST
# Clone dotfile repository into home directory
dot_init
if [ $? -ne 0 ]; then
echo "Destination $DOTDEST is not clean. Run 'dot clear' first."
dot_fail
fi
dot_submodule_init
if [ $? -ne 0 ]; then
echo "Destination $DOTDEST is not clean. Run 'dot clear' first."
dot_fail
fi
dot_success
elif [ "$subcmd" = "shell" ]; then
cd $OLDCWD
GIT_DIR=$DOTGIT zsh
dot_success
else
# else pass all arguments to git with GIT_DIR set
cd $OLDCWD
GIT_DIR=$DOTGIT git "$@"
fi