GetFile: Use "ref" in-query if posible #491
|
@ -9,6 +9,7 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -121,7 +122,7 @@ func (c *Client) GetFile(owner, repo, ref, tree string) ([]byte, *Response, erro
|
|||
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)
|
||||
}
|
||||
6543 marked this conversation as resolved
Outdated
|
||||
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/raw/%s?ref=%s", owner, repo, tree, ref), nil, nil)
|
||||
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/raw/%s?ref=%s", owner, repo, tree, url.QueryEscape(ref)), nil, nil)
|
||||
6543 marked this conversation as resolved
Outdated
zeripath
commented
tree needs to be e.g. from Gitea modules/util
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
6543
commented
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
|
||||
|
|
|
@ -59,7 +59,7 @@ func TestFileCreateUpdateGet(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
_, resp, err := c.GetFile(repo.Owner.UserName, repo.Name, "master", "A")
|
||||
assert.EqualValues(t, "404 Not Found", err.Error())
|
||||
assert.EqualValues(t, 404, resp.StatusCode )
|
||||
assert.EqualValues(t, 404, resp.StatusCode)
|
||||
|
||||
licence, _, err := c.GetContents(repo.Owner.UserName, repo.Name, "", "LICENSE")
|
||||
assert.NoError(t, err)
|
||||
|
@ -68,14 +68,14 @@ func TestFileCreateUpdateGet(t *testing.T) {
|
|||
FileOptions: FileOptions{
|
||||
Message: "Overwrite",
|
||||
BranchName: "master",
|
||||
NewBranchName: "overwrite_licence",
|
||||
NewBranchName: "overwrite/licence",
|
||||
},
|
||||
SHA: licence.SHA,
|
||||
Content: "Tk9USElORyBJUyBIRVJFIEFOWU1PUkUKSUYgWU9VIExJS0UgVE8gRklORCBTT01FVEhJTkcKV0FJVCBGT1IgVEhFIEZVVFVSRQo=",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, updatedFile)
|
||||
licenceRawNew, _, err := c.GetFile(repo.Owner.UserName, repo.Name, "overwrite_licence", "LICENSE")
|
||||
licenceRawNew, _, err := c.GetFile(repo.Owner.UserName, repo.Name, "overwrite/licence", "LICENSE")
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, licence)
|
||||
assert.False(t, bytes.Equal(licenceRaw, licenceRawNew))
|
||||
|
|
Loading…
Reference in New Issue
Block a user
tree and ref should both be escaped properly.
ref is simple as it's url.QueryEscape but tree needs escaping using the partial pathescape.