Commit f71e897ffae3529fa7a71f7ce3ed071288fd1975

Authored by Arash Rouhani
1 parent 290edb0058

Apply DRY principles in git invocations

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