summaryrefslogtreecommitdiffstats
path: root/vendor
diff options
context:
space:
mode:
authorFrancisco Miamoto <francisco@dorayaki.co>2022-05-22 13:42:45 -0300
committerFrancisco Miamoto <francisco@dorayaki.co>2022-06-12 20:01:32 -0300
commit0b08a0b298d515e34b3e09c7990110dbc4a324d3 (patch)
tree8f9d38f77229eae8e7934466526831a2ca5f4776 /vendor
parent2bccbee32b285fcdec39622ec4facdf4fdb0fc02 (diff)
build: add github.com/fsmiamoto/git-todo-parser
Diffstat (limited to 'vendor')
-rw-r--r--vendor/github.com/fsmiamoto/git-todo-parser/todo/parse.go141
-rw-r--r--vendor/github.com/fsmiamoto/git-todo-parser/todo/todo.go75
-rw-r--r--vendor/modules.txt3
3 files changed, 219 insertions, 0 deletions
diff --git a/vendor/github.com/fsmiamoto/git-todo-parser/todo/parse.go b/vendor/github.com/fsmiamoto/git-todo-parser/todo/parse.go
new file mode 100644
index 000000000..8203d3151
--- /dev/null
+++ b/vendor/github.com/fsmiamoto/git-todo-parser/todo/parse.go
@@ -0,0 +1,141 @@
+package todo
+
+import (
+ "bufio"
+ "errors"
+ "fmt"
+ "io"
+ "strings"
+)
+
+var (
+ ErrUnexpectedCommand = errors.New("unexpected command")
+ ErrMissingLabel = errors.New("missing label")
+ ErrMissingCommit = errors.New("missing commit")
+ ErrMissingExecCmd = errors.New("missing command for exec")
+)
+
+func Parse(f io.Reader) ([]Todo, error) {
+ var result []Todo
+
+ scanner := bufio.NewScanner(f)
+ scanner.Split(bufio.ScanLines)
+
+ for scanner.Scan() {
+ line := scanner.Text()
+
+ trimmed := strings.TrimSpace(line)
+ if trimmed == "" {
+ continue
+ }
+
+ cmd, err := parseLine(line)
+ if err != nil {
+ return nil, fmt.Errorf("failed to parse line %q: %w", line, err)
+ }
+
+ result = append(result, cmd)
+ }
+
+ if err := scanner.Err(); err != nil {
+ return nil, fmt.Errorf("failed to parse input: %w", err)
+ }
+
+ return result, nil
+}
+
+func parseLine(line string) (Todo, error) {
+ var todo Todo
+
+ if strings.HasPrefix(line, CommentChar) {
+ todo.Command = Comment
+ todo.Comment = strings.TrimLeft(line, CommentChar)
+ return todo, nil
+ }
+
+ fields := strings.Fields(line)
+
+ for i := TodoCommand(Pick); i < Comment; i++ {
+ if isCommand(i, fields[0]) {
+ todo.Command = TodoCommand(i)
+ fields = fields[1:]
+ break
+ }
+ }
+
+ if todo.Command == 0 {
+ // unexpected command
+ return todo, ErrUnexpectedCommand
+ }
+
+ if todo.Command == Break {
+ return todo, nil
+ }
+
+ if todo.Command == Label || todo.Command == Reset {
+ if len(fields) == 0 {
+ return todo, ErrMissingLabel
+ }
+ todo.Label = fields[0]
+ return todo, nil
+ }
+
+ if todo.Command == Exec {
+ if len(fields) == 0 {
+ return todo, ErrMissingExecCmd
+ }
+ todo.ExecCommand = strings.Join(fields, " ")
+ return todo, nil
+ }
+
+ if todo.Command == Merge {
+ if fields[0] == "-C" || fields[0] == "-c" {
+ fields = fields[1:]
+ if len(fields) == 0 {
+ return todo, ErrMissingCommit
+ }
+ todo.Commit = fields[0]
+ fields = fields[1:]
+ }
+ if len(fields) == 0 {
+ return todo, ErrMissingLabel
+ }
+ todo.Label = fields[0]
+ fields = fields[1:]
+ if fields[0] == "#" {
+ fields = fields[1:]
+ todo.Msg = strings.Join(fields, " ")
+ }
+ return todo, nil
+ }
+
+ if todo.Command == Fixup {
+ if len(fields) == 0 {
+ return todo, ErrMissingCommit
+ }
+ // Skip flags
+ if fields[0] == "-C" || fields[0] == "-c" {
+ fields = fields[1:]
+ }
+ }
+
+ if len(fields) == 0 {
+ return todo, ErrMissingCommit
+ }
+
+ todo.Commit = fields[0]
+ fields = fields[1:]
+
+ // Trim # and whitespace
+ todo.Msg = strings.TrimPrefix(strings.Join(fields, " "), CommentChar+" ")
+
+ return todo, nil
+}
+
+func isCommand(i TodoCommand, s string) bool {
+ if i < 0 || i > Comment {
+ return false
+ }
+ return len(s) > 0 &&
+ (todoCommandInfo[i].cmd == s || todoCommandInfo[i].nickname == s)
+}
diff --git a/vendor/github.com/fsmiamoto/git-todo-parser/todo/todo.go b/vendor/github.com/fsmiamoto/git-todo-parser/todo/todo.go
new file mode 100644
index 000000000..ce16652db
--- /dev/null
+++ b/vendor/github.com/fsmiamoto/git-todo-parser/todo/todo.go
@@ -0,0 +1,75 @@
+package todo
+
+type TodoCommand int
+
+const (
+ Pick TodoCommand = iota + 1
+ Revert
+ Edit
+ Reword
+ Fixup
+ Squash
+
+ Exec
+ Break
+ Label
+ Reset
+ Merge
+
+ NoOp
+ Drop
+
+ Comment
+)
+
+const CommentChar = "#"
+
+type Todo struct {
+ Command TodoCommand
+ Commit string
+ Comment string
+ ExecCommand string
+ Label string
+ Msg string
+}
+
+func (t TodoCommand) String() string {
+ return commandToString[t]
+}
+
+var commandToString = map[TodoCommand]string{
+ Pick: "pick",
+ Revert: "revert",
+ Edit: "edit",
+ Reword: "reword",
+ Fixup: "fixup",
+ Squash: "squash",
+ Exec: "exec",
+ Break: "break",
+ Label: "label",
+ Reset: "reset",
+ Merge: "merge",
+ NoOp: "noop",
+ Drop: "drop",
+ Comment: "comment",
+}
+
+var todoCommandInfo = [14]struct {
+ nickname string
+ cmd string
+}{
+ {"", ""}, // dummy value since we're using 1-based indexing
+ {"p", "pick"},
+ {"", "revert"},
+ {"e", "edit"},
+ {"r", "reword"},
+ {"f", "fixup"},
+ {"s", "squash"},
+ {"x", "exec"},
+ {"b", "break"},
+ {"l", "label"},
+ {"t", "reset"},
+ {"m", "merge"},
+ {"", "noop"},
+ {"d", "drop"},
+}
diff --git a/vendor/modules.txt b/vendor/modules.txt
index 4079924d6..14915d4da 100644
--- a/vendor/modules.txt
+++ b/vendor/modules.txt
@@ -30,6 +30,9 @@ github.com/emirpasic/gods/utils
# github.com/fatih/color v1.9.0
## explicit; go 1.13
github.com/fatih/color
+# github.com/fsmiamoto/git-todo-parser v0.0.2
+## explicit; go 1.13
+github.com/fsmiamoto/git-todo-parser/todo
# github.com/fsnotify/fsnotify v1.4.7
## explicit
github.com/fsnotify/fsnotify