Commit 2923c490cc7e8f4d3677aef51e0605d91c61ed85

Authored by Shrikant Sharat
1 parent 759e381e13

Branch information is saved along with the url.

Keeping the branch and the repo url as separate fields for each bundle entry,
seems to be only causing trouble. Now, the repo url and the branch are joined
togather with a `|` character and split only when needed.

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