Commit b6db07ac277e651bbf20c5c2e20ec336b3ff9e1d

Authored by Shrikant Sharat
1 parent 41abcf6628

Cleanup omits bundles with no local clones.

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