aboutsummaryrefslogtreecommitdiffstats
path: root/storage/filesystem/internal/dotgit/refs.go
blob: ca11f6c26a514969c5e09f1a06bd6ce9d1de10de (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package dotgit

import (
	"bufio"
	"errors"
	"io/ioutil"
	"os"
	"strings"

	"gopkg.in/src-d/go-git.v4/core"
)

var (
	// ErrPackedRefsDuplicatedRef is returned when a duplicated
	// reference is found in the packed-ref file. This is usually the
	// case for corrupted git repositories.
	ErrPackedRefsDuplicatedRef = errors.New("duplicated ref found in packed-ref file")
	// ErrPackedRefsBadFormat is returned when the packed-ref file
	// corrupt.
	ErrPackedRefsBadFormat = errors.New("malformed packed-ref")
	// ErrSymRefTargetNotFound is returned when a symbolic reference is
	// targeting a non-existing object. This usually means the
	// repository is corrupt.
	ErrSymRefTargetNotFound = errors.New("symbolic reference target not found")
)

const (
	refsPath = "refs"
)

func (d *DotGit) addRefsFromPackedRefs(refs *[]*core.Reference) (err error) {
	f, err := d.fs.Open(packedRefsPath)
	if err != nil {
		if os.IsNotExist(err) {
			return nil
		}
		return err
	}

	defer func() {
		if errClose := f.Close(); err == nil {
			err = errClose
		}
	}()
	s := bufio.NewScanner(f)
	for s.Scan() {
		ref, err := d.processLine(s.Text())
		if err != nil {
			return err
		}

		if ref != nil {
			*refs = append(*refs, ref)
		}
	}

	return s.Err()
}

// process lines from a packed-refs file
func (d *DotGit) processLine(line string) (*core.Reference, error) {
	switch line[0] {
	case '#': // comment - ignore
		return nil, nil
	case '^': // annotated tag commit of the previous line - ignore
		return nil, nil
	default:
		ws := strings.Split(line, " ") // hash then ref
		if len(ws) != 2 {
			return nil, ErrPackedRefsBadFormat
		}

		return core.NewReferenceFromStrings(ws[1], ws[0]), nil
	}
}

func (d *DotGit) addRefsFromRefDir(refs *[]*core.Reference) error {
	return d.walkReferencesTree(refs, refsPath)
}

func (d *DotGit) walkReferencesTree(refs *[]*core.Reference, relPath string) error {
	files, err := d.fs.ReadDir(relPath)
	if err != nil {
		return err
	}

	for _, f := range files {
		newRelPath := d.fs.Join(relPath, f.Name())
		if f.IsDir() {
			if err = d.walkReferencesTree(refs, newRelPath); err != nil {
				return err
			}

			continue
		}

		ref, err := d.readReferenceFile(".", newRelPath)
		if err != nil {
			return err
		}

		if ref != nil {
			*refs = append(*refs, ref)
		}
	}

	return nil
}

func (d *DotGit) addRefFromHEAD(refs *[]*core.Reference) error {
	ref, err := d.readReferenceFile(".", "HEAD")
	if err != nil {
		return err
	}

	*refs = append(*refs, ref)
	return nil
}

func (d *DotGit) readReferenceFile(refsPath, refFile string) (ref *core.Reference, err error) {
	path := d.fs.Join(refsPath, refFile)

	f, err := d.fs.Open(path)
	if err != nil {
		return nil, err
	}

	defer func() {
		if errClose := f.Close(); err == nil {
			err = errClose
		}
	}()

	b, err := ioutil.ReadAll(f)
	if err != nil {
		return nil, err
	}

	line := strings.TrimSpace(string(b))
	return core.NewReferenceFromStrings(refFile, line), nil
}