Skip to content

Raw GetBlob() support #658

@obno

Description

@obno

Current implementation of Git.GetBlob() API only supports the JSON media type. This can be inconvenient for vey large git blobs since they are stored completely in memory in the corresponding github.Blob struct.

The Github GET /repos/:owner/:repo/git/blobs/:sha API also allows for blobs to be fetched in raw rather than json format using the application/vnd.github.VERSION.raw media type. This gets around the above limitation for very large blobs.

You can abuse the current API like below but it would be nice to have this feature built into the API.

func DownloadFromGithub(accessToken, owner, repo, sha, toFileName string) error{
	//create github client
	httpClient := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{
		AccessToken: accessToken,
	}))
	ghClient := github.NewClient(httpClient)

	out, err := os.Create(toFileName)
	if err != nil  {
		return err
	}
	defer out.Close()

	//download the git blob as raw data
	u := fmt.Sprintf("repos/%v/%v/git/blobs/%v", owner, repo, sha)
	req, err := ghClient.NewRequest("GET", u, nil)
	if err != nil{
		return err
	}
	req.Header.Del("Accept")
	req.Header.Set("Accept", "application/vnd.github.VERSION.raw")
	req.Header.Del("Content-Type")
	_, err = ghClient.Do(context.Background(), req, out)
	if err != nil{
		return err
	}
	return nil

}

Maybe provide something like this?

func (s *GitService) GetRawBlob(ctx context.Context, owner string, repo string, sha string, to io.Writer) (*Response, error)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions