summaryrefslogtreecommitdiff
path: root/textproto
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2017-11-15 21:39:40 -0500
committerLuke Shumaker <lukeshu@lukeshu.com>2017-11-15 21:39:40 -0500
commitefc7a5ebc9e10983571c080017100a8b39eee1d0 (patch)
treeb11aea0aaefb6c16521b2433338ebe7d0440de14 /textproto
parent7675ebe9567bd7cae4306f484e3677bf88ba8b55 (diff)
more
Diffstat (limited to 'textproto')
-rw-r--r--textproto/io.go146
-rw-r--r--textproto/types.go65
2 files changed, 211 insertions, 0 deletions
diff --git a/textproto/io.go b/textproto/io.go
new file mode 100644
index 0000000..af2cb19
--- /dev/null
+++ b/textproto/io.go
@@ -0,0 +1,146 @@
+package textproto
+
+import (
+ "bufio"
+ "fmt"
+ "io"
+ "strconv"
+ "strings"
+)
+
+type FIReader struct {
+ r *bufio.Reader
+}
+
+func NewFIReader(r io.Reader) *FIReader {
+ return &FIReader{
+ r: bufio.NewReader(r),
+ }
+}
+
+func (fir *FIReader) ReadLine() (line string, err error) {
+retry:
+ line, err = fir.r.ReadString('\n')
+ if err != nil {
+ return
+ }
+ if len(line) == 1 {
+ goto retry
+ }
+
+ 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 []byte) error {
+ err := fiw.WriteLine("data", len(data))
+ if err != nil {
+ return err
+ }
+ _, err = fiw.w.Write(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) {
+retry:
+ line, err = cbr.r.ReadString('\n')
+ if err != nil {
+ return
+ }
+ if len(line) == 1 {
+ goto retry
+ }
+
+ // 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 (cbw *CatBlobWriter) WriteLine(a ...interface{}) error {
+ _, err := fmt.Fprintln(cbw.w, a...)
+ return err
+}
+
+func (cbw *CatBlobWriter) WriteBlob(sha1 string, data []byte) error {
+ err := cbw.WriteLine(sha1, "blob", len(data))
+ if err != nil {
+ return err
+ }
+ _, err = cbw.w.Write(data)
+ return err
+}
diff --git a/textproto/types.go b/textproto/types.go
new file mode 100644
index 0000000..2109517
--- /dev/null
+++ b/textproto/types.go
@@ -0,0 +1,65 @@
+package textproto
+
+import (
+ "fmt"
+ "strings"
+ "time"
+)
+
+type UserTime struct {
+ Name string
+ Email string
+ Time time.Time
+}
+
+func (ut UserTime) 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"))
+ }
+}
+
+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 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)
+}