From 221873029a6166acaba1fef37508f9f1544180cd Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 24 Nov 2017 12:21:55 -0500 Subject: clean up textproto; move types.go into libfastimport --- backend.go | 2 +- cmd_command.go | 14 ++--- cmd_comment.go | 6 +- cmd_commit.go | 24 ++++---- frontend.go | 8 +-- parse_catblob.go | 8 +-- textproto/catblob.go | 77 ++++++++++++++++++++++++ textproto/fastimport.go | 83 ++++++++++++++++++++++++++ textproto/io.go | 151 ------------------------------------------------ textproto/types.go | 111 ----------------------------------- types.go | 111 +++++++++++++++++++++++++++++++++++ 11 files changed, 298 insertions(+), 297 deletions(-) create mode 100644 textproto/catblob.go create mode 100644 textproto/fastimport.go delete mode 100644 textproto/io.go delete mode 100644 textproto/types.go create mode 100644 types.go diff --git a/backend.go b/backend.go index 52b2441..7e1c50b 100644 --- a/backend.go +++ b/backend.go @@ -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 )? SP LT GT SP 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) { // SP ('blob' | 'tree' | 'commit') SP HT LF // or // 'missing' SP 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 : LF + // cat-blob : SP 'blob' SP LF + // LF + // ls : SP ('blob' | 'tree' | 'commit') SP HT LF + // ls : 'missing' SP 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 : LF - // cat-blob : SP 'blob' SP LF LF - // ls : SP ('blob' | 'tree' | 'commit') SP HT LF - // ls : 'missing' SP 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/textproto/types.go deleted file mode 100644 index a4b438a..0000000 --- a/textproto/types.go +++ /dev/null @@ -1,111 +0,0 @@ -package textproto - -import ( - "fmt" - "strconv" - "strings" - "time" -) - -// BUG(lukeshu): Only supports the "raw" date format (not "rfc2822" or -// "now") -type Ident struct { - Name string - Email string - Time time.Time -} - -func (ut Ident) String() string { - if ut.Name == "" { - return fmt.Sprintf("<%s> %d %s", - ut.Name, - ut.Email, - ut.Time.Unix(), - ut.Time.Format("-0700")) - } else { - return fmt.Sprintf("%s <%s> %d %s", - ut.Name, - ut.Email, - ut.Time.Unix(), - ut.Time.Format("-0700")) - } -} - -func ParseIdent(str string) (Ident, error) { - ret := Ident{} - lt := strings.IndexAny(str, "<>") - if lt < 0 || str[lt] != '<' { - return ret, fmt.Errorf("Missing < in ident string: %v", str) - } - if lt > 0 { - if str[lt-1] != ' ' { - return ret, fmt.Errorf("Missing space before < in ident string: %v", str) - } - ret.Name = str[:lt-1] - } - gt := lt + 1 + strings.IndexAny(str[lt+1:], "<>") - if gt < lt+1 || str[gt] != '>' { - return ret, fmt.Errorf("Missing > in ident string: %v", str) - } - if str[gt+1] != ' ' { - return ret, fmt.Errorf("Missing space after > in ident string: %v", str) - } - ret.Email = str[lt+1 : gt] - - strWhen := str[gt+2:] - sp := strings.IndexByte(strWhen, ' ') - if sp < 0 { - return ret, fmt.Errorf("missing time zone in when: %v", str) - } - sec, err := strconv.ParseInt(strWhen[:sp], 10, 64) - if err != nil { - return ret, err - } - tzt, err := time.Parse("-0700", strWhen[sp+1:]) - if err != nil { - return ret, err - } - ret.Time = time.Unix(sec, 0).In(tzt.Location()) - - return ret, nil -} - -type Mode uint32 // 18 bits - -var ( - ModeFil = Mode(0100644) - ModeExe = Mode(0100755) - ModeSym = Mode(0120000) - ModeGit = Mode(0160000) - ModeDir = Mode(0040000) -) - -func (m Mode) String() string { - return fmt.Sprintf("%06o", m) -} - -func (m Mode) GoString() string { - return fmt.Sprintf("%07o", m) -} - -func PathEscape(path Path) string { - if strings.HasPrefix(string(path), "\"") || strings.ContainsRune(string(path), '\n') { - return "\"" + strings.Replace(strings.Replace(strings.Replace(string(path), "\\", "\\\\", -1), "\"", "\\\"", -1), "\n", "\\n", -1) + "\"" - } else { - return string(path) - } -} - -func PathUnescape(epath string) Path { - if strings.HasPrefix(epath, "\"") { - return Path(strings.Replace(strings.Replace(strings.Replace(epath[1:len(epath)-1], "\\n", "\n", -1), "\\\"", "\"", -1), "\\\\", "\\", -1)) - } else { - return Path(epath) - } -} - -type Path string - -func (p Path) String() string { - return PathEscape(p) -} diff --git a/types.go b/types.go new file mode 100644 index 0000000..e30ad28 --- /dev/null +++ b/types.go @@ -0,0 +1,111 @@ +package libfastimport + +import ( + "fmt" + "strconv" + "strings" + "time" +) + +// BUG(lukeshu): Only supports the "raw" date format (not "rfc2822" or +// "now") +type Ident struct { + Name string + Email string + Time time.Time +} + +func (ut Ident) String() string { + if ut.Name == "" { + return fmt.Sprintf("<%s> %d %s", + ut.Name, + ut.Email, + ut.Time.Unix(), + ut.Time.Format("-0700")) + } else { + return fmt.Sprintf("%s <%s> %d %s", + ut.Name, + ut.Email, + ut.Time.Unix(), + ut.Time.Format("-0700")) + } +} + +func ParseIdent(str string) (Ident, error) { + ret := Ident{} + lt := strings.IndexAny(str, "<>") + if lt < 0 || str[lt] != '<' { + return ret, fmt.Errorf("Missing < in ident string: %v", str) + } + if lt > 0 { + if str[lt-1] != ' ' { + return ret, fmt.Errorf("Missing space before < in ident string: %v", str) + } + ret.Name = str[:lt-1] + } + gt := lt + 1 + strings.IndexAny(str[lt+1:], "<>") + if gt < lt+1 || str[gt] != '>' { + return ret, fmt.Errorf("Missing > in ident string: %v", str) + } + if str[gt+1] != ' ' { + return ret, fmt.Errorf("Missing space after > in ident string: %v", str) + } + ret.Email = str[lt+1 : gt] + + strWhen := str[gt+2:] + sp := strings.IndexByte(strWhen, ' ') + if sp < 0 { + return ret, fmt.Errorf("missing time zone in when: %v", str) + } + sec, err := strconv.ParseInt(strWhen[:sp], 10, 64) + if err != nil { + return ret, err + } + tzt, err := time.Parse("-0700", strWhen[sp+1:]) + if err != nil { + return ret, err + } + ret.Time = time.Unix(sec, 0).In(tzt.Location()) + + return ret, nil +} + +type Mode uint32 // 18 bits + +var ( + ModeFil = Mode(0100644) + ModeExe = Mode(0100755) + ModeSym = Mode(0120000) + ModeGit = Mode(0160000) + ModeDir = Mode(0040000) +) + +func (m Mode) String() string { + return fmt.Sprintf("%06o", m) +} + +func (m Mode) GoString() string { + return fmt.Sprintf("%07o", m) +} + +func PathEscape(path Path) string { + if strings.HasPrefix(string(path), "\"") || strings.ContainsRune(string(path), '\n') { + return "\"" + strings.Replace(strings.Replace(strings.Replace(string(path), "\\", "\\\\", -1), "\"", "\\\"", -1), "\n", "\\n", -1) + "\"" + } else { + return string(path) + } +} + +func PathUnescape(epath string) Path { + if strings.HasPrefix(epath, "\"") { + return Path(strings.Replace(strings.Replace(strings.Replace(epath[1:len(epath)-1], "\\n", "\n", -1), "\\\"", "\"", -1), "\\\\", "\\", -1)) + } else { + return Path(epath) + } +} + +type Path string + +func (p Path) String() string { + return PathEscape(p) +} -- cgit v1.2.3