| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. | 1 // Copyright 2016 The LUCI Authors. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 } | 153 } |
| 154 nextPath := subPath + "&s=" + resp.Next | 154 nextPath := subPath + "&s=" + resp.Next |
| 155 resp = &logResponse{} | 155 resp = &logResponse{} |
| 156 if err := c.get(ctx, repoURL, nextPath, resp); err != nil { | 156 if err := c.get(ctx, repoURL, nextPath, resp); err != nil { |
| 157 return nil, err | 157 return nil, err |
| 158 } | 158 } |
| 159 result = append(result, resp.Log...) | 159 result = append(result, resp.Log...) |
| 160 } | 160 } |
| 161 } | 161 } |
| 162 | 162 |
| 163 // Refs returns a map resolving each ref in a repo to git revision. |
| 164 // |
| 165 // refsPath limits which refs to resolve to only those matching {refsPath}/*. |
| 166 // refsPath should start with "refs" and should not include glob '*'. |
| 167 // Typically, "refs/heads" should be used. |
| 168 // |
| 169 // To fetch **all** refs in a repo, specify just "refs" but beware of two |
| 170 // caveats: |
| 171 // * refs returned include a ref for each patchset for each Gerrit change |
| 172 // associated with the repo |
| 173 // * returned map will contain special "HEAD" ref whose value in resulting map |
| 174 // will be name of the actual ref to which "HEAD" points, which is typically |
| 175 // "refs/heads/master". |
| 176 // |
| 177 // Thus, if you are looking for all tags and all branches of repo, it's |
| 178 // recommended to issue two Refs calls limited to "refs/tags" and "refs/heads" |
| 179 // instead of one call for "refs". |
| 180 // |
| 181 // Since Gerrit allows per-ref ACLs, it is possible that some refs matching |
| 182 // refPrefix would not be present in results because current user isn't granted |
| 183 // read permission on them. |
| 184 func (c *Client) Refs(ctx context.Context, repoURL, refsPath string) (map[string
]string, error) { |
| 185 repoURL, err := NormalizeRepoURL(repoURL) |
| 186 if err != nil { |
| 187 return nil, err |
| 188 } |
| 189 if refsPath != "refs" && !strings.HasPrefix(refsPath, "refs/") { |
| 190 return nil, fmt.Errorf("refsPath must start with \"refs\": %q",
refsPath) |
| 191 } |
| 192 refsPath = strings.TrimRight(refsPath, "/") |
| 193 |
| 194 subPath := fmt.Sprintf("+%s?format=json", url.PathEscape(refsPath)) |
| 195 resp := refsResponse{} |
| 196 if err := c.get(ctx, repoURL, subPath, &resp); err != nil { |
| 197 return nil, err |
| 198 } |
| 199 r := make(map[string]string, len(resp)) |
| 200 for ref, v := range resp { |
| 201 switch { |
| 202 case v.Value == "": |
| 203 // Weird case of what looks like hash with a target in a
t least Chromium |
| 204 // repo. |
| 205 continue |
| 206 case ref == "HEAD": |
| 207 r["HEAD"] = v.Target |
| 208 case refsPath != "refs": |
| 209 // Gitiles omits refsPath from each ref if refsPath != "
refs". Undo this |
| 210 // inconsistency. |
| 211 r[refsPath+"/"+ref] = v.Value |
| 212 default: |
| 213 r[ref] = v.Value |
| 214 } |
| 215 } |
| 216 return r, nil |
| 217 } |
| 218 |
| 163 //////////////////////////////////////////////////////////////////////////////// | 219 //////////////////////////////////////////////////////////////////////////////// |
| 164 | 220 |
| 221 type refsResponseRefInfo struct { |
| 222 Value string `json:"value"` |
| 223 Target string `json:"target"` |
| 224 } |
| 225 |
| 226 // refsResponse is the JSON response from querying gitiles for a Refs request. |
| 227 type refsResponse map[string]refsResponseRefInfo |
| 228 |
| 165 // logResponse is the JSON response from querying gitiles for a log request. | 229 // logResponse is the JSON response from querying gitiles for a log request. |
| 166 type logResponse struct { | 230 type logResponse struct { |
| 167 Log []Commit `json:"log"` | 231 Log []Commit `json:"log"` |
| 168 Next string `json:"next"` | 232 Next string `json:"next"` |
| 169 } | 233 } |
| 170 | 234 |
| 171 func (c *Client) get(ctx context.Context, repoURL, subPath string, result interf
ace{}) error { | 235 func (c *Client) get(ctx context.Context, repoURL, subPath string, result interf
ace{}) error { |
| 172 URL := fmt.Sprintf("%s/%s", repoURL, subPath) | 236 URL := fmt.Sprintf("%s/%s", repoURL, subPath) |
| 173 if c.mockRepoURL != "" { | 237 if c.mockRepoURL != "" { |
| 174 URL = fmt.Sprintf("%s/%s", c.mockRepoURL, subPath) | 238 URL = fmt.Sprintf("%s/%s", c.mockRepoURL, subPath) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 193 return errors.Annotate(err, "unexpected response from Gitiles").
Err() | 257 return errors.Annotate(err, "unexpected response from Gitiles").
Err() |
| 194 } | 258 } |
| 195 if cnt != 4 || ")]}'" != string(trash) { | 259 if cnt != 4 || ")]}'" != string(trash) { |
| 196 return errors.New("unexpected response from Gitiles") | 260 return errors.New("unexpected response from Gitiles") |
| 197 } | 261 } |
| 198 if err = json.NewDecoder(r.Body).Decode(result); err != nil { | 262 if err = json.NewDecoder(r.Body).Decode(result); err != nil { |
| 199 return errors.Annotate(err, "failed to decode Gitiles response i
nto %T", result).Err() | 263 return errors.Annotate(err, "failed to decode Gitiles response i
nto %T", result).Err() |
| 200 } | 264 } |
| 201 return nil | 265 return nil |
| 202 } | 266 } |
| OLD | NEW |