smithy

ref: 8fbf436312dd2794ca688ab0e8b9b72efe362b94

pkg/go-git-http/git_reader.go


 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
package githttp

import (
	"errors"
	"io"
	"regexp"
)

// GitReader scans for errors in the output of a git command
type GitReader struct {
	// Underlying reader (to relay calls to)
	io.Reader

	// Error
	GitError error
}

// Regex to detect errors
var (
	gitErrorRegex = regexp.MustCompile("error: (.*)")
)

// Implement the io.Reader interface
func (g *GitReader) Read(p []byte) (n int, err error) {
	// Relay call
	n, err = g.Reader.Read(p)

	// Scan for errors
	g.scan(p)

	return n, err
}

func (g *GitReader) scan(data []byte) {
	// Already got an error
	// the main error will be the first error line
	if g.GitError != nil {
		return
	}

	matches := gitErrorRegex.FindSubmatch(data)

	// Skip, no matches found
	if matches == nil {
		return
	}

	g.GitError = errors.New(string(matches[1][:]))
}