diff options
Diffstat (limited to 'cmd_commit.go')
-rw-r--r-- | cmd_commit.go | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/cmd_commit.go b/cmd_commit.go index 5c882f8..a5dcb90 100644 --- a/cmd_commit.go +++ b/cmd_commit.go @@ -1,9 +1,15 @@ package libfastimport import ( + "fmt" + "strconv" + "strings" + "git.lukeshu.com/go/libfastimport/textproto" ) +// M /////////////////////////////////////////////////////////////////////////// + type FileModify struct { Mode textproto.Mode Path textproto.Path @@ -14,6 +20,47 @@ func (o FileModify) fiCmdClass() cmdClass { return cmdClassCommit } func (o FileModify) fiCmdWrite(fiw *textproto.FIWriter) error { return fiw.WriteLine("M", o.Mode, o.DataRef, o.Path) } +func init() { parser_registerCmd("M ", FileModify{}) } +func (FileModify) fiCmdRead(fir fiReader) (cmd Cmd, err error) { + // NB: This parses both FileModify and FileModifyInline + line, err := fir.ReadLine() + if err != nil { + return nil, err + } + str := trimLinePrefix(line, "M ") + sp1 := strings.IndexByte(str, ' ') + sp2 := strings.IndexByte(str[sp1+1:], ' ') + if sp1 < 0 || sp2 < 0 { + return nil, fmt.Errorf("commit: malformed modify command: %v", line) + } + nMode, err := strconv.ParseUint(str[:sp1], 8, 18) + if err != nil { + return nil, err + } + ref := str[sp1+1 : sp2] + path := textproto.PathUnescape(str[sp2+1:]) + if ref == "inline" { + line, err = fir.ReadLine() + if err != nil { + return nil, err + } + data, err := parse_data(line) + if err != nil { + return nil, err + } + return FileModifyInline{ + Mode: textproto.Mode(nMode), + Path: path, + Data: data, + }, nil + } else { + return FileModify{ + Mode: textproto.Mode(nMode), + Path: path, + DataRef: ref, + }, nil + } +} type FileModifyInline struct { Mode textproto.Mode @@ -28,6 +75,9 @@ func (o FileModifyInline) fiCmdWrite(fiw *textproto.FIWriter) error { ez.WriteData(o.Data) return ez.err } +func (FileModifyInline) fiCmdRead(fiReader) (Cmd, error) { panic("not reached") } + +// D /////////////////////////////////////////////////////////////////////////// type FileDelete struct { Path textproto.Path @@ -37,6 +87,16 @@ func (o FileDelete) fiCmdClass() cmdClass { return cmdClassCommit } func (o FileDelete) fiCmdWrite(fiw *textproto.FIWriter) error { return fiw.WriteLine("D", o.Path) } +func init() { parser_registerCmd("D ", FileDelete{}) } +func (FileDelete) fiCmdRead(fir fiReader) (cmd Cmd, err error) { + line, err := fir.ReadLine() + if err != nil { + return nil, err + } + return FileDelete{Path: textproto.PathUnescape(trimLinePrefix(line, "D "))}, nil +} + +// C /////////////////////////////////////////////////////////////////////////// type FileCopy struct { Src textproto.Path @@ -47,6 +107,13 @@ func (o FileCopy) fiCmdClass() cmdClass { return cmdClassCommit } func (o FileCopy) fiCmdWrite(fiw *textproto.FIWriter) error { return fiw.WriteLine("C", o.Src, o.Dst) } +func init() { parser_registerCmd("C ", FileDelete{}) } +func (FileCopy) fiCmdRead(fir fiReader) (cmd Cmd, err error) { + // BUG(lukeshu): TODO: commit C not implemented + panic("TODO: commit C not implemented") +} + +// R /////////////////////////////////////////////////////////////////////////// type FileRename struct { Src string @@ -57,6 +124,13 @@ func (o FileRename) fiCmdClass() cmdClass { return cmdClassCommit } func (o FileRename) fiCmdWrite(fiw *textproto.FIWriter) error { return fiw.WriteLine("R", o.Src, o.Dst) } +func init() { parser_registerCmd("R ", FileDelete{}) } +func (FileRename) fiCmdRead(fir fiReader) (cmd Cmd, err error) { + // BUG(lukeshu): TODO: commit R not implemented + panic("TODO: commit R not implemented") +} + +// deleteall /////////////////////////////////////////////////////////////////// type FileDeleteAll struct{} @@ -64,6 +138,16 @@ func (o FileDeleteAll) fiCmdClass() cmdClass { return cmdClassCommit } func (o FileDeleteAll) fiCmdWrite(fiw *textproto.FIWriter) error { return fiw.WriteLine("deleteall") } +func init() { parser_registerCmd("deleteall\n", FileDeleteAll{}) } +func (FileDeleteAll) fiCmdRead(fir fiReader) (cmd Cmd, err error) { + _, err = fir.ReadLine() + if err != nil { + return nil, err + } + return FileDeleteAll{}, nil +} + +// N /////////////////////////////////////////////////////////////////////////// type NoteModify struct { CommitIsh string @@ -74,6 +158,40 @@ func (o NoteModify) fiCmdClass() cmdClass { return cmdClassCommit } func (o NoteModify) fiCmdWrite(fiw *textproto.FIWriter) error { return fiw.WriteLine("N", o.DataRef, o.CommitIsh) } +func init() { parser_registerCmd("N ", NoteModify{}) } +func (NoteModify) fiCmdRead(fir fiReader) (cmd Cmd, err error) { + // NB: This parses both NoteModify and NoteModifyInline + line, err := fir.ReadLine() + if err != nil { + return nil, err + } + str := trimLinePrefix(line, "N ") + sp := strings.IndexByte(str, ' ') + if sp < 0 { + return nil, fmt.Errorf("commit: malformed notemodify command: %v", line) + } + ref := str[:sp] + commitish := str[sp+1:] + if ref == "inline" { + line, err = fir.ReadLine() + if err != nil { + return nil, err + } + data, err := parse_data(line) + if err != nil { + return nil, err + } + return NoteModifyInline{ + CommitIsh: commitish, + Data: data, + }, nil + } else { + return NoteModify{ + CommitIsh: commitish, + DataRef: ref, + }, nil + } +} type NoteModifyInline struct { CommitIsh string @@ -87,3 +205,4 @@ func (o NoteModifyInline) fiCmdWrite(fiw *textproto.FIWriter) error { ez.WriteData(o.Data) return ez.err } +func (NoteModifyInline) fiCmdRead(fiReader) (Cmd, error) { panic("not reached") } |