this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Create parser skeleton and utilities

+90
+10
gitdiff/gitdiff.go
··· 1 + package gitdiff 2 + 3 + // File describes changes to a single file. It can be either a text file or a 4 + // binary file. 5 + type File struct { 6 + Fragments []*Fragment 7 + } 8 + 9 + // Fragment describes changed lines starting at a specific line in a text file. 10 + type Fragment struct{}
+80
gitdiff/parser.go
··· 1 + package gitdiff 2 + 3 + import ( 4 + "bufio" 5 + "fmt" 6 + "io" 7 + ) 8 + 9 + // Parse parses a patch with changes for one or more files. Any content 10 + // preceding the first file header is ignored. If an error occurs while 11 + // parsing, files will contain all files parsed before the error. 12 + func Parse(r io.Reader) (files []*File, err error) { 13 + p := &parser{r: bufio.NewReader(r)} 14 + 15 + var file *File 16 + for { 17 + file, err = p.ParseNextFileHeader() 18 + if err != nil { 19 + return 20 + } 21 + if file == nil { 22 + break 23 + } 24 + 25 + err = p.ParseFileChanges(file) 26 + if err != nil { 27 + return 28 + } 29 + 30 + files = append(files, file) 31 + } 32 + 33 + return files, nil 34 + } 35 + 36 + type parser struct { 37 + r *bufio.Reader 38 + lineno int64 39 + nextLine string 40 + } 41 + 42 + // ParseNextFileHeader finds and parses the next file header in the stream. It 43 + // returns nil if no headers are found before the end of the stream. 44 + func (p *parser) ParseNextFileHeader() (*File, error) { 45 + panic("unimplemented") 46 + } 47 + 48 + // ParseFileChanges parses file changes until the next file header or the end 49 + // of the stream and attaches them to the given file. 50 + func (p *parser) ParseFileChanges(f *File) error { 51 + panic("unimplemented") 52 + } 53 + 54 + // Line reads and returns the next line. 55 + func (p *parser) Line() (line string, err error) { 56 + if p.nextLine != "" { 57 + line = p.nextLine 58 + p.nextLine = "" 59 + } else { 60 + line, err = p.r.ReadString('\n') 61 + } 62 + p.lineno++ 63 + return 64 + } 65 + 66 + // PeekLine reads and returns the next line without advancing the parser. 67 + func (p *parser) PeekLine() (line string, err error) { 68 + if p.nextLine != "" { 69 + line = p.nextLine 70 + } else { 71 + line, err = p.r.ReadString('\n') 72 + } 73 + p.nextLine = line 74 + return 75 + } 76 + 77 + // Errorf generates an error and appends the current line information. 78 + func (p *parser) Errorf(msg string, args ...interface{}) error { 79 + return fmt.Errorf("gitdiff: line %d: %s", p.lineno, fmt.Sprintf(msg, args...)) 80 + }