2019-04-25 13:06:53 -04:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Reference represents a Git reference.
|
|
|
|
type Reference struct {
|
|
|
|
Ref string `json:"ref"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Object *GitObject `json:"object"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GitObject represents a Git object.
|
|
|
|
type GitObject struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
SHA string `json:"sha"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepoRef get one ref's information of one repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetRepoRef(user, repo, ref string) (*Reference, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
ref = strings.TrimPrefix(ref, "refs/")
|
|
|
|
r := new(Reference)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/git/refs/%s", user, repo, ref), nil, nil, &r)
|
2019-04-25 13:06:53 -04:00
|
|
|
if _, ok := err.(*json.UnmarshalTypeError); ok {
|
|
|
|
// Multiple refs
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, resp, errors.New("no exact match found for this ref")
|
2019-04-25 13:06:53 -04:00
|
|
|
} else if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2020-09-15 22:01:41 -04:00
|
|
|
return r, resp, nil
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepoRefs get list of ref's information of one repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetRepoRefs(user, repo, ref string) ([]*Reference, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
ref = strings.TrimPrefix(ref, "refs/")
|
2020-09-15 22:01:41 -04:00
|
|
|
data, resp, err := c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/git/refs/%s", user, repo, ref), nil, nil)
|
2019-04-25 13:06:53 -04:00
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to unmarshal single returned ref.
|
|
|
|
r := new(Reference)
|
2020-09-15 22:01:41 -04:00
|
|
|
refErr := json.Unmarshal(data, r)
|
2019-04-25 13:06:53 -04:00
|
|
|
if refErr == nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return []*Reference{r}, resp, nil
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to unmarshal multiple refs.
|
|
|
|
var rs []*Reference
|
2020-09-15 22:01:41 -04:00
|
|
|
refsErr := json.Unmarshal(data, &rs)
|
2019-04-25 13:06:53 -04:00
|
|
|
if refsErr == nil {
|
|
|
|
if len(rs) == 0 {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, resp, errors.New("unexpected response: an array of refs with length 0")
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
return rs, resp, nil
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, resp, fmt.Errorf("unmarshalling failed for both single and multiple refs: %s and %s", refErr, refsErr)
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|