File tree 2 files changed +82
-0
lines changed
2 files changed +82
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Copyright 2022 The Gogs Authors. All rights reserved.
2
+ // Use of this source code is governed by a MIT-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package git
6
+
7
+ import "time"
8
+
9
+ // CatFileBlobOptions contains optional arguments for verifying the objects.
10
+ //
11
+ // Docs: https://git-scm.com/docs/git-cat-file#Documentation/git-cat-file.txt
12
+ type CatFileBlobOptions struct {
13
+ // The timeout duration before giving up for each shell command execution.
14
+ // The default timeout duration will be used when not supplied.
15
+ Timeout time.Duration
16
+ // The additional options to be passed to the underlying git.
17
+ CommandOptions
18
+ }
19
+
20
+ // CatFileBlob returns the blob corresponding to the given revision of the repository.
21
+ func (r * Repository ) CatFileBlob (rev string , opts ... CatFileBlobOptions ) (* Blob , error ) {
22
+ var opt CatFileBlobOptions
23
+ if len (opts ) > 0 {
24
+ opt = opts [0 ]
25
+ }
26
+
27
+ rev , err := r .RevParse (rev , RevParseOptions {Timeout : opt .Timeout }) //nolint
28
+ if err != nil {
29
+ return nil , err
30
+ }
31
+
32
+ typ , err := r .CatFileType (rev )
33
+ if err != nil {
34
+ return nil , err
35
+ }
36
+
37
+ if typ != ObjectBlob {
38
+ return nil , ErrNotBlob
39
+ }
40
+
41
+ return & Blob {
42
+ TreeEntry : & TreeEntry {
43
+ mode : EntryBlob ,
44
+ typ : ObjectBlob ,
45
+ id : MustIDFromString (rev ),
46
+ parent : & Tree {
47
+ repo : r ,
48
+ },
49
+ },
50
+ }, nil
51
+ }
Original file line number Diff line number Diff line change
1
+ // Copyright 2022 The Gogs Authors. All rights reserved.
2
+ // Use of this source code is governed by a MIT-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package git
6
+
7
+ import (
8
+ "testing"
9
+
10
+ "github.com/stretchr/testify/assert"
11
+ "github.com/stretchr/testify/require"
12
+ )
13
+
14
+ func TestRepository_CatFileBlob (t * testing.T ) {
15
+ t .Run ("not a blob" , func (t * testing.T ) {
16
+ _ , err := testrepo .CatFileBlob ("007cb92318c7bd3b56908ea8c2e54370245562f8" )
17
+ assert .Equal (t , ErrNotBlob , err )
18
+ })
19
+
20
+ t .Run ("get a blob, no full rev hash" , func (t * testing.T ) {
21
+ b , err := testrepo .CatFileBlob ("021a" )
22
+ require .NoError (t , err )
23
+ assert .True (t , b .IsBlob ())
24
+ })
25
+
26
+ t .Run ("get a blob" , func (t * testing.T ) {
27
+ b , err := testrepo .CatFileBlob ("021a721a61a1de65865542c405796d1eb985f784" )
28
+ require .NoError (t , err )
29
+ assert .True (t , b .IsBlob ())
30
+ })
31
+ }
You can’t perform that action at this time.
0 commit comments