Commit 292d226b93fbe03860268cb6481fa84eb03cd35a

Authored by Shrikant Sharat
1 parent 2873acbf1a

Use `--plugin-git` function to handle the clone.

Showing 1 changed file with 2 additions and 5 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>, <has-local-clone> 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 # Parse the given arguments. (Will overwrite the above values). 22 # Parse the given arguments. (Will overwrite the above values).
23 eval "$(-antigen-parse-args \ 23 eval "$(-antigen-parse-args \
24 'url?, loc? ; branch:?, no-local-clone?, btype:?' \ 24 'url?, loc? ; branch:?, no-local-clone?, btype:?' \
25 "$@")" 25 "$@")"
26 26
27 # Check if url is just the plugin name. Super short syntax. 27 # Check if url is just the plugin name. Super short syntax.
28 if [[ "$url" != */* ]]; then 28 if [[ "$url" != */* ]]; then
29 loc="plugins/$url" 29 loc="plugins/$url"
30 url="$ANTIGEN_DEFAULT_REPO_URL" 30 url="$ANTIGEN_DEFAULT_REPO_URL"
31 fi 31 fi
32 32
33 # Resolve the url. 33 # Resolve the url.
34 url="$(-antigen-resolve-bundle-url "$url")" 34 url="$(-antigen-resolve-bundle-url "$url")"
35 35
36 # Add the branch information to the url. 36 # Add the branch information to the url.
37 if [[ ! -z $branch ]]; then 37 if [[ ! -z $branch ]]; then
38 url="$url|$branch" 38 url="$url|$branch"
39 fi 39 fi
40 40
41 # The `make_local_clone` variable better represents whether there should be 41 # The `make_local_clone` variable better represents whether there should be
42 # a local clone made. For cloning to be avoided, firstly, the `$url` should 42 # a local clone made. For cloning to be avoided, firstly, the `$url` should
43 # be an absolute local path and `$branch` should be empty. In addition to 43 # be an absolute local path and `$branch` should be empty. In addition to
44 # these two conditions, either the `--no-local-clone` option should be 44 # these two conditions, either the `--no-local-clone` option should be
45 # given, or `$url` should not a git repo. 45 # given, or `$url` should not a git repo.
46 local make_local_clone=true 46 local make_local_clone=true
47 if [[ $url == /* && -z $branch && 47 if [[ $url == /* && -z $branch &&
48 ( $no_local_clone == true || ! -d $url/.git ) ]]; then 48 ( $no_local_clone == true || ! -d $url/.git ) ]]; then
49 make_local_clone=false 49 make_local_clone=false
50 fi 50 fi
51 51
52 # Add the theme extension to `loc`, if this is a theme. 52 # Add the theme extension to `loc`, if this is a theme.
53 if [[ $btype == theme && $loc != *.zsh-theme ]]; then 53 if [[ $btype == theme && $loc != *.zsh-theme ]]; then
54 loc="$loc.zsh-theme" 54 loc="$loc.zsh-theme"
55 fi 55 fi
56 56
57 # Add it to the record. 57 # Add it to the record.
58 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype" 58 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype"
59 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD $make_local_clone" 59 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD $make_local_clone"
60 60
61 # Ensure a clone exists for this repo, if needed. 61 # Ensure a clone exists for this repo, if needed.
62 if $make_local_clone; then 62 if $make_local_clone; then
63 -antigen-ensure-repo "$url" 63 -antigen-ensure-repo "$url"
64 fi 64 fi
65 65
66 # Load the plugin. 66 # Load the plugin.
67 -antigen-load "$url" "$loc" "$btype" "$make_local_clone" 67 -antigen-load "$url" "$loc" "$btype" "$make_local_clone"
68 68
69 } 69 }
70 70
71 -antigen-resolve-bundle-url () { 71 -antigen-resolve-bundle-url () {
72 # Given an acceptable short/full form of a bundle's repo url, this function 72 # Given an acceptable short/full form of a bundle's repo url, this function
73 # echoes the full form of the repo's clone url. 73 # echoes the full form of the repo's clone url.
74 74
75 local url="$1" 75 local url="$1"
76 76
77 # Expand short github url syntax: `username/reponame`. 77 # Expand short github url syntax: `username/reponame`.
78 if [[ $url != git://* && 78 if [[ $url != git://* &&
79 $url != https://* && 79 $url != https://* &&
80 $url != /* && 80 $url != /* &&
81 $url != git@github.com:*/* 81 $url != git@github.com:*/*
82 ]]; then 82 ]]; then
83 url="https://github.com/${url%.git}.git" 83 url="https://github.com/${url%.git}.git"
84 fi 84 fi
85 85
86 echo "$url" 86 echo "$url"
87 } 87 }
88 88
89 antigen-bundles () { 89 antigen-bundles () {
90 # Bulk add many bundles at one go. Empty lines and lines starting with a `#` 90 # Bulk add many bundles at one go. Empty lines and lines starting with a `#`
91 # are ignored. Everything else is given to `antigen-bundle` as is, no 91 # are ignored. Everything else is given to `antigen-bundle` as is, no
92 # quoting rules applied. 92 # quoting rules applied.
93 93
94 local line 94 local line
95 95
96 grep -v '^\s*$\|^#' | while read line; do 96 grep -v '^\s*$\|^#' | while read line; do
97 # Using `eval` so that we can use the shell-style quoting in each line 97 # Using `eval` so that we can use the shell-style quoting in each line
98 # piped to `antigen-bundles`. 98 # piped to `antigen-bundles`.
99 eval "antigen-bundle $line" 99 eval "antigen-bundle $line"
100 done 100 done
101 } 101 }
102 102
103 antigen-update () { 103 antigen-update () {
104 # Update your bundles, i.e., `git pull` in all the plugin repos. 104 # Update your bundles, i.e., `git pull` in all the plugin repos.
105 105
106 date > $ADOTDIR/revert-info 106 date > $ADOTDIR/revert-info
107 107
108 -antigen-echo-record | 108 -antigen-echo-record |
109 awk '$4 == "true" {print $1}' | 109 awk '$4 == "true" {print $1}' |
110 sort -u | 110 sort -u |
111 while read url; do 111 while read url; do
112 echo "**** Pulling $url" 112 echo "**** Pulling $url"
113 113
114 local clone_dir="$(-antigen-get-clone-dir "$url")" 114 local clone_dir="$(-antigen-get-clone-dir "$url")"
115 if [[ -d "$clone_dir" ]]; then 115 if [[ -d "$clone_dir" ]]; then
116 (echo -n "$clone_dir:" 116 (echo -n "$clone_dir:"
117 cd "$clone_dir" 117 cd "$clone_dir"
118 git rev-parse HEAD) >> $ADOTDIR/revert-info 118 git rev-parse HEAD) >> $ADOTDIR/revert-info
119 fi 119 fi
120 120
121 -antigen-ensure-repo "$url" --update --verbose 121 -antigen-ensure-repo "$url" --update --verbose
122 122
123 echo 123 echo
124 done 124 done
125 } 125 }
126 126
127 antigen-revert () { 127 antigen-revert () {
128 if ! [[ -f $ADOTDIR/revert-info ]]; then 128 if ! [[ -f $ADOTDIR/revert-info ]]; then
129 echo 'No revert information available. Cannot revert.' >&2 129 echo 'No revert information available. Cannot revert.' >&2
130 fi 130 fi
131 131
132 cat $ADOTDIR/revert-info | sed '1!p' | while read line; do 132 cat $ADOTDIR/revert-info | sed '1!p' | while read line; do
133 dir="$(echo "$line" | cut -d: -f1)" 133 dir="$(echo "$line" | cut -d: -f1)"
134 git --git-dir="$dir/.git" --work-tree="$dir" \ 134 git --git-dir="$dir/.git" --work-tree="$dir" \
135 checkout "$(echo "$line" | cut -d: -f2)" 2> /dev/null 135 checkout "$(echo "$line" | cut -d: -f2)" 2> /dev/null
136 done 136 done
137 137
138 echo "Reverted to state before running -update on $( 138 echo "Reverted to state before running -update on $(
139 cat $ADOTDIR/revert-info | sed -n 1p)." 139 cat $ADOTDIR/revert-info | sed -n 1p)."
140 } 140 }
141 141
142 -antigen-get-clone-dir () { 142 -antigen-get-clone-dir () {
143 # Takes a repo url and gives out the path that this url needs to be cloned 143 # Takes a repo url and gives out the path that this url needs to be cloned
144 # to. Doesn't actually clone anything. 144 # to. Doesn't actually clone anything.
145 echo -n $ADOTDIR/repos/ 145 echo -n $ADOTDIR/repos/
146 146
147 if [[ "$1" == "https://github.com/sorin-ionescu/prezto.git" ]]; then 147 if [[ "$1" == "https://github.com/sorin-ionescu/prezto.git" ]]; then
148 # Prezto's directory *has* to be `.zprezto`. 148 # Prezto's directory *has* to be `.zprezto`.
149 echo .zprezto 149 echo .zprezto
150 150
151 else 151 else
152 echo "$1" | sed \ 152 echo "$1" | sed \
153 -e 's./.-SLASH-.g' \ 153 -e 's./.-SLASH-.g' \
154 -e 's.:.-COLON-.g' \ 154 -e 's.:.-COLON-.g' \
155 -e 's.|.-PIPE-.g' 155 -e 's.|.-PIPE-.g'
156 156
157 fi 157 fi
158 } 158 }
159 159
160 -antigen-get-clone-url () { 160 -antigen-get-clone-url () {
161 # Takes a repo's clone dir and gives out the repo's original url that was 161 # Takes a repo's clone dir and gives out the repo's original url that was
162 # used to create the given directory path. 162 # used to create the given directory path.
163 163
164 if [[ "$1" == ".zprezto" ]]; then 164 if [[ "$1" == ".zprezto" ]]; then
165 # Prezto's (in `.zprezto`), is assumed to be from `sorin-ionescu`'s 165 # Prezto's (in `.zprezto`), is assumed to be from `sorin-ionescu`'s
166 # remote. 166 # remote.
167 echo https://github.com/sorin-ionescu/prezto.git 167 echo https://github.com/sorin-ionescu/prezto.git
168 168
169 else 169 else
170 echo "$1" | sed \ 170 echo "$1" | sed \
171 -e "s:^$ADOTDIR/repos/::" \ 171 -e "s:^$ADOTDIR/repos/::" \
172 -e 's.-SLASH-./.g' \ 172 -e 's.-SLASH-./.g' \
173 -e 's.-COLON-.:.g' \ 173 -e 's.-COLON-.:.g' \
174 -e 's.-PIPE-.|.g' 174 -e 's.-PIPE-.|.g'
175 175
176 fi 176 fi
177 } 177 }
178 178
179 -antigen-ensure-repo () { 179 -antigen-ensure-repo () {
180 180
181 # Ensure that a clone exists for the given repo url and branch. If the first 181 # Ensure that a clone exists for the given repo url and branch. If the first
182 # argument is `--update` and if a clone already exists for the given repo 182 # argument is `--update` and if a clone already exists for the given repo
183 # and branch, it is pull-ed, i.e., updated. 183 # and branch, it is pull-ed, i.e., updated.
184 184
185 # Argument defaults. 185 # Argument defaults.
186 # The url. No sane default for this, so just empty. 186 # The url. No sane default for this, so just empty.
187 local url= 187 local url=
188 # Check if we have to update. 188 # Check if we have to update.
189 local update=false 189 local update=false
190 # Verbose output. 190 # Verbose output.
191 local verbose=false 191 local verbose=false
192 192
193 eval "$(-antigen-parse-args 'url ; update?, verbose?' "$@")" 193 eval "$(-antigen-parse-args 'url ; update?, verbose?' "$@")"
194 shift $# 194 shift $#
195 195
196 # Get the clone's directory as per the given repo url and branch. 196 # Get the clone's directory as per the given repo url and branch.
197 local clone_dir="$(-antigen-get-clone-dir $url)" 197 local clone_dir="$(-antigen-get-clone-dir $url)"
198 198
199 # A temporary function wrapping the `git` command with repeated arguments. 199 # A temporary function wrapping the `git` command with repeated arguments.
200 --plugin-git () { 200 --plugin-git () {
201 eval git --no-pager \ 201 eval git --no-pager \
202 --git-dir=$clone_dir/.git --work-tree=$clone_dir "$@" 202 --git-dir=$clone_dir/.git --work-tree=$clone_dir "$@"
203 } 203 }
204 204
205 # Clone if it doesn't already exist. 205 # Clone if it doesn't already exist.
206 if [[ ! -d $clone_dir ]]; then 206 if [[ ! -d $clone_dir ]]; then
207 git clone --recursive "${url%|*}" "$clone_dir" 207 git clone --recursive "${url%|*}" "$clone_dir"
208 elif $update; then 208 elif $update; then
209 # Save current revision. 209 # Save current revision.
210 local old_rev="$(--plugin-git rev-parse HEAD)" 210 local old_rev="$(--plugin-git rev-parse HEAD)"
211 # Pull changes if update requested. 211 # Pull changes if update requested.
212 --plugin-git pull 212 --plugin-git pull
213 # update submodules 213 # Update submodules.
214 pushd 214 --plugin-git submodule update --recursive
215 cd "$clone_dir"
216 git submodule update --recursive
217 popd
218 # Get the new revision. 215 # Get the new revision.
219 local new_rev="$(--plugin-git rev-parse HEAD)" 216 local new_rev="$(--plugin-git rev-parse HEAD)"
220 fi 217 fi
221 218
222 # If its a specific branch that we want, checkout that branch. 219 # If its a specific branch that we want, checkout that branch.
223 if [[ $url == *\|* ]]; then 220 if [[ $url == *\|* ]]; then
224 local current_branch=${$(--plugin-git symbolic-ref HEAD)##refs/heads/} 221 local current_branch=${$(--plugin-git symbolic-ref HEAD)##refs/heads/}
225 local requested_branch="${url#*|}" 222 local requested_branch="${url#*|}"
226 # Only do the checkout when we are not already on the branch. 223 # Only do the checkout when we are not already on the branch.
227 [[ $requested_branch != $current_branch ]] && 224 [[ $requested_branch != $current_branch ]] &&
228 --plugin-git checkout $requested_branch 225 --plugin-git checkout $requested_branch
229 fi 226 fi
230 227
231 if ! [[ -z $old_rev || $old_rev == $new_rev ]]; then 228 if ! [[ -z $old_rev || $old_rev == $new_rev ]]; then
232 echo Updated from ${old_rev:0:7} to ${new_rev:0:7}. 229 echo Updated from ${old_rev:0:7} to ${new_rev:0:7}.
233 if $verbose; then 230 if $verbose; then
234 --plugin-git log --oneline --reverse --no-merges --stat '@{1}..' 231 --plugin-git log --oneline --reverse --no-merges --stat '@{1}..'
235 fi 232 fi
236 fi 233 fi
237 234
238 # Remove the temporary git wrapper function. 235 # Remove the temporary git wrapper function.
239 unfunction -- --plugin-git 236 unfunction -- --plugin-git
240 237
241 } 238 }
242 239
243 -antigen-load () { 240 -antigen-load () {
244 241
245 local url="$1" 242 local url="$1"
246 local loc="$2" 243 local loc="$2"
247 local btype="$3" 244 local btype="$3"
248 local make_local_clone="$4" 245 local make_local_clone="$4"
249 246
250 # The full location where the plugin is located. 247 # The full location where the plugin is located.
251 local location 248 local location
252 if $make_local_clone; then 249 if $make_local_clone; then
253 location="$(-antigen-get-clone-dir "$url")/$loc" 250 location="$(-antigen-get-clone-dir "$url")/$loc"
254 else 251 else
255 location="$url" 252 location="$url"
256 fi 253 fi
257 254
258 if [[ $btype == theme ]]; then 255 if [[ $btype == theme ]]; then
259 256
260 # Of course, if its a theme, the location would point to the script 257 # Of course, if its a theme, the location would point to the script
261 # file. 258 # file.
262 source "$location" 259 source "$location"
263 260
264 else 261 else
265 262
266 # Source the plugin script. 263 # Source the plugin script.
267 # FIXME: I don't know. Looks very very ugly. Needs a better 264 # FIXME: I don't know. Looks very very ugly. Needs a better
268 # implementation once tests are ready. 265 # implementation once tests are ready.
269 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')" 266 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')"
270 267
271 if [[ -f $location/$script_loc ]]; then 268 if [[ -f $location/$script_loc ]]; then
272 # If we have a `*.plugin.zsh`, source it. 269 # If we have a `*.plugin.zsh`, source it.
273 source "$location/$script_loc" 270 source "$location/$script_loc"
274 271
275 elif [[ -f $location/init.zsh ]]; then 272 elif [[ -f $location/init.zsh ]]; then
276 # If we have a `init.zsh`, source it. 273 # If we have a `init.zsh`, source it.
277 source "$location/init.zsh" 274 source "$location/init.zsh"
278 275
279 elif ls "$location" | grep -qm1 '\.zsh$'; then 276 elif ls "$location" | grep -qm1 '\.zsh$'; then
280 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` 277 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh`
281 # files. 278 # files.
282 for script ($location/*.zsh(N)) source "$script" 279 for script ($location/*.zsh(N)) source "$script"
283 280
284 elif ls "$location" | grep -qm1 '\.sh$'; then 281 elif ls "$location" | grep -qm1 '\.sh$'; then
285 # If there are no `*.zsh` files either, we look for and source any 282 # If there are no `*.zsh` files either, we look for and source any
286 # `*.sh` files instead. 283 # `*.sh` files instead.
287 for script ($location/*.sh(N)) source "$script" 284 for script ($location/*.sh(N)) source "$script"
288 285
289 fi 286 fi
290 287
291 # Add to $fpath, for completion(s). 288 # Add to $fpath, for completion(s).
292 fpath=($location $fpath) 289 fpath=($location $fpath)
293 290
294 fi 291 fi
295 292
296 } 293 }
297 294
298 antigen-cleanup () { 295 antigen-cleanup () {
299 296
300 # Cleanup unused repositories. 297 # Cleanup unused repositories.
301 298
302 local force=false 299 local force=false
303 if [[ $1 == --force ]]; then 300 if [[ $1 == --force ]]; then
304 force=true 301 force=true
305 fi 302 fi
306 303
307 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then 304 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then
308 echo "You don't have any bundles." 305 echo "You don't have any bundles."
309 return 0 306 return 0
310 fi 307 fi
311 308
312 # Find directores in ADOTDIR/repos, that are not in the bundles record. 309 # Find directores in ADOTDIR/repos, that are not in the bundles record.
313 local unused_clones="$(comm -13 \ 310 local unused_clones="$(comm -13 \
314 <(-antigen-echo-record | 311 <(-antigen-echo-record |
315 awk '$4 == "true" {print $1}' | 312 awk '$4 == "true" {print $1}' |
316 while read line; do 313 while read line; do
317 -antigen-get-clone-dir "$line" 314 -antigen-get-clone-dir "$line"
318 done | 315 done |
319 sort -u) \ 316 sort -u) \
320 <(ls -d "$ADOTDIR/repos/"* | sort -u))" 317 <(ls -d "$ADOTDIR/repos/"* | sort -u))"
321 318
322 if [[ -z $unused_clones ]]; then 319 if [[ -z $unused_clones ]]; then
323 echo "You don't have any unidentified bundles." 320 echo "You don't have any unidentified bundles."
324 return 0 321 return 0
325 fi 322 fi
326 323
327 echo 'You have clones for the following repos, but are not used.' 324 echo 'You have clones for the following repos, but are not used.'
328 echo "$unused_clones" | 325 echo "$unused_clones" |
329 while read line; do 326 while read line; do
330 -antigen-get-clone-url "$line" 327 -antigen-get-clone-url "$line"
331 done | 328 done |
332 sed -e 's/^/ /' -e 's/|/, branch /' 329 sed -e 's/^/ /' -e 's/|/, branch /'
333 330
334 if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then 331 if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then
335 echo 332 echo
336 echo 333 echo
337 echo "$unused_clones" | while read line; do 334 echo "$unused_clones" | while read line; do
338 echo -n "Deleting clone for $(-antigen-get-clone-url "$line")..." 335 echo -n "Deleting clone for $(-antigen-get-clone-url "$line")..."
339 rm -rf "$line" 336 rm -rf "$line"
340 echo ' done.' 337 echo ' done.'
341 done 338 done
342 else 339 else
343 echo 340 echo
344 echo Nothing deleted. 341 echo Nothing deleted.
345 fi 342 fi
346 } 343 }
347 344
348 antigen-lib () { 345 antigen-lib () {
349 antigen-bundle --loc=lib 346 antigen-bundle --loc=lib
350 } 347 }
351 348
352 antigen-prezto-lib () { 349 antigen-prezto-lib () {
353 antigen-bundle sorin-ionescu/prezto 350 antigen-bundle sorin-ionescu/prezto
354 export ZDOTDIR=$ADOTDIR/repos/ 351 export ZDOTDIR=$ADOTDIR/repos/
355 } 352 }
356 353
357 antigen-theme () { 354 antigen-theme () {
358 355
359 if [[ "$1" != */* && "$1" != --* ]]; then 356 if [[ "$1" != */* && "$1" != --* ]]; then
360 # The first argument is just a name of the plugin, to be picked up from 357 # The first argument is just a name of the plugin, to be picked up from
361 # the default repo. 358 # the default repo.
362 local name="${1:-robbyrussell}" 359 local name="${1:-robbyrussell}"
363 antigen-bundle --loc=themes/$name --btype=theme 360 antigen-bundle --loc=themes/$name --btype=theme
364 361
365 else 362 else
366 antigen-bundle "$@" --btype=theme 363 antigen-bundle "$@" --btype=theme
367 364
368 fi 365 fi
369 366
370 } 367 }
371 368
372 antigen-apply () { 369 antigen-apply () {
373 # Initialize completion. 370 # Initialize completion.
374 # TODO: Only load completions if there are any changes to the bundle 371 # TODO: Only load completions if there are any changes to the bundle
375 # repositories. 372 # repositories.
376 compinit -i 373 compinit -i
377 } 374 }
378 375
379 antigen-list () { 376 antigen-list () {
380 # List all currently installed bundles. 377 # List all currently installed bundles.
381 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then 378 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
382 echo "You don't have any bundles." >&2 379 echo "You don't have any bundles." >&2
383 return 1 380 return 1
384 else 381 else
385 -antigen-echo-record | sort -u 382 -antigen-echo-record | sort -u
386 fi 383 fi
387 } 384 }
388 385
389 antigen-snapshot () { 386 antigen-snapshot () {
390 387
391 local snapshot_file="${1:-antigen-shapshot}" 388 local snapshot_file="${1:-antigen-shapshot}"
392 389
393 # The snapshot content lines are pairs of repo-url and git version hash, in 390 # The snapshot content lines are pairs of repo-url and git version hash, in
394 # the form: 391 # the form:
395 # <version-hash> <repo-url> 392 # <version-hash> <repo-url>
396 local snapshot_content="$(-antigen-echo-record | 393 local snapshot_content="$(-antigen-echo-record |
397 grep 'true$' | 394 grep 'true$' |
398 sed 's/ .*$//' | 395 sed 's/ .*$//' |
399 sort -u | 396 sort -u |
400 while read url; do 397 while read url; do
401 local dir="$(-antigen-get-clone-dir "$url")" 398 local dir="$(-antigen-get-clone-dir "$url")"
402 local version_hash="$(cd "$dir" && git rev-parse HEAD)" 399 local version_hash="$(cd "$dir" && git rev-parse HEAD)"
403 echo "$version_hash $url" 400 echo "$version_hash $url"
404 done)" 401 done)"
405 402
406 { 403 {
407 # The first line in the snapshot file is for metadata, in the form: 404 # The first line in the snapshot file is for metadata, in the form:
408 # key='value'; key='value'; key='value'; 405 # key='value'; key='value'; key='value';
409 # Where `key`s are valid shell variable names. 406 # Where `key`s are valid shell variable names.
410 407
411 # Snapshot version. Has no relation to antigen version. If the snapshot 408 # Snapshot version. Has no relation to antigen version. If the snapshot
412 # file format changes, this number can be incremented. 409 # file format changes, this number can be incremented.
413 echo -n "version='1';" 410 echo -n "version='1';"
414 411
415 # Snapshot creation date+time. 412 # Snapshot creation date+time.
416 echo -n " created_on='$(date)';" 413 echo -n " created_on='$(date)';"
417 414
418 # Add a checksum with the md5 checksum of all the snapshot lines. 415 # Add a checksum with the md5 checksum of all the snapshot lines.
419 local checksum="$(echo "$snapshot_content" | md5sum)" 416 local checksum="$(echo "$snapshot_content" | md5sum)"
420 echo -n " checksum='${checksum%% *}';" 417 echo -n " checksum='${checksum%% *}';"
421 418
422 # A newline after the metadata and then the snapshot lines. 419 # A newline after the metadata and then the snapshot lines.
423 echo "\n$snapshot_content" 420 echo "\n$snapshot_content"
424 421
425 } > "$snapshot_file" 422 } > "$snapshot_file"
426 423
427 } 424 }
428 425
429 antigen-restore () { 426 antigen-restore () {
430 427
431 if [[ $# == 0 ]]; then 428 if [[ $# == 0 ]]; then
432 echo 'Please provide a snapshot file to restore from.' >&2 429 echo 'Please provide a snapshot file to restore from.' >&2
433 return 1 430 return 1
434 fi 431 fi
435 432
436 local snapshot_file="$1" 433 local snapshot_file="$1"
437 434
438 # TODO: Before doing anything with the snapshot file, verify its checksum. 435 # TODO: Before doing anything with the snapshot file, verify its checksum.
439 # If it fails, notify this to the user and confirm if restore should 436 # If it fails, notify this to the user and confirm if restore should
440 # proceed. 437 # proceed.
441 438
442 echo -n "Restoring from $snapshot_file..." 439 echo -n "Restoring from $snapshot_file..."
443 440
444 sed -n '1!p' "$snapshot_file" | 441 sed -n '1!p' "$snapshot_file" |
445 while read line; do 442 while read line; do
446 443
447 local version_hash="${line%% *}" 444 local version_hash="${line%% *}"
448 local url="${line##* }" 445 local url="${line##* }"
449 local clone_dir="$(-antigen-get-clone-dir "$url")" 446 local clone_dir="$(-antigen-get-clone-dir "$url")"
450 447
451 if [[ ! -d $clone_dir ]]; then 448 if [[ ! -d $clone_dir ]]; then
452 git clone "$url" "$clone_dir" > /dev/null 449 git clone "$url" "$clone_dir" > /dev/null
453 fi 450 fi
454 451
455 (cd "$clone_dir" && git checkout $version_hash) 2> /dev/null 452 (cd "$clone_dir" && git checkout $version_hash) 2> /dev/null
456 453
457 done 454 done
458 455
459 echo ' done.' 456 echo ' done.'
460 echo 'Please open a new shell to get the restored changes.' 457 echo 'Please open a new shell to get the restored changes.'
461 } 458 }
462 459
463 antigen-help () { 460 antigen-help () {
464 cat <<EOF 461 cat <<EOF
465 Antigen is a plugin management system for zsh. It makes it easy to grab awesome 462 Antigen is a plugin management system for zsh. It makes it easy to grab awesome
466 shell scripts and utilities, put up on github. For further details and complete 463 shell scripts and utilities, put up on github. For further details and complete
467 documentation, visit the project's page at 'http://antigen.sharats.me'. 464 documentation, visit the project's page at 'http://antigen.sharats.me'.
468 EOF 465 EOF
469 } 466 }
470 467
471 # A syntax sugar to avoid the `-` when calling antigen commands. With this 468 # A syntax sugar to avoid the `-` when calling antigen commands. With this
472 # function, you can write `antigen-bundle` as `antigen bundle` and so on. 469 # function, you can write `antigen-bundle` as `antigen bundle` and so on.
473 antigen () { 470 antigen () {
474 local cmd="$1" 471 local cmd="$1"
475 shift 472 shift
476 "antigen-$cmd" "$@" 473 "antigen-$cmd" "$@"
477 } 474 }
478 475
479 -antigen-parse-args () { 476 -antigen-parse-args () {
480 # An argument parsing functionality to parse arguments the *antigen* way :). 477 # An argument parsing functionality to parse arguments the *antigen* way :).
481 # Takes one first argument (called spec), which dictates how to parse and 478 # Takes one first argument (called spec), which dictates how to parse and
482 # the rest of the arguments are parsed. Outputs a piece of valid shell code 479 # the rest of the arguments are parsed. Outputs a piece of valid shell code
483 # that can be passed to `eval` inside a function which creates the arguments 480 # that can be passed to `eval` inside a function which creates the arguments
484 # and their values as local variables. Suggested use is to set the defaults 481 # and their values as local variables. Suggested use is to set the defaults
485 # to all arguments first and then eval the output of this function. 482 # to all arguments first and then eval the output of this function.
486 483
487 # Spec: Only long argument supported. No support for parsing short options. 484 # Spec: Only long argument supported. No support for parsing short options.
488 # The spec must have two sections, separated by a `;`. 485 # The spec must have two sections, separated by a `;`.
489 # '<positional-arguments>;<keyword-only-arguments>' 486 # '<positional-arguments>;<keyword-only-arguments>'
490 # Positional arguments are passed as just values, like `command a b`. 487 # Positional arguments are passed as just values, like `command a b`.
491 # Keyword arguments are passed as a `--name=value` pair, like `command 488 # Keyword arguments are passed as a `--name=value` pair, like `command
492 # --arg1=a --arg2=b`. 489 # --arg1=a --arg2=b`.
493 490
494 # Each argument in the spec is separated by a `,`. Each keyword argument can 491 # Each argument in the spec is separated by a `,`. Each keyword argument can
495 # end in a `:` to specifiy that this argument wants a value, otherwise it 492 # end in a `:` to specifiy that this argument wants a value, otherwise it
496 # doesn't take a value. (The value in the output when the keyword argument 493 # doesn't take a value. (The value in the output when the keyword argument
497 # doesn't have a `:` is `true`). 494 # doesn't have a `:` is `true`).
498 495
499 # Arguments in either section can end with a `?` (should come after `:`, if 496 # Arguments in either section can end with a `?` (should come after `:`, if
500 # both are present), means optional. FIXME: Not yet implemented. 497 # both are present), means optional. FIXME: Not yet implemented.
501 498
502 # See the test file, tests/arg-parser.t for (working) examples. 499 # See the test file, tests/arg-parser.t for (working) examples.
503 500
504 local spec="$1" 501 local spec="$1"
505 shift 502 shift
506 503
507 # Sanitize the spec 504 # Sanitize the spec
508 spec="$(echo "$spec" | tr '\n' ' ' | sed 's/[[:space:]]//g')" 505 spec="$(echo "$spec" | tr '\n' ' ' | sed 's/[[:space:]]//g')"
509 506
510 local code='' 507 local code=''
511 508
512 --add-var () { 509 --add-var () {
513 test -z "$code" || code="$code\n" 510 test -z "$code" || code="$code\n"
514 code="${code}local $1='$2'" 511 code="${code}local $1='$2'"
515 } 512 }
516 513
517 local positional_args="$(echo "$spec" | cut -d\; -f1)" 514 local positional_args="$(echo "$spec" | cut -d\; -f1)"
518 local positional_args_count="$(echo $positional_args | 515 local positional_args_count="$(echo $positional_args |
519 awk -F, '{print NF}')" 516 awk -F, '{print NF}')"
520 517
521 # Set spec values based on the positional arguments. 518 # Set spec values based on the positional arguments.
522 local i=1 519 local i=1
523 while ! [[ -z $1 || $1 == --* ]]; do 520 while ! [[ -z $1 || $1 == --* ]]; do
524 521
525 if (( $i > $positional_args_count )); then 522 if (( $i > $positional_args_count )); then
526 echo "Only $positional_args_count positional arguments allowed." >&2 523 echo "Only $positional_args_count positional arguments allowed." >&2
527 echo "Found at least one more: '$1'" >&2 524 echo "Found at least one more: '$1'" >&2
528 return 525 return
529 fi 526 fi
530 527
531 local name_spec="$(echo "$positional_args" | cut -d, -f$i)" 528 local name_spec="$(echo "$positional_args" | cut -d, -f$i)"
532 local name="${${name_spec%\?}%:}" 529 local name="${${name_spec%\?}%:}"
533 local value="$1" 530 local value="$1"
534 531
535 if echo "$code" | grep -qm1 "^local $name="; then 532 if echo "$code" | grep -qm1 "^local $name="; then
536 echo "Argument '$name' repeated with the value '$value'". >&2 533 echo "Argument '$name' repeated with the value '$value'". >&2
537 return 534 return
538 fi 535 fi
539 536
540 --add-var $name "$value" 537 --add-var $name "$value"
541 538
542 shift 539 shift
543 i=$(($i + 1)) 540 i=$(($i + 1))
544 done 541 done
545 542
546 local keyword_args="$( 543 local keyword_args="$(
547 # Positional arguments can double up as keyword arguments too. 544 # Positional arguments can double up as keyword arguments too.
548 echo "$positional_args" | tr , '\n' | 545 echo "$positional_args" | tr , '\n' |
549 while read line; do 546 while read line; do
550 if [[ $line == *\? ]]; then 547 if [[ $line == *\? ]]; then
551 echo "${line%?}:?" 548 echo "${line%?}:?"
552 else 549 else
553 echo "$line:" 550 echo "$line:"
554 fi 551 fi
555 done 552 done
556 553
557 # Specified keyword arguments. 554 # Specified keyword arguments.
558 echo "$spec" | cut -d\; -f2 | tr , '\n' 555 echo "$spec" | cut -d\; -f2 | tr , '\n'
559 )" 556 )"
560 local keyword_args_count="$(echo $keyword_args | awk -F, '{print NF}')" 557 local keyword_args_count="$(echo $keyword_args | awk -F, '{print NF}')"
561 558
562 # Set spec values from keyword arguments, if any. The remaining arguments 559 # Set spec values from keyword arguments, if any. The remaining arguments
563 # are all assumed to be keyword arguments. 560 # are all assumed to be keyword arguments.
564 while [[ $1 == --* ]]; do 561 while [[ $1 == --* ]]; do
565 # Remove the `--` at the start. 562 # Remove the `--` at the start.
566 local arg="${1#--}" 563 local arg="${1#--}"
567 564
568 # Get the argument name and value. 565 # Get the argument name and value.
569 if [[ $arg != *=* ]]; then 566 if [[ $arg != *=* ]]; then
570 local name="$arg" 567 local name="$arg"
571 local value='' 568 local value=''
572 else 569 else
573 local name="${arg%\=*}" 570 local name="${arg%\=*}"
574 local value="${arg#*=}" 571 local value="${arg#*=}"
575 fi 572 fi
576 573
577 if echo "$code" | grep -qm1 "^local $name="; then 574 if echo "$code" | grep -qm1 "^local $name="; then
578 echo "Argument '$name' repeated with the value '$value'". >&2 575 echo "Argument '$name' repeated with the value '$value'". >&2
579 return 576 return
580 fi 577 fi
581 578
582 # The specification for this argument, used for validations. 579 # The specification for this argument, used for validations.
583 local arg_line="$(echo "$keyword_args" | grep -m1 "^$name:\??\?")" 580 local arg_line="$(echo "$keyword_args" | grep -m1 "^$name:\??\?")"
584 581
585 # Validate argument and value. 582 # Validate argument and value.
586 if [[ -z $arg_line ]]; then 583 if [[ -z $arg_line ]]; then
587 # This argument is not known to us. 584 # This argument is not known to us.
588 echo "Unknown argument '$name'." >&2 585 echo "Unknown argument '$name'." >&2
589 return 586 return
590 587
591 elif (echo "$arg_line" | grep -qm1 ':') && [[ -z $value ]]; then 588 elif (echo "$arg_line" | grep -qm1 ':') && [[ -z $value ]]; then
592 # This argument needs a value, but is not provided. 589 # This argument needs a value, but is not provided.
593 echo "Required argument for '$name' not provided." >&2 590 echo "Required argument for '$name' not provided." >&2
594 return 591 return
595 592
596 elif (echo "$arg_line" | grep -vqm1 ':') && [[ ! -z $value ]]; then 593 elif (echo "$arg_line" | grep -vqm1 ':') && [[ ! -z $value ]]; then
597 # This argument doesn't need a value, but is provided. 594 # This argument doesn't need a value, but is provided.
598 echo "No argument required for '$name', but provided '$value'." >&2 595 echo "No argument required for '$name', but provided '$value'." >&2
599 return 596 return
600 597
601 fi 598 fi
602 599
603 if [[ -z $value ]]; then 600 if [[ -z $value ]]; then
604 value=true 601 value=true
605 fi 602 fi
606 603
607 --add-var "${name//-/_}" "$value" 604 --add-var "${name//-/_}" "$value"
608 shift 605 shift
609 done 606 done
610 607
611 echo "$code" 608 echo "$code"
612 609
613 unfunction -- --add-var 610 unfunction -- --add-var
614 611
615 } 612 }
616 613
617 # Echo the bundle specs as in the record. The first line is not echoed since it 614 # Echo the bundle specs as in the record. The first line is not echoed since it
618 # is a blank line. 615 # is a blank line.
619 -antigen-echo-record () { 616 -antigen-echo-record () {
620 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' 617 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
621 } 618 }
622 619
623 -antigen-env-setup () { 620 -antigen-env-setup () {
624 # Pre-startup initializations. 621 # Pre-startup initializations.
625 -set-default ANTIGEN_DEFAULT_REPO_URL \ 622 -set-default ANTIGEN_DEFAULT_REPO_URL \
626 https://github.com/robbyrussell/oh-my-zsh.git 623 https://github.com/robbyrussell/oh-my-zsh.git
627 -set-default ADOTDIR $HOME/.antigen 624 -set-default ADOTDIR $HOME/.antigen
628 625
629 # Load the compinit module. Required for `compdef` to be defined, which is 626 # Load the compinit module. Required for `compdef` to be defined, which is
630 # used by many plugins to define completions. 627 # used by many plugins to define completions.
631 autoload -U compinit 628 autoload -U compinit
632 compinit -i 629 compinit -i
633 630
634 # Setup antigen's own completion. 631 # Setup antigen's own completion.
635 compdef _antigen antigen 632 compdef _antigen antigen
636 } 633 }
637 634
638 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is 635 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
639 # not already set. 636 # not already set.
640 -set-default () { 637 -set-default () {
641 local arg_name="$1" 638 local arg_name="$1"
642 local arg_value="$2" 639 local arg_value="$2"
643 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 640 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
644 } 641 }
645 642
646 # Setup antigen's autocompletion 643 # Setup antigen's autocompletion
647 _antigen () { 644 _antigen () {
648 compadd \ 645 compadd \
649 bundle\ 646 bundle\
650 bundles\ 647 bundles\
651 update\ 648 update\
652 revert\ 649 revert\
653 list\ 650 list\
654 cleanup\ 651 cleanup\
655 lib\ 652 lib\
656 theme\ 653 theme\
657 apply\ 654 apply\
658 help 655 help
659 } 656 }
660 657
661 -antigen-env-setup 658 -antigen-env-setup
662 659