aboutsummaryrefslogtreecommitdiffstats
path: root/storage/seekable/internal/gitdir/refs.go
blob: 9c2e8fb785ba4b49aefa4239b13e7ee8a0555e44 (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
142
143
144
145
146
147
148
149
150
151
152
package gitdir

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

	"gopkg.in/src-d/go-git.v3/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 (
	symRefPrefix = "ref: "
)

func (d *GitDir) addRefsFromPackedRefs() (err error) {
	path := d.fs.Join(d.path, packedRefsPath)
	f, err := d.fs.Open(path)
	if err != nil {
		if err == os.ErrNotExist {
			return nil
		}
		return err
	}
	defer func() {
		errClose := f.Close()
		if err == nil {
			err = errClose
		}
	}()

	s := bufio.NewScanner(f)
	for s.Scan() {
		line := s.Text()
		if err = d.processLine(line); err != nil {
			return err
		}
	}

	return s.Err()
}

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

		if _, ok := d.refs[r]; ok {
			return ErrPackedRefsDuplicatedRef
		}
		d.refs[r] = core.NewHash(h)
	}

	return nil
}

func (d *GitDir) addRefsFromRefDir() error {
	return d.walkTree("refs")
}

func (d *GitDir) walkTree(relPath string) error {
	files, err := d.fs.ReadDir(d.fs.Join(d.path, relPath))
	if err != nil {
		return err
	}

	for _, f := range files {
		newRelPath := d.fs.Join(relPath, f.Name())

		if f.IsDir() {
			if err = d.walkTree(newRelPath); err != nil {
				return err
			}
		} else {
			filePath := d.fs.Join(d.path, newRelPath)
			h, err := d.readHashFile(filePath)
			if err != nil {
				return err
			}
			d.refs[newRelPath] = h
		}
	}

	return nil
}

// ReadHashFile reads a single hash from a file.  If a symbolic
// reference is found instead of a hash, the reference is resolved and
// the proper hash is returned.
func (d *GitDir) readHashFile(path string) (h core.Hash, err error) {
	f, err := d.fs.Open(path)
	if err != nil {
		return core.ZeroHash, err
	}
	defer func() {
		errClose := f.Close()
		if err == nil {
			err = errClose
		}
	}()

	b, err := ioutil.ReadAll(f)
	if err != nil {
		return core.ZeroHash, err
	}
	line := strings.TrimSpace(string(b))

	if isSymRef(line) {
		return d.resolveSymRef(line)
	}

	return core.NewHash(line), nil
}

func isSymRef(contents string) bool {
	return strings.HasPrefix(contents, symRefPrefix)
}

func (d *GitDir) resolveSymRef(symRef string) (core.Hash, error) {
	ref := strings.TrimPrefix(symRef, symRefPrefix)

	hash, ok := d.refs[ref]
	if !ok {
		return core.ZeroHash, ErrSymRefTargetNotFound
	}

	return hash, nil
}