Commit c7827f8ac0319804e0ab0ceae544e565de28289d

Authored by Shrikant Sharat
1 parent 7af602daed

Fix comments to indicate the fourth field.

There is a fourth boolean field in the record, which indicates wheather the
plugin has a local clone made.

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