Commit a5e21a6a10f3aa0862b3c0641ae233d434bb8d17

Authored by Shrikant Sharat
1 parent 01590aa28a

Do comparisons with directory names in -cleanup.

The directory names were being converted to urls and then compared to the
contents of the record. The problem with this is there could be directories with
extremely wierd names in there, not created by antigen. This could throw the
dir->url conversion off.

So, with this, the urls from the record are converted to directory names and are
compared with the contents of repos/. Any directory unaccounted for will be
converted to a repo url, solely for the purpose of displaying to the user. After
confirmation, these directories are deleted, whether they represent a git clone
or not.

Showing 1 changed file with 11 additions and 9 deletions Inline Diff

1 #!/bin/zsh 1 #!/bin/zsh
2 2
3 # Each line in this string has the following entries separated by a space 3 # Each line in this string has the following entries separated by a space
4 # character. 4 # character.
5 # <repo-url>, <plugin-location>, <bundle-type> 5 # <repo-url>, <plugin-location>, <bundle-type>
6 # FIXME: Is not kept local by zsh! 6 # FIXME: Is not kept local by zsh!
7 local _ANTIGEN_BUNDLE_RECORD="" 7 local _ANTIGEN_BUNDLE_RECORD=""
8 8
9 # Syntaxes 9 # Syntaxes
10 # antigen-bundle <url> [<loc>=/] 10 # antigen-bundle <url> [<loc>=/]
11 # Keyword only arguments: 11 # Keyword only arguments:
12 # branch - The branch of the repo to use for this bundle. 12 # branch - The branch of the repo to use for this bundle.
13 antigen-bundle () { 13 antigen-bundle () {
14 14
15 # Bundle spec arguments' default values. 15 # Bundle spec arguments' default values.
16 local url="$ANTIGEN_DEFAULT_REPO_URL" 16 local url="$ANTIGEN_DEFAULT_REPO_URL"
17 local loc=/ 17 local loc=/
18 local branch= 18 local branch=
19 local btype=plugin 19 local btype=plugin
20 20
21 # Set spec values based on the positional arguments. 21 # Set spec values based on the positional arguments.
22 local position_args='url loc' 22 local position_args='url loc'
23 local i=1 23 local i=1
24 while ! [[ -z $1 || $1 == --*=* ]]; do 24 while ! [[ -z $1 || $1 == --*=* ]]; do
25 local arg_name="$(echo "$position_args" | cut -d\ -f$i)" 25 local arg_name="$(echo "$position_args" | cut -d\ -f$i)"
26 local arg_value="$1" 26 local arg_value="$1"
27 eval "local $arg_name='$arg_value'" 27 eval "local $arg_name='$arg_value'"
28 shift 28 shift
29 i=$(($i + 1)) 29 i=$(($i + 1))
30 done 30 done
31 31
32 # Check if url is just the plugin name. Super short syntax. 32 # Check if url is just the plugin name. Super short syntax.
33 if [[ "$url" != */* ]]; then 33 if [[ "$url" != */* ]]; then
34 loc="plugins/$url" 34 loc="plugins/$url"
35 url="$ANTIGEN_DEFAULT_REPO_URL" 35 url="$ANTIGEN_DEFAULT_REPO_URL"
36 fi 36 fi
37 37
38 # Set spec values from keyword arguments, if any. The remaining arguments 38 # Set spec values from keyword arguments, if any. The remaining arguments
39 # are all assumed to be keyword arguments. 39 # are all assumed to be keyword arguments.
40 while [[ $1 == --*=* ]]; do 40 while [[ $1 == --*=* ]]; do
41 local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')" 41 local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')"
42 local arg_value="$(echo "$1" | cut -d= -f2)" 42 local arg_value="$(echo "$1" | cut -d= -f2)"
43 eval "local $arg_name='$arg_value'" 43 eval "local $arg_name='$arg_value'"
44 shift 44 shift
45 done 45 done
46 46
47 # Resolve the url. 47 # Resolve the url.
48 url="$(-antigen-resolve-bundle-url "$url")" 48 url="$(-antigen-resolve-bundle-url "$url")"
49 49
50 # Add the branch information to the url. 50 # Add the branch information to the url.
51 if [[ ! -z $branch ]]; then 51 if [[ ! -z $branch ]]; then
52 url="$url|$branch" 52 url="$url|$branch"
53 fi 53 fi
54 54
55 # Add it to the record. 55 # Add it to the record.
56 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype" 56 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype"
57 57
58 # Ensure a clone exists for this repo. 58 # Ensure a clone exists for this repo.
59 -antigen-ensure-repo "$url" 59 -antigen-ensure-repo "$url"
60 60
61 # Load the plugin. 61 # Load the plugin.
62 -antigen-load "$url" "$loc" "$btype" 62 -antigen-load "$url" "$loc" "$btype"
63 63
64 } 64 }
65 65
66 -antigen-resolve-bundle-url () { 66 -antigen-resolve-bundle-url () {
67 # Given an acceptable short/full form of a bundle's repo url, this function 67 # Given an acceptable short/full form of a bundle's repo url, this function
68 # echoes the full form of the repo's clone url. 68 # echoes the full form of the repo's clone url.
69 69
70 local url="$1" 70 local url="$1"
71 71
72 if [[ $url != git://* && \ 72 if [[ $url != git://* && \
73 $url != https://* && \ 73 $url != https://* && \
74 $url != /* && \ 74 $url != /* && \
75 $url != git@github.com:*/* 75 $url != git@github.com:*/*
76 ]]; then 76 ]]; then
77 url="${url%.git}" 77 url="${url%.git}"
78 url="https://github.com/$url.git" 78 url="https://github.com/$url.git"
79 fi 79 fi
80 80
81 echo "$url" 81 echo "$url"
82 } 82 }
83 83
84 antigen-bundles () { 84 antigen-bundles () {
85 # Bulk add many bundles at one go. Empty lines and lines starting with a `#` 85 # Bulk add many bundles at one go. Empty lines and lines starting with a `#`
86 # are ignored. Everything else is given to `antigen-bundle` as is, no 86 # are ignored. Everything else is given to `antigen-bundle` as is, no
87 # quoting rules applied. 87 # quoting rules applied.
88 88
89 local line 89 local line
90 90
91 grep -v '^\s*$\|^#' | while read line; do 91 grep -v '^\s*$\|^#' | while read line; do
92 # Using `eval` so that we can use the shell-style quoting in each line 92 # Using `eval` so that we can use the shell-style quoting in each line
93 # piped to `antigen-bundles`. 93 # piped to `antigen-bundles`.
94 eval "antigen-bundle $line" 94 eval "antigen-bundle $line"
95 done 95 done
96 } 96 }
97 97
98 antigen-update () { 98 antigen-update () {
99 # Update your bundles, i.e., `git pull` in all the plugin repos. 99 # Update your bundles, i.e., `git pull` in all the plugin repos.
100 -antigen-echo-record \ 100 -antigen-echo-record \
101 | awk '{print $1}' \ 101 | awk '{print $1}' \
102 | sort -u \ 102 | sort -u \
103 | while read url; do 103 | while read url; do
104 -antigen-ensure-repo --update "$url" 104 -antigen-ensure-repo --update "$url"
105 done 105 done
106 } 106 }
107 107
108 -antigen-get-clone-dir () { 108 -antigen-get-clone-dir () {
109 # Takes a repo url and gives out the path that this url needs to be cloned 109 # Takes a repo url and gives out the path that this url needs to be cloned
110 # to. Doesn't actually clone anything. 110 # to. Doesn't actually clone anything.
111 # TODO: Memoize? 111 # TODO: Memoize?
112 112
113 # The url given. 113 # The url given.
114 local url="$1" 114 local url="$1"
115 115
116 # Echo the full path to the clone directory. 116 # Echo the full path to the clone directory.
117 echo -n $ADOTDIR/repos/ 117 echo -n $ADOTDIR/repos/
118 echo "$url" | sed \ 118 echo "$url" | sed \
119 -e 's/\.git$//' \
120 -e 's./.-SLASH-.g' \ 119 -e 's./.-SLASH-.g' \
121 -e 's.:.-COLON-.g' \ 120 -e 's.:.-COLON-.g' \
122 -e 's.|.-PIPE-.g' 121 -e 's.|.-PIPE-.g'
123 } 122 }
124 123
125 -antigen-get-clone-url () { 124 -antigen-get-clone-url () {
126 # Takes a repo's clone dir and gives out the repo's original url that was 125 # Takes a repo's clone dir and gives out the repo's original url that was
127 # used to create the given directory path. 126 # used to create the given directory path.
128 # TODO: Memoize? 127 # TODO: Memoize?
129 echo "$1" | sed \ 128 echo "$1" | sed \
130 -e "s:^$ADOTDIR/repos/::" \ 129 -e "s:^$ADOTDIR/repos/::" \
131 -e 's.-SLASH-./.g' \ 130 -e 's.-SLASH-./.g' \
132 -e 's.-COLON-.:.g' \ 131 -e 's.-COLON-.:.g' \
133 -e 's.-PIPE-.|.g' \ 132 -e 's.-PIPE-.|.g'
134 | awk '/^\// {print; next} {print $0 ".git"}'
135 } 133 }
136 134
137 -antigen-ensure-repo () { 135 -antigen-ensure-repo () {
138 136
139 # Ensure that a clone exists for the given repo url and branch. If the first 137 # Ensure that a clone exists for the given repo url and branch. If the first
140 # argument is `--update` and if a clone already exists for the given repo 138 # argument is `--update` and if a clone already exists for the given repo
141 # and branch, it is pull-ed, i.e., updated. 139 # and branch, it is pull-ed, i.e., updated.
142 140
143 # Check if we have to update. 141 # Check if we have to update.
144 local update=false 142 local update=false
145 if [[ $1 == --update ]]; then 143 if [[ $1 == --update ]]; then
146 update=true 144 update=true
147 shift 145 shift
148 fi 146 fi
149 147
150 # Get the clone's directory as per the given repo url and branch. 148 # Get the clone's directory as per the given repo url and branch.
151 local url="$1" 149 local url="$1"
152 local clone_dir="$(-antigen-get-clone-dir $url)" 150 local clone_dir="$(-antigen-get-clone-dir $url)"
153 151
154 # Clone if it doesn't already exist. 152 # Clone if it doesn't already exist.
155 if [[ ! -d $clone_dir ]]; then 153 if [[ ! -d $clone_dir ]]; then
156 git clone "${url%|*}" "$clone_dir" 154 git clone "${url%|*}" "$clone_dir"
157 elif $update; then 155 elif $update; then
158 # Pull changes if update requested. 156 # Pull changes if update requested.
159 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" pull 157 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" pull
160 fi 158 fi
161 159
162 # If its a specific branch that we want, checkout that branch. 160 # If its a specific branch that we want, checkout that branch.
163 if [[ $url == *\|* ]]; then 161 if [[ $url == *\|* ]]; then
164 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \ 162 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \
165 checkout "${url#*|}" 163 checkout "${url#*|}"
166 fi 164 fi
167 165
168 } 166 }
169 167
170 -antigen-load () { 168 -antigen-load () {
171 169
172 local url="$1" 170 local url="$1"
173 local loc="$2" 171 local loc="$2"
174 local btype="$3" 172 local btype="$3"
175 173
176 # The full location where the plugin is located. 174 # The full location where the plugin is located.
177 local location="$(-antigen-get-clone-dir "$url")/$loc" 175 local location="$(-antigen-get-clone-dir "$url")/$loc"
178 176
179 if [[ $btype == theme ]]; then 177 if [[ $btype == theme ]]; then
180 178
181 # Of course, if its a theme, the location would point to the script 179 # Of course, if its a theme, the location would point to the script
182 # file. 180 # file.
183 source "$location" 181 source "$location"
184 182
185 else 183 else
186 184
187 # Source the plugin script 185 # Source the plugin script
188 # FIXME: I don't know. Looks very very ugly. Needs a better 186 # FIXME: I don't know. Looks very very ugly. Needs a better
189 # implementation once tests are ready. 187 # implementation once tests are ready.
190 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')" 188 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')"
191 189
192 if [[ -f $script_loc ]]; then 190 if [[ -f $script_loc ]]; then
193 # If we have a `*.plugin.zsh`, source it. 191 # If we have a `*.plugin.zsh`, source it.
194 source "$script_loc" 192 source "$script_loc"
195 193
196 elif [[ ! -z "$(ls "$location" | grep -m1 '\.zsh$')" ]]; then 194 elif [[ ! -z "$(ls "$location" | grep -m1 '\.zsh$')" ]]; then
197 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` 195 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh`
198 # files. 196 # files.
199 for script ($location/*.zsh(N)) source "$script" 197 for script ($location/*.zsh(N)) source "$script"
200 198
201 elif [[ ! -z "$(ls "$location" | grep -m1 '\.sh$')" ]]; then 199 elif [[ ! -z "$(ls "$location" | grep -m1 '\.sh$')" ]]; then
202 # If there are no `*.zsh` files either, we look for and source any 200 # If there are no `*.zsh` files either, we look for and source any
203 # `*.sh` files instead. 201 # `*.sh` files instead.
204 for script ($location/*.sh(N)) source "$script" 202 for script ($location/*.sh(N)) source "$script"
205 203
206 fi 204 fi
207 205
208 # Add to $fpath, for completion(s). 206 # Add to $fpath, for completion(s).
209 fpath=($location $fpath) 207 fpath=($location $fpath)
210 208
211 fi 209 fi
212 210
213 } 211 }
214 212
215 antigen-cleanup () { 213 antigen-cleanup () {
216 214
217 # Cleanup unused repositories. 215 # Cleanup unused repositories.
218 216
219 local force=false 217 local force=false
220 if [[ $1 == --force ]]; then 218 if [[ $1 == --force ]]; then
221 force=true 219 force=true
222 fi 220 fi
223 221
224 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then 222 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then
225 echo "You don't have any bundles." 223 echo "You don't have any bundles."
226 return 0 224 return 0
227 fi 225 fi
228 226
229 # Find directores in ADOTDIR/repos, that are not in the bundles record. 227 # Find directores in ADOTDIR/repos, that are not in the bundles record.
230 local unused_clones="$(comm -13 \ 228 local unused_clones="$(comm -13 \
231 <(-antigen-echo-record | awk '{print $1}' | sort -u) \ 229 <(-antigen-echo-record \
232 <(ls "$ADOTDIR/repos" \ 230 | awk '{print $1}' \
233 | while read line; do 231 | while read line; do
234 -antigen-get-clone-url "$line" 232 -antigen-get-clone-dir "$line"
235 done \ 233 done \
236 | sort -u))" 234 | sort -u) \
235 <(ls -d "$ADOTDIR/repos/"* | sort -u))"
237 236
238 if [[ -z $unused_clones ]]; then 237 if [[ -z $unused_clones ]]; then
239 echo "You don't have any unidentified bundles." 238 echo "You don't have any unidentified bundles."
240 return 0 239 return 0
241 fi 240 fi
242 241
243 echo 'You have clones for the following repos, but are not used.' 242 echo 'You have clones for the following repos, but are not used.'
244 echo "$unused_clones" \ 243 echo "$unused_clones" \
244 | while read line; do
245 -antigen-get-clone-url "$line"
246 done \
245 | sed -e 's/^/ /' -e 's/|/, branch /' 247 | sed -e 's/^/ /' -e 's/|/, branch /'
246 248
247 if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then 249 if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then
248 echo 250 echo
249 echo 251 echo
250 echo "$unused_clones" | while read line; do 252 echo "$unused_clones" | while read line; do
251 echo -n "Deleting clone for $line..." 253 echo -n "Deleting clone for $(-antigen-get-clone-url "$line")..."
252 rm -rf "$(-antigen-get-clone-dir $line)" 254 rm -rf "$line"
253 echo ' done.' 255 echo ' done.'
254 done 256 done
255 else 257 else
256 echo 258 echo
257 echo Nothing deleted. 259 echo Nothing deleted.
258 fi 260 fi
259 } 261 }
260 262
261 antigen-lib () { 263 antigen-lib () {
262 antigen-bundle --loc=lib 264 antigen-bundle --loc=lib
263 } 265 }
264 266
265 antigen-theme () { 267 antigen-theme () {
266 local name="${1:-robbyrussell}" 268 local name="${1:-robbyrussell}"
267 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme 269 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme
268 } 270 }
269 271
270 antigen-apply () { 272 antigen-apply () {
271 # Initialize completion. 273 # Initialize completion.
272 # TODO: Only load completions if there are any changes to the bundle 274 # TODO: Only load completions if there are any changes to the bundle
273 # repositories. 275 # repositories.
274 compinit -i 276 compinit -i
275 } 277 }
276 278
277 antigen-list () { 279 antigen-list () {
278 # List all currently installed bundles 280 # List all currently installed bundles
279 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then 281 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
280 echo "You don't have any bundles." >&2 282 echo "You don't have any bundles." >&2
281 return 1 283 return 1
282 else 284 else
283 -antigen-echo-record | sort -u 285 -antigen-echo-record | sort -u
284 fi 286 fi
285 } 287 }
286 288
287 antigen-help () { 289 antigen-help () {
288 cat <<EOF 290 cat <<EOF
289 Antigen is a plugin management system for zsh. It makes it easy to grab awesome 291 Antigen is a plugin management system for zsh. It makes it easy to grab awesome
290 shell scripts and utilities, put up on github. For further details and complete 292 shell scripts and utilities, put up on github. For further details and complete
291 documentation, visit the project's page at 'http://antigen.sharats.me'. 293 documentation, visit the project's page at 'http://antigen.sharats.me'.
292 EOF 294 EOF
293 } 295 }
294 296
295 # A syntax sugar to avoid the `-` when calling antigen commands. With this 297 # A syntax sugar to avoid the `-` when calling antigen commands. With this
296 # function, you can write `antigen-bundle` as `antigen bundle` and so on. 298 # function, you can write `antigen-bundle` as `antigen bundle` and so on.
297 antigen () { 299 antigen () {
298 local cmd="$1" 300 local cmd="$1"
299 shift 301 shift
300 "antigen-$cmd" "$@" 302 "antigen-$cmd" "$@"
301 } 303 }
302 304
303 # Echo the bundle specs as in the record. The first line is not echoed since it 305 # Echo the bundle specs as in the record. The first line is not echoed since it
304 # is a blank line. 306 # is a blank line.
305 -antigen-echo-record () { 307 -antigen-echo-record () {
306 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' 308 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
307 } 309 }
308 310
309 -antigen-env-setup () { 311 -antigen-env-setup () {
310 # Pre-startup initializations 312 # Pre-startup initializations
311 -set-default ANTIGEN_DEFAULT_REPO_URL \ 313 -set-default ANTIGEN_DEFAULT_REPO_URL \
312 https://github.com/robbyrussell/oh-my-zsh.git 314 https://github.com/robbyrussell/oh-my-zsh.git
313 -set-default ADOTDIR $HOME/.antigen 315 -set-default ADOTDIR $HOME/.antigen
314 316
315 # Load the compinit module 317 # Load the compinit module
316 autoload -U compinit 318 autoload -U compinit
317 319
318 # Without the following, `compdef` function is not defined. 320 # Without the following, `compdef` function is not defined.
319 compinit -i 321 compinit -i
320 } 322 }
321 323
322 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is 324 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
323 # not already set. 325 # not already set.
324 -set-default () { 326 -set-default () {
325 local arg_name="$1" 327 local arg_name="$1"
326 local arg_value="$2" 328 local arg_value="$2"
327 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 329 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
328 } 330 }
329 331