s
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package gitcmd
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Blob struct {
|
||||
Ref string `json:"ref"`
|
||||
Path string `json:"path"`
|
||||
Content string `json:"content"`
|
||||
Encoding string `json:"encoding"`
|
||||
Size int64 `json:"size"`
|
||||
SHA string `json:"sha"`
|
||||
IsBinary bool `json:"is_binary"`
|
||||
}
|
||||
|
||||
func (r *Repository) GetBlob(ref, path string) (*Blob, error) {
|
||||
|
||||
shaOut, _, err := NewCommand("rev-parse").
|
||||
AddDynamicArguments(ref + ":" + path).
|
||||
WithDir(r.Path).
|
||||
RunStdString(r.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sha := strings.TrimSpace(shaOut)
|
||||
|
||||
content, _, err := NewCommand("show").
|
||||
AddDynamicArguments(ref + ":" + path).
|
||||
WithDir(r.Path).
|
||||
RunStdString(r.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
isBinary := strings.ContainsRune(content, 0)
|
||||
encoding := "text"
|
||||
if isBinary {
|
||||
encoding = "base64"
|
||||
}
|
||||
|
||||
return &Blob{
|
||||
Ref: ref,
|
||||
Path: path,
|
||||
Content: content,
|
||||
Encoding: encoding,
|
||||
Size: int64(len(content)),
|
||||
SHA: sha,
|
||||
IsBinary: isBinary,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *Repository) GetRawContent(ref, path string) (string, error) {
|
||||
out, _, err := NewCommand("show").
|
||||
AddDynamicArguments(ref + ":" + path).
|
||||
WithDir(r.Path).
|
||||
RunStdString(r.ctx)
|
||||
return out, err
|
||||
}
|
||||
Reference in New Issue
Block a user