Commit 41abcf6628756f0d04fce4ec3337820d71357a10

Authored by Shrikant Sharat
1 parent b43a3cf1f1

Simpler implementation of --no-local-clone.

Using a new `make_local_clone` variable which is an internal field that better
represents whether we need a local clone.

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