Debian Codegraph @

plumbing/object/file.go

raw
1 package object
2
3 import (
4 "bytes"
5 "io"
6 "strings"
7
8 "github.com/go-git/go-git/v5/plumbing/filemode"
9 "github.com/go-git/go-git/v5/plumbing/storer"
10 "github.com/go-git/go-git/v5/utils/binary"
11 "github.com/go-git/go-git/v5/utils/ioutil"
12 )
13
14 // File represents git file objects.
15 type File struct {
16 // Name is the path of the file. It might be relative to a tree,
17 // depending of the function that generates it.
18 Name string
19 // Mode is the file mode.
20 Mode filemode.FileMode
21 // Blob with the contents of the file.
22 Blob
23 }
24
25 // NewFile returns a File based on the given blob object
26 func NewFile(name string, m filemode.FileMode, b *Blob) *File {
27 return &File{Name: name, Mode: m, Blob: *b}
28 }
29
30 // Contents returns the contents of a file as a string.
31 func (f *File) Contents() (content string, err error) {
32 reader, err := f.Reader()
33 if err != nil {
34 return "", err
35 }
36 defer ioutil.CheckClose(reader, &err)
37
38 buf := new(bytes.Buffer)
39 if _, err := buf.ReadFrom(reader); err != nil {
40 return "", err
41 }
42
43 return buf.String(), nil
44 }
45
46 // IsBinary returns if the file is binary or not
47 func (f *File) IsBinary() (bin bool, err error) {
48 reader, err := f.Reader()
49 if err != nil {
50 return false, err
51 }
52 defer ioutil.CheckClose(reader, &err)
53
54 return binary.IsBinary(reader)
55 }
56
57 // Lines returns a slice of lines from the contents of a file, stripping
58 // all end of line characters. If the last line is empty (does not end
59 // in an end of line), it is also stripped.
60 func (f *File) Lines() ([]string, error) {
61 content, err := f.Contents()
62 if err != nil {
63 return nil, err
64 }
65
66 splits := strings.Split(content, "\n")
67 // remove the last line if it is empty
68 if splits[len(splits)-1] == "" {
69 return splits[:len(splits)-1], nil
70 }
71
72 return splits, nil
73 }
74
75 // FileIter provides an iterator for the files in a tree.
76 type FileIter struct {
77 s storer.EncodedObjectStorer
78 w TreeWalker
79 }
80
81 // NewFileIter takes a storer.EncodedObjectStorer and a Tree and returns a
82 // *FileIter that iterates over all files contained in the tree, recursively.
83 func NewFileIter(s storer.EncodedObjectStorer, t *Tree) *FileIter {
84 return &FileIter{s: s, w: *NewTreeWalker(t, true, nil)}
85 }
86
87 // Next moves the iterator to the next file and returns a pointer to it. If
88 // there are no more files, it returns io.EOF.
89 func (iter *FileIter) Next() (*File, error) {
90 for {
91 name, entry, err := iter.w.Next()
92 if err != nil {
93 return nil, err
94 }
95
96 if entry.Mode == filemode.Dir || entry.Mode == filemode.Submodule {
97 continue
98 }
99
100 blob, err := GetBlob(iter.s, entry.Hash)
101 if err != nil {
102 return nil, err
103 }
104
105 return NewFile(name, entry.Mode, blob), nil
106 }
107 }
108
109 // ForEach call the cb function for each file contained in this iter until
110 // an error happens or the end of the iter is reached. If plumbing.ErrStop is sent
111 // the iteration is stop but no error is returned. The iterator is closed.
112 func (iter *FileIter) ForEach(cb func(*File) error) error {
113 defer iter.Close()
114
115 for {
116 f, err := iter.Next()
117 if err != nil {
118 if err == io.EOF {
119 return nil
120 }
121
122 return err
123 }
124
125 if err := cb(f); err != nil {
126 if err == storer.ErrStop {
127 return nil
128 }
129
130 return err
131 }
132 }
133 }
134
135 func (iter *FileIter) Close() {
136 iter.w.Close()
137 }