diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2017-11-24 12:21:55 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2017-11-24 12:33:03 -0500 |
commit | 221873029a6166acaba1fef37508f9f1544180cd (patch) | |
tree | 8fedd9d27d25c0591e2f60468670335a4ded9a25 | |
parent | 4a566395874113e38b5785fc5e436c96c001b032 (diff) |
clean up textproto; move types.go into libfastimport
-rw-r--r-- | backend.go | 2 | ||||
-rw-r--r-- | cmd_command.go | 14 | ||||
-rw-r--r-- | cmd_comment.go | 6 | ||||
-rw-r--r-- | cmd_commit.go | 24 | ||||
-rw-r--r-- | frontend.go | 8 | ||||
-rw-r--r-- | parse_catblob.go | 8 | ||||
-rw-r--r-- | textproto/catblob.go | 77 | ||||
-rw-r--r-- | textproto/fastimport.go | 83 | ||||
-rw-r--r-- | textproto/io.go | 151 | ||||
-rw-r--r-- | types.go (renamed from textproto/types.go) | 2 |
10 files changed, 188 insertions, 187 deletions
@@ -122,7 +122,7 @@ func (b *Backend) CatBlob(cmd CmdCatBlob) (sha1 string, data string, err error) return } -func (b *Backend) Ls(cmd CmdLs) (mode textproto.Mode, dataref string, path textproto.Path, err error) { +func (b *Backend) Ls(cmd CmdLs) (mode Mode, dataref string, path Path, err error) { err = b.Do(cmd) if err != nil { return diff --git a/cmd_command.go b/cmd_command.go index c6e7080..b004488 100644 --- a/cmd_command.go +++ b/cmd_command.go @@ -4,8 +4,6 @@ import ( "fmt" "strconv" "strings" - - "git.lukeshu.com/go/libfastimport/textproto" ) // commit ////////////////////////////////////////////////////////////////////// @@ -13,8 +11,8 @@ import ( type CmdCommit struct { Ref string Mark int // optional; < 1 for non-use - Author *textproto.Ident - Committer textproto.Ident + Author *Ident + Committer Ident Msg string From string Merge []string @@ -58,7 +56,7 @@ func (CmdCommit) fiCmdRead(fir fiReader) (cmd Cmd, err error) { // ('author' (SP <name>)? SP LT <email> GT SP <when> LF)? if strings.HasPrefix(ez.PeekLine(), "author ") { - author, err := textproto.ParseIdent(trimLinePrefix(ez.ReadLine(), "author ")) + author, err := ParseIdent(trimLinePrefix(ez.ReadLine(), "author ")) ez.Errcheck(err) c.Author = &author } @@ -67,7 +65,7 @@ func (CmdCommit) fiCmdRead(fir fiReader) (cmd Cmd, err error) { if !strings.HasPrefix(ez.PeekLine(), "committer ") { ez.Errcheck(fmt.Errorf("commit: expected committer command: %v", ez.ReadLine())) } - c.Committer, err = textproto.ParseIdent(trimLinePrefix(ez.ReadLine(), "committer ")) + c.Committer, err = ParseIdent(trimLinePrefix(ez.ReadLine(), "committer ")) ez.Errcheck(err) // data @@ -99,7 +97,7 @@ func (CmdCommitEnd) fiCmdRead(fir fiReader) (Cmd, error) { panic("not reached") type CmdTag struct { RefName string CommitIsh string - Tagger textproto.Ident + Tagger Ident Data string } @@ -132,7 +130,7 @@ func (CmdTag) fiCmdRead(fir fiReader) (cmd Cmd, err error) { if !strings.HasPrefix(ez.PeekLine(), "tagger ") { ez.Errcheck(fmt.Errorf("tag: expected tagger command: %v", ez.ReadLine())) } - c.Tagger, err = textproto.ParseIdent(trimLinePrefix(ez.ReadLine(), "tagger ")) + c.Tagger, err = ParseIdent(trimLinePrefix(ez.ReadLine(), "tagger ")) ez.Errcheck(err) // data diff --git a/cmd_comment.go b/cmd_comment.go index d2aaccc..9d5aa88 100644 --- a/cmd_comment.go +++ b/cmd_comment.go @@ -4,8 +4,6 @@ import ( "fmt" "strconv" "strings" - - "git.lukeshu.com/go/libfastimport/textproto" ) // comment ///////////////////////////////////////////////////////////////////// @@ -74,7 +72,7 @@ func (CmdCatBlob) fiCmdRead(fir fiReader) (cmd Cmd, err error) { type CmdLs struct { DataRef string // optional if inside of a commit - Path textproto.Path + Path Path } // If you're thinking "but wait, parser_registerCmd will see CmdLs as @@ -111,7 +109,7 @@ func (CmdLs) fiCmdRead(fir fiReader) (cmd Cmd, err error) { sp = strings.IndexByte(line, ' ') } c := CmdLs{} - c.Path = textproto.PathUnescape(str[sp+1:]) + c.Path = PathUnescape(str[sp+1:]) if sp >= 0 { c.DataRef = str[:sp] } diff --git a/cmd_commit.go b/cmd_commit.go index 1e2672d..3c2a46e 100644 --- a/cmd_commit.go +++ b/cmd_commit.go @@ -4,15 +4,13 @@ import ( "fmt" "strconv" "strings" - - "git.lukeshu.com/go/libfastimport/textproto" ) // M /////////////////////////////////////////////////////////////////////////// type FileModify struct { - Mode textproto.Mode - Path textproto.Path + Mode Mode + Path Path DataRef string } @@ -39,7 +37,7 @@ func (FileModify) fiCmdRead(fir fiReader) (cmd Cmd, err error) { } ref := fields[1] - path := textproto.PathUnescape(fields[2]) + path := PathUnescape(fields[2]) if ref == "inline" { line, err = fir.ReadLine() @@ -51,13 +49,13 @@ func (FileModify) fiCmdRead(fir fiReader) (cmd Cmd, err error) { return nil, err } return FileModifyInline{ - Mode: textproto.Mode(nMode), + Mode: Mode(nMode), Path: path, Data: data, }, nil } else { return FileModify{ - Mode: textproto.Mode(nMode), + Mode: Mode(nMode), Path: path, DataRef: ref, }, nil @@ -65,8 +63,8 @@ func (FileModify) fiCmdRead(fir fiReader) (cmd Cmd, err error) { } type FileModifyInline struct { - Mode textproto.Mode - Path textproto.Path + Mode Mode + Path Path Data string } @@ -82,7 +80,7 @@ func (FileModifyInline) fiCmdRead(fiReader) (Cmd, error) { panic("not reached") // D /////////////////////////////////////////////////////////////////////////// type FileDelete struct { - Path textproto.Path + Path Path } func (o FileDelete) fiCmdClass() cmdClass { return cmdClassCommit } @@ -95,14 +93,14 @@ func (FileDelete) fiCmdRead(fir fiReader) (cmd Cmd, err error) { if err != nil { return nil, err } - return FileDelete{Path: textproto.PathUnescape(trimLinePrefix(line, "D "))}, nil + return FileDelete{Path: PathUnescape(trimLinePrefix(line, "D "))}, nil } // C /////////////////////////////////////////////////////////////////////////// type FileCopy struct { - Src textproto.Path - Dst textproto.Path + Src Path + Dst Path } func (o FileCopy) fiCmdClass() cmdClass { return cmdClassCommit } diff --git a/frontend.go b/frontend.go index 285bbd9..d0c8c10 100644 --- a/frontend.go +++ b/frontend.go @@ -67,21 +67,21 @@ func (f *Frontend) RespondCatBlob(sha1 string, data string) error { return f.catBlobFlush.Flush() } -func (f *Frontend) RespondLs(mode textproto.Mode, dataref string, path textproto.Path) error { +func (f *Frontend) RespondLs(mode Mode, dataref string, path Path) error { var err error if mode == 0 { err = f.catBlobWrite.WriteLine("missing", path) } else { var t string switch mode { - case textproto.ModeDir: + case ModeDir: t = "tree" - case textproto.ModeGit: + case ModeGit: t = "commit" default: t = "blob" } - err = f.catBlobWrite.WriteLine(mode, t, dataref+"\t"+textproto.PathEscape(path)) + err = f.catBlobWrite.WriteLine(mode, t, dataref+"\t"+PathEscape(path)) } if err != nil { return err diff --git a/parse_catblob.go b/parse_catblob.go index 7a018cf..6d5bd87 100644 --- a/parse_catblob.go +++ b/parse_catblob.go @@ -4,8 +4,6 @@ import ( "fmt" "strconv" "strings" - - "git.lukeshu.com/go/libfastimport/textproto" ) func cbpGetMark(line string) (string, error) { @@ -67,7 +65,7 @@ func cbpCatBlob(full string) (sha1 string, data string, err error) { return sha1, data, err } -func cbpLs(line string) (mode textproto.Mode, dataref string, path textproto.Path, err error) { +func cbpLs(line string) (mode Mode, dataref string, path Path, err error) { // <mode> SP ('blob' | 'tree' | 'commit') SP <dataref> HT <path> LF // or // 'missing' SP <path> LF @@ -78,7 +76,7 @@ func cbpLs(line string) (mode textproto.Mode, dataref string, path textproto.Pat if strings.HasPrefix(line, "missing ") { strPath := line[8:] - return 0, "", textproto.PathUnescape(strPath), nil + return 0, "", PathUnescape(strPath), nil } else { fields := strings.SplitN(line, " ", 3) if len(fields) < 3 { @@ -97,6 +95,6 @@ func cbpLs(line string) (mode textproto.Mode, dataref string, path textproto.Pat if err != nil { return 0, "", "", err } - return textproto.Mode(nMode), strRef, textproto.PathUnescape(strPath), nil + return Mode(nMode), strRef, PathUnescape(strPath), nil } } diff --git a/textproto/catblob.go b/textproto/catblob.go new file mode 100644 index 0000000..9b0f8d7 --- /dev/null +++ b/textproto/catblob.go @@ -0,0 +1,77 @@ +package textproto + +import ( + "bufio" + "fmt" + "io" + "strconv" +) + +type CatBlobReader struct { + r *bufio.Reader +} + +func NewCatBlobReader(r io.Reader) *CatBlobReader { + return &CatBlobReader{ + r: bufio.NewReader(r), + } +} + +func (cbr *CatBlobReader) ReadLine() (line string, err error) { + for len(line) <= 1 { + line, err = cbr.r.ReadString('\n') + if err != nil { + return + } + } + + // get-mark : <sha1> LF + // cat-blob : <sha1> SP 'blob' SP <size> LF + // <data> LF + // ls : <mode> SP ('blob' | 'tree' | 'commit') SP <dataref> HT <path> LF + // ls : 'missing' SP <path> LF + + // decide if we have a cat-blob result (return early if we don't) + if len(line) <= 46 || line[40:46] != " blob " { + return + } + for _, b := range line[:40] { + if !(('0' <= b && b <= '9') || ('a' <= b && b <= 'f')) { + return + } + } + // we have a cat-blob result + var size int + size, err = strconv.Atoi(line[46 : len(line)-1]) + if err != nil { + return + } + data := make([]byte, size+1) + _, err = io.ReadFull(cbr.r, data) + line += string(data[:size]) + return +} + +type CatBlobWriter struct { + w io.Writer +} + +func NewCatBlobWriter(w io.Writer) *CatBlobWriter { + return &CatBlobWriter{ + w: w, + } +} + +func (cbw *CatBlobWriter) WriteLine(a ...interface{}) error { + _, err := fmt.Fprintln(cbw.w, a...) + return err +} + +func (cbw *CatBlobWriter) WriteBlob(sha1 string, data string) error { + err := cbw.WriteLine(sha1, "blob", len(data)) + if err != nil { + return err + } + _, err = io.WriteString(cbw.w, data) + return err +} diff --git a/textproto/fastimport.go b/textproto/fastimport.go new file mode 100644 index 0000000..c845f7f --- /dev/null +++ b/textproto/fastimport.go @@ -0,0 +1,83 @@ +package textproto + +import ( + "bufio" + "fmt" + "io" + "strconv" + "strings" +) + +type FIReader struct { + r *bufio.Reader + + line *string + err error +} + +func NewFIReader(r io.Reader) *FIReader { + return &FIReader{ + r: bufio.NewReader(r), + } +} + +func (fir *FIReader) ReadLine() (line string, err error) { + for len(line) <= 1 { + line, err = fir.r.ReadString('\n') + if err != nil { + return + } + } + + if strings.HasPrefix(line, "data ") { + if line[5:7] == "<<" { + // Delimited format + delim := line[7 : len(line)-1] + suffix := "\n" + delim + "\n" + + for !strings.HasSuffix(line, suffix) { + var _line string + _line, err = fir.r.ReadString('\n') + line += _line + if err != nil { + return + } + } + } else { + // Exact byte count format + var size int + size, err = strconv.Atoi(line[5 : len(line)-1]) + if err != nil { + return + } + data := make([]byte, size) + _, err = io.ReadFull(fir.r, data) + line += string(data) + } + } + return +} + +type FIWriter struct { + w io.Writer +} + +func NewFIWriter(w io.Writer) *FIWriter { + return &FIWriter{ + w: w, + } +} + +func (fiw *FIWriter) WriteLine(a ...interface{}) error { + _, err := fmt.Fprintln(fiw.w, a...) + return err +} + +func (fiw *FIWriter) WriteData(data string) error { + err := fiw.WriteLine("data", len(data)) + if err != nil { + return err + } + _, err = io.WriteString(fiw.w, data) + return err +} diff --git a/textproto/io.go b/textproto/io.go deleted file mode 100644 index 09f36d7..0000000 --- a/textproto/io.go +++ /dev/null @@ -1,151 +0,0 @@ -package textproto - -import ( - "bufio" - "fmt" - "io" - "strconv" - "strings" -) - -type FIReader struct { - r *bufio.Reader - - line *string - err error -} - -func NewFIReader(r io.Reader) *FIReader { - return &FIReader{ - r: bufio.NewReader(r), - } -} - -func (fir *FIReader) ReadLine() (line string, err error) { - for len(line) <= 1 { - line, err = fir.r.ReadString('\n') - if err != nil { - return - } - } - - if strings.HasPrefix(line, "data ") { - if line[5:7] == "<<" { - // Delimited format - delim := line[7 : len(line)-1] - suffix := "\n" + delim + "\n" - - for !strings.HasSuffix(line, suffix) { - var _line string - _line, err = fir.r.ReadString('\n') - line += _line - if err != nil { - return - } - } - } else { - // Exact byte count format - var size int - size, err = strconv.Atoi(line[5 : len(line)-1]) - if err != nil { - return - } - data := make([]byte, size) - _, err = io.ReadFull(fir.r, data) - line += string(data) - } - } - return -} - -type FIWriter struct { - w io.Writer -} - -func NewFIWriter(w io.Writer) *FIWriter { - return &FIWriter{ - w: w, - } -} - -func (fiw *FIWriter) WriteLine(a ...interface{}) error { - _, err := fmt.Fprintln(fiw.w, a...) - return err -} - -func (fiw *FIWriter) WriteData(data string) error { - err := fiw.WriteLine("data", len(data)) - if err != nil { - return err - } - _, err = io.WriteString(fiw.w, data) - return err -} - -type CatBlobReader struct { - r *bufio.Reader -} - -func NewCatBlobReader(r io.Reader) *CatBlobReader { - return &CatBlobReader{ - r: bufio.NewReader(r), - } -} - -func (cbr *CatBlobReader) ReadLine() (line string, err error) { - for len(line) <= 1 { - line, err = cbr.r.ReadString('\n') - if err != nil { - return - } - } - - // get-mark : <sha1> LF - // cat-blob : <sha1> SP 'blob' SP <size> LF <data> LF - // ls : <mode> SP ('blob' | 'tree' | 'commit') SP <dataref> HT <path> LF - // ls : 'missing' SP <path> LF - - // decide if we have a cat-blob result - if len(line) <= 46 || line[40:46] != " blob " { - return - } - for _, b := range line[:40] { - if !(('0' <= b && b <= '9') || ('a' <= b && b <= 'f')) { - return - } - } - // we have a cat-blob result - var size int - size, err = strconv.Atoi(line[46 : len(line)-1]) - if err != nil { - return - } - data := make([]byte, size+1) - _, err = io.ReadFull(cbr.r, data) - line += string(data[:size]) - return -} - -type CatBlobWriter struct { - w io.Writer -} - -func NewCatBlobWriter(w io.Writer) *CatBlobWriter { - return &CatBlobWriter{ - w: w, - } -} - -func (cbw *CatBlobWriter) WriteLine(a ...interface{}) error { - _, err := fmt.Fprintln(cbw.w, a...) - return err -} - -func (cbw *CatBlobWriter) WriteBlob(sha1 string, data string) error { - err := cbw.WriteLine(sha1, "blob", len(data)) - if err != nil { - return err - } - _, err = io.WriteString(cbw.w, data) - return err -} diff --git a/textproto/types.go b/types.go index a4b438a..e30ad28 100644 --- a/textproto/types.go +++ b/types.go @@ -1,4 +1,4 @@ -package textproto +package libfastimport import ( "fmt" |