Commit c1479baff862e554946839b249cc4a9226b57674

Authored by Shrikant Sharat
1 parent 27ff756cd1

Accept a --verbose option to -ensure-repo command.

This will be used to make the -ensure-repo command output more detailed
information on the changes, if any, made to the clone.

Showing 1 changed file with 8 additions and 3 deletions Inline Diff

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