Skip to content

Commit

Permalink
blob: get blob and check image by index (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon authored Mar 30, 2020
1 parent c80dc60 commit 40097be
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 6 deletions.
14 changes: 11 additions & 3 deletions commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ func (w *limitWriter) Write(p []byte) (int, error) {
return len(p), err
}

// IsImageFile returns true if the commit is an image blob.
func (c *Commit) IsImageFile(subpath string) (bool, error) {
blob, err := c.Blob(subpath)
func (c *Commit) isImageFile(blob *Blob, err error) (bool, error) {
if err != nil {
if err == ErrNotBlob {
return false, nil
Expand All @@ -162,3 +160,13 @@ func (c *Commit) IsImageFile(subpath string) (bool, error) {

return strings.Contains(http.DetectContentType(buf.Bytes()), "image/"), nil
}

// IsImageFile returns true if the blob of the commit is an image by subpath.
func (c *Commit) IsImageFile(subpath string) (bool, error) {
return c.isImageFile(c.Blob(subpath))
}

// IsImageFileByIndex returns true if the blob of the commit is an image by index.
func (c *Commit) IsImageFileByIndex(index string) (bool, error) {
return c.isImageFile(c.BlobByIndex(index))
}
47 changes: 47 additions & 0 deletions commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,50 @@ func TestCommit_IsImageFile(t *testing.T) {
})
}
}

func TestCommit_IsImageFileByIndex(t *testing.T) {
t.Run("not a blob", func(t *testing.T) {
c, err := testrepo.CatFileCommit("4e59b72440188e7c2578299fc28ea425fbe9aece")
if err != nil {
t.Fatal(err)
}

isImage, err := c.IsImageFileByIndex("fcf7087e732bfe3c25328248a9bf8c3ccd85bed4") // "gogs"
if err != nil {
t.Fatal(err)
}
assert.False(t, isImage)
})

tests := []struct {
id string
index string
expVal bool
}{
{
id: "4eaa8d4b05e731e950e2eaf9e8b92f522303ab41",
index: "adfd6da3c0a3fb038393144becbf37f14f780087", // "README.txt"
expVal: false,
},
{
id: "4eaa8d4b05e731e950e2eaf9e8b92f522303ab41",
index: "2ce918888b0fdd4736767360fc5e3e83daf47fce", // "img/sourcegraph.png"
expVal: true,
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
c, err := testrepo.CatFileCommit(test.id)
if err != nil {
t.Fatal(err)
}

isImage, err := c.IsImageFileByIndex(test.index)
if err != nil {
t.Fatal(err)
}

assert.Equal(t, test.expVal, isImage)
})
}
}
23 changes: 23 additions & 0 deletions repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,29 @@ func (r *Repository) CatFileCommit(rev string, opts ...CatFileCommitOptions) (*C
return c, nil
}

// CatFileTypeOptions contains optional arguments for showing the object type.
// Docs: https://git-scm.com/docs/git-cat-file#Documentation/git-cat-file.txt--t
type CatFileTypeOptions struct {
// The timeout duration before giving up for each shell command execution.
// The default timeout duration will be used when not supplied.
Timeout time.Duration
}

// CatFileType returns the object type of given revision of the repository.
func (r *Repository) CatFileType(rev string, opts ...CatFileTypeOptions) (ObjectType, error) {
var opt CatFileTypeOptions
if len(opts) > 0 {
opt = opts[0]
}

typ, err := NewCommand("cat-file", "-t", rev).RunInDirWithTimeout(opt.Timeout, r.path)
if err != nil {
return "", err
}
typ = bytes.TrimSpace(typ)
return ObjectType(typ), nil
}

// BranchCommit returns the latest commit of given branch of the repository.
// The branch must be given in short name e.g. "master".
func (r *Repository) BranchCommit(branch string, opts ...CatFileCommitOptions) (*Commit, error) {
Expand Down
5 changes: 2 additions & 3 deletions repo_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,13 @@ func (r *Repository) getTag(timeout time.Duration, id *SHA1) (*Tag, error) {
}

// Check tag type
typ, err := NewCommand("cat-file", "-t", id.String()).RunInDirWithTimeout(timeout, r.path)
typ, err := r.CatFileType(id.String(), CatFileTypeOptions{Timeout: timeout})
if err != nil {
return nil, err
}
typ = bytes.TrimSpace(typ)

var tag *Tag
switch ObjectType(typ) {
switch typ {
case ObjectCommit: // Tag is a commit
tag = &Tag{
typ: ObjectCommit,
Expand Down
26 changes: 26 additions & 0 deletions tree_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,29 @@ func (t *Tree) Blob(subpath string, opts ...LsTreeOptions) (*Blob, error) {

return nil, ErrNotBlob
}

// BlobByIndex returns blob object by given index.
func (t *Tree) BlobByIndex(index string) (*Blob, error) {
typ, err := t.repo.CatFileType(index)
if err != nil {
return nil, err
}

if typ != ObjectBlob {
return nil, ErrNotBlob
}

id, err := t.repo.RevParse(index)
if err != nil {
return nil, err
}

return &Blob{
TreeEntry: &TreeEntry{
mode: EntryBlob,
typ: ObjectBlob,
id: MustIDFromString(id),
parent: t,
},
}, nil
}

0 comments on commit 40097be

Please sign in to comment.