Commit 8d0d219d6f179def61158166d83e22db4770a46b

Authored by Shrikant Sharat
1 parent e7bf36002c

Fix #9 Update command created extra clone.

The update command is not aware of the --branch functionality and is not updated
to behave well in its precense. Because of this, it creates a clone to a
incorrect directory instead of updating the existing clone directory.

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