GetFile: Use "ref" in-query if posible #491

Merged
6543 merged 9 commits from 6543/go-sdk:GetFile_use-ref_in-query_if-posible into master 2021-02-16 20:07:45 +00:00
Showing only changes of commit 0b46b13343 - Show all commits

View File

@ -116,9 +116,12 @@ type FileDeleteResponse struct {
}
// GetFile downloads a file of repository, ref can be branch/tag/commit.
// e.g.: ref -> master, tree -> macaron.go(no leading slash)
func (c *Client) GetFile(user, repo, ref, tree string) ([]byte, *Response, error) {
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/raw/%s/%s", user, repo, ref, tree), nil, nil)
// e.g.: ref -> master, tree -> README.md (no leading slash)
func (c *Client) GetFile(owner, repo, ref, tree string) ([]byte, *Response, error) {
if c.checkServerVersionGreaterThanOrEqual(version1_14_0) != nil {
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/raw/%s/%s", owner, repo, ref, tree), nil, nil)
}
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/raw/%s?ref=%s", owner, repo, tree, ref), nil, nil)
6543 marked this conversation as resolved Outdated

tree and ref should both be escaped properly.

ref is simple as it's url.QueryEscape but tree needs escaping using the partial pathescape.

tree and ref should both be escaped properly. ref is simple as it's url.QueryEscape but tree needs escaping using the partial pathescape.
}
6543 marked this conversation as resolved Outdated

tree needs to be util.PathEscapeSegments or something like it.

e.g. from Gitea modules/util

// PathEscapeSegments escapes segments of a path while not escaping forward slash
func PathEscapeSegments(path string) string {
	slice := strings.Split(path, "/")
	for index := range slice {
		slice[index] = url.PathEscape(slice[index])
	}
	escapedPath := strings.Join(slice, "/")
	return escapedPath
}

as does ref and tree on line 123

tree needs to be `util.PathEscapeSegments` or something like it. e.g. from Gitea modules/util ``` // PathEscapeSegments escapes segments of a path while not escaping forward slash func PathEscapeSegments(path string) string { slice := strings.Split(path, "/") for index := range slice { slice[index] = url.PathEscape(slice[index]) } escapedPath := strings.Join(slice, "/") return escapedPath } ``` as does ref and tree on line 123
Outdated
Review

this is a more genneral issue witch I like to catch with #273 since we need this not only for this function...

this is a more genneral issue witch I like to catch with #273 since we need this not only for this function...
// GetContents get the metadata and contents of a file in a repository