Commit 0912ba47242d8d83a90aa774a5ab2836f8b28d21

Authored by Shrikant Sharat
1 parent c1479baff8

Print a summary of changes pulled, on -update.

Running an `antigen-update` will now print a --oneline git log of the changes
that are pulled. Helpful if interested in what has just been updated and a
glimpse on what can be expected.

Showing 1 changed file with 17 additions and 2 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 --verbose "$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 # Argument defaults.
144 # Check if we have to update. 144 # Check if we have to update.
145 local update=false 145 local update=false
146 # Verbose output. 146 # Verbose output.
147 local verbose=false 147 local verbose=false
148 148
149 # Load any boolean arguments specified. 149 # Load any boolean arguments specified.
150 while [[ $1 == --* ]]; do 150 while [[ $1 == --* ]]; do
151 eval "local '${1#--}=true'" 151 eval "local '${1#--}=true'"
152 shift 152 shift
153 done 153 done
154 154
155 # 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.
156 local url="$1" 156 local url="$1"
157 local clone_dir="$(-antigen-get-clone-dir $url)" 157 local clone_dir="$(-antigen-get-clone-dir $url)"
158 158
159 # Clone if it doesn't already exist. 159 # Clone if it doesn't already exist.
160 if [[ ! -d $clone_dir ]]; then 160 if [[ ! -d $clone_dir ]]; then
161 git clone "${url%|*}" "$clone_dir" 161 git clone "${url%|*}" "$clone_dir"
162 elif $update; then 162 elif $update; then
163 # Save current revision.
164 old_rev="$(git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \
165 rev-parse HEAD)"
163 # Pull changes if update requested. 166 # Pull changes if update requested.
164 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" pull 167 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \
168 pull
169 # Get the new revision.
170 new_rev="$(git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \
171 rev-parse HEAD)"
165 fi 172 fi
166 173
167 # If its a specific branch that we want, checkout that branch. 174 # If its a specific branch that we want, checkout that branch.
168 if [[ $url == *\|* ]]; then 175 if [[ $url == *\|* ]]; then
169 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \ 176 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \
170 checkout "${url#*|}" 177 checkout "${url#*|}"
171 fi 178 fi
172 179
180 if ! [[ -z $old_rev || $old_rev == $new_rev ]]; then
181 echo Updated from ${old_rev:0:7} to ${new_rev:0:7}.
182 if $verbose; then
183 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \
184 log --oneline --reverse --no-merges --stat '@{1}..'
185 fi
186 fi
187
173 } 188 }
174 189
175 -antigen-load () { 190 -antigen-load () {
176 191
177 local url="$1" 192 local url="$1"
178 local loc="$2" 193 local loc="$2"
179 local btype="$3" 194 local btype="$3"
180 195
181 # The full location where the plugin is located. 196 # The full location where the plugin is located.
182 local location="$(-antigen-get-clone-dir "$url")/$loc" 197 local location="$(-antigen-get-clone-dir "$url")/$loc"
183 198
184 if [[ $btype == theme ]]; then 199 if [[ $btype == theme ]]; then
185 200
186 # Of course, if its a theme, the location would point to the script 201 # Of course, if its a theme, the location would point to the script
187 # file. 202 # file.
188 source "$location" 203 source "$location"
189 204
190 else 205 else
191 206
192 # Source the plugin script 207 # Source the plugin script
193 # FIXME: I don't know. Looks very very ugly. Needs a better 208 # FIXME: I don't know. Looks very very ugly. Needs a better
194 # implementation once tests are ready. 209 # implementation once tests are ready.
195 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')" 210 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')"
196 211
197 if [[ -f $script_loc ]]; then 212 if [[ -f $script_loc ]]; then
198 # If we have a `*.plugin.zsh`, source it. 213 # If we have a `*.plugin.zsh`, source it.
199 source "$script_loc" 214 source "$script_loc"
200 215
201 elif [[ ! -z "$(ls "$location" | grep -m1 '\.zsh$')" ]]; then 216 elif [[ ! -z "$(ls "$location" | grep -m1 '\.zsh$')" ]]; then
202 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` 217 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh`
203 # files. 218 # files.
204 for script ($location/*.zsh(N)) source "$script" 219 for script ($location/*.zsh(N)) source "$script"
205 220
206 elif [[ ! -z "$(ls "$location" | grep -m1 '\.sh$')" ]]; then 221 elif [[ ! -z "$(ls "$location" | grep -m1 '\.sh$')" ]]; then
207 # If there are no `*.zsh` files either, we look for and source any 222 # If there are no `*.zsh` files either, we look for and source any
208 # `*.sh` files instead. 223 # `*.sh` files instead.
209 for script ($location/*.sh(N)) source "$script" 224 for script ($location/*.sh(N)) source "$script"
210 225
211 fi 226 fi
212 227
213 # Add to $fpath, for completion(s). 228 # Add to $fpath, for completion(s).
214 fpath=($location $fpath) 229 fpath=($location $fpath)
215 230
216 fi 231 fi
217 232
218 } 233 }
219 234
220 antigen-cleanup () { 235 antigen-cleanup () {
221 236
222 # Cleanup unused repositories. 237 # Cleanup unused repositories.
223 238
224 local force=false 239 local force=false
225 if [[ $1 == --force ]]; then 240 if [[ $1 == --force ]]; then
226 force=true 241 force=true
227 fi 242 fi
228 243
229 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then 244 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then
230 echo "You don't have any bundles." 245 echo "You don't have any bundles."
231 return 0 246 return 0
232 fi 247 fi
233 248
234 # Find directores in ADOTDIR/repos, that are not in the bundles record. 249 # Find directores in ADOTDIR/repos, that are not in the bundles record.
235 local unused_clones="$(comm -13 \ 250 local unused_clones="$(comm -13 \
236 <(-antigen-echo-record \ 251 <(-antigen-echo-record \
237 | awk '{print $1}' \ 252 | awk '{print $1}' \
238 | while read line; do 253 | while read line; do
239 -antigen-get-clone-dir "$line" 254 -antigen-get-clone-dir "$line"
240 done \ 255 done \
241 | sort -u) \ 256 | sort -u) \
242 <(ls -d "$ADOTDIR/repos/"* | sort -u))" 257 <(ls -d "$ADOTDIR/repos/"* | sort -u))"
243 258
244 if [[ -z $unused_clones ]]; then 259 if [[ -z $unused_clones ]]; then
245 echo "You don't have any unidentified bundles." 260 echo "You don't have any unidentified bundles."
246 return 0 261 return 0
247 fi 262 fi
248 263
249 echo 'You have clones for the following repos, but are not used.' 264 echo 'You have clones for the following repos, but are not used.'
250 echo "$unused_clones" \ 265 echo "$unused_clones" \
251 | while read line; do 266 | while read line; do
252 -antigen-get-clone-url "$line" 267 -antigen-get-clone-url "$line"
253 done \ 268 done \
254 | sed -e 's/^/ /' -e 's/|/, branch /' 269 | sed -e 's/^/ /' -e 's/|/, branch /'
255 270
256 if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then 271 if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then
257 echo 272 echo
258 echo 273 echo
259 echo "$unused_clones" | while read line; do 274 echo "$unused_clones" | while read line; do
260 echo -n "Deleting clone for $(-antigen-get-clone-url "$line")..." 275 echo -n "Deleting clone for $(-antigen-get-clone-url "$line")..."
261 rm -rf "$line" 276 rm -rf "$line"
262 echo ' done.' 277 echo ' done.'
263 done 278 done
264 else 279 else
265 echo 280 echo
266 echo Nothing deleted. 281 echo Nothing deleted.
267 fi 282 fi
268 } 283 }
269 284
270 antigen-lib () { 285 antigen-lib () {
271 antigen-bundle --loc=lib 286 antigen-bundle --loc=lib
272 } 287 }
273 288
274 antigen-theme () { 289 antigen-theme () {
275 local name="${1:-robbyrussell}" 290 local name="${1:-robbyrussell}"
276 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme 291 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme
277 } 292 }
278 293
279 antigen-apply () { 294 antigen-apply () {
280 # Initialize completion. 295 # Initialize completion.
281 # TODO: Only load completions if there are any changes to the bundle 296 # TODO: Only load completions if there are any changes to the bundle
282 # repositories. 297 # repositories.
283 compinit -i 298 compinit -i
284 } 299 }
285 300
286 antigen-list () { 301 antigen-list () {
287 # List all currently installed bundles 302 # List all currently installed bundles
288 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then 303 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
289 echo "You don't have any bundles." >&2 304 echo "You don't have any bundles." >&2
290 return 1 305 return 1
291 else 306 else
292 -antigen-echo-record | sort -u 307 -antigen-echo-record | sort -u
293 fi 308 fi
294 } 309 }
295 310
296 antigen-help () { 311 antigen-help () {
297 cat <<EOF 312 cat <<EOF
298 Antigen is a plugin management system for zsh. It makes it easy to grab awesome 313 Antigen is a plugin management system for zsh. It makes it easy to grab awesome
299 shell scripts and utilities, put up on github. For further details and complete 314 shell scripts and utilities, put up on github. For further details and complete
300 documentation, visit the project's page at 'http://antigen.sharats.me'. 315 documentation, visit the project's page at 'http://antigen.sharats.me'.
301 EOF 316 EOF
302 } 317 }
303 318
304 # A syntax sugar to avoid the `-` when calling antigen commands. With this 319 # A syntax sugar to avoid the `-` when calling antigen commands. With this
305 # function, you can write `antigen-bundle` as `antigen bundle` and so on. 320 # function, you can write `antigen-bundle` as `antigen bundle` and so on.
306 antigen () { 321 antigen () {
307 local cmd="$1" 322 local cmd="$1"
308 shift 323 shift
309 "antigen-$cmd" "$@" 324 "antigen-$cmd" "$@"
310 } 325 }
311 326
312 # Echo the bundle specs as in the record. The first line is not echoed since it 327 # Echo the bundle specs as in the record. The first line is not echoed since it
313 # is a blank line. 328 # is a blank line.
314 -antigen-echo-record () { 329 -antigen-echo-record () {
315 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' 330 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
316 } 331 }
317 332
318 -antigen-env-setup () { 333 -antigen-env-setup () {
319 # Pre-startup initializations 334 # Pre-startup initializations
320 -set-default ANTIGEN_DEFAULT_REPO_URL \ 335 -set-default ANTIGEN_DEFAULT_REPO_URL \
321 https://github.com/robbyrussell/oh-my-zsh.git 336 https://github.com/robbyrussell/oh-my-zsh.git
322 -set-default ADOTDIR $HOME/.antigen 337 -set-default ADOTDIR $HOME/.antigen
323 338
324 # Load the compinit module 339 # Load the compinit module
325 autoload -U compinit 340 autoload -U compinit
326 341
327 # Without the following, `compdef` function is not defined. 342 # Without the following, `compdef` function is not defined.
328 compinit -i 343 compinit -i
329 } 344 }
330 345
331 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is 346 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
332 # not already set. 347 # not already set.
333 -set-default () { 348 -set-default () {
334 local arg_name="$1" 349 local arg_name="$1"
335 local arg_value="$2" 350 local arg_value="$2"
336 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 351 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
337 } 352 }
338 353
339 -antigen-env-setup 354 -antigen-env-setup
340 355