-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot-man
More file actions
executable file
·390 lines (367 loc) · 14.8 KB
/
dot-man
File metadata and controls
executable file
·390 lines (367 loc) · 14.8 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/bin/bash
################################################################################
# dot-man
#
# Copyright (c) 2015-2025 Charles Neill
# Github: cneill | Twitter: ccneill
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
################################################################################
platform="unknown"
config_file=$HOME/.config/dot-man/config
editor=${EDITOR:-vi}
# Command line arguments
HELP=0
IGNORE_LAST_RUN=0
CHECK_LAST_RUN=0
LOAD_UNSAFE_CONFIG=0
LAZY_PLATFORM_DETECT=0
VERBOSE=0
REVERSE_PROMPTS=0
# Colors / formatting
sep="\033[31m================================================================================\033[0m"
b="\033[1m"
b_="\033[0m"
hi="${b}\033[92m"
hi_="\033[0m${b_}"
f="\033[4m\033[34m"
f_="\033[0m\033[24m"
# Return true if "yes", false if "no"
function yn_prompt {
if [[ -z $1 ]]; then
echo "ERROR! BAD PROMPT"
exit 1
fi
question="${b}$1${b_}"
if [[ $REVERSE_PROMPTS == 0 ]]; then
answers="[y/${hi}N${hi_}]"
elif [[ $REVERSE_PROMPTS == 1 ]]; then
answers="[${hi}Y${hi_}/n]"
fi
echo -e "$question $answers"
read -p " > " y_n
if [[ $REVERSE_PROMPTS == 0 ]]; then
if [[ $y_n == "y" ]] || [[ $y_n == "Y" ]]; then
return 0
else
return 1
fi
elif [[ $REVERSE_PROMPTS == 1 ]]; then
if [[ $y_n == "n" ]] || [[ $y_n == "N" ]]; then
return 1
else
return 0
fi
fi
}
# Load our configuration file
function load_config {
if [[ $LOAD_UNSAFE_CONFIG == 1 ]]; then
. "$config_file"
fi
clean=$(grep -vE '(^[# a-zA-Z]$)|(^[a-z]+=[a-z" ]$)' "$config_file")
if [[ -n $clean ]] && [[ $LOAD_UNSAFE_CONFIG == 0 ]]; then
echo -e "Warning: your configuration file may have unsafe lines in it. \n${b}What would you like to do?${b_} [c]ontinue, [o]pen, or [${hi}Q${hi_}]uit:"
read -p " > " continue_reading
if [[ $continue_reading == "c" ]] || [[ $continue_reading == "C" ]]; then
. "$config_file"
elif [[ $continue_reading == "o" ]] || [[ $continue_reading == "O" ]]; then
$editor "$config_file"
if yn_prompt "Continue loading configuration file?"; then
. "$config_file"
else
echo "Unable to load configuration. Quitting."
exit 1
fi
else
echo "Quitting."
exit 0
fi
fi
}
# See when we last ran dot-man
function check_last_run {
thedate=$(date)
thetime=""
if [[ $platform == "Linux" ]]; then
thetime=$(date -d "$thedate" "+%s")
elif [[ $platform == "OS X" ]]; then
thetime=$(date -j -f "%a %b %d %T %Z %Y" "$thedate" "+%s")
fi
if [[ -f $dotfiles_last_run_file ]]; then
raw=$(cat "$dotfiles_last_run_file")
if [[ $platform == "Linux" ]]; then
last_time=$(date -d "@$raw")
elif [[ $platform == "OS X" ]]; then
last_time=$(date -r "$raw")
fi
time_diff=$thetime-$raw
if [[ $CHECK_LAST_RUN == 1 ]]; then
if [[ $time_diff -lt $dotfiles_update_interval ]]; then
exit 0
else
echo "$thetime" > "$dotfiles_last_run_file"
fi
else
if yn_prompt "Last run: $last_time. Continue?"; then
echo "$thetime" > "$dotfiles_last_run_file"
else
echo "Quitting."
exit 0
fi
fi
else
touch "$dotfiles_last_run_file"
echo "$thetime" > "$dotfiles_last_run_file"
fi
}
# check to make sure we have the right platform
function check_platform {
unamestr=$(uname)
if [[ $unamestr == "Linux" ]]; then
platform="Linux"
elif [[ $unamestr == "Darwin" ]]; then
platform="OS X"
fi
if [[ $platform != "unknown" ]]; then
if [[ -z $dotfiles_branch ]]; then
if [[ $platform == "Linux" ]]; then
dotfiles_branch="linux"
elif [[ $platform == "OS X" ]]; then
dotfiles_branch="osx"
fi
elif [[ $VERBOSE == 1 ]]; then
echo "Using branch $dotfiles_branch (defined in $config_file)."
fi
if [[ $LAZY_PLATFORM_DETECT == 0 ]]; then
if ! yn_prompt "It looks like your platform is $platform. Is this correct?"; then
echo "Unknown platform. Quitting."
exit 1
fi
fi
else
echo "Unknown platform. Quitting."
exit 1
fi
}
# check to see if our dotfiles folder exists
function check_dotfiles_exist {
echo -e "Checking dotfiles exist...\n"
if [[ ! -d $dotfiles_repo_loc ]]; then
if [[ -e $dotfiles_repo_loc ]]; then
if yn_prompt "${f}$dotfiles_repo_loc${f_} is not a directory. Delete and create directory?"; then
rm -rf "$dotfiles_repo_loc"
mkdir "$dotfiles_repo_loc"
else
echo -e "Can't continue without a valid \$dotfiles_repo_loc."
exit 1
fi
else
if yn_prompt "No folder named $dotfiles_repo_loc. Create directory ${f}$dotfiles_repo_loc${f_}?"; then
mkdir "$dotfiles_repo_loc"
else
echo -e "Can't continue without a valid \$dotfiles_repo_loc."
exit 1
fi
fi
fi
if (git -C "$dotfiles_repo_loc" status 2>&1 | fgrep -q 'Not a git repository'); then
echo -e "Git repository not found at this location.\n${b}Initialize one or clone?${b_}\n[i]nitialize, [c]lone, or [${hi}Q${hi_}]uit: "
read -p " > " initialize_git
if [[ $initialize_git == "i" ]] || [[ $initialize_git == "I" ]]; then
git -C "$dotfiles_repo_loc" init
git -C "$dotfiles_repo_loc" checkout -b "$dotfiles_branch"
elif [[ $initialize_git == "c" ]] || [[ $initialize_git == "C" ]]; then
is_dirty=$(ls "$dotfiles_repo_loc")
if [[ -n $is_dirty ]]; then
echo -e "Existing files in ${f}$dotfiles_repo_loc${f_}:\n$is_dirty\n"
if yn_prompt "Delete files?"; then
rm -rf "$dotfiles_repo_loc"
mkdir "$dotfiles_repo_loc"
else
echo -e "Cannot clone repository without clean folder: ${f}$dotfiles_repo_loc${f_}. Clean up this directory and try again."
exit 1
fi
fi
git clone "$dotfiles_repo" "$dotfiles_repo_loc"
git -C "$dotfiles_repo_loc" checkout -b "$dotfiles_branch"
else
echo "Quitting."
exit 1
fi
else
echo "Found dotfiles repository."
fi
}
# update dotfiles repository from remote
function update_dotfiles {
echo -e "Checking status of repository...\n"
current_branch=$(git -C "$dotfiles_repo_loc" status | grep "On branch " | sed "s/On branch //")
if [[ $current_branch != $dotfiles_branch ]]; then
git -C "$dotfiles_repo_loc" checkout "$dotfiles_branch"
git -C "$dotfiles_repo_loc" branch -u "origin/$dotfiles_branch"
fi
git -C "$dotfiles_repo_loc" remote update
status=$(git -C "$dotfiles_repo_loc" status)
up_to_date=$(grep "Your branch is up-to-date" <<< "$status")
up_to_date2=$(grep -E "nothing (added )*to commit" <<< "$status") # Dirty branch is thrown away here. Maybe we want to update?
if [[ $up_to_date ]] || [[ $up_to_date2 ]]; then
echo "Local repository is up-to-date with remote repository."
else
if yn_prompt "The dotfiles repository has been updated, or has unstaged changes. Pull latest version?"; then
git -C "$dotfiles_repo_loc" pull -u origin "$dotfiles_branch"
else
echo "Not updating repository from remote."
fi
fi
}
# diff directories and copy any updates
function compare_dotfiles {
echo -e "Comparing files from ${f}$dotfiles_repo_loc${f_} with files in ${f}$dotfiles_home${f_}.\n"
for file in $(git -C "$dotfiles_repo_loc" ls-files); do
localFileDir=$(dirname "$dotfiles_home/$file")
repoFileDir=$(dirname "$dotfiles_repo_loc/$file")
if [[ $file != "$dotfiles_repo_loc/." ]] && [[ $file != "$dotfiles_repo_loc/.." ]] && [[ $file != ".git" ]] && [[ $file != ".gitignore" ]]; then
if [[ $dotfiles_repo_loc/$file -nt $dotfiles_home/$file ]]; then
if [[ ! -f $dotfiles_home/$file ]]; then
if yn_prompt "File ${f}$file${f_} doesn't exist in ${f}$dotfiles_home${f_}. Copy from repo?"; then
if [[ ! -d "$localFileDir" ]]; then
mkdir -p "$localFileDir"
fi
cp "$dotfiles_repo_loc/$file" "$dotfiles_home/$file"
fi
else
difference=$(diff "$dotfiles_repo_loc/$file" "$dotfiles_home/$file")
if [[ -n $difference ]]; then
if yn_prompt "Dotfiles repository has newer version of ${f}$file${f_}. Open sdiff?"; then
outfile=$(mktemp TEMP_DOTFILE.XXXXXXXXXX)
sdiff -w "${sdiff_width}" -o "$outfile" "$dotfiles_repo_loc/$file" "$dotfiles_home/$file"
if [[ ! -d "$localFileDir" ]]; then
mkdir -p "$localFileDir"
fi
cp "$outfile" "$dotfiles_home/$file"
rm "$outfile"
fi
elif [[ $VERBOSE == 1 ]]; then
echo -e "${f}$file${f_} is up-to-date!"
fi
fi
elif [[ $dotfiles_home/$file -nt $dotfiles_repo_loc/$file ]]; then
difference=$(diff "$dotfiles_home/$file" "$dotfiles_repo_loc/$file")
if [[ -n $difference ]]; then
if yn_prompt "$sep\nDifferences found in ${f}$dotfiles_home/$file${f_}. Open sdiff?"; then
outfile=$(mktemp TEMP_DOTFILE.XXXXXXXXXX)
sdiff -w "${sdiff_width}" -o "$outfile" "$dotfiles_home/$file" "$dotfiles_repo_loc/$file"
home_difference=$(diff "$outfile" "$dotfiles_home/$file")
repo_difference=$(diff "$outfile" "$dotfiles_repo_loc/$file")
if [[ -n $home_difference ]]; then
if yn_prompt "Copy to $dotfiles_home?"; then
if [[ ! -d "$localFileDir" ]]; then
mkdir -p "$localFileDir"
fi
cp "$outfile" "$dotfiles_home/$file"
fi
fi
if [[ -n $repo_difference ]]; then
if yn_prompt "Copy to $dotfiles_repo_loc?"; then
if [[ ! -d "$repoFileDir" ]]; then
mkdir -p "$repoFileDir"
fi
cp "$outfile" "$dotfiles_repo_loc/$file"
if yn_prompt "Update dotfiles repository?"; then
git -C "$dotfiles_repo_loc" add "$dotfiles_repo_loc/$file"
commit_message="Updating $file on $(hostname) at $(date)."
if yn_prompt "Custom commit message?"; then
echo "Enter commit message: "
read -p " > " custom_commit_message
commit_message=$custom_commit_message
fi
git -C "$dotfiles_repo_loc" commit -m "$commit_message" -q
git -C "$dotfiles_repo_loc" push -u "$dotfiles_repo"
echo "Pushed updates to $dotfiles_repo!"
fi
fi
fi
rm "$outfile"
else
echo -e "Leaving differences in ${f}$file${f_} unchanged."
fi
elif [[ $VERBOSE == 1 ]]; then
echo -e "${f}$file${f_} is up-to-date!"
fi
fi
fi
done
echo "Finished with all dotfiles!"
}
for arg in "$@"; do
case $arg in
-h)
HELP=1
IGNORE_LAST_RUN=1
;;
-d)
LAZY_PLATFORM_DETECT=1
;;
-f)
LOAD_UNSAFE_CONFIG=1
;;
-i)
IGNORE_LAST_RUN=1
;;
-l)
CHECK_LAST_RUN=1
;;
-r)
REVERSE_PROMPTS=1
;;
-v)
VERBOSE=1
;;
esac
done
if [[ $HELP == 1 ]]; then
echo -e "Usage:\n
$(basename "$0") [-h] [-d] [-f] [-i] [-l] [-r] [-v]
\nOptions:\n
\t-h\tShow this help message
\t-d\tUse best guess for platform
\t-f\tForce auto-load of config file (no check for unsafe values)
\t-i\tIgnore last run check entirely
\t-l\tDon't check for updates unless it's been \$dotfiles_update_interval seconds since last run
\t-r\tReverse prompts (i.e. make enter default-destructive)
\t-v\tVerbose: print more information"
exit
fi
load_config
check_platform
echo -e "$sep"
printf "dot-man (•‿•)%67s\n" "$USER@$(hostname):$dotfiles_branch"
echo -e "$sep"
if [[ $IGNORE_LAST_RUN == 0 ]]; then
check_last_run
fi
check_dotfiles_exist
echo -e "$sep"
update_dotfiles
echo -e "$sep"
compare_dotfiles
echo -e "$sep"