From e3b3c08e62f07c75426536c3407dfebc4b71405a Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 30 Nov 2017 20:41:00 -0500 Subject: use github.com/pkg/errors --- backend.go | 6 +++--- cmd_command.go | 9 +++++---- cmd_comment.go | 5 +++-- cmd_commit.go | 7 ++++--- parse_catblob.go | 29 +++++++++++++++-------------- parse_fastimport.go | 8 ++++---- types.go | 12 +++++++----- util.go | 9 +++++---- 8 files changed, 46 insertions(+), 39 deletions(-) diff --git a/backend.go b/backend.go index 96cdbbf..668e06e 100644 --- a/backend.go +++ b/backend.go @@ -2,10 +2,10 @@ package libfastimport import ( "bufio" - "fmt" "io" "git.lukeshu.com/go/libfastimport/textproto" + "github.com/pkg/errors" ) // A Backend is something that consumes a fast-import stream; the @@ -64,12 +64,12 @@ func (b *Backend) Do(cmd Cmd) error { _, b.inCommit = cmd.(CmdCommit) case cmdClassCommit: if !b.inCommit { - panic(fmt.Errorf("Cannot issue commit sub-command outside of a commit: %[1]T(%#[1]v)", cmd)) + panic(errors.Errorf("Cannot issue commit sub-command outside of a commit: %[1]T(%#[1]v)", cmd)) } case cmdClassComment: /* do nothing */ default: - panic(fmt.Errorf("invalid cmdClass: %d", cmd.fiCmdClass())) + panic(errors.Errorf("invalid cmdClass: %d", cmd.fiCmdClass())) } err := cmd.fiCmdWrite(b.fastImportWrite) diff --git a/cmd_command.go b/cmd_command.go index b004488..a6c5ec1 100644 --- a/cmd_command.go +++ b/cmd_command.go @@ -1,9 +1,10 @@ package libfastimport import ( - "fmt" "strconv" "strings" + + "github.com/pkg/errors" ) // commit ////////////////////////////////////////////////////////////////////// @@ -63,7 +64,7 @@ func (CmdCommit) fiCmdRead(fir fiReader) (cmd Cmd, err error) { // 'committer' (SP )? SP LT GT SP LF if !strings.HasPrefix(ez.PeekLine(), "committer ") { - ez.Errcheck(fmt.Errorf("commit: expected committer command: %v", ez.ReadLine())) + ez.Errcheck(errors.Errorf("commit: expected committer command: %v", ez.ReadLine())) } c.Committer, err = ParseIdent(trimLinePrefix(ez.ReadLine(), "committer ")) ez.Errcheck(err) @@ -122,13 +123,13 @@ func (CmdTag) fiCmdRead(fir fiReader) (cmd Cmd, err error) { // 'from' SP LF if !strings.HasPrefix(ez.PeekLine(), "from ") { - ez.Errcheck(fmt.Errorf("tag: expected from command: %v", ez.ReadLine())) + ez.Errcheck(errors.Errorf("tag: expected from command: %v", ez.ReadLine())) } c.CommitIsh = trimLinePrefix(ez.ReadLine(), "from ") // 'tagger' (SP )? SP LT GT SP LF if !strings.HasPrefix(ez.PeekLine(), "tagger ") { - ez.Errcheck(fmt.Errorf("tag: expected tagger command: %v", ez.ReadLine())) + ez.Errcheck(errors.Errorf("tag: expected tagger command: %v", ez.ReadLine())) } c.Tagger, err = ParseIdent(trimLinePrefix(ez.ReadLine(), "tagger ")) ez.Errcheck(err) diff --git a/cmd_comment.go b/cmd_comment.go index 9d5aa88..b554487 100644 --- a/cmd_comment.go +++ b/cmd_comment.go @@ -1,9 +1,10 @@ package libfastimport import ( - "fmt" "strconv" "strings" + + "github.com/pkg/errors" ) // comment ///////////////////////////////////////////////////////////////////// @@ -44,7 +45,7 @@ func (CmdGetMark) fiCmdRead(fir fiReader) (cmd Cmd, err error) { c := CmdGetMark{} c.Mark, err = strconv.Atoi(trimLinePrefix(line, "get-mark :")) if err != nil { - return nil, fmt.Errorf("get-mark: %v", err) + return nil, errors.Wrap(err, "get-mark") } return c, nil } diff --git a/cmd_commit.go b/cmd_commit.go index 3c2a46e..db22642 100644 --- a/cmd_commit.go +++ b/cmd_commit.go @@ -1,9 +1,10 @@ package libfastimport import ( - "fmt" "strconv" "strings" + + "github.com/pkg/errors" ) // M /////////////////////////////////////////////////////////////////////////// @@ -28,7 +29,7 @@ func (FileModify) fiCmdRead(fir fiReader) (cmd Cmd, err error) { str := trimLinePrefix(line, "M ") fields := strings.SplitN(str, " ", 3) if len(fields) != 3 { - return nil, fmt.Errorf("commit: malformed modify command: %v", line) + return nil, errors.Errorf("commit: malformed modify command: %v", line) } nMode, err := strconv.ParseUint(fields[0], 8, 18) @@ -168,7 +169,7 @@ func (NoteModify) fiCmdRead(fir fiReader) (cmd Cmd, err error) { str := trimLinePrefix(line, "N ") sp := strings.IndexByte(str, ' ') if sp < 0 { - return nil, fmt.Errorf("commit: malformed notemodify command: %v", line) + return nil, errors.Errorf("commit: malformed notemodify command: %v", line) } ref := str[:sp] diff --git a/parse_catblob.go b/parse_catblob.go index 6d5bd87..c6f66c8 100644 --- a/parse_catblob.go +++ b/parse_catblob.go @@ -1,21 +1,22 @@ package libfastimport import ( - "fmt" "strconv" "strings" + + "github.com/pkg/errors" ) func cbpGetMark(line string) (string, error) { if len(line) != 41 { - return "", fmt.Errorf("get-mark: short \\n: %q", line) + return "", errors.Errorf("get-mark: short \\n: %q", line) } if line[40] != '\n' { - return "", fmt.Errorf("get-mark: malformed \\n: %q", line) + return "", errors.Errorf("get-mark: malformed \\n: %q", line) } for _, b := range line[:40] { if !(('0' <= b && b <= '9') || ('a' <= b && b <= 'f')) { - return "", fmt.Errorf("get-mark: malformed : %q", line[:40]) + return "", errors.Errorf("get-mark: malformed : %q", line[:40]) } } return line[:40], nil @@ -28,38 +29,38 @@ func cbpCatBlob(full string) (sha1 string, data string, err error) { // LF if full[len(full)-1] != '\n' { - return "", "", fmt.Errorf("cat-blob: missing trailing newline") + return "", "", errors.Errorf("cat-blob: missing trailing newline") } lf := strings.IndexByte(full, '\n') if lf < 0 || lf == len(full)-1 { - return "", "", fmt.Errorf("cat-blob: malformed header: %q", full) + return "", "", errors.Errorf("cat-blob: malformed header: %q", full) } head := full[:lf] data = full[lf+1 : len(full)-1] if len(head) < 40+6+1 { - return "", "", fmt.Errorf("cat-blob: malformed header: %q", head) + return "", "", errors.Errorf("cat-blob: malformed header: %q", head) } sha1 = head[:40] for _, b := range sha1 { if !(('0' <= b && b <= '9') || ('a' <= b && b <= 'f')) { - return "", "", fmt.Errorf("cat-blob: malformed : %q", sha1) + return "", "", errors.Errorf("cat-blob: malformed : %q", sha1) } } if string(head[40:46]) != " blob " { - return "", "", fmt.Errorf("cat-blob: malformed header: %q", head) + return "", "", errors.Errorf("cat-blob: malformed header: %q", head) } size, err := strconv.Atoi(head[46:]) if err != nil { - return "", "", fmt.Errorf("cat-blob: malformed blob size: %v", err) + return "", "", errors.Wrap(err, "cat-blob: malformed blob size") } if size != len(data) { - return "", "", fmt.Errorf("cat-blob: size header (%d) didn't match delivered size (%d)", size, len(data)) + return "", "", errors.Errorf("cat-blob: size header (%d) didn't match delivered size (%d)", size, len(data)) } return sha1, data, err @@ -70,7 +71,7 @@ func cbpLs(line string) (mode Mode, dataref string, path Path, err error) { // or // 'missing' SP LF if line[len(line)-1] != '\n' { - return 0, "", "", fmt.Errorf("ls: missing trailing newline") + return 0, "", "", errors.New("ls: missing trailing newline") } line = line[:len(line)-1] @@ -80,11 +81,11 @@ func cbpLs(line string) (mode Mode, dataref string, path Path, err error) { } else { fields := strings.SplitN(line, " ", 3) if len(fields) < 3 { - return 0, "", "", fmt.Errorf("ls: malformed line: %q", line) + return 0, "", "", errors.Errorf("ls: malformed line: %q", line) } ht := strings.IndexByte(fields[2], '\t') if ht < 0 { - return 0, "", "", fmt.Errorf("ls: malformed line: %q", line) + return 0, "", "", errors.Errorf("ls: malformed line: %q", line) } strMode := fields[0] //strType := fields[1] diff --git a/parse_fastimport.go b/parse_fastimport.go index 80488de..14080b6 100644 --- a/parse_fastimport.go +++ b/parse_fastimport.go @@ -1,10 +1,10 @@ package libfastimport import ( - "fmt" "strings" "git.lukeshu.com/go/libfastimport/textproto" + "github.com/pkg/errors" ) var parser_regularCmds = make(map[string]Cmd) @@ -17,7 +17,7 @@ func parser_registerCmd(prefix string, cmd Cmd) { case cmdClassComment: parser_commentCmds[prefix] = cmd default: - panic(fmt.Errorf("invalid cmdClass: %d", cmd.fiCmdClass())) + panic(errors.Errorf("invalid cmdClass: %d", cmd.fiCmdClass())) } } @@ -121,12 +121,12 @@ func (p *parser) parse() error { _, p.inCommit = cmd.(CmdCommit) case cmdClassCommit: if !p.inCommit { - return fmt.Errorf("Got in-commit-only command outside of a commit: %[1]T(%#[1]v)", cmd) + return errors.Errorf("Got in-commit-only command outside of a commit: %[1]T(%#[1]v)", cmd) } case cmdClassComment: /* do nothing */ default: - panic(fmt.Errorf("invalid cmdClass: %d", cmd.fiCmdClass())) + panic(errors.Errorf("invalid cmdClass: %d", cmd.fiCmdClass())) } p.ret_cmd <- cmd diff --git a/types.go b/types.go index e30ad28..8bb3450 100644 --- a/types.go +++ b/types.go @@ -5,6 +5,8 @@ import ( "strconv" "strings" "time" + + "github.com/pkg/errors" ) // BUG(lukeshu): Only supports the "raw" date format (not "rfc2822" or @@ -35,27 +37,27 @@ 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) + return ret, errors.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) + return ret, errors.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) + return ret, errors.Errorf("Missing > in ident string: %v", str) } if str[gt+1] != ' ' { - return ret, fmt.Errorf("Missing space after > in ident string: %v", str) + return ret, errors.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) + return ret, errors.Errorf("missing time zone in when: %v", str) } sec, err := strconv.ParseInt(strWhen[:sp], 10, 64) if err != nil { diff --git a/util.go b/util.go index dbb7d70..39bf2bd 100644 --- a/util.go +++ b/util.go @@ -1,9 +1,10 @@ package libfastimport import ( - "fmt" "strconv" "strings" + + "github.com/pkg/errors" ) func trimLinePrefix(line string, prefix string) string { @@ -19,19 +20,19 @@ func trimLinePrefix(line string, prefix string) string { func parse_data(line string) (data string, err error) { nl := strings.IndexByte(line, '\n') if nl < 0 { - return "", fmt.Errorf("data: expected newline: %v", data) + return "", errors.Errorf("data: expected newline: %v", data) } head := line[:nl+1] rest := line[nl+1:] if !strings.HasPrefix(head, "data ") { - return "", fmt.Errorf("data: could not parse: %v", data) + return "", errors.Errorf("data: could not parse: %v", data) } if strings.HasPrefix(head, "data <<") { // Delimited format delim := trimLinePrefix(head, "data <<") suffix := "\n" + delim + "\n" if !strings.HasSuffix(rest, suffix) { - return "", fmt.Errorf("data: did not find suffix: %v", suffix) + return "", errors.Errorf("data: did not find suffix: %v", suffix) } data = strings.TrimSuffix(rest, suffix) } else { -- cgit v1.1-4-g5e80