File: file.go

package info (click to toggle)
golang-github-go-git-go-git 5.16.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,684 kB
  • sloc: makefile: 81; sh: 76
file content (76 lines) | stat: -rw-r--r-- 1,408 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package fsnoder

import (
	"bytes"
	"fmt"
	"hash/fnv"

	"github.com/go-git/go-git/v5/utils/merkletrie/noder"
)

// file values represent file-like noders in a merkle trie.
type file struct {
	name     string // relative
	contents string
	hash     []byte // memoized
}

// newFile returns a noder representing a file with the given contents.
func newFile(name, contents string) (*file, error) {
	if name == "" {
		return nil, fmt.Errorf("files cannot have empty names")
	}

	return &file{
		name:     name,
		contents: contents,
	}, nil
}

// The hash of a file is just its contents.
// Empty files will have the fnv64 basis offset as its hash.
func (f *file) Hash() []byte {
	if f.hash == nil {
		h := fnv.New64a()
		h.Write([]byte(f.contents)) // it never returns an error.
		f.hash = h.Sum(nil)
	}

	return f.hash
}

func (f *file) Name() string {
	return f.name
}

func (f *file) IsDir() bool {
	return false
}

func (f *file) Children() ([]noder.Noder, error) {
	return noder.NoChildren, nil
}

func (f *file) NumChildren() (int, error) {
	return 0, nil
}

func (f *file) Skip() bool {
	return false
}

const (
	fileStartMark = '<'
	fileEndMark   = '>'
)

// String returns a string formatted as: name<contents>.
func (f *file) String() string {
	var buf bytes.Buffer
	buf.WriteString(f.name)
	buf.WriteRune(fileStartMark)
	buf.WriteString(f.contents)
	buf.WriteRune(fileEndMark)

	return buf.String()
}