From 53aa96b7a80fd22ed05b98f297a998e88ae9c056 Mon Sep 17 00:00:00 2001 From: jonatan Date: Sat, 11 Jun 2022 14:39:12 +0300 Subject: [PATCH 1/8] feat(repo_blob): add CatFileBlob --- repo_blob.go | 28 ++++++++++++++++++++++++++++ repo_blob_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 repo_blob.go create mode 100644 repo_blob_test.go diff --git a/repo_blob.go b/repo_blob.go new file mode 100644 index 00000000..c325e51b --- /dev/null +++ b/repo_blob.go @@ -0,0 +1,28 @@ +// Copyright 2015 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package git + +// CatFileBlob returns the blob corresponding to the given revision of the repository. +func (repo *Repository) CatFileBlob(rev string) (*Blob, error) { + typ, err := repo.CatFileType(rev) + if err != nil { + return nil, err + } + + if typ != ObjectBlob { + return nil, ErrNotBlob + } + + return &Blob{ + TreeEntry: &TreeEntry{ + mode: EntryBlob, + typ: ObjectBlob, + id: MustIDFromString(rev), + parent: &Tree{ + repo: repo, + }, + }, + }, nil +} diff --git a/repo_blob_test.go b/repo_blob_test.go new file mode 100644 index 00000000..85f72a05 --- /dev/null +++ b/repo_blob_test.go @@ -0,0 +1,27 @@ +// Copyright 2020 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package git + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRepository_CatFileBlob(t *testing.T) { + t.Run("not a blob", func(t *testing.T) { + _, err := testrepo.CatFileBlob("007cb92318c7bd3b56908ea8c2e54370245562f8") + assert.Equal(t, ErrNotBlob, err) + }) + + t.Run("get a blob", func(t *testing.T) { + b, err := testrepo.CatFileBlob("021a721a61a1de65865542c405796d1eb985f784") + if err != nil { + t.Fatal(err) + } + + assert.True(t, b.IsBlob()) + }) +} From a3f4204fd86b5751824f52b0ab822b8030181259 Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Tue, 21 Jun 2022 07:13:31 +0800 Subject: [PATCH 2/8] Apply suggestions from code review --- repo_blob.go | 2 +- repo_blob_test.go | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/repo_blob.go b/repo_blob.go index c325e51b..dbc45bb1 100644 --- a/repo_blob.go +++ b/repo_blob.go @@ -1,4 +1,4 @@ -// Copyright 2015 The Gogs Authors. All rights reserved. +// Copyright 2022 The Gogs Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. diff --git a/repo_blob_test.go b/repo_blob_test.go index 85f72a05..58b00c85 100644 --- a/repo_blob_test.go +++ b/repo_blob_test.go @@ -1,4 +1,4 @@ -// Copyright 2020 The Gogs Authors. All rights reserved. +// Copyright 2022 The Gogs Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -18,9 +18,7 @@ func TestRepository_CatFileBlob(t *testing.T) { t.Run("get a blob", func(t *testing.T) { b, err := testrepo.CatFileBlob("021a721a61a1de65865542c405796d1eb985f784") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) assert.True(t, b.IsBlob()) }) From a1555ced9c8b80ada3b9dbd19aca764ee0804928 Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Tue, 21 Jun 2022 07:14:31 +0800 Subject: [PATCH 3/8] Apply suggestions from code review --- repo_blob_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repo_blob_test.go b/repo_blob_test.go index 58b00c85..5557fee3 100644 --- a/repo_blob_test.go +++ b/repo_blob_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestRepository_CatFileBlob(t *testing.T) { @@ -19,7 +20,6 @@ func TestRepository_CatFileBlob(t *testing.T) { t.Run("get a blob", func(t *testing.T) { b, err := testrepo.CatFileBlob("021a721a61a1de65865542c405796d1eb985f784") require.NoError(t, err) - assert.True(t, b.IsBlob()) }) } From 32dea589da07687f6bcee5273de05493dadcade0 Mon Sep 17 00:00:00 2001 From: Yehonatan Ezron <37303618+jonatan5524@users.noreply.github.com> Date: Thu, 23 Jun 2022 13:00:01 +0300 Subject: [PATCH 4/8] add revParse after code review --- repo_blob.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/repo_blob.go b/repo_blob.go index dbc45bb1..ee19058e 100644 --- a/repo_blob.go +++ b/repo_blob.go @@ -6,7 +6,12 @@ package git // CatFileBlob returns the blob corresponding to the given revision of the repository. func (repo *Repository) CatFileBlob(rev string) (*Blob, error) { - typ, err := repo.CatFileType(rev) + revHash, err := r.RevParse(rev, RevParseOptions{Timeout: opt.Timeout}) //nolint + if err != nil { + return nil, err + } + + typ, err := repo.CatFileType(revHash) if err != nil { return nil, err } From 03c0c2bacba83f60047e58fdb8228eef805be816 Mon Sep 17 00:00:00 2001 From: Yehonatan Ezron <37303618+jonatan5524@users.noreply.github.com> Date: Thu, 23 Jun 2022 13:34:37 +0300 Subject: [PATCH 5/8] add test for not full rev after CR --- repo_blob.go | 2 +- repo_blob_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/repo_blob.go b/repo_blob.go index ee19058e..2e49a295 100644 --- a/repo_blob.go +++ b/repo_blob.go @@ -6,7 +6,7 @@ package git // CatFileBlob returns the blob corresponding to the given revision of the repository. func (repo *Repository) CatFileBlob(rev string) (*Blob, error) { - revHash, err := r.RevParse(rev, RevParseOptions{Timeout: opt.Timeout}) //nolint + rev, err := r.RevParse(rev, RevParseOptions{Timeout: opt.Timeout}) //nolint if err != nil { return nil, err } diff --git a/repo_blob_test.go b/repo_blob_test.go index 5557fee3..7def8039 100644 --- a/repo_blob_test.go +++ b/repo_blob_test.go @@ -17,6 +17,12 @@ func TestRepository_CatFileBlob(t *testing.T) { assert.Equal(t, ErrNotBlob, err) }) + t.Run("get a blob, no full rev hash", func(t *testing.T) { + b, err := testrepo.CatFileBlob("021a") + require.NoError(t, err) + assert.True(t, b.IsBlob()) + }) + t.Run("get a blob", func(t *testing.T) { b, err := testrepo.CatFileBlob("021a721a61a1de65865542c405796d1eb985f784") require.NoError(t, err) From bee5c6fb3ef97a1112a77044a894e521a6dd5908 Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Thu, 23 Jun 2022 20:18:57 +0800 Subject: [PATCH 6/8] Update repo_blob.go --- repo_blob.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repo_blob.go b/repo_blob.go index 2e49a295..1c422ff2 100644 --- a/repo_blob.go +++ b/repo_blob.go @@ -11,7 +11,7 @@ func (repo *Repository) CatFileBlob(rev string) (*Blob, error) { return nil, err } - typ, err := repo.CatFileType(revHash) + typ, err := repo.CatFileType(rev) if err != nil { return nil, err } From aa263018f77f9298388126b341d785b9e9dc7f89 Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Thu, 23 Jun 2022 20:21:23 +0800 Subject: [PATCH 7/8] Apply suggestions from code review --- repo_blob.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/repo_blob.go b/repo_blob.go index 1c422ff2..c9238010 100644 --- a/repo_blob.go +++ b/repo_blob.go @@ -5,13 +5,13 @@ package git // CatFileBlob returns the blob corresponding to the given revision of the repository. -func (repo *Repository) CatFileBlob(rev string) (*Blob, error) { +func (r *Repository) CatFileBlob(rev string) (*Blob, error) { rev, err := r.RevParse(rev, RevParseOptions{Timeout: opt.Timeout}) //nolint if err != nil { return nil, err } - typ, err := repo.CatFileType(rev) + typ, err := r.CatFileType(rev) if err != nil { return nil, err } @@ -26,7 +26,7 @@ func (repo *Repository) CatFileBlob(rev string) (*Blob, error) { typ: ObjectBlob, id: MustIDFromString(rev), parent: &Tree{ - repo: repo, + repo: r, }, }, }, nil From 03c927cf5d7e47e1aad1d8107e6d0fc5fc30ab88 Mon Sep 17 00:00:00 2001 From: jonatan Date: Fri, 24 Jun 2022 13:07:42 +0300 Subject: [PATCH 8/8] fix compiler errors --- repo_blob.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/repo_blob.go b/repo_blob.go index c9238010..e9f04c0c 100644 --- a/repo_blob.go +++ b/repo_blob.go @@ -4,8 +4,26 @@ package git +import "time" + +// CatFileBlobOptions contains optional arguments for verifying the objects. +// +// Docs: https://git-scm.com/docs/git-cat-file#Documentation/git-cat-file.txt +type CatFileBlobOptions 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 + // The additional options to be passed to the underlying git. + CommandOptions +} + // CatFileBlob returns the blob corresponding to the given revision of the repository. -func (r *Repository) CatFileBlob(rev string) (*Blob, error) { +func (r *Repository) CatFileBlob(rev string, opts ...CatFileBlobOptions) (*Blob, error) { + var opt CatFileBlobOptions + if len(opts) > 0 { + opt = opts[0] + } + rev, err := r.RevParse(rev, RevParseOptions{Timeout: opt.Timeout}) //nolint if err != nil { return nil, err