-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage
More file actions
executable file
·226 lines (182 loc) · 4.86 KB
/
Copy pathmanage
File metadata and controls
executable file
·226 lines (182 loc) · 4.86 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
#!/bin/sh
CMD_LS="ls"
CMD_SYNC="sync"
CMD_ADD="add"
CMD_RM="rm"
CMD_COLORTEST="color-check"
CMDS=($CMD_LS $CMD_SYNC $CMD_ADD $CMD_RM $CMD_COLORTEST)
SYNC_EXCLUDE="^README.md$\|^$(basename $0)$"
argv=($@)
cmd=$0
subcmd="${argv[0]}"
if test -z $cmd; then cmd="null"; fi
if test -z $subcmd; then subcmd="null"; fi
function add_tracked_file() {
file=$1
if test ! -e $file; then
echoe "$file: No such file" && return 1;
else
trackedFile=./$(basename $file)
# NOTE: DESTRUCTIVE
mv -v $(realpath $file) $trackedFile
git add --verbose $trackedFile
fi
}
function clean_orphaned_symlinks() {
# This is some arcane looking shit
# but it's mostly implicitly telling find
# to return broken symlinks (because it only
# recognizes it's a symlink if it's broken with -L enabled)
echoe -e "Cleaning orphaned dotfiles...\n"
for brokenLink in $(find -L ~ -type l); do
brokenPath=$(realpath -m $brokenLink)
# Make doubly sure it's broken and only mess with our own broken stuff
if ! test -e $brokenLink && test $(dirname $brokenPath) = $(pwd); then
echoe "$brokenLink ($brokenPath) is orphan"
# NOTE: DESTRUCTIVE
rm -v $brokenLink
fi
done
echoe
}
function symlink_tracked_files() {
echoe -e "Symlinking managed dotfiles\n"
for file in $(get_tracked_files); do
linkname=~/$(basename $file)
if test -z $linkname; then echoe "Bad linkname" && exit 1; fi
test -e $linkname
looseTest=$?
test -e $linkname -a $(realpath $linkname) = $(realpath $file)
managedTest=$?
test -e $linkname -a ! -L $linkname
unmanagedTest=$?
if test $managedTest -eq 0;
then echoe "$linkname: Managed";
elif test $unmanagedTest -eq 0;
then echoe "$linkname: Exists but not managed";
elif test $looseTest -eq 0;
then echoe "$linkname: Exists as loose file";
else
# NOTE: DESTRUCTIVE
ln -s -v $(realpath $file) $linkname
fi
done
echoe
}
function untrack_file() {
file=./$1
# NOTE: DESTRUCTIVE
mv -v $file ~ &&
git rm -rf $file
}
function get_tracked_files() {
# Skip manage script and project stuff
git ls-files|grep -v $SYNC_EXCLUDE
}
function filter_untracked_files() {
trackedFiles=$(get_tracked_files)
args=$@
toRemove=${args[@]}
for file in $toRemove; do
validRemove=""
for trackedFile in $trackedFiles; do
if test $file == $trackedFile; then
validRemove=$file
break
fi
done
if test -z $validRemove; then
echoe "$file: Not tracked" && return 1;
else
echo $validRemove
fi
done
}
function echoe() {
echo $@ > /dev/stderr
}
function commit_reminder() {
echoe "Needs git commit"
}
function truecolor_test() {
# From https://gist.github.com/XVilka/8346728
echoe -e "Truecolor test\n------\n"
awk -v term_cols="${width:-$(tput cols || echo 80)}" 'BEGIN{
s="/\\";
for (colnum = 0; colnum<term_cols; colnum++) {
r = 255-(colnum*255/term_cols);
g = (colnum*510/term_cols);
b = (colnum*255/term_cols);
if (g>255) g = 510-g;
printf "\033[48;2;%d;%d;%dm", r,g,b;
printf "\033[38;2;%d;%d;%dm", 255-r,255-g,255-b;
printf "%s\033[0m", substr(s,colnum%2+1,1);
}
printf "\n";
}'
echo -en "\x1b[0m"
}
function bytecolor_test() {
# From https://gist.github.com/HaleTom/89ffe32783f89f403bba96bd7bcd1263
# Modified by Lagg to not be so awkward about the nibblerow
echoe -e "Bytecolor test\n------\n"
for i in {0..255} ; do
if (($i > 0)) && (($i % 8 == 0)); then echo; fi
printf "\x1b[38;5;${i}m%03d " "${i}"
done
echo -e "\x1b[0m"
}
function codecolor_test() {
# From Lagg
echoe -e "Codecolor test\n------\n"
# Foreground
for i in {30..37}; do
printf "\x1b[${i}m%03d " "$i"
done
echo -e "\x1b[0m"
# Background
for i in {40..47}; do
printf "\x1b[${i}m%03d " "$i"
done
echo -e "\x1b[0m"
# Bright foreground
for i in {90..97}; do
printf "\x1b[${i}m%03d " "$i"
done
echo -e "\x1b[0m"
}
if test $subcmd = $CMD_LS; then
get_tracked_files
elif test $subcmd = $CMD_COLORTEST; then
codecolor_test
echoe
bytecolor_test
echoe
truecolor_test
elif test $subcmd = $CMD_SYNC; then
if git pull; then
symlink_tracked_files
clean_orphaned_symlinks
else
echoe "Pull error"
fi
elif test $subcmd = $CMD_ADD; then
for file in ${argv[@]:1}; do
if ! add_tracked_file $file; then exit 1; fi
done
symlink_tracked_files
commit_reminder
elif test $subcmd = $CMD_RM; then
toRemove=${argv[@]:1}
validRemoves=$(filter_untracked_files $toRemove)
if ! test $? -eq 0; then
exit 1;
fi
for file in $validRemoves; do
if ! untrack_file $file; then exit 1; fi
done
commit_reminder
else
echo "Commands: "
for cmd in ${CMDS[@]}; do echo " $cmd"; done
fi