Commit 843297b2062084271496e3998623e446c397b832

Authored by Shrikant Sharat
1 parent 2e6d0c4163

Initial implementation of --branch.

The `antigen-bundle` command takes a `--branch` keyword-only argument that
specified the branch of the repo to be used for this bundle.

Alpha implementation. Needs more testing.

Showing 1 changed file with 34 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:
12 # branch - The branch of the repo to use for this bundle.
11 antigen-bundle () { 13 antigen-bundle () {
12 14
13 # Bundle spec arguments' default values. 15 # Bundle spec arguments' default values.
14 local url="$ANTIGEN_DEFAULT_REPO_URL" 16 local url="$ANTIGEN_DEFAULT_REPO_URL"
15 local loc=/ 17 local loc=/
18 local branch=-
16 local btype=plugin 19 local btype=plugin
17 20
18 # Set spec values based on the positional arguments. 21 # Set spec values based on the positional arguments.
19 local position_args='url loc' 22 local position_args='url loc'
20 local i=1 23 local i=1
21 while ! [[ -z $1 || $1 == --*=* ]]; do 24 while ! [[ -z $1 || $1 == --*=* ]]; do
22 local arg_name="$(echo "$position_args" | cut -d\ -f$i)" 25 local arg_name="$(echo "$position_args" | cut -d\ -f$i)"
23 local arg_value="$1" 26 local arg_value="$1"
24 eval "local $arg_name='$arg_value'" 27 eval "local $arg_name='$arg_value'"
25 shift 28 shift
26 i=$(($i + 1)) 29 i=$(($i + 1))
27 done 30 done
28 31
29 # Check if url is just the plugin name. Super short syntax. 32 # Check if url is just the plugin name. Super short syntax.
30 if [[ "$url" != */* ]]; then 33 if [[ "$url" != */* ]]; then
31 loc="plugins/$url" 34 loc="plugins/$url"
32 url="$ANTIGEN_DEFAULT_REPO_URL" 35 url="$ANTIGEN_DEFAULT_REPO_URL"
33 fi 36 fi
34 37
35 # Set spec values from keyword arguments, if any. The remaining arguments 38 # Set spec values from keyword arguments, if any. The remaining arguments
36 # are all assumed to be keyword arguments. 39 # are all assumed to be keyword arguments.
37 while [[ $1 == --*=* ]]; do 40 while [[ $1 == --*=* ]]; do
38 local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')" 41 local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')"
39 local arg_value="$(echo "$1" | cut -d= -f2)" 42 local arg_value="$(echo "$1" | cut -d= -f2)"
40 eval "local $arg_name='$arg_value'" 43 eval "local $arg_name='$arg_value'"
41 shift 44 shift
42 done 45 done
43 46
44 # Resolve the url. 47 # Resolve the url.
45 if [[ $url != git://* && $url != https://* && $url != /* ]]; then 48 if [[ $url != git://* && $url != https://* && $url != /* ]]; then
46 url="${url%.git}" 49 url="${url%.git}"
47 url="https://github.com/$url.git" 50 url="https://github.com/$url.git"
48 fi 51 fi
49 52
50 # Add it to the record. 53 # Add it to the record.
51 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype" 54 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype $branch"
52 55
53 -antigen-ensure-repo "$url" 56 -antigen-ensure-repo "$url" "$branch"
54 57
55 -antigen-load "$url" "$loc" "$btype" 58 -antigen-load "$url" "$loc" "$btype" "$branch"
56 59
57 } 60 }
58 61
59 antigen-bundles () { 62 antigen-bundles () {
60 # Bulk add many bundles at one go. Empty lines and lines starting with a `#` 63 # Bulk add many bundles at one go. Empty lines and lines starting with a `#`
61 # are ignored. Everything else is given to `antigen-bundle` as is, no 64 # are ignored. Everything else is given to `antigen-bundle` as is, no
62 # quoting rules applied. 65 # quoting rules applied.
63 66
64 local line 67 local line
65 68
66 grep -v '^\s*$\|^#' | while read line; do 69 grep -v '^\s*$\|^#' | while read line; do
67 # Using `eval` so that we can use the shell-style quoting in each line 70 # Using `eval` so that we can use the shell-style quoting in each line
68 # piped to `antigen-bundles`. 71 # piped to `antigen-bundles`.
69 eval "antigen-bundle $line" 72 eval "antigen-bundle $line"
70 done 73 done
71 } 74 }
72 75
73 antigen-update () { 76 antigen-update () {
74 # Update your bundles, i.e., `git pull` in all the plugin repos. 77 # Update your bundles, i.e., `git pull` in all the plugin repos.
75 -antigen-echo-record | awk '{print $1}' | sort -u | while read url; do 78 -antigen-echo-record | awk '{print $1}' | sort -u | while read url; do
76 -antigen-ensure-repo --update "$url" 79 -antigen-ensure-repo --update "$url"
77 done 80 done
78 } 81 }
79 82
80 -antigen-get-clone-dir () { 83 -antigen-get-clone-dir () {
81 # Takes a repo url and gives out the path that this url needs to be cloned 84 # Takes a repo url and gives out the path that this url needs to be cloned
82 # to. Doesn't actually clone anything. 85 # to. Doesn't actually clone anything.
83 # TODO: Memoize? 86 # TODO: Memoize?
87 local url="$1"
88 local branch="$2"
84 echo -n $ADOTDIR/repos/ 89 echo -n $ADOTDIR/repos/
85 echo "$1" | sed \ 90
91 local branched_url="$url"
92
93 if [[ "$branch" != - ]]; then
94 branched_url="$branched_url|$branch"
95 fi
96
97 echo "$branched_url" | sed \
86 -e 's/\.git$//' \ 98 -e 's/\.git$//' \
87 -e 's./.-SLASH-.g' \ 99 -e 's./.-SLASH-.g' \
88 -e 's.:.-COLON-.g' 100 -e 's.:.-COLON-.g' \
101 -e 's.|.-PIPE-.g'
89 } 102 }
90 103
91 -antigen-get-clone-url () { 104 -antigen-get-clone-url () {
92 # Takes a repo's clone dir and gives out the repo's original url that was 105 # Takes a repo's clone dir and gives out the repo's original url that was
93 # used to create the given directory path. 106 # used to create the given directory path.
94 # TODO: Memoize? 107 # TODO: Memoize?
95 echo "$1" | sed \ 108 echo "$1" | sed \
96 -e "s:^$ADOTDIR/repos/::" \ 109 -e "s:^$ADOTDIR/repos/::" \
97 -e 's/$/.git/' \ 110 -e 's/$/.git/' \
98 -e 's.-SLASH-./.g' \ 111 -e 's.-SLASH-./.g' \
99 -e 's.-COLON-.:.g' 112 -e 's.-COLON-.:.g' \
113 -e 's.-PIPE-.|.g'
100 } 114 }
101 115
102 -antigen-ensure-repo () { 116 -antigen-ensure-repo () {
103 117
104 local update=false 118 local update=false
105 if [[ $1 == --update ]]; then 119 if [[ $1 == --update ]]; then
106 update=true 120 update=true
107 shift 121 shift
108 fi 122 fi
109 123
110 local url="$1" 124 local url="$1"
111 local clone_dir="$(-antigen-get-clone-dir $url)" 125 local branch="$2"
126 local clone_dir="$(-antigen-get-clone-dir $url $branch)"
112 127
113 if [[ ! -d $clone_dir ]]; then 128 if [[ ! -d $clone_dir ]]; then
114 git clone "$url" "$clone_dir" 129 git clone "$url" "$clone_dir"
115 elif $update; then 130 elif $update; then
116 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" pull 131 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" pull
117 fi 132 fi
118 133
134 if [[ "$branch" != - ]]; then
135 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \
136 checkout "$branch"
137 fi
138
119 } 139 }
120 140
121 -antigen-load () { 141 -antigen-load () {
122 142
123 local url="$1" 143 local url="$1"
124 local location="$(-antigen-get-clone-dir "$url")/$2" 144 local loc="$2"
125 local btype="$3" 145 local btype="$3"
146 local branch="$4"
147
148 local location="$(-antigen-get-clone-dir "$url" "$branch")/$loc"
126 149
127 if [[ $btype == theme ]]; then 150 if [[ $btype == theme ]]; then
128 151
129 # Of course, if its a theme, the location would point to the script 152 # Of course, if its a theme, the location would point to the script
130 # file. 153 # file.
131 source "$location" 154 source "$location"
132 155
133 else 156 else
134 157
135 # Source the plugin script 158 # Source the plugin script
136 # FIXME: I don't know. Looks very very ugly. Needs a better 159 # FIXME: I don't know. Looks very very ugly. Needs a better
137 # implementation once tests are ready. 160 # implementation once tests are ready.
138 local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')" 161 local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')"
139 if [[ -f $script_loc ]]; then 162 if [[ -f $script_loc ]]; then
140 # If we have a `*.plugin.zsh`, source it. 163 # If we have a `*.plugin.zsh`, source it.
141 source "$script_loc" 164 source "$script_loc"
142 elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then 165 elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then
143 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` 166 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh`
144 # files. 167 # files.
145 for script ($location/*.zsh) source "$script" 168 for script ($location/*.zsh) source "$script"
146 elif [[ ! -z "$(ls "$location" | grep -m1 '.sh$')" ]]; then 169 elif [[ ! -z "$(ls "$location" | grep -m1 '.sh$')" ]]; then
147 # If there are no `*.zsh` files either, we look for and source any 170 # If there are no `*.zsh` files either, we look for and source any
148 # `*.sh` files instead. 171 # `*.sh` files instead.
149 for script ($location/*.sh) source "$script" 172 for script ($location/*.sh) source "$script"
150 fi 173 fi
151 174
152 # Add to $fpath, for completion(s) 175 # Add to $fpath, for completion(s)
153 fpath=($location $fpath) 176 fpath=($location $fpath)
154 177
155 fi 178 fi
156 179
157 } 180 }
158 181
159 antigen-cleanup () { 182 antigen-cleanup () {
160 183
161 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then 184 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then
162 echo "You don't have any bundles." 185 echo "You don't have any bundles."
163 return 0 186 return 0
164 fi 187 fi
165 188
166 # Find directores in ADOTDIR/repos, that are not in the bundles record. 189 # Find directores in ADOTDIR/repos, that are not in the bundles record.
167 local unused_clones="$(comm -13 \ 190 local unused_clones="$(comm -13 \
168 <(-antigen-echo-record | awk '{print $1}' | sort -u) \ 191 <(-antigen-echo-record | awk '{print $1 "|" $4}' | sort -u) \
169 <(ls "$ADOTDIR/repos" | while read line; do 192 <(ls "$ADOTDIR/repos" | while read line; do
170 -antigen-get-clone-url "$line" 193 -antigen-get-clone-url "$line"
171 done))" 194 done))"
172 195
173 if [[ -z $unused_clones ]]; then 196 if [[ -z $unused_clones ]]; then
174 echo "You don't have any unidentified bundles." 197 echo "You don't have any unidentified bundles."
175 return 0 198 return 0
176 fi 199 fi
177 200
178 echo 'You have clones for the following repos, but are not used.' 201 echo 'You have clones for the following repos, but are not used.'
179 echo "$unused_clones" | sed 's/^/ /' 202 echo "$unused_clones" \
203 | sed -e 's/^/ /' -e 's/|/, branch /'
180 204
181 echo -n '\nDelete them all? [y/N] ' 205 echo -n '\nDelete them all? [y/N] '
182 if read -q; then 206 if read -q; then
183 echo 207 echo
184 echo 208 echo
185 echo "$unused_clones" | while read url; do 209 echo "$unused_clones" | while read url; do
186 echo -n "Deleting clone for $url..." 210 echo -n "Deleting clone for $url..."
187 rm -rf "$(-antigen-get-clone-dir $url)" 211 rm -rf "$(-antigen-get-clone-dir $url)"
188 echo ' done.' 212 echo ' done.'
189 done 213 done
190 else 214 else
191 echo 215 echo
192 echo Nothing deleted. 216 echo Nothing deleted.
193 fi 217 fi
194 } 218 }
195 219
196 antigen-lib () { 220 antigen-lib () {
197 antigen-bundle --loc=lib 221 antigen-bundle --loc=lib
198 } 222 }
199 223
200 antigen-theme () { 224 antigen-theme () {
201 local name="${1:-robbyrussell}" 225 local name="${1:-robbyrussell}"
202 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme 226 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme
203 } 227 }
204 228
205 antigen-apply () { 229 antigen-apply () {
206 # Initialize completion. 230 # Initialize completion.
207 # TODO: Only load completions if there are any changes to the bundle 231 # TODO: Only load completions if there are any changes to the bundle
208 # repositories. 232 # repositories.
209 compinit -i 233 compinit -i
210 } 234 }
211 235
212 antigen-list () { 236 antigen-list () {
213 # List all currently installed bundles 237 # List all currently installed bundles
214 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then 238 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
215 echo "You don't have any bundles." >&2 239 echo "You don't have any bundles." >&2
216 return 1 240 return 1
217 else 241 else
218 -antigen-echo-record 242 -antigen-echo-record
219 fi 243 fi
220 } 244 }
221 245
222 # Echo the bundle specs as in the record. The first line is not echoed since it 246 # Echo the bundle specs as in the record. The first line is not echoed since it
223 # is a blank line. 247 # is a blank line.
224 -antigen-echo-record () { 248 -antigen-echo-record () {
225 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' 249 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
226 } 250 }
227 251
228 -antigen-env-setup () { 252 -antigen-env-setup () {
229 # Pre-startup initializations 253 # Pre-startup initializations
230 -set-default ANTIGEN_DEFAULT_REPO_URL \ 254 -set-default ANTIGEN_DEFAULT_REPO_URL \
231 https://github.com/robbyrussell/oh-my-zsh.git 255 https://github.com/robbyrussell/oh-my-zsh.git
232 -set-default ADOTDIR $HOME/.antigen 256 -set-default ADOTDIR $HOME/.antigen
233 257
234 # Load the compinit module 258 # Load the compinit module
235 autoload -U compinit 259 autoload -U compinit
236 260
237 # Without the following, `compdef` function is not defined. 261 # Without the following, `compdef` function is not defined.
238 compinit -i 262 compinit -i
239 } 263 }
240 264
241 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is 265 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
242 # not already set. 266 # not already set.
243 -set-default () { 267 -set-default () {
244 local arg_name="$1" 268 local arg_name="$1"
245 local arg_value="$2" 269 local arg_value="$2"
246 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 270 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
247 } 271 }
248 272
249 -antigen-env-setup 273 -antigen-env-setup
250 274