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 /textproto | |
parent | 4a566395874113e38b5785fc5e436c96c001b032 (diff) |
clean up textproto; move types.go into libfastimport
Diffstat (limited to 'textproto')
-rw-r--r-- | textproto/catblob.go | 77 | ||||
-rw-r--r-- | textproto/fastimport.go | 83 | ||||
-rw-r--r-- | textproto/io.go | 151 | ||||
-rw-r--r-- | textproto/types.go | 111 |
4 files changed, 160 insertions, 262 deletions
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/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) -} |