1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-17 00:18:05 +02:00

Switch git-todo-parser from fsmiamoto original repo to stefanhaller's fork

Sometimes it takes a while to get PRs accepted upstream, and this blocks our
progress. Since I'm pretty much the only one making changes there anyway, it
makes sense to point to my fork directly.
This commit is contained in:
Stefan Haller
2024-04-06 14:45:49 +02:00
parent 69153acfdb
commit 7270dea48d
20 changed files with 20 additions and 21 deletions

View File

@ -0,0 +1,7 @@
Copyright © 2023 Flavio Miamoto
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,158 @@
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")
ErrMissingRef = errors.New("missing ref")
)
func Parse(f io.Reader, commentChar byte) ([]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, commentChar)
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, commentChar byte) (Todo, error) {
var todo Todo
if line[0] == commentChar {
todo.Command = Comment
todo.Comment = line[1:]
return todo, nil
}
fields := strings.Fields(line)
var commandLen int
for i := Pick; i < Comment; i++ {
if isCommand(i, fields[0]) {
todo.Command = i
commandLen = len(fields[0])
fields = fields[1:]
break
}
}
if todo.Command == 0 {
// unexpected command
return todo, ErrUnexpectedCommand
}
if todo.Command == Break || todo.Command == NoOp {
return todo, nil
}
if todo.Command == Label || todo.Command == Reset {
restOfLine := strings.TrimSpace(line[commandLen:])
if todo.Command == Reset && restOfLine == "[new root]" {
todo.Label = restOfLine
} else if len(fields) == 0 {
return todo, ErrMissingLabel
} else {
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" {
todo.Flag = fields[0]
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" {
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
}
todo.Commit = fields[0]
fields = fields[1:]
// Trim comment char and whitespace
todo.Msg = strings.TrimPrefix(strings.Join(fields, " "), fmt.Sprintf("%c ", 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)
}

View File

@ -0,0 +1,78 @@
package todo
type TodoCommand int
const (
Pick TodoCommand = iota + 1
Revert
Edit
Reword
Fixup
Squash
Exec
Break
Label
Reset
Merge
NoOp
Drop
UpdateRef
Comment
)
type Todo struct {
Command TodoCommand
Commit string
Flag string
Comment string
ExecCommand string
Label string
Msg string
Ref 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",
UpdateRef: "update-ref",
Comment: "comment",
}
var todoCommandInfo = [15]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"},
{"u", "update-ref"},
}

View File

@ -0,0 +1,91 @@
package todo
import (
"io"
"strings"
)
func Write(f io.Writer, todos []Todo, commentChar byte) error {
for _, todo := range todos {
if err := writeTodo(f, todo, commentChar); err != nil {
return err
}
}
return nil
}
func writeTodo(f io.Writer, todo Todo, commentChar byte) error {
var sb strings.Builder
if todo.Command != Comment {
sb.WriteString(todo.Command.String())
}
switch todo.Command {
case NoOp:
case Comment:
sb.WriteByte(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
}