1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-12-03 22:59:46 +02:00

Bump github.com/fsmiamoto/git-todo-parser to latest main version

This commit is contained in:
Stefan Haller
2023-04-04 21:29:19 +02:00
parent 981ba4c66c
commit 62c5c32fbb
7 changed files with 136 additions and 21 deletions

View File

@@ -13,6 +13,7 @@ var (
ErrMissingLabel = errors.New("missing label")
ErrMissingCommit = errors.New("missing commit")
ErrMissingExecCmd = errors.New("missing command for exec")
ErrMissingRef = errors.New("missing ref")
)
func Parse(f io.Reader) ([]Todo, error) {
@@ -55,9 +56,9 @@ func parseLine(line string) (Todo, error) {
fields := strings.Fields(line)
for i := TodoCommand(Pick); i < Comment; i++ {
for i := Pick; i < Comment; i++ {
if isCommand(i, fields[0]) {
todo.Command = TodoCommand(i)
todo.Command = i
fields = fields[1:]
break
}
@@ -90,6 +91,7 @@ func parseLine(line string) (Todo, error) {
if todo.Command == Merge {
if fields[0] == "-C" || fields[0] == "-c" {
todo.Flag = fields[0]
fields = fields[1:]
if len(fields) == 0 {
return todo, ErrMissingCommit
@@ -115,10 +117,19 @@ func parseLine(line string) (Todo, error) {
}
// Skip flags
if fields[0] == "-C" || fields[0] == "-c" {
todo.Flag = fields[0]
fields = fields[1:]
}
}
if todo.Command == UpdateRef {
if len(fields) == 0 {
return todo, ErrMissingRef
}
todo.Ref = fields[0]
return todo, nil
}
if len(fields) == 0 {
return todo, ErrMissingCommit
}

View File

@@ -18,6 +18,7 @@ const (
NoOp
Drop
UpdateRef
Comment
)
@@ -27,10 +28,12 @@ const CommentChar = "#"
type Todo struct {
Command TodoCommand
Commit string
Flag string
Comment string
ExecCommand string
Label string
Msg string
Ref string
}
func (t TodoCommand) String() string {
@@ -38,23 +41,24 @@ func (t TodoCommand) String() string {
}
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",
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",
UpdateRef: "update-ref",
Comment: "comment",
}
var todoCommandInfo = [14]struct {
var todoCommandInfo = [15]struct {
nickname string
cmd string
}{
@@ -72,4 +76,5 @@ var todoCommandInfo = [14]struct {
{"m", "merge"},
{"", "noop"},
{"d", "drop"},
{"u", "update-ref"},
}

View File

@@ -0,0 +1,92 @@
package todo
import (
"io"
"strings"
)
func Write(f io.Writer, todos []Todo) error {
for _, todo := range todos {
if err := writeTodo(f, todo); err != nil {
return err
}
}
return nil
}
func writeTodo(f io.Writer, todo Todo) error {
var sb strings.Builder
if todo.Command != Comment {
sb.WriteString(todo.Command.String())
}
switch todo.Command {
case NoOp:
return nil
case Comment:
sb.WriteString(CommentChar)
sb.WriteString(todo.Comment)
case Break:
case Label:
fallthrough
case Reset:
sb.WriteByte(' ')
sb.WriteString(todo.Label)
case Exec:
sb.WriteByte(' ')
sb.WriteString(todo.ExecCommand)
case Merge:
sb.WriteByte(' ')
if todo.Commit != "" {
sb.WriteString(todo.Flag)
sb.WriteByte(' ')
sb.WriteString(todo.Commit)
sb.WriteByte(' ')
}
sb.WriteString(todo.Label)
if todo.Msg != "" {
sb.WriteString(" # ")
sb.WriteString(todo.Msg)
}
case Fixup:
sb.WriteByte(' ')
if todo.Flag != "" {
sb.WriteString(todo.Flag)
sb.WriteByte(' ')
}
sb.WriteString(todo.Commit)
case UpdateRef:
sb.WriteByte(' ')
sb.WriteString(todo.Ref)
case Pick:
fallthrough
case Revert:
fallthrough
case Edit:
fallthrough
case Reword:
fallthrough
case Squash:
fallthrough
case Drop:
sb.WriteByte(' ')
sb.WriteString(todo.Commit)
if todo.Msg != "" {
sb.WriteByte(' ')
sb.WriteString(todo.Msg)
}
}
sb.WriteByte('\n')
_, err := f.Write([]byte(sb.String()))
return err
}